MT_AF.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. /**************************************************************************************************
  2. Filename: MT_AF.c
  3. Revised: $Date: 2011-05-27 16:00:27 -0700 (Fri, 27 May 2011) $
  4. Revision: $Revision: 26145 $
  5. Description: MonitorTest functions for the AF layer.
  6. Copyright 2007-2011 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. */
  37. #include "ZComDef.h"
  38. #include "OSAL.h"
  39. #include "MT.h"
  40. #include "MT_AF.h"
  41. #include "MT_ZDO.h"
  42. #include "nwk.h"
  43. #include "OnBoard.h"
  44. #include "MT_UART.h"
  45. #if defined INTER_PAN
  46. #include "stub_aps.h"
  47. #endif
  48. /* ------------------------------------------------------------------------------------------------
  49. * Constants
  50. * ------------------------------------------------------------------------------------------------
  51. */
  52. #if !defined MT_AF_EXEC_CNT
  53. #define MT_AF_EXEC_CNT 15
  54. #endif
  55. #if !defined MT_AF_EXEC_DLY
  56. #define MT_AF_EXEC_DLY 1000
  57. #endif
  58. /* ------------------------------------------------------------------------------------------------
  59. * Typedefs
  60. * ------------------------------------------------------------------------------------------------
  61. */
  62. typedef struct
  63. {
  64. uint8 *data;
  65. afAddrType_t dstAddr;
  66. endPointDesc_t *epDesc;
  67. uint16 cId;
  68. uint16 dataLen;
  69. uint8 transId;
  70. uint8 txOpts;
  71. uint8 radius;
  72. uint8 tick;
  73. } mtAfDataReq_t;
  74. typedef struct _mtAfInMsgList_t
  75. {
  76. struct _mtAfInMsgList_t *next;
  77. uint8 *data;
  78. uint32 timestamp; // Receipt timestamp from MAC.
  79. uint8 tick;
  80. } mtAfInMsgList_t;
  81. /* ------------------------------------------------------------------------------------------------
  82. * Local Variables
  83. * ------------------------------------------------------------------------------------------------
  84. */
  85. mtAfInMsgList_t *pMtAfInMsgList = NULL;
  86. mtAfDataReq_t *pMtAfDataReq = NULL;
  87. /* ------------------------------------------------------------------------------------------------
  88. * Global Variables
  89. * ------------------------------------------------------------------------------------------------
  90. */
  91. #if defined ( MT_AF_CB_FUNC )
  92. uint16 _afCallbackSub;
  93. #endif
  94. /* ------------------------------------------------------------------------------------------------
  95. * Local Functions
  96. * ------------------------------------------------------------------------------------------------
  97. */
  98. void MT_AfRegister(uint8 *pBuf);
  99. void MT_AfDataRequest(uint8 *pBuf);
  100. #if defined ( ZIGBEE_SOURCE_ROUTING )
  101. void MT_AfDataRequestSrcRtg(uint8 *pBuf);
  102. #endif
  103. #if defined INTER_PAN
  104. static void MT_AfInterPanCtl(uint8 *pBuf);
  105. #endif
  106. static void MT_AfDataRetrieve(uint8 *pBuf);
  107. static void MT_AfDataStore(uint8 *pBuf);
  108. static void MT_AfAPSF_ConfigSet(uint8 *pBuf);
  109. /**************************************************************************************************
  110. * @fn MT_AfExec
  111. *
  112. * @brief This function is invoked by an MT timer event.
  113. *
  114. * input parameters
  115. *
  116. * None.
  117. *
  118. * output parameters
  119. *
  120. * None.
  121. *
  122. * @return None.
  123. **************************************************************************************************
  124. */
  125. void MT_AfExec(void)
  126. {
  127. mtAfInMsgList_t *pPrev, *pItem = pMtAfInMsgList;
  128. while (pItem != NULL)
  129. {
  130. if (--(pItem->tick) == 0)
  131. {
  132. if (pMtAfInMsgList == pItem)
  133. {
  134. pMtAfInMsgList = pItem->next;
  135. (void)osal_mem_free(pItem);
  136. pItem = pMtAfInMsgList;
  137. }
  138. else
  139. {
  140. pPrev->next = pItem->next;
  141. (void)osal_mem_free(pItem);
  142. pItem = pPrev->next;
  143. }
  144. }
  145. else
  146. {
  147. pPrev = pItem;
  148. pItem = pItem->next;
  149. }
  150. }
  151. if (pMtAfDataReq != NULL)
  152. {
  153. if (--(pMtAfDataReq->tick) == 0)
  154. {
  155. (void)osal_mem_free(pMtAfDataReq);
  156. pMtAfDataReq = NULL;
  157. }
  158. }
  159. if ((pMtAfInMsgList != NULL) || (pMtAfDataReq != NULL))
  160. {
  161. if (ZSuccess != osal_start_timerEx(MT_TaskID, MT_AF_EXEC_EVT, MT_AF_EXEC_DLY))
  162. {
  163. osal_set_event(MT_TaskID, MT_AF_EXEC_EVT);
  164. }
  165. }
  166. }
  167. /***************************************************************************************************
  168. * @fn MT_AfCommandProcessing
  169. *
  170. * @brief Process all the AF commands that are issued by test tool
  171. *
  172. * @param pBuf - pointer to the received buffer
  173. *
  174. * @return status
  175. ***************************************************************************************************/
  176. uint8 MT_AfCommandProcessing(uint8 *pBuf)
  177. {
  178. uint8 status = MT_RPC_SUCCESS;
  179. switch (pBuf[MT_RPC_POS_CMD1])
  180. {
  181. case MT_AF_REGISTER:
  182. MT_AfRegister(pBuf);
  183. break;
  184. case MT_AF_DATA_REQUEST:
  185. case MT_AF_DATA_REQUEST_EXT:
  186. MT_AfDataRequest(pBuf);
  187. break;
  188. #if defined ( ZIGBEE_SOURCE_ROUTING )
  189. case MT_AF_DATA_REQUEST_SRCRTG:
  190. MT_AfDataRequestSrcRtg(pBuf);
  191. break;
  192. #endif
  193. #if defined INTER_PAN
  194. case MT_AF_INTER_PAN_CTL:
  195. MT_AfInterPanCtl(pBuf);
  196. break;
  197. #endif
  198. case MT_AF_DATA_RETRIEVE:
  199. MT_AfDataRetrieve(pBuf);
  200. break;
  201. case MT_AF_DATA_STORE:
  202. MT_AfDataStore(pBuf);
  203. break;
  204. case MT_AF_APSF_CONFIG_SET:
  205. MT_AfAPSF_ConfigSet(pBuf);
  206. break;
  207. default:
  208. status = MT_RPC_ERR_COMMAND_ID;
  209. break;
  210. }
  211. return status;
  212. }
  213. /***************************************************************************************************
  214. * @fn MT_AfRegister
  215. *
  216. * @brief Process AF Register command
  217. *
  218. * @param pBuf - pointer to the received buffer
  219. *
  220. * @return none
  221. ***************************************************************************************************/
  222. void MT_AfRegister(uint8 *pBuf)
  223. {
  224. uint8 cmdId;
  225. uint8 retValue = ZMemError;
  226. endPointDesc_t *epDesc;
  227. /* parse header */
  228. cmdId = pBuf[MT_RPC_POS_CMD1];
  229. pBuf += MT_RPC_FRAME_HDR_SZ;
  230. epDesc = (endPointDesc_t *)osal_mem_alloc(sizeof(endPointDesc_t));
  231. if ( epDesc )
  232. {
  233. epDesc->task_id = &MT_TaskID;
  234. retValue = MT_BuildEndpointDesc( pBuf, epDesc );
  235. if ( retValue == ZSuccess )
  236. {
  237. retValue = afRegister( epDesc );
  238. }
  239. if ( retValue != ZSuccess )
  240. {
  241. osal_mem_free( epDesc );
  242. }
  243. }
  244. /* Build and send back the response */
  245. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_AF), cmdId, 1, &retValue);
  246. }
  247. /***************************************************************************************************
  248. * @fn MT_AfDataRequest
  249. *
  250. * @brief Process AF Register command
  251. *
  252. * @param pBuf - pointer to the received buffer
  253. *
  254. * @return none
  255. ***************************************************************************************************/
  256. void MT_AfDataRequest(uint8 *pBuf)
  257. {
  258. #define MT_AF_REQ_MSG_LEN 10
  259. #define MT_AF_REQ_MSG_EXT 10
  260. endPointDesc_t *epDesc;
  261. afAddrType_t dstAddr;
  262. cId_t cId;
  263. uint8 transId, txOpts, radius;
  264. uint8 cmd0, cmd1;
  265. uint8 retValue = ZFailure;
  266. uint16 dataLen, tempLen;
  267. /* Parse header */
  268. cmd0 = pBuf[MT_RPC_POS_CMD0];
  269. cmd1 = pBuf[MT_RPC_POS_CMD1];
  270. pBuf += MT_RPC_FRAME_HDR_SZ;
  271. if (cmd1 == MT_AF_DATA_REQUEST_EXT)
  272. {
  273. dstAddr.addrMode = (afAddrMode_t)*pBuf++;
  274. if (dstAddr.addrMode == afAddr64Bit)
  275. {
  276. (void)osal_memcpy(dstAddr.addr.extAddr, pBuf, Z_EXTADDR_LEN);
  277. }
  278. else
  279. {
  280. dstAddr.addr.shortAddr = BUILD_UINT16(pBuf[0], pBuf[1]);
  281. }
  282. pBuf += Z_EXTADDR_LEN;
  283. dstAddr.endPoint = *pBuf++;
  284. dstAddr.panId = BUILD_UINT16(pBuf[0], pBuf[1]);
  285. pBuf += 2;
  286. }
  287. else
  288. {
  289. /* Destination address */
  290. dstAddr.addrMode = afAddr16Bit;
  291. dstAddr.addr.shortAddr = BUILD_UINT16(pBuf[0], pBuf[1]);
  292. pBuf += 2;
  293. /* Destination endpoint */
  294. dstAddr.endPoint = *pBuf++;
  295. dstAddr.panId = 0;
  296. }
  297. /* Source endpoint */
  298. epDesc = afFindEndPointDesc(*pBuf++);
  299. /* ClusterId */
  300. cId = BUILD_UINT16(pBuf[0], pBuf[1]);
  301. pBuf +=2;
  302. /* TransId */
  303. transId = *pBuf++;
  304. /* TxOption */
  305. txOpts = *pBuf++;
  306. /* Radius */
  307. radius = *pBuf++;
  308. /* Length */
  309. if (cmd1 == MT_AF_DATA_REQUEST_EXT)
  310. {
  311. dataLen = BUILD_UINT16(pBuf[0], pBuf[1]);
  312. tempLen = dataLen + MT_AF_REQ_MSG_LEN + MT_AF_REQ_MSG_EXT;
  313. pBuf += 2;
  314. }
  315. else
  316. {
  317. dataLen = *pBuf++;
  318. tempLen = dataLen + MT_AF_REQ_MSG_LEN;
  319. }
  320. if ( epDesc == NULL )
  321. {
  322. retValue = afStatus_INVALID_PARAMETER;
  323. }
  324. else if (tempLen > (uint16)MT_RPC_DATA_MAX)
  325. {
  326. if (pMtAfDataReq != NULL)
  327. {
  328. retValue = afStatus_INVALID_PARAMETER;
  329. }
  330. else if ((pMtAfDataReq = osal_mem_alloc(sizeof(mtAfDataReq_t) + dataLen)) == NULL)
  331. {
  332. retValue = afStatus_MEM_FAIL;
  333. }
  334. else
  335. {
  336. retValue = afStatus_SUCCESS;
  337. pMtAfDataReq->data = (uint8 *)(pMtAfDataReq+1);
  338. (void)osal_memcpy(&(pMtAfDataReq->dstAddr), &dstAddr, sizeof(afAddrType_t));
  339. pMtAfDataReq->epDesc = epDesc;
  340. pMtAfDataReq->cId = cId;
  341. pMtAfDataReq->dataLen = dataLen;
  342. pMtAfDataReq->transId = transId;
  343. pMtAfDataReq->txOpts = txOpts;
  344. pMtAfDataReq->radius = radius;
  345. // Setup to time-out the huge outgoing item if host does not MT_AF_DATA_STORE it.
  346. pMtAfDataReq->tick = MT_AF_EXEC_CNT;
  347. if (ZSuccess != osal_start_timerEx(MT_TaskID, MT_AF_EXEC_EVT, MT_AF_EXEC_DLY))
  348. {
  349. (void)osal_set_event(MT_TaskID, MT_AF_EXEC_EVT);
  350. }
  351. }
  352. }
  353. else
  354. {
  355. retValue = AF_DataRequest(&dstAddr, epDesc, cId, dataLen, pBuf, &transId, txOpts, radius);
  356. }
  357. if (MT_RPC_CMD_SREQ == (cmd0 & MT_RPC_CMD_TYPE_MASK))
  358. {
  359. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP|(uint8)MT_RPC_SYS_AF), cmd1, 1, &retValue);
  360. }
  361. }
  362. #if defined ( ZIGBEE_SOURCE_ROUTING )
  363. /***************************************************************************************************
  364. * @fn MT_AfDataRequestSrcRtg
  365. *
  366. * @brief Process AF Register command
  367. *
  368. * @param pBuf - pointer to the received buffer
  369. *
  370. * @return none
  371. ***************************************************************************************************/
  372. void MT_AfDataRequestSrcRtg(uint8 *pBuf)
  373. {
  374. uint8 cmdId, dataLen = 0;
  375. uint8 retValue = ZFailure;
  376. endPointDesc_t *epDesc;
  377. byte transId;
  378. afAddrType_t dstAddr;
  379. cId_t cId;
  380. byte txOpts, radius, srcEP, relayCnt;
  381. uint16 *pRelayList;
  382. uint8 i;
  383. /* parse header */
  384. cmdId = pBuf[MT_RPC_POS_CMD1];
  385. pBuf += MT_RPC_FRAME_HDR_SZ;
  386. /* Destination address */
  387. /* Initialize the panID field to zero to avoid inter-pan */
  388. osal_memset( &dstAddr, 0, sizeof(afAddrType_t) );
  389. dstAddr.addrMode = afAddr16Bit;
  390. dstAddr.addr.shortAddr = BUILD_UINT16(pBuf[0], pBuf[1]);
  391. pBuf += 2;
  392. /* Destination endpoint */
  393. dstAddr.endPoint = *pBuf++;
  394. /* Source endpoint */
  395. srcEP = *pBuf++;
  396. epDesc = afFindEndPointDesc( srcEP );
  397. /* ClusterId */
  398. cId = BUILD_UINT16(pBuf[0], pBuf[1]);
  399. pBuf +=2;
  400. /* TransId */
  401. transId = *pBuf++;
  402. /* TxOption */
  403. txOpts = *pBuf++;
  404. /* Radius */
  405. radius = *pBuf++;
  406. /* Source route relay count */
  407. relayCnt = *pBuf++;
  408. /* Convert the source route relay list */
  409. if( (pRelayList = osal_mem_alloc( relayCnt * sizeof( uint16 ))) != NULL )
  410. {
  411. for( i = 0; i < relayCnt; i++ )
  412. {
  413. pRelayList[i] = BUILD_UINT16( pBuf[0], pBuf[1] );
  414. pBuf += 2;
  415. }
  416. /* Data payload Length */
  417. dataLen = *pBuf++;
  418. if ( epDesc == NULL )
  419. {
  420. retValue = afStatus_INVALID_PARAMETER;
  421. }
  422. else
  423. {
  424. retValue = AF_DataRequestSrcRtg( &dstAddr, epDesc, cId, dataLen, pBuf,
  425. &transId, txOpts, radius, relayCnt, pRelayList );
  426. }
  427. /* Free the memory allocated */
  428. osal_mem_free( pRelayList );
  429. }
  430. else
  431. {
  432. retValue = afStatus_MEM_FAIL;
  433. }
  434. /* Build and send back the response */
  435. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_AF), cmdId, 1, &retValue);
  436. }
  437. #endif
  438. #if defined INTER_PAN
  439. /***************************************************************************************************
  440. * @fn MT_AfInterPanCtl
  441. *
  442. * @brief Process the AF Inter Pan control command.
  443. *
  444. * @param pBuf - pointer to the received buffer
  445. *
  446. * @return none
  447. ***************************************************************************************************/
  448. static void MT_AfInterPanCtl(uint8 *pBuf)
  449. {
  450. uint8 cmd, rtrn;
  451. uint16 panId;
  452. endPointDesc_t *pEP;
  453. cmd = pBuf[MT_RPC_POS_CMD1];
  454. pBuf += MT_RPC_FRAME_HDR_SZ;
  455. switch (*pBuf++) // Inter-pan request parameter.
  456. {
  457. case InterPanClr:
  458. rtrn = StubAPS_SetIntraPanChannel(); // Switch channel back to the NIB channel.
  459. break;
  460. case InterPanSet:
  461. rtrn = StubAPS_SetInterPanChannel(*pBuf); // Set channel for inter-pan communication.
  462. break;
  463. case InterPanReg:
  464. if ((pEP = afFindEndPointDesc(*pBuf)))
  465. {
  466. StubAPS_RegisterApp(pEP);
  467. rtrn = SUCCESS;
  468. }
  469. else
  470. {
  471. rtrn = FAILURE;
  472. }
  473. break;
  474. case InterPanChk:
  475. panId = BUILD_UINT16(pBuf[0], pBuf[1]);
  476. rtrn = (StubAPS_InterPan(panId, pBuf[2])) ? ZSuccess : ZFailure;
  477. break;
  478. default:
  479. rtrn = afStatus_INVALID_PARAMETER;
  480. break;
  481. }
  482. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_AF), cmd, 1, &rtrn);
  483. }
  484. #endif
  485. /***************************************************************************************************
  486. * @fn MT_AfDataConfirm
  487. *
  488. * @brief Process
  489. *
  490. * @param pBuf - pointer to the received buffer
  491. *
  492. * @return none
  493. ***************************************************************************************************/
  494. void MT_AfDataConfirm(afDataConfirm_t *pMsg)
  495. {
  496. uint8 retArray[3];
  497. retArray[0] = pMsg->hdr.status;
  498. retArray[1] = pMsg->endpoint;
  499. retArray[2] = pMsg->transID;
  500. /* Build and send back the response */
  501. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_AREQ | (uint8)MT_RPC_SYS_AF), MT_AF_DATA_CONFIRM, 3, retArray);
  502. }
  503. /***************************************************************************************************
  504. * @fn MT_AfIncomingMsg
  505. *
  506. * @brief Process the callback subscription for AF Incoming data.
  507. *
  508. * @param pkt - Incoming AF data.
  509. *
  510. * @return none
  511. ***************************************************************************************************/
  512. void MT_AfIncomingMsg(afIncomingMSGPacket_t *pMsg)
  513. {
  514. #define MT_AF_INC_MSG_LEN 17
  515. #define MT_AF_INC_MSG_EXT 10
  516. uint16 dataLen = pMsg->cmd.DataLength; // Length of the data section in the response packet.
  517. uint16 respLen = MT_AF_INC_MSG_LEN + dataLen;
  518. uint8 cmd = MT_AF_INCOMING_MSG;
  519. uint8 *pRsp, *pTmp;
  520. mtAfInMsgList_t *pItem = NULL;
  521. #if defined INTER_PAN
  522. if (StubAPS_InterPan(pMsg->srcAddr.panId, pMsg->srcAddr.endPoint))
  523. {
  524. cmd = MT_AF_INCOMING_MSG_EXT;
  525. }
  526. else
  527. #endif
  528. if ((pMsg->srcAddr.addrMode == afAddr64Bit) ||
  529. (respLen > (uint16)(MT_RPC_DATA_MAX - MT_AF_INC_MSG_EXT)))
  530. {
  531. cmd = MT_AF_INCOMING_MSG_EXT;
  532. }
  533. if (cmd == MT_AF_INCOMING_MSG_EXT)
  534. {
  535. respLen += MT_AF_INC_MSG_EXT;
  536. }
  537. if (respLen > (uint16)MT_RPC_DATA_MAX)
  538. {
  539. if ((pItem = (mtAfInMsgList_t *)osal_mem_alloc(sizeof(mtAfInMsgList_t) + dataLen)) == NULL)
  540. {
  541. return; // If cannot hold a huge message, cannot give indication at all.
  542. }
  543. pItem->data = (uint8 *)(pItem+1);
  544. respLen -= dataLen; // Zero data bytes are sent with an over-sized incoming indication.
  545. }
  546. // Attempt to allocate memory for the response packet.
  547. if ((pRsp = osal_mem_alloc(respLen)) == NULL)
  548. {
  549. if (pItem != NULL)
  550. {
  551. (void)osal_mem_free(pItem);
  552. }
  553. return;
  554. }
  555. pTmp = pRsp;
  556. /* Group ID */
  557. *pTmp++ = LO_UINT16(pMsg->groupId);
  558. *pTmp++ = HI_UINT16(pMsg->groupId);
  559. /* Cluster ID */
  560. *pTmp++ = LO_UINT16(pMsg->clusterId);
  561. *pTmp++ = HI_UINT16(pMsg->clusterId);
  562. if (cmd == MT_AF_INCOMING_MSG_EXT)
  563. {
  564. *pTmp++ = pMsg->srcAddr.addrMode;
  565. if (pMsg->srcAddr.addrMode == afAddr64Bit)
  566. {
  567. (void)osal_memcpy(pTmp, pMsg->srcAddr.addr.extAddr, Z_EXTADDR_LEN);
  568. }
  569. else
  570. {
  571. pTmp[0] = LO_UINT16(pMsg->srcAddr.addr.shortAddr);
  572. pTmp[1] = HI_UINT16(pMsg->srcAddr.addr.shortAddr);
  573. }
  574. pTmp += Z_EXTADDR_LEN;
  575. *pTmp++ = pMsg->srcAddr.endPoint;
  576. #if defined INTER_PAN
  577. *pTmp++ = LO_UINT16(pMsg->srcAddr.panId);
  578. *pTmp++ = HI_UINT16(pMsg->srcAddr.panId);
  579. #else
  580. *pTmp++ = 0;
  581. *pTmp++ = 0;
  582. #endif
  583. }
  584. else
  585. {
  586. /* Source Address */
  587. *pTmp++ = LO_UINT16(pMsg->srcAddr.addr.shortAddr);
  588. *pTmp++ = HI_UINT16(pMsg->srcAddr.addr.shortAddr);
  589. /* Source EP */
  590. *pTmp++ = pMsg->srcAddr.endPoint;
  591. }
  592. /* Destination EP */
  593. *pTmp++ = pMsg->endPoint;
  594. /* WasBroadCast */
  595. *pTmp++ = pMsg->wasBroadcast;
  596. /* LinkQuality */
  597. *pTmp++ = pMsg->LinkQuality;
  598. /* SecurityUse */
  599. *pTmp++ = pMsg->SecurityUse;
  600. /* Timestamp */
  601. *pTmp++ = BREAK_UINT32(pMsg->timestamp, 0);
  602. *pTmp++ = BREAK_UINT32(pMsg->timestamp, 1);
  603. *pTmp++ = BREAK_UINT32(pMsg->timestamp, 2);
  604. *pTmp++ = BREAK_UINT32(pMsg->timestamp, 3);
  605. /* Data Length */
  606. if (cmd == MT_AF_INCOMING_MSG_EXT)
  607. {
  608. /* Z-Tool apparently takes the last Byte before the data buffer as the dynamic length and
  609. * ignores the bigger UInt16 length of an EXT incoming message. But no data bytes will be sent
  610. * with a huge message, so it's necessary to work-around and fake-out Z-Tool with a zero here.
  611. */
  612. *pTmp++ = 0; // TODO - workaround Z-Tool shortcoming; should be: = pMsg->cmd.TransSeqNumber;
  613. *pTmp++ = LO_UINT16(dataLen);
  614. *pTmp++ = HI_UINT16(dataLen);
  615. }
  616. else
  617. {
  618. *pTmp++ = pMsg->cmd.TransSeqNumber;
  619. *pTmp++ = dataLen;
  620. }
  621. /* Data */
  622. if (pItem != NULL)
  623. {
  624. // Enqueue the new huge incoming item.
  625. pItem->next = pMtAfInMsgList;
  626. pMtAfInMsgList = pItem;
  627. // Setup to time-out the huge incoming item if host does not MT_AF_DATA_RETRIEVE it.
  628. pItem->tick = MT_AF_EXEC_CNT;
  629. if (ZSuccess != osal_start_timerEx(MT_TaskID, MT_AF_EXEC_EVT, MT_AF_EXEC_DLY))
  630. {
  631. (void)osal_set_event(MT_TaskID, MT_AF_EXEC_EVT);
  632. }
  633. pItem->timestamp = pMsg->timestamp;
  634. (void)osal_memcpy(pItem->data, pMsg->cmd.Data, dataLen);
  635. }
  636. else
  637. {
  638. (void)osal_memcpy(pTmp, pMsg->cmd.Data, dataLen);
  639. }
  640. /* Build and send back the response */
  641. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_AREQ|(uint8)MT_RPC_SYS_AF), cmd, respLen, pRsp);
  642. (void)osal_mem_free(pRsp);
  643. }
  644. /**************************************************************************************************
  645. * @fn MT_AfDataRetrieve
  646. *
  647. * @brief Process AF Data Retrieve command to incrementally read out a very large
  648. * incoming AF message.
  649. *
  650. * input parameters
  651. *
  652. * @param pBuf - pointer to the received buffer
  653. *
  654. * output parameters
  655. *
  656. * @param rtrn - AF-Status of the operation.
  657. *
  658. * @return None.
  659. **************************************************************************************************
  660. */
  661. void MT_AfDataRetrieve(uint8 *pBuf)
  662. {
  663. #define MT_AF_RTV_HDR_SZ 2
  664. uint32 timestamp;
  665. mtAfInMsgList_t *pPrev, *pItem = pMtAfInMsgList;
  666. uint8 rtrn = afStatus_FAILED;
  667. uint8 len = 0;
  668. pBuf += MT_RPC_FRAME_HDR_SZ;
  669. timestamp = BUILD_UINT32(pBuf[0], pBuf[1], pBuf[2], pBuf[3]);
  670. while (pItem != NULL)
  671. {
  672. pPrev = pItem;
  673. if (pItem->timestamp == timestamp)
  674. {
  675. break;
  676. }
  677. pItem = pItem->next;
  678. }
  679. if (pItem != NULL)
  680. {
  681. uint16 idx;
  682. uint8 *pRsp;
  683. pBuf += 4;
  684. idx = BUILD_UINT16(pBuf[0], pBuf[1]);
  685. len = pBuf[2];
  686. if (len == 0) // Indication to delete the afIncomingMSGPacket.
  687. {
  688. if (pMtAfInMsgList == pItem)
  689. {
  690. pMtAfInMsgList = pItem->next;
  691. }
  692. else
  693. {
  694. pPrev->next = pItem->next;
  695. }
  696. (void)osal_mem_free(pItem);
  697. rtrn = afStatus_SUCCESS;
  698. }
  699. else if ((pRsp = osal_mem_alloc(len + MT_AF_RTV_HDR_SZ)) == NULL)
  700. {
  701. rtrn = afStatus_MEM_FAIL;
  702. len = 0;
  703. }
  704. else
  705. {
  706. pRsp[0] = ZSuccess;
  707. pRsp[1] = len;
  708. (void)osal_memcpy(pRsp + MT_AF_RTV_HDR_SZ, pItem->data+idx, len);
  709. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_AF),
  710. MT_AF_DATA_RETRIEVE, len + MT_AF_RTV_HDR_SZ, pRsp);
  711. (void)osal_mem_free(pRsp);
  712. return;
  713. }
  714. }
  715. pBuf[0] = rtrn;
  716. pBuf[1] = len;
  717. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_AF),
  718. MT_AF_DATA_RETRIEVE, MT_AF_RTV_HDR_SZ, pBuf);
  719. }
  720. /**************************************************************************************************
  721. * @fn MT_AfDataStore
  722. *
  723. * @brief Process AF Data Store command to incrementally store the data buffer for very large
  724. * outgoing AF message.
  725. *
  726. * input parameters
  727. *
  728. * @param pBuf - pointer to the received buffer
  729. *
  730. * output parameters
  731. *
  732. * @param rtrn - AF-Status of the operation.
  733. *
  734. * @return None.
  735. **************************************************************************************************
  736. */
  737. void MT_AfDataStore(uint8 *pBuf)
  738. {
  739. uint16 idx;
  740. uint8 len, rtrn = afStatus_FAILED;
  741. pBuf += MT_RPC_FRAME_HDR_SZ;
  742. idx = BUILD_UINT16(pBuf[0], pBuf[1]);
  743. len = pBuf[2];
  744. pBuf += 3;
  745. if (pMtAfDataReq == NULL)
  746. {
  747. rtrn = afStatus_MEM_FAIL;
  748. }
  749. else if (len == 0) // Indication to send the message.
  750. {
  751. rtrn = AF_DataRequest(&(pMtAfDataReq->dstAddr), pMtAfDataReq->epDesc, pMtAfDataReq->cId,
  752. pMtAfDataReq->dataLen, pMtAfDataReq->data,
  753. &(pMtAfDataReq->transId), pMtAfDataReq->txOpts, pMtAfDataReq->radius);
  754. (void)osal_mem_free(pMtAfDataReq);
  755. pMtAfDataReq = NULL;
  756. }
  757. else
  758. {
  759. (void)osal_memcpy(pMtAfDataReq->data+idx, pBuf, len);
  760. rtrn = afStatus_SUCCESS;
  761. }
  762. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_AF),
  763. MT_AF_DATA_STORE, 1, &rtrn);
  764. }
  765. /**************************************************************************************************
  766. * @fn MT_AfAPSF_ConfigSet
  767. *
  768. * @brief This function is the MT proxy for afAPSF_ConfigSet().
  769. *
  770. * input parameters
  771. *
  772. * @param pBuf - Pointer to the received buffer.
  773. *
  774. * output parameters
  775. *
  776. * None.
  777. *
  778. * @return None.
  779. */
  780. static void MT_AfAPSF_ConfigSet(uint8 *pBuf)
  781. {
  782. afAPSF_Config_t cfg = { pBuf[MT_RPC_POS_DAT0+1], pBuf[MT_RPC_POS_DAT0+2] };
  783. afStatus_t rtrn = afAPSF_ConfigSet(pBuf[MT_RPC_POS_DAT0], &cfg);
  784. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_AF),
  785. MT_AF_APSF_CONFIG_SET, 1, (uint8 *)&rtrn);
  786. }
  787. /***************************************************************************************************
  788. ***************************************************************************************************/