Code Review Time Synchronization Presented by Yong Chen Department of Computer Science University of Virginia Outline Clock Timer GlobalAbsoluteTimer TimeStamping Time Synchronization Clock Local time source CPU clock: high resolution (7.37MHz), not stable, stops in power-down mode (SysTimeC) External clock: low resolution, stable, keeps running in power-down mode ClockC component A high precision real time clock (with 1/32768 sec resolution) The Clock interface (Clock.nc) has been extended to support the setting and retrieval of the current (actual) clock rate in 1/32768 second units. Timer The new Timer component provides timers with 1/32768 sec resolution. If two timers use the same timer id, timer["id"], only the later timer takes effect, the previous one would not work. so timer should be used like TimerC.Timer[unique("Timer")]; Provide as many timers as needed Change the setting of TIMER_NUM_TIMERS GlobalAbsoluteTimer Triggered at the absolute time point instead of time interval as previous timer The resolution is 1/1000 or 1/32768 second If two timers use the same timer id, like GlobalAbsoluteTimer[id], only the later timer takes effect, the previous one would not work. so timer should be used like GlobalAbsoluteTimer[unique("AbsoluteTimer")] Provide as many timers as needed Change the setting of MAX_NUM_ABS_TIMERS TimeStamping Time stamping is a time synchronization primitive: establishing time reference points between a sender and receiver(s) using a single radio message Sender Sender obtains timestamp when the message was actually sent in its own local time The message can contain the local time of the sender at the time of transmission Method addStamp() Adds a time stamp to the next message sent by the radio. This method must be called immediatelly after SendMsg.send() returns SUCCESS. Receiver Receiver obtains timestamp when the message was received in its own local time Method getStamp() Returns the time stamp of the last received message. This method should be called when the ReceiveMsg.receive() is fired. The returned value contains the local time when the message was received. Time Synchronization Flooding Time Synchronization Protocol for Sensor Networks, VU Each node maintains two time variables Local Time Represents the local physical time Never reset or adjusted Global Time learns from its neighboring nodes. Reflects the time of the network Formula for Global Time and Local Time globalTime = localTime + offsetAverage + skew* (localTime – localAverage) variable offsetAverage, skew, and localAverage are decided by messages from neighboring nodes Time Synchronization Root The network is synchronized to the root node Sends out synchronization beacon messages periodically Other nodes are synchronized to the root, after synchronized, it will send out synch beacons periodically Who can be the root The node Set isForceRoot = 1 The node is set to be the root The node Set isForceRoot = -1 The node is never to be a root The node Set isForceRoot = 0 The node can be a root if it doesn’t receive any aynchronization message for a period of time It is possible there are several roots simultaneously, if so, the root with smaller ID would dominate the network. Time Synchronization Comments Actually, in the code, the isForceRoot is set to be either 1 or -1, so the code on isForceRoot == 0 can be eliminated. some repeated statements in command result_t StdControl.init() outgoingMsg->rootID = 0xFFFF; outgoingMsg->nodeID = TOS_LOCAL_ADDRESS; in command result_t StdControl.start() outgoingMsg->rootID = 0xFFFF; outgoingMsg->nodeID = TOS_LOCAL_ADDRESS; They can appear once in StdControl.init() only once