[31] | 1 | /************************************************************************************ |
---|
| 2 | * Copyright (C) 2008 by Politehnica University of Bucharest and Rutgers University |
---|
| 3 | * All rights reserved. |
---|
| 4 | * Refer to LICENSE for terms and conditions of use. |
---|
| 5 | ***********************************************************************************/ |
---|
| 6 | //Petroaca - Physical layer module |
---|
| 7 | package vnsim.network.dsrc; |
---|
| 8 | |
---|
| 9 | |
---|
| 10 | import java.util.List; |
---|
| 11 | |
---|
| 12 | import java.util.List; |
---|
| 13 | |
---|
| 14 | import vnsim.core.*; |
---|
| 15 | import vnsim.core.events.*; |
---|
| 16 | import vnsim.map.object.*; |
---|
| 17 | import vnsim.map.utils.*; |
---|
| 18 | import vnsim.network.propagation.*; |
---|
| 19 | |
---|
| 20 | |
---|
| 21 | |
---|
| 22 | |
---|
| 23 | public class WirelessPhy |
---|
| 24 | { |
---|
| 25 | private int stateMachine; |
---|
| 26 | |
---|
| 27 | private NoiseMonitor noiseMonitor; |
---|
| 28 | |
---|
| 29 | // the duration of a frame receive ; this time is taken from the crtTime in Engine.java |
---|
| 30 | private int currentTime; |
---|
| 31 | |
---|
| 32 | //the Signal to Interference/Noise Ratio of the current frame |
---|
| 33 | private double SINR; |
---|
| 34 | |
---|
| 35 | //if the ratio of the strongest sgignal to teh sum of the others is less tahn this value then they collide |
---|
| 36 | private double cpThreshold=Globals.CP_THRESHOLD; |
---|
| 37 | |
---|
| 38 | //if a frame has a receievd power below this value then it is too weak to be received. |
---|
| 39 | private double csThreshold; |
---|
| 40 | |
---|
| 41 | //if a frame has a received power below this value then it can be received by phy but it is corrupted |
---|
| 42 | private double rxThreshold; |
---|
| 43 | |
---|
| 44 | private double txPower=Globals.TRANSMITTER_POWER; |
---|
| 45 | |
---|
| 46 | private int propagationType=Engine.propagationModel; |
---|
| 47 | |
---|
| 48 | private double totalPower=0; |
---|
| 49 | |
---|
| 50 | private double maxPower=0; |
---|
| 51 | |
---|
| 52 | private double SINRThreshold=Globals.SINRThresholdBPSK; |
---|
| 53 | |
---|
| 54 | private int totalReceived; |
---|
| 55 | |
---|
| 56 | public WirelessPhy(int currentTime) |
---|
| 57 | { |
---|
| 58 | stateMachine=Globals.IDLE; |
---|
| 59 | |
---|
| 60 | noiseMonitor=new NoiseMonitor(fromDBmToW(Globals.ThermalNoise)); |
---|
| 61 | this.SINRThreshold=Globals.SINRThresholdBPSK; |
---|
| 62 | |
---|
| 63 | this.csThreshold=Globals.CS_THRESHOLD_S; |
---|
| 64 | this.rxThreshold=Globals.RX_THRESHOLD_S; |
---|
| 65 | |
---|
| 66 | this.currentTime=currentTime; |
---|
| 67 | |
---|
| 68 | this.totalReceived=0; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | public ReceiveEvent recvFromChannel(List<ReceiveEvent> receivedSignals,int currentTime) |
---|
| 72 | { |
---|
| 73 | //DsrcFrame frame=new DsrcFrame(); |
---|
| 74 | int noSignals=0; |
---|
| 75 | |
---|
| 76 | |
---|
| 77 | //check stateMachine |
---|
| 78 | if(this.stateMachine==Globals.IDLE) |
---|
| 79 | { |
---|
| 80 | |
---|
| 81 | ReceiveEvent best=null; |
---|
| 82 | |
---|
| 83 | for(ReceiveEvent re : receivedSignals) |
---|
| 84 | { |
---|
| 85 | double receivedPower=0; |
---|
| 86 | |
---|
| 87 | Road r1 = (Road) Globals.map.roads.get(re.sender.getRoadIdx()); |
---|
| 88 | Point p1 = (Point) r1.points.get(re.sender.getPointIdx()); |
---|
| 89 | Road r2 = (Road) Globals.map.roads.get(re.receiver.getRoadIdx()); |
---|
| 90 | Point p2 = (Point) r2.points.get(re.receiver.getPointIdx()); |
---|
| 91 | |
---|
| 92 | double R = GPSutil.distance(p1, p2); |
---|
| 93 | |
---|
| 94 | R*=1000; |
---|
| 95 | |
---|
| 96 | switch(this.propagationType) |
---|
| 97 | { |
---|
| 98 | case Globals.SHADOWING :receivedPower=Shadowing.getPr(fromDBmToW(this.txPower),R,true); |
---|
| 99 | break; |
---|
| 100 | case Globals.TWO_RAY_GROUND: receivedPower=TwoRay.getPr(fromDBmToW(this.txPower),R); |
---|
| 101 | break; |
---|
| 102 | case Globals.RICEAN: receivedPower=Engine.riceanModule.getPr(fromDBmToW(this.txPower),R,currentTime); |
---|
| 103 | break; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | if(receivedPower>this.csThreshold) |
---|
| 107 | { |
---|
| 108 | this.totalPower+=receivedPower; |
---|
| 109 | noSignals++; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | if(receivedPower>this.maxPower) |
---|
| 113 | { |
---|
| 114 | maxPower=receivedPower; |
---|
| 115 | best=re; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | if((re.getMessage())[0]==Globals.PROT_EMERGENCY) |
---|
| 119 | Globals.EMERGENCY_SENT++; |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | |
---|
| 123 | Globals.DSRC_PACKETS_TOTAL+=receivedSignals.size(); |
---|
| 124 | this.totalReceived+=noSignals; |
---|
| 125 | Globals.DSRC_PACKETS_LOST_WEAK+=(receivedSignals.size()-noSignals); |
---|
| 126 | |
---|
| 127 | if(best==null) |
---|
| 128 | return null; |
---|
| 129 | |
---|
| 130 | if(maxPower<this.csThreshold) |
---|
| 131 | { |
---|
| 132 | //weak signal |
---|
| 133 | this.noiseMonitor.addInterference(totalPower); |
---|
| 134 | return null; |
---|
| 135 | } |
---|
| 136 | else |
---|
| 137 | { |
---|
| 138 | this.noiseMonitor.addInterference(totalPower-maxPower); |
---|
| 139 | |
---|
| 140 | |
---|
| 141 | |
---|
| 142 | //can decode the preamble and phy header |
---|
| 143 | |
---|
| 144 | |
---|
| 145 | |
---|
| 146 | this.setSINR(maxPower); |
---|
| 147 | |
---|
| 148 | if(maxPower>this.rxThreshold) |
---|
| 149 | { |
---|
| 150 | |
---|
| 151 | //frame received ok |
---|
| 152 | |
---|
| 153 | this.stateMachine=Globals.RXing; |
---|
| 154 | // send frame to MAC |
---|
| 155 | Globals.DSRC_PACKETS_LOST_RX+=noSignals-1; |
---|
| 156 | return best; |
---|
| 157 | } |
---|
| 158 | else |
---|
| 159 | { |
---|
| 160 | |
---|
| 161 | if(!this.checkCollision(maxPower)) |
---|
| 162 | { |
---|
| 163 | //collision |
---|
| 164 | Globals.DSRC_PACKETS_LOST_COLLISION+=noSignals; |
---|
| 165 | this.noiseMonitor.addInterference(maxPower); |
---|
| 166 | return null; |
---|
| 167 | } |
---|
| 168 | //tag frame as corrupted and send to mac |
---|
| 169 | Globals.DSRC_PACKETS_LOST_CORRUPTED+=noSignals-1; |
---|
| 170 | best.setSender(null); |
---|
| 171 | return best; |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | //in transmit mode |
---|
| 179 | Globals.DSRC_PACKETS_LOST_TX+=receivedSignals.size(); |
---|
| 180 | return null; |
---|
| 181 | |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | public byte[] sendToChannel(byte[] message,int currentTime) |
---|
| 185 | { |
---|
| 186 | |
---|
| 187 | //if(this.stateMachine==Globals.IDLE) |
---|
| 188 | //{ |
---|
| 189 | //PCLP frame formation - make a packet out of the received frame |
---|
| 190 | //byte[] phyHeader=new byte[3]; |
---|
| 191 | //byte[] preamble=new byte[12]; |
---|
| 192 | //DsrcPacket packet=new DsrcPacket(preamble,phyHeader,new DsrcFrame()); |
---|
| 193 | |
---|
| 194 | //DSSS modulation |
---|
| 195 | |
---|
| 196 | this.stateMachine=Globals.TXing; |
---|
| 197 | return message; |
---|
| 198 | //} |
---|
| 199 | |
---|
| 200 | //return null; |
---|
| 201 | |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | // if the layer was transmitting or receiving a frame then after the duration of a frame it must return back to idle |
---|
| 205 | public void refreshFrameTime(int currentTime) |
---|
| 206 | { |
---|
| 207 | int ret; |
---|
| 208 | |
---|
| 209 | if(currentTime-this.currentTime>=Globals.WIRELESS_TRANSMISSION_FRAMES) |
---|
| 210 | { |
---|
| 211 | this.stateMachine=Globals.IDLE; |
---|
| 212 | this.noiseMonitor.refreshInterference(); |
---|
| 213 | this.totalPower=0; |
---|
| 214 | this.maxPower=0; |
---|
| 215 | |
---|
| 216 | if(currentTime%4==0) |
---|
| 217 | this.totalReceived=0; |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | this.currentTime=currentTime; |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | public NoiseMonitor getNoiseMonitor() |
---|
| 224 | { |
---|
| 225 | return this.noiseMonitor; |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | public DsrcPacket recvFromMac(DsrcFrame frame) |
---|
| 229 | { |
---|
| 230 | return null; |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | // calculates the SINR for the frame received |
---|
| 234 | public void setSINR(double receivedPower) |
---|
| 235 | { |
---|
| 236 | //this.SINR=10 * Math.log10(receivedPower/this.noiseMonitor.getInterference()); |
---|
| 237 | this.SINR=receivedPower/this.noiseMonitor.getInterference(); |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | public double getSINR() |
---|
| 241 | { |
---|
| 242 | return this.SINR; |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | |
---|
| 246 | //checks the ratio of the strongest signal to the rest to see if there is a collision |
---|
| 247 | public boolean checkCollision(double receivedPower) |
---|
| 248 | { |
---|
| 249 | if(receivedPower/(this.noiseMonitor.getInterference()-this.noiseMonitor.getThermalNoise())>this.cpThreshold) |
---|
| 250 | return true; |
---|
| 251 | |
---|
| 252 | return false; |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | // checks if the IAndN level is above the SINRThreshold |
---|
| 256 | public boolean checkInterferenceAndNoiseLevel() |
---|
| 257 | { |
---|
| 258 | if(this.noiseMonitor.getInterference()>=this.SINRThreshold) |
---|
| 259 | return false; |
---|
| 260 | |
---|
| 261 | return true; |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | public double fromDBmToW(double dbm) |
---|
| 265 | { |
---|
| 266 | return Math.pow(10, (dbm - 30) / 10); |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | public int getPropagation() |
---|
| 270 | { |
---|
| 271 | return this.propagationType; |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | public void setPropagation(int set) |
---|
| 275 | { |
---|
| 276 | this.propagationType=set; |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | public int getTotalReceivedPackets() |
---|
| 280 | { |
---|
| 281 | return this.totalReceived; |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | } |
---|