⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timesyncm.nc

📁 Tinyos LEACH protocol modify
💻 NC
字号:
/*
* TimeSyncM.nc - A module to handle time synchronisation
*
* @author Geoff Martin
* @date 18th April 2004
* @version 1.0
*/
includes AM;
module TimeSyncM
{
	provides
	{
		interface StdControl;
		interface GlobalTime;
	}
	uses
	{
		interface Timer;
		interface Time;
		interface ReceiveMsg;
		interface SendMsg;
		interface Leds;
		interface StdControl as TSCommControl;
		interface StdControl as TimerControl;
		interface StdControl as SimpleControl;
		interface StdControl as TSQControl;
	}
}
implementation
{
	/*---------------------------------------------------------------------
	* Types and Variables
	*---------------------------------------------------------------------*/
	/*
	* Type Definitions
	*/
	typedef struct TimePacket
	{
		uint16_t nodeID;
		uint32_t time;
	} TimePacket;
	/*
	* Constants and variables
	*/
	enum
	{
		UPDATE_RATE = 5000, // 5 seconds
		HOP_DELAY = 22 // 22ms given by TimeSyncTest
	};
	int offset; // Offset from actual time
	bool synchronised; // Is the mote synchronised
	bool sendBusy; // Sending busy flag
	TOS_Msg timeMsg; // Message to store data to be sent in

	/*---------------------------------------------------------------------
	* Tasks
	*---------------------------------------------------------------------*/
	// Advertise current global time as known by this mote
	task void advertise()
	{
		TimePacket *pTP = (TimePacket *) &timeMsg.data[0];
		uint8_t length = sizeof(TimePacket);
	// Don't send if already sending
		if (sendBusy == TRUE)
		{
			return;
		}
		pTP->nodeID = TOS_LOCAL_ADDRESS;
		pTP->time = call Time.getLow32() + offset;
	// Send message
		if (call SendMsg.send(TOS_BCAST_ADDR, length, &timeMsg) == SUCCESS)
		{
	// Toggle red LED to indicate transmit (Tx)
			atomic
			{
				call Leds.redToggle();
			}
			atomic sendBusy = TRUE;
		}
	}

	/*---------------------------------------------------------------------
	* Provided Interface Commands
	*---------------------------------------------------------------------*/
	command result_t StdControl.init()
	{
	// Network synchronises on node 0
		if (TOS_LOCAL_ADDRESS == 0)
		{
			offset = 0;
			synchronised = TRUE;
		}
		else
		{
			synchronised = FALSE;
		}
		sendBusy = FALSE;
		call TSCommControl.init();
		call TimerControl.init();
		call SimpleControl.init();
		call TSQControl.init();
		return SUCCESS;
	}

	command result_t StdControl.start()
	{
		call TSCommControl.start();
		call TimerControl.start();
		call SimpleControl.start();
		call TSQControl.start();
		return call Timer.start(TIMER_REPEAT, UPDATE_RATE);
	}

	command result_t StdControl.stop()
	{
		call TSCommControl.stop();
		call TimerControl.stop();
		call SimpleControl.stop();
		call TSQControl.stop();
		return call Timer.stop();
	}

	// Get if the mote is synchronised
	command bool GlobalTime.isSynchronised()
	{
		return synchronised;
	}

	// Get the global time in milliseconds
	command uint32_t GlobalTime.getGlobalTime()
	{
	// Return current global time or 0 if not synchronised
		if (synchronised == TRUE)
		{
			return (call Time.getLow32() + offset);
		}
		else
		{
			return 0;
		}
	}

	/*---------------------------------------------------------------------
	* Used Interface Events
	*---------------------------------------------------------------------*/
	// Timer fired event
	event result_t Timer.fired()
	{
	// Advertise global time if syncronised
		if (synchronised == TRUE)
		{
			post advertise();
		}
		return SUCCESS;
	}

	// Receive message event
	event TOS_MsgPtr ReceiveMsg.receive(TOS_MsgPtr m)
	{
	// Toggle red LED to indicate receive (Rx)
		atomic
		{
			call Leds.redToggle();
		}
	// Determine time difference if not the base node
		if (TOS_LOCAL_ADDRESS != 0)
		{
			TimePacket *pTP = (TimePacket *) m->data;
			offset = (pTP->time + HOP_DELAY) - call Time.getLow32();
			if (synchronised == FALSE)
			{
				synchronised = TRUE;
				dbg(DBG_TEMP, "TimeSyncM - Node synchronised to global time\n");
			}
		}
		return m;
	}

	// Event for signalling that send is done
	event result_t SendMsg.sendDone(TOS_MsgPtr msg, result_t success)
	{
		atomic sendBusy = FALSE;
		return SUCCESS;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -