MT_SYS.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  1. /******************************************************************************
  2. Filename: MT_SYS.c
  3. Revised: $Date: 2012-03-07 01:04:58 -0800 (Wed, 07 Mar 2012) $
  4. Revision: $Revision: 29656 $
  5. Description: MonitorTest functions for SYS commands.
  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 "MT.h"
  38. #include "MT_SYS.h"
  39. #include "MT_VERSION.h"
  40. #include "nwk_util.h"
  41. #include "OSAL.h"
  42. #include "OSAL_NV.h"
  43. #include "Onboard.h" /* This is here because RAM read/write macros need it */
  44. #include "hal_adc.h"
  45. #include "ZGlobals.h"
  46. #include "OSAL_Clock.h"
  47. #include "mac_low_level.h"
  48. /***************************************************************************************************
  49. * MACROS
  50. ***************************************************************************************************/
  51. #define MT_SYS_DEVICE_INFO_RESPONSE_LEN 14
  52. #define MT_NV_ITEM_MAX_LENGTH 250
  53. #if !defined HAL_GPIO || !HAL_GPIO
  54. #define GPIO_DIR_IN(IDX)
  55. #define GPIO_DIR_OUT(IDX)
  56. #define GPIO_TRI(IDX)
  57. #define GPIO_PULL_UP(IDX)
  58. #define GPIO_PULL_DN(IDX)
  59. #define GPIO_SET(IDX)
  60. #define GPIO_CLR(IDX)
  61. #define GPIO_TOG(IDX)
  62. #define GPIO_GET(IDX) 0
  63. #define GPIO_HiD_SET() (val = 0)
  64. #define GPIO_HiD_CLR() (val = 0)
  65. #endif
  66. /***************************************************************************************************
  67. * CONSTANTS
  68. ***************************************************************************************************/
  69. #if !defined MT_SYS_OSAL_NV_READ_CERTIFICATE_DATA
  70. #define MT_SYS_OSAL_NV_READ_CERTIFICATE_DATA FALSE
  71. #endif
  72. const uint16 MT_SysOsalEventId [] = {
  73. MT_SYS_OSAL_EVENT_0,
  74. MT_SYS_OSAL_EVENT_1,
  75. MT_SYS_OSAL_EVENT_2,
  76. MT_SYS_OSAL_EVENT_3
  77. };
  78. typedef enum {
  79. GPIO_DIR,
  80. GPIO_TRI,
  81. GPIO_SET,
  82. GPIO_CLR,
  83. GPIO_TOG,
  84. GPIO_GET,
  85. GPIO_HiD = 0x12
  86. } GPIO_Op_t;
  87. /***************************************************************************************************
  88. * LOCAL FUNCTIONS
  89. ***************************************************************************************************/
  90. #if defined (MT_SYS_FUNC)
  91. void MT_SysReset(uint8 *pBuf);
  92. void MT_SysPing(void);
  93. void MT_SysVersion(void);
  94. void MT_SysSetExtAddr(uint8 *pBuf);
  95. void MT_SysGetExtAddr(void);
  96. void MT_SysOsalNVItemInit(uint8 *pBuf);
  97. void MT_SysOsalNVDelete(uint8 *pBuf);
  98. void MT_SysOsalNVLength(uint8 *pBuf);
  99. void MT_SysOsalNVRead(uint8 *pBuf);
  100. void MT_SysOsalNVWrite(uint8 *pBuf);
  101. void MT_SysOsalStartTimer(uint8 *pBuf);
  102. void MT_SysOsalStopTimer(uint8 *pBuf);
  103. void MT_SysRandom(void);
  104. void MT_SysAdcRead(uint8 *pBuf);
  105. void MT_SysGpio(uint8 *pBuf);
  106. void MT_SysStackTune(uint8 *pBuf);
  107. void MT_SysGetDeviceInfo(uint8 *pBuf);
  108. void MT_SysSetUtcTime(uint8 *pBuf);
  109. void MT_SysGetUtcTime(void);
  110. void MT_SysSetTxPower(uint8 *pBuf);
  111. #endif /* MT_SYS_FUNC */
  112. #if defined (MT_SYS_FUNC)
  113. /***************************************************************************************************
  114. * @fn MT_SysProcessing
  115. *
  116. * @brief Process all the SYS commands that are issued by test tool
  117. *
  118. * @param pBuf - pointer to the msg buffer
  119. *
  120. * | LEN | CMD0 | CMD1 | DATA |
  121. * | 1 | 1 | 1 | 0-255 |
  122. *
  123. * @return status
  124. ***************************************************************************************************/
  125. uint8 MT_SysCommandProcessing(uint8 *pBuf)
  126. {
  127. uint8 status = MT_RPC_SUCCESS;
  128. switch (pBuf[MT_RPC_POS_CMD1])
  129. {
  130. case MT_SYS_RESET_REQ:
  131. MT_SysReset(pBuf);
  132. break;
  133. case MT_SYS_PING:
  134. MT_SysPing();
  135. break;
  136. case MT_SYS_VERSION:
  137. MT_SysVersion();
  138. break;
  139. case MT_SYS_SET_EXTADDR:
  140. MT_SysSetExtAddr(pBuf);
  141. break;
  142. case MT_SYS_GET_EXTADDR:
  143. MT_SysGetExtAddr();
  144. break;
  145. // CC253X MAC Network Processor does not have NV support
  146. #if !defined(CC253X_MACNP)
  147. case MT_SYS_OSAL_NV_DELETE:
  148. MT_SysOsalNVDelete(pBuf);
  149. break;
  150. case MT_SYS_OSAL_NV_ITEM_INIT:
  151. MT_SysOsalNVItemInit(pBuf);
  152. break;
  153. case MT_SYS_OSAL_NV_LENGTH:
  154. MT_SysOsalNVLength(pBuf);
  155. break;
  156. case MT_SYS_OSAL_NV_READ:
  157. MT_SysOsalNVRead(pBuf);
  158. break;
  159. case MT_SYS_OSAL_NV_WRITE:
  160. MT_SysOsalNVWrite(pBuf);
  161. break;
  162. #endif
  163. case MT_SYS_OSAL_START_TIMER:
  164. MT_SysOsalStartTimer(pBuf);
  165. break;
  166. case MT_SYS_OSAL_STOP_TIMER:
  167. MT_SysOsalStopTimer(pBuf);
  168. break;
  169. case MT_SYS_RANDOM:
  170. MT_SysRandom();
  171. break;
  172. case MT_SYS_ADC_READ:
  173. MT_SysAdcRead(pBuf);
  174. break;
  175. case MT_SYS_GPIO:
  176. MT_SysGpio(pBuf);
  177. break;
  178. case MT_SYS_STACK_TUNE:
  179. MT_SysStackTune(pBuf);
  180. break;
  181. case MT_SYS_SET_TIME:
  182. MT_SysSetUtcTime(pBuf);
  183. break;
  184. case MT_SYS_GET_TIME:
  185. MT_SysGetUtcTime();
  186. break;
  187. case MT_SYS_SET_TX_POWER:
  188. MT_SysSetTxPower(pBuf);
  189. break;
  190. default:
  191. status = MT_RPC_ERR_COMMAND_ID;
  192. break;
  193. }
  194. return status;
  195. }
  196. /**************************************************************************************************
  197. * @fn MT_SysReset
  198. *
  199. * @brief Reset the device.
  200. * @param typID: 0=reset, 1=serial bootloader
  201. *
  202. * @return None
  203. *************************************************************************************************/
  204. void MT_SysReset(uint8 *pBuf)
  205. {
  206. if (pBuf[MT_RPC_POS_DAT0] == 0)
  207. {
  208. SystemReset();
  209. }
  210. #if !(defined(HAL_BOARD_F2618) || defined(HAL_BOARD_F5438) || defined(HAL_BOARD_LM3S))
  211. else
  212. {
  213. SystemResetSoft(); // Especially useful for CC2531 to not break comm with USB Host.
  214. }
  215. #endif
  216. }
  217. /***************************************************************************************************
  218. * @fn MT_SysPing
  219. *
  220. * @brief Process the Ping command
  221. *
  222. * @param None
  223. *
  224. * @return None
  225. ***************************************************************************************************/
  226. void MT_SysPing(void)
  227. {
  228. uint16 tmp16;
  229. uint8 retArray[2];
  230. /* Build Capabilities */
  231. tmp16 = MT_CAP_SYS | MT_CAP_MAC | MT_CAP_NWK | MT_CAP_AF | MT_CAP_ZDO |
  232. MT_CAP_SAPI | MT_CAP_UTIL | MT_CAP_DEBUG | MT_CAP_APP | MT_CAP_ZOAD;
  233. /* Convert to high byte first into temp buffer */
  234. retArray[0] = LO_UINT16( tmp16 );
  235. retArray[1] = HI_UINT16( tmp16 );
  236. /* Build and send back the response */
  237. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_SYS), MT_SYS_PING,
  238. sizeof (tmp16), retArray );
  239. }
  240. /***************************************************************************************************
  241. * @fn MT_SysVersion
  242. *
  243. * @brief Process the Version command
  244. *
  245. * @param None
  246. *
  247. * @return None
  248. ***************************************************************************************************/
  249. void MT_SysVersion(void)
  250. {
  251. byte *verStr = (byte *)MTVersionString;
  252. uint8 respLen = sizeof(MTVersionString);
  253. /* Build and send back the response */
  254. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_SYS), MT_SYS_VERSION,
  255. respLen, verStr);
  256. }
  257. /***************************************************************************************************
  258. * @fn MT_SysSetExtAddr
  259. *
  260. * @brief Set the Extended Address
  261. *
  262. * @param pBuf
  263. *
  264. * @return None
  265. ***************************************************************************************************/
  266. void MT_SysSetExtAddr(uint8 *pBuf)
  267. {
  268. uint8 retValue = ZFailure;
  269. uint8 cmdId;
  270. /* parse header */
  271. cmdId = pBuf[MT_RPC_POS_CMD1];
  272. pBuf += MT_RPC_FRAME_HDR_SZ;
  273. if ( ZMacSetReq(ZMacExtAddr, pBuf) == ZMacSuccess )
  274. {
  275. // CC253X MAC Network Processor does not have NV support
  276. #if !defined(CC253X_MACNP)
  277. retValue = osal_nv_write(ZCD_NV_EXTADDR, 0, Z_EXTADDR_LEN, pBuf);
  278. #endif
  279. }
  280. /* Build and send back the response */
  281. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_SYS), cmdId, 1, &retValue);
  282. }
  283. /***************************************************************************************************
  284. * @fn MT_SysGetExtAddr
  285. *
  286. * @brief Get the Extended Address
  287. *
  288. * @param None
  289. *
  290. * @return None
  291. ***************************************************************************************************/
  292. void MT_SysGetExtAddr(void)
  293. {
  294. uint8 extAddr[Z_EXTADDR_LEN];
  295. ZMacGetReq( ZMacExtAddr, extAddr );
  296. /* Build and send back the response */
  297. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_SYS), MT_SYS_GET_EXTADDR,
  298. Z_EXTADDR_LEN, extAddr);
  299. }
  300. /***************************************************************************************************
  301. * @fn MT_SysOsalNVRead
  302. *
  303. * @brief Read a NV value
  304. *
  305. * @param uint8 pBuf - pointer to the data
  306. *
  307. * @return None
  308. ***************************************************************************************************/
  309. void MT_SysOsalNVRead(uint8 *pBuf)
  310. {
  311. uint16 nvId;
  312. uint8 nvItemLen=0, nvItemOffset=0;
  313. uint8 *pRetBuf=NULL;
  314. uint8 respLen;
  315. /* Skip over RPC header */
  316. pBuf += MT_RPC_FRAME_HDR_SZ;
  317. /* Get the ID */
  318. nvId = BUILD_UINT16(pBuf[0], pBuf[1]);
  319. /* Get the offset */
  320. nvItemOffset = pBuf[2];
  321. #if !MT_SYS_OSAL_NV_READ_CERTIFICATE_DATA
  322. if ((ZCD_NV_IMPLICIT_CERTIFICATE == nvId) ||
  323. (ZCD_NV_CA_PUBLIC_KEY == nvId) ||
  324. (ZCD_NV_DEVICE_PRIVATE_KEY == nvId))
  325. {
  326. uint8 tmp[2] = { INVALIDPARAMETER, 0 };
  327. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_SYS),
  328. MT_SYS_OSAL_NV_READ, 2, tmp);
  329. return;
  330. }
  331. #endif
  332. #if !MT_SYS_KEY_MANAGEMENT
  333. if ( (nvId == ZCD_NV_NWK_ACTIVE_KEY_INFO) ||
  334. (nvId == ZCD_NV_NWK_ALTERN_KEY_INFO) ||
  335. ((nvId >= ZCD_NV_TCLK_TABLE_START) && (nvId <= ZCD_NV_TCLK_TABLE_END)) ||
  336. ((nvId >= ZCD_NV_APS_LINK_KEY_DATA_START) && (nvId <= ZCD_NV_APS_LINK_KEY_DATA_END)) ||
  337. ((nvId >= ZCD_NV_MASTER_KEY_DATA_START) && (nvId <= ZCD_NV_MASTER_KEY_DATA_END)) ||
  338. (nvId == ZCD_NV_PRECFGKEY) )
  339. {
  340. uint8 tmp1[2] = { INVALIDPARAMETER, 0 };
  341. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_SYS),
  342. MT_SYS_OSAL_NV_READ, 2, tmp1);
  343. return;
  344. }
  345. #endif //!MT_SYS_KEY_MANAGEMENT
  346. nvItemLen = osal_nv_item_len(nvId);
  347. /* Return only 250 bytes max */
  348. if (nvItemLen > MT_NV_ITEM_MAX_LENGTH)
  349. {
  350. nvItemLen = MT_NV_ITEM_MAX_LENGTH;
  351. }
  352. if ((nvItemLen > 0) && ((nvItemLen - nvItemOffset) > 0))
  353. {
  354. respLen = nvItemLen - nvItemOffset + 2;
  355. }
  356. else
  357. {
  358. respLen = 2;
  359. }
  360. pRetBuf = osal_mem_alloc(respLen);
  361. if (pRetBuf != NULL)
  362. {
  363. osal_memset(pRetBuf, 0, respLen);
  364. /* Default to ZFailure */
  365. pRetBuf[0] = ZFailure;
  366. if (respLen > 2)
  367. {
  368. if (((osal_nv_read( nvId, (uint16)nvItemOffset, (uint16)nvItemLen, &pRetBuf[2])) == ZSUCCESS) && (respLen > 2))
  369. {
  370. pRetBuf[0] = ZSuccess;
  371. }
  372. pRetBuf[1] = nvItemLen - nvItemOffset;
  373. }
  374. else
  375. {
  376. pRetBuf[1] = 0;
  377. }
  378. /* Build and send back the response */
  379. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_SYS),
  380. MT_SYS_OSAL_NV_READ, respLen, pRetBuf );
  381. osal_mem_free(pRetBuf);
  382. }
  383. }
  384. /***************************************************************************************************
  385. * @fn MT_SysOsalNVWrite
  386. *
  387. * @brief
  388. *
  389. * @param uint8 pData - pointer to the data
  390. *
  391. * @return None
  392. ***************************************************************************************************/
  393. void MT_SysOsalNVWrite(uint8 *pBuf)
  394. {
  395. uint16 nvId;
  396. uint8 nvItemLen=0, nvItemOffset=0;
  397. uint8 rtrn;
  398. /* Skip over RPC header */
  399. pBuf += MT_RPC_FRAME_HDR_SZ;
  400. /* Get the ID */
  401. nvId = BUILD_UINT16(pBuf[0], pBuf[1]);
  402. /* Get the offset */
  403. nvItemOffset = pBuf[2];
  404. /* Get the length */
  405. nvItemLen = pBuf[3];
  406. pBuf += 4;
  407. /* Default to ZFailure */
  408. rtrn = ZFailure;
  409. /* Set the Z-Globals value of this NV item. */
  410. zgSetItem( nvId, (uint16)nvItemLen, pBuf );
  411. if ((osal_nv_write(nvId, (uint16)nvItemOffset, (uint16)nvItemLen, pBuf)) == ZSUCCESS)
  412. {
  413. if (nvId == ZCD_NV_EXTADDR)
  414. {
  415. rtrn = ZMacSetReq(ZMacExtAddr, pBuf);
  416. }
  417. else
  418. {
  419. rtrn = ZSuccess;
  420. }
  421. }
  422. /* Build and send back the response */
  423. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_SYS),
  424. MT_SYS_OSAL_NV_WRITE, 1, &rtrn);
  425. }
  426. /***************************************************************************************************
  427. * @fn MT_SysOsalNVItemInit
  428. *
  429. * @brief Attempt to create an NV item
  430. *
  431. * @param uint8 pData - pointer to the data
  432. *
  433. * @return None
  434. ***************************************************************************************************/
  435. void MT_SysOsalNVItemInit(uint8 *pBuf)
  436. {
  437. uint8 ret;
  438. uint8 idLen;
  439. uint16 nvId;
  440. uint16 nvLen;
  441. /* Skip over RPC header */
  442. pBuf += MT_RPC_FRAME_HDR_SZ;
  443. /* NV item ID */
  444. nvId = BUILD_UINT16(pBuf[0], pBuf[1]);
  445. /* NV item length */
  446. nvLen = BUILD_UINT16(pBuf[2], pBuf[3]);
  447. /* Initialization data length */
  448. idLen = pBuf[4];
  449. pBuf += 5;
  450. if ( idLen < nvLen )
  451. {
  452. /* Attempt to create a new NV item */
  453. ret = osal_nv_item_init( nvId, nvLen, NULL );
  454. if ( (ret == NV_ITEM_UNINIT) && (idLen > 0) )
  455. {
  456. /* Write initialization data to first part of new item */
  457. (void) osal_nv_write( nvId, 0, (uint16)idLen, pBuf );
  458. }
  459. }
  460. else
  461. {
  462. /* Attempt to create/initialize a new NV item */
  463. ret = osal_nv_item_init( nvId, nvLen, pBuf );
  464. }
  465. /* Build and send back the response */
  466. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_SYS),
  467. MT_SYS_OSAL_NV_ITEM_INIT, 1, &ret);
  468. }
  469. /***************************************************************************************************
  470. * @fn MT_SysOsalNVDelete
  471. *
  472. * @brief Attempt to delete an NV item
  473. *
  474. * @param uint8 pData - pointer to the data
  475. *
  476. * @return None
  477. ***************************************************************************************************/
  478. void MT_SysOsalNVDelete(uint8 *pBuf)
  479. {
  480. uint16 nvId;
  481. uint16 nvLen;
  482. uint8 ret;
  483. /* Skip over RPC header */
  484. pBuf += MT_RPC_FRAME_HDR_SZ;
  485. /* Get the ID */
  486. nvId = BUILD_UINT16(pBuf[0], pBuf[1]);
  487. /* Get the length */
  488. nvLen = BUILD_UINT16(pBuf[2], pBuf[3]);
  489. /* Attempt to delete the NV item */
  490. ret = osal_nv_delete( nvId, nvLen );
  491. /* Build and send back the response */
  492. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_SYS),
  493. MT_SYS_OSAL_NV_DELETE, 1, &ret);
  494. }
  495. /***************************************************************************************************
  496. * @fn MT_SysOsalNVLength
  497. *
  498. * @brief Attempt to get the length to an NV item
  499. *
  500. * @param uint8 pData - pointer to the data
  501. *
  502. * @return None
  503. ***************************************************************************************************/
  504. void MT_SysOsalNVLength(uint8 *pBuf)
  505. {
  506. uint16 nvId;
  507. uint16 nvLen;
  508. uint8 rsp[2];
  509. /* Skip over RPC header */
  510. pBuf += MT_RPC_FRAME_HDR_SZ;
  511. /* Get the ID */
  512. nvId = BUILD_UINT16(pBuf[0], pBuf[1]);
  513. /* Attempt to get NV item length */
  514. nvLen = osal_nv_item_len( nvId );
  515. rsp[0] = LO_UINT16( nvLen );
  516. rsp[1] = HI_UINT16( nvLen );
  517. /* Build and send back the response */
  518. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_SYS),
  519. MT_SYS_OSAL_NV_LENGTH, 2, rsp);
  520. }
  521. /***************************************************************************************************
  522. * @fn MT_SysOsalStartTimer
  523. *
  524. * @brief
  525. *
  526. * @param uint8 pData - pointer to the data
  527. *
  528. * @return None
  529. ***************************************************************************************************/
  530. void MT_SysOsalStartTimer(uint8 *pBuf)
  531. {
  532. uint16 eventId;
  533. uint8 retValue = ZFailure;
  534. uint8 cmdId;
  535. /* parse header */
  536. cmdId = pBuf[MT_RPC_POS_CMD1];
  537. pBuf += MT_RPC_FRAME_HDR_SZ;
  538. if (*pBuf <= 3)
  539. {
  540. eventId = (uint16) MT_SysOsalEventId[*pBuf];
  541. retValue = osal_start_timerEx(MT_TaskID, eventId, BUILD_UINT16(pBuf[1], pBuf[2]));
  542. }
  543. else
  544. {
  545. retValue = ZInvalidParameter;
  546. }
  547. /* Build and send back the response */
  548. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_SYS), cmdId, 1, &retValue);
  549. }
  550. /***************************************************************************************************
  551. * @fn MT_SysOsalStopTimer
  552. *
  553. * @brief
  554. *
  555. * @param uint8 pData - pointer to the data
  556. *
  557. * @return None
  558. ***************************************************************************************************/
  559. void MT_SysOsalStopTimer(uint8 *pBuf)
  560. {
  561. uint16 eventId;
  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. if (*pBuf <= 3)
  568. {
  569. eventId = (uint16) MT_SysOsalEventId[*pBuf];
  570. retValue = osal_stop_timerEx(MT_TaskID, eventId);
  571. }
  572. else
  573. {
  574. retValue = ZInvalidParameter;
  575. }
  576. /* Build and send back the response */
  577. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_SYS), cmdId, 1, &retValue );
  578. }
  579. /***************************************************************************************************
  580. * @fn MT_SysRandom
  581. *
  582. * @brief
  583. *
  584. * @param uint8 pData - pointer to the data
  585. *
  586. * @return None
  587. ***************************************************************************************************/
  588. void MT_SysRandom()
  589. {
  590. uint16 randValue = Onboard_rand();
  591. uint8 retArray[2];
  592. retArray[0] = LO_UINT16(randValue);
  593. retArray[1] = HI_UINT16(randValue);
  594. /* Build and send back the response */
  595. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_SYS), MT_SYS_RANDOM, 2, retArray );
  596. }
  597. /***************************************************************************************************
  598. * @fn MT_SysAdcRead
  599. *
  600. * @brief Reading ADC value, temperature sensor and voltage
  601. *
  602. * @param uint8 pData - pointer to the data
  603. *
  604. * @return None
  605. ***************************************************************************************************/
  606. void MT_SysAdcRead(uint8 *pBuf)
  607. {
  608. #ifndef HAL_BOARD_LM3S
  609. uint8 channel, resolution;
  610. uint16 tempValue;
  611. uint8 retArray[2];
  612. uint8 cmdId;
  613. /* parse header */
  614. cmdId = pBuf[MT_RPC_POS_CMD1];
  615. pBuf += MT_RPC_FRAME_HDR_SZ;
  616. /* Channel */
  617. channel = *pBuf++;
  618. /* Resolution */
  619. resolution = *pBuf++;
  620. /* Voltage reading */
  621. switch (channel)
  622. {
  623. /* Analog input channel */
  624. case HAL_ADC_CHANNEL_0:
  625. case HAL_ADC_CHANNEL_1:
  626. case HAL_ADC_CHANNEL_2:
  627. case HAL_ADC_CHANNEL_3:
  628. case HAL_ADC_CHANNEL_4:
  629. case HAL_ADC_CHANNEL_5:
  630. case HAL_ADC_CHANNEL_6:
  631. case HAL_ADC_CHANNEL_7:
  632. tempValue = HalAdcRead(channel, resolution);
  633. break;
  634. /* Temperature sensor */
  635. case(HAL_ADC_CHANNEL_TEMP):
  636. tempValue = HalAdcRead(HAL_ADC_CHANNEL_TEMP, HAL_ADC_RESOLUTION_14);
  637. break;
  638. /* Voltage reading */
  639. case(HAL_ADC_CHANNEL_VDD):
  640. tempValue = HalAdcRead(HAL_ADC_CHANNEL_VDD, HAL_ADC_RESOLUTION_14);
  641. break;
  642. /* Undefined channels */
  643. default:
  644. tempValue = 0x00;
  645. break;
  646. }
  647. retArray[0] = LO_UINT16(tempValue);
  648. retArray[1] = HI_UINT16(tempValue);
  649. /* Build and send back the response */
  650. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_SYS), cmdId, 2, retArray);
  651. #endif /* #ifndef HAL_BOARD_LM3S */
  652. }
  653. /**************************************************************************************************
  654. * @fn MT_SysGpio
  655. *
  656. * @brief ZAccel RPC interface for controlling the available GPIO pins.
  657. *
  658. * @param uint8 pData - Pointer to the data.
  659. *
  660. * @return None
  661. *************************************************************************************************/
  662. void MT_SysGpio(uint8 *pBuf)
  663. {
  664. uint8 cmd, val;
  665. GPIO_Op_t op;
  666. cmd = pBuf[MT_RPC_POS_CMD1];
  667. pBuf += MT_RPC_FRAME_HDR_SZ;
  668. op = (GPIO_Op_t)(*pBuf++);
  669. val = *pBuf;
  670. switch (op)
  671. {
  672. case GPIO_DIR:
  673. if (val & BV(0)) {GPIO_DIR_OUT(0);} else {GPIO_DIR_IN(0);}
  674. if (val & BV(1)) {GPIO_DIR_OUT(1);} else {GPIO_DIR_IN(1);}
  675. if (val & BV(2)) {GPIO_DIR_OUT(2);} else {GPIO_DIR_IN(2);}
  676. if (val & BV(3)) {GPIO_DIR_OUT(3);} else {GPIO_DIR_IN(3);}
  677. break;
  678. case GPIO_TRI:
  679. if(val & BV(0)) {GPIO_TRI(0);} else if(val & BV(4)) {GPIO_PULL_DN(0);} else {GPIO_PULL_UP(0);}
  680. if(val & BV(1)) {GPIO_TRI(1);} else if(val & BV(5)) {GPIO_PULL_DN(1);} else {GPIO_PULL_UP(1);}
  681. if(val & BV(2)) {GPIO_TRI(2);} else if(val & BV(6)) {GPIO_PULL_DN(2);} else {GPIO_PULL_UP(2);}
  682. if(val & BV(3)) {GPIO_TRI(3);} else if(val & BV(7)) {GPIO_PULL_DN(3);} else {GPIO_PULL_UP(3);}
  683. break;
  684. case GPIO_SET:
  685. if (val & BV(0)) {GPIO_SET(0);}
  686. if (val & BV(1)) {GPIO_SET(1);}
  687. if (val & BV(2)) {GPIO_SET(2);}
  688. if (val & BV(3)) {GPIO_SET(3);}
  689. break;
  690. case GPIO_CLR:
  691. if (val & BV(0)) {GPIO_CLR(0);}
  692. if (val & BV(1)) {GPIO_CLR(1);}
  693. if (val & BV(2)) {GPIO_CLR(2);}
  694. if (val & BV(3)) {GPIO_CLR(3);}
  695. break;
  696. case GPIO_TOG:
  697. if (val & BV(0)) {GPIO_TOG(0);}
  698. if (val & BV(1)) {GPIO_TOG(1);}
  699. if (val & BV(2)) {GPIO_TOG(2);}
  700. if (val & BV(3)) {GPIO_TOG(3);}
  701. break;
  702. case GPIO_GET:
  703. break;
  704. case GPIO_HiD:
  705. (val) ? GPIO_HiD_SET() : GPIO_HiD_CLR();
  706. break;
  707. default:
  708. break;
  709. }
  710. val = (GPIO_GET(0)) ? BV(0) : 0;
  711. val |= (GPIO_GET(1)) ? BV(1) : 0;
  712. val |= (GPIO_GET(2)) ? BV(2) : 0;
  713. val |= (GPIO_GET(3)) ? BV(3) : 0;
  714. /* Build and send back the response */
  715. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_SYS), cmd, 1, &val);
  716. }
  717. /**************************************************************************************************
  718. * @fn MT_SysStackTune
  719. *
  720. * @brief ZAccel RPC interface for tuning the stack parameters to adjust performance
  721. *
  722. * @param uint8 pData - Pointer to the data.
  723. *
  724. * @return None
  725. *************************************************************************************************/
  726. void MT_SysStackTune(uint8 *pBuf)
  727. {
  728. uint8 cmd, rtrn;
  729. cmd = pBuf[MT_RPC_POS_CMD1];
  730. pBuf += MT_RPC_FRAME_HDR_SZ;
  731. switch (*pBuf++)
  732. {
  733. case STK_TX_PWR:
  734. rtrn = ZMacSetReq(ZMacPhyTransmitPowerSigned, pBuf);
  735. break;
  736. case STK_RX_ON_IDLE:
  737. if ((*pBuf != TRUE) && (*pBuf != FALSE))
  738. {
  739. (void)ZMacGetReq(ZMacRxOnIdle, &rtrn);
  740. }
  741. else
  742. {
  743. rtrn = ZMacSetReq(ZMacRxOnIdle, pBuf);
  744. }
  745. break;
  746. default:
  747. rtrn = ZInvalidParameter;
  748. break;
  749. }
  750. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_SYS), cmd, 1, &rtrn);
  751. }
  752. /***************************************************************************************************
  753. * @fn MT_SysSetUtcTime
  754. *
  755. * @brief Set the OSAL UTC Time. UTC rollover is: 06:28:16 02/07/2136
  756. *
  757. * @param pBuf - pointer to time parameters
  758. *
  759. * @return None
  760. ***************************************************************************************************/
  761. void MT_SysSetUtcTime(uint8 *pBuf)
  762. {
  763. uint8 cmdId;
  764. uint8 retStat;
  765. UTCTime utcSecs;
  766. /* Parse header */
  767. cmdId = pBuf[MT_RPC_POS_CMD1];
  768. pBuf += MT_RPC_FRAME_HDR_SZ;
  769. utcSecs = osal_build_uint32( pBuf, 4 );
  770. if ( utcSecs == 0 )
  771. {
  772. UTCTimeStruct utc;
  773. /* Skip past UTC time */
  774. pBuf += 4;
  775. /* Get time and date parameters */
  776. utc.hour = *pBuf++;
  777. utc.minutes = *pBuf++;
  778. utc.seconds = *pBuf++;
  779. utc.month = (*pBuf++) - 1;
  780. utc.day = (*pBuf++) - 1;
  781. utc.year = osal_build_uint16 ( pBuf );
  782. if ((utc.hour < 24) && (utc.minutes < 60) && (utc.seconds < 60) &&
  783. (utc.month < 12) && (utc.day < 31) && (utc.year > 1999) && (utc.year < 2136))
  784. {
  785. /* Got past the course filter, now check for leap year */
  786. if ((utc.month != 1) || (utc.day < (IsLeapYear( utc.year ) ? 29 : 28)))
  787. {
  788. /* Numbers look reasonable, convert to UTC */
  789. utcSecs = osal_ConvertUTCSecs( &utc );
  790. }
  791. }
  792. }
  793. if ( utcSecs == 0 )
  794. {
  795. /* Bad parameter(s) */
  796. retStat = ZInvalidParameter;
  797. }
  798. else
  799. {
  800. /* Parameters accepted, set the time */
  801. osal_setClock( utcSecs );
  802. retStat = ZSuccess;
  803. }
  804. /* Build and send back the response */
  805. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_SYS),
  806. cmdId, 1, &retStat);
  807. }
  808. /***************************************************************************************************
  809. * @fn MT_SysGetUtcTime
  810. *
  811. * @brief Get the OSAL UTC time
  812. *
  813. * @param None
  814. *
  815. * @return 32-bit and Parsed UTC time
  816. ***************************************************************************************************/
  817. void MT_SysGetUtcTime(void)
  818. {
  819. uint8 len;
  820. uint8 *buf;
  821. len = sizeof( UTCTime ) + sizeof( UTCTimeStruct );
  822. buf = osal_mem_alloc( len );
  823. if ( buf )
  824. {
  825. uint8 *pBuf;
  826. UTCTime utcSecs;
  827. UTCTimeStruct utcTime;
  828. // Get current 32-bit UTC time and parse it
  829. utcSecs = osal_getClock();
  830. osal_ConvertUTCTime( &utcTime, utcSecs );
  831. // Start with 32-bit UTC time
  832. pBuf = osal_buffer_uint32( buf, utcSecs );
  833. // Concatenate parsed UTC time fields
  834. *pBuf++ = utcTime.hour;
  835. *pBuf++ = utcTime.minutes;
  836. *pBuf++ = utcTime.seconds;
  837. *pBuf++ = utcTime.month + 1; // Convert to human numbers
  838. *pBuf++ = utcTime.day + 1;
  839. *pBuf++ = LO_UINT16( utcTime.year );
  840. *pBuf++ = HI_UINT16( utcTime.year );
  841. /* Build and send back the response */
  842. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_SYS),
  843. MT_SYS_GET_TIME, (uint8)(pBuf-buf), buf);
  844. osal_mem_free( buf );
  845. }
  846. }
  847. /***************************************************************************************************
  848. * @fn MT_SysSetTxPower
  849. *
  850. * @brief Set the transmit power.
  851. *
  852. * @param pBuf - MT message containing the ZMacTransmitPower_t power level to set.
  853. *
  854. * @return None
  855. ***************************************************************************************************/
  856. void MT_SysSetTxPower(uint8 *pBuf)
  857. {
  858. /* A local variable to hold the signed dBm value of TxPower that is being requested. */
  859. uint8 signed_dBm_of_TxPower_requeseted;
  860. /*
  861. * A local variable to hold the signed dBm value of TxPower that can be set which is closest to
  862. * the requested dBm value of TxPower, but which is also valid according to a complex set of
  863. * compile-time and run-time configuration which is interpreted by the macRadioSetTxPower()
  864. * function.
  865. */
  866. uint8 signed_dBm_of_TxPower_range_corrected;
  867. /* Parse the requested dBm from the RPC message. */
  868. signed_dBm_of_TxPower_requeseted = pBuf[MT_RPC_POS_DAT0];
  869. /*
  870. * MAC_MlmeSetReq() will store an out-of-range dBm parameter value into the NIB. So it is not
  871. * possible to learn the actual dBm value that will be set by invoking MACMlmeGetReq().
  872. * But this actual dBm value is a required return value in the SRSP to this SREQ. Therefore,
  873. * it is necessary to make this redundant pre-call to macRadioSetTxPower() here in order to run
  874. * the code that will properly constrain the requested dBm to a valid range based on both the
  875. * compile-time and the run-time configurations that affect the available valid ranges
  876. * (i.e. MAC_MlmeSetReq() itself will invoke for a second time the macRadioSetTxPower() function).
  877. */
  878. signed_dBm_of_TxPower_range_corrected = macRadioSetTxPower(signed_dBm_of_TxPower_requeseted);
  879. /*
  880. * Call the function to store the requested dBm in the MAC PIB and to set the TxPower as closely
  881. * as possible within the TxPower range that is valid for the compile-time and run-time
  882. * configuration.
  883. */
  884. (void)MAC_MlmeSetReq(MAC_PHY_TRANSMIT_POWER_SIGNED, &signed_dBm_of_TxPower_requeseted);
  885. /* Build and send back the response that includes the actual dBm TxPower that can be set. */
  886. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_SYS),
  887. MT_SYS_SET_TX_POWER, 1,
  888. &signed_dBm_of_TxPower_range_corrected);
  889. }
  890. #endif /* MT_SYS_FUNC */
  891. /***************************************************************************************************
  892. * SUPPORT
  893. ***************************************************************************************************/
  894. /***************************************************************************************************
  895. * @fn MT_SysResetInd()
  896. *
  897. * @brief Sends a ZTOOL "reset response" message.
  898. *
  899. * @param None
  900. *
  901. * @return None
  902. *
  903. ***************************************************************************************************/
  904. void MT_SysResetInd(void)
  905. {
  906. uint8 retArray[6];
  907. retArray[0] = ResetReason(); /* Reason */
  908. osal_memcpy( &retArray[1], MTVersionString, 5 ); /* Revision info */
  909. /* Send out Reset Response message */
  910. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_AREQ | (uint8)MT_RPC_SYS_SYS), MT_SYS_RESET_IND,
  911. sizeof(retArray), retArray);
  912. }
  913. /***************************************************************************************************
  914. * @fn MT_SysOsalTimerExpired()
  915. *
  916. * @brief Sends a SYS Osal Timer Expired
  917. *
  918. * @param None
  919. *
  920. * @return None
  921. *
  922. ***************************************************************************************************/
  923. void MT_SysOsalTimerExpired(uint8 Id)
  924. {
  925. uint8 retValue;
  926. retValue = Id;
  927. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_AREQ | (uint8)MT_RPC_SYS_SYS), MT_SYS_OSAL_TIMER_EXPIRED, 1, &retValue);
  928. }
  929. /***************************************************************************************************
  930. ***************************************************************************************************/