ZDApp.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /**************************************************************************************************
  2. Filename: ZDApp.h
  3. Revised: $Date: 2008-03-28 11:57:25 -0700 (Fri, 28 Mar 2008) $
  4. Revision: $Revision: 16672 $
  5. Description: This file contains the interface to the Zigbee Device Application. This is the
  6. Application part that the use can change. This also contains the Task functions.
  7. Copyright 2004-2007 Texas Instruments Incorporated. All rights reserved.
  8. IMPORTANT: Your use of this Software is limited to those specific rights
  9. granted under the terms of a software license agreement between the user
  10. who downloaded the software, his/her employer (which must be your employer)
  11. and Texas Instruments Incorporated (the "License"). You may not use this
  12. Software unless you agree to abide by the terms of the License. The License
  13. limits your use, and you acknowledge, that the Software may not be modified,
  14. copied or distributed unless embedded on a Texas Instruments microcontroller
  15. or used solely and exclusively in conjunction with a Texas Instruments radio
  16. frequency transceiver, which is integrated into your product. Other than for
  17. the foregoing purpose, you may not use, reproduce, copy, prepare derivative
  18. works of, modify, distribute, perform, display or sell this Software and/or
  19. its documentation for any purpose.
  20. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  21. PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  22. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  23. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  24. TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
  25. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  26. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  27. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  28. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  29. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  30. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  31. Should you have any questions regarding your right to use this Software,
  32. contact Texas Instruments Incorporated at www.TI.com.
  33. **************************************************************************************************/
  34. #ifndef ZDAPP_H
  35. #define ZDAPP_H
  36. #ifdef __cplusplus
  37. extern "C"
  38. {
  39. #endif
  40. /*********************************************************************
  41. * INCLUDES
  42. */
  43. #include "ZComDef.h"
  44. #include "ZMac.h"
  45. #include "NLMEDE.h"
  46. #include "APS.h"
  47. #include "AF.h"
  48. #include "ZDProfile.h"
  49. /*********************************************************************
  50. * MACROS
  51. */
  52. /*********************************************************************
  53. * CONSTANTS
  54. */
  55. // Set this value for use in choosing a PAN to join
  56. // modify ZDApp.c to enable this...
  57. #define ZDO_CONFIG_MAX_BO 15
  58. // Task Events
  59. #define ZDO_NETWORK_INIT 0x0001
  60. #define ZDO_NETWORK_START 0x0002
  61. #define ZDO_DEVICE_RESET 0x0004
  62. #define ZDO_COMMAND_CNF 0x0008
  63. #define ZDO_STATE_CHANGE_EVT 0x0010
  64. #define ZDO_ROUTER_START 0x0020
  65. #define ZDO_NEW_DEVICE 0x0040
  66. #define ZDO_DEVICE_AUTH 0x0080
  67. #define ZDO_SECMGR_EVENT 0x0100
  68. #define ZDO_NWK_UPDATE_NV 0x0200
  69. #define ZDO_FRAMECOUNTER_CHANGE 0x0400
  70. // Incoming to ZDO
  71. #define ZDO_NWK_DISC_CNF 0x01
  72. #define ZDO_NWK_JOIN_IND 0x02
  73. #define ZDO_NWK_JOIN_REQ 0x03
  74. #define ZDO_ESTABLISH_KEY_CFM 0x04
  75. #define ZDO_ESTABLISH_KEY_IND 0x05
  76. #define ZDO_TRANSPORT_KEY_IND 0x06
  77. #define ZDO_UPDATE_DEVICE_IND 0x07
  78. #define ZDO_REMOVE_DEVICE_IND 0x08
  79. #define ZDO_REQUEST_KEY_IND 0x09
  80. #define ZDO_SWITCH_KEY_IND 0x0A
  81. #define ZDO_AUTHENTICATE_IND 0x0B
  82. #define ZDO_AUTHENTICATE_CFM 0x0C
  83. // ZDO command message fields
  84. #define ZDO_CMD_ID 0
  85. #define ZDO_CMD_ID_LEN 1
  86. // ZDO security message fields
  87. #define ZDO_ESTABLISH_KEY_CFM_LEN \
  88. ((uint8) \
  89. (sizeof(ZDO_EstablishKeyCfm_t) ) )
  90. #define ZDO_ESTABLISH_KEY_IND_LEN \
  91. ((uint8) \
  92. (sizeof(ZDO_EstablishKeyInd_t) ) )
  93. #define ZDO_TRANSPORT_KEY_IND_LEN \
  94. ((uint8) \
  95. (sizeof(ZDO_TransportKeyInd_t) ) )
  96. #define ZDO_UPDATE_DEVICE_IND_LEN \
  97. ((uint8) \
  98. (sizeof(ZDO_UpdateDeviceInd_t) ) )
  99. #define ZDO_REMOVE_DEVICE_IND_LEN \
  100. ((uint8) \
  101. (sizeof(ZDO_RemoveDeviceInd_t) ) )
  102. #define ZDO_REQUEST_KEY_IND_LEN \
  103. ((uint8) \
  104. (sizeof(ZDO_RequestKeyInd_t) ) )
  105. #define ZDO_SWITCH_KEY_IND_LEN \
  106. ((uint8) \
  107. (sizeof(ZDO_SwitchKeyInd_t) ) )
  108. #define ZDO_AUTHENTICATE_IND_LEN \
  109. ((uint8) \
  110. (sizeof(ZDO_AuthenticateInd_t) ) )
  111. #define ZDO_AUTHENTICATE_CFM_LEN \
  112. ((uint8) \
  113. (sizeof(ZDO_AuthenticateCfm_t) ) )
  114. #define NWK_RETRY_DELAY 1000 // in milliseconds
  115. #define ZDO_MATCH_DESC_ACCEPT_ACTION 1 // Message field
  116. // Options for ZDApp_StartUpFromApp()
  117. #define ZDAPP_STARTUP_COORD 2 // Start up as coordinator only
  118. #define ZDAPP_STARTUP_ROUTER 1 // Start up as router only
  119. #define ZDAPP_STARTUP_AUTO 0 // Startup in auto, look for coord,
  120. // if not found, become coord.
  121. #define NUM_DISC_ATTEMPTS 2
  122. // ZDOInitDevice return values
  123. #define ZDO_INITDEV_RESTORED_NETWORK_STATE 0x00
  124. #define ZDO_INITDEV_NEW_NETWORK_STATE 0x01
  125. #define ZDO_INITDEV_LEAVE_NOT_STARTED 0x02
  126. #if defined ( MANAGED_SCAN )
  127. // Only use in a battery powered device
  128. // This is the number of times a channel is scanned at the shortest possible
  129. // scan time (which is 30 MS (2 x 15). The idea is to scan one channel at a
  130. // time (from the channel list), but scan it multiple times.
  131. #define MANAGEDSCAN_TIMES_PRE_CHANNEL 5
  132. #define MANAGEDSCAN_DELAY_BETWEEN_SCANS 150 // milliseconds
  133. extern uint8 zdoDiscCounter;
  134. #endif // MANAGED_SCAN
  135. // Use the following to delay application data request after startup.
  136. #define ZDAPP_HOLD_DATA_REQUESTS_TIMEOUT 0 // in milliseconds
  137. /*********************************************************************
  138. * TYPEDEFS
  139. */
  140. typedef enum
  141. {
  142. DEV_HOLD, // Initialized - not started automatically
  143. DEV_INIT, // Initialized - not connected to anything
  144. DEV_NWK_DISC, // Discovering PAN's to join
  145. DEV_NWK_JOINING, // Joining a PAN
  146. DEV_NWK_REJOIN, // ReJoining a PAN, only for end devices
  147. DEV_END_DEVICE_UNAUTH, // Joined but not yet authenticated by trust center
  148. DEV_END_DEVICE, // Started as device after authentication
  149. DEV_ROUTER, // Device joined, authenticated and is a router
  150. DEV_COORD_STARTING, // Started as Zigbee Coordinator
  151. DEV_ZB_COORD, // Started as Zigbee Coordinator
  152. DEV_NWK_ORPHAN // Device has lost information about its parent..
  153. } devStates_t;
  154. typedef enum
  155. {
  156. ZDO_SUCCESS,
  157. ZDO_FAIL
  158. } zdoStatus_t;
  159. typedef struct
  160. {
  161. osal_event_hdr_t hdr;
  162. uint8 panIdLSB;
  163. uint8 panIdMSB;
  164. uint8 logicalChannel;
  165. uint8 version;
  166. uint8 extendedPANID[Z_EXTADDR_LEN];
  167. } ZDO_NetworkDiscoveryCfm_t;
  168. typedef struct
  169. {
  170. osal_event_hdr_t hdr;
  171. uint8 dstAddrDstEP;
  172. zAddrType_t dstAddr;
  173. uint8 dstAddrClusterIDLSB;
  174. uint8 dstAddrClusterIDMSB;
  175. uint8 dstAddrRemove;
  176. uint8 dstAddrEP;
  177. } ZDO_NewDstAddr_t;
  178. // ZDO security message types
  179. typedef struct
  180. {
  181. osal_event_hdr_t hdr;
  182. uint8 partExtAddr[Z_EXTADDR_LEN];
  183. uint8 status;
  184. } ZDO_EstablishKeyCfm_t;
  185. typedef struct
  186. {
  187. osal_event_hdr_t hdr;
  188. uint16 srcAddr;
  189. uint8 initExtAddr[Z_EXTADDR_LEN];
  190. uint8 method;
  191. uint8 apsSecure;
  192. uint8 nwkSecure;
  193. //devtag.0604.todo - remove obsolete
  194. } ZDO_EstablishKeyInd_t;
  195. typedef struct
  196. {
  197. osal_event_hdr_t hdr;
  198. uint16 srcAddr;
  199. uint8 keyType;
  200. uint8 keySeqNum;
  201. uint8 key[SEC_KEY_LEN];
  202. uint8 srcExtAddr[Z_EXTADDR_LEN];
  203. uint8 initiator;
  204. uint8 secure;
  205. } ZDO_TransportKeyInd_t;
  206. typedef struct
  207. {
  208. osal_event_hdr_t hdr;
  209. uint16 srcAddr;
  210. uint8 devExtAddr[Z_EXTADDR_LEN];
  211. uint16 devAddr;
  212. uint8 status;
  213. } ZDO_UpdateDeviceInd_t;
  214. typedef struct
  215. {
  216. osal_event_hdr_t hdr;
  217. uint16 srcAddr;
  218. uint8 childExtAddr[Z_EXTADDR_LEN];
  219. } ZDO_RemoveDeviceInd_t;
  220. typedef struct
  221. {
  222. osal_event_hdr_t hdr;
  223. uint16 srcAddr;
  224. uint8 keyType;
  225. uint8 partExtAddr[Z_EXTADDR_LEN];
  226. } ZDO_RequestKeyInd_t;
  227. typedef struct
  228. {
  229. osal_event_hdr_t hdr;
  230. uint16 srcAddr;
  231. uint8 keySeqNum;
  232. } ZDO_SwitchKeyInd_t;
  233. typedef struct
  234. {
  235. osal_event_hdr_t hdr;
  236. APSME_AuthenticateInd_t aps;
  237. } ZDO_AuthenticateInd_t;
  238. typedef struct
  239. {
  240. osal_event_hdr_t hdr;
  241. APSME_AuthenticateCfm_t aps;
  242. } ZDO_AuthenticateCfm_t;
  243. typedef struct
  244. {
  245. osal_event_hdr_t hdr;
  246. uint16 nwkAddr;
  247. uint8 numInClusters;
  248. uint16 *pInClusters;
  249. uint8 numOutClusters;
  250. uint16 *pOutClusters;
  251. } ZDO_MatchDescRspSent_t;
  252. /*********************************************************************
  253. * GLOBAL VARIABLES
  254. */
  255. extern uint8 ZDAppTaskID;
  256. extern uint8 nwkStatus;
  257. extern devStates_t devState;
  258. /* Always kept up to date by the network state changed logic, so use this addr
  259. * in function calls the require my network address as one of the parameters.
  260. */
  261. extern zAddrType_t ZDAppNwkAddr;
  262. extern uint8 saveExtAddr[]; // Loaded with value by ZDApp_Init().
  263. extern uint8 zdappMgmtNwkDiscRspTransSeq;
  264. extern uint8 zdappMgmtNwkDiscReqInProgress;
  265. extern zAddrType_t zdappMgmtNwkDiscRspAddr;
  266. extern uint8 zdappMgmtNwkDiscStartIndex;
  267. extern uint8 zdappMgmtSavedNwkState;
  268. extern uint8 ZDO_UseExtendedPANID[Z_EXTADDR_LEN];
  269. /*********************************************************************
  270. * FUNCTIONS - API
  271. */
  272. extern void ZDO_AddrChangeIndicationCB( uint16 newAddr );
  273. /*********************************************************************
  274. * Task Level Control
  275. */
  276. /*
  277. * ZdApp Task Initialization Function
  278. */
  279. extern void ZDApp_Init( uint8 task_id );
  280. /*
  281. * ZdApp Task Event Processing Function
  282. */
  283. extern UINT16 ZDApp_event_loop( uint8 task_id, UINT16 events );
  284. /*********************************************************************
  285. * Application Level Functions
  286. */
  287. /*
  288. * Start the device in the network. This function will read
  289. * ZCD_NV_STARTUP_OPTION (NV item) to determine whether or not to
  290. * restore the network state of the device.
  291. *
  292. * startDelay - timeDelay to start device (in milliseconds).
  293. * There is a jitter added to this delay:
  294. * ((NWK_START_DELAY + startDelay)
  295. * + (osal_rand() & EXTENDED_JOINING_RANDOM_MASK))
  296. *
  297. * NOTE: If the application would like to force a "new" join, the
  298. * application should set the ZCD_STARTOPT_DEFAULT_NETWORK_STATE
  299. * bit in the ZCD_NV_STARTUP_OPTION NV item before calling
  300. * this function.
  301. *
  302. * returns:
  303. * ZDO_INITDEV_RESTORED_NETWORK_STATE - The device's network state was
  304. * restored.
  305. * ZDO_INITDEV_NEW_NETWORK_STATE - The network state was initialized.
  306. * This could mean that ZCD_NV_STARTUP_OPTION said to not restore, or
  307. * it could mean that there was no network state to restore.
  308. * ZDO_INITDEV_LEAVE_NOT_STARTED - Before the reset, a network leave was issued
  309. * with the rejoin option set to TRUE. So, the device was not
  310. * started in the network (one time only). The next time this
  311. * function is called it will start.
  312. */
  313. extern uint8 ZDOInitDevice( uint16 startDelay );
  314. /*
  315. * Sends an osal message to ZDApp with parameter as the msg data byte.
  316. */
  317. extern void ZDApp_SendEventMsg( uint8 cmd, uint8 len, uint8 *buf );
  318. /*
  319. * ZdApp Function for Establishing a Link Key
  320. */
  321. extern ZStatus_t ZDApp_EstablishKey( uint8 endPoint,
  322. uint16 nwkAddr,
  323. uint8* extAddr );
  324. /*
  325. * Start the network formation process
  326. * delay - millisecond wait before
  327. */
  328. extern void ZDApp_NetworkInit( uint16 delay );
  329. /*********************************************************************
  330. * Callback FUNCTIONS - API
  331. */
  332. /*********************************************************************
  333. * Call Back Functions from NWK - API
  334. */
  335. /*
  336. * ZDO_NetworkDiscoveryConfirmCB - scan results
  337. */
  338. extern ZStatus_t ZDO_NetworkDiscoveryConfirmCB( uint8 ResultCount,
  339. networkDesc_t *NetworkList );
  340. /*
  341. * ZDO_NetworkFormationConfirm - results of the request to
  342. * initialize a coordinator in a network
  343. */
  344. extern void ZDO_NetworkFormationConfirmCB( ZStatus_t Status );
  345. /*
  346. * ZDO_JoinConfirmCB - results of its request to join itself or another
  347. * device to a network
  348. */
  349. extern void ZDO_JoinConfirmCB( uint16 PanId, ZStatus_t Status );
  350. /*
  351. * ZDO_JoinIndicationCB - notified of a remote join request
  352. */
  353. extern ZStatus_t ZDO_JoinIndicationCB( uint16 ShortAddress,
  354. uint8 *ExtendedAddress, uint8 CapabilityInformation, uint8 type );
  355. /*
  356. * ZDO_ConcentratorIndicationCB - notified of a concentrator existence
  357. */
  358. extern void ZDO_ConcentratorIndicationCB( uint16 nwkAddr );
  359. /*
  360. * ZDO_StartRouterConfirm - results of the request to
  361. * start functioning as a router in a network
  362. */
  363. extern void ZDO_StartRouterConfirmCB( ZStatus_t Status );
  364. /*
  365. * ZDO_LeaveCnf - results of the request for this or a child device
  366. * to leave
  367. */
  368. extern void ZDO_LeaveCnf( NLME_LeaveCnf_t* cnf );
  369. /*
  370. * ZDO_LeaveInd - notified of a remote leave request or indication
  371. */
  372. extern void ZDO_LeaveInd( NLME_LeaveInd_t* ind );
  373. /*
  374. * ZDO_SyncIndicationCB - notified of sync loss with parent
  375. */
  376. extern void ZDO_SyncIndicationCB( uint8 type, uint16 shortAddr );
  377. /*
  378. * ZDO_ManytoOneFailureIndicationCB - notified a many-to-one route failure
  379. */
  380. extern void ZDO_ManytoOneFailureIndicationCB( void );
  381. /*
  382. * ZDO_PollConfirmCB - notified of poll confirm
  383. */
  384. extern void ZDO_PollConfirmCB( uint8 status );
  385. /*********************************************************************
  386. * Call Back Functions from Security - API
  387. */
  388. extern ZStatus_t ZDO_UpdateDeviceIndication( uint8 *extAddr, uint8 status );
  389. /*
  390. * ZDApp_InMsgCB - Allow the ZDApp to handle messages that are not supported
  391. */
  392. extern void ZDApp_InMsgCB( zdoIncomingMsg_t *inMsg );
  393. extern void ZDO_StartRouterConfirm( ZStatus_t Status );
  394. /*********************************************************************
  395. * Call Back Functions from Apllication - API
  396. */
  397. /*
  398. * ZDO_NwkUpdateCB - Network state info has changed
  399. */
  400. extern void ZDApp_NwkStateUpdateCB( void );
  401. /*********************************************************************
  402. * ZDO Control Functions
  403. */
  404. /*
  405. * ZDApp_ChangeMatchDescRespPermission
  406. * - Change the Match Descriptor Response permission.
  407. */
  408. extern void ZDApp_ChangeMatchDescRespPermission( uint8 endpoint, uint8 action );
  409. /*
  410. * ZDApp_ResetNwkKey
  411. * - Re initialize the NV Nwk Key
  412. */
  413. extern void ZDApp_ResetNwkKey( void );
  414. /*
  415. * ZDApp_StartJoiningCycle
  416. * - Starts the joining cycle of a device. This will only continue an
  417. * already started (or stopped) joining cycle.
  418. *
  419. * returns TRUE if joining started, FALSE if not in joining or rejoining
  420. */
  421. extern uint8 ZDApp_StartJoiningCycle( void );
  422. /*
  423. * ZDApp_StopJoiningCycle
  424. * - Stops the joining or rejoining process of a device.
  425. *
  426. * returns TRUE if joining stopped, FALSE if joining or rejoining
  427. */
  428. extern uint8 ZDApp_StopJoiningCycle( void );
  429. /*
  430. * ZDApp_AnnounceNewAddress
  431. * - Announce a new address
  432. */
  433. extern void ZDApp_AnnounceNewAddress( void );
  434. /*
  435. * ZDApp_NVUpdate - Initiate an NV update
  436. */
  437. extern void ZDApp_NVUpdate( void );
  438. /*
  439. * Callback from network layer when coordinator start has a conflict with
  440. * an existing PAN ID.
  441. */
  442. extern uint16 ZDApp_CoordStartPANIDConflictCB( uint16 panid );
  443. /*********************************************************************
  444. *********************************************************************/
  445. #ifdef __cplusplus
  446. }
  447. #endif
  448. #endif /* ZDOBJECT_H */