forked from dresden-elektronik/deconz-rest-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
de_otau.cpp
346 lines (285 loc) · 9.38 KB
/
de_otau.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
/*
* 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"
// de otau specific
#define OTAU_IMAGE_NOTIFY_CLID 0x0201
#define OTAU_QUERY_NEXT_IMAGE_REQUEST_CLID 0x0202
#define OTAU_QUERY_NEXT_IMAGE_RESPONSE_CLID 0x8202
#define OTAU_IMAGE_BLOCK_REQUEST_CLID 0x0203
#define OTAU_IMAGE_BLOCK_RESPONSE_CLID 0x8203
#define OTAU_REPORT_STATUS_CLID 0x0205
// std otau specific
#define OTAU_IMAGE_NOTIFY_CMD_ID 0x00
#define OTAU_QUERY_NEXT_IMAGE_REQUEST_CMD_ID 0x01
#define OTAU_IMAGE_BLOCK_REQUEST_CMD_ID 0x03
#define OTAU_IMAGE_PAGE_REQUEST_CMD_ID 0x04
#define OTAU_UPGRADE_END_REQUEST_CMD_ID 0x06
// artifical attribute ids for some OTAU related parameters
#define OTAU_SWVERSION_ID 0x1000
#define DONT_CARE_FILE_VERSION 0xFFFFFFFFUL
#define OTAU_IMAGE_TYPE_QJ 0x00 // Query jitter
#define OTAU_IMAGE_TYPE_QJ_MFC 0x01 // Query jitter, manufacturer code
#define OTAU_IMAGE_TYPE_QJ_MFC_IT 0x02 // Query jitter, manufacturer code, image type
#define OTAU_IMAGE_TYPE_QJ_MFC_IT_FV 0x03 // Query jitter, manufacturer code, image type, file version
#define OTAU_NOTIFY_INTERVAL (1000 * 60 * 30)
#define OTAU_IDLE_TICKS_NOTIFY 60 // seconds
#define OTAU_BUSY_TICKS 60 // seconds
/*! Inits the otau manager.
*/
void DeRestPluginPrivate::initOtau()
{
otauIdleTicks = 0;
otauBusyTicks = 0;
otauNotifyIter = 0;
otauIdleTotalCounter = 0;
otauUnbindIdleTotalCounter = 0;
otauNotifyDelay = deCONZ::appArgumentNumeric("--otau-notify-delay", OTAU_IDLE_TICKS_NOTIFY);
otauTimer = new QTimer(this);
otauTimer->setSingleShot(false);
connect(otauTimer, SIGNAL(timeout()),
this, SLOT(otauTimerFired()));
if (otauNotifyDelay > 0)
{
otauTimer->start(1000);
}
}
/*! Handler for incoming otau packets.
*/
void DeRestPluginPrivate::otauDataIndication(const deCONZ::ApsDataIndication &ind, const deCONZ::ZclFrame &zclFrame)
{
if ((ind.clusterId() == OTAU_CLUSTER_ID) && (zclFrame.commandId() == OTAU_QUERY_NEXT_IMAGE_REQUEST_CMD_ID))
{
LightNode *lightNode = getLightNodeForAddress(ind.srcAddress(), ind.srcEndpoint());
// extract software version from request
if (lightNode)
{
QDataStream stream(zclFrame.payload());
stream.setByteOrder(QDataStream::LittleEndian);
quint8 fieldControl;
quint16 manufacturerId;
quint16 imageType;
quint32 swVersion;
quint16 hwVersion;
stream >> fieldControl;
stream >> manufacturerId;
stream >> imageType;
stream >> swVersion;
if (fieldControl & 0x01)
{
stream >> hwVersion;
}
deCONZ::NumericUnion val = {0};
val.u32 = swVersion;
lightNode->setZclValue(NodeValue::UpdateByZclRead, OTAU_CLUSTER_ID, OTAU_SWVERSION_ID, val);
if (lightNode->swBuildId().isEmpty())
{
QString version;
version = QString::asprintf("%08X", swVersion);
lightNode->setSwBuildId(version);
lightNode->setNeedSaveDatabase(true);
updateEtag(lightNode->etag);
// read real sw build id
lightNode->setLastRead(READ_SWBUILD_ID, idleTotalCounter);
lightNode->enableRead(READ_SWBUILD_ID);
lightNode->setNextReadTime(READ_SWBUILD_ID, queryTime);
queryTime = queryTime.addSecs(5);
}
}
}
else if ((ind.clusterId() == OTAU_CLUSTER_ID) && (zclFrame.commandId() == OTAU_UPGRADE_END_REQUEST_CMD_ID))
{
LightNode *lightNode = getLightNodeForAddress(ind.srcAddress(), ind.srcEndpoint());
if (lightNode)
{
lightNode->setLastRead(READ_SWBUILD_ID, idleTotalCounter);
lightNode->enableRead(READ_SWBUILD_ID);
lightNode->setNextReadTime(READ_SWBUILD_ID, queryTime.addSecs(120));
}
}
else if ((ind.clusterId() == OTAU_CLUSTER_ID) && ((zclFrame.commandId() == OTAU_IMAGE_PAGE_REQUEST_CMD_ID) || (zclFrame.commandId() == OTAU_IMAGE_BLOCK_REQUEST_CMD_ID)))
{
// remember last activity time
otauIdleTotalCounter = idleTotalCounter;
LightNode *lightNode = getLightNodeForAddress(ind.srcAddress(), ind.srcEndpoint());
storeRecoverOnOffBri(lightNode);
}
if (!isOtauActive())
{
return;
}
if (((ind.profileId() == DE_PROFILE_ID) && (ind.clusterId() == OTAU_IMAGE_BLOCK_REQUEST_CLID)) ||
((ind.clusterId() == OTAU_CLUSTER_ID) && (zclFrame.commandId() == OTAU_IMAGE_BLOCK_REQUEST_CMD_ID)) ||
((ind.clusterId() == OTAU_CLUSTER_ID) && (zclFrame.commandId() == OTAU_IMAGE_PAGE_REQUEST_CMD_ID)))
{
if (otauIdleTicks > 0)
{
otauIdleTicks = 0;
}
if (otauBusyTicks <= 0)
{
updateEtag(gwConfigEtag);
}
otauBusyTicks = OTAU_BUSY_TICKS;
}
}
/*! Sends otau notifcation (std otau cluster) to \p node.
The node will then send a query next image request.
*/
void DeRestPluginPrivate::otauSendStdNotify(LightNode *node)
{
deCONZ::ApsDataRequest req;
deCONZ::ZclFrame zclFrame;
req.setProfileId(HA_PROFILE_ID);
req.setClusterId(OTAU_CLUSTER_ID);
req.setDstAddressMode(deCONZ::ApsExtAddress);
req.dstAddress().setExt(node->address().ext());
req.setDstEndpoint(node->haEndpoint().endpoint());
req.setSrcEndpoint(endpoint());
req.setState(deCONZ::FireAndForgetState);
zclFrame.setSequenceNumber(zclSeq++);
zclFrame.setCommandId(OTAU_IMAGE_NOTIFY_CMD_ID);
zclFrame.setFrameControl(deCONZ::ZclFCClusterCommand |
deCONZ::ZclFCDirectionServerToClient |
deCONZ::ZclFCDisableDefaultResponse);
{ // payload
QDataStream stream(&zclFrame.payload(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
uint8_t payloadType = 0x00; // query jitter
uint8_t queryJitter = 100;
stream << payloadType;
stream << queryJitter;
}
{ // ZCL frame
QDataStream stream(&req.asdu(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
zclFrame.writeToStream(stream);
}
if (apsCtrl && apsCtrl->apsdeDataRequest(req) != deCONZ::Success)
{
DBG_Printf(DBG_INFO, "otau failed to send image notify request\n");
}
}
/*! Returns true if otau is busy with uploading data.
*/
bool DeRestPluginPrivate::isOtauBusy()
{
if (isInNetwork() && isOtauActive() && (otauBusyTicks > 0))
{
return true;
}
return false;
}
/*! Returns true if otau is activated.
*/
bool DeRestPluginPrivate::isOtauActive()
{
if (apsCtrl)
{
return apsCtrl->getParameter(deCONZ::ParamOtauActive) == 1;
}
return false;
}
int DeRestPluginPrivate::otauLastBusyTimeDelta() const
{
if (otauIdleTotalCounter == 0)
{
return INT_MAX; // not valid
}
if (idleTotalCounter >= otauIdleTotalCounter)
{
return idleTotalCounter - otauIdleTotalCounter;
}
return INT_MAX;
}
/*! Unicasts otau notify packets to the nodes.
*/
void DeRestPluginPrivate::otauTimerFired()
{
if (!isOtauActive())
{
return;
}
if (otauNotifyDelay == 0)
{
return;
}
if (!isInNetwork())
{
return;
}
if (nodes.empty())
{
return;
}
if (otauIdleTicks < INT_MAX)
{
otauIdleTicks++;
}
if (otauBusyTicks > 0)
{
otauBusyTicks--;
if (otauBusyTicks == 0)
{
updateEtag(gwConfigEtag);
}
}
if (otauIdleTicks < otauNotifyDelay)
{
return;
}
if (otauNotifyIter >= nodes.size())
{
otauNotifyIter = 0;
}
// dont do anything if sensors are triggering group commands
if ((idleTotalCounter - sensorIndIdleTotalCounter) < (60 * 10))
{
return;
}
LightNode *lightNode = &nodes[otauNotifyIter];
otauNotifyIter++;
if (!lightNode->isAvailable() &&
lightNode->otauClusterId() != OTAU_CLUSTER_ID)
{
return;
}
// filter vendor
if (lightNode->manufacturerCode() != VENDOR_DDEL)
{
return;
}
// whitelist active notify to some devices
if (lightNode->modelId().startsWith("FLS-NB"))
{ }
else if (lightNode->modelId().startsWith("FLS-PP3"))
{ }
else if (lightNode->modelId().startsWith("FLS-A"))
{ }
else
{
return;
}
QDateTime now = QDateTime::currentDateTime();
NodeValue &val = lightNode->getZclValue(OTAU_CLUSTER_ID, OTAU_SWVERSION_ID);
if (val.updateType == NodeValue::UpdateByZclRead)
{
if (val.timestamp.isValid() && val.timestamp.secsTo(now) < OTAU_NOTIFY_INTERVAL)
{
return;
}
if (val.timestampLastReadRequest.isValid() && val.timestampLastReadRequest.secsTo(now) < OTAU_NOTIFY_INTERVAL)
{
return;
}
val.timestampLastReadRequest = now;
}
otauSendStdNotify(lightNode);
otauIdleTicks = 0;
}