forked from dresden-elektronik/deconz-rest-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate_change.cpp
373 lines (313 loc) · 9.71 KB
/
state_change.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
/*
* 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 "device_descriptions.h"
#include "resource.h"
#include "state_change.h"
#define ONOFF_CLUSTER_ID 0x0006
#define ONOFF_COMMAND_OFF 0x00
#define ONOFF_COMMAND_ON 0x01
#define ONOFF_COMMAND_OFF_WITH_EFFECT 0x040
quint8 zclNextSequenceNumber(); // todo defined in de_web_plugin_private.h
StateChange::StateChange(StateChange::State initialState, StateChangeFunction_t fn, quint8 dstEndpoint) :
m_state(initialState),
m_changeFunction(fn),
m_dstEndpoint(dstEndpoint)
{
Q_ASSERT(initialState == StateCallFunction || initialState == StateWaitSync);
Q_ASSERT(fn);
m_stateTimer.start();
m_changeTimer.start();
}
/*! Tick function for the inner state machine.
Is called from the Device state machine on certain events.
\returns 0 when nothing was sent, 1 if an APS request was enqueued
*/
int StateChange::tick(uint64_t extAddr, Resource *r, deCONZ::ApsController *apsCtrl)
{
int result = 0;
if (m_state == StateFinished || m_state == StateFailed)
{
return result;
}
Q_ASSERT(m_stateTimer.isValid());
Q_ASSERT(m_changeTimer.isValid());
const char *uniqueId = "";
{
const ResourceItem *item = r->item(RAttrUniqueId);
if (item)
{
uniqueId = item->toCString();
}
}
if (m_state == StateWaitSync)
{
if (m_stateTimer.elapsed() > m_stateTimeoutMs)
{
m_state = StateCallFunction;
for (auto &i : m_items)
{
if (i.verified == VerifyUnknown) // didn't receive a ZCL attribute read or report command
{
m_state = StateRead;
break;
}
}
}
}
if (m_state == StateFailed)
{
}
else if (m_changeTimeoutMs > 0 && m_changeTimer.elapsed() > m_changeTimeoutMs)
{
m_state = StateFailed;
}
else if (DA_ApsUnconfirmedRequests() > 5)
{
// wait
}
else if (m_state == StateCallFunction && m_changeFunction)
{
DBG_Printf(DBG_INFO, "SC tick --> StateCallFunction\n");
if (m_changeFunction(r, this, apsCtrl) == 0)
{
for (auto &i : m_items)
{
if (i.verified == VerifyNotSynced)
{
i.verified = VerifyUnknown; // read again
}
}
m_stateTimer.start();
m_state = StateWaitSync;
result = 1;
}
}
else if (m_state == StateRead && DA_ApsUnconfirmedRequestsForExtAddress(extAddr) == 0)
{
ResourceItem *item = nullptr;
for (auto &i : m_items)
{
if (i.verified == VerifyUnknown)
{
item = r->item(i.suffix);
break;
}
}
m_state = StateFailed;
m_readResult = {};
if (item)
{
const auto &ddfItem = DDF_GetItem(item);
const auto readFunction = DA_GetReadFunction(ddfItem.readParameters);
if (readFunction && ddfItem.isValid())
{
m_readResult = readFunction(r, item, apsCtrl, ddfItem.readParameters);
if (m_readResult.isEnqueued)
{
DBG_Printf(DBG_INFO, "SC tick --> StateRead %s, %s\n", item->descriptor().suffix, uniqueId);
result = 1;
}
m_stateTimer.start();
m_state = StateWaitSync;
}
}
}
return result;
}
/*! Should be called when the item was set by a ZCL read or report attribute command.
When all items are verified the StateChange::state() is set to StateFinished.
*/
void StateChange::verifyItemChange(const ResourceItem *item)
{
Q_ASSERT(item);
size_t syncedItems = 0;
if (item->valueSource() != ResourceItem::SourceDevice)
{
return;
}
for (auto &i : m_items)
{
if (i.suffix == item->descriptor().suffix)
{
if (i.targetValue == item->toVariant())
{
i.verified = VerifySynced;
DBG_Printf(DBG_INFO, "SC %s: synced\n", i.suffix);
}
else
{
i.verified = VerifyNotSynced;
DBG_Printf(DBG_INFO, "SC %s: not synced\n", i.suffix);
}
}
if (i.verified == VerifySynced)
{
syncedItems++;
}
}
if (syncedItems == m_items.size() && m_state != StateFinished)
{
m_state = StateFinished;
DBG_Printf(DBG_INFO, "SC --> StateFinished\n");
}
}
/*! Adds a target value. */
void StateChange::addTargetValue(const char *suffix, const QVariant &value)
{
if (value.isValid())
{
m_items.push_back({suffix, value});
}
else
{
DBG_Printf(DBG_ERROR, "SC add invalid traget value for: %s\n", suffix);
}
}
/*! Adds a parameter. If the parameter already exsits it will be replaced. */
void StateChange::addParameter(const QString &name, const QVariant &value)
{
auto i = std::find_if(m_parameters.begin(), m_parameters.end(), [name](const Param &x){
return x.name == name;
});
if (i != m_parameters.end())
{
i->value = value;
}
else
{
m_parameters.push_back({name, value});
}
}
bool StateChange::operator==(const StateChange &other) const
{
if (m_changeFunction == other.m_changeFunction && m_items.size() == other.m_items.size())
{
for (size_t i = 0; i < m_items.size(); i++)
{
if (m_items[i].suffix != other.m_items[i].suffix)
{
return false;
}
}
return true;
}
return false;
}
/*! Calls the ZCL write function of item(s) to write target value(s).
\returns 0 - if the command has been enqueued, or a negative number on failure.
*/
int SC_WriteZclAttribute(const Resource *r, const StateChange *stateChange, deCONZ::ApsController *apsCtrl)
{
Q_ASSERT(r);
Q_ASSERT(stateChange);
Q_ASSERT(apsCtrl);
int written = 0;
for (const auto &i : stateChange->items())
{
const auto *item = r->item(i.suffix);
if (!item)
{
return -1;
}
const auto ddfItem = DDF_GetItem(item);
if (ddfItem.writeParameters.isNull())
{
return -2;
}
const auto fn = DA_GetWriteFunction(ddfItem.writeParameters);
if (!fn)
{
return -3;
}
// create a copy since item is const
ResourceItem copy(item->descriptor());
copy.setValue(i.targetValue);
if (!fn(r, ©, apsCtrl, ddfItem.writeParameters))
{
return -4;
}
written++;
}
return written > 0 ? 0 : -5;
}
/*! Sends a ZCL command to the on/off cluster.
StateChange::parameters() -> "cmd"
ONOFF_COMMAND_ON
ONOFF_COMMAND_OFF
ONOFF_COMMAND_OFF_WITH_EFFECT
\returns 0 - if the command has been enqueued, or a negative number on failure.
*/
int SC_SetOnOff(const Resource *r, const StateChange *stateChange, deCONZ::ApsController *apsCtrl)
{
Q_ASSERT(r);
Q_ASSERT(stateChange);
Q_ASSERT(apsCtrl);
quint8 cmd = 0xff;
if (r->parentResource())
{
r = r->parentResource(); // Device* has nwk/ext address
}
for (const auto &i : stateChange->parameters())
{
if (i.name == QLatin1String("cmd"))
{
bool ok;
auto val = i.value.toUInt(&ok);
if (ok && (val == ONOFF_COMMAND_ON || val == ONOFF_COMMAND_OFF || val == ONOFF_COMMAND_OFF_WITH_EFFECT))
{
cmd = static_cast<quint8>(val);
}
break;
}
}
if (cmd == 0xff)
{
return -1;
}
deCONZ::ApsDataRequest req;
deCONZ::ZclFrame zclFrame;
req.setClusterId(ONOFF_CLUSTER_ID);
req.setProfileId(HA_PROFILE_ID);
req.dstAddress().setNwk(r->item(RAttrNwkAddress)->toNumber());
req.dstAddress().setExt(r->item(RAttrExtAddress)->toNumber());
req.setDstAddressMode(deCONZ::ApsNwkAddress);
req.setDstEndpoint(stateChange->dstEndpoint());
req.setSrcEndpoint(0x01);
zclFrame.payload().clear();
zclFrame.setSequenceNumber(zclNextSequenceNumber());
zclFrame.setCommandId(cmd);
zclFrame.setFrameControl(deCONZ::ZclFCClusterCommand |
deCONZ::ZclFCDirectionClientToServer |
deCONZ::ZclFCDisableDefaultResponse);
if (cmd == ONOFF_COMMAND_OFF_WITH_EFFECT)
{
const quint8 effect = 0;
const quint8 variant = 0;
QDataStream stream(&zclFrame.payload(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
stream << effect;
stream << variant;
}
// else if (cmd == ONOFF_COMMAND_ON_WITH_TIMED_OFF)
// {
// const quint16 offWaitTime = 0;
// QDataStream stream(&zclFrame.payload(), QIODevice::WriteOnly);
// stream.setByteOrder(QDataStream::LittleEndian);
// // stream << (quint8)0x80; // 0x01 accept only when on --> no, 0x80 overwrite ontime (yes, non standard)
// stream << flags;
// stream << ontime;
// stream << offWaitTime;
// }
QDataStream stream(&req.asdu(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
zclFrame.writeToStream(stream);
DBG_Printf(DBG_INFO, "SC_SetOnOff()\n");
return apsCtrl->apsdeDataRequest(req) == deCONZ::Success ? 0 : -2;
}