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

📄 network.c

📁 通过5120芯片实现无线UART进行数据的传输
💻 C
📖 第 1 页 / 共 2 页
字号:
/****************************************************************************
 *
 * MODULE              Jenie Wireless UART
 *
 * COMPONENT           $RCSfile: Network.c,v $
 *
 * VERSION             $Name:  $
 *
 * REVISION            $Revision: 1.1 $
 *
 * DATED               $Date: 2007/11/02 12:32:41 $
 *
 * STATUS              $State: Exp $
 *
 * AUTHOR              Martin Looker
 *
 * DESCRIPTION         Jenie Wireless UART - Network.
 *
 * CHANGE HISTORY
 *
 * $Log: Network.c,v $
 * Revision 1.1  2007/11/02 12:32:41  mlook
 * Adding new application notes
 *
 *
 *
 * LAST MODIFIED BY    $Author: mlook $
 *                     $Modtime: $
 *
 ****************************************************************************
 *
 * This software is owned by Jennic and/or its supplier and is protected
 * under applicable copyright laws. All rights are reserved. We grant You,
 * and any third parties, a license to use this software solely and
 * exclusively on Jennic products. You, and any third parties must reproduce
 * the copyright and warranty notice and any other legend of ownership on each
 * copy or partial copy of the software.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS". JENNIC MAKES NO WARRANTIES, WHETHER
 * EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE,
 * ACCURACY OR LACK OF NEGLIGENCE. JENNIC SHALL NOT, IN ANY CIRCUMSTANCES,
 * BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, SPECIAL,
 * INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY REASON WHATSOEVER.
 *
 * Copyright Jennic Ltd 2005, 2006, 2007. All rights reserved
 *
 ****************************************************************************/

/****************************************************************************/
/***        Include files                                                 ***/
/****************************************************************************/
#include <jendefs.h>
#include <jenie.h>
#include <LedControl.h>
#include <AppHardwareApi.h>
#include <gdb.h>

#include "Wuart.h"
#include "Network.h"
#include "Serial.h"
#include "SerialQ.h"
#include "Uart.h"

/****************************************************************************/
/***        Macro Definitions                                             ***/
/****************************************************************************/

/****************************************************************************/
/***        Type Definitions                                              ***/
/****************************************************************************/
typedef enum
{
	E_SERVICE_STATE_IDLE	= 0,	/**< Not setting up services  			*/
	E_SERVICE_STATE_REGISTER,		/**< Attempting to register services	*/
	E_SERVICE_STATE_REQUEST,		/**< Attempting to request services		*/
} teServiceState;

typedef struct
{
	uint32 u32Add;				/**< New services to set up */
	uint32 u32Pend;				/**< Services pending a response */
	uint32 u32Live;				/**< Services live */
} tsServices;

/****************************************************************************/
/***        Local Function Prototypes                                     ***/
/****************************************************************************/
PRIVATE void 	vNetwork_Service_State 		 	(teServiceState);
PRIVATE void 	vNetwork_Service_Timeout 		(void);
PRIVATE bool_t 	bNetwork_Services_Add 	 		(tsServices *, uint32);
PRIVATE void 	vNetwork_Services_RegisterStart (void);
PRIVATE void 	vNetwork_Services_RequestStart  (void);

/****************************************************************************/
/***        Exported Variables                                            ***/
/****************************************************************************/

/****************************************************************************/
/***        Local Variables                                               ***/
/****************************************************************************/
PRIVATE teJenieDeviceType	  eDeviceType;		  /**< Device type 			*/
PRIVATE bool_t				  bNetworkUp;		  /**< Network is up 		*/
PRIVATE bool_t				  bRegisterUp;		  /**< Register Services are up */
PRIVATE bool_t				  bRequestUp;		  /**< Request services are up  */
PRIVATE	teServiceState		  eServiceState;	  /**< Service state        */
PRIVATE	uint16				u16ServiceStateTimer; /**< Service timer        */
PRIVATE tsServices			  sRegister;		  /**< Register services    */
PRIVATE tsServices			  sRequest;		  	  /**< Register services    */
PRIVATE uint8				 u8Children;		  /**< Number of children   */
PRIVATE uint8				 u8MaxChildren;		  /**< Maximum children     */
PRIVATE uint8				 u8ServiceRx;		  /**< Service receive timer */
PRIVATE uint8 				 u8ServiceTx;		  /**< Service transmit timer */

/****************************************************************************/
/***        Local Constants                                               ***/
/****************************************************************************/
/* Timeouts in 100ms units */
PRIVATE const uint16 			   au16ServiceStateTimeout[] = {0, 10, 20};

