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

📄 mac_packet_test.c

📁 语言为C语言
💻 C
字号:
/*
  V0.1 Initial Release   10/July/2006

*/



//Basic packet sending test at the MAC level, used for internal testing only.
//This packet test has one node sending out a variety of
//differently formatted packets to two assumed destination nodes.

#include "wx_lrwpan.h"



char rfd1_dest_addr[8]={0x070,0x21,0x01,0x00,0x00,0x4B,0x12,0x00}; //little endian order!
char rfd2_dest_addr[8]={0x071,0x21,0x01,0x00,0x00,0x4B,0x12,0x00}; //little endian order!


BYTE test_number;

//WARNING - this assumes the payload is a null terminated string!
void do_send(BOOL bcast_flag,BOOL rfd2_dst, BYTE fcflsb, BYTE fcfmsb, UINT16 dstPANID,
			 UINT16 srcPANID, BYTE *payload)
{

	BYTE *ptr;
	BYTE plen;
	char i;

	//grab the TX buffer lock before proceeding
	while(phyTxLocked());
	phyGrabTxLock();
	//just make sure MAC is not busy
	while(macBusy());
	//at this point, have the TX lock and MAC is not busy.

	//if short address is used, it is the last two bytes of the long addr!!!
	a_mac_tx_data.fcflsb = fcflsb;
	a_mac_tx_data.fcfmsb = fcfmsb;
	ptr = &a_mac_tx_data.DestAddr.laddr.bytes[0];
	for(i=0;i<8;i++) {
		if (bcast_flag) *ptr = 0xFF;
		else {
			if (rfd2_dst) {
				*ptr= rfd2_dest_addr[i];
			} else {
				*ptr= rfd1_dest_addr[i];
			}
		}
		ptr++;
	}
	a_mac_tx_data.DestPANID = dstPANID;
	a_mac_tx_data.SrcPANID = srcPANID;
	//use the temp space available in the PHY for the TX packet
	//point at end of buffer
	phy_pib.currentTxFrm= &tmpTxBuff[LRWPAN_MAX_FRAME_SIZE];
	//copy the payload into this temp space, the frame is built
	//in reverse order.
	plen =  strlen((char *)&payload[0])+1;
	//copy in reverse order
	while (plen) {
		phy_pib.currentTxFrm--;  //decrement to first free location
		plen--;
		*(phy_pib.currentTxFrm) = payload[plen];
	}

	phy_pib.currentTxFlen = (BYTE) strlen((char *)&payload[0])+1;

	a_mac_service.cmd = LRWPAN_SVC_MAC_GENERIC_TX;

	conPrintROMString("hit a key to send packet\n");
	halGetch();


	macDoService();
	phyReleaseTxLock();
}

