MT.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /***************************************************************************************************
  2. Filename: MT.c
  3. Revised: $Date: 2011-06-07 12:12:44 -0700 (Tue, 07 Jun 2011) $
  4. Revision: $Revision: 26238 $
  5. Description: MonitorTest Event Loop functions.
  6. Everything in the MonitorTest Task (except the serial driver).
  7. Copyright 2007-2011 Texas Instruments Incorporated. All rights reserved.
  8. IMPORTANT: Your use of this Software is limited to those specific rights
  9. granted under the terms of a software license agreement between the user
  10. who downloaded the software, his/her employer (which must be your employer)
  11. and Texas Instruments Incorporated (the "License"). You may not use this
  12. Software unless you agree to abide by the terms of the License. The License
  13. limits your use, and you acknowledge, that the Software may not be modified,
  14. copied or distributed unless embedded on a Texas Instruments microcontroller
  15. or used solely and exclusively in conjunction with a Texas Instruments radio
  16. frequency transceiver, which is integrated into your product. Other than for
  17. the foregoing purpose, you may not use, reproduce, copy, prepare derivative
  18. works of, modify, distribute, perform, display or sell this Software and/or
  19. its documentation for any purpose.
  20. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  21. PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  22. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  23. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  24. TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
  25. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  26. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  27. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  28. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  29. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  30. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  31. Should you have any questions regarding your right to use this Software,
  32. contact Texas Instruments Incorporated at www.TI.com.
  33. **************************************************************************************************/
  34. /**************************************************************************************************
  35. * INCLUDES
  36. **************************************************************************************************/
  37. #include "ZComDef.h"
  38. #include "MT.h"
  39. #include "MT_APP.h"
  40. #include "MT_DEBUG.h"
  41. #include "MT_UTIL.h"
  42. #include "MT_SYS.h"
  43. #include "OnBoard.h"
  44. #include "OSAL.h"
  45. #include "OSAL_Memory.h"
  46. #include "OSAL_Nv.h"
  47. #include "DebugTrace.h"
  48. #include "ZMAC.h"
  49. #if !defined ( NONWK )
  50. #include "NLMEDE.h"
  51. #include "nwk_bufs.h"
  52. #include "ZDObject.h"
  53. #include "ssp.h"
  54. #include "nwk_util.h"
  55. #include "AF.h"
  56. #include "MT_SAPI.h"
  57. #endif
  58. #if defined( MT_MAC_FUNC ) || defined( MT_MAC_CB_FUNC )
  59. #include "MT_MAC.h"
  60. #endif
  61. #if defined( MT_NWK_FUNC ) || defined( MT_NWK_CB_FUNC )
  62. #include "MT_NWK.h"
  63. #include "nwk.h"
  64. #include "nwk_bufs.h"
  65. #endif
  66. #if defined( MT_AF_FUNC ) || defined( MT_AF_CB_FUNC )
  67. #include "MT_AF.h"
  68. #endif
  69. #if defined( MT_USER_TEST_FUNC )
  70. #include "AF.h"
  71. #endif
  72. #if defined( MT_ZDO_FUNC )
  73. #include "MT_ZDO.h"
  74. #endif
  75. #if defined (MT_SAPI_FUNC)
  76. #include "MT_SAPI.h"
  77. #endif
  78. #if defined (MT_OTA_FUNC)
  79. #include "MT_OTA.h"
  80. #endif
  81. #if defined( APP_TP )
  82. #include "TestProfile.h"
  83. #endif
  84. #if defined( APP_TP2 )
  85. #include "TestProfile2.h"
  86. #endif
  87. #if defined(APP_TGEN)
  88. #include "TrafficGenApp.h"
  89. #endif
  90. #if defined(APP_DEBUG)
  91. #include "DebugApp.h"
  92. #endif
  93. #if defined (NWK_TEST)
  94. #include "HWTTApp.h"
  95. #endif
  96. /* HAL */
  97. #include "hal_uart.h"
  98. #include "hal_led.h"
  99. #include "hal_key.h"
  100. #include "MT_UART.h"
  101. /**************************************************************************************************
  102. * CONSTANTS
  103. **************************************************************************************************/
  104. mtProcessMsg_t mtProcessIncoming[] =
  105. {
  106. NULL,
  107. #if defined (MT_SYS_FUNC)
  108. MT_SysCommandProcessing,
  109. #else
  110. NULL,
  111. #endif
  112. #if defined (MT_MAC_FUNC)
  113. MT_MacCommandProcessing,
  114. #else
  115. NULL,
  116. #endif
  117. #if defined (MT_NWK_FUNC)
  118. MT_NwkCommandProcessing,
  119. #else
  120. NULL,
  121. #endif
  122. #if defined (MT_AF_FUNC)
  123. MT_AfCommandProcessing,
  124. #else
  125. NULL,
  126. #endif
  127. #if defined (MT_ZDO_FUNC)
  128. MT_ZdoCommandProcessing,
  129. #else
  130. NULL,
  131. #endif
  132. #if defined (MT_SAPI_FUNC)
  133. MT_SapiCommandProcessing,
  134. #else
  135. NULL,
  136. #endif
  137. #if defined (MT_UTIL_FUNC)
  138. MT_UtilCommandProcessing,
  139. #else
  140. NULL,
  141. #endif
  142. #if defined (MT_DEBUG_FUNC)
  143. MT_DebugCommandProcessing,
  144. #else
  145. NULL,
  146. #endif
  147. #if defined (MT_APP_FUNC)
  148. MT_AppCommandProcessing,
  149. #else
  150. NULL,
  151. #endif
  152. #if defined (MT_OTA_FUNC)
  153. MT_OtaCommandProcessing,
  154. #else
  155. NULL,
  156. #endif
  157. };
  158. /**************************************************************************************************
  159. * GLOBAL VARIABLES
  160. **************************************************************************************************/
  161. byte debugThreshold;
  162. byte debugCompId;
  163. /**************************************************************************************************
  164. * LOCAL FUNCTIONS
  165. **************************************************************************************************/
  166. void MT_MsgQueueInit( void );
  167. void MT_ResetMsgQueue( void );
  168. byte MT_QueueMsg( byte *msg , byte len );
  169. void MT_ProcessQueue( void );
  170. #if defined ( MT_USER_TEST_FUNC )
  171. void MT_ProcessAppUserCmd( byte *pData );
  172. #endif
  173. /**************************************************************************************************
  174. * @fn MT_Init
  175. *
  176. * @brief This function is the secondary initialization that resolves conflicts during
  177. * osalInitTasks(). For example, since MT is the highest priority task, and
  178. * specifically because the MT task is initialized before the ZDApp task, if MT_Init()
  179. * registers anything with ZDO_RegisterForZdoCB(), it is wiped out when ZDApp task
  180. * initialization invokes ZDApp_InitZdoCBFunc().
  181. * There may be other existing or future such races, so try to do all possible
  182. * MT initialization here vice in MT_TaskInit().
  183. *
  184. * input parameters
  185. *
  186. * None.
  187. *
  188. * output parameters
  189. *
  190. * None.
  191. *
  192. * @return None.
  193. **************************************************************************************************
  194. */
  195. void MT_Init(void)
  196. {
  197. #if defined (MT_ZDO_FUNC)
  198. MT_ZdoInit();
  199. #endif
  200. MT_SysResetInd();
  201. }
  202. /***************************************************************************************************
  203. * @fn MT_BuildSPIMsg
  204. *
  205. * @brief
  206. *
  207. * Format an SPI message.
  208. *
  209. * @param UINT16 cmd - command id
  210. * @param byte *msg - pointer to message buffer
  211. * @param byte dataLen - length of data field
  212. * @param byte *pData - pointer to data field
  213. *
  214. * @return void
  215. ***************************************************************************************************/
  216. void MT_BuildSPIMsg( uint8 cmdType, uint8 cmdId, byte *msg, byte dataLen, byte *pData )
  217. {
  218. /* Fill in the CMD and Data Length */
  219. *msg++ = dataLen;
  220. *msg++ = cmdType;
  221. *msg++ = cmdId;
  222. /* Fill in the data */
  223. if ( pData )
  224. {
  225. osal_memcpy( msg, pData, dataLen );
  226. }
  227. }
  228. /***************************************************************************************************
  229. * @fn MT_BuildAndSendZToolResponse
  230. *
  231. * @brief Build and send a ZTOOL msg
  232. * @param uint8 cmdType - include type and subsystem
  233. * uint8 cmdId - command ID
  234. * byte dataLen
  235. * byte *pData
  236. *
  237. * @return void
  238. ***************************************************************************************************/
  239. void MT_BuildAndSendZToolResponse(uint8 cmdType, uint8 cmdId, uint8 dataLen, uint8 *pData)
  240. {
  241. uint8 *msg_ptr;
  242. /* Allocate memory including SOP and FCS */
  243. msg_ptr = MT_TransportAlloc((mtRpcCmdType_t)(cmdType & 0xE0), dataLen);
  244. if (msg_ptr)
  245. {
  246. /* Build the message */
  247. MT_BuildSPIMsg(cmdType, cmdId, msg_ptr, dataLen, pData);
  248. /* Send out the msg */
  249. MT_TransportSend(msg_ptr);
  250. }
  251. }
  252. /***************************************************************************************************
  253. * @fn MT_ProcessIncoming
  254. *
  255. * @brief Process Incoming Message.
  256. *
  257. * @param byte *pBuf - pointer to event message
  258. *
  259. * @return void
  260. ***************************************************************************************************/
  261. void MT_ProcessIncoming(uint8 *pBuf)
  262. {
  263. mtProcessMsg_t func;
  264. uint8 rsp[MT_RPC_FRAME_HDR_SZ];
  265. /* pre-build response message: | status | cmd0 | cmd1 | */
  266. rsp[1] = pBuf[MT_RPC_POS_CMD0];
  267. rsp[2] = pBuf[MT_RPC_POS_CMD1];
  268. /* check length */
  269. if (pBuf[MT_RPC_POS_LEN] > MT_RPC_DATA_MAX)
  270. {
  271. rsp[0] = MT_RPC_ERR_LENGTH;
  272. }
  273. /* check subsystem range */
  274. else if ((rsp[1] & MT_RPC_SUBSYSTEM_MASK) < MT_RPC_SYS_MAX)
  275. {
  276. /* look up processing function */
  277. func = mtProcessIncoming[rsp[1] & MT_RPC_SUBSYSTEM_MASK];
  278. if (func)
  279. {
  280. /* execute processing function */
  281. rsp[0] = (*func)(pBuf);
  282. }
  283. else
  284. {
  285. rsp[0] = MT_RPC_ERR_SUBSYSTEM;
  286. }
  287. }
  288. else
  289. {
  290. rsp[0] = MT_RPC_ERR_SUBSYSTEM;
  291. }
  292. /* if error and this was an SREQ, send error message */
  293. if ((rsp[0] != MT_RPC_SUCCESS) && ((rsp[1] & MT_RPC_CMD_TYPE_MASK) == MT_RPC_CMD_SREQ))
  294. {
  295. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_RES0), 0,
  296. MT_RPC_FRAME_HDR_SZ, rsp);
  297. }
  298. }
  299. /***************************************************************************************************
  300. * @fn MTProcessAppRspMsg
  301. *
  302. * @brief Process the User App Response Message
  303. *
  304. * @param data - output serial buffer. The first byte must be the
  305. * endpoint that send this message.
  306. * @param len - data length
  307. *
  308. * @return none
  309. ***************************************************************************************************/
  310. void MTProcessAppRspMsg( byte *pData, byte len )
  311. {
  312. /* Send out Reset Response message */
  313. MT_BuildAndSendZToolResponse( ((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_APP), MT_APP_RSP, len, pData );
  314. }
  315. /***************************************************************************************************
  316. * @fn MT_ReverseBytes
  317. *
  318. * @brief
  319. *
  320. * Reverses bytes within an array
  321. *
  322. * @param data - ptr to data buffer to reverse
  323. * @param len - number of bytes in buffer
  324. *
  325. * @return void
  326. ***************************************************************************************************/
  327. void MT_ReverseBytes( byte *pData, byte len )
  328. {
  329. byte i,j;
  330. byte temp;
  331. for ( i = 0, j = len-1; len > 1; len-=2 )
  332. {
  333. temp = pData[i];
  334. pData[i++] = pData[j];
  335. pData[j--] = temp;
  336. }
  337. }
  338. /***************************************************************************************************
  339. * @fn MT_Word2Buf
  340. *
  341. * @brief Copy a uint16 array to a byte array, little endian.
  342. *
  343. * @param pBuf - byte array
  344. * @param pWord - uint16 array
  345. * @param len - length of uint16 array
  346. *
  347. * @return pointer to end of byte array
  348. ***************************************************************************************************/
  349. uint8 *MT_Word2Buf( uint8 *pBuf, uint16 *pWord, uint8 len )
  350. {
  351. while ( len-- > 0 )
  352. {
  353. *pBuf++ = LO_UINT16( *pWord );
  354. *pBuf++ = HI_UINT16( *pWord );
  355. pWord++;
  356. }
  357. return pBuf;
  358. }
  359. #if !defined(NONWK)
  360. /***************************************************************************************************
  361. * @fn MT_BuildEndpointDesc
  362. *
  363. * @brief Build endpoint descriptor and simple descriptor structure from incoming buffer
  364. *
  365. * @param pBuf - byte array
  366. *
  367. * @return epDesc - pointer to the endpoint descriptor
  368. ***************************************************************************************************/
  369. uint8 MT_BuildEndpointDesc( uint8 *pBuf, void *param )
  370. {
  371. uint8 i;
  372. uint8 ret = ZSuccess;
  373. endPointDesc_t *epDesc;
  374. epDesc = (endPointDesc_t *)param;
  375. /* check if this endpoint is already registered */
  376. if ( afFindEndPointDesc( *pBuf ) != NULL )
  377. {
  378. ret = ZApsDuplicateEntry;
  379. }
  380. else if ( epDesc )
  381. {
  382. epDesc->endPoint = *pBuf;
  383. /* Ignore the latency reqs */
  384. epDesc->latencyReq = noLatencyReqs;
  385. /* allocate memory for the simple descriptor */
  386. epDesc->simpleDesc = (SimpleDescriptionFormat_t *) osal_mem_alloc(sizeof(SimpleDescriptionFormat_t));
  387. if (epDesc->simpleDesc)
  388. {
  389. /* Endpoint */
  390. epDesc->simpleDesc->EndPoint = *pBuf++;
  391. /* AppProfId */
  392. epDesc->simpleDesc->AppProfId = BUILD_UINT16(pBuf[0], pBuf[1]);
  393. pBuf += sizeof(uint16);
  394. /* AppDeviceId */
  395. epDesc->simpleDesc->AppDeviceId = BUILD_UINT16(pBuf[0],pBuf[1]);
  396. pBuf += sizeof(uint16);
  397. /* AppDevVer */
  398. epDesc->simpleDesc->AppDevVer = (*pBuf++) & AF_APP_DEV_VER_MASK ;
  399. /* LatencyReq */
  400. pBuf++;
  401. /* AppNumInClusters */
  402. epDesc->simpleDesc->AppNumInClusters = *pBuf++;
  403. if (epDesc->simpleDesc->AppNumInClusters)
  404. {
  405. epDesc->simpleDesc->pAppInClusterList = (uint16 *)
  406. osal_mem_alloc((epDesc->simpleDesc->AppNumInClusters)*sizeof(uint16));
  407. if ( epDesc->simpleDesc->pAppInClusterList )
  408. {
  409. for (i=0; i<(epDesc->simpleDesc->AppNumInClusters); i++)
  410. {
  411. epDesc->simpleDesc->pAppInClusterList[i] = BUILD_UINT16(*pBuf, *(pBuf+1));
  412. pBuf += 2;
  413. }
  414. }
  415. else
  416. {
  417. ret = ZMemError;
  418. }
  419. }
  420. /* AppNumOutClusters */
  421. epDesc->simpleDesc->AppNumOutClusters = *pBuf++;
  422. if (epDesc->simpleDesc->AppNumOutClusters)
  423. {
  424. epDesc->simpleDesc->pAppOutClusterList = (uint16 *)
  425. osal_mem_alloc((epDesc->simpleDesc->AppNumOutClusters)*sizeof(uint16));
  426. if (epDesc->simpleDesc->pAppOutClusterList)
  427. {
  428. for (i=0; i<(epDesc->simpleDesc->AppNumOutClusters); i++)
  429. {
  430. epDesc->simpleDesc->pAppOutClusterList[i] = BUILD_UINT16(*pBuf, *(pBuf+1));
  431. pBuf += 2;
  432. }
  433. }
  434. else
  435. {
  436. ret = ZMemError;
  437. }
  438. }
  439. /* if any list cannot be allocated...free all */
  440. if ( ret == ZMemError )
  441. {
  442. if (epDesc->simpleDesc->pAppInClusterList)
  443. {
  444. osal_mem_free(epDesc->simpleDesc->pAppInClusterList);
  445. }
  446. if (epDesc->simpleDesc->AppNumOutClusters)
  447. {
  448. osal_mem_free(epDesc->simpleDesc->pAppOutClusterList);
  449. }
  450. osal_mem_free(epDesc->simpleDesc);
  451. }
  452. }
  453. else
  454. {
  455. ret = ZMemError;
  456. }
  457. }
  458. return ret;
  459. }
  460. #endif
  461. /***************************************************************************************************
  462. ***************************************************************************************************/