📄 network.c
字号:
/****************************************************************************//*! *\MODULE JN-AN-1072 Jenie Wireless Keyboard * *\COMPONENT $RCSfile: Network.c,v $ * *\VERSION $Name: $ * *\REVISION $Revision: 1.2 $ * *\DATED $Date: 2007/11/22 08:33:21 $ * *\STATUS $State: Exp $ * *\AUTHOR Martin Looker * *\DESCRIPTION Network common tasks handling. * * This file provides code that is common to most Jenie applications for all * node types. The first set of * functions are equivalent to the standard Jenie callback functions and should * be called by those functions from the node type specific files, (Coordinator.c, * Router.c, EndDevice.c). * * Functions are included that will add to the list of services that are to * be registered, (for receiving data), and requested, (for sending data). * When services are added in this way code is included to register and request * the services, handling the Jenie stack operations that make this possible. * * The registered services, which receive data, are registered as soon as the * application starts. It is possible to specify the maximum number of requesting * services that can bind to a registered service, once the maximum number of bindings * are in place the template will not allow any further bindings. * * The requested services, which transmit data, are requested as soon as the * application starts. It is possible to specify a minimum number of bindings for a * requested service. The template will continously attempt to request services until * the minimum number of bindings are in place. * * Functions are also included to transmit data to a requested service and to * receive data for a registered service. *//*\CHANGE HISTORY * * $Log: Network.c,v $ * Revision 1.2 2007/11/22 08:33:21 mlook * JPI updates * Project and makefile renaming * * Revision 1.1 2007/11/14 10:18:23 mlook * Initial checkin * * * *\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 ***//****************************************************************************//* Jennic include files */#include <jendefs.h>#include <jenie.h>#include <JPI.h>#include <AppHardwareApi.h>#include <LedControl.h>#include <Printf.h>/* Standard library include files */#include <string.h>/* Debugging include files */#include <gdb.h>/* Local include files */#include "Network.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; /**< Services to be added */ uint32 u32Pend; /**< Services pending a response */ uint32 u32Ready; /**< Services ready, (registered services or newly bound requested services). */ uint32 u32Bound; /**< Services bound */ uint8 au8BindLimit[32]; /**< Services binding limits */ uint8 au8BindCount[32]; /**< Services binding counts */} tsServices;/****************************************************************************//*** Local Function Prototypes ***//****************************************************************************/PRIVATE void vNetwork_Service_State (teServiceState);PRIVATE void vNetwork_Service_Timeout (void);PRIVATE void vNetwork_Services_RegisterStart (void);PRIVATE void vNetwork_Services_RequestStart (void);PRIVATE teJenieStatusCode eNetwork_Service_BindTx (char, uint8, uint64);PRIVATE void vNetwork_Service_BindRx (uint64, uint16, uint8 *);PRIVATE teJenieStatusCode eNetwork_Service_Bind (uint8, uint64);#if NETWORK_DEBUGPRIVATE bool_t bNetwork_IsString (uint16, uint8 *);#endif/****************************************************************************//*** 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 bool_t bUartUp; /**< Uart is open */PRIVATE teServiceState eServiceState; /**< Service state */PRIVATE uint8 u8ServiceStateTimer; /**< 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 u8ServiceRxTimer; /**< Service receive timer */PRIVATE uint8 u8ServiceTxTimer; /**< Service transmit timer *//****************************************************************************//*** Local Constants ***//****************************************************************************//* Timeouts in 100ms units */PRIVATE const uint8 au8ServiceStateTimeout[] = {0, 10, 30};/* Debugging strings */#if NETWORK_DEBUGPRIVATE const char aszDeviceType[][20] = { "COORDINATOR", "ROUTER", "END_DEVICE"};PRIVATE const char aszServiceState[][20] = { "IDLE", "REGISTER", "REQUEST"};PRIVATE const char aszEventType[][20] = { "REG_SVC_RSP", "SVC_REQ_RSP", "POLL_CMPLT", "PACKET_SENT", "PACKET_FAILED", "NETWORK_UP", "CHILD_JOINED", "DATA", "DATA_TO_SERVICE", "DATA_ACK", "DATA_TO_SERVICE_ACK", "STACK_RESET"};PRIVATE const char aszStatusCode[][20] = { "SUCCESS", "DEFERRED", "ERR_UNKNOWN", "ERR_INVLD_PARAM", "ERR_STACK_RSRC", "ERR_STACK_BUSY"};#endif/****************************************************************************//*** Public network functions ***//****************************************************************************//**************************************************************************** * * NAME vNetwork_ConfigureNetwork *//*! *\DESCRIPTION Configures the network before the stack is initialised. * * This function performs network configuration before the stack is initialised. * * This function should be called from vJenie_CbConfigureNetwork() as it performs * the network configuration tasks common to all Jenie device types. * * This function is only called during a cold start. *//* RETURNS * None * ****************************************************************************/PUBLIC void vNetwork_ConfigureNetwork ( teJenieDeviceType eConfigDeviceType, /**< Type of device to run as */ uint8 u8ConfigMaxChildren, /**< Maximum child devices allowed */ bool_t bInitFfd) /**< Initialise full function devices flag */{ /* Debug hooks: include these regardless of whether debugging or not */ HAL_GDB_INIT(); HAL_BREAKPOINT(); /* Initialise and turn on LEDs */ if (bInitFfd) { vLedInitFfd(); vLedControl(2,FALSE); vLedControl(3,FALSE); } else { vLedInitRfd(); } vLedControl(0,TRUE); vLedControl(1,TRUE); /* Set network identification parameters */ gJenie_NetworkApplicationID = NETWORK_APPLICATION_ID; gJenie_PanID = NETWORK_PAN_ID; gJenie_Channel = NETWORK_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; bUartUp = FALSE; u8ServiceRxTimer = 0; u8ServiceTxTimer = 0; /* No services set up yet */ memset(&sRegister, 0, sizeof(sRegister)); memset(&sRequest, 0, sizeof(sRequest)); /* Service state is idle */ vNetwork_Service_State(E_SERVICE_STATE_IDLE);}/**************************************************************************** * * NAME vNetwork_Init *//*! *\DESCRIPTION Initialise the network and start the stack. * * Allows initialisation to take place and then starts the stack. * * This function should be called from vJenie_CbInit() as it performs * the network initialisation tasks common to all Jenie device types. * * This function called during both a cold and warm start. *//* RETURNS * None * ****************************************************************************/PUBLIC void vNetwork_Init ( bool_t bWarmStart, /**< Specifies if a warm start is taking place */ bool_t bInitUart) /**< Specifies if the UART should be opened for printf support */{ bool_t bUart; teJenieStatusCode eStatus; /* Note passed in uart setting */ bUart = bInitUart; /* Using GDB ? */ #ifdef GDB /* Override passed in UART setting to turn it off - reserved for GDB */ bUart = FALSE; #else /* Want to run network debugging ? */ #if NETWORK_DEBUG /* Override passed in UART setting to turn it on - needed for debugging */ bUart = TRUE; #endif #endif /* Uart not yet up */ bUartUp = FALSE; /* Want to open UART ? */ if (bUart) { /* Open UART */ vUART_printInit(); /* Note it is open */ bUartUp = TRUE; } /* Enable wake timer 1 with interrupt */ vJPI_WakeTimerEnable(E_JPI_WAKE_TIMER_1, TRUE); /* Run the timer for 100ms */ vJPI_WakeTimerStart(E_JPI_WAKE_TIMER_1, 3200); /* Start the stack running for our device type */ eStatus = eJenie_Start(eDeviceType); #if NETWORK_DEBUG if (bUartUp) { if (! bWarmStart) { vPrintf("\n------------------------------\n"); vPrintf("Version(%d)\n", NETWORK_VERSION); vPrintf("ApplicationId(%x)\n", gJenie_NetworkApplicationID); vPrintf("PanId(%x)\n", gJenie_PanID); vPrintf("Channel(%d)\n", gJenie_Channel); vPrintf("MaxChildren(%d)\n", gJenie_MaxChildren); vPrintf("DeviceType(%s)\n", aszDeviceType[eDeviceType]); } vPrintf("vNetwork_Init(%d, %d)\n", bWarmStart, bInitUart); vPrintf("eJenie_Start(%s) = %s\n", aszDeviceType[eDeviceType], aszStatusCode[eStatus]); } #endif}/**************************************************************************** * * NAME: vNetwork_Main *//*! *\DESCRIPTION Main application task, called repeatedly by the stack. * * This function should be called from vJenie_CbMain() as it performs * the network tasks common to all Jenie device types. * * 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 without a button press ? */ else if (sRequest.u32Add != 0) { /* Attempt to request non-button services */ vNetwork_Services_RequestStart(); } /* Have new services just been bound ? */ if (sRequest.u32Ready) { /* Clear newly bound services flag */ sRequest.u32Ready = 0; } } /* 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); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -