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 #include <packet.h>
00054 #include <random.h>
00055 #include "p802_15_4csmaca.h"
00056 #include "p802_15_4timer.h"
00057
00058
00059
00060
00061 Mac802_15_4Timer::Mac802_15_4Timer()
00062 {
00063 reset();
00064 }
00065
00066 void Mac802_15_4Timer::reset(void)
00067 {
00068 busy_ = 0;
00069 paused_ = 0;
00070 stime = 0.0;
00071 wtime = 0.0;
00072 }
00073
00074 void Mac802_15_4Timer::start(double time)
00075 {
00076 Scheduler &s = Scheduler::instance();
00077
00078 assert(busy_ == 0);
00079 busy_ = 1;
00080 paused_ = 0;
00081 stime = s.clock();
00082 wtime = time;
00083 assert(wtime >= 0.0);
00084 event.uid_ = 0;
00085 s.schedule(this, &event, wtime);
00086 }
00087
00088 void Mac802_15_4Timer::stop(void)
00089 {
00090 Scheduler &s = Scheduler::instance();
00091
00092 assert(busy_);
00093 if(paused_ == 0)
00094 s.cancel(&event);
00095 reset();
00096 }
00097
00098
00099
00100 macBackoffTimer::macBackoffTimer(CsmaCA802_15_4 *csma) : Mac802_15_4Timer()
00101 {
00102 csmaca = csma;
00103 }
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118 void macBackoffTimer::handle(Event *)
00119 {
00120 reset();
00121 csmaca->backoffHandler();
00122 }
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155 macBeaconOtherTimer::macBeaconOtherTimer(CsmaCA802_15_4 *csma) : Mac802_15_4Timer()
00156 {
00157 csmaca = csma;
00158 }
00159
00160 void macBeaconOtherTimer::handle(Event *)
00161 {
00162 reset();
00163 csmaca->bcnOtherHandler();
00164 }
00165
00166
00167
00168 macDeferCCATimer::macDeferCCATimer(CsmaCA802_15_4 *csma) : Mac802_15_4Timer()
00169 {
00170 csmaca = csma;
00171 }
00172
00173 void macDeferCCATimer::handle(Event *)
00174 {
00175 reset();
00176 csmaca->deferCCAHandler();
00177 }
00178
00179
00180
00181 void macTxOverTimer::handle(Event *)
00182 {
00183 reset();
00184 mac->txOverHandler();
00185 }
00186
00187
00188
00189 void macTxTimer::handle(Event *)
00190 {
00191 reset();
00192 mac->txHandler();
00193 }
00194
00195
00196
00197 void macExtractTimer::backoffCAP(double time)
00198 {
00199 double t_bcnRxTime,t_sSlotDuration,t_endCAP,t_endBackoff;
00200
00201 t_bcnRxTime = mac->macBcnRxTime / mac->phy->getRate('s');
00202 t_sSlotDuration = mac->sfSpec2.sd / mac->phy->getRate('s');
00203
00204
00205
00206
00207 {
00208 double tmpf;
00209 tmpf = (mac->sfSpec2.FinCAP + 1) * t_sSlotDuration;
00210 t_endCAP = t_bcnRxTime + tmpf;
00211 }
00212
00213 t_endBackoff = CURRENT_TIME + time;
00214 if (t_endBackoff > t_endCAP)
00215 leftTime = t_endBackoff - t_endCAP;
00216 else
00217 {
00218 onlyCAP = false;
00219 Mac802_15_4Timer::start(time);
00220 }
00221 }
00222
00223 void macExtractTimer::start(double time,bool onlycap)
00224 {
00225 if (!onlycap)
00226 {
00227 onlyCAP = false;
00228 Mac802_15_4Timer::start(time);
00229 }
00230 else
00231 {
00232 onlyCAP = true;
00233 backoffCAP(time);
00234 }
00235 }
00236
00237 void macExtractTimer::stop(void)
00238 {
00239 if (onlyCAP)
00240 onlyCAP = false;
00241 else
00242 Mac802_15_4Timer::stop();
00243 }
00244
00245 void macExtractTimer::handle(Event *)
00246 {
00247 onlyCAP = false;
00248 reset();
00249 mac->extractHandler();
00250 }
00251
00252 void macExtractTimer::resume(void)
00253 {
00254
00255 if (onlyCAP)
00256 backoffCAP(leftTime);
00257 }
00258
00259
00260
00261 void macAssoRspWaitTimer::handle(Event *)
00262 {
00263 reset();
00264 mac->assoRspWaitHandler();
00265 }
00266
00267
00268
00269 void macDataWaitTimer::handle(Event *)
00270 {
00271 reset();
00272 mac->dataWaitHandler();
00273 }
00274
00275
00276
00277 void macRxEnableTimer::handle(Event *)
00278 {
00279 reset();
00280 mac->rxEnableHandler();
00281 }
00282
00283
00284
00285 void macScanTimer::handle(Event *)
00286 {
00287 reset();
00288 mac->scanHandler();
00289 }
00290
00291
00292
00293 void macBeaconTxTimer::start(bool reset, bool fortx, double wt)
00294 {
00295 double wtime;
00296
00297 if (reset)
00298 {
00299 forTX = fortx;
00300 macBeaconOrder_last = 15;
00301 if (Mac802_15_4Timer::busy())
00302 Mac802_15_4Timer::stop();
00303 Mac802_15_4Timer::start(wt);
00304 return;
00305 }
00306 else
00307 {
00308 forTX = (!forTX);
00309 }
00310 if (!forTX)
00311 Mac802_15_4Timer::start(12 / mac->phy->getRate('s'));
00312 else if (mac->mpib.macBeaconOrder != 15)
00313 {
00314 wtime = ((aBaseSuperframeDuration * (1 << mac->mpib.macBeaconOrder) - 12) / mac->phy->getRate('s'));
00315 Mac802_15_4Timer::start(wtime);
00316 macBeaconOrder_last = mac->mpib.macBeaconOrder;
00317 }
00318 else if (macBeaconOrder_last != 15)
00319 {
00320 wtime = ((aBaseSuperframeDuration * (1 << macBeaconOrder_last) - 12) / mac->phy->getRate('s'));
00321 Mac802_15_4Timer::start(wtime);
00322 }
00323 }
00324
00325 void macBeaconTxTimer::handle(Event *e)
00326 {
00327 reset();
00328 mac->beaconTxHandler(forTX);
00329 }
00330
00331
00332
00333 void macBeaconRxTimer::start(void)
00334 {
00335 double BI,bcnRxTime,now,len12s,wtime;
00336 double tmpf;
00337
00338 BI = (aBaseSuperframeDuration * (1 << mac->macBeaconOrder2)) / mac->phy->getRate('s');
00339 bcnRxTime = mac->macBcnRxTime / mac->phy->getRate('s');
00340 now = CURRENT_TIME;
00341 while (now - bcnRxTime > BI)
00342 bcnRxTime += BI;
00343 len12s = 12 / mac->phy->getRate('s');
00344
00345
00346
00347
00348 {
00349 tmpf = (now - bcnRxTime);;
00350 wtime = BI - tmpf;
00351 }
00352
00353 if (wtime >= len12s)
00354 wtime -= len12s;
00355
00356
00357
00358
00359 tmpf = now + wtime;
00360 if (tmpf - lastTime < BI - len12s)
00361 {
00362 tmpf = 2 * BI;
00363 tmpf = tmpf - now;
00364 tmpf = tmpf + bcnRxTime;
00365 wtime = tmpf - len12s;
00366
00367 }
00368 lastTime = now + wtime;
00369 Mac802_15_4Timer::start(wtime);
00370 }
00371
00372 void macBeaconRxTimer::handle(Event *e)
00373 {
00374 reset();
00375 mac->beaconRxHandler();
00376 }
00377
00378
00379
00380 void macBeaconSearchTimer::handle(Event *e)
00381 {
00382 reset();
00383 mac->beaconSearchHandler();
00384 }
00385
00386