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

📄 sink.c

📁 是zmac的协议的全部完整的解析.代码例子很全
💻 C
字号:
// *******************************************************************
//	Sink.c
//	As a coordinator
//
//	Copyright 2006 by Holley Corporation. All rights reserved.		   
// *******************************************************************
#include	"app/sensor/common.h"
#include	"Holley_ext.h"

/****************************************************************************/
/*	注意要配置以下参数:													*/
/*		取消 DISABLE_WATCHDOG												*/
/*		EMBER_SERIAL1_TX_QUEUE_SIZE	= 128									*/
/*		EMBER_SERIAL1_RX_QUEUE_SIZE	= 32									*/
/****************************************************************************/


// ************************************************************************
// Ember endpoint and interface	configuration

int8u						emberEndpointCount = 1;
EmberEndpointDescription	PGM	endpointDescription	= {	PROFILE_ID,	0, };
EmberEndpoint				emberEndpoints[] = {
													{ ENDPOINT,	&endpointDescription },
											   };
// End Ember endpoint and interface	configuration
// *******************************************************************

BOOL		buttonZeroPress	= FALSE;  // for button	push - see halButtonIsr


//int			main( void );
//void			emberIncomingMessageHandler( EmberIncomingMessageType type, EmberApsFrame *apsFrame, EmberMessageBuffer message );
void			emberStackStatusHandler	( EmberStatus status );
static	void	applicationTick( void );
//void			halButtonIsr( int8u	button,	int8u state	);

//void			emberMessageSent( int8u	bindingTableIndex, int8u clusterId, EmberMessageBuffer message, EmberStatus status );
//void			emberUnicastSent ( EmberNodeId destination,	EmberApsFrame *apsFrame, EmberMessageBuffer message, EmberStatus status );
//EmberStatus	emberRemoteSetBindingHandler( EmberBindingTableEntry *entry	);
//EmberStatus	emberRemoteDeleteBindingHandler( int8u index );
//void			bootloadUtilQueryResponseHandler( BOOL bootloaderActive, int16u manufacturerId, int8u *hardwareTag, EmberEUI64 targetEui, int8u bootloaderCapabilities, int8u platform, int8u micro, int8u phy, int16u ulversion );
//BOOL			bootloadUtilLaunchRequestHandler( int16u manufacturerId, int8u *hardwareTag, EmberEUI64 sourceEui );

// *******************************************************************

// *******************************************************************

int	main(void)
{
	EmberNetworkParameters	networkParams;
	EmberResetType			reset;
	EmberStatus				status;

	halInit();			//Initialize the hal

	INTERRUPTS_ON();	// allow interrupts

	#ifdef	HL_OUTPUT_DEBUG_INFO_VIA_SERIAL
		emberSerialInit( APP_SERIAL, BAUD_19200, PARITY_EVEN, 1	);
	#endif

	#ifdef	HL_OUTPUT_DEBUG_INFO_VIA_SERIAL
		emberSerialPrintf( APP_SERIAL, "\r\nSet Baud Rate %x\r\n", BAUD_19200 );
	#endif
	if ( emberSerialInit( HOLLEY_SERIAL, BAUD_19200, PARITY_EVEN, 1	) != EMBER_SUCCESS ) 
	{
		#ifdef	HL_OUTPUT_DEBUG_INFO_VIA_SERIAL
			emberSerialPrintf( APP_SERIAL, "\r\nSet Baud Rate %x failed.\r\n", BAUD_19200 );
		#endif
		assert(FALSE);
	}

	#ifdef DEBUG
		emberDebugInit(DEBUG_SERIAL);
	#endif

	reset =	halGetResetInfo();
	status = emberInit(reset);	// emberInit must be called	before other EmberNet stack	functions

	if ( status != EMBER_SUCCESS ) 
	{
		#ifdef	HL_OUTPUT_DEBUG_INFO_VIA_SERIAL
			emberSerialGuaranteedPrintf ( APP_SERIAL,
										  "\r\nERROR: Coord emberInit 0x%x\r\n", status );
		#endif

		assert(FALSE);
	} 
	else
	{
		#ifdef	HL_OUTPUT_DEBUG_INFO_VIA_SERIAL
			emberSerialPrintf( APP_SERIAL, "\r\nEVENT: emberInit passed\r\n" );
		#endif
	}

	emberPermitJoining(0xff);	// Sink ini
	GPIO_SETL =	BIT(13);			//turn off Led1	 tx	data
	GPIO_SETL =	BIT(14);			//turn off Led2	 rx	data
	emberClearBindingTable();

	Sink_Para_Init();

	// Set the security	key	- specific to this application,	all	variants 
	// of this application (sink, sensor, sleepy-sensor, mobile-sensor)
	// need	to use the same	key. This function is in app/sensor/common.c
	setSecurityKey();

	 #ifdef	USE_BOOTLOADER_LIB
		// Using the same port for application serial print	and	passthru 
		// bootloading.	 User must be careful not to print anything	to the port
		// while doing passthru	bootload since that	can	interfere with the data
		// stream.	Also the port's	baud rate will be set to 115200	kbps in	order
		// to maximize bootload	performance.
		bootloadUtilInit(APP_SERIAL, DEBUG_SERIAL);
	#endif // USE_BOOTLOADER_LIB

	// try and rejoin the network this node	was	previously a part of
	// is status is	not	EMBER_SUCCESS then the node	didn't rejoin it's old network
	// sink	nodes need to be coordinators, so ensure we	are	a coordinator
//	if ( ( emberGetNodeType(&nodeType) !=	EMBER_SUCCESS) ||
//		 ( nodeType	!= EMBER_COORDINATOR) ||
//		 ( emberNetworkInit() != EMBER_SUCCESS ) )
//	{
		// Bring the network up.
		#ifndef USE_HARDCODED_NETWORK_SETTINGS
			// use the scan	utilities in app/scan/scan-utils.[ch]
			networkParams.radioChannel = scanUtilFindGoodChannel();
			networkParams.panId	= scanUtilFindFreeNetwork(networkParams.radioChannel);
			networkParams.radioTxPower = 3;
		#endif

		Read_OD( HL_NETWORK_PARAMETER_INDEX, 0, aucSwam );
		networkParams.panId			= Get_BIT16_Value( aucSwam + 20 );
		networkParams.radioTxPower	= aucSwam[3];
		networkParams.radioChannel	= aucSwam[0];
//		networkParams.panId			= APP_PANID;
//		networkParams.radioTxPower	= APP_POWER;
//		networkParams.radioChannel	= APP_CHANNEL;

		#ifdef	HL_OUTPUT_DEBUG_INFO_VIA_SERIAL
			emberSerialPrintf( APP_SERIAL, "\r\nEVENT: emberFormNetwork() with: PANID=%2x, Channel=%x, TX Power=%x\r\n", networkParams.panId, networkParams.radioChannel, networkParams.radioTxPower );
		#endif

		status = emberFormNetwork(&networkParams);		// Form	a new ZigBee network
		if (status != EMBER_SUCCESS) 
		{
			#ifdef	HL_OUTPUT_DEBUG_INFO_VIA_SERIAL
				emberSerialPrintf( APP_SERIAL, "\r\nEVENT: emberFormNetwork() return status %x\r\n", status );
			#endif
			assert(FALSE);
		}
//	}

	while(TRUE)	
	{
		halResetWatchdog();

		emberTick();

		Sink_Store_UART_Data();

		Sink_Process_Buffered_UART_Data();

		Sink_Process_Buffered_RF_Data();

		applicationTick();

		#ifdef DEBUG
			emberSerialBufferTick();	// Needed for debug which uses buffered serial
		#endif
	}
}