/****************************************************************************/
/***        Public network functions                                      ***/
/****************************************************************************/

/****************************************************************************
 *
 * NAME: vNetwork_ConfigureNetwork
 *
 * DESCRIPTION:
 * Set stack parameters prior to stack initialisation.
 * Initialise items here for cold start only.
 *
 * RETURNS:
 * None
 *
 ****************************************************************************/
PUBLIC void vNetwork_ConfigureNetwork (teJenieDeviceType  eConfigDeviceType,
						   			   uint8 			 u8ConfigMaxChildren)
{
	/* Debug hooks: include these regardless of whether debugging or not */
	HAL_GDB_INIT();
    HAL_BREAKPOINT();

	/* Initialise and turn on LEDs */
	vLedInitRfd();
	vLedControl(0,TRUE);
	vLedControl(1,TRUE);

	/* Set network identification parameters */
	gJenie_NetworkApplicationID  = WUART_APPLICATION_ID;
	gJenie_PanID 			 	 = WUART_PAN_ID;
	gJenie_Channel 			 	 = WUART_CHANNEL;

	/* Note initialisation settings */
	eDeviceType          = eConfigDeviceType;
	u8MaxChildren        = u8ConfigMaxChildren;

	/* Limit maximum children to the absolute maximum allowed (10) */
	if (u8MaxChildren > 10) u8MaxChildren = 10;
	/* Don't allow any children for an end device */
	if (eDeviceType == E_JENIE_END_DEVICE) u8MaxChildren = 0;

	/* Set up maximum children */
	gJenie_MaxChildren = u8MaxChildren;
	u8Children  	   = 0;

	/* Network is not up yet */
	bNetworkUp  = FALSE;
	bRegisterUp = FALSE;
	bRequestUp  = FALSE;
	u8ServiceRx = 0;
	u8ServiceTx = 0;

	/* No services set up yet */
	sRegister.u32Add  = 0;
	sRegister.u32Pend = 0;
	sRegister.u32Live = 0;
	sRequest.u32Add   = 0;
	sRequest.u32Pend  = 0;
	sRequest.u32Live  = 0;

	/* Service state is idle */
	vNetwork_Service_State(E_SERVICE_STATE_IDLE);
}

/****************************************************************************
 *
 * NAME: vNetwork_Init
 *
 * DESCRIPTION:
 * Perform application initialisation after stack has initialised.
 * Initialise items here for a cold and warm start.
 *
 * RETURNS:
 * None
 *
 ****************************************************************************/
PUBLIC void vNetwork_Init (bool_t 			  bWarmStart,
						   bool_t			  bInitUart)
{
	/* Enable timer 0 for 244 pulses per second from internal clock */
    vAHI_TimerEnable(E_AHI_TIMER_0, 16, FALSE, TRUE, FALSE);
    vAHI_TimerClockSelect(E_AHI_TIMER_0, FALSE, TRUE);
    /* Set repeating timer for 10th second (100ms) */
    vAHI_TimerStartRepeat(E_AHI_TIMER_0, 0, 24);
}

/****************************************************************************
 *
 * NAME: vNetwork_Start
 *
 * DESCRIPTION:
 * Start running the stack.
 *
 * RETURNS:
 * None
 *
 ****************************************************************************/
PUBLIC void vNetwork_Start (void)
{
	teJenieStatusCode eStatus;

	/* Start the stack running for our device type */
	eStatus = eJenie_Start(eDeviceType);
}

/****************************************************************************
 *
 * NAME: vNetwork_Main
 *
 * DESCRIPTION:
 * Main application task, called repeatedly by the stack
 * This function should be non-blocking.
 *
 * RETURNS:
 * None
 *
 ****************************************************************************/
PUBLIC void vNetwork_Main (void)
{
	/* Is the network up ? */
	if (bNetworkUp)
	{
		/* Service state is idle ? */
		if (eServiceState == E_SERVICE_STATE_IDLE)
		{
			/* Got services to register ? */
			if (sRegister.u32Add != 0)
			{
				/* Attempt to register services */
				vNetwork_Services_RegisterStart();
			}
			/* Got services to request ? */
			else if (sRequest.u32Add != 0)
			{
				/* Attempt to request services */
				vNetwork_Services_RequestStart();
			}
		}
		/* Registered services not up yet ? */
		if (! bRegisterUp)
		{
			/* Nothing waiting to come up ? */
			if (sRegister.u32Add == 0 && sRegister.u32Pend == 0)
			{
				/* Registered services are up */
				bRegisterUp = TRUE;
				/* Light LED0 */
				vLedControl(0, FALSE);
			}
		}
		/* Requested services not yet up ? */
		if (! bRequestUp)
		{
			/* Nothing waiting to come up ? */
			if (sRequest.u32Add == 0 && sRequest.u32Pend == 0)
			{
				/* Requested services are up */
				bRequestUp = TRUE;
				/* Light LED1 */
				vLedControl(1, FALSE);
			}
		}
	}
}

/****************************************************************************
 *
 * NAME: vNetwork_StackMgmtEvent
 *
 * DESCRIPTION:
 * Called when stack management event has occurred.
 *
 * RETURNS:
 * None
 *
 ****************************************************************************/
PUBLIC void vNetwork_StackMgmtEvent (teEventType   eEventType,
								       void        *pvEventPrim)
{
	teJenieStatusCode eStatus;
	uint8   u8Service;
	uint32 u32Service;

	/* Which event has occurred ? */
	switch (eEventType)
	{

	/* Register Service Response ? */
	case E_JENIE_REG_SVC_RSP:
		{
			/* Are we waiting for a register response ? */
			if (eServiceState == E_SERVICE_STATE_REGISTER)
			{
				/* Note the services we registered */
				sRegister.u32Live |= sRegister.u32Pend;
				sRegister.u32Pend  = 0;
				/* Return to idle state */
				vNetwork_Service_State(E_SERVICE_STATE_IDLE);
			}
		}
		break;

	/* Request Service Response ? */
	case E_JENIE_SVC_REQ_RSP:
		{
			/* Get pointer to correct primitive structure */
			tsSvcReqRsp *psSvcReqRsp = (tsSvcReqRsp *) pvEventPrim;

			/* Are we waiting for a request response ? */
			if (eServiceState == E_SERVICE_STATE_REQUEST)
			{
				/* Loop through individual services */
				for (u8Service = 1; u8Service <= 32; u8Service++)
				{
					/* Get mask for service */
					u32Service = u32Network_Service_Mask(u8Service);
					/* Is this service supported ? */
					if (psSvcReqRsp->u32Services & u32Service)
					{
						/* Attempt to bind to the service */
						eStatus = eJenie_BindService(u8Service,
													 psSvcReqRsp->u64SrcAddress,
													 u8Service);

						/* Bind successful ? */
						if (eStatus == E_JENIE_SUCCESS)
						{
							/* Note the services we registered */
							sRequest.u32Live |=   u32Service;
							sRequest.u32Pend &= (~u32Service);
							#if 0
							/* Send hello message */
							{
								uint8 au8Data[6] = "hello";
								(void) bNetwork_Service_Tx(u8Service, 6, au8Data);
							}
							#endif
						}
					}
				}
				/* No more requested services pending ? */
				if (sRequest.u32Pend == 0)
				{
					/* Return to idle state */
					vNetwork_Service_State(E_SERVICE_STATE_IDLE);
				}
			}
		}
		break;

	/* Poll complete ? */
	case E_JENIE_POLL_CMPLT:
		/* Don't do any debugging - too many of these */
		;
		break;

	/* Network up ? */
	case E_JENIE_NETWORK_UP:
		{
			/* Note network is now up */
			bNetworkUp = TRUE;

			/* End device or we have all the children we want ? */
			if (eDeviceType == E_JENIE_END_DEVICE || u8Children >= u8MaxChildren )
			{
				/* Don't allow devices to join to us */
				eStatus = eJenie_SetPermitJoin(FALSE);
			}
			else
			{
				/* Allow devices to join to us */
				eStatus = eJenie_SetPermitJoin(TRUE);
			}
		}
		break;

	/* Child joined ? */
	case E_JENIE_CHILD_JOINED:
		{
			/* Update number of children */
			u8Children++;

			/* Got all the children we want ? */
			if (u8Children >= u8MaxChildren )
			{
				/* Don't allow devices to join to us */
				eStatus = eJenie_SetPermitJoin(FALSE);
			}
		}
		break;

	/* Others ? */
	default:
		{
		}
		break;
	}
}

/****************************************************************************
 *
 * NAME: vNetwork_StackDataEvent
 *
 * DESCRIPTION:
 * Called when data event has occurred.
 *
 * RETURNS:
 * None
 *
 ****************************************************************************/
PUBLIC void vNetwork_StackDataEvent (teEventType   eEventType,

⌨️ 快捷键说明

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