MT_X.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. /**************************************************************************************************
  2. Filename: MT_X.c
  3. Revised: $Date: 2009-08-21 13:10:13 -0700 (Fri, 21 Aug 2009) $
  4. Revision: $Revision: 20630 $
  5. Description: This file contains the MT cross-over interface to simultaneously support
  6. ZOAD on MT V1.0 and Z-TOOL on MT V2.0.
  7. Copyright 2008-2009 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. */
  38. #include <string.h>
  39. #include "hal_uart.h"
  40. #include "hal_types.h"
  41. #include "mt.h"
  42. #include "mt_rpc.h"
  43. #include "mt_x.h"
  44. #include "mt_nwk.h"
  45. #include "mt_uart.h"
  46. #include "mt_zdo.h"
  47. #include "NLMEDE.h"
  48. #include "oad_app.h"
  49. #include "ZDApp.h"
  50. #include "ZGlobals.h"
  51. /* ------------------------------------------------------------------------------------------------
  52. * Macros
  53. * ------------------------------------------------------------------------------------------------
  54. */
  55. /* ------------------------------------------------------------------------------------------------
  56. * Constants
  57. * ------------------------------------------------------------------------------------------------
  58. */
  59. #define SOP_STATE 0x00
  60. #define CMD_STATE1 0x01
  61. #define CMD_STATE2 0x02
  62. #define LEN_STATE 0x03
  63. #define DATA_STATE 0x04
  64. #define FCS_STATE 0x05
  65. #define SOP_VALUE 0x02
  66. // Index into the cross-over command array.
  67. #define MTX_OLD 0x00
  68. #define MTX_NEW 0x01
  69. // Max entries in the cross-over command array.
  70. #define MTX_MAX 12
  71. // MT_RPC_SUBSYSTEM_MASK for the equivalent cmd as a uint16.
  72. #define MTX_SSM (((uint16)MT_RPC_SUBSYSTEM_MASK << 8) | 0xFF)
  73. // SPI_RESPONSE_BIT mask for the equivalent cmd as a uint8.
  74. #define MTX_RSP ((uint8)(SPI_RESPONSE_BIT >> 8))
  75. // Old MT commands from MTEL.h, MT_NWK.h & MT_ZDO.h.
  76. #define SPI_CMD_SYS_PING 0x0007
  77. #define SPI_CMD_SYS_VERSION 0x0008
  78. #define SPI_CMD_SYS_GET_DEVICE_INFO 0x0014
  79. #define SPI_CMD_SYS_APP_MSG 0x0018
  80. #define SPI_CMD_SYS_LED_CONTROL 0x0019
  81. #define SPI_CMD_NLME_JOIN_REQ 0x0104
  82. #define SPI_CMD_NLME_LEAVE_REQ 0x0105
  83. #define SPI_CB_NLME_JOIN_CNF 0x0183
  84. #define SPI_CMD_NLME_NWK_DISC_REQ 0x010B
  85. #define SPI_CB_NLME_NWK_DISC_CNF 0x018D
  86. #define SPI_CMD_ZDO_IEEE_ADDR_REQ 0x0A03
  87. #define SPI_CB_ZDO_IEEE_ADDR_RSP 0x0A81
  88. // Hard-code the most simple sys version response to indicate the ZPortApp capability: -ZP
  89. const uint8 msgVersion[] = {
  90. SOP_VALUE, // 0x02
  91. MTX_RSP, // 0x10
  92. SPI_CMD_SYS_VERSION, // 0x08
  93. 18, // 0x12
  94. '1', // 0x31
  95. '.', // 0x2E
  96. '1', // 0x31
  97. '0', // 0x30
  98. ' ', // 0x20
  99. '(', // 0x28
  100. 'F', // 0x46
  101. '8', // 0x38
  102. 'W', // 0x57
  103. '1', // 0x31
  104. '.', // 0x2E
  105. '4', // 0x34
  106. '.', // 0x2E
  107. '2', // 0x32
  108. '-', // 0x2D
  109. 'Z', // 0x5A
  110. 'P', // 0x50
  111. ')', // 0x29
  112. 0x0C // FCS
  113. };
  114. const uint16 mtxCmd[MTX_MAX][2] =
  115. {
  116. {SPI_CMD_SYS_APP_MSG, 0x2900}, // MT_APP_MSG
  117. {SPI_CMD_SYS_APP_MSG, 0x6980}, // MT_APP_RSP
  118. {SPI_CMD_SYS_PING, 0x2101},
  119. {SPI_CMD_SYS_GET_DEVICE_INFO, 0x2700},
  120. {SPI_CMD_SYS_VERSION, 0x2102},
  121. {SPI_CMD_ZDO_IEEE_ADDR_REQ, 0x2501},
  122. {SPI_CB_ZDO_IEEE_ADDR_RSP, 0x4581},
  123. {SPI_CMD_NLME_NWK_DISC_REQ, 0x2309},
  124. {SPI_CB_NLME_NWK_DISC_CNF, 0x4389},
  125. {SPI_CMD_NLME_JOIN_REQ, 0x2304},
  126. {SPI_CMD_NLME_LEAVE_REQ, 0x2305},
  127. {SPI_CB_NLME_JOIN_CNF, 0x4383}
  128. };
  129. /* ------------------------------------------------------------------------------------------------
  130. * Typedefs
  131. * ------------------------------------------------------------------------------------------------
  132. */
  133. /* ------------------------------------------------------------------------------------------------
  134. * Global Variables
  135. * ------------------------------------------------------------------------------------------------
  136. */
  137. extern byte MT_TaskID;
  138. uint8 mtxMode;
  139. /* ------------------------------------------------------------------------------------------------
  140. * Global Functions
  141. * ------------------------------------------------------------------------------------------------
  142. */
  143. extern void MT_UartProcessZToolByte(uint8 ch);
  144. /* ------------------------------------------------------------------------------------------------
  145. * Local Variables
  146. * ------------------------------------------------------------------------------------------------
  147. */
  148. static uint8 mtxCmdToken[2];
  149. static uint8 mtxFCSToken;
  150. static uint8 mtxIdxToken;
  151. static uint8 mtxLenToken;
  152. static uint8 mtxState;
  153. static mtOSALSerialData_t *mtxMsg;
  154. static uint8 mtxRsp[128];
  155. /* ------------------------------------------------------------------------------------------------
  156. * Local Functions
  157. * ------------------------------------------------------------------------------------------------
  158. */
  159. static uint8 cmdO2N(uint8 *cmd);
  160. static uint8 cmdN2O(uint8 *cmd);
  161. static uint8 msgO2N(uint8 *cmd, uint8 *msg);
  162. static uint8 msgN2O(uint8 *cmd, uint8 idx);
  163. static void mtxIEEEAddrRspCB(uint8 *cmd);
  164. /**************************************************************************************************
  165. * @fn cmdO2N
  166. *
  167. * @brief This function translates from an old MT command to the corresponding new one and
  168. * returns TRUE if the command is recognized as pertinent to ZOAD traffic.
  169. * This function must only be called to convert incoming commands.
  170. *
  171. * input parameters
  172. *
  173. * @param cmd - A command that is to be translated if it is pertinent to ZOAD traffic.
  174. *
  175. * output parameters
  176. *
  177. * @param cmd - The translated command.
  178. *
  179. * @return TRUE if a command that is pertinent to OAD traffic is recognized and converted.
  180. * FALSE otherwise.
  181. **************************************************************************************************
  182. */
  183. static uint8 cmdO2N(uint8 *cmd)
  184. {
  185. uint16 tmp;
  186. uint8 idx;
  187. // Incoming command, so no need to mask for SPI_RESPONSE_BIT.
  188. tmp = BUILD_UINT16(cmd[1], cmd[0]);
  189. for (idx = 0; idx < MTX_MAX; idx++)
  190. {
  191. if (mtxCmd[idx][MTX_OLD] == tmp)
  192. {
  193. // Incoming command cross-over to SREQ or AREQ done by table value.
  194. cmd[0] = HI_UINT16(mtxCmd[idx][MTX_NEW]);
  195. cmd[1] = LO_UINT16(mtxCmd[idx][MTX_NEW]);
  196. break;
  197. }
  198. }
  199. return (idx == MTX_MAX) ? FALSE : TRUE;
  200. }
  201. /**************************************************************************************************
  202. * @fn cmdN2O
  203. *
  204. * @brief This function translates from a new MT command to the corresponding old one and
  205. * returns TRUE if the command is recognized as pertinent to ZOAD traffic.
  206. * This function must only be called to convert outgoing commands.
  207. *
  208. * input parameters
  209. *
  210. * @param cmd - A command that is to be translated if it is pertinent to ZOAD traffic.
  211. *
  212. * output parameters
  213. *
  214. * @param cmd - The translated command.
  215. *
  216. * @return TRUE if a command that is pertinent to OAD traffic is recognized and converted.
  217. * FALSE otherwise.
  218. **************************************************************************************************
  219. */
  220. static uint8 cmdN2O(uint8 *cmd)
  221. {
  222. uint16 tmp;
  223. uint8 idx;
  224. tmp = BUILD_UINT16(cmd[1], (cmd[0] & MT_RPC_SUBSYSTEM_MASK));
  225. for (idx = 0; idx < MTX_MAX; idx++)
  226. {
  227. if ((mtxCmd[idx][MTX_NEW] & MTX_SSM) == tmp)
  228. {
  229. return msgN2O(cmd, idx);
  230. }
  231. }
  232. return FALSE;
  233. }
  234. /**************************************************************************************************
  235. * @fn msgO2N
  236. *
  237. * @brief This function translates from the old MT message data format to the corresponding
  238. * new one. This function must only be called to convert incoming message data.
  239. *
  240. * input parameters
  241. *
  242. * @param cmd - The command that has been translated to the new format.
  243. * @param msg - The message data to be translated to the new format.
  244. *
  245. * output parameters
  246. *
  247. * @param msg - The translated message data.
  248. *
  249. * @return TRUE if the message should be sent to the destination OSAL task.
  250. **************************************************************************************************
  251. */
  252. static uint8 msgO2N(uint8 *cmd, uint8 *msg)
  253. {
  254. uint16 tmp = BUILD_UINT16(cmd[1], cmd[0]);
  255. uint8 rtrn = TRUE;
  256. if (tmp == 0x2309) // SPI_CMD_NLME_NWK_DISC_REQ
  257. {
  258. MT_ReverseBytes(msg, 4);
  259. _nwkCallbackSub = 0xFFFF;
  260. }
  261. else if (tmp == 0x2304) // SPI_CMD_NLME_JOIN_REQ
  262. {
  263. // Fake success to the NLME_JOIN_REQ while making a ZDO Init device request.
  264. const uint8 fakeNwkJoinAck[] = { 0x02, 0x11, 0x04, 0x01, 0x00, 0x14 };
  265. zgConfigPANID = BUILD_UINT16(msg[1], msg[0]);
  266. zgDefaultChannelList = 0x00000800;
  267. zgDefaultChannelList <<= (msg[2] - 11);
  268. zgDefaultStartingScanDuration = 0;
  269. ZDOInitDevice(0);
  270. _nwkCallbackSub = 0;
  271. HalUARTWrite(MT_UART_DEFAULT_PORT, (uint8 *)fakeNwkJoinAck, sizeof(fakeNwkJoinAck));
  272. rtrn = FALSE;
  273. }
  274. else if (tmp == 0x2501) // SPI_CMD_ZDO_IEEE_ADDR_REQ
  275. {
  276. uint16 addr = BUILD_UINT16(msg[0], msg[1]);
  277. if (addr != NLME_GetShortAddr())
  278. {
  279. (void)ZDP_IEEEAddrReq(addr, msg[2], msg[3], 0);
  280. }
  281. rtrn = FALSE;
  282. }
  283. else if (tmp == 0x2305) // SPI_CMD_NLME_LEAVE_REQ
  284. {
  285. // Fake a leave success and reset.
  286. mtxRsp[0] = SOP_VALUE;
  287. mtxRsp[1] = 0x01;
  288. mtxRsp[2] = 0x85;
  289. mtxRsp[3] = Z_EXTADDR_LEN + 1;
  290. osal_cpyExtAddr(mtxRsp+4, aExtendedAddress);
  291. MT_ReverseBytes(mtxRsp+4, Z_EXTADDR_LEN);
  292. mtxRsp[4+Z_EXTADDR_LEN] = ZSuccess;
  293. mtxRsp[SPI_0DATA_MSG_LEN + Z_EXTADDR_LEN] = MT_UartCalcFCS(mtxRsp+1, Z_EXTADDR_LEN+3);
  294. HalUARTWrite(MT_UART_DEFAULT_PORT, mtxRsp, SPI_0DATA_MSG_LEN+Z_EXTADDR_LEN+1);
  295. osal_start_timerEx(oad_app_taskId, ZLOAD_RESET_BOARD_EVT, 6000);
  296. }
  297. return rtrn;
  298. }
  299. /**************************************************************************************************
  300. * @fn msgN2O
  301. *
  302. * @brief This function translates from the new MT message data format to the corresponding
  303. * old one. This function must only be called to convert outgoing message data.
  304. *
  305. * input parameters
  306. *
  307. * @param cmd - The command that has been translated to the old format.
  308. * @param msg - The message data to be translated to the old format.
  309. *
  310. * output parameters
  311. *
  312. * @param msg - The translated message data.
  313. *
  314. * @return TRUE if message should be sent on UART; FALSE if handled here.
  315. **************************************************************************************************
  316. */
  317. static uint8 msgN2O(uint8 *cmd, uint8 idx)
  318. {
  319. if (mtxCmd[idx][MTX_OLD] == SPI_CMD_SYS_VERSION)
  320. {
  321. HalUARTWrite(MT_UART_DEFAULT_PORT, (uint8 *)msgVersion, sizeof(msgVersion));
  322. idx = MTX_MAX; // Force return of FALSE since command handled here.
  323. }
  324. else
  325. {
  326. // Outgoing command needs to determine if it is a response or not.
  327. if ((cmd[0] & MT_RPC_CMD_SRSP) == MT_RPC_CMD_SRSP)
  328. {
  329. cmd[0] = MTX_RSP;
  330. }
  331. else
  332. {
  333. cmd[0] = 0;
  334. }
  335. cmd[0] |= HI_UINT16(mtxCmd[idx][MTX_OLD]);
  336. cmd[1] = LO_UINT16(mtxCmd[idx][MTX_OLD]);
  337. if (mtxCmd[idx][MTX_OLD] == SPI_CMD_SYS_GET_DEVICE_INFO)
  338. {
  339. MT_ReverseBytes(cmd+3, Z_EXTADDR_LEN); // Reverse the IEEE.
  340. MT_ReverseBytes(cmd+3+Z_EXTADDR_LEN, 2); // Reverse the Nwk Addr.
  341. }
  342. else if (mtxCmd[idx][MTX_OLD] == SPI_CB_NLME_NWK_DISC_CNF)
  343. {
  344. idx = cmd[2];
  345. cmd += 3;
  346. while (idx--)
  347. {
  348. MT_ReverseBytes(cmd, 2); // Reverse the PanId for every network discovered.
  349. cmd += 9;
  350. }
  351. }
  352. /*else if (mtxCmd[idx][MTX_OLD] == SPI_CB_NLME_JOIN_CNF)
  353. {
  354. MT_ReverseBytes(cmd+2, Z_EXTADDR_LEN); // Reverse the IEEE.
  355. MT_ReverseBytes(cmd+10, 2); // Reverse the PanId.
  356. }*/
  357. else if (mtxCmd[idx][MTX_OLD] == SPI_CB_ZDO_IEEE_ADDR_RSP)
  358. {
  359. mtxIEEEAddrRspCB(cmd-1);
  360. idx = MTX_MAX; // Force return of FALSE since command handled here.
  361. }
  362. }
  363. return (idx == MTX_MAX) ? FALSE : TRUE;
  364. }
  365. /**************************************************************************************************
  366. * @fn mtxIEEEAddrRspCB
  367. *
  368. * @brief Make the big conversion from new to old IEEE MT response.
  369. *
  370. * input parameters
  371. *
  372. * @param cmd - New response buffer beginning with a converted command.
  373. *
  374. * output parameters
  375. *
  376. * None.
  377. *
  378. * @return None.
  379. **************************************************************************************************
  380. */
  381. static void mtxIEEEAddrRspCB(uint8 *cmd)
  382. {
  383. uint8 cnt = cmd[15] * 2;
  384. if (cnt > 12) cnt = 12;
  385. mtxRsp[0] = SOP_VALUE;
  386. mtxRsp[1] = cmd[1];
  387. mtxRsp[2] = cmd[2];
  388. mtxRsp[3] = 36;
  389. // Set the network address.
  390. mtxRsp[4] = Addr16Bit;
  391. (void)memset(mtxRsp+5, 0, Z_EXTADDR_LEN-2);
  392. mtxRsp[11] = cmd[13];
  393. mtxRsp[12] = cmd[12];
  394. mtxRsp[13] = cmd[3]; // Status.
  395. // Copy and reverse the IEEE.
  396. (void)memcpy(mtxRsp+14, cmd+4, Z_EXTADDR_LEN);
  397. MT_ReverseBytes(mtxRsp+14, Z_EXTADDR_LEN);
  398. // Copy and reverse the start index and count.
  399. mtxRsp[22] = cmd[15];
  400. mtxRsp[23] = cmd[14];
  401. // Copy each Nwk Addr.
  402. (void)memcpy(mtxRsp+24, cmd+16, cnt);
  403. // Zero out unused addresses.
  404. (void)memset(mtxRsp+24+cnt, 0, 12-cnt);
  405. mtxRsp[SPI_0DATA_MSG_LEN-1 + 36] = MT_UartCalcFCS(mtxRsp+1, (MT_RPC_FRAME_HDR_SZ + 36));
  406. HalUARTWrite(MT_UART_DEFAULT_PORT, mtxRsp, SPI_0DATA_MSG_LEN+36);
  407. }
  408. /**************************************************************************************************
  409. * @fn MT_X_UartProcessZToolData
  410. *
  411. * @brief This function is called by Hal_UART_SendCallBack to read incoming Rx data.
  412. *
  413. * Attempt to sync on and parse the old MT 1.0 format:
  414. * | SOP | Data Length | CMD | Data | FCS |
  415. * | 1 | 1 | 2 | 0-Len | 1 |
  416. *
  417. * And then pass on all data to the MT 2.0 by invoking
  418. * void MT_UartProcessZToolData ( uint8 port, uint8 event )
  419. *
  420. * input parameters
  421. *
  422. * @param port - UART port.
  423. * @param event - Event that causes the callback.
  424. *
  425. * output parameters
  426. *
  427. * None.
  428. *
  429. * @return None.
  430. **************************************************************************************************
  431. */
  432. void MT_X_UartProcessZToolData(uint8 port, uint8 event)
  433. {
  434. uint8 ch;
  435. (void)event;
  436. while (HalUARTRead(port, &ch, 1))
  437. {
  438. if (mtxMode)
  439. {
  440. switch (mtxState)
  441. {
  442. case SOP_STATE:
  443. if (ch == SOP_VALUE)
  444. {
  445. mtxState = CMD_STATE1;
  446. }
  447. break;
  448. case CMD_STATE1:
  449. mtxCmdToken[0] = ch;
  450. mtxFCSToken = 0;
  451. mtxState = CMD_STATE2;
  452. break;
  453. case CMD_STATE2:
  454. mtxCmdToken[1] = ch;
  455. /* If it is an old command that is pertinent to the ZOAD traffic, convert it to new and
  456. * continue to parse, otherwise re-start the sync.
  457. */
  458. if (cmdO2N(mtxCmdToken))
  459. {
  460. mtxState = LEN_STATE;
  461. }
  462. else
  463. {
  464. mtxState = SOP_STATE;
  465. }
  466. break;
  467. case LEN_STATE:
  468. if (ch == 0)
  469. {
  470. mtxState = FCS_STATE;
  471. }
  472. else
  473. {
  474. mtxState = DATA_STATE;
  475. }
  476. mtxLenToken = ch;
  477. mtxIdxToken = 0;
  478. /* Allocate memory for the data */
  479. mtxMsg = (mtOSALSerialData_t *)osal_msg_allocate(sizeof(mtOSALSerialData_t) +
  480. MT_RPC_FRAME_HDR_SZ + mtxLenToken);
  481. if (mtxMsg)
  482. {
  483. mtxMsg->hdr.event = CMD_SERIAL_MSG;
  484. mtxMsg->msg = (uint8*)(mtxMsg+1);
  485. mtxMsg->msg[MT_RPC_POS_LEN] = mtxLenToken;
  486. mtxMsg->msg[MT_RPC_POS_CMD0] = mtxCmdToken[0];
  487. mtxMsg->msg[MT_RPC_POS_CMD1] = mtxCmdToken[1];
  488. }
  489. else
  490. {
  491. mtxState = SOP_STATE;
  492. }
  493. break;
  494. case DATA_STATE:
  495. mtxMsg->msg[MT_RPC_FRAME_HDR_SZ + mtxIdxToken++] = ch;
  496. if (--mtxLenToken == 0)
  497. {
  498. mtxState = FCS_STATE;
  499. }
  500. break;
  501. case FCS_STATE:
  502. if (mtxFCSToken == ch)
  503. {
  504. if (msgO2N(mtxCmdToken, (mtxMsg->msg)+MT_RPC_POS_DAT0))
  505. {
  506. osal_msg_send(MT_TaskID, (byte *)mtxMsg);
  507. }
  508. else
  509. {
  510. osal_msg_deallocate((uint8 *)mtxMsg);
  511. }
  512. }
  513. else
  514. {
  515. osal_msg_deallocate ( (uint8 *)mtxMsg );
  516. }
  517. mtxState = SOP_STATE;
  518. break;
  519. default:
  520. break;
  521. }
  522. mtxFCSToken ^= ch;
  523. }
  524. else
  525. {
  526. MT_UartProcessZToolByte(ch);
  527. }
  528. }
  529. }
  530. /**************************************************************************************************
  531. * @fn MT_X_TransportSend
  532. *
  533. * @brief Prepare message to send by old MT format if it is pertinent to ZOAD.
  534. * Copy message header to re-use to also send in new MT format and then
  535. * fill in SOP and FCS then send out the msg.
  536. *
  537. * input parameters
  538. *
  539. * @param msg - pointer to the message that contains spare byte, CMD, length, data and FCS.
  540. *
  541. * output parameters
  542. *
  543. * None.
  544. *
  545. * @return None.
  546. **************************************************************************************************
  547. */
  548. void MT_X_TransportSend(uint8 *msg)
  549. {
  550. if (mtxMode)
  551. {
  552. if (cmdN2O(msg+2))
  553. {
  554. // Re-position the 2 cmd bytes & length to the old format positions.
  555. msg[0] = msg[1]; // Temporarily save data len.
  556. (void)memcpy(msg+1, msg+2, 2); // Move command bytes to first location after SOP.
  557. msg[3] = msg[0]; // Data len.
  558. msg[0] = SOP_VALUE;
  559. msg[SPI_0DATA_MSG_LEN-1 + msg[3]] = MT_UartCalcFCS(msg+1, (MT_RPC_FRAME_HDR_SZ + msg[3]));
  560. HalUARTWrite(MT_UART_DEFAULT_PORT, msg, SPI_0DATA_MSG_LEN + msg[3]);
  561. }
  562. }
  563. else
  564. {
  565. msg[0] = MT_UART_SOF;
  566. msg[SPI_0DATA_MSG_LEN - 1 + msg[1]] = MT_UartCalcFCS (msg+1, (3 + msg[1]));
  567. HalUARTWrite(MT_UART_DEFAULT_PORT, msg, msg[1] + SPI_0DATA_MSG_LEN);
  568. }
  569. osal_msg_deallocate(msg);
  570. }
  571. /**************************************************************************************************
  572. * @fn MT_X_FakeNwkJoinCnf
  573. *
  574. * @brief Fake a SPI_CB_NLME_JOIN_CNF to the old ZOAD.
  575. *
  576. * input parameters
  577. *
  578. * None.
  579. *
  580. * output parameters
  581. *
  582. * None.
  583. *
  584. * @return None.
  585. **************************************************************************************************
  586. */
  587. void MT_X_FakeNwkJoinCnf(void)
  588. {
  589. uint8 *msg;
  590. if ((msg=osal_mem_alloc(SPI_0DATA_MSG_LEN+11)))
  591. {
  592. msg[0] = SOP_VALUE;
  593. msg[1] = 0x01;
  594. msg[2] = 0x83;
  595. msg[3] = 11;
  596. osal_nv_read(ZCD_NV_EXTADDR, 0, Z_EXTADDR_LEN, msg+4);
  597. MT_ReverseBytes(msg+4, Z_EXTADDR_LEN);
  598. msg[12] = HI_UINT16(zgConfigPANID);
  599. msg[13] = LO_UINT16(zgConfigPANID);
  600. msg[14] = 0;
  601. msg[SPI_0DATA_MSG_LEN-1 + 11] = MT_UartCalcFCS(msg+1, (MT_RPC_FRAME_HDR_SZ + 11));
  602. HalUARTWrite(MT_UART_DEFAULT_PORT, msg, SPI_0DATA_MSG_LEN+11);
  603. osal_mem_free(msg);
  604. }
  605. }
  606. /**************************************************************************************************
  607. */