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

📄 router_app.c

📁 //Basic packet sending test at the MAC level, used for internal testing only. //This packet test ha
💻 C
字号:
/*
  V0.1 Initial Release   10/July/2006  RBR

*/



/*
This is intended to be a router application
that recovers from dropped networks.
It starts by joining a network and then runnning
the stack. 

It occassionally pings its
parent to ensure that the parent is still
alive. If multiple ping attempts fail, the
router uses a 'rejoin' to join the network.
If this fail, the router tries a 'join'.
If this fails, then the router resets itself.


*/

#include "msstate_lrwpan.h"


#define PING_TIMEOUT  15  //in seconds, don't ping too often
#define MAX_PING_FAILURES 3
#define MAX_REJOIN_FAILURES 3

void printJoinInfo(void);

void printJoinInfo(void){

	conPrintROMString("My ShortAddress is: ");
	conPrintUINT16(aplGetMyShortAddress());
	conPCRLF();
	conPrintROMString("Parent LADDR: ");
	conPrintLADDR(aplGetParentLongAddress());
	conPrintROMString(", Parent SADDR: ");
	conPrintUINT16(aplGetParentShortAddress());
	conPCRLF();
}


typedef enum _ROUTER_STATE_ENUM {
	ROUTER_STATE_JOIN_NETWORK,
	ROUTER_STATE_JOIN_WAIT,
	ROUTER_STATE_NORMAL,
	ROUTER_STATE_DO_PING,
	ROUTER_STATE_WAIT_FOR_PING,
	ROUTER_STATE_REJOIN_NETWORK,
	ROUTER_STATE_REJOIN_WAIT  
}ROUTER_STATE_ENUM;

ROUTER_STATE_ENUM routerState;

