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

📄 pcbind_node.c

📁 本程序是基于Zigbee协议的无线温度传感器网络系统
💻 C
字号:
/*
V0.2 added PC-based binding         21/July/2006
V0.1 Initial Release                10/July/2006

*/



/*
This is a two node test, requires a Coordinator
and an RFD. The coordinator and node simply
ping-pong a packet back and forth, and print
out the RSSI byte.  The RFD waits before
bouncing it back, while the coordinator responds
immediately.

Expects coordinator, and one RFD.
The topology to test should be:

Coordinator ->  RFD1


Start the coordinator first, then
RFD1. If a RFD1 fails to join the network, try
again. The RFD1 will prompt the user to hit
a key to start the ping-pong.

You can connect multiple RFDs if desired.

You can also ping-pong through a router; see
the note in usrJoinVerifyCallback(). The topology
for a router would be:

coord -> router -> RFD1
-> RFD2
-> ..RFDn


This  requires Virtual Boards to be running,
since a switch press is needed to start the pinging.


*/

#include "wx_lrwpan.h"
#include "pcbind.h"

//define two endpoints for each node, a light and a switch
#define SWITCH_EP  1
#define LIGHT_EP   2
#define LS_CLUSTER 1

#define PING_TIME  60        //in seconds!!
#define MAX_PING_FAILURES 2


UINT32 my_timer, ping_timer;
BYTE ping_failures, seconds_cnt;


LADDR_UNION dstADDR;


void printJoinInfo(void){

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

typedef enum _APP_STATE_ENUM {
	APP_STATE_START_JOIN,
	APP_STATE_WAIT_FOR_JOIN,
	APP_STATE_SEND_ANNOUNCE,
	APP_STATE_ANNOUNCE_WAIT,
	APP_STATE_SEND_INFO,
	APP_STATE_INFO_WAIT,
	APP_STATE_RUN_APP1,
	APP_STATE_RUN_APP2,
	APP_STATE_RUN_APP3,
	APP_STATE_SEND_PING,
    APP_STATE_PING_WAIT,
	APP_STATE_START_REJOIN,
	APP_STATE_WAIT_FOR_REJOIN,
}APP_STATE_ENUM;

APP_STATE_ENUM appState;

#define MAX_REJOIN_FAILURES 3





void main (void){


    UINT8 failures;

	
	
	//HalInit, evbInit will have to be called by the user

	my_timer = 0;
	halInit();
	evbInit();

	aplInit();  //init the stack

#ifdef LRWPAN_COORDINATOR
#ifdef LRWPAN_USE_PC_BIND
	pbdInit();   //init the PC binding code
#endif
#endif



	conPrintConfig();
	ENABLE_GLOBAL_INTERRUPT();  //enable interrupts

	EVB_LED1_OFF();
	EVB_LED2_OFF();

	//register the endpoints numbers so that they can be reported
	aplRegisterEndPoint(SWITCH_EP);
	aplRegisterEndPoint(LIGHT_EP);

	appState = APP_STATE_START_JOIN;
	
	//debug_level = 10;

	while (1) {
		apsFSM();

#ifdef LRWPAN_COORDINATOR
#ifdef LRWPAN_USE_PC_BIND
		//the coordinator must also call the PCbinding FSM
		//to check for serial input from the PC client
		pbdFSM();
#endif
#endif

		switch (appState) {

		  case APP_STATE_START_JOIN:
			  EVB_LED1_OFF();
#ifdef LRWPAN_COORDINATOR
			  aplFormNetwork();
#else
			  aplJoinNetwork();
#endif
			  appState = APP_STATE_WAIT_FOR_JOIN;
			  break;
		  case APP_STATE_WAIT_FOR_JOIN:
			  if (apsBusy()) break;   //if stack is busy, continue
			  if (aplGetStatus() == LRWPAN_STATUS_SUCCESS) {
#ifdef LRWPAN_COORDINATOR
				  conPrintROMString("Network formation succeeded!\n");
				  appState = APP_STATE_SEND_INFO;  //coordinator just has to send info
#else
                  conPrintROMString("Network join succeeded! \n");
				  //RFDs, routers have to send announcement for indirect msgs,
				  //and then send the info for the PC client
				  appState = APP_STATE_SEND_ANNOUNCE;
#endif
				  EVB_LED1_ON();  //turn on LED1 indicating network is formed.
				
			  } else {
				  //only reason for this to fail is some sort of hardware failure
#ifdef LRWPAN_COORDINATOR
				  conPrintROMString("Network formation failed, waiting, trying again\n");
#else
                  conPrintROMString("Network join failed, waiting, trying again \n");
#endif
				  my_timer= halGetMACTimer();
				  //wait for 2 seconds				
				  while ((halMACTimerNowDelta(my_timer))< MSECS_TO_MACTICKS(2*1000));
				  appState = APP_STATE_START_JOIN;
			  }
			  break;
		  case APP_STATE_SEND_ANNOUNCE:
			  conPrintROMString("Sending Device Announcement\n");
			  aplSendEndDeviceAnnounce(0);
			  appState = APP_STATE_ANNOUNCE_WAIT;
			  break;

		 case APP_STATE_ANNOUNCE_WAIT:
			  if (apsBusy()) break;   //if stack is busy, continue
			  if (aplGetStatus() == LRWPAN_STATUS_SUCCESS) {
				  conPrintROMString("Announce Succeeded\n");
				  appState = APP_STATE_SEND_INFO;
			  }else {
                  conPrintROMString("Announce failed, waiting, trying again\n");
				  my_timer= halGetMACTimer();
				  //wait for 2 seconds
				  while ((halMACTimerNowDelta(my_timer))< MSECS_TO_MACTICKS(2*1000));
				  appState = APP_STATE_SEND_ANNOUNCE;
			  }

		 case APP_STATE_SEND_INFO:
			  conPrintROMString("Sending Device Information\n");
			  aplSendNodeInfo(0);
			  appState = APP_STATE_INFO_WAIT;
			  break;

          case APP_STATE_INFO_WAIT:
			  if (apsBusy()) break;   //if stack is busy, continue
			  if (aplGetStatus() == LRWPAN_STATUS_SUCCESS) {
				  conPrintROMString("Send Info Succeeded\n");
				  conPrintROMString("Waiting for switch press...\n");
				  ping_timer =  halGetMACTimer();
				  seconds_cnt = 0;
				  ping_failures = 0;
                          failures = 0;
				  appState = APP_STATE_RUN_APP1;
			  }else {
                  conPrintROMString("Send Info failed, waiting, trying again\n");
				  my_timer= halGetMACTimer();
				  //wait for 2 seconds
				  while ((halMACTimerNowDelta(my_timer))< MSECS_TO_MACTICKS(2*1000));
				  appState = APP_STATE_SEND_INFO;
			  }


		  case APP_STATE_RUN_APP1:
			  if (EVB_SW1_PRESSED()) appState = APP_STATE_RUN_APP2;	
#ifndef LRWPAN_COORDINATOR
			  if ((halMACTimerNowDelta(ping_timer))> MSECS_TO_MACTICKS(1000)){
                  seconds_cnt++;
				  if (seconds_cnt ==PING_TIME) {					
                     //send a ping
					  appState = APP_STATE_SEND_PING;
				  } else {
					  ping_timer =  halGetMACTimer();
				  }
			  }
#endif
			  break;
		  case APP_STATE_RUN_APP2:
			  if (EVB_SW1_PRESSED()) break;
			  //switch is now released
			  conPrintROMString("Switch press/release, sending message!\n")
			  aplSendMSG (APS_DSTMODE_NONE,
			                  NULL,
			                  0,
			                  LS_CLUSTER,
			                  SWITCH_EP,
			                  NULL,
			                  0,
			                  apsGenTSN(),
			                  TRUE);  //request an APS ack
              appState = APP_STATE_RUN_APP3;
			  break;			
		  case APP_STATE_RUN_APP3:
			  if (apsBusy()) break;
			  if (aplGetStatus() == LRWPAN_STATUS_SUCCESS)
			  {
				  conPrintROMString("MSG send succeeded!\n");
			  }else
			  {
				  conPrintROMString("MSG send failed!\n");
			  }
			  conPrintROMString("Waiting for switch press...\n");
			  appState = APP_STATE_RUN_APP1;

			  break;

#ifndef LRWPAN_COORDINATOR
		  case APP_STATE_SEND_PING:
			  //send the ping all the way to the coordinator so that
			  //the status is updated in the PC Client application
			  conPrintROMString("Pinging Coordinator...\n")
			  aplPingNode(0);
			  appState = APP_STATE_PING_WAIT;
			  break;

		   case APP_STATE_PING_WAIT:
			   if (apsBusy()) break;   //if stack is busy, continue
			   if (aplGetStatus() == LRWPAN_STATUS_SUCCESS) {
				  ping_failures = 0;
				  seconds_cnt = 0;
				  ping_timer =  halGetMACTimer();
				  appState = APP_STATE_RUN_APP1;
			   }else
			   {
				   ping_failures++;
				   conPrintROMString("Ping Failed!\n");
				   if (ping_failures == MAX_PING_FAILURES) {
					   failures = 0;
					   appState = APP_STATE_START_REJOIN;
				   }else {
					   appState = APP_STATE_SEND_PING;
				   }
			   }
			   break;


		  case APP_STATE_START_REJOIN:
			  EVB_LED1_OFF();
			  conPrintROMString("Trying to rejoin network!\n");
			  aplRejoinNetwork();
			  appState = APP_STATE_WAIT_FOR_REJOIN;
			  break;

		  case APP_STATE_WAIT_FOR_REJOIN:
			  if (apsBusy()) break;   //if stack is busy, continue
			  if (aplGetStatus() == LRWPAN_STATUS_SUCCESS) {
				  failures = 0;
				  EVB_LED1_ON();
				  conPrintROMString("Network Rejoin succeeded!\n");
				  printJoinInfo();
				  ping_timer =  halGetMACTimer();
				  seconds_cnt = 0;
				  ping_failures = 0;
				  appState = APP_STATE_RUN_APP1;
			  } else {
				  failures++;
				  if (failures == MAX_REJOIN_FAILURES) {
					  //this starts everything over
					  conPrintROMString("Max Rejoins failed, trying to join.\n");
					  appState = APP_STATE_START_JOIN;
				  } else {
					  //else, wait to try again
					  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));
					  appState = APP_STATE_START_REJOIN;
				  }
			  }
			  break;
#endif

		  default:
			  appState = APP_STATE_START_JOIN;

		}

	}
}





//########## Callbacks ##########

//callback for anytime the Zero Endpoint RX handles a command
//user can use the APS functions to access the arguments
//and take additional action is desired.
//the callback occurs after the ZEP has already taken
//its action.
LRWPAN_STATUS_ENUM usrZepRxCallback(void){
	
    return LRWPAN_STATUS_SUCCESS;
}

//callback from APS when packet is received
//user must do something with data as it is freed
//within the stack upon return.

LRWPAN_STATUS_ENUM usrRxPacketCallback(void) {

	
	//just print out this data

	conPrintROMString("User Data Packet Received: \n");
	//check the endpoint
	if (aplGetRxDstEp() == LIGHT_EP) {
		conPrintROMString("LED1 toggled\n");
		//our "light" has received a packet, lets toggle it
		if (EVB_LED1_STATE())
		{
			 EVB_LED1_OFF();
		} else
		{
			EVB_LED1_ON();
		}
	}
	return (LRWPAN_STATUS_SUCCESS);
	
}

#ifdef LRWPAN_FFD
//Callback to user level to see if OK for this node
//to join - implement Access Control Lists here based
//upon IEEE address if desired
BOOL usrJoinVerifyCallback(LADDR *ptr, BYTE capinfo){\

#if 0      //set this to '1' if you want to test through a router
//only accept routers.
//only let routers join us if we are coord
#ifdef LRWPAN_COORDINATOR
if (LRWPAN_GET_CAPINFO_DEVTYPE(capinfo)) {
	//this is a router, let it join
	conPrintROMString("Accepting router\n");
	return TRUE;
}else {
	conPrintROMString("Rejecting non-router\n");
	return FALSE;
}
#else
return TRUE;
#endif

#else

return TRUE;

#endif

}

BOOL usrJoinNotifyCallback(LADDR *ptr){

	//allow anybody to join

	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){}


#ifdef LRWPAN_COORDINATOR
#ifdef LRWPAN_USE_PC_BIND
//This callback is a way for you to extend the PC client binding demo
//with your own commands that you send to the coordinator via the serial
//link from the PC. See the documentation for details. If you use this
//callback, then you will be compatiable with future releases.
//See the file pcbind.c for when this callback is invoked.
void usrPbdCallback(UINT16 pktlen, BYTE crc){}
#endif
#endif



⌨️ 快捷键说明

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