📄 zdapp.c
字号:
/**************************************************************************************************
Filename: ZDApp.c
Revised: $Date: 2007-11-28 15:44:45 -0800 (Wed, 28 Nov 2007) $
Revision: $Revision: 15983 $
Description: This file contains the interface to the Zigbee Device Application. This is the
Application part that the use can change. This also contains the Task functions.
Copyright 2004-2007 Texas Instruments Incorporated. All rights reserved.
IMPORTANT: Your use of this Software is limited to those specific rights
granted under the terms of a software license agreement between the user
who downloaded the software, his/her employer (which must be your employer)
and Texas Instruments Incorporated (the "License"). You may not use this
Software unless you agree to abide by the terms of the License. The License
limits your use, and you acknowledge, that the Software may not be modified,
copied or distributed unless embedded on a Texas Instruments microcontroller
or used solely and exclusively in conjunction with a Texas Instruments radio
frequency transceiver, which is integrated into your product. Other than for
the foregoing purpose, you may not use, reproduce, copy, prepare derivative
works of, modify, distribute, perform, display or sell this Software and/or
its documentation for any purpose.
YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
PROVIDED 揂S IS?WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
(INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
Should you have any questions regarding your right to use this Software,
contact Texas Instruments Incorporated at www.TI.com.
**************************************************************************************************/
/*********************************************************************
* INCLUDES
*/
#include "ZComDef.h"
#include "ZMac.h"
#include "OSAL.h"
#include "OSAL_Tasks.h"
#include "OSAL_PwrMgr.h"
#include "OSAL_Nv.h"
#include "AF.h"
#include "APSMEDE.h"
#include "NLMEDE.h"
#include "AddrMgr.h"
#include "ZDProfile.h"
#include "ZDObject.h"
#include "ZDConfig.h"
#include "ZDSecMgr.h"
#include "ZDApp.h"
#include "DebugTrace.h"
#include "nwk_util.h"
#include "OnBoard.h"
#include "ZGlobals.h"
#if ( SECURE != 0 )
#include "ssp.h"
#endif
#if defined( MT_ZDO_FUNC )
#include "MT_ZDO.h"
#endif
/* HAL */
#include "hal_led.h"
//#include "hal_lcd.h"
#if defined( LCD_SUPPORTED )
#include "lcd128_64.h"
#endif
#include "hal_key.h"
#if defined( MT_MAC_FUNC ) || defined( MT_MAC_CB_FUNC )
#error "ERROR! MT_MAC functionalities should be disabled on ZDO devices"
#endif
/*********************************************************************
* MACROS
*/
/*********************************************************************
* CONSTANTS
*/
#if !defined( NWK_START_DELAY )
#define NWK_START_DELAY 100 // in milliseconds
#endif
#if !defined( EXTENDED_JOINING_RANDOM_MASK )
#define EXTENDED_JOINING_RANDOM_MASK 0x007F
#endif
#if !defined( BEACON_REQUEST_DELAY )
#define BEACON_REQUEST_DELAY 100 // in milliseconds
#endif
#if !defined( BEACON_REQ_DELAY_MASK )
#define BEACON_REQ_DELAY_MASK 0x007F
#endif
#if defined (AUTO_SOFT_START)
#define MAX_RESUME_RETRY 3
#define NUM_DISC_ATTEMPTS 3
#else
#define MAX_RESUME_RETRY 1
#endif
#define MAX_DEVICE_UNAUTH_TIMEOUT 5000 // 5 seconds
// Beacon Order Settings (see NLMEDE.h)
#define DEFAULT_BEACON_ORDER BEACON_ORDER_NO_BEACONS
#define DEFAULT_SUPERFRAME_ORDER DEFAULT_BEACON_ORDER
#if ( SECURE != 0 )
#if !defined( MAX_NWK_FRAMECOUNTER_CHANGES )
// The number of times the frame counter can change before
// saving to NV
#define MAX_NWK_FRAMECOUNTER_CHANGES 1000
#endif
#endif
// Leave control bits
#define ZDAPP_LEAVE_CTRL_INIT 0
#define ZDAPP_LEAVE_CTRL_SET 1
#define ZDAPP_LEAVE_CTRL_RA 2
// Standard time to update NWK NV data
#define ZDAPP_UPDATE_NWK_NV_TIME 700
// Address Manager Stub Implementation
#define ZDApp_NwkWriteNVRequest AddrMgrWriteNVRequest
/*********************************************************************
* TYPEDEFS
*/
/*********************************************************************
* GLOBAL VARIABLES
*/
#if defined( LCD_SUPPORTED )
byte MatchRsps = 0;
#endif
byte zdoDiscCounter = 1;
zAddrType_t ZDAppNwkAddr;
#if defined ( ZDO_MGMT_NWKDISC_RESPONSE )
byte zdappMgmtNwkDiscRspTransSeq;
byte zdappMgmtNwkDiscReqInProgress = FALSE;
zAddrType_t zdappMgmtNwkDiscRspAddr;
byte zdappMgmtNwkDiscStartIndex;
byte zdappMgmtSavedNwkState;
#endif
#if ( SECURE != 0 )
uint16 nwkFrameCounterChanges = 0;
#endif
#if defined ( SOFT_START )
static uint8 softStartAllowCoord = TRUE;
#endif
uint8 continueJoining = TRUE;
byte _tmpRejoinState;
/*********************************************************************
* EXTERNAL VARIABLES
*/
/*********************************************************************
* EXTERNAL FUNCTIONS
*/
/*********************************************************************
* LOCAL FUNCTIONS
*/
void ZDApp_NetworkStartEvt( void );
void ZDApp_DeviceAuthEvt( void );
void ZDApp_SaveNetworkStateEvt( void );
uint8 ZDApp_ReadNetworkRestoreState( void );
uint8 ZDApp_RestoreNetworkState( void );
void ZDAppDetermineDeviceType( void );
void ZDApp_InitUserDesc( void );
void ZDAppCheckForHoldKey( void );
void ZDApp_ProcessOSALMsg( osal_event_hdr_t *msgPtr );
void ZDApp_ProcessNetworkJoin( void );
void ZDApp_SetCoordAddress( byte endPoint, byte dstEP );
#if ( SECURE != 0 )
void ZDApp_SaveNwkKey( void );
byte ZDApp_RestoreNwkKey( void );
#endif
void ZDApp_SendMsg( byte taskID, byte cmd, byte len, byte *buf );
void ZDApp_ResetTimerStart( uint16 delay );
void ZDApp_ResetTimerCancel( void );
void ZDApp_LeaveCtrlInit( void );
void ZDApp_LeaveCtrlSet( uint8 ra );
uint8 ZDApp_LeaveCtrlBypass( void );
void ZDApp_LeaveCtrlStartup( devStates_t* state, uint16* startDelay );
void ZDApp_LeaveReset( uint8 ra );
void ZDApp_LeaveUpdate( uint16 nwkAddr, uint8* extAddr,
uint8 removeChildren );
void ZDApp_NodeProfileSync( ZDO_NetworkDiscoveryCfm_t* cfm );
void ZDApp_ProcessMsgCBs( zdoIncomingMsg_t *inMsg );
void ZDApp_RegisterCBs( void );
void ZDApp_SecInit( uint8 state );
/*********************************************************************
* LOCAL VARIABLES
*/
byte ZDAppTaskID;
byte nwkStatus;
endPointDesc_t *ZDApp_AutoFindMode_epDesc = (endPointDesc_t *)NULL;
uint8 ZDApp_LeaveCtrl;
#if defined( HOLD_AUTO_START )
devStates_t devState = DEV_HOLD;
#else
devStates_t devState = DEV_INIT;
#endif
#if defined( ZDO_COORDINATOR ) && !defined( SOFT_START )
// Set the default to coodinator
devStartModes_t devStartMode = MODE_HARD;
#else
devStartModes_t devStartMode = MODE_JOIN; // Assume joining
//devStartModes_t devStartMode = MODE_RESUME; // if already "directly joined"
// to parent. Set to make the device do an Orphan scan.
#endif
#if !defined( ZDO_COORDINATOR ) || defined( SOFT_START )
static uint8 retryCnt;
#endif
endPointDesc_t ZDApp_epDesc =
{
ZDO_EP,
&ZDAppTaskID,
(SimpleDescriptionFormat_t *)NULL, // No Simple description for ZDO
(afNetworkLatencyReq_t)0 // No Network Latency req
};
/*********************************************************************
* @fn ZDApp_Init
*
* @brief ZDApp Initialization function.
*
* @param task_id - ZDApp Task ID
*
* @return None
*/
void ZDApp_Init( byte task_id )
{
uint8 capabilities;
// Save the task ID
ZDAppTaskID = task_id;
// Initialize the ZDO global device short address storage
ZDAppNwkAddr.addrMode = Addr16Bit;
ZDAppNwkAddr.addr.shortAddr = INVALID_NODE_ADDR;
(void)NLME_GetExtAddr(); // Load the saveExtAddr pointer.
// Check for manual "Hold Auto Start"
ZDAppCheckForHoldKey();
// Initialize ZDO items and setup the device - type of device to create.
ZDO_Init();
// Register the endpoint description with the AF
// This task doesn't have a Simple description, but we still need
// to register the endpoint.
afRegister( (endPointDesc_t *)&ZDApp_epDesc );
#if defined( ZDO_USERDESC_RESPONSE )
ZDApp_InitUserDesc();
#endif // ZDO_USERDESC_RESPONSE
// set broadcast address mask to support broadcast filtering
NLME_GetRequest(nwkCapabilityInfo, 0, &capabilities);
NLME_SetBroadcastFilter( capabilities );
// Start the device?
if ( devState != DEV_HOLD )
{
ZDOInitDevice( 0 );
}
else
{
// Blink LED to indicate HOLD_START
HalLedBlink ( HAL_LED_4, 0, 50, 500 );
}
ZDApp_RegisterCBs();
} /* ZDO_Init() */
/*********************************************************************
* @fn ZDApp_SecInit
*
* @brief ZDApp initialize security.
*
* @param state - device initialization state
*
* @return none
*/
void ZDApp_SecInit( uint8 state )
{
// Initialize ZigBee Device Security Manager
ZDSecMgrInit();
// -----------------------------------------------------------------
#if ( SECURE != 0 )
// -----------------------------------------------------------------
nwkFrameCounter = 0;
if( _NIB.nwkKeyLoaded == FALSE )
{
// ---------------------------------------------------------------
#if ( SECURITY_MODE == SECURITY_RESIDENTIAL )
// ---------------------------------------------------------------
#if !defined (ZDO_COORDINATOR)
if ( zgPreConfigKeys )
#endif
{
SSP_UpdateNwkKey( (byte*)zgPreConfigKey, 0);
SSP_SwitchNwkKey( 0 );
}
// ---------------------------------------------------------------
#endif // ( SECURITY_MODE == SECURITY_RESIDENTIAL )
// ---------------------------------------------------------------
}
if ( state == ZDO_INITDEV_RESTORED_NETWORK_STATE )
{
//Restore NWK frame counter
}
// -----------------------------------------------------------------
#endif
// -----------------------------------------------------------------
}
/*********************************************************************
* @fn ZDApp_event_loop()
*
* @brief Main event loop for Zigbee device objects task. This function
* should be called at periodic intervals.
*
* @param task_id - Task ID
* @param events - Bitmap of events
*
* @return none
*/
UINT16 ZDApp_event_loop( byte task_id, UINT16 events )
{
uint8 *msg_ptr;
if ( events & SYS_EVENT_MSG )
{
while ( (msg_ptr = osal_msg_receive( ZDAppTaskID )) )
{
ZDApp_ProcessOSALMsg( (osal_event_hdr_t *)msg_ptr );
// Release the memory
osal_msg_deallocate( msg_ptr );
}
// Return unprocessed events
return (events ^ SYS_EVENT_MSG);
}
if ( events & ZDO_STATE_CHANGE_EVT )
{
ZDO_UpdateNwkStatus( devState );
// Return unprocessed events
return (events ^ ZDO_STATE_CHANGE_EVT);
}
if ( events & ZDO_NETWORK_INIT )
{
// Initialize apps and start the network
devState = DEV_INIT;
ZDO_StartDevice( (uint8)ZDO_Config_Node_Descriptor.LogicalType, devStartMode,
DEFAULT_BEACON_ORDER, DEFAULT_SUPERFRAME_ORDER );
// Return unprocessed events
return (events ^ ZDO_NETWORK_INIT);
}
#if defined (RTR_NWK)
if ( events & ZDO_NETWORK_START )
{
ZDApp_NetworkStartEvt();
// Return unprocessed events
return (events ^ ZDO_NETWORK_START);
}
#endif //RTR_NWK
#if defined ( RTR_NWK )
if ( events & ZDO_ROUTER_START )
{
if ( nwkStatus == ZSuccess )
{
if ( devState == DEV_END_DEVICE )
devState = DEV_ROUTER;
osal_pwrmgr_device( PWRMGR_ALWAYS_ON );
}
else
{
// remain as end device!!
}
osal_set_event( ZDAppTaskID, ZDO_STATE_CHANGE_EVT );
// Return unprocessed events
return (events ^ ZDO_ROUTER_START);
}
#endif // RTR
if ( events & ZDO_COMMAND_CNF )
{
// User defined logic
// Return unprocessed events
return (events ^ ZDO_COMMAND_CNF);
}
#if defined( ZDSECMGR_SECURE ) && defined( RTR_NWK )
if ( events & ZDO_NEW_DEVICE )
{
// process the new device event
if ( ZDSecMgrNewDeviceEvent() == TRUE )
{
osal_start_timerEx( ZDAppTaskID, ZDO_NEW_DEVICE, 1000 );
}
// Return unprocessed events
return (events ^ ZDO_NEW_DEVICE);
}
#endif // ZDSECMGR_SECURE && RTR
#if defined ( ZDSECMGR_COMMERCIAL )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -