-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathznp.c
734 lines (639 loc) · 21.3 KB
/
znp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
/**************************************************************************************************
* Filename: dataSendRcv.c
* Description: This file contains dataSendRcv application.
*
*
* Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com/
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* Neither the name of Texas Instruments Incorporated nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
/*********************************************************************
* INCLUDES
*/
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include "znp.h"
#include "rpc.h"
#include "mtSys.h"
#include "mtZdo.h"
#include "mtAf.h"
#include "mtParser.h"
#include "rpcTransport.h"
#include "dbgPrint.h"
#include "hostConsole.h"
/*********************************************************************
* MACROS
*/
/*********************************************************************
* TYPES
*/
/*********************************************************************
* LOCAL VARIABLE
*/
// init ZDO device state
devStates_t devState = DEV_HOLD;
uint8_t gSrcEndPoint = 1;
uint8_t gDstEndPoint = 1;
/***********************************************************************/
static const char *TAG = "znp";
/*********************************************************************
* LOCAL FUNCTIONS
*/
// ZDO Callbacks
static uint8_t mtSysPingIndCb(PingSrspFormat_t *msg);
static uint8_t mtZdoStateChangeIndCb(uint8_t newDevState);
static uint8_t mtZdoSimpleDescRspCb(SimpleDescRspFormat_t *msg);
static uint8_t mtZdoActiveEpRspCb(ActiveEpRspFormat_t *msg);
static uint8_t mtZdoEndDeviceAnnceIndCb(EndDeviceAnnceIndFormat_t *msg);
static uint8_t mtZdoMgmtLqiRspCb(MgmtLqiRspFormat_t *msg);
// SYS Callbacks
static uint8_t mtSysResetIndCb(ResetIndFormat_t *msg);
// AF callbacks
static uint8_t mtAfDataConfirmCb(DataConfirmFormat_t *msg);
static uint8_t mtAfIncomingMsgCb(IncomingMsgFormat_t *msg);
// helper functions
static uint8_t setNVStartup(uint8_t startupOption);
static uint8_t setNVChanList(uint32_t chanList);
static uint8_t setNVPanID(uint32_t panId);
static uint8_t setNVDevType(uint8_t devType);
static int32_t startNetwork(void);
static int32_t registerAf(void);
/*********************************************************************
* CALLBACK FUNCTIONS
*/
// SYS callbacks
static mtSysCb_t mtSysCb =
{mtSysPingIndCb, NULL, NULL, mtSysResetIndCb, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL};
static mtZdoCb_t mtZdoCb =
{NULL, // MT_ZDO_NWK_ADDR_RSP
NULL, // MT_ZDO_IEEE_ADDR_RSP
NULL, // MT_ZDO_NODE_DESC_RSP
NULL, // MT_ZDO_POWER_DESC_RSP
mtZdoSimpleDescRspCb, // MT_ZDO_SIMPLE_DESC_RSP
mtZdoActiveEpRspCb, // MT_ZDO_ACTIVE_EP_RSP
NULL, // MT_ZDO_MATCH_DESC_RSP
NULL, // MT_ZDO_COMPLEX_DESC_RSP
NULL, // MT_ZDO_USER_DESC_RSP
NULL, // MT_ZDO_USER_DESC_CONF
NULL, // MT_ZDO_SERVER_DISC_RSP
NULL, // MT_ZDO_END_DEVICE_BIND_RSP
NULL, // MT_ZDO_BIND_RSP
NULL, // MT_ZDO_UNBIND_RSP
NULL, // MT_ZDO_MGMT_NWK_DISC_RSP
mtZdoMgmtLqiRspCb, // MT_ZDO_MGMT_LQI_RSP
NULL, // MT_ZDO_MGMT_RTG_RSP
NULL, // MT_ZDO_MGMT_BIND_RSP
NULL, // MT_ZDO_MGMT_LEAVE_RSP
NULL, // MT_ZDO_MGMT_DIRECT_JOIN_RSP
NULL, // MT_ZDO_MGMT_PERMIT_JOIN_RSP
mtZdoStateChangeIndCb, // MT_ZDO_STATE_CHANGE_IND
mtZdoEndDeviceAnnceIndCb, // MT_ZDO_END_DEVICE_ANNCE_IND
NULL, // MT_ZDO_SRC_RTG_IND
NULL, // MT_ZDO_BEACON_NOTIFY_IND
NULL, // MT_ZDO_JOIN_CNF
NULL, // MT_ZDO_NWK_DISCOVERY_CNF
NULL, // MT_ZDO_CONCENTRATOR_IND_CB
NULL, // MT_ZDO_LEAVE_IND
NULL, // MT_ZDO_STATUS_ERROR_RSP
NULL, // MT_ZDO_MATCH_DESC_RSP_SENT
NULL, NULL};
static mtAfCb_t mtAfCb =
{
mtAfDataConfirmCb, // MT_AF_DATA_CONFIRM
mtAfIncomingMsgCb, // MT_AF_INCOMING_MSG
NULL, // MT_AF_INCOMING_MSG_EXT
NULL, // MT_AF_DATA_RETRIEVE
NULL, // MT_AF_REFLECT_ERROR
};
typedef struct
{
uint16_t ChildAddr;
uint8_t Type;
} ChildNode_t;
typedef struct
{
uint16_t NodeAddr;
uint8_t Type;
uint8_t ChildCount;
ChildNode_t childs[256];
} Node_t;
Node_t nodeList[64];
uint8_t nodeCount = 0;
/********************************************************************
* START OF SYS CALL BACK FUNCTIONS
*/
static uint8_t mtSysResetIndCb(ResetIndFormat_t *msg)
{
consolePrint(TAG, "ZNP Version: %d.%d.%d", msg->MajorRel, msg->MinorRel,
msg->HwRev);
return 0;
}
static uint8_t mtSysPingIndCb(PingSrspFormat_t *msg)
{
consolePrint(TAG, "ping: %s", msg);
return 0;
}
/********************************************************************
* START OF ZDO CALL BACK FUNCTIONS
*/
/********************************************************************
* @fn Callback function for ZDO State Change Indication
* @brief receives the AREQ status and specifies the change ZDO state
*
* @param uint8 zdoState
*
* @return SUCCESS or FAILURE
*/
static uint8_t mtZdoStateChangeIndCb(uint8_t newDevState)
{
switch (newDevState)
{
case DEV_HOLD:
dbg_print(TAG, PRINT_LEVEL_INFO,
"mtZdoStateChangeIndCb: Initialized - not started automatically");
break;
case DEV_INIT:
dbg_print(TAG, PRINT_LEVEL_INFO,
"mtZdoStateChangeIndCb: Initialized - not connected to anything");
break;
case DEV_NWK_DISC:
dbg_print(TAG, PRINT_LEVEL_INFO,
"mtZdoStateChangeIndCb: Discovering PAN's to join");
consolePrint(TAG, "Network Discovering");
break;
case DEV_NWK_JOINING:
dbg_print(TAG, PRINT_LEVEL_INFO, "mtZdoStateChangeIndCb: Joining a PAN");
consolePrint(TAG, "Network Joining");
break;
case DEV_NWK_REJOIN:
dbg_print(TAG, PRINT_LEVEL_INFO,
"mtZdoStateChangeIndCb: ReJoining a PAN, only for end devices");
consolePrint(TAG, "Network Rejoining");
break;
case DEV_END_DEVICE_UNAUTH:
consolePrint(TAG, "Network Authenticating");
dbg_print(TAG, PRINT_LEVEL_INFO,
"mtZdoStateChangeIndCb: Joined but not yet authenticated by trust center");
break;
case DEV_END_DEVICE:
consolePrint(TAG, "Network Joined");
dbg_print(TAG, PRINT_LEVEL_INFO,
"mtZdoStateChangeIndCb: Started as device after authentication");
break;
case DEV_ROUTER:
consolePrint(TAG, "Network Joined");
dbg_print(TAG, PRINT_LEVEL_INFO,
"mtZdoStateChangeIndCb: Device joined, authenticated and is a router");
break;
case DEV_COORD_STARTING:
consolePrint(TAG, "Network Starting");
dbg_print(TAG, PRINT_LEVEL_INFO,
"mtZdoStateChangeIndCb: Started as Zigbee Coordinator");
break;
case DEV_ZB_COORD:
consolePrint(TAG, "Network Started");
dbg_print(TAG, PRINT_LEVEL_INFO,
"mtZdoStateChangeIndCb: Started as Zigbee Coordinator");
break;
case DEV_NWK_ORPHAN:
consolePrint(TAG, "Network Orphaned");
dbg_print(TAG, PRINT_LEVEL_INFO,
"mtZdoStateChangeIndCb: Device has lost information about its parent");
break;
default:
dbg_print(TAG, PRINT_LEVEL_INFO, "mtZdoStateChangeIndCb: unknown state");
break;
}
zigbeeNetworkState->devState = (devStates_t)newDevState;
return SUCCESS;
}
static uint8_t mtZdoSimpleDescRspCb(SimpleDescRspFormat_t *msg)
{
if (msg->Status == MT_RPC_SUCCESS)
{
consolePrint(TAG, "\tEndpoint: 0x%02X", msg->Endpoint);
consolePrint(TAG, "\tProfileID: 0x%04X", msg->ProfileID);
consolePrint(TAG, "\tDeviceID: 0x%04X", msg->DeviceID);
consolePrint(TAG, "\tDeviceVersion: 0x%02X", msg->DeviceVersion);
consolePrint(TAG, "\tNumInClusters: %d", msg->NumInClusters);
uint32_t i;
for (i = 0; i < msg->NumInClusters; i++)
{
consolePrint(TAG, "\t\tInClusterList[%d]: 0x%04X", i,
msg->InClusterList[i]);
}
consolePrint(TAG, "\tNumOutClusters: %d", msg->NumOutClusters);
for (i = 0; i < msg->NumOutClusters; i++)
{
consolePrint(TAG, "\t\tOutClusterList[%d]: 0x%04X", i,
msg->OutClusterList[i]);
}
consolePrint(TAG, "");
}
else
{
consolePrint(TAG, "SimpleDescRsp Status: FAIL 0x%02X", msg->Status);
}
return msg->Status;
}
static uint8_t mtZdoMgmtLqiRspCb(MgmtLqiRspFormat_t *msg)
{
uint8_t devType = 0;
uint8_t devRelation = 0;
MgmtLqiReqFormat_t req;
if (msg->Status == MT_RPC_SUCCESS)
{
nodeList[nodeCount].NodeAddr = msg->SrcAddr;
nodeList[nodeCount].Type = (msg->SrcAddr == 0 ? DEVICETYPE_COORDINATOR : DEVICETYPE_ROUTER);
nodeList[nodeCount].ChildCount = 0;
uint32_t i;
for (i = 0; i < msg->NeighborLqiListCount; i++)
{
devType = msg->NeighborLqiList[i].DevTyp_RxOnWhenIdle_Relat & 3;
devRelation = ((msg->NeighborLqiList[i].DevTyp_RxOnWhenIdle_Relat >> 4) & 7);
if (devRelation == 1 || devRelation == 3)
{
uint8_t cCount = nodeList[nodeCount].ChildCount;
nodeList[nodeCount].childs[cCount].ChildAddr =
msg->NeighborLqiList[i].NetworkAddress;
nodeList[nodeCount].childs[cCount].Type = devType;
nodeList[nodeCount].ChildCount++;
if (devType == DEVICETYPE_ROUTER)
{
req.DstAddr = msg->NeighborLqiList[i].NetworkAddress;
req.StartIndex = 0;
zdoMgmtLqiReq(&req);
}
}
}
nodeCount++;
}
else
{
consolePrint(TAG, "MgmtLqiRsp Status: FAIL 0x%02X", msg->Status);
}
return msg->Status;
}
static uint8_t mtZdoActiveEpRspCb(ActiveEpRspFormat_t *msg)
{
// SimpleDescReqFormat_t simReq;
consolePrint(TAG, "NwkAddr: 0x%04X", msg->NwkAddr);
if (msg->Status == MT_RPC_SUCCESS)
{
consolePrint(TAG, "Number of Endpoints: %d\nActive Endpoints: ",
msg->ActiveEPCount);
uint32_t i;
for (i = 0; i < msg->ActiveEPCount; i++)
{
consolePrint(TAG, "0x%02X\t", msg->ActiveEPList[i]);
}
consolePrint(TAG, "");
}
else
{
consolePrint(TAG, "ActiveEpRsp Status: FAIL 0x%02X", msg->Status);
}
return msg->Status;
}
static uint8_t mtZdoEndDeviceAnnceIndCb(EndDeviceAnnceIndFormat_t *msg)
{
ActiveEpReqFormat_t actReq;
actReq.DstAddr = msg->NwkAddr;
actReq.NwkAddrOfInterest = msg->NwkAddr;
consolePrint(TAG, "\nNew device joined network.");
zdoActiveEpReq(&actReq);
return 0;
}
/********************************************************************
* AF CALL BACK FUNCTIONS
*/
static uint8_t mtAfDataConfirmCb(DataConfirmFormat_t *msg)
{
if (msg->Status == MT_RPC_SUCCESS)
{
consolePrint(TAG, "Message transmited Succesfully!");
}
else
{
consolePrint(TAG, "Message failed to transmit");
}
return msg->Status;
}
static uint8_t mtAfIncomingMsgCb(IncomingMsgFormat_t *msg)
{
consolePrint(TAG,
"\nIncoming Message from Endpoint 0x%02X and Address 0x%04X:",
msg->SrcEndpoint, msg->SrcAddr);
msg->Data[msg->Len] = '\0';
consolePrint(TAG, "%s", (char *)msg->Data);
consolePrint(TAG,
"\nEnter message to send or type CHANGE to change the destination \nor QUIT to exit:");
return 0;
}
/********************************************************************
* HELPER FUNCTIONS
*/
// helper functions for building and sending the NV messages
static uint8_t setNVStartup(uint8_t startupOption)
{
uint8_t status;
OsalNvWriteFormat_t nvWrite;
// sending startup option
nvWrite.Id = ZCD_NV_STARTUP_OPTION;
nvWrite.Offset = 0;
nvWrite.Len = 1;
nvWrite.Value[0] = startupOption;
status = sysOsalNvWrite(&nvWrite);
dbg_print(TAG, PRINT_LEVEL_INFO, "");
dbg_print(TAG, PRINT_LEVEL_INFO, "NV Write Startup Option cmd sent[%d]...",
status);
return status;
}
static uint8_t setNVDevType(uint8_t devType)
{
uint8_t status;
OsalNvWriteFormat_t nvWrite;
// setting dev type
nvWrite.Id = ZCD_NV_LOGICAL_TYPE;
nvWrite.Offset = 0;
nvWrite.Len = 1;
nvWrite.Value[0] = devType;
status = sysOsalNvWrite(&nvWrite);
dbg_print(TAG, PRINT_LEVEL_INFO, "");
dbg_print(TAG, PRINT_LEVEL_INFO, "NV Write Device Type cmd sent... [%d]",
status);
return status;
}
static uint8_t setNVPanID(uint32_t panId)
{
uint8_t status;
OsalNvWriteFormat_t nvWrite;
dbg_print(TAG, PRINT_LEVEL_INFO, "");
dbg_print(TAG, PRINT_LEVEL_INFO, "NV Write PAN ID cmd sending...");
nvWrite.Id = ZCD_NV_PANID;
nvWrite.Offset = 0;
nvWrite.Len = 2;
nvWrite.Value[0] = LO_UINT16(panId);
nvWrite.Value[1] = HI_UINT16(panId);
status = sysOsalNvWrite(&nvWrite);
dbg_print(TAG, PRINT_LEVEL_INFO, "");
dbg_print(TAG, PRINT_LEVEL_INFO, "NV Write PAN ID cmd sent...[%d]", status);
return status;
}
static uint8_t setNVChanList(uint32_t chanList)
{
OsalNvWriteFormat_t nvWrite;
uint8_t status;
// setting chanList
nvWrite.Id = ZCD_NV_CHANLIST;
nvWrite.Offset = 0;
nvWrite.Len = 4;
nvWrite.Value[0] = BREAK_UINT32(chanList, 0);
nvWrite.Value[1] = BREAK_UINT32(chanList, 1);
nvWrite.Value[2] = BREAK_UINT32(chanList, 2);
nvWrite.Value[3] = BREAK_UINT32(chanList, 3);
status = sysOsalNvWrite(&nvWrite);
dbg_print(TAG, PRINT_LEVEL_INFO, "");
dbg_print(TAG, PRINT_LEVEL_INFO, "NV Write Channel List cmd sent...[%d]",
status);
return status;
}
uint8_t dType;
static int32_t startNetwork(void)
{
uint8_t devType = DEVICETYPE_COORDINATOR;
int32_t status = MT_RPC_SUCCESS;
uint8_t newNwk = 0;
status = setNVStartup(
ZCD_STARTOPT_CLEAR_STATE | ZCD_STARTOPT_CLEAR_CONFIG);
newNwk = 1;
if (status != MT_RPC_SUCCESS)
{
dbg_print(TAG, PRINT_LEVEL_WARNING, "network start failed");
return -1;
}
consolePrint(TAG, "Resetting ZNP");
ResetReqFormat_t resReq;
resReq.Type = 1;
sysResetReq(&resReq);
// flush the rsp
rpcWaitMqClientMsg(5000);
if (newNwk)
{
#ifndef CC26xx
// 协调器
devType = DEVICETYPE_COORDINATOR;
status = setNVDevType(devType);
if (status != MT_RPC_SUCCESS)
{
dbg_print(TAG, PRINT_LEVEL_WARNING, "setNVDevType failed");
return 0;
}
#endif // CC26xx
// Select random PAN ID for Coord and join any PAN for RTR/ED
status = setNVPanID(0xFFFF);
if (status != MT_RPC_SUCCESS)
{
dbg_print(TAG, PRINT_LEVEL_WARNING, "setNVPanID failed");
return -1;
}
// 默认 11 信道
status = setNVChanList(1 << atoi("11"));
if (status != MT_RPC_SUCCESS)
{
dbg_print(TAG, PRINT_LEVEL_INFO, "setNVPanID failed");
return -1;
}
}
registerAf();
consolePrint(TAG, "EndPoint: 1");
status = zdoInit();
if (status == NEW_NETWORK)
{
dbg_print(TAG, PRINT_LEVEL_INFO, "zdoInit NEW_NETWORK");
status = MT_RPC_SUCCESS;
}
else if (status == RESTORED_NETWORK)
{
dbg_print(TAG, PRINT_LEVEL_INFO, "zdoInit RESTORED_NETWORK");
status = MT_RPC_SUCCESS;
}
else
{
dbg_print(TAG, PRINT_LEVEL_INFO, "zdoInit failed");
status = -1;
}
dbg_print(TAG, PRINT_LEVEL_INFO, "process zdoStatechange callbacks");
// flush AREQ ZDO State Change messages
while (status != -1)
{
status = rpcWaitMqClientMsg(5000);
if (((devType == DEVICETYPE_COORDINATOR) && (zigbeeNetworkState->devState == DEV_ZB_COORD)) || ((devType == DEVICETYPE_ROUTER) && (zigbeeNetworkState->devState == DEV_ROUTER)) || ((devType == DEVICETYPE_ENDDEVICE) && (zigbeeNetworkState->devState == DEV_END_DEVICE)))
{
break;
}
}
// set startup option back to keep configuration in case of reset
status = setNVStartup(0);
if (zigbeeNetworkState->devState < DEV_END_DEVICE)
{
// start network failed
return -1;
}
return 0;
}
static int32_t registerAf(void)
{
int32_t status = 0;
RegisterFormat_t reg;
reg.EndPoint = 1;
reg.AppProfId = 0x0104;
reg.AppDeviceId = 0x0100;
reg.AppDevVer = 1;
reg.LatencyReq = 0;
reg.AppNumInClusters = 1;
reg.AppInClusterList[0] = 0x0006;
reg.AppNumOutClusters = 0;
status = afRegister(®);
return status;
}
static void displayDevices(void)
{
ActiveEpReqFormat_t actReq;
int32_t status;
MgmtLqiReqFormat_t req;
req.DstAddr = 0;
req.StartIndex = 0;
nodeCount = 0;
zdoMgmtLqiReq(&req);
do
{
status = rpcWaitMqClientMsg(1000);
} while (status != -1);
consolePrint(TAG, "Available devices:");
uint8_t i;
for (i = 0; i < nodeCount; i++)
{
char *devtype =
(nodeList[i].Type == DEVICETYPE_ROUTER ? "ROUTER" : "COORDINATOR");
consolePrint(TAG, "Type: %s", devtype);
actReq.DstAddr = nodeList[i].NodeAddr;
actReq.NwkAddrOfInterest = nodeList[i].NodeAddr;
zdoActiveEpReq(&actReq);
rpcGetMqClientMsg();
do
{
status = rpcWaitMqClientMsg(1000);
} while (status != -1);
uint8_t cI;
for (cI = 0; cI < nodeList[i].ChildCount; cI++)
{
uint8_t type = nodeList[i].childs[cI].Type;
if (type == DEVICETYPE_ENDDEVICE)
{
consolePrint(TAG, "Type: END DEVICE");
actReq.DstAddr = nodeList[i].childs[cI].ChildAddr;
actReq.NwkAddrOfInterest = nodeList[i].childs[cI].ChildAddr;
zdoActiveEpReq(&actReq);
status = 0;
rpcGetMqClientMsg();
while (status != -1)
{
status = rpcWaitMqClientMsg(1000);
}
}
}
consolePrint(TAG, "");
}
}
/*********************************************************************
* INTERFACE FUNCTIONS
*/
uint32_t znpInit(void)
{
int32_t status = 0;
uint32_t msgCnt = 0;
// Flush all messages from the que
while (status != -1)
{
status = rpcWaitMqClientMsg(10);
if (status != -1)
{
msgCnt++;
}
}
dbg_print(TAG, PRINT_LEVEL_INFO, "flushed %d message from msg queue", msgCnt);
// Register Callbacks MT system callbacks
sysRegisterCallbacks(mtSysCb);
zdoRegisterCallbacks(mtZdoCb);
afRegisterCallbacks(mtAfCb);
return 0;
}
uint8_t initDone = 0;
void *znpMsgProcess(void *argument)
{
if (initDone)
{
rpcWaitMqClientMsg(10000);
}
return 0;
}
void appProcess(void *argument)
{
int32_t status;
// Flush all messages from the que
do
{
status = rpcWaitMqClientMsg(50);
} while (status != -1);
devState = DEV_HOLD;
status = startNetwork();
if (status != -1)
{
consolePrint(TAG, "Network up");
}
else
{
consolePrint(TAG, "Network Error");
}
OsalNvWriteFormat_t nvWrite;
nvWrite.Id = ZCD_NV_ZDO_DIRECT_CB;
nvWrite.Offset = 0;
nvWrite.Len = 1;
nvWrite.Value[0] = 1;
status = sysOsalNvWrite(&nvWrite);
displayDevices();
sysGetExtAddr();
return;
}