p802_15_4timer.cc

Go to the documentation of this file.
00001 /********************************************/
00002 /*     NS2 Simulator for IEEE 802.15.4      */
00003 /*           (per P802.15.4/D18)            */
00004 /*------------------------------------------*/
00005 /* by:        Jianliang Zheng               */
00006 /*        (zheng@ee.ccny.cuny.edu)          */
00007 /*              Myung J. Lee                */
00008 /*          (lee@ccny.cuny.edu)             */
00009 /*        ~~~~~~~~~~~~~~~~~~~~~~~~~         */
00010 /*           SAIT-CUNY Joint Lab            */
00011 /********************************************/
00012 
00013 // File:  p802_15_4timer.cc
00014 // Mode:  C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t
00015 
00016 // $Header: /nfs/jade/vint/CVSROOT/ns-2/wpan/p802_15_4timer.cc,v 1.1 2005/01/24 18:34:25 haldar Exp $
00017 
00018 /*
00019  * Copyright (c) 2003-2004 Samsung Advanced Institute of Technology and
00020  * The City University of New York. All rights reserved.
00021  *
00022  * Redistribution and use in source and binary forms, with or without
00023  * modification, are permitted provided that the following conditions
00024  * are met:
00025  * 1. Redistributions of source code must retain the above copyright
00026  *    notice, this list of conditions and the following disclaimer.
00027  * 2. Redistributions in binary form must reproduce the above copyright
00028  *    notice, this list of conditions and the following disclaimer in the
00029  *    documentation and/or other materials provided with the distribution.
00030  * 3. All advertising materials mentioning features or use of this software
00031  *    must display the following acknowledgement:
00032  *  This product includes software developed by the Joint Lab of Samsung 
00033  *      Advanced Institute of Technology and The City University of New York.
00034  * 4. Neither the name of Samsung Advanced Institute of Technology nor of 
00035  *    The City University of New York may be used to endorse or promote 
00036  *    products derived from this software without specific prior written 
00037  *    permission.
00038  *
00039  * THIS SOFTWARE IS PROVIDED BY THE JOINT LAB OF SAMSUNG ADVANCED INSTITUTE
00040  * OF TECHNOLOGY AND THE CITY UNIVERSITY OF NEW YORK ``AS IS'' AND ANY EXPRESS 
00041  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
00042  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 
00043  * NO EVENT SHALL SAMSUNG ADVANCED INSTITUTE OR THE CITY UNIVERSITY OF NEW YORK 
00044  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
00045  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
00046  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
00047  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
00048  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
00049  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 //--base timer class for MAC sublayer---
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 //---timers for MAC sublayer---
00099 
00100 macBackoffTimer::macBackoffTimer(CsmaCA802_15_4 *csma) : Mac802_15_4Timer()
00101 {
00102     csmaca = csma;
00103 }
00104 
00105 /*
00106 void macBackoffTimer::start(double time, double st, bool idle)
00107 {
00108     wtime = time;
00109     slottime = st;
00110 
00111     if(idle == 0)
00112         paused_ = 1;
00113     else
00114         Mac802_15_4Timer::start(wtime);
00115 }
00116 */
00117 
00118 void macBackoffTimer::handle(Event *)
00119 {
00120     reset();
00121     csmaca->backoffHandler();
00122 }
00123 
00124 /*
00125 void macBackoffTimer::pause()
00126 {
00127     int slots;
00128     double sr;
00129     Scheduler &s = Scheduler::instance();
00130 
00131     sr = s.clock() - stime;
00132     slots = (int)(sr/slottime);
00133     if(slots < 0) slots = 0;
00134     assert(busy_ && ! paused_);
00135     paused_ = 1;
00136     wtime -= (slots * slottime);
00137     assert(wtime >= 0.0);
00138     s.cancel(&event);
00139 }
00140 
00141 void macBackoffTimer::resume(double beacontime)
00142 {
00143     Scheduler &s = Scheduler::instance();
00144 
00145     assert(busy_ && paused_);
00146     paused_ = 0;
00147     stime = s.clock();
00148     assert(wtime >= 0.0);
00149     s.schedule(this, &event, wtime);
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     /* Linux floating number compatibility
00205     t_endCAP = t_bcnRxTime + (mac->sfSpec2.FinCAP + 1) * t_sSlotDuration;
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)    //count-down should be paused at the end of CAP and resumed when receiving next superframe
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     //this function will be called by MAC each time a new beacon transmitted by the coordinator (only in beacon enabled mode)
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     /* Linux floating number compatibility
00346     wtime = BI - (now - bcnRxTime);
00347     */
00348     {
00349     tmpf = (now - bcnRxTime);;
00350     wtime = BI - tmpf;
00351     }
00352 
00353     if (wtime >= len12s)
00354         wtime -= len12s;
00355 
00356     /* Linux floating number compatibility
00357     if (now + wtime - lastTime < BI - len12s)
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         //wtime = 2* BI - (now - bcnRxTime) - len12s;
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 // End of file: p802_15_4timer.cc

Generated on Tue Mar 6 16:47:48 2007 for ns2 Network Simulator 2.29 by  doxygen 1.4.6