forked from arendst/Tasmota
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xdrv_23_zigbee_2_devices.ino
1016 lines (895 loc) · 36.1 KB
/
xdrv_23_zigbee_2_devices.ino
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
/*
xdrv_23_zigbee.ino - zigbee support for Tasmota
Copyright (C) 2020 Theo Arends and Stephan Hadinger
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef USE_ZIGBEE
#include <vector>
#ifndef ZIGBEE_SAVE_DELAY_SECONDS
#define ZIGBEE_SAVE_DELAY_SECONDS 2; // wait for 2s before saving Zigbee info
#endif
const uint16_t kZigbeeSaveDelaySeconds = ZIGBEE_SAVE_DELAY_SECONDS; // wait for x seconds
typedef int32_t (*Z_DeviceTimer)(uint16_t shortaddr, uint16_t groupaddr, uint16_t cluster, uint8_t endpoint, uint32_t value);
const size_t endpoints_max = 8; // we limit to 8 endpoints
typedef struct Z_Device {
uint64_t longaddr; // 0x00 means unspecified
char * manufacturerId;
char * modelId;
char * friendlyName;
uint8_t endpoints[endpoints_max]; // static array to limit memory consumption, list of endpoints until 0x00 or end of array
// json buffer used for attribute reporting
DynamicJsonBuffer *json_buffer;
JsonObject *json;
// sequence number for Zigbee frames
uint16_t shortaddr; // unique key if not null, or unspecified if null
uint8_t seqNumber;
// Light information for Hue integration integration, last known values
int8_t bulbtype; // number of channel for the bulb: 0-5, or 0xFF if no Hue integration
uint8_t power; // power state (boolean)
uint8_t colormode; // 0x00: Hue/Sat, 0x01: XY, 0x02: CT
uint8_t dimmer; // last Dimmer value: 0-254
uint8_t sat; // last Sat: 0..254
uint16_t ct; // last CT: 153-500
uint16_t hue; // last Hue: 0..359
uint16_t x, y; // last color [x,y]
} Z_Device;
// Category for Deferred actions, this allows to selectively remove active deferred or update them
typedef enum Z_Def_Category {
Z_CAT_NONE = 0, // no category, it will happen anyways
Z_CAT_READ_ATTR, // Attribute reporting, either READ_ATTRIBUTE or REPORT_ATTRIBUTE, we coalesce all attributes reported if we can
Z_CAT_VIRTUAL_ATTR, // Creation of a virtual attribute, typically after a time-out. Ex: Aqara presence sensor
Z_CAT_READ_0006, // Read 0x0006 cluster
Z_CAT_READ_0008, // Read 0x0008 cluster
Z_CAT_READ_0102, // Read 0x0300 cluster
Z_CAT_READ_0300, // Read 0x0300 cluster
} Z_Def_Category;
typedef struct Z_Deferred {
// below are per device timers, used for example to query the new state of the device
uint32_t timer; // millis() when to fire the timer, 0 if no timer
uint16_t shortaddr; // identifier of the device
uint16_t groupaddr; // group address (if needed)
uint16_t cluster; // cluster to use for the timer
uint8_t endpoint; // endpoint to use for timer
uint8_t category; // which category of deferred is it
uint32_t value; // any raw value to use for the timer
Z_DeviceTimer func; // function to call when timer occurs
} Z_Deferred;
// All devices are stored in a Vector
// Invariants:
// - shortaddr is unique if not null
// - longaddr is unique if not null
// - shortaddr and longaddr cannot be both null
class Z_Devices {
public:
Z_Devices() {};
// Probe the existence of device keys
// Results:
// - 0x0000 = not found
// - 0xFFFF = bad parameter
// - 0x<shortaddr> = the device's short address
uint16_t isKnownShortAddr(uint16_t shortaddr) const;
uint16_t isKnownLongAddr(uint64_t longaddr) const;
uint16_t isKnownIndex(uint32_t index) const;
uint16_t isKnownFriendlyName(const char * name) const;
uint64_t getDeviceLongAddr(uint16_t shortaddr) const;
uint8_t findFirstEndpoint(uint16_t shortaddr) const;
// Add new device, provide ShortAddr and optional longAddr
// If it is already registered, update information, otherwise create the entry
void updateDevice(uint16_t shortaddr, uint64_t longaddr = 0);
// Add an endpoint to a device
void addEndpoint(uint16_t shortaddr, uint8_t endpoint);
// Add cluster
void addCluster(uint16_t shortaddr, uint8_t endpoint, uint16_t cluster);
void setManufId(uint16_t shortaddr, const char * str);
void setModelId(uint16_t shortaddr, const char * str);
void setFriendlyName(uint16_t shortaddr, const char * str);
const char * getFriendlyName(uint16_t shortaddr) const;
const char * getModelId(uint16_t shortaddr) const;
// get next sequence number for (increment at each all)
uint8_t getNextSeqNumber(uint16_t shortaddr);
// Dump json
String dumpLightState(uint16_t shortaddr) const;
String dump(uint32_t dump_mode, uint16_t status_shortaddr = 0) const;
// Hue support
void setHueBulbtype(uint16_t shortaddr, int8_t bulbtype);
int8_t getHueBulbtype(uint16_t shortaddr) const ;
void updateHueState(uint16_t shortaddr,
const uint8_t *power, const uint8_t *colormode,
const uint8_t *dimmer, const uint8_t *sat,
const uint16_t *ct, const uint16_t *hue,
const uint16_t *x, const uint16_t *y);
bool getHueState(uint16_t shortaddr,
uint8_t *power, uint8_t *colormode,
uint8_t *dimmer, uint8_t *sat,
uint16_t *ct, uint16_t *hue,
uint16_t *x, uint16_t *y) const ;
// Timers
void resetTimersForDevice(uint16_t shortaddr, uint16_t groupaddr, uint8_t category);
void setTimer(uint16_t shortaddr, uint16_t groupaddr, uint32_t wait_ms, uint16_t cluster, uint8_t endpoint, uint8_t category, uint32_t value, Z_DeviceTimer func);
void runTimer(void);
// Append or clear attributes Json structure
void jsonClear(uint16_t shortaddr);
void jsonAppend(uint16_t shortaddr, const JsonObject &values);
const JsonObject *jsonGet(uint16_t shortaddr);
void jsonPublishFlush(uint16_t shortaddr); // publish the json message and clear buffer
bool jsonIsConflict(uint16_t shortaddr, const JsonObject &values);
void jsonPublishNow(uint16_t shortaddr, JsonObject &values);
// Iterator
size_t devicesSize(void) const {
return _devices.size();
}
const Z_Device &devicesAt(size_t i) const {
return *(_devices.at(i));
}
// Remove device from list
bool removeDevice(uint16_t shortaddr);
// Mark data as 'dirty' and requiring to save in Flash
void dirty(void);
void clean(void); // avoid writing to flash the last changes
void shrinkToFit(uint16_t shortaddr);
// Find device by name, can be short_addr, long_addr, number_in_array or name
uint16_t parseDeviceParam(const char * param, bool short_must_be_known = false) const;
private:
std::vector<Z_Device*> _devices = {};
std::vector<Z_Deferred> _deferred = {}; // list of deferred calls
uint32_t _saveTimer = 0;
uint8_t _seqNumber = 0; // global seqNumber if device is unknown
template < typename T>
static bool findInVector(const std::vector<T> & vecOfElements, const T & element);
template < typename T>
static int32_t findEndpointInVector(const std::vector<T> & vecOfElements, uint8_t element);
Z_Device & getShortAddr(uint16_t shortaddr); // find Device from shortAddr, creates it if does not exist
const Z_Device & getShortAddrConst(uint16_t shortaddr) const ; // find Device from shortAddr, creates it if does not exist
Z_Device & getLongAddr(uint64_t longaddr); // find Device from shortAddr, creates it if does not exist
int32_t findShortAddr(uint16_t shortaddr) const;
int32_t findLongAddr(uint64_t longaddr) const;
int32_t findFriendlyName(const char * name) const;
// Create a new entry in the devices list - must be called if it is sure it does not already exist
Z_Device & createDeviceEntry(uint16_t shortaddr, uint64_t longaddr = 0);
void freeDeviceEntry(Z_Device *device);
};
Z_Devices zigbee_devices = Z_Devices();
// Local coordinator information
uint64_t localIEEEAddr = 0;
// https://thispointer.com/c-how-to-find-an-element-in-vector-and-get-its-index/
template < typename T>
bool Z_Devices::findInVector(const std::vector<T> & vecOfElements, const T & element) {
// Find given element in vector
auto it = std::find(vecOfElements.begin(), vecOfElements.end(), element);
if (it != vecOfElements.end()) {
return true;
} else {
return false;
}
}
template < typename T>
int32_t Z_Devices::findEndpointInVector(const std::vector<T> & vecOfElements, uint8_t element) {
// Find given element in vector
int32_t found = 0;
for (auto &elem : vecOfElements) {
if (elem == element) { return found; }
found++;
}
return -1;
}
//
// Create a new Z_Device entry in _devices. Only to be called if you are sure that no
// entry with same shortaddr or longaddr exists.
//
Z_Device & Z_Devices::createDeviceEntry(uint16_t shortaddr, uint64_t longaddr) {
if (!shortaddr && !longaddr) { return *(Z_Device*) nullptr; } // it is not legal to create an enrty with both short/long addr null
//Z_Device* device_alloc = (Z_Device*) malloc(sizeof(Z_Device));
Z_Device* device_alloc = new Z_Device{
longaddr,
nullptr, // ManufId
nullptr, // DeviceId
nullptr, // FriendlyName
{ 0, 0, 0, 0, 0, 0, 0, 0 }, // endpoints
nullptr, nullptr,
shortaddr,
0, // seqNumber
// Hue support
-1, // no Hue support
0, // power
0, // colormode
0, // dimmer
0, // sat
200, // ct
0, // hue
0, 0, // x, y
};
device_alloc->json_buffer = new DynamicJsonBuffer(16);
_devices.push_back(device_alloc);
dirty();
return *(_devices.back());
}
void Z_Devices::freeDeviceEntry(Z_Device *device) {
if (device->manufacturerId) { free(device->manufacturerId); }
if (device->modelId) { free(device->modelId); }
if (device->friendlyName) { free(device->friendlyName); }
free(device);
}
//
// Scan all devices to find a corresponding shortaddr
// Looks info device.shortaddr entry
// In:
// shortaddr (non null)
// Out:
// index in _devices of entry, -1 if not found
//
int32_t Z_Devices::findShortAddr(uint16_t shortaddr) const {
if (!shortaddr) { return -1; } // does not make sense to look for 0x0000 shortaddr (localhost)
int32_t found = 0;
if (shortaddr) {
for (auto &elem : _devices) {
if (elem->shortaddr == shortaddr) { return found; }
found++;
}
}
return -1;
}
//
// Scan all devices to find a corresponding longaddr
// Looks info device.longaddr entry
// In:
// longaddr (non null)
// Out:
// index in _devices of entry, -1 if not found
//
int32_t Z_Devices::findLongAddr(uint64_t longaddr) const {
if (!longaddr) { return -1; }
int32_t found = 0;
if (longaddr) {
for (auto &elem : _devices) {
if (elem->longaddr == longaddr) { return found; }
found++;
}
}
return -1;
}
//
// Scan all devices to find a corresponding friendlyNme
// Looks info device.friendlyName entry
// In:
// friendlyName (null terminated, should not be empty)
// Out:
// index in _devices of entry, -1 if not found
//
int32_t Z_Devices::findFriendlyName(const char * name) const {
if (!name) { return -1; } // if pointer is null
size_t name_len = strlen(name);
int32_t found = 0;
if (name_len) {
for (auto &elem : _devices) {
if (elem->friendlyName) {
if (strcmp(elem->friendlyName, name) == 0) { return found; }
}
found++;
}
}
return -1;
}
// Probe if device is already known but don't create any entry
uint16_t Z_Devices::isKnownShortAddr(uint16_t shortaddr) const {
int32_t found = findShortAddr(shortaddr);
if (found >= 0) {
return shortaddr;
} else {
return 0; // unknown
}
}
uint16_t Z_Devices::isKnownLongAddr(uint64_t longaddr) const {
int32_t found = findLongAddr(longaddr);
if (found >= 0) {
const Z_Device & device = devicesAt(found);
return device.shortaddr; // can be zero, if not yet registered
} else {
return 0;
}
}
uint16_t Z_Devices::isKnownIndex(uint32_t index) const {
if (index < devicesSize()) {
const Z_Device & device = devicesAt(index);
return device.shortaddr;
} else {
return 0;
}
}
uint16_t Z_Devices::isKnownFriendlyName(const char * name) const {
if ((!name) || (0 == strlen(name))) { return 0xFFFF; } // Error
int32_t found = findFriendlyName(name);
if (found >= 0) {
const Z_Device & device = devicesAt(found);
return device.shortaddr; // can be zero, if not yet registered
} else {
return 0;
}
}
uint64_t Z_Devices::getDeviceLongAddr(uint16_t shortaddr) const {
const Z_Device & device = getShortAddrConst(shortaddr);
return device.longaddr;
}
//
// We have a seen a shortaddr on the network, get the corresponding
//
Z_Device & Z_Devices::getShortAddr(uint16_t shortaddr) {
if (!shortaddr) { return *(Z_Device*) nullptr; } // this is not legal
int32_t found = findShortAddr(shortaddr);
if (found >= 0) {
return *(_devices[found]);
}
//Serial.printf("Device entry created for shortaddr = 0x%02X, found = %d\n", shortaddr, found);
return createDeviceEntry(shortaddr, 0);
}
// Same version but Const
const Z_Device & Z_Devices::getShortAddrConst(uint16_t shortaddr) const {
if (!shortaddr) { return *(Z_Device*) nullptr; } // this is not legal
int32_t found = findShortAddr(shortaddr);
if (found >= 0) {
return *(_devices[found]);
}
return *((Z_Device*)nullptr);
}
// find the Device object by its longaddr (unique key if not null)
Z_Device & Z_Devices::getLongAddr(uint64_t longaddr) {
if (!longaddr) { return *(Z_Device*) nullptr; }
int32_t found = findLongAddr(longaddr);
if (found > 0) {
return *(_devices[found]);
}
return createDeviceEntry(0, longaddr);
}
// Remove device from list, return true if it was known, false if it was not recorded
bool Z_Devices::removeDevice(uint16_t shortaddr) {
int32_t found = findShortAddr(shortaddr);
if (found >= 0) {
freeDeviceEntry(_devices.at(found));
_devices.erase(_devices.begin() + found);
dirty();
return true;
}
return false;
}
//
// We have just seen a device on the network, update the info based on short/long addr
// In:
// shortaddr
// longaddr (both can't be null at the same time)
void Z_Devices::updateDevice(uint16_t shortaddr, uint64_t longaddr) {
int32_t s_found = findShortAddr(shortaddr); // is there already a shortaddr entry
int32_t l_found = findLongAddr(longaddr); // is there already a longaddr entry
if ((s_found >= 0) && (l_found >= 0)) { // both shortaddr and longaddr are already registered
if (s_found == l_found) {
} else { // they don't match
// the device with longaddr got a new shortaddr
_devices[l_found]->shortaddr = shortaddr; // update the shortaddr corresponding to the longaddr
// erase the previous shortaddr
freeDeviceEntry(_devices.at(s_found));
_devices.erase(_devices.begin() + s_found);
dirty();
}
} else if (s_found >= 0) {
// shortaddr already exists but longaddr not
// add the longaddr to the entry
_devices[s_found]->longaddr = longaddr;
dirty();
} else if (l_found >= 0) {
// longaddr entry exists, update shortaddr
_devices[l_found]->shortaddr = shortaddr;
dirty();
} else {
// neither short/lonf addr are found.
if (shortaddr || longaddr) {
createDeviceEntry(shortaddr, longaddr);
}
}
}
//
// Add an endpoint to a shortaddr
//
void Z_Devices::addEndpoint(uint16_t shortaddr, uint8_t endpoint) {
if (!shortaddr) { return; }
if (0x00 == endpoint) { return; }
Z_Device &device = getShortAddr(shortaddr);
if (&device == nullptr) { return; } // don't crash if not found
for (uint32_t i = 0; i < endpoints_max; i++) {
if (endpoint == device.endpoints[i]) {
return; // endpoint already there
}
if (0 == device.endpoints[i]) {
device.endpoints[i] = endpoint;
dirty();
return;
}
}
}
// Find the first endpoint of the device
uint8_t Z_Devices::findFirstEndpoint(uint16_t shortaddr) const {
int32_t found = findShortAddr(shortaddr);
if (found < 0) return 0; // avoid creating an entry if the device was never seen
const Z_Device &device = devicesAt(found);
return device.endpoints[0]; // returns 0x00 if no endpoint
}
void Z_Devices::setManufId(uint16_t shortaddr, const char * str) {
Z_Device & device = getShortAddr(shortaddr);
if (&device == nullptr) { return; } // don't crash if not found
size_t str_len = str ? strlen(str) : 0; // len, handle both null ptr and zero length string
if ((!device.manufacturerId) && (0 == str_len)) { return; } // if both empty, don't do anything
if (device.manufacturerId) {
// we already have a value
if (strcmp(device.manufacturerId, str) != 0) {
// new value
free(device.manufacturerId); // free previous value
device.manufacturerId = nullptr;
} else {
return; // same value, don't change anything
}
}
if (str_len) {
device.manufacturerId = (char*) malloc(str_len + 1);
strlcpy(device.manufacturerId, str, str_len + 1);
}
dirty();
}
void Z_Devices::setModelId(uint16_t shortaddr, const char * str) {
Z_Device & device = getShortAddr(shortaddr);
if (&device == nullptr) { return; } // don't crash if not found
size_t str_len = str ? strlen(str) : 0; // len, handle both null ptr and zero length string
if ((!device.modelId) && (0 == str_len)) { return; } // if both empty, don't do anything
if (device.modelId) {
// we already have a value
if (strcmp(device.modelId, str) != 0) {
// new value
free(device.modelId); // free previous value
device.modelId = nullptr;
} else {
return; // same value, don't change anything
}
}
if (str_len) {
device.modelId = (char*) malloc(str_len + 1);
strlcpy(device.modelId, str, str_len + 1);
}
dirty();
}
void Z_Devices::setFriendlyName(uint16_t shortaddr, const char * str) {
Z_Device & device = getShortAddr(shortaddr);
if (&device == nullptr) { return; } // don't crash if not found
size_t str_len = str ? strlen(str) : 0; // len, handle both null ptr and zero length string
if ((!device.friendlyName) && (0 == str_len)) { return; } // if both empty, don't do anything
if (device.friendlyName) {
// we already have a value
if (strcmp(device.friendlyName, str) != 0) {
// new value
free(device.friendlyName); // free previous value
device.friendlyName = nullptr;
} else {
return; // same value, don't change anything
}
}
if (str_len) {
device.friendlyName = (char*) malloc(str_len + 1);
strlcpy(device.friendlyName, str, str_len + 1);
}
dirty();
}
const char * Z_Devices::getFriendlyName(uint16_t shortaddr) const {
int32_t found = findShortAddr(shortaddr);
if (found >= 0) {
const Z_Device & device = devicesAt(found);
return device.friendlyName;
}
return nullptr;
}
const char * Z_Devices::getModelId(uint16_t shortaddr) const {
int32_t found = findShortAddr(shortaddr);
if (found >= 0) {
const Z_Device & device = devicesAt(found);
return device.modelId;
}
return nullptr;
}
// get the next sequance number for the device, or use the global seq number if device is unknown
uint8_t Z_Devices::getNextSeqNumber(uint16_t shortaddr) {
int32_t short_found = findShortAddr(shortaddr);
if (short_found >= 0) {
Z_Device &device = getShortAddr(shortaddr);
device.seqNumber += 1;
return device.seqNumber;
} else {
_seqNumber += 1;
return _seqNumber;
}
}
// Hue support
void Z_Devices::setHueBulbtype(uint16_t shortaddr, int8_t bulbtype) {
Z_Device &device = getShortAddr(shortaddr);
if (bulbtype != device.bulbtype) {
device.bulbtype = bulbtype;
dirty();
}
}
int8_t Z_Devices::getHueBulbtype(uint16_t shortaddr) const {
int32_t found = findShortAddr(shortaddr);
if (found >= 0) {
return _devices[found]->bulbtype;
} else {
return -1; // Hue not activated
}
}
// Hue support
void Z_Devices::updateHueState(uint16_t shortaddr,
const uint8_t *power, const uint8_t *colormode,
const uint8_t *dimmer, const uint8_t *sat,
const uint16_t *ct, const uint16_t *hue,
const uint16_t *x, const uint16_t *y) {
Z_Device &device = getShortAddr(shortaddr);
if (power) { device.power = *power; }
if (colormode){ device.colormode = *colormode; }
if (dimmer) { device.dimmer = *dimmer; }
if (sat) { device.sat = *sat; }
if (ct) { device.ct = *ct; }
if (hue) { device.hue = *hue; }
if (x) { device.x = *x; }
if (y) { device.y = *y; }
}
// return true if ok
bool Z_Devices::getHueState(uint16_t shortaddr,
uint8_t *power, uint8_t *colormode,
uint8_t *dimmer, uint8_t *sat,
uint16_t *ct, uint16_t *hue,
uint16_t *x, uint16_t *y) const {
int32_t found = findShortAddr(shortaddr);
if (found >= 0) {
const Z_Device &device = *(_devices[found]);
if (power) { *power = device.power; }
if (colormode){ *colormode = device.colormode; }
if (dimmer) { *dimmer = device.dimmer; }
if (sat) { *sat = device.sat; }
if (ct) { *ct = device.ct; }
if (hue) { *hue = device.hue; }
if (x) { *x = device.x; }
if (y) { *y = device.y; }
return true;
} else {
return false;
}
}
// Deferred actions
// Parse for a specific category, of all deferred for a device if category == 0xFF
void Z_Devices::resetTimersForDevice(uint16_t shortaddr, uint16_t groupaddr, uint8_t category) {
// iterate the list of deferred, and remove any linked to the shortaddr
for (auto it = _deferred.begin(); it != _deferred.end(); it++) {
// Notice that the iterator is decremented after it is passed
// to erase() but before erase() is executed
// see https://www.techiedelight.com/remove-elements-vector-inside-loop-cpp/
if ((it->shortaddr == shortaddr) && (it->groupaddr == groupaddr)) {
if ((0xFF == category) || (it->category == category)) {
_deferred.erase(it--);
}
}
}
}
// Set timer for a specific device
void Z_Devices::setTimer(uint16_t shortaddr, uint16_t groupaddr, uint32_t wait_ms, uint16_t cluster, uint8_t endpoint, uint8_t category, uint32_t value, Z_DeviceTimer func) {
// First we remove any existing timer for same device in same category, except for category=0x00 (they need to happen anyway)
if (category) { // if category == 0, we leave all previous
resetTimersForDevice(shortaddr, groupaddr, category); // remove any cluster
}
// Now create the new timer
Z_Deferred deferred = { wait_ms + millis(), // timer
shortaddr,
groupaddr,
cluster,
endpoint,
category,
value,
func };
_deferred.push_back(deferred);
}
// Run timer at each tick
void Z_Devices::runTimer(void) {
// visit all timers
for (auto it = _deferred.begin(); it != _deferred.end(); it++) {
Z_Deferred &defer = *it;
uint32_t timer = defer.timer;
if (TimeReached(timer)) {
(*defer.func)(defer.shortaddr, defer.groupaddr, defer.cluster, defer.endpoint, defer.value);
_deferred.erase(it--); // remove from list
}
}
// check if we need to save to Flash
if ((_saveTimer) && TimeReached(_saveTimer)) {
saveZigbeeDevices();
_saveTimer = 0;
}
}
// Clear the JSON buffer for coalesced and deferred attributes
void Z_Devices::jsonClear(uint16_t shortaddr) {
Z_Device & device = getShortAddr(shortaddr);
if (&device == nullptr) { return; } // don't crash if not found
device.json = nullptr;
device.json_buffer->clear();
}
// Copy JSON from one object to another, this helps preserving the order of attributes
void CopyJsonVariant(JsonObject &to, const String &key, const JsonVariant &val) {
// first remove the potentially existing key in the target JSON, so new adds will be at the end of the list
to.remove(key); // force remove to have metadata like LinkQuality at the end
if (val.is<char*>()) {
String sval = val.as<String>(); // force a copy of the String value, avoiding crash
to.set(key, sval);
} else if (val.is<JsonArray>()) {
JsonArray &nested_arr = to.createNestedArray(key);
CopyJsonArray(nested_arr, val.as<JsonArray>()); // deep copy
} else if (val.is<JsonObject>()) {
JsonObject &nested_obj = to.createNestedObject(key);
CopyJsonObject(nested_obj, val.as<JsonObject>()); // deep copy
} else {
to.set(key, val); // general case for non array, object or string
}
}
// Shallow copy of array, we skip any sub-array or sub-object. It may be added in the future
void CopyJsonArray(JsonArray &to, const JsonArray &arr) {
for (auto v : arr) {
if (v.is<char*>()) {
String sval = v.as<String>(); // force a copy of the String value
to.add(sval);
} else if (v.is<JsonArray>()) {
} else if (v.is<JsonObject>()) {
} else {
to.add(v);
}
}
}
// Deep copy of object
void CopyJsonObject(JsonObject &to, const JsonObject &from) {
for (auto kv : from) {
String key_string = kv.key;
JsonVariant &val = kv.value;
CopyJsonVariant(to, key_string, val);
}
}
// does the new payload conflicts with the existing payload, i.e. values would be overwritten
// true - one attribute (except LinkQuality) woudl be lost, there is conflict
// false - new attributes can be safely added
bool Z_Devices::jsonIsConflict(uint16_t shortaddr, const JsonObject &values) {
Z_Device & device = getShortAddr(shortaddr);
if (&device == nullptr) { return false; } // don't crash if not found
if (&values == nullptr) { return false; }
if (nullptr == device.json) {
return false; // if no previous value, no conflict
}
// compare groups
// Special case for group addresses. Group attribute is only present if the target
// address is a group address, so just comparing attributes will not work.
// Eg: if the first packet has no group attribute, and the second does, conflict would not be detected
// Here we explicitly compute the group address of both messages, and compare them. No group means group=0x0000
// (we use the property of an missing attribute returning 0)
// (note: we use .get() here which is case-sensitive. We know however that the attribute was set with the exact syntax D_CMND_ZIGBEE_GROUP, so we don't need a case-insensitive get())
uint16_t group1 = device.json->get<unsigned int>(D_CMND_ZIGBEE_GROUP);
uint16_t group2 = values.get<unsigned int>(D_CMND_ZIGBEE_GROUP);
if (group1 != group2) {
return true; // if group addresses differ, then conflict
}
// parse all other parameters
for (auto kv : values) {
String key_string = kv.key;
if (0 == strcasecmp_P(kv.key, PSTR(D_CMND_ZIGBEE_GROUP))) {
// ignore group, it was handled already
} else if (0 == strcasecmp_P(kv.key, PSTR(D_CMND_ZIGBEE_ENDPOINT))) {
// attribute "Endpoint" or "Group"
if (device.json->containsKey(kv.key)) {
if (kv.value.as<unsigned int>() != device.json->get<unsigned int>(kv.key)) {
return true;
}
}
} else if (strcasecmp_P(kv.key, PSTR(D_CMND_ZIGBEE_LINKQUALITY))) { // exception = ignore duplicates for LinkQuality
if (device.json->containsKey(kv.key)) {
return true; // conflict!
}
}
}
return false;
}
void Z_Devices::jsonAppend(uint16_t shortaddr, const JsonObject &values) {
Z_Device & device = getShortAddr(shortaddr);
if (&device == nullptr) { return; } // don't crash if not found
if (&values == nullptr) { return; }
if (nullptr == device.json) {
device.json = &(device.json_buffer->createObject());
}
// Prepend Device, will be removed later if redundant
char sa[8];
snprintf_P(sa, sizeof(sa), PSTR("0x%04X"), shortaddr);
device.json->set(F(D_JSON_ZIGBEE_DEVICE), sa);
// Prepend Friendly Name if it has one
const char * fname = zigbee_devices.getFriendlyName(shortaddr);
if (fname) {
device.json->set(F(D_JSON_ZIGBEE_NAME), (char*) fname); // (char*) forces ArduinoJson to make a copy of the cstring
}
// copy all values from 'values' to 'json'
CopyJsonObject(*device.json, values);
}
const JsonObject *Z_Devices::jsonGet(uint16_t shortaddr) {
Z_Device & device = getShortAddr(shortaddr);
if (&device == nullptr) { return nullptr; } // don't crash if not found
return device.json;
}
void Z_Devices::jsonPublishFlush(uint16_t shortaddr) {
Z_Device & device = getShortAddr(shortaddr);
if (&device == nullptr) { return; } // don't crash if not found
JsonObject * json = device.json;
if (json == nullptr) { return; } // abort if nothing in buffer
const char * fname = zigbee_devices.getFriendlyName(shortaddr);
bool use_fname = (Settings.flag4.zigbee_use_names) && (fname); // should we replace shortaddr with friendlyname?
// Remove redundant "Name" or "Device"
if (use_fname) {
json->remove(F(D_JSON_ZIGBEE_NAME));
} else {
json->remove(F(D_JSON_ZIGBEE_DEVICE));
}
String msg = "";
json->printTo(msg);
zigbee_devices.jsonClear(shortaddr);
if (use_fname) {
Response_P(PSTR("{\"" D_JSON_ZIGBEE_RECEIVED "\":{\"%s\":%s}}"), fname, msg.c_str());
} else {
Response_P(PSTR("{\"" D_JSON_ZIGBEE_RECEIVED "\":{\"0x%04X\":%s}}"), shortaddr, msg.c_str());
}
if (Settings.flag4.zigbee_distinct_topics) {
char subtopic[16];
snprintf_P(subtopic, sizeof(subtopic), PSTR("%04X/" D_RSLT_SENSOR), shortaddr);
MqttPublishPrefixTopic_P(TELE, subtopic, Settings.flag.mqtt_sensor_retain);
} else {
MqttPublishPrefixTopic_P(TELE, PSTR(D_RSLT_SENSOR), Settings.flag.mqtt_sensor_retain);
}
XdrvRulesProcess();
}
void Z_Devices::jsonPublishNow(uint16_t shortaddr, JsonObject & values) {
jsonPublishFlush(shortaddr); // flush any previous buffer
jsonAppend(shortaddr, values);
jsonPublishFlush(shortaddr); // publish now
}
void Z_Devices::dirty(void) {
_saveTimer = kZigbeeSaveDelaySeconds * 1000 + millis();
}
void Z_Devices::clean(void) {
_saveTimer = 0;
}
// Parse the command parameters for either:
// - a short address starting with "0x", example: 0x1234
// - a long address starting with "0x", example: 0x7CB03EBB0A0292DD
// - a number 0..99, the index number in ZigbeeStatus
// - a friendly name, between quotes, example: "Room_Temp"
uint16_t Z_Devices::parseDeviceParam(const char * param, bool short_must_be_known) const {
if (nullptr == param) { return 0; }
size_t param_len = strlen(param);
char dataBuf[param_len + 1];
strcpy(dataBuf, param);
RemoveSpace(dataBuf);
uint16_t shortaddr = 0;
if (strlen(dataBuf) < 4) {
// simple number 0..99
if ((XdrvMailbox.payload > 0) && (XdrvMailbox.payload <= 99)) {
shortaddr = zigbee_devices.isKnownIndex(XdrvMailbox.payload - 1);
}
} else if ((dataBuf[0] == '0') && (dataBuf[1] == 'x')) {
// starts with 0x
if (strlen(dataBuf) < 18) {
// expect a short address
shortaddr = strtoull(dataBuf, nullptr, 0);
if (short_must_be_known) {
shortaddr = zigbee_devices.isKnownShortAddr(shortaddr);
}
// else we don't check if it's already registered to force unregistered devices
} else {
// expect a long address
uint64_t longaddr = strtoull(dataBuf, nullptr, 0);
shortaddr = zigbee_devices.isKnownLongAddr(longaddr);
}
} else {
// expect a Friendly Name
shortaddr = zigbee_devices.isKnownFriendlyName(dataBuf);
}
return shortaddr;
}
// Display the tracked status for a light
String Z_Devices::dumpLightState(uint16_t shortaddr) const {
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
char hex[8];
int32_t found = findShortAddr(shortaddr);
if (found >= 0) {
const Z_Device & device = devicesAt(found);
const char * fname = getFriendlyName(shortaddr);
bool use_fname = (Settings.flag4.zigbee_use_names) && (fname); // should we replace shortaddr with friendlyname?
snprintf_P(hex, sizeof(hex), PSTR("0x%04X"), shortaddr);
JsonObject& dev = use_fname ? json.createNestedObject((char*) fname) // casting (char*) forces a copy
: json.createNestedObject(hex);
if (use_fname) {
dev[F(D_JSON_ZIGBEE_DEVICE)] = hex;
} else if (fname) {
dev[F(D_JSON_ZIGBEE_NAME)] = (char*) fname;
}
// expose the last known status of the bulb, for Hue integration
dev[F(D_JSON_ZIGBEE_LIGHT)] = device.bulbtype; // sign extend, 0xFF changed as -1
if (0 <= device.bulbtype) {
// bulbtype is defined
dev[F("Power")] = device.power;
if (1 <= device.bulbtype) {
dev[F("Dimmer")] = device.dimmer;
}
if (2 <= device.bulbtype) {
dev[F("Colormode")] = device.colormode;
}
if ((2 == device.bulbtype) || (5 == device.bulbtype)) {
dev[F("CT")] = device.ct;
}
if (3 <= device.bulbtype) {
dev[F("Sat")] = device.sat;
dev[F("Hue")] = device.hue;
dev[F("X")] = device.x;
dev[F("Y")] = device.y;
}
}
}
String payload = "";
payload.reserve(200);
json.printTo(payload);
return payload;
}
// Dump the internal memory of Zigbee devices
// Mode = 1: simple dump of devices addresses
// Mode = 2: simple dump of devices addresses and names
// Mode = 3: Mode 2 + also dump the endpoints, profiles and clusters
String Z_Devices::dump(uint32_t dump_mode, uint16_t status_shortaddr) const {
DynamicJsonBuffer jsonBuffer;
JsonArray& json = jsonBuffer.createArray();
JsonArray& devices = json;
for (std::vector<Z_Device*>::const_iterator it = _devices.begin(); it != _devices.end(); ++it) {
const Z_Device &device = **it;
uint16_t shortaddr = device.shortaddr;
char hex[22];
// ignore non-current device, if specified device is non-zero
if ((status_shortaddr) && (status_shortaddr != shortaddr)) { continue; }
JsonObject& dev = devices.createNestedObject();
snprintf_P(hex, sizeof(hex), PSTR("0x%04X"), shortaddr);
dev[F(D_JSON_ZIGBEE_DEVICE)] = hex;
if (device.friendlyName > 0) {
dev[F(D_JSON_ZIGBEE_NAME)] = (char*) device.friendlyName;
}
if (2 <= dump_mode) {
hex[0] = '0'; // prefix with '0x'
hex[1] = 'x';
Uint64toHex(device.longaddr, &hex[2], 64);
dev[F("IEEEAddr")] = hex;
if (device.modelId) {
dev[F(D_JSON_MODEL D_JSON_ID)] = device.modelId;
}
if (device.manufacturerId) {
dev[F("Manufacturer")] = device.manufacturerId;
}
JsonArray& dev_endpoints = dev.createNestedArray(F("Endpoints"));