/************************************************************************************ * Copyright (C) 2008 by Politehnica University of Bucharest and Rutgers University * All rights reserved. * Refer to LICENSE for terms and conditions of use. ***********************************************************************************/ //Petroaca - Physical layer module package vnsim.network.dsrc; import java.util.List; import java.util.List; import vnsim.core.*; import vnsim.core.events.*; import vnsim.map.object.*; import vnsim.map.utils.*; import vnsim.network.propagation.*; public class WirelessPhy { private int stateMachine; private NoiseMonitor noiseMonitor; // the duration of a frame receive ; this time is taken from the crtTime in Engine.java private int currentTime; //the Signal to Interference/Noise Ratio of the current frame private double SINR; //if the ratio of the strongest sgignal to teh sum of the others is less tahn this value then they collide private double cpThreshold=Globals.CP_THRESHOLD; //if a frame has a receievd power below this value then it is too weak to be received. private double csThreshold; //if a frame has a received power below this value then it can be received by phy but it is corrupted private double rxThreshold; private double txPower=Globals.TRANSMITTER_POWER; private int propagationType=Engine.propagationModel; private double totalPower=0; private double maxPower=0; private double SINRThreshold=Globals.SINRThresholdBPSK; private int totalReceived; public WirelessPhy(int currentTime) { stateMachine=Globals.IDLE; noiseMonitor=new NoiseMonitor(fromDBmToW(Globals.ThermalNoise)); this.SINRThreshold=Globals.SINRThresholdBPSK; this.csThreshold=Globals.CS_THRESHOLD_S; this.rxThreshold=Globals.RX_THRESHOLD_S; this.currentTime=currentTime; this.totalReceived=0; } public ReceiveEvent recvFromChannel(List receivedSignals,int currentTime) { //DsrcFrame frame=new DsrcFrame(); int noSignals=0; //check stateMachine if(this.stateMachine==Globals.IDLE) { ReceiveEvent best=null; for(ReceiveEvent re : receivedSignals) { double receivedPower=0; Road r1 = (Road) Globals.map.roads.get(re.sender.getRoadIdx()); Point p1 = (Point) r1.points.get(re.sender.getPointIdx()); Road r2 = (Road) Globals.map.roads.get(re.receiver.getRoadIdx()); Point p2 = (Point) r2.points.get(re.receiver.getPointIdx()); double R = GPSutil.distance(p1, p2); R*=1000; switch(this.propagationType) { case Globals.SHADOWING :receivedPower=Shadowing.getPr(fromDBmToW(this.txPower),R,true); break; case Globals.TWO_RAY_GROUND: receivedPower=TwoRay.getPr(fromDBmToW(this.txPower),R); break; case Globals.RICEAN: receivedPower=Engine.riceanModule.getPr(fromDBmToW(this.txPower),R,currentTime); break; } if(receivedPower>this.csThreshold) { this.totalPower+=receivedPower; noSignals++; } if(receivedPower>this.maxPower) { maxPower=receivedPower; best=re; } if((re.getMessage())[0]==Globals.PROT_EMERGENCY) Globals.EMERGENCY_SENT++; } Globals.DSRC_PACKETS_TOTAL+=receivedSignals.size(); this.totalReceived+=noSignals; Globals.DSRC_PACKETS_LOST_WEAK+=(receivedSignals.size()-noSignals); if(best==null) return null; if(maxPowerthis.rxThreshold) { //frame received ok this.stateMachine=Globals.RXing; // send frame to MAC Globals.DSRC_PACKETS_LOST_RX+=noSignals-1; return best; } else { if(!this.checkCollision(maxPower)) { //collision Globals.DSRC_PACKETS_LOST_COLLISION+=noSignals; this.noiseMonitor.addInterference(maxPower); return null; } //tag frame as corrupted and send to mac Globals.DSRC_PACKETS_LOST_CORRUPTED+=noSignals-1; best.setSender(null); return best; } } } //in transmit mode Globals.DSRC_PACKETS_LOST_TX+=receivedSignals.size(); return null; } public byte[] sendToChannel(byte[] message,int currentTime) { //if(this.stateMachine==Globals.IDLE) //{ //PCLP frame formation - make a packet out of the received frame //byte[] phyHeader=new byte[3]; //byte[] preamble=new byte[12]; //DsrcPacket packet=new DsrcPacket(preamble,phyHeader,new DsrcFrame()); //DSSS modulation this.stateMachine=Globals.TXing; return message; //} //return null; } // if the layer was transmitting or receiving a frame then after the duration of a frame it must return back to idle public void refreshFrameTime(int currentTime) { int ret; if(currentTime-this.currentTime>=Globals.WIRELESS_TRANSMISSION_FRAMES) { this.stateMachine=Globals.IDLE; this.noiseMonitor.refreshInterference(); this.totalPower=0; this.maxPower=0; if(currentTime%4==0) this.totalReceived=0; } this.currentTime=currentTime; } public NoiseMonitor getNoiseMonitor() { return this.noiseMonitor; } public DsrcPacket recvFromMac(DsrcFrame frame) { return null; } // calculates the SINR for the frame received public void setSINR(double receivedPower) { //this.SINR=10 * Math.log10(receivedPower/this.noiseMonitor.getInterference()); this.SINR=receivedPower/this.noiseMonitor.getInterference(); } public double getSINR() { return this.SINR; } //checks the ratio of the strongest signal to the rest to see if there is a collision public boolean checkCollision(double receivedPower) { if(receivedPower/(this.noiseMonitor.getInterference()-this.noiseMonitor.getThermalNoise())>this.cpThreshold) return true; return false; } // checks if the IAndN level is above the SINRThreshold public boolean checkInterferenceAndNoiseLevel() { if(this.noiseMonitor.getInterference()>=this.SINRThreshold) return false; return true; } public double fromDBmToW(double dbm) { return Math.pow(10, (dbm - 30) / 10); } public int getPropagation() { return this.propagationType; } public void setPropagation(int set) { this.propagationType=set; } public int getTotalReceivedPackets() { return this.totalReceived; } }