-
Notifications
You must be signed in to change notification settings - Fork 279
/
zStackAdapter.ts
1084 lines (952 loc) · 48.3 KB
/
zStackAdapter.ts
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
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {
NetworkOptions, SerialPortOptions, Coordinator, CoordinatorVersion, NodeDescriptor,
DeviceType, ActiveEndpoints, SimpleDescriptor, LQI, RoutingTable, NetworkParameters,
StartResult, LQINeighbor, RoutingTableEntry, AdapterOptions,
} from '../../tstype';
import {ZnpVersion} from './tstype';
import * as Events from '../../events';
import Adapter from '../../adapter';
import {Znp, ZpiObject} from '../znp';
import {Constants as UnpiConstants} from '../unpi';
import {ZclFrame, FrameType, Direction, Foundation} from '../../../zcl';
import {Queue, Waitress, Wait} from '../../../utils';
import * as Constants from '../constants';
import Debug from "debug";
import debounce from 'debounce';
import {LoggerStub} from "../../../controller/logger-stub";
import {ZnpAdapterManager} from "./manager";
import * as Models from "../../../models";
import assert from 'assert';
const debug = Debug("zigbee-herdsman:adapter:zStack:adapter");
const Subsystem = UnpiConstants.Subsystem;
const Type = UnpiConstants.Type;
const {ZnpCommandStatus, AddressMode} = Constants.COMMON;
const DataConfirmTimeout = 9999; // Not an actual code
const DataConfirmErrorCodeLookup: {[k: number]: string} = {
[DataConfirmTimeout]: 'Timeout',
26: 'MAC no resources',
183: 'APS no ack',
205: 'No network route',
225: 'MAC channel access failure',
233: 'MAC no ack',
240: 'MAC transaction expired',
};
interface WaitressMatcher {
address: number | string;
endpoint: number;
transactionSequenceNumber?: number;
frameType: FrameType;
clusterID: number;
commandIdentifier: number;
direction: number;
}
class DataConfirmError extends Error {
public code: number;
constructor (code: number) {
const message = `Data request failed with error: '${DataConfirmErrorCodeLookup[code]}' (${code})`;
super(message);
this.code = code;
}
}
class ZStackAdapter extends Adapter {
private deviceAnnounceRouteDiscoveryDebouncers : Map<number, () => void>;
private znp: Znp;
private adapterManager: ZnpAdapterManager;
private transactionID: number;
private version: {
product: number; transportrev: number; majorrel: number; minorrel: number; maintrel: number; revision: string;
};
private closing: boolean;
private queue: Queue;
private supportsLED: boolean = null;
private interpanLock: boolean;
private interpanEndpointRegistered: boolean;
private waitress: Waitress<Events.ZclDataPayload, WaitressMatcher>;
public constructor(networkOptions: NetworkOptions,
serialPortOptions: SerialPortOptions, backupPath: string, adapterOptions: AdapterOptions, logger?: LoggerStub) {
super(networkOptions, serialPortOptions, backupPath, adapterOptions, logger);
this.znp = new Znp(this.serialPortOptions.path, this.serialPortOptions.baudRate, this.serialPortOptions.rtscts);
this.transactionID = 0;
this.deviceAnnounceRouteDiscoveryDebouncers = new Map();
this.interpanLock = false;
this.interpanEndpointRegistered = false;
this.closing = false;
this.waitress = new Waitress<Events.ZclDataPayload, WaitressMatcher>(
this.waitressValidator, this.waitressTimeoutFormatter
);
this.znp.on('received', this.onZnpRecieved.bind(this));
this.znp.on('close', this.onZnpClose.bind(this));
}
/**
* Adapter methods
*/
public async start(): Promise<StartResult> {
await this.znp.open();
const attempts = 3;
for (let i = 0; i < attempts; i++) {
try {
await this.znp.request(Subsystem.SYS, 'ping', {capabilities: 1});
break;
} catch (e) {
if (attempts - 1 === i) {
throw new Error(`Failed to connect to the adapter (${e})`);
}
}
}
// Old firmware did not support version, assume it's Z-Stack 1.2 for now.
try {
this.version = (await this.znp.request(Subsystem.SYS, 'version', {})).payload;
} catch (e) {
debug(`Failed to get zStack version, assuming 1.2`);
this.version = {"transportrev":2, "product":0, "majorrel":2, "minorrel":0, "maintrel":0, "revision":""};
}
const concurrent = this.adapterOptions && this.adapterOptions.concurrent ?
this.adapterOptions.concurrent :
(this.version.product === ZnpVersion.zStack3x0 ? 16 : 2);
debug(`Adapter concurrent: ${concurrent}`);
this.queue = new Queue(concurrent);
debug(`Detected znp version '${ZnpVersion[this.version.product]}' (${JSON.stringify(this.version)})`);
this.adapterManager = new ZnpAdapterManager(
this.znp,
{
backupPath: this.backupPath,
version: this.version.product,
greenPowerGroup: this.greenPowerGroup,
networkOptions: this.networkOptions,
adapterOptions: this.adapterOptions,
},
this.logger
);
const startResult = this.adapterManager.start();
if (this.adapterOptions.disableLED) {
// Wait a bit for adapter to startup, otherwise led doesn't disable (tested with CC2531)
await Wait(200);
await this.setLED('disable');
}
return startResult;
}
public async stop(): Promise<void> {
this.closing = true;
await this.znp.close();
}
public static async isValidPath(path: string): Promise<boolean> {
return Znp.isValidPath(path);
}
public static async autoDetectPath(): Promise<string> {
return Znp.autoDetectPath();
}
public async getCoordinator(): Promise<Coordinator> {
return this.queue.execute<Coordinator>(async () => {
this.checkInterpanLock();
const activeEpRsp = this.znp.waitFor(UnpiConstants.Type.AREQ, Subsystem.ZDO, 'activeEpRsp');
await this.znp.request(Subsystem.ZDO, 'activeEpReq', {dstaddr: 0, nwkaddrofinterest: 0}, activeEpRsp.ID);
const activeEp = await activeEpRsp.start().promise;
const deviceInfo = await this.znp.request(Subsystem.UTIL, 'getDeviceInfo', {});
const endpoints = [];
for (const endpoint of activeEp.payload.activeeplist) {
const simpleDescRsp = this.znp.waitFor(
UnpiConstants.Type.AREQ, Subsystem.ZDO, 'simpleDescRsp', {endpoint}
);
await this.znp.request(
Subsystem.ZDO, 'simpleDescReq', {dstaddr: 0, nwkaddrofinterest: 0, endpoint}, simpleDescRsp.ID
);
const simpleDesc = await simpleDescRsp.start().promise;
endpoints.push({
ID: simpleDesc.payload.endpoint,
profileID: simpleDesc.payload.profileid,
deviceID: simpleDesc.payload.deviceid,
inputClusters: simpleDesc.payload.inclusterlist,
outputClusters: simpleDesc.payload.outclusterlist,
});
}
return {
networkAddress: 0,
manufacturerID: 0,
ieeeAddr: deviceInfo.payload.ieeeaddr,
endpoints,
};
});
}
public async permitJoin(seconds: number, networkAddress: number): Promise<void> {
const addrmode = networkAddress === null ? 0x0F : 0x02;
const dstaddr = networkAddress || 0xFFFC;
await this.queue.execute<void>(async () => {
this.checkInterpanLock();
const payload = {addrmode, dstaddr , duration: seconds, tcsignificance: 0};
await this.znp.request(Subsystem.ZDO, 'mgmtPermitJoinReq', payload);
await this.setLED(seconds == 0 ? 'off' : 'on');
});
}
public async getCoordinatorVersion(): Promise<CoordinatorVersion> {
return {type: ZnpVersion[this.version.product], meta: this.version};
}
public async reset(type: 'soft' | 'hard'): Promise<void> {
if (type === 'soft') {
await this.znp.request(Subsystem.SYS, 'resetReq', {type: Constants.SYS.resetType.SOFT});
} else {
await this.znp.request(Subsystem.SYS, 'resetReq', {type: Constants.SYS.resetType.HARD});
}
}
private async setLED(action: 'disable' | 'on' | 'off'): Promise<void> {
if (this.supportsLED == null) {
// Only zStack3x0 with 20210430 and greater support LED
const zStack3x0 = this.version.product === ZnpVersion.zStack3x0;
this.supportsLED = !zStack3x0 || (zStack3x0 && parseInt(this.version.revision) >= 20210430);
}
if (!this.supportsLED || (this.adapterOptions.disableLED && action !== 'disable')) {
return;
}
// Firmwares build on and after 20211029 should handle LED themselves
const firmwareControlsLed = parseInt(this.version.revision) >= 20211029;
const lookup = {
'disable': firmwareControlsLed ? {ledid: 0xFF, mode: 5} : {ledid: 3, mode: 0},
'on': firmwareControlsLed ? null : {ledid: 3, mode: 1},
'off': firmwareControlsLed ? null : {ledid: 3, mode: 0},
};
const payload = lookup[action];
if (payload) {
this.znp.request(Subsystem.UTIL, 'ledControl', payload, null, 500).catch(() => {
// We cannot 100% correctly determine if an adapter supports LED. E.g. the zStack 1.2 20190608
// fw supports led on the CC2531 but not on the CC2530. Therefore if a led request fails never thrown
// an error but instead mark the led as unsupported.
// https://github.com/Koenkk/zigbee-herdsman/issues/377
// https://github.com/Koenkk/zigbee2mqtt/issues/7693
this.supportsLED = false;
});
}
}
private async requestNetworkAddress(ieeeAddr: string): Promise<number> {
/**
* NOTE: There are cases where multiple nwkAddrRsp are recevied with different network addresses,
* this is currently not handled, the first nwkAddrRsp is taken.
*/
debug("Request network address of '%s'", ieeeAddr);
const response = this.znp.waitFor(UnpiConstants.Type.AREQ, Subsystem.ZDO, 'nwkAddrRsp', {ieeeaddr: ieeeAddr});
await this.znp.request(Subsystem.ZDO, 'nwkAddrReq', {ieeeaddr: ieeeAddr, reqtype: 0, startindex: 0});
const result = await response.start().promise;
return result.payload.nwkaddr;
}
private supportsAssocRemove(): boolean {
return this.version.product === ZnpVersion.zStack3x0 && parseInt(this.version.revision) >= 20200805;
}
private supportsAssocAdd(): boolean {
return this.version.product === ZnpVersion.zStack3x0 && parseInt(this.version.revision) >= 20201026;
}
private async discoverRoute(networkAddress: number, wait=true): Promise<void> {
debug('Discovering route to %d', networkAddress);
const payload = {dstAddr: networkAddress, options: 0, radius: Constants.AF.DEFAULT_RADIUS};
await this.znp.request(Subsystem.ZDO, 'extRouteDisc', payload);
if (wait) {
await Wait(3000);
}
}
public async nodeDescriptor(networkAddress: number): Promise<NodeDescriptor> {
return this.queue.execute<NodeDescriptor>(async () => {
this.checkInterpanLock();
try {
const result = await this.nodeDescriptorInternal(networkAddress);
return result;
} catch (error) {
debug(`Node descriptor request for '${networkAddress}' failed (${error}), retry`);
// Doing a route discovery after simple descriptor request fails makes it succeed sometimes.
// https://github.com/Koenkk/zigbee2mqtt/issues/3276
await this.discoverRoute(networkAddress);
const result = await this.nodeDescriptorInternal(networkAddress);
return result;
}
}, networkAddress);
}
private async nodeDescriptorInternal(networkAddress: number): Promise<NodeDescriptor> {
const response = this.znp.waitFor(Type.AREQ, Subsystem.ZDO, 'nodeDescRsp', {nwkaddr: networkAddress});
const payload = {dstaddr: networkAddress, nwkaddrofinterest: networkAddress};
await this.znp.request(Subsystem.ZDO, 'nodeDescReq', payload, response.ID);
const descriptor = await response.start().promise;
let type: DeviceType = 'Unknown';
const logicalType = descriptor.payload.logicaltype_cmplxdescavai_userdescavai & 0x07;
for (const [key, value] of Object.entries(Constants.ZDO.deviceLogicalType)) {
if (value === logicalType) {
if (key === 'COORDINATOR') type = 'Coordinator';
else if (key === 'ROUTER') type = 'Router';
else if (key === 'ENDDEVICE') type = 'EndDevice';
break;
}
}
return {manufacturerCode: descriptor.payload.manufacturercode, type};
}
public async activeEndpoints(networkAddress: number): Promise<ActiveEndpoints> {
return this.queue.execute<ActiveEndpoints>(async () => {
this.checkInterpanLock();
const response = this.znp.waitFor(Type.AREQ, Subsystem.ZDO, 'activeEpRsp', {nwkaddr: networkAddress});
const payload = {dstaddr: networkAddress, nwkaddrofinterest: networkAddress};
await this.znp.request(Subsystem.ZDO, 'activeEpReq', payload, response.ID);
const activeEp = await response.start().promise;
return {endpoints: activeEp.payload.activeeplist};
}, networkAddress);
}
public async simpleDescriptor(networkAddress: number, endpointID: number): Promise<SimpleDescriptor> {
return this.queue.execute<SimpleDescriptor>(async () => {
this.checkInterpanLock();
const responsePayload = {nwkaddr: networkAddress, endpoint: endpointID};
const response = this.znp.waitFor(Type.AREQ, Subsystem.ZDO, 'simpleDescRsp', responsePayload);
const payload = {dstaddr: networkAddress, nwkaddrofinterest: networkAddress, endpoint: endpointID};
await this.znp.request(Subsystem.ZDO, 'simpleDescReq', payload, response.ID);
const descriptor = await response.start().promise;
return {
profileID: descriptor.payload.profileid,
endpointID: descriptor.payload.endpoint,
deviceID: descriptor.payload.deviceid,
inputClusters: descriptor.payload.inclusterlist,
outputClusters: descriptor.payload.outclusterlist,
};
}, networkAddress);
}
public async sendZclFrameToEndpoint(
ieeeAddr: string, networkAddress: number, endpoint: number, zclFrame: ZclFrame, timeout: number,
disableResponse: boolean, disableRecovery: boolean, sourceEndpoint?: number,
): Promise<Events.ZclDataPayload> {
return this.queue.execute<Events.ZclDataPayload>(async () => {
this.checkInterpanLock();
return this.sendZclFrameToEndpointInternal(
ieeeAddr, networkAddress, endpoint, sourceEndpoint || 1, zclFrame, timeout, disableResponse,
disableRecovery, 0, 0, false, false, false, null
);
}, networkAddress);
}
private async sendZclFrameToEndpointInternal(
ieeeAddr: string, networkAddress: number, endpoint: number, sourceEndpoint: number, zclFrame: ZclFrame,
timeout: number, disableResponse: boolean, disableRecovery: boolean, responseAttempt: number,
dataRequestAttempt: number, checkedNetworkAddress: boolean, discoveredRoute: boolean, assocRemove: boolean,
assocRestore: {ieeeadr: string, nwkaddr: number, noderelation: number}
): Promise<Events.ZclDataPayload> {
debug('sendZclFrameToEndpointInternal %s:%i/%i (%i,%i,%i)',
ieeeAddr, networkAddress, endpoint, responseAttempt, dataRequestAttempt, this.queue.count());
let response = null;
const command = zclFrame.getCommand();
if (command.hasOwnProperty('response') && disableResponse === false) {
response = this.waitForInternal(
networkAddress, endpoint, zclFrame.Header.frameControl.frameType, Direction.SERVER_TO_CLIENT,
zclFrame.Header.transactionSequenceNumber, zclFrame.Cluster.ID, command.response, timeout
);
} else if (!zclFrame.Header.frameControl.disableDefaultResponse) {
response = this.waitForInternal(
networkAddress, endpoint, FrameType.GLOBAL, Direction.SERVER_TO_CLIENT,
zclFrame.Header.transactionSequenceNumber, zclFrame.Cluster.ID, Foundation.defaultRsp.ID,
timeout,
);
}
const dataConfirmResult = await this.dataRequest(
networkAddress, endpoint, sourceEndpoint, zclFrame.Cluster.ID, Constants.AF.DEFAULT_RADIUS,
zclFrame.toBuffer(), timeout
);
if (dataConfirmResult !== ZnpCommandStatus.SUCCESS) {
// In case dataConfirm timesout (= null) or gives an error, try to recover
debug('Data confirm error (%s:%d,%d,%d)', ieeeAddr, networkAddress, dataConfirmResult, dataRequestAttempt);
if (response !== null) response.cancel();
/**
* In case we did an assocRemove in the previous attempt and it still fails after this, assume that the
* coordinator is still the parent of the device (but for some reason the device is not available now).
* Re-add the device to the assoc table, otherwise we will never be able to reach it anymore.
*/
if (assocRemove && assocRestore && this.supportsAssocAdd()) {
debug('assocAdd(%s)', assocRestore.ieeeadr);
await this.znp.request(Subsystem.UTIL, 'assocAdd', assocRestore);
assocRestore = null;
}
const recoverableErrors = [
ZnpCommandStatus.NWK_NO_ROUTE, ZnpCommandStatus.MAC_NO_ACK, ZnpCommandStatus.MAC_CHANNEL_ACCESS_FAILURE,
ZnpCommandStatus.MAC_TRANSACTION_EXPIRED, ZnpCommandStatus.BUFFER_FULL,
ZnpCommandStatus.MAC_NO_RESOURCES,
];
if (dataRequestAttempt >= 4 || !recoverableErrors.includes(dataConfirmResult) || disableRecovery) {
throw new DataConfirmError(dataConfirmResult);
}
if (dataConfirmResult === ZnpCommandStatus.MAC_CHANNEL_ACCESS_FAILURE ||
dataConfirmResult === ZnpCommandStatus.BUFFER_FULL ||
dataConfirmResult === ZnpCommandStatus.MAC_NO_RESOURCES) {
/**
* MAC_CHANNEL_ACCESS_FAILURE: When many commands at once are executed we can end up in a MAC
* channel access failure error. This is because there is too much traffic on the network.
* Retry this command once after a cooling down period.
* BUFFER_FULL: When many commands are executed at once the buffer can get full, wait
* some time and retry.
* MAC_NO_RESOURCES: Operation could not be completed because no memory resources are available,
* wait some time and retry.
*/
await Wait(2000);
return this.sendZclFrameToEndpointInternal(
ieeeAddr, networkAddress, endpoint, sourceEndpoint, zclFrame, timeout, disableResponse,
disableRecovery, responseAttempt, dataRequestAttempt + 1, checkedNetworkAddress, discoveredRoute,
assocRemove, assocRestore,
);
} else {
let doAssocRemove = false;
if (!assocRemove && dataConfirmResult === ZnpCommandStatus.MAC_TRANSACTION_EXPIRED &&
dataRequestAttempt >= 1 && this.supportsAssocRemove()) {
const match = await this.znp.request(
Subsystem.UTIL, 'assocGetWithAddress',{extaddr: ieeeAddr, nwkaddr: networkAddress}
);
if (match.payload.nwkaddr !== 0xFFFE && match.payload.noderelation !== 255) {
doAssocRemove = true;
assocRestore =
{ieeeadr: ieeeAddr, nwkaddr: networkAddress, noderelation: match.payload.noderelation};
}
assocRemove = true;
}
// NWK_NO_ROUTE: no network route => rediscover route
// MAC_NO_ACK: route may be corrupted
// MAC_TRANSACTION_EXPIRED: Mac layer is sleeping
if (doAssocRemove) {
/**
* Since child aging is disabled on the firmware, when a end device is directly connected
* to the coordinator and changes parent and the coordinator does not recevie this update,
* it still thinks it's directly connected.
* A discoverRoute() is not send out in this case, therefore remove it from the associated device
* list and try again.
* Note: assocRemove is a custom command, not available by default, only available on recent
* z-stack-firmware firmware version. In case it's not supported by the coordinator we will
* automatically timeout after 60000ms.
*/
debug('assocRemove(%s)', ieeeAddr);
await this.znp.request(Subsystem.UTIL, 'assocRemove', {ieeeadr: ieeeAddr});
} else if (!discoveredRoute && dataRequestAttempt >= 1) {
discoveredRoute = true;
await this.discoverRoute(networkAddress);
} else if (!checkedNetworkAddress && dataRequestAttempt >= 1) {
// Figure out once if the network address has been changed.
try {
checkedNetworkAddress = true;
const actualNetworkAddress = await this.requestNetworkAddress(ieeeAddr);
if (networkAddress !== actualNetworkAddress) {
debug(`Failed because request was done with wrong network address`);
discoveredRoute = true;
networkAddress = actualNetworkAddress;
await this.discoverRoute(actualNetworkAddress);
} else {debug('Network address did not change');}
} catch {}
} else {
debug('Wait 2000ms');
await Wait(2000);
}
return this.sendZclFrameToEndpointInternal(
ieeeAddr, networkAddress, endpoint, sourceEndpoint, zclFrame, timeout,
disableResponse, disableRecovery, responseAttempt, dataRequestAttempt + 1, checkedNetworkAddress,
discoveredRoute, assocRemove, assocRestore,
);
}
}
if (response !== null) {
try {
const result = await response.start().promise;
return result;
} catch (error) {
debug('Response timeout (%s:%d,%d)', ieeeAddr, networkAddress, responseAttempt);
if (responseAttempt < 1 && !disableRecovery) {
// No response could be because the radio of the end device is turned off:
// Sometimes the coordinator does not properly set the PENDING flag.
// Try to rewrite the device entry in the association table, this fixes it sometimes.
const match = await this.znp.request(
Subsystem.UTIL, 'assocGetWithAddress',{extaddr: ieeeAddr, nwkaddr: networkAddress}
);
debug(`Response timeout recovery: Node relation ${
match.payload.noderelation} (${ieeeAddr} / ${match.payload.nwkaddr})`);
if (this.supportsAssocAdd() && this.supportsAssocRemove() &&
match.payload.nwkaddr !== 0xFFFE && match.payload.noderelation == 1
) {
debug(`Response timeout recovery: Rewrite association table entry (${ieeeAddr})`);
await this.znp.request(Subsystem.UTIL, 'assocRemove', {ieeeadr: ieeeAddr});
await this.znp.request(Subsystem.UTIL, 'assocAdd',
{ieeeadr: ieeeAddr, nwkaddr: networkAddress, noderelation: match.payload.noderelation}
);
}
// No response could be of invalid route, e.g. when message is send to wrong parent of end device.
await this.discoverRoute(networkAddress);
return this.sendZclFrameToEndpointInternal(
ieeeAddr, networkAddress, endpoint, sourceEndpoint, zclFrame, timeout, disableResponse,
disableRecovery, responseAttempt + 1, dataRequestAttempt, checkedNetworkAddress,
discoveredRoute, assocRemove, assocRestore,
);
} else {
throw error;
}
}
} else {
return null;
}
}
public async sendZclFrameToGroup(groupID: number, zclFrame: ZclFrame, sourceEndpoint?: number): Promise<void> {
return this.queue.execute<void>(async () => {
this.checkInterpanLock();
await this.dataRequestExtended(
AddressMode.ADDR_GROUP, groupID, 0xFF, 0, sourceEndpoint || 1, zclFrame.Cluster.ID,
Constants.AF.DEFAULT_RADIUS, zclFrame.toBuffer(), 3000, true
);
/**
* As a group command is not confirmed and thus immidiately returns
* (contrary to network address requests) we will give the
* command some time to 'settle' in the network.
*/
await Wait(200);
});
}
public async sendZclFrameToAll(endpoint: number, zclFrame: ZclFrame, sourceEndpoint: number): Promise<void> {
return this.queue.execute<void>(async () => {
this.checkInterpanLock();
await this.dataRequestExtended(
AddressMode.ADDR_16BIT, 0xFFFD, endpoint, 0, sourceEndpoint,
zclFrame.Cluster.ID, Constants.AF.DEFAULT_RADIUS, zclFrame.toBuffer(), 3000, false, 0
);
/**
* As a broadcast command is not confirmed and thus immidiately returns
* (contrary to network address requests) we will give the
* command some time to 'settle' in the network.
*/
await Wait(200);
});
}
public async lqi(networkAddress: number): Promise<LQI> {
return this.queue.execute<LQI>(async (): Promise<LQI> => {
this.checkInterpanLock();
const neighbors: LQINeighbor[] = [];
// eslint-disable-next-line
const request = async (startIndex: number): Promise<any> => {
const response = this.znp.waitFor(Type.AREQ, Subsystem.ZDO, 'mgmtLqiRsp', {srcaddr: networkAddress});
await this.znp.request(
Subsystem.ZDO, 'mgmtLqiReq', {dstaddr: networkAddress, startindex: startIndex}, response.ID
);
const result = await response.start().promise;
if (result.payload.status !== ZnpCommandStatus.SUCCESS) {
throw new Error(`LQI for '${networkAddress}' failed with error: '${
ZnpCommandStatus[result.payload.status]}' (${result.payload.status})`);
}
return result;
};
// eslint-disable-next-line
const add = (list: any) => {
for (const entry of list) {
neighbors.push({
linkquality: entry.lqi,
networkAddress: entry.nwkAddr,
ieeeAddr: entry.extAddr,
relationship: entry.relationship,
depth: entry.depth,
});
}
};
let response = await request(0);
add(response.payload.neighborlqilist);
const size = response.payload.neighbortableentries;
let nextStartIndex = response.payload.neighborlqilist.length;
while (neighbors.length < size) {
response = await request(nextStartIndex);
add(response.payload.neighborlqilist);
nextStartIndex += response.payload.neighborlqilist.length;
}
return {neighbors};
}, networkAddress);
}
public async routingTable(networkAddress: number): Promise<RoutingTable> {
return this.queue.execute<RoutingTable>(async (): Promise<RoutingTable> => {
this.checkInterpanLock();
const table: RoutingTableEntry[] = [];
// eslint-disable-next-line
const request = async (startIndex: number): Promise<any> => {
const response = this.znp.waitFor(Type.AREQ, Subsystem.ZDO, 'mgmtRtgRsp', {srcaddr: networkAddress});
await this.znp.request(
Subsystem.ZDO, 'mgmtRtgReq', {dstaddr: networkAddress, startindex: startIndex}, response.ID
);
const result = await response.start().promise;
if (result.payload.status !== ZnpCommandStatus.SUCCESS) {
throw new Error(`Routing table for '${networkAddress}' failed with error: '${
ZnpCommandStatus[result.payload.status]}' (${result.payload.status})`);
}
return result;
};
// eslint-disable-next-line
const add = (list: any) => {
for (const entry of list) {
table.push({
destinationAddress: entry.destNwkAddr,
status: entry.routeStatus,
nextHop: entry.nextHopNwkAddr,
});
}
};
let response = await request(0);
add(response.payload.routingtablelist);
const size = response.payload.routingtableentries;
let nextStartIndex = response.payload.routingtablelist.length;
while (table.length < size) {
response = await request(nextStartIndex);
add(response.payload.routingtablelist);
nextStartIndex += response.payload.routingtablelist.length;
}
return {table};
}, networkAddress);
}
public async addInstallCode(ieeeAddress: string, key: Buffer): Promise<void> {
assert(this.version.product !== ZnpVersion.zStack12, 'Install code is not supported for ZStack 1.2 adapter');
const payload = {installCodeFormat: key.length === 18 ? 1 : 2, ieeeaddr: ieeeAddress, installCode: key};
await this.znp.request(Subsystem.APP_CNF, 'bdbAddInstallCode', payload);
}
public async bind(
destinationNetworkAddress: number, sourceIeeeAddress: string, sourceEndpoint: number,
clusterID: number, destinationAddressOrGroup: string | number, type: 'endpoint' | 'group',
destinationEndpoint?: number
): Promise<void> {
return this.queue.execute<void>(async () => {
this.checkInterpanLock();
const responsePayload = {srcaddr: destinationNetworkAddress};
const response = this.znp.waitFor(Type.AREQ, Subsystem.ZDO, 'bindRsp', responsePayload);
const payload = {
dstaddr: destinationNetworkAddress,
srcaddr: sourceIeeeAddress,
srcendpoint: sourceEndpoint,
clusterid: clusterID,
dstaddrmode: type === 'group' ?
AddressMode.ADDR_GROUP : AddressMode.ADDR_64BIT,
dstaddress: this.toAddressString(destinationAddressOrGroup),
dstendpoint: type === 'group' ? 0xFF : destinationEndpoint,
};
await this.znp.request(Subsystem.ZDO, 'bindReq', payload, response.ID);
await response.start().promise;
}, destinationNetworkAddress);
}
public async unbind(
destinationNetworkAddress: number, sourceIeeeAddress: string, sourceEndpoint: number,
clusterID: number, destinationAddressOrGroup: string | number, type: 'endpoint' | 'group',
destinationEndpoint: number
): Promise<void> {
return this.queue.execute<void>(async () => {
this.checkInterpanLock();
const response = this.znp.waitFor(
Type.AREQ, Subsystem.ZDO, 'unbindRsp', {srcaddr: destinationNetworkAddress}
);
const payload = {
dstaddr: destinationNetworkAddress,
srcaddr: sourceIeeeAddress,
srcendpoint: sourceEndpoint,
clusterid: clusterID,
dstaddrmode: type === 'group' ?
AddressMode.ADDR_GROUP : AddressMode.ADDR_64BIT,
dstaddress: this.toAddressString(destinationAddressOrGroup),
dstendpoint: type === 'group' ? 0xFF : destinationEndpoint,
};
await this.znp.request(Subsystem.ZDO, 'unbindReq', payload, response.ID);
await response.start().promise;
}, destinationNetworkAddress);
}
public removeDevice(networkAddress: number, ieeeAddr: string): Promise<void> {
return this.queue.execute<void>(async () => {
this.checkInterpanLock();
const response = this.znp.waitFor(
UnpiConstants.Type.AREQ, Subsystem.ZDO, 'mgmtLeaveRsp', {srcaddr: networkAddress}
);
const payload = {
dstaddr: networkAddress,
deviceaddress: ieeeAddr,
removechildrenRejoin: 0,
};
await this.znp.request(Subsystem.ZDO, 'mgmtLeaveReq', payload, response.ID);
await response.start().promise;
}, networkAddress);
}
/**
* Event handlers
*/
public onZnpClose(): void {
if (!this.closing) {
this.emit(Events.Events.disconnected);
}
}
public onZnpRecieved(object: ZpiObject): void {
if (object.type !== UnpiConstants.Type.AREQ) {
return;
}
if (object.subsystem === Subsystem.ZDO) {
if (object.command === 'tcDeviceInd') {
const payload: Events.DeviceJoinedPayload = {
networkAddress: object.payload.nwkaddr,
ieeeAddr: object.payload.extaddr,
};
this.emit(Events.Events.deviceJoined, payload);
} else if (object.command === 'endDeviceAnnceInd') {
const payload: Events.DeviceAnnouncePayload = {
networkAddress: object.payload.nwkaddr,
ieeeAddr: object.payload.ieeeaddr,
};
// Only discover routes to end devices, if bit 1 of capabilities === 0 it's an end device.
const isEndDevice = (object.payload.capabilities & 1<<1) === 0;
if (isEndDevice) {
if (!this.deviceAnnounceRouteDiscoveryDebouncers.has(payload.networkAddress)) {
// If a device announces multiple times in a very short time, it makes no sense
// to rediscover the route every time.
const debouncer = debounce(() => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.queue.execute<void>(async () => {
/* istanbul ignore next */
this.discoverRoute(payload.networkAddress, false).catch(() => {});
}, payload.networkAddress);
}, 60 * 1000, {immediate: true});
this.deviceAnnounceRouteDiscoveryDebouncers.set(payload.networkAddress, debouncer);
}
this.deviceAnnounceRouteDiscoveryDebouncers.get(payload.networkAddress)();
}
this.emit(Events.Events.deviceAnnounce, payload);
} else if (object.command === 'nwkAddrRsp') {
const payload: Events.NetworkAddressPayload = {
networkAddress: object.payload.nwkaddr,
ieeeAddr: object.payload.ieeeaddr,
};
this.emit(Events.Events.networkAddress, payload);
} else {
/* istanbul ignore else */
if (object.command === 'leaveInd') {
if (object.payload.rejoin) {
debug(`Device leave: Got leave indication with rejoin=true, nothing to do`);
} else {
const payload: Events.DeviceLeavePayload = {
networkAddress: object.payload.srcaddr,
ieeeAddr: object.payload.extaddr,
};
this.emit(Events.Events.deviceLeave, payload);
}
}
}
} else {
/* istanbul ignore else */
if (object.subsystem === Subsystem.AF) {
/* istanbul ignore else */
if (object.command === 'incomingMsg' || object.command === 'incomingMsgExt') {
try {
const payload: Events.ZclDataPayload = {
frame: ZclFrame.fromBuffer(object.payload.clusterid, object.payload.data),
address: object.payload.srcaddr,
endpoint: object.payload.srcendpoint,
linkquality: object.payload.linkquality,
groupID: object.payload.groupid,
wasBroadcast: object.payload.wasbroadcast === 1,
destinationEndpoint: object.payload.dstendpoint,
};
this.waitress.resolve(payload);
this.emit(Events.Events.zclData, payload);
} catch (error) {
const payload: Events.RawDataPayload = {
clusterID: object.payload.clusterid,
data: object.payload.data,
address: object.payload.srcaddr,
endpoint: object.payload.srcendpoint,
linkquality: object.payload.linkquality,
groupID: object.payload.groupid,
wasBroadcast: object.payload.wasbroadcast === 1,
destinationEndpoint: object.payload.dstendpoint,
};
this.emit(Events.Events.rawData, payload);
}
}
}
}
}
public async getNetworkParameters(): Promise<NetworkParameters> {
const result = await this.znp.request(Subsystem.ZDO, 'extNwkInfo', {});
return {
panID: result.payload.panid, extendedPanID: result.payload.extendedpanid,
channel: result.payload.channel
};
}
public async supportsBackup(): Promise<boolean> {
return true;
}
public async backup(ieeeAddressesInDatabase: string[]): Promise<Models.Backup> {
return this.adapterManager.backup.createBackup(ieeeAddressesInDatabase);
}
public async setChannelInterPAN(channel: number): Promise<void> {
return this.queue.execute<void>(async () => {
this.interpanLock = true;
await this.znp.request(Subsystem.AF, 'interPanCtl', {cmd: 1, data: [channel]});
if (!this.interpanEndpointRegistered) {
// Make sure that endpoint 12 is registered to proxy the InterPAN messages.
await this.znp.request(Subsystem.AF, 'interPanCtl', {cmd: 2, data: [12]});
this.interpanEndpointRegistered = true;
}
});
}
public async sendZclFrameInterPANToIeeeAddr(zclFrame: ZclFrame, ieeeAddr: string): Promise<void> {
return this.queue.execute<void>(async () => {
await this.dataRequestExtended(
AddressMode.ADDR_64BIT, ieeeAddr, 0xFE, 0xFFFF,
12, zclFrame.Cluster.ID, 30, zclFrame.toBuffer(), 10000, false,
);
});
}
public async sendZclFrameInterPANBroadcast(zclFrame: ZclFrame, timeout: number): Promise<Events.ZclDataPayload> {
return this.queue.execute<Events.ZclDataPayload>(async () => {
const command = zclFrame.getCommand();
if (!command.hasOwnProperty('response')) {
throw new Error(`Command '${command.name}' has no response, cannot wait for response`);
}
const response = this.waitForInternal(
null, 0xFE, zclFrame.Header.frameControl.frameType, Direction.SERVER_TO_CLIENT, null,
zclFrame.Cluster.ID, command.response, timeout
);
try {
await this.dataRequestExtended(
AddressMode.ADDR_16BIT, 0xFFFF, 0xFE, 0xFFFF,
12, zclFrame.Cluster.ID, 30, zclFrame.toBuffer(), 10000, false,
);
} catch (error) {
response.cancel();
throw error;
}
return response.start().promise;
});
}
public async restoreChannelInterPAN(): Promise<void> {
return this.queue.execute<void>(async () => {
await this.znp.request(Subsystem.AF, 'interPanCtl', {cmd: 0, data: []});
// Give adapter some time to restore, otherwise stuff crashes
await Wait(3000);
this.interpanLock = false;
});
}
public async setTransmitPower(value: number): Promise<void> {
return this.queue.execute<void>(async () => {
await this.znp.request(Subsystem.SYS, 'stackTune', {operation: 0, value});
});
}
private waitForInternal(
networkAddress: number, endpoint: number, frameType: FrameType, direction: Direction,
transactionSequenceNumber: number, clusterID: number, commandIdentifier: number, timeout: number,
): {start: () => {promise: Promise<Events.ZclDataPayload>}; cancel: () => void} {
const payload = {
address: networkAddress, endpoint, clusterID, commandIdentifier, frameType, direction,
transactionSequenceNumber,
};
const waiter = this.waitress.waitFor(payload, timeout);
const cancel = (): void => this.waitress.remove(waiter.ID);
return {start: waiter.start, cancel};
}
public waitFor(
networkAddress: number, endpoint: number, frameType: FrameType, direction: Direction,
transactionSequenceNumber: number, clusterID: number, commandIdentifier: number, timeout: number,
): {promise: Promise<Events.ZclDataPayload>; cancel: () => void} {
const waiter = this.waitForInternal(
networkAddress, endpoint, frameType, direction, transactionSequenceNumber, clusterID,
commandIdentifier, timeout,
);
return {cancel: waiter.cancel, promise: waiter.start().promise};
}
/**
* Private methods
*/
private async dataRequest(
destinationAddress: number, destinationEndpoint: number, sourceEndpoint: number, clusterID: number,
radius: number, data: Buffer, timeout: number,
): Promise<number> {
const transactionID = this.nextTransactionID();
const response = this.znp.waitFor(
Type.AREQ, Subsystem.AF, 'dataConfirm', {transid: transactionID}, timeout
);
await this.znp.request(Subsystem.AF, 'dataRequest', {
dstaddr: destinationAddress,
destendpoint: destinationEndpoint,
srcendpoint: sourceEndpoint,
clusterid: clusterID,
transid: transactionID,
options: 0,
radius: radius,
len: data.length,
data: data,
}, response.ID);
let result = null;
try {
const dataConfirm = await response.start().promise;
result = dataConfirm.payload.status;
} catch {
result = DataConfirmTimeout;
}
return result;
}
private async dataRequestExtended(
addressMode: number, destinationAddressOrGroupID: number | string, destinationEndpoint: number, panID: number,
sourceEndpoint: number, clusterID: number, radius: number, data: Buffer, timeout: number, confirmation: boolean,
attemptsLeft = 5,
): Promise<ZpiObject> {
const transactionID = this.nextTransactionID();
const response = confirmation ?
this.znp.waitFor(Type.AREQ, Subsystem.AF, 'dataConfirm', {transid: transactionID}, timeout) : null;
await this.znp.request(Subsystem.AF, 'dataRequestExt', {
dstaddrmode: addressMode,
dstaddr: this.toAddressString(destinationAddressOrGroupID),
destendpoint: destinationEndpoint,