📄 zdapp.c
字号:
/*********************************************************************
Filename: ZDApp.c
Revised: $Date: 2007-05-31 15:56:04 -0700 (Thu, 31 May 2007) $
Revision: $Revision: 14490 $
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.
Notes:
Copyright (c) 2006 by Texas Instruments, Inc.
All Rights Reserved. Permission to use, reproduce, copy, prepare
derivative works, modify, distribute, perform, display or sell this
software and/or its documentation for any purpose is prohibited
without the express written consent of Texas Instruments, Inc.
*********************************************************************/
/*********************************************************************
* 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 "ZDCache.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"
#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 100
// 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 ZDAppSetupProtoVersion( 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 );
void ZDApp_SendNewDstAddr( byte dstEP, zAddrType_t *dstAddr,
cId_t clusterID, byte removeFlag, byte task_id, byte endpoint );
#if ( SECURE != 0 )
void ZDApp_SaveNwkKey( void );
byte ZDApp_RestoreNwkKey( void );
#endif
void ZDApp_SendMsg( byte taskID, byte cmd, byte len, byte *buf );
#if defined ( ZDO_BIND_UNBIND_RESPONSE ) && !defined ( REFLECTOR )
extern void ZDApp_AppBindReq( byte TransSeq, zAddrType_t *SrcAddr, byte *SrcAddress,
byte SrcEndPoint, cId_t ClusterID, byte *DstAddress,
byte DstEndPoint, byte SecurityUse, uint8 Type );
#endif
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 );
/*********************************************************************
* LOCAL VARIABLES
*/
byte ZDAppTaskID;
byte nwkStatus;
endPointDesc_t *ZDApp_AutoFindMode_epDesc = (endPointDesc_t *)NULL;
uint8 ZDApp_LeaveCtrl;
#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_IEEEADDR_REQUEST )
static byte ZDApp_IEEEAddrRsp_TaskID = 0; // Initialized to NO TASK
#endif
#if defined ( ZDO_NWKADDR_REQUEST )
static byte ZDApp_NwkAddrRsp_TaskID = 0; // Initialized to NO TASK
#endif
static byte ZDApp_MatchDescRsp_TaskID = 0; // Initialized to NO TASK
static byte ZDApp_EndDeviceAnnounce_TaskID = 0; // Initialized to NO TASK
#if defined ( ZDO_BIND_UNBIND_REQUEST )
static byte ZDApp_BindUnbindRsp_TaskID = TASK_NO_TASK;
#endif
#if defined ( ZDO_BIND_UNBIND_RESPONSE ) && !defined ( REFLECTOR )
static byte ZDApp_BindReq_TaskID = 0; // Initialized to NO TASK
#endif
#if defined ( ZDO_MGMT_BIND_RESPONSE ) && !defined ( REFLECTOR )
static byte ZDApp_MgmtBindReq_TaskID = 0; // Initialized to NO TASK
#endif
#if !defined( ZDO_COORDINATOR ) || defined( SOFT_START )
static uint8 retryCnt;
#endif
// a little awkward -- this is will hold the list of versions that are legal given other
// constraints such as NV value, macro values etc. list used in ZDO_NetworkDiscoveryConfirmCB()
// when a joining device is deciding which network to join.
static byte sPVerList[] = {ZB_PROT_V1_1, ZB_PROT_V1_0};
endPointDesc_t ZDApp_epDesc =
{
ZDO_EP,
&ZDAppTaskID,
(SimpleDescriptionFormat_t *)NULL, // No Simple description for ZDO
(afNetworkLatencyReq_t)0 // No Network Latency req
};
#if defined( HOLD_AUTO_START )
devStates_t devState = DEV_HOLD;
#else
devStates_t devState = DEV_INIT;
#endif
extern uint8 SampleApp_TaskID;
/*********************************************************************
* @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
#if defined( ZDO_CACHE )
ZDCacheInit();
#endif
// Setup the Zigbee Network Protocol Version
ZDAppSetupProtoVersion();
// 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 );
}
} /* ZDO_Init() */
/*********************************************************************
* @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_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_STATE_CHANGE_EVT )
{
ZDO_UpdateNwkStatus( devState );
// Return unprocessed events
return (events ^ ZDO_STATE_CHANGE_EVT);
}
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 )
if ( events & ZDO_SECMGR_EVENT )
{
ZDSecMgrEvent();
// Return unprocessed events
return (events ^ ZDO_SECMGR_EVENT);
}
#endif // defined( ZDSECMGR_COMMERCIAL )
#if ( SECURE != 0 )
if ( events & ZDO_DEVICE_AUTH )
{
ZDApp_DeviceAuthEvt();
// Return unprocessed events
return (events ^ ZDO_DEVICE_AUTH);
}
#endif // SECURE
if ( events & ZDO_NWK_UPDATE_NV )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -