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
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080 #ifndef NS_SMAC
00081 #define NS_SMAC
00082
00083
00084
00085
00086
00087
00088 #include "mac.h"
00089 #include "mac-802_11.h"
00090 #include "cmu-trace.h"
00091 #include "random.h"
00092 #include "timer-handler.h"
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106 #ifndef SMAC_MAX_NUM_NEIGHBORS
00107 #define SMAC_MAX_NUM_NEIGHBORS 20
00108 #endif
00109
00110 #ifndef SMAC_MAX_NUM_SCHEDULES
00111 #define SMAC_MAX_NUM_SCHEDULES 4
00112 #endif
00113
00114 #ifndef SMAC_DUTY_CYCLE
00115 #define SMAC_DUTY_CYCLE 10
00116 #endif
00117
00118 #ifndef SMAC_RETRY_LIMIT
00119 #define SMAC_RETRY_LIMIT 5
00120 #endif
00121
00122 #ifndef SMAC_EXTEND_LIMIT
00123 #define SMAC_EXTEND_LIMIT 5
00124 #endif
00125
00126 #ifdef JOURNAL_PAPER
00127
00128 #ifndef SMAC_UPDATE_NEIGHB_PERIOD
00129 #define SMAC_UPDATE_NEIGHB_PERIOD 50
00130 #endif
00131
00132 #ifndef GUARDTIME
00133 #define GUARDTIME 0.001
00134 #endif
00135
00136 #endif
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149 #define SYNC_CW 31
00150 #define DATA_CW 63
00151 #define SYNCPERIOD 10
00152 #define SYNCPKTTIME 3 // an adhoc value used for now later shld converge with durSyncPkt_
00153
00154 #define SRCH_CYCLES_SHORT 3
00155 #define SRCH_CYCLES_LONG 22
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170 #define CLOCKRES 1 // clock resolution is 1ms
00171 #define BANDWIDTH 20 // kbps =>CHANGE BYTE_TX_TIME WHENEVER BANDWIDTH CHANGES
00172
00173 #define PRE_PKT_BYTES 5
00174 #define ENCODE_RATIO 2
00175 #define PROC_DELAY 1
00176
00177
00178
00179
00180
00181 #define CLKTICK2SEC(x) ((x) * (CLOCKRES / 1.0e3))
00182 #define SEC2CLKTICK(x) ((x) / (CLOCKRES / 1.0e3))
00183
00184
00185
00186 #define SLEEP 0 // radio is turned off, can't Tx or Rx
00187 #define IDLE 1 // radio in Rx mode, and can start Tx
00188
00189 #define CR_SENSE 2 // medium is free, do it before initiate a Tx
00190
00191 #define WAIT_CTS 3 // sent RTS, waiting for CTS
00192 #define WAIT_DATA 4 // sent CTS, waiting for DATA
00193 #define WAIT_ACK 5 // sent DATA, waiting for ACK
00194 #ifdef JOURNAL_PAPER
00195 #define TX_NEXT_FRAG 6 // send one fragment, waiting for next from upper layer
00196 #else
00197 #define WAIT_NEXTFRAG 6 // send one fragment, waiting for next from upper layer
00198 #endif
00199
00200 #ifdef JOURNAL_PAPER
00201 #define DATA_SENSE1 7 // received a RTS destined to another node, keep listening until confirm sender gets a CTS or starts tx data
00202 #define DATA_SENSE2 8 // received a RTS destined to another node,and did not receive a RTS, keep listening until timeout or receive data
00203 #define TX_PKT 9 // before sending CTS/DATA/ACK, need to wait for a sifs_ time
00204 #endif
00205
00206
00207 #define BCASTSYNC 0
00208 #define BCASTDATA 1
00209 #define UNICAST 2
00210
00211 #ifdef JOURNAL_PAPER
00212 #define UNICAST_ADDR 0
00213 #endif
00214
00215
00216 #define DATA_PKT 0
00217 #define RTS_PKT 1
00218 #define CTS_PKT 2
00219 #define ACK_PKT 3
00220 #define SYNC_PKT 4
00221
00222
00223
00224 #define RADIO_SLP 0 // radio off
00225 #define RADIO_IDLE 1 // radio idle
00226 #define RADIO_RX 2 // recv'ing mode
00227 #define RADIO_TX 3 // transmitting mode
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269 #define SIZEOF_SMAC_DATAPKT 50 // hdr(10) + payload - fixed size pkts
00270 #define SIZEOF_SMAC_CTRLPKT 10
00271 #define SIZEOF_SMAC_SYNCPKT 9
00272
00273
00274
00275
00276 struct smac_sync_frame {
00277 int type;
00278 int length;
00279 int srcAddr;
00280
00281 int syncNode;
00282 double sleepTime;
00283 #ifdef JOURNAL_PAPER
00284 int state;
00285 #endif
00286 int crc;
00287 };
00288
00289
00290 struct smac_control_frame {
00291 int type;
00292 int length;
00293 int dstAddr;
00294 int srcAddr;
00295 double duration;
00296 int crc;
00297 };
00298
00299
00300 struct hdr_smac {
00301 int type;
00302 int length;
00303 int dstAddr;
00304 int srcAddr;
00305 double duration;
00306
00307 int crc;
00308 };
00309
00310
00311 struct SchedTable {
00312 int txSync;
00313 int txData;
00314 int numPeriods;
00315 #ifdef JOURNAL_PAPER
00316 int numNodes;
00317 int syncNode;
00318 int chkSched;
00319 #endif
00320 };
00321
00322 struct NeighbList {
00323 int nodeId;
00324 int schedId;
00325 #ifdef JOURNAL_PAPER
00326 int active;
00327 int state;
00328 #endif
00329 };
00330
00331 class SMAC;
00332
00333
00334 class SmacTimer : public TimerHandler {
00335 public:
00336 SmacTimer(SMAC *a) : TimerHandler() {a_ = a; }
00337 virtual void expire(Event *e) = 0 ;
00338 int busy() ;
00339 protected:
00340 SMAC *a_;
00341 };
00342
00343 #ifdef JOURNAL_PAPER
00344
00345 class SmacUpdateNeighbTimer : public SmacTimer {
00346 public:
00347 SmacUpdateNeighbTimer(SMAC *a) : SmacTimer(a) {}
00348 void expire(Event *e);
00349 };
00350
00351
00352 class SmacAdaptiveListenTimer : public SmacTimer {
00353 public:
00354 SmacAdaptiveListenTimer(SMAC *a) : SmacTimer(a) {}
00355 void expire(Event *e);
00356 };
00357 #endif
00358
00359
00360 class SmacGeneTimer : public SmacTimer {
00361 public:
00362 SmacGeneTimer(SMAC *a) : SmacTimer(a) {}
00363 void expire(Event *e);
00364 };
00365
00366
00367 class SmacRecvTimer : public SmacTimer {
00368 public:
00369 SmacRecvTimer(SMAC *a) : SmacTimer(a) { stime_ = rtime_ = 0; }
00370 void sched(double duration);
00371 void resched(double time);
00372 void expire(Event *e);
00373 double timeToExpire();
00374 protected:
00375 double stime_;
00376 double rtime_;
00377 };
00378
00379
00380 class SmacSendTimer : public SmacTimer {
00381 public:
00382 SmacSendTimer(SMAC *a) : SmacTimer(a) {}
00383 void expire(Event *e);
00384 };
00385
00386
00387 class SmacNavTimer : public SmacTimer {
00388 public:
00389 SmacNavTimer(SMAC *a) : SmacTimer(a) {}
00390 void expire(Event *e);
00391 };
00392
00393
00394
00395 class SmacNeighNavTimer : public SmacTimer {
00396 public:
00397 SmacNeighNavTimer(SMAC *a) : SmacTimer(a) { stime_ = rtime_ = 0; }
00398 void sched(double duration);
00399 void expire(Event *e);
00400 double timeToExpire();
00401 protected:
00402 double stime_;
00403 double rtime_;
00404 };
00405
00406
00407 class SmacCsTimer : public SmacTimer {
00408 public:
00409 SmacCsTimer(SMAC *a) : SmacTimer(a) {}
00410 void expire(Event *e);
00411 void checkToCancel();
00412 };
00413
00414
00415 class SmacCounterTimer : public SmacTimer {
00416 public:
00417 friend class SMAC;
00418 SmacCounterTimer(SMAC *a, int i) : SmacTimer(a) {index_ = i;}
00419 void sched(double t);
00420 void expire(Event *e);
00421 double timeToSleep();
00422 protected:
00423 int index_;
00424 double value_;
00425 double syncTime_;
00426 double dataTime_;
00427 double listenTime_;
00428 double sleepTime_;
00429 double cycleTime_;
00430 double tts_;
00431 double stime_;
00432 };
00433
00434
00435
00436 class SMAC : public Mac {
00437
00438 friend class SmacGeneTimer;
00439 friend class SmacRecvTimer;
00440 friend class SmacSendTimer;
00441 friend class SmacNavTimer;
00442 friend class SmacNeighNavTimer;
00443 friend class SmacCsTimer;
00444 friend class SmacCounterTimer;
00445 #ifdef JOURNAL_PAPER
00446 friend class SmacUpdateNeighbTimer;
00447 friend class SmacAdaptiveListenTimer;
00448 #endif
00449
00450 public:
00451 SMAC(void);
00452 ~SMAC() {
00453 for (int i=0; i< SMAC_MAX_NUM_SCHEDULES; i++) {
00454 delete mhCounter_[i];
00455 }
00456 }
00457 void recv(Packet *p, Handler *h);
00458
00459 protected:
00460
00461
00462 #ifdef JOURNAL_PAPER
00463 void handleUpdateNeighbTimer();
00464 void handleAdaptiveListenTimer();
00465 #endif
00466 void handleGeneTimer();
00467 void handleRecvTimer();
00468 void handleSendTimer();
00469 void handleNavTimer();
00470 void handleNeighNavTimer();
00471 void handleCsTimer();
00472
00473 void handleCounterTimer(int i);
00474
00475
00476 double slotTime_;
00477 double slotTime_sec_;
00478 double difs_;
00479 double sifs_;
00480 double eifs_;
00481 double guardTime_;
00482 double byte_tx_time_;
00483 double dutyCycle_;
00484
00485 private:
00486
00487 void setMySched(Packet *syncpkt);
00488 void sleep();
00489 void wakeup();
00490
00491 #ifdef JOURNAL_PAPER
00492
00493 void check_schedFlag();
00494 void update_schedTab_neighbList();
00495 void update_myNeighbList();
00496 void update_neighbList();
00497 void checkMySched();
00498 void dump();
00499 #endif
00500
00501
00502
00503 void rxMsgDone(Packet* p);
00504
00505 #ifdef JOURNAL_PAPER
00506 void rxFragDone(Packet *p);
00507 #endif
00508 void handleRTS(Packet *p);
00509 void handleCTS(Packet *p);
00510 void handleDATA(Packet *p);
00511 void handleACK(Packet *p);
00512 void handleSYNC(Packet *p);
00513
00514
00515
00516
00517
00518 int checkToSend();
00519
00520 bool chkRadio();
00521 void transmit(Packet *p);
00522
00523 bool sendMsg(Packet *p, Handler *h);
00524 bool bcastMsg(Packet *p);
00525 bool unicastMsg(int n, Packet *p);
00526
00527
00528 void txMsgDone();
00529
00530
00531 #ifdef JOURNAL_PAPER
00532
00533 bool txNextFrag(void* data);
00534 void txFragDone();
00535
00536
00537 void adaptiveListen();
00538 #endif
00539
00540 int startBcast();
00541 int startUcast();
00542
00543 bool sendRTS();
00544 bool sendCTS(double duration);
00545 bool sendDATA();
00546 bool sendACK(double duration);
00547 bool sendSYNC();
00548
00549 void sentRTS(Packet *p);
00550 void sentCTS(Packet *p);
00551 void sentDATA(Packet *p);
00552 void sentACK(Packet *p);
00553 void sentSYNC(Packet *p);
00554
00555
00556 void collision(Packet *p);
00557 void capture(Packet *p);
00558 double txtime(Packet *p);
00559
00560 void updateNav(double duration);
00561 void updateNeighNav(double duration);
00562
00563 void mac_log(Packet *p) {
00564 logtarget_->recv(p, (Handler*) 0);
00565 }
00566
00567 void discard(Packet *p, const char* why);
00568 int drop_RTS(Packet *p, const char* why);
00569 int drop_CTS(Packet *p, const char* why);
00570 int drop_DATA(Packet *p, const char* why);
00571 int drop_SYNC(Packet *p, const char* why);
00572
00573
00574 inline int hdr_dst(char* hdr, int dst = -2) {
00575 struct hdr_smac *sh = (struct hdr_smac *) hdr;
00576 if (dst > -2)
00577 sh->dstAddr = dst;
00578 return sh->dstAddr;
00579 }
00580 inline int hdr_src(char* hdr, int src = -2) {
00581 struct hdr_smac *sh = (struct hdr_smac *) hdr;
00582 if (src > -2)
00583 sh->srcAddr = src;
00584 return sh->srcAddr;
00585 }
00586 inline int hdr_type(char *hdr, u_int16_t type = 0) {
00587 struct hdr_smac *sh = (struct hdr_smac *) hdr;
00588 if (type)
00589 sh->type = type;
00590 return sh->type;
00591 }
00592
00593
00594
00595 NsObject* logtarget_;
00596
00597
00598 int state_;
00599 int radioState_;
00600 int tx_active_;
00601 int mac_collision_;
00602
00603 int sendAddr_;
00604 int recvAddr_;
00605
00606 double nav_;
00607 double neighNav_;
00608
00609
00610 #ifdef JOURNAL_PAPER
00611 SmacUpdateNeighbTimer mhUpdateNeighb_;
00612 SmacAdaptiveListenTimer mhAdap_;
00613 #endif
00614 SmacNavTimer mhNav_;
00615 SmacNeighNavTimer mhNeighNav_;
00616 SmacSendTimer mhSend_;
00617 SmacRecvTimer mhRecv_;
00618 SmacGeneTimer mhGene_;
00619 SmacCsTimer mhCS_;
00620
00621
00622
00623 SmacCounterTimer *mhCounter_[SMAC_MAX_NUM_SCHEDULES];
00624
00625
00626 int numRetry_;
00627 int numExtend_;
00628 #ifdef JOURNAL_PAPER
00629 int numFrags_;
00630 int succFrags_;
00631 #endif
00632
00633
00634 int lastRxFrag_;
00635
00636 int howToSend_;
00637
00638 double durSyncPkt_;
00639 double durDataPkt_;
00640 double durCtrlPkt_;
00641 double timeWaitCtrl_;
00642
00643 struct SchedTable schedTab_[SMAC_MAX_NUM_SCHEDULES];
00644 struct NeighbList neighbList_[SMAC_MAX_NUM_NEIGHBORS];
00645
00646 int mySyncNode_;
00647
00648 int currSched_;
00649 int numSched_;
00650 int numNeighb_;
00651 int numBcast_;
00652
00653 Packet *dataPkt_;
00654 Packet *pktRx_;
00655 Packet *pktTx_;
00656
00657
00658
00659 int txData_ ;
00660
00661 int syncFlag_;
00662 int selfConfigFlag_;
00663 double startTime_;
00664
00665
00666 double syncTime_;
00667 double dataTime_;
00668 double listenTime_;
00669 double sleepTime_;
00670 double cycleTime_;
00671
00672 #ifdef JOURNAL_PAPER
00673 int adapTime_;
00674 int adaptiveListen_;
00675 int adapSend_;
00676 int txRequest_;
00677 int dataSched_;
00678 int syncSched_;
00679 int sendAddr;
00680
00681 int schedState_;
00682
00683 int globalSchedule_;
00684
00685 int updateNeighbList_;
00686 char sendSYNCFlag_;
00687 #endif
00688
00689
00690 int searchNeighb_;
00691 int schedListen_;
00692 int numSync_;
00693
00694 protected:
00695 int command(int argc, const char*const* argv);
00696 virtual int initialized() {
00697 return (netif_ && uptarget_ && downtarget_);
00698 }
00699 };
00700
00701
00702 #endif //NS_SMAC