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

📄 network.c

📁 关于zigbee厂家jennic的zigbee通信模块JN5139的一些示例程序。
💻 C
📖 第 1 页 / 共 5 页
字号:
							psData->u8MsgFlags,
							psData->u16Length);
					}
				}
			#endif

			/* Note address we last received from */
			u64LastRxAddress = psData->u64SrcAddress;

			/* Receive the data */
			vNetwork_Rx(psData->u64SrcAddress,
					    psData->u16Length,
						psData->pau8Data);
			}
		break;

	/* Data ack ? */
	case E_JENIE_DATA_ACK:
		{
			/* Get pointer to correct primitive structure */
			tsDataAck *psDataAck = (tsDataAck *) pvEventPrim;

			/* Is this ack for our head packet ? */
			if (sNetworkTxQ.bPend == TRUE &&
				sNetworkTxQ.asTx[sNetworkTxQ.u8Head].u64Address == psDataAck->u64SrcAddress)
			{
				/* Indicate success by setting attempts to 0xFF */
				sNetworkTxQ.u8Attempts = 0xFF;
			}

			/* Network debugging */
			#if NETWORK_DEBUG
				if (bUartUp)
				{
					/* Debug */
					vPrintf("vNetwork_StackDataEvent(%s, %x:%x)\n",
							aszEventType[eEventType],
							(uint32) (psDataAck->u64SrcAddress >> 32),
							(uint32) (psDataAck->u64SrcAddress  & 0xFFFFFFFF));
				}
			#endif
			}
		break;

	/* Others ? */
	default:
		/* Network debugging */
		#if NETWORK_DEBUG
			if (bUartUp)
			{
				vPrintf("vNetwork_StackDataEvent(%s)\n", aszEventType[eEventType]);
			}
		#endif
		break;
	}
}

/****************************************************************************
 *
 * NAME 		vNetwork_HwEvent
 */
/*!
 *\DESCRIPTION	Called when a stack hardware event has occurred.
 *
 * This function should be called from vJenie_HwEvent() as it handles
 * the timer used whilst registering and requesting services.
 */
/* RETURNS
 * None
 *
 ****************************************************************************/
