forked from dresden-elektronik/deconz-rest-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
change_channel.cpp
539 lines (464 loc) · 15.3 KB
/
change_channel.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
/*
* 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 "de_web_plugin_private.h"
#define CC_CHANNELCHANGE_WAIT_TIME 1000
#define CC_CHANNELCHANGE_WAIT_CONFIRM_TIME 10000
#define CC_CHANNELCHANGE_VERIFY_TIME 1000
#define CC_DISCONNECT_CHECK_DELAY 100
#define NETWORK_ATTEMPS 10
#define CC_RECONNECT_CHECK_DELAY 5000
#define CC_RECONNECT_NOW 100
/*! Init the change channel api and helpers.
*/
void DeRestPluginPrivate::initChangeChannelApi()
{
channelChangeState = CC_Idle;
ccRetries = 0;
channelchangeTimer = new QTimer(this);
channelchangeTimer->setSingleShot(true);
connect(channelchangeTimer, SIGNAL(timeout()),
this, SLOT(channelchangeTimerFired()));
QTimer *wdTimer = new QTimer(this);
wdTimer->setSingleShot(false);
connect(wdTimer, SIGNAL(timeout()), this, SLOT(networkWatchdogTimerFired()));
wdTimer->start(10000);
}
/*! Starts the whole channel changing process if connected.
\param channel - user input channel
\return true if connected.
*/
bool DeRestPluginPrivate::startChannelChange(quint8 channel)
{
if (!isInNetwork())
{
return false;
}
ccRetries = 0;
gwZigbeeChannel = channel;
queSaveDb(DB_CONFIG, DB_SHORT_SAVE_DELAY);
if (channelChangeState != CC_Idle)
{
DBG_Printf(DBG_INFO, "channel change in progress.\n");
return true;
}
channelChangeState = CC_Verify_Channel;
DBG_Printf(DBG_INFO_L2, "ChannelChangeState: CC_verify_Channel\n");
channelchangeTimer->start(CC_CHANNELCHANGE_VERIFY_TIME);
return true;
}
/*! Check if user input channel equals gateway channel.
\param channel - user input channel
\return true if equal
*/
bool DeRestPluginPrivate::verifyChannel(quint8 channel)
{
DBG_Assert(apsCtrl != 0);
if (!apsCtrl)
{
return false;
}
if (!isInNetwork())
{
return false;
}
quint8 currentChannel = apsCtrl->getParameter(deCONZ::ParamCurrentChannel);
quint64 apsUseExtPanid = apsCtrl->getParameter(deCONZ::ParamApsUseExtendedPANID);
quint64 tcAddress = apsCtrl->getParameter(deCONZ::ParamTrustCenterAddress);
quint64 macAddress = apsCtrl->getParameter(deCONZ::ParamMacAddress);
quint8 deviceType = apsCtrl->getParameter(deCONZ::ParamDeviceType);
bool ok = true;
if (currentChannel != channel)
{
ok = false;
}
if (deviceType == deCONZ::Coordinator)
{
if (apsUseExtPanid != 0)
{
ok = false;
}
else if (tcAddress != macAddress)
{
ok = false;
}
}
if (ok)
{
DBG_Printf(DBG_INFO, "network configuration verified!\n");
return true;
}
else
{
DBG_Printf(DBG_INFO, "network configuration NOT verified!\n");
return false;
}
}
/*! Sends request to gateway to change channel
\param channel - user input channel
*/
void DeRestPluginPrivate::changeChannel(quint8 channel)
{
if (!apsCtrl)
{
}
else if ((gwDeviceAddress.ext() & deMacPrefix) != deMacPrefix)
{
// require valid mac address
}
else if (ccRetries < 3)
{
DBG_Assert(channel >= 11 && channel <= 26);
if (apsCtrl && (channel >= 11) && (channel <= 26))
{
uint8_t nwkUpdateId = (apsCtrl->getParameter(deCONZ::ParamNetworkUpdateId));
if (nwkUpdateId < 255)
{
nwkUpdateId++;
}
else if (nwkUpdateId == 255)
{
nwkUpdateId = 1;
}
const quint8 zdpSeq = (qrand() % 255);
const quint32 scanChannels = (1 << static_cast<uint>(channel));
const quint8 scanDuration = 0xfe; //special value = channel change
DBG_Printf(DBG_INFO, "change channel with nwkUpdateId = %u\n", nwkUpdateId);
apsCtrl->setParameter(deCONZ::ParamCurrentChannel, channel);
apsCtrl->setParameter(deCONZ::ParamNetworkUpdateId, nwkUpdateId);
deCONZ::ApsDataRequest req;
req.setTxOptions(nullptr);
req.setDstEndpoint(ZDO_ENDPOINT);
req.setDstAddressMode(deCONZ::ApsNwkAddress);
req.dstAddress().setNwk(deCONZ::BroadcastRxOnWhenIdle);
req.setProfileId(ZDP_PROFILE_ID);
req.setClusterId(ZDP_MGMT_NWK_UPDATE_REQ_CLID);
req.setSrcEndpoint(ZDO_ENDPOINT);
req.setRadius(0);
QDataStream stream(&req.asdu(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
stream << zdpSeq;
stream << scanChannels;
stream << scanDuration;
stream << nwkUpdateId;
if (apsCtrl && apsCtrl->apsdeDataRequest(req) == deCONZ::Success)
{
channelChangeApsRequestId = req.id();
DBG_Printf(DBG_INFO, "change channel to %d, channel mask = 0x%08lX\n", channel, scanChannels);
channelChangeState = CC_WaitConfirm;
channelchangeTimer->start(CC_CHANNELCHANGE_WAIT_CONFIRM_TIME);
DBG_Printf(DBG_INFO_L2, "ChannelChangeState: CC_WaitConfirm\n");
return;
}
else
{
DBG_Printf(DBG_ERROR, "cant send change channel\n");
}
}
channelChangeState = CC_Verify_Channel;
DBG_Printf(DBG_INFO_L2, "ChannelChangeState: CC_verify_Channel\n");
channelchangeTimer->start(CC_CHANNELCHANGE_VERIFY_TIME);
return;
}
if (apsCtrl && isInNetwork())
{
if (gwZigbeeChannel != apsCtrl->getParameter(deCONZ::ParamCurrentChannel))
{
}
}
ccRetries = 0;
channelChangeState = CC_Idle;
DBG_Printf(DBG_INFO_L2, "ChannelChangeState: CC_Idle\n");
DBG_Printf(DBG_INFO, "channel change not successful.\n");
return;
}
/*! Handle confirmation of ZDP channel change request.
\param success true on success
*/
void DeRestPluginPrivate::channelChangeSendConfirm(bool success)
{
channelchangeTimer->stop();
if (channelChangeState == CC_WaitConfirm)
{
if (success)
{
channelChangeDisconnectNetwork();
}
else
{
channelChangeState = CC_Verify_Channel;
DBG_Printf(DBG_INFO_L2, "ChannelChangeState: CC_verify_Channel\n");
channelchangeTimer->start(CC_CHANNELCHANGE_VERIFY_TIME);
}
}
}
/*! Request to disconnect from network.
*/
void DeRestPluginPrivate::channelChangeDisconnectNetwork()
{
DBG_Assert(channelChangeState == CC_WaitConfirm);
if (channelChangeState != CC_WaitConfirm)
{
return;
}
DBG_Assert(apsCtrl != 0);
if (!apsCtrl)
{
return;
}
ccNetworkDisconnectAttempts = NETWORK_ATTEMPS;
ccNetworkConnectedBefore = gwRfConnectedExpected;
channelChangeState = CC_DisconnectingNetwork;
DBG_Printf(DBG_INFO_L2, "ChannelChangeState: CC_DisconnectingNetwork\n");
apsCtrl->setNetworkState(deCONZ::NotInNetwork);
channelchangeTimer->start(CC_DISCONNECT_CHECK_DELAY);
}
/*! Checks if network is disconnected to proceed with further actions.
*/
void DeRestPluginPrivate::checkChannelChangeNetworkDisconnected()
{
if (channelChangeState != CC_DisconnectingNetwork)
{
return;
}
if (ccNetworkDisconnectAttempts > 0)
{
ccNetworkDisconnectAttempts--;
}
if (isInNetwork())
{
if (ccNetworkDisconnectAttempts == 0)
{
DBG_Printf(DBG_INFO, "disconnect from network failed.\n");
// even if we seem to be connected force a delayed reconnect attemp to
// prevent the case that the disconnect happens shortly after here
channelChangeStartReconnectNetwork(CC_RECONNECT_CHECK_DELAY);
}
else
{
DBG_Assert(apsCtrl != 0);
if (apsCtrl)
{
DBG_Printf(DBG_INFO, "disconnect from network failed, try again\n");
apsCtrl->setNetworkState(deCONZ::NotInNetwork);
channelchangeTimer->start(CC_DISCONNECT_CHECK_DELAY);
}
else
{ // sanity
channelChangeState = CC_Idle;
DBG_Printf(DBG_INFO_L2, "ChannelChangeState: CC_Idle\n");
}
}
return;
}
channelChangeStartReconnectNetwork(CC_RECONNECT_NOW);
}
/*! Reconnect to previous network state, trying serveral times if necessary.
\param delay - the delay after which reconnecting shall be started
*/
void DeRestPluginPrivate::channelChangeStartReconnectNetwork(int delay)
{
channelChangeState = CC_ReconnectNetwork;
DBG_Printf(DBG_INFO_L2, "ChannelChangeState: CC_ReconnectNetwork\n");
ccNetworkReconnectAttempts = NETWORK_ATTEMPS;
DBG_Printf(DBG_INFO, "start reconnect to network\n");
channelchangeTimer->stop();
if (delay > 0)
{
channelchangeTimer->start(delay);
}
else
{
channelChangeReconnectNetwork();
}
}
/*! Helper to reconnect to previous network state, trying serveral times if necessary.
*/
void DeRestPluginPrivate::channelChangeReconnectNetwork()
{
if (channelChangeState != CC_ReconnectNetwork)
{
return;
}
if (isInNetwork())
{
channelChangeState = CC_Verify_Channel;
DBG_Printf(DBG_INFO_L2, "ChannelChangeState: CC_verify_Channel\n");
channelchangeTimer->start(CC_CHANNELCHANGE_VERIFY_TIME);
DBG_Printf(DBG_INFO, "reconnect network done\n");
return;
}
// respect former state
if (!ccNetworkConnectedBefore)
{
channelChangeState = CC_Idle;
DBG_Printf(DBG_INFO_L2, "ChannelChangeState: CC_Idle\n");
DBG_Printf(DBG_INFO, "network was not connected before\n");
return;
}
if (ccNetworkReconnectAttempts > 0)
{
if (apsCtrl->networkState() != deCONZ::Connecting)
{
ccNetworkReconnectAttempts--;
const quint8 deviceType = apsCtrl->getParameter(deCONZ::ParamDeviceType);
if (deviceType == deCONZ::Coordinator)
{
apsCtrl->setParameter(deCONZ::ParamApsUseExtendedPANID, 0); // will become mac address
apsCtrl->setParameter(deCONZ::ParamTrustCenterAddress, gwDeviceAddress.ext());
apsCtrl->setParameter(deCONZ::ParamStaticNwkAddress, false);
apsCtrl->setParameter(deCONZ::ParamNwkAddress, 0);
}
if (apsCtrl->setNetworkState(deCONZ::InNetwork) != deCONZ::Success)
{
DBG_Printf(DBG_INFO, "failed to reconnect to network try=%d\n", (NETWORK_ATTEMPS - ccNetworkReconnectAttempts));
}
else
{
DBG_Printf(DBG_INFO, "try to reconnect to network try=%d\n", (NETWORK_ATTEMPS - ccNetworkReconnectAttempts));
}
}
channelchangeTimer->start(CC_RECONNECT_CHECK_DELAY);
}
else
{
channelChangeState = CC_Idle;
DBG_Printf(DBG_INFO_L2, "ChannelChangeState: CC_Idle\n");
DBG_Printf(DBG_INFO, "reconnect network failed\n");
}
}
/*! Checks if network uses parameters it's suppoesed to be.
*/
void DeRestPluginPrivate::networkWatchdogTimerFired()
{
if (!apsCtrl || channelChangeState != CC_Idle)
{
return;
}
if (!isInNetwork())
{
return;
}
if (saveDatabaseItems & DB_NOSAVE)
{
return; // deCONZ will restart shortly
}
quint8 channel = apsCtrl->getParameter(deCONZ::ParamCurrentChannel);
quint32 channelMask = apsCtrl->getParameter(deCONZ::ParamChannelMask);
quint64 apsUseExtPanid = apsCtrl->getParameter(deCONZ::ParamApsUseExtendedPANID);
quint64 tcAddress = apsCtrl->getParameter(deCONZ::ParamTrustCenterAddress);
quint64 macAddress = apsCtrl->getParameter(deCONZ::ParamMacAddress);
quint8 deviceType = apsCtrl->getParameter(deCONZ::ParamDeviceType);
if (gwZigbeeChannel == 0)
{
if (channel >= 11 && channel <= 26)
{
gwZigbeeChannel = channel;
queSaveDb(DB_CONFIG, DB_SHORT_SAVE_DELAY);
}
}
if (channel < 11 || channel > 26)
{
DBG_Printf(DBG_INFO, "invalid current channel %u (TODO)\n", channel);
return;
}
if (channelMask && (channelMask & (1 << channel)) == 0)
{
DBG_Printf(DBG_INFO, "channel %u does not match channel mask 0x%08X (TODO)\n", channel, channelMask);
}
if (gwZigbeeChannel == 0)
{
DBG_Printf(DBG_INFO, "invalid gwZigbeeChannel %u (TODO)\n", gwZigbeeChannel);
return;
}
else if (deviceType != deCONZ::Coordinator)
{
DBG_Printf(DBG_INFO, "unsupported device type %u (TODO)\n", deviceType);
return;
}
else if ((macAddress & deMacPrefix) != deMacPrefix) // only support our mac address
{
DBG_Printf(DBG_INFO, "invalid mac address 0x%016llX\n", macAddress);
return;
}
else if (gwZigbeeChannel < 11 || gwZigbeeChannel > 26)
{
DBG_Assert(0); // should never happen
return;
}
bool needCheck = false;
if (channel != gwZigbeeChannel)
{
needCheck = true;
DBG_Printf(DBG_INFO, "channel is %u but should be %u, start channel change\n", channel, gwZigbeeChannel);
}
else if (deviceType == deCONZ::Coordinator)
{
if (apsUseExtPanid != 0)
{
needCheck = true;
DBG_Printf(DBG_INFO, "apsUseExtPanid is 0x%016llX but should be 0, start reconfiguration\n", apsUseExtPanid);
}
if (tcAddress != macAddress)
{
needCheck = true;
DBG_Printf(DBG_INFO, "tcAddress is 0x%016llX but should be 0x%016llX, start reconfiguration\n", tcAddress, macAddress);
}
if (needCheck)
{
gwDeviceAddress.setExt(macAddress);
gwDeviceAddress.setNwk(0x0000);
}
}
if (needCheck)
{
//startChannelChange(gwZigbeeChannel);
DBG_Printf(DBG_INFO, "Skip automatic channel change, TODO warn user\n");
}
}
/*! Starts a delayed action based on current channelchange state.
*/
void DeRestPluginPrivate::channelchangeTimerFired()
{
switch (channelChangeState)
{
case CC_Idle:
break;
case CC_Verify_Channel:
if (!verifyChannel(gwZigbeeChannel))
{
channelChangeState = CC_Change_Channel;
DBG_Printf(DBG_INFO_L2, "ChannelChangeState: CC_Change_Channel\n");
channelchangeTimer->start(CC_CHANNELCHANGE_WAIT_TIME);
}
else
{
channelChangeState = CC_Idle;
DBG_Printf(DBG_INFO_L2, "ChannelChangeState: CC_Idle\n");
}
break;
case CC_Change_Channel:
ccRetries++;
changeChannel(gwZigbeeChannel);
break;
case CC_ReconnectNetwork:
channelChangeReconnectNetwork();
break;
case CC_DisconnectingNetwork:
checkChannelChangeNetworkDisconnected();
break;
case CC_WaitConfirm:
DBG_Printf(DBG_INFO, "channel change not successful.\n");
channelChangeState = CC_Idle;
break;
default:
DBG_Printf(DBG_INFO, "channelChangeTimerFired() unhandled state %d\n", channelChangeState);
break;
}
}