nwk_util.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /**************************************************************************************************
  2. Filename: nwk_util.h
  3. Revised: $Date: 2011-01-06 16:37:10 -0800 (Thu, 06 Jan 2011) $
  4. Revision: $Revision: 24739 $
  5. Description: Network layer utility functions.
  6. Copyright 2004-2011 Texas Instruments Incorporated. All rights reserved.
  7. IMPORTANT: Your use of this Software is limited to those specific rights
  8. granted under the terms of a software license agreement between the user
  9. who downloaded the software, his/her employer (which must be your employer)
  10. and Texas Instruments Incorporated (the "License"). You may not use this
  11. Software unless you agree to abide by the terms of the License. The License
  12. limits your use, and you acknowledge, that the Software may not be modified,
  13. copied or distributed unless embedded on a Texas Instruments microcontroller
  14. or used solely and exclusively in conjunction with a Texas Instruments radio
  15. frequency transceiver, which is integrated into your product. Other than for
  16. the foregoing purpose, you may not use, reproduce, copy, prepare derivative
  17. works of, modify, distribute, perform, display or sell this Software and/or
  18. its documentation for any purpose.
  19. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  20. PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  21. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  22. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  23. TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
  24. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  25. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  26. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  27. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  28. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  29. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  30. Should you have any questions regarding your right to use this Software,
  31. contact Texas Instruments Incorporated at www.TI.com.
  32. **************************************************************************************************/
  33. #ifndef NWK_UTIL_H
  34. #define NWK_UTIL_H
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. /*********************************************************************
  39. * INCLUDES
  40. */
  41. #include "NLMEDE.h"
  42. #include "nwk.h"
  43. /*********************************************************************
  44. * MACROS
  45. */
  46. /*********************************************************************
  47. * CONSTANTS
  48. */
  49. /*********************************************************************
  50. * FRAME FORMAT CONSTANTS
  51. */
  52. // Command identifiers
  53. #define CMD_ID_RREQ 0x01
  54. #define CMD_ID_RREP 0x02
  55. #define CMD_ID_NETWORK_STATUS 0x03 // New name for Route Error (CMD_ID_RERR)
  56. #define CMD_ID_LEAVE 0x04
  57. #define CMD_ID_RREC 0x05
  58. #define CMD_ID_REJOIN_REQ 0x06
  59. #define CMD_ID_REJOIN_RSP 0x07
  60. #define CMD_ID_LINK_STATUS 0x08
  61. #define CMD_ID_NETWORK_REPORT 0x09
  62. #define CMD_ID_NETWORK_UPDATE 0x0A
  63. #define CMD_ID_PING 7
  64. #define CMD_ID_PING_RSP 8
  65. #define CMD_ID_TREE_REQ 9
  66. #define CMD_ID_TREE_RSP 10
  67. #define CMD_ID_PARENT_REQ 11
  68. #define CMD_ID_PARENT_RSP 12
  69. // header fields
  70. #define NWK_HDR_FRAME_CTRL_LSB 0
  71. #define NWK_HDR_FRAME_CTRL_MSB 1
  72. #define NWK_HDR_DST_ADDR_LSB 2
  73. #define NWK_HDR_DST_ADDR_MSB 3
  74. #define NWK_HDR_SRC_ADDR_LSB 4
  75. #define NWK_HDR_SRC_ADDR_MSB 5
  76. #define NWK_HDR_RADIUS 6
  77. #define NWK_HDR_SEQ_NUM 7
  78. #define NWK_HDR_LEN 8
  79. // optional header fields
  80. #define NWK_HDR_DST_EXTADDR_LEN Z_EXTADDR_LEN
  81. #define NWK_HDR_SRC_EXTADDR_LEN Z_EXTADDR_LEN
  82. #define NWK_HDR_MULTICAST_CTRL_LEN 1
  83. #define NWK_HDR_SRC_ROUTE_SUBFRAME_LEN 0
  84. #define NWK_HDR_SRCRTG_CNT_LEN 1
  85. // frame control fields
  86. #define NWK_FC_FRAME_TYPE 0
  87. #define NWK_FC_PROT_VERSION 2
  88. #define NWK_FC_DISC_ROUTE 6
  89. #define NWK_FC_MULTICAST 8
  90. #define NWK_FC_SECURE 9
  91. #define NWK_FC_SRC_ROUTE 10
  92. #define NWK_FC_DST_EXTADDR 11
  93. #define NWK_FC_SRC_EXTADDR 12
  94. #define NWK_FC_RESERVED 13
  95. // frame control field masks
  96. #define NWK_FC_FRAME_TYPE_MASK 0x03
  97. #define NWK_FC_PROT_VERSION_MASK 0x0F
  98. #define NWK_FC_DISC_ROUTE_MASK 0x03
  99. #define NWK_FC_MULTICAST_MASK 0x01
  100. #define NWK_FC_SECURE_MASK 0x01
  101. #define NWK_FC_SRC_ROUTE_MASK 0x01
  102. #define NWK_FC_DST_EXTADDR_MASK 0x01
  103. #define NWK_FC_SRC_EXTADDR_MASK 0x01
  104. #define NWK_FC_RESERVED_MASK 0x07
  105. // Frame Type sub-field
  106. #define DATA_FRAME_TYPE 0x00
  107. #define CMD_FRAME_TYPE 0x01
  108. #define STUB_NWK_FRAME_TYPE 0x03
  109. #define NWK_FC_ROUTE_DISC_ENABLED 0x01
  110. // Network command fields
  111. #define NWK_CMD_LEAVE_OPTIONS 1
  112. #define NWK_CMD_LEAVE_SIZE 2
  113. #define NWK_CMD_LEAVE_RJ 0x20 // rejoin
  114. #define NWK_CMD_LEAVE_REQ 0x40 // request(1)/indication(0)
  115. #define NWK_CMD_LEAVE_RC 0x80 // remove children
  116. #define NWK_CMD_LEAVE_OPTIONS_RESERVE 0x1F
  117. #define NWK_CMD_REJOIN_REQ_SIZE 2
  118. #define NWK_CMD_REJOIN_RSP_SIZE 4
  119. // Command lengths
  120. #define NSDU_SIZE_RREQ 6
  121. #define NSDU_SIZE_RREQ_EXT NSDU_SIZE_RREQ + 8
  122. #define NSDU_SIZE_RREP 8
  123. #define NSDU_SIZE_NETWORK_STATUS 4
  124. #define NSDU_SIZE_LINK_STATUS_DEFAULT 2
  125. #define NSDU_SIZE_NETWORK_REPORT 10
  126. #define NSDU_SIZE_NETWORK_UPDATE 11
  127. #define NWK_AUX_HDR_LEN 14
  128. // Status Codes for Network Status Command Frame
  129. #define NWKSTAT_NO_ROUTE_AVAIL 0x00
  130. #define NWKSTAT_TREE_LINK_FAILURE 0x01
  131. #define NWKSTAT_NONTREE_LINK_FAILURE 0x02
  132. #define NWKSTAT_LOW_BATTERY_LEVEL 0x03
  133. #define NWKSTAT_NO_ROUTING_CAPACITY 0x04
  134. #define NWKSTAT_NO_INDIRECT_CAPACITY 0x05
  135. #define NWKSTAT_INDIRECT_TRANS_EXPIRY 0x06
  136. #define NWKSTAT_TARGET_DEVICE_UNAVAIL 0x07
  137. #define NWKSTAT_TARGET_ADDRESS_UNALLOCATED 0x08
  138. #define NWKSTAT_PARENT_LINK_FAILURE 0x09
  139. #define NWKSTAT_VALIDATE_ROUTE 0x0a
  140. #define NWKSTAT_SOURCE_ROUTE_FAILURE 0x0b
  141. #define NWKSTAT_MANY_TO_ONE_ROUTE_FAILURE 0x0c
  142. #define NWKSTAT_ADDRESS_CONFLICT 0x0d
  143. #define NWKSTAT_VERIFY_ADDRESSES 0x0e
  144. #define NWKSTAT_PANID_UPDATE 0x0f
  145. #define NWKSTAT_NWK_ADDRESS_UPDATE 0x10
  146. #define NWKSTAT_BAD_FRAME_COUNT 0x11
  147. #define NWKSTAT_BAD_KEY_SEQ_NUMBER 0x12
  148. // Link Status Option Field definitions
  149. #define LS_OPTION_LAST_FRAME 0x40
  150. #define LS_OPTION_FIRST_FRAME 0x20
  151. #define LS_OPTION_ENTRY_COUNT_MASK 0x1F
  152. #define LS_OPTION_RESERVED 0x80
  153. #define LS_ENTRY_COST_MASK 0x07
  154. #define LS_ENTRY_TXCOST_SHIFT 4
  155. #define LS_ENTRY_RESERVED 0x88
  156. #define LS_TABLE_SIZE 3
  157. // Multicast Control Fields
  158. #define NWK_MC_MODE_MASK 0x03
  159. #define NWK_MC_MODE_NONMEMBER 0x00
  160. #define NWK_MC_MODE_MEMBER 0x01
  161. #define NWK_MC_RADIUS_MASK 0x1C
  162. #define NWK_MC_RADIUS_SHIFT 2
  163. #define NWK_MC_MAXRADIUS_MASK 0xE0
  164. #define NWK_MC_MAXRADIUS_SHIFT 5
  165. #define NWK_MC_RESET_RADIUS_SHIFT (NWK_MC_MAXRADIUS_SHIFT - NWK_MC_RADIUS_SHIFT)
  166. #define NWK_MC_INFINITE_RADIUS 7
  167. // Report Types for Network Report Command Frame
  168. #define NWKREPORT_PANID_CONFLICT 0x00
  169. // Network Report Command Options Field bit mask
  170. #define NWKREPORT_INFO_CNT 0x1F
  171. #define NWKREPORT_CMD_ID 0xE0
  172. // Update Types for Network Update Command Frame
  173. #define NWKUPDATE_PANID_UPDATE 0x00
  174. // Network Update Command Options Field bit mask
  175. #define NWKUPDATE_INFO_CNT 0x1F
  176. #define NWKUPDATE_CMD_ID 0xE0
  177. /*********************************************************************
  178. * TYPEDEFS
  179. */
  180. typedef struct
  181. {
  182. uint16 dstAddr;
  183. uint8* extAddr;
  184. uint8 rejoin;
  185. uint8 request;
  186. uint8 removeChildren;
  187. uint8 cnf;
  188. } NLME_LeaveCmd_t;
  189. typedef struct
  190. {
  191. uint16 devAddr;
  192. uint8 txCost;
  193. uint8 rxCost;
  194. } linkStatusListItem_t;
  195. // Used for ED Scan Confirm message
  196. typedef struct
  197. {
  198. uint8 status;
  199. uint32 scannedChannels;
  200. uint8 *energyDetectList;
  201. } NLME_EDScanConfirm_t;
  202. // Used for Channel Interference detection message
  203. typedef struct
  204. {
  205. uint16 totalTransmissions;
  206. uint16 txFailures;
  207. } NLME_ChanInterference_t;
  208. /*********************************************************************
  209. * GLOBAL VARIABLES
  210. */
  211. /*********************************************************************
  212. * FUNCTION PROTOTYPES
  213. */
  214. /*
  215. * Build a NWK data indication and send to the next higher layer.
  216. */
  217. extern ZStatus_t NLDE_DataIndSend( NLDE_DataReq_t* req );
  218. /*
  219. * Build a NWK data service frame and send to the MAC.
  220. */
  221. extern ZStatus_t NLDE_DataReqSend( NLDE_DataReq_t* req );
  222. /*
  223. * Send an msdu
  224. */
  225. extern ZStatus_t NLDE_SendMsg( uint8* msdu, uint16 nextHopAddr, uint16 macSrcAddr,
  226. uint8 msduLength, uint8 nsduHandle,
  227. uint16 nsduHandleOptions,
  228. nwkDB_UserData_t* ud );
  229. /*
  230. * Call this function to parse an incoming message.
  231. */
  232. extern uint8 NLDE_ParseMsg( byte *buf, byte bufLength, NLDE_FrameFormat_t *ff );
  233. /*
  234. * Updates entry in the neighbor table
  235. */
  236. extern void RTG_UpdateNeighborEntry( uint16 nodeAddress, uint16 panId,
  237. byte linkQuality, byte status, byte sent);
  238. /*
  239. * Remove all entries with short address only.
  240. */
  241. extern void nwkNeighborRemoveAllStranded( void );
  242. extern void NLME_SetAssocFlags( void );
  243. extern void NLME_SetUpdateID( uint8 updateID );
  244. /*
  245. * Send a simulated MAC->NWK Data Confirm message
  246. */
  247. extern ZStatus_t nwkBuildDataCnf( byte handle, byte status );
  248. /*
  249. * Send the NWK LEAVE cmd
  250. */
  251. extern ZStatus_t NLME_LeaveCmdSend( NLME_LeaveCmd_t* cmd );
  252. /*
  253. * Process the NWK LEAVE cmd
  254. */
  255. extern uint8 NLME_LeaveCmdProcess( uint8 handle, NLDE_FrameFormat_t *ff );
  256. /*
  257. * Handle NWK commands during MACCB_DATA_CNF_CMD processing
  258. */
  259. extern void NLME_CmdCnf( NLDE_DataCnf_t* cnf );
  260. /*
  261. * Check for NWK commands that can legally be broadcast
  262. */
  263. extern uint8 NLME_CmdBcast( uint8 cmdID );
  264. /*
  265. * Allocate a NLDE_DatatReq_t buffer for NWK commands
  266. */
  267. extern NLDE_DataReq_t* NLME_CmdDataReqAlloc
  268. ( NLDE_DataReqAlloc_t* dra );
  269. /*
  270. * Stub to load the next higher layer frame data
  271. */
  272. extern void* NLDE_FrameDataLoad( NLDE_DataReq_t* req );
  273. /*
  274. * Process Rejoin Rsp command pkt
  275. */
  276. extern void NLME_RejoinRspCmdProcess( NLDE_FrameFormat_t* ff );
  277. /*
  278. * Send Rejoin Rsp command pkt
  279. */
  280. extern ZStatus_t NLME_RejoinRspSend( ZMacAssociateRsp_t *AssocRsp, byte relation, uint8 type );
  281. /*
  282. * Process Rejoin Request command pkt
  283. */
  284. extern void NLME_RejoinReqCmdProcess( NLDE_FrameFormat_t* ff );
  285. /*
  286. * Build src routing header frame
  287. */
  288. extern uint16 NLDE_BuildSrcRtgFrame( NLDE_SrcFrameFormat_t* sff, uint16 dstAddr );
  289. /*********************************************************************
  290. * HELPERS FUNCTION PROTOTYPES
  291. */
  292. /*
  293. * Returns a unique handle to identify a packet transaction
  294. */
  295. extern byte NLDE_GetHandle( void );
  296. extern byte NLME_GetProtocolVersion( void );
  297. extern uint8 NLME_GetEnergyThreshold( void );
  298. extern void NLME_SetEnergyThreshold( uint8 value );
  299. extern void NLME_SetBroadcastFilter(byte capabilities);
  300. extern addr_filter_t NLME_IsAddressBroadcast(uint16 shortAddress);
  301. extern void NLME_RemoveChild( uint8* extAddr, uint8 dealloc );
  302. extern uint8 nwk_multicast( NLDE_FrameFormat_t* ff );
  303. extern void NLME_NwkStatusAddrConflictProcess( uint16 addr );
  304. extern uint8 NLME_ProcessAddressConflictLocal( uint16 addr );
  305. extern ZStatus_t NLDE_CheckForAddrConflict( NLDE_FrameFormat_t *ff );
  306. extern ZStatus_t NLME_SendNetworkStatus( uint16 dstAddr,
  307. uint16 statusAddr, uint8 statusCode, uint8 forceSeqNum );
  308. extern uint8 NLME_ProcessNetworkStatus( NLDE_FrameFormat_t *ff, uint8 handle );
  309. extern void nwkHandleBrokenRoute( nwkDB_t *rec );
  310. extern uint8 NLDE_ParseFrameControl( uint16 fc, NLDE_FrameFormat_t *ff );
  311. // Functions for addressing schemes
  312. extern void NLME_AddressConflictAssignNewStochastic( void );
  313. // Functions pointers for addressing schemes
  314. extern void (*pnwk_AssignNewAddr)( void );
  315. extern ZStatus_t NLME_SendNetworkReport( uint16 dstAddr, uint8 reportType, uint8 *EPID,
  316. uint8 reportInfoCnt, uint16 *panIDs );
  317. extern void NLME_ProcessNetworkReport( NLDE_FrameFormat_t *ff );
  318. extern ZStatus_t NLME_SendNetworkUpdate( uint16 dstAddr, uint8 updateType, uint8 *EPID,
  319. uint8 updateId, uint16 newPID );
  320. extern uint8 NLME_ProcessNetworkUpdate( uint8 handle, NLDE_FrameFormat_t *ff );
  321. extern void nwkAddPanId( uint16 panID );
  322. extern void nwkProcessPanIdScan( void );
  323. extern void nwkChangePanID( void );
  324. extern void nwkFreePanIdList( void );
  325. extern uint16 nwkTransmissionFailures( uint8 reset );
  326. extern uint8 nwk_ExtPANIDValid( byte *panID );
  327. /****************************************************************************
  328. * Link Status Functions
  329. ****************************************************************************/
  330. extern void NLME_InitLinkStatus( void );
  331. extern void NLME_UpdateLinkStatus( void );
  332. extern void NLME_SetLinkStatusTimer( void );
  333. extern ZStatus_t nwkSendLinkStatus( void );
  334. extern ZStatus_t NLME_SendLinkStatus( uint8 neighbors, uint8 options,
  335. linkStatusListItem_t *pList );
  336. extern void NLME_ProcessLinkStatus( NLDE_FrameFormat_t *ff );
  337. extern void (*pNLME_SetLinkStatusTimer)( void );
  338. extern void (*pNLME_UpdateLinkStatus)( void );
  339. extern void (*pNLME_ProcessLinkStatus)( NLDE_FrameFormat_t *ff );
  340. extern uint8 nwkConvertLqiToCost( uint8 lqi );
  341. /****************************************************************************
  342. * Neighbor Table Functions
  343. ****************************************************************************/
  344. #define NWKNEIGHBOR_NOT_FOUND 0x00 // device not found
  345. #define NWKNEIGHBOR_FOUND_SHORT 0x0A // found matching short, but
  346. // extended address doesn't match
  347. #define NWKNEIGHBOR_FOUND_IEEE 0xA0 // found matching extended, but
  348. // short address doesn't match
  349. #define NWKNEIGHBOR_FOUND 0xAA // found matching extended and short
  350. // Neighbor table entry
  351. typedef struct
  352. {
  353. uint16 neighborAddress;
  354. uint8 neighborExtAddr[Z_EXTADDR_LEN];
  355. uint16 panId;
  356. uint8 age; // number of nwkLinkStatusPeriod since last link status
  357. linkInfo_t linkInfo;
  358. } neighborEntry_t;
  359. extern neighborEntry_t neighborTable[];
  360. extern neighborEntry_t *nwkNeighborFind( uint16 nwkAddr, uint16 panID );
  361. extern neighborEntry_t *nwkNeighborFindEmptySlot( void );
  362. extern uint16 nwkNeighborCount( void );
  363. extern void nwkNeighborRemove( uint16 NeighborAddress, uint16 PanId );
  364. extern neighborEntry_t *nwkNeighborGetWithIndex( uint16 Index );
  365. extern void nwkNeighborInitTable( void );
  366. extern void nwkNeighborClearEntry( neighborEntry_t *entry );
  367. extern void nwkNeighborKeepPANID( uint16 PanID );
  368. extern linkInfo_t *nwkNeighborGetLinkInfo( uint16 NeighborAddress, uint16 panId );
  369. extern byte nwkNeighborCheckAddr( uint16 nwkAddr, uint8 *extAddr );
  370. extern void nwkNeighborUpdateAge( void );
  371. extern uint16 nwkNeighborFindRandom( uint16 skipAddr, uint16 skipAddr2, uint16 skipAddr3 );
  372. extern void nwkNeighborUpdateNwkAddr( uint16 nwkAddr, uint8 *ieeeAddr );
  373. extern void nwkNeighborAdd( uint16 nwkAddr, uint16 panId, uint8 linkQuality );
  374. extern void nwkNeighborChangePANID( uint16 oldPanID, uint16 newPanID );
  375. /*********************************************************************
  376. * Frequency Agility Functions
  377. ********************************************************************/
  378. extern void (*pNwkFreqAgilityScanCfg)( ZMacScanCnf_t *param );
  379. extern void (*pNwkFreqAgilityIncTotalTx)( void );
  380. extern void (*pNwkFreqAgilityIncErrorCnt)( linkInfo_t *linkInfo, uint8 status );
  381. extern void NwkFreqAgilityInit( void );
  382. /****************************************************************************
  383. ****************************************************************************/
  384. #ifdef __cplusplus
  385. }
  386. #endif
  387. #endif /* NWK_UTIL_H */