ZGlobals.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. /**************************************************************************************************
  2. Filename: ZGlobals.c
  3. Revised: $Date: 2012-04-02 17:02:19 -0700 (Mon, 02 Apr 2012) $
  4. Revision: $Revision: 29996 $
  5. Description: User definable Z-Stack parameters.
  6. Copyright 2007-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. /*********************************************************************
  34. * INCLUDES
  35. */
  36. #include "ZComDef.h"
  37. #include "OSAL_Nv.h"
  38. #include "ZDObject.h"
  39. #include "ZGlobals.h"
  40. #include "ZDNwkMgr.h"
  41. #include "OnBoard.h"
  42. #include "ZDSecMgr.h"
  43. /*********************************************************************
  44. * MACROS
  45. */
  46. /*********************************************************************
  47. * CONSTANTS
  48. */
  49. /*********************************************************************
  50. * TYPEDEFS
  51. */
  52. typedef struct zgItem
  53. {
  54. uint16 id;
  55. uint16 len;
  56. void *buf;
  57. } zgItem_t;
  58. /*********************************************************************
  59. * NWK GLOBAL VARIABLES
  60. */
  61. // Polling values
  62. uint16 zgPollRate = POLL_RATE;
  63. uint16 zgQueuedPollRate = QUEUED_POLL_RATE;
  64. uint16 zgResponsePollRate = RESPONSE_POLL_RATE;
  65. uint16 zgRejoinPollRate = REJOIN_POLL_RATE;
  66. // Transmission retries numbers
  67. uint8 zgMaxDataRetries = NWK_MAX_DATA_RETRIES;
  68. uint8 zgMaxPollFailureRetries = MAX_POLL_FAILURE_RETRIES;
  69. // Default channel list
  70. uint32 zgDefaultChannelList = DEFAULT_CHANLIST;
  71. // Default starting scan duration
  72. uint8 zgDefaultStartingScanDuration = STARTING_SCAN_DURATION;
  73. // Stack profile Id
  74. uint8 zgStackProfile = STACK_PROFILE_ID;
  75. // Default indirect message holding timeout
  76. uint8 zgIndirectMsgTimeout = NWK_INDIRECT_MSG_TIMEOUT;
  77. // Security mode
  78. uint8 zgSecurityMode = ZG_SECURITY_MODE;
  79. // Secure permit join
  80. uint8 zgSecurePermitJoin = true;
  81. // Trust center address
  82. uint16 zgTrustCenterAddr = ZG_TRUSTCENTER_ADDR;
  83. // Route Discovery Time - amount of time that a route request lasts
  84. uint8 zgRouteDiscoveryTime = ROUTE_DISCOVERY_TIME;
  85. // Route expiry
  86. uint8 zgRouteExpiryTime = ROUTE_EXPIRY_TIME;
  87. // Extended PAN Id
  88. uint8 zgExtendedPANID[Z_EXTADDR_LEN];
  89. // Broadcast parameters
  90. uint8 zgMaxBcastRetires = MAX_BCAST_RETRIES;
  91. uint8 zgPassiveAckTimeout = PASSIVE_ACK_TIMEOUT;
  92. uint8 zgBcastDeliveryTime = BCAST_DELIVERY_TIME;
  93. // Network mode
  94. uint8 zgNwkMode = NWK_MODE;
  95. // Many-to-one values
  96. uint8 zgConcentratorEnable = CONCENTRATOR_ENABLE;
  97. uint8 zgConcentratorDiscoveryTime = CONCENTRATOR_DISCOVERY_TIME;
  98. uint8 zgConcentratorRadius = CONCENTRATOR_RADIUS;
  99. uint8 zgConcentratorRC = CONCENTRATOR_ROUTE_CACHE; // concentrator with route cache (no memory constraints)
  100. uint8 zgNwkSrcRtgExpiryTime = SRC_RTG_EXPIRY_TIME;
  101. // Cleanup Child Table according to routing traffic
  102. uint8 zgRouterOffAssocCleanup = FALSE;
  103. /*********************************************************************
  104. * APS GLOBAL VARIABLES
  105. */
  106. // The maximum number of retries allowed after a transmission failure
  107. uint8 zgApscMaxFrameRetries = APSC_MAX_FRAME_RETRIES;
  108. // The maximum number of seconds (milliseconds) to wait for an
  109. // acknowledgement to a transmitted frame.
  110. // This number is used by polled devices.
  111. uint16 zgApscAckWaitDurationPolled = APSC_ACK_WAIT_DURATION_POLLED;
  112. // This number is used by non-polled devices in the following formula:
  113. // (100 mSec) * (_NIB.MaxDepth * zgApsAckWaitMultiplier)
  114. uint8 zgApsAckWaitMultiplier = 2;
  115. // The maximum number of milliseconds for the end device binding
  116. uint16 zgApsDefaultMaxBindingTime = APS_DEFAULT_MAXBINDING_TIME;
  117. // The 64-big identifier of the network to join or form.
  118. // Default set to all zeros
  119. uint8 zgApsUseExtendedPANID[Z_EXTADDR_LEN] = {00,00,00,00,00,00,00,00};
  120. // A boolean flag that indicates whether it is OK to use insecure join
  121. // on startup. Default set to true
  122. uint8 zgApsUseInsecureJoin = TRUE;
  123. // The radius of broadcast multicast transmissions
  124. uint8 zgApsNonMemberRadius = APS_DEFAULT_NONMEMBER_RADIUS;
  125. /*********************************************************************
  126. * SECURITY GLOBAL VARIABLES
  127. */
  128. // If true, preConfigKey should be configured on all devices on the network
  129. // If false, it is configured only on the coordinator and sent to other
  130. // devices upon joining.
  131. uint8 zgPreConfigKeys = FALSE;// TRUE;
  132. // If true, defaultTCLinkKey should be configured on all devices on the
  133. // network. If false, individual trust center link key between each device and
  134. // the trust center should be manually configured via MT_WRITE_NV
  135. uint8 zgUseDefaultTCLK = TRUE; // FALSE
  136. /*********************************************************************
  137. * ZDO GLOBAL VARIABLES
  138. */
  139. // Configured PAN ID
  140. uint16 zgConfigPANID = ZDAPP_CONFIG_PAN_ID;
  141. // Device Logical Type
  142. uint8 zgDeviceLogicalType = DEVICE_LOGICAL_TYPE;
  143. // Startup Delay
  144. uint8 zgStartDelay = START_DELAY;
  145. #if !defined MT_TASK
  146. // Flag to use verbose (i.e. "cc2480-style") direct MT callbacks in ZDProfile.c, ZDP_IncomingData().
  147. uint8 zgZdoDirectCB = FALSE;
  148. #endif
  149. // Min number of attempted transmissions for Channel Interference detection
  150. uint8 zgNwkMgrMinTransmissions = ZDNWKMGR_MIN_TRANSMISSIONS;
  151. /*********************************************************************
  152. * APPLICATION GLOBAL VARIABLES
  153. */
  154. // Network Manager Mode
  155. uint8 zgNwkMgrMode = ZDNWKMGR_ENABLE;
  156. /*********************************************************************
  157. * NON-STANDARD GLOBAL VARIABLES
  158. */
  159. // Simple API Endpoint
  160. uint8 zgSapiEndpoint = SAPI_ENDPOINT;
  161. /*********************************************************************
  162. * LOCAL VARIABLES
  163. */
  164. /*********************************************************************
  165. * ZGlobal Item Table
  166. */
  167. static CONST zgItem_t zgItemTable[] =
  168. {
  169. #if defined ( NV_INIT )
  170. #if !defined MT_TASK
  171. {
  172. ZCD_NV_ZDO_DIRECT_CB, sizeof(zgZdoDirectCB), &zgZdoDirectCB
  173. },
  174. #endif
  175. {
  176. ZCD_NV_LOGICAL_TYPE, sizeof(zgDeviceLogicalType), &zgDeviceLogicalType
  177. },
  178. {
  179. ZCD_NV_POLL_RATE, sizeof(zgPollRate), &zgPollRate
  180. },
  181. {
  182. ZCD_NV_QUEUED_POLL_RATE, sizeof(zgQueuedPollRate), &zgQueuedPollRate
  183. },
  184. {
  185. ZCD_NV_RESPONSE_POLL_RATE, sizeof(zgResponsePollRate), &zgResponsePollRate
  186. },
  187. {
  188. ZCD_NV_REJOIN_POLL_RATE, sizeof(zgRejoinPollRate), &zgRejoinPollRate
  189. },
  190. {
  191. ZCD_NV_DATA_RETRIES, sizeof(zgMaxDataRetries), &zgMaxDataRetries
  192. },
  193. {
  194. ZCD_NV_POLL_FAILURE_RETRIES, sizeof(zgMaxPollFailureRetries), &zgMaxPollFailureRetries
  195. },
  196. {
  197. ZCD_NV_CHANLIST, sizeof(zgDefaultChannelList), &zgDefaultChannelList
  198. },
  199. {
  200. ZCD_NV_SCAN_DURATION, sizeof(zgDefaultStartingScanDuration), &zgDefaultStartingScanDuration
  201. },
  202. {
  203. ZCD_NV_STACK_PROFILE, sizeof(zgStackProfile), &zgStackProfile
  204. },
  205. {
  206. ZCD_NV_INDIRECT_MSG_TIMEOUT, sizeof(zgIndirectMsgTimeout), &zgIndirectMsgTimeout
  207. },
  208. {
  209. ZCD_NV_ROUTE_EXPIRY_TIME, sizeof(zgRouteExpiryTime), &zgRouteExpiryTime
  210. },
  211. {
  212. ZCD_NV_EXTENDED_PAN_ID, Z_EXTADDR_LEN, zgExtendedPANID
  213. },
  214. {
  215. ZCD_NV_BCAST_RETRIES, sizeof(zgMaxBcastRetires), &zgMaxBcastRetires
  216. },
  217. {
  218. ZCD_NV_PASSIVE_ACK_TIMEOUT, sizeof(zgPassiveAckTimeout), &zgPassiveAckTimeout
  219. },
  220. {
  221. ZCD_NV_BCAST_DELIVERY_TIME, sizeof(zgBcastDeliveryTime), &zgBcastDeliveryTime
  222. },
  223. {
  224. ZCD_NV_NWK_MODE, sizeof(zgNwkMode), &zgNwkMode
  225. },
  226. {
  227. ZCD_NV_CONCENTRATOR_ENABLE, sizeof(zgConcentratorEnable), &zgConcentratorEnable
  228. },
  229. {
  230. ZCD_NV_CONCENTRATOR_DISCOVERY, sizeof(zgConcentratorDiscoveryTime), &zgConcentratorDiscoveryTime
  231. },
  232. {
  233. ZCD_NV_CONCENTRATOR_RADIUS, sizeof(zgConcentratorRadius), &zgConcentratorRadius
  234. },
  235. {
  236. ZCD_NV_CONCENTRATOR_RC, sizeof(zgConcentratorRC), &zgConcentratorRC
  237. },
  238. {
  239. ZCD_NV_SRC_RTG_EXPIRY_TIME, sizeof(zgNwkSrcRtgExpiryTime), &zgNwkSrcRtgExpiryTime
  240. },
  241. {
  242. ZCD_NV_ROUTE_DISCOVERY_TIME, sizeof(zgRouteDiscoveryTime), &zgRouteDiscoveryTime
  243. },
  244. #ifndef NONWK
  245. {
  246. ZCD_NV_PANID, sizeof(zgConfigPANID), &zgConfigPANID
  247. },
  248. {
  249. ZCD_NV_PRECFGKEYS_ENABLE, sizeof(zgPreConfigKeys), &zgPreConfigKeys
  250. },
  251. {
  252. ZCD_NV_SECURITY_MODE, sizeof(zgSecurityMode), &zgSecurityMode
  253. },
  254. {
  255. ZCD_NV_SECURE_PERMIT_JOIN, sizeof(zgSecurePermitJoin), &zgSecurePermitJoin
  256. },
  257. {
  258. ZCD_NV_USE_DEFAULT_TCLK, sizeof(zgUseDefaultTCLK), &zgUseDefaultTCLK
  259. },
  260. {
  261. ZCD_NV_TRUSTCENTER_ADDR, sizeof(zgTrustCenterAddr), &zgTrustCenterAddr
  262. },
  263. #endif // NONWK
  264. {
  265. ZCD_NV_APS_FRAME_RETRIES, sizeof(zgApscMaxFrameRetries), &zgApscMaxFrameRetries
  266. },
  267. {
  268. ZCD_NV_APS_ACK_WAIT_DURATION, sizeof(zgApscAckWaitDurationPolled), &zgApscAckWaitDurationPolled
  269. },
  270. {
  271. ZCD_NV_APS_ACK_WAIT_MULTIPLIER, sizeof(zgApsAckWaitMultiplier), &zgApsAckWaitMultiplier
  272. },
  273. {
  274. ZCD_NV_BINDING_TIME, sizeof(zgApsDefaultMaxBindingTime), &zgApsDefaultMaxBindingTime
  275. },
  276. {
  277. ZCD_NV_APS_USE_EXT_PANID, Z_EXTADDR_LEN, zgApsUseExtendedPANID
  278. },
  279. {
  280. ZCD_NV_APS_USE_INSECURE_JOIN, sizeof(zgApsUseInsecureJoin), &zgApsUseInsecureJoin
  281. },
  282. {
  283. ZCD_NV_APS_NONMEMBER_RADIUS, sizeof(zgApsNonMemberRadius), &zgApsNonMemberRadius
  284. },
  285. {
  286. ZCD_NV_START_DELAY, sizeof(zgStartDelay), &zgStartDelay
  287. },
  288. {
  289. ZCD_NV_SAPI_ENDPOINT, sizeof(zgSapiEndpoint), &zgSapiEndpoint
  290. },
  291. {
  292. ZCD_NV_NWK_MGR_MODE, sizeof(zgNwkMgrMode), &zgNwkMgrMode
  293. },
  294. {
  295. ZCD_NV_NWKMGR_MIN_TX, sizeof(zgNwkMgrMinTransmissions), &zgNwkMgrMinTransmissions
  296. },
  297. {
  298. ZCD_NV_ROUTER_OFF_ASSOC_CLEANUP, sizeof(zgRouterOffAssocCleanup), &zgRouterOffAssocCleanup
  299. },
  300. #endif // NV_INIT
  301. // Last item -- DO NOT MOVE IT!
  302. {
  303. 0x00, 0, NULL
  304. }
  305. };
  306. /*********************************************************************
  307. * LOCAL FUNCTIONS
  308. */
  309. static uint8 zgItemInit( uint16 id, uint16 len, void *buf, uint8 setDefault );
  310. #ifndef NONWK
  311. static uint8 zgPreconfigKeyInit( uint8 setDefault );
  312. #endif
  313. /*********************************************************************
  314. * @fn zgItemInit()
  315. *
  316. * @brief
  317. *
  318. * Initialize a global item. If the item doesn't exist in NV memory,
  319. * write the system default (value passed in) into NV memory. But if
  320. * it exists, set the item to the value stored in NV memory.
  321. *
  322. * Also, if setDefault is TRUE and the item exists, we will write
  323. * the default value to NV space.
  324. *
  325. * @param id - item id
  326. * @param len - item len
  327. * @param buf - pointer to the item
  328. * @param setDefault - TRUE to set default, not read
  329. *
  330. * @return ZSUCCESS if successful, NV_ITEM_UNINIT if item did not
  331. * exist in NV, NV_OPER_FAILED if failure.
  332. */
  333. static uint8 zgItemInit( uint16 id, uint16 len, void *buf, uint8 setDefault )
  334. {
  335. uint8 status;
  336. // If the item doesn't exist in NV memory, create and initialize
  337. // it with the value passed in.
  338. status = osal_nv_item_init( id, len, buf );
  339. if ( status == ZSUCCESS )
  340. {
  341. if ( setDefault )
  342. {
  343. // Write the default value back to NV
  344. status = osal_nv_write( id, 0, len, buf );
  345. }
  346. else
  347. {
  348. // The item exists in NV memory, read it from NV memory
  349. status = osal_nv_read( id, 0, len, buf );
  350. }
  351. }
  352. return (status);
  353. }
  354. /*********************************************************************
  355. * API FUNCTIONS
  356. */
  357. /*********************************************************************
  358. * @fn zgInit
  359. *
  360. * @brief
  361. *
  362. * Initialize the Z-Stack Globals. If an item doesn't exist in
  363. * NV memory, write the system default into NV memory. But if
  364. * it exists, set the item to the value stored in NV memory.
  365. *
  366. * NOTE: The Startup Options (ZCD_NV_STARTUP_OPTION) indicate
  367. * that the Config state items (zgItemTable) need to be
  368. * set to defaults (ZCD_STARTOPT_DEFAULT_CONFIG_STATE). The
  369. *
  370. * @param none
  371. *
  372. * @return ZSUCCESS if successful, NV_ITEM_UNINIT if item did not
  373. * exist in NV, NV_OPER_FAILED if failure.
  374. */
  375. uint8 zgInit( void )
  376. {
  377. uint8 setDefault = FALSE;
  378. // Do we want to default the Config state values
  379. if ( zgReadStartupOptions() & ZCD_STARTOPT_DEFAULT_CONFIG_STATE )
  380. {
  381. setDefault = TRUE;
  382. }
  383. #if 0
  384. // Enable this section if you need to track the number of resets
  385. // This section is normally disabled to minimize "wear" on NV memory
  386. uint16 bootCnt = 0;
  387. // Update the Boot Counter
  388. if ( osal_nv_item_init( ZCD_NV_BOOTCOUNTER, sizeof(bootCnt), &bootCnt ) == ZSUCCESS )
  389. {
  390. // Get the old value from NV memory
  391. osal_nv_read( ZCD_NV_BOOTCOUNTER, 0, sizeof(bootCnt), &bootCnt );
  392. }
  393. // Increment the Boot Counter and store it into NV memory
  394. if ( setDefault )
  395. bootCnt = 0;
  396. else
  397. bootCnt++;
  398. osal_nv_write( ZCD_NV_BOOTCOUNTER, 0, sizeof(bootCnt), &bootCnt );
  399. #endif
  400. // Initialize the Extended PAN ID as my own extended address
  401. ZMacGetReq( ZMacExtAddr, zgExtendedPANID );
  402. // Initialize the items table
  403. zgInitItems( setDefault );
  404. #ifndef NONWK
  405. if ( ZG_SECURE_ENABLED )
  406. {
  407. // Initialize the Pre-Configured Key to the default key
  408. zgPreconfigKeyInit( setDefault );
  409. // Initialize NV items for all Keys: NWK, APS, TCLK and Master
  410. ZDSecMgrInitNVKeyTables( setDefault );
  411. }
  412. #endif // NONWK
  413. // Clear the Config State default
  414. if ( setDefault )
  415. {
  416. zgWriteStartupOptions( ZG_STARTUP_CLEAR, ZCD_STARTOPT_DEFAULT_CONFIG_STATE );
  417. }
  418. return ( ZSUCCESS );
  419. }
  420. /*********************************************************************
  421. * @fn zgInitItems
  422. *
  423. * @brief Initializes RAM variables from NV. If NV items don't
  424. * exist, then the NV is initialize with what is in RAM
  425. * variables.
  426. *
  427. * @param none
  428. *
  429. * @return none
  430. */
  431. void zgInitItems( uint8 setDefault )
  432. {
  433. uint8 i = 0;
  434. while ( zgItemTable[i].id != 0x00 )
  435. {
  436. // Initialize the item
  437. zgItemInit( zgItemTable[i].id, zgItemTable[i].len, zgItemTable[i].buf, setDefault );
  438. // Move on to the next item
  439. i++;
  440. }
  441. }
  442. /*********************************************************************
  443. * @fn zgReadStartupOptions
  444. *
  445. * @brief Reads the ZCD_NV_STARTUP_OPTION NV Item.
  446. *
  447. * @param none
  448. *
  449. * @return the ZCD_NV_STARTUP_OPTION NV item
  450. */
  451. uint8 zgReadStartupOptions( void )
  452. {
  453. // Default to Use Config State and Use Network State
  454. uint8 startupOption = 0;
  455. // This should have been done in ZMain.c, but just in case.
  456. if ( osal_nv_item_init( ZCD_NV_STARTUP_OPTION,
  457. sizeof(startupOption),
  458. &startupOption ) == ZSUCCESS )
  459. {
  460. // Read saved startup control
  461. osal_nv_read( ZCD_NV_STARTUP_OPTION,
  462. 0,
  463. sizeof( startupOption ),
  464. &startupOption);
  465. }
  466. return ( startupOption );
  467. }
  468. /*********************************************************************
  469. * @fn zgWriteStartupOptions
  470. *
  471. * @brief Writes bits into the ZCD_NV_STARTUP_OPTION NV Item.
  472. *
  473. * @param action - ZG_STARTUP_SET set bit, ZG_STARTUP_CLEAR to
  474. * clear bit. The set bit is an OR operation, and the
  475. * clear bit is an AND ~(bitOptions) operation.
  476. *
  477. * @param bitOptions - which bits to perform action on:
  478. * ZCD_STARTOPT_DEFAULT_CONFIG_STATE
  479. * ZCD_STARTOPT_DEFAULT_NETWORK_STATE
  480. *
  481. * @return ZSUCCESS if successful
  482. */
  483. uint8 zgWriteStartupOptions( uint8 action, uint8 bitOptions )
  484. {
  485. uint8 status;
  486. uint8 startupOptions = 0;
  487. status = osal_nv_read( ZCD_NV_STARTUP_OPTION,
  488. 0,
  489. sizeof( startupOptions ),
  490. &startupOptions );
  491. if ( status == ZSUCCESS )
  492. {
  493. if ( action == ZG_STARTUP_SET )
  494. {
  495. // Set bits
  496. startupOptions |= bitOptions;
  497. }
  498. else
  499. {
  500. // Clear bits
  501. startupOptions &= (bitOptions ^ 0xFF);
  502. }
  503. // Changed?
  504. status = osal_nv_write( ZCD_NV_STARTUP_OPTION,
  505. 0,
  506. sizeof( startupOptions ),
  507. &startupOptions );
  508. }
  509. return ( status );
  510. }
  511. /*********************************************************************
  512. * @fn zgSetItem
  513. *
  514. * @brief Set RAM variables from set-NV, if it exist in the zgItemTable
  515. *
  516. * @param id - NV ID
  517. * len - NV item length
  518. * buf - pointer to the input buffer
  519. *
  520. * @return none
  521. */
  522. void zgSetItem( uint16 id, uint16 len, void *buf )
  523. {
  524. uint8 i = 0;
  525. // Look up the NV item table
  526. while ( zgItemTable[i].id != 0x00 )
  527. {
  528. if( zgItemTable[i].id == id )
  529. {
  530. if ( zgItemTable[i].len == len )
  531. {
  532. osal_memcpy( zgItemTable[i].buf, buf, len );
  533. }
  534. break;
  535. }
  536. // Move on to the next item
  537. i++;
  538. }
  539. }
  540. #ifndef NONWK
  541. /*********************************************************************
  542. * @fn zgPreconfigKeyInit()
  543. *
  544. * @brief
  545. *
  546. * Initialize ZCD_NV_PRECFGKEY NV item. If the item doesn't exist in NV memory,
  547. * write the system default (value passed in) into NV memory. But if
  548. * it exists do not overwrite it.
  549. *
  550. * Also, if setDefault is TRUE and the item exists, we will write
  551. * the default value to NV space.
  552. *
  553. * @param setDefault - TRUE to set default
  554. *
  555. * @return ZSUCCESS if successful, NV_ITEM_UNINIT if item did not
  556. * exist in NV, NV_OPER_FAILED if failure.
  557. */
  558. static uint8 zgPreconfigKeyInit( uint8 setDefault )
  559. {
  560. uint8 zgPreConfigKey[SEC_KEY_LEN];
  561. uint8 status;
  562. // Initialize the Pre-Configured Key to the default key
  563. osal_memcpy( zgPreConfigKey, defaultKey, SEC_KEY_LEN );
  564. // If the item doesn't exist in NV memory, create and initialize it
  565. status = osal_nv_item_init( ZCD_NV_PRECFGKEY, SEC_KEY_LEN, zgPreConfigKey );
  566. if ( status == ZSUCCESS )
  567. {
  568. if ( setDefault )
  569. {
  570. // Write the default value back to NV
  571. status = osal_nv_write( ZCD_NV_PRECFGKEY, 0, SEC_KEY_LEN, zgPreConfigKey );
  572. }
  573. }
  574. // clear local copy of default key
  575. osal_memset(zgPreConfigKey, 0x00, SEC_KEY_LEN);
  576. return (status);
  577. }
  578. #endif
  579. /*********************************************************************
  580. *********************************************************************/