PUBLIC void vNetwork_HwEvent (
	uint32 u32DeviceId,		/**< Device that generated the event. */
 	uint32 u32ItemBitmap)	/**< Source within device that generated the event. */
{
	teJenieStatusCode eStatus;

	/* Wake timer 1 event ? */
	if (u32DeviceId  == E_JPI_DEVICE_SYSCTRL &&
	   (u32ItemBitmap & E_JPI_SYSCTRL_WK1_MASK) != 0)
	{
		/* Is our source service bound so we can send data ? */
		if (bNetwork_Source_Bound(1))
		{
			/* Transmit any pending data */
			vProtocol_Tx();
		}
		/* End Device and network is up ? */
		if (eDeviceType == E_JENIE_END_DEVICE && bNetworkUp)
		{
			/* Poll timer running ? */
			if (u16PollTimer > 0)
			{
				/* Decrement poll timer */
				u16PollTimer--;
				/* Poll timer expired ? */
				if (u16PollTimer == 0)
				{
					/* Poll parent for data */
					eStatus = eJenie_PollParent();
					/* Restart timer */
					u16PollTimer = u16PollPeriod;
				}
			}
		}
		#if NETWORK_BUTTON_BIND
		{
			uint8 u8NewButton;
			/* Read buttons */
			u8NewButton = u8ButtonReadRfd();
			/* Buttons changed ? */
			if (u8NewButton != u8Button)
			{
				/* Has button 0 been released ? */
				if ((u8NewButton & BUTTON_0_MASK) == 0 && (u8Button & BUTTON_0_MASK) != 0)
				{
					/* Invert binding enabled flag */
					bBind = ! bBind;
					/* Set appropriate binding state */
					(void) eNetwork_Destination_Enabled(1, bBind);
					(void) eNetwork_Source_Enabled(1, 1, bBind);
					#if NETWORK_LED
					/* Is the network up ? */
					if (bNetworkUp)
						{
						if (!bNetwork_Destination_Bound(1)) vLedControl(0, !bBind);
						if (!bNetwork_Source_Bound(1))      vLedControl(1, !bBind);
						}
					#endif
				}
				/* Note new button setting */
				u8Button = u8NewButton;
			}
		}
		#endif
		/* Service state timer running ? */
		if (u16ServiceStateTimer > 0)
		{
			/* Decrement timer */
			u16ServiceStateTimer--;
		}
		/* Network transmit queue timer running ? */
		if (sNetworkTxQ.u16Timer > 0)
		{
			/* Decrement timer */
			sNetworkTxQ.u16Timer--;
		}
		#if NETWORK_LED
		/* Is the network up ? */
		if (bNetworkUp)
		{
			/* Is our destination service bound ? */
			if (bNetwork_Destination_Bound(1))
			{
				/* LED 0 timer running ? */
				if (u8Led0Timer > 0)
				{
					/* Timer is start of pulse ? */
					if (u8Led0Timer == LED_PULSE)
					{
						/* Turn on LED 0 */
						vLedControl(0, TRUE);
					}
					/* Decrement */
					u8Led0Timer--;
					/* Timer no run out ? */
					if (u8Led0Timer == 0)
					{
						/* Turn out LED 0 */
						vLedControl(0, FALSE);
					}
				}
			}
			#if NETWORK_BUTTON_BIND
			/* Is our destination service allowing binding ? */
			else if (asServices[0].u32Allow & 1)
			{
				/* Flash LED 0 */
				u8Led0Timer++;
				vLedControl(0, (u8Led0Timer & 0x4) ? TRUE : FALSE);
			}
			#endif
			/* Is our source service bound ? */
			if (bNetwork_Source_Bound(1))
			{
				/* LED 1 timer running ? */
				if (u8Led1Timer > 0)
				{
					/* Timer is start of pulse ? */
					if (u8Led1Timer == LED_PULSE)
					{
						/* Turn on LED 1 */
						vLedControl(1, TRUE);
					}
					/* Decrement */
					u8Led1Timer--;
					/* Timer no run out ? */
					if (u8Led1Timer == 0)
					{
						/* Turn out LED 1 */
						vLedControl(1, FALSE);
					}
				}
			}
			#if NETWORK_BUTTON_BIND
			/* Is our source service allowing binding ? */
			else if (asServices[1].u32Allow & 1)
			{
				/* Flash LED 1 */
				u8Led1Timer++;
				vLedControl(1, (u8Led1Timer & 0x4) ? TRUE : FALSE);
			}
			#endif
		}
		#endif
	    /* Update display */
		vWUartDisp_Update();
		/* Run the timer for another 10ms */
		vJPI_WakeTimerStart(E_JPI_WAKE_TIMER_1, 320);
	}
}

/****************************************************************************
 *
 * NAME 		vNetwork_Tx
 */
/*!
 *\DESCRIPTION	Queues data for transmission over the network - independent of a service.
 *
 * Transmits data over the network to a specified address, no service is
 * used.
 *
 * This function is used to implement the binding permission protocol.
 */
/* RETURNS
 * None
 *
 ****************************************************************************/
PUBLIC bool_t bNetwork_Tx (
	uint64 u64Address, 	/**< Address to transmit data to */
	uint8   u8TxFlags,	/**< Transmisison flags */
	uint16 u16Length,  	/**< Length of data */
	uint8 *pu8Data)		/**< Pointer to data */
{
	bool_t bReturn;

	/* Add to queue */
	bReturn = bNetwork_Tx_Add(u64Address, u8TxFlags, u16Length, pu8Data);

	/* Network debugging */
	#if NETWORK_DEBUG
		if (bUartUp)
		{
			if (bNetwork_IsString(u16Length, pu8Data))
			{
				/* Debug with data */
				vPrintf("bNetwork_Tx(%x:%x, %x, %d, \"%s\") = %d\n",
						(uint32) (u64Address >> 32),
						(uint32) (u64Address  & 0xFFFFFFFF),
						u8TxFlags,
						u16Length,
						(char *) pu8Data,
						bReturn);
			}
			else
			{
				/* Debug without data */
				vPrintf("bNetwork_Tx(%x:%x, %x, %d) = %d\n",
						(uint32) (u64Address >> 32),
						(uint32) (u64Address  & 0xFFFFFFFF),
						u8TxFlags,
						u16Length,
						bReturn);
			}
		}
	#endif

	return bReturn;	/**< \return TRUE is queued */
}

/****************************************************************************
 *
 * NAME	 		vNetwork_Rx
 */
/*!
 *\DESCRIPTION	Receive data from network - independent of service.
 *
 * This function is called by vNetwork_StackDataEvent() when a data message
 * has been received from an address, no service is used.
 *
 * This function is used to implement the binding permission protocol.
 */
/* RETURNS:
 * None
 *
 ****************************************************************************/
