OLD | NEW |
(Empty) | |
| 1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
| 2 /* |
| 3 * Copyright (c) 2011 Universidad de Cantabria |
| 4 * |
| 5 * This program is free software; you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 2 as |
| 7 * published by the Free Software Foundation; |
| 8 * |
| 9 * This program is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with this program; if not, write to the Free Software |
| 16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 * |
| 18 * Author: David Gómez Fernández <dgomez@tlmat.unican.es> |
| 19 * Ramón Agüero Calvo <ramon@tlmat.unican.es> |
| 20 */ |
| 21 |
| 22 #ifndef AR_MODEL_H_ |
| 23 #define AR_MODEL_H_ |
| 24 |
| 25 #include <string> |
| 26 #include <vector> |
| 27 #include <map> |
| 28 #include <unistd.h> |
| 29 |
| 30 #include "ns3/object.h" |
| 31 #include "ns3/random-variable.h" |
| 32 |
| 33 //AR model will play the role as the derived classes of both Error and Propagati
on Loss models |
| 34 #include "ns3/error-model.h" |
| 35 #include "ns3/mobility-model.h" |
| 36 #include "ns3/propagation-loss-model.h" |
| 37 |
| 38 #include "ns3/event-id.h" |
| 39 |
| 40 //Needed to parse the packet content (BurstyErrorModel::ParsePacket) |
| 41 #include "ns3/wifi-mac-header.h" |
| 42 #include "ns3/llc-snap-header.h" |
| 43 #include "ns3/ipv4-header.h" |
| 44 #include "ns3/tcp-header.h" |
| 45 #include "ns3/udp-header.h" |
| 46 |
| 47 //Tracing |
| 48 #include "ns3/traced-value.h" |
| 49 #include "ns3/traced-callback.h" |
| 50 |
| 51 using namespace std; |
| 52 namespace ns3 { |
| 53 |
| 54 class Packet; |
| 55 class BurstyErrorModel; |
| 56 typedef int32_t nsaddr_t; |
| 57 |
| 58 struct prevValues_t { |
| 59 double snr; |
| 60 Time time; |
| 61 }; |
| 62 |
| 63 typedef struct { |
| 64 double propagationSnr; |
| 65 double slowFading; |
| 66 double fastFading; |
| 67 double totalSnr; |
| 68 } snrComposition_t; |
| 69 |
| 70 class ChannelKey { |
| 71 public: |
| 72 ChannelKey() {} |
| 73 ChannelKey (nsaddr_t tx, nsaddr_t rx) : m_tx(tx), m_rx(rx) {} |
| 74 virtual ~ChannelKey () {} |
| 75 |
| 76 bool operator < (const ChannelKey& key) const { |
| 77 if(m_tx < key.m_tx) { |
| 78 return true; |
| 79 } else if(m_tx == key.m_tx) { |
| 80 return(m_rx < key.m_rx); |
| 81 } else { |
| 82 return false; |
| 83 } |
| 84 } |
| 85 |
| 86 u_int32_t GetRx () const; |
| 87 u_int32_t GetTx () const; |
| 88 void SetRx (u_int32_t rx); |
| 89 void SetTx (u_int32_t tx); |
| 90 |
| 91 private: |
| 92 int m_tx; |
| 93 int m_rx; |
| 94 }; |
| 95 |
| 96 class ArChannelEntry: public Object { |
| 97 public: |
| 98 /** |
| 99 * Constructor |
| 100 */ |
| 101 ArChannelEntry (); |
| 102 /** |
| 103 * \param order Auto Regressive filter order |
| 104 * \ |
| 105 */ |
| 106 ArChannelEntry (int order, double coherenceTime); |
| 107 |
| 108 /** |
| 109 * Destructor |
| 110 */ |
| 111 ~ArChannelEntry (); |
| 112 /** |
| 113 * When a new frame arrives, we need to update the vector that contains
the AR order previous frames information; furthermore, we must |
| 114 * update the ArChannelEntry oldest received frame timeout |
| 115 * \param snr SNR read from the physical layer |
| 116 * \param order Order of the AR filter |
| 117 */ |
| 118 void UpdateSnr (double snr); |
| 119 |
| 120 /** |
| 121 * \param previousSnr Vector which will hold the SNR values (window) |
| 122 * \returns The size of the input vector |
| 123 */ |
| 124 int GetPreviousSnr (vector<double> &previousSnr); |
| 125 |
| 126 /** |
| 127 * \param coherenceTime Channel coherence |
| 128 * \returns The instant the next timeout should be invoked |
| 129 */ |
| 130 double GetNextTimeout(); |
| 131 /** |
| 132 * \brief Handle the timeout event when the coherence timer expires |
| 133 */ |
| 134 void HandleCoherenceTimeout (); |
| 135 |
| 136 u_int32_t GetRx () const; |
| 137 u_int32_t GetTx () const; |
| 138 void SetRx (u_int32_t rx); |
| 139 void SetTx (u_int32_t tx); |
| 140 |
| 141 /** |
| 142 * \brief Print the captured packet queue (AR model window) |
| 143 */ |
| 144 void DisplaySnrQueue (void); |
| 145 |
| 146 private: |
| 147 |
| 148 u_int32_t m_tx; |
| 149 u_int32_t m_rx; |
| 150 |
| 151 //Vector that stores the SNR and the timestamp of the overheard packets
(Size set by the AR filter order) |
| 152 vector<struct prevValues_t> m_previousSnr; |
| 153 |
| 154 //One timer per ChannelEntry object |
| 155 EventId m_coherenceTimeout; |
| 156 |
| 157 int m_order; |
| 158 double m_coherenceTime; |
| 159 }; |
| 160 |
| 161 |
| 162 class ArModel: public PropagationLossModel |
| 163 { |
| 164 public: |
| 165 /** |
| 166 * Attribute handler |
| 167 */ |
| 168 static TypeId GetTypeId (void); |
| 169 /** |
| 170 * Default constructor |
| 171 */ |
| 172 ArModel (); |
| 173 |
| 174 /** |
| 175 * Default destructor |
| 176 */ |
| 177 virtual ~ArModel (); |
| 178 |
| 179 /** |
| 180 * \param name the name of the model to set |
| 181 * \param n0 the name of the attribute to set |
| 182 * \param v0 the value of the attribute to set |
| 183 * \param n1 the name of the attribute to set |
| 184 * \param v1 the value of the attribute to set |
| 185 * \param n2 the name of the attribute to set |
| 186 * \param v2 the value of the attribute to set |
| 187 * \param n3 the name of the attribute to set |
| 188 * \param v3 the value of the attribute to set |
| 189 * \param n4 the name of the attribute to set |
| 190 * \param v4 the value of the attribute to set |
| 191 * \param n5 the name of the attribute to set |
| 192 * \param v5 the value of the attribute to set |
| 193 * \param n6 the name of the attribute to set |
| 194 * \param v6 the value of the attribute to set |
| 195 * \param n7 the name of the attribute to set |
| 196 * \param v7 the value of the attribute to set |
| 197 * |
| 198 * Configure a propagation delay for this channel. |
| 199 */ |
| 200 void SetPropagationLoss (std::string name, |
| 201 std::string n0 = "", const AttributeValue &v0 = EmptyAtt
ributeValue (), |
| 202 std::string n1 = "", const AttributeValue &v1 = EmptyAtt
ributeValue (), |
| 203 std::string n2 = "", const AttributeValue &v2 = EmptyAtt
ributeValue (), |
| 204 std::string n3 = "", const AttributeValue &v3 = EmptyAtt
ributeValue (), |
| 205 std::string n4 = "", const AttributeValue &v4 = EmptyAtt
ributeValue (), |
| 206 std::string n5 = "", const AttributeValue &v5 = EmptyAtt
ributeValue (), |
| 207 std::string n6 = "", const AttributeValue &v6 = EmptyAtt
ributeValue (), |
| 208 std::string n7 = "", const AttributeValue &v7 = EmptyAtt
ributeValue ()); |
| 209 |
| 210 /** |
| 211 * \brief Set the propagation loss model |
| 212 * \param loss Pointer to the propagation loss model to set |
| 213 */ |
| 214 void SetPropagationLoss (Ptr<PropagationLossModel> loss); |
| 215 |
| 216 /** |
| 217 * \brief Set the error model |
| 218 * \param error Pointer to the error model to set |
| 219 */ |
| 220 void SetErrorModel (Ptr<BurstyErrorModel> error); |
| 221 |
| 222 /** |
| 223 * \returns The Bursty error model linked to this class |
| 224 */ |
| 225 Ptr<BurstyErrorModel> GetErrorModel (); |
| 226 /** |
| 227 * \param fileName Name of the file which holds the AR filter coefficien
t |
| 228 */ |
| 229 bool GetCoefficients (string fileName); |
| 230 |
| 231 /** |
| 232 * \param key AR coefficient set map key value |
| 233 * \param vectorPosition The position in which stays the desired coeffic
ient |
| 234 * \returns AR coefficient |
| 235 */ |
| 236 double GetArCoefficient (int key, int vectorPosition) const; |
| 237 |
| 238 /** |
| 239 * \brief |
| 240 * \param tx Transmitter index |
| 241 * \param rx Reveiver index |
| 242 * \returns Auto Regressive filter obtained value (dB) |
| 243 */ |
| 244 double GetCurrentArValue (Ptr<MobilityModel> sender, Ptr<MobilityModel>
receiver) const; |
| 245 |
| 246 private: |
| 247 |
| 248 |
| 249 //PropagationLossModel virtual (abstract) method --> Equivalent to ns-2
arModel::Pr (PacketStamp *t_, PacketStamp *r_, WirelessPhy *ifp_) |
| 250 virtual double DoCalcRxPower (double txPowerDbm, |
| 251 Ptr<MobilityModel> a, |
| 252 Ptr<MobilityModel> b) const; |
| 253 |
| 254 /* AR mode parameters */ |
| 255 int m_order; /* Order of the AR filter */ |
| 256 double m_variance; /* Input noise variance */ |
| 257 |
| 258 double m_ffVariance; /* Fast Fading variance */ |
| 259 |
| 260 double m_coherenceTime; /* Coherence time */ |
| 261 bool m_symmetry; /* Am I using symmetry? */ |
| 262 |
| 263 /* Other parameters of the channel */ |
| 264 double m_pathLossExp; /* path loss exponent */ |
| 265 double m_stdDevDb; /* standard deviation (dB) */ |
| 266 |
| 267 double m_noise; /* Noise Power used to model the
SNR */ |
| 268 |
| 269 RandomVariable m_ranvar; |
| 270 |
| 271 /* All the channels (one per tx/rx pair) */ |
| 272 typedef map<ChannelKey, Ptr <ArChannelEntry> > channelSet_t; |
| 273 typedef channelSet_t::const_iterator channelSetIter_t; |
| 274 |
| 275 channelSet_t m_channelSetMap; |
| 276 |
| 277 /* The coeficients of the AR model */ |
| 278 typedef map<int, vector<double> > coefSet_t; |
| 279 typedef coefSet_t::const_iterator coefSetIter_t; |
| 280 |
| 281 coefSet_t m_arCoefficientsMap; |
| 282 |
| 283 //Generic propagation model |
| 284 Ptr<PropagationLossModel> m_propagationLoss; |
| 285 |
| 286 //BEAR model |
| 287 Ptr<BurstyErrorModel> m_errorModel; |
| 288 |
| 289 //Coefficient file name |
| 290 string m_coefficientsFile; |
| 291 |
| 292 protected: |
| 293 /** |
| 294 * \return The current path (in string format) |
| 295 */ |
| 296 static std::string GetCwd (); |
| 297 |
| 298 }; |
| 299 |
| 300 //Error model selection |
| 301 typedef enum { |
| 302 NO_ERROR_MODEL, |
| 303 BEAR_MODEL, |
| 304 SHADOWING_MODEL |
| 305 } errorModelOption_t; |
| 306 |
| 307 //Parameter to carry out the error decision |
| 308 typedef struct { |
| 309 double a; |
| 310 double b; |
| 311 double c; |
| 312 int lowThreshold; |
| 313 int highThreshold; |
| 314 } burstyErrorModelParams_t; |
| 315 |
| 316 enum PacketType{ |
| 317 IEEE_80211_ACK, |
| 318 IEEE_80211_NODATA, |
| 319 ARP_PACKET, |
| 320 UDP_DATA, |
| 321 TCP_DATA |
| 322 }; |
| 323 |
| 324 typedef struct { |
| 325 WifiMacHeader wifiHdr; |
| 326 LlcSnapHeader llcHdr; |
| 327 Ipv4Header ipv4Hdr; |
| 328 TcpHeader tcpHdr; |
| 329 UdpHeader udpHdr; |
| 330 u_int16_t payloadLength; |
| 331 PacketType type; |
| 332 |
| 333 } packetInfo_t; |
| 334 |
| 335 class BurstyErrorModel: public ErrorModel |
| 336 { |
| 337 public: |
| 338 /** |
| 339 * arg1: packet received successfully |
| 340 * arg2: packet timestamp |
| 341 * arg3: Boolean that represents whether a packet has been succesfully r
eceived or not |
| 342 * arg4: snr (global) of packet |
| 343 * arg5: snr due to the AR model (Slow fading) |
| 344 * arg6: Fast Fading related SNR |
| 345 */ |
| 346 typedef Callback<void,Ptr<Packet>, Time, bool, double, double, double> B
earRxCallback_t; |
| 347 |
| 348 /** |
| 349 * Attribute handler |
| 350 */ |
| 351 static TypeId GetTypeId (void); |
| 352 /** |
| 353 * Default constructor |
| 354 */ |
| 355 BurstyErrorModel (); |
| 356 /** |
| 357 * Default destructor |
| 358 */ |
| 359 virtual ~BurstyErrorModel (); |
| 360 /** |
| 361 * \brief Apply a logistic function to decide whether a data frame is co
rrect or not |
| 362 * \param packet The packet received |
| 363 * \returns True if the packet is corrupted |
| 364 */ |
| 365 bool CorruptDataFrame (Ptr<Packet> packet); |
| 366 /** |
| 367 * \brief Apply a logistic function to decide whether an ack (TCP) frame
is correct or not |
| 368 * \param packet The packet received |
| 369 * \returns True if the packet is corrupted |
| 370 */ |
| 371 bool CorruptAckFrame (Ptr<Packet> packet); |
| 372 /** |
| 373 * \brief Apply a logistic function to decide whether a broadcast/contro
l/management frame is correct or not |
| 374 * \param packet The packet received |
| 375 * \returns True if the packet is corrupted |
| 376 */ |
| 377 bool CorruptBcastCtrlFrame (Ptr<Packet> packet); |
| 378 |
| 379 /** |
| 380 * \brief Use the logistic function to obtain the FER |
| 381 * \param params Struct that holds the logistic function parameters |
| 382 * \returns The FER value |
| 383 */ |
| 384 double GetBearFer (burstyErrorModelParams_t *params); |
| 385 |
| 386 /** |
| 387 * \param rxComponent Value obtained from the propagation loss model |
| 388 * \param slowFadingComponent Auto regressive component that tries to em
ulate the Slow Fading wireless channel behaviour |
| 389 * \param fastFadingComponent Random value which represent Fast Fading |
| 390 */ |
| 391 void SetUnwrappedSnr (double propagationSnr, double slowFadingComponent,
double fastFadingComponent); |
| 392 |
| 393 //Callback invoked when a packet is received by the error model object |
| 394 void SetRxCallback (BearRxCallback_t callback); |
| 395 |
| 396 /** |
| 397 * \param packet Packet received by the node |
| 398 * \returns Struct which contains the info relative to the packet |
| 399 */ |
| 400 packetInfo_t ParsePacket (Ptr<Packet> packet); |
| 401 |
| 402 private: |
| 403 //ErrorModel virtual (abstract) methods |
| 404 virtual bool DoCorrupt (Ptr<Packet>); |
| 405 virtual void DoReset (void); |
| 406 |
| 407 RandomVariable m_ranvar; |
| 408 |
| 409 //Logistic function configuration parameters |
| 410 burstyErrorModelParams_t m_dataLogParams; |
| 411 burstyErrorModelParams_t m_ackLogParams; |
| 412 burstyErrorModelParams_t m_bcastCtrlLogParams; |
| 413 |
| 414 //Choose among the different options (0- No model, 1- BEAR model, 2- Sha
dowing model) |
| 415 errorModelOption_t m_arModel; |
| 416 |
| 417 snrComposition_t m_unwrappedSnr; |
| 418 |
| 419 // Tracing |
| 420 /** |
| 421 * The trace source fired when a packet ends the reception process from |
| 422 * the medium. |
| 423 * |
| 424 * \see class CallBackTraceSource |
| 425 */ |
| 426 TracedCallback<Ptr<const Packet>, Time, bool, double, double, double> m_
rxTrace; |
| 427 BearRxCallback_t m_rxCallback; |
| 428 |
| 429 }; |
| 430 |
| 431 |
| 432 } //namespace ns3 |
| 433 #endif /* AR_MODEL_H_ */ |
OLD | NEW |