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

📄 indirect_msg_test.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 tests indirect packets through coordinator.
Minimum topology is:
Coordinator-> RFD1
-> RFD2
If you want to use a router, then set USE_ROUTER to 1,
see note in  usrJoinVerifyCallback()

Router topology: coordinator, one router, two RFDs.
The topology is:

Coordinator ->  router -> RFD1
-> RFD2


Start the coordinator first, then the router, then
each RFD. If a RFD fails to join the network, try
again. The usrJoinVerifyCallback() is written such
that the Coordinator will reject the RFDs, so that
the RFDs should join the router.

For this to work, the bindings in staticbind.h must
be correct. This test assumes that the LSBs of the
long addresses of the two RFDs are non-zero, and
are not equal to each other, and uses these as the
endpoints for the indirect messages.

Also, if using Win32, you need to be running the virtual board
interface for each of the RFDs so that you can push a button to send
a packet.


For WIN32, the Router project also has LRWPAN_RSSI defined
as a stronger value than the default (see util_get_rssi()
in halStack.c), so that packets
from the router appear to be closer in physical distance
than from other nodes, since RFDs use the RSSI value to
choose a node to respond to when they get multiple responses
from a beacon request.


In WIN32, start the coord first, then the router so that it
joins the coord, then RFDs so that they join the router.

*/

#include "msstate_lrwpan.h"

#define USE_ROUTER 0            //set this to '1' if you want test to use a router, see usrJoinVerifyCallback()

BYTE myLongAddress[8];
BYTE test_number;
UINT32 my_timer;

BYTE payload[32];  //buffer for payload

LADDR_UNION dstADDR;

static void print_test(void);
void getpayload(void);
static void get_SW1_press_and_release(void);

#define NUM_MESSAGES 8
//convoluted mess because of different ways compilers treat const char strings
void getpayload(void) {
	BYTE *dst;
	ROMCHAR *src;
	BYTE msgnum;

	static ROMCHAR _x0_[] = "Hello!";
	static ROMCHAR _x1_[] = "Goodbye?";
	static ROMCHAR _x2_[] = "WTH?";
	static ROMCHAR _x3_[] = "Ouchies!";
	static ROMCHAR _x4_[] = "Shazbot!";
	static ROMCHAR _x5_[] = "FerSur!";
	static ROMCHAR _x6_[] = "NoSir!";
	static ROMCHAR _x7_[] = "Wazzup?";

	msgnum = halGetRandomByte();
	msgnum = msgnum % NUM_MESSAGES;

	switch (msgnum) {
	 case 0 :  src = &_x0_[0]; break;
	 case 1 :  src = &_x1_[0]; break;
	 case 2 :  src = &_x2_[0]; break;
	 case 3 :  src = &_x3_[0]; break;
	 case 4 :  src = &_x4_[0]; break;
	 case 5 :  src = &_x5_[0]; break;
	 case 6 :  src = &_x6_[0]; break;
	 default :  src = &_x7_[0]; break;
	}
	dst = &payload[0];
	while (*src) {
		*dst = *src;
		dst++;src++;
	}
	*dst = *src;
}


void print_test(void){
	conPrintROMString("Test "); conPrintUINT8((UINT8) test_number);
	conPrintROMString(", Sending msg: ");
	conPrintString((char *)&payload[0]);
	conPCRLF();
}

//in this loop keep running the stack FSM so
//that we can receive packets
void get_SW1_press_and_release(void){
	while(EVB_SW1_RELEASED()) {apsFSM();evbPoll();}
	while(EVB_SW1_PRESSED()){apsFSM();evbPoll();}

}

void packet_test(void) {

	switch(test_number) {
	  case 0:
		  getpayload();
		  print_test();
		  conPrintROMString("Indirect MSG, no ack requested, SrcEP:  ");
		  conPrintUINT8(myLongAddress[0]);
		  conPrintROMString("Cluster: ");
		  conPrintUINT8(LRWPAN_APP_CLUSTER);
		  conPCRLF();
		  conPrintROMString("Press SW1 to send packet\n");
		  get_SW1_press_and_release();
		  aplSendMSG (APS_DSTMODE_NONE,
			  NULL,
			  0,
			  LRWPAN_APP_CLUSTER,
			  myLongAddress[0],
			  payload,
			  strlen((char *)payload)+1,
			  apsGenTSN(),
			  FALSE);
		  test_number++;
		  break;
	  case 1:
		  getpayload();
		  print_test();
		  conPrintROMString("Indirect MSG, APS ack requested, SrcEP:  ");
		  conPrintUINT8(myLongAddress[0]);
		  conPrintROMString("Cluster: ");
		  conPrintUINT8(LRWPAN_APP_CLUSTER);
		  conPCRLF();
		  conPrintROMString("Press SW1 to send packet\n");
		  get_SW1_press_and_release();
		  dstADDR.saddr = 0; //Coordinator has address 0
		  aplSendMSG (APS_DSTMODE_NONE,
			  NULL,
			  0,
			  LRWPAN_APP_CLUSTER,
			  myLongAddress[0],
			  payload,
			  strlen((char *)payload)+1,
			  apsGenTSN(),
			  TRUE);
		  test_number = 0;
		  break;



	  default:
		  test_number = 0;

		  break;
	}


	//block, and see if message sent successfully
	while(apsBusy()) {apsFSM();}
	if (aplGetStatus() == LRWPAN_STATUS_SUCCESS) {
		conPrintROMString("MSG send succeeded!\n");
	}else {
		conPrintROMString("MSG send FAILED!\n");
	}


}