PUBLIC void vNetwork_Rx	(
	uint64 u64Address, 	/**< Address data received from */
	uint16 u16Length, 	/**< Length of data */
	uint8 *pu8Data)		/**< Pointer to data */
{

	/* Network debugging */
	#if NETWORK_DEBUG
		if (bUartUp)
		{
			if (bNetwork_IsString(u16Length, pu8Data))
			{
				/* Debug with data */
				vPrintf("vNetwork_Rx(%x:%x, %d, \"%s\")\n",
								(uint32) (u64Address >> 32),
								(uint32) (u64Address  & 0xFFFFFFFF),
								u16Length,
								(char *) pu8Data);
			}
			else
			{
				/* Debug with data */
				vPrintf("vNetwork_Rx(%x:%x, %d)\n",
								(uint32) (u64Address >> 32),
								(uint32) (u64Address  & 0xFFFFFFFF),
								u16Length);
			}
		}
	#endif

	/* Does this look like an binding protocol message ? */
	if (pu8Data[0] == '<' && pu8Data[1] == 'B')
	{
		/* Handle the binding message */
		vNetwork_Services_Bind_Rx(u64Address, u16Length, pu8Data);
	}
	else
	{
		/* Pass data to node type file - Coordinator.c, Router.c or EndDevice.c */
		//vDevice_Rx(u64Address, u16Length, pu8Data);
	}
}

/****************************************************************************
 *
 * NAME	 		eNetwork_Sleep
 */
/*!
 *\DESCRIPTION	Puts an end device to sleep.
 *
 * This function should be used in preference to calling eJenie_Sleep
 * directly as it disables the wake timer used internally that would otherwise
 * wake a sleeping end device if left running.
 */
/* RETURNS:
 * None
 *
 ****************************************************************************/
PUBLIC teJenieStatusCode eNetwork_Sleep	(
	uint32 u32SleepPeriodMs, 	/**< Length of time to sleep for (ms) */
	bool_t bMemHold) 			/**< Memory hold */
{
	teJenieStatusCode  eStatus = E_JENIE_ERR_INVLD_PARAM;

	/* Are we an end device and the network is up ? */
	if (eNetwork_DeviceType() == E_JENIE_END_DEVICE && bNetworkUp == TRUE)
	{
		/* Disable wake timer 1 interrupt so we don't get woken from sleeping */
		vJPI_WakeTimerEnable(E_JPI_WAKE_TIMER_1, FALSE);
		/* Set the sleep period (this is set in ms) */
		(void) eJenie_SetSleepPeriod(u32SleepPeriodMs);
		/* Go to sleep */
		eStatus = eJenie_Sleep(bMemHold);
		/* Network debugging */
		#if NETWORK_DEBUG
			if (bUartUp)
			{
				vPrintf("eJenie_Sleep() = %s\n",
						aszStatusCode[eStatus]);
			}
		#endif
	}

	return eStatus;
}

/****************************************************************************
 *
 * NAME	 		eNetwork_SetPermitJoin
 */
/*!
 *\DESCRIPTION	Permits joining of network.
 *
 * This function is a simple wrapper for eJenie_SetPermitJoin to provide
 * debugging.
 */
/* RETURNS:
 * None
 *
 ****************************************************************************/
PUBLIC teJenieStatusCode eNetwork_SetPermitJoin	(
	bool_t bPermitJoin) 			/**< Permit join flag */
{
	teJenieStatusCode  eStatus = E_JENIE_ERR_INVLD_PARAM;

	/* Call Jenie function */
	eStatus = eJenie_SetPermitJoin(bPermitJoin);
	/* Network debugging */
	#if NETWORK_DEBUG
		if (bUartUp)
		{
			vPrintf("eJenie_SetPermitJoin(%d) = %s\n",
					bPermitJoin,
					aszStatusCode[eStatus]);
		}
	#endif

	return eStatus;
}

/****************************************************************************
 *
 * NAME	 		eNetwork_SetPermitJoin
 */
/*!
 *\DESCRIPTION	Permits joining of network.
 *
 * This function is a simple wrapper for eJenie_SetPermitJoin to provide
 * debugging.
 */
/* RETURNS:
 * None
 *
 ****************************************************************************/
PUBLIC void vNetwork_PollPeriod	(
	uint16 u16Period) 			/**< New poll period */
{
	/* End device and the new poll period different to the current poll period ? */
	if (eDeviceType == E_JENIE_END_DEVICE && u16PollPeriod != u16Period)

⌨️ 快捷键说明

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