zcl_ccserver.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232
  1. /**************************************************************************************************
  2. Filename: zcl_ccserver.c
  3. Revised: $Date: 2011-06-03 13:17:26 -0700 (Fri, 03 Jun 2011) $
  4. Revision: $Revision: 26202 $
  5. Description: Zigbee Cluster Library - Commissioning Cluster Server Application.
  6. Copyright 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. This device will be like an On/Off Switch device. This application
  35. is not intended to be a On/Off Switch device, but will use the device
  36. description to implement this sample code.
  37. *********************************************************************/
  38. /*********************************************************************
  39. * INCLUDES
  40. */
  41. #include "ZComDef.h"
  42. #include "OSAL.h"
  43. #include "OSAL_Nv.h"
  44. #include "ZDApp.h"
  45. #include "zcl.h"
  46. #include "zba.h"
  47. #include "zcl_cc.h"
  48. #include "zcl_ccserver.h"
  49. #include "onboard.h"
  50. /* HAL */
  51. #include "hal_lcd.h"
  52. #include "hal_led.h"
  53. #include "hal_key.h"
  54. /*********************************************************************
  55. * MACROS
  56. */
  57. #define nullExtendedPANID( EPID ) osal_isbufset( (EPID), 0x00, Z_EXTADDR_LEN )
  58. #define nullKey( key ) osal_isbufset( (key), 0x00, SEC_KEY_LEN )
  59. #define NvIdFromAttrId( attrId ) ( (attrId) == ATTRID_CC_TRUST_CENTER_MASTER_KEY ? \
  60. ZCD_NV_SAS_CURR_TC_MASTER_KEY : \
  61. (attrId) == ATTRID_CC_NETWORK_KEY ? \
  62. ZCD_NV_SAS_CURR_NWK_KEY : \
  63. ZCD_NV_SAS_CURR_PRECFG_LINK_KEY )
  64. /*********************************************************************
  65. * CONSTANTS
  66. */
  67. #define TO_JOIN_OPERATIONAL_NWK 0x01
  68. #define TO_JOIN_COMMISSIONING_NWK 0x02
  69. #define CCSERVER_REJOIN_TIMEOUT 2500 // 2.5 sec
  70. /*********************************************************************
  71. * TYPEDEFS
  72. */
  73. typedef struct zgItem
  74. {
  75. uint16 id;
  76. uint16 len;
  77. void *buf;
  78. } nvItem_t;
  79. /*********************************************************************
  80. * GLOBAL VARIABLES
  81. */
  82. uint8 zclCCServer_TaskID;
  83. /*********************************************************************
  84. * GLOBAL FUNCTIONS
  85. */
  86. /*********************************************************************
  87. * LOCAL VARIABLES
  88. */
  89. static uint8 leaveInitiated;
  90. static zclCCRestartDevice_t restartDevice;
  91. /*********************************************************************
  92. * ZCL CC Item Table
  93. */
  94. static CONST nvItem_t nvItemTable[] =
  95. {
  96. {
  97. ZCD_NV_SAS_SHORT_ADDR, sizeof( zclCCServer_ShortAddress ), &zclCCServer_ShortAddress
  98. },
  99. {
  100. ZCD_NV_SAS_EXT_PANID, Z_EXTADDR_LEN, zclCCServer_ExtendedPanId
  101. },
  102. {
  103. ZCD_NV_SAS_PANID, sizeof( zclCCServer_PanId ), &zclCCServer_PanId
  104. },
  105. {
  106. ZCD_NV_SAS_CHANNEL_MASK, sizeof( zclCCServer_ChannelMask ), &zclCCServer_ChannelMask
  107. },
  108. {
  109. ZCD_NV_SAS_PROTOCOL_VER, sizeof( zclCCServer_ProtocolVersion ), &zclCCServer_ProtocolVersion
  110. },
  111. {
  112. ZCD_NV_SAS_STACK_PROFILE, sizeof( zclCCServer_StackProfile ), &zclCCServer_StackProfile
  113. },
  114. {
  115. ZCD_NV_SAS_STARTUP_CTRL, sizeof( zclCCServer_StartUpControl ), &zclCCServer_StartUpControl
  116. },
  117. {
  118. ZCD_NV_SAS_TC_ADDR, Z_EXTADDR_LEN, zclCCServer_TrustCenterAddr
  119. },
  120. {
  121. ZCD_NV_SAS_USE_INSEC_JOIN, sizeof( zclCCServer_UseInsecureJoin ), &zclCCServer_UseInsecureJoin
  122. },
  123. {
  124. ZCD_NV_SAS_NWK_KEY_SEQ_NUM, sizeof(zclCCServer_NetworkKeySeqNum ), &zclCCServer_NetworkKeySeqNum
  125. },
  126. {
  127. ZCD_NV_SAS_NWK_KEY_TYPE, sizeof( zclCCServer_NetworkKeyType ), &zclCCServer_NetworkKeyType
  128. },
  129. {
  130. ZCD_NV_SAS_NWK_MGR_ADDR, sizeof( zclCCServer_NwkManagerAddr ), &zclCCServer_NwkManagerAddr
  131. },
  132. // Last item -- DO NOT MOVE IT!
  133. {
  134. 0x00, 0, NULL
  135. }
  136. };
  137. /*********************************************************************
  138. * LOCAL FUNCTIONS
  139. */
  140. static void zclCCServer_HandleKeys( uint8 shift, uint8 keys );
  141. static void zclCCServer_InitStartupParameters( uint8 initNv );
  142. static void zclCCServer_UseStartupParameters( void );
  143. // CC Cluster Callback functions
  144. static void zclCCServer_Restart_DeviceCB( zclCCRestartDevice_t *pCmd,
  145. afAddrType_t *srcAddr, uint8 seqNum );
  146. static void zclCCServer_Save_StartupParametersCB( zclCCStartupParams_t *pCmd,
  147. afAddrType_t *srcAddr, uint8 seqNum );
  148. static void zclCCServer_Restore_StartupParametersCB( zclCCStartupParams_t *pCmd,
  149. afAddrType_t *srcAddr, uint8 seqNum );
  150. static void zclCCServer_Reset_StartupParametersCB( zclCCStartupParams_t *pCmd,
  151. afAddrType_t *srcAddr, uint8 seqNum );
  152. static void zclCCServer_Restart_DeviceRspCB( zclCCServerParamsRsp_t *pRsp,
  153. afAddrType_t *srcAddr, uint8 seqNum );
  154. static void zclCCServer_Save_StartupParametersRspCB( zclCCServerParamsRsp_t *pRsp,
  155. afAddrType_t *srcAddr, uint8 seqNum );
  156. static void zclCCServer_Restore_StartupParametersRspCB( zclCCServerParamsRsp_t *pRsp,
  157. afAddrType_t *srcAddr, uint8 seqNum );
  158. static void zclCCServer_Reset_StartupParametersRspCB( zclCCServerParamsRsp_t *pRsp,
  159. afAddrType_t *srcAddr, uint8 seqNum );
  160. static ZStatus_t zclCCServer_SendLeaveReq( void );
  161. static void zclCCServer_SavePreconfigLinkKey( void );
  162. static uint8 zclCCServer_ValidateAttrDataCB( zclAttrRec_t *pAttr, zclWriteRec_t *pAttrInfo );
  163. static ZStatus_t zclCCServer_ReadWriteCB( uint16 clusterId, uint16 attrId,
  164. uint8 oper, uint8 *pValue, uint16 *pLen );
  165. static ZStatus_t zclCCServer_AuthorizeCB( afAddrType_t *srcAddr, zclAttrRec_t *pAttr, uint8 oper );
  166. static void *zclCCServer_ZdoLeaveCnfCB( void *pParam );
  167. static void zclCCServer_InitStartupParametersInNV( void );
  168. static void zclCCServer_ResetStartupParametersInNV( void );
  169. static void zclCCServer_SaveStartupParametersInNV(void);
  170. static void zclCCServer_RestoreStartupParametersInNV( void );
  171. /*********************************************************************
  172. * ZCL CC Clusters Callback table
  173. */
  174. static zclCC_AppCallbacks_t zclCCServer_CmdCallbacks =
  175. {
  176. zclCCServer_Restart_DeviceCB,
  177. zclCCServer_Save_StartupParametersCB,
  178. zclCCServer_Restore_StartupParametersCB,
  179. zclCCServer_Reset_StartupParametersCB,
  180. zclCCServer_Restart_DeviceRspCB,
  181. zclCCServer_Save_StartupParametersRspCB,
  182. zclCCServer_Restore_StartupParametersRspCB,
  183. zclCCServer_Reset_StartupParametersRspCB,
  184. };
  185. /*********************************************************************
  186. * @fn zclCCServer_Init
  187. *
  188. * @brief Initialization function for the ZCL Commissioing Cluster
  189. * Server Application.
  190. *
  191. * @param task_id - task id
  192. *
  193. * @return none
  194. */
  195. void zclCCServer_Init( uint8 task_id )
  196. {
  197. zclCCServer_TaskID = task_id;
  198. leaveInitiated = FALSE;
  199. // This app is part of the Home Automation Profile
  200. zba_Init( &zclCCServer_SimpleDesc );
  201. // Register the ZCL Commissioning Cluster Library callback functions
  202. zclCC_RegisterCmdCallbacks( CCSERVER_ENDPOINT, &zclCCServer_CmdCallbacks );
  203. // Register the application's attribute list
  204. zcl_registerAttrList( CCSERVER_ENDPOINT, CCSERVER_MAX_ATTRIBUTES, zclCCServer_Attrs );
  205. // Register the application's attribute data validation callback function
  206. zcl_registerValidateAttrData( zclCCServer_ValidateAttrDataCB );
  207. // Register the application's callback function to read/write attribute data
  208. zcl_registerReadWriteCB( CCSERVER_ENDPOINT, zclCCServer_ReadWriteCB, zclCCServer_AuthorizeCB );
  209. // Register for Initiator to receive Leave Confirm
  210. ZDO_RegisterForZdoCB( ZDO_LEAVE_CNF_CBID, zclCCServer_ZdoLeaveCnfCB );
  211. // Register for all key events - This app will handle all key events
  212. RegisterForKeys( zclCCServer_TaskID );
  213. // Initialize ZBA Startup Attributes Set (SAS)
  214. zclCCServer_InitStartupParameters( TRUE );
  215. // See if the device is factory new
  216. if ( !ZDApp_DeviceConfigured() )
  217. {
  218. osal_nv_item_init( ZCD_NV_NWKMGR_ADDR, sizeof( zclCCServer_NwkManagerAddr ),
  219. (void *)&zclCCServer_NwkManagerAddr );
  220. // On startup, attempt to join the network specified by the startup SAS
  221. // on all channels at least once
  222. // ZBA Default Settings with the default ZBA Key Material and ZBA EPID
  223. if ( nullExtendedPANID( zgApsUseExtendedPANID ) )
  224. {
  225. osal_cpyExtAddr( zgApsUseExtendedPANID, zbaGlobalCommissioningEPID );
  226. }
  227. // Default Network Key and Pre-configured Link Key should already be set
  228. }
  229. }
  230. /*********************************************************************
  231. * @fn zclServerApp_event_loop
  232. *
  233. * @brief Event Loop Processor for the ZCL Commissioing Cluster
  234. * Server Application.
  235. *
  236. * @param task_id - task id
  237. * @param events - event bitmap
  238. *
  239. * @return unprocessed events
  240. */
  241. uint16 zclCCServer_event_loop( uint8 task_id, uint16 events )
  242. {
  243. (void)task_id; // Intentionally unreferenced parameter
  244. if ( events & SYS_EVENT_MSG )
  245. {
  246. afIncomingMSGPacket_t *MSGpkt;
  247. while ( (MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( zclCCServer_TaskID )) )
  248. {
  249. switch ( MSGpkt->hdr.event )
  250. {
  251. case ZCL_INCOMING_MSG:
  252. // Incoming ZCL Foundation command/response messages
  253. break;
  254. case ZDO_STATE_CHANGE:
  255. // Process State Change messages
  256. break;
  257. case KEY_CHANGE:
  258. zclCCServer_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
  259. break;
  260. default:
  261. break;
  262. }
  263. // Release the memory
  264. osal_msg_deallocate( (uint8 *)MSGpkt );
  265. }
  266. // return unprocessed events
  267. return (events ^ SYS_EVENT_MSG);
  268. }
  269. if ( events & CCSERVER_LEAVE_TIMER_EVT )
  270. {
  271. if ( zcl_CCStartupMode( restartDevice.options ) == CC_STARTUP_MODE_REPLACE_RESTART )
  272. {
  273. // Perform a Leave on our old network
  274. if ( zclCCServer_SendLeaveReq() == ZSuccess )
  275. {
  276. // Wait for Leave confirmation before joining the new network
  277. leaveInitiated = TO_JOIN_OPERATIONAL_NWK;
  278. }
  279. else
  280. {
  281. // Notify our task to restart the network
  282. osal_set_event( zclCCServer_TaskID, CCSERVER_RESTART_TIMER_EVT );
  283. }
  284. }
  285. else
  286. {
  287. // Notify our task to restart the network
  288. osal_set_event( zclCCServer_TaskID, CCSERVER_RESTART_TIMER_EVT );
  289. }
  290. return ( events ^ CCSERVER_LEAVE_TIMER_EVT );
  291. }
  292. if ( events & CCSERVER_RESTART_TIMER_EVT )
  293. {
  294. if ( zcl_CCStartupMode( restartDevice.options ) == CC_STARTUP_MODE_REPLACE_RESTART )
  295. {
  296. // Set the NV startup option to force a "new" join.
  297. zgWriteStartupOptions( ZG_STARTUP_SET, ZCD_STARTOPT_DEFAULT_NETWORK_STATE );
  298. }
  299. else
  300. {
  301. // Reset the NV startup option to resume from NV by clearing
  302. // the "new" join option.
  303. zgWriteStartupOptions( ZG_STARTUP_CLEAR, ZCD_STARTOPT_DEFAULT_NETWORK_STATE );
  304. }
  305. SystemResetSoft();
  306. return ( events ^ CCSERVER_RESTART_TIMER_EVT );
  307. }
  308. if ( events & CCSERVER_RESET_TIMER_EVT )
  309. {
  310. // Set the NV startup option to default the Config state values
  311. zgWriteStartupOptions( ZG_STARTUP_SET, ZCD_STARTOPT_DEFAULT_CONFIG_STATE );
  312. // Set the NV startup option to force a "new" join.
  313. zgWriteStartupOptions( ZG_STARTUP_SET, ZCD_STARTOPT_DEFAULT_NETWORK_STATE );
  314. SystemResetSoft();
  315. return ( events ^ CCSERVER_RESET_TIMER_EVT );
  316. }
  317. // Discard unknown events
  318. return 0;
  319. }
  320. /*********************************************************************
  321. * @fn zclCCServer_HandleKeys
  322. *
  323. * @brief Handles all key events for this device.
  324. *
  325. * @param shift - true if in shift/alt.
  326. * @param keys - bit field for key events. Valid entries:
  327. * HAL_KEY_SW_4
  328. * HAL_KEY_SW_3
  329. * HAL_KEY_SW_2
  330. * HAL_KEY_SW_1
  331. *
  332. * @return none
  333. */
  334. static void zclCCServer_HandleKeys( uint8 shift, uint8 keys )
  335. {
  336. (void)shift; // Intentionally unreferenced parameter
  337. if ( keys & HAL_KEY_SW_1 )
  338. {
  339. }
  340. if ( keys & HAL_KEY_SW_2 )
  341. {
  342. zclCCServer_ResetToZBADefault();
  343. }
  344. if ( keys & HAL_KEY_SW_3 )
  345. {
  346. }
  347. if ( keys & HAL_KEY_SW_4 )
  348. {
  349. }
  350. }
  351. /*********************************************************************
  352. * @fn zclCCServer_ResetToZBADefault
  353. *
  354. * @brief Reset the local device to ZBA Default.
  355. *
  356. * Note: This function will decommission the device to return
  357. * to the ZBA Default Settings with the default ZBA Key
  358. * Material and ZBA Extended PAN ID.
  359. *
  360. * @param none
  361. *
  362. * @return none
  363. */
  364. void zclCCServer_ResetToZBADefault( void )
  365. {
  366. // Set the NV startup option to force a "new" join.
  367. zgWriteStartupOptions( ZG_STARTUP_SET, ZCD_STARTOPT_DEFAULT_NETWORK_STATE );
  368. // Perform a Leave on our old network
  369. if ( zclCCServer_SendLeaveReq() == ZSuccess )
  370. {
  371. // Wait for Leave confirmation before resetting
  372. leaveInitiated = TO_JOIN_COMMISSIONING_NWK;
  373. }
  374. else
  375. {
  376. // Notify our task to reset the device
  377. osal_set_event( zclCCServer_TaskID, CCSERVER_RESET_TIMER_EVT );
  378. }
  379. }
  380. /*********************************************************************
  381. * @fn zclCCServer_SendLeaveReq
  382. *
  383. * @brief Send out a Leave Request command.
  384. *
  385. * @param void
  386. *
  387. * @return ZStatus_t
  388. */
  389. static ZStatus_t zclCCServer_SendLeaveReq( void )
  390. {
  391. NLME_LeaveReq_t leaveReq;
  392. // Set every field to 0
  393. osal_memset( &leaveReq, 0, sizeof( NLME_LeaveReq_t ) );
  394. // Send out our leave
  395. return ( NLME_LeaveReq( &leaveReq ) );
  396. }
  397. /*********************************************************************
  398. * @fn zclCCServer_SavePreconfigLinkKey
  399. *
  400. * @brief Save the Pre-Configured Link Key.
  401. *
  402. * @param void
  403. *
  404. * @return ZStatus_t
  405. */
  406. static void zclCCServer_SavePreconfigLinkKey( void )
  407. {
  408. APSME_TCLinkKey_t *pKeyData;
  409. pKeyData = (APSME_TCLinkKey_t *)osal_mem_alloc( sizeof( APSME_TCLinkKey_t ) );
  410. if (pKeyData != NULL)
  411. {
  412. // Making sure data is cleared for every key all the time
  413. osal_memset( pKeyData, 0x00, sizeof( APSME_TCLinkKey_t ) );
  414. osal_memset( pKeyData->extAddr, 0xFF, Z_EXTADDR_LEN );
  415. osal_nv_read( ZCD_NV_SAS_CURR_PRECFG_LINK_KEY, 0, SEC_KEY_LEN, pKeyData->key );
  416. // Save the Pre-Configured Link Key as the default TC Link Key the in NV
  417. osal_nv_write( ZCD_NV_TCLK_TABLE_START, 0, sizeof( APSME_TCLinkKey_t ), pKeyData );
  418. // Clear copy of key in RAM
  419. osal_memset( pKeyData, 0x00, sizeof( APSME_TCLinkKey_t ) );
  420. osal_mem_free( pKeyData );
  421. }
  422. }
  423. /*********************************************************************
  424. * @fn zclCCServer_InitStartupParameters
  425. *
  426. * @brief Initialize the current Startup Parameters Set (SAS) to
  427. * their default values.
  428. *
  429. * @param initNv - whether to initialize keys in NV
  430. *
  431. * @return none
  432. */
  433. static void zclCCServer_InitStartupParameters( uint8 initNv )
  434. {
  435. uint8 zbaDefaultKey[SEC_KEY_LEN] = DEFAULT_TC_LINK_KEY;
  436. zclCCServer_ShortAddress = CC_DEFAULT_SHORT_ADDR;
  437. osal_cpyExtAddr( zclCCServer_ExtendedPanId, zbaGlobalCommissioningEPID );
  438. zclCCServer_PanId = CC_DEFAULT_PANID;
  439. zclCCServer_ChannelMask = DEFAULT_CHANLIST;
  440. zclCCServer_ProtocolVersion = ZB_PROT_VERS;
  441. zclCCServer_StackProfile = STACK_PROFILE_ID;
  442. if ( ZG_BUILD_COORDINATOR_TYPE && ZG_DEVICE_COORDINATOR_TYPE )
  443. {
  444. // Form the Commissioning Network
  445. zclCCServer_StartUpControl = CC_STARTUP_CONTROL_OPTION_1;
  446. }
  447. else
  448. {
  449. // Join the Commissioning Network
  450. zclCCServer_StartUpControl = CC_STARTUP_CONTROL_OPTION_3;
  451. }
  452. osal_memcpy( zclCCServer_TrustCenterAddr, 0x00, Z_EXTADDR_LEN);
  453. if ( initNv )
  454. {
  455. // If the item doesn't exist in NV memory, create and initialize it
  456. if ( osal_nv_item_init( ZCD_NV_SAS_CURR_PRECFG_LINK_KEY,
  457. SEC_KEY_LEN, zbaDefaultKey ) == SUCCESS )
  458. {
  459. // Write the default value back to NV
  460. osal_nv_write( ZCD_NV_SAS_CURR_PRECFG_LINK_KEY, 0, SEC_KEY_LEN, zbaDefaultKey );
  461. }
  462. osal_memset( zbaDefaultKey, 0x00, SEC_KEY_LEN );
  463. if ( osal_nv_item_init( ZCD_NV_SAS_CURR_TC_MASTER_KEY,
  464. SEC_KEY_LEN, zbaDefaultKey ) == SUCCESS )
  465. {
  466. // Write the default value back to NV
  467. osal_nv_write( ZCD_NV_SAS_CURR_TC_MASTER_KEY, 0, SEC_KEY_LEN, zbaDefaultKey );
  468. }
  469. if ( osal_nv_item_init( ZCD_NV_SAS_CURR_NWK_KEY,
  470. SEC_KEY_LEN, zbaDefaultKey ) == SUCCESS )
  471. {
  472. // Write the default value back to NV
  473. osal_nv_write( ZCD_NV_SAS_CURR_NWK_KEY, 0, SEC_KEY_LEN, zbaDefaultKey );
  474. }
  475. }
  476. else
  477. {
  478. // Save the default keys directly in the NV
  479. osal_nv_write( ZCD_NV_SAS_CURR_PRECFG_LINK_KEY, 0, SEC_KEY_LEN, zbaDefaultKey );
  480. osal_memset( zbaDefaultKey, 0x00, SEC_KEY_LEN );
  481. osal_nv_write( ZCD_NV_SAS_CURR_TC_MASTER_KEY, 0, SEC_KEY_LEN, zbaDefaultKey );
  482. osal_nv_write( ZCD_NV_SAS_CURR_NWK_KEY, 0, SEC_KEY_LEN, zbaDefaultKey );
  483. }
  484. zclCCServer_UseInsecureJoin = TRUE;
  485. zclCCServer_NetworkKeySeqNum = CC_DEFAULT_NETWORK_KEY_SEQ_NUM;
  486. zclCCServer_NetworkKeyType = KEY_TYPE_NWK;
  487. zclCCServer_NwkManagerAddr = CC_DEFAULT_NETWORK_MANAGER_ADDR;
  488. }
  489. /*********************************************************************
  490. * @fn zclCCServer_UseStartupParameters
  491. *
  492. * @brief Set the network parameters to the current Startup Parameters.
  493. *
  494. * @param none
  495. *
  496. * @return none
  497. */
  498. static void zclCCServer_UseStartupParameters( void )
  499. {
  500. if ( zclCCServer_StartUpControl == CC_STARTUP_CONTROL_OPTION_1 )
  501. {
  502. uint8 networkKey[SEC_KEY_LEN];
  503. // Form the Commissioning network and become the Coordinator for that network
  504. // Required attributes: Extended PAN ID
  505. osal_nv_write( ZCD_NV_EXTENDED_PAN_ID, 0, Z_EXTADDR_LEN,
  506. zclCCServer_ExtendedPanId );
  507. osal_nv_write( ZCD_NV_APS_USE_EXT_PANID, 0, Z_EXTADDR_LEN,
  508. zclCCServer_ExtendedPanId );
  509. // Optional attributes: PAN ID, Channel Mask, Network Manager Address,
  510. // Network Key, Network Key Type (only KEY_TYPE_NWK is currently supported),
  511. // and Trust Center Address (which is used with Preconfigured Link Key).
  512. osal_nv_write( ZCD_NV_PANID, 0, sizeof( zclCCServer_PanId ),
  513. &zclCCServer_PanId );
  514. osal_nv_write( ZCD_NV_CHANLIST, 0, sizeof( zclCCServer_ChannelMask ),
  515. &zclCCServer_ChannelMask );
  516. osal_nv_write( ZCD_NV_NWKMGR_ADDR, 0, sizeof( zclCCServer_NwkManagerAddr ),
  517. &zclCCServer_NwkManagerAddr );
  518. osal_nv_read( ZCD_NV_SAS_CURR_NWK_KEY, 0, SEC_KEY_LEN, networkKey );
  519. if ( !nullKey( networkKey ) )
  520. {
  521. // Save the Network Key as the Pre-Configured Key in NV
  522. osal_nv_write( ZCD_NV_PRECFGKEY, 0, SEC_KEY_LEN, networkKey );
  523. // Clear copy of key in RAM
  524. osal_memset( networkKey, 0x00, SEC_KEY_LEN );
  525. }
  526. }
  527. else if ( zclCCServer_StartUpControl == CC_STARTUP_CONTROL_OPTION_3 )
  528. {
  529. // Join the Commissioning network
  530. // Required attributes: none
  531. // Optional attributes: Extended PAN ID, Channel Mask, and
  532. // Preconfigured Link Key
  533. osal_nv_write( ZCD_NV_EXTENDED_PAN_ID, 0, Z_EXTADDR_LEN,
  534. zclCCServer_ExtendedPanId );
  535. osal_nv_write( ZCD_NV_CHANLIST, 0, sizeof( zclCCServer_ChannelMask ),
  536. &zclCCServer_ChannelMask );
  537. zclCCServer_SavePreconfigLinkKey();
  538. }
  539. }
  540. /*********************************************************************
  541. * @fn zclCCServer_Restart_DeviceCB
  542. *
  543. * @brief Callback from the ZCL commissioning Cluster Library when
  544. * it received a Restart Device for this application.
  545. *
  546. * @param pCmd - restart command
  547. * @param srcAddr - source address
  548. * @param seqNum - message sequence number
  549. *
  550. * @return none
  551. */
  552. static void zclCCServer_Restart_DeviceCB( zclCCRestartDevice_t *pCmd,
  553. afAddrType_t *srcAddr, uint8 seqNum )
  554. {
  555. zclCCServerParamsRsp_t rsp;
  556. // Make sure the device is not in the Operational Network
  557. if ( osal_ExtAddrEqual( _NIB.extendedPANID, zbaGlobalCommissioningEPID ) )
  558. {
  559. // If form network or associate from scratch, set the default network state.
  560. // Only Coordiunator can form (Option 1) and Router or EndDevices can
  561. // associate (Option 3).
  562. if ( ( ( zclCCServer_StartUpControl == CC_STARTUP_CONTROL_OPTION_1 ) && // (form &&
  563. ( zgDeviceLogicalType == ZG_DEVICETYPE_COORDINATOR ) ) || // Coord) ||
  564. ( ( zclCCServer_StartUpControl == CC_STARTUP_CONTROL_OPTION_3 ) && // (associate &&
  565. ( ( zgDeviceLogicalType == ZG_DEVICETYPE_ROUTER ) || // (RTR ||
  566. ( zgDeviceLogicalType == ZG_DEVICETYPE_ENDDEVICE ) ) ) ) // ED))
  567. {
  568. // Form or Join a new network
  569. rsp.status = SUCCESS;
  570. }
  571. else
  572. {
  573. // Startup state is currently not supported
  574. rsp.status = ZCL_STATUS_INCONSISTENT_STARTUP_STATE;
  575. }
  576. }
  577. else
  578. {
  579. // No access to Commissioning cluster in the Operational Network
  580. rsp.status = ZCL_STATUS_NOT_AUTHORIZED;
  581. }
  582. zclCC_Send_RestartDeviceRsp( CCSERVER_ENDPOINT, srcAddr, &rsp, TRUE, seqNum );
  583. if ( rsp.status == SUCCESS )
  584. {
  585. uint16 delay;
  586. // If restart immediately is specified the should restart at a convienent
  587. // time when all messages have been sent. Give 1s for response to be sent
  588. // and then restart. Else, calculate the delay time.
  589. if ( zcl_CCImmediate( pCmd->options ) )
  590. {
  591. delay = 1000;
  592. }
  593. else
  594. {
  595. delay = ( pCmd->delay * 1000 ) + ( osal_rand() % ( pCmd->jitter * 80 ) );
  596. }
  597. if ( zcl_CCStartupMode( pCmd->options ) == CC_STARTUP_MODE_REPLACE_RESTART )
  598. {
  599. zclCCServer_UseStartupParameters();
  600. }
  601. // Save the received command for later
  602. restartDevice = *pCmd;
  603. osal_start_timerEx( zclCCServer_TaskID, CCSERVER_LEAVE_TIMER_EVT, delay );
  604. }
  605. }
  606. /*********************************************************************
  607. * @fn zclCCServer_Save_StartupParametersCB
  608. *
  609. * @brief Callback from the ZCL Commissioning Cluster Library when
  610. * it received a Save startup parameters for this application.
  611. *
  612. * @param pCmd - save command
  613. * @param srcAddr - source address
  614. * @param seqNum - message sequence number
  615. *
  616. * @return none
  617. */
  618. static void zclCCServer_Save_StartupParametersCB( zclCCStartupParams_t *pCmd,
  619. afAddrType_t *srcAddr, uint8 seqNum )
  620. {
  621. static uint8 nvInitFlag = FALSE;
  622. zclCCServerParamsRsp_t rsp;
  623. if( pCmd->index < CCSERVER_MAX_NWK_STARTUP_PARAMS )
  624. {
  625. if ( !nvInitFlag )
  626. {
  627. nvInitFlag = TRUE;
  628. zclCCServer_InitStartupParametersInNV();
  629. }
  630. else
  631. {
  632. zclCCServer_SaveStartupParametersInNV();
  633. }
  634. rsp.status = SUCCESS;
  635. }
  636. else
  637. {
  638. rsp.status = ZCL_STATUS_INSUFFICIENT_SPACE;
  639. }
  640. zclCC_Send_SaveStartupParamsRsp( CCSERVER_ENDPOINT, srcAddr, &rsp, TRUE, seqNum );
  641. }
  642. /*********************************************************************
  643. * @fn zclCCServer_Restore_StartupParametersCB
  644. *
  645. * @brief Callback from the ZCL commissioning Cluster Library when
  646. * it received a Restore startup parameters for this application.
  647. *
  648. * @param pCmd - restore command
  649. * @param srcAddr - source address
  650. * @param seqNum - message sequence number
  651. *
  652. * @return none
  653. */
  654. static void zclCCServer_Restore_StartupParametersCB( zclCCStartupParams_t *pCmd,
  655. afAddrType_t *srcAddr, uint8 seqNum )
  656. {
  657. zclCCServerParamsRsp_t rsp;
  658. if ( pCmd->index < CCSERVER_MAX_NWK_STARTUP_PARAMS )
  659. {
  660. zclCCServer_RestoreStartupParametersInNV();
  661. rsp.status = SUCCESS;
  662. }
  663. else
  664. {
  665. rsp.status = ZCL_STATUS_INVALID_FIELD;
  666. }
  667. zclCC_Send_RestoreStartupParamsRsp( CCSERVER_ENDPOINT, srcAddr, &rsp, TRUE, seqNum );
  668. }
  669. /*********************************************************************
  670. * @fn zclCCServer_Reset_StartupParametersCB
  671. *
  672. * @brief Callback from the ZCL commissioning Cluster Library when
  673. * it received a Reset startup parameters for this application.
  674. *
  675. * @param pCmd - reset command
  676. * @param srcAddr - source address
  677. * @param seqNum - message sequence number
  678. *
  679. * @return none
  680. */
  681. static void zclCCServer_Reset_StartupParametersCB( zclCCStartupParams_t *pCmd,
  682. afAddrType_t *srcAddr, uint8 seqNum )
  683. {
  684. zclCCServerParamsRsp_t rsp;
  685. // Reset current SAS
  686. if ( pCmd->options & CC_RESET_CURRENT )
  687. {
  688. zclCCServer_InitStartupParameters( FALSE );
  689. rsp.status = SUCCESS;
  690. }
  691. // Only one set of Startup parameters is stored in NV. So index based
  692. // Startup parameter reset is valid only for index 0
  693. if ( ( pCmd->options & CC_RESET_ALL ) || ( pCmd->index < CCSERVER_MAX_NWK_STARTUP_PARAMS ) )
  694. {
  695. zclCCServer_ResetStartupParametersInNV();
  696. rsp.status = SUCCESS;
  697. }
  698. // Freeing the NV storage associated with an index is not supported. So Erase Index
  699. // is denied
  700. if ( pCmd->options & CC_ERASE_INDEX )
  701. {
  702. rsp.status = ZCL_STATUS_ACTION_DENIED;
  703. }
  704. zclCC_Send_ResetStartupParamsRsp( CCSERVER_ENDPOINT, srcAddr, &rsp, TRUE, seqNum );
  705. }
  706. /*********************************************************************
  707. * @fn zclCCServer_Restart_DeviceRspCB
  708. *
  709. * @brief Callback from the ZCL commissioning Cluster Library when
  710. * it received a Restart device Response for this application.
  711. *
  712. * @param pCmd - restart response
  713. * @param srcAddr - source address
  714. * @param seqNum - message sequence number
  715. *
  716. * @return none
  717. */
  718. static void zclCCServer_Restart_DeviceRspCB( zclCCServerParamsRsp_t *pCmd,
  719. afAddrType_t *srcAddr, uint8 seqNum )
  720. {
  721. // Add application code here
  722. }
  723. /*********************************************************************
  724. * @fn zclCCServer_Save_StartupParametersRspCB
  725. *
  726. * @brief Callback from the ZCL commissioning Cluster Library when
  727. * it received a save startup parameter Response for
  728. * this application.
  729. *
  730. * @param pCmd - save response
  731. * @param srcAddr - source address
  732. * @param seqNum - message sequence number
  733. *
  734. * @return none
  735. */
  736. static void zclCCServer_Save_StartupParametersRspCB( zclCCServerParamsRsp_t *pRsp,
  737. afAddrType_t *srcAddr, uint8 seqNum )
  738. {
  739. // Add application code here
  740. }
  741. /*********************************************************************
  742. * @fn zclCCServer_Restore_StartupParametersRspCB
  743. *
  744. * @brief Callback from the ZCL commissioning Cluster Library when
  745. * it received a Restore startup parameter Response for
  746. * this application.
  747. *
  748. * @param pCmd - restore response
  749. * @param srcAddr - source address
  750. * @param seqNum - message sequence number
  751. *
  752. * @return none
  753. */
  754. static void zclCCServer_Restore_StartupParametersRspCB( zclCCServerParamsRsp_t *pRsp,
  755. afAddrType_t *srcAddr, uint8 seqNum )
  756. {
  757. // Add application code here
  758. }
  759. /*********************************************************************
  760. * @fn zclCCServer_Reset_StartupParametersRspCB
  761. *
  762. * @brief Callback from the ZCL commissioning Cluster Library when
  763. * it received a Reset startup parameter Response for
  764. * this application.
  765. *
  766. * @param pCmd - reset response
  767. * @param srcAddr - source address
  768. * @param seqNum - message sequence number
  769. *
  770. * @return none
  771. */
  772. static void zclCCServer_Reset_StartupParametersRspCB( zclCCServerParamsRsp_t *pRsp,
  773. afAddrType_t *srcAddr, uint8 seqNum )
  774. {
  775. // Add application code here
  776. }
  777. /*********************************************************************
  778. * @fn zclCCServer_ValidateAttrDataCB
  779. *
  780. * @brief Check to see if the supplied value for the attribute data
  781. * is within the specified range of the attribute.
  782. *
  783. * @param pAttr - pointer to attribute
  784. * @param pAttrInfo - pointer to attribute info
  785. *
  786. * @return TRUE if data valid. FALSE otherwise.
  787. */
  788. static uint8 zclCCServer_ValidateAttrDataCB( zclAttrRec_t *pAttr, zclWriteRec_t *pAttrInfo )
  789. {
  790. uint8 valid = TRUE;
  791. if ( pAttr->attr.dataPtr == &zclCCServer_StartUpControl )
  792. {
  793. if ( ( *(pAttrInfo->attrData) != CC_STARTUP_CONTROL_OPTION_1 ) &&
  794. ( *(pAttrInfo->attrData) != CC_STARTUP_CONTROL_OPTION_3 ) )
  795. {
  796. valid = FALSE;
  797. }
  798. }
  799. else if ( pAttr->attr.dataPtr == &zclCCServer_NetworkKeyType )
  800. {
  801. if ( *(pAttrInfo->attrData) != KEY_TYPE_NWK )
  802. {
  803. valid = FALSE;
  804. }
  805. }
  806. return ( valid );
  807. }
  808. /*********************************************************************
  809. * @fn zclCCServer_ReadWriteCB
  810. *
  811. * @brief Read/write attribute data. This callback function should
  812. * only be called for the Network Security Key attributes.
  813. *
  814. * Note: This function is only required when the attribute data
  815. * format is unknown to ZCL. This function gets called
  816. * when the pointer 'dataPtr' to the attribute value is
  817. * NULL in the attribute database registered with the ZCL.
  818. *
  819. * @param clusterId - cluster that attribute belongs to
  820. * @param attrId - attribute to be read or written
  821. * @param oper - ZCL_OPER_LEN, ZCL_OPER_READ, or ZCL_OPER_WRITE
  822. * @param pValue - pointer to attribute value
  823. * @param pLen - length of attribute value read
  824. *
  825. * @return ZStatus_t
  826. */
  827. static ZStatus_t zclCCServer_ReadWriteCB( uint16 clusterId, uint16 attrId,
  828. uint8 oper, uint8 *pValue, uint16 *pLen )
  829. {
  830. ZStatus_t status = ZCL_STATUS_SUCCESS;
  831. switch ( oper )
  832. {
  833. case ZCL_OPER_LEN:
  834. *pLen = SEC_KEY_LEN;
  835. break;
  836. case ZCL_OPER_READ:
  837. osal_nv_read( NvIdFromAttrId( attrId ), 0, SEC_KEY_LEN, pValue );
  838. if ( pLen != NULL )
  839. {
  840. *pLen = SEC_KEY_LEN;
  841. }
  842. break;
  843. case ZCL_OPER_WRITE:
  844. osal_nv_write( NvIdFromAttrId( attrId ), 0, SEC_KEY_LEN, pValue );
  845. break;
  846. default:
  847. status = ZCL_STATUS_SOFTWARE_FAILURE; // Should never get here!
  848. break;
  849. }
  850. return ( status );
  851. }
  852. /*********************************************************************
  853. * @fn zclCCServer_AuthorizeCB
  854. *
  855. * @brief Authorize a Read or Write operation on a given attribute.
  856. *
  857. * Note: The pfnAuthorizeCB callback function is only required
  858. * when the Read/Write operation on an attribute requires
  859. * authorization (i.e., attributes with ACCESS_CONTROL_AUTH_READ
  860. * or ACCESS_CONTROL_AUTH_WRITE access permissions).
  861. *
  862. * @param srcAddr - source Address
  863. * @param pAttr - pointer to attribute
  864. * @param oper - ZCL_OPER_READ, or ZCL_OPER_WRITE
  865. *
  866. * @return ZCL_STATUS_SUCCESS: Operation authorized
  867. * ZCL_STATUS_NOT_AUTHORIZED: Operation not authorized
  868. */
  869. static ZStatus_t zclCCServer_AuthorizeCB( afAddrType_t *srcAddr, zclAttrRec_t *pAttr, uint8 oper )
  870. {
  871. // Make sure the device is not in the Operational Network
  872. if ( osal_ExtAddrEqual( _NIB.extendedPANID, zbaGlobalCommissioningEPID ) )
  873. {
  874. return ( ZCL_STATUS_SUCCESS );
  875. }
  876. return ( ZCL_STATUS_NOT_AUTHORIZED );
  877. }
  878. /******************************************************************************
  879. * @fn zclCCServer_ZdoLeaveCnfCB
  880. *
  881. * @brief This callback is called to process a Leave Confirmation message.
  882. *
  883. * Note: this callback function returns a pointer if it has handled
  884. * the confirmation message and no further action should be
  885. * taken with it. It returns NULL if it has not handled the
  886. * confirmation message and normal processing should take place.
  887. *
  888. * @param pParam - received message
  889. *
  890. * @return Pointer if message processed. NULL, otherwise.
  891. */
  892. static void *zclCCServer_ZdoLeaveCnfCB( void *pParam )
  893. {
  894. (void)pParam;
  895. // Did we initiate the leave?
  896. if ( leaveInitiated == FALSE )
  897. {
  898. return ( NULL );
  899. }
  900. if ( leaveInitiated == TO_JOIN_OPERATIONAL_NWK )
  901. {
  902. // Notify our task to join the restart network
  903. osal_set_event( zclCCServer_TaskID, CCSERVER_RESTART_TIMER_EVT );
  904. }
  905. else
  906. {
  907. // Notify our task to reset the device
  908. osal_set_event( zclCCServer_TaskID, CCSERVER_RESET_TIMER_EVT );
  909. }
  910. return ( (void *)&leaveInitiated );
  911. }
  912. /*********************************************************************
  913. * @fn zclCCServer_InitStartupParametersInNV
  914. *
  915. * @brief Initialize the Startup Parameters in NV.
  916. *
  917. * @param none
  918. *
  919. * @return none
  920. */
  921. static void zclCCServer_InitStartupParametersInNV( void )
  922. {
  923. uint8 key[SEC_KEY_LEN];
  924. for ( uint8 i = 0; nvItemTable[i].id != 0x00; i++ )
  925. {
  926. // Initialize the item
  927. osal_nv_item_init( nvItemTable[i].id, nvItemTable[i].len, nvItemTable[i].buf );
  928. }
  929. // Use the default key values in the NV
  930. osal_nv_read( ZCD_NV_SAS_CURR_TC_MASTER_KEY, 0, SEC_KEY_LEN, key );
  931. osal_nv_item_init( ZCD_NV_SAS_TC_MASTER_KEY, SEC_KEY_LEN, key );
  932. osal_nv_read( ZCD_NV_SAS_CURR_NWK_KEY, 0, SEC_KEY_LEN, key );
  933. osal_nv_item_init( ZCD_NV_SAS_NWK_KEY, SEC_KEY_LEN, key );
  934. osal_nv_read( ZCD_NV_SAS_CURR_PRECFG_LINK_KEY, 0, SEC_KEY_LEN, key );
  935. osal_nv_item_init( ZCD_NV_SAS_PRECFG_LINK_KEY, SEC_KEY_LEN, key );
  936. // Clear copy of key in RAM
  937. osal_memset( key, 0x00, SEC_KEY_LEN );
  938. }
  939. /*********************************************************************
  940. * @fn zclCCServer_ResetStartupParametersInNV
  941. *
  942. * @brief Reset the Startup Parameters in NV.
  943. *
  944. * @param none
  945. *
  946. * @return none
  947. */
  948. static void zclCCServer_ResetStartupParametersInNV( void )
  949. {
  950. uint8 tmpByte;
  951. uint16 tmpShort;
  952. uint32 tmpLong;
  953. uint8 tmpExtAddr[Z_EXTADDR_LEN];
  954. uint8 zbaDefaultKey[SEC_KEY_LEN] = DEFAULT_TC_LINK_KEY;
  955. tmpShort = CC_DEFAULT_SHORT_ADDR;
  956. osal_nv_write( ZCD_NV_SAS_SHORT_ADDR, 0, sizeof( tmpShort ), &tmpShort );
  957. osal_cpyExtAddr( tmpExtAddr, zbaGlobalCommissioningEPID );
  958. osal_nv_write( ZCD_NV_SAS_EXT_PANID, 0, Z_EXTADDR_LEN, tmpExtAddr );
  959. tmpShort = CC_DEFAULT_PANID;
  960. osal_nv_write( ZCD_NV_SAS_PANID, 0, sizeof( tmpShort ), &tmpShort );
  961. tmpLong = DEFAULT_CHANLIST;
  962. osal_nv_write( ZCD_NV_SAS_CHANNEL_MASK, 0, sizeof( tmpLong ), &tmpLong );
  963. tmpByte = ZB_PROT_VERS;
  964. osal_nv_write( ZCD_NV_SAS_PROTOCOL_VER, 0, sizeof( tmpShort ), &tmpShort );
  965. tmpByte = STACK_PROFILE_ID;
  966. osal_nv_write( ZCD_NV_SAS_STACK_PROFILE, 0, sizeof( tmpShort ), &tmpShort );
  967. if ( ZG_BUILD_COORDINATOR_TYPE && ZG_DEVICE_COORDINATOR_TYPE )
  968. {
  969. // Form the Commissioning Network
  970. tmpByte = CC_STARTUP_CONTROL_OPTION_1;
  971. }
  972. else
  973. {
  974. // Join the Commissioning Network
  975. tmpByte = CC_STARTUP_CONTROL_OPTION_3;
  976. }
  977. osal_nv_write( ZCD_NV_SAS_STARTUP_CTRL, 0, sizeof( tmpByte ), &tmpByte );
  978. osal_memset( tmpExtAddr, 0x00, Z_EXTADDR_LEN );
  979. osal_nv_write( ZCD_NV_SAS_TC_ADDR, 0, Z_EXTADDR_LEN, tmpExtAddr );
  980. osal_nv_write( ZCD_NV_SAS_PRECFG_LINK_KEY, 0, SEC_KEY_LEN, zbaDefaultKey );
  981. osal_memset( zbaDefaultKey, 0x00, SEC_KEY_LEN );
  982. osal_nv_write( ZCD_NV_SAS_TC_MASTER_KEY, 0, SEC_KEY_LEN, zbaDefaultKey );
  983. osal_nv_write( ZCD_NV_SAS_NWK_KEY, 0, SEC_KEY_LEN, zbaDefaultKey );
  984. tmpByte = TRUE;
  985. osal_nv_write( ZCD_NV_SAS_USE_INSEC_JOIN, 0, sizeof( tmpByte ), &tmpByte );
  986. tmpByte = CC_DEFAULT_NETWORK_KEY_SEQ_NUM;
  987. osal_nv_write( ZCD_NV_SAS_NWK_KEY_SEQ_NUM, 0, sizeof( tmpByte ), &tmpByte );
  988. tmpByte = KEY_TYPE_NWK;
  989. osal_nv_write( ZCD_NV_SAS_NWK_KEY_TYPE, 0, sizeof( tmpByte ), &tmpByte );
  990. tmpShort = CC_DEFAULT_NETWORK_MANAGER_ADDR;
  991. osal_nv_write( ZCD_NV_SAS_NWK_MGR_ADDR, 0, sizeof( tmpShort ), &tmpShort );
  992. }
  993. /*********************************************************************
  994. * @fn zclCCServer_RestoreStartupParametersInNV
  995. *
  996. * @brief Save the Startup Parameters in NV
  997. *
  998. * @param none
  999. *
  1000. * @return none
  1001. */
  1002. static void zclCCServer_SaveStartupParametersInNV( void )
  1003. {
  1004. uint8 key[SEC_KEY_LEN];
  1005. for ( uint8 i = 0; nvItemTable[i].id != 0x00; i++ )
  1006. {
  1007. // Write the item
  1008. osal_nv_write( nvItemTable[i].id, 0, nvItemTable[i].len, nvItemTable[i].buf );
  1009. }
  1010. // Update the keys in the NV
  1011. osal_nv_read( ZCD_NV_SAS_CURR_TC_MASTER_KEY, 0, SEC_KEY_LEN, key );
  1012. osal_nv_write( ZCD_NV_SAS_TC_MASTER_KEY, 0, SEC_KEY_LEN, key );
  1013. osal_nv_read( ZCD_NV_SAS_CURR_NWK_KEY, 0, SEC_KEY_LEN, key );
  1014. osal_nv_write( ZCD_NV_SAS_NWK_KEY, 0, SEC_KEY_LEN, key );
  1015. osal_nv_read( ZCD_NV_SAS_CURR_PRECFG_LINK_KEY, 0, SEC_KEY_LEN, key );
  1016. osal_nv_write( ZCD_NV_SAS_PRECFG_LINK_KEY, 0, SEC_KEY_LEN, key );
  1017. // Clear copy of key in RAM
  1018. osal_memset( key, 0x00, SEC_KEY_LEN );
  1019. }
  1020. /*********************************************************************
  1021. * @fn zclCCServer_RestoreStartupParametersInNV
  1022. *
  1023. * @brief Restore the Startup Parameters from NV
  1024. *
  1025. * @param none
  1026. *
  1027. * @return none
  1028. */
  1029. static void zclCCServer_RestoreStartupParametersInNV( void )
  1030. {
  1031. uint8 key[SEC_KEY_LEN];
  1032. for ( uint8 i = 0; nvItemTable[i].id != 0x00; i++ )
  1033. {
  1034. // Read the item
  1035. osal_nv_read( nvItemTable[i].id, 0, nvItemTable[i].len, nvItemTable[i].buf );
  1036. }
  1037. // Update the current keys in the NV
  1038. osal_nv_read( ZCD_NV_SAS_TC_MASTER_KEY, 0, SEC_KEY_LEN, key );
  1039. osal_nv_write( ZCD_NV_SAS_CURR_TC_MASTER_KEY, 0, SEC_KEY_LEN, key );
  1040. osal_nv_read( ZCD_NV_SAS_NWK_KEY, 0, SEC_KEY_LEN, key );
  1041. osal_nv_write( ZCD_NV_SAS_CURR_NWK_KEY, 0, SEC_KEY_LEN, key );
  1042. osal_nv_read( ZCD_NV_SAS_PRECFG_LINK_KEY, 0, SEC_KEY_LEN, key );
  1043. osal_nv_write( ZCD_NV_SAS_CURR_PRECFG_LINK_KEY, 0, SEC_KEY_LEN, key );
  1044. // Clear copy of key in RAM
  1045. osal_memset( key, 0x00, SEC_KEY_LEN );
  1046. }
  1047. /****************************************************************************
  1048. ****************************************************************************/