forked from dresden-elektronik/deconz-rest-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest_touchlink.cpp
918 lines (775 loc) · 25.7 KB
/
rest_touchlink.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
/*
* Copyright (c) 2016 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 <QApplication>
#include <QString>
#include <QTcpSocket>
#include <QVariantMap>
#include "de_web_plugin.h"
#include "de_web_plugin_private.h"
#include "json.h"
// duration to wait for scan responses
#define TL_SCAN_WAIT_TIME 250
#define TL_TRANSACTION_TIMEOUT 7000 // default 8s, subtract 1s for sanity
#define TL_RECONNECT_NOW 100 // small delay to prevent false positives
#define TL_RECONNECT_CHECK_DELAY 5000
#define TL_DISCONNECT_CHECK_DELAY 100
#define TL_SCAN_COUNT (touchlinkChannel == 11 ? 5 : 1)
// Touchlink ZCL command ids
#define TL_CMD_SCAN_REQ 0x00
#define TL_CMD_SCAN_RSP 0x01
#define TL_CMD_DEVICE_INFORMATION_REQ 0x02
#define TL_CMD_DEVICE_INFORMATION_RSP 0x03
#define TL_CMD_IDENTIFY_REQ 0x06
#define TL_CMD_RESET_TO_FACTORY_NEW_REQ 0x07
#define TL_CMD_NETWORK_START_REQ 0x10
#define TL_CMD_NETWORK_START_RSP 0x11
#define TL_CMD_NETWORK_JOIN_ROUTER_REQ 0x12
#define TL_CMD_NETWORK_JOIN_ROUTER_RSP 0x13
#define TL_CMD_NETWORK_JOIN_ENDDEVICE_REQ 0x14
#define TL_CMD_NETWORK_JOIN_ENDDEVICE_RSP 0x15
#define TL_CMD_NETWORK_UPDATE_REQ 0x16
#define TL_CMD_ENDPOINT_INFORMATION 0x40
// flag for factory new in scan response 9th byte
#define FACTORY_NEW_FLAG 0x01
#define NETWORK_ATTEMPS 10
/*! Init the touchlink api and helpers.
*/
void DeRestPluginPrivate::initTouchlinkApi()
{
touchlinkState = TL_Idle;
touchlinkCtrl = deCONZ::TouchlinkController::instance();
DBG_Assert(touchlinkCtrl != 0);
connect(touchlinkCtrl, SIGNAL(startInterpanModeConfirm(deCONZ::TouchlinkStatus)),
this, SLOT(startTouchlinkModeConfirm(deCONZ::TouchlinkStatus)));
connect(touchlinkCtrl, SIGNAL(sendInterpanConfirm(deCONZ::TouchlinkStatus)),
this, SLOT(sendTouchlinkConfirm(deCONZ::TouchlinkStatus)));
connect(touchlinkCtrl, SIGNAL(interpanIndication(QByteArray)),
this, SLOT(interpanDataIndication(QByteArray)));
touchlinkTimer = new QTimer(this);
touchlinkTimer->setSingleShot(true);
connect(touchlinkTimer, SIGNAL(timeout()),
this, SLOT(touchlinkTimerFired()));
}
/*! Touchlink REST API broker.
\param req - request data
\param rsp - response data
\return REQ_READY_SEND
REQ_NOT_HANDLED
*/
int DeRestPluginPrivate::handleTouchlinkApi(const ApiRequest &req, ApiResponse &rsp)
{
if (req.path[2] != "touchlink")
{
return REQ_NOT_HANDLED;
}
// POST /api/<apikey>/touchlink/scan
if ((req.path.size() == 4) && (req.hdr.method() == "POST") && (req.path[3] == "scan"))
{
return touchlinkScan(req, rsp);
}
// GET /api/<apikey>/touchlink/scan
if ((req.path.size() == 4) && (req.hdr.method() == "GET") && (req.path[3] == "scan"))
{
return getTouchlinkScanResults(req, rsp);
}
// POST /api/<apikey>/touchlink/<id>/identify
if ((req.path.size() == 5) && (req.hdr.method() == "POST") && (req.path[4] == "identify"))
{
return identifyLight(req, rsp);
}
// POST /api/<apikey>/touchlink/<id>/reset
if ((req.path.size() == 5) && (req.hdr.method() == "POST") && (req.path[4] == "reset"))
{
return resetLight(req, rsp);
}
return REQ_NOT_HANDLED;
}
/*! POST /api/<apikey>/touchlink/scan
\param req - request data
\param rsp - response data
\return REQ_READY_SEND
REQ_NOT_HANDLED
*/
int DeRestPluginPrivate::touchlinkScan(const ApiRequest &req, ApiResponse &rsp)
{
Q_UNUSED(req);
if (touchlinkState != TL_Idle)
{
rsp.httpStatus = HttpStatusServiceUnavailable;
return REQ_READY_SEND;
}
uint32_t transactionId = qrand();
if (transactionId == 0)
{
transactionId++;
}
touchlinkAction = TouchlinkScan;
touchlinkChannel = 11; // start channel
touchlinkScanCount = 0;
touchlinkScanResponses.clear();
touchlinkScanTime = QDateTime::currentDateTime();
touchlinkReq.setTransactionId(transactionId);
touchlinkDisconnectNetwork();
rsp.httpStatus = HttpStatusOk;
return REQ_READY_SEND;
}
/*! GET /api/<apikey>/touchlink/scan
\param req - request data
\param rsp - response data
\return REQ_READY_SEND
REQ_NOT_HANDLED
*/
int DeRestPluginPrivate::getTouchlinkScanResults(const ApiRequest &req, ApiResponse &rsp)
{
Q_UNUSED(req);
rsp.httpStatus = HttpStatusOk;
bool scanning = false;
if ((touchlinkAction == TouchlinkScan) && (touchlinkState != TL_Idle))
{
scanning = true;
}
rsp.map["scanstate"] = (scanning ? "scanning" : "idle");
rsp.map["lastscan"] = touchlinkScanTime.toString("yyyy-MM-ddTHH:mm:ss");
QVariantMap result;
std::vector<ScanResponse>::const_iterator i = touchlinkScanResponses.begin();
std::vector<ScanResponse>::const_iterator end = touchlinkScanResponses.end();
for (; i != end; ++i)
{
QVariantMap item;
item["address"] = i->address.toStringExt();
item["factorynew"] = i->factoryNew;
item["rssi"] = (double)i->rssi;
item["channel"] = (double)i->channel;
item["panid"] = (double)i->panid;
result[i->id] = item;
}
rsp.map["result"] = result;
return REQ_READY_SEND;
}
/*! POST /api/<apikey>/touchlink/<id>/identify
\param req - request data
\param rsp - response data
\return REQ_READY_SEND
REQ_NOT_HANDLED
*/
int DeRestPluginPrivate::identifyLight(const ApiRequest &req, ApiResponse &rsp)
{
/*
* - disconnect
* - start interpan mode
* - send interpan scan request
* - send interpan identify
* - reconnect
*/
if (touchlinkState != TL_Idle)
{
rsp.httpStatus = HttpStatusServiceUnavailable;
return REQ_READY_SEND;
}
QString id = req.path[3];
touchlinkDevice.id.clear(); // mark as undefined
{ // search the device according to its id
std::vector<ScanResponse>::const_iterator i = touchlinkScanResponses.begin();
std::vector<ScanResponse>::const_iterator end = touchlinkScanResponses.end();
for (; i != end; ++i)
{
if (i->id == id)
{
touchlinkDevice = *i;
break;
}
}
}
if (touchlinkDevice.id.isEmpty())
{
rsp.httpStatus = HttpStatusNotFound;
return REQ_READY_SEND;
}
uint32_t transactionId = qrand();
if (transactionId == 0)
{
transactionId++;
}
touchlinkReq.setTransactionId(transactionId);
touchlinkAction = TouchlinkIdentify;
touchlinkChannel = touchlinkDevice.channel;
touchlinkDisconnectNetwork();
rsp.httpStatus = HttpStatusOk;
return REQ_READY_SEND;
}
/*! POST /api/<apikey>/touchlink/<id>/reset
\param req - request data
\param rsp - response data
\return REQ_READY_SEND
REQ_NOT_HANDLED
*/
int DeRestPluginPrivate::resetLight(const ApiRequest &req, ApiResponse &rsp)
{
/*
* - disconnect
* - start interpan mode
* - send interpan scan request
* - send interpan reset to factory new request
* - reconnect
*/
if (touchlinkState != TL_Idle)
{
rsp.httpStatus = HttpStatusServiceUnavailable;
return REQ_READY_SEND;
}
QString id = req.path[3];
touchlinkDevice.id.clear(); // mark as undefined
{ // search the device according to its id
std::vector<ScanResponse>::const_iterator i = touchlinkScanResponses.begin();
std::vector<ScanResponse>::const_iterator end = touchlinkScanResponses.end();
for (; i != end; ++i)
{
if (i->id == id)
{
touchlinkDevice = *i;
break;
}
}
}
if (touchlinkDevice.id.isEmpty())
{
rsp.httpStatus = HttpStatusNotFound;
return REQ_READY_SEND;
}
uint32_t transactionId = qrand();
if (transactionId == 0)
{
transactionId++;
}
touchlinkReq.setTransactionId(transactionId);
touchlinkAction = TouchlinkReset;
touchlinkChannel = touchlinkDevice.channel;
DBG_Printf(DBG_TLINK, "start touchlink reset for 0x%016llX\n", touchlinkDevice.address.ext());
touchlinkDisconnectNetwork();
rsp.httpStatus = HttpStatusOk;
return REQ_READY_SEND;
}
/*! Starts the interpan mode.
\param channel the channel which shall be used for interpan communication
*/
void DeRestPluginPrivate::startTouchlinkMode(uint8_t channel)
{
DBG_Printf(DBG_TLINK, "start interpan mode on channel %u\n", channel);
touchlinkChannel = channel;
touchlinkState = TL_StartingInterpanMode;
if (touchlinkCtrl->startInterpanMode(touchlinkChannel) != 0)
{
DBG_Printf(DBG_TLINK, "start interpan mode on channel %u failed\n", channel);
// abort and restore previous network state
touchlinkStartReconnectNetwork(TL_RECONNECT_NOW);
}
}
/*! Callback slot for the touchlink mode confirmation.
\param status tells if starting the touchlink mode was successful
*/
void DeRestPluginPrivate::startTouchlinkModeConfirm(deCONZ::TouchlinkStatus status)
{
DBG_Printf(DBG_TLINK, "start touchlink mode %s\n", (status == deCONZ::TouchlinkSuccess ? "success" : "failed"));
if (touchlinkState != TL_StartingInterpanMode)
{
return;
}
if (status != deCONZ::TouchlinkSuccess)
{
// abort and restore previous network state
touchlinkStartReconnectNetwork(TL_RECONNECT_NOW);
return;
}
if (touchlinkAction == TouchlinkScan)
{
sendTouchlinkScanRequest();
}
else if (touchlinkAction == TouchlinkIdentify)
{
// must be send prior to identify request because we need a valid transaction id
sendTouchlinkScanRequest();
}
else if (touchlinkAction == TouchlinkReset)
{
// must be send prior to identify request because we need a valid transaction id
sendTouchlinkScanRequest();
}
else
{
// abort and restore previous network state
touchlinkStartReconnectNetwork(TL_RECONNECT_NOW);
}
}
/*! Request to disconnect from network.
*/
void DeRestPluginPrivate::touchlinkDisconnectNetwork()
{
DBG_Assert(touchlinkState == TL_Idle);
if (touchlinkState != TL_Idle)
{
return;
}
DBG_Assert(apsCtrl != 0);
if (!apsCtrl)
{
return;
}
touchlinkNetworkDisconnectAttempts = NETWORK_ATTEMPS;
touchlinkNetworkConnectedBefore = gwRfConnectedExpected;
touchlinkState = TL_DisconnectingNetwork;
apsCtrl->setNetworkState(deCONZ::NotInNetwork);
touchlinkTimer->start(TL_DISCONNECT_CHECK_DELAY);
}
/*! Checks if network is disconnected to proceed with further actions.
*/
void DeRestPluginPrivate::checkTouchlinkNetworkDisconnected()
{
if (touchlinkState != TL_DisconnectingNetwork)
{
return;
}
if (touchlinkNetworkDisconnectAttempts > 0)
{
touchlinkNetworkDisconnectAttempts--;
}
if (isInNetwork())
{
if (touchlinkNetworkDisconnectAttempts == 0)
{
DBG_Printf(DBG_TLINK, "disconnect from network failed, abort touchlink action\n");
// even if we seem to be connected force a delayed reconnect attemp to
// prevent the case that the disconnect happens shortly after here
touchlinkStartReconnectNetwork(TL_RECONNECT_CHECK_DELAY);
}
else
{
DBG_Assert(apsCtrl != 0);
if (apsCtrl)
{
DBG_Printf(DBG_TLINK, "disconnect from network failed, try again\n");
apsCtrl->setNetworkState(deCONZ::NotInNetwork);
touchlinkTimer->start(TL_DISCONNECT_CHECK_DELAY);
}
else
{ // sanity
touchlinkState = TL_Idle;
}
}
return;
}
startTouchlinkMode(touchlinkChannel);
}
/*! Sends the touchlink scan request as broadcast.
*/
void DeRestPluginPrivate::sendTouchlinkScanRequest()
{
touchlinkReq.setChannel(touchlinkChannel);
touchlinkReq.setDstAddressMode(deCONZ::ApsNwkAddress);
touchlinkReq.dstAddress().setNwk(0xFFFF);
touchlinkReq.setPanId(0xFFFF);
touchlinkReq.setClusterId(0x1000);
touchlinkReq.setProfileId(ZLL_PROFILE_ID);
touchlinkReq.asdu().clear();
QDataStream stream(&touchlinkReq.asdu(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
uint8_t cmd = TL_CMD_SCAN_REQ;
uint8_t frameControl = deCONZ::ZclFCProfileCommand | deCONZ::ZclFCDirectionClientToServer;
uint8_t seq = touchlinkReq.transactionId();
uint8_t zigbeeInfo = 0x02; // 0x01 /* router */ | 0x04 /* rx on when idle */;
uint8_t zllInfo = 0x33;
stream << frameControl;
stream << seq;
stream << cmd;
stream << touchlinkReq.transactionId();
stream << zigbeeInfo;
stream << zllInfo;
touchlinkScanCount++;
DBG_Printf(DBG_TLINK, "send scan request TrId: 0x%08X\n", touchlinkReq.transactionId());
if (touchlinkCtrl->sendInterpanRequest(touchlinkReq) == 0)
{
touchlinkState = TL_SendingScanRequest;
}
else
{
DBG_Printf(DBG_TLINK, "touchlink send scan request failed\n");
// abort and restore previous network state
touchlinkStartReconnectNetwork(TL_RECONNECT_NOW);
return;
}
}
/*! Sends the touchlink identify request to a device.
*/
void DeRestPluginPrivate::sendTouchlinkIdentifyRequest()
{
touchlinkReq.setChannel(touchlinkChannel);
touchlinkReq.setDstAddressMode(deCONZ::ApsExtAddress);
touchlinkReq.dstAddress() = touchlinkDevice.address;
touchlinkReq.setPanId(touchlinkDevice.panid);
touchlinkReq.setClusterId(0x1000);
touchlinkReq.setProfileId(ZLL_PROFILE_ID);
touchlinkReq.asdu().clear();
QDataStream stream(&touchlinkReq.asdu(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
uint8_t cmd = TL_CMD_IDENTIFY_REQ;
uint8_t frameControl = deCONZ::ZclFCProfileCommand | deCONZ::ZclFCDirectionClientToServer;
uint8_t seq = touchlinkReq.transactionId();
uint16_t duration = 5;
stream << frameControl;
stream << seq;
stream << cmd;
stream << touchlinkReq.transactionId();
stream << duration;
DBG_Printf(DBG_TLINK, "send identify request TrId: 0x%08X\n", touchlinkReq.transactionId());
if (touchlinkCtrl->sendInterpanRequest(touchlinkReq) == 0)
{
touchlinkState = TL_SendingIdentifyRequest;
}
else
{
DBG_Printf(DBG_TLINK, "touchlink send identify request failed\n");
// abort and restore previous network state
touchlinkStartReconnectNetwork(TL_RECONNECT_NOW);
}
}
/*! Sends the touchlink reset request to a device.
*/
void DeRestPluginPrivate::sendTouchlinkResetRequest()
{
touchlinkReq.setChannel(touchlinkChannel);
touchlinkReq.setDstAddressMode(deCONZ::ApsExtAddress);
touchlinkReq.dstAddress() = touchlinkDevice.address;
touchlinkReq.setPanId(touchlinkDevice.panid);
touchlinkReq.setClusterId(0x1000);
touchlinkReq.setProfileId(ZLL_PROFILE_ID);
touchlinkReq.asdu().clear();
QDataStream stream(&touchlinkReq.asdu(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
uint8_t cmd = TL_CMD_RESET_TO_FACTORY_NEW_REQ;
uint8_t frameControl = deCONZ::ZclFCProfileCommand | deCONZ::ZclFCDirectionClientToServer;
uint8_t seq = touchlinkReq.transactionId();
stream << frameControl;
stream << seq;
stream << cmd;
stream << touchlinkReq.transactionId();
DBG_Printf(DBG_TLINK, "send reset request TrId: 0x%08X\n", touchlinkReq.transactionId());
if (touchlinkCtrl->sendInterpanRequest(touchlinkReq) == 0)
{
touchlinkState = TL_SendingResetRequest;
}
else
{
DBG_Printf(DBG_TLINK, "touchlink send reset request failed\n");
// abort and restore previous network state
touchlinkStartReconnectNetwork(TL_RECONNECT_NOW);
}
}
/*! Starts a delayed action based on current touchlink state.
*/
void DeRestPluginPrivate::touchlinkTimerFired()
{
switch (touchlinkState)
{
case TL_Idle:
break;
case TL_WaitScanResponses:
touchlinkScanTimeout();
break;
case TL_ReconnectNetwork:
touchlinkReconnectNetwork();
break;
case TL_DisconnectingNetwork:
checkTouchlinkNetworkDisconnected();
break;
case TL_SendingScanRequest:
sendTouchlinkScanRequest();
break;
default:
DBG_Printf(DBG_TLINK, "touchlinkTimerFired() unhandled state %d\n", touchlinkState);
break;
}
}
/*! Confirmation callback for a interpan request.
\param status tells if the request was sent
*/
void DeRestPluginPrivate::sendTouchlinkConfirm(deCONZ::TouchlinkStatus status)
{
if (status != deCONZ::TouchlinkSuccess)
{
DBG_Printf(DBG_TLINK, "touchlink confirm status %d for action %d\n", status, touchlinkAction);
}
if (touchlinkState == TL_SendingScanRequest)
{
switch (touchlinkAction)
{
case TouchlinkScan:
{
if (touchlinkScanCount > TL_SCAN_COUNT)
{
touchlinkState = TL_WaitScanResponses;
touchlinkTimer->start(TL_SCAN_WAIT_TIME);
}
else
{
touchlinkTimer->start(1);
}
}
break;
case TouchlinkIdentify:
case TouchlinkReset:
{
touchlinkState = TL_WaitScanResponses;
touchlinkTimer->start(TL_TRANSACTION_TIMEOUT);
}
break;
default:
{
DBG_Printf(DBG_TLINK, "unknown touchlink action: %d, abort\n", touchlinkAction);
// abort and restore previous network state
touchlinkStartReconnectNetwork(TL_RECONNECT_NOW);
}
break;
}
}
else if (touchlinkState == TL_WaitScanResponses)
{
// empty
}
else if ((touchlinkState == TL_SendingIdentifyRequest) ||
(touchlinkState == TL_SendingResetRequest))
{
if (status == deCONZ::TouchlinkSuccess)
{
// mark the reset node as not available
if (touchlinkState == TL_SendingResetRequest)
{
std::vector<LightNode>::iterator i = nodes.begin();
std::vector<LightNode>::iterator end = nodes.end();
for (; i != end; ++i)
{
if (i->address().ext() == touchlinkDevice.address.ext())
{
// TODO: remove the node from groups
i->item(RStateReachable)->setValue(false);
updateEtag(i->etag);
updateEtag(gwConfigEtag);
}
}
}
}
// finished go back to normal operationg state and reconnect to network
touchlinkStartReconnectNetwork(TL_RECONNECT_NOW);
}
else if (touchlinkState != TL_Idle)
{
DBG_Printf(DBG_TLINK, "touchlink send confirm in unexpected state: %d\n", touchlinkState);
}
}
/*! Timer callback than duration for scan responses expires.
*/
void DeRestPluginPrivate::touchlinkScanTimeout()
{
if (touchlinkState != TL_WaitScanResponses)
{
return;
}
if (touchlinkAction == TouchlinkReset || touchlinkAction == TouchlinkIdentify)
{
DBG_Printf(DBG_TLINK, "wait for scan response before reset/identify to fn timeout\n");
touchlinkStartReconnectNetwork(TL_RECONNECT_NOW);
return;
}
else if (touchlinkAction == TouchlinkScan)
{
if (touchlinkChannel < 26)
{
touchlinkChannel++;
touchlinkScanCount = 0;
startTouchlinkMode(touchlinkChannel);
}
else
{
DBG_Printf(DBG_TLINK, "scan finished found %u device(s)\n", (uint)touchlinkScanResponses.size());
touchlinkStartReconnectNetwork(TL_RECONNECT_NOW);
}
}
}
/*! Callback if interpan data like touchlink frames are received.
\param data the indication data
*/
void DeRestPluginPrivate::interpanDataIndication(const QByteArray &data)
{
if (touchlinkState == TL_Idle)
{
DBG_Printf(DBG_TLINK, "discard ipan frame in TL_Idle state\n");
return;
}
QDataStream stream(data);
stream.setByteOrder(QDataStream::LittleEndian);
uint16_t srcPanId;
quint64 srcAddress;
uint16_t dstPanId;
uint8_t dstAddressMode;
quint64 dstExtAddress; // if dstAddressMode is 0x3
uint16_t dstNwkAddress; // if dstAddressMode is 0x2 or 0x01
uint16_t profileId;
uint16_t clusterId;
uint8_t asduLength;
//uint8_t asdu[asduLength];
uint8_t lqi;
int8_t rssi;
stream >> srcPanId;
stream >> srcAddress;
stream >> dstPanId;
stream >> dstAddressMode;
if (dstAddressMode == 0x03)
{
stream >> dstExtAddress;
}
else
{
stream >> dstNwkAddress;
}
stream >> profileId;
stream >> clusterId;
stream >> asduLength;
QByteArray asdu;
for (uint i = 0; i < asduLength; i++)
{
uint8_t byte;
stream >> byte;
asdu.append(byte);
}
stream >> lqi;
stream >> rssi;
// check if ZLL specific
if ((profileId == ZLL_PROFILE_ID) && (clusterId == 0x1000) && (asdu.size() >= 3))
{
// uint8_t frameControl = asdu[0];
// uint8_t seq = asdu[1];
uint8_t cmd = asdu[2];
ScanResponse scanResponse;
if (cmd == TL_CMD_SCAN_RSP)
{
scanResponse.id = QString::number(touchlinkScanResponses.size() + 1);
scanResponse.address.setExt(srcAddress);
scanResponse.factoryNew = ((asdu[9] & FACTORY_NEW_FLAG) != 0);
scanResponse.channel = touchlinkChannel;
scanResponse.panid = srcPanId;
scanResponse.transactionId = touchlinkReq.transactionId();
scanResponse.rssi = rssi;
DBG_Printf(DBG_TLINK, "scan response %s, fn=%u, channel=%u rssi=%d TrId=0x%08X in state=%d action=%d\n",
qPrintable(scanResponse.address.toStringExt()),
scanResponse.factoryNew, touchlinkChannel, rssi, scanResponse.transactionId,
touchlinkState, touchlinkAction);
if (touchlinkAction == TouchlinkScan)
{
if (asdu.size() >= 9)
{
std::vector<ScanResponse>::iterator i = touchlinkScanResponses.begin();
std::vector<ScanResponse>::iterator end = touchlinkScanResponses.end();
// check if already known
for (; i != end; ++i)
{
if (i->address.ext() == srcAddress)
{
// update transaction id
i->transactionId = touchlinkReq.transactionId();
return;
}
}
touchlinkScanResponses.push_back(scanResponse);
}
}
else if (touchlinkAction == TouchlinkReset)
{
if (scanResponse.address.ext() == touchlinkDevice.address.ext())
{
touchlinkTimer->stop();
sendTouchlinkResetRequest();
}
}
else if (touchlinkAction == TouchlinkIdentify)
{
if (scanResponse.address.ext() == touchlinkDevice.address.ext())
{
touchlinkTimer->stop();
sendTouchlinkIdentifyRequest();
}
}
}
}
}
/*! Reconnect to previous network state, trying serveral times if necessary.
\param delay - the delay after which reconnecting shall be started
*/
void DeRestPluginPrivate::touchlinkStartReconnectNetwork(int delay)
{
touchlinkState = TL_ReconnectNetwork;
touchlinkNetworkReconnectAttempts = NETWORK_ATTEMPS;
DBG_Printf(DBG_TLINK, "start reconnect to network\n");
touchlinkTimer->stop();
if (delay > 0)
{
touchlinkTimer->start(delay);
}
else
{
touchlinkReconnectNetwork();
}
}
/*! Helper to reconnect to previous network state, trying serveral times if necessary.
*/
void DeRestPluginPrivate::touchlinkReconnectNetwork()
{
if (touchlinkState != TL_ReconnectNetwork)
{
return;
}
if (isInNetwork())
{
touchlinkState = TL_Idle;
DBG_Printf(DBG_TLINK, "reconnect network done\n");
return;
}
// respect former state
if (!touchlinkNetworkConnectedBefore)
{
touchlinkState = TL_Idle;
DBG_Printf(DBG_TLINK, "network was not connected before\n");
return;
}
if (touchlinkNetworkReconnectAttempts > 0)
{
if (apsCtrl->networkState() != deCONZ::Connecting)
{
touchlinkNetworkReconnectAttempts--;
if (apsCtrl->setNetworkState(deCONZ::InNetwork) != deCONZ::Success)
{
DBG_Printf(DBG_TLINK, "touchlink failed to reconnect to network try=%d\n", (NETWORK_ATTEMPS - touchlinkNetworkReconnectAttempts));
}
else
{
DBG_Printf(DBG_TLINK, "touchlink try to reconnect to network try=%d\n", (NETWORK_ATTEMPS - touchlinkNetworkReconnectAttempts));
}
}
touchlinkTimer->start(TL_RECONNECT_CHECK_DELAY);
}
else
{
touchlinkState = TL_Idle;
DBG_Printf(DBG_TLINK, "reconnect network failed\n");
}
}
/*! Returns true while touchlink is running.
*/
bool DeRestPluginPrivate::isTouchlinkActive()
{
return (touchlinkState != TL_Idle);
}