void	emberIncomingMessageHandler(	EmberIncomingMessageType type,
										EmberApsFrame *apsFrame,
										EmberMessageBuffer message )
{
	#ifdef USE_BOOTLOADER_LIB
		// If we are in	the	middle of bootloading, then	we want	to limit the
		// radio activity to minimum to	avoid causing any interruptions	to the
		// bootloading process.
		if ( IS_BOOTLOADING ) 
			return;
	#endif

//	if ( type == EMBER_INCOMING_DATAGRAM || type == EMBER_INCOMING_MULTICAST )
	if ( type != EMBER_INCOMING_MULTICAST_LOOPBACK )
	{
		if ( Store_RF_Data( message ) == FALSE )
		{
			#ifdef	HL_OUTPUT_DEBUG_INFO_VIA_SERIAL
				emberSerialPrintf( APP_SERIAL, "\r\nOut	of aucRF_MSG_Index slot. Oldest RF frame is deleted\r\n" );
				emberSerialWaitSend( APP_SERIAL	);
			#endif
		}

		#ifdef	HL_OUTPUT_DEBUG_INFO_VIA_SERIAL

			switch ( apsFrame->clusterId ) 
			{
				case HL_CMD_READ_OD:
					emberSerialPrintf( APP_SERIAL, "\r\nReceive Read_OD Resp+.\r\n"	);
					emberSerialWaitSend( APP_SERIAL	);
					break; 

				case HL_CMD_WRITE_OD:
					emberSerialPrintf( APP_SERIAL, "\r\nReceive Write_OD Resp+.\r\n" );
					emberSerialWaitSend( APP_SERIAL	);
					break; 

				case HL_CMD_STATEMENT:
					emberSerialPrintf( APP_SERIAL, "\r\nReceive Advertise from HHU.\r\n" );
					emberSerialWaitSend( APP_SERIAL	);
					break; 

				case HL_CMD_COLLECT_DATA:
					emberSerialPrintf( APP_SERIAL, "\r\nReceive Collect Data Resp.\r\n"	);
					emberSerialWaitSend( APP_SERIAL	);
					break; 

				case HL_CMD_TRANSFER_MTR:
					emberSerialPrintf( APP_SERIAL, "\r\nReceive Transfer Data Resp.\r\n" );
					emberSerialWaitSend( APP_SERIAL	);
					break;

//				case HL_CMD_STATEMENT_ACK:
//					emberSerialPrintf( APP_SERIAL, "\r\nReceive Advertise Ack.\r\n"	);
//					emberSerialWaitSend( APP_SERIAL	);
//					break; 

				case HL_CMD_READ_OD_ERR:
					emberSerialPrintf( APP_SERIAL, "\r\nReceive Read_OD Resp-.\r\n"	);
					emberSerialWaitSend( APP_SERIAL	);
					break; 

				case HL_CMD_WRITE_OD_ERR:
					emberSerialPrintf( APP_SERIAL, "\r\nReceive Write_OD Resp-.\r\n" );
					emberSerialWaitSend( APP_SERIAL );
					break; 
			}

		#endif	// HL_OUTPUT_DEBUG_INFO_VIA_SERIAL
	}
}



