MT_UTIL.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485
  1. /***************************************************************************************************
  2. Filename: MT_UTIL.c
  3. Revised: $Date: 2012-02-25 17:43:09 -0800 (Sat, 25 Feb 2012) $
  4. Revision: $Revision: 29520 $
  5. Description: MonitorTest Utility Functions
  6. Copyright 2007-2012 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 "AddrMgr.h"
  38. #include "OnBoard.h" /* This is here because of the key reading */
  39. #include "hal_key.h"
  40. #include "hal_led.h"
  41. #include "OSAL_Nv.h"
  42. #include "osal.h"
  43. #include "NLMEDE.h"
  44. #include "MT.h"
  45. #include "MT_UTIL.h"
  46. #include "MT_MAC.h"
  47. #include "ssp.h"
  48. #if defined ZCL_KEY_ESTABLISH
  49. #include "zcl_key_establish.h"
  50. #endif
  51. #if defined TC_LINKKEY_JOIN
  52. #include "zcl_se.h"
  53. #endif
  54. #if !defined NONWK
  55. #include "MT_ZDO.h"
  56. #include "MT_SAPI.h"
  57. #include "MT_NWK.h"
  58. #include "MT_AF.h"
  59. #include "AssocList.h"
  60. #include "ZDApp.h"
  61. #include "ZDSecMgr.h"
  62. #endif
  63. /***************************************************************************************************
  64. * CONSTANTS
  65. ***************************************************************************************************/
  66. #define MT_UTIL_DEVICE_INFO_RESPONSE_LEN 14
  67. #define MT_UTIL_STATUS_LEN 1
  68. #define MT_UTIL_FRM_CTR_LEN 4
  69. // Status + LinkKeyDataLen + Tx+Rx Frame counter.
  70. #define MT_APSME_LINKKEY_GET_RSP_LEN (MT_UTIL_STATUS_LEN + SEC_KEY_LEN + (MT_UTIL_FRM_CTR_LEN * 2))
  71. // Status + NV id
  72. #define MT_APSME_LINKKEY_NV_ID_GET_RSP_LEN (MT_UTIL_STATUS_LEN + 2)
  73. /***************************************************************************************************
  74. * LOCAL VARIABLES
  75. ***************************************************************************************************/
  76. #if defined ZCL_KEY_ESTABLISH
  77. uint8 zcl_key_establish_task_id;
  78. #endif
  79. /***************************************************************************************************
  80. * LOCAL FUNCTIONS
  81. ***************************************************************************************************/
  82. #ifdef AUTO_PEND
  83. static void MT_UtilRevExtCpy( uint8 *pDst, uint8 *pSrc );
  84. static void MT_UtilSpi2Addr( zAddrType_t *pDst, uint8 *pSrc );
  85. #endif
  86. #if defined (MT_UTIL_FUNC)
  87. void MT_UtilGetDeviceInfo(void);
  88. void MT_UtilGetNvInfo(void);
  89. void MT_UtilSetPanID(uint8 *pBuf);
  90. void MT_UtilSetChannels(uint8 *pBuf);
  91. void MT_UtilSetSecLevel(uint8 *pBuf);
  92. void MT_UtilSetPreCfgKey(uint8 *pBuf);
  93. void MT_UtilCallbackSub(uint8 *pData);
  94. void MT_UtilKeyEvent(uint8 *pBuf);
  95. void MT_UtilTimeAlive(void);
  96. void MT_UtilLedControl(uint8 *pBuf);
  97. void MT_UtilSrcMatchEnable (uint8 *pBuf);
  98. void MT_UtilSrcMatchAddEntry (uint8 *pBuf);
  99. void MT_UtilSrcMatchDeleteEntry (uint8 *pBuf);
  100. void MT_UtilSrcMatchCheckSrcAddr (uint8 *pBuf);
  101. void MT_UtilSrcMatchAckAllPending (uint8 *pBuf);
  102. void MT_UtilSrcMatchCheckAllPending (uint8 *pBuf);
  103. #if !defined NONWK
  104. void MT_UtilDataReq(uint8 *pBuf);
  105. static void MT_UtilAddrMgrEntryLookupExt(uint8 *pBuf);
  106. static void MT_UtilAddrMgrEntryLookupNwk(uint8 *pBuf);
  107. #if defined MT_SYS_KEY_MANAGEMENT
  108. static void MT_UtilAPSME_LinkKeyDataGet(uint8 *pBuf);
  109. static void MT_UtilAPSME_LinkKeyNvIdGet(uint8 *pBuf);
  110. #endif //MT_SYS_KEY_MANAGEMENT
  111. static void MT_UtilAPSME_RequestKeyCmd(uint8 *pBuf);
  112. static void MT_UtilAssocCount(uint8 *pBuf);
  113. static void MT_UtilAssocFindDevice(uint8 *pBuf);
  114. static void MT_UtilAssocGetWithAddress(uint8 *pBuf);
  115. static void packDev_t(uint8 *pBuf, associated_devices_t *pDev);
  116. #if defined ZCL_KEY_ESTABLISH
  117. static void MT_UtilzclGeneral_KeyEstablish_InitiateKeyEstablishment(uint8 *pBuf);
  118. static void MT_UtilzclGeneral_KeyEstablishment_ECDSASign(uint8 *pBuf);
  119. #endif // ZCL_KEY_ESTABLISH
  120. static void MT_UtilSync(void);
  121. #endif // !defined NONWK
  122. #endif // MT_UTIL_FUNC
  123. #if defined (MT_UTIL_FUNC)
  124. /***************************************************************************************************
  125. * @fn MT_UtilProcessing
  126. *
  127. * @brief Process all the DEBUG commands that are issued by test tool
  128. *
  129. * @param pBuf - pointer to received SPI data message
  130. *
  131. * @return status
  132. ***************************************************************************************************/
  133. uint8 MT_UtilCommandProcessing(uint8 *pBuf)
  134. {
  135. uint8 status = MT_RPC_SUCCESS;
  136. switch (pBuf[MT_RPC_POS_CMD1])
  137. {
  138. // CC253X MAC Network Processor does not have NV support
  139. #if !defined(CC253X_MACNP)
  140. case MT_UTIL_GET_DEVICE_INFO:
  141. MT_UtilGetDeviceInfo();
  142. break;
  143. case MT_UTIL_GET_NV_INFO:
  144. MT_UtilGetNvInfo();
  145. break;
  146. case MT_UTIL_SET_PANID:
  147. MT_UtilSetPanID(pBuf);
  148. break;
  149. case MT_UTIL_SET_CHANNELS:
  150. MT_UtilSetChannels(pBuf);
  151. break;
  152. case MT_UTIL_SET_SECLEVEL:
  153. MT_UtilSetSecLevel(pBuf);
  154. break;
  155. case MT_UTIL_SET_PRECFGKEY:
  156. MT_UtilSetPreCfgKey(pBuf);
  157. break;
  158. #endif
  159. case MT_UTIL_CALLBACK_SUB_CMD:
  160. MT_UtilCallbackSub(pBuf);
  161. break;
  162. case MT_UTIL_KEY_EVENT:
  163. #if (defined HAL_KEY) && (HAL_KEY == TRUE)
  164. MT_UtilKeyEvent(pBuf);
  165. #endif
  166. break;
  167. case MT_UTIL_TIME_ALIVE:
  168. MT_UtilTimeAlive();
  169. break;
  170. case MT_UTIL_LED_CONTROL:
  171. #if (defined HAL_LED) && (HAL_LED == TRUE)
  172. MT_UtilLedControl(pBuf);
  173. #endif
  174. break;
  175. case MT_UTIL_SRC_MATCH_ENABLE:
  176. MT_UtilSrcMatchEnable(pBuf);
  177. break;
  178. case MT_UTIL_SRC_MATCH_ADD_ENTRY:
  179. MT_UtilSrcMatchAddEntry(pBuf);
  180. break;
  181. case MT_UTIL_SRC_MATCH_DEL_ENTRY:
  182. MT_UtilSrcMatchDeleteEntry(pBuf);
  183. break;
  184. case MT_UTIL_SRC_MATCH_CHECK_SRC_ADDR:
  185. MT_UtilSrcMatchCheckSrcAddr(pBuf);
  186. break;
  187. case MT_UTIL_SRC_MATCH_ACK_ALL_PENDING:
  188. MT_UtilSrcMatchAckAllPending(pBuf);
  189. break;
  190. case MT_UTIL_SRC_MATCH_CHECK_ALL_PENDING:
  191. MT_UtilSrcMatchCheckAllPending(pBuf);
  192. break;
  193. case MT_UTIL_TEST_LOOPBACK:
  194. MT_BuildAndSendZToolResponse((MT_RPC_CMD_SRSP|(uint8)MT_RPC_SYS_UTIL), MT_UTIL_TEST_LOOPBACK,
  195. pBuf[MT_RPC_POS_LEN], (pBuf+MT_RPC_FRAME_HDR_SZ));
  196. break;
  197. #if !defined NONWK
  198. case MT_UTIL_DATA_REQ:
  199. MT_UtilDataReq(pBuf);
  200. break;
  201. case MT_UTIL_ADDRMGR_EXT_ADDR_LOOKUP:
  202. MT_UtilAddrMgrEntryLookupExt(pBuf);
  203. break;
  204. case MT_UTIL_ADDRMGR_NWK_ADDR_LOOKUP:
  205. MT_UtilAddrMgrEntryLookupNwk(pBuf);
  206. break;
  207. #if defined MT_SYS_KEY_MANAGEMENT
  208. case MT_UTIL_APSME_LINK_KEY_DATA_GET:
  209. MT_UtilAPSME_LinkKeyDataGet(pBuf);
  210. break;
  211. case MT_UTIL_APSME_LINK_KEY_NV_ID_GET:
  212. MT_UtilAPSME_LinkKeyNvIdGet(pBuf);
  213. break;
  214. #endif // MT_SYS_KEY_MANAGEMENT
  215. case MT_UTIL_APSME_REQUEST_KEY_CMD:
  216. MT_UtilAPSME_RequestKeyCmd(pBuf);
  217. break;
  218. case MT_UTIL_ASSOC_COUNT:
  219. MT_UtilAssocCount(pBuf);
  220. break;
  221. case MT_UTIL_ASSOC_FIND_DEVICE:
  222. MT_UtilAssocFindDevice(pBuf);
  223. break;
  224. case MT_UTIL_ASSOC_GET_WITH_ADDRESS:
  225. MT_UtilAssocGetWithAddress(pBuf);
  226. break;
  227. #if defined ZCL_KEY_ESTABLISH
  228. case MT_UTIL_ZCL_KEY_EST_INIT_EST:
  229. MT_UtilzclGeneral_KeyEstablish_InitiateKeyEstablishment(pBuf);
  230. break;
  231. case MT_UTIL_ZCL_KEY_EST_SIGN:
  232. MT_UtilzclGeneral_KeyEstablishment_ECDSASign(pBuf);
  233. break;
  234. #endif
  235. case MT_UTIL_SYNC_REQ:
  236. MT_UtilSync();
  237. break;
  238. #endif /* !defined NONWK */
  239. default:
  240. status = MT_RPC_ERR_COMMAND_ID;
  241. break;
  242. }
  243. return status;
  244. }
  245. /***************************************************************************************************
  246. * @fn MT_UtilGetDeviceInfo
  247. *
  248. * @brief The Get Device Info serial message.
  249. *
  250. * @param None.
  251. *
  252. * @return void
  253. ***************************************************************************************************/
  254. void MT_UtilGetDeviceInfo(void)
  255. {
  256. uint8 *buf;
  257. uint8 *pBuf;
  258. uint8 bufLen = MT_UTIL_DEVICE_INFO_RESPONSE_LEN;
  259. uint16 *assocList = NULL;
  260. #if !defined NONWK
  261. uint8 assocCnt = 0;
  262. if (ZG_DEVICE_RTR_TYPE)
  263. {
  264. assocList = AssocMakeList( &assocCnt );
  265. bufLen += (assocCnt * sizeof(uint16));
  266. }
  267. #endif
  268. buf = osal_mem_alloc( bufLen );
  269. if ( buf )
  270. {
  271. pBuf = buf;
  272. *pBuf++ = ZSUCCESS; // Status
  273. osal_nv_read( ZCD_NV_EXTADDR, 0, Z_EXTADDR_LEN, pBuf );
  274. pBuf += Z_EXTADDR_LEN;
  275. #if defined NONWK
  276. // Skip past ZStack only parameters for NONWK
  277. *pBuf++ = 0;
  278. *pBuf++ = 0;
  279. *pBuf++ = 0;
  280. *pBuf++ = 0;
  281. *pBuf = 0;
  282. #else
  283. {
  284. uint16 shortAddr = NLME_GetShortAddr();
  285. *pBuf++ = LO_UINT16( shortAddr );
  286. *pBuf++ = HI_UINT16( shortAddr );
  287. }
  288. /* Return device type */
  289. *pBuf++ = ZSTACK_DEVICE_BUILD;
  290. /*Return device state */
  291. *pBuf++ = (uint8)devState;
  292. if (ZG_DEVICE_RTR_TYPE)
  293. {
  294. *pBuf++ = assocCnt;
  295. if ( assocCnt )
  296. {
  297. uint8 x;
  298. uint16 *puint16 = assocList;
  299. for ( x = 0; x < assocCnt; x++, puint16++ )
  300. {
  301. *pBuf++ = LO_UINT16( *puint16 );
  302. *pBuf++ = HI_UINT16( *puint16 );
  303. }
  304. }
  305. }
  306. else
  307. {
  308. *pBuf++ = 0;
  309. }
  310. #endif
  311. MT_BuildAndSendZToolResponse( ((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_UTIL),
  312. MT_UTIL_GET_DEVICE_INFO,
  313. bufLen, buf );
  314. osal_mem_free( buf );
  315. }
  316. if ( assocList )
  317. {
  318. osal_mem_free( assocList );
  319. }
  320. }
  321. /***************************************************************************************************
  322. * @fn MT_UtilGetNvInfo
  323. *
  324. * @brief The Get NV Info serial message.
  325. *
  326. * @param None.
  327. *
  328. * @return void
  329. ***************************************************************************************************/
  330. void MT_UtilGetNvInfo(void)
  331. {
  332. uint8 len;
  333. uint8 stat;
  334. uint8 *buf;
  335. uint8 *pBuf;
  336. uint16 tmp16;
  337. uint32 tmp32;
  338. /*
  339. Get required length of buffer
  340. Status + ExtAddr + ChanList + PanID + SecLevel + PreCfgKey
  341. */
  342. len = 1 + Z_EXTADDR_LEN + 4 + 2 + 1 + SEC_KEY_LEN;
  343. buf = osal_mem_alloc( len );
  344. if ( buf )
  345. {
  346. /* Assume NV not available */
  347. osal_memset( buf, 0xFF, len );
  348. /* Skip over status */
  349. pBuf = buf + 1;
  350. /* Start with 64-bit extended address */
  351. stat = osal_nv_read( ZCD_NV_EXTADDR, 0, Z_EXTADDR_LEN, pBuf );
  352. if ( stat ) stat = 0x01;
  353. pBuf += Z_EXTADDR_LEN;
  354. /* Scan channel list (bit mask) */
  355. if ( osal_nv_read( ZCD_NV_CHANLIST, 0, sizeof( tmp32 ), &tmp32 ) )
  356. stat |= 0x02;
  357. else
  358. {
  359. pBuf[0] = BREAK_UINT32( tmp32, 3 );
  360. pBuf[1] = BREAK_UINT32( tmp32, 2 );
  361. pBuf[2] = BREAK_UINT32( tmp32, 1 );
  362. pBuf[3] = BREAK_UINT32( tmp32, 0 );
  363. }
  364. pBuf += sizeof( tmp32 );
  365. /* ZigBee PanID */
  366. if ( osal_nv_read( ZCD_NV_PANID, 0, sizeof( tmp16 ), &tmp16 ) )
  367. stat |= 0x04;
  368. else
  369. {
  370. pBuf[0] = LO_UINT16( tmp16 );
  371. pBuf[1] = HI_UINT16( tmp16 );
  372. }
  373. pBuf += sizeof( tmp16 );
  374. /* Security level */
  375. if ( osal_nv_read( ZCD_NV_SECURITY_LEVEL, 0, sizeof( uint8 ), pBuf++ ) )
  376. stat |= 0x08;
  377. /* Pre-configured security key */
  378. if ( osal_nv_read( ZCD_NV_PRECFGKEY, 0, SEC_KEY_LEN, pBuf ) )
  379. stat |= 0x10;
  380. /* Status bit mask - bit=1 indicates failure */
  381. *buf = stat;
  382. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_UTIL), MT_UTIL_GET_NV_INFO,
  383. len, buf );
  384. osal_mem_free( buf );
  385. }
  386. }
  387. /***************************************************************************************************
  388. * @fn MT_UtilSetPanID
  389. *
  390. * @brief Set PanID message
  391. *
  392. * @param pBuf - pointer to the data
  393. *
  394. * @return void
  395. ***************************************************************************************************/
  396. void MT_UtilSetPanID(uint8 *pBuf)
  397. {
  398. uint16 temp16;
  399. uint8 retValue = ZFailure;
  400. uint8 cmdId;
  401. /* parse header */
  402. cmdId = pBuf[MT_RPC_POS_CMD1];
  403. pBuf += MT_RPC_FRAME_HDR_SZ;
  404. temp16 = BUILD_UINT16(pBuf[0], pBuf[1]);
  405. pBuf += sizeof(uint16);
  406. retValue = osal_nv_write(ZCD_NV_PANID, 0, osal_nv_item_len( ZCD_NV_PANID ), &temp16);
  407. /* Build and send back the response */
  408. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_UTIL), cmdId, 1, &retValue);
  409. }
  410. /***************************************************************************************************
  411. * @fn MT_UtilSetChannels
  412. *
  413. * @brief Set Channels
  414. *
  415. * @param pBuf - pointer to the data
  416. *
  417. * @return void
  418. ***************************************************************************************************/
  419. void MT_UtilSetChannels(uint8 *pBuf)
  420. {
  421. uint32 tmp32;
  422. uint8 retValue = ZFailure;
  423. uint8 cmdId;
  424. /* parse header */
  425. cmdId = pBuf[MT_RPC_POS_CMD1];
  426. pBuf += MT_RPC_FRAME_HDR_SZ;
  427. tmp32 = BUILD_UINT32(pBuf[0], pBuf[1], pBuf[2], pBuf[3]);
  428. retValue = osal_nv_write(ZCD_NV_CHANLIST, 0, osal_nv_item_len( ZCD_NV_CHANLIST ), &tmp32);
  429. /* Build and send back the response */
  430. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_UTIL), cmdId, 1, &retValue);
  431. }
  432. /***************************************************************************************************
  433. * @fn MT_UtilSetSecLevel
  434. *
  435. * @brief Set Sec Level
  436. *
  437. * @param byte *msg - pointer to the data
  438. *
  439. * @return void
  440. ***************************************************************************************************/
  441. void MT_UtilSetSecLevel(uint8 *pBuf)
  442. {
  443. uint8 retValue = ZFailure;
  444. uint8 cmdId;
  445. /* parse header */
  446. cmdId = pBuf[MT_RPC_POS_CMD1];
  447. pBuf += MT_RPC_FRAME_HDR_SZ;
  448. retValue = osal_nv_write( ZCD_NV_SECURITY_LEVEL, 0, osal_nv_item_len( ZCD_NV_SECURITY_LEVEL ), pBuf);
  449. /* Build and send back the response */
  450. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_UTIL), cmdId, 1, &retValue );
  451. }
  452. /***************************************************************************************************
  453. * @fn MT_UtilSetPreCfgKey
  454. *
  455. * @brief Set Pre Cfg Key
  456. *
  457. * @param pBuf - pointer to the data
  458. *
  459. * @return void
  460. ***************************************************************************************************/
  461. void MT_UtilSetPreCfgKey(uint8 *pBuf)
  462. {
  463. uint8 retValue = ZFailure;
  464. uint8 cmdId;
  465. /* parse header */
  466. cmdId = pBuf[MT_RPC_POS_CMD1];
  467. pBuf += MT_RPC_FRAME_HDR_SZ;
  468. retValue = osal_nv_write( ZCD_NV_PRECFGKEY, 0, osal_nv_item_len( ZCD_NV_PRECFGKEY ), pBuf);
  469. /* Build and send back the response */
  470. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_UTIL), cmdId, 1, &retValue );
  471. }
  472. /***************************************************************************************************
  473. * @fn MT_UtilCallbackSub
  474. *
  475. * @brief The Callback subscribe.
  476. *
  477. * @param pBuf - pointer to the data
  478. *
  479. * @return void
  480. ***************************************************************************************************/
  481. void MT_UtilCallbackSub(uint8 *pBuf)
  482. {
  483. uint8 cmdId = pBuf[MT_RPC_POS_CMD1];
  484. uint8 retValue = ZFailure;
  485. #if defined(MT_MAC_CB_FUNC) || defined(MT_NWK_CB_FUNC) || defined(MT_ZDO_CB_FUNC) || defined(MT_AF_CB_FUNC) || defined(MT_SAPI_CB_FUNC)
  486. uint8 subSystem;
  487. uint16 subscribed_command;
  488. // Move past header
  489. retValue = ZSuccess;
  490. pBuf += MT_RPC_FRAME_HDR_SZ;
  491. /* Command */
  492. subscribed_command = BUILD_UINT16(pBuf[0], pBuf[1]);
  493. pBuf += 2;
  494. /* Subsystem - 5 bits on the MSB of the command */
  495. subSystem = HI_UINT16(subscribed_command) & 0x1F ;
  496. /* What is the action - SUBSCRIBE or !SUBSCRIBE */
  497. if (*pBuf)
  498. {
  499. /* Turn ON */
  500. #if defined( MT_MAC_CB_FUNC )
  501. if ((subSystem == MT_RPC_SYS_MAC) || (subscribed_command == 0xFFFF))
  502. _macCallbackSub = 0xFFFF;
  503. #endif
  504. #if defined( MT_NWK_CB_FUNC )
  505. if ((subSystem == MT_RPC_SYS_NWK) || (subscribed_command == 0xFFFF))
  506. _nwkCallbackSub = 0xFFFF;
  507. #endif
  508. #if defined( MT_ZDO_CB_FUNC )
  509. if ((subSystem == MT_RPC_SYS_ZDO) || (subscribed_command == 0xFFFF))
  510. _zdoCallbackSub = 0xFFFFFFFF;
  511. #endif
  512. #if defined( MT_AF_CB_FUNC )
  513. if ((subSystem == MT_RPC_SYS_AF) || (subscribed_command == 0xFFFF))
  514. _afCallbackSub = 0xFFFF;
  515. #endif
  516. #if defined( MT_SAPI_CB_FUNC )
  517. if ((subSystem == MT_RPC_SYS_SAPI) || (subscribed_command == 0xFFFF))
  518. _sapiCallbackSub = 0xFFFF;
  519. #endif
  520. }
  521. else
  522. {
  523. /* Turn OFF */
  524. #if defined( MT_MAC_CB_FUNC )
  525. if ((subSystem == MT_RPC_SYS_MAC) || (subscribed_command == 0xFFFF))
  526. _macCallbackSub = 0x0000;
  527. #endif
  528. #if defined( MT_NWK_CB_FUNC )
  529. if ((subSystem == MT_RPC_SYS_NWK) || (subscribed_command == 0xFFFF))
  530. _nwkCallbackSub = 0x0000;
  531. #endif
  532. #if defined( MT_ZDO_CB_FUNC )
  533. if ((subSystem == MT_RPC_SYS_ZDO) || (subscribed_command == 0xFFFF))
  534. _zdoCallbackSub = 0x00000000;
  535. #endif
  536. #if defined( MT_AF_CB_FUNC )
  537. if ((subSystem == MT_RPC_SYS_AF) || (subscribed_command == 0xFFFF))
  538. _afCallbackSub = 0x0000;
  539. #endif
  540. #if defined( MT_SAPI_CB_FUNC )
  541. if ((subSystem == MT_RPC_SYS_SAPI) || (subscribed_command == 0xFFFF))
  542. _sapiCallbackSub = 0x0000;
  543. #endif
  544. }
  545. #endif // MT_MAC_CB_FUNC || MT_NWK_CB_FUNC || MT_ZDO_CB_FUNC || MT_AF_CB_FUNC || MT_SAPI_CB_FUNC || MT_SAPI_CB_FUNC
  546. /* Build and send back the response */
  547. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_UTIL), cmdId, 1, &retValue );
  548. }
  549. #if (defined HAL_KEY) && (HAL_KEY == TRUE)
  550. /***************************************************************************************************
  551. * @fn MT_UtilKeyEvent
  552. *
  553. * @brief Process Key Event
  554. *
  555. * @param pBuf - pointer to the data
  556. *
  557. * @return void
  558. ***************************************************************************************************/
  559. void MT_UtilKeyEvent(uint8 *pBuf)
  560. {
  561. uint8 x = 0;
  562. uint8 retValue = ZFailure;
  563. uint8 cmdId;
  564. /* parse header */
  565. cmdId = pBuf[MT_RPC_POS_CMD1];
  566. pBuf += MT_RPC_FRAME_HDR_SZ;
  567. /* Translate between SPI values to device values */
  568. if ( *pBuf & 0x01 )
  569. x |= HAL_KEY_SW_1;
  570. if ( *pBuf & 0x02 )
  571. x |= HAL_KEY_SW_2;
  572. if ( *pBuf & 0x04 )
  573. x |= HAL_KEY_SW_3;
  574. if ( *pBuf & 0x08 )
  575. x |= HAL_KEY_SW_4;
  576. #if defined ( HAL_KEY_SW_5 )
  577. if ( *pBuf & 0x10 )
  578. x |= HAL_KEY_SW_5;
  579. #endif
  580. #if defined ( HAL_KEY_SW_6 )
  581. if ( *pBuf & 0x20 )
  582. x |= HAL_KEY_SW_6;
  583. #endif
  584. #if defined ( HAL_KEY_SW_7 )
  585. if ( *pBuf & 0x40 )
  586. x |= HAL_KEY_SW_7;
  587. #endif
  588. #if defined ( HAL_KEY_SW_8 )
  589. if ( *pBuf & 0x80 )
  590. x |= HAL_KEY_SW_8;
  591. #endif
  592. pBuf++;
  593. retValue = OnBoard_SendKeys(x, *pBuf);
  594. /* Build and send back the response */
  595. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_UTIL), cmdId, 1, &retValue );
  596. }
  597. #endif
  598. /***************************************************************************************************
  599. * @fn MT_UtilTimeAlive
  600. *
  601. * @brief Process Time Alive
  602. *
  603. * @param None.
  604. *
  605. * @return None
  606. ***************************************************************************************************/
  607. void MT_UtilTimeAlive(void)
  608. {
  609. uint8 timeAlive[4];
  610. uint32 tmp32;
  611. /* Time since last reset (seconds) */
  612. tmp32 = osal_GetSystemClock() / 1000;
  613. /* Convert to high byte first into temp buffer */
  614. timeAlive[0] = BREAK_UINT32(tmp32, 0);
  615. timeAlive[1] = BREAK_UINT32(tmp32, 1);
  616. timeAlive[2] = BREAK_UINT32(tmp32, 2);
  617. timeAlive[3] = BREAK_UINT32(tmp32, 3);
  618. /* Build and send back the response */
  619. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_UTIL),
  620. MT_UTIL_TIME_ALIVE, sizeof(timeAlive), timeAlive);
  621. }
  622. #if (defined HAL_LED) && (HAL_LED == TRUE)
  623. /***************************************************************************************************
  624. * @fn MT_UtilLedControl
  625. *
  626. * @brief Process the LED Control Message
  627. *
  628. * @param pBuf - pointer to the received data
  629. *
  630. * @return None
  631. ***************************************************************************************************/
  632. void MT_UtilLedControl(uint8 *pBuf)
  633. {
  634. uint8 iLed, Led, iMode, Mode, cmdId;
  635. uint8 retValue = ZFailure;
  636. /* parse header */
  637. cmdId = pBuf[MT_RPC_POS_CMD1];
  638. pBuf += MT_RPC_FRAME_HDR_SZ;
  639. /* LED and Mode */
  640. iLed = *pBuf++;
  641. iMode = *pBuf;
  642. if ( iLed == 1 )
  643. Led = HAL_LED_1;
  644. else if ( iLed == 2 )
  645. Led = HAL_LED_2;
  646. else if ( iLed == 3 )
  647. Led = HAL_LED_3;
  648. else if ( iLed == 4 )
  649. Led = HAL_LED_4;
  650. else if ( iLed == 0xFF )
  651. Led = HAL_LED_ALL;
  652. else
  653. Led = 0;
  654. if ( iMode == 0 )
  655. Mode = HAL_LED_MODE_OFF;
  656. else if ( iMode == 1 )
  657. Mode = HAL_LED_MODE_ON;
  658. else if ( iMode == 2 )
  659. Mode = HAL_LED_MODE_BLINK;
  660. else if ( iMode == 3 )
  661. Mode = HAL_LED_MODE_FLASH;
  662. else if ( iMode == 4 )
  663. Mode = HAL_LED_MODE_TOGGLE;
  664. else
  665. Led = 0;
  666. if ( Led != 0 )
  667. {
  668. HalLedSet (Led, Mode);
  669. retValue = ZSuccess;
  670. }
  671. /* Build and send back the response */
  672. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_UTIL), cmdId, 1, &retValue );
  673. }
  674. #endif /* HAL_LED */
  675. /***************************************************************************************************
  676. * @fn MT_UtilSrcMatchEnable
  677. *
  678. * @brief Enabled AUTOPEND and source address matching.
  679. *
  680. * @param pBuf - Buffer contains the data
  681. *
  682. * @return void
  683. ***************************************************************************************************/
  684. void MT_UtilSrcMatchEnable (uint8 *pBuf)
  685. {
  686. uint8 retValue, cmdId;
  687. /* Parse header */
  688. cmdId = pBuf[MT_RPC_POS_CMD1];
  689. pBuf += MT_RPC_FRAME_HDR_SZ;
  690. #ifdef AUTO_PEND
  691. /* Call the routine */
  692. retValue = ZMacSrcMatchEnable (pBuf[0], pBuf[1]);
  693. #else
  694. retValue = ZMacUnsupported;
  695. #endif
  696. /* Build and send back the response */
  697. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_UTIL), cmdId, 1, &retValue );
  698. }
  699. /***************************************************************************************************
  700. * @fn MT_UtilSrcMatchAddEntry
  701. *
  702. * @brief Add a short or extended address to source address table.
  703. *
  704. * @param pBuf - Buffer contains the data
  705. *
  706. * @return void
  707. ***************************************************************************************************/
  708. void MT_UtilSrcMatchAddEntry (uint8 *pBuf)
  709. {
  710. uint8 retValue, cmdId;
  711. /* Parse header */
  712. cmdId = pBuf[MT_RPC_POS_CMD1];
  713. pBuf += MT_RPC_FRAME_HDR_SZ;
  714. #ifdef AUTO_PEND
  715. uint16 panID;
  716. zAddrType_t devAddr;
  717. /* Address mode */
  718. devAddr.addrMode = *pBuf++;
  719. /* Address based on the address mode */
  720. MT_UtilSpi2Addr( &devAddr, pBuf);
  721. pBuf += Z_EXTADDR_LEN;
  722. /* PanID */
  723. panID = BUILD_UINT16( pBuf[0] , pBuf[1] );
  724. /* Call the routine */
  725. retValue = ZMacSrcMatchAddEntry (&devAddr, panID);
  726. #else
  727. retValue = ZMacUnsupported;
  728. #endif
  729. /* Build and send back the response */
  730. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_UTIL), cmdId, 1, &retValue );
  731. }
  732. /***************************************************************************************************
  733. * @fn MT_UtilSrcMatchDeleteEntry
  734. *
  735. * @brief Delete a short or extended address from source address table.
  736. *
  737. * @param pBuf - Buffer contains the data
  738. *
  739. * @return void
  740. ***************************************************************************************************/
  741. void MT_UtilSrcMatchDeleteEntry (uint8 *pBuf)
  742. {
  743. uint8 retValue, cmdId;
  744. /* Parse header */
  745. cmdId = pBuf[MT_RPC_POS_CMD1];
  746. pBuf += MT_RPC_FRAME_HDR_SZ;
  747. #ifdef AUTO_PEND
  748. uint16 panID;
  749. zAddrType_t devAddr;
  750. /* Address mode */
  751. devAddr.addrMode = *pBuf++;
  752. /* Address based on the address mode */
  753. MT_UtilSpi2Addr( &devAddr, pBuf);
  754. pBuf += Z_EXTADDR_LEN;
  755. /* PanID */
  756. panID = BUILD_UINT16( pBuf[0] , pBuf[1] );
  757. /* Call the routine */
  758. retValue = ZMacSrcMatchDeleteEntry (&devAddr, panID);
  759. #else
  760. retValue = ZMacUnsupported;
  761. #endif
  762. /* Build and send back the response */
  763. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_UTIL), cmdId, 1, &retValue );
  764. }
  765. /***************************************************************************************************
  766. * @fn MT_UtilSrcMatchCheckSrcAddr
  767. *
  768. * @brief Check if a short or extended address is in the source address table.
  769. *
  770. * @param pBuf - Buffer contains the data
  771. *
  772. * @return void
  773. ***************************************************************************************************/
  774. void MT_UtilSrcMatchCheckSrcAddr (uint8 *pBuf)
  775. {
  776. uint8 cmdId;
  777. uint8 retArray[2];
  778. /* Parse header */
  779. cmdId = pBuf[MT_RPC_POS_CMD1];
  780. pBuf += MT_RPC_FRAME_HDR_SZ;
  781. #if 0 /* Unsupported */
  782. uint16 panID;
  783. zAddrType_t devAddr;
  784. /* Address mode */
  785. devAddr.addrMode = *pBuf++;
  786. /* Address based on the address mode */
  787. MT_UtilSpi2Addr( &devAddr, pBuf);
  788. pBuf += Z_EXTADDR_LEN;
  789. /* PanID */
  790. panID = BUILD_UINT16( pBuf[0] , pBuf[1] );
  791. /* Call the routine */
  792. retArray[1] = ZMacSrcMatchCheckSrcAddr (&devAddr, panID);
  793. /* Return failure if the index is invalid */
  794. if (retArray[1] == ZMacSrcMatchInvalidIndex )
  795. {
  796. retArray[0] = ZFailure;
  797. }
  798. else
  799. {
  800. retArray[0] = ZSuccess;
  801. }
  802. #else
  803. retArray[0] = ZMacUnsupported;
  804. retArray[1] = ZMacSrcMatchInvalidIndex;
  805. #endif
  806. /* Build and send back the response */
  807. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_UTIL), cmdId, 2, retArray );
  808. }
  809. /***************************************************************************************************
  810. * @fn MT_UtilSrcMatchAckAllPending
  811. *
  812. * @brief Enabled/disable acknowledging all packets with pending bit set
  813. * It is normally enabled when adding new entries to
  814. * the source address table fails due to the table is full, or
  815. * disabled when more entries are deleted and the table has
  816. * empty slots.
  817. *
  818. * @param pBuf - Buffer contains the data
  819. *
  820. * @return void
  821. ***************************************************************************************************/
  822. void MT_UtilSrcMatchAckAllPending (uint8 *pBuf)
  823. {
  824. uint8 retValue, cmdId;
  825. /* Parse header */
  826. cmdId = pBuf[MT_RPC_POS_CMD1];
  827. pBuf += MT_RPC_FRAME_HDR_SZ;
  828. #ifdef AUTO_PEND
  829. /* Call the routine */
  830. retValue = ZMacSrcMatchAckAllPending(*pBuf);
  831. #else
  832. retValue = ZMacUnsupported;
  833. #endif
  834. /* Build and send back the response */
  835. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_UTIL), cmdId, 1, &retValue );
  836. }
  837. /***************************************************************************************************
  838. * @fn MT_UtilSrcMatchCheckAllPending
  839. *
  840. * @brief Check if acknowledging all packets with pending bit set
  841. * is enabled.
  842. *
  843. * @param pBuf - Buffer contains the data
  844. *
  845. * @return void
  846. ***************************************************************************************************/
  847. void MT_UtilSrcMatchCheckAllPending (uint8 *pBuf)
  848. {
  849. uint8 retArray[2], cmdId;
  850. /* Parse header */
  851. cmdId = pBuf[MT_RPC_POS_CMD1];
  852. pBuf += MT_RPC_FRAME_HDR_SZ;
  853. #ifdef AUTO_PEND
  854. /* Call the routine */
  855. retArray[0] = ZMacSuccess;
  856. retArray[1] = ZMacSrcMatchCheckAllPending();
  857. #else
  858. retArray[0] = ZMacUnsupported;
  859. retArray[1] = FALSE;
  860. #endif
  861. /* Build and send back the response */
  862. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_UTIL), cmdId, 2, retArray );
  863. }
  864. /***************************************************************************************************
  865. * SUPPORT
  866. ***************************************************************************************************/
  867. #ifdef AUTO_PEND
  868. /***************************************************************************************************
  869. * @fn MT_UtilRevExtCpy
  870. *
  871. * @brief
  872. *
  873. * Reverse-copy an extended address.
  874. *
  875. * @param pDst - Pointer to data destination
  876. * @param pSrc - Pointer to data source
  877. *
  878. * @return void
  879. ***************************************************************************************************/
  880. static void MT_UtilRevExtCpy( uint8 *pDst, uint8 *pSrc )
  881. {
  882. int8 i;
  883. for ( i = Z_EXTADDR_LEN - 1; i >= 0; i-- )
  884. {
  885. *pDst++ = pSrc[i];
  886. }
  887. }
  888. /***************************************************************************************************
  889. * @fn MT_UtilSpi2Addr
  890. *
  891. * @brief Copy an address from an SPI message to an address struct. The
  892. * addrMode in pAddr must already be set.
  893. *
  894. * @param pDst - Pointer to address struct
  895. * @param pSrc - Pointer SPI message byte array
  896. *
  897. * @return void
  898. ***************************************************************************************************/
  899. static void MT_UtilSpi2Addr( zAddrType_t *pDst, uint8 *pSrc )
  900. {
  901. if ( pDst->addrMode == Addr16Bit )
  902. {
  903. pDst->addr.shortAddr = BUILD_UINT16( pSrc[0] , pSrc[1] );
  904. }
  905. else if ( pDst->addrMode == Addr64Bit )
  906. {
  907. MT_UtilRevExtCpy( pDst->addr.extAddr, pSrc );
  908. }
  909. }
  910. #endif // AUTO_PEND
  911. #if !defined NONWK
  912. /**************************************************************************************************
  913. * @fn MT_UtilDataReq
  914. *
  915. * @brief Process the MAC Data Request command.
  916. *
  917. * @param pBuf - pointer to the received data
  918. *
  919. * @return None
  920. **************************************************************************************************/
  921. void MT_UtilDataReq(uint8 *pBuf)
  922. {
  923. uint8 rtrn = NwkPollReq(pBuf[MT_RPC_POS_DAT0]);
  924. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_UTIL), MT_UTIL_DATA_REQ,
  925. 1, &rtrn);
  926. }
  927. /***************************************************************************************************
  928. * @fn MT_UtilAddrMgrEntryLookupExt
  929. *
  930. * @brief Proxy the AddrMgrEntryLookupExt() function.
  931. *
  932. * @param pBuf - pointer to the received buffer
  933. *
  934. * @return void
  935. ***************************************************************************************************/
  936. static void MT_UtilAddrMgrEntryLookupExt(uint8 *pBuf)
  937. {
  938. uint8 nwkAddr[2];
  939. AddrMgrEntry_t entry;
  940. uint8 cmdId = pBuf[MT_RPC_POS_CMD1];
  941. pBuf += MT_RPC_FRAME_HDR_SZ;
  942. osal_memcpy(entry.extAddr, pBuf, Z_EXTADDR_LEN);
  943. (void)AddrMgrEntryLookupExt(&entry);
  944. nwkAddr[0] = LO_UINT16(entry.nwkAddr);
  945. nwkAddr[1] = HI_UINT16(entry.nwkAddr);
  946. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_UTIL),
  947. cmdId, sizeof(uint16), nwkAddr);
  948. }
  949. /***************************************************************************************************
  950. * @fn MT_UtilAddrMgrEntryLookupNwk
  951. *
  952. * @brief Proxy the AddrMgrEntryLookupNwk() function.
  953. *
  954. * @param pBuf - pointer to the received buffer
  955. *
  956. * @return void
  957. ***************************************************************************************************/
  958. static void MT_UtilAddrMgrEntryLookupNwk(uint8 *pBuf)
  959. {
  960. AddrMgrEntry_t entry;
  961. uint8 cmdId = pBuf[MT_RPC_POS_CMD1];
  962. pBuf += MT_RPC_FRAME_HDR_SZ;
  963. entry.nwkAddr = BUILD_UINT16(pBuf[0], pBuf[1]);
  964. (void)AddrMgrEntryLookupNwk(&entry);
  965. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_UTIL),
  966. cmdId, Z_EXTADDR_LEN, entry.extAddr);
  967. }
  968. #if defined MT_SYS_KEY_MANAGEMENT
  969. /***************************************************************************************************
  970. * @fn MT_UtilAPSME_LinkKeyDataGet
  971. *
  972. * @brief Retrieves APS Link Key data from NV.
  973. *
  974. * @param pBuf - pointer to the received buffer
  975. *
  976. * @return void
  977. ***************************************************************************************************/
  978. static void MT_UtilAPSME_LinkKeyDataGet(uint8 *pBuf)
  979. {
  980. uint8 rsp[MT_APSME_LINKKEY_GET_RSP_LEN];
  981. APSME_LinkKeyData_t *pData = NULL;
  982. uint8 cmdId = pBuf[MT_RPC_POS_CMD1];
  983. uint16 apsLinkKeyNvId;
  984. uint32 *apsRxFrmCntr;
  985. uint32 *apsTxFrmCntr;
  986. pBuf += MT_RPC_FRAME_HDR_SZ;
  987. *rsp = APSME_LinkKeyNVIdGet(pBuf, &apsLinkKeyNvId);
  988. if (SUCCESS == *rsp)
  989. {
  990. pData = (APSME_LinkKeyData_t *)osal_mem_alloc(sizeof(APSME_LinkKeyData_t));
  991. if (pData != NULL)
  992. {
  993. // retrieve key from NV
  994. if ( osal_nv_read( apsLinkKeyNvId, 0,
  995. sizeof(APSME_LinkKeyData_t), pData) == SUCCESS)
  996. {
  997. apsRxFrmCntr = &ApsLinkKeyFrmCntr[apsLinkKeyNvId - ZCD_NV_APS_LINK_KEY_DATA_START].rxFrmCntr;
  998. apsTxFrmCntr = &ApsLinkKeyFrmCntr[apsLinkKeyNvId - ZCD_NV_APS_LINK_KEY_DATA_START].txFrmCntr;
  999. uint8 *ptr = rsp+1;
  1000. (void)osal_memcpy(ptr, pData->key, SEC_KEY_LEN);
  1001. ptr += SEC_KEY_LEN;
  1002. *ptr++ = BREAK_UINT32(*apsTxFrmCntr, 0);
  1003. *ptr++ = BREAK_UINT32(*apsTxFrmCntr, 1);
  1004. *ptr++ = BREAK_UINT32(*apsTxFrmCntr, 2);
  1005. *ptr++ = BREAK_UINT32(*apsTxFrmCntr, 3);
  1006. *ptr++ = BREAK_UINT32(*apsRxFrmCntr, 0);
  1007. *ptr++ = BREAK_UINT32(*apsRxFrmCntr, 1);
  1008. *ptr++ = BREAK_UINT32(*apsRxFrmCntr, 2);
  1009. *ptr++ = BREAK_UINT32(*apsRxFrmCntr, 3);
  1010. }
  1011. // clear copy of key in RAM
  1012. osal_memset( pData, 0x00, sizeof(APSME_LinkKeyData_t) );
  1013. osal_mem_free(pData);
  1014. }
  1015. }
  1016. else
  1017. {
  1018. // set data key and counters 0xFF
  1019. osal_memset(&rsp[1], 0xFF, SEC_KEY_LEN + (MT_UTIL_FRM_CTR_LEN * 2));
  1020. }
  1021. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_UTIL), cmdId,
  1022. MT_APSME_LINKKEY_GET_RSP_LEN, rsp);
  1023. // clear key data
  1024. osal_memset(rsp, 0x00, MT_APSME_LINKKEY_GET_RSP_LEN);
  1025. }
  1026. /***************************************************************************************************
  1027. * @fn MT_UtilAPSME_LinkKeyNvIdGet
  1028. *
  1029. * @brief Retrieves APS Link Key NV ID from the entry table.
  1030. *
  1031. * @param pBuf - pointer to the received buffer
  1032. *
  1033. * @return void
  1034. ***************************************************************************************************/
  1035. static void MT_UtilAPSME_LinkKeyNvIdGet(uint8 *pBuf)
  1036. {
  1037. uint8 rsp[MT_APSME_LINKKEY_NV_ID_GET_RSP_LEN];
  1038. uint8 cmdId = pBuf[MT_RPC_POS_CMD1];
  1039. uint16 apsLinkKeyNvId;
  1040. pBuf += MT_RPC_FRAME_HDR_SZ;
  1041. *rsp = APSME_LinkKeyNVIdGet(pBuf, &apsLinkKeyNvId);
  1042. if (SUCCESS == *rsp)
  1043. {
  1044. rsp[1] = LO_UINT16(apsLinkKeyNvId);
  1045. rsp[2] = HI_UINT16(apsLinkKeyNvId);
  1046. }
  1047. else
  1048. {
  1049. // send failure response with invalid NV ID
  1050. osal_memset(&rsp[1], 0xFF, 2);
  1051. }
  1052. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_UTIL), cmdId,
  1053. MT_APSME_LINKKEY_NV_ID_GET_RSP_LEN, rsp);
  1054. }
  1055. #endif // MT_SYS_KEY_MANAGEMENT
  1056. /***************************************************************************************************
  1057. * @fn MT_UtilAPSME_RequestKeyCmd
  1058. *
  1059. * @brief Send RequestKey command message to TC for a specific partner Address.
  1060. *
  1061. * @param pBuf - pointer to the received buffer
  1062. *
  1063. * @return void
  1064. ***************************************************************************************************/
  1065. void MT_UtilAPSME_RequestKeyCmd(uint8 *pBuf)
  1066. {
  1067. uint8 cmdId;
  1068. uint8 partnerAddr[Z_EXTADDR_LEN];
  1069. uint8 retValue;
  1070. // parse header
  1071. cmdId = pBuf[MT_RPC_POS_CMD1];
  1072. pBuf += MT_RPC_FRAME_HDR_SZ;
  1073. /* PartnerAddress */
  1074. osal_memcpy(partnerAddr, pBuf, Z_EXTADDR_LEN);
  1075. retValue = (uint8)ZDSecMgrRequestAppKey(partnerAddr);
  1076. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_UTIL), cmdId, 1, &retValue);
  1077. }
  1078. /***************************************************************************************************
  1079. * @fn MT_UtilAssocCount
  1080. *
  1081. * @brief Proxy the AssocCount() function.
  1082. *
  1083. * @param pBuf - pointer to the received buffer
  1084. *
  1085. * @return void
  1086. ***************************************************************************************************/
  1087. static void MT_UtilAssocCount(uint8 *pBuf)
  1088. {
  1089. uint16 cnt;
  1090. uint8 cmdId = pBuf[MT_RPC_POS_CMD1];
  1091. pBuf += MT_RPC_FRAME_HDR_SZ;
  1092. cnt = AssocCount(pBuf[0], pBuf[1]);
  1093. pBuf[0] = LO_UINT16(cnt);
  1094. pBuf[1] = HI_UINT16(cnt);
  1095. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_UTIL), cmdId, 2, pBuf);
  1096. }
  1097. /***************************************************************************************************
  1098. * @fn MT_UtilAssocFindDevice
  1099. *
  1100. * @brief Get an associated device by index.
  1101. *
  1102. * @param pBuf - pointer to the received buffer
  1103. *
  1104. * @return void
  1105. ***************************************************************************************************/
  1106. static void MT_UtilAssocFindDevice(uint8 *pBuf)
  1107. {
  1108. uint8 cmdId = pBuf[MT_RPC_POS_CMD1];
  1109. uint8 buf[sizeof(associated_devices_t)];
  1110. packDev_t(buf, AssocFindDevice(pBuf[MT_RPC_FRAME_HDR_SZ]));
  1111. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_UTIL), cmdId,
  1112. sizeof(associated_devices_t), buf);
  1113. }
  1114. /***************************************************************************************************
  1115. * @fn MT_UtilAssocGetWithAddress
  1116. *
  1117. * @brief Get an associated device by address.
  1118. *
  1119. * @param pBuf - pointer to the received buffer
  1120. *
  1121. * @return void
  1122. ***************************************************************************************************/
  1123. static void MT_UtilAssocGetWithAddress(uint8 *pBuf)
  1124. {
  1125. extern associated_devices_t *AssocGetWithAddress(uint8 *extAddr, uint16 shortAddr);
  1126. uint8 cmdId = pBuf[MT_RPC_POS_CMD1];
  1127. uint8 buf[sizeof(associated_devices_t)];
  1128. pBuf += MT_RPC_FRAME_HDR_SZ;
  1129. packDev_t(buf, AssocGetWithAddress(((AddrMgrExtAddrValid(pBuf)) ? pBuf : NULL),
  1130. BUILD_UINT16(pBuf[Z_EXTADDR_LEN], pBuf[Z_EXTADDR_LEN+1])));
  1131. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_UTIL), cmdId,
  1132. sizeof(associated_devices_t), buf);
  1133. }
  1134. /***************************************************************************************************
  1135. * @fn packDev_t
  1136. *
  1137. * @brief Pack an associated_devices_t structure into a byte buffer (pack INVALID_NODE_ADDR if
  1138. * the pDev parameter is NULL).
  1139. *
  1140. * @param pBuf - pointer to the buffer into which to pack the structure.
  1141. * @param pDev - pointer to the structure.
  1142. *
  1143. * @return void
  1144. ***************************************************************************************************/
  1145. static void packDev_t(uint8 *pBuf, associated_devices_t *pDev)
  1146. {
  1147. if (NULL == pDev)
  1148. {
  1149. uint16 rtrn = INVALID_NODE_ADDR;
  1150. *pBuf++ = LO_UINT16(rtrn);
  1151. *pBuf++ = HI_UINT16(rtrn);
  1152. }
  1153. else
  1154. {
  1155. *pBuf++ = LO_UINT16(pDev->shortAddr);
  1156. *pBuf++ = HI_UINT16(pDev->shortAddr);
  1157. *pBuf++ = LO_UINT16(pDev->addrIdx);
  1158. *pBuf++ = HI_UINT16(pDev->addrIdx);
  1159. *pBuf++ = pDev->nodeRelation;
  1160. *pBuf++ = pDev->devStatus;
  1161. *pBuf++ = pDev->assocCnt;
  1162. *pBuf++ = pDev->age;
  1163. *pBuf++ = pDev->linkInfo.txCounter;
  1164. *pBuf++ = pDev->linkInfo.txCost;
  1165. *pBuf++ = pDev->linkInfo.rxLqi;
  1166. *pBuf++ = pDev->linkInfo.inKeySeqNum;
  1167. *pBuf++ = BREAK_UINT32(pDev->linkInfo.inFrmCntr, 0);
  1168. *pBuf++ = BREAK_UINT32(pDev->linkInfo.inFrmCntr, 1);
  1169. *pBuf++ = BREAK_UINT32(pDev->linkInfo.inFrmCntr, 2);
  1170. *pBuf++ = BREAK_UINT32(pDev->linkInfo.inFrmCntr, 3);
  1171. *pBuf++ = LO_UINT16(pDev->linkInfo.txFailure);
  1172. *pBuf++ = HI_UINT16(pDev->linkInfo.txFailure);
  1173. }
  1174. }
  1175. #if defined ZCL_KEY_ESTABLISH
  1176. /***************************************************************************************************
  1177. * @fn MT_UtilzclGeneral_KeyEstablish_InitiateKeyEstablishment
  1178. *
  1179. * @brief Proxy the zclGeneral_KeyEstablish_InitiateKeyEstablishment() function.
  1180. *
  1181. * @param pBuf - pointer to the received buffer
  1182. *
  1183. * @return void
  1184. ***************************************************************************************************/
  1185. static void MT_UtilzclGeneral_KeyEstablish_InitiateKeyEstablishment(uint8 *pBuf)
  1186. {
  1187. afAddrType_t partnerAddr;
  1188. uint8 cmdId = pBuf[MT_RPC_POS_CMD1];
  1189. pBuf += MT_RPC_FRAME_HDR_SZ;
  1190. partnerAddr.panId = 0; // Not an inter-pan message.
  1191. partnerAddr.endPoint = pBuf[2];
  1192. partnerAddr.addrMode = (afAddrMode_t)pBuf[3];
  1193. if (afAddr64Bit == partnerAddr.addrMode)
  1194. {
  1195. (void)osal_memcpy(partnerAddr.addr.extAddr, pBuf+4, Z_EXTADDR_LEN);
  1196. }
  1197. else
  1198. {
  1199. partnerAddr.addr.shortAddr = BUILD_UINT16(pBuf[4], pBuf[5]);
  1200. }
  1201. zcl_key_establish_task_id = pBuf[0];
  1202. *pBuf = zclGeneral_KeyEstablish_InitiateKeyEstablishment(MT_TaskID, &partnerAddr, pBuf[1]);
  1203. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_UTIL), cmdId, 1, pBuf);
  1204. }
  1205. /***************************************************************************************************
  1206. * @fn MT_UtilzclGeneral_KeyEstablishment_ECDSASign
  1207. *
  1208. * @brief Proxy the zclGeneral_KeyEstablishment_ECDSASign() function.
  1209. *
  1210. * @param pBuf - pointer to the received buffer
  1211. *
  1212. * @return void
  1213. ***************************************************************************************************/
  1214. static void MT_UtilzclGeneral_KeyEstablishment_ECDSASign(uint8 *pBuf)
  1215. {
  1216. #if defined TC_LINKKEY_JOIN
  1217. uint8 *output = osal_mem_alloc(SE_PROFILE_SIGNATURE_LENGTH+1);
  1218. uint8 cmdId = pBuf[MT_RPC_POS_CMD1];
  1219. pBuf += MT_RPC_FRAME_HDR_SZ;
  1220. if (NULL == output)
  1221. {
  1222. *pBuf = FAILURE;
  1223. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_UTIL), cmdId, 1, pBuf);
  1224. }
  1225. else
  1226. {
  1227. *output = zclGeneral_KeyEstablishment_ECDSASign(pBuf+1, *pBuf, output+1);
  1228. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_UTIL), cmdId,
  1229. SE_PROFILE_SIGNATURE_LENGTH+1, output);
  1230. osal_mem_free(output);
  1231. }
  1232. #endif
  1233. }
  1234. /***************************************************************************************************
  1235. * @fn MT_UtilKeyEstablishInd
  1236. *
  1237. * @brief Proxy the ZCL_KEY_ESTABLISH_IND command.
  1238. *
  1239. * @param pInd - Pointer to a keyEstablishmentInd_t structure.
  1240. *
  1241. * @return None
  1242. ***************************************************************************************************/
  1243. void MT_UtilKeyEstablishInd(keyEstablishmentInd_t *pInd)
  1244. {
  1245. uint8 msg[6];
  1246. msg[0] = zcl_key_establish_task_id;
  1247. msg[1] = pInd->hdr.event;
  1248. msg[2] = pInd->hdr.status;
  1249. msg[3] = pInd->waitTime;
  1250. msg[4] = LO_UINT16(pInd->keyEstablishmentSuite);
  1251. msg[5] = HI_UINT16(pInd->keyEstablishmentSuite);
  1252. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_AREQ | (uint8)MT_RPC_SYS_UTIL),
  1253. MT_UTIL_ZCL_KEY_ESTABLISH_IND, 6, msg);
  1254. }
  1255. #endif
  1256. /***************************************************************************************************
  1257. * @fn MT_UtilSync
  1258. *
  1259. * @brief Process the MT_UTIL_SYNC command
  1260. *
  1261. * @param None
  1262. *
  1263. * @return None
  1264. ***************************************************************************************************/
  1265. static void MT_UtilSync(void)
  1266. {
  1267. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_AREQ|(uint8)MT_RPC_SYS_UTIL),MT_UTIL_SYNC_REQ,0,0);
  1268. }
  1269. #endif /* !defined NONWK */
  1270. #endif /* MT_UTIL_FUNC */
  1271. /**************************************************************************************************
  1272. **************************************************************************************************/