nwk_globals.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /**************************************************************************************************
  2. Filename: nwk_globals.c
  3. Revised: $Date: 2009-03-24 09:41:39 -0700 (Tue, 24 Mar 2009) $
  4. Revision: $Revision: 19538 $
  5. Description: User definable Network Parameters.
  6. Copyright 2004-2009 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. /*********************************************************************
  34. * INCLUDES
  35. */
  36. #include "ZComdef.h"
  37. #include "OSAL.h"
  38. #include "AddrMgr.h"
  39. #include "AssocList.h"
  40. #include "BindingTable.h"
  41. #include "nwk_util.h"
  42. #include "nwk_globals.h"
  43. #include "APS.h"
  44. #include "ssp.h"
  45. #include "rtg.h"
  46. #include "ZDConfig.h"
  47. #include "ZGlobals.h"
  48. #if defined ( LCD_SUPPORTED )
  49. #include "OnBoard.h"
  50. #endif
  51. /* HAL */
  52. #include "hal_lcd.h"
  53. /*********************************************************************
  54. * MACROS
  55. */
  56. /*********************************************************************
  57. * CONSTANTS
  58. */
  59. // Maximums for the data buffer queue
  60. #define NWK_MAX_DATABUFS_WAITING 8 // Waiting to be sent to MAC
  61. #define NWK_MAX_DATABUFS_SCHEDULED 5 // Timed messages to be sent
  62. #define NWK_MAX_DATABUFS_CONFIRMED 5 // Held after MAC confirms
  63. #define NWK_MAX_DATABUFS_TOTAL 12 // Total number of buffers
  64. // 1-255 (0 -> 256) X RTG_TIMER_INTERVAL
  65. // A known shortcoming is that when a message is enqueued as "hold" for a
  66. // sleeping device, the timer tick may have counted down to 1, so that msg
  67. // will not be held as long as expected. If NWK_INDIRECT_MSG_TIMEOUT is set to 1
  68. // the hold time will vary randomly from 0 - CNT_RTG_TIMER ticks.
  69. // So the hold time will vary within this interval:
  70. // { (NWK_INDIRECT_MSG_TIMEOUT-1)*CNT_RTG_TIMER,
  71. // NWK_INDIRECT_MSG_TIMEOUT*CNT_RTG_TIMER }
  72. #define NWK_INDIRECT_CNT_RTG_TMR 1
  73. // To hold msg for sleeping end devices for 30 secs:
  74. // #define CNT_RTG_TIMER 1
  75. // #define NWK_INDIRECT_MSG_TIMEOUT 30
  76. // To hold msg for sleeping end devices for 30 mins:
  77. // #define CNT_RTG_TIMER 60
  78. // #define NWK_INDIRECT_MSG_TIMEOUT 30
  79. // To hold msg for sleeping end devices for 30 days:
  80. // #define CNT_RTG_TIMER 60
  81. // #define NWK_INDIRECT_MSG_TIMEOUT (30 * 24 * 60)
  82. // Maximum msgs to hold per associated device.
  83. #define NWK_INDIRECT_MSG_MAX_PER 3
  84. // Maximum total msgs to hold for all associated devices.
  85. #define NWK_INDIRECT_MSG_MAX_ALL \
  86. (NWK_MAX_DATABUFS_TOTAL - NWK_INDIRECT_MSG_MAX_PER)
  87. /*********************************************************************
  88. * TYPEDEFS
  89. */
  90. /*********************************************************************
  91. * NWK GLOBAL VARIABLES
  92. */
  93. // Variables for MAX list size
  94. CONST uint16 gNWK_MAX_DEVICE_LIST = NWK_MAX_DEVICES;
  95. // Variables for MAX Sleeping End Devices
  96. CONST uint8 gNWK_MAX_SLEEPING_END_DEVICES = NWK_MAX_DEVICES - NWK_MAX_ROUTERS;
  97. // Variables for MAX data buffer levels
  98. CONST byte gNWK_MAX_DATABUFS_WAITING = NWK_MAX_DATABUFS_WAITING;
  99. CONST byte gNWK_MAX_DATABUFS_SCHEDULED = NWK_MAX_DATABUFS_SCHEDULED;
  100. CONST byte gNWK_MAX_DATABUFS_CONFIRMED = NWK_MAX_DATABUFS_CONFIRMED;
  101. CONST byte gNWK_MAX_DATABUFS_TOTAL = NWK_MAX_DATABUFS_TOTAL;
  102. CONST byte gNWK_INDIRECT_CNT_RTG_TMR = NWK_INDIRECT_CNT_RTG_TMR;
  103. CONST byte gNWK_INDIRECT_MSG_MAX_PER = NWK_INDIRECT_MSG_MAX_PER;
  104. CONST byte gNWK_INDIRECT_MSG_MAX_ALL = NWK_INDIRECT_MSG_MAX_ALL;
  105. // change this if using a different stack profile...
  106. // Cskip array
  107. uint16 *Cskip;
  108. #if ( STACK_PROFILE_ID == ZIGBEEPRO_PROFILE )
  109. byte CskipRtrs[1] = {0};
  110. byte CskipChldrn[1] = {0};
  111. #elif ( STACK_PROFILE_ID == HOME_CONTROLS )
  112. byte CskipRtrs[MAX_NODE_DEPTH+1] = {6,6,6,6,6,0};
  113. byte CskipChldrn[MAX_NODE_DEPTH+1] = {20,20,20,20,20,0};
  114. #elif ( STACK_PROFILE_ID == GENERIC_STAR )
  115. byte CskipRtrs[MAX_NODE_DEPTH+1] = {5,5,5,5,5,0};
  116. byte CskipChldrn[MAX_NODE_DEPTH+1] = {5,5,5,5,5,0};
  117. #elif ( STACK_PROFILE_ID == NETWORK_SPECIFIC )
  118. byte CskipRtrs[MAX_NODE_DEPTH+1] = {5,5,5,5,5,0};
  119. byte CskipChldrn[MAX_NODE_DEPTH+1] = {5,5,5,5,5,0};
  120. #endif // STACK_PROFILE_ID
  121. // Minimum lqi value that is required for association
  122. byte gMIN_TREE_LINK_COST = MIN_LQI_COST_3;
  123. // Statically defined Associated Device List
  124. associated_devices_t AssociatedDevList[NWK_MAX_DEVICES];
  125. CONST byte gMAX_RTG_ENTRIES = MAX_RTG_ENTRIES;
  126. CONST byte gMAX_RTG_SRC_ENTRIES = MAX_RTG_SRC_ENTRIES;
  127. CONST byte gMAX_RREQ_ENTRIES = MAX_RREQ_ENTRIES;
  128. CONST byte gMAX_NEIGHBOR_ENTRIES = MAX_NEIGHBOR_ENTRIES;
  129. // Table of neighboring nodes (not including child nodes)
  130. neighborEntry_t neighborTable[MAX_NEIGHBOR_ENTRIES];
  131. // Routing table
  132. rtgEntry_t rtgTable[MAX_RTG_ENTRIES];
  133. #if defined ( ZIGBEE_SOURCE_ROUTING )
  134. rtgSrcEntry_t rtgSrcTable[MAX_RTG_SRC_ENTRIES];
  135. #endif
  136. // Table of current RREQ packets in the network
  137. rtDiscEntry_t rtDiscTable[MAX_RREQ_ENTRIES];
  138. // Table of data broadcast packets currently in circulation.
  139. bcastEntry_t bcastTable[MAX_BCAST];
  140. // These 2 arrays are to be used as an array of struct { uint8, uint32 }.
  141. uint8 bcastHoldHandle[MAX_BCAST];
  142. uint32 bcastHoldAckMask[MAX_BCAST];
  143. CONST byte gMAX_BCAST = MAX_BCAST;
  144. // For tree addressing, this switch allows the allocation of a
  145. // router address to an end device when end device address are
  146. // all used up. If this option is enabled, address space
  147. // could be limited.
  148. CONST uint8 gNWK_TREE_ALLOCATE_ROUTERADDR_FOR_ENDDEVICE = FALSE;
  149. /*********************************************************************
  150. * APS GLOBAL VARIABLES
  151. */
  152. // The Maximum number of binding records
  153. // This number is defined in BindingTable.h - change it there.
  154. CONST uint16 gNWK_MAX_BINDING_ENTRIES = NWK_MAX_BINDING_ENTRIES;
  155. #if defined ( REFLECTOR )
  156. // The Maximum number of cluster IDs in a binding record
  157. // This number is defined in BindingTable.h - change it there.
  158. CONST byte gMAX_BINDING_CLUSTER_IDS = MAX_BINDING_CLUSTER_IDS;
  159. CONST uint16 gBIND_REC_SIZE = sizeof( BindingEntry_t );
  160. // Binding Table
  161. BindingEntry_t BindingTable[NWK_MAX_BINDING_ENTRIES];
  162. #endif
  163. // Maximum number allowed in the groups table.
  164. CONST uint8 gAPS_MAX_GROUPS = APS_MAX_GROUPS;
  165. // APS End Device Broadcast Table
  166. #if ( ZG_BUILD_ENDDEVICE_TYPE )
  167. apsEndDeviceBroadcast_t apsEndDeviceBroadcastTable[APS_MAX_ENDDEVICE_BROADCAST_ENTRIES];
  168. uint8 gAPS_MAX_ENDDEVICE_BROADCAST_ENTRIES = APS_MAX_ENDDEVICE_BROADCAST_ENTRIES;
  169. #endif
  170. /*********************************************************************
  171. * SECURITY GLOBAL VARIABLES
  172. */
  173. // This is the default pre-configured key,
  174. // change this to make a unique key
  175. // SEC_KEY_LEN is defined in ssp.h.
  176. CONST byte defaultKey[SEC_KEY_LEN] =
  177. {
  178. #if defined ( APP_TP ) || defined ( APP_TP2 )
  179. // Key for ZigBee Conformance Testing
  180. 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
  181. 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
  182. #else
  183. // Key for In-House Testing
  184. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  185. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  186. #endif
  187. };
  188. // This is the default pre-configured Trust Center Link key,
  189. // change this to make a unique key, SEC_KEY_LEN is defined in ssp.h.
  190. CONST byte defaultTCLinkKey[SEC_KEY_LEN] =
  191. {
  192. 0x56, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77,
  193. 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77
  194. };
  195. /*********************************************************************
  196. * STATUS STRINGS
  197. */
  198. #if defined ( LCD_SUPPORTED )
  199. const char PingStr[] = "Ping Rcvd from";
  200. const char AssocCnfStr[] = "Assoc Cnf";
  201. const char SuccessStr[] = "Success";
  202. const char EndDeviceStr[] = "EndDevice:";
  203. const char ParentStr[] = "Parent:";
  204. const char ZigbeeCoordStr[] = "ZigBee Coord";
  205. const char NetworkIDStr[] = "Network ID:";
  206. const char RouterStr[] = "Router:";
  207. const char OrphanRspStr[] = "Orphan Response";
  208. const char SentStr[] = "Sent";
  209. const char FailedStr[] = "Failed";
  210. const char AssocRspFailStr[] = "Assoc Rsp fail";
  211. const char AssocIndStr[] = "Assoc Ind";
  212. const char AssocCnfFailStr[] = "Assoc Cnf fail";
  213. const char EnergyLevelStr[] = "Energy Level";
  214. const char ScanFailedStr[] = "Scan Failed";
  215. #endif
  216. /*********************************************************************
  217. * @fn nwk_globals_init()
  218. *
  219. * @brief
  220. *
  221. * Initialize nwk layer globals. These are the system defaults and
  222. * should be changed by the user here. The default definitions are
  223. * defined in nwk.h or NLMEDE.h.
  224. *
  225. * @param none
  226. *
  227. * @return none
  228. */
  229. void nwk_globals_init( void )
  230. {
  231. AddrMgrInit( NWK_MAX_ADDRESSES );
  232. #if !defined ( ZIGBEE_STOCHASTIC_ADDRESSING )
  233. if ( ZSTACK_ROUTER_BUILD )
  234. {
  235. // Initialize the Cskip Table
  236. Cskip = osal_mem_alloc(sizeof(uint16) *(MAX_NODE_DEPTH+1));
  237. RTG_FillCSkipTable(CskipChldrn, CskipRtrs, MAX_NODE_DEPTH, Cskip);
  238. }
  239. #endif
  240. // To compile out the Link Status Feature, set NWK_LINK_STATUS_PERIOD
  241. // to 0 (compiler flag).
  242. if ( NWK_LINK_STATUS_PERIOD )
  243. {
  244. NLME_InitLinkStatus();
  245. }
  246. #if defined ( ZIGBEE_FREQ_AGILITY )
  247. NwkFreqAgilityInit();
  248. #endif
  249. }
  250. /*********************************************************************
  251. * @fn NIB_init()
  252. *
  253. * @brief
  254. *
  255. * Initialize attribute values in NIB
  256. *
  257. * @param none
  258. *
  259. * @return none
  260. */
  261. void NIB_init()
  262. {
  263. _NIB.SequenceNum = LO_UINT16(osal_rand());
  264. _NIB.nwkProtocolVersion = ZB_PROT_VERS;
  265. _NIB.MaxDepth = MAX_NODE_DEPTH;
  266. #if ( NWK_MODE == NWK_MODE_MESH )
  267. _NIB.beaconOrder = BEACON_ORDER_NO_BEACONS;
  268. _NIB.superFrameOrder = BEACON_ORDER_NO_BEACONS;
  269. #endif
  270. // BROADCAST SETTINGS:
  271. // *******************
  272. // Broadcast Delivery Time
  273. // - set to multiples of 100ms
  274. // - should be 500ms more than the retry time
  275. // - "retry time" = PassiveAckTimeout * (MaxBroadcastRetries + 1)
  276. // Passive Ack Timeout
  277. // - set to multiples of 100ms
  278. _NIB.BroadcastDeliveryTime = zgBcastDeliveryTime;
  279. _NIB.PassiveAckTimeout = zgPassiveAckTimeout;
  280. _NIB.MaxBroadcastRetries = zgMaxBcastRetires;
  281. _NIB.ReportConstantCost = 0;
  282. _NIB.RouteDiscRetries = 0;
  283. _NIB.SecureAllFrames = USE_NWK_SECURITY;
  284. if ( ZG_SECURE_ENABLED )
  285. {
  286. _NIB.SecurityLevel = SECURITY_LEVEL;
  287. }
  288. else
  289. {
  290. _NIB.SecurityLevel = 0;
  291. }
  292. #if defined ( ZIGBEEPRO )
  293. _NIB.SymLink = FALSE;
  294. #else
  295. _NIB.SymLink = TRUE;
  296. #endif
  297. _NIB.CapabilityInfo = ZDO_Config_Node_Descriptor.CapabilityFlags;
  298. _NIB.TransactionPersistenceTime = zgIndirectMsgTimeout;
  299. _NIB.RouteDiscoveryTime = 5;
  300. _NIB.RouteExpiryTime = zgRouteExpiryTime;
  301. _NIB.nwkDevAddress = INVALID_NODE_ADDR;
  302. _NIB.nwkLogicalChannel = 0;
  303. _NIB.nwkCoordAddress = INVALID_NODE_ADDR;
  304. osal_memset( _NIB.nwkCoordExtAddress, 0, Z_EXTADDR_LEN );
  305. _NIB.nwkPanId = INVALID_NODE_ADDR;
  306. osal_cpyExtAddr( _NIB.extendedPANID, zgExtendedPANID );
  307. _NIB.nwkKeyLoaded = FALSE;
  308. #if defined ( ZIGBEE_STOCHASTIC_ADDRESSING )
  309. _NIB.nwkAddrAlloc = NWK_ADDRESSING_STOCHASTIC;
  310. _NIB.nwkUniqueAddr = FALSE;
  311. #else
  312. _NIB.nwkAddrAlloc = NWK_ADDRESSING_DISTRIBUTED;
  313. _NIB.nwkUniqueAddr = TRUE;
  314. #endif
  315. _NIB.nwkLinkStatusPeriod = NWK_LINK_STATUS_PERIOD;
  316. _NIB.nwkRouterAgeLimit = NWK_ROUTE_AGE_LIMIT;
  317. //MTO routing
  318. _NIB.nwkConcentratorDiscoveryTime = zgConcentratorDiscoveryTime;
  319. _NIB.nwkIsConcentrator = 0;
  320. _NIB.nwkConcentratorRadius = 0x0a;
  321. _NIB.nwkMaxSourceRoute = 0x0c;
  322. #if defined ( ZIGBEE_MULTICAST )
  323. _NIB.nwkUseMultiCast = TRUE;
  324. #else
  325. _NIB.nwkUseMultiCast = FALSE;
  326. #endif
  327. _NIB.nwkManagerAddr = 0x0000;
  328. _NIB.nwkUpdateId = 0;
  329. _NIB.nwkTotalTransmissions = 0;
  330. if ( ZSTACK_ROUTER_BUILD )
  331. {
  332. #if defined ( ZIGBEE_STOCHASTIC_ADDRESSING )
  333. NLME_InitStochasticAddressing();
  334. #else
  335. NLME_InitTreeAddressing();
  336. #endif
  337. }
  338. }
  339. /*********************************************************************
  340. * @fn nwk_Status()
  341. *
  342. * @brief
  343. *
  344. * Status report.
  345. *
  346. * @param statusCode
  347. * @param statusValue
  348. *
  349. * @return none
  350. */
  351. void nwk_Status( uint16 statusCode, uint16 statusValue )
  352. {
  353. #if defined ( LCD_SUPPORTED )
  354. switch ( statusCode )
  355. {
  356. case NWK_STATUS_COORD_ADDR:
  357. if ( ZSTACK_ROUTER_BUILD )
  358. {
  359. HalLcdWriteString( (char*)ZigbeeCoordStr, HAL_LCD_LINE_1 );
  360. HalLcdWriteStringValue( (char*)NetworkIDStr, statusValue, 16, HAL_LCD_LINE_2 );
  361. BuzzerControl( BUZZER_BLIP );
  362. }
  363. break;
  364. case NWK_STATUS_ROUTER_ADDR:
  365. if ( ZSTACK_ROUTER_BUILD )
  366. {
  367. HalLcdWriteStringValue( (char*)RouterStr, statusValue, 16, HAL_LCD_LINE_1 );
  368. }
  369. break;
  370. case NWK_STATUS_ORPHAN_RSP:
  371. if ( ZSTACK_ROUTER_BUILD )
  372. {
  373. if ( statusValue == ZSuccess )
  374. HalLcdWriteScreen( (char*)OrphanRspStr, (char*)SentStr );
  375. else
  376. HalLcdWriteScreen( (char*)OrphanRspStr, (char*)FailedStr );
  377. }
  378. break;
  379. case NWK_ERROR_ASSOC_RSP:
  380. if ( ZSTACK_ROUTER_BUILD )
  381. {
  382. HalLcdWriteString( (char*)AssocRspFailStr, HAL_LCD_LINE_1 );
  383. HalLcdWriteValue( (uint32)(statusValue), 16, HAL_LCD_LINE_2 );
  384. }
  385. break;
  386. case NWK_STATUS_ED_ADDR:
  387. if ( ZSTACK_END_DEVICE_BUILD )
  388. {
  389. HalLcdWriteStringValue( (char*)EndDeviceStr, statusValue, 16, HAL_LCD_LINE_1 );
  390. }
  391. break;
  392. case NWK_STATUS_PARENT_ADDR:
  393. HalLcdWriteStringValue( (char*)ParentStr, statusValue, 16, HAL_LCD_LINE_2 );
  394. break;
  395. case NWK_STATUS_ASSOC_CNF:
  396. HalLcdWriteScreen( (char*)AssocCnfStr, (char*)SuccessStr );
  397. break;
  398. case NWK_ERROR_ASSOC_CNF_DENIED:
  399. HalLcdWriteString((char*)AssocCnfFailStr, HAL_LCD_LINE_1 );
  400. HalLcdWriteValue( (uint32)(statusValue), 16, HAL_LCD_LINE_2 );
  401. break;
  402. case NWK_ERROR_ENERGY_SCAN_FAILED:
  403. HalLcdWriteScreen( (char*)EnergyLevelStr, (char*)ScanFailedStr );
  404. break;
  405. }
  406. #endif
  407. }
  408. /*********************************************************************
  409. *********************************************************************/