forked from dresden-elektronik/deconz-rest-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
device_tick.cpp
251 lines (220 loc) · 6.54 KB
/
device_tick.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
/*
* Copyright (c) 2021 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 <QElapsedTimer>
#include <QTimer>
#include <deconz/dbg_trace.h>
#include "event.h"
#include "resource.h"
#include "device_tick.h"
#define DEV_TICK_BOOT_TIME 8000
#define TICK_INTERVAL_JOIN 500
#define TICK_INTERVAL_IDLE 1000
extern int DEV_ApsQueueSize();
struct JoinDevice
{
DeviceKey deviceKey;
quint8 macCapabilities;
};
static const char *RLocal = nullptr;
typedef void (*DT_StateHandler)(DeviceTickPrivate *d, const Event &event);
static void DT_StateInit(DeviceTickPrivate *d, const Event &event);
static void DT_StateJoin(DeviceTickPrivate *d, const Event &event);
static void DT_StateIdle(DeviceTickPrivate *d, const Event &event);
class DeviceTickPrivate
{
public:
DT_StateHandler stateHandler = DT_StateInit;
std::vector<JoinDevice> joinDevices;
DeviceTick *q = nullptr;
QTimer *timer = nullptr;
size_t devIter = 0;
const DeviceContainer *devices = nullptr;
};
/*! Constructor.
*/
DeviceTick::DeviceTick(const DeviceContainer &devices, QObject *parent) :
QObject(parent),
d(new DeviceTickPrivate)
{
d->devices = &devices;
d->q = this;
d->timer = new QTimer(this);
d->timer->setSingleShot(true);
connect(d->timer, &QTimer::timeout, this, &DeviceTick::timoutFired);
d->timer->start(DEV_TICK_BOOT_TIME);
}
/*! Destructor.
*/
DeviceTick::~DeviceTick()
{
Q_ASSERT(d);
delete d;
d = nullptr;
}
/*! Public event entry.
*/
void DeviceTick::handleEvent(const Event &event)
{
d->stateHandler(d, event);
}
/*! State timer callback.
*/
void DeviceTick::timoutFired()
{
d->stateHandler(d, Event(RLocal, REventStateTimeout, 0));
}
/*! Sets a new DeviceTick state.
The events REventStateLeave and REventStateEnter will be triggered accordingly.
*/
static void DT_SetState(DeviceTickPrivate *d, DT_StateHandler state)
{
if (d->stateHandler != state)
{
d->stateHandler(d, Event(RLocal, REventStateLeave, 0));
d->stateHandler = state;
d->stateHandler(d, Event(RLocal, REventStateEnter, 0));
}
}
/*! Starts the state timer. Free function to be mocked by test code.
*/
static void DT_StartTimer(DeviceTickPrivate *d, int timeoutMs)
{
d->timer->start(timeoutMs);
}
/*! Stops the state timer. Free function to be mocked by test code.
*/
static void DT_StopTimer(DeviceTickPrivate *d)
{
d->timer->stop();
}
/*! Initial state to wait DEV_TICK_BOOT_TIME seconds before starting normal operation.
*/
static void DT_StateInit(DeviceTickPrivate *d, const Event &event)
{
if (event.resource() == RLocal && event.what() == REventStateTimeout)
{
DBG_Printf(DBG_INFO, "DEV Tick.Init: booted after %lld seconds\n", DEV_TICK_BOOT_TIME);
DT_SetState(d, DT_StateIdle);
}
}
/*! Emits REventPoll to the next device in DT_StateIdle.
*/
static void DT_PollNextIdleDevice(DeviceTickPrivate *d)
{
const auto devCount = d->devices->size();
if (devCount == 0)
{
return;
}
d->devIter %= devCount;
const auto &device = d->devices->at(d->devIter);
Q_ASSERT(device);
if (device->reachable())
{
emit d->q->eventNotify(Event(device->prefix(), REventPoll, 0, device->key()));
}
d->devIter++;
}
/*! This state is active while Permit Join is disabled for normal idle operation.
It walks over all Devices in TICK_INTERVAL_IDLE spacing.
The state transitions to DT_StateJoin when REventPermitjoinEnabled is received.
*/
static void DT_StateIdle(DeviceTickPrivate *d, const Event &event)
{
if (event.what() == REventPermitjoinEnabled)
{
DT_SetState(d, DT_StateJoin);
}
else if (event.resource() == RLocal)
{
if (event.what() == REventStateTimeout)
{
if (DEV_ApsQueueSize() < 4)
{
DT_PollNextIdleDevice(d);
}
DT_StartTimer(d, TICK_INTERVAL_IDLE);
}
else if (event.what() == REventStateEnter)
{
DT_StartTimer(d, TICK_INTERVAL_IDLE);
}
else if (event.what() == REventStateLeave)
{
DT_StopTimer(d);
}
}
}
/*! Adds a joining device entry to the queue if not already present.
*/
static void DT_RegisterJoiningDevice(DeviceTickPrivate *d, DeviceKey deviceKey, quint8 macCapabilities)
{
Q_ASSERT(deviceKey != 0); // if this triggers we have problems elsewhere
auto i = std::find_if(d->joinDevices.cbegin(), d->joinDevices.cend(), [&deviceKey](const JoinDevice &dev)
{
return deviceKey == dev.deviceKey;
});
if (i == d->joinDevices.cend())
{
JoinDevice dev;
dev.deviceKey = deviceKey;
dev.macCapabilities = macCapabilities;
d->joinDevices.push_back(dev);
DBG_Printf(DBG_INFO, "DEV Tick: fast poll 0x%016llX, mac capabilities: 0x%02X\n", deviceKey, macCapabilities);
}
}
/*! Emits REventPoll to the next device in DT_StateJoin.
*/
static void DT_PollNextJoiningDevice(DeviceTickPrivate *d)
{
if (d->joinDevices.empty())
{
return;
}
d->devIter %= d->joinDevices.size();
Q_ASSERT(d->devIter < d->joinDevices.size());
const JoinDevice &device = d->joinDevices.at(d->devIter);
emit d->q->eventNotify(Event(RDevices, REventAwake, 0, device.deviceKey));
d->devIter++;
}
/*! This state is active while Permit Join is enabled.
When a REventDeviceAnnounce event is received, the device is added to a joining
queue and processed exclusivly and quickly.
The state transitions to DT_StateIdle when Permit Join is disabled.
*/
static void DT_StateJoin(DeviceTickPrivate *d, const Event &event)
{
if (event.what() == REventPermitjoinDisabled)
{
DT_SetState(d, DT_StateIdle);
}
else if (event.what() == REventDeviceAnnounce)
{
DBG_Printf(DBG_INFO, "DEV Tick.Join: %s\n", event.what());
DT_RegisterJoiningDevice(d, event.deviceKey(), static_cast<quint8>(event.num()));
}
else if (event.resource() == RLocal)
{
if (event.what() == REventStateTimeout)
{
DT_PollNextJoiningDevice(d);
DT_StartTimer(d, TICK_INTERVAL_JOIN);
}
else if (event.what() == REventStateEnter)
{
DT_StartTimer(d, TICK_INTERVAL_JOIN);
}
else if (event.what() == REventStateLeave)
{
DT_StopTimer(d);
d->joinDevices.clear();
}
}
}