forked from dresden-elektronik/deconz-rest-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bindings.cpp
2119 lines (1878 loc) · 65.6 KB
/
bindings.cpp
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
/*
* Copyright (c) 2017 dresden elektronik ingenieurtechnik gmbh.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
* style license a copy of which has been included with this distribution in
* the LICENSE.txt file.
*
*/
#include "de_web_plugin.h"
#include "de_web_plugin_private.h"
#define MAX_ACTIVE_BINDING_TASKS 3
/*! Constructor. */
Binding::Binding() :
srcAddress(0),
srcEndpoint(0),
clusterId(0),
dstAddrMode(0),
dstEndpoint(0)
{
dstAddress.ext = 0;
}
/*! Returns true if bindings are equal. */
bool Binding::operator==(const Binding &rhs) const
{
if (rhs.dstAddrMode == dstAddrMode &&
rhs.srcAddress == srcAddress &&
rhs.dstAddress.ext == dstAddress.ext &&
rhs.clusterId == clusterId &&
rhs.dstEndpoint == dstEndpoint &&
rhs.srcEndpoint == srcEndpoint)
{
return true;
}
return false;
}
/*! Returns false if bindings are not equal. */
bool Binding::operator!=(const Binding &rhs) const
{
return !(*this == rhs);
}
/*! Reads a binding entry from stream. */
bool Binding::readFromStream(QDataStream &stream)
{
if (stream.atEnd()) return false;
stream >> srcAddress;
if (stream.atEnd()) return false;
stream >> srcEndpoint;
if (stream.atEnd()) return false;
stream >> clusterId;
if (stream.atEnd()) return false;
stream >> dstAddrMode;
if (dstAddrMode == GroupAddressMode)
{
if (stream.atEnd()) return false;
stream >> dstAddress.group;
dstEndpoint = 0; // not present
return true;
}
else if (dstAddrMode == ExtendedAddressMode)
{
if (stream.atEnd()) return false;
stream >> dstAddress.ext;
if (stream.atEnd()) return false;
stream >> dstEndpoint;
return true;
}
return false;
}
/*! Writes a binding to stream.
\param stream the data stream
*/
bool Binding::writeToStream(QDataStream &stream) const
{
if (!srcAddress || !srcEndpoint)
{
return false;
}
stream << srcAddress;
stream << srcEndpoint;
stream << clusterId;
stream << dstAddrMode;
if (dstAddrMode == GroupAddressMode)
{
stream << dstAddress.group;
return true;
}
else if ((dstAddrMode == ExtendedAddressMode) && (dstAddress.ext != 0) && (dstEndpoint != 0))
{
stream << dstAddress.ext;
stream << dstEndpoint;
return true;
}
return false;
}
/*! Queue reading ZDP binding table.
\param node the node from which the binding table shall be read
\param startIndex the index to start the reading
\return true if the request is queued
*/
bool DeRestPluginPrivate::readBindingTable(RestNodeBase *node, quint8 startIndex)
{
DBG_Assert(node != 0);
if (!node && node->node())
{
return false;
}
// whitelist
if ((node->address().ext() & macPrefixMask) == deMacPrefix)
{
}
else
{
node->clearRead(READ_BINDING_TABLE);
return false;
}
std::vector<BindingTableReader>::iterator i = bindingTableReaders.begin();
std::vector<BindingTableReader>::iterator end = bindingTableReaders.end();
for (; i != end; ++i)
{
if (i->apsReq.dstAddress().ext() == node->address().ext())
{
// already running
if (i->state == BindingTableReader::StateIdle)
{
i->index = startIndex;
DBG_Assert(bindingTableReaderTimer->isActive());
}
return true;
}
}
BindingTableReader btReader;
btReader.state = BindingTableReader::StateIdle;
btReader.index = startIndex;
btReader.isEndDevice = node->node()->isEndDevice();
btReader.apsReq.dstAddress() = node->address();
bindingTableReaders.push_back(btReader);
if (!bindingTableReaderTimer->isActive())
{
bindingTableReaderTimer->start();
}
return false;
}
/*! Handle bind table confirm.
\param conf a APSDE-DATA.confirm
\return true if confirm was processed
*/
bool DeRestPluginPrivate::handleMgmtBindRspConfirm(const deCONZ::ApsDataConfirm &conf)
{
if (conf.srcEndpoint() != ZDO_ENDPOINT || conf.dstEndpoint() != ZDO_ENDPOINT)
{
return false;
}
std::vector<BindingTableReader>::iterator i = bindingTableReaders.begin();
std::vector<BindingTableReader>::iterator end = bindingTableReaders.end();
for (; i != end; ++i)
{
if (i->apsReq.id() == conf.id())
{
if (i->state == BindingTableReader::StateWaitConfirm)
{
i->time.start();
i->state = BindingTableReader::StateWaitResponse;
}
return true;
}
}
return false;
}
/*! Handle bind table response.
\param ind a ZDP MgmtBind_rsp
*/
void DeRestPluginPrivate::handleMgmtBindRspIndication(const deCONZ::ApsDataIndication &ind)
{
if (ind.asdu().size() < 2)
{
// at least seq number and status
return;
}
BindingTableReader *btReader = 0;
{
std::vector<BindingTableReader>::iterator i = bindingTableReaders.begin();
std::vector<BindingTableReader>::iterator end = bindingTableReaders.end();
if (ind.srcAddress().hasExt())
{
for (; i != end; ++i)
{
if (i->apsReq.dstAddress().ext() == ind.srcAddress().ext())
{
btReader = &(*i);
break;
}
}
}
else if (ind.srcAddress().hasNwk())
{
for (; i != end; ++i)
{
if (i->apsReq.dstAddress().nwk() == ind.srcAddress().nwk())
{
btReader = &(*i);
break;
}
}
}
}
RestNodeBase *node = getSensorNodeForAddress(ind.srcAddress());
if (!node)
{
node = getLightNodeForAddress(ind.srcAddress());
}
if (!node)
{
if (btReader)
{
// no more needed
btReader->state = BindingTableReader::StateFinished;
}
return;
}
QDataStream stream(ind.asdu());
stream.setByteOrder(QDataStream::LittleEndian);
quint8 seqNo;
quint8 status;
stream >> seqNo;
stream >> status;
if (btReader)
{
DBG_Printf(DBG_ZDP, "MgmtBind_rsp id: %d %s seq: %u, status 0x%02X \n", btReader->apsReq.id(),
qPrintable(node->address().toStringExt()), seqNo, status);
}
else
{
DBG_Printf(DBG_ZDP, "MgmtBind_rsp (no BTR) %s seq: %u, status 0x%02X \n", qPrintable(node->address().toStringExt()), seqNo, status);
}
if (status != deCONZ::ZdpSuccess)
{
if (status == deCONZ::ZdpNotPermitted ||
status == deCONZ::ZdpNotSupported)
{
if (node->mgmtBindSupported())
{
DBG_Printf(DBG_ZDP, "MgmtBind_req/rsp %s not supported, deactivate \n", qPrintable(node->address().toStringExt()));
node->setMgmtBindSupported(false);
}
}
if (btReader)
{
// no more needed
btReader->state = BindingTableReader::StateFinished;
}
return;
}
quint8 entries;
quint8 startIndex;
quint8 listCount;
bool bend = false;
stream >> entries;
stream >> startIndex;
stream >> listCount;
if (entries > (startIndex + listCount))
{
if (btReader)
{
if (btReader->state == BindingTableReader::StateWaitResponse || btReader->state == BindingTableReader::StateWaitConfirm)
{
// read more
btReader->state = BindingTableReader::StateIdle;
btReader->index = startIndex + listCount;
}
else
{
DBG_Printf(DBG_ZDP, "unexpected BTR state %d\n", (int)btReader->state);
}
}
}
else
{
bend = true;
if (btReader)
{
btReader->state = BindingTableReader::StateFinished;
}
}
while (listCount && !stream.atEnd())
{
Binding bnd;
if (bnd.readFromStream(stream))
{
if (bnd.dstAddrMode == deCONZ::ApsExtAddress)
{
DBG_Printf(DBG_ZDP, "found binding 0x%04X, 0x%02X -> 0x%016llX : 0x%02X\n", bnd.clusterId, bnd.srcEndpoint, bnd.dstAddress.ext, bnd.dstEndpoint);
}
else if (bnd.dstAddrMode == deCONZ::ApsGroupAddress)
{
DBG_Printf(DBG_ZDP, "found binding 0x%04X, 0x%02X -> 0x%04X\n", bnd.clusterId, bnd.srcEndpoint, bnd.dstAddress.group);
}
else
{
continue;
}
if (std::find(bindingToRuleQueue.begin(), bindingToRuleQueue.end(), bnd) == bindingToRuleQueue.end())
{
DBG_Printf(DBG_ZDP, "add binding to check rule queue size: %d\n", static_cast<int>(bindingToRuleQueue.size()));
bindingToRuleQueue.push_back(bnd);
}
else
{
DBG_Printf(DBG_ZDP, "binding already in binding to rule queue\n");
}
std::list<BindingTask>::iterator i = bindingQueue.begin();
std::list<BindingTask>::iterator end = bindingQueue.end();
for (;i != end; ++i)
{
if (i->binding == bnd)
{
if (i->action == BindingTask::ActionBind && i->state != BindingTask::StateFinished)
{
DBG_Printf(DBG_ZDP, "binding 0x%04X, 0x%02X already exists, drop task\n", bnd.clusterId, bnd.dstEndpoint);
i->state = BindingTask::StateFinished; // already existing
sendConfigureReportingRequest(*i); // (re?)configure
}
else if (i->action == BindingTask::ActionUnbind && i->state == BindingTask::StateCheck)
{
DBG_Printf(DBG_ZDP, "binding 0x%04X, 0x%02X exists, start unbind task\n", bnd.clusterId, bnd.dstEndpoint);
i->state = BindingTask::StateIdle; // exists -> unbind
}
break;
}
}
}
else // invalid
{
DBG_Printf(DBG_ZDP, "invalid binding entry");
break;
}
listCount--;
}
// end, check remaining tasks
if (bend)
{
std::list<BindingTask>::iterator i = bindingQueue.begin();
std::list<BindingTask>::iterator end = bindingQueue.end();
for (;i != end; ++i)
{
if (i->state == BindingTask::StateCheck &&
i->binding.srcAddress == ind.srcAddress().ext())
{
// if binding was not found, activate binding task
if (i->action == BindingTask::ActionBind)
{
DBG_Printf(DBG_ZDP, "binding 0x%04X, 0x%02X not found, start bind task\n", i->binding.clusterId, i->binding.dstEndpoint);
i->state = BindingTask::StateIdle;
}
else if (i->action == BindingTask::ActionUnbind)
{
// nothing to unbind
DBG_Printf(DBG_ZDP, "binding 0x%04X, 0x%02X not found, remove unbind task\n", i->binding.clusterId, i->binding.dstEndpoint);
i->state = BindingTask::StateFinished; // already existing
}
}
}
}
if (!bindingToRuleTimer->isActive() && !bindingToRuleQueue.empty())
{
bindingToRuleTimer->start();
}
}
/*! Handle bind/unbind response.
\param ind a ZDP Bind/Unbind_rsp
*/
void DeRestPluginPrivate::handleBindAndUnbindRspIndication(const deCONZ::ApsDataIndication &ind)
{
QDataStream stream(ind.asdu());
stream.setByteOrder(QDataStream::LittleEndian);
quint8 zdpSeqNum;
quint8 status;
stream >> zdpSeqNum;
stream >> status;
std::list<BindingTask>::iterator i = bindingQueue.begin();
std::list<BindingTask>::iterator end = bindingQueue.end();
for (; i != end; ++i)
{
if (i->zdpSeqNum == zdpSeqNum)
{
const char *what = (ind.clusterId() == ZDP_BIND_RSP_CLID) ? "Bind" : "Unbind";
if (status == deCONZ::ZdpSuccess)
{
DBG_Printf(DBG_INFO, "%s response success\n", what);
if (ind.clusterId() == ZDP_BIND_RSP_CLID)
{
sendConfigureReportingRequest(*i);
}
}
else
{
DBG_Printf(DBG_INFO, "%s response failed with status 0x%02X\n", what, status);
}
i->state = BindingTask::StateFinished;
return;
}
}
}
/*! Sends a ZDP bind request.
\param bt a binding task
*/
bool DeRestPluginPrivate::sendBindRequest(BindingTask &bt)
{
DBG_Assert(apsCtrl != 0);
if (!apsCtrl)
{
return false;
}
Binding &bnd = bt.binding;
deCONZ::ApsDataRequest apsReq;
// set destination addressing
apsReq.setDstAddressMode(deCONZ::ApsExtAddress);
apsReq.setTxOptions(deCONZ::ApsTxAcknowledgedTransmission);
apsReq.dstAddress().setExt(bnd.srcAddress);
apsReq.setDstEndpoint(ZDO_ENDPOINT);
apsReq.setSrcEndpoint(ZDO_ENDPOINT);
apsReq.setProfileId(ZDP_PROFILE_ID);
if (bt.action == BindingTask::ActionBind)
{
apsReq.setClusterId(ZDP_BIND_REQ_CLID);
}
else
{
apsReq.setClusterId(ZDP_UNBIND_REQ_CLID);
}
// prepare payload
QDataStream stream(&apsReq.asdu(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
// generate and remember a new ZDP transaction sequence number
bt.zdpSeqNum = (uint8_t)qrand();
stream << bt.zdpSeqNum; // ZDP transaction sequence number
if (!bnd.writeToStream(stream))
{
return false;
}
if (apsCtrl && (apsCtrl->apsdeDataRequest(apsReq) == deCONZ::Success))
{
return true;
}
return false;
}
/*! Sends a ZCL configure attribute reporting request.
\param bt a former binding task
\param requests list of configure reporting requests which will be combined in a message
*/
bool DeRestPluginPrivate::sendConfigureReportingRequest(BindingTask &bt, const std::vector<ConfigureReportingRequest> &requests)
{
DBG_Assert(!requests.empty());
if (requests.empty())
{
return false;
}
LightNode *lightNode = dynamic_cast<LightNode*>(bt.restNode);
QDateTime now = QDateTime::currentDateTime();
std::vector<ConfigureReportingRequest> out;
for (const ConfigureReportingRequest &rq : requests)
{
NodeValue &val = bt.restNode->getZclValue(bt.binding.clusterId, rq.attributeId);
if (val.clusterId == bt.binding.clusterId)
{
// value exists
if (val.timestampLastReport.isValid() &&
val.timestampLastReport.secsTo(now) < qMin((rq.maxInterval * 3), 1800))
{
DBG_Printf(DBG_INFO, "skip configure report for cluster: 0x%04X attr: 0x%04X of node 0x%016llX (seems to be active)\n",
bt.binding.clusterId, rq.attributeId, bt.restNode->address().ext());
}
else
{
if (!val.timestampLastReport.isValid())
{
// fake first report timestamp to mark succesful binding
// and prevent further bind requests before reports arrive
val.timestampLastReport = QDateTime::currentDateTime();
}
out.push_back(rq);
}
}
else if (lightNode)
{
// wait for value is created via polling
DBG_Printf(DBG_INFO, "skip configure report for cluster: 0x%04X attr: 0x%04X of node 0x%016llX (wait reading or unsupported)\n",
bt.binding.clusterId, rq.attributeId, bt.restNode->address().ext());
}
else // sensors
{
// values doesn't exist, create
deCONZ::NumericUnion dummy;
dummy.u64 = 0;
bt.restNode->setZclValue(NodeValue::UpdateByZclReport, bt.binding.clusterId, rq.attributeId, dummy);
out.push_back(rq);
}
}
if (out.empty())
{
return false;
}
deCONZ::ApsDataRequest apsReq;
// ZDP Header
apsReq.dstAddress() = bt.restNode->address();
apsReq.setDstAddressMode(deCONZ::ApsExtAddress);
apsReq.setDstEndpoint(bt.binding.srcEndpoint);
apsReq.setSrcEndpoint(endpoint());
apsReq.setProfileId(HA_PROFILE_ID);
apsReq.setRadius(0);
apsReq.setClusterId(bt.binding.clusterId);
apsReq.setTxOptions(deCONZ::ApsTxAcknowledgedTransmission);
deCONZ::ZclFrame zclFrame;
zclFrame.setSequenceNumber(zclSeq++);
zclFrame.setCommandId(deCONZ::ZclConfigureReportingId);
if (requests.front().manufacturerCode)
{
zclFrame.setFrameControl(deCONZ::ZclFCProfileCommand |
deCONZ::ZclFCManufacturerSpecific |
deCONZ::ZclFCDirectionClientToServer |
deCONZ::ZclFCDisableDefaultResponse);
zclFrame.setManufacturerCode(requests.front().manufacturerCode);
}
else
{
zclFrame.setFrameControl(deCONZ::ZclFCProfileCommand |
deCONZ::ZclFCDirectionClientToServer |
deCONZ::ZclFCDisableDefaultResponse);
}
{ // payload
QDataStream stream(&zclFrame.payload(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
for (const ConfigureReportingRequest &rq : out)
{
stream << rq.direction;
stream << rq.attributeId;
stream << rq.dataType;
stream << rq.minInterval;
stream << rq.maxInterval;
if (rq.reportableChange16bit != 0xFFFF)
{
stream << rq.reportableChange16bit;
}
else if (rq.reportableChange8bit != 0xFF)
{
stream << rq.reportableChange8bit;
}
DBG_Printf(DBG_INFO_L2, "configure reporting for 0x%016llX, attribute 0x%04X/0x%04X\n", bt.restNode->address().ext(), bt.binding.clusterId, rq.attributeId);
}
}
{ // ZCL frame
QDataStream stream(&apsReq.asdu(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
zclFrame.writeToStream(stream);
}
if (apsCtrl && apsCtrl->apsdeDataRequest(apsReq) == deCONZ::Success)
{
queryTime = queryTime.addSecs(1);
return true;
}
return false;
}
/*! Sends a ZCL configure attribute reporting request.
\param bt a former binding task
*/
bool DeRestPluginPrivate::sendConfigureReportingRequest(BindingTask &bt)
{
if (!bt.restNode || !bt.restNode->node())
{
return false;
}
deCONZ::SimpleDescriptor *sd = bt.restNode->node()->getSimpleDescriptor(bt.binding.srcEndpoint);
if (!sd)
{
return false;
}
// check if bound cluster is server cluster
deCONZ:: ZclCluster *cl = sd->cluster(bt.binding.clusterId, deCONZ::ServerCluster);
if (!cl)
{
return false;
}
ConfigureReportingRequest rq;
if (bt.binding.clusterId == OCCUPANCY_SENSING_CLUSTER_ID)
{
rq.dataType = deCONZ::Zcl8BitBitMap;
rq.attributeId = 0x0000; // occupancy
rq.minInterval = 1; // value used by Hue bridge
rq.maxInterval = 300; // value used by Hue bridge
if (sendConfigureReportingRequest(bt, {rq}))
{
Sensor *sensor = static_cast<Sensor *>(bt.restNode);
if (sensor && sensor->modelId() == QLatin1String("SML001")) // Hue motion sensor
{
rq = ConfigureReportingRequest();
rq.dataType = deCONZ::Zcl8BitUint;
rq.attributeId = 0x0030; // sensitivity
rq.minInterval = 5; // value used by Hue bridge
rq.maxInterval = 7200; // value used by Hue bridge
rq.reportableChange8bit = 1; // value used by Hue bridge
rq.manufacturerCode = VENDOR_PHILIPS;
return sendConfigureReportingRequest(bt, {rq});
}
return true;
}
return false;
}
else if (bt.binding.clusterId == ILLUMINANCE_MEASUREMENT_CLUSTER_ID)
{
rq.dataType = deCONZ::Zcl16BitUint;
rq.attributeId = 0x0000; // measured value
rq.minInterval = 5; // value used by Hue bridge
rq.maxInterval = 300; // value used by Hue bridge
rq.reportableChange16bit = 2000; // value used by Hue bridge
return sendConfigureReportingRequest(bt, {rq});
}
else if (bt.binding.clusterId == TEMPERATURE_MEASUREMENT_CLUSTER_ID)
{
rq.dataType = deCONZ::Zcl16BitInt;
rq.attributeId = 0x0000; // measured value
rq.minInterval = 10; // value used by Hue bridge
rq.maxInterval = 300; // value used by Hue bridge
rq.reportableChange16bit = 20; // value used by Hue bridge
return sendConfigureReportingRequest(bt, {rq});
}
else if (bt.binding.clusterId == RELATIVE_HUMIDITY_CLUSTER_ID)
{
rq.dataType = deCONZ::Zcl16BitUint;
rq.attributeId = 0x0000; // measured value
rq.minInterval = 10;
rq.maxInterval = 300;
rq.reportableChange16bit = 20;
return sendConfigureReportingRequest(bt, {rq});
}
else if (bt.binding.clusterId == PRESSURE_MEASUREMENT_CLUSTER_ID)
{
rq.dataType = deCONZ::Zcl16BitUint;
rq.attributeId = 0x0000; // measured value
rq.minInterval = 10;
rq.maxInterval = 300;
rq.reportableChange16bit = 20;
return sendConfigureReportingRequest(bt, {rq});
}
else if (bt.binding.clusterId == POWER_CONFIGURATION_CLUSTER_ID)
{
Sensor *sensor = static_cast<Sensor *>(bt.restNode);
rq.dataType = deCONZ::Zcl8BitUint;
rq.attributeId = 0x0021; // battery percentage remaining
if (sensor && sensor->modelId() == QLatin1String("SML001")) // Hue motion sensor
{
rq.minInterval = 7200; // value used by Hue bridge
rq.maxInterval = 7200; // value used by Hue bridge
rq.reportableChange8bit = 0; // value used by Hue bridge
}
else if (sensor && sensor->modelId().startsWith(QLatin1String("RWL02"))) // Hue dimmer switch
{
rq.minInterval = 300; // value used by Hue bridge
rq.maxInterval = 300; // value used by Hue bridge
rq.reportableChange8bit = 0; // value used by Hue bridge
}
else
{
rq.minInterval = 300;
rq.maxInterval = 60 * 45;
rq.reportableChange8bit = 1;
}
return sendConfigureReportingRequest(bt, {rq});
}
else if (bt.binding.clusterId == ONOFF_CLUSTER_ID)
{
rq.dataType = deCONZ::ZclBoolean;
rq.attributeId = 0x0000; // on/off
if ((bt.restNode->address().ext() & macPrefixMask) == deMacPrefix)
{
rq.minInterval = 5;
rq.maxInterval = 180;
}
else // default configuration
{
rq.minInterval = 1;
rq.maxInterval = 300;
}
return sendConfigureReportingRequest(bt, {rq});
}
else if (bt.binding.clusterId == LEVEL_CLUSTER_ID)
{
rq.dataType = deCONZ::Zcl8BitUint;
rq.attributeId = 0x0000; // current level
if ((bt.restNode->address().ext() & macPrefixMask) == deMacPrefix)
{
rq.minInterval = 5;
rq.maxInterval = 180;
rq.reportableChange8bit = 5;
}
else // default configuration
{
rq.minInterval = 1;
rq.maxInterval = 300;
rq.reportableChange8bit = 1;
}
return sendConfigureReportingRequest(bt, {rq});
}
else if (bt.binding.clusterId == COLOR_CLUSTER_ID)
{
rq.dataType = deCONZ::Zcl16BitUint;
rq.attributeId = 0x0007; // color temperature
rq.minInterval = 1;
rq.maxInterval = 300;
rq.reportableChange16bit = 1;
ConfigureReportingRequest rq2;
rq2.dataType = deCONZ::Zcl16BitUint;
rq2.attributeId = 0x0003; // colorX
rq2.minInterval = 1;
rq2.maxInterval = 300;
rq2.reportableChange16bit = 10;
ConfigureReportingRequest rq3;
rq3.dataType = deCONZ::Zcl16BitUint;
rq3.attributeId = 0x0004; // colorY
rq3.minInterval = 1;
rq3.maxInterval = 300;
rq3.reportableChange16bit = 10;
ConfigureReportingRequest rq4;
rq4.dataType = deCONZ::Zcl8BitEnum;
rq4.attributeId = 0x0008; // color mode
rq4.minInterval = 1;
rq4.maxInterval = 300;
return sendConfigureReportingRequest(bt, {rq, rq2, rq3, rq4});
}
else if (bt.binding.clusterId == BASIC_CLUSTER_ID &&
(bt.restNode->address().ext() & macPrefixMask) == philipsMacPrefix)
{
rq.dataType = deCONZ::ZclBoolean;
rq.attributeId = 0x0032; // usertest
rq.minInterval = 5; // value used by Hue bridge
rq.maxInterval = 7200; // value used by Hue bridge
rq.manufacturerCode = VENDOR_PHILIPS;
if (sendConfigureReportingRequest(bt, {rq}))
{
rq = ConfigureReportingRequest();
rq.dataType = deCONZ::ZclBoolean;
rq.attributeId = 0x0033; // ledindication
rq.minInterval = 5; // value used by Hue bridge
rq.maxInterval = 7200; // value used by Hue bridge
rq.manufacturerCode = VENDOR_PHILIPS;
return sendConfigureReportingRequest(bt, {rq});
}
return false;
}
return false;
}
/*! Creates binding for attribute reporting to gateway node. */
void DeRestPluginPrivate::checkLightBindingsForAttributeReporting(LightNode *lightNode)
{
if (!apsCtrl || !lightNode || !lightNode->address().hasExt())
{
return;
}
// prevent binding action if otau was busy recently
if (otauLastBusyTimeDelta() < OTA_LOW_PRIORITY_TIME)
{
if (lightNode->modelId().startsWith(QLatin1String("FLS-")))
{
DBG_Printf(DBG_INFO, "don't check binding for attribute reporting of %s (otau busy)\n", qPrintable(lightNode->name()));
return;
}
}
BindingTask::Action action = BindingTask::ActionUnbind;
// whitelist
if (gwReportingEnabled)
{
action = BindingTask::ActionBind;
if (lightNode->modelId().startsWith(QLatin1String("FLS-NB")))
{
}
else if (lightNode->manufacturer() == QLatin1String("OSRAM"))
{
}
else if (lightNode->manufacturerCode() == VENDOR_UBISYS)
{
}
else if (lightNode->manufacturerCode() == VENDOR_IKEA)
{
}
else
{
return;
}
}
else
{
return;
}
QList<deCONZ::ZclCluster>::const_iterator i = lightNode->haEndpoint().inClusters().begin();
QList<deCONZ::ZclCluster>::const_iterator end = lightNode->haEndpoint().inClusters().end();
int tasksAdded = 0;
QDateTime now = QDateTime::currentDateTime();
for (; i != end; ++i)
{
switch (i->id())
{
case ONOFF_CLUSTER_ID:
case LEVEL_CLUSTER_ID:
case COLOR_CLUSTER_ID:
{
bool bindingExists = false;
for (const NodeValue &val : lightNode->zclValues())
{
if (val.clusterId != i->id() || !val.timestampLastReport.isValid())
{
continue;
}
if (val.timestampLastReport.secsTo(now) < (10 * 60))
{
bindingExists = true;
break;
}
}
BindingTask bt;
if ((lightNode->address().ext() & macPrefixMask) == deMacPrefix)
{
bt.state = BindingTask::StateCheck;
}
else
{
bt.state = BindingTask::StateIdle;
}
bt.action = action;
bt.restNode = lightNode;
Binding &bnd = bt.binding;
bnd.srcAddress = lightNode->address().ext();
bnd.dstAddrMode = deCONZ::ApsExtAddress;
bnd.srcEndpoint = lightNode->haEndpoint().endpoint();
bnd.clusterId = i->id();
bnd.dstAddress.ext = apsCtrl->getParameter(deCONZ::ParamMacAddress);
bnd.dstEndpoint = endpoint();
if (bnd.dstEndpoint > 0) // valid gateway endpoint?
{
if (bindingExists)
{
DBG_Printf(DBG_INFO, "binding for cluster 0x%04X of 0x%016llX exists (verified by reporting)\n", i->id(), lightNode->address().ext());
sendConfigureReportingRequest(bt);
}
else
{
DBG_Printf(DBG_INFO_L2, "create binding for attribute reporting of cluster 0x%04X\n", i->id());
queueBindingTask(bt);
tasksAdded++;
}
}
}
break;
default:
break;
}
}
if (tasksAdded == 0)
{
return;
}
if ((lightNode->address().ext() & macPrefixMask) == deMacPrefix)
{
lightNode->enableRead(READ_BINDING_TABLE);
lightNode->setNextReadTime(READ_BINDING_TABLE, queryTime);
queryTime = queryTime.addSecs(5);
Q_Q(DeRestPlugin);
q->startZclAttributeTimer(1000);
}
if (!bindingTimer->isActive())
{
bindingTimer->start();
}
}
/*! Creates binding for attribute reporting to gateway node. */
void DeRestPluginPrivate::checkSensorBindingsForAttributeReporting(Sensor *sensor)
{
if (!apsCtrl || !sensor || !sensor->address().hasExt() || !sensor->node() || !sensor->toBool(RConfigReachable))
{
return;
}
if (findSensorsState != FindSensorsActive &&
idleTotalCounter < (IDLE_READ_LIMIT + 120)) // wait for some input before fire bindings
{
return;
}
bool endDeviceSupported = false;
// whitelist
// Climax