// this	is called when the stack status	changes
void	emberStackStatusHandler(EmberStatus	status)
{
	switch (status)	
	{
		case EMBER_NETWORK_UP:
			Set_Multicast_Binding();
			Send_Advertise( 0, 1, HL_RESP_OD_VERSION );
			#ifdef	HL_OUTPUT_DEBUG_INFO_VIA_SERIAL
				emberSerialPrintf ( APP_SERIAL,
									"\r\nEVENT: stack Status now EMBER_NETWORK_UP\r\n" );
			#endif
			break;

		case EMBER_NETWORK_DOWN:
			#ifdef	HL_OUTPUT_DEBUG_INFO_VIA_SERIAL
				emberSerialPrintf ( APP_SERIAL,
									"\r\nEVENT: stack Status now EMBER_NETWORK_DOWN\r\n");
			#endif
			break;

		case EMBER_JOIN_FAILED:
			#ifdef	HL_OUTPUT_DEBUG_INFO_VIA_SERIAL
				emberSerialPrintf ( APP_SERIAL,
									"\r\nEVENT: stack Status now EMBER_JOIN_FAILED\r\n");
			#endif
			break;

		default:
			#ifdef	HL_OUTPUT_DEBUG_INFO_VIA_SERIAL
				emberSerialPrintf ( APP_SERIAL, "\r\nEVENT: stackStatus now 0x%x\r\n", status);
			#endif
			break;
	}
}





/****************************************************************************/
/*	在本函数中完成以下功能:												*/
/*		1. 发送广告															*/
/*		2. 创建聚合路由														*/
/*		3. 过时绑定表的清理													*/
/*		4. 处理按键事件														*/
/****************************************************************************/
static void	applicationTick( void )
{
	static int16u			lastBlinkTime =	0;
	static int8u			ucCount = 4;
	int16u					time;
	int8u					i;
	EmberStatus				status;
	EmberBindingTableEntry	entry;

	#ifdef USE_BOOTLOADER_LIB
		bootloadUtilTick();
	#endif

	time = halCommonGetInt16uMillisecondTick();

	if ( time -	lastBlinkTime >	120 ) 
		GPIO_SETL =	BIT(15);			//turn OFF Led3

	if ( time -	lastBlinkTime >	250 ) 
	{
		lastBlinkTime =	time;
		GPIO_CLRL =	BIT(15);			//turn on Led3 HeartBeat

		ucCount --;
		if ( ucCount == 0 )				// 调整实时时间
		{
			Adjust_ucCurrent_Time( &sCurrent_Time );
			ucCount = 4;
		}

		if ( ucReset_Token > 0 )
		{
			switch ( ucReset_Token )		// 判断是否需要将 Token 恢复为缺省值
			{
				case 2:
					halCommonSetToken( TOKEN_HL_NETWORK_PARA,(int8u*)&sHL_Network_Para );
					break;
				case 1:
					halCommonSetToken( TOKEN_HL_AMR_PARA_GATEWAY,(int8u*)&sHL_AMR_Para_Gateway );
					break;
				default:
					break;
			}

			#ifdef	HL_OUTPUT_DEBUG_INFO_VIA_SERIAL
				emberSerialPrintf( APP_SERIAL, "\r\nReset Token %x to default value.\r\n", ucReset_Token );
			#endif
			ucReset_Token --;
		}

		if ( emberNetworkState() ==	EMBER_JOINED_NETWORK )
		{
			timeBeforeSinkAdvertise	--;

//			if ( timeBeforeSinkAdvertise ==	4 )		// create aggregation routes
			if ( timeBeforeSinkAdvertise ==	12 )	// 2007-1-31 lvyong modified
			{
				status = emberCreateAggregationRoutes();
				#ifdef	HL_OUTPUT_DEBUG_INFO_VIA_SERIAL
					emberSerialPrintf( APP_SERIAL,
						"\r\nEVENT: sink create aggregation routes, status 0x%x\r\n", status);
				#endif
			}

			if ( timeBeforeSinkAdvertise ==	0 )		// Send Advertisement
			{
				Read_OD( HL_AMR_PARA_GATEWAY_INDEX, 0, aucSwam );

				Send_Advertise( aucSwam[11], aucSwam[9], aucSwam[10] );

				timeBeforeSinkAdvertise	= Get_BIT16_Value( aucSwam ) * 4;	// uiAD_Cycle << 2;
			}
		}

		for ( i=0; i<EMBER_BINDING_TABLE_SIZE-1; i++ )		// 删除长时间未使用的绑定
		{
			status = emberGetBinding( i, &entry );
			if ( ( status == EMBER_SUCCESS ) &&	( entry.type == EMBER_UNICAST_BINDING ) )
			{
				ticksSinceLastHeard[i] ++;
				if ( ticksSinceLastHeard[i] > HL_BIND_TABLE_RELEASE_GAP )
				{
					status	= emberDeleteBinding(i);
					ticksSinceLastHeard[i]	= 0;

					#ifdef	HL_OUTPUT_DEBUG_INFO_VIA_SERIAL
						emberSerialPrintf (	APP_SERIAL,
											"\r\nEVENT: too long since last heard, " );
						emberSerialPrintf (	APP_SERIAL,
								"\r\ndeleting binding table index %x, status %x\r\n", i, status );
					#endif
				}
			}
		}

		if ( buttonZeroPress )
		{
			buttonZeroPress = FALSE;

			#ifdef	HL_OUTPUT_DEBUG_INFO_VIA_SERIAL
				emberSerialPrintf ( APP_SERIAL, "\r\nBUTTON0 Pressed\r\n" );
			#endif
		}
	}
}






