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 #include <delay.h>
00040 #include <connector.h>
00041 #include <packet.h>
00042 #include <random.h>
00043
00044
00045
00046 #include <arp.h>
00047 #include <ll.h>
00048 #include <mac.h>
00049 #include <mrcl_mac-timers.h>
00050 #include <miracle_mac-802_11.h>
00051
00052
00053
00054
00055
00056
00057
00058
00059 #ifdef USE_SLOT_TIME
00060 #error "Incorrect slot time implementation - don't use USE_SLOT_TIME..."
00061 #endif
00062
00063 #define ROUND_TIME() \
00064 { \
00065 assert(slottime); \
00066 double rmd = remainder(s.clock() + rtime, slottime); \
00067 if(rmd > 0.0) \
00068 rtime += (slottime - rmd); \
00069 else \
00070 rtime += (-rmd); \
00071 }
00072
00073
00074
00075
00076
00077
00078 void
00079 MrclMacTimer::start(double time)
00080 {
00081 Scheduler &s = Scheduler::instance();
00082 assert(busy_ == 0);
00083
00084 busy_ = 1;
00085 paused_ = 0;
00086 stime = s.clock();
00087 rtime = time;
00088 assert(rtime >= 0.0);
00089
00090
00091 s.schedule(this, &intr, rtime);
00092 }
00093
00094 void
00095 MrclMacTimer::stop(void)
00096 {
00097 Scheduler &s = Scheduler::instance();
00098
00099 assert(busy_);
00100
00101 if(paused_ == 0)
00102 s.cancel(&intr);
00103
00104 busy_ = 0;
00105 paused_ = 0;
00106 stime = 0.0;
00107 rtime = 0.0;
00108 }
00109
00110
00111
00112
00113 void
00114 MrclDeferTimer::start(double time)
00115 {
00116 Scheduler &s = Scheduler::instance();
00117
00118 assert(busy_ == 0);
00119
00120 busy_ = 1;
00121 paused_ = 0;
00122 stime = s.clock();
00123 rtime = time;
00124 #ifdef USE_SLOT_TIME
00125 ROUND_TIME();
00126 #endif
00127 assert(rtime >= 0.0);
00128
00129 s.schedule(this, &intr, rtime);
00130 }
00131
00132
00133 void
00134 MrclDeferTimer::handle(Event *)
00135 {
00136 busy_ = 0;
00137 paused_ = 0;
00138 stime = 0.0;
00139 rtime = 0.0;
00140
00141
00142
00143 mac->deferHandler();
00144 }
00145
00146
00147
00148
00149
00150 void
00151 MrclNavTimer::handle(Event *)
00152 {
00153 busy_ = 0;
00154 paused_ = 0;
00155 stime = 0.0;
00156 rtime = 0.0;
00157
00158 mac->navHandler();
00159 }
00160
00161
00162
00163
00164
00165 void
00166 MrclRxTimer::handle(Event *)
00167 {
00168 busy_ = 0;
00169 paused_ = 0;
00170 stime = 0.0;
00171 rtime = 0.0;
00172
00173 mac->recvHandler();
00174 }
00175
00176
00177
00178
00179
00180 void
00181 MrclTxTimer::handle(Event *)
00182 {
00183 busy_ = 0;
00184 paused_ = 0;
00185 stime = 0.0;
00186 rtime = 0.0;
00187
00188
00189
00190 mac->sendHandler();
00191 }
00192
00193
00194
00195
00196
00197 void
00198 MrclIFTimer::handle(Event *)
00199 {
00200 busy_ = 0;
00201 paused_ = 0;
00202 stime = 0.0;
00203 rtime = 0.0;
00204
00205 mac->txHandler();
00206 }
00207
00208
00209
00210
00211
00212 void
00213 MrclBackoffTimer::handle(Event *)
00214 {
00215 busy_ = 0;
00216 paused_ = 0;
00217 stime = 0.0;
00218 rtime = 0.0;
00219 difs_wait = 0.0;
00220 mac->backoffHandler();
00221 }
00222
00223 void
00224 MrclBackoffTimer::start(int cw, int idle, double difs)
00225 {
00226 Scheduler &s = Scheduler::instance();
00227
00228 assert(busy_ == 0);
00229
00230 busy_ = 1;
00231 paused_ = 0;
00232 stime = s.clock();
00233
00234 rtime = (Random::random() % cw) * mac->phymib_.getSlotTime();
00235
00236
00237
00238 #ifdef USE_SLOT_TIME
00239 ROUND_TIME();
00240 #endif
00241 difs_wait = difs;
00242
00243 if(idle == 0)
00244 paused_ = 1;
00245 else {
00246 assert(rtime + difs_wait >= 0.0);
00247 s.schedule(this, &intr, difs_wait);
00248 }
00249 }
00250
00251
00252 void
00253 MrclBackoffTimer::pause()
00254 {
00255 Scheduler &s = Scheduler::instance();
00256
00257
00258
00259
00260 double st = s.clock();
00261 double rt = stime + difs_wait;
00262 double sr = st - rt;
00263 double mst = (mac->phymib_.getSlotTime());
00264
00265
00266
00267 int slots = int (sr/mst);
00268
00269 if(slots < 0)
00270 slots = 0;
00271 assert(busy_ && ! paused_);
00272
00273 paused_ = 1;
00274 rtime -= (slots * mac->phymib_.getSlotTime());
00275
00276
00277 assert(rtime >= 0.0);
00278
00279 difs_wait = 0.0;
00280
00281 s.cancel(&intr);
00282 }
00283
00284
00285 void
00286 MrclBackoffTimer::resume(double difs)
00287 {
00288 Scheduler &s = Scheduler::instance();
00289
00290 assert(busy_ && paused_);
00291
00292 paused_ = 0;
00293 stime = s.clock();
00294
00295
00296
00297
00298
00299 difs_wait = difs;
00300
00301
00302
00303
00304
00305 assert(rtime + difs_wait >= 0.0);
00306 s.schedule(this, &intr, rtime + difs_wait);
00307 }
00308
00309