MT_AF.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /**************************************************************************************************
  2. Filename: MT_AF.c
  3. Revised: $Date: 2008-10-10 09:56:24 -0700 (Fri, 10 Oct 2008) $
  4. Revision: $Revision: 18257 $
  5. Description: MonitorTest functions for the AF layer.
  6. Copyright 2007 Texas Instruments Incorporated. All rights reserved.
  7. IMPORTANT: Your use of this Software is limited to those specific rights
  8. granted under the terms of a software license agreement between the user
  9. who downloaded the software, his/her employer (which must be your employer)
  10. and Texas Instruments Incorporated (the "License"). You may not use this
  11. Software unless you agree to abide by the terms of the License. The License
  12. limits your use, and you acknowledge, that the Software may not be modified,
  13. copied or distributed unless embedded on a Texas Instruments microcontroller
  14. or used solely and exclusively in conjunction with a Texas Instruments radio
  15. frequency transceiver, which is integrated into your product. Other than for
  16. the foregoing purpose, you may not use, reproduce, copy, prepare derivative
  17. works of, modify, distribute, perform, display or sell this Software and/or
  18. its documentation for any purpose.
  19. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  20. PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  21. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  22. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  23. TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
  24. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  25. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  26. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  27. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  28. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  29. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  30. Should you have any questions regarding your right to use this Software,
  31. contact Texas Instruments Incorporated at www.TI.com.
  32. **************************************************************************************************/
  33. /***************************************************************************************************
  34. * INCLUDES
  35. ***************************************************************************************************/
  36. #include "ZComDef.h"
  37. #include "OSAL.h"
  38. #include "MT.h"
  39. #include "MT_AF.h"
  40. #include "nwk.h"
  41. #include "OnBoard.h"
  42. #include "MT_UART.h"
  43. /***************************************************************************************************
  44. * GLOBAL VARIABLES
  45. ***************************************************************************************************/
  46. #if defined ( MT_AF_CB_FUNC )
  47. uint16 _afCallbackSub;
  48. #endif
  49. /***************************************************************************************************
  50. * LOCAL FUNCTIONS
  51. ***************************************************************************************************/
  52. void MT_AfRegister(uint8 *pBuf);
  53. void MT_AfDataRequest(uint8 *pBuf);
  54. /***************************************************************************************************
  55. * @fn MT_afCommandProcessing
  56. *
  57. * @brief Process all the AF commands that are issued by test tool
  58. *
  59. * @param pBuf - pointer to the received buffer
  60. *
  61. * @return status
  62. ***************************************************************************************************/
  63. uint8 MT_AfCommandProcessing(uint8 *pBuf)
  64. {
  65. uint8 status = MT_RPC_SUCCESS;
  66. switch (pBuf[MT_RPC_POS_CMD1])
  67. {
  68. case MT_AF_REGISTER:
  69. MT_AfRegister(pBuf);
  70. break;
  71. case MT_AF_DATA_REQUEST:
  72. MT_AfDataRequest(pBuf);
  73. break;
  74. default:
  75. status = MT_RPC_ERR_COMMAND_ID;
  76. break;
  77. }
  78. return status;
  79. }
  80. /***************************************************************************************************
  81. * @fn MT_AfRegister
  82. *
  83. * @brief Process AF Register command
  84. *
  85. * @param pBuf - pointer to the received buffer
  86. *
  87. * @return none
  88. ***************************************************************************************************/
  89. void MT_AfRegister(uint8 *pBuf)
  90. {
  91. uint8 cmdId;
  92. uint8 retValue = ZMemError;
  93. endPointDesc_t *epDesc;
  94. /* parse header */
  95. cmdId = pBuf[MT_RPC_POS_CMD1];
  96. pBuf += MT_RPC_FRAME_HDR_SZ;
  97. epDesc = (endPointDesc_t *)osal_mem_alloc(sizeof(endPointDesc_t));
  98. if ( epDesc )
  99. {
  100. epDesc->task_id = &MT_TaskID;
  101. retValue = MT_BuildEndpointDesc( pBuf, epDesc );
  102. if ( retValue == ZSuccess )
  103. {
  104. retValue = afRegister( epDesc );
  105. }
  106. if ( retValue != ZSuccess )
  107. {
  108. osal_mem_free( epDesc );
  109. }
  110. }
  111. /* Build and send back the response */
  112. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_AF), cmdId, 1, &retValue);
  113. }
  114. /***************************************************************************************************
  115. * @fn MT_AfDataRequest
  116. *
  117. * @brief Process AF Register command
  118. *
  119. * @param pBuf - pointer to the received buffer
  120. *
  121. * @return none
  122. ***************************************************************************************************/
  123. void MT_AfDataRequest(uint8 *pBuf)
  124. {
  125. uint8 cmdId, tempLen = 0;
  126. uint8 retValue = ZFailure;
  127. endPointDesc_t *epDesc;
  128. byte transId;
  129. afAddrType_t dstAddr;
  130. cId_t cId;
  131. byte txOpts, radius, srcEP;
  132. /* parse header */
  133. cmdId = pBuf[MT_RPC_POS_CMD1];
  134. pBuf += MT_RPC_FRAME_HDR_SZ;
  135. /* Destination address */
  136. dstAddr.addrMode = afAddr16Bit;
  137. dstAddr.addr.shortAddr = BUILD_UINT16(pBuf[0], pBuf[1]);
  138. pBuf += 2;
  139. /* Destination endpoint */
  140. dstAddr.endPoint = *pBuf++;
  141. /* Source endpoint */
  142. srcEP = *pBuf++;
  143. epDesc = afFindEndPointDesc( srcEP );
  144. /* ClusterId */
  145. cId = BUILD_UINT16(pBuf[0], pBuf[1]);
  146. pBuf +=2;
  147. /* TransId */
  148. transId = *pBuf++;
  149. /* TxOption */
  150. txOpts = *pBuf++;
  151. /* Radius */
  152. radius = *pBuf++;
  153. /* Length */
  154. tempLen = *pBuf++;
  155. if ( epDesc == NULL )
  156. {
  157. retValue = afStatus_INVALID_PARAMETER;
  158. }
  159. else
  160. {
  161. retValue = AF_DataRequest( &dstAddr, epDesc, cId, tempLen, pBuf, &transId, txOpts, radius );
  162. }
  163. /* Build and send back the response */
  164. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_AF), cmdId, 1, &retValue);
  165. }
  166. /***************************************************************************************************
  167. * @fn MT_AfDataConfirm
  168. *
  169. * @brief Process
  170. *
  171. * @param pBuf - pointer to the received buffer
  172. *
  173. * @return none
  174. ***************************************************************************************************/
  175. void MT_AfDataConfirm(afDataConfirm_t *pMsg)
  176. {
  177. uint8 retArray[3];
  178. retArray[0] = pMsg->hdr.status;
  179. retArray[1] = pMsg->endpoint;
  180. retArray[2] = pMsg->transID;
  181. /* Build and send back the response */
  182. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_AREQ | (uint8)MT_RPC_SYS_AF), MT_AF_DATA_CONFIRM, 3, retArray);
  183. }
  184. /***************************************************************************************************
  185. * @fn MT_AfIncomingMsg
  186. *
  187. * @brief Process the callback subscription for AF Incoming data.
  188. *
  189. * @param pkt - Incoming AF data.
  190. *
  191. * @return none
  192. ***************************************************************************************************/
  193. void MT_AfIncomingMsg(afIncomingMSGPacket_t *pMsg)
  194. {
  195. uint8 respLen; /* Length of the whole response packet */
  196. uint8 dataLen; /* Length of the data section in the response packet */
  197. uint8 *respPtr, *tempPtr;
  198. /* dataLen */
  199. dataLen = pMsg->cmd.DataLength;
  200. /* respLen */
  201. respLen = 17 + dataLen;
  202. /* Allocate memory for the response packet */
  203. respPtr = osal_mem_alloc(respLen);
  204. if (!respPtr)
  205. {
  206. return;
  207. }
  208. tempPtr = respPtr;
  209. /* Fill in the data */
  210. /* Group ID */
  211. *tempPtr++ = LO_UINT16(pMsg->groupId);
  212. *tempPtr++ = HI_UINT16(pMsg->groupId);
  213. /* Cluster ID */
  214. *tempPtr++ = LO_UINT16(pMsg->clusterId);
  215. *tempPtr++ = HI_UINT16(pMsg->clusterId);
  216. /* Source Address */
  217. *tempPtr++ = LO_UINT16(pMsg->srcAddr.addr.shortAddr);
  218. *tempPtr++ = HI_UINT16(pMsg->srcAddr.addr.shortAddr);
  219. /* Source EP */
  220. *tempPtr++ = pMsg->srcAddr.endPoint;
  221. /* Destination EP */
  222. *tempPtr++ = pMsg->endPoint;
  223. /* WasBroadCast */
  224. *tempPtr++ = pMsg->wasBroadcast;
  225. /* LinkQuality */
  226. *tempPtr++ = pMsg->LinkQuality;
  227. /* SecurityUse */
  228. *tempPtr++ = pMsg->SecurityUse;
  229. /* Timestamp */
  230. *tempPtr++ = BREAK_UINT32(pMsg->timestamp, 0);
  231. *tempPtr++ = BREAK_UINT32(pMsg->timestamp, 1);
  232. *tempPtr++ = BREAK_UINT32(pMsg->timestamp, 2);
  233. *tempPtr++ = BREAK_UINT32(pMsg->timestamp, 3);
  234. /* Transmit Sequence Number */
  235. *tempPtr++ = pMsg->cmd.TransSeqNumber;
  236. /* Data Length */
  237. *tempPtr++ = dataLen;
  238. /* Data */
  239. if (dataLen)
  240. {
  241. osal_memcpy(tempPtr, pMsg->cmd.Data, dataLen);
  242. }
  243. /* Build and send back the response */
  244. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_AREQ | (uint8)MT_RPC_SYS_AF), MT_AF_INCOMING_MSG, respLen, respPtr );
  245. /* Free memory */
  246. osal_mem_free(respPtr);
  247. }
  248. /***************************************************************************************************
  249. ***************************************************************************************************/