void	halButtonIsr( int8u	button,	int8u state	)
{
	if ( button	== BUTTON0 && state	== BUTTON_PRESSED )
		buttonZeroPress	= TRUE;
}





// Called when a message has completed transmission	--
// status indicates	whether	the	message	was	successfully
// transmitted or not.
void	emberMessageSent(	int8u bindingTableIndex,
							int8u clusterId,
							EmberMessageBuffer message,
							EmberStatus	status	)
{
}

void	emberUnicastSent(	EmberNodeId	destination,
							EmberApsFrame *apsFrame,
							EmberMessageBuffer message,
							EmberStatus	status )
{
}

EmberStatus	emberRemoteSetBindingHandler( EmberBindingTableEntry *entry	)
{
	return EMBER_INVALID_BINDING_INDEX;
}

EmberStatus	emberRemoteDeleteBindingHandler( int8u index )
{
	return EMBER_INVALID_BINDING_INDEX;
}



#ifdef USE_BOOTLOADER_LIB
	// When	a device sends out a bootloader	query, the bootloader
	// query response messages are parsed by the bootloader-util
	// libray and and handled to this function.	This application
	// simply prints out the EUI of	the	device sending the query
	// response.
void bootloadUtilQueryResponseHandler(	boolean	bootloaderActive,
										int16u manufacturerId,
										int8u *hardwareTag,
										EmberEUI64 targetEui,
										int8u bootloaderCapabilities,
										int8u platform,
										int8u micro,
										int8u phy,
										int16u ulversion )
{
	emberSerialPrintf(APP_SERIAL,"RX [BL QUERY RESP] eui: ");
	printEUI64(APP_SERIAL, (EmberEUI64*)targetEui);
	emberSerialPrintf(APP_SERIAL," running %p\r\n",	
					bootloaderActive ? "bootloader":"stack");
	emberSerialWaitSend(APP_SERIAL);
}

	// This	function is	called by the bootloader-util library 
	// to ask the application if it	is ok to start the bootloader.
	// This	happens	when the device	is meant to	be the target of
	// a bootload. The application could compare the manufacturerId	
	// and/or hardwareTag arguments	to known values	to enure that 
	// the correct image will be bootloaded	to this	device.
BOOL	bootloadUtilLaunchRequestHandler( int16u manufacturerId,
										  int8u	*hardwareTag,
										  EmberEUI64 sourceEui)	
{
	// TODO: Compare arguments to known	values.

	// TODO: Check for minimum required	radio signal strength (RSSI).

	// TODO: Do	not	agree to launch	the	bootloader if any of the above conditions
	// are not met.	 For now, always agree to launch the bootloader.
	return TRUE;
}
#endif	// #ifdef USE_BOOTLOADER_LIB

⌨️ 快捷键说明

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