]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - znp-host-framework/znp-host-framework.git/blob - framework/mt/Zdo/mtZdo.c
7e8fc22462b6035635f9616f198adb7c10896411
[znp-host-framework/znp-host-framework.git] / framework / mt / Zdo / mtZdo.c
1 /*
2  * mtZdo.c
3  *
4  * This module contains the API for the MT ZDO Interface
5  *
6  * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com/
7  *
8  *
9  *  Redistribution and use in source and binary forms, with or without
10  *  modification, are permitted provided that the following conditions
11  *  are met:
12  *
13  *    Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  *
16  *    Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the
19  *    distribution.
20  *
21  *    Neither the name of Texas Instruments Incorporated nor the names of
22  *    its contributors may be used to endorse or promote products derived
23  *    from this software without specific prior written permission.
24  *
25  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  *
37  */
39 /*********************************************************************
40  * INCLUDES
41  */
42 #include <stdio.h>
43 #include <string.h>
44 #include <stdlib.h>
46 #include "mtZdo.h"
47 #include "mtSys.h"
48 #include "mtParser.h"
49 #include "rpc.h"
50 #include "hostConsole.h"
51 #include "dbgPrint.h"
53 /*********************************************************************
54  * MACROS
55  */
56 #define STARTDELAY 0
58 /*********************************************************************
59  * LOCAL VARIABLES
60  */
61 static mtZdoCb_t mtZdoCbs;
62 extern uint8_t srspRpcBuff[RPC_MAX_LEN];
63 extern uint8_t srspRpcLen;
65 /*********************************************************************
66  * LOCAL FUNCTIONS
67  */
68 static void processSrsp(uint8_t *rpcBuff, uint8_t rpcLen);
69 static void processStateChange(uint8_t *rpcBuff, uint8_t rpcLen);
70 static void processNwkAddrRsp(uint8_t *rpcBuff, uint8_t rpcLen);
72 /*********************************************************************
73  * @fn      processStateChange
74  *
75  * @brief  receives and decodes the ZDO State Change Ind msg
76  *
77  * @param   uint8_t *rpcBuff
78  *
79  * @return  none
80  */
81 static void processStateChange(uint8_t *rpcBuff, uint8_t rpcLen)
82 {
84         uint8_t zdoState = rpcBuff[2];
85         //passes the state to the callback function
86         if (mtZdoCbs.pfnmtZdoStateChangeInd)
87         {
88                 mtZdoCbs.pfnmtZdoStateChangeInd(zdoState);
89         }
90 }
92 /*********************************************************************
93  * @fn      zdoNwkAddrReq
94  *
95  * @brief   Send ZDO_NWK_ADDR_REQ to ZNP
96  *
97  * @param    req - Pointer to outgoing command structure
98  *
99  * @return   status
100  */
101 uint8_t zdoNwkAddrReq(NwkAddrReqFormat_t *req)
103         uint8_t status;
104         uint8_t cmInd = 0;
105         uint32_t cmdLen = 10;
106         uint8_t *cmd = malloc(cmdLen);
108         if (cmd)
109         {
111                 memcpy((cmd + cmInd), req->IEEEAddress, 8);
112                 cmInd += 8;
113                 cmd[cmInd++] = req->ReqType;
114                 cmd[cmInd++] = req->StartIndex;
116                 status = rpcSendFrame((MT_RPC_CMD_SREQ | MT_RPC_SYS_ZDO),
117                 MT_ZDO_NWK_ADDR_REQ, cmd, cmdLen);
119                 if (status == MT_RPC_SUCCESS)
120                 {
121                         rpcWaitMqClientMsg(50);
122                         status = srspRpcBuff[2];
123                 }
125                 free(cmd);
126                 return status;
127         }
128         else
129         {
130                 dbg_print(PRINT_LEVEL_WARNING, "Memory for cmd was not allocated\n");
131                 return 1;
132         }
135 /*********************************************************************
136  * @fn      zdoIeeeAddrReq
137  *
138  * @brief   Send ZDO_IEEE_ADDR_REQ to ZNP
139  *
140  * @param    req - Pointer to outgoing command structure
141  *
142  * @return   status
143  */
144 uint8_t zdoIeeeAddrReq(IeeeAddrReqFormat_t *req)
146         uint8_t status;
147         uint8_t cmInd = 0;
148         uint32_t cmdLen = 4;
149         uint8_t *cmd = malloc(cmdLen);
151         if (cmd)
152         {
154                 cmd[cmInd++] = (uint8_t)(req->ShortAddr & 0xFF);
155                 cmd[cmInd++] = (uint8_t)((req->ShortAddr >> 8) & 0xFF);
156                 cmd[cmInd++] = req->ReqType;
157                 cmd[cmInd++] = req->StartIndex;
159                 status = rpcSendFrame((MT_RPC_CMD_SREQ | MT_RPC_SYS_ZDO),
160                 MT_ZDO_IEEE_ADDR_REQ, cmd, cmdLen);
162                 if (status == MT_RPC_SUCCESS)
163                 {
164                         rpcWaitMqClientMsg(50);
165                         status = srspRpcBuff[2];
166                 }
168                 free(cmd);
169                 return status;
170         }
171         else
172         {
173                 dbg_print(PRINT_LEVEL_WARNING, "Memory for cmd was not allocated\n");
174                 return 1;
175         }
178 /*********************************************************************
179  * @fn      zdoNodeDescReq
180  *
181  * @brief   Send ZDO_NODE_DESC_REQ to ZNP
182  *
183  * @param    req - Pointer to outgoing command structure
184  *
185  * @return   status
186  */
187 uint8_t zdoNodeDescReq(NodeDescReqFormat_t *req)
189         uint8_t status;
190         uint8_t cmInd = 0;
191         uint32_t cmdLen = 4;
192         uint8_t *cmd = malloc(cmdLen);
194         if (cmd)
195         {
197                 cmd[cmInd++] = (uint8_t)(req->DstAddr & 0xFF);
198                 cmd[cmInd++] = (uint8_t)((req->DstAddr >> 8) & 0xFF);
199                 cmd[cmInd++] = (uint8_t)(req->NwkAddrOfInterest & 0xFF);
200                 cmd[cmInd++] = (uint8_t)((req->NwkAddrOfInterest >> 8) & 0xFF);
202                 status = rpcSendFrame((MT_RPC_CMD_SREQ | MT_RPC_SYS_ZDO),
203                 MT_ZDO_NODE_DESC_REQ, cmd, cmdLen);
205                 if (status == MT_RPC_SUCCESS)
206                 {
207                         rpcWaitMqClientMsg(50);
208                         status = srspRpcBuff[2];
209                 }
211                 free(cmd);
212                 return status;
213         }
214         else
215         {
216                 dbg_print(PRINT_LEVEL_WARNING, "Memory for cmd was not allocated\n");
217                 return 1;
218         }
221 /*********************************************************************
222  * @fn      zdoPowerDescReq
223  *
224  * @brief   Send ZDO_POWER_DESC_REQ to ZNP
225  *
226  * @param    req - Pointer to outgoing command structure
227  *
228  * @return   status
229  */
230 uint8_t zdoPowerDescReq(PowerDescReqFormat_t *req)
232         uint8_t status;
233         uint8_t cmInd = 0;
234         uint32_t cmdLen = 4;
235         uint8_t *cmd = malloc(cmdLen);
237         if (cmd)
238         {
240                 cmd[cmInd++] = (uint8_t)(req->DstAddr & 0xFF);
241                 cmd[cmInd++] = (uint8_t)((req->DstAddr >> 8) & 0xFF);
242                 cmd[cmInd++] = (uint8_t)(req->NwkAddrOfInterest & 0xFF);
243                 cmd[cmInd++] = (uint8_t)((req->NwkAddrOfInterest >> 8) & 0xFF);
245                 status = rpcSendFrame((MT_RPC_CMD_SREQ | MT_RPC_SYS_ZDO),
246                 MT_ZDO_POWER_DESC_REQ, cmd, cmdLen);
248                 if (status == MT_RPC_SUCCESS)
249                 {
250                         rpcWaitMqClientMsg(50);
251                         status = srspRpcBuff[2];
252                 }
254                 free(cmd);
255                 return status;
256         }
257         else
258         {
259                 dbg_print(PRINT_LEVEL_WARNING, "Memory for cmd was not allocated\n");
260                 return 1;
261         }
264 /*********************************************************************
265  * @fn      zdoSimpleDescReq
266  *
267  * @brief   Send ZDO_SIMPLE_DESC_REQ to ZNP
268  *
269  * @param    req - Pointer to outgoing command structure
270  *
271  * @return   status
272  */
273 uint8_t zdoSimpleDescReq(SimpleDescReqFormat_t *req)
275         uint8_t status;
276         uint8_t cmInd = 0;
277         uint32_t cmdLen = 5;
278         uint8_t *cmd = malloc(cmdLen);
280         if (cmd)
281         {
283                 cmd[cmInd++] = (uint8_t)(req->DstAddr & 0xFF);
284                 cmd[cmInd++] = (uint8_t)((req->DstAddr >> 8) & 0xFF);
285                 cmd[cmInd++] = (uint8_t)(req->NwkAddrOfInterest & 0xFF);
286                 cmd[cmInd++] = (uint8_t)((req->NwkAddrOfInterest >> 8) & 0xFF);
287                 cmd[cmInd++] = req->Endpoint;
289                 status = rpcSendFrame((MT_RPC_CMD_SREQ | MT_RPC_SYS_ZDO),
290                 MT_ZDO_SIMPLE_DESC_REQ, cmd, cmdLen);
292                 if (status == MT_RPC_SUCCESS)
293                 {
294                         rpcWaitMqClientMsg(50);
295                         status = srspRpcBuff[2];
296                 }
298                 free(cmd);
299                 return status;
300         }
301         else
302         {
303                 dbg_print(PRINT_LEVEL_WARNING, "Memory for cmd was not allocated\n");
304                 return 1;
305         }
308 /*********************************************************************
309  * @fn      zdoActiveEpReq
310  *
311  * @brief   Send ZDO_ACTIVE_EP_REQ to ZNP
312  *
313  * @param    req - Pointer to outgoing command structure
314  *
315  * @return   status
316  */
317 uint8_t zdoActiveEpReq(ActiveEpReqFormat_t *req)
319         uint8_t status;
320         uint8_t cmInd = 0;
321         uint32_t cmdLen = 4;
322         uint8_t *cmd = malloc(cmdLen);
324         if (cmd)
325         {
327                 cmd[cmInd++] = (uint8_t)(req->DstAddr & 0xFF);
328                 cmd[cmInd++] = (uint8_t)((req->DstAddr >> 8) & 0xFF);
329                 cmd[cmInd++] = (uint8_t)(req->NwkAddrOfInterest & 0xFF);
330                 cmd[cmInd++] = (uint8_t)((req->NwkAddrOfInterest >> 8) & 0xFF);
332                 status = rpcSendFrame((MT_RPC_CMD_SREQ | MT_RPC_SYS_ZDO),
333                 MT_ZDO_ACTIVE_EP_REQ, cmd, cmdLen);
335                 if (status == MT_RPC_SUCCESS)
336                 {
337                         rpcWaitMqClientMsg(50);
338                         status = srspRpcBuff[2];
339                 }
341                 free(cmd);
342                 return status;
343         }
344         else
345         {
346                 dbg_print(PRINT_LEVEL_WARNING, "Memory for cmd was not allocated\n");
347                 return 1;
348         }
351 /*********************************************************************
352  * @fn      zdoMatchDescReq
353  *
354  * @brief   Send ZDO_MATCH_DESC_REQ to ZNP
355  *
356  * @param    req - Pointer to outgoing command structure
357  *
358  * @return   status
359  */
360 uint8_t zdoMatchDescReq(MatchDescReqFormat_t *req)
362         uint8_t status;
363         uint8_t cmInd = 0;
364         uint32_t cmdLen = 8 + (req->NumInClusters * 2) + (req->NumOutClusters * 2);
365         uint8_t *cmd = malloc(cmdLen);
367         if (cmd)
368         {
370                 int idx;
372                 cmd[cmInd++] = (uint8_t)(req->DstAddr & 0xFF);
373                 cmd[cmInd++] = (uint8_t)((req->DstAddr >> 8) & 0xFF);
374                 cmd[cmInd++] = (uint8_t)(req->NwkAddrOfInterest & 0xFF);
375                 cmd[cmInd++] = (uint8_t)((req->NwkAddrOfInterest >> 8) & 0xFF);
376                 cmd[cmInd++] = (uint8_t)(req->ProfileID & 0xFF);
377                 cmd[cmInd++] = (uint8_t)((req->ProfileID >> 8) & 0xFF);
378                 cmd[cmInd++] = req->NumInClusters;
379                 for (idx = 0; idx < req->NumInClusters; idx++)
380                 {
381                         cmd[cmInd++] = (uint8_t)(req->InClusterList[idx] & 0xFF);
382                         cmd[cmInd++] = (uint8_t)((req->InClusterList[idx] >> 8) & 0xFF);
383                 }
384                 cmd[cmInd++] = req->NumOutClusters;
385                 for (idx = 0; idx < req->NumOutClusters; idx++)
386                 {
387                         cmd[cmInd++] = (uint8_t)(req->OutClusterList[idx] & 0xFF);
388                         cmd[cmInd++] = (uint8_t)((req->OutClusterList[idx] >> 8) & 0xFF);
389                 }
391                 status = rpcSendFrame((MT_RPC_CMD_SREQ | MT_RPC_SYS_ZDO),
392                 MT_ZDO_MATCH_DESC_REQ, cmd, cmdLen);
394                 if (status == MT_RPC_SUCCESS)
395                 {
396                         rpcWaitMqClientMsg(50);
397                         status = srspRpcBuff[2];
398                 }
400                 free(cmd);
401                 return status;
402         }
403         else
404         {
405                 dbg_print(PRINT_LEVEL_WARNING, "Memory for cmd was not allocated\n");
406                 return 1;
407         }
410 /*********************************************************************
411  * @fn      zdoComplexDescReq
412  *
413  * @brief   Send ZDO_COMPLEX_DESC_REQ to ZNP
414  *
415  * @param    req - Pointer to outgoing command structure
416  *
417  * @return   status
418  */
419 uint8_t zdoComplexDescReq(ComplexDescReqFormat_t *req)
421         uint8_t status;
422         uint8_t cmInd = 0;
423         uint32_t cmdLen = 4;
424         uint8_t *cmd = malloc(cmdLen);
426         if (cmd)
427         {
429                 cmd[cmInd++] = (uint8_t)(req->DstAddr & 0xFF);
430                 cmd[cmInd++] = (uint8_t)((req->DstAddr >> 8) & 0xFF);
431                 cmd[cmInd++] = (uint8_t)(req->NwkAddrOfInterest & 0xFF);
432                 cmd[cmInd++] = (uint8_t)((req->NwkAddrOfInterest >> 8) & 0xFF);
434                 status = rpcSendFrame((MT_RPC_CMD_SREQ | MT_RPC_SYS_ZDO),
435                 MT_ZDO_COMPLEX_DESC_REQ, cmd, cmdLen);
437                 if (status == MT_RPC_SUCCESS)
438                 {
439                         rpcWaitMqClientMsg(50);
440                         status = srspRpcBuff[2];
441                 }
443                 free(cmd);
444                 return status;
445         }
446         else
447         {
448                 dbg_print(PRINT_LEVEL_WARNING, "Memory for cmd was not allocated\n");
449                 return 1;
450         }
453 /*********************************************************************
454  * @fn      zdoUserDescReq
455  *
456  * @brief   Send ZDO_USER_DESC_REQ to ZNP
457  *
458  * @param    req - Pointer to outgoing command structure
459  *
460  * @return   status
461  */
462 uint8_t zdoUserDescReq(UserDescReqFormat_t *req)
464         uint8_t status;
465         uint8_t cmInd = 0;
466         uint32_t cmdLen = 4;
467         uint8_t *cmd = malloc(cmdLen);
469         if (cmd)
470         {
472                 cmd[cmInd++] = (uint8_t)(req->DstAddr & 0xFF);
473                 cmd[cmInd++] = (uint8_t)((req->DstAddr >> 8) & 0xFF);
474                 cmd[cmInd++] = (uint8_t)(req->NwkAddrOfInterest & 0xFF);
475                 cmd[cmInd++] = (uint8_t)((req->NwkAddrOfInterest >> 8) & 0xFF);
477                 status = rpcSendFrame((MT_RPC_CMD_SREQ | MT_RPC_SYS_ZDO),
478                 MT_ZDO_USER_DESC_REQ, cmd, cmdLen);
480                 if (status == MT_RPC_SUCCESS)
481                 {
482                         rpcWaitMqClientMsg(50);
483                         status = srspRpcBuff[2];
484                 }
486                 free(cmd);
487                 return status;
488         }
489         else
490         {
491                 dbg_print(PRINT_LEVEL_WARNING, "Memory for cmd was not allocated\n");
492                 return 1;
493         }
496 /*********************************************************************
497  * @fn      zdoDeviceAnnce
498  *
499  * @brief   Send ZDO_DEVICE_ANNCE_REQ to ZNP
500  *
501  * @param    req - Pointer to outgoing command structure
502  *
503  * @return   status
504  */
505 uint8_t zdoDeviceAnnce(DeviceAnnceFormat_t *req)
507         uint8_t status;
508         uint8_t cmInd = 0;
509         uint32_t cmdLen = 11;
510         uint8_t *cmd = malloc(cmdLen);
512         if (cmd)
513         {
515                 cmd[cmInd++] = (uint8_t)(req->NWKAddr & 0xFF);
516                 cmd[cmInd++] = (uint8_t)((req->NWKAddr >> 8) & 0xFF);
517                 memcpy((cmd + cmInd), req->IEEEAddr, 8);
518                 cmInd += 8;
519                 cmd[cmInd++] = req->Capabilities;
521                 status = rpcSendFrame((MT_RPC_CMD_SREQ | MT_RPC_SYS_ZDO),
522                 MT_ZDO_DEVICE_ANNCE, cmd, cmdLen);
524                 if (status == MT_RPC_SUCCESS)
525                 {
526                         rpcWaitMqClientMsg(50);
527                         status = srspRpcBuff[2];
528                 }
530                 free(cmd);
531                 return status;
532         }
533         else
534         {
535                 dbg_print(PRINT_LEVEL_WARNING, "Memory for cmd was not allocated\n");
536                 return 1;
537         }
540 /*********************************************************************
541  * @fn      zdoUserDescSet
542  *
543  * @brief   Send ZDO_USER_DESC_SET to ZNP
544  *
545  * @param    req - Pointer to outgoing command structure
546  *
547  * @return   status
548  */
549 uint8_t zdoUserDescSet(UserDescSetFormat_t *req)
551         uint8_t status;
552         uint8_t cmInd = 0;
553         uint32_t cmdLen = 5 + req->Len;
554         uint8_t *cmd = malloc(cmdLen);
556         if (cmd)
557         {
559                 int idx;
561                 cmd[cmInd++] = (uint8_t)(req->DstAddr & 0xFF);
562                 cmd[cmInd++] = (uint8_t)((req->DstAddr >> 8) & 0xFF);
563                 cmd[cmInd++] = (uint8_t)(req->NwkAddrOfInterest & 0xFF);
564                 cmd[cmInd++] = (uint8_t)((req->NwkAddrOfInterest >> 8) & 0xFF);
565                 cmd[cmInd++] = req->Len;
566                 for (idx = 0; idx < req->Len; idx++)
567                 {
568                         cmd[cmInd++] = req->UserDescriptor[idx];
569                 }
571                 status = rpcSendFrame((MT_RPC_CMD_SREQ | MT_RPC_SYS_ZDO),
572                 MT_ZDO_USER_DESC_SET, cmd, cmdLen);
574                 if (status == MT_RPC_SUCCESS)
575                 {
576                         rpcWaitMqClientMsg(50);
577                         status = srspRpcBuff[2];
578                 }
580                 free(cmd);
581                 return status;
582         }
583         else
584         {
585                 dbg_print(PRINT_LEVEL_WARNING, "Memory for cmd was not allocated\n");
586                 return 1;
587         }
590 /*********************************************************************
591  * @fn      zdoServerDiscReq
592  *
593  * @brief   Send ZDO_SERVER_DISC_REQ to ZNP
594  *
595  * @param    req - Pointer to outgoing command structure
596  *
597  * @return   status
598  */
599 uint8_t zdoServerDiscReq(ServerDiscReqFormat_t *req)
601         uint8_t status;
602         uint8_t cmInd = 0;
603         uint32_t cmdLen = 2;
604         uint8_t *cmd = malloc(cmdLen);
606         if (cmd)
607         {
609                 cmd[cmInd++] = (uint8_t)(req->ServerMask & 0xFF);
610                 cmd[cmInd++] = (uint8_t)((req->ServerMask >> 8) & 0xFF);
612                 status = rpcSendFrame((MT_RPC_CMD_SREQ | MT_RPC_SYS_ZDO),
613                 MT_ZDO_SERVER_DISC_REQ, cmd, cmdLen);
615                 if (status == MT_RPC_SUCCESS)
616                 {
617                         rpcWaitMqClientMsg(50);
618                         status = srspRpcBuff[2];
619                 }
621                 free(cmd);
622                 return status;
623         }
624         else
625         {
626                 dbg_print(PRINT_LEVEL_WARNING, "Memory for cmd was not allocated\n");
627                 return 1;
628         }
631 /*********************************************************************
632  * @fn      zdoEndDeviceBindReq
633  *
634  * @brief   Send ZDO_END_DEVICE_BIND_REQ to ZNP
635  *
636  * @param    req - Pointer to outgoing command structure
637  *
638  * @return   status
639  */
640 uint8_t zdoEndDeviceBindReq(EndDeviceBindReqFormat_t *req)
642         uint8_t status;
643         uint8_t cmInd = 0;
644         uint32_t cmdLen = 17 + (req->NumInClusters * 2) + (req->NumOutClusters * 2);
645         uint8_t *cmd = malloc(cmdLen);
647         if (cmd)
648         {
650                 int idx;
652                 cmd[cmInd++] = (uint8_t)(req->DstAddr & 0xFF);
653                 cmd[cmInd++] = (uint8_t)((req->DstAddr >> 8) & 0xFF);
654                 cmd[cmInd++] = (uint8_t)(req->LocalCoordinator & 0xFF);
655                 cmd[cmInd++] = (uint8_t)((req->LocalCoordinator >> 8) & 0xFF);
656                 memcpy((cmd + cmInd), req->CoordinatorIEEE, 8);
657                 cmInd += 8;
658                 cmd[cmInd++] = req->EndPoint;
659                 cmd[cmInd++] = (uint8_t)(req->ProfileID & 0xFF);
660                 cmd[cmInd++] = (uint8_t)((req->ProfileID >> 8) & 0xFF);
661                 cmd[cmInd++] = req->NumInClusters;
662                 for (idx = 0; idx < req->NumInClusters; idx++)
663                 {
664                         cmd[cmInd++] = (uint8_t)(req->InClusterList[idx] & 0xFF);
665                         cmd[cmInd++] = (uint8_t)((req->InClusterList[idx] >> 8) & 0xFF);
666                 }
667                 cmd[cmInd++] = req->NumOutClusters;
668                 for (idx = 0; idx < req->NumOutClusters; idx++)
669                 {
670                         cmd[cmInd++] = (uint8_t)(req->OutClusterList[idx] & 0xFF);
671                         cmd[cmInd++] = (uint8_t)((req->OutClusterList[idx] >> 8) & 0xFF);
672                 }
674                 status = rpcSendFrame((MT_RPC_CMD_SREQ | MT_RPC_SYS_ZDO),
675                 MT_ZDO_END_DEVICE_BIND_REQ, cmd, cmdLen);
677                 if (status == MT_RPC_SUCCESS)
678                 {
679                         rpcWaitMqClientMsg(50);
680                         status = srspRpcBuff[2];
681                 }
683                 free(cmd);
684                 return status;
685         }
686         else
687         {
688                 dbg_print(PRINT_LEVEL_WARNING, "Memory for cmd was not allocated\n");
689                 return 1;
690         }
693 /*********************************************************************
694  * @fn      zdoBindReq
695  *
696  * @brief   Send ZDO__BIND_REQ to ZNP
697  *
698  * @param    req - Pointer to outgoing command structure
699  *
700  * @return   status
701  */
702 uint8_t zdoBindReq(BindReqFormat_t *req)
704         uint8_t status;
705         uint8_t addrmd = (req->DstAddrMode == 3 ? 8 : 2);
706         uint8_t cmInd = 0;
707         uint8_t endP = (req->DstAddrMode == 3 ? 1 : 0);
708         uint32_t cmdLen = 14 + addrmd + endP;
709         uint8_t *cmd = malloc(cmdLen);
711         if (cmd)
712         {
714                 cmd[cmInd++] = (uint8_t)(req->DstAddr & 0xFF);
715                 cmd[cmInd++] = (uint8_t)((req->DstAddr >> 8) & 0xFF);
716                 memcpy((cmd + cmInd), req->SrcAddress, 8);
717                 cmInd += 8;
718                 cmd[cmInd++] = req->SrcEndpoint;
719                 cmd[cmInd++] = (uint8_t)(req->ClusterID & 0xFF);
720                 cmd[cmInd++] = (uint8_t)((req->ClusterID >> 8) & 0xFF);
721                 cmd[cmInd++] = req->DstAddrMode;
722                 memcpy((cmd + cmInd), req->DstAddress, addrmd);
723                 cmInd += addrmd;
724                 if (endP)
725                         cmd[cmInd++] = req->DstEndpoint;
727                 status = rpcSendFrame((MT_RPC_CMD_SREQ | MT_RPC_SYS_ZDO),
728                 MT_ZDO_BIND_REQ, cmd, cmdLen);
730                 if (status == MT_RPC_SUCCESS)
731                 {
732                         rpcWaitMqClientMsg(50);
733                         status = srspRpcBuff[2];
734                 }
736                 free(cmd);
737                 return status;
738         }
739         else
740         {
741                 dbg_print(PRINT_LEVEL_WARNING, "Memory for cmd was not allocated\n");
742                 return 1;
743         }
746 /*********************************************************************
747  * @fn      zdoUnbindReq
748  *
749  * @brief   Send ZDO_UNBIND_REQ to ZNP
750  *
751  * @param    req - Pointer to outgoing command structure
752  *
753  * @return   status
754  */
755 uint8_t zdoUnbindReq(UnbindReqFormat_t *req)
757         uint8_t status;
758         uint8_t cmInd = 0;
759         uint8_t addrmd = (req->DstAddrMode == 3 ? 8 : 2);
760         uint8_t endP = (req->DstAddrMode == 3 ? 1 : 0);
761         uint32_t cmdLen = 16;
762         uint8_t *cmd = malloc(cmdLen);
764         if (cmd)
765         {
767                 cmd[cmInd++] = (uint8_t)(req->DstAddr & 0xFF);
768                 cmd[cmInd++] = (uint8_t)((req->DstAddr >> 8) & 0xFF);
769                 memcpy((cmd + cmInd), req->SrcAddress, 8);
770                 cmInd += 8;
771                 cmd[cmInd++] = req->SrcEndpoint;
772                 cmd[cmInd++] = (uint8_t)(req->ClusterID & 0xFF);
773                 cmd[cmInd++] = (uint8_t)((req->ClusterID >> 8) & 0xFF);
774                 cmd[cmInd++] = req->DstAddrMode;
775                 memcpy((cmd + cmInd), req->DstAddress, addrmd);
776                 cmInd += addrmd;
777                 if (endP)
778                         cmd[cmInd++] = req->DstEndpoint;
780                 status = rpcSendFrame((MT_RPC_CMD_SREQ | MT_RPC_SYS_ZDO),
781                 MT_ZDO_UNBIND_REQ, cmd, cmdLen);
783                 if (status == MT_RPC_SUCCESS)
784                 {
785                         rpcWaitMqClientMsg(50);
786                         status = srspRpcBuff[2];
787                 }
789                 free(cmd);
790                 return status;
791         }
792         else
793         {
794                 dbg_print(PRINT_LEVEL_WARNING, "Memory for cmd was not allocated\n");
795                 return 1;
796         }
799 /*********************************************************************
800  * @fn      zdoMgmtNwkDiscReq
801  *
802  * @brief   Send ZDO_MGMT_NWK_DISC_REQ to ZNP
803  *
804  * @param    req - Pointer to outgoing command structure
805  *
806  * @return   status
807  */
808 uint8_t zdoMgmtNwkDiscReq(MgmtNwkDiscReqFormat_t *req)
810         uint8_t status;
811         uint8_t cmInd = 0;
812         uint32_t cmdLen = 8;
813         uint8_t *cmd = malloc(cmdLen);
815         if (cmd)
816         {
818                 cmd[cmInd++] = (uint8_t)(req->DstAddr & 0xFF);
819                 cmd[cmInd++] = (uint8_t)((req->DstAddr >> 8) & 0xFF);
820                 memcpy((cmd + cmInd), req->ScanChannels, 4);
821                 cmInd += 4;
822                 cmd[cmInd++] = req->ScanDuration;
823                 cmd[cmInd++] = req->StartIndex;
825                 status = rpcSendFrame((MT_RPC_CMD_SREQ | MT_RPC_SYS_ZDO),
826                 MT_ZDO_MGMT_NWK_DISC_REQ, cmd, cmdLen);
828                 if (status == MT_RPC_SUCCESS)
829                 {
830                         rpcWaitMqClientMsg(50);
831                         status = srspRpcBuff[2];
832                 }
834                 free(cmd);
835                 return status;
836         }
837         else
838         {
839                 dbg_print(PRINT_LEVEL_WARNING, "Memory for cmd was not allocated\n");
840                 return 1;
841         }
844 /*********************************************************************
845  * @fn      zdoMgmtLqiReq
846  *
847  * @brief   Send ZDO_MGMT_LQI_REQ to ZNP
848  *
849  * @param    req - Pointer to outgoing command structure
850  *
851  * @return   status
852  */
853 uint8_t zdoMgmtLqiReq(MgmtLqiReqFormat_t *req)
855         uint8_t status;
856         uint8_t cmInd = 0;
857         uint32_t cmdLen = 3;
858         uint8_t *cmd = malloc(cmdLen);
860         if (cmd)
861         {
863                 cmd[cmInd++] = (uint8_t)(req->DstAddr & 0xFF);
864                 cmd[cmInd++] = (uint8_t)((req->DstAddr >> 8) & 0xFF);
865                 cmd[cmInd++] = req->StartIndex;
867                 status = rpcSendFrame((MT_RPC_CMD_SREQ | MT_RPC_SYS_ZDO),
868                 MT_ZDO_MGMT_LQI_REQ, cmd, cmdLen);
870                 if (status == MT_RPC_SUCCESS)
871                 {
872                         rpcWaitMqClientMsg(50);
873                         status = srspRpcBuff[2];
874                 }
876                 free(cmd);
877                 return status;
878         }
879         else
880         {
881                 dbg_print(PRINT_LEVEL_WARNING, "Memory for cmd was not allocated\n");
882                 return 1;
883         }
886 /*********************************************************************
887  * @fn      zdoMgmtRtgReq
888  *
889  * @brief   Send ZDO_MGMT_RTG_REQ to ZNP
890  *
891  * @param    req - Pointer to outgoing command structure
892  *
893  * @return   status
894  */
895 uint8_t zdoMgmtRtgReq(MgmtRtgReqFormat_t *req)
897         uint8_t status;
898         uint8_t cmInd = 0;
899         uint32_t cmdLen = 3;
900         uint8_t *cmd = malloc(cmdLen);
902         if (cmd)
903         {
905                 cmd[cmInd++] = (uint8_t)(req->DstAddr & 0xFF);
906                 cmd[cmInd++] = (uint8_t)((req->DstAddr >> 8) & 0xFF);
907                 cmd[cmInd++] = req->StartIndex;
909                 status = rpcSendFrame((MT_RPC_CMD_SREQ | MT_RPC_SYS_ZDO),
910                 MT_ZDO_MGMT_RTG_REQ, cmd, cmdLen);
912                 if (status == MT_RPC_SUCCESS)
913                 {
914                         rpcWaitMqClientMsg(50);
915                         status = srspRpcBuff[2];
916                 }
918                 free(cmd);
919                 return status;
920         }
921         else
922         {
923                 dbg_print(PRINT_LEVEL_WARNING, "Memory for cmd was not allocated\n");
924                 return 1;
925         }
928 /*********************************************************************
929  * @fn      zdoMgmtBindReq
930  *
931  * @brief   Send ZDO_MGMT_BIND_REQ to ZNP
932  *
933  * @param    req - Pointer to outgoing command structure
934  *
935  * @return   status
936  */
937 uint8_t zdoMgmtBindReq(MgmtBindReqFormat_t *req)
939         uint8_t status;
940         uint8_t cmInd = 0;
941         uint32_t cmdLen = 3;
942         uint8_t *cmd = malloc(cmdLen);
944         if (cmd)
945         {
947                 cmd[cmInd++] = (uint8_t)(req->DstAddr & 0xFF);
948                 cmd[cmInd++] = (uint8_t)((req->DstAddr >> 8) & 0xFF);
949                 cmd[cmInd++] = req->StartIndex;
951                 status = rpcSendFrame((MT_RPC_CMD_SREQ | MT_RPC_SYS_ZDO),
952                 MT_ZDO_MGMT_BIND_REQ, cmd, cmdLen);
954                 if (status == MT_RPC_SUCCESS)
955                 {
956                         rpcWaitMqClientMsg(50);
957                         status = srspRpcBuff[2];
958                 }
960                 free(cmd);
961                 return status;
962         }
963         else
964         {
965                 dbg_print(PRINT_LEVEL_WARNING, "Memory for cmd was not allocated\n");
966                 return 1;
967         }
970 /*********************************************************************
971  * @fn      zdoMgmtLeaveReq
972  *
973  * @brief   Send ZDO_MGMT_LEAVE_REQ to ZNP
974  *
975  * @param    req - Pointer to outgoing command structure
976  *
977  * @return   status
978  */
979 uint8_t zdoMgmtLeaveReq(MgmtLeaveReqFormat_t *req)
981         uint8_t status;
982         uint8_t cmInd = 0;
983         uint32_t cmdLen = 11;
984         uint8_t *cmd = malloc(cmdLen);
986         if (cmd)
987         {
989                 cmd[cmInd++] = (uint8_t)(req->DstAddr & 0xFF);
990                 cmd[cmInd++] = (uint8_t)((req->DstAddr >> 8) & 0xFF);
991                 memcpy((cmd + cmInd), req->DeviceAddr, 8);
992                 cmInd += 8;
993                 cmd[cmInd++] = req->RemoveChildre_Rejoin;
995                 status = rpcSendFrame((MT_RPC_CMD_SREQ | MT_RPC_SYS_ZDO),
996                 MT_ZDO_MGMT_LEAVE_REQ, cmd, cmdLen);
998                 if (status == MT_RPC_SUCCESS)
999                 {
1000                         rpcWaitMqClientMsg(50);
1001                         status = srspRpcBuff[2];
1002                 }
1004                 free(cmd);
1005                 return status;
1006         }
1007         else
1008         {
1009                 dbg_print(PRINT_LEVEL_WARNING, "Memory for cmd was not allocated\n");
1010                 return 1;
1011         }
1014 /*********************************************************************
1015  * @fn      zdoMgmtDirectJoinReq
1016  *
1017  * @brief   Send ZDO_MGMT_DIRECT_JOIN_REQ to ZNP
1018  *
1019  * @param    req - Pointer to outgoing command structure
1020  *
1021  * @return   status
1022  */
1023 uint8_t zdoMgmtDirectJoinReq(MgmtDirectJoinReqFormat_t *req)
1025         uint8_t status;
1026         uint8_t cmInd = 0;
1027         uint32_t cmdLen = 11;
1028         uint8_t *cmd = malloc(cmdLen);
1030         if (cmd)
1031         {
1033                 cmd[cmInd++] = (uint8_t)(req->DstAddr & 0xFF);
1034                 cmd[cmInd++] = (uint8_t)((req->DstAddr >> 8) & 0xFF);
1035                 memcpy((cmd + cmInd), req->DeviceAddr, 8);
1036                 cmInd += 8;
1037                 cmd[cmInd++] = req->CapInfo;
1039                 status = rpcSendFrame((MT_RPC_CMD_SREQ | MT_RPC_SYS_ZDO),
1040                 MT_ZDO_MGMT_DIRECT_JOIN_REQ, cmd, cmdLen);
1042                 if (status == MT_RPC_SUCCESS)
1043                 {
1044                         rpcWaitMqClientMsg(50);
1045                         status = srspRpcBuff[2];
1046                 }
1048                 free(cmd);
1049                 return status;
1050         }
1051         else
1052         {
1053                 dbg_print(PRINT_LEVEL_WARNING, "Memory for cmd was not allocated\n");
1054                 return 1;
1055         }
1058 /*********************************************************************
1059  * @fn      zdoMgmtPermitJoinReq
1060  *
1061  * @brief   Send ZDO_MGMT_PERMIT_JOIN_REQ to ZNP
1062  *
1063  * @param    req - Pointer to outgoing command structure
1064  *
1065  * @return   status
1066  */
1067 uint8_t zdoMgmtPermitJoinReq(MgmtPermitJoinReqFormat_t *req)
1069         uint8_t status;
1070         uint8_t cmInd = 0;
1071         uint32_t cmdLen = 5;
1072         uint8_t *cmd = malloc(cmdLen);
1074         if (cmd)
1075         {
1077                 cmd[cmInd++] = req->AddrMode;
1078                 cmd[cmInd++] = (uint8_t)(req->DstAddr & 0xFF);
1079                 cmd[cmInd++] = (uint8_t)((req->DstAddr >> 8) & 0xFF);
1080                 cmd[cmInd++] = req->Duration;
1081                 cmd[cmInd++] = req->TCSignificance;
1083                 status = rpcSendFrame((MT_RPC_CMD_SREQ | MT_RPC_SYS_ZDO),
1084                 MT_ZDO_MGMT_PERMIT_JOIN_REQ, cmd, cmdLen);
1086                 if (status == MT_RPC_SUCCESS)
1087                 {
1088                         rpcWaitMqClientMsg(50);
1089                         status = srspRpcBuff[2];
1090                 }
1092                 free(cmd);
1093                 return status;
1094         }
1095         else
1096         {
1097                 dbg_print(PRINT_LEVEL_WARNING, "Memory for cmd was not allocated\n");
1098                 return 1;
1099         }
1102 /*********************************************************************
1103  * @fn      zdoMgmtNwkUpdateReq
1104  *
1105  * @brief   Send ZDO_MGMT_NWK_UPDATE_REQ to ZNP
1106  *
1107  * @param    req - Pointer to outgoing command structure
1108  *
1109  * @return   status
1110  */
1111 uint8_t zdoMgmtNwkUpdateReq(MgmtNwkUpdateReqFormat_t *req)
1113         uint8_t status;
1114         uint8_t cmInd = 0;
1115         uint32_t cmdLen = 11;
1116         uint8_t *cmd = malloc(cmdLen);
1118         if (cmd)
1119         {
1121                 cmd[cmInd++] = (uint8_t)(req->DstAddr & 0xFF);
1122                 cmd[cmInd++] = (uint8_t)((req->DstAddr >> 8) & 0xFF);
1123                 cmd[cmInd++] = req->DstAddrMode;
1124                 memcpy((cmd + cmInd), req->ChannelMask, 4);
1125                 cmInd += 4;
1126                 cmd[cmInd++] = req->ScanDuration;
1127                 cmd[cmInd++] = req->ScanCount;
1128                 cmd[cmInd++] = (uint8_t)(req->NwkManagerAddr & 0xFF);
1129                 cmd[cmInd++] = (uint8_t)((req->NwkManagerAddr >> 8) & 0xFF);
1131                 status = rpcSendFrame((MT_RPC_CMD_SREQ | MT_RPC_SYS_ZDO),
1132                 MT_ZDO_MGMT_NWK_UPDATE_REQ, cmd, cmdLen);
1134                 if (status == MT_RPC_SUCCESS)
1135                 {
1136                         rpcWaitMqClientMsg(50);
1137                         status = srspRpcBuff[2];
1138                 }
1140                 free(cmd);
1141                 return status;
1142         }
1143         else
1144         {
1145                 dbg_print(PRINT_LEVEL_WARNING, "Memory for cmd was not allocated\n");
1146                 return 1;
1147         }
1150 /*********************************************************************
1151  * @fn      zdoStartupFromApp
1152  *
1153  * @brief   Send ZDO_STARTUP_FROM_APP_REQ to ZNP
1154  *
1155  * @param    req - Pointer to outgoing command structure
1156  *
1157  * @return   status
1158  */
1159 uint8_t zdoStartupFromApp(StartupFromAppFormat_t *req)
1161         uint8_t status;
1162         uint8_t cmInd = 0;
1163         uint32_t cmdLen = 2;
1164         uint8_t *cmd = malloc(cmdLen);
1166         if (cmd)
1167         {
1169                 cmd[cmInd++] = LO_UINT16(req->StartDelay);
1170                 cmd[cmInd++] = HI_UINT16(req->StartDelay);
1171                 status = rpcSendFrame((MT_RPC_CMD_SREQ | MT_RPC_SYS_ZDO),
1172                 MT_ZDO_STARTUP_FROM_APP, cmd, cmdLen);
1174                 if (status == MT_RPC_SUCCESS)
1175                 {
1176                         rpcWaitMqClientMsg(50);
1177                         status = srspRpcBuff[2];
1178                 }
1180                 free(cmd);
1181                 return status;
1182         }
1183         else
1184         {
1185                 dbg_print(PRINT_LEVEL_WARNING, "Memory for cmd was not allocated\n");
1186                 return 1;
1187         }
1190 /*********************************************************************
1191  * @fn      zdoAutoFindDestination
1192  *
1193  * @brief   Send ZDO_AUTO_FIND_DESTINATION_REQ to ZNP
1194  *
1195  * @param    req - Pointer to outgoing command structure
1196  *
1197  * @return   status
1198  */
1199 uint8_t zdoAutoFindDestination(AutoFindDestinationFormat_t *req)
1201         uint8_t status;
1202         uint8_t cmInd = 0;
1203         uint32_t cmdLen = 1;
1204         uint8_t *cmd = malloc(cmdLen);
1206         if (cmd)
1207         {
1209                 cmd[cmInd++] = req->Endpoint;
1211                 status = rpcSendFrame((MT_RPC_CMD_SREQ | MT_RPC_SYS_ZDO),
1212                 MT_ZDO_AUTO_FIND_DESTINATION, cmd, cmdLen);
1214                 if (status == MT_RPC_SUCCESS)
1215                 {
1216                         rpcWaitMqClientMsg(50);
1217                         status = srspRpcBuff[2];
1218                 }
1220                 free(cmd);
1221                 return status;
1222         }
1223         else
1224         {
1225                 dbg_print(PRINT_LEVEL_WARNING, "Memory for cmd was not allocated\n");
1226                 return 1;
1227         }
1230 /*********************************************************************
1231  * @fn      zdoSetLinkKey
1232  *
1233  * @brief   Send ZDO_SET_LINK_KEY to ZNP
1234  *
1235  * @param    req - Pointer to outgoing command structure
1236  *
1237  * @return   status
1238  */
1239 uint8_t zdoSetLinkKey(SetLinkKeyFormat_t *req)
1241         uint8_t status;
1242         uint8_t cmInd = 0;
1243         uint32_t cmdLen = 26;
1244         uint8_t *cmd = malloc(cmdLen);
1246         if (cmd)
1247         {
1249                 cmd[cmInd++] = (uint8_t)(req->ShortAddr & 0xFF);
1250                 cmd[cmInd++] = (uint8_t)((req->ShortAddr >> 8) & 0xFF);
1251                 memcpy((cmd + cmInd), req->IEEEaddr, 8);
1252                 cmInd += 8;
1253                 memcpy((cmd + cmInd), req->LinkKeyData, 16);
1254                 cmInd += 16;
1256                 status = rpcSendFrame((MT_RPC_CMD_SREQ | MT_RPC_SYS_ZDO),
1257                 MT_ZDO_SET_LINK_KEY, cmd, cmdLen);
1259                 if (status == MT_RPC_SUCCESS)
1260                 {
1261                         rpcWaitMqClientMsg(50);
1262                         status = srspRpcBuff[2];
1263                 }
1265                 free(cmd);
1266                 return status;
1267         }
1268         else
1269         {
1270                 dbg_print(PRINT_LEVEL_WARNING, "Memory for cmd was not allocated\n");
1271                 return 1;
1272         }
1275 /*********************************************************************
1276  * @fn      zdoRemoveLinkKey
1277  *
1278  * @brief   Send ZDO_REMOVE_LINK_KEY to ZNP
1279  *
1280  * @param    req - Pointer to outgoing command structure
1281  *
1282  * @return   status
1283  */
1284 uint8_t zdoRemoveLinkKey(RemoveLinkKeyFormat_t *req)
1286         uint8_t status;
1287         uint8_t cmInd = 0;
1288         uint32_t cmdLen = 8;
1289         uint8_t *cmd = malloc(cmdLen);
1291         if (cmd)
1292         {
1294                 memcpy((cmd + cmInd), req->IEEEaddr, 8);
1295                 cmInd += 8;
1297                 status = rpcSendFrame((MT_RPC_CMD_SREQ | MT_RPC_SYS_ZDO),
1298                 MT_ZDO_REMOVE_LINK_KEY, cmd, cmdLen);
1300                 if (status == MT_RPC_SUCCESS)
1301                 {
1302                         rpcWaitMqClientMsg(50);
1303                         status = srspRpcBuff[2];
1304                 }
1306                 free(cmd);
1307                 return status;
1308         }
1309         else
1310         {
1311                 dbg_print(PRINT_LEVEL_WARNING, "Memory for cmd was not allocated\n");
1312                 return 1;
1313         }
1316 /*********************************************************************
1317  * @fn      zdoGetLinkKey
1318  *
1319  * @brief   Send ZDO_GET_LINK_KEY to ZNP
1320  *
1321  * @param    req - Pointer to outgoing command structure
1322  *
1323  * @return   status
1324  */
1325 uint8_t zdoGetLinkKey(GetLinkKeyFormat_t *req)
1327         uint8_t status;
1328         uint8_t cmInd = 0;
1329         uint32_t cmdLen = 8;
1330         uint8_t *cmd = malloc(cmdLen);
1332         if (cmd)
1333         {
1335                 memcpy((cmd + cmInd), req->IEEEaddr, 8);
1336                 cmInd += 8;
1338                 status = rpcSendFrame((MT_RPC_CMD_SREQ | MT_RPC_SYS_ZDO),
1339                 MT_ZDO_GET_LINK_KEY, cmd, cmdLen);
1341                 if (status == MT_RPC_SUCCESS)
1342                 {
1343                         rpcWaitMqClientMsg(50);
1344                         status = srspRpcBuff[2];
1345                 }
1347                 free(cmd);
1348                 return status;
1349         }
1350         else
1351         {
1352                 dbg_print(PRINT_LEVEL_WARNING, "Memory for cmd was not allocated\n");
1353                 return 1;
1354         }
1357 /*********************************************************************
1358  * @fn      zdoNwkDiscoveryReq
1359  *
1360  * @brief   Send ZDO_NWK_DISC_REQ to ZNP
1361  *
1362  * @param    req - Pointer to outgoing command structure
1363  *
1364  * @return   status
1365  */
1366 uint8_t zdoNwkDiscoveryReq(NwkDiscoveryReqFormat_t *req)
1368         uint8_t status;
1369         uint8_t cmInd = 0;
1370         uint32_t cmdLen = 5;
1371         uint8_t *cmd = malloc(cmdLen);
1373         if (cmd)
1374         {
1376                 memcpy((cmd + cmInd), req->ScanChannels, 4);
1377                 cmInd += 4;
1378                 cmd[cmInd++] = req->ScanDuration;
1380                 status = rpcSendFrame((MT_RPC_CMD_SREQ | MT_RPC_SYS_ZDO),
1381                 MT_ZDO_NWK_DISCOVERY_REQ, cmd, cmdLen);
1383                 if (status == MT_RPC_SUCCESS)
1384                 {
1385                         rpcWaitMqClientMsg(50);
1386                         status = srspRpcBuff[2];
1387                 }
1389                 free(cmd);
1390                 return status;
1391         }
1392         else
1393         {
1394                 dbg_print(PRINT_LEVEL_WARNING, "Memory for cmd was not allocated\n");
1395                 return 1;
1396         }
1399 /*********************************************************************
1400  * @fn      zdoJoinReq
1401  *
1402  * @brief   Send ZDO_JOIN_REQ to ZNP
1403  *
1404  * @param    req - Pointer to outgoing command structure
1405  *
1406  * @return   status
1407  */
1408 uint8_t zdoJoinReq(JoinReqFormat_t *req)
1410         uint8_t status;
1411         uint8_t cmInd = 0;
1412         uint32_t cmdLen = 15;
1413         uint8_t *cmd = malloc(cmdLen);
1415         if (cmd)
1416         {
1418                 cmd[cmInd++] = req->LogicalChannel;
1419                 cmd[cmInd++] = (uint8_t)(req->PanID & 0xFF);
1420                 cmd[cmInd++] = (uint8_t)((req->PanID >> 8) & 0xFF);
1421                 memcpy((cmd + cmInd), req->ExtendedPanID, 8);
1422                 cmInd += 8;
1423                 cmd[cmInd++] = (uint8_t)(req->ChosenParent & 0xFF);
1424                 cmd[cmInd++] = (uint8_t)((req->ChosenParent >> 8) & 0xFF);
1425                 cmd[cmInd++] = req->ParentDepth;
1426                 cmd[cmInd++] = req->StackProfile;
1428                 status = rpcSendFrame((MT_RPC_CMD_SREQ | MT_RPC_SYS_ZDO),
1429                 MT_ZDO_JOIN_REQ, cmd, cmdLen);
1431                 if (status == MT_RPC_SUCCESS)
1432                 {
1433                         rpcWaitMqClientMsg(50);
1434                         status = srspRpcBuff[2];
1435                 }
1437                 free(cmd);
1438                 return status;
1439         }
1440         else
1441         {
1442                 dbg_print(PRINT_LEVEL_WARNING, "Memory for cmd was not allocated\n");
1443                 return 1;
1444         }
1447 /*********************************************************************
1448  * @fn      zdoMsgCbRegister
1449  *
1450  * @brief   Send ZDO_MSG_CB_REGISTER to ZNP
1451  *
1452  * @param    req - Pointer to outgoing command structure
1453  *
1454  * @return   status
1455  */
1456 uint8_t zdoMsgCbRegister(MsgCbRegisterFormat_t *req)
1458         uint8_t status;
1459         uint8_t cmInd = 0;
1460         uint32_t cmdLen = 2;
1461         uint8_t *cmd = malloc(cmdLen);
1463         if (cmd)
1464         {
1466                 cmd[cmInd++] = (uint8_t)(req->ClusterID & 0xFF);
1467                 cmd[cmInd++] = (uint8_t)((req->ClusterID >> 8) & 0xFF);
1469                 status = rpcSendFrame((MT_RPC_CMD_SREQ | MT_RPC_SYS_ZDO),
1470                 MT_ZDO_MSG_CB_REGISTER, cmd, cmdLen);
1472                 if (status == MT_RPC_SUCCESS)
1473                 {
1474                         rpcWaitMqClientMsg(50);
1475                         status = srspRpcBuff[2];
1476                 }
1478                 free(cmd);
1479                 return status;
1480         }
1481         else
1482         {
1483                 dbg_print(PRINT_LEVEL_WARNING, "Memory for cmd was not allocated\n");
1484                 return 1;
1485         }
1488 /*********************************************************************
1489  * @fn      zdoMsgCbRemove
1490  *
1491  * @brief   Send ZDO_MSG_CB_REMOVE to ZNP
1492  *
1493  * @param    req - Pointer to outgoing command structure
1494  *
1495  * @return   status
1496  */
1497 uint8_t zdoMsgCbRemove(MsgCbRemoveFormat_t *req)
1499         uint8_t status;
1500         uint8_t cmInd = 0;
1501         uint32_t cmdLen = 2;
1502         uint8_t *cmd = malloc(cmdLen);
1504         if (cmd)
1505         {
1507                 cmd[cmInd++] = (uint8_t)(req->ClusterID & 0xFF);
1508                 cmd[cmInd++] = (uint8_t)((req->ClusterID >> 8) & 0xFF);
1510                 status = rpcSendFrame((MT_RPC_CMD_SREQ | MT_RPC_SYS_ZDO),
1511                 MT_ZDO_MSG_CB_REMOVE, cmd, cmdLen);
1513                 if (status == MT_RPC_SUCCESS)
1514                 {
1515                         rpcWaitMqClientMsg(50);
1516                         status = srspRpcBuff[2];
1517                 }
1519                 free(cmd);
1520                 return status;
1521         }
1522         else
1523         {
1524                 dbg_print(PRINT_LEVEL_WARNING, "Memory for cmd was not allocated\n");
1525                 return 1;
1526         }
1529 /*********************************************************************
1530  * @fn      processGetLinkKey
1531  *
1532  * @brief   processes incoming command from ZNP
1533  *
1534  * @param    rpcBuff - Buffer from rpc layer, contains command data
1535  * @param    rpcLen - Length of rpcBuff
1536  *
1537  * @return
1538  */
1539 static void processGetLinkKey(uint8_t *rpcBuff, uint8_t rpcLen)
1541         if (mtZdoCbs.pfnZdoGetLinkKey)
1542         {
1543                 uint8_t msgIdx = 2;
1544                 GetLinkKeySrspFormat_t rsp;
1545                 if (rpcLen < 25)
1546                 {
1547                         printf("MT_RPC_ERR_LENGTH\n");
1549                 }
1550                 //printf("rpcLen = %d\n", rpcLen);
1552                 rsp.Status = rpcBuff[msgIdx++];
1553                 rsp.IEEEAddr = 0;
1554                 uint8_t i;
1555                 for (i = 0; i < 8; i++)
1556                         rsp.IEEEAddr |= ((uint64_t) rpcBuff[msgIdx++]) << (i * 8);
1557                 memcpy(rsp.LinkKeyData, &rpcBuff[msgIdx], 16);
1558                 msgIdx += 16;
1560                 mtZdoCbs.pfnZdoGetLinkKey(&rsp);
1561         }
1564 /*********************************************************************
1565  * @fn      processNwkAddrRsp
1566  *
1567  * @brief   processes incoming command from ZNP
1568  *
1569  * @param    rpcBuff - Buffer from rpc layer, contains command data
1570  * @param    rpcLen - Length of rpcBuff
1571  *
1572  * @return
1573  */
1574 static void processNwkAddrRsp(uint8_t *rpcBuff, uint8_t rpcLen)
1576         if (mtZdoCbs.pfnZdoNwkAddrRsp)
1577         {
1578                 uint8_t msgIdx = 2;
1579                 NwkAddrRspFormat_t rsp;
1580                 if (rpcLen < 13)
1581                 {
1582                         printf("MT_RPC_ERR_LENGTH\n");
1584                 }
1585                 //printf("rpcLen = %d\n", rpcLen);
1587                 rsp.Status = rpcBuff[msgIdx++];
1588                 rsp.IEEEAddr = 0;
1589                 uint8_t i;
1590                 for (i = 0; i < 8; i++)
1591                         rsp.IEEEAddr |= ((uint64_t) rpcBuff[msgIdx++]) << (i * 8);
1592                 rsp.NwkAddr = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
1593                 msgIdx += 2;
1594                 rsp.StartIndex = rpcBuff[msgIdx++];
1595                 rsp.NumAssocDev = rpcBuff[msgIdx++];
1596                 if (rpcLen > 13)
1597                 {
1598                         uint32_t i;
1599                         for (i = 0; i < rsp.NumAssocDev; i++)
1600                         {
1601                                 rsp.AssocDevList[i] = BUILD_UINT16(rpcBuff[msgIdx],
1602                                         rpcBuff[msgIdx + 1]);
1603                                 msgIdx += 2;
1604                         }
1605                 }
1606                 mtZdoCbs.pfnZdoNwkAddrRsp(&rsp);
1607         }
1610 /*********************************************************************
1611  * @fn      processIeeeAddrRsp
1612  *
1613  * @brief   processes incoming command from ZNP
1614  *
1615  * @param    rpcBuff - Buffer from rpc layer, contains command data
1616  * @param    rpcLen - Length of rpcBuff
1617  *
1618  * @return
1619  */
1620 static void processIeeeAddrRsp(uint8_t *rpcBuff, uint8_t rpcLen)
1622         if (mtZdoCbs.pfnZdoIeeeAddrRsp)
1623         {
1624                 uint8_t msgIdx = 2;
1625                 IeeeAddrRspFormat_t rsp;
1626                 if (rpcLen < 13)
1627                 {
1628                         printf("MT_RPC_ERR_LENGTH\n");
1630                 }
1631                 //printf("rpcLen = %d\n", rpcLen);
1633                 rsp.Status = rpcBuff[msgIdx++];
1634                 rsp.IEEEAddr = 0;
1635                 uint8_t i;
1636                 for (i = 0; i < 8; i++)
1637                         rsp.IEEEAddr |= ((uint64_t) rpcBuff[msgIdx++]) << (i * 8);
1638                 rsp.NwkAddr = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
1639                 msgIdx += 2;
1640                 rsp.StartIndex = rpcBuff[msgIdx++];
1641                 rsp.NumAssocDev = rpcBuff[msgIdx++];
1642                 rsp.StartIndex = (rsp.NumAssocDev == 0 ? 0 : rsp.StartIndex);
1643                 if (rpcLen > 13)
1644                 {
1645                         uint32_t i;
1646                         for (i = 0; i < rsp.NumAssocDev; i++)
1647                         {
1648                                 rsp.AssocDevList[i] = BUILD_UINT16(rpcBuff[msgIdx],
1649                                         rpcBuff[msgIdx + 1]);
1650                                 msgIdx += 2;
1651                         }
1652                 }
1653                 mtZdoCbs.pfnZdoIeeeAddrRsp(&rsp);
1654         }
1657 /*********************************************************************
1658  * @fn      processNodeDescRsp
1659  *
1660  * @brief   processes incoming command from ZNP
1661  *
1662  * @param    rpcBuff - Buffer from rpc layer, contains command data
1663  * @param    rpcLen - Length of rpcBuff
1664  *
1665  * @return
1666  */
1667 static void processNodeDescRsp(uint8_t *rpcBuff, uint8_t rpcLen)
1669         if (mtZdoCbs.pfnZdoNodeDescRsp)
1670         {
1671                 uint8_t msgIdx = 2;
1672                 NodeDescRspFormat_t rsp;
1673                 if (rpcLen < 18)
1674                 {
1675                         printf("MT_RPC_ERR_LENGTH\n");
1677                 }
1678                 //printf("rpcLen = %d\n", rpcLen);
1680                 rsp.SrcAddr = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
1681                 msgIdx += 2;
1682                 rsp.Status = rpcBuff[msgIdx++];
1683                 rsp.NwkAddr = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
1684                 msgIdx += 2;
1685                 rsp.LoTy_ComDescAv_UsrDesAv = rpcBuff[msgIdx++];
1686                 rsp.APSFlg_FrqBnd = rpcBuff[msgIdx++];
1687                 rsp.MACCapFlg = rpcBuff[msgIdx++];
1688                 rsp.ManufacturerCode = BUILD_UINT16(rpcBuff[msgIdx],
1689                         rpcBuff[msgIdx + 1]);
1690                 msgIdx += 2;
1691                 rsp.MaxBufferSize = rpcBuff[msgIdx++];
1692                 rsp.MaxTransferSize = BUILD_UINT16(rpcBuff[msgIdx],
1693                         rpcBuff[msgIdx + 1]);
1694                 msgIdx += 2;
1695                 rsp.ServerMask = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
1696                 msgIdx += 2;
1697                 rsp.MaxOutTransferSize = BUILD_UINT16(rpcBuff[msgIdx],
1698                         rpcBuff[msgIdx + 1]);
1699                 msgIdx += 2;
1700                 rsp.DescriptorCapabilities = rpcBuff[msgIdx++];
1702                 mtZdoCbs.pfnZdoNodeDescRsp(&rsp);
1703         }
1706 /*********************************************************************
1707  * @fn      processPowerDescRsp
1708  *
1709  * @brief   processes incoming command from ZNP
1710  *
1711  * @param    rpcBuff - Buffer from rpc layer, contains command data
1712  * @param    rpcLen - Length of rpcBuff
1713  *
1714  * @return
1715  */
1716 static void processPowerDescRsp(uint8_t *rpcBuff, uint8_t rpcLen)
1718         if (mtZdoCbs.pfnZdoPowerDescRsp)
1719         {
1720                 uint8_t msgIdx = 2;
1721                 PowerDescRspFormat_t rsp;
1722                 if (rpcLen < 7)
1723                 {
1724                         printf("MT_RPC_ERR_LENGTH\n");
1726                 }
1727                 //printf("rpcLen = %d\n", rpcLen);
1729                 rsp.SrcAddr = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
1730                 msgIdx += 2;
1731                 rsp.Status = rpcBuff[msgIdx++];
1732                 rsp.NwkAddr = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
1733                 msgIdx += 2;
1734                 rsp.CurrntPwrMode_AvalPwrSrcs = rpcBuff[msgIdx++];
1735                 rsp.CurrntPwrSrc_CurrntPwrSrcLvl = rpcBuff[msgIdx++];
1737                 mtZdoCbs.pfnZdoPowerDescRsp(&rsp);
1738         }
1741 /*********************************************************************
1742  * @fn      processSimpleDescRsp
1743  *
1744  * @brief   processes incoming command from ZNP
1745  *
1746  * @param    rpcBuff - Buffer from rpc layer, contains command data
1747  * @param    rpcLen - Length of rpcBuff
1748  *
1749  * @return
1750  */
1751 static void processSimpleDescRsp(uint8_t *rpcBuff, uint8_t rpcLen)
1753         if (mtZdoCbs.pfnZdoSimpleDescRsp)
1754         {
1755                 uint8_t msgIdx = 2;
1756                 SimpleDescRspFormat_t rsp;
1757                 if (rpcLen < 6)
1758                 {
1759                         printf("MT_RPC_ERR_LENGTH\n");
1761                 }
1762                 //printf("rpcLen = %d\n", rpcLen);
1764                 rsp.SrcAddr = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
1765                 msgIdx += 2;
1766                 rsp.Status = rpcBuff[msgIdx++];
1767                 rsp.NwkAddr = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
1768                 msgIdx += 2;
1769                 rsp.Len = rpcBuff[msgIdx++];
1770                 if (rpcLen > 6)
1771                 {
1772                         rsp.Endpoint = rpcBuff[msgIdx++];
1773                         rsp.ProfileID = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
1774                         msgIdx += 2;
1775                         rsp.DeviceID = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
1776                         msgIdx += 2;
1777                         rsp.DeviceVersion = rpcBuff[msgIdx++];
1778                         rsp.NumInClusters = rpcBuff[msgIdx++];
1779                         uint32_t i;
1780                         for (i = 0; i < rsp.NumInClusters; i++)
1781                         {
1782                                 rsp.InClusterList[i] = BUILD_UINT16(rpcBuff[msgIdx],
1783                                         rpcBuff[msgIdx + 1]);
1784                                 msgIdx += 2;
1785                         }
1786                         rsp.NumOutClusters = rpcBuff[msgIdx++];
1787                         for (i = 0; i < rsp.NumOutClusters; i++)
1788                         {
1789                                 rsp.OutClusterList[i] = BUILD_UINT16(rpcBuff[msgIdx],
1790                                         rpcBuff[msgIdx + 1]);
1791                                 msgIdx += 2;
1792                         }
1793                 }
1794                 mtZdoCbs.pfnZdoSimpleDescRsp(&rsp);
1795         }
1798 /*********************************************************************
1799  * @fn      processActiveEpRsp
1800  *
1801  * @brief   processes incoming command from ZNP
1802  *
1803  * @param    rpcBuff - Buffer from rpc layer, contains command data
1804  * @param    rpcLen - Length of rpcBuff
1805  *
1806  * @return
1807  */
1808 static void processActiveEpRsp(uint8_t *rpcBuff, uint8_t rpcLen)
1810         if (mtZdoCbs.pfnZdoActiveEpRsp)
1811         {
1812                 uint8_t msgIdx = 2;
1813                 ActiveEpRspFormat_t rsp;
1814                 if (rpcLen < 6)
1815                 {
1816                         printf("MT_RPC_ERR_LENGTH\n");
1818                 }
1819                 //printf("rpcLen = %d\n", rpcLen);
1821                 rsp.SrcAddr = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
1822                 msgIdx += 2;
1823                 rsp.Status = rpcBuff[msgIdx++];
1824                 rsp.NwkAddr = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
1825                 msgIdx += 2;
1826                 rsp.ActiveEPCount = rpcBuff[msgIdx++];
1827                 if (rpcLen > 6)
1828                 {
1829                         uint32_t i;
1830                         for (i = 0; i < rsp.ActiveEPCount; i++)
1831                         {
1832                                 rsp.ActiveEPList[i] = rpcBuff[msgIdx++];
1833                         }
1834                 }
1835                 mtZdoCbs.pfnZdoActiveEpRsp(&rsp);
1836         }
1839 /*********************************************************************
1840  * @fn      processMatchDescRsp
1841  *
1842  * @brief   processes incoming command from ZNP
1843  *
1844  * @param    rpcBuff - Buffer from rpc layer, contains command data
1845  * @param    rpcLen - Length of rpcBuff
1846  *
1847  * @return
1848  */
1849 static void processMatchDescRsp(uint8_t *rpcBuff, uint8_t rpcLen)
1851         if (mtZdoCbs.pfnZdoMatchDescRsp)
1852         {
1853                 uint8_t msgIdx = 2;
1854                 MatchDescRspFormat_t rsp;
1855                 if (rpcLen < 6)
1856                 {
1857                         printf("MT_RPC_ERR_LENGTH\n");
1859                 }
1860                 //printf("rpcLen = %d\n", rpcLen);
1862                 rsp.SrcAddr = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
1863                 msgIdx += 2;
1864                 rsp.Status = rpcBuff[msgIdx++];
1865                 rsp.NwkAddr = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
1866                 msgIdx += 2;
1867                 rsp.MatchLength = rpcBuff[msgIdx++];
1868                 if (rpcLen > 6)
1869                 {
1870                         uint32_t i;
1871                         for (i = 0; i < rsp.MatchLength; i++)
1872                         {
1873                                 rsp.MatchList[i] = rpcBuff[msgIdx++];
1874                         }
1875                 }
1876                 mtZdoCbs.pfnZdoMatchDescRsp(&rsp);
1877         }
1880 /*********************************************************************
1881  * @fn      processComplexDescRsp
1882  *
1883  * @brief   processes incoming command from ZNP
1884  *
1885  * @param    rpcBuff - Buffer from rpc layer, contains command data
1886  * @param    rpcLen - Length of rpcBuff
1887  *
1888  * @return
1889  */
1890 static void processComplexDescRsp(uint8_t *rpcBuff, uint8_t rpcLen)
1892         if (mtZdoCbs.pfnZdoComplexDescRsp)
1893         {
1894                 uint8_t msgIdx = 2;
1895                 ComplexDescRspFormat_t rsp;
1896                 if (rpcLen < 6)
1897                 {
1898                         printf("MT_RPC_ERR_LENGTH\n");
1900                 }
1901                 //printf("rpcLen = %d\n", rpcLen);
1903                 rsp.SrcAddr = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
1904                 msgIdx += 2;
1905                 rsp.Status = rpcBuff[msgIdx++];
1906                 rsp.NwkAddr = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
1907                 msgIdx += 2;
1908                 rsp.ComplexLength = rpcBuff[msgIdx++];
1909                 if (rpcLen > 6)
1910                 {
1911                         uint32_t i;
1912                         for (i = 0; i < rsp.ComplexLength; i++)
1913                         {
1914                                 rsp.ComplexList[i] = rpcBuff[msgIdx++];
1915                         }
1916                 }
1917                 mtZdoCbs.pfnZdoComplexDescRsp(&rsp);
1918         }
1921 /*********************************************************************
1922  * @fn      processUserDescRsp
1923  *
1924  * @brief   processes incoming command from ZNP
1925  *
1926  * @param    rpcBuff - Buffer from rpc layer, contains command data
1927  * @param    rpcLen - Length of rpcBuff
1928  *
1929  * @return
1930  */
1931 static void processUserDescRsp(uint8_t *rpcBuff, uint8_t rpcLen)
1933         if (mtZdoCbs.pfnZdoUserDescRsp)
1934         {
1935                 uint8_t msgIdx = 2;
1936                 UserDescRspFormat_t rsp;
1937                 if (rpcLen < 6)
1938                 {
1939                         printf("MT_RPC_ERR_LENGTH\n");
1941                 }
1942                 //printf("rpcLen = %d\n", rpcLen);
1944                 rsp.SrcAddr = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
1945                 msgIdx += 2;
1946                 rsp.Status = rpcBuff[msgIdx++];
1947                 rsp.NwkAddr = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
1948                 msgIdx += 2;
1949                 rsp.Len = rpcBuff[msgIdx++];
1950                 if (rpcLen > 6)
1951                 {
1952                         uint32_t i;
1953                         for (i = 0; i < rsp.Len; i++)
1954                         {
1955                                 rsp.CUserDescriptor[i] = rpcBuff[msgIdx++];
1956                         }
1957                 }
1958                 mtZdoCbs.pfnZdoUserDescRsp(&rsp);
1959         }
1962 /*********************************************************************
1963  * @fn      processUserDescConf
1964  *
1965  * @brief   processes incoming command from ZNP
1966  *
1967  * @param    rpcBuff - Buffer from rpc layer, contains command data
1968  * @param    rpcLen - Length of rpcBuff
1969  *
1970  * @return
1971  */
1972 static void processUserDescConf(uint8_t *rpcBuff, uint8_t rpcLen)
1974         if (mtZdoCbs.pfnZdoUserDescConf)
1975         {
1976                 uint8_t msgIdx = 2;
1977                 UserDescConfFormat_t rsp;
1978                 if (rpcLen < 5)
1979                 {
1980                         printf("MT_RPC_ERR_LENGTH\n");
1982                 }
1983                 //printf("rpcLen = %d\n", rpcLen);
1985                 rsp.SrcAddr = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
1986                 msgIdx += 2;
1987                 rsp.Status = rpcBuff[msgIdx++];
1988                 rsp.NwkAddr = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
1989                 msgIdx += 2;
1991                 mtZdoCbs.pfnZdoUserDescConf(&rsp);
1992         }
1995 /*********************************************************************
1996  * @fn      processServerDiscRsp
1997  *
1998  * @brief   processes incoming command from ZNP
1999  *
2000  * @param    rpcBuff - Buffer from rpc layer, contains command data
2001  * @param    rpcLen - Length of rpcBuff
2002  *
2003  * @return
2004  */
2005 static void processServerDiscRsp(uint8_t *rpcBuff, uint8_t rpcLen)
2007         if (mtZdoCbs.pfnZdoServerDiscRsp)
2008         {
2009                 uint8_t msgIdx = 2;
2010                 ServerDiscRspFormat_t rsp;
2011                 if (rpcLen < 5)
2012                 {
2013                         printf("MT_RPC_ERR_LENGTH\n");
2015                 }
2016                 //printf("rpcLen = %d\n", rpcLen);
2018                 rsp.SrcAddr = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
2019                 msgIdx += 2;
2020                 rsp.Status = rpcBuff[msgIdx++];
2021                 rsp.ServerMask = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
2022                 msgIdx += 2;
2024                 mtZdoCbs.pfnZdoServerDiscRsp(&rsp);
2025         }
2028 /*********************************************************************
2029  * @fn      processEndDeviceBindRsp
2030  *
2031  * @brief   processes incoming command from ZNP
2032  *
2033  * @param    rpcBuff - Buffer from rpc layer, contains command data
2034  * @param    rpcLen - Length of rpcBuff
2035  *
2036  * @return
2037  */
2038 static void processEndDeviceBindRsp(uint8_t *rpcBuff, uint8_t rpcLen)
2040         if (mtZdoCbs.pfnZdoEndDeviceBindRsp)
2041         {
2042                 uint8_t msgIdx = 2;
2043                 EndDeviceBindRspFormat_t rsp;
2044                 if (rpcLen < 3)
2045                 {
2046                         printf("MT_RPC_ERR_LENGTH\n");
2048                 }
2049                 //printf("rpcLen = %d\n", rpcLen);
2051                 rsp.SrcAddr = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
2052                 msgIdx += 2;
2053                 rsp.Status = rpcBuff[msgIdx++];
2055                 mtZdoCbs.pfnZdoEndDeviceBindRsp(&rsp);
2056         }
2059 /*********************************************************************
2060  * @fn      processBindRsp
2061  *
2062  * @brief   processes incoming command from ZNP
2063  *
2064  * @param    rpcBuff - Buffer from rpc layer, contains command data
2065  * @param    rpcLen - Length of rpcBuff
2066  *
2067  * @return
2068  */
2069 static void processBindRsp(uint8_t *rpcBuff, uint8_t rpcLen)
2071         if (mtZdoCbs.pfnZdoBindRsp)
2072         {
2073                 uint8_t msgIdx = 2;
2074                 BindRspFormat_t rsp;
2075                 if (rpcLen < 3)
2076                 {
2077                         printf("MT_RPC_ERR_LENGTH\n");
2079                 }
2080                 //printf("rpcLen = %d\n", rpcLen);
2082                 rsp.SrcAddr = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
2083                 msgIdx += 2;
2084                 rsp.Status = rpcBuff[msgIdx++];
2086                 mtZdoCbs.pfnZdoBindRsp(&rsp);
2087         }
2090 /*********************************************************************
2091  * @fn      processUnbindRsp
2092  *
2093  * @brief   processes incoming command from ZNP
2094  *
2095  * @param    rpcBuff - Buffer from rpc layer, contains command data
2096  * @param    rpcLen - Length of rpcBuff
2097  *
2098  * @return
2099  */
2100 static void processUnbindRsp(uint8_t *rpcBuff, uint8_t rpcLen)
2102         if (mtZdoCbs.pfnZdoUnbindRsp)
2103         {
2104                 uint8_t msgIdx = 2;
2105                 UnbindRspFormat_t rsp;
2106                 if (rpcLen < 3)
2107                 {
2108                         printf("MT_RPC_ERR_LENGTH\n");
2110                 }
2111                 //printf("rpcLen = %d\n", rpcLen);
2113                 rsp.SrcAddr = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
2114                 msgIdx += 2;
2115                 rsp.Status = rpcBuff[msgIdx++];
2117                 mtZdoCbs.pfnZdoUnbindRsp(&rsp);
2118         }
2121 /*********************************************************************
2122  * @fn      processMgmtNwkDiscRsp
2123  *
2124  * @brief   processes incoming command from ZNP
2125  *
2126  * @param    rpcBuff - Buffer from rpc layer, contains command data
2127  * @param    rpcLen - Length of rpcBuff
2128  *
2129  * @return
2130  */
2131 static void processMgmtNwkDiscRsp(uint8_t *rpcBuff, uint8_t rpcLen)
2133         if (mtZdoCbs.pfnZdoMgmtNwkDiscRsp)
2134         {
2135                 uint8_t msgIdx = 2;
2136                 MgmtNwkDiscRspFormat_t rsp;
2137                 if (rpcLen < 6)
2138                 {
2139                         printf("MT_RPC_ERR_LENGTH\n");
2141                 }
2142                 //printf("rpcLen = %d\n", rpcLen);
2144                 rsp.SrcAddr = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
2145                 msgIdx += 2;
2146                 rsp.Status = rpcBuff[msgIdx++];
2147                 rsp.NetworkCount = rpcBuff[msgIdx++];
2148                 rsp.StartIndex = rpcBuff[msgIdx++];
2149                 rsp.NetworkListCount = rpcBuff[msgIdx++];
2150                 if (rpcLen > 6)
2151                 {
2152                         uint32_t i;
2153                         for (i = 0; i < rsp.NetworkListCount; i++)
2154                         {
2155                                 rsp.NetworkList[i].PanID = 0;
2156                                 uint8_t ind;
2157                                 for (ind = 0; ind < 8; ind++)
2158                                         rsp.NetworkList[i].PanID |= ((uint64_t) rpcBuff[msgIdx++])
2159                                                 << (ind * 8);
2160                                 rsp.NetworkList[i].LogicalChannel = rpcBuff[msgIdx++];
2161                                 rsp.NetworkList[i].StackProf_ZigVer = rpcBuff[msgIdx++];
2162                                 rsp.NetworkList[i].BeacOrd_SupFramOrd = rpcBuff[msgIdx++];
2163                                 rsp.NetworkList[i].PermitJoin = rpcBuff[msgIdx++];
2164                         }
2165                 }
2166                 mtZdoCbs.pfnZdoMgmtNwkDiscRsp(&rsp);
2167         }
2170 /*********************************************************************
2171  * @fn      processMgmtLqiRsp
2172  *
2173  * @brief   processes incoming command from ZNP
2174  *
2175  * @param    rpcBuff - Buffer from rpc layer, contains command data
2176  * @param    rpcLen - Length of rpcBuff
2177  *
2178  * @return
2179  */
2180 static void processMgmtLqiRsp(uint8_t *rpcBuff, uint8_t rpcLen)
2182         if (mtZdoCbs.pfnZdoMgmtLqiRsp)
2183         {
2184                 uint8_t msgIdx = 2;
2185                 MgmtLqiRspFormat_t rsp;
2186                 if (rpcLen < 6)
2187                 {
2188                         printf("MT_RPC_ERR_LENGTH\n");
2190                 }
2191                 //printf("rpcLen = %d\n", rpcLen);
2193                 rsp.SrcAddr = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
2194                 msgIdx += 2;
2195                 rsp.Status = rpcBuff[msgIdx++];
2196                 rsp.NeighborTableEntries = rpcBuff[msgIdx++];
2197                 rsp.StartIndex = rpcBuff[msgIdx++];
2198                 rsp.NeighborLqiListCount = rpcBuff[msgIdx++];
2199                 if (rpcLen > 6)
2200                 {
2201                         uint32_t i;
2202                         for (i = 0; i < rsp.NeighborLqiListCount; i++)
2203                         {
2205                                 rsp.NeighborLqiList[i].ExtendedPanID = 0;
2206                                 uint8_t ind;
2207                                 for (ind = 0; ind < 8; ind++)
2208                                         rsp.NeighborLqiList[i].ExtendedPanID |=
2209                                                 ((uint64_t) rpcBuff[msgIdx++]) << (ind * 8);
2210                                 rsp.NeighborLqiList[i].ExtendedAddress = 0;
2211                                 for (ind = 0; ind < 8; ind++)
2212                                         rsp.NeighborLqiList[i].ExtendedAddress |=
2213                                                 ((uint64_t) rpcBuff[msgIdx++]) << (ind * 8);
2214                                 rsp.NeighborLqiList[i].NetworkAddress = BUILD_UINT16(
2215                                         rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
2216                                 msgIdx += 2;
2217                                 rsp.NeighborLqiList[i].DevTyp_RxOnWhenIdle_Relat =
2218                                         rpcBuff[msgIdx++];
2219                                 rsp.NeighborLqiList[i].PermitJoining = rpcBuff[msgIdx++];
2220                                 rsp.NeighborLqiList[i].Depth = rpcBuff[msgIdx++];
2221                                 rsp.NeighborLqiList[i].LQI = rpcBuff[msgIdx++];
2223                         }
2224                 }
2225                 MgmtLqiRspFormat_t *copyy = &rsp;
2226                 mtZdoCbs.pfnZdoMgmtLqiRsp(copyy);
2227         }
2230 /*********************************************************************
2231  * @fn      processMgmtRtgRsp
2232  *
2233  * @brief   processes incoming command from ZNP
2234  *
2235  * @param    rpcBuff - Buffer from rpc layer, contains command data
2236  * @param    rpcLen - Length of rpcBuff
2237  *
2238  * @return
2239  */
2240 static void processMgmtRtgRsp(uint8_t *rpcBuff, uint8_t rpcLen)
2242         if (mtZdoCbs.pfnZdoMgmtRtgRsp)
2243         {
2244                 uint8_t msgIdx = 2;
2245                 MgmtRtgRspFormat_t rsp;
2246                 if (rpcLen < 6)
2247                 {
2248                         printf("MT_RPC_ERR_LENGTH\n");
2250                 }
2251                 //printf("rpcLen = %d\n", rpcLen);
2253                 rsp.SrcAddr = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
2254                 msgIdx += 2;
2255                 rsp.Status = rpcBuff[msgIdx++];
2256                 rsp.RoutingTableEntries = rpcBuff[msgIdx++];
2257                 rsp.StartIndex = rpcBuff[msgIdx++];
2258                 rsp.RoutingTableListCount = rpcBuff[msgIdx++];
2259                 if (rpcLen > 6)
2260                 {
2261                         uint32_t i;
2262                         for (i = 0; i < rsp.RoutingTableListCount; i++)
2263                         {
2264                                 rsp.RoutingTableList[i].DstAddr = BUILD_UINT16(rpcBuff[msgIdx],
2265                                         rpcBuff[msgIdx + 1]);
2266                                 msgIdx += 2;
2267                                 rsp.RoutingTableList[i].Status = rpcBuff[msgIdx++];
2268                                 rsp.RoutingTableList[i].NextHop = BUILD_UINT16(rpcBuff[msgIdx],
2269                                         rpcBuff[msgIdx + 1]);
2270                                 msgIdx += 2;
2271                         }
2272                 }
2273                 mtZdoCbs.pfnZdoMgmtRtgRsp(&rsp);
2274         }
2277 /*********************************************************************
2278  * @fn      processMgmtBindRsp
2279  *
2280  * @brief   processes incoming command from ZNP
2281  *
2282  * @param    rpcBuff - Buffer from rpc layer, contains command data
2283  * @param    rpcLen - Length of rpcBuff
2284  *
2285  * @return
2286  */
2287 static void processMgmtBindRsp(uint8_t *rpcBuff, uint8_t rpcLen)
2289         if (mtZdoCbs.pfnZdoMgmtBindRsp)
2290         {
2291                 uint8_t msgIdx = 2;
2292                 MgmtBindRspFormat_t rsp;
2293                 if (rpcLen < 6)
2294                 {
2295                         printf("MT_RPC_ERR_LENGTH\n");
2297                 }
2298                 //printf("rpcLen = %d\n", rpcLen);
2300                 rsp.SrcAddr = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
2301                 msgIdx += 2;
2302                 rsp.Status = rpcBuff[msgIdx++];
2303                 rsp.BindingTableEntries = rpcBuff[msgIdx++];
2304                 rsp.StartIndex = rpcBuff[msgIdx++];
2305                 rsp.BindingTableListCount = rpcBuff[msgIdx++];
2306                 if (rpcLen > 6)
2307                 {
2308                         uint32_t i;
2309                         for (i = 0; i < rsp.BindingTableListCount; i++)
2310                         {
2311                                 rsp.BindingTableList[i].SrcIEEEAddr = 0;
2312                                 uint8_t i;
2313                                 for (i = 0; i < 8; i++)
2314                                         rsp.BindingTableList[i].SrcIEEEAddr |=
2315                                                 ((uint64_t) rpcBuff[msgIdx++]) << (i * 8);
2316                                 rsp.BindingTableList[i].SrcEndpoint = rpcBuff[msgIdx++];
2317                                 rsp.BindingTableList[i].ClusterID = rpcBuff[msgIdx++];
2318                                 rsp.BindingTableList[i].DstAddrMode = rpcBuff[msgIdx++];
2319                                 rsp.BindingTableList[i].DstIEEEAddr = 0;
2320                                 for (i = 0; i < 8; i++)
2321                                         rsp.BindingTableList[i].DstIEEEAddr |=
2322                                                 ((uint64_t) rpcBuff[msgIdx++]) << (i * 8);
2323                                 rsp.BindingTableList[i].DstEndpoint = rpcBuff[msgIdx++];
2324                         }
2325                 }
2326                 mtZdoCbs.pfnZdoMgmtBindRsp(&rsp);
2327         }
2330 /*********************************************************************
2331  * @fn      processMgmtLeaveRsp
2332  *
2333  * @brief   processes incoming command from ZNP
2334  *
2335  * @param    rpcBuff - Buffer from rpc layer, contains command data
2336  * @param    rpcLen - Length of rpcBuff
2337  *
2338  * @return
2339  */
2340 static void processMgmtLeaveRsp(uint8_t *rpcBuff, uint8_t rpcLen)
2342         if (mtZdoCbs.pfnZdoMgmtLeaveRsp)
2343         {
2344                 uint8_t msgIdx = 2;
2345                 MgmtLeaveRspFormat_t rsp;
2346                 if (rpcLen < 3)
2347                 {
2348                         printf("MT_RPC_ERR_LENGTH\n");
2350                 }
2351                 //printf("rpcLen = %d\n", rpcLen);
2353                 rsp.SrcAddr = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
2354                 msgIdx += 2;
2355                 rsp.Status = rpcBuff[msgIdx++];
2357                 mtZdoCbs.pfnZdoMgmtLeaveRsp(&rsp);
2358         }
2361 /*********************************************************************
2362  * @fn      processMgmtDirectJoinRsp
2363  *
2364  * @brief   processes incoming command from ZNP
2365  *
2366  * @param    rpcBuff - Buffer from rpc layer, contains command data
2367  * @param    rpcLen - Length of rpcBuff
2368  *
2369  * @return
2370  */
2371 static void processMgmtDirectJoinRsp(uint8_t *rpcBuff, uint8_t rpcLen)
2373         if (mtZdoCbs.pfnZdoMgmtDirectJoinRsp)
2374         {
2375                 uint8_t msgIdx = 2;
2376                 MgmtDirectJoinRspFormat_t rsp;
2377                 if (rpcLen < 3)
2378                 {
2379                         printf("MT_RPC_ERR_LENGTH\n");
2381                 }
2382                 //printf("rpcLen = %d\n", rpcLen);
2384                 rsp.SrcAddr = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
2385                 msgIdx += 2;
2386                 rsp.Status = rpcBuff[msgIdx++];
2388                 mtZdoCbs.pfnZdoMgmtDirectJoinRsp(&rsp);
2389         }
2392 /*********************************************************************
2393  * @fn      processMgmtPermitJoinRsp
2394  *
2395  * @brief   processes incoming command from ZNP
2396  *
2397  * @param    rpcBuff - Buffer from rpc layer, contains command data
2398  * @param    rpcLen - Length of rpcBuff
2399  *
2400  * @return
2401  */
2402 static void processMgmtPermitJoinRsp(uint8_t *rpcBuff, uint8_t rpcLen)
2404         if (mtZdoCbs.pfnZdoMgmtPermitJoinRsp)
2405         {
2406                 uint8_t msgIdx = 2;
2407                 MgmtPermitJoinRspFormat_t rsp;
2408                 if (rpcLen < 3)
2409                 {
2410                         printf("MT_RPC_ERR_LENGTH\n");
2412                 }
2413                 //printf("rpcLen = %d\n", rpcLen);
2415                 rsp.SrcAddr = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
2416                 msgIdx += 2;
2417                 rsp.Status = rpcBuff[msgIdx++];
2419                 mtZdoCbs.pfnZdoMgmtPermitJoinRsp(&rsp);
2420         }
2423 /*********************************************************************
2424  * @fn      processEndDeviceAnnceInd
2425  *
2426  * @brief   processes incoming command from ZNP
2427  *
2428  * @param    rpcBuff - Buffer from rpc layer, contains command data
2429  * @param    rpcLen - Length of rpcBuff
2430  *
2431  * @return
2432  */
2433 static void processEndDeviceAnnceInd(uint8_t *rpcBuff, uint8_t rpcLen)
2435         if (mtZdoCbs.pfnZdoEndDeviceAnnceInd)
2436         {
2437                 uint8_t msgIdx = 2;
2438                 EndDeviceAnnceIndFormat_t rsp;
2439                 if (rpcLen < 13)
2440                 {
2441                         printf("MT_RPC_ERR_LENGTH\n");
2443                 }
2444                 //printf("rpcLen = %d\n", rpcLen);
2446                 rsp.SrcAddr = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
2447                 msgIdx += 2;
2448                 rsp.NwkAddr = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
2449                 msgIdx += 2;
2450                 rsp.IEEEAddr = 0;
2451                 uint8_t i;
2452                 for (i = 0; i < 8; i++)
2453                         rsp.IEEEAddr |= ((uint64_t) rpcBuff[msgIdx++]) << (i * 8);
2454                 rsp.Capabilities = rpcBuff[msgIdx++];
2456                 mtZdoCbs.pfnZdoEndDeviceAnnceInd(&rsp);
2457         }
2460 /*********************************************************************
2461  * @fn      processMatchDescRspSent
2462  *
2463  * @brief   processes incoming command from ZNP
2464  *
2465  * @param    rpcBuff - Buffer from rpc layer, contains command data
2466  * @param    rpcLen - Length of rpcBuff
2467  *
2468  * @return
2469  */
2470 static void processMatchDescRspSent(uint8_t *rpcBuff, uint8_t rpcLen)
2472         if (mtZdoCbs.pfnZdoMatchDescRspSent)
2473         {
2474                 uint8_t msgIdx = 2;
2475                 MatchDescRspSentFormat_t rsp;
2476                 if (rpcLen < 4)
2477                 {
2478                         printf("MT_RPC_ERR_LENGTH\n");
2480                 }
2481                 //printf("rpcLen = %d\n", rpcLen);
2483                 rsp.NwkAddr = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
2484                 msgIdx += 2;
2485                 rsp.NumInClusters = rpcBuff[msgIdx++];
2486                 uint32_t i;
2487                 for (i = 0; i < rsp.NumInClusters; i++)
2488                 {
2489                         rsp.InClusterList[i] = BUILD_UINT16(rpcBuff[msgIdx],
2490                                 rpcBuff[msgIdx + 1]);
2491                         msgIdx += 2;
2492                 }
2493                 rsp.NumOutClusters = rpcBuff[msgIdx++];
2494                 for (i = 0; i < rsp.NumOutClusters; i++)
2495                 {
2496                         rsp.OutClusterList[i] = BUILD_UINT16(rpcBuff[msgIdx],
2497                                 rpcBuff[msgIdx + 1]);
2498                         msgIdx += 2;
2499                 }
2501                 mtZdoCbs.pfnZdoMatchDescRspSent(&rsp);
2502         }
2505 /*********************************************************************
2506  * @fn      processStatusErrorRsp
2507  *
2508  * @brief   processes incoming command from ZNP
2509  *
2510  * @param    rpcBuff - Buffer from rpc layer, contains command data
2511  * @param    rpcLen - Length of rpcBuff
2512  *
2513  * @return
2514  */
2515 static void processStatusErrorRsp(uint8_t *rpcBuff, uint8_t rpcLen)
2517         if (mtZdoCbs.pfnZdoStatusErrorRsp)
2518         {
2519                 uint8_t msgIdx = 2;
2520                 StatusErrorRspFormat_t rsp;
2521                 if (rpcLen < 3)
2522                 {
2523                         printf("MT_RPC_ERR_LENGTH\n");
2525                 }
2526                 //printf("rpcLen = %d\n", rpcLen);
2528                 rsp.SrcAddr = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
2529                 msgIdx += 2;
2530                 rsp.Status = rpcBuff[msgIdx++];
2532                 mtZdoCbs.pfnZdoStatusErrorRsp(&rsp);
2533         }
2536 /*********************************************************************
2537  * @fn      processSrcRtgInd
2538  *
2539  * @brief   processes incoming command from ZNP
2540  *
2541  * @param    rpcBuff - Buffer from rpc layer, contains command data
2542  * @param    rpcLen - Length of rpcBuff
2543  *
2544  * @return
2545  */
2546 static void processSrcRtgInd(uint8_t *rpcBuff, uint8_t rpcLen)
2548         if (mtZdoCbs.pfnZdoSrcRtgInd)
2549         {
2550                 uint8_t msgIdx = 2;
2551                 SrcRtgIndFormat_t rsp;
2552                 if (rpcLen < 4)
2553                 {
2554                         printf("MT_RPC_ERR_LENGTH\n");
2556                 }
2557                 //printf("rpcLen = %d\n", rpcLen);
2559                 rsp.DstAddr = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
2560                 msgIdx += 2;
2561                 rsp.RelayCount = rpcBuff[msgIdx++];
2562                 uint32_t i;
2563                 for (i = 0; i < rsp.RelayCount; i++)
2564                 {
2565                         rsp.RelayList[i] = BUILD_UINT16(rpcBuff[msgIdx],
2566                                 rpcBuff[msgIdx + 1]);
2567                         msgIdx += 2;
2568                 }
2570                 mtZdoCbs.pfnZdoSrcRtgInd(&rsp);
2571         }
2573 /*********************************************************************
2574  * @fn      processBeaconNotifyInd
2575  *
2576  * @brief   processes incoming command from ZNP
2577  *
2578  * @param    rpcBuff - Buffer from rpc layer, contains command data
2579  * @param    rpcLen - Length of rpcBuff
2580  *
2581  * @return
2582  */
2583 static void processBeaconNotifyInd(uint8_t *rpcBuff, uint8_t rpcLen)
2585         if (mtZdoCbs.pfnZdoBeaconNotifyInd)
2586         {
2587                 uint8_t msgIdx = 2;
2588                 BeaconNotifyIndFormat_t rsp;
2589                 if (rpcLen < 1)
2590                 {
2591                         printf("MT_RPC_ERR_LENGTH\n");
2592                 }
2593                 printf("rpcLen = %d\n", rpcLen);
2595                 rsp.BeaconCount = rpcBuff[msgIdx++];
2596                 if (rpcLen > 1)
2597                 {
2598                         uint32_t i;
2599                         for (i = 0; i < rsp.BeaconCount; i++)
2600                         {
2601                                 rsp.BeaconList[i].SrcAddr = BUILD_UINT16(rpcBuff[msgIdx],
2602                                         rpcBuff[msgIdx + 1]);
2603                                 msgIdx += 2;
2604                                 rsp.BeaconList[i].PanId = BUILD_UINT16(rpcBuff[msgIdx],
2605                                         rpcBuff[msgIdx + 1]);
2606                                 msgIdx += 2;
2607                                 rsp.BeaconList[i].LogicalChannel = rpcBuff[msgIdx++];
2608                                 rsp.BeaconList[i].PermitJoining = rpcBuff[msgIdx++];
2609                                 rsp.BeaconList[i].RouterCap = rpcBuff[msgIdx++];
2610                                 rsp.BeaconList[i].DevCap = rpcBuff[msgIdx++];
2611                                 rsp.BeaconList[i].ProtocolVer = rpcBuff[msgIdx++];
2612                                 rsp.BeaconList[i].StackProf = rpcBuff[msgIdx++];
2613                                 rsp.BeaconList[i].Lqi = rpcBuff[msgIdx++];
2614                                 rsp.BeaconList[i].Depth = rpcBuff[msgIdx++];
2615                                 rsp.BeaconList[i].UpdateId = rpcBuff[msgIdx++];
2617                                 rsp.BeaconList[i].ExtendedPanId = 0;
2618                                 uint8_t ind;
2619                                 for (ind = 0; ind < 8; ind++)
2620                                         rsp.BeaconList[i].ExtendedPanId |=
2621                                                 ((uint64_t) rpcBuff[msgIdx++]) << (ind * 8);
2623                         }
2624                 }
2625                 mtZdoCbs.pfnZdoBeaconNotifyInd(&rsp);
2626         }
2629 /*********************************************************************
2630  * @fn      processJoinCnf
2631  *
2632  * @brief   processes incoming command from ZNP
2633  *
2634  * @param    rpcBuff - Buffer from rpc layer, contains command data
2635  * @param    rpcLen - Length of rpcBuff
2636  *
2637  * @return
2638  */
2639 static void processJoinCnf(uint8_t *rpcBuff, uint8_t rpcLen)
2641         if (mtZdoCbs.pfnZdoJoinCnf)
2642         {
2643                 uint8_t msgIdx = 2;
2644                 JoinCnfFormat_t rsp;
2645                 if (rpcLen < 5)
2646                 {
2647                         printf("MT_RPC_ERR_LENGTH\n");
2648                 }
2649                 printf("rpcLen = %d\n", rpcLen);
2651                 rsp.Status = rpcBuff[msgIdx++];
2652                 rsp.DevAddr = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
2653                 msgIdx += 2;
2654                 rsp.ParentAddr = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
2655                 msgIdx += 2;
2657                 mtZdoCbs.pfnZdoJoinCnf(&rsp);
2658         }
2661 /*********************************************************************
2662  * @fn      processNwkDiscoveryCnf
2663  *
2664  * @brief   processes incoming command from ZNP
2665  *
2666  * @param    rpcBuff - Buffer from rpc layer, contains command data
2667  * @param    rpcLen - Length of rpcBuff
2668  *
2669  * @return
2670  */
2671 static void processNwkDiscoveryCnf(uint8_t *rpcBuff, uint8_t rpcLen)
2673         if (mtZdoCbs.pfnZdoNwkDiscoveryCnf)
2674         {
2675                 uint8_t msgIdx = 2;
2676                 NwkDiscoveryCnfFormat_t rsp;
2677                 if (rpcLen < 1)
2678                 {
2679                         printf("MT_RPC_ERR_LENGTH\n");
2680                 }
2681                 printf("rpcLen = %d\n", rpcLen);
2683                 rsp.Status = rpcBuff[msgIdx++];
2685                 mtZdoCbs.pfnZdoNwkDiscoveryCnf(&rsp);
2686         }
2688 /*********************************************************************
2689  * @fn      processLeaveInd
2690  *
2691  * @brief   processes incoming command from ZNP
2692  *
2693  * @param    rpcBuff - Buffer from rpc layer, contains command data
2694  * @param    rpcLen - Length of rpcBuff
2695  *
2696  * @return
2697  */
2698 static void processLeaveInd(uint8_t *rpcBuff, uint8_t rpcLen)
2700         if (mtZdoCbs.pfnZdoLeaveInd)
2701         {
2702                 uint8_t msgIdx = 2;
2703                 LeaveIndFormat_t rsp;
2704                 if (rpcLen < 13)
2705                 {
2706                         printf("MT_RPC_ERR_LENGTH\n");
2708                 }
2709                 //printf("rpcLen = %d\n", rpcLen);
2711                 rsp.SrcAddr = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
2712                 msgIdx += 2;
2713                 rsp.ExtAddr = 0;
2714                 uint8_t i;
2715                 for (i = 0; i < 8; i++)
2716                         rsp.ExtAddr |= ((uint64_t) rpcBuff[msgIdx++]) << (i * 8);
2717                 rsp.Request = rpcBuff[msgIdx++];
2718                 rsp.Remove = rpcBuff[msgIdx++];
2719                 rsp.Rejoin = rpcBuff[msgIdx++];
2721                 mtZdoCbs.pfnZdoLeaveInd(&rsp);
2722         }
2725 /*********************************************************************
2726  * @fn      processMsgCbIncoming
2727  *
2728  * @brief   processes incoming command from ZNP
2729  *
2730  * @param    rpcBuff - Buffer from rpc layer, contains command data
2731  * @param    rpcLen - Length of rpcBuff
2732  *
2733  * @return
2734  */
2735 static void processMsgCbIncoming(uint8_t *rpcBuff, uint8_t rpcLen)
2737         if (mtZdoCbs.pfnZdoMsgCbIncoming)
2738         {
2739                 uint8_t msgIdx = 2;
2740                 MsgCbIncomingFormat_t rsp;
2741                 if (rpcLen < 9)
2742                 {
2743                         printf("MT_RPC_ERR_LENGTH\n");
2745                 }
2746                 //printf("rpcLen = %d\n", rpcLen);
2748                 rsp.SrcAddr = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
2749                 msgIdx += 2;
2750                 rsp.WasBroadcast = rpcBuff[msgIdx++];
2751                 rsp.ClusterID = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
2752                 msgIdx += 2;
2753                 rsp.SecurityUse = rpcBuff[msgIdx++];
2754                 rsp.SeqNum = rpcBuff[msgIdx++];
2755                 rsp.MacDstAddr = BUILD_UINT16(rpcBuff[msgIdx], rpcBuff[msgIdx + 1]);
2756                 msgIdx += 2;
2757                 if (rpcLen > 9)
2758                 {
2759                         uint32_t i;
2760                         for (i = 0; i < rsp.MacDstAddr; i++)
2761                         {
2762                                 rsp.Data[i] = rpcBuff[msgIdx++];
2763                         }
2764                 }
2765                 mtZdoCbs.pfnZdoMsgCbIncoming(&rsp);
2766         }
2769 /*********************************************************************
2770  * @fn      zdoInit
2771  *
2772  * @brief  Sends the ZD0_startup_from_App command to start the network
2773  *
2774  * @param   none
2775  *
2776  * @return  none
2777  */
2778 uint8_t zdoInit(void)
2780         uint8_t status;
2781         // build the buffer
2782         uint32_t cmdLen = 2;
2783         uint8_t *cmd = malloc(cmdLen);
2785         if (cmd)
2786         {
2788                 cmd[0] = LO_UINT16(STARTDELAY);
2789                 cmd[1] = HI_UINT16(STARTDELAY);
2791                 status = rpcSendFrame((MT_RPC_CMD_SREQ | MT_RPC_SYS_ZDO),
2792                 MT_ZDO_STARTUP_FROM_APP, cmd, cmdLen);
2794                 //read the SREQ from the queue
2795                 if (status == MT_RPC_SUCCESS)
2796                 {
2797                         //rpcSendFrame will block on the SRSP's, which will be
2798                         //pushed to the front of the queue
2799                         rpcWaitMqClientMsg(50);
2801                         //set status to status of srsp
2802                         status = srspRpcBuff[2];
2803                 }
2805                 free(cmd);
2806                 return status;
2807         }
2808         else
2809         {
2810                 dbg_print(PRINT_LEVEL_WARNING, "Memory for cmd was not allocated\n");
2811                 return 1;
2812         }
2815 /*************************************************************************************************
2816  * @fn      zdoProcess()
2817  *
2818  * @brief   read and process the RPC ZDO message from the ZB SoC
2819  *
2820  * @param   none
2821  *
2822  * @return  length of current Rx Buffer
2823  ***********************************************************************************************/
2824 void zdoProcess(uint8_t *rpcBuff, uint8_t rpcLen)
2826         dbg_print(PRINT_LEVEL_VERBOSE, "zdoProcess: processing CMD0:%x, CMD1:%x\n",
2827                 rpcBuff[0], rpcBuff[1]);
2829         //process the synchronous SRSP from SREQ
2830         if ((rpcBuff[0] & MT_RPC_CMD_TYPE_MASK) == MT_RPC_CMD_SRSP)
2831         {
2832                 processSrsp(rpcBuff, rpcLen);
2833         }
2834         else
2835         {
2836                 //Read CMD1 and processes the specific SREQ
2837                 switch (rpcBuff[1])
2838                 {
2839                 case MT_ZDO_STATE_CHANGE_IND:
2840                         dbg_print(PRINT_LEVEL_VERBOSE,
2841                                 "zdoProcess: MT_ZDO_STATE_CHANGE_IND\n");
2842                         processStateChange(rpcBuff, rpcLen);
2843                         break;
2844                 case MT_ZDO_NWK_ADDR_RSP:
2845                         dbg_print(PRINT_LEVEL_VERBOSE, "zdoProcess: MT_ZDO_NWK_ADDR_RSP\n");
2846                         processNwkAddrRsp(rpcBuff, rpcLen);
2847                         break;
2848                 case MT_ZDO_IEEE_ADDR_RSP:
2849                         dbg_print(PRINT_LEVEL_VERBOSE,
2850                                 "zdoProcess: MT_ZDO_IEEE_ADDR_RSP\n");
2851                         processIeeeAddrRsp(rpcBuff, rpcLen);
2852                         break;
2853                 case MT_ZDO_NODE_DESC_RSP:
2854                         dbg_print(PRINT_LEVEL_VERBOSE,
2855                                 "zdoProcess: MT_ZDO_NODE_DESC_RSP\n");
2856                         processNodeDescRsp(rpcBuff, rpcLen);
2857                         break;
2858                 case MT_ZDO_POWER_DESC_RSP:
2859                         dbg_print(PRINT_LEVEL_VERBOSE,
2860                                 "zdoProcess: MT_ZDO_POWER_DESC_RSP\n");
2861                         processPowerDescRsp(rpcBuff, rpcLen);
2862                         break;
2863                 case MT_ZDO_SIMPLE_DESC_RSP:
2864                         dbg_print(PRINT_LEVEL_VERBOSE,
2865                                 "zdoProcess: MT_ZDO_SIMPLE_DESC_RSP\n");
2866                         processSimpleDescRsp(rpcBuff, rpcLen);
2867                         break;
2868                 case MT_ZDO_ACTIVE_EP_RSP:
2869                         dbg_print(PRINT_LEVEL_VERBOSE,
2870                                 "zdoProcess: MT_ZDO_ACTIVE_EP_RSP\n");
2871                         processActiveEpRsp(rpcBuff, rpcLen);
2872                         break;
2873                 case MT_ZDO_MATCH_DESC_RSP:
2874                         dbg_print(PRINT_LEVEL_VERBOSE,
2875                                 "zdoProcess: MT_ZDO_MATCH_DESC_RSP\n");
2876                         processMatchDescRsp(rpcBuff, rpcLen);
2877                         break;
2878                 case 0x90:
2879                         dbg_print(PRINT_LEVEL_VERBOSE,
2880                                 "zdoProcess: MT_ZDO_COMPLEX_DESC_RSP\n");
2881                         processComplexDescRsp(rpcBuff, rpcLen);
2882                         break;
2883                 case MT_ZDO_USER_DESC_RSP:
2884                         dbg_print(PRINT_LEVEL_VERBOSE,
2885                                 "zdoProcess: MT_ZDO_USER_DESC_RSP\n");
2886                         processUserDescRsp(rpcBuff, rpcLen);
2887                         break;
2888                 case MT_ZDO_USER_DESC_CONF:
2889                         dbg_print(PRINT_LEVEL_VERBOSE,
2890                                 "zdoProcess: MT_ZDO_USER_DESC_CONF\n");
2891                         processUserDescConf(rpcBuff, rpcLen);
2892                         break;
2893                 case MT_ZDO_SERVER_DISC_RSP:
2894                         dbg_print(PRINT_LEVEL_VERBOSE,
2895                                 "zdoProcess: MT_ZDO_SERVER_DISC_RSP\n");
2896                         processServerDiscRsp(rpcBuff, rpcLen);
2897                         break;
2898                 case MT_ZDO_END_DEVICE_BIND_RSP:
2899                         dbg_print(PRINT_LEVEL_VERBOSE,
2900                                 "zdoProcess: MT_ZDO_END_DEVICE_BIND_RSP\n");
2901                         processEndDeviceBindRsp(rpcBuff, rpcLen);
2902                         break;
2903                 case MT_ZDO_BIND_RSP:
2904                         dbg_print(PRINT_LEVEL_VERBOSE, "zdoProcess: MT_ZDO_BIND_RSP\n");
2905                         processBindRsp(rpcBuff, rpcLen);
2906                         break;
2907                 case MT_ZDO_UNBIND_RSP:
2908                         dbg_print(PRINT_LEVEL_VERBOSE, "zdoProcess: MT_ZDO_UNBIND_RSP\n");
2909                         processUnbindRsp(rpcBuff, rpcLen);
2910                         break;
2911                 case MT_ZDO_MGMT_NWK_DISC_RSP:
2912                         dbg_print(PRINT_LEVEL_VERBOSE,
2913                                 "zdoProcess: MT_ZDO_MGMT_NWK_DISC_RSP\n");
2914                         processMgmtNwkDiscRsp(rpcBuff, rpcLen);
2915                         break;
2916                 case MT_ZDO_MGMT_LQI_RSP:
2917                         dbg_print(PRINT_LEVEL_VERBOSE, "zdoProcess: MT_ZDO_MGMT_LQI_RSP\n");
2918                         processMgmtLqiRsp(rpcBuff, rpcLen);
2919                         break;
2920                 case MT_ZDO_MGMT_RTG_RSP:
2921                         dbg_print(PRINT_LEVEL_VERBOSE, "zdoProcess: MT_ZDO_MGMT_RTG_RSP\n");
2922                         processMgmtRtgRsp(rpcBuff, rpcLen);
2923                         break;
2924                 case MT_ZDO_MGMT_BIND_RSP:
2925                         dbg_print(PRINT_LEVEL_VERBOSE,
2926                                 "zdoProcess: MT_ZDO_MGMT_BIND_RSP\n");
2927                         processMgmtBindRsp(rpcBuff, rpcLen);
2928                         break;
2929                 case MT_ZDO_MGMT_LEAVE_RSP:
2930                         dbg_print(PRINT_LEVEL_VERBOSE,
2931                                 "zdoProcess: MT_ZDO_MGMT_LEAVE_RSP\n");
2932                         processMgmtLeaveRsp(rpcBuff, rpcLen);
2933                         break;
2934                 case MT_ZDO_MGMT_DIRECT_JOIN_RSP:
2935                         dbg_print(PRINT_LEVEL_VERBOSE,
2936                                 "zdoProcess: MT_ZDO_MGMT_DIRECT_JOIN_RSP\n");
2937                         processMgmtDirectJoinRsp(rpcBuff, rpcLen);
2938                         break;
2939                 case MT_ZDO_MGMT_PERMIT_JOIN_RSP:
2940                         dbg_print(PRINT_LEVEL_VERBOSE,
2941                                 "zdoProcess: MT_ZDO_MGMT_PERMIT_JOIN_RSP\n");
2942                         processMgmtPermitJoinRsp(rpcBuff, rpcLen);
2943                         break;
2944                 case MT_ZDO_END_DEVICE_ANNCE_IND:
2945                         dbg_print(PRINT_LEVEL_VERBOSE,
2946                                 "zdoProcess: MT_ZDO_END_DEVICE_ANNCE_IND\n");
2947                         processEndDeviceAnnceInd(rpcBuff, rpcLen);
2948                         break;
2949                 case MT_ZDO_MATCH_DESC_RSP_SENT:
2950                         dbg_print(PRINT_LEVEL_VERBOSE,
2951                                 "zdoProcess: MT_ZDO_MATCH_DESC_RSP_SENT\n");
2952                         processMatchDescRspSent(rpcBuff, rpcLen);
2953                         break;
2954                 case MT_ZDO_STATUS_ERROR_RSP:
2955                         dbg_print(PRINT_LEVEL_VERBOSE,
2956                                 "zdoProcess: MT_ZDO_STATUS_ERROR_RSP\n");
2957                         processStatusErrorRsp(rpcBuff, rpcLen);
2958                         break;
2959                 case MT_ZDO_SRC_RTG_IND:
2960                         dbg_print(PRINT_LEVEL_VERBOSE, "zdoProcess: MT_ZDO_SRC_RTG_IND\n");
2961                         processSrcRtgInd(rpcBuff, rpcLen);
2962                         break;
2963                 case MT_ZDO_BEACON_NOTIFY_IND:
2964                         dbg_print(PRINT_LEVEL_VERBOSE,
2965                                 "zdoProcess: MT_ZDO_BEACON_NOTIFY_IND\n");
2966                         processBeaconNotifyInd(rpcBuff, rpcLen);
2967                         break;
2968                 case MT_ZDO_JOIN_CNF:
2969                         dbg_print(PRINT_LEVEL_VERBOSE, "zdoProcess: MT_ZDO_JOIN_CNF\n");
2970                         processJoinCnf(rpcBuff, rpcLen);
2971                         break;
2972                 case MT_ZDO_NWK_DISCOVERY_CNF:
2973                         dbg_print(PRINT_LEVEL_VERBOSE,
2974                                 "zdoProcess: MT_ZDO_NWK_DISCOVERY_CNF\n");
2975                         processNwkDiscoveryCnf(rpcBuff, rpcLen);
2976                         break;
2977                 case MT_ZDO_LEAVE_IND:
2978                         dbg_print(PRINT_LEVEL_VERBOSE, "zdoProcess: MT_ZDO_LEAVE_IND\n");
2979                         processLeaveInd(rpcBuff, rpcLen);
2980                         break;
2981                 case MT_ZDO_MSG_CB_INCOMING:
2982                         dbg_print(PRINT_LEVEL_VERBOSE,
2983                                 "zdoProcess: MT_ZDO_MSG_CB_INCOMING\n");
2984                         processMsgCbIncoming(rpcBuff, rpcLen);
2985                         break;
2987                 default:
2988                         dbg_print(PRINT_LEVEL_WARNING,
2989                                 "zdoProcess: CMD0:%x, CMD1:%x, not handled\n", rpcBuff[0],
2990                                 rpcBuff[1]);
2991                         break;
2992                 }
2993         }
2995 /*********************************************************************
2996  * @fn      processSrsp
2997  *
2998  * @brief  Generic function for processing the SRSP and copying it to
2999  *         local buffer for SREQ function to deal with
3000  *
3001  * @param
3002  *
3003  * @return
3004  */
3005 static void processSrsp(uint8_t *rpcBuff, uint8_t rpcLen)
3007         //copies sresp to local buffer
3008         memcpy(srspRpcBuff, rpcBuff, rpcLen);
3009         //srspRpcLen = rpcLen;
3010         switch (rpcBuff[1])
3011         {
3012         case MT_ZDO_GET_LINK_KEY:
3013                 dbg_print(PRINT_LEVEL_VERBOSE, "zdoProcess: MT_ZDO_GET_LINK_KEY\n");
3014                 processGetLinkKey(rpcBuff, rpcLen);
3015                 break;
3016         default:
3017                 dbg_print(PRINT_LEVEL_INFO, "processSrsp: unsupported message\n");
3018                 break;
3019         }
3022 /*********************************************************************
3023  * @fn      zbRegisterZdoCallbacks
3024  *
3025  * @brief
3026  *
3027  * @param
3028  *
3029  * @return
3030  */
3031 void zdoRegisterCallbacks(mtZdoCb_t cbs)
3033         memcpy(&mtZdoCbs, &cbs, sizeof(mtZdoCb_t));