ZGlobals.c 19 KB

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