//assumes that Radio PANID is  already set to the default PANID
//with address rejection turned on
void packet_test(void) {

	BOOL rfd_dest;

	conPrintROMString("Test "); conPrintUINT8((UINT8) test_number);
	if (test_number % 2) rfd_dest = TRUE;
	else rfd_dest = FALSE;
	if (rfd_dest) {
		conPrintROMString(" to RFD2: ");
	}
	else {
		conPrintROMString(" to RFD1: ");
	}

	switch(test_number) {
	  case 0:
	  case 1:
		  conPrintROMString("Long DST, Long SRC, default PANID, ackreq\n");
		  do_send(FALSE, rfd_dest,
			  (LRWPAN_FRAME_TYPE_DATA|LRWPAN_FCF_INTRAPAN_MASK |LRWPAN_FCF_ACKREQ_MASK ),
			  (LRWPAN_FCF_DSTMODE_LADDR|LRWPAN_FCF_SRCMODE_LADDR),
			  LRWPAN_DEFAULT_PANID,
			  LRWPAN_DEFAULT_PANID,
			  "Hello!");
		  test_number++;
		  break;
	  case 2:
	  case 3:
		  conPrintROMString("Long DST, Long SRC, default PANID, no ackreq\n\n");
		  do_send(FALSE, rfd_dest,
			  (LRWPAN_FRAME_TYPE_DATA|LRWPAN_FCF_INTRAPAN_MASK ),
			  (LRWPAN_FCF_DSTMODE_LADDR|LRWPAN_FCF_SRCMODE_LADDR),
			  LRWPAN_DEFAULT_PANID,
			  LRWPAN_DEFAULT_PANID,
			  "Study Hard!");
		  test_number++;
		  break;
	  case 4:
	  case 5:
		  conPrintROMString("Short DST, Short Src, default PANID, ackreq\n\n");
		  do_send(FALSE, rfd_dest,
			  (LRWPAN_FRAME_TYPE_DATA|LRWPAN_FCF_INTRAPAN_MASK |LRWPAN_FCF_ACKREQ_MASK),
			  (LRWPAN_FCF_DSTMODE_SADDR|LRWPAN_FCF_SRCMODE_SADDR),
			  LRWPAN_DEFAULT_PANID,
			  LRWPAN_DEFAULT_PANID,
			  "Make Good Grades!");
		  test_number++;
		  break;
	  case 6:
	  case 7:
		  conPrintROMString("Short DST, Short Src, Wrong PANID, ackreq\n\n");
		  do_send(FALSE, rfd_dest,
			  (LRWPAN_FRAME_TYPE_DATA|LRWPAN_FCF_INTRAPAN_MASK |LRWPAN_FCF_ACKREQ_MASK),
			  (LRWPAN_FCF_DSTMODE_SADDR|LRWPAN_FCF_SRCMODE_SADDR),
			  LRWPAN_DEFAULT_PANID+1,
			  LRWPAN_DEFAULT_PANID+1,
			  "Enjoy Life!");
		  test_number++;
		  break;
	  case 8:
	  case 9:
		  conPrintROMString("BCAST DST, Short Src, default PanID, no ackreq\n\n");
		  do_send(TRUE, rfd_dest,
			  (LRWPAN_FRAME_TYPE_DATA|LRWPAN_FCF_INTRAPAN_MASK),
			  (LRWPAN_FCF_DSTMODE_SADDR|LRWPAN_FCF_SRCMODE_SADDR),
			  LRWPAN_DEFAULT_PANID,
			  LRWPAN_DEFAULT_PANID,
			  "Need any other advice?");
		  test_number++;
		  break;

	  default:
		  test_number = 0;

		  break;
	}

}

void main (void){
	MACPKT *pkt;
	BYTE laddr[8];
	SADDR saddr;

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

	//HalInit, evbInit will have to be called by the user
	halInit();
	evbInit();
	//phyInit, macInit will eventually be called from the stack
	phyInit();
	macInit();   //does a PhyInit->evbInit->halInit
	conPrintConfig();
	ENABLE_GLOBAL_INTERRUPT();
	test_number = 0;

	debug_level = 10;

	//for testing purposes, set our shortADDR to an assigned value
	halGetProcessorIEEEAddress(&laddr[0]);
#ifdef LWRPAN_COORDINATOR
	saddr = 0;
#else
	//set short address to our last two address bytes
	saddr = (((UINT16) laddr[1])<<8) + laddr[0];
#endif
	halSetRadioShortAddr(saddr);


	while (1) {
#ifdef LWRPAN_COORDINATOR

		packet_test();
		while(macBusy()) {HAL_SUSPEND(0);macFSM();} //wait for finish
#else
		//call PhyFSM here check for packets
		conPrintROMString("Waiting on reception\n");
		while(macRxBuffEmpty()){
			HAL_SUSPEND(0);
			phyFSM();
		}
		pkt = macGetRxPacket();
		//have a packet
		printf("Contents of Rx packet: \n");
		dbgPrintMacPacket (pkt->data+1, *(pkt->data));
		//free the packet
		macFreeRxPacket(TRUE);

#endif
	}

}

void usrIntCallback(void){
	
}

LRWPAN_STATUS_ENUM  usrRxPacketCallback(void) {return LRWPAN_STATUS_SUCCESS;}
LRWPAN_STATUS_ENUM usrZepRxCallback(void){ return LRWPAN_STATUS_SUCCESS; }

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


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

	return TRUE;

}

BOOL usrJoinNotifyCallback(LADDR *ptr){

	return TRUE;
}
#endif

⌨️ 快捷键说明

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