00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 #ifndef NS_SMAC
00049 #define NS_SMAC
00050
00051 #include "mac.h"
00052 #include "mac-802_11.h"
00053 #include "cmu-trace.h"
00054 #include "random.h"
00055 #include "timer-handler.h"
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069 #ifndef SMAC_MAX_NUM_NEIGHBORS
00070 #define SMAC_MAX_NUM_NEIGHBORS 20
00071 #endif
00072
00073 #ifndef SMAC_MAX_NUM_SCHEDULES
00074 #define SMAC_MAX_NUM_SCHEDULES 4
00075 #endif
00076
00077 #ifndef SMAC_DUTY_CYCLE
00078 #define SMAC_DUTY_CYCLE 10
00079 #endif
00080
00081 #ifndef SMAC_RETRY_LIMIT
00082 #define SMAC_RETRY_LIMIT 5
00083 #endif
00084
00085 #ifndef SMAC_EXTEND_LIMIT
00086 #define SMAC_EXTEND_LIMIT 5
00087 #endif
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098 #define SYNC_CW 31
00099 #define DATA_CW 63
00100 #define SYNCPERIOD 10
00101 #define SYNCPKTTIME 3 // an adhoc value used for now later shld converge with durSyncPkt_
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115 #define CLOCKRES 1 // clock resolution is 1ms
00116 #define BANDWIDTH 20 // kbps =>CHANGE BYTE_TX_TIME WHENEVER BANDWIDTH CHANGES
00117
00118 #define PRE_PKT_BYTES 5
00119 #define ENCODE_RATIO 2
00120 #define PROC_DELAY 1
00121
00122
00123
00124
00125
00126 #define CLKTICK2SEC(x) ((x) * (CLOCKRES / 1.0e3))
00127 #define SEC2CLKTICK(x) ((x) / (CLOCKRES / 1.0e3))
00128
00129
00130
00131 #define SLEEP 0 // radio is turned off, can't Tx or Rx
00132 #define IDLE 1 // radio in Rx mode, and can start Tx
00133
00134 #define CR_SENSE 2 // medium is free, do it before initiate a Tx
00135
00136 #define WAIT_CTS 3 // sent RTS, waiting for CTS
00137 #define WAIT_DATA 4 // sent CTS, waiting for DATA
00138 #define WAIT_ACK 5 // sent DATA, waiting for ACK
00139 #define WAIT_NEXTFRAG 6 // send one fragment, waiting for next from upper layer
00140
00141
00142
00143 #define BCASTSYNC 0
00144 #define BCASTDATA 1
00145 #define UNICAST 2
00146
00147
00148 #define DATA_PKT 0
00149 #define RTS_PKT 1
00150 #define CTS_PKT 2
00151 #define ACK_PKT 3
00152 #define SYNC_PKT 4
00153
00154
00155
00156 #define RADIO_SLP 0 // radio off
00157 #define RADIO_IDLE 1 // radio idle
00158 #define RADIO_RX 2 // recv'ing mode
00159 #define RADIO_TX 3 // transmitting mode
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201 #define SIZEOF_SMAC_DATAPKT 50 // hdr(10) + payload - fixed size pkts
00202 #define SIZEOF_SMAC_CTRLPKT 10
00203 #define SIZEOF_SMAC_SYNCPKT 9
00204
00205
00206
00207
00208 struct smac_sync_frame {
00209 int type;
00210 int length;
00211 int srcAddr;
00212
00213 int syncNode;
00214 double sleepTime;
00215 int crc;
00216 };
00217
00218
00219 struct smac_control_frame {
00220 int type;
00221 int length;
00222 int dstAddr;
00223 int srcAddr;
00224 double duration;
00225 int crc;
00226 };
00227
00228
00229 struct hdr_smac {
00230 int type;
00231 int length;
00232 int dstAddr;
00233 int srcAddr;
00234 double duration;
00235
00236 int crc;
00237 };
00238
00239
00240 struct SchedTable {
00241 int txSync;
00242 int txData;
00243 int numPeriods;
00244 };
00245
00246 struct NeighbList {
00247 int nodeId;
00248 int schedId;
00249 };
00250
00251 class SMAC;
00252
00253
00254 class SmacTimer : public TimerHandler {
00255 public:
00256 SmacTimer(SMAC *a) : TimerHandler() {a_ = a; }
00257 virtual void expire(Event *e) = 0 ;
00258 int busy() ;
00259 protected:
00260 SMAC *a_;
00261 };
00262
00263
00264 class SmacGeneTimer : public SmacTimer {
00265 public:
00266 SmacGeneTimer(SMAC *a) : SmacTimer(a) {}
00267 void expire(Event *e);
00268 };
00269
00270
00271 class SmacRecvTimer : public SmacTimer {
00272 public:
00273 SmacRecvTimer(SMAC *a) : SmacTimer(a) { stime_ = rtime_ = 0; }
00274 void sched(double duration);
00275 void expire(Event *e);
00276 double timeToExpire();
00277 protected:
00278 double stime_;
00279 double rtime_;
00280 };
00281
00282
00283 class SmacSendTimer : public SmacTimer {
00284 public:
00285 SmacSendTimer(SMAC *a) : SmacTimer(a) {}
00286 void expire(Event *e);
00287 };
00288
00289
00290 class SmacNavTimer : public SmacTimer {
00291 public:
00292 SmacNavTimer(SMAC *a) : SmacTimer(a) {}
00293 void expire(Event *e);
00294 };
00295
00296
00297
00298 class SmacNeighNavTimer : public SmacTimer {
00299 public:
00300 SmacNeighNavTimer(SMAC *a) : SmacTimer(a) { stime_ = rtime_ = 0; }
00301 void sched(double duration);
00302 void expire(Event *e);
00303 double timeToExpire();
00304 protected:
00305 double stime_;
00306 double rtime_;
00307 };
00308
00309
00310 class SmacCsTimer : public SmacTimer {
00311 public:
00312 SmacCsTimer(SMAC *a) : SmacTimer(a) {}
00313 void expire(Event *e);
00314 void checkToCancel();
00315 };
00316
00317
00318 class SmacCounterTimer : public SmacTimer {
00319 public:
00320 friend class SMAC;
00321 SmacCounterTimer(SMAC *a, int i) : SmacTimer(a) {index_ = i;}
00322 void sched(double t);
00323 void expire(Event *e);
00324 double timeToSleep();
00325 protected:
00326 int index_;
00327 int value_;
00328 int syncTime_;
00329 int dataTime_;
00330 int listenTime_;
00331 int sleepTime_;
00332 int cycleTime_;
00333 double tts_;
00334 double stime_;
00335 };
00336
00337
00338
00339 class SMAC : public Mac {
00340
00341 friend class SmacGeneTimer;
00342 friend class SmacRecvTimer;
00343 friend class SmacSendTimer;
00344 friend class SmacNavTimer;
00345 friend class SmacNeighNavTimer;
00346 friend class SmacCsTimer;
00347 friend class SmacCounterTimer;
00348
00349 public:
00350 SMAC(void);
00351 ~SMAC() {
00352 for (int i=0; i< SMAC_MAX_NUM_SCHEDULES; i++) {
00353 delete mhCounter_[i];
00354 }
00355 }
00356 void recv(Packet *p, Handler *h);
00357
00358 protected:
00359
00360
00361 void handleGeneTimer();
00362 void handleRecvTimer();
00363 void handleSendTimer();
00364 void handleNavTimer();
00365 void handleNeighNavTimer();
00366 void handleCsTimer();
00367
00368 void handleCounterTimer(int i);
00369
00370
00371 double slotTime_;
00372 double slotTime_sec_;
00373 double difs_;
00374 double sifs_;
00375 double eifs_;
00376 double guardTime_;
00377 double byte_tx_time_;
00378
00379 private:
00380
00381 void setMySched(Packet *syncpkt);
00382 void sleep();
00383 void wakeup();
00384
00385
00386
00387 void rxMsgDone(Packet* p);
00388
00389
00390 void handleRTS(Packet *p);
00391 void handleCTS(Packet *p);
00392 void handleDATA(Packet *p);
00393 void handleACK(Packet *p);
00394 void handleSYNC(Packet *p);
00395
00396
00397
00398
00399
00400 int checkToSend();
00401
00402 bool chkRadio();
00403 void transmit(Packet *p);
00404
00405 bool sendMsg(Packet *p, Handler *h);
00406 bool bcastMsg(Packet *p);
00407 bool unicastMsg(int n, Packet *p);
00408
00409
00410 void txMsgDone();
00411
00412
00413 int startBcast();
00414 int startUcast();
00415
00416 bool sendRTS();
00417 bool sendCTS(double duration);
00418 bool sendDATA();
00419 bool sendACK(double duration);
00420 bool sendSYNC();
00421
00422 void sentRTS(Packet *p);
00423 void sentCTS(Packet *p);
00424 void sentDATA(Packet *p);
00425 void sentACK(Packet *p);
00426 void sentSYNC(Packet *p);
00427
00428
00429 void collision(Packet *p);
00430 void capture(Packet *p);
00431 double txtime(Packet *p);
00432
00433 void updateNav(double duration);
00434 void updateNeighNav(double duration);
00435
00436 void mac_log(Packet *p) {
00437 logtarget_->recv(p, (Handler*) 0);
00438 }
00439
00440 void discard(Packet *p, const char* why);
00441 int drop_RTS(Packet *p, const char* why);
00442 int drop_CTS(Packet *p, const char* why);
00443 int drop_DATA(Packet *p, const char* why);
00444 int drop_SYNC(Packet *p, const char* why);
00445
00446
00447 inline int hdr_dst(char* hdr, int dst = -2) {
00448 struct hdr_smac *sh = (struct hdr_smac *) hdr;
00449 if (dst > -2)
00450 sh->dstAddr = dst;
00451 return sh->dstAddr;
00452 }
00453 inline int hdr_src(char* hdr, int src = -2) {
00454 struct hdr_smac *sh = (struct hdr_smac *) hdr;
00455 if (src > -2)
00456 sh->srcAddr = src;
00457 return sh->srcAddr;
00458 }
00459 inline int hdr_type(char *hdr, u_int16_t type = 0) {
00460 struct hdr_smac *sh = (struct hdr_smac *) hdr;
00461 if (type)
00462 sh->type = type;
00463 return sh->type;
00464 }
00465
00466
00467
00468 NsObject* logtarget_;
00469
00470
00471 int state_;
00472 int radioState_;
00473 int tx_active_;
00474 int mac_collision_;
00475
00476 int sendAddr_;
00477 int recvAddr_;
00478
00479 double nav_;
00480 double neighNav_;
00481
00482
00483 SmacNavTimer mhNav_;
00484 SmacNeighNavTimer mhNeighNav_;
00485 SmacSendTimer mhSend_;
00486 SmacRecvTimer mhRecv_;
00487 SmacGeneTimer mhGene_;
00488 SmacCsTimer mhCS_;
00489
00490
00491
00492 SmacCounterTimer *mhCounter_[SMAC_MAX_NUM_SCHEDULES];
00493
00494
00495 int numRetry_;
00496 int numExtend_;
00497
00498
00499 int lastRxFrag_;
00500
00501 int howToSend_;
00502
00503 double durSyncPkt_;
00504 double durDataPkt_;
00505 double durCtrlPkt_;
00506 double timeWaitCtrl_;
00507
00508 struct SchedTable schedTab_[SMAC_MAX_NUM_SCHEDULES];
00509 struct NeighbList neighbList_[SMAC_MAX_NUM_NEIGHBORS];
00510
00511 int mySyncNode_;
00512
00513 int currSched_;
00514 int numSched_;
00515 int numNeighb_;
00516 int numBcast_;
00517
00518 Packet *dataPkt_;
00519 Packet *pktRx_;
00520 Packet *pktTx_;
00521
00522
00523
00524 int txData_ ;
00525
00526 int syncFlag_;
00527
00528
00529 int syncTime_;
00530 int dataTime_;
00531 int listenTime_;
00532 int sleepTime_;
00533 int cycleTime_;
00534
00535 protected:
00536 int command(int argc, const char*const* argv);
00537 virtual int initialized() {
00538 return (netif_ && uptarget_ && downtarget_);
00539 }
00540 };
00541
00542
00543 #endif //NS_SMAC