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

📄 network.c

📁 通过5120芯片实现无线UART进行数据的传输
💻 C
📖 第 1 页 / 共 2 页
字号:
								     void             *pvEventPrim)
{
	/* Which event has occurred ? */
	switch (eEventType)
	{
	/* Network up ? */
	case E_JENIE_DATA_TO_SERVICE:
		{
			/* Get pointer to correct primitive structure */
			tsDataToService *psDataToService = (tsDataToService *) pvEventPrim;

			/* Receive the data */
			(void) bNetwork_Service_Rx(psDataToService->u64SrcAddress,
									   psDataToService->u8SrcService,
									   psDataToService->u16Length,
									   psDataToService->pau8Data);
			}
		break;
	/* Others ? */
	default:
		break;
	}
}

/****************************************************************************
 *
 * NAME: vNetwork_HwEvent
 *
 * DESCRIPTION:
 * Called when a stack hardware event has occurred.
 *
 * RETURNS:
 * None
 *
 ****************************************************************************/
PUBLIC void vNetwork_HwEvent (uint32 u32DeviceId,
							  uint32 u32ItemBitmap)
{
	teJenieStatusCode eStatus;

	/* Tick timer event ? */
	if (u32DeviceId == E_AHI_DEVICE_TIMER0)
	{
		/* Transmit any outstanding data over the radio */
		if (bNetwork_Service_RequestUp(WUART_SERVICE)) vWuart_TxData();

		/* End Device and network is up ? */
		if (eDeviceType == E_JENIE_END_DEVICE && bNetworkUp)
		{
			/* Poll parent for data */
			eStatus = eJenie_PollParent();
		}
		/* Timer running ? */
		if (u16ServiceStateTimer > 0)
		{
			/* Decrement timer */
			u16ServiceStateTimer--;
			/* Timer expired ? */
			if (u16ServiceStateTimer == 0)
			{
				/* Handle the timeout */
				vNetwork_Service_Timeout();
			}
		}
		/* Receive LED timer running ? */
		if (u8ServiceRx > 0)
		{
			u8ServiceRx--;
			/* Expired - turn out LED */
			if (u8ServiceRx == 0 && bRegisterUp) vLedControl(0, FALSE);
			/* Running - turn on */
			else vLedControl(0, TRUE);
		}
		/* Transmit LED timer running ? */
		if (u8ServiceTx > 0)
		{
			u8ServiceTx--;
			/* Expired - turn out LED */
			if (u8ServiceTx == 0 && bRequestUp) vLedControl(1, FALSE);
			/* Running - turn on */
			else vLedControl(1, TRUE);
		}
	}
}

/****************************************************************************/
/***        Public service functions                                      ***/
/****************************************************************************/

/****************************************************************************
 *
 * NAME: vNetwork_Service_Register
 *
 * DESCRIPTION:
 * Add a service to be registered.
 *
 * RETURNS:
 * None
 *
 ****************************************************************************/
PUBLIC void vNetwork_Service_Register (uint8 u8Service)
{
	/* Attempt to add services for registration */
	if (bNetwork_Services_Add(&sRegister, u32Network_Service_Mask(u8Service)))
	{
		bRegisterUp = FALSE;
		vLedControl(0, TRUE);
	}
}

/****************************************************************************
 *
 * NAME: vNetwork_Service_Request
 *
 * DESCRIPTION:
 * Add a service to be requested and bound.
 *
 * RETURNS:
 * None
 *
 ****************************************************************************/
PUBLIC void vNetwork_Service_Request (uint8 u8Service)
{
	/* Attempt to add services for registration */
	if (bNetwork_Services_Add(&sRequest, u32Network_Service_Mask(u8Service)))
	{
		bRequestUp = FALSE;
		vLedControl(1, TRUE);
	}
}

/****************************************************************************
 *
 * NAME: vNetwork_Service_Tx
 *
 * DESCRIPTION:
 * Transmit data to a service.
 *
 * RETURNS:
 * None
 *
 ****************************************************************************/
