MT_NWK.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  1. /**************************************************************************************************
  2. Filename: MT_NWK.c
  3. Revised: $Date: 2008-03-14 12:54:09 -0700 (Fri, 14 Mar 2008) $
  4. Revision: $Revision: 16592 $
  5. Description: MonitorTest functions for the NWK 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 "NLMEDE.h"
  40. #include "nwk.h"
  41. #include "ZDApp.h"
  42. #include "nwk_globals.h"
  43. #include "MT_NWK.h"
  44. #if !defined( WIN32 )
  45. #include "OnBoard.h"
  46. #endif
  47. /***************************************************************************************************
  48. * GLOBAL VARIABLES
  49. ***************************************************************************************************/
  50. uint16 _nwkCallbackSub;
  51. /*********************************************************************
  52. * TYPEDEFS
  53. */
  54. /***************************************************************************************************
  55. * LOCAL FUNCTIONS
  56. ***************************************************************************************************/
  57. #if defined (MT_NWK_FUNC)
  58. void MT_NldeDataRequest(uint8 *pBuf);
  59. void MT_NlmeNetworkFormationRequest(uint8 *pBuf);
  60. void MT_NlmePermitJoiningRequest(uint8 *pBuf);
  61. void MT_NlmeStartRouterRequest(uint8 *pBuf);
  62. void MT_NlmeJoinRequest(uint8 *pBuf);
  63. void MT_NlmeLeaveRequest(uint8 *pBuf);
  64. void MT_NlmeResetRequest(uint8 *pBuf);
  65. void MT_NlmeGetRequest(uint8 *pBuf);
  66. void MT_NlmeSetRequest(uint8 *pBuf);
  67. void MT_NlmeNetworkDiscoveryRequest(uint8 *pBuf);
  68. void MT_NlmeRouteDiscoveryRequest(uint8 *pBuf);
  69. void MT_NlmeDirectJoinRequest(uint8 *pBuf);
  70. void MT_NlmeOrphanJoinRequest(uint8 *pBuf);
  71. uint8 MT_Nwk_DataRequest( uint16 dstAddr, uint8 nsduLen, uint8* nsdu,
  72. uint8 nsduHandle, uint16 nsduHandleOptions,
  73. uint8 secure, uint8 discoverRoute,
  74. uint8 radius);
  75. #endif /* MT_NWK_FUNC */
  76. #if defined (MT_NWK_FUNC)
  77. /***************************************************************************************************
  78. * @fn MT_NwkCommandProcessing
  79. *
  80. * @brief
  81. *
  82. * Process all the NWK commands that are issued by test tool
  83. *
  84. * @param cmd_id - Command ID
  85. * @param len - Length of received SPI data message
  86. * @param pData - pointer to received SPI data message
  87. *
  88. * @return status
  89. ***************************************************************************************************/
  90. uint8 MT_NwkCommandProcessing(uint8 *pBuf)
  91. {
  92. uint8 status = MT_RPC_SUCCESS;
  93. switch (pBuf[MT_RPC_POS_CMD1])
  94. {
  95. case MT_NWK_INIT:
  96. nwk_init(NWK_TaskID);
  97. break;
  98. case MT_NLDE_DATA_REQ:
  99. MT_NldeDataRequest(pBuf);
  100. break;
  101. case MT_NLME_NETWORK_FORMATION_REQ:
  102. MT_NlmeNetworkFormationRequest(pBuf);
  103. break;
  104. case MT_NLME_PERMIT_JOINING_REQ:
  105. MT_NlmePermitJoiningRequest(pBuf);
  106. break;
  107. case MT_NLME_JOIN_REQ:
  108. MT_NlmeJoinRequest(pBuf);
  109. break;
  110. case MT_NLME_LEAVE_REQ:
  111. MT_NlmeLeaveRequest(pBuf);
  112. break;
  113. case MT_NLME_RESET_REQ:
  114. MT_NlmeResetRequest(pBuf);
  115. break;
  116. case MT_NLME_GET_REQ:
  117. MT_NlmeGetRequest(pBuf);
  118. break;
  119. case MT_NLME_SET_REQ:
  120. MT_NlmeSetRequest(pBuf);
  121. break;
  122. case MT_NLME_NETWORK_DISCOVERY_REQ:
  123. MT_NlmeNetworkDiscoveryRequest(pBuf);
  124. break;
  125. case MT_NLME_ROUTE_DISCOVERY_REQ:
  126. MT_NlmeRouteDiscoveryRequest(pBuf);
  127. break;
  128. case MT_NLME_DIRECT_JOIN_REQ:
  129. MT_NlmeDirectJoinRequest(pBuf);
  130. break;
  131. case MT_NLME_ORPHAN_JOIN_REQ:
  132. MT_NlmeOrphanJoinRequest(pBuf);
  133. break;
  134. case MT_NLME_START_ROUTER_REQ:
  135. MT_NlmeStartRouterRequest(pBuf);
  136. break;
  137. default:
  138. status = MT_RPC_ERR_COMMAND_ID;
  139. break;
  140. }
  141. return status;
  142. }
  143. /***************************************************************************************************
  144. * @fn MT_NldeDataReq
  145. *
  146. * @brief Process NLDE Data Request commands
  147. *
  148. * @param pBuf - pointer to received buffer
  149. *
  150. * @return void
  151. ***************************************************************************************************/
  152. void MT_NldeDataRequest(uint8 *pBuf)
  153. {
  154. uint8 retValue = ZFailure;
  155. uint16 dstAddr;
  156. uint8 dataLen = 0;
  157. uint8 *dataPtr;
  158. uint8 cmdId;
  159. /* parse header */
  160. cmdId = pBuf[MT_RPC_POS_CMD1];
  161. pBuf += MT_RPC_FRAME_HDR_SZ;
  162. /* First read the DstAddr */
  163. dstAddr = BUILD_UINT16( pBuf[0], pBuf[1] );
  164. pBuf += sizeof( dstAddr );
  165. /* Get the NSDU details */
  166. dataLen = *pBuf++;
  167. dataPtr = pBuf;
  168. /* Skip a length of ZTEST_DEFAULT_DATA_LEN */
  169. pBuf += ZTEST_DEFAULT_DATA_LEN;
  170. /* Send out Data Request */
  171. retValue = MT_Nwk_DataRequest(dstAddr, dataLen, dataPtr, pBuf[0], BUILD_UINT16(pBuf[1], pBuf[2]),
  172. pBuf[3], pBuf[4], pBuf[5]);
  173. /* Build and send back the response */
  174. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_NWK), cmdId, 1, &retValue);
  175. }
  176. /***************************************************************************************************
  177. * @fn MT_NlmeNetworkFormationRequest
  178. *
  179. * @brief Network Formation Request
  180. *
  181. * @param pBuf - pointer to the received buffer
  182. *
  183. * @return void
  184. ***************************************************************************************************/
  185. void MT_NlmeNetworkFormationRequest(uint8 *pBuf)
  186. {
  187. uint8 retValue = ZFailure;
  188. uint16 panId;
  189. uint32 channelList;
  190. uint8 cmdId;
  191. /* parse header */
  192. cmdId = pBuf[MT_RPC_POS_CMD1];
  193. pBuf += MT_RPC_FRAME_HDR_SZ;
  194. /* Build panId */
  195. panId = BUILD_UINT16( pBuf[0], pBuf[1]);
  196. pBuf += sizeof(uint16);
  197. /* Build the channel list */
  198. channelList = osal_build_uint32(pBuf, 4);
  199. pBuf += sizeof(uint32);
  200. if ( ZG_BUILD_COORDINATOR_TYPE && ZG_DEVICE_COORDINATOR_TYPE )
  201. {
  202. retValue = NLME_NetworkFormationRequest( panId, NULL, channelList, pBuf[0], pBuf[1], pBuf[2], pBuf[3] );
  203. }
  204. else
  205. {
  206. retValue = ZUnsupportedMode;
  207. }
  208. /* Build and send back the response */
  209. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_NWK), cmdId, 1, &retValue);
  210. }
  211. /***************************************************************************************************
  212. * @fn MT_NlmePermitJoiningRequest
  213. *
  214. * @brief Permit Joining Request
  215. *
  216. * @param pBuf - pointer to the received buffer
  217. *
  218. * @return void
  219. ***************************************************************************************************/
  220. void MT_NlmePermitJoiningRequest(uint8 *pBuf)
  221. {
  222. uint8 retValue = ZFailure;
  223. uint8 cmdId;
  224. /* parse header */
  225. cmdId = pBuf[MT_RPC_POS_CMD1];
  226. pBuf += MT_RPC_FRAME_HDR_SZ;
  227. if (ZSTACK_ROUTER_BUILD)
  228. {
  229. retValue = NLME_PermitJoiningRequest(*pBuf);
  230. }
  231. else
  232. {
  233. retValue = ZUnsupportedMode;
  234. }
  235. /* Build and send back the response */
  236. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_NWK), cmdId, 1, &retValue);
  237. }
  238. /***************************************************************************************************
  239. * @fn MT_NlmeStartRouterRequest
  240. *
  241. * @brief Start Router Request
  242. *
  243. * @param pBuf - pointer to the received buffer
  244. *
  245. * @return void
  246. ***************************************************************************************************/
  247. void MT_NlmeStartRouterRequest(uint8 *pBuf)
  248. {
  249. uint8 retValue = ZFailure;
  250. uint8 cmdId;
  251. /* parse header */
  252. cmdId = pBuf[MT_RPC_POS_CMD1];
  253. pBuf += MT_RPC_FRAME_HDR_SZ;
  254. if ( ZSTACK_ROUTER_BUILD )
  255. {
  256. retValue = (byte)NLME_StartRouterRequest(pBuf[0], pBuf[1], pBuf[2]);
  257. }
  258. else
  259. {
  260. retValue = ZUnsupportedMode;
  261. }
  262. /* Build and send back the response */
  263. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_NWK), cmdId, 1, &retValue);
  264. }
  265. /***************************************************************************************************
  266. * @fn MT_NlmeJoinRequest
  267. *
  268. * @brief Join Request
  269. *
  270. * @param pBuf - pointer to the received buffer
  271. *
  272. * @return void
  273. ***************************************************************************************************/
  274. void MT_NlmeJoinRequest(uint8 *pBuf)
  275. {
  276. uint8 retValue = ZFailure;
  277. uint8 dummyExPANID[Z_EXTADDR_LEN] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  278. uint8 cmdId;
  279. /* parse header */
  280. cmdId = pBuf[MT_RPC_POS_CMD1];
  281. pBuf += MT_RPC_FRAME_HDR_SZ;
  282. retValue = NLME_JoinRequest(dummyExPANID, BUILD_UINT16(pBuf[0], pBuf[1]), pBuf[2], pBuf[3]);
  283. if ( pBuf[6] & CAPINFO_RCVR_ON_IDLE )
  284. {
  285. /* The receiver is on, turn network layer polling off. */
  286. NLME_SetPollRate( 0 );
  287. NLME_SetQueuedPollRate( 0 );
  288. NLME_SetResponseRate( 0 );
  289. }
  290. /* Build and send back the response */
  291. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_NWK), cmdId, 1, &retValue);
  292. }
  293. /***************************************************************************************************
  294. * @fn MT_NlmeLeaveRequest
  295. *
  296. * @brief Leave Request
  297. *
  298. * @param pBuf - pointer to the received buffer
  299. *
  300. * @return void
  301. ***************************************************************************************************/
  302. void MT_NlmeLeaveRequest(uint8 *pBuf)
  303. {
  304. NLME_LeaveReq_t req;
  305. uint8 retValue = ZFailure;
  306. uint8 index, cmdId;
  307. /* parse header */
  308. cmdId = pBuf[MT_RPC_POS_CMD1];
  309. pBuf += MT_RPC_FRAME_HDR_SZ;
  310. /* If extAddr is all zeros, it means null pointer */
  311. for(index=0;((index < Z_EXTADDR_LEN) && (pBuf[index] == 0));index++);
  312. if (index == Z_EXTADDR_LEN)
  313. {
  314. req.extAddr = NULL;
  315. }
  316. else
  317. {
  318. req.extAddr = pBuf;
  319. }
  320. /* Increment the pointer */
  321. pBuf += Z_EXTADDR_LEN;
  322. req.removeChildren = FALSE;
  323. req.rejoin = FALSE;
  324. req.silent = FALSE;
  325. retValue = NLME_LeaveReq(&req);
  326. /* Build and send back the response */
  327. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_NWK), cmdId, 1, &retValue);
  328. }
  329. /***************************************************************************************************
  330. * @fn MT_NlmeResetRequest
  331. *
  332. * @brief Leave Request
  333. *
  334. * @param pBuf - pointer to the received buffer
  335. *
  336. * @return void
  337. ***************************************************************************************************/
  338. void MT_NlmeResetRequest(uint8 *pBuf)
  339. {
  340. uint8 retValue = NLME_ResetRequest();
  341. /* Build and send back the response */
  342. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_NWK), MT_NLME_RESET_REQ, 1, &retValue);
  343. }
  344. /***************************************************************************************************
  345. * @fn MT_NlmeGetRequest
  346. *
  347. * @brief Get Request
  348. *
  349. * @param pBuf - pointer to the received buffer
  350. *
  351. * @return void
  352. ***************************************************************************************************/
  353. void MT_NlmeGetRequest(uint8 *pBuf)
  354. {
  355. uint8 dataBuf[11];
  356. uint8 attr, index, cmdId;
  357. /* parse header */
  358. cmdId = pBuf[MT_RPC_POS_CMD1];
  359. pBuf += MT_RPC_FRAME_HDR_SZ;
  360. attr = *pBuf++;
  361. index = *pBuf;
  362. dataBuf[0] = NLME_GetRequest((ZNwkAttributes_t )attr, index, &dataBuf[1]);
  363. /* Build and send back the response */
  364. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_NWK), cmdId,
  365. 11, dataBuf );
  366. }
  367. /***************************************************************************************************
  368. * @fn MT_NlmeSetRequest
  369. *
  370. * @brief Set Request
  371. *
  372. * @param pBuf - pointer to the received buffer
  373. *
  374. * @return void
  375. ***************************************************************************************************/
  376. void MT_NlmeSetRequest(uint8 *pBuf)
  377. {
  378. uint8 retValue = ZFailure;
  379. uint8 cmdId;
  380. /* parse header */
  381. cmdId = pBuf[MT_RPC_POS_CMD1];
  382. pBuf += MT_RPC_FRAME_HDR_SZ;
  383. retValue = NLME_SetRequest((ZNwkAttributes_t)pBuf[0], pBuf[1], &pBuf[2]);
  384. /* Update NV */
  385. ZDApp_NVUpdate();
  386. /* Build and send back the response */
  387. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_NWK), cmdId, 1, &retValue );
  388. }
  389. /***************************************************************************************************
  390. * @fn MT_NlmeNetworkDiscoveryRequest
  391. *
  392. * @brief Network Discovery Request
  393. *
  394. * @param pBuf - pointer to the received buffer
  395. *
  396. * @return void
  397. ***************************************************************************************************/
  398. void MT_NlmeNetworkDiscoveryRequest(uint8 *pBuf)
  399. {
  400. uint8 retValue = ZFailure;
  401. uint8 cmdId;
  402. uint32 scanChannels;
  403. /* parse header */
  404. cmdId = pBuf[MT_RPC_POS_CMD1];
  405. pBuf += MT_RPC_FRAME_HDR_SZ;
  406. /* Scan channels */
  407. scanChannels = osal_build_uint32(pBuf, 4);
  408. pBuf += sizeof(uint32);
  409. retValue = NLME_NetworkDiscoveryRequest(scanChannels, *pBuf);
  410. /* Build and send back the response */
  411. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_NWK), cmdId, 1, &retValue );
  412. }
  413. /***************************************************************************************************
  414. * @fn MT_NlmeRouteDiscoveryRequest
  415. *
  416. * @brief Route Discovery Request
  417. *
  418. * @param pBuf - pointer to the received buffer
  419. *
  420. * @return void
  421. ***************************************************************************************************/
  422. void MT_NlmeRouteDiscoveryRequest(uint8 *pBuf)
  423. {
  424. uint8 retValue = ZFailure;
  425. uint8 cmdId;
  426. /* parse header */
  427. cmdId = pBuf[MT_RPC_POS_CMD1];
  428. pBuf += MT_RPC_FRAME_HDR_SZ;
  429. if ( ZSTACK_ROUTER_BUILD )
  430. {
  431. retValue = NLME_RouteDiscoveryRequest(BUILD_UINT16(pBuf[0], pBuf[1]), 0, pBuf[2]);
  432. }
  433. else
  434. {
  435. retValue = ZUnsupportedMode;
  436. }
  437. /* Build and send back the response */
  438. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_NWK), cmdId, 1, &retValue);
  439. }
  440. /***************************************************************************************************
  441. * @fn MT_NlmeDirectJoinRequest
  442. *
  443. * @brief Direct Join Request
  444. *
  445. * @param pBuf - pointer to the received buffer
  446. *
  447. * @return void
  448. ***************************************************************************************************/
  449. void MT_NlmeDirectJoinRequest(uint8 *pBuf)
  450. {
  451. uint8 retValue = ZFailure;
  452. uint8 cmdId;
  453. /* parse header */
  454. cmdId = pBuf[MT_RPC_POS_CMD1];
  455. pBuf += MT_RPC_FRAME_HDR_SZ;
  456. if ( ZSTACK_ROUTER_BUILD )
  457. {
  458. retValue = NLME_DirectJoinRequest( pBuf, pBuf[8] );
  459. }
  460. else
  461. {
  462. retValue = ZUnsupportedMode;
  463. }
  464. /* Build and send back the response */
  465. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_NWK), cmdId, 1, &retValue);
  466. }
  467. /***************************************************************************************************
  468. * @fn MT_NlmeOrphanJoinRequest
  469. *
  470. * @brief Orphan Join Request
  471. *
  472. * @param pBuf - pointer to the received buffer
  473. *
  474. * @return void
  475. ***************************************************************************************************/
  476. void MT_NlmeOrphanJoinRequest(uint8 *pBuf)
  477. {
  478. uint8 i, j, attr;
  479. uint8 retValue = ZFailure;
  480. uint32 channelList;
  481. uint8 cmdId;
  482. if ( ZSTACK_END_DEVICE_BUILD )
  483. {
  484. /* parse header */
  485. cmdId = pBuf[MT_RPC_POS_CMD1];
  486. pBuf += MT_RPC_FRAME_HDR_SZ;
  487. /* Channel list bit mask */
  488. channelList = osal_build_uint32(pBuf, 4);
  489. pBuf += sizeof(uint32);
  490. /* Count number of channels */
  491. j = attr = 0;
  492. for (i = 0; i < ED_SCAN_MAXCHANNELS; i++)
  493. {
  494. if (channelList & (1 << i))
  495. {
  496. j++;
  497. attr = i;
  498. }
  499. }
  500. /* If only one channel specified */
  501. if (j == 1)
  502. {
  503. _NIB.scanDuration = *pBuf;
  504. _NIB.nwkLogicalChannel = attr;
  505. _NIB.channelList = channelList;
  506. if ( !_NIB.CapabilityInfo )
  507. {
  508. _NIB.CapabilityInfo = ZDO_Config_Node_Descriptor.CapabilityFlags;
  509. }
  510. devState = DEV_NWK_ORPHAN;
  511. retValue = (byte)NLME_OrphanJoinRequest(channelList, *pBuf);
  512. }
  513. else
  514. {
  515. retValue = ZNwkInvalidParam;
  516. }
  517. }
  518. else
  519. {
  520. retValue = ZUnsupportedMode;
  521. }
  522. /* Build and send back the response */
  523. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_NWK), cmdId, 1, &retValue);
  524. }
  525. /***************************************************************************************************
  526. * @fn MT_Nwk_DataRequest
  527. *
  528. * @brief Nwk Data Request
  529. *
  530. * @param dstAddr, nsduLen, nsdu, nsduHandle, nsduHandleOptions, secure, discoverRoute, radius
  531. *
  532. * @return void
  533. ***************************************************************************************************/
  534. uint8 MT_Nwk_DataRequest(uint16 dstAddr, uint8 nsduLen, uint8* nsdu,
  535. uint8 nsduHandle, uint16 nsduHandleOptions,
  536. uint8 secure, uint8 discoverRoute,
  537. uint8 radius)
  538. {
  539. uint8 status;
  540. NLDE_DataReqAlloc_t dra;
  541. NLDE_DataReq_t* req;
  542. dra.overhead = sizeof(NLDE_DataReq_t);
  543. dra.nsduLen = nsduLen;
  544. dra.secure = secure;
  545. req = NLDE_DataReqAlloc(&dra);
  546. if ( req != NULL )
  547. {
  548. osal_memcpy(req->nfd.nsdu, nsdu, nsduLen);
  549. req->nfd.dstAddr = dstAddr;
  550. req->nfd.nsduHandleOptions = nsduHandleOptions;
  551. req->nfd.discoverRoute = discoverRoute;
  552. req->nfd.radius = radius;
  553. status = NLDE_DataReq( req );
  554. }
  555. else
  556. {
  557. status = ZMemError;
  558. }
  559. return status;
  560. }
  561. #endif /* MT_NWK_FUNC */
  562. #if defined ( MT_NWK_CB_FUNC ) //NWK callback commands
  563. /***************************************************************************************************
  564. * @fn nwk_MTCallbackSubDataConfirm
  565. *
  566. * @brief Process the callback subscription for NLDE-DATA.confirm
  567. *
  568. * @param nsduHandle - APS handle
  569. * @param Status - result of data request
  570. *
  571. * @return none
  572. ***************************************************************************************************/
  573. void nwk_MTCallbackSubDataConfirm(byte nsduHandle, ZStatus_t status)
  574. {
  575. byte buf[2];
  576. buf[0] = nsduHandle;
  577. buf[1] = (byte)status;
  578. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_AREQ | (uint8)MT_RPC_SYS_NWK), MT_NLDE_DATA_CONF, 2, buf );
  579. }
  580. /***************************************************************************************************
  581. * @fn nwk_MTCallbackSubDataIndication
  582. *
  583. * @brief Process the callback subscription for NLDE-DATA.indication
  584. *
  585. * @param SrcAddress - 16 bit address
  586. * @param nsduLength - Length of incoming data
  587. * @param nsdu - Pointer to incoming data
  588. * @param LinkQuality - Link quality measured during
  589. * reception.
  590. *
  591. * @return none
  592. ***************************************************************************************************/
  593. void nwk_MTCallbackSubDataIndication(uint16 SrcAddress, int16 nsduLength, byte *nsdu, byte LinkQuality)
  594. {
  595. byte *msgPtr;
  596. byte *msg;
  597. byte msgLen;
  598. msgLen = sizeof( uint16 ) + sizeof( uint8 ) + ZTEST_DEFAULT_DATA_LEN
  599. + sizeof( byte);
  600. msgPtr = osal_mem_alloc( msgLen );
  601. if ( msgPtr )
  602. {
  603. //Fill up the data bytes
  604. msg = msgPtr;
  605. //First fill in details
  606. *msg++ = LO_UINT16( SrcAddress );
  607. *msg++ = HI_UINT16( SrcAddress );
  608. //Since the max packet size is less than 255 bytes, a byte is enough
  609. //to represent nsdu length
  610. *msg++ = ( uint8 ) nsduLength;
  611. osal_memset( msg, NULL, ZTEST_DEFAULT_DATA_LEN ); // Clear the mem
  612. osal_memcpy( msg, nsdu, nsduLength );
  613. msg += ZTEST_DEFAULT_DATA_LEN;
  614. *msg++ = LinkQuality;
  615. MT_BuildAndSendZToolResponse( ((uint8)MT_RPC_CMD_AREQ | (uint8)MT_RPC_SYS_NWK), MT_NLDE_DATA_IND, msgLen, msgPtr );
  616. osal_mem_free( msgPtr );
  617. }
  618. }
  619. /***************************************************************************************************
  620. * @fn nwk_MTCallbackSubInitCoordConfirm
  621. *
  622. * @brief Process the callback subscription for NLME-INIT-COORD.confirm
  623. *
  624. * @param Status - Result of NLME_InitCoordinatorRequest()
  625. *
  626. * @return none
  627. ***************************************************************************************************/
  628. void nwk_MTCallbackSubInitCoordConfirm( ZStatus_t Status )
  629. {
  630. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_AREQ | (uint8)MT_RPC_SYS_NWK), MT_NLME_NETWORK_FORMATION_CONF,
  631. sizeof(byte), (byte*)&Status);
  632. }
  633. /***************************************************************************************************
  634. * @fn nwk_MTCallbackSubStartRouterConfirm
  635. *
  636. * @brief Process the callback subscription for NLME-START-ROUTER.confirm
  637. *
  638. * @param Status - Result of NLME_StartRouterRequest()
  639. *
  640. * @return none
  641. ***************************************************************************************************/
  642. void nwk_MTCallbackSubStartRouterConfirm( ZStatus_t Status )
  643. {
  644. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_AREQ | (uint8)MT_RPC_SYS_NWK), MT_NLME_START_ROUTER_CONF,
  645. sizeof(byte), (byte*)&Status);
  646. }
  647. /***************************************************************************************************
  648. * @fn nwk_MTCallbackSubJoinConfirm
  649. *
  650. * @brief Process the callback subscription for NLME-JOIN.confirm
  651. *
  652. * @param Status - Result of NLME_JoinRequest()
  653. *
  654. * @return none
  655. ***************************************************************************************************/
  656. void nwk_MTCallbackSubJoinConfirm(uint16 PanId, ZStatus_t Status)
  657. {
  658. byte msg[Z_EXTADDR_LEN + 3];
  659. /* This device's 64-bit address */
  660. ZMacGetReq( ZMacExtAddr, msg );
  661. msg[Z_EXTADDR_LEN + 0] = LO_UINT16(PanId);
  662. msg[Z_EXTADDR_LEN + 1] = HI_UINT16(PanId);
  663. msg[Z_EXTADDR_LEN + 2] = (byte)Status;
  664. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_AREQ | (uint8)MT_RPC_SYS_NWK), MT_NLME_JOIN_CONF, Z_EXTADDR_LEN + 3, msg );
  665. }
  666. /***************************************************************************************************
  667. * @fn nwk_MTCallbackSubNetworkDiscoveryConfirm
  668. *
  669. * @brief Process the callback subscription for NLME-NWK_DISC.confirm
  670. *
  671. * @param ResultCount - number of networks discovered
  672. * @param NetworkList - pointer to list of network descriptors
  673. *
  674. * @return void
  675. ***************************************************************************************************/
  676. void nwk_MTCallbackSubNetworkDiscoveryConfirm( byte ResultCount, networkDesc_t *NetworkList )
  677. {
  678. byte len;
  679. byte *msgPtr;
  680. byte *msg;
  681. byte i;
  682. // The message cannot be bigger then SPI_TX_BUFF_MAX. Reduce resultCount if necessary
  683. if (ResultCount * sizeof(networkDesc_t) > MT_UART_TX_BUFF_MAX - (1 + SPI_0DATA_MSG_LEN))
  684. {
  685. ResultCount = (MT_UART_TX_BUFF_MAX - (1 + SPI_0DATA_MSG_LEN)) / sizeof(networkDesc_t);
  686. }
  687. len = 1 + ResultCount * sizeof(networkDesc_t);
  688. msgPtr = osal_mem_alloc( len );
  689. if ( msgPtr )
  690. {
  691. /* Fill up the data bytes */
  692. msg = msgPtr;
  693. *msg++ = ResultCount;
  694. for ( i = 0; i < ResultCount; i++ )
  695. {
  696. *msg++ = LO_UINT16( NetworkList->panId );
  697. *msg++ = HI_UINT16( NetworkList->panId );
  698. *msg++ = NetworkList->logicalChannel;
  699. *msg++ = NetworkList->beaconOrder;
  700. *msg++ = NetworkList->superFrameOrder;
  701. *msg++ = NetworkList->routerCapacity;
  702. *msg++ = NetworkList->deviceCapacity;
  703. *msg++ = NetworkList->version;
  704. *msg++ = NetworkList->stackProfile;
  705. //*msg++ = NetworkList->securityLevel;
  706. NetworkList = (networkDesc_t*)NetworkList->nextDesc;
  707. }
  708. MT_BuildAndSendZToolResponse (((uint8)MT_RPC_CMD_AREQ | (uint8)MT_RPC_SYS_NWK), MT_NLME_NETWORK_DISCOVERY_CONF, len, msgPtr);
  709. osal_mem_free( msgPtr );
  710. }
  711. }
  712. /***************************************************************************************************
  713. * @fn nwk_MTCallbackSubJoinIndication
  714. *
  715. * @brief Process the callback subscription for NLME-INIT-COORD.indication
  716. *
  717. * @param ShortAddress - 16-bit address
  718. * @param ExtendedAddress - IEEE (64-bit) address
  719. * @param CapabilityInformation - Association Capability Information
  720. *
  721. * @return ZStatus_t
  722. ***************************************************************************************************/
  723. void nwk_MTCallbackSubJoinIndication( uint16 ShortAddress, byte *ExtendedAddress,
  724. byte CapabilityInformation )
  725. {
  726. byte *msgPtr;
  727. byte *msg;
  728. byte len;
  729. len = sizeof( uint16 ) + Z_EXTADDR_LEN + sizeof( byte );
  730. msgPtr = osal_mem_alloc( len );
  731. if ( msgPtr )
  732. {
  733. /* Fill up the data bytes */
  734. msg = msgPtr;
  735. /* First fill in details */
  736. *msg++ = LO_UINT16( ShortAddress );
  737. *msg++ = HI_UINT16( ShortAddress );
  738. osal_cpyExtAddr( msg, ExtendedAddress );
  739. msg += Z_EXTADDR_LEN;
  740. *msg = CapabilityInformation;
  741. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_AREQ | (uint8)MT_RPC_SYS_NWK), MT_NLME_JOIN_IND, len, msgPtr );
  742. osal_mem_free( msgPtr );
  743. }
  744. }
  745. /***************************************************************************************************
  746. * @fn nwk_MTCallbackSubLeaveConfirm
  747. *
  748. * @brief Process the callback subscription for NLME-LEAVE.confirm
  749. *
  750. * @param DeviceAddress - IEEE (64-bit) address
  751. * @param Status - Result of NLME_LeaveRequest()
  752. *
  753. * @return none
  754. ***************************************************************************************************/
  755. void nwk_MTCallbackSubLeaveConfirm( byte *DeviceAddress, ZStatus_t Status )
  756. {
  757. byte *msgPtr;
  758. byte *msg;
  759. msgPtr = osal_mem_alloc( Z_EXTADDR_LEN + sizeof( byte ) );
  760. if ( msgPtr )
  761. {
  762. /* Fill up the data bytes */
  763. msg = msgPtr;
  764. /* First fill in details */
  765. osal_cpyExtAddr( msg, DeviceAddress );
  766. msg += Z_EXTADDR_LEN;
  767. *msg = (byte)Status;
  768. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_AREQ | (uint8)MT_RPC_SYS_NWK), MT_NLME_LEAVE_CONF,
  769. Z_EXTADDR_LEN + sizeof( byte ), msgPtr );
  770. osal_mem_free( msgPtr );
  771. }
  772. }
  773. /***************************************************************************************************
  774. * @fn nwk_MTCallbackSubLeaveIndication
  775. *
  776. * @brief Process the callback subscription for NLME-LEAVE.indication
  777. *
  778. * @param DeviceAddress - IEEE (64-bit) address
  779. *
  780. * @return NULL
  781. ***************************************************************************************************/
  782. void nwk_MTCallbackSubLeaveIndication( byte *DeviceAddress )
  783. {
  784. byte msg[Z_EXTADDR_LEN+1];
  785. /* First fill in details */
  786. if ( DeviceAddress )
  787. {
  788. osal_cpyExtAddr( msg, DeviceAddress );
  789. }
  790. else
  791. {
  792. osal_memset( msg, 0, Z_EXTADDR_LEN );
  793. }
  794. /* Status, assume good if we get this far */
  795. msg[Z_EXTADDR_LEN] = 0;
  796. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_AREQ | (uint8)MT_RPC_SYS_NWK), MT_NLME_LEAVE_IND, Z_EXTADDR_LEN+1, msg );
  797. }
  798. /***************************************************************************************************
  799. * @fn nwk_MTCallbackSubSyncIndication
  800. *
  801. * @brief Process the callback subscription for NLME-SYNC.indication
  802. *
  803. * @param none
  804. *
  805. * @return none
  806. ***************************************************************************************************/
  807. void nwk_MTCallbackSubSyncIndication( void )
  808. {
  809. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_AREQ | (uint8)MT_RPC_SYS_NWK), MT_NLME_SYNC_IND, 0, NULL );
  810. }
  811. /***************************************************************************************************
  812. * @fn nwk_MTCallbackSubPollConfirm
  813. *
  814. * @brief Process the callback subscription for NLME-POLL.confirm
  815. *
  816. * @param status - status of the poll operation
  817. *
  818. * @return none
  819. ***************************************************************************************************/
  820. void nwk_MTCallbackSubPollConfirm( byte status )
  821. {
  822. byte msg = status;
  823. MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_AREQ | (uint8)MT_RPC_SYS_NWK), MT_NLME_POLL_CONF, 1, &msg );
  824. }
  825. #endif /* NWK Callback commands */
  826. /***************************************************************************************************
  827. ***************************************************************************************************/