void main (void){

	UINT32  my_timer;
	UINT8   failures, ping_cnt;

	//HalInit, evbInit will have to be called by the user
	halInit();
	evbInit();

	aplInit();  //init the stack
	conPrintConfig();
	ENABLE_GLOBAL_INTERRUPT();  //enable interrupts

	EVB_LED1_OFF();
	EVB_LED2_OFF();


	//debug_level = 10;

#ifdef LRWPAN_ROUTER
	routerState = ROUTER_STATE_JOIN_NETWORK;
	while(1) {      
		apsFSM();
		switch(routerState) {
		  case ROUTER_STATE_JOIN_NETWORK:
			  aplJoinNetwork();
			  routerState = ROUTER_STATE_JOIN_WAIT;
			  break;
		  case ROUTER_STATE_JOIN_WAIT:
			  if (apsBusy()) break;
			  if (aplGetStatus() == LRWPAN_STATUS_SUCCESS) {
				  conPrintROMString("Network Join succeeded!\n");
				  printJoinInfo();
				  my_timer = halGetMACTimer();
				  routerState = ROUTER_STATE_NORMAL;
				  ping_cnt = 0;
				  failures = 0;
				  EVB_LED1_ON();
			  } else 
			  {
				  conPrintROMString("Network Join FAILED! Waiting, then trying again\n");
				  my_timer= halGetMACTimer();
				  //wait for 2 seconds
				  while ((halMACTimerNowDelta(my_timer))< MSECS_TO_MACTICKS(2*1000));
				  routerState = ROUTER_STATE_JOIN_NETWORK;
			  }
			  break;

		  case ROUTER_STATE_NORMAL:
			  //check ping timeout
			  if (halMACTimerNowDelta(my_timer) > MSECS_TO_MACTICKS(1000)){
				  my_timer= halGetMACTimer(); //reset timer
				  //long timeouts are done in segments of short intervals as 
				  //the maximum timeout on the HAL MAC timer is platform dependent
				  ping_cnt++;
				  if (ping_cnt == PING_TIMEOUT) {
					  ping_cnt = 0;
					  //send a ping
					  routerState= ROUTER_STATE_DO_PING;
				  }
			  }
			  break;
		  case ROUTER_STATE_DO_PING:
			  conPrintROMString("Sending Ping!\n");
			  //aplPingParent uses an APS ack in order to ensure that this
			  //node is still associated with the parent
			  aplPingParent();
			  routerState= ROUTER_STATE_WAIT_FOR_PING;
			  break;
		  case ROUTER_STATE_WAIT_FOR_PING:
			  if (apsBusy()) break;
			  if (aplGetStatus() == LRWPAN_STATUS_SUCCESS) {
				  //all is well
				  failures = 0;
				  my_timer = halGetMACTimer();
				  routerState= ROUTER_STATE_NORMAL;
			  }else {
				  failures++;
				  conPrintROMString("Ping failed!\n");
				  if (failures == MAX_PING_FAILURES) {
					  failures = 0;
					  routerState= ROUTER_STATE_REJOIN_NETWORK;
				  }
				  break;
		  case ROUTER_STATE_REJOIN_NETWORK:
			  //A rejoin takes less time than a join, and does 
			  //not erase the neighbor table. A join erases
			  //the neighbor table, forcing all of the router's
			  //childen to re-issue joins.
			  EVB_LED1_OFF();  //not connected to a network
			  aplRejoinNetwork();
			  routerState= ROUTER_STATE_REJOIN_WAIT;
			  break;
		  case ROUTER_STATE_REJOIN_WAIT:
			  if (apsBusy()) break;
			  if (aplGetStatus() == LRWPAN_STATUS_SUCCESS) {
				  my_timer = halGetMACTimer();
				  routerState = ROUTER_STATE_NORMAL;
				  ping_cnt = 0;
				  failures = 0;
				  EVB_LED1_ON();
				  conPrintROMString("Network Rejoin succeeded!\n");
				  printJoinInfo();
				  routerState = ROUTER_STATE_NORMAL;
			  } else {
				  failures++;
				  if (failures == MAX_REJOIN_FAILURES) {
					  conPrintROMString("Network ReJoin max tries exceeded. Trying a join.");
					  routerState = ROUTER_STATE_JOIN_NETWORK;
				  } else {
					  conPrintROMString("Network ReJoin FAILED! Waiting, then trying again\n");
					  my_timer= halGetMACTimer();
					  //wait for 2 seconds
					  while ((halMACTimerNowDelta(my_timer))< MSECS_TO_MACTICKS(2*1000));
					  routerState = ROUTER_STATE_REJOIN_NETWORK;
					  break;
				  }
			  }
			  break;
		  default: 
			  routerState = ROUTER_STATE_JOIN_NETWORK;
			  }

		}
	} //end while(1)
#else
	conPrintROMString("This application is intended for a router!\n");
	conPrintROMString("Entering infinite loop, doing nothing.\n");
	while(1);

#endif


}

//########## Callbacks ##########
//don't do much with these callbacks in the router application

LRWPAN_STATUS_ENUM usrZepRxCallback(void){return LRWPAN_STATUS_SUCCESS; }

//no user endpoint in this application to receive data
LRWPAN_STATUS_ENUM usrRxPacketCallback(void) {return LRWPAN_STATUS_SUCCESS;}


#ifdef LRWPAN_FFD
//allow anybody to join
BOOL usrJoinVerifyCallback(LADDR *ptr, BYTE capinfo){

	return TRUE;

}

BOOL usrJoinNotifyCallback(LADDR *ptr){
	conPrintROMString("Node joined: ");
	conPrintLADDR(ptr);
	conPCRLF();
	DEBUG_PRINTNEIGHBORS(DBG_INFO);
	return TRUE;
}
#endif

//called when the slow timer interrupt occurs
#ifdef LRWPAN_ENABLE_SLOW_TIMER
void usrSlowTimerInt(void ) {}
#endif


//general interrupt callback , when this is called depends on the HAL layer.
void usrIntCallback(void){}

⌨️ 快捷键说明

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