forked from dresden-elektronik/deconz-rest-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bindings.cpp
2361 lines (2084 loc) · 74.3 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;
}
Resource *r = dynamic_cast<Resource*>(node);
// whitelist
if ((node->address().ext() & macPrefixMask) == deMacPrefix)
{
}
else if (r && r->item(RAttrModelId)->toString().startsWith(QLatin1String("FLS-")))
{
}
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()->nodeDescriptor().receiverOnWhenIdle();
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 incoming ZCL configure reporting response.
*/
void DeRestPluginPrivate::handleZclConfigureReportingResponseIndication(const deCONZ::ApsDataIndication &ind, deCONZ::ZclFrame &zclFrame)
{
QDateTime now = QDateTime::currentDateTime();
std::vector<RestNodeBase*> allNodes;
for (Sensor &s : sensors)
{
allNodes.push_back(&s);
}
for (LightNode &l : nodes)
{
allNodes.push_back(&l);
}
for (RestNodeBase * restNode : allNodes)
{
if (restNode->address().ext() != ind.srcAddress().ext())
{
continue;
}
QDataStream stream(zclFrame.payload());
stream.setByteOrder(QDataStream::LittleEndian);
while (!stream.atEnd())
{
quint8 status;
quint8 direction;
quint16 attrId;
stream >> status;
if (stream.status() == QDataStream::ReadPastEnd)
{
break;
}
// optional fields
stream >> direction;
stream >> attrId;
for (NodeValue &val : restNode->zclValues())
{
if (val.zclSeqNum != zclFrame.sequenceNumber())
{
continue;
}
if (val.minInterval == 0 && val.maxInterval == 0)
{
continue;
}
DBG_Printf(DBG_INFO, "ZCL configure reporting rsp seq: %u 0x%016llX for cluster 0x%04X attr 0x%04X status 0x%02X\n", zclFrame.sequenceNumber(), ind.srcAddress().ext(), ind.clusterId(), val.attributeId, status);
// mark as succefully configured
val.timestampLastConfigured = now;
}
}
}
}
/*! 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(requests.front().zclSeqNum);
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;
}
else if (rq.reportableChange24bit != 0xFFFFFF)
{
stream << (qint8) (rq.reportableChange24bit & 0xFF);
stream << (qint8) ((rq.reportableChange24bit >> 8) & 0xFF);
stream << (qint8) ((rq.reportableChange24bit >> 16) & 0xFF);
}
else if (rq.reportableChange48bit != 0xFFFFFFFF)
{
stream << (qint8) (rq.reportableChange48bit & 0xFF);
stream << (qint8) ((rq.reportableChange48bit >> 8) & 0xFF);
stream << (qint8) ((rq.reportableChange48bit >> 16) & 0xFF);
stream << (qint8) ((rq.reportableChange48bit >> 24) & 0xFF);
stream << (qint8) 0x00;
stream << (qint8) 0x00;
}
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;
}
QDateTime now = QDateTime::currentDateTime();
ConfigureReportingRequest rq;
rq.zclSeqNum = zclSeq++; // to match in configure reporting response handler
if (bt.binding.clusterId == OCCUPANCY_SENSING_CLUSTER_ID)
{
// add values if not already present
deCONZ::NumericUnion dummy;
dummy.u64 = 0;
if (bt.restNode->getZclValue(bt.binding.clusterId, 0x0000).clusterId != bt.binding.clusterId)
{
bt.restNode->setZclValue(NodeValue::UpdateInvalid, bt.binding.clusterId, 0x0000, dummy);
}
NodeValue &val = bt.restNode->getZclValue(bt.binding.clusterId, 0x0000);
val.zclSeqNum = rq.zclSeqNum;
rq.dataType = deCONZ::Zcl8BitBitMap;
rq.attributeId = 0x0000; // occupancy
val.minInterval = 1; // value used by Hue bridge
val.maxInterval = 300; // value used by Hue bridge
rq.minInterval = val.minInterval;
rq.maxInterval = val.maxInterval;
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 = 100; // resolution: 1%
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 = dynamic_cast<Sensor *>(bt.restNode);
// add values if not already present
deCONZ::NumericUnion dummy;
dummy.u64 = 0;
if (bt.restNode->getZclValue(POWER_CONFIGURATION_CLUSTER_ID, 0x0021).attributeId != 0x0021)
{
bt.restNode->setZclValue(NodeValue::UpdateInvalid, BASIC_CLUSTER_ID, 0x0021, dummy);
}
NodeValue &val = bt.restNode->getZclValue(POWER_CONFIGURATION_CLUSTER_ID, 0x0021);
val.zclSeqNum = rq.zclSeqNum;
rq.dataType = deCONZ::Zcl8BitUint;
rq.attributeId = 0x0021; // battery percentage remaining
if (sensor && sensor->modelId() == QLatin1String("SML001")) // Hue motion sensor
{
val.minInterval = 7200; // value used by Hue bridge
val.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
{
val.minInterval = 300; // value used by Hue bridge
val.maxInterval = 300; // value used by Hue bridge
rq.reportableChange8bit = 0; // value used by Hue bridge
}
else
{
val.minInterval = 300;
val.maxInterval = 60 * 45;
rq.reportableChange8bit = 1;
}
if (val.timestampLastReport.isValid() && (val.timestampLastReport.secsTo(now) < val.maxInterval * 1.5))
{
return false;
}
rq.minInterval = val.minInterval;
rq.maxInterval = val.maxInterval;
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 == METERING_CLUSTER_ID)
{
rq.dataType = deCONZ::Zcl48BitUint;
rq.attributeId = 0x0000; // Curent Summation Delivered
rq.minInterval = 1;
rq.maxInterval = 300;
rq.reportableChange48bit = 10; // 0.01 kWh
ConfigureReportingRequest rq2;
rq2.dataType = deCONZ::Zcl24BitInt;
rq2.attributeId = 0x0400; // Instantaneous Demand
rq2.minInterval = 1;
rq2.maxInterval = 300;
rq2.reportableChange24bit = 10; // 1 W
return sendConfigureReportingRequest(bt, {rq, rq2});
}
else if (bt.binding.clusterId == ELECTRICAL_MEASUREMENT_CLUSTER_ID)
{
rq.dataType = deCONZ::Zcl16BitInt;
rq.attributeId = 0x050B; // Active power
rq.minInterval = 1;
rq.maxInterval = 300;
rq.reportableChange16bit = 10; // 1 W
ConfigureReportingRequest rq2;
rq2.dataType = deCONZ::Zcl16BitUint;
rq2.attributeId = 0x0505; // RMS Voltage
rq2.minInterval = 1;
rq2.maxInterval = 300;
rq2.reportableChange16bit = 100; // 1 V
ConfigureReportingRequest rq3;
rq3.dataType = deCONZ::Zcl16BitUint;
rq3.attributeId = 0x0508; // RMS Current
rq3.minInterval = 1;
rq3.maxInterval = 300;
rq3.reportableChange16bit = 1; // 0.1 A
return sendConfigureReportingRequest(bt, {rq, rq2, rq3});
}
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)
{
Sensor *sensor = dynamic_cast<Sensor*>(bt.restNode);
if (!sensor)
{
return false;
}
// only process for presence sensor: don't issue configuration for temperature and illuminance sensors
// TODO check if just used for SML001 sensor or also hue dimmer switch?
if (sensor->type() != QLatin1String("ZHAPresence"))
{
return false;
}
deCONZ::NumericUnion dummy;
dummy.u64 = 0;
// add usertest value if not already present