MT_MAC.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779
  1. /**************************************************************************************************
  2. Filename: MT_MAC.c
  3. Revised: $Date: 2011-03-25 18:16:38 -0700 (Fri, 25 Mar 2011) $
  4. Revision: $Revision: 25528 $
  5. Description: MonitorTest functions for the MAC layer.
  6. Copyright 2004-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. #if defined (MT_MAC_FUNC) || defined (MT_MAC_CB_FUNC) //MAC commands
  34. /***************************************************************************************************
  35. * INCLUDES
  36. ***************************************************************************************************/
  37. #include "ZComDef.h"
  38. #include "OSAL.h"
  39. #include "MT.h"
  40. #include "MT_UART.h"
  41. #include "ZMAC.h"
  42. #include "MT_MAC.h"
  43. #if !defined( WIN32 )
  44. #include "OnBoard.h"
  45. #endif
  46. /* MAC radio */
  47. #include "mac_radio_defs.h"
  48. /* Hal */
  49. #include "hal_uart.h"
  50. #ifdef MAC_SECURITY
  51. #include "mac_security_pib.h"
  52. #endif
  53. /***************************************************************************************************
  54. * MACROS
  55. ***************************************************************************************************/
  56. /* The length in bytes of the pending address fields in the beacon */
  57. #define MT_MAC_PEND_LEN(pendAddrSpec) ((((pendAddrSpec) & 0x07) * 2) + \
  58. ((((pendAddrSpec) & 0x70) >> 4) * 8))
  59. /* This matches the value used by nwk */
  60. #define MT_MAC_ED_SCAN_MAXCHANNELS 27
  61. /* Maximum size of pending address spec in beacon notify ind */
  62. #define MT_MAC_PEND_LEN_MAX 32
  63. /* Maximum size of the payload SDU in beacon notify ind */
  64. #define MT_MAC_SDU_LEN_MAX 32
  65. /* Maximum length of scan result in bytes */
  66. #define MT_MAC_SCAN_RESULT_LEN_MAX 32
  67. /* Maximum size of beacon payload */
  68. #define MT_MAC_BEACON_PAYLOAD_MAX 16
  69. /***************************************************************************************************
  70. * CONSTANTS
  71. ***************************************************************************************************/
  72. #define DEFAULT_NSDU_HANDLE 0x00
  73. #define MT_MAC_LEN_ASSOCIATE_IND 0x14 /* Associate Indication */
  74. #define MT_MAC_LEN_ASSOCIATE_CNF 0x0E /* Associate Confirmation */
  75. #define MT_MAC_LEN_DISASSOCIATE_IND 0x14 /* Disassociate Indication */
  76. #define MT_MAC_LEN_DISASSOCIATE_CNF 0x0c /* Disassociate Confirmation */
  77. #define MT_MAC_LEN_BEACON_NOTIFY_IND 0x63 /* Beacon Notification */
  78. #define MT_MAC_LEN_ORPHAN_IND 0x13 /* Orphan Indication */
  79. #define MT_MAC_LEN_SCAN_CNF 0x09 /* Scan Confirmation */
  80. #define MT_MAC_LEN_SYNC_LOSS_IND 0x10 /* Sync Loss Indication */
  81. #define MT_MAC_LEN_COMM_STATUS_IND 0x21 /* Comm Status Indication */
  82. #define MT_MAC_LEN_DATA_CNF 0x08 /* Data Confirmation */
  83. #define MT_MAC_LEN_DATA_IND 0x2C /* Data Indication */
  84. #define MT_MAC_LEN_PURGE_CNF 0x02 /* Purge Confirmation */
  85. /***************************************************************************************************
  86. * GLOBAL VARIABLES
  87. ***************************************************************************************************/
  88. uint16 _macCallbackSub;
  89. /* storage for MAC beacon payload */
  90. static uint8 mtMacBeaconPayload[MT_MAC_BEACON_PAYLOAD_MAX];
  91. /***************************************************************************************************
  92. * LOCAL FUNCTIONS
  93. ***************************************************************************************************/
  94. static void MT_MacSpi2Sec( ZMacSec_t *pSec, uint8 *pSrc );
  95. static void MT_MacSpi2Addr( zAddrType_t *pDst, uint8 *pSrc );
  96. static void MT_MacAddr2Spi( uint8 *pDst, zAddrType_t *pSrc );
  97. static void MT_MacExtCpy( uint8 *pDst, uint8 *pSrc );
  98. static void MT_MacRevExtCpy( uint8 *pDst, uint8 *pSrc );
  99. void MT_MacResetReq(uint8 *pBuf);
  100. void MT_MacInit(uint8 *pBuf);
  101. void MT_MacStartReq(uint8 *pBuf);
  102. void MT_MacSyncReq(uint8 *pBuf);
  103. void MT_MacDataReq(uint8 *pBuf);
  104. void MT_MacAssociateReq(uint8 *pBuf);
  105. void MT_MacDisassociateReq(uint8 *pBuf);
  106. void MT_MacGetReq(uint8 *pBuf);
  107. void MT_MacSetReq(uint8 *pBuf);
  108. #ifdef MAC_SECURITY
  109. void MT_MacSecurityGetReq(uint8 *pBuf);
  110. void MT_MacSecuritySetReq(uint8 *pBuf);
  111. #endif
  112. void MT_MacScanReq(uint8 * pBuf);
  113. void MT_MacPollReq(uint8 *pBuf);
  114. void MT_MacPurgeReq(uint8 *pBuf);
  115. void MT_MacSetRxGainReq(uint8 *pBuf);
  116. void MT_MacAssociateRsp(uint8 *pBuf);
  117. void MT_MacOrphanRsp(uint8 *pBuf);
  118. /***************************************************************************************************
  119. * @fn MT_MacCommandProcessing
  120. *
  121. * @brief Process all the MAC commands that are issued by test tool
  122. *
  123. * @param pBuf - pointer to the msg buffer
  124. *
  125. * | LEN | CMD0 | CMD1 | DATA |
  126. * | 1 | 1 | 1 | 0-255 |
  127. *
  128. * @return void
  129. ***************************************************************************************************/
  130. uint8 MT_MacCommandProcessing (uint8 *pBuf)
  131. {
  132. uint8 status = MT_RPC_SUCCESS;
  133. switch (pBuf[MT_RPC_POS_CMD1])
  134. {
  135. case MT_MAC_RESET_REQ:
  136. MT_MacResetReq(pBuf);
  137. break;
  138. case MT_MAC_INIT:
  139. MT_MacInit(pBuf);
  140. break;
  141. case MT_MAC_START_REQ:
  142. MT_MacStartReq(pBuf);
  143. break;
  144. case MT_MAC_SYNC_REQ:
  145. MT_MacSyncReq(pBuf);
  146. break;
  147. case MT_MAC_DATA_REQ:
  148. MT_MacDataReq(pBuf);
  149. break;
  150. case MT_MAC_ASSOCIATE_REQ:
  151. MT_MacAssociateReq(pBuf);
  152. break;
  153. case MT_MAC_DISASSOCIATE_REQ:
  154. MT_MacDisassociateReq(pBuf);
  155. break;
  156. case MT_MAC_GET_REQ:
  157. MT_MacGetReq(pBuf);
  158. break;
  159. case MT_MAC_SET_REQ:
  160. MT_MacSetReq(pBuf);
  161. break;
  162. #ifdef MAC_SECURITY
  163. case MT_MAC_SECURITY_GET_REQ:
  164. MT_MacSecurityGetReq(pBuf);
  165. break;
  166. case MT_MAC_SECURITY_SET_REQ:
  167. MT_MacSecuritySetReq(pBuf);
  168. break;
  169. #endif /* MAC_SECURITY */
  170. case MT_MAC_GTS_REQ:
  171. /* Not supported */
  172. break;
  173. case MT_MAC_SCAN_REQ:
  174. MT_MacScanReq(pBuf);
  175. break;
  176. case MT_MAC_POLL_REQ:
  177. MT_MacPollReq(pBuf);
  178. break;
  179. case MT_MAC_PURGE_REQ:
  180. MT_MacPurgeReq(pBuf);
  181. break;
  182. case MT_MAC_SET_RX_GAIN_REQ:
  183. MT_MacSetRxGainReq(pBuf);
  184. break;
  185. case MT_MAC_ASSOCIATE_RSP:
  186. MT_MacAssociateRsp(pBuf);
  187. break;
  188. case MT_MAC_ORPHAN_RSP:
  189. MT_MacOrphanRsp(pBuf);
  190. break;
  191. default:
  192. status = MT_RPC_ERR_COMMAND_ID;
  193. break;
  194. }
  195. return status;
  196. }
  197. /***************************************************************************************************
  198. * @fn MT_MacReset
  199. *
  200. * @brief Process MAC Reset command that are issued by test tool
  201. *
  202. * @param pBuf - Buffer contains the data
  203. *
  204. * @return void
  205. ***************************************************************************************************/
  206. void MT_MacResetReq(uint8 *pBuf)
  207. {
  208. uint8 retValue, cmdId;
  209. /* Parse header */
  210. cmdId = pBuf[MT_RPC_POS_CMD1];
  211. pBuf += MT_RPC_FRAME_HDR_SZ;
  212. retValue = ZMacReset(*pBuf);
  213. /* Build and send back the response */
  214. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_MAC), cmdId, 1, &retValue );
  215. }
  216. /***************************************************************************************************
  217. * @fn MT_MacInit
  218. *
  219. * @brief Process Mac Init command that are issued by test tool
  220. *
  221. * @param pBuf - Buffer contains the data
  222. *
  223. * @return void
  224. ***************************************************************************************************/
  225. void MT_MacInit(uint8 *pBuf)
  226. {
  227. uint8 retValue, cmdId;
  228. /* Parse header */
  229. cmdId = pBuf[MT_RPC_POS_CMD1];
  230. pBuf += MT_RPC_FRAME_HDR_SZ;
  231. retValue = ZMacInit();
  232. /* Build and send back the response */
  233. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_MAC), cmdId, 1, &retValue );
  234. }
  235. /***************************************************************************************************
  236. * @fn MT_MacStart
  237. *
  238. * @brief Process MAC Start command that are issued by test tool
  239. *
  240. * @param pBuf - Buffer contains the data
  241. *
  242. * @return void
  243. ***************************************************************************************************/
  244. void MT_MacStartReq(uint8 *pBuf)
  245. {
  246. uint8 retValue, cmdId;
  247. #ifdef RTR_NWK
  248. ZMacStartReq_t startReq;
  249. #endif
  250. /* Parse header */
  251. cmdId = pBuf[MT_RPC_POS_CMD1];
  252. pBuf += MT_RPC_FRAME_HDR_SZ;
  253. #ifdef RTR_NWK
  254. /* StartTime */
  255. startReq.StartTime = BUILD_UINT32 (pBuf[0], pBuf[1], pBuf[2], pBuf[3]);
  256. pBuf += 4;
  257. /* PanID */
  258. startReq.PANID = BUILD_UINT16( pBuf[0] , pBuf[1] );
  259. pBuf += 2;
  260. /* Fill in other fields sequentially incrementing the pointer*/
  261. startReq.LogicalChannel = *pBuf++;
  262. startReq.ChannelPage = *pBuf++;
  263. startReq.BeaconOrder = *pBuf++;
  264. startReq.SuperframeOrder = *pBuf++;
  265. startReq.PANCoordinator = *pBuf++;
  266. startReq.BatteryLifeExt = *pBuf++;
  267. startReq.CoordRealignment = *pBuf++;
  268. /* Realign Security Information */
  269. MT_MacSpi2Sec( &startReq.RealignSec, pBuf );
  270. pBuf += ZTEST_DEFAULT_SEC_LEN;
  271. /* Beacon Security Information */
  272. MT_MacSpi2Sec( &startReq.BeaconSec, pBuf );
  273. /* Call corresponding ZMAC function */
  274. retValue = ZMacStartReq( &startReq );
  275. #else
  276. retValue = ZMacDenied;
  277. #endif
  278. /* Build and send back the response */
  279. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_MAC), cmdId, 1, &retValue );
  280. }
  281. /***************************************************************************************************
  282. * @fn MT_MacSync
  283. *
  284. * @brief Process MAC Sync command that are issued by test tool
  285. *
  286. * @param pBuf - Buffer contains the data
  287. *
  288. * @return void
  289. ***************************************************************************************************/
  290. void MT_MacSyncReq(uint8 *pBuf)
  291. {
  292. uint8 retValue, cmdId;
  293. ZMacSyncReq_t syncReq;
  294. /* Parse header */
  295. cmdId = pBuf[MT_RPC_POS_CMD1];
  296. pBuf += MT_RPC_FRAME_HDR_SZ;
  297. /* LogicalChannel */
  298. syncReq.LogicalChannel = *pBuf++;
  299. /* ChannelPage */
  300. syncReq.ChannelPage = *pBuf++;
  301. /* TrackBeacon */
  302. syncReq.TrackBeacon = *pBuf;
  303. /* Call corresponding ZMAC function */
  304. retValue = ZMacSyncReq( &syncReq );
  305. /* Build and send back the response */
  306. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_MAC), cmdId, 1, &retValue );
  307. }
  308. /***************************************************************************************************
  309. * @fn MT_MacDataReq
  310. *
  311. * @brief Process MAC Data Request command that are issued by test tool
  312. *
  313. * @param pBuf - Buffer contains the data
  314. *
  315. * @return void
  316. ***************************************************************************************************/
  317. void MT_MacDataReq(uint8 *pBuf)
  318. {
  319. uint8 retValue, cmdId;
  320. ZMacDataReq_t dataReq;
  321. /* Parse header */
  322. cmdId = pBuf[MT_RPC_POS_CMD1];
  323. pBuf += MT_RPC_FRAME_HDR_SZ;
  324. /* Destination address mode */
  325. dataReq.DstAddr.addrMode = *pBuf++;
  326. /* Destination address */
  327. MT_MacSpi2Addr( &dataReq.DstAddr, pBuf);
  328. pBuf += Z_EXTADDR_LEN;
  329. /* Destination Pan ID */
  330. dataReq.DstPANId = BUILD_UINT16( pBuf[0] , pBuf[1] );
  331. pBuf += 2;
  332. /* Source address mode */
  333. dataReq.SrcAddrMode = *pBuf++;
  334. /* Handle */
  335. dataReq.Handle = *pBuf++;
  336. /* TxOptions */
  337. dataReq.TxOptions = *pBuf++;
  338. /* Channel */
  339. dataReq.Channel = *pBuf++;
  340. /* Power */
  341. dataReq.Power = *pBuf++;
  342. /* Data Security */
  343. MT_MacSpi2Sec( &dataReq.Sec, pBuf );
  344. pBuf += ZTEST_DEFAULT_SEC_LEN;
  345. /* Data length */
  346. dataReq.msduLength = *pBuf++;
  347. /* Data - Just pass the pointer to the structure */
  348. dataReq.msdu = pBuf;
  349. /* Call corresponding ZMAC function */
  350. retValue = ZMacDataReq( &dataReq );
  351. /* Build and send back the response */
  352. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_MAC), cmdId, 1, &retValue );
  353. }
  354. /***************************************************************************************************
  355. * @fn MT_MacAssociateReq
  356. *
  357. * @brief Process MAC Get Request command that are issued by test tool
  358. *
  359. * @param pBuf - Buffer contains the data
  360. *
  361. * @return void
  362. ***************************************************************************************************/
  363. void MT_MacAssociateReq(uint8 *pBuf)
  364. {
  365. uint8 retValue, cmdId;
  366. ZMacAssociateReq_t assocReq;
  367. /* Parse header */
  368. cmdId = pBuf[MT_RPC_POS_CMD1];
  369. pBuf += MT_RPC_FRAME_HDR_SZ;
  370. /* Logical Channel */
  371. assocReq.LogicalChannel = *pBuf++;
  372. /* Channel Page */
  373. assocReq.ChannelPage = *pBuf++;
  374. /* Address Mode */
  375. assocReq.CoordAddress.addrMode = *pBuf++;
  376. /* Coordinator Address, address mode must be set at this point */
  377. MT_MacSpi2Addr( &assocReq.CoordAddress, pBuf );
  378. pBuf += Z_EXTADDR_LEN;
  379. /* Coordinator PanID */
  380. assocReq.CoordPANId = BUILD_UINT16( pBuf[0] , pBuf[1] );
  381. pBuf += 2;
  382. /* Capability information */
  383. assocReq.CapabilityFlags = *pBuf++;
  384. /* Security Information */
  385. MT_MacSpi2Sec( &assocReq.Sec, pBuf );
  386. /* Call corresponding ZMAC function */
  387. retValue = ZMacAssociateReq( &assocReq );
  388. /* Build and send back the response */
  389. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_MAC), cmdId, 1, &retValue );
  390. }
  391. /***************************************************************************************************
  392. * @fn MT_MacDisassociateReq
  393. *
  394. * @brief Process MAC Get Request command that are issued by test tool
  395. *
  396. * @param pBuf - Buffer contains the data
  397. *
  398. * @return void
  399. ***************************************************************************************************/
  400. void MT_MacDisassociateReq(uint8 *pBuf)
  401. {
  402. uint8 retValue, cmdId;
  403. ZMacDisassociateReq_t disassocReq;
  404. /* Parse header */
  405. cmdId = pBuf[MT_RPC_POS_CMD1];
  406. pBuf += MT_RPC_FRAME_HDR_SZ;
  407. /* Device address mode */
  408. disassocReq.DeviceAddress.addrMode = *pBuf++;
  409. /* Device address - Device address mode have to be set to use this function*/
  410. MT_MacSpi2Addr( &disassocReq.DeviceAddress, pBuf);
  411. pBuf += Z_EXTADDR_LEN;
  412. /* Pan ID */
  413. disassocReq.DevicePanId = BUILD_UINT16( pBuf[0] , pBuf[1] );
  414. pBuf += 2;
  415. /* Disassociate reason */
  416. disassocReq.DisassociateReason = *pBuf++;
  417. /* TxIndirect */
  418. disassocReq.TxIndirect = *pBuf++;
  419. /* Security Information */
  420. MT_MacSpi2Sec( &disassocReq.Sec, pBuf );
  421. /* Call corresponding ZMAC function */
  422. retValue = ZMacDisassociateReq( &disassocReq );
  423. /* Build and send back the response */
  424. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_MAC), cmdId, 1, &retValue );
  425. }
  426. /***************************************************************************************************
  427. * @fn MT_MacGetReq
  428. *
  429. * @brief Process MAC Get Request command that are issued by test tool
  430. *
  431. * @param pBuf - Buffer contains the data
  432. *
  433. * @return void
  434. ***************************************************************************************************/
  435. void MT_MacGetReq(uint8 *pBuf)
  436. {
  437. uint8 respLen, cmdId, attr;
  438. uint8 *pRetBuf;
  439. /* Parse header */
  440. cmdId = pBuf[MT_RPC_POS_CMD1];
  441. pBuf += MT_RPC_FRAME_HDR_SZ;
  442. /* Response length is 16 bytes + 1 byte status */
  443. respLen = ZTEST_DEFAULT_PARAM_LEN;
  444. /* Allocate */
  445. pRetBuf = osal_mem_alloc(respLen);
  446. /* Attribute to be read */
  447. attr = *pBuf;
  448. if (pRetBuf)
  449. {
  450. /* Zero everything */
  451. osal_memset(pRetBuf, 0, respLen);
  452. /* Default to ZFailure */
  453. pRetBuf[0] = ZFailure;
  454. /* Read the pib value */
  455. pRetBuf[0] = ZMacGetReq(attr, &pRetBuf[1]);
  456. /* Build and send back the response */
  457. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_MAC), cmdId, respLen, pRetBuf );
  458. /* Deallocate */
  459. osal_mem_free(pRetBuf);
  460. }
  461. }
  462. /***************************************************************************************************
  463. * @fn MT_MacSetReq
  464. *
  465. * @brief Process MAC Set Req command that are issued by test tool
  466. *
  467. * @param pBuf - Buffer contains the data
  468. *
  469. * @return void
  470. ***************************************************************************************************/
  471. void MT_MacSetReq(uint8 *pBuf)
  472. {
  473. uint8 retValue, cmdId, attr;
  474. /* Parse header */
  475. cmdId = pBuf[MT_RPC_POS_CMD1];
  476. pBuf += MT_RPC_FRAME_HDR_SZ;
  477. /*
  478. In the data field of 'msg', the first byte is the attribute and remainder
  479. is the attribute value. So the pointer 'pBuf' points directly to the attribute.
  480. The value of the attribute is from the next byte position
  481. */
  482. attr = *pBuf;
  483. /* special case for beacon payload */
  484. if ( attr == ZMacBeaconMSDU )
  485. {
  486. osal_memcpy( mtMacBeaconPayload, pBuf + 1, MT_MAC_BEACON_PAYLOAD_MAX );
  487. retValue = ZMacSetReq( (ZMacAttributes_t)attr , (byte *) &mtMacBeaconPayload );
  488. }
  489. else
  490. {
  491. retValue = ZMacSetReq( (ZMacAttributes_t)attr , pBuf + 1 );
  492. }
  493. /* Build and send back the response */
  494. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_MAC), cmdId, 1, &retValue );
  495. }
  496. #ifdef MAC_SECURITY
  497. /***************************************************************************************************
  498. * @fn MT_MacSecurityGetReq
  499. *
  500. * @brief Process MAC Security Get Request command that are issued by test tool
  501. *
  502. * @param pBuf - Buffer contains the data
  503. *
  504. * @return void
  505. ***************************************************************************************************/
  506. void MT_MacSecurityGetReq(uint8 *pBuf)
  507. {
  508. uint8 respLen, cmdId, attr;
  509. uint8 *pRetBuf;
  510. /* Parse header */
  511. cmdId = pBuf[MT_RPC_POS_CMD1];
  512. pBuf += MT_RPC_FRAME_HDR_SZ;
  513. /* Response length is 25 bytes + 2 bytes index + 1 byte status */
  514. respLen = ZTEST_DEFAULT_SEC_PARAM_LEN;
  515. /* Allocate */
  516. pRetBuf = osal_mem_alloc(respLen);
  517. /* Attribute to be read */
  518. attr = *pBuf++;
  519. if (pRetBuf)
  520. {
  521. /* Zero everything */
  522. osal_memset(pRetBuf, 0, respLen);
  523. switch (attr)
  524. {
  525. case MAC_KEY_ID_LOOKUP_ENTRY:
  526. case MAC_KEY_DEVICE_ENTRY:
  527. case MAC_KEY_USAGE_ENTRY:
  528. /* These security PIBs have two parameters */
  529. pRetBuf[1] = *pBuf++;
  530. pRetBuf[2] = *pBuf;
  531. break;
  532. case MAC_KEY_ENTRY:
  533. case MAC_DEVICE_ENTRY:
  534. case MAC_SECURITY_LEVEL_ENTRY:
  535. /* These security PIBs have one parameter */
  536. pRetBuf[1] = *pBuf;
  537. break;
  538. }
  539. /* Other MAC Security PIB items. Read the pib value */
  540. pRetBuf[0] = ZMacSecurityGetReq(attr, &pRetBuf[1]);
  541. /* Build and send back the response */
  542. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_MAC), cmdId, respLen, pRetBuf );
  543. /* Deallocate */
  544. osal_mem_free(pRetBuf);
  545. }
  546. }
  547. /***************************************************************************************************
  548. * @fn MT_MacSecuritySetReq
  549. *
  550. * @brief Process MAC Set Req command that are issued by test tool
  551. *
  552. * @param pBuf - Buffer contains the data
  553. *
  554. * @return void
  555. ***************************************************************************************************/
  556. void MT_MacSecuritySetReq(uint8 *pBuf)
  557. {
  558. uint8 cmdId, attr;
  559. uint8 retValue = ZMAC_SUCCESS;
  560. /* Parse header */
  561. cmdId = pBuf[MT_RPC_POS_CMD1];
  562. pBuf += MT_RPC_FRAME_HDR_SZ;
  563. /*
  564. In the data field of 'msg', the first byte is the attribute and remainder
  565. is the attribute value. So the pointer 'pBuf' points directly to the attribute.
  566. The value of the attribute is from the next byte position
  567. */
  568. attr = *pBuf++;
  569. if (attr == MAC_KEY_TABLE || attr == MAC_DEVICE_TABLE || attr == MAC_SECURITY_LEVEL_TABLE)
  570. {
  571. pBuf = NULL;
  572. }
  573. retValue = ZMacSecuritySetReq( (ZMacAttributes_t)attr , pBuf );
  574. /* Build and send back the response */
  575. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_MAC), cmdId, 1, &retValue );
  576. }
  577. #endif /* MAC_SECURITY */
  578. /***************************************************************************************************
  579. * @fn MT_MacScanReq
  580. *
  581. * @brief Process MAC Scan Req command that are issued by test tool
  582. *
  583. * @param pBuf - Buffer contains the data
  584. *
  585. * @return void
  586. ***************************************************************************************************/
  587. void MT_MacScanReq(uint8 * pBuf)
  588. {
  589. uint8 retValue, cmdId;
  590. ZMacScanReq_t scanReq;
  591. /* Parse header */
  592. cmdId = pBuf[MT_RPC_POS_CMD1];
  593. pBuf += MT_RPC_FRAME_HDR_SZ;
  594. /* ScanChannels is the 32-bit channel list */
  595. scanReq.ScanChannels = BUILD_UINT32 (pBuf[0], pBuf[1], pBuf[2], pBuf[3]);
  596. pBuf += 4;
  597. /* Fill in fields sequentially incrementing the pointer */
  598. scanReq.ScanType = *pBuf++;
  599. /* ScanDuration */
  600. scanReq.ScanDuration = *pBuf++;
  601. /* Channel Page */
  602. scanReq.ChannelPage = *pBuf++;
  603. /* MaxResults */
  604. scanReq.MaxResults = *pBuf++;
  605. /* Security Information */
  606. MT_MacSpi2Sec( &scanReq.Sec, pBuf );
  607. /* Call corresponding ZMAC function */
  608. retValue = ZMacScanReq( &scanReq );
  609. /* Build and send back the response */
  610. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_MAC), cmdId, 1, &retValue );
  611. }
  612. /***************************************************************************************************
  613. * @fn MT_MacPollReq
  614. *
  615. * @brief Process MAC Poll Req command that are issued by test tool
  616. *
  617. * @param pBuf - Buffer contains the data
  618. *
  619. * @return void
  620. ***************************************************************************************************/
  621. void MT_MacPollReq(uint8 *pBuf)
  622. {
  623. uint8 retValue, cmdId;
  624. ZMacPollReq_t pollReq;
  625. /* Parse header */
  626. cmdId = pBuf[MT_RPC_POS_CMD1];
  627. pBuf += MT_RPC_FRAME_HDR_SZ;
  628. /* Coordinator address mode */
  629. pollReq.CoordAddress.addrMode = *pBuf++;
  630. /* Coordinator address - Device address mode have to be set to use this function */
  631. MT_MacSpi2Addr( &pollReq.CoordAddress, pBuf);
  632. pBuf += Z_EXTADDR_LEN;
  633. /* Coordinator Pan ID */
  634. pollReq.CoordPanId = BUILD_UINT16(pBuf[0] , pBuf[1]);
  635. pBuf += 2;
  636. /* Security Information */
  637. MT_MacSpi2Sec(&pollReq.Sec, pBuf);
  638. /* Call corresponding ZMAC function */
  639. retValue = ZMacPollReq( &pollReq );
  640. /* Build and send back the response */
  641. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_MAC), cmdId, 1, &retValue );
  642. }
  643. /***************************************************************************************************
  644. * @fn MT_MacPurgeReq
  645. *
  646. * @brief Process MAC Purge Req command that are issued by test tool
  647. *
  648. * @param pBuf - Buffer contains the data
  649. *
  650. * @return void
  651. ***************************************************************************************************/
  652. void MT_MacPurgeReq(uint8 *pBuf)
  653. {
  654. uint8 retValue, cmdId;
  655. /* Parse header */
  656. cmdId = pBuf[MT_RPC_POS_CMD1];
  657. pBuf += MT_RPC_FRAME_HDR_SZ;
  658. /* First and only byte - MsduHandle */
  659. retValue = ZMacPurgeReq (*pBuf);
  660. /* Build and send back the response */
  661. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_MAC), cmdId, 1, &retValue );
  662. }
  663. /***************************************************************************************************
  664. * @fn MT_MacSetRxGainReq
  665. *
  666. * @brief Process MAC Rx Gain Req command that are issued by test tool
  667. *
  668. * @param pBuf - Buffer contains the data
  669. *
  670. * @return void
  671. ***************************************************************************************************/
  672. void MT_MacSetRxGainReq(uint8 *pBuf)
  673. {
  674. uint8 retValue, cmdId;
  675. /* Parse header */
  676. cmdId = pBuf[MT_RPC_POS_CMD1];
  677. pBuf += MT_RPC_FRAME_HDR_SZ;
  678. #ifdef HAL_PA_LNA
  679. /* Toggle PA/LNA mode */
  680. if (*pBuf)
  681. {
  682. HAL_PA_LNA_RX_HGM();
  683. }
  684. else
  685. {
  686. HAL_PA_LNA_RX_LGM();
  687. }
  688. retValue = ZSuccess;
  689. #else
  690. retValue = ZFailure;
  691. #endif
  692. /* Build and send back the response */
  693. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_MAC), cmdId, 1, &retValue );
  694. }
  695. /***************************************************************************************************
  696. * @fn MT_MacAssociateRsp
  697. *
  698. * @brief Process MAC Associate Rsp command that are issued by test tool
  699. *
  700. * @param pBuf - Buffer contains the data
  701. *
  702. * @return void
  703. ***************************************************************************************************/
  704. void MT_MacAssociateRsp(uint8 *pBuf)
  705. {
  706. uint8 retValue, cmdId;
  707. ZMacAssociateRsp_t assocRsp;
  708. /* Parse header */
  709. cmdId = pBuf[MT_RPC_POS_CMD1];
  710. pBuf += MT_RPC_FRAME_HDR_SZ;
  711. #ifdef RTR_NWK
  712. /* The address of the device requesting association */
  713. MT_MacExtCpy(assocRsp.DeviceAddress, pBuf);
  714. pBuf += Z_EXTADDR_LEN;
  715. /* The short address allocated to the (associated) device */
  716. assocRsp.AssocShortAddress = BUILD_UINT16(pBuf[0],pBuf[1]);
  717. pBuf += 2;
  718. /* Status of the association */
  719. assocRsp.Status = *pBuf++;
  720. /* Security Information */
  721. MT_MacSpi2Sec( &assocRsp.Sec, pBuf );
  722. /* Call corresponding ZMAC function */
  723. retValue = ZMacAssociateRsp( &assocRsp );
  724. #else
  725. retValue = ZMacDenied;
  726. #endif
  727. /* Build and send back the response */
  728. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_MAC), cmdId, 1, &retValue);
  729. }
  730. /***************************************************************************************************
  731. * @fn MT_MacOrphanRsp
  732. *
  733. * @brief Process MAC Orphan Response command that are issued by test tool
  734. *
  735. * @param pBuf - Buffer contains the data
  736. *
  737. * @return void
  738. ***************************************************************************************************/
  739. void MT_MacOrphanRsp(uint8 *pBuf)
  740. {
  741. uint8 retValue, cmdId;
  742. ZMacOrphanRsp_t orphanRsp;
  743. /* Parse header */
  744. cmdId = pBuf[MT_RPC_POS_CMD1];
  745. pBuf += MT_RPC_FRAME_HDR_SZ;
  746. /* Extended address of the device sending the notification */
  747. MT_MacRevExtCpy( orphanRsp.OrphanAddress, pBuf );
  748. pBuf += Z_EXTADDR_LEN;
  749. /* Short address of the orphan device */
  750. orphanRsp.ShortAddress = BUILD_UINT16( pBuf[0] , pBuf[1] );
  751. pBuf += 2;
  752. /* Associated member */
  753. orphanRsp.AssociatedMember = *pBuf++;
  754. /* Security Information */
  755. MT_MacSpi2Sec( &orphanRsp.Sec, pBuf );
  756. /* Call corresponding ZMAC function */
  757. retValue = ZMacOrphanRsp( &orphanRsp );
  758. /* Build and send back the response */
  759. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_MAC), cmdId, 1, &retValue);
  760. }
  761. #if defined ( MT_MAC_CB_FUNC )
  762. /***************************************************************************************************
  763. * @fn nwk_MTCallbackSubNwkSyncLossInd
  764. *
  765. * @brief Process the callback subscription for nwk_sync_loss_ind
  766. *
  767. * @param byte LossReason
  768. *
  769. * @return None
  770. ***************************************************************************************************/
  771. void nwk_MTCallbackSubNwkSyncLossInd( ZMacSyncLossInd_t *param )
  772. {
  773. uint8 respLen;
  774. uint8 *pRetBuf, *tp;
  775. respLen = MT_MAC_LEN_SYNC_LOSS_IND;
  776. pRetBuf = osal_mem_alloc (respLen);
  777. if ( pRetBuf )
  778. {
  779. tp = pRetBuf;
  780. /* Status - loss reason */
  781. *tp++ = param->hdr.Status;
  782. /* Pan Id */
  783. *tp++ = LO_UINT16( param->PANId );
  784. *tp++ = HI_UINT16( param->PANId );
  785. /* Logical Channel */
  786. *tp++ = param->LogicalChannel;
  787. /* Channel Page */
  788. *tp++ = param->ChannelPage;
  789. /* Security */
  790. MT_MacSpi2Sec ((ZMacSec_t *)tp, (uint8 *)&param->Sec);
  791. /* Build and send back the response */
  792. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_AREQ | (uint8)MT_RPC_SYS_MAC), MT_MAC_SYNC_LOSS_IND, respLen, pRetBuf);
  793. /* Deallocate */
  794. osal_mem_free(pRetBuf);
  795. }
  796. }
  797. /***************************************************************************************************
  798. * @fn nwk_MTCallbackSubNwkAssociateInd
  799. *
  800. * @brief Process the callback subscription for nwk_associate_ind
  801. *
  802. * @param pointer of type macnwk_associate_ind_t
  803. *
  804. * @return None
  805. ***************************************************************************************************/
  806. void nwk_MTCallbackSubNwkAssociateInd( ZMacAssociateInd_t *param )
  807. {
  808. uint8 respLen;
  809. uint8 *pRetBuf, *tp;
  810. respLen = MT_MAC_LEN_ASSOCIATE_IND;
  811. pRetBuf = osal_mem_alloc (respLen);
  812. if ( pRetBuf )
  813. {
  814. tp = pRetBuf;
  815. /* Extended address */
  816. MT_MacAddr2Spi(pRetBuf, (zAddrType_t*)param->DeviceAddress);
  817. tp += Z_EXTADDR_LEN;
  818. /* Capability Information */
  819. *tp++ = param->CapabilityFlags;
  820. /* Security */
  821. MT_MacSpi2Sec ((ZMacSec_t *)tp, (uint8 *)&param->Sec);
  822. /* Build and send back the response */
  823. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_AREQ | (uint8)MT_RPC_SYS_MAC), MT_MAC_ASSOCIATE_IND, respLen, pRetBuf);
  824. /* Deallocate */
  825. osal_mem_free(pRetBuf);
  826. }
  827. }
  828. /***************************************************************************************************
  829. * @fn nwk_MTCallbackSubNwkAssociateCnf
  830. *
  831. * @brief Process the callback subscription for nwk_associate_cnf
  832. *
  833. * @param pointer of type macnwk_associate_cnf_t
  834. *
  835. * @return SUCCESS if message sent succesfully , else N_FAIL
  836. ***************************************************************************************************/
  837. void nwk_MTCallbackSubNwkAssociateCnf( ZMacAssociateCnf_t *param )
  838. {
  839. uint8 respLen;
  840. uint8 *pRetBuf, *tp;
  841. respLen = MT_MAC_LEN_ASSOCIATE_CNF;
  842. pRetBuf = osal_mem_alloc (respLen);
  843. if ( pRetBuf )
  844. {
  845. tp = pRetBuf;
  846. /* Status */
  847. *tp++ = param->hdr.Status;
  848. /* Short address */
  849. *tp++ = LO_UINT16( param->AssocShortAddress );
  850. *tp++ = HI_UINT16( param->AssocShortAddress );
  851. /* Security */
  852. MT_MacSpi2Sec ((ZMacSec_t *)tp, (uint8 *)&param->Sec);
  853. /* Build and send back the response */
  854. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_AREQ | (uint8)MT_RPC_SYS_MAC), MT_MAC_ASSOCIATE_CNF, respLen, pRetBuf);
  855. /* Deallocate */
  856. osal_mem_free(pRetBuf);
  857. }
  858. }
  859. /***************************************************************************************************
  860. * @fn nwk_MTCallbackSubNwkBeaconNotifyInd
  861. *
  862. * @brief Process the callback subscription for
  863. * beacon_notify_ind.
  864. *
  865. * @param pointer to ZMacBeaconNotifyInd_t
  866. *
  867. * @return None
  868. ***************************************************************************************************/
  869. void nwk_MTCallbackSubNwkBeaconNotifyInd ( ZMacBeaconNotifyInd_t *param )
  870. {
  871. uint8 respLen;
  872. uint8 *pRetBuf, *tp;
  873. respLen = MT_MAC_LEN_BEACON_NOTIFY_IND;
  874. pRetBuf = osal_mem_alloc (respLen);
  875. if ( pRetBuf )
  876. {
  877. tp = pRetBuf;
  878. /* BSN */
  879. *tp++ = param->BSN;
  880. /* Timestamp */
  881. *tp++ = BREAK_UINT32( param->pPanDesc->TimeStamp, 0 );
  882. *tp++ = BREAK_UINT32( param->pPanDesc->TimeStamp, 1 );
  883. *tp++ = BREAK_UINT32( param->pPanDesc->TimeStamp, 2 );
  884. *tp++ = BREAK_UINT32( param->pPanDesc->TimeStamp, 3 );
  885. /* Coordinator address mode */
  886. *tp++ = param->pPanDesc->CoordAddress.addrMode;
  887. /* Coordinator address */
  888. MT_MacAddr2Spi( tp, &param->pPanDesc->CoordAddress );
  889. tp += Z_EXTADDR_LEN;
  890. /* PAN ID */
  891. *tp++ = LO_UINT16( param->pPanDesc->CoordPANId );
  892. *tp++ = HI_UINT16( param->pPanDesc->CoordPANId );
  893. /* Superframe spec */
  894. *tp++ = LO_UINT16( param->pPanDesc->SuperframeSpec );
  895. *tp++ = HI_UINT16( param->pPanDesc->SuperframeSpec );
  896. /* LogicalChannel */
  897. *tp++ = param->pPanDesc->LogicalChannel;
  898. /* GTSPermit */
  899. *tp++ = param->pPanDesc->GTSPermit;
  900. /* LinkQuality */
  901. *tp++ = param->pPanDesc->LinkQuality;
  902. /* SecurityFailure */
  903. *tp++ = param->pPanDesc->SecurityFailure;
  904. /* Security */
  905. MT_MacSpi2Sec ((ZMacSec_t *)tp, (uint8 *)&param->pPanDesc->Sec);
  906. tp += ZTEST_DEFAULT_SEC_LEN;
  907. /* PendingAddrSpec */
  908. *tp++ = param->PendAddrSpec;
  909. /* AddrList */
  910. osal_memset( tp, 0, MT_MAC_PEND_LEN_MAX );
  911. osal_memcpy( tp, param->AddrList, MIN(MT_MAC_PEND_LEN_MAX, MT_MAC_PEND_LEN(param->PendAddrSpec)) );
  912. tp += MT_MAC_PEND_LEN_MAX;
  913. /* SDULength */
  914. *tp++ = param->sduLength;
  915. /* SDU */
  916. osal_memset( tp, 0, MT_MAC_SDU_LEN_MAX );
  917. osal_memcpy( tp, param->sdu, MIN(MT_MAC_SDU_LEN_MAX, param->sduLength) );
  918. /* Build and send back the response */
  919. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_AREQ | (uint8)MT_RPC_SYS_MAC), MT_MAC_BEACON_NOTIFY_IND, respLen, pRetBuf);
  920. /* Deallocate */
  921. osal_mem_free(pRetBuf);
  922. }
  923. }
  924. /***************************************************************************************************
  925. * @fn nwk_MTCallbackSubNwkDataCnf
  926. *
  927. * @brief Process the callback subscription for nwk_data_cnf
  928. *
  929. * @param pointer of type macnwk_data_cnf_t
  930. *
  931. * @return None
  932. ***************************************************************************************************/
  933. void nwk_MTCallbackSubNwkDataCnf( ZMacDataCnf_t *param )
  934. {
  935. uint8 *pRetBuf, *tp;
  936. /* Allocate */
  937. pRetBuf = osal_mem_alloc(MT_MAC_LEN_DATA_CNF);
  938. if (pRetBuf)
  939. {
  940. tp = pRetBuf;
  941. /* Status */
  942. *tp++ = param->hdr.Status;
  943. /* Handle */
  944. *tp++ = param->msduHandle;
  945. /* Timestamp */
  946. *tp++ = BREAK_UINT32( param->Timestamp, 0 );
  947. *tp++ = BREAK_UINT32( param->Timestamp, 1 );
  948. *tp++ = BREAK_UINT32( param->Timestamp, 2 );
  949. *tp++ = BREAK_UINT32( param->Timestamp, 3 );
  950. /* Timestamp2 */
  951. *tp++ = LO_UINT16( param->Timestamp2);
  952. *tp = HI_UINT16( param->Timestamp2);
  953. /* Build and send back the response */
  954. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_AREQ | (uint8)MT_RPC_SYS_MAC), MT_MAC_DATA_CNF, MT_MAC_LEN_DATA_CNF, pRetBuf);
  955. /* Deallocate */
  956. osal_mem_free(pRetBuf);
  957. }
  958. }
  959. /***************************************************************************************************
  960. * @fn nwk_MTCallbackSubNwkDataInd
  961. *
  962. * @brief Process the callback subscription for nwk_data_ind
  963. *
  964. * @param pointer of type macnwk_data_ind_t
  965. *
  966. * @return None
  967. ***************************************************************************************************/
  968. void nwk_MTCallbackSubNwkDataInd( ZMacDataInd_t *param )
  969. {
  970. uint8 respLen, tempLen;
  971. uint8 *pRetBuf, *tp;
  972. /* Packet length is 44 + 102 data */
  973. respLen = MT_MAC_LEN_DATA_IND + ZTEST_DEFAULT_DATA_LEN;
  974. pRetBuf = osal_mem_alloc (respLen);
  975. if ( pRetBuf )
  976. {
  977. tp = pRetBuf;
  978. /* Src address mode */
  979. *tp++ = param->SrcAddr.addrMode;
  980. if (param->SrcAddr.addrMode != SADDR_MODE_NONE)
  981. {
  982. /* Src Address */
  983. MT_MacAddr2Spi( tp, &param->SrcAddr );
  984. }
  985. else
  986. {
  987. /* No address */
  988. for ( uint8 i = 0; i< Z_EXTADDR_LEN; i++ )
  989. {
  990. tp[i] = 0x00;
  991. }
  992. }
  993. tp += Z_EXTADDR_LEN;
  994. /* Dst address mode */
  995. *tp++ = param->DstAddr.addrMode;
  996. /* Dst address */
  997. MT_MacAddr2Spi( tp, &param->DstAddr );
  998. tp += Z_EXTADDR_LEN;
  999. /* Timestamp */
  1000. *tp++ = BREAK_UINT32( param->Timestamp, 0 );
  1001. *tp++ = BREAK_UINT32( param->Timestamp, 1 );
  1002. *tp++ = BREAK_UINT32( param->Timestamp, 2 );
  1003. *tp++ = BREAK_UINT32( param->Timestamp, 3 );
  1004. /* Timestamp2 */
  1005. *tp++ = LO_UINT16( param->Timestamp2);
  1006. *tp++ = HI_UINT16( param->Timestamp2);
  1007. /* Src Pan Id */
  1008. *tp++ = LO_UINT16( param->SrcPANId );
  1009. *tp++ = HI_UINT16( param->SrcPANId );
  1010. /* Dst Pan Id */
  1011. *tp++ = LO_UINT16( param->DstPANId );
  1012. *tp++ = HI_UINT16( param->DstPANId );
  1013. /* mpdu Link Quality */
  1014. *tp++ = param->mpduLinkQuality;
  1015. /* LQI */
  1016. *tp++ = param->Correlation;
  1017. /* RSSI */
  1018. *tp++ = param->Rssi;
  1019. /* DSN */
  1020. *tp++ = param->Dsn;
  1021. /* Security */
  1022. MT_MacSpi2Sec ((ZMacSec_t *)tp, (uint8 *)&param->Sec);
  1023. tp += ZTEST_DEFAULT_SEC_LEN;
  1024. /* Determine the length of the actual data */
  1025. if ( param->msduLength < ZTEST_DEFAULT_DATA_LEN )
  1026. tempLen = param->msduLength;
  1027. else
  1028. tempLen = ZTEST_DEFAULT_DATA_LEN;
  1029. /* Length */
  1030. *tp++ = tempLen;
  1031. /* Copy the data according to Len, the rest are zeroed out */
  1032. osal_memset( tp, 0, ZTEST_DEFAULT_DATA_LEN );
  1033. osal_memcpy( tp, param->msdu, tempLen );
  1034. /* Build and send back the response */
  1035. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_AREQ | (uint8)MT_RPC_SYS_MAC), MT_MAC_DATA_IND, respLen, pRetBuf);
  1036. /* Deallocate */
  1037. osal_mem_free(pRetBuf);
  1038. }
  1039. }
  1040. /***************************************************************************************************
  1041. * @fn nwk_MTCallbackSubNwkDisassociateInd
  1042. *
  1043. * @brief Process the callback subscription for nwk_disassociate_ind
  1044. *
  1045. * @param pointer of type macnwk_disassociate_ind_t
  1046. *
  1047. * @return None
  1048. ***************************************************************************************************/
  1049. void nwk_MTCallbackSubNwkDisassociateInd( ZMacDisassociateInd_t *param )
  1050. {
  1051. uint8 respLen;
  1052. uint8 *pRetBuf, *tp;
  1053. respLen = MT_MAC_LEN_DISASSOCIATE_IND;
  1054. pRetBuf = osal_mem_alloc (respLen);
  1055. if ( pRetBuf )
  1056. {
  1057. tp = pRetBuf;
  1058. /* Extended address */
  1059. MT_MacRevExtCpy( pRetBuf, param->DeviceAddress );
  1060. tp += Z_EXTADDR_LEN;
  1061. /* Disassociate Reason */
  1062. *tp++ = param->DisassociateReason;
  1063. /* Security */
  1064. MT_MacSpi2Sec ((ZMacSec_t *)tp, (uint8 *)&param->Sec);
  1065. /* Build and send back the response */
  1066. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_AREQ | (uint8)MT_RPC_SYS_MAC), MT_MAC_DISASSOCIATE_IND, respLen, pRetBuf);
  1067. /* Deallocate */
  1068. osal_mem_free(pRetBuf);
  1069. }
  1070. }
  1071. /***************************************************************************************************
  1072. * @fn nwk_MTCallbackSubNwkDisassociateCnf
  1073. *
  1074. * @brief Process the callback subscription for nwk_disassociate_cnf
  1075. *
  1076. * @param param
  1077. *
  1078. * @return None
  1079. ***************************************************************************************************/
  1080. void nwk_MTCallbackSubNwkDisassociateCnf( ZMacDisassociateCnf_t *param )
  1081. {
  1082. uint8 respLen;
  1083. uint8 *pRetBuf, *tp;
  1084. respLen = MT_MAC_LEN_DISASSOCIATE_CNF;
  1085. pRetBuf = osal_mem_alloc (respLen);
  1086. if ( pRetBuf )
  1087. {
  1088. tp = pRetBuf;
  1089. /* Status */
  1090. *tp++ = param->hdr.Status;
  1091. /* DeviceAddress */
  1092. *tp++ = param->DeviceAddress.addrMode;
  1093. /* Copy Address */
  1094. MT_MacAddr2Spi( tp, &param->DeviceAddress );
  1095. tp += Z_EXTADDR_LEN;
  1096. /* Pan ID */
  1097. *tp++ = LO_UINT16( param->panID );
  1098. *tp = HI_UINT16( param->panID );
  1099. /* Build and send back the response */
  1100. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_AREQ | (uint8)MT_RPC_SYS_MAC), MT_MAC_DISASSOCIATE_CNF, respLen, pRetBuf);
  1101. /* Deallocate */
  1102. osal_mem_free(pRetBuf);
  1103. }
  1104. }
  1105. /***************************************************************************************************
  1106. * @fn nwk_MTCallbackSubNwkOrphanInd
  1107. *
  1108. * @brief Process the callback subscription for nwk_orphan_ind
  1109. *
  1110. * @param pointer of type macnwk_orphan_ind_t
  1111. *
  1112. * @return SUCCESS if message sent succesfully , else N_FAIL
  1113. ***************************************************************************************************/
  1114. void nwk_MTCallbackSubNwkOrphanInd( ZMacOrphanInd_t *param )
  1115. {
  1116. uint8 respLen;
  1117. uint8 *pRetBuf, *tp;
  1118. respLen = MT_MAC_LEN_ORPHAN_IND;
  1119. pRetBuf = osal_mem_alloc (respLen);
  1120. if ( pRetBuf )
  1121. {
  1122. tp = pRetBuf;
  1123. /* Extended address */
  1124. MT_MacRevExtCpy( tp, param->OrphanAddress );
  1125. tp += Z_EXTADDR_LEN;
  1126. /* Security */
  1127. MT_MacSpi2Sec ((ZMacSec_t *)tp, (uint8 *)&param->Sec);
  1128. /* Build and send back the response */
  1129. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_AREQ | (uint8)MT_RPC_SYS_MAC), MT_MAC_ORPHAN_IND, respLen, pRetBuf);
  1130. /* Deallocate */
  1131. osal_mem_free(pRetBuf);
  1132. }
  1133. }
  1134. /***************************************************************************************************
  1135. * @fn nwk_MTCallbackSubNwkPollCnf
  1136. *
  1137. * @brief Process the callback subscription for nwk_poll_cnf
  1138. *
  1139. * @param byte Status
  1140. *
  1141. * @return None
  1142. ***************************************************************************************************/
  1143. void nwk_MTCallbackSubNwkPollCnf( byte Status )
  1144. {
  1145. uint8 retValue;
  1146. /*The only data byte is Status */
  1147. retValue = Status;
  1148. /* Build and send back the response */
  1149. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_AREQ | (uint8)MT_RPC_SYS_MAC), MT_MAC_POLL_CNF, 1, &retValue);
  1150. }
  1151. /***************************************************************************************************
  1152. * @fn nwk_MTCallbackSubNwkScanCnf
  1153. *
  1154. * @brief Process the callback subscription for nwk_scan_cnf
  1155. *
  1156. * @param pointer of type macnwk_scan_cnf_t
  1157. *
  1158. * @return SUCCESS if message sent succesfully , else N_FAIL
  1159. ***************************************************************************************************/
  1160. void nwk_MTCallbackSubNwkScanCnf( ZMacScanCnf_t *param )
  1161. {
  1162. uint8 respLen, resultLen;
  1163. uint8 *pRetBuf, *tp;
  1164. /* Depends on the type of scan, calculate the required length */
  1165. if ( param->ScanType == ZMAC_ED_SCAN )
  1166. resultLen = MT_MAC_ED_SCAN_MAXCHANNELS;
  1167. else if ( param->ScanType == ZMAC_ACTIVE_SCAN )
  1168. resultLen = (param->ResultListSize * sizeof( ZMacPanDesc_t ));
  1169. else if ( param->ScanType == ZMAC_PASSIVE_SCAN )
  1170. resultLen = (param->ResultListSize * sizeof( ZMacPanDesc_t ));
  1171. else if ( param->ScanType == ZMAC_ORPHAN_SCAN )
  1172. resultLen = 0;
  1173. else
  1174. return;
  1175. /* Make sure the result wont be more than the size */
  1176. resultLen = MIN(resultLen, MT_MAC_SCAN_RESULT_LEN_MAX);
  1177. respLen = MT_MAC_LEN_SCAN_CNF + MT_MAC_SCAN_RESULT_LEN_MAX + 1; /* Extra byte for the length of the list */
  1178. /* Allocate */
  1179. pRetBuf = osal_mem_alloc (respLen);
  1180. if ( pRetBuf )
  1181. {
  1182. tp = pRetBuf;
  1183. /* Status */
  1184. *tp++ = param->hdr.Status;
  1185. /* ED max energy parameter no longer used */
  1186. *tp++ = 0;
  1187. /* Scan type */
  1188. *tp++ = param->ScanType;
  1189. /* Channel page */
  1190. *tp++ = param->ChannelPage;
  1191. /* Unscanned channel list */
  1192. *tp++ = BREAK_UINT32( param->UnscannedChannels, 0 );
  1193. *tp++ = BREAK_UINT32( param->UnscannedChannels, 1 );
  1194. *tp++ = BREAK_UINT32( param->UnscannedChannels, 2 );
  1195. *tp++ = BREAK_UINT32( param->UnscannedChannels, 3 );
  1196. /* Result count */
  1197. *tp++ = param->ResultListSize;
  1198. /* MAX length of the result List */
  1199. *tp++ = MT_MAC_SCAN_RESULT_LEN_MAX;
  1200. /* PAN descriptor information */
  1201. osal_memcpy( tp, param->Result.pPanDescriptor, resultLen );
  1202. /* clear extra buffer space at end, if any */
  1203. osal_memset( tp, 0, (MT_MAC_SCAN_RESULT_LEN_MAX - resultLen));
  1204. /* Build and send back the response */
  1205. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_AREQ | (uint8)MT_RPC_SYS_MAC), MT_MAC_SCAN_CNF, respLen, pRetBuf);
  1206. /* Deallocate */
  1207. osal_mem_free(pRetBuf);
  1208. }
  1209. }
  1210. /***************************************************************************************************
  1211. * @fn nwk_MTCallbackSubCommStatusInd
  1212. *
  1213. * @brief Process the callback subscription for comm_status_ind.
  1214. *
  1215. * @param None
  1216. *
  1217. * @return None
  1218. ***************************************************************************************************/
  1219. void nwk_MTCallbackSubCommStatusInd ( ZMacCommStatusInd_t *param )
  1220. {
  1221. uint8 respLen;
  1222. uint8 *pRetBuf, *tp;
  1223. respLen = MT_MAC_LEN_COMM_STATUS_IND;
  1224. pRetBuf = osal_mem_alloc (respLen);
  1225. if ( pRetBuf )
  1226. {
  1227. tp = pRetBuf;
  1228. /* Status */
  1229. *tp++ = param->hdr.Status;
  1230. /* Source address */
  1231. *tp++ = param->SrcAddress.addrMode;
  1232. MT_MacAddr2Spi( tp, &param->SrcAddress );
  1233. tp += Z_EXTADDR_LEN;
  1234. /* Destination address */
  1235. *tp++ = param->DstAddress.addrMode;
  1236. MT_MacAddr2Spi( tp, &param->DstAddress );
  1237. tp += Z_EXTADDR_LEN;
  1238. /* PAN ID */
  1239. *tp++ = LO_UINT16( param->PANId );
  1240. *tp++ = HI_UINT16( param->PANId );
  1241. /* Reason */
  1242. *tp++ = param->Reason;
  1243. /* Security */
  1244. MT_MacSpi2Sec ((ZMacSec_t *)tp, (uint8 *)&param->Sec);
  1245. /* Build and send back the response */
  1246. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_AREQ | (uint8)MT_RPC_SYS_MAC), MT_MAC_COMM_STATUS_IND, respLen, pRetBuf);
  1247. /* Deallocate */
  1248. osal_mem_free(pRetBuf);
  1249. }
  1250. }
  1251. /***************************************************************************************************
  1252. * @fn nwk_MTCallbackSubNwkStartCnf
  1253. *
  1254. * @brief Process the callback subscription for nwk_start_cnf
  1255. *
  1256. * @param byte Status
  1257. *
  1258. * @return None
  1259. ***************************************************************************************************/
  1260. void nwk_MTCallbackSubNwkStartCnf( uint8 Status )
  1261. {
  1262. uint8 retValue;
  1263. retValue = Status;
  1264. /* Build and send back the response */
  1265. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_AREQ | (uint8)MT_RPC_SYS_MAC), MT_MAC_START_CNF, 1, &retValue);
  1266. }
  1267. /***************************************************************************************************
  1268. * @fn nwk_MTCallbackSubNwkRxEnableCnf
  1269. *
  1270. * @brief Process the callback subscription for nwk_Rx_Enable_cnf
  1271. *
  1272. * @param
  1273. *
  1274. * @return SUCCESS if message sent succesfully , else N_FAIL
  1275. ***************************************************************************************************/
  1276. void nwk_MTCallbackSubNwkRxEnableCnf ( byte Status )
  1277. {
  1278. uint8 retValue;
  1279. /* The only data byte is Status */
  1280. retValue = Status;
  1281. /* Build and send back the response */
  1282. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_AREQ | (uint8)MT_RPC_SYS_MAC), MT_MAC_RX_ENABLE_CNF, 1, &retValue);
  1283. }
  1284. /***************************************************************************************************
  1285. * @fn nwk_MTCallbackSubNwkPurgeCnf
  1286. *
  1287. * @brief Process the callback subscription for nwk_purge_cnf
  1288. *
  1289. * @param pointer of type ZMacPurgeCnf_t
  1290. *
  1291. * @return SUCCESS if message sent succesfully , else N_FAIL
  1292. ***************************************************************************************************/
  1293. void nwk_MTCallbackSubNwkPurgeCnf( ZMacPurgeCnf_t *param )
  1294. {
  1295. uint8 respLen;
  1296. uint8 *pRetBuf, *tp;
  1297. respLen = MT_MAC_LEN_PURGE_CNF;
  1298. pRetBuf = osal_mem_alloc (respLen);
  1299. if ( pRetBuf )
  1300. {
  1301. tp = pRetBuf;
  1302. /* Status */
  1303. *tp++ = param->hdr.Status;
  1304. /* Handle */
  1305. *tp = param->msduHandle;
  1306. /* Build and send back the response */
  1307. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_AREQ | (uint8)MT_RPC_SYS_MAC), MT_MAC_PURGE_CNF, respLen, pRetBuf);
  1308. /* Deallocate */
  1309. osal_mem_free(pRetBuf);
  1310. }
  1311. }
  1312. #endif // MT_MAC_CB_FUNC
  1313. /***************************************************************************************************
  1314. * SUPPORT
  1315. ***************************************************************************************************/
  1316. /***************************************************************************************************
  1317. * @fn MT_MacExtCpy
  1318. *
  1319. * @brief
  1320. *
  1321. * Copy an extended address.
  1322. *
  1323. * @param pDst - Pointer to data destination
  1324. * @param pSrc - Pointer to data source
  1325. *
  1326. * @return void
  1327. ***************************************************************************************************/
  1328. static void MT_MacExtCpy( uint8 *pDst, uint8 *pSrc )
  1329. {
  1330. int8 i;
  1331. for ( i = 0; i < Z_EXTADDR_LEN; i++ )
  1332. {
  1333. *pDst++ = pSrc[i];
  1334. }
  1335. }
  1336. /***************************************************************************************************
  1337. * @fn MT_MacRevExtCpy
  1338. *
  1339. * @brief
  1340. *
  1341. * Reverse-copy an extended address.
  1342. *
  1343. * @param pDst - Pointer to data destination
  1344. * @param pSrc - Pointer to data source
  1345. *
  1346. * @return void
  1347. ***************************************************************************************************/
  1348. static void MT_MacRevExtCpy( uint8 *pDst, uint8 *pSrc )
  1349. {
  1350. int8 i;
  1351. for ( i = Z_EXTADDR_LEN - 1; i >= 0; i-- )
  1352. {
  1353. *pDst++ = pSrc[i];
  1354. }
  1355. }
  1356. /***************************************************************************************************
  1357. * @fn MT_MacSpi2Addr
  1358. *
  1359. * @brief Copy an address from an SPI message to an address struct. The
  1360. * addrMode in pAddr must already be set.
  1361. *
  1362. * @param pDst - Pointer to address struct
  1363. * @param pSrc - Pointer SPI message byte array
  1364. *
  1365. * @return void
  1366. ***************************************************************************************************/
  1367. static void MT_MacSpi2Addr( zAddrType_t *pDst, uint8 *pSrc )
  1368. {
  1369. if ( pDst->addrMode == Addr16Bit )
  1370. {
  1371. pDst->addr.shortAddr = BUILD_UINT16( pSrc[0] , pSrc[1] );
  1372. }
  1373. else if ( pDst->addrMode == Addr64Bit )
  1374. {
  1375. MT_MacRevExtCpy( pDst->addr.extAddr, pSrc );
  1376. }
  1377. }
  1378. /***************************************************************************************************
  1379. * @fn MT_MacSpi2Sec
  1380. *
  1381. * @brief Copy Security information from SPI message to a Sec structure
  1382. *
  1383. * @param pSec - Pointer to security struct
  1384. * @param pSrc - Pointer SPI message byte array
  1385. *
  1386. * @return void
  1387. ***************************************************************************************************/
  1388. static void MT_MacSpi2Sec( ZMacSec_t *pSec, uint8 *pSrc )
  1389. {
  1390. /* Copy the security structure directly from the byte array */
  1391. osal_memcpy (pSec, pSrc, sizeof (ZMacSec_t));
  1392. }
  1393. /***************************************************************************************************
  1394. * @fn MT_MacAddr2Spi
  1395. *
  1396. * @brief Copy an address from an address struct to an SPI message.
  1397. *
  1398. * @param pDst - Pointer SPI message byte array
  1399. * @param pSrc - Pointer to address struct
  1400. *
  1401. * @return void
  1402. ***************************************************************************************************/
  1403. static void MT_MacAddr2Spi( uint8 *pDst, zAddrType_t *pSrc )
  1404. {
  1405. if ( pSrc->addrMode == Addr16Bit )
  1406. {
  1407. *pDst++ = LO_UINT16( pSrc->addr.shortAddr );
  1408. *pDst++ = HI_UINT16( pSrc->addr.shortAddr );
  1409. *pDst++ = 0; *pDst++ = 0; *pDst++ = 0;
  1410. *pDst++ = 0; *pDst++ = 0; *pDst = 0;
  1411. }
  1412. else if ( pSrc->addrMode == Addr64Bit )
  1413. {
  1414. for ( uint8 i = 0; i< Z_EXTADDR_LEN; i++ )
  1415. {
  1416. *pDst++ = pSrc->addr.extAddr[i];
  1417. }
  1418. }
  1419. else
  1420. {
  1421. for ( uint8 i = 0; i< Z_EXTADDR_LEN; i++ )
  1422. {
  1423. *pDst++ = pSrc->addr.extAddr[i];
  1424. }
  1425. }
  1426. }
  1427. /***************************************************************************************************
  1428. ***************************************************************************************************/
  1429. #endif // MT_MAC_FUNC