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
00044 #ifndef MMAC_CSMA_H
00045 #define MMAC_CSMA_H
00046
00047 #include<module.h>
00048 #include<mmac.h>
00049 #include<timer-handler.h>
00050 #include<queue>
00051
00052
00053
00054 extern packet_t PT_MMAC_ACK;
00055
00056 class MMacCsma;
00057
00058 enum TIMER_STATUS{
00059 STATUS_STOPPED = 1,
00060 STATUS_RUNNING,
00061 STATUS_FROZEN
00062 };
00063
00064
00065 class Backoff_Timer : public TimerHandler
00066 {
00067 public:
00068 Backoff_Timer(MMacCsma *m);
00069 void start(double duration);
00070 void freeze();
00071 void defrost();
00072 bool isFrozen();
00073
00074 protected:
00075 virtual void expire(Event *e);
00076 MMacCsma* mac;
00077 enum TIMER_STATUS status;
00078 double start_time;
00079 double left_duration;
00080 };
00081
00082 class Ack_Timer : public TimerHandler
00083 {
00084 public:
00085 Ack_Timer(MMacCsma *m);
00086 void start(double duration);
00087 void stop();
00088 bool isExpired();
00089
00090 protected:
00091 virtual void expire(Event *e);
00092 MMacCsma* mac;
00093 enum TIMER_STATUS status;
00094 };
00095
00096
00097
00098
00099 enum MMAC_CSMA_STATE {
00100 STATE_IDLE = 0,
00101 STATE_RX_DATA,
00102 STATE_TX_ACK,
00103 STATE_CHK_PENDING_PKT,
00104 STATE_CHK_FROZEN_BACKOFF,
00105 STATE_BACKOFF,
00106 STATE_FREEZE_BACKOFF,
00107 STATE_TX_DATA,
00108 STATE_START_ACK_TIMEOUT,
00109 STATE_WAIT_ACK,
00110 STATE_RX_ACK,
00111 STATE_CHK_ACK_TIMEOUT_EXPIRED,
00112 STATE_TX_SUCCESSFUL,
00113 STATE_TX_FAILED,
00114 NUM_MMAC_CSMA_STATES
00115 };
00116
00117
00118 class MMacCsma : public MMac
00119 {
00120 public:
00121 MMacCsma();
00122
00123 protected:
00124
00125
00126 virtual void recvFromUpperLayers(Packet* p);
00127 virtual void Phy2MacEndTx(const Packet* p);
00128 virtual void Phy2MacStartRx(const Packet* p);
00129 virtual void Phy2MacEndRx(Packet* p);
00130 virtual void recvData4Me(Packet* p);
00131 virtual void recvAck4Me(Packet* p);
00132 virtual void recvWhateverElse(Packet* p);
00133
00134 public:
00135 virtual void AckTimeout();
00136 virtual void BackoffEnded();
00137
00138 protected:
00139
00140
00141
00142 virtual void enterState_IDLE();
00143 virtual void enterState_RX_DATA();
00144 virtual void enterState_TX_ACK(int macSA_of_data_pkt);
00145 virtual void enterState_CHK_PENDING_PKT();
00146 virtual void enterState_CHK_FROZEN_BACKOFF();
00147 virtual void enterState_BACKOFF();
00148 virtual void enterState_FREEZE_BACKOFF();
00149 virtual void enterState_TX_DATA();
00150 virtual void enterState_START_ACK_TIMEOUT();
00151 virtual void enterState_WAIT_ACK();
00152 virtual void enterState_RX_ACK();
00153 virtual void enterState_CHK_ACK_TIMEOUT_EXPIRED();
00154 virtual void enterState_TX_SUCCESSFUL();
00155 virtual void enterState_TX_FAILED();
00156
00157 void setState(enum MMAC_CSMA_STATE s);
00158
00159 enum MMAC_CSMA_STATE state;
00160
00161
00162
00163
00164
00165 bool PendingPacket();
00166 bool FrozenBackoff();
00167 bool AckTimeoutExpired();
00168 const Packet* PktRx;
00169 Packet* PktTx;
00170 std::queue<Packet*> Q;
00171 int ConsecutiveFailedTxAttempts;
00172
00173
00174 Backoff_Timer backoff_timer;
00175 Ack_Timer ack_timer;
00176
00177
00178 int HeaderSize_;
00179 double BaseBackoffTime_;
00180 double AckTimeout_;
00181
00182 int debug_states_;
00183
00184 static int uidcnt;
00185
00186 };
00187
00188
00189
00190
00191 #endif