MT_NWK.c 30 KB

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