1、无线传感网络中的SMAC编程代码/* * smac.cc * Copyright (C) 2000 by the University of Southern California * $Id: smac.cc,v 1.18 2005/12/10 17:57:13 liyuan Exp $ * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License, * version 2, as published
2、 by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have rec
3、eived a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. * * * The copyright of this module includes the following * linking-with-specific-other-licenses addition: * * In a
4、ddition, as a special exception, the copyright holders of * this module give you permission to combine (via static or * dynamic linking) this module with free software programs or * libraries that are released under the GNU LGPL and with code * included in the standard release of ns-2 under the Apac
5、he 2.0 * license or under otherwise-compatible licenses with advertising * requirements (or modified versions of such code, with unchanged * license). You may copy and distribute such a system following the * terms of the GNU GPL for this module and the licenses of the * other code concerned, provid
6、ed that you include the source code of * that other code when and as the GNU GPL requires distribution of * source code. * * Note that people who make modified versions of this module * are not obligated to grant this special exception for their * modified versions; it is their choice whether to do
7、so. The GNU * General Public License gives permission to release a modified * version without this exception; this exception also makes it * possible to release a modified version which carries forward this * exception. * */ smac is designed and developed by Wei Ye (SCADDS/ISI)/ and is re-written fo
8、r ns by Padma Haldar (CONSER/ISI)./ Contributors: Yuan Li/ This module implements Sensor-MAC/ http:/www.isi.edu/scadds/papers/smac_report.pdf/ It has the following functions./ 1) Both virtual and physical carrier sense/ 2) RTS/CTS for hidden terminal problem/ 3) Backoff and retry/ 4) Broadcast packe
9、ts are sent directly without using RTS/CTS/ACK./ 5) A long unicast message is divided into multiple TOS_MSG (by upper/ layer). The RTS/CTS reserves the medium for the entire message./ ACK is used for each TOS_MSG for immediate error recovery./ 6) Node goes to sleep when its neighbor is communicating
10、 with another/ node./ 7) Each node follows a periodic listen/sleep schedule/ 8.1) At bootup time each node listens for a fixed SYNCPERIOD and then/ tries to send out a sync packet. It suppresses sending out of sync pkt/ if it happens to receive a sync pkt from a neighbor and follows the/ neighbors s
11、chedule./ 8.2) Or a node can choose its own schecule instead of following others, the/ schedule start time is user configurable/ 9) Neighbor Discovery: in order to prevent that two neighbors can not/ find each other due to following complete different schedules, each/ node periodically listen for a
12、whole period of the SYNCPERIOD/ 10) Duty cycle is user configurable/ New features including adaptive listen/ See http:/www.isi.edu/weiye/pub/smac_ton.pdf #include wireless-phy.h#include smac.hstatic class MacSmacClass : public TclClass public: MacSmacClass() : TclClass(Mac/SMAC) TclObject* create(in
13、t, const char*const*) return (new SMAC(); class_macSMAC; /创建MacSmacClass : public TclClass/ Timers call on expirationint SmacTimer:busy() if (status_ != TIMER_PENDING) return 0; else return 1; /定义busy()#ifdef JOURNAL_PAPERvoid SmacUpdateNeighbTimer:expire(Event *e) a_-handleUpdateNeighbTimer(); /用于阶
14、段更新邻居表的定时器 void SmacAdaptiveListenTimer:expire(Event *e) a_-handleAdaptiveListenTimer();#endif /在自适应侦听后用于让节点进入睡眠的定时器void SmacGeneTimer:expire(Event *e) a_-handleGeneTimer(); / 一般的时间用于sync, CTS and ACK的超时设定void SmacRecvTimer:expire(Event *e) stime_ = rtime_ = 0; a_-handleRecvTimer(); / 用于接收数据包的接收定时器中
15、的超时设定void SmacRecvTimer:sched(double time) TimerHandler:sched(time); stime_ = Scheduler:instance().clock(); rtime_ = time; / 用于接收数据包的接收定时器中的接收时间段void SmacRecvTimer:resched(double time) TimerHandler:resched(time); stime_ = Scheduler:instance().clock(); rtime_ = time; / 用于接收数据包的接收定时器中的二次接收时间段double Sm
16、acRecvTimer:timeToExpire() return (stime_ + rtime_) - Scheduler:instance().clock(); / 用于接收数据包的接收定时器中的剩余接收时间定时器void SmacSendTimer:expire(Event *e) a_-handleSendTimer(); /用于发送数据包的发送定时器中的超时设定void SmacNavTimer:expire(Event *e) a_-handleNavTimer(); /用于NAV的定时器设定void SmacNeighNavTimer:sched(double time) Ti
17、merHandler:sched(time); stime_ = Scheduler:instance().clock(); rtime_ = time; /用于显示邻居节点信道的NAV定时器的时间段void SmacNeighNavTimer:expire(Event *e) stime_ = rtime_ = 0; a_-handleNeighNavTimer(); /用于邻居节点NAV定时器的超时设定double SmacNeighNavTimer:timeToExpire() return (stime_ + rtime_) - Scheduler:instance().clock()
18、; /用于邻居节点NAV定时器的剩余到期设定void SmacCsTimer:expire(Event *e) a_-handleCsTimer(); / 用于载波侦听的定时器/ if pending, cancel timervoid SmacCsTimer:checkToCancel() if (status_ = TIMER_PENDING) cancel();/ void SmacChkSendTimer:expire(Event *e) / a_-handleChkSendTimer();/ / 同步定时器,用于控制睡眠/唤醒周期void SmacCounterTimer:sched
19、(double time) / the cycle timer assumes that all time shall be scheduled with time left to sleep / and not the absolute time for a given state (sleep, sync or data). Thus in order / to schedule for a sleep state, need to schedule with aggregate time CYCLETIME / (sleeptime+synctime+dadatime). / Simil
20、arly for sync state, schedule with listenTime_ (synctime+datattime) / This is implemented to be in step with the counter used in actual smac. / 周期时间设定所有的时间器应该用进入睡眠剩余时间定义,而不是确定状态下的绝对时间(睡眠,同步,数据)。因此为了对睡眠状态进行预订,需要 / 确定周期(sleeptime+synctime+dadatime)的和。 / 同样对于同步状态也是如此,这样做是为了SMAC中节点的计数器一致。 tts_ = time; /
21、 time before it goes to sleep again stime_ = Scheduler:instance().clock(); if (time CLKTICK2SEC(listenTime_) / in sleep state value_ = sleepTime_; if (status_ = TIMER_IDLE) TimerHandler:sched(time - CLKTICK2SEC(listenTime_); else TimerHandler:resched(time - CLKTICK2SEC(listenTime_); 对sched和resched理解
22、不清楚 else if ( time CLKTICK2SEC(dataTime_) / in sync state value_ = syncTime_; if (status_ = TIMER_IDLE) TimerHandler:sched(time - CLKTICK2SEC(dataTime_); else TimerHandler:resched(time - CLKTICK2SEC(dataTime_); else / in data state assert(time handleCounterTimer(index_); /同步定时器超时设定#ifdef JOURNAL_PAP
23、ERSMAC:SMAC() : Mac(), mhUpdateNeighb_(this),mhNav_(this), mhNeighNav_(this), mhSend_(this), mhRecv_(this), mhGene_(this), mhCS_(this), mhAdap_(this), syncFlag_(0) int i;#elseSMAC:SMAC() : Mac(), mhNav_(this), mhNeighNav_(this), mhSend_(this), mhRecv_(this), mhGene_(this), mhCS_(this), syncFlag_(0)
24、#endif state_ = IDLE; radioState_ = RADIO_IDLE; tx_active_ = 0; mac_collision_ = 0; sendAddr_ = -1; recvAddr_ = -1; nav_ = 0; neighNav_ = 0; numRetry_ = 0; numExtend_ = 0; lastRxFrag_ = -3; / since -1, -2 and 0 could be valid pkt uids /numFrags_ = 0; /succFrags_ = 0;#ifdef JOURNAL_PAPER numFrags_ =
25、0; succFrags_ = 0; dataSched_ = 0; syncSched_ = 0; globalSchedule_ = 0; / Do not test global schedule /globalSchedule_ = 1; / Test global schedule updateNeighbList_ = 0; sendSYNCFlag_ = 0; sendAddr = -1; adapSend_ = 0; txRequest_ = 0; adaptiveListen_ = 0;#endif dataPkt_ = 0; pktRx_ = 0; pktTx_ = 0;
26、/* setup internal mac and physical parameters - byte_tx_time_: time to transmit a byte, in ms. Derived from bandwidth slotTime_: time of each slot in contention window. It should be large enough to receive the whole start symbol but cannot be smaller than clock resolution. in msec slotTime_sec_: slo
27、ttime in sec difs_: DCF interframe space (from 802.11), in ms. It is used at the beginning of each contention window. Its the minmum time to wait to start a new transmission. (Distributed Coordination Function) sifs_: short interframe space (f /rom 802.11), in ms. It is used before sending an CTS or
28、 ACK packet. It takes care of the processing delay of each pkt. eifs_: Entended interfrane space (from 802.11) in ms. Used for backing off incase of a collision. guardTime_: guard time at the end of each listen interval, in ms. */ byte_tx_time_ = 8.0 / BANDWIDTH; double start_symbol = byte_tx_time_
29、* 2.5; / time to tx 20 bits slotTime_ = CLOCKRES = start_symbol ? CLOCKRES : start_symbol; / in msec slotTime_sec_ = slotTime_ / 1.0e3; / in sec difs_ = 10.0 * slotTime_; sifs_ = 5.0 * slotTime_; eifs_ = 50.0 * slotTime_; guardTime_ = 4.0 * slotTime_; / calculate packet duration. Following equations assume 4b/6b cod
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1