PUBLIC bool_t 	bNetwork_Service_Tx	(uint8 u8Service, uint16 u16Length, uint8 *pu8Data)
{
	bool_t 			  bLive = FALSE;
	teJenieStatusCode eStatus = E_JENIE_ERR_INVLD_PARAM;
	uint32			 u32Service;

	/* Get mask for service */
	u32Service = u32Network_Service_Mask(u8Service);

	/* Is this service live ? */
	if (u32Service & sRequest.u32Live)
	{
		/* Note we're live */
		bLive = TRUE;
		/* Set transmit timer - used to flash LED */
		u8ServiceTx = 2;
		/* Send the data */
		eStatus = eJenie_SendDataToBoundService(u8Service, pu8Data, u16Length, TXOPTION_ACKREQ);
	}

	return bLive;
}

/****************************************************************************
 *
 * NAME: vNetwork_Service_Rx
 *
 * DESCRIPTION:
 * Receive data for a service.
 *
 * RETURNS:
 * None
 *
 ****************************************************************************/
PUBLIC bool_t 	bNetwork_Service_Rx	(uint64 u64Address, uint8 u8Service, uint16 u16Length, uint8 *pu8Data)
{
	bool_t 			  bLive = FALSE;
	uint32			 u32Service;

	/* Get mask for service */
	u32Service = u32Network_Service_Mask(u8Service);

	/* Is this service live ? */
	if (u32Service & sRegister.u32Live)
	{
		/* Note we're live */
		bLive = TRUE;
		/* Set receive timer - used to flash LED */
		u8ServiceRx = 2;

		/* Pass data to wuart for handling */
		vWuart_RxData(u16Length, pu8Data);
	}

	return bLive;
}

/****************************************************************************
 *
 * NAME: vNetwork_Service_Mask
 *
 * DESCRIPTION:
 * Calculate bitmask for a service.
 *
 * RETURNS:
 * None
 *
 ****************************************************************************/
PUBLIC uint32 u32Network_Service_Mask (uint8 u8Service)
{
	uint32 u32Service = 0;

	if (u8Service >= 1 && u8Service <= 32)
	{
		u32Service = ((uint32) 1 << (u8Service - 1));
	}

	return u32Service;
}

/****************************************************************************
 *
 * NAME: vNetwork_Service_RegisterUp
 *
 * DESCRIPTION:
 * Checks whether a registered service is up and running.
 *
 * RETURNS:
 * None
 *
 ****************************************************************************/
PUBLIC bool_t   bNetwork_Service_RegisterUp (uint8 u8Service)
{
	return (sRegister.u32Live & u32Network_Service_Mask(u8Service) ? TRUE : FALSE);
}

/****************************************************************************
 *
 * NAME: vNetwork_Service_RequestUp
 *
 * DESCRIPTION:
 * Checks whether a requested service is up and running.
 *
 * RETURNS:
 * None
 *
 ****************************************************************************/
PUBLIC bool_t   bNetwork_Service_RequestUp (uint8 u8Service)
{
	return (sRequest.u32Live & u32Network_Service_Mask(u8Service) ? TRUE : FALSE);
}

/****************************************************************************/
/***        Public data access functions                                  ***/
/****************************************************************************/

PUBLIC bool_t 	bNetwork_Up 		 		 (void) { return  bNetworkUp;  }
PUBLIC bool_t 	bNetwork_Services_Up 		 (void) { return (bRegisterUp & bRequestUp); }
PUBLIC bool_t 	bNetwork_Services_RegisterUp (void) { return  bRegisterUp; }
PUBLIC bool_t 	bNetwork_Services_RequestUp  (void) { return  bRequestUp; }
PUBLIC teJenieDeviceType eNetwork_DeviceType (void) { return  eDeviceType; }

/****************************************************************************/
/***        Private service functions                                     ***/
/****************************************************************************/

/****************************************************************************
 *
 * NAME: vNetwork_Service_State
 *
 * DESCRIPTION:
 * Update service state machine and timer.
 *
 * RETURNS:
 * None
 *
 ****************************************************************************/
PRIVATE void vNetwork_Service_State (teServiceState eState)
{
	/* Service state changed ? */
	if (eServiceState != eState)
	{
		/* Note the new service state */
		eServiceState = eState;

		/* Set the service state timer */
		u16ServiceStateTimer = au16ServiceStateTimeout[eServiceState];
	}
}

/****************************************************************************
 *
 * NAME: vNetwork_Service_Timeout
 *
 * DESCRIPTION:
 * Handle a service state timeout.
 *
 * RETURNS:
 * None
 *
 ****************************************************************************/
PRIVATE void vNetwork_Service_Timeout (void)
{
	/* Are we waiting for a register response ? */
	if (eServiceState == E_SERVICE_STATE_REGISTER)
	{
		/* Try to register the pending service again */
		sRegister.u32Add |= sRegister.u32Pend;
		sRegister.u32Pend  = 0;
	}
	/* Are we waiting for a request response ? */
	else if (eServiceState == E_SERVICE_STATE_REQUEST)
	{
		/* Try to register the pending service again */
		sRequest.u32Add  |= sRequest.u32Pend;
		sRequest.u32Pend  = 0;
	}

	/* Return to idle state */
	vNetwork_Service_State(E_SERVICE_STATE_IDLE);
}

/****************************************************************************
 *
 * NAME: vNetwork_Services_Add
 *
 * DESCRIPTION:
 * Set the bitmask to add a new service, only if not already in progress
 *
 * RETURNS:
 * None
 *
 ****************************************************************************/
PRIVATE bool_t bNetwork_Services_Add (tsServices *psServices, uint32 u32Services)
{
	bool_t   bAdded = FALSE;
	uint32 u32Mask;

	/* Get mask for registered services we don't already have in progress */
	u32Mask = ~(psServices->u32Add | psServices->u32Pend | psServices->u32Live);

	/* Prepare to add any services we don't have already */
	psServices->u32Add |= (u32Services & u32Mask);

	/* Got services to add ? */
	if (psServices->u32Add != 0)
	{
		bAdded = TRUE;
	}

	return bAdded;
}

/****************************************************************************
 *
 * NAME: vNetwork_Services_RegisterStart
 *
 * DESCRIPTION:
 * Starts registering services waiting to be added.
 *
 * RETURNS:
 * None
 *
 ****************************************************************************/
PRIVATE void vNetwork_Services_RegisterStart (void)
{
	teJenieStatusCode eStatus;

	/* Check we are idle and have services to register */
	if (eServiceState    == E_SERVICE_STATE_IDLE &&
		sRegister.u32Add != 0)
	{
		/* Note the services we are going to try to register */
		sRegister.u32Pend = sRegister.u32Add;
		sRegister.u32Add  = 0;

		/* Attempt to register services */
		eStatus = eJenie_RegisterServices(sRegister.u32Pend);

		/* Success ? */
		if (eStatus == E_JENIE_SUCCESS)
		{
			/* Note the services we registered */
			sRegister.u32Live |= sRegister.u32Pend;
			sRegister.u32Pend  = 0;
		}
		else
		{
			/* Wait for register service response next */
			vNetwork_Service_State(E_SERVICE_STATE_REGISTER);
		}
	}
}

/****************************************************************************
 *
 * NAME: vNetwork_Services_RequestStart
 *
 * DESCRIPTION:
 * Starts requesting services waiting to be added.
 *
 * RETURNS:
 * None
 *
 ****************************************************************************/
PRIVATE void vNetwork_Services_RequestStart (void)
{
	teJenieStatusCode eStatus;

	/* Check we are idle and have services to request */
	if (eServiceState    == E_SERVICE_STATE_IDLE &&
		sRequest.u32Add != 0)
	{
		/* Note the services we are going to try to request */
		sRequest.u32Pend = sRequest.u32Add;
		sRequest.u32Add  = 0;

		/* Attempt to request services */
		eStatus = eJenie_RequestServices(sRequest.u32Pend, FALSE);

		/* Wait for request service response next */
		vNetwork_Service_State(E_SERVICE_STATE_REQUEST);
	}
}

/****************************************************************************/
/***        END OF FILE                                                   ***/
/****************************************************************************/

⌨️ 快捷键说明

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