MT.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /***************************************************************************************************
  2. Filename: MTEL.c
  3. Revised: $Date: 2008-10-10 09:56:24 -0700 (Fri, 10 Oct 2008) $
  4. Revision: $Revision: 18257 $
  5. Description: MonitorTest Event Loop functions. Everything in the
  6. MonitorTest Task (except the serial driver).
  7. Copyright 2007 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 "MT_SAPI.h"
  44. #include "AF.h"
  45. #include "OnBoard.h"
  46. #include "OSAL.h"
  47. #include "OSAL_Memory.h"
  48. #include "OSAL_Nv.h"
  49. #include "DebugTrace.h"
  50. #include "ZMAC.h"
  51. #if !defined ( NONWK )
  52. #include "NLMEDE.h"
  53. #include "nwk_bufs.h"
  54. #include "ZDObject.h"
  55. #include "ssp.h"
  56. #include "nwk_util.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( APP_TP )
  79. #include "TestProfile.h"
  80. #endif
  81. #if defined( APP_TP2 )
  82. #include "TestProfile2.h"
  83. #endif
  84. #if defined(APP_TGEN)
  85. #include "TrafficGenApp.h"
  86. #endif
  87. #if defined(APP_DEBUG)
  88. #include "DebugApp.h"
  89. #endif
  90. #if defined (NWK_TEST)
  91. #include "HWTTApp.h"
  92. #endif
  93. /* HAL */
  94. #include "hal_uart.h"
  95. #include "hal_led.h"
  96. #include "hal_key.h"
  97. #include "MT_UART.h"
  98. /***************************************************************************************************
  99. * MACROS
  100. ***************************************************************************************************/
  101. #define MTEL_DEBUG_INFO( nParams, p1, p2, p3 ) DEBUG_INFO( COMPID_MTEL, nParams, p1, p2, p3 )
  102. #define MT_ERROR_SRSP_LEN 3
  103. /***************************************************************************************************
  104. * CONSTANTS
  105. ***************************************************************************************************/
  106. mtProcessMsg_t mtProcessIncoming[] =
  107. {
  108. NULL,
  109. #if defined (MT_SYS_FUNC)
  110. MT_SysCommandProcessing,
  111. #else
  112. NULL,
  113. #endif
  114. #if defined (MT_MAC_FUNC)
  115. MT_MacCommandProcessing,
  116. #else
  117. NULL,
  118. #endif
  119. #if defined (MT_NWK_FUNC)
  120. MT_NwkCommandProcessing,
  121. #else
  122. NULL,
  123. #endif
  124. #if defined (MT_AF_FUNC)
  125. MT_AfCommandProcessing,
  126. #else
  127. NULL,
  128. #endif
  129. #if defined (MT_ZDO_FUNC)
  130. MT_ZdoCommandProcessing,
  131. #else
  132. NULL,
  133. #endif
  134. #if defined (MT_SAPI_FUNC)
  135. MT_SapiCommandProcessing,
  136. #else
  137. NULL,
  138. #endif
  139. #if defined (MT_UTIL_FUNC)
  140. MT_UtilCommandProcessing,
  141. #else
  142. NULL,
  143. #endif
  144. #if defined (MT_DEBUG_FUNC)
  145. MT_DebugCommandProcessing,
  146. #else
  147. NULL,
  148. #endif
  149. #if defined (MT_APP_FUNC)
  150. MT_AppCommandProcessing,
  151. #else
  152. NULL,
  153. #endif
  154. };
  155. /***************************************************************************************************
  156. * TYPEDEFS
  157. ***************************************************************************************************/
  158. /***************************************************************************************************
  159. * GLOBAL VARIABLES
  160. ***************************************************************************************************/
  161. UINT16 save_cmd;
  162. byte MT_TaskID;
  163. byte debugThreshold;
  164. byte debugCompId;
  165. /***************************************************************************************************
  166. * EXTERNAL FUNCTIONS
  167. ***************************************************************************************************/
  168. extern unsigned int mac_sim_eventLoop( void );
  169. #ifdef MACSIM
  170. extern void MACSIM_TranslateMsg( byte *buf, byte bLen ); /* Used to pass Zignet message */
  171. #endif
  172. /***************************************************************************************************
  173. * LOCAL VARIABLES
  174. ***************************************************************************************************/
  175. /***************************************************************************************************
  176. * LOCAL FUNCTIONS
  177. ***************************************************************************************************/
  178. void MT_MsgQueueInit( void );
  179. void MT_ResetMsgQueue( void );
  180. byte MT_QueueMsg( byte *msg , byte len );
  181. void MT_ProcessQueue( void );
  182. #if defined ( MT_USER_TEST_FUNC )
  183. void MT_ProcessAppUserCmd( byte *pData );
  184. #endif
  185. /***************************************************************************************************
  186. * @fn MT_Init()
  187. *
  188. * @brief Initialize MT.
  189. *
  190. * @param uint8 taskId - taskId
  191. *
  192. * @return void
  193. ***************************************************************************************************/
  194. void MT_Init(uint8 taskID)
  195. {
  196. MT_TaskID = taskID;
  197. debugThreshold = 0;
  198. debugCompId = 0;
  199. }
  200. /***************************************************************************************************
  201. * @fn MT_BuildSPIMsg
  202. *
  203. * @brief
  204. *
  205. * Format an SPI message.
  206. *
  207. * @param UINT16 cmd - command id
  208. * @param byte *msg - pointer to message buffer
  209. * @param byte dataLen - length of data field
  210. * @param byte *pData - pointer to data field
  211. *
  212. * @return void
  213. ***************************************************************************************************/
  214. void MT_BuildSPIMsg( uint8 cmdType, uint8 cmdId, byte *msg, byte dataLen, byte *pData )
  215. {
  216. /* Fill in the CMD and Data Length */
  217. *msg++ = dataLen;
  218. *msg++ = cmdType;
  219. *msg++ = cmdId;
  220. /* Fill in the data */
  221. if ( pData )
  222. {
  223. osal_memcpy( msg, pData, dataLen );
  224. }
  225. }
  226. /***************************************************************************************************
  227. * @fn MT_BuildAndSendZToolResponse
  228. *
  229. * @brief Build and send a ZTOOL msg
  230. * @param uint8 cmdType - include type and subsystem
  231. * uint8 cmdId - command ID
  232. * byte dataLen
  233. * byte *pData
  234. *
  235. * @return void
  236. ***************************************************************************************************/
  237. void MT_BuildAndSendZToolResponse(uint8 cmdType, uint8 cmdId, uint8 dataLen, uint8 *pData)
  238. {
  239. uint8 *msg_ptr;
  240. /* Allocate memory including SOP and FCS */
  241. msg_ptr = MT_TransportAlloc((mtRpcCmdType_t)(cmdType & 0xE0), dataLen);
  242. if (msg_ptr)
  243. {
  244. /* Build the message */
  245. MT_BuildSPIMsg(cmdType, cmdId, msg_ptr, dataLen, pData);
  246. /* Send out the msg */
  247. MT_TransportSend(msg_ptr);
  248. }
  249. }
  250. /***************************************************************************************************
  251. * @fn MT_ProcessIncoming
  252. *
  253. * @brief Process Incoming Message.
  254. *
  255. * @param byte *pBuf - pointer to event message
  256. *
  257. * @return void
  258. ***************************************************************************************************/
  259. void MT_ProcessIncoming(uint8 *pBuf)
  260. {
  261. mtProcessMsg_t func;
  262. uint8 rsp[MT_ERROR_SRSP_LEN];
  263. /* pre-build response message: | status | cmd0 | cmd1 | */
  264. rsp[1] = pBuf[MT_RPC_POS_CMD0];
  265. rsp[2] = pBuf[MT_RPC_POS_CMD1];
  266. /* check length */
  267. if (pBuf[MT_RPC_POS_LEN] > MT_RPC_DATA_MAX)
  268. {
  269. rsp[0] = MT_RPC_ERR_LENGTH;
  270. }
  271. /* check subsystem range */
  272. else if ((rsp[1] & MT_RPC_SUBSYSTEM_MASK) < MT_RPC_SYS_MAX)
  273. {
  274. /* look up processing function */
  275. func = mtProcessIncoming[rsp[1] & MT_RPC_SUBSYSTEM_MASK];
  276. if (func)
  277. {
  278. /* execute processing function */
  279. rsp[0] = (*func)(pBuf);
  280. }
  281. else
  282. {
  283. rsp[0] = MT_RPC_ERR_SUBSYSTEM;
  284. }
  285. }
  286. else
  287. {
  288. rsp[0] = MT_RPC_ERR_SUBSYSTEM;
  289. }
  290. /* if error and this was an SREQ, send error message */
  291. if ((rsp[0] != MT_RPC_SUCCESS) && ((rsp[1] & MT_RPC_CMD_TYPE_MASK) == MT_RPC_CMD_SREQ))
  292. {
  293. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_RES0), 0, MT_ERROR_SRSP_LEN, rsp);
  294. }
  295. }
  296. /***************************************************************************************************
  297. * @fn MTProcessAppRspMsg
  298. *
  299. * @brief Process the User App Response Message
  300. *
  301. * @param data - output serial buffer. The first byte must be the
  302. * endpoint that send this message.
  303. * @param len - data length
  304. *
  305. * @return none
  306. ***************************************************************************************************/
  307. void MTProcessAppRspMsg( byte *pData, byte len )
  308. {
  309. /* Send out Reset Response message */
  310. MT_BuildAndSendZToolResponse( ((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_APP), MT_APP_RSP, len, pData );
  311. }
  312. /***************************************************************************************************
  313. * @fn MT_ReverseBytes
  314. *
  315. * @brief
  316. *
  317. * Reverses bytes within an array
  318. *
  319. * @param data - ptr to data buffer to reverse
  320. * @param len - number of bytes in buffer
  321. *
  322. * @return void
  323. ***************************************************************************************************/
  324. void MT_ReverseBytes( byte *pData, byte len )
  325. {
  326. byte i,j;
  327. byte temp;
  328. for ( i = 0, j = len-1; len > 1; len-=2 )
  329. {
  330. temp = pData[i];
  331. pData[i++] = pData[j];
  332. pData[j--] = temp;
  333. }
  334. }
  335. /***************************************************************************************************
  336. * @fn MT_Word2Buf
  337. *
  338. * @brief Copy a uint16 array to a byte array, little endian.
  339. *
  340. * @param pBuf - byte array
  341. * @param pWord - uint16 array
  342. * @param len - length of uint16 array
  343. *
  344. * @return pointer to end of byte array
  345. ***************************************************************************************************/
  346. uint8 *MT_Word2Buf( uint8 *pBuf, uint16 *pWord, uint8 len )
  347. {
  348. while ( len-- > 0 )
  349. {
  350. *pBuf++ = LO_UINT16( *pWord );
  351. *pBuf++ = HI_UINT16( *pWord );
  352. pWord++;
  353. }
  354. return pBuf;
  355. }
  356. /***************************************************************************************************
  357. * @fn MT_BuildEndpointDesc
  358. *
  359. * @brief Build endpoint descriptor and simple descriptor structure from incoming buffer
  360. *
  361. * @param pBuf - byte array
  362. *
  363. * @return epDesc - pointer to the endpoint descriptor
  364. ***************************************************************************************************/
  365. uint8 MT_BuildEndpointDesc( uint8 *pBuf, void *param )
  366. {
  367. uint8 i;
  368. uint8 ret = ZSuccess;
  369. endPointDesc_t *epDesc;
  370. epDesc = (endPointDesc_t *)param;
  371. /* check if this endpoint is already registered */
  372. if ( afFindEndPointDesc( *pBuf ) != NULL )
  373. {
  374. ret = ZApsDuplicateEntry;
  375. }
  376. else if ( epDesc )
  377. {
  378. epDesc->endPoint = *pBuf;
  379. /* Ignore the latency reqs */
  380. epDesc->latencyReq = noLatencyReqs;
  381. /* allocate memory for the simple descriptor */
  382. epDesc->simpleDesc = (SimpleDescriptionFormat_t *) osal_mem_alloc(sizeof(SimpleDescriptionFormat_t));
  383. if (epDesc->simpleDesc)
  384. {
  385. /* Endpoint */
  386. epDesc->simpleDesc->EndPoint = *pBuf++;
  387. /* AppProfId */
  388. epDesc->simpleDesc->AppProfId = BUILD_UINT16(pBuf[0], pBuf[1]);
  389. pBuf += sizeof(uint16);
  390. /* AppDeviceId */
  391. epDesc->simpleDesc->AppDeviceId = BUILD_UINT16(pBuf[0],pBuf[1]);
  392. pBuf += sizeof(uint16);
  393. /* AppDevVer */
  394. epDesc->simpleDesc->AppDevVer = (*pBuf++) & AF_APP_DEV_VER_MASK ;
  395. /* LatencyReq */
  396. pBuf++;
  397. /* AppNumInClusters */
  398. epDesc->simpleDesc->AppNumInClusters = *pBuf++;
  399. if (epDesc->simpleDesc->AppNumInClusters)
  400. {
  401. epDesc->simpleDesc->pAppInClusterList = (uint16 *)
  402. osal_mem_alloc((epDesc->simpleDesc->AppNumInClusters)*sizeof(uint16));
  403. if ( epDesc->simpleDesc->pAppInClusterList )
  404. {
  405. for (i=0; i<(epDesc->simpleDesc->AppNumInClusters); i++)
  406. {
  407. epDesc->simpleDesc->pAppInClusterList[i] = BUILD_UINT16(*pBuf, *(pBuf+1));
  408. pBuf += 2;
  409. }
  410. }
  411. else
  412. {
  413. ret = ZMemError;
  414. }
  415. }
  416. /* AppNumOutClusters */
  417. epDesc->simpleDesc->AppNumOutClusters = *pBuf++;
  418. if (epDesc->simpleDesc->AppNumOutClusters)
  419. {
  420. epDesc->simpleDesc->pAppOutClusterList = (uint16 *)
  421. osal_mem_alloc((epDesc->simpleDesc->AppNumOutClusters)*sizeof(uint16));
  422. if (epDesc->simpleDesc->pAppOutClusterList)
  423. {
  424. for (i=0; i<(epDesc->simpleDesc->AppNumOutClusters); i++)
  425. {
  426. epDesc->simpleDesc->pAppOutClusterList[i] = BUILD_UINT16(*pBuf, *(pBuf+1));
  427. pBuf += 2;
  428. }
  429. }
  430. else
  431. {
  432. ret = ZMemError;
  433. }
  434. }
  435. /* if any list cannot be allocated...free all */
  436. if ( ret == ZMemError )
  437. {
  438. if (epDesc->simpleDesc->pAppInClusterList)
  439. {
  440. osal_mem_free(epDesc->simpleDesc->pAppInClusterList);
  441. }
  442. if (epDesc->simpleDesc->AppNumOutClusters)
  443. {
  444. osal_mem_free(epDesc->simpleDesc->pAppOutClusterList);
  445. }
  446. osal_mem_free(epDesc->simpleDesc);
  447. }
  448. }
  449. else
  450. {
  451. ret = ZMemError;
  452. }
  453. }
  454. return ret;
  455. }
  456. /***************************************************************************************************
  457. ***************************************************************************************************/