ZDProfile.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904
  1. /**************************************************************************************************
  2. Filename: ZDProfile.c
  3. Revised: $Date: 2009-03-12 16:25:22 -0700 (Thu, 12 Mar 2009) $
  4. Revision: $Revision: 19404 $
  5. Description: This is the Zigbee Device Profile.
  6. Copyright 2004-2009 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 "AF.h"
  39. #include "NLMEDE.h"
  40. #include "nwk_util.h"
  41. #include "APS.h"
  42. #include "AddrMgr.h"
  43. #include "ZDConfig.h"
  44. #include "ZDProfile.h"
  45. #include "ZDObject.h"
  46. #include "ZDNwkMgr.h"
  47. #if defined( LCD_SUPPORTED )
  48. #include "OnBoard.h"
  49. #endif
  50. #include "nwk_util.h"
  51. #if defined( MT_ZDO_FUNC )
  52. #include "MT_ZDO.h"
  53. #endif
  54. /*********************************************************************
  55. * MACROS
  56. */
  57. #define ZADDR_TO_AFADDR( pZADDR, AFADDR ) { \
  58. (AFADDR).endPoint = ZDP_AF_ENDPOINT; \
  59. (AFADDR).addrMode = (afAddrMode_t)(pZADDR)->addrMode; \
  60. (AFADDR).addr.shortAddr = (pZADDR)->addr.shortAddr; \
  61. }
  62. #define FillAndSendBuffer( TRANSSEQ, ADDR, ID, LEN, BUF ) { \
  63. afStatus_t stat; \
  64. ZDP_TmpBuf = (BUF)+1; \
  65. stat = fillAndSend( (TRANSSEQ), (ADDR), (ID), (LEN) ); \
  66. osal_mem_free( (BUF) ); \
  67. ZDP_TmpBuf = ZDP_Buf+1; \
  68. return stat; \
  69. }
  70. #define FillAndSendTxOptions( TRANSSEQ, ADDR, ID, LEN, TxO ) { \
  71. afStatus_t stat; \
  72. ZDP_TxOptions = (TxO); \
  73. stat = fillAndSend( (TRANSSEQ), (ADDR), (ID), (LEN) ); \
  74. ZDP_TxOptions = AF_TX_OPTIONS_NONE; \
  75. return stat; \
  76. }
  77. #define FillAndSendBufferTxOptions( TRANSSEQ, ADDR, ID, LEN, BUF, TxO ) { \
  78. afStatus_t stat; \
  79. ZDP_TmpBuf = (BUF)+1; \
  80. ZDP_TxOptions = (TxO); \
  81. stat = fillAndSend( (TRANSSEQ), (ADDR), (ID), (LEN) ); \
  82. osal_mem_free( (BUF) ); \
  83. ZDP_TmpBuf = ZDP_Buf+1; \
  84. ZDP_TxOptions = AF_TX_OPTIONS_NONE; \
  85. return stat; \
  86. }
  87. /*********************************************************************
  88. * CONSTANTS
  89. */
  90. #define ZDP_BUF_SZ 80
  91. CONST byte ZDP_AF_ENDPOINT = 0;
  92. /*********************************************************************
  93. * TYPEDEFS
  94. */
  95. typedef struct
  96. {
  97. void *next;
  98. uint8 taskID;
  99. uint16 clusterID;
  100. } ZDO_MsgCB_t;
  101. /*********************************************************************
  102. * GLOBAL VARIABLES
  103. */
  104. byte ZDP_TransID = 0;
  105. /*********************************************************************
  106. * EXTERNAL VARIABLES
  107. */
  108. extern endPointDesc_t ZDApp_epDesc;
  109. /*********************************************************************
  110. * EXTERNAL FUNCTIONS
  111. */
  112. /*********************************************************************
  113. * LOCAL FUNCTIONS
  114. */
  115. static afStatus_t fillAndSend( uint8 *transSeq, zAddrType_t *addr, cId_t clusterID, byte len );
  116. uint8 ZDO_SendMsgCBs( zdoIncomingMsg_t *inMsg );
  117. void zdpProcessAddrReq( zdoIncomingMsg_t *inMsg );
  118. /*********************************************************************
  119. * LOCAL VARIABLES
  120. */
  121. static uint8 ZDP_Buf[ ZDP_BUF_SZ ];
  122. static uint8 *ZDP_TmpBuf = ZDP_Buf+1;
  123. static byte ZDP_TxOptions = AF_TX_OPTIONS_NONE;
  124. ZDO_MsgCB_t *zdoMsgCBs = (ZDO_MsgCB_t *)NULL;
  125. /*********************************************************************
  126. * ZDO Message Processing table
  127. */
  128. typedef void (*pfnZDPMsgProcessor)( zdoIncomingMsg_t *inMsg );
  129. typedef struct
  130. {
  131. uint16 clusterID;
  132. pfnZDPMsgProcessor pFn;
  133. } zdpMsgProcItem_t;
  134. CONST zdpMsgProcItem_t zdpMsgProcs[] =
  135. {
  136. #if ( RFD_RCVC_ALWAYS_ON==TRUE ) || ( ZG_BUILD_RTR_TYPE )
  137. // These aren't processed by sleeping end devices.
  138. { NWK_addr_req, zdpProcessAddrReq },
  139. { Device_annce, ZDO_ProcessDeviceAnnce },
  140. #endif
  141. { IEEE_addr_req, zdpProcessAddrReq },
  142. { Node_Desc_req, ZDO_ProcessNodeDescReq },
  143. { Power_Desc_req, ZDO_ProcessPowerDescReq },
  144. { Simple_Desc_req, ZDO_ProcessSimpleDescReq },
  145. { Active_EP_req, ZDO_ProcessActiveEPReq },
  146. { Match_Desc_req, ZDO_ProcessMatchDescReq },
  147. #if defined ( ZDO_MGMT_NWKDISC_RESPONSE )
  148. { Mgmt_NWK_Disc_req, ZDO_ProcessMgmtNwkDiscReq },
  149. #endif
  150. #if defined ( ZDO_MGMT_LQI_RESPONSE ) && ( ZG_BUILD_RTR_TYPE )
  151. { Mgmt_Lqi_req, ZDO_ProcessMgmtLqiReq },
  152. #endif
  153. #if defined ( ZDO_MGMT_RTG_RESPONSE ) && ( ZG_BUILD_RTR_TYPE )
  154. { Mgmt_Rtg_req, ZDO_ProcessMgmtRtgReq },
  155. #endif
  156. #if defined ( ZDO_MGMT_BIND_RESPONSE ) && defined ( REFLECTOR )
  157. { Mgmt_Bind_req, ZDO_ProcessMgmtBindReq },
  158. #endif
  159. #if defined ( ZDO_MGMT_JOINDIRECT_RESPONSE ) && ( ZG_BUILD_RTR_TYPE )
  160. { Mgmt_Direct_Join_req, ZDO_ProcessMgmtDirectJoinReq },
  161. #endif
  162. #if defined ( ZDO_MGMT_LEAVE_RESPONSE )
  163. { Mgmt_Leave_req, ZDO_ProcessMgmtLeaveReq },
  164. #endif
  165. #if defined ( ZDO_MGMT_PERMIT_JOIN_RESPONSE ) && ( ZG_BUILD_RTR_TYPE )
  166. { Mgmt_Permit_Join_req, ZDO_ProcessMgmtPermitJoinReq },
  167. #endif
  168. #if defined ( ZDO_USERDESC_RESPONSE )
  169. { User_Desc_req, ZDO_ProcessUserDescReq },
  170. #endif
  171. #if defined ( ZDO_USERDESCSET_RESPONSE )
  172. { User_Desc_set, ZDO_ProcessUserDescSet },
  173. #endif
  174. #if defined ( ZDO_SERVERDISC_RESPONSE )
  175. { Server_Discovery_req, ZDO_ProcessServerDiscReq },
  176. #endif
  177. {0xFFFF, NULL} // Last
  178. };
  179. /*********************************************************************
  180. * @fn fillAndSend
  181. *
  182. * @brief Combined to reduce space
  183. *
  184. * @param
  185. * @param
  186. *
  187. * @return afStatus_t
  188. */
  189. static afStatus_t fillAndSend( uint8 *transSeq, zAddrType_t *addr, cId_t clusterID, byte len )
  190. {
  191. afAddrType_t afAddr;
  192. osal_memset( &afAddr, 0, sizeof(afAddrType_t) );
  193. ZADDR_TO_AFADDR( addr, afAddr );
  194. *(ZDP_TmpBuf-1) = *transSeq;
  195. return AF_DataRequest( &afAddr, &ZDApp_epDesc, clusterID,
  196. (uint16)(len+1), (uint8*)(ZDP_TmpBuf-1),
  197. transSeq, ZDP_TxOptions, AF_DEFAULT_RADIUS );
  198. }
  199. /*********************************************************************
  200. * @fn ZDP_SendData
  201. *
  202. * @brief This builds and send a request message that has
  203. * NWKAddrOfInterest as its only parameter.
  204. *
  205. * @param dstAddr - destination address
  206. * @param cmd - clusterID
  207. * @param dataLen - number of bytes of data
  208. * @param data - pointer to the data
  209. * @param SecurityEnable - Security Options
  210. *
  211. * @return afStatus_t
  212. */
  213. afStatus_t ZDP_SendData( uint8 *TransSeq, zAddrType_t *dstAddr, uint16 cmd,
  214. byte len, uint8 *buf, byte SecurityEnable )
  215. {
  216. uint8 *pBuf = ZDP_TmpBuf;
  217. byte cnt = len;
  218. while ( cnt-- )
  219. {
  220. *pBuf++ = *buf++;
  221. }
  222. FillAndSendTxOptions( TransSeq, dstAddr, cmd, len,
  223. (AF_DEFAULT_RADIUS | ((SecurityEnable) ? AF_EN_SECURITY : 0)) );
  224. }
  225. /*********************************************************************
  226. * @fn ZDP_NWKAddrOfInterestReq
  227. *
  228. * @brief This builds and send a request message that has
  229. * NWKAddrOfInterest as its only parameter.
  230. *
  231. * @param dstAddr - destination address
  232. * @param nwkAddr - 16 bit address
  233. * @param SecurityEnable - Security Options
  234. *
  235. * @return afStatus_t
  236. */
  237. afStatus_t ZDP_NWKAddrOfInterestReq( zAddrType_t *dstAddr, uint16 nwkAddr,
  238. byte cmd, byte SecurityEnable )
  239. {
  240. (void)SecurityEnable; // Intentionally unreferenced parameter
  241. ZDP_TmpBuf[0] = LO_UINT16( nwkAddr );
  242. ZDP_TmpBuf[1] = HI_UINT16( nwkAddr );
  243. return fillAndSend( &ZDP_TransID, dstAddr, cmd, 2 );
  244. }
  245. /*********************************************************************
  246. * Address Requests
  247. */
  248. /*********************************************************************
  249. * @fn ZDP_NwkAddrReq
  250. *
  251. * @brief This builds and send a NWK_addr_req message. This
  252. * function sends a broadcast message looking for a 16
  253. * bit address with a 64 bit address as bait.
  254. *
  255. * @param IEEEAddress - looking for this device
  256. * @param SecurityEnable - Security Options
  257. *
  258. * @return afStatus_t
  259. */
  260. afStatus_t ZDP_NwkAddrReq( uint8 *IEEEAddress, byte ReqType,
  261. byte StartIndex, byte SecurityEnable )
  262. {
  263. uint8 *pBuf = ZDP_TmpBuf;
  264. byte len = Z_EXTADDR_LEN + 1 + 1; // IEEEAddress + ReqType + StartIndex.
  265. zAddrType_t dstAddr;
  266. (void)SecurityEnable; // Intentionally unreferenced parameter
  267. if ( osal_ExtAddrEqual( saveExtAddr, IEEEAddress ) == FALSE )
  268. {
  269. dstAddr.addrMode = AddrBroadcast;
  270. dstAddr.addr.shortAddr = NWK_BROADCAST_SHORTADDR_DEVRXON;
  271. }
  272. else
  273. {
  274. dstAddr.addrMode = Addr16Bit;
  275. dstAddr.addr.shortAddr = ZDAppNwkAddr.addr.shortAddr;
  276. }
  277. pBuf = osal_cpyExtAddr( pBuf, IEEEAddress );
  278. *pBuf++ = ReqType;
  279. *pBuf++ = StartIndex;
  280. return fillAndSend( &ZDP_TransID, &dstAddr, NWK_addr_req, len );
  281. }
  282. /*********************************************************************
  283. * @fn ZDP_IEEEAddrReq
  284. *
  285. * @brief This builds and send a IEEE_addr_req message. This
  286. * function sends a unicast message looking for a 64
  287. * bit IEEE address with a 16 bit address as bait.
  288. *
  289. * @param ReqType - ZDP_IEEEADDR_REQTYPE_SINGLE or
  290. * ZDP_IEEEADDR_REQTYPE_EXTENDED
  291. * @param SecurityEnable - Security Options
  292. *
  293. * @return afStatus_t
  294. */
  295. afStatus_t ZDP_IEEEAddrReq( uint16 shortAddr, byte ReqType,
  296. byte StartIndex, byte SecurityEnable )
  297. {
  298. uint8 *pBuf = ZDP_TmpBuf;
  299. byte len = 2 + 1 + 1; // shortAddr + ReqType + StartIndex.
  300. zAddrType_t dstAddr;
  301. (void)SecurityEnable; // Intentionally unreferenced parameter
  302. dstAddr.addrMode = (afAddrMode_t)Addr16Bit;
  303. dstAddr.addr.shortAddr = shortAddr;
  304. *pBuf++ = LO_UINT16( shortAddr );
  305. *pBuf++ = HI_UINT16( shortAddr );
  306. *pBuf++ = ReqType;
  307. *pBuf++ = StartIndex;
  308. return fillAndSend( &ZDP_TransID, &dstAddr, IEEE_addr_req, len );
  309. }
  310. /*********************************************************************
  311. * @fn ZDP_MatchDescReq
  312. *
  313. * @brief This builds and send a Match_Desc_req message. This
  314. * function sends a broadcast or unicast message
  315. * requesting the list of endpoint/interfaces that
  316. * match profile ID and cluster IDs.
  317. *
  318. * @param dstAddr - destination address
  319. * @param ProfileID - Profile ID
  320. * @param NumInClusters - number of input clusters
  321. * @param InClusterList - input cluster ID list
  322. * @param NumOutClusters - number of output clusters
  323. * @param OutClusterList - output cluster ID list
  324. * @param SecurityEnable - Security Options
  325. *
  326. * @return afStatus_t
  327. */
  328. afStatus_t ZDP_MatchDescReq( zAddrType_t *dstAddr, uint16 nwkAddr,
  329. uint16 ProfileID,
  330. byte NumInClusters, cId_t *InClusterList,
  331. byte NumOutClusters, cId_t *OutClusterList,
  332. byte SecurityEnable )
  333. {
  334. uint8 *pBuf = ZDP_TmpBuf;
  335. // nwkAddr+ProfileID+NumInClusters+NumOutClusters.
  336. byte i, len = 2 + 2 + 1 + 1; // nwkAddr+ProfileID+NumInClusters+NumOutClusters.
  337. (void)SecurityEnable; // Intentionally unreferenced parameter
  338. len += (NumInClusters + NumOutClusters) * sizeof(uint16);
  339. if ( len >= ZDP_BUF_SZ-1 )
  340. {
  341. return afStatus_MEM_FAIL;
  342. }
  343. *pBuf++ = LO_UINT16( nwkAddr ); // NWKAddrOfInterest
  344. *pBuf++ = HI_UINT16( nwkAddr );
  345. *pBuf++ = LO_UINT16( ProfileID ); // Profile ID
  346. *pBuf++ = HI_UINT16( ProfileID );
  347. *pBuf++ = NumInClusters; // Input cluster list
  348. if ( NumInClusters )
  349. {
  350. for (i=0; i<NumInClusters; ++i) {
  351. *pBuf++ = LO_UINT16( InClusterList[i] );
  352. *pBuf++ = HI_UINT16( InClusterList[i] );
  353. }
  354. }
  355. *pBuf++ = NumOutClusters; // Output cluster list
  356. if ( NumOutClusters )
  357. {
  358. for (i=0; i<NumOutClusters; ++i) {
  359. *pBuf++ = LO_UINT16( OutClusterList[i] );
  360. *pBuf++ = HI_UINT16( OutClusterList[i] );
  361. }
  362. }
  363. return fillAndSend( &ZDP_TransID, dstAddr, Match_Desc_req, len );
  364. }
  365. /*********************************************************************
  366. * @fn ZDP_SimpleDescReq
  367. *
  368. * @brief This builds and send a NWK_Simple_Desc_req
  369. * message. This function sends unicast message to the
  370. * destination device.
  371. *
  372. * @param dstAddr - destination address
  373. * @param nwkAddr - 16 bit address
  374. * @param epIntf - endpoint/interface
  375. * @param SecurityEnable - Security Options
  376. *
  377. * @return afStatus_t
  378. */
  379. afStatus_t ZDP_SimpleDescReq( zAddrType_t *dstAddr, uint16 nwkAddr,
  380. byte endPoint, byte SecurityEnable )
  381. {
  382. (void)SecurityEnable; // Intentionally unreferenced parameter
  383. ZDP_TmpBuf[0] = LO_UINT16( nwkAddr );
  384. ZDP_TmpBuf[1] = HI_UINT16( nwkAddr );
  385. ZDP_TmpBuf[2] = endPoint;
  386. return fillAndSend( &ZDP_TransID, dstAddr, Simple_Desc_req, 3 );
  387. }
  388. /*********************************************************************
  389. * @fn ZDP_UserDescSet
  390. *
  391. * @brief This builds and send a User_Desc_set message to set
  392. * the user descriptor. This function sends unicast
  393. * message to the destination device.
  394. *
  395. * @param dstAddr - destination address
  396. * @param nwkAddr - 16 bit address
  397. * @param UserDescriptor - user descriptor
  398. * @param SecurityEnable - Security Options
  399. *
  400. * @return afStatus_t
  401. */
  402. afStatus_t ZDP_UserDescSet( zAddrType_t *dstAddr, uint16 nwkAddr,
  403. UserDescriptorFormat_t *UserDescriptor,
  404. byte SecurityEnable )
  405. {
  406. uint8 *pBuf = ZDP_TmpBuf;
  407. byte len = (UserDescriptor->len < AF_MAX_USER_DESCRIPTOR_LEN) ?
  408. UserDescriptor->len : AF_MAX_USER_DESCRIPTOR_LEN;
  409. byte addrLen = 2;
  410. (void)SecurityEnable; // Intentionally unreferenced parameter
  411. *pBuf++ = LO_UINT16( nwkAddr );
  412. *pBuf++ = HI_UINT16( nwkAddr );
  413. *pBuf++ = len;
  414. addrLen = 3;
  415. pBuf = osal_memcpy( pBuf, UserDescriptor->desc, len );
  416. osal_memset( pBuf, AF_USER_DESCRIPTOR_FILL, AF_MAX_USER_DESCRIPTOR_LEN-len );
  417. return fillAndSend( &ZDP_TransID, dstAddr, User_Desc_set, (AF_MAX_USER_DESCRIPTOR_LEN + addrLen) );
  418. }
  419. /*********************************************************************
  420. * @fn ZDP_ServerDiscReq
  421. *
  422. * @brief Build and send a Server_Discovery_req request message.
  423. *
  424. * @param serverMask - 16-bit bit-mask of server services being sought.
  425. * @param SecurityEnable - Security Options
  426. *
  427. * @return afStatus_t
  428. */
  429. afStatus_t ZDP_ServerDiscReq( uint16 serverMask, byte SecurityEnable )
  430. {
  431. uint8 *pBuf = ZDP_TmpBuf;
  432. zAddrType_t dstAddr;
  433. dstAddr.addrMode = AddrBroadcast;
  434. dstAddr.addr.shortAddr = NWK_BROADCAST_SHORTADDR_DEVRXON;
  435. *pBuf++ = LO_UINT16( serverMask );
  436. *pBuf = HI_UINT16( serverMask );
  437. FillAndSendTxOptions( &ZDP_TransID, &dstAddr, Server_Discovery_req, 2,
  438. ((SecurityEnable) ? AF_EN_SECURITY : AF_TX_OPTIONS_NONE) );
  439. }
  440. /*********************************************************************
  441. * @fn ZDP_DeviceAnnce
  442. *
  443. * @brief This builds and send a Device_Annce message. This
  444. * function sends a broadcast message.
  445. *
  446. * @param nwkAddr - 16 bit address of the device
  447. * @param IEEEAddr - 64 bit address of the device
  448. * @param capabilities - device capabilities. This field is only
  449. * sent for v1.1 networks.
  450. * @param SecurityEnable - Security Options
  451. *
  452. * @return afStatus_t
  453. */
  454. afStatus_t ZDP_DeviceAnnce( uint16 nwkAddr, uint8 *IEEEAddr,
  455. byte capabilities, byte SecurityEnable )
  456. {
  457. zAddrType_t dstAddr;
  458. uint8 len;
  459. (void)SecurityEnable; // Intentionally unreferenced parameter
  460. dstAddr.addrMode = (afAddrMode_t)AddrBroadcast;
  461. dstAddr.addr.shortAddr = NWK_BROADCAST_SHORTADDR_DEVRXON;
  462. ZDP_TmpBuf[0] = LO_UINT16( nwkAddr );
  463. ZDP_TmpBuf[1] = HI_UINT16( nwkAddr );
  464. osal_cpyExtAddr( &ZDP_TmpBuf[2], IEEEAddr );
  465. len = 2 + Z_EXTADDR_LEN;
  466. ZDP_TmpBuf[10] = capabilities;
  467. len++;
  468. return fillAndSend( &ZDP_TransID, &dstAddr, Device_annce, len );
  469. }
  470. /*********************************************************************
  471. * Address Responses
  472. */
  473. /*********************************************************************
  474. * @fn zdpProcessAddrReq
  475. *
  476. * @brief Process an incoming NWK_addr_req or IEEE_addr_req message and then
  477. * build and send a corresponding NWK_addr_rsp or IEEE_addr_rsp msg.
  478. *
  479. * @param inMsg - incoming message
  480. *
  481. * @return none
  482. */
  483. void zdpProcessAddrReq( zdoIncomingMsg_t *inMsg )
  484. {
  485. associated_devices_t *pAssoc;
  486. uint8 reqType;
  487. uint16 aoi = INVALID_NODE_ADDR;
  488. uint8 *ieee = NULL;
  489. reqType = inMsg->asdu[(inMsg->clusterID == NWK_addr_req) ? Z_EXTADDR_LEN : sizeof( uint16 ) ];
  490. if ( inMsg->clusterID == NWK_addr_req )
  491. {
  492. ieee = inMsg->asdu;
  493. if ( osal_ExtAddrEqual( saveExtAddr, ieee ) )
  494. {
  495. aoi = ZDAppNwkAddr.addr.shortAddr;
  496. }
  497. // Handle response for sleeping end devices
  498. else if ( (ZSTACK_ROUTER_BUILD)
  499. && (((pAssoc = AssocGetWithExt( ieee )) != NULL)
  500. && (pAssoc->nodeRelation == CHILD_RFD)) )
  501. {
  502. aoi = pAssoc->shortAddr;
  503. if ( reqType != ZDP_ADDR_REQTYPE_SINGLE )
  504. reqType = 0xFF; // Force Invalid
  505. }
  506. }
  507. else // if ( inMsg->clusterID == IEEE_addr_req )
  508. {
  509. aoi = BUILD_UINT16( inMsg->asdu[0], inMsg->asdu[1] );
  510. if ( aoi == ZDAppNwkAddr.addr.shortAddr )
  511. {
  512. ieee = saveExtAddr;
  513. }
  514. else if ( (ZSTACK_ROUTER_BUILD)
  515. && (((pAssoc = AssocGetWithShort( aoi )) != NULL)
  516. && (pAssoc->nodeRelation == CHILD_RFD)) )
  517. {
  518. AddrMgrEntry_t addrEntry;
  519. addrEntry.user = ADDRMGR_USER_DEFAULT;
  520. addrEntry.index = pAssoc->addrIdx;
  521. if ( AddrMgrEntryGet( &addrEntry ) )
  522. {
  523. ieee = addrEntry.extAddr;
  524. }
  525. if ( reqType != ZDP_ADDR_REQTYPE_SINGLE )
  526. reqType = 0xFF; // Force Invalid
  527. }
  528. }
  529. if ( (aoi != INVALID_NODE_ADDR) && (ieee != NULL) )
  530. {
  531. uint8 *pBuf = ZDP_TmpBuf;
  532. // Status + IEEE-Addr + Nwk-Addr.
  533. byte len = 1 + Z_EXTADDR_LEN + 2;
  534. byte stat = ((reqType == ZDP_ADDR_REQTYPE_SINGLE) ||
  535. (reqType == ZDP_ADDR_REQTYPE_EXTENDED) ||
  536. ((reqType == ZDP_ADDR_REQTYPE_MEMBERSHIP) && (inMsg->clusterID == NWK_addr_req)) ) ?
  537. ZDP_SUCCESS : ZDP_INVALID_REQTYPE;
  538. *pBuf++ = stat;
  539. pBuf = osal_cpyExtAddr( pBuf, ieee );
  540. *pBuf++ = LO_UINT16( aoi );
  541. *pBuf++ = HI_UINT16( aoi );
  542. if ( ZSTACK_ROUTER_BUILD )
  543. {
  544. if ( (reqType == ZDP_ADDR_REQTYPE_EXTENDED) &&
  545. (aoi == ZDAppNwkAddr.addr.shortAddr) )
  546. {
  547. byte cnt = 0;
  548. uint16 *list = AssocMakeList( &cnt );
  549. if ( list != NULL )
  550. {
  551. byte idx = inMsg->asdu[(((inMsg->clusterID == NWK_addr_req) ? Z_EXTADDR_LEN : sizeof( uint16 )) + 1)];
  552. uint16 *pList = list + idx;
  553. // NumAssocDev field is only present on success.
  554. if ( cnt > idx )
  555. {
  556. cnt -= idx;
  557. len += (cnt * sizeof( uint16 ));
  558. }
  559. else
  560. {
  561. cnt = 0;
  562. }
  563. *pBuf++ = cnt;
  564. len++;
  565. // StartIndex field is only present if NumAssocDev field is non-zero.
  566. *pBuf++ = idx;
  567. len++;
  568. while ( cnt != 0 )
  569. {
  570. *pBuf++ = LO_UINT16( *pList );
  571. *pBuf++ = HI_UINT16( *pList );
  572. pList++;
  573. cnt--;
  574. }
  575. osal_mem_free( (uint8 *)list );
  576. }
  577. else
  578. {
  579. // NumAssocDev field is only present on success.
  580. *pBuf++ = 0;
  581. len++;
  582. }
  583. }
  584. }
  585. ZDP_TxOptions = AF_MSG_ACK_REQUEST;
  586. fillAndSend( &(inMsg->TransSeq), &(inMsg->srcAddr), (cId_t)(inMsg->clusterID | ZDO_RESPONSE_BIT), len );
  587. ZDP_TxOptions = AF_TX_OPTIONS_NONE;
  588. }
  589. }
  590. /*********************************************************************
  591. * @fn ZDP_NodeDescMsg
  592. *
  593. * @brief Builds and sends a Node Descriptor message, unicast to the
  594. * specified device.
  595. *
  596. * @param inMsg - incoming message
  597. * @param nwkAddr - 16 bit network address for device
  598. * @param pNodeDesc - pointer to the node descriptor
  599. *
  600. * @return afStatus_t
  601. */
  602. afStatus_t ZDP_NodeDescMsg( zdoIncomingMsg_t *inMsg,
  603. uint16 nwkAddr, NodeDescriptorFormat_t *pNodeDesc )
  604. {
  605. uint8 *pBuf = ZDP_TmpBuf;
  606. byte len;
  607. len = 1 + 2 + 13; // Status + nwkAddr + Node descriptor
  608. *pBuf++ = ZDP_SUCCESS;
  609. *pBuf++ = LO_UINT16( nwkAddr );
  610. *pBuf++ = HI_UINT16( nwkAddr );
  611. *pBuf++ = (byte)((pNodeDesc->ComplexDescAvail << 3) |
  612. (pNodeDesc->UserDescAvail << 4) |
  613. (pNodeDesc->LogicalType & 0x07));
  614. *pBuf++ = (byte)((pNodeDesc->FrequencyBand << 3) | (pNodeDesc->APSFlags & 0x07));
  615. *pBuf++ = pNodeDesc->CapabilityFlags;
  616. *pBuf++ = pNodeDesc->ManufacturerCode[0];
  617. *pBuf++ = pNodeDesc->ManufacturerCode[1];
  618. *pBuf++ = pNodeDesc->MaxBufferSize;
  619. *pBuf++ = pNodeDesc->MaxInTransferSize[0];
  620. *pBuf++ = pNodeDesc->MaxInTransferSize[1];
  621. *pBuf++ = LO_UINT16( pNodeDesc->ServerMask );
  622. *pBuf++ = HI_UINT16( pNodeDesc->ServerMask );
  623. *pBuf++ = pNodeDesc->MaxOutTransferSize[0];
  624. *pBuf++ = pNodeDesc->MaxOutTransferSize[1];
  625. *pBuf = pNodeDesc->DescriptorCapability;
  626. return fillAndSend( &(inMsg->TransSeq), &(inMsg->srcAddr), Node_Desc_rsp, len );
  627. }
  628. /*********************************************************************
  629. * @fn ZDP_PowerDescMsg
  630. *
  631. * @brief Builds and sends a Power Descriptor message, unicast to the
  632. * specified device.
  633. *
  634. * @param inMsg - incoming message (request)
  635. * @param nwkAddr - 16 bit network address for device
  636. * @param pPowerDesc - pointer to the node descriptor
  637. *
  638. * @return afStatus_t
  639. */
  640. afStatus_t ZDP_PowerDescMsg( zdoIncomingMsg_t *inMsg,
  641. uint16 nwkAddr, NodePowerDescriptorFormat_t *pPowerDesc )
  642. {
  643. uint8 *pBuf = ZDP_TmpBuf;
  644. byte len = 1 + 2 + 2; // Status + nwkAddr + Node Power descriptor.
  645. *pBuf++ = ZDP_SUCCESS;
  646. *pBuf++ = LO_UINT16( nwkAddr );
  647. *pBuf++ = HI_UINT16( nwkAddr );
  648. *pBuf++ = (byte)((pPowerDesc->AvailablePowerSources << 4)
  649. | (pPowerDesc->PowerMode & 0x0F));
  650. *pBuf++ = (byte)((pPowerDesc->CurrentPowerSourceLevel << 4)
  651. | (pPowerDesc->CurrentPowerSource & 0x0F));
  652. return fillAndSend( &(inMsg->TransSeq), &(inMsg->srcAddr), Power_Desc_rsp, len );
  653. }
  654. /*********************************************************************
  655. * @fn ZDP_SimpleDescMsg
  656. *
  657. * @brief Builds and sends a Simple Descriptor message, unicast to the
  658. * specified device.
  659. *
  660. * @param inMsg - incoming message (request)
  661. * @param Status - message status (ZDP_SUCCESS or other)
  662. * @param pSimpleDesc - pointer to the node descriptor
  663. *
  664. * @return afStatus_t
  665. */
  666. afStatus_t ZDP_SimpleDescMsg( zdoIncomingMsg_t *inMsg, byte Status,
  667. SimpleDescriptionFormat_t *pSimpleDesc )
  668. {
  669. uint8 *pBuf = ZDP_TmpBuf;
  670. uint8 i, len;
  671. if ( Status == ZDP_SUCCESS && pSimpleDesc )
  672. {
  673. // Status + NWKAddrOfInterest + desc length + empty simple descriptor.
  674. len = 1 + 2 + 1 + 8;
  675. len += (pSimpleDesc->AppNumInClusters + pSimpleDesc->AppNumOutClusters) * sizeof ( uint16 );
  676. }
  677. else
  678. {
  679. len = 1 + 2 + 1; // Status + desc length
  680. }
  681. if ( len >= ZDP_BUF_SZ-1 )
  682. {
  683. return afStatus_MEM_FAIL;
  684. }
  685. *pBuf++ = Status;
  686. *pBuf++ = LO_UINT16( ZDAppNwkAddr.addr.shortAddr );
  687. *pBuf++ = HI_UINT16( ZDAppNwkAddr.addr.shortAddr );
  688. if ( len > 4 )
  689. {
  690. *pBuf++ = len - 4; // Simple descriptor length
  691. *pBuf++ = pSimpleDesc->EndPoint;
  692. *pBuf++ = LO_UINT16( pSimpleDesc->AppProfId );
  693. *pBuf++ = HI_UINT16( pSimpleDesc->AppProfId );
  694. *pBuf++ = LO_UINT16( pSimpleDesc->AppDeviceId );
  695. *pBuf++ = HI_UINT16( pSimpleDesc->AppDeviceId );
  696. *pBuf++ = (byte)(pSimpleDesc->AppDevVer << 4);
  697. *pBuf++ = pSimpleDesc->AppNumInClusters;
  698. if ( pSimpleDesc->AppNumInClusters )
  699. {
  700. for (i=0; i<pSimpleDesc->AppNumInClusters; ++i)
  701. {
  702. *pBuf++ = LO_UINT16( pSimpleDesc->pAppInClusterList[i] );
  703. *pBuf++ = HI_UINT16( pSimpleDesc->pAppInClusterList[i] );
  704. }
  705. }
  706. *pBuf++ = pSimpleDesc->AppNumOutClusters;
  707. if ( pSimpleDesc->AppNumOutClusters )
  708. {
  709. for (i=0; i<pSimpleDesc->AppNumOutClusters; ++i)
  710. {
  711. *pBuf++ = LO_UINT16( pSimpleDesc->pAppOutClusterList[i] );
  712. *pBuf++ = HI_UINT16( pSimpleDesc->pAppOutClusterList[i] );
  713. }
  714. }
  715. }
  716. else
  717. {
  718. *pBuf = 0; // Description Length = 0;
  719. }
  720. return fillAndSend( &(inMsg->TransSeq), &(inMsg->srcAddr), Simple_Desc_rsp, len );
  721. }
  722. /*********************************************************************
  723. * @fn ZDP_EPRsp
  724. *
  725. * @brief This builds and send an endpoint list. Used in
  726. * Active_EP_rsp and Match_Desc_Rsp
  727. * message. This function sends unicast message to the
  728. * requesting device.
  729. *
  730. * @param MsgType - either Active_EP_rsp or Match_Desc_Rsp
  731. * @param dstAddr - destination address
  732. * @param Status - message status (ZDP_SUCCESS or other)
  733. * @param nwkAddr - Device's short address that this response describes
  734. * @param Count - number of endpoint/interfaces in list
  735. * @param pEPIntfList - Array of Endpoint/Interfaces
  736. * @param SecurityEnable - Security Options
  737. *
  738. * @return afStatus_t
  739. */
  740. afStatus_t ZDP_EPRsp( uint16 MsgType, byte TransSeq, zAddrType_t *dstAddr,
  741. byte Status, uint16 nwkAddr, byte Count,
  742. uint8 *pEPList,
  743. byte SecurityEnable )
  744. {
  745. uint8 *pBuf = ZDP_TmpBuf;
  746. byte len = 1 + 2 + 1; // Status + nwkAddr + endpoint/interface count.
  747. byte txOptions;
  748. (void)SecurityEnable; // Intentionally unreferenced parameter
  749. if ( MsgType == Match_Desc_rsp )
  750. txOptions = AF_MSG_ACK_REQUEST;
  751. else
  752. txOptions = 0;
  753. *pBuf++ = Status;
  754. *pBuf++ = LO_UINT16( nwkAddr );
  755. *pBuf++ = HI_UINT16( nwkAddr );
  756. *pBuf++ = Count; // Endpoint/Interface count
  757. if ( Count )
  758. {
  759. len += Count;
  760. osal_memcpy( pBuf, pEPList, Count );
  761. }
  762. FillAndSendTxOptions( &TransSeq, dstAddr, MsgType, len, txOptions );
  763. }
  764. /*********************************************************************
  765. * @fn ZDP_UserDescRsp
  766. *
  767. * @brief Build and send the User Decriptor Response.
  768. *
  769. *
  770. * @param dstAddr - destination address
  771. * @param nwkAddrOfInterest -
  772. * @param userDesc -
  773. * @param SecurityEnable - Security Options
  774. *
  775. * @return ZStatus_t
  776. */
  777. ZStatus_t ZDP_UserDescRsp( byte TransSeq, zAddrType_t *dstAddr,
  778. uint16 nwkAddrOfInterest, UserDescriptorFormat_t *userDesc,
  779. byte SecurityEnable )
  780. {
  781. uint8 *pBuf = ZDP_TmpBuf;
  782. byte len = 1 + 2 + 1; // Status + nwkAddr + descriptor length.
  783. (void)SecurityEnable; // Intentionally unreferenced parameter
  784. len += userDesc->len;
  785. *pBuf++ = ZSUCCESS;
  786. *pBuf++ = LO_UINT16( nwkAddrOfInterest );
  787. *pBuf++ = HI_UINT16( nwkAddrOfInterest );
  788. *pBuf++ = userDesc->len;
  789. osal_memcpy( pBuf, userDesc->desc, userDesc->len );
  790. return (ZStatus_t)fillAndSend( &TransSeq, dstAddr, User_Desc_rsp, len );
  791. }
  792. /*********************************************************************
  793. * @fn ZDP_ServerDiscRsp
  794. *
  795. * @brief Build and send the Server_Discovery_rsp response.
  796. *
  797. * @param transID - Transaction sequence number of request.
  798. * @param dstAddr - Network Destination Address.
  799. * @param status - Status of response to request.
  800. * @param aoi - Network Address of Interest of request.
  801. * @param serverMask - Bit map of service(s) being sought.
  802. * @param SecurityEnable - Security Options
  803. *
  804. * @return ZStatus_t
  805. */
  806. ZStatus_t ZDP_ServerDiscRsp( byte transID, zAddrType_t *dstAddr, byte status,
  807. uint16 aoi, uint16 serverMask, byte SecurityEnable )
  808. {
  809. const byte len = 1 + 2; // status + aoi + mask.
  810. uint8 *pBuf = ZDP_TmpBuf;
  811. ZStatus_t stat;
  812. // Intentionally unreferenced parameters
  813. (void)aoi;
  814. (void)SecurityEnable;
  815. *pBuf++ = status;
  816. *pBuf++ = LO_UINT16( serverMask );
  817. *pBuf++ = HI_UINT16( serverMask );
  818. ZDP_TxOptions = AF_MSG_ACK_REQUEST;
  819. stat = fillAndSend( &transID, dstAddr, Server_Discovery_rsp, len );
  820. ZDP_TxOptions = AF_TX_OPTIONS_NONE;
  821. return ( stat );
  822. }
  823. /*********************************************************************
  824. * @fn ZDP_GenericRsp
  825. *
  826. * @brief Sends a response message with only the parameter status
  827. * byte and the addr of interest for data.
  828. * This function sends unicast message to the
  829. * requesting device.
  830. *
  831. * @param dstAddr - destination address
  832. * @param status - generic status for response
  833. * @param aoi - address of interest
  834. * @param dstAddr - destination address
  835. * @param rspId - response cluster ID
  836. * @param SecurityEnable - Security Options
  837. *
  838. * @return afStatus_t
  839. */
  840. afStatus_t ZDP_GenericRsp( byte TransSeq, zAddrType_t *dstAddr,
  841. byte status, uint16 aoi, uint16 rspID, byte SecurityEnable )
  842. {
  843. uint8 len;
  844. (void)SecurityEnable; // Intentionally unreferenced parameter
  845. ZDP_TmpBuf[0] = status;
  846. ZDP_TmpBuf[1] = LO_UINT16( aoi );
  847. ZDP_TmpBuf[2] = HI_UINT16( aoi );
  848. // Length byte
  849. ZDP_TmpBuf[3] = 0;
  850. len = 4;
  851. return fillAndSend( &TransSeq, dstAddr, rspID, len );
  852. }
  853. /*********************************************************************
  854. * Binding
  855. */
  856. /*********************************************************************
  857. * @fn ZDP_EndDeviceBindReq
  858. *
  859. * @brief This builds and sends a End_Device_Bind_req message.
  860. * This function sends a unicast message.
  861. *
  862. * @param dstAddr - destination address
  863. * @param LocalCoordinator - short address of local coordinator
  864. * @param epIntf - Endpoint/Interface of Simple Desc
  865. * @param ProfileID - Profile ID
  866. *
  867. * The Input cluster list is the opposite of what you would think.
  868. * This is the output cluster list of this device
  869. * @param NumInClusters - number of input clusters
  870. * @param InClusterList - input cluster ID list
  871. *
  872. * The Output cluster list is the opposite of what you would think.
  873. * This is the input cluster list of this device
  874. * @param NumOutClusters - number of output clusters
  875. * @param OutClusterList - output cluster ID list
  876. *
  877. * @param SecurityEnable - Security Options
  878. *
  879. * @return afStatus_t
  880. */
  881. afStatus_t ZDP_EndDeviceBindReq( zAddrType_t *dstAddr,
  882. uint16 LocalCoordinator,
  883. byte endPoint,
  884. uint16 ProfileID,
  885. byte NumInClusters, cId_t *InClusterList,
  886. byte NumOutClusters, cId_t *OutClusterList,
  887. byte SecurityEnable )
  888. {
  889. uint8 *pBuf = ZDP_TmpBuf;
  890. uint8 i, len;
  891. uint8 *ieeeAddr;
  892. (void)SecurityEnable; // Intentionally unreferenced parameter
  893. // LocalCoordinator + SrcExtAddr + ep + ProfileID + NumInClusters + NumOutClusters.
  894. len = 2 + Z_EXTADDR_LEN + 1 + 2 + 1 + 1;
  895. len += (NumInClusters + NumOutClusters) * sizeof ( uint16 );
  896. if ( len >= ZDP_BUF_SZ-1 )
  897. {
  898. return afStatus_MEM_FAIL;
  899. }
  900. if ( LocalCoordinator != NLME_GetShortAddr() )
  901. {
  902. return afStatus_INVALID_PARAMETER;
  903. }
  904. *pBuf++ = LO_UINT16( LocalCoordinator );
  905. *pBuf++ = HI_UINT16( LocalCoordinator );
  906. ieeeAddr = NLME_GetExtAddr();
  907. pBuf = osal_cpyExtAddr( pBuf, ieeeAddr );
  908. *pBuf++ = endPoint;
  909. *pBuf++ = LO_UINT16( ProfileID ); // Profile ID
  910. *pBuf++ = HI_UINT16( ProfileID );
  911. *pBuf++ = NumInClusters; // Input cluster list
  912. for ( i = 0; i < NumInClusters; ++i )
  913. {
  914. *pBuf++ = LO_UINT16(InClusterList[i]);
  915. *pBuf++ = HI_UINT16(InClusterList[i]);
  916. }
  917. *pBuf++ = NumOutClusters; // Output cluster list
  918. for ( i = 0; i < NumOutClusters; ++i )
  919. {
  920. *pBuf++ = LO_UINT16(OutClusterList[i]);
  921. *pBuf++ = HI_UINT16(OutClusterList[i]);
  922. }
  923. return fillAndSend( &ZDP_TransID, dstAddr, End_Device_Bind_req, len );
  924. }
  925. /*********************************************************************
  926. * @fn ZDP_BindUnbindReq
  927. *
  928. * @brief This builds and send a Bind_req or Unbind_req message
  929. * Depending on the ClusterID. This function
  930. * sends a unicast message to the local coordinator.
  931. *
  932. * @param BindOrUnbind - either Bind_req or Unbind_req
  933. * @param dstAddr - destination address of the message
  934. * @param SourceAddr - source 64 bit address of the binding
  935. * @param SrcEPIntf - Source endpoint/interface
  936. * @param ClusterID - Binding cluster ID
  937. * @param DestinationAddr - destination 64 bit addr of binding
  938. * @param DstEPIntf - destination endpoint/interface
  939. * @param SecurityEnable - Security Options
  940. *
  941. * @return afStatus_t
  942. */
  943. afStatus_t ZDP_BindUnbindReq( uint16 BindOrUnbind, zAddrType_t *dstAddr,
  944. uint8 *SourceAddr, byte SrcEndPoint,
  945. cId_t ClusterID,
  946. zAddrType_t *destinationAddr, byte DstEndPoint,
  947. byte SecurityEnable )
  948. {
  949. uint8 *pBuf = ZDP_TmpBuf;
  950. byte len;
  951. (void)SecurityEnable; // Intentionally unreferenced parameter
  952. // SourceAddr + SrcEPIntf + ClusterID + addrMode.
  953. len = Z_EXTADDR_LEN + 1 + sizeof( cId_t ) + sizeof( uint8 );
  954. if ( destinationAddr->addrMode == Addr64Bit )
  955. len += Z_EXTADDR_LEN + 1; // +1 for DstEPIntf
  956. else if ( destinationAddr->addrMode == AddrGroup )
  957. len += sizeof ( uint16 );
  958. pBuf = osal_cpyExtAddr( pBuf, SourceAddr );
  959. *pBuf++ = SrcEndPoint;
  960. *pBuf++ = LO_UINT16( ClusterID );
  961. *pBuf++ = HI_UINT16( ClusterID );
  962. *pBuf++ = destinationAddr->addrMode;
  963. if ( destinationAddr->addrMode == Addr64Bit )
  964. {
  965. pBuf = osal_cpyExtAddr( pBuf, destinationAddr->addr.extAddr );
  966. *pBuf = DstEndPoint;
  967. }
  968. else if ( destinationAddr->addrMode == AddrGroup )
  969. {
  970. *pBuf++ = LO_UINT16( destinationAddr->addr.shortAddr );
  971. *pBuf++ = HI_UINT16( destinationAddr->addr.shortAddr );
  972. }
  973. FillAndSendTxOptions( &ZDP_TransID, dstAddr, BindOrUnbind, len, AF_MSG_ACK_REQUEST );
  974. }
  975. /*********************************************************************
  976. * Network Management
  977. */
  978. /*********************************************************************
  979. * @fn ZDP_MgmtNwkDiscReq
  980. *
  981. * @brief This builds and send a Mgmt_NWK_Disc_req message. This
  982. * function sends a unicast message.
  983. *
  984. * @param dstAddr - destination address of the message
  985. * @param ScanChannels - 32 bit address bit map
  986. * @param StartIndex - Starting index within the reporting network
  987. * list
  988. * @param SecurityEnable - Security Options
  989. *
  990. * @return afStatus_t
  991. */
  992. afStatus_t ZDP_MgmtNwkDiscReq( zAddrType_t *dstAddr,
  993. uint32 ScanChannels,
  994. byte ScanDuration,
  995. byte StartIndex,
  996. byte SecurityEnable )
  997. {
  998. uint8 *pBuf = ZDP_TmpBuf;
  999. byte len = sizeof( uint32 )+1+1; // ScanChannels + ScanDuration + StartIndex.
  1000. (void)SecurityEnable; // Intentionally unreferenced parameter
  1001. pBuf = osal_buffer_uint32( pBuf, ScanChannels );
  1002. *pBuf++ = ScanDuration;
  1003. *pBuf = StartIndex;
  1004. return fillAndSend( &ZDP_TransID, dstAddr, Mgmt_NWK_Disc_req, len );
  1005. }
  1006. /*********************************************************************
  1007. * @fn ZDP_MgmtDirectJoinReq
  1008. *
  1009. * @brief This builds and send a Mgmt_Direct_Join_req message. This
  1010. * function sends a unicast message.
  1011. *
  1012. * @param dstAddr - destination address of the message
  1013. * @param deviceAddr - 64 bit IEEE Address
  1014. * @param SecurityEnable - Security Options
  1015. *
  1016. * @return afStatus_t
  1017. */
  1018. afStatus_t ZDP_MgmtDirectJoinReq( zAddrType_t *dstAddr,
  1019. uint8 *deviceAddr,
  1020. byte capInfo,
  1021. byte SecurityEnable )
  1022. {
  1023. (void)SecurityEnable; // Intentionally unreferenced parameter
  1024. osal_cpyExtAddr( ZDP_TmpBuf, deviceAddr );
  1025. ZDP_TmpBuf[Z_EXTADDR_LEN] = capInfo;
  1026. return fillAndSend( &ZDP_TransID, dstAddr, Mgmt_Direct_Join_req, (Z_EXTADDR_LEN + 1) );
  1027. }
  1028. /*********************************************************************
  1029. * @fn ZDP_MgmtPermitJoinReq
  1030. *
  1031. * @brief This builds and send a Mgmt_Permit_Join_req message.
  1032. *
  1033. * @param dstAddr - destination address of the message
  1034. * @param duration - Permit duration
  1035. * @param TcSignificance - Trust Center Significance
  1036. *
  1037. * @return afStatus_t
  1038. */
  1039. afStatus_t ZDP_MgmtPermitJoinReq( zAddrType_t *dstAddr, byte duration,
  1040. byte TcSignificance, byte SecurityEnable )
  1041. {
  1042. (void)SecurityEnable; // Intentionally unreferenced parameter
  1043. // Build buffer
  1044. ZDP_TmpBuf[ZDP_MGMT_PERMIT_JOIN_REQ_DURATION] = duration;
  1045. ZDP_TmpBuf[ZDP_MGMT_PERMIT_JOIN_REQ_TC_SIG] = TcSignificance;
  1046. // Send the message
  1047. return fillAndSend( &ZDP_TransID, dstAddr, Mgmt_Permit_Join_req,
  1048. ZDP_MGMT_PERMIT_JOIN_REQ_SIZE );
  1049. }
  1050. /*********************************************************************
  1051. * @fn ZDP_MgmtLeaveReq
  1052. *
  1053. * @brief This builds and send a Mgmt_Leave_req message.
  1054. *
  1055. * @param dstAddr - destination address of the message
  1056. * IEEEAddr - IEEE adddress of device that is removed
  1057. * RemoveChildren - set to 1 to remove the children of the
  1058. * device as well. 0 otherwise.
  1059. * Rejoin - set to 1 if the removed device should rejoin
  1060. afterwards. 0 otherwise.
  1061. *
  1062. * @return afStatus_t
  1063. */
  1064. afStatus_t ZDP_MgmtLeaveReq( zAddrType_t *dstAddr, uint8 *IEEEAddr, uint8 RemoveChildren,
  1065. uint8 Rejoin, uint8 SecurityEnable )
  1066. {
  1067. (void)SecurityEnable; // Intentionally unreferenced parameter
  1068. osal_cpyExtAddr( ZDP_TmpBuf, IEEEAddr );
  1069. ZDP_TmpBuf[Z_EXTADDR_LEN] = 0;
  1070. if ( RemoveChildren == TRUE )
  1071. {
  1072. ZDP_TmpBuf[Z_EXTADDR_LEN] |= ZDP_MGMT_LEAVE_REQ_RC;
  1073. }
  1074. if ( Rejoin == TRUE )
  1075. {
  1076. ZDP_TmpBuf[Z_EXTADDR_LEN] |= ZDP_MGMT_LEAVE_REQ_REJOIN;
  1077. }
  1078. return fillAndSend( &ZDP_TransID, dstAddr, Mgmt_Leave_req, (Z_EXTADDR_LEN + 1) );
  1079. }
  1080. /*********************************************************************
  1081. * @fn ZDP_MgmtNwkUpdateReq
  1082. *
  1083. * @brief This builds and send a Mgmt_NWK_Update_req message. This
  1084. * function sends a unicast or broadcast message.
  1085. *
  1086. * @param dstAddr - destination address of the message
  1087. * @param ChannelMask - 32 bit address bit map
  1088. * @param ScanDuration - length of time to spend scanning each channel
  1089. * @param ScanCount - number of energy scans to be conducted
  1090. * @param NwkUpdateId - NWk Update Id value
  1091. * @param NwkManagerAddr - NWK address for device with Network Manager
  1092. * bit set in its Node Descriptor
  1093. *
  1094. * @return afStatus_t
  1095. */
  1096. afStatus_t ZDP_MgmtNwkUpdateReq( zAddrType_t *dstAddr,
  1097. uint32 ChannelMask,
  1098. uint8 ScanDuration,
  1099. uint8 ScanCount,
  1100. uint8 NwkUpdateId,
  1101. uint16 NwkManagerAddr )
  1102. {
  1103. uint8 *pBuf = ZDP_TmpBuf;
  1104. byte len = sizeof( uint32 ) + 1; // ChannelMask + ScanDuration
  1105. pBuf = osal_buffer_uint32( pBuf, ChannelMask );
  1106. *pBuf++ = ScanDuration;
  1107. if ( ScanDuration <= 0x05 )
  1108. {
  1109. // Request is to scan over channelMask
  1110. len += sizeof( uint8 );
  1111. *pBuf++ = ScanCount;
  1112. }
  1113. else if ( ( ScanDuration == 0xFE ) || ( ScanDuration == 0xFF ) )
  1114. {
  1115. // Request is to change Channel (0xFE) or apsChannelMask and NwkManagerAddr (0xFF)
  1116. len += sizeof( uint8 );
  1117. *pBuf++ = NwkUpdateId;
  1118. if ( ScanDuration == 0xFF )
  1119. {
  1120. len += sizeof( uint16 );
  1121. *pBuf++ = LO_UINT16( NwkManagerAddr );
  1122. *pBuf++ = HI_UINT16( NwkManagerAddr );
  1123. }
  1124. }
  1125. return fillAndSend( &ZDP_TransID, dstAddr, Mgmt_NWK_Update_req, len );
  1126. }
  1127. /*********************************************************************
  1128. * Network Management Responses
  1129. */
  1130. /*********************************************************************
  1131. * @fn ZDP_MgmtNwkDiscRsp
  1132. *
  1133. * @brief This builds and send a Mgmt_NWK_Disc_rsp message. This
  1134. * function sends a unicast message.
  1135. *
  1136. * @param dstAddr - destination address of the message
  1137. * @param Status - message status (ZDP_SUCCESS or other)
  1138. * @param NetworkCount - Total number of networks found
  1139. * @param StartIndex - Starting index within the reporting network
  1140. * list
  1141. * @param NetworkListCount - number of network lists included
  1142. * in this message
  1143. * @param NetworkList - List of network descriptors
  1144. * @param SecurityEnable - Security Options
  1145. *
  1146. * @return afStatus_t
  1147. */
  1148. afStatus_t ZDP_MgmtNwkDiscRsp( byte TransSeq, zAddrType_t *dstAddr,
  1149. byte Status,
  1150. byte NetworkCount,
  1151. byte StartIndex,
  1152. byte NetworkListCount,
  1153. networkDesc_t *NetworkList,
  1154. byte SecurityEnable )
  1155. {
  1156. uint8 *buf;
  1157. uint8 *pBuf;
  1158. byte len = 1+1+1+1; // Status + NetworkCount + StartIndex + NetworkCountList.
  1159. byte idx;
  1160. (void)SecurityEnable; // Intentionally unreferenced parameter
  1161. len += (NetworkListCount * ( ZDP_NETWORK_EXTENDED_DISCRIPTOR_SIZE - 2 ));
  1162. buf = osal_mem_alloc( len+1 );
  1163. if ( buf == NULL )
  1164. {
  1165. return afStatus_MEM_FAIL;
  1166. }
  1167. pBuf = buf+1;
  1168. *pBuf++ = Status;
  1169. *pBuf++ = NetworkCount;
  1170. *pBuf++ = StartIndex;
  1171. *pBuf++ = NetworkListCount;
  1172. for ( idx = 0; idx < NetworkListCount; idx++ )
  1173. {
  1174. osal_cpyExtAddr( pBuf, NetworkList->extendedPANID);
  1175. pBuf += Z_EXTADDR_LEN;
  1176. *pBuf++ = NetworkList->logicalChannel; // LogicalChannel
  1177. *pBuf = NetworkList->stackProfile; // Stack profile
  1178. *pBuf++ |= (byte)(NetworkList->version << 4); // ZigBee Version
  1179. *pBuf = NetworkList->beaconOrder; // Beacon Order
  1180. *pBuf++ |= (byte)(NetworkList->superFrameOrder << 4); // Superframe Order
  1181. if ( NetworkList->chosenRouter != INVALID_NODE_ADDR )
  1182. {
  1183. *pBuf++ = TRUE; // Permit Joining
  1184. }
  1185. else
  1186. {
  1187. *pBuf++ = FALSE;
  1188. }
  1189. NetworkList = NetworkList->nextDesc; // Move to next list entry
  1190. }
  1191. FillAndSendBuffer( &TransSeq, dstAddr, Mgmt_NWK_Disc_rsp, len, buf );
  1192. }
  1193. /*********************************************************************
  1194. * @fn ZDP_MgmtLqiRsp
  1195. *
  1196. * @brief This builds and send a Mgmt_Lqi_rsp message. This
  1197. * function sends a unicast message.
  1198. *
  1199. * @param dstAddr - destination address of the message
  1200. * @param Status - message status (ZDP_SUCCESS or other)
  1201. * @param NeighborLqiEntries - Total number of entries found
  1202. * @param StartIndex - Starting index within the reporting list
  1203. * @param NeighborLqiCount - number of lists included
  1204. * in this message
  1205. * @param NeighborLqiList - List of NeighborLqiItems. This list
  1206. * is the list to be sent, not the entire list
  1207. * @param SecurityEnable - true if secure
  1208. *
  1209. * @return ZStatus_t
  1210. */
  1211. ZStatus_t ZDP_MgmtLqiRsp( byte TransSeq, zAddrType_t *dstAddr,
  1212. byte Status,
  1213. byte NeighborLqiEntries,
  1214. byte StartIndex,
  1215. byte NeighborLqiCount,
  1216. ZDP_MgmtLqiItem_t* NeighborList,
  1217. byte SecurityEnable )
  1218. {
  1219. ZDP_MgmtLqiItem_t* list = NeighborList;
  1220. uint8 *buf, *pBuf;
  1221. byte len, x;
  1222. (void)SecurityEnable; // Intentionally unreferenced parameter
  1223. if ( ZSuccess != Status )
  1224. {
  1225. ZDP_TmpBuf[0] = Status;
  1226. return fillAndSend( &TransSeq, dstAddr, Mgmt_Lqi_rsp, 1 );
  1227. }
  1228. // (Status + NeighborLqiEntries + StartIndex + NeighborLqiCount) +
  1229. // neighbor LQI data.
  1230. len = (1 + 1 + 1 + 1) + (NeighborLqiCount * ZDP_MGMTLQI_EXTENDED_SIZE);
  1231. buf = osal_mem_alloc( len+1 );
  1232. if ( buf == NULL )
  1233. {
  1234. return afStatus_MEM_FAIL;
  1235. }
  1236. pBuf = buf+1;
  1237. *pBuf++ = Status;
  1238. *pBuf++ = NeighborLqiEntries;
  1239. *pBuf++ = StartIndex;
  1240. *pBuf++ = NeighborLqiCount;
  1241. for ( x = 0; x < NeighborLqiCount; x++ )
  1242. {
  1243. osal_cpyExtAddr( pBuf, list->extPanID); // Extended PanID
  1244. pBuf += Z_EXTADDR_LEN;
  1245. // EXTADDR
  1246. pBuf = osal_cpyExtAddr( pBuf, list->extAddr );
  1247. // NWKADDR
  1248. *pBuf++ = LO_UINT16( list->nwkAddr );
  1249. *pBuf++ = HI_UINT16( list->nwkAddr );
  1250. // DEVICETYPE
  1251. *pBuf = list->devType;
  1252. // RXONIDLE
  1253. *pBuf |= (uint8)(list->rxOnIdle << 2);
  1254. // RELATIONSHIP
  1255. *pBuf++ |= (uint8)(list->relation << 4);
  1256. // PERMITJOINING
  1257. *pBuf++ = (uint8)(list->permit);
  1258. // DEPTH
  1259. *pBuf++ = list->depth;
  1260. // LQI
  1261. *pBuf++ = list->lqi;
  1262. list++; // next list entry
  1263. }
  1264. FillAndSendBuffer( &TransSeq, dstAddr, Mgmt_Lqi_rsp, len, buf );
  1265. }
  1266. /*********************************************************************
  1267. * @fn ZDP_MgmtRtgRsp
  1268. *
  1269. * @brief This builds and send a Mgmt_Rtg_rsp message. This
  1270. * function sends a unicast message.
  1271. *
  1272. * @param dstAddr - destination address of the message
  1273. * @param Status - message status (ZDP_SUCCESS or other)
  1274. * @param RoutingTableEntries - Total number of entries
  1275. * @param StartIndex - Starting index within the reporting list
  1276. * @param RoutingTableListCount - number of entries included
  1277. * in this message
  1278. * @param RoutingTableList - List of Routing Table entries
  1279. * @param SecurityEnable - true to enable security for this message
  1280. *
  1281. * @return ZStatus_t
  1282. */
  1283. ZStatus_t ZDP_MgmtRtgRsp( byte TransSeq, zAddrType_t *dstAddr,
  1284. byte Status,
  1285. byte RoutingTableEntries,
  1286. byte StartIndex,
  1287. byte RoutingListCount,
  1288. rtgItem_t *RoutingTableList,
  1289. byte SecurityEnable )
  1290. {
  1291. uint8 *buf;
  1292. uint8 *pBuf;
  1293. // Status + RoutingTableEntries + StartIndex + RoutingListCount.
  1294. byte len = 1 + 1 + 1 + 1;
  1295. byte x;
  1296. (void)SecurityEnable; // Intentionally unreferenced parameter
  1297. // Add an array for Routing List data
  1298. len += (RoutingListCount * ZDP_ROUTINGENTRY_SIZE);
  1299. buf = osal_mem_alloc( (short)(len+1) );
  1300. if ( buf == NULL )
  1301. {
  1302. return afStatus_MEM_FAIL;
  1303. }
  1304. pBuf = buf+1;
  1305. *pBuf++ = Status;
  1306. *pBuf++ = RoutingTableEntries;
  1307. *pBuf++ = StartIndex;
  1308. *pBuf++ = RoutingListCount;
  1309. for ( x = 0; x < RoutingListCount; x++ )
  1310. {
  1311. *pBuf++ = LO_UINT16( RoutingTableList->dstAddress ); // Destination Address
  1312. *pBuf++ = HI_UINT16( RoutingTableList->dstAddress );
  1313. *pBuf++ = RoutingTableList->status;
  1314. *pBuf++ = LO_UINT16( RoutingTableList->nextHopAddress ); // Next hop
  1315. *pBuf++ = HI_UINT16( RoutingTableList->nextHopAddress );
  1316. RoutingTableList++; // Move to next list entry
  1317. }
  1318. FillAndSendBuffer( &TransSeq, dstAddr, Mgmt_Rtg_rsp, len, buf );
  1319. }
  1320. /*********************************************************************
  1321. * @fn ZDP_MgmtBindRsp
  1322. *
  1323. * @brief This builds and send a Mgmt_Bind_rsp message. This
  1324. * function sends a unicast message.
  1325. *
  1326. * @param dstAddr - destination address of the message
  1327. * @param Status - message status (ZDP_SUCCESS or other)
  1328. * @param BindingTableEntries - Total number of entries
  1329. * @param StartIndex - Starting index within the reporting list
  1330. * @param BindingTableListCount - number of entries included
  1331. * in this message
  1332. * @param BindingTableList - List of Binding Table entries
  1333. * @param SecurityEnable - Security Options
  1334. *
  1335. * @return ZStatus_t
  1336. */
  1337. ZStatus_t ZDP_MgmtBindRsp( byte TransSeq, zAddrType_t *dstAddr,
  1338. byte Status,
  1339. byte BindingTableEntries,
  1340. byte StartIndex,
  1341. byte BindingTableListCount,
  1342. apsBindingItem_t *BindingTableList,
  1343. byte SecurityEnable )
  1344. {
  1345. uint8 *buf;
  1346. uint8 *pBuf;
  1347. uint8 maxLen; // maxLen is the maximum packet length to allocate enough memory space
  1348. uint8 len; // Actual length varies due to different addrMode
  1349. uint8 x;
  1350. byte extZdpBindEntrySize = ZDP_BINDINGENTRY_SIZE + 1 + 1; // One more byte for cluserID and DstAddrMode
  1351. byte shortZdpBindEntrySize = ZDP_BINDINGENTRY_SIZE + 1 + 1 + 2 - 8 - 1; // clusterID + DstAddrMode + shortAddr - ExtAddr - DstEndpoint
  1352. (void)SecurityEnable; // Intentionally unreferenced parameter
  1353. // Status + BindingTableEntries + StartIndex + BindingTableListCount.
  1354. maxLen = 1 + 1 + 1 + 1;
  1355. maxLen += (BindingTableListCount * extZdpBindEntrySize ); //max length
  1356. buf = osal_mem_alloc( maxLen + 1 ); // +1 for transaction ID
  1357. if ( buf == NULL )
  1358. {
  1359. return afStatus_MEM_FAIL;
  1360. }
  1361. pBuf = buf+1;
  1362. *pBuf++ = Status;
  1363. *pBuf++ = BindingTableEntries;
  1364. *pBuf++ = StartIndex;
  1365. *pBuf++ = BindingTableListCount;
  1366. // Initial length = Status + BindingTableEntries + StartIndex + BindingTableListCount.
  1367. // length += ZDP_BINDINGENTRY_SIZE -- Version 1.0
  1368. // extZdpBindEntrySize -- Version 1.1 extended address mode
  1369. // shortZdpBindEntrySize -- Version 1.1 group address mode
  1370. len = 1 + 1 + 1 + 1;
  1371. for ( x = 0; x < BindingTableListCount; x++ )
  1372. {
  1373. pBuf = osal_cpyExtAddr( pBuf, BindingTableList->srcAddr );
  1374. *pBuf++ = BindingTableList->srcEP;
  1375. // Cluster ID
  1376. *pBuf++ = LO_UINT16( BindingTableList->clusterID );
  1377. *pBuf++ = HI_UINT16( BindingTableList->clusterID );
  1378. *pBuf++ = BindingTableList->dstAddr.addrMode;
  1379. if ( BindingTableList->dstAddr.addrMode == Addr64Bit )
  1380. {
  1381. len += extZdpBindEntrySize;
  1382. pBuf = osal_cpyExtAddr( pBuf, BindingTableList->dstAddr.addr.extAddr );
  1383. *pBuf++ = BindingTableList->dstEP;
  1384. }
  1385. else
  1386. {
  1387. len += shortZdpBindEntrySize;
  1388. *pBuf++ = LO_UINT16( BindingTableList->dstAddr.addr.shortAddr );
  1389. *pBuf++ = HI_UINT16( BindingTableList->dstAddr.addr.shortAddr );
  1390. }
  1391. BindingTableList++; // Move to next list entry
  1392. }
  1393. FillAndSendBuffer( &TransSeq, dstAddr, Mgmt_Bind_rsp, len, buf );
  1394. }
  1395. /*********************************************************************
  1396. * @fn ZDP_MgmtNwkUpdateNotify
  1397. *
  1398. * @brief This builds and send a Mgmt_NWK_Update_notify message. This
  1399. * function sends a unicast message.
  1400. *
  1401. * @param dstAddr - destination address of the message
  1402. * @param status - message status (ZDP_SUCCESS or other)
  1403. * @param scannedChannels - List of channels scanned by the request
  1404. * @param totalTransmissions - Total transmissions
  1405. * @param transmissionFailures - Sum of transmission failures
  1406. * @param listCount - Number of records contained in the energyValues list
  1407. * @param energyValues - List of descriptors, one for each of ListCount,
  1408. * of the enegry detect descriptors
  1409. * @param txOptions - Transmit options
  1410. * @param securityEnable - Security options
  1411. *
  1412. * @return afStatus_t
  1413. */
  1414. afStatus_t ZDP_MgmtNwkUpdateNotify( uint8 TransSeq, zAddrType_t *dstAddr,
  1415. uint8 status, uint32 scannedChannels,
  1416. uint16 totalTransmissions, uint16 transmissionFailures,
  1417. uint8 listCount, uint8 *energyValues, uint8 txOptions,
  1418. uint8 securityEnable )
  1419. {
  1420. uint8 *buf;
  1421. uint8 *pBuf;
  1422. uint8 len;
  1423. (void)securityEnable; // Intentionally unreferenced parameter
  1424. // Status + ScannedChannels + totalTransmissions + transmissionFailures + ListCount + energyValues
  1425. len = 1 + 4 + 2 + 2 + 1 + listCount;
  1426. buf = osal_mem_alloc( len+1 ); // +1 for transaction ID
  1427. if ( buf == NULL )
  1428. {
  1429. return afStatus_MEM_FAIL;
  1430. }
  1431. pBuf = buf+1;
  1432. *pBuf++ = status;
  1433. pBuf = osal_buffer_uint32( pBuf, scannedChannels );
  1434. *pBuf++ = LO_UINT16( totalTransmissions );
  1435. *pBuf++ = HI_UINT16( totalTransmissions );
  1436. *pBuf++ = LO_UINT16( transmissionFailures );
  1437. *pBuf++ = HI_UINT16( transmissionFailures );
  1438. *pBuf++ = listCount;
  1439. if ( listCount > 0 )
  1440. osal_memcpy( pBuf, energyValues, listCount );
  1441. FillAndSendBufferTxOptions( &TransSeq, dstAddr, Mgmt_NWK_Update_notify, len, buf, txOptions );
  1442. }
  1443. /*********************************************************************
  1444. * Functions to register for ZDO Over-the-air messages
  1445. */
  1446. /*********************************************************************
  1447. * @fn ZDO_RegisterForZDOMsg
  1448. *
  1449. * @brief Call this function to register of an incoming over
  1450. * the air ZDO message - probably a response message
  1451. * but requests can also be received.
  1452. * Messages are delivered to the task with ZDO_CB_MSG
  1453. * as the message ID.
  1454. *
  1455. * @param taskID - Where you would like the message delivered
  1456. * @param clusterID - What message?
  1457. *
  1458. * @return ZSuccess - successful, ZMemError if not
  1459. */
  1460. ZStatus_t ZDO_RegisterForZDOMsg( uint8 taskID, uint16 clusterID )
  1461. {
  1462. ZDO_MsgCB_t *pList;
  1463. ZDO_MsgCB_t *pLast;
  1464. ZDO_MsgCB_t *pNew;
  1465. // Look for duplicate
  1466. pList = zdoMsgCBs;
  1467. while ( pList )
  1468. {
  1469. if ( pList->taskID == taskID && pList->clusterID == clusterID )
  1470. return ( ZSuccess );
  1471. pLast = pList;
  1472. pList = (ZDO_MsgCB_t *)pList->next;
  1473. }
  1474. // Add to the list
  1475. pNew = (ZDO_MsgCB_t *)osal_mem_alloc( sizeof ( ZDO_MsgCB_t ) );
  1476. if ( pNew )
  1477. {
  1478. pNew->taskID = taskID;
  1479. pNew->clusterID = clusterID;
  1480. pNew->next = NULL;
  1481. if ( zdoMsgCBs )
  1482. {
  1483. pLast->next = pNew;
  1484. }
  1485. else
  1486. zdoMsgCBs = pNew;
  1487. return ( ZSuccess );
  1488. }
  1489. else
  1490. return ( ZMemError );
  1491. }
  1492. /*********************************************************************
  1493. * @fn ZDO_RemoveRegisteredCB
  1494. *
  1495. * @brief Call this function if you don't want to receive the
  1496. * incoming message.
  1497. *
  1498. * @param taskID - Where the messages are being delivered.
  1499. * @param clusterID - What message?
  1500. *
  1501. * @return ZSuccess - successful, ZFailure if not found
  1502. */
  1503. ZStatus_t ZDO_RemoveRegisteredCB( uint8 taskID, uint16 clusterID )
  1504. {
  1505. ZDO_MsgCB_t *pList;
  1506. ZDO_MsgCB_t *pLast = NULL;
  1507. pList = zdoMsgCBs;
  1508. while ( pList )
  1509. {
  1510. if ( pList->taskID == taskID && pList->clusterID == clusterID )
  1511. {
  1512. if ( pLast )
  1513. {
  1514. // remove this one from the linked list
  1515. pLast->next = pList->next;
  1516. }
  1517. else if ( pList->next )
  1518. {
  1519. // remove the first one from the linked list
  1520. zdoMsgCBs = pList->next;
  1521. }
  1522. else
  1523. {
  1524. // remove the only item from the list
  1525. zdoMsgCBs = (ZDO_MsgCB_t *)NULL;
  1526. }
  1527. osal_mem_free( pList );
  1528. return ( ZSuccess );
  1529. }
  1530. pLast = pList;
  1531. pList = pList->next;
  1532. }
  1533. return ( ZFailure );
  1534. }
  1535. /*********************************************************************
  1536. * @fn ZDO_SendMsgCBs
  1537. *
  1538. * @brief This function sends messages to registered tasks.
  1539. * Local to ZDO and shouldn't be called outside of ZDO.
  1540. *
  1541. * @param inMsg - incoming message
  1542. *
  1543. * @return TRUE if sent to at least 1 task, FALSE if not
  1544. */
  1545. uint8 ZDO_SendMsgCBs( zdoIncomingMsg_t *inMsg )
  1546. {
  1547. uint8 ret = FALSE;
  1548. ZDO_MsgCB_t *pList = zdoMsgCBs;
  1549. while ( pList )
  1550. {
  1551. if ( pList->clusterID == inMsg->clusterID )
  1552. {
  1553. zdoIncomingMsg_t *msgPtr;
  1554. // Send the address to the task
  1555. msgPtr = (zdoIncomingMsg_t *)osal_msg_allocate( sizeof( zdoIncomingMsg_t ) + inMsg->asduLen );
  1556. if ( msgPtr )
  1557. {
  1558. // copy struct
  1559. osal_memcpy( msgPtr, inMsg, sizeof( zdoIncomingMsg_t ));
  1560. if ( inMsg->asduLen )
  1561. {
  1562. msgPtr->asdu = (byte*)(((byte*)msgPtr) + sizeof( zdoIncomingMsg_t ));
  1563. osal_memcpy( msgPtr->asdu, inMsg->asdu, inMsg->asduLen );
  1564. }
  1565. msgPtr->hdr.event = ZDO_CB_MSG;
  1566. osal_msg_send( pList->taskID, (uint8 *)msgPtr );
  1567. ret = TRUE;
  1568. }
  1569. }
  1570. pList = (ZDO_MsgCB_t *)pList->next;
  1571. }
  1572. return ( ret );
  1573. }
  1574. /*********************************************************************
  1575. * Incoming message processor
  1576. */
  1577. /*********************************************************************
  1578. * @fn ZDP_IncomingData
  1579. *
  1580. * @brief This function indicates the transfer of a data PDU (ASDU)
  1581. * from the APS sub-layer to the ZDO.
  1582. *
  1583. * @param pData - Incoming Message
  1584. *
  1585. * @return none
  1586. */
  1587. void ZDP_IncomingData( afIncomingMSGPacket_t *pData )
  1588. {
  1589. uint8 x = 0;
  1590. uint8 handled;
  1591. zdoIncomingMsg_t inMsg;
  1592. inMsg.srcAddr.addrMode = Addr16Bit;
  1593. inMsg.srcAddr.addr.shortAddr = pData->srcAddr.addr.shortAddr;
  1594. inMsg.wasBroadcast = pData->wasBroadcast;
  1595. inMsg.clusterID = pData->clusterId;
  1596. inMsg.SecurityUse = pData->SecurityUse;
  1597. inMsg.asduLen = pData->cmd.DataLength-1;
  1598. inMsg.asdu = pData->cmd.Data+1;
  1599. inMsg.TransSeq = pData->cmd.Data[0];
  1600. inMsg.macDestAddr = pData->macDestAddr;
  1601. handled = ZDO_SendMsgCBs( &inMsg );
  1602. #if defined( MT_ZDO_CB_FUNC )
  1603. MT_ZdoDirectCB( pData, &inMsg );
  1604. #endif
  1605. while ( zdpMsgProcs[x].clusterID != 0xFFFF )
  1606. {
  1607. if ( zdpMsgProcs[x].clusterID == inMsg.clusterID )
  1608. {
  1609. zdpMsgProcs[x].pFn( &inMsg );
  1610. return;
  1611. }
  1612. x++;
  1613. }
  1614. // Handle unhandled messages
  1615. if ( !handled )
  1616. ZDApp_InMsgCB( &inMsg );
  1617. }
  1618. /*********************************************************************
  1619. *********************************************************************/