void main (void){


	//this initialization set our SADDR to 0xFFFF,
	//PANID to the default PANID

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

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

	//debug_level = 10;
	EVB_LED1_OFF();
	EVB_LED2_OFF();

	//get this for reference, will use the LSB as srcEP for indirect message
	halGetProcessorIEEEAddress(&myLongAddress[0]);

#ifdef LRWPAN_COORDINATOR

	aplFormNetwork();
	while(apsBusy()) {apsFSM();} //wait for finish
	EVB_LED1_ON();
	conPrintROMString("Nwk formed, waiting for join and reception\n");
	while(1) {apsFSM();}  //coordinator just resolves indirect packets.

#else
	do {
		aplJoinNetwork();
		while(apsBusy()) {apsFSM();} //wait for finish
		if (aplGetStatus() == LRWPAN_STATUS_SUCCESS) {
			EVB_LED1_ON();
			conPrintROMString("Network Join succeeded!\n");
			conPrintROMString("My ShortAddress is: ");
			conPrintUINT16(aplGetMyShortAddress());
			conPCRLF();
			conPrintROMString("Parent LADDR: ")
				conPrintLADDR(aplGetParentLongAddress());
			conPrintROMString(", Parent SADDR: ");
			conPrintUINT16(aplGetParentShortAddress());
			conPCRLF();
			break;
		}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));
		}
	} while(1);


#ifdef LRWPAN_RFD
	//announce ourselves to the coordinator so that we can test indirect messaging
	//this is only necessary if there are routers between us and the coordinator,
	//but since don't know the network topology, do it always if RFD.
	do {
		aplSendEndDeviceAnnounce(0); //send to coordinator as it resolves bindings
		while(apsBusy()) {apsFSM();} //wait for finish
		if (aplGetStatus() == LRWPAN_STATUS_SUCCESS) {
			conPrintROMString("End Device Announce succeeded!\n");
			break;
		}
		else {
			conPrintROMString("End Device Announce FAILED! Waiting, then trying again\n");
			my_timer= halGetMACTimer();
			//wait for 2 seconds
			while ((halMACTimerNowDelta(my_timer))< MSECS_TO_MACTICKS(2*1000));
		}
	}while(1);

	//now send packets
	while (1) {
		packet_test();
		while(apsBusy()) {apsFSM();} //wait for finish
	}
#endif
#ifdef LRWPAN_ROUTER
	//router does nothing, just routes
	conPrintROMString("Router, doing its thing.!\n");
	while(1) {apsFSM();}
#endif

#endif


}

//########## 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){

#ifdef LRWPAN_COORDINATOR
	if (aplGetRxCluster() == ZEP_END_DEVICE_ANNOUNCE) {
		//a new end device has announced itself, print out the
		//the neightbor table and address map
		dbgPrintNeighborTable();
	}
#endif
  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) {

	BYTE len, *ptr;

	//just print out this data

	conPrintROMString("User Data Packet Received: \n");
	conPrintROMString("SrcSADDR: ");
	conPrintUINT16(aplGetRxSrcSADDR());
	conPrintROMString(", DstEp: ");
	conPrintUINT8(aplGetRxDstEp());
	conPrintROMString(", Cluster: ");
	conPrintUINT8(aplGetRxCluster());
	conPrintROMString(", Msg Length: ");
	len = aplGetRxMsgLen();
	conPrintUINT8(len);
	conPCRLF();
	conPrintROMString("Msg: ");
	ptr = aplGetRxMsgData();
	while(len){
		halPutch(*ptr);
		ptr++; len--;
	}
	conPCRLF();
	//toggle LED2 for each new message RX
	if (EVB_LED2_STATE()) EVB_LED2_OFF(); else EVB_LED2_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  USE_ROUTER             //set to '1' if you want to use a router
#ifdef LRWPAN_COORDINATOR
	//only let routers join us
	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


void usrIntCallback(void){

}

#ifdef LRWPAN_ENABLE_SLOW_TIMER
void usrSlowTimerInt(void ) {}
#endif

⌨️ 快捷键说明

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