forked from dresden-elektronik/deconz-rest-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
device_ddf_init.cpp
574 lines (475 loc) · 17.7 KB
/
device_ddf_init.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
/*
* Copyright (c) 2021-2024 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 "deconz/u_assert.h"
#include "device.h"
#include "device_compat.h"
#include "device_descriptions.h"
#include "device_ddf_init.h"
#include "deconz/u_sstream_ex.h"
#include "database.h"
#include "poll_control.h"
#include "utils/utils.h"
void DEV_AllocateGroup(const Device *device, Resource *rsub, ResourceItem *item);
static QString uniqueIdFromTemplate(const QStringList &templ, const Device *device)
{
bool ok = false;
quint8 endpoint = 0;
quint16 clusterId = 0;
quint64 extAddress;
extAddress = device->item(RAttrExtAddress)->toNumber();
// <mac>-<endpoint>
// <mac>-<endpoint>-<cluster>
/* supported templates
["$address.ext", <endpoint>]
["$address.ext", <endpoint>, <cluster>]
*/
if (templ.size() > 1 && templ.first() == QLatin1String("$address.ext"))
{
endpoint = templ.at(1).toUInt(&ok, 0);
if (ok && templ.size() > 2)
{
ok = false;
const QString pos2 = templ.at(2);
if (pos2.at(0).isDigit())
{
clusterId = pos2.toUInt(&ok, 0);
}
}
}
if (ok)
{
return generateUniqueId(extAddress, endpoint, clusterId);
}
return {};
}
/*! Creates a ResourceItem if not exist, initialized with \p ddfItem content.
*/
static ResourceItem *DEV_InitDeviceDescriptionItem(const DeviceDescription::Item &ddfItem, const std::vector<DB_ResourceItem> &dbItems, Resource *rsub)
{
U_ASSERT(rsub);
U_ASSERT(ddfItem.isValid());
auto *item = rsub->item(ddfItem.descriptor.suffix);
const char *uniqueId = rsub->item(RAttrUniqueId)->toCString();
U_ASSERT(uniqueId);
if (item)
{
if (DBG_IsEnabled(DBG_INFO_L2))
{
DBG_Printf(DBG_DDF, "sub-device: %s, has item: %s\n", uniqueId, ddfItem.descriptor.suffix);
}
}
else
{
if (DBG_IsEnabled(DBG_INFO_L2))
{
DBG_Printf(DBG_DDF, "sub-device: %s, create item: %s\n", uniqueId, ddfItem.descriptor.suffix);
}
item = rsub->addItem(ddfItem.descriptor.type, ddfItem.descriptor.suffix);
DBG_Assert(item);
if (!item)
{
return nullptr;
}
}
U_ASSERT(item);
const auto dbItem = std::find_if(dbItems.cbegin(), dbItems.cend(), [&ddfItem](const auto &dbItem)
{
return ddfItem.name == dbItem.name;
});
if (!ddfItem.isStatic && dbItem != dbItems.cend())
{
if (item->descriptor().suffix == RAttrId && !item->toString().isEmpty())
{
// keep 'id', it might have been loaded from legacy db
// and will be updated in 'resource_items' table on next write
}
else if (item->lastSet().isValid() && item->toVariant() == dbItem->value)
{
// nothing to do
}
else
{
item->setValue(dbItem->value);
item->setTimeStamps(QDateTime::fromMSecsSinceEpoch(dbItem->timestampMs));
}
item->clearNeedStore(); // already in DB
}
else if (!ddfItem.isStatic && dbItem == dbItems.cend() && !item->lastSet().isValid())
{
// try load from legacy sensors/nodes db tables
auto dbLegacyItem = std::make_unique<DB_LegacyItem>();
dbLegacyItem->uniqueId = uniqueId;
dbLegacyItem->column.setString(item->descriptor().suffix);
if (rsub->prefix() == RSensors)
{
DB_LoadLegacySensorValue(dbLegacyItem.get());
}
// TODO lights needs some more investigation, might not be needed..
// else if (rsub->prefix() == RLights)
// {
// DB_LoadLegacyLightValue(dbLegacyItem.get());
// }
if (!dbLegacyItem->value.empty())
{
item->setValue(QVariant(QString(dbLegacyItem->value.c_str())));
item->setTimeStamps(item->lastSet().addSecs(-120)); // TODO extract from 'lastupdated'?
}
}
if (ddfItem.defaultValue.isValid())
{
if (ddfItem.isStatic || !item->lastSet().isValid())
{
item->setValue(ddfItem.defaultValue);
item->clearNeedStore(); // already in DB
}
}
U_ASSERT(ddfItem.handle != DeviceDescription::Item::InvalidItemHandle);
item->setDdfItemHandle(ddfItem.handle);
// check updates
item->setIsPublic(ddfItem.isPublic);
item->setAwake(ddfItem.awake);
if (ddfItem.refreshInterval != DeviceDescription::Item::NoRefreshInterval)
{
item->setRefreshInterval(deCONZ::TimeSeconds{ddfItem.refreshInterval});
}
if (item->refreshInterval().val == 0 && !ddfItem.readParameters.isNull())
{
// If a DDF doesn't specify a refresh.interval and also not the generic item
// default to 30 seconds to relax polling a bit.
// Note: ideally this should be specified in a DDF/generic item.
const auto m = ddfItem.readParameters.toMap();
if (m.value(QLatin1String("fn")) != QLatin1String("none"))
{
item->setRefreshInterval(deCONZ::TimeSeconds{30});
}
}
item->setParseFunction(nullptr);
return item;
}
/*! Creates and initializes sub-device Resources and ResourceItems if not already present.
This function replaces the legacy database loading and joining device initialization.
*/
bool DEV_InitDeviceFromDescription(Device *device, const DeviceDescription &ddf)
{
Q_ASSERT(device);
Q_ASSERT(ddf.isValid());
size_t subCount = 0;
auto *dd = DeviceDescriptions::instance();
if (ddf.storageLocation == deCONZ::DdfBundleLocation || ddf.storageLocation == deCONZ::DdfBundleUserLocation)
{
ResourceItem *ddfhashItem = device->item(RAttrDdfHash);
U_ASSERT(ddfhashItem);
char buf[72];
U_SStream ss;
U_sstream_init(&ss, buf, sizeof(buf));
U_sstream_put_hex(&ss, &ddf.sha256Hash[0], sizeof(ddf.sha256Hash));
for (unsigned i = 0; i < ss.pos; i++) // workaround to convert to lower case
{
if (buf[i] >= 'A' && buf[i] <= 'F')
buf[i] = buf[i] + ('a' - 'A');
}
U_ASSERT(ss.pos == 64);
ddfhashItem->setValue(buf, 64);
}
for (const auto &sub : ddf.subDevices)
{
Q_ASSERT(sub.isValid());
const auto uniqueId = uniqueIdFromTemplate(sub.uniqueId, device);
if (uniqueId.isEmpty())
{
DBG_Printf(DBG_DDF, "failed to init sub-device uniqueid: %s, %s\n", qPrintable(sub.uniqueId.join('-')), qPrintable(sub.type));
return false;
}
Resource *rsub = DEV_GetSubDevice(device, nullptr, uniqueId);
if (!rsub)
{
rsub = DEV_InitCompatNodeFromDescription(device, ddf, sub, uniqueId);
}
if (!rsub)
{
DBG_Printf(DBG_DDF, "sub-device: %s, failed to setup: %s\n", qPrintable(uniqueId), qPrintable(sub.type));
return false;
}
subCount++;
auto *mf = rsub->item(RAttrManufacturerName);
if (mf && mf->toLatin1String().size() == 0)
{
mf->setValue(dd->constantToString(device->item(RAttrManufacturerName)->toString()));
}
// TODO storing should be done else where, since this is init code
DB_StoreSubDevice(rsub->item(RAttrUniqueId)->toCString());
DB_StoreSubDeviceItem(rsub, rsub->item(RAttrManufacturerName));
DB_StoreSubDeviceItem(rsub, rsub->item(RAttrModelId));
const auto dbItems = DB_LoadSubDeviceItems(rsub->item(RAttrUniqueId)->toLatin1String());
for (const auto &ddfItem : sub.items)
{
auto *item = DEV_InitDeviceDescriptionItem(ddfItem, dbItems, rsub);
if (!item)
{
continue;
}
if (item->descriptor().suffix == RStatePresence && item->toBool())
{
DBG_Printf(DBG_DDF, "sub-device: %s, presence state is true, reverting to false\n", qPrintable(uniqueId));
item->setValue(false);
item->clearNeedStore();
}
if (item->descriptor().suffix == RConfigGroup)
{
DEV_AllocateGroup(device, rsub, item);
}
if (!ddfItem.defaultValue.isNull() && !ddfItem.writeParameters.isNull())
{
QString writeFunction;
const auto writeParam = ddfItem.writeParameters.toMap();
if (writeParam.contains(QLatin1String("fn")))
{
writeFunction = writeParam.value(QLatin1String("fn")).toString();
}
if (writeFunction.isEmpty() || writeFunction.startsWith(QLatin1String("zcl")))
{
bool ok;
QVariant value = item->toVariant();
if (!value.isValid())
{
value = ddfItem.defaultValue;
}
StateChange stateChange(StateChange::StateWaitSync, SC_WriteZclAttribute, sub.uniqueId.at(1).toUInt());
stateChange.addTargetValue(item->descriptor().suffix, value);
stateChange.setChangeTimeoutMs(1000 * 60 * 60);
if (writeParam.contains(QLatin1String("state.timeout")))
{
int stateTimeout = writeParam.value(QLatin1String("state.timeout")).toInt(&ok);
if (ok && stateTimeout > 0)
{
stateChange.setStateTimeoutMs(1000 * stateTimeout);
}
}
rsub->addStateChange(stateChange);
}
}
// DDF enforce sub device "type" (enables overwriting the type from C++ code)
if (item->descriptor().suffix == RAttrType)
{
const QString type = DeviceDescriptions::instance()->constantToString(sub.type);
if (type != item->toString() && !type.startsWith('$'))
{
item->setValue(type);
}
}
if (item->descriptor().suffix == RConfigBattery || item->descriptor().suffix == RStateBattery)
{
DEV_ForwardNodeChange(device, QLatin1String(item->descriptor().suffix), QString::number(item->toNumber()));
}
if (item->descriptor().suffix == RConfigCheckin)
{
if (PC_GetPollControlEndpoint(device->node()) > 0)
{
auto *itemPending = rsub->item(RConfigPending);
if (itemPending) // TODO long poll interval via StateChange
{
itemPending->setValue(itemPending->toNumber() | R_PENDING_SET_LONG_POLL_INTERVAL);
}
}
}
}
}
if (ddf.sleeper >= 0)
{
device->item(RCapSleeper)->setValue(ddf.sleeper == 1);
}
if (ddf.supportsMgmtBind >= 0)
{
device->setSupportsMgmtBind(ddf.supportsMgmtBind == 1);
}
device->clearBindings();
for (const auto &bnd : ddf.bindings)
{
device->addBinding(bnd);
}
return subCount == ddf.subDevices.size();
}
bool DEV_InitBaseDescriptionForDevice(Device *device, DeviceDescription &ddf)
{
ddf.status = QLatin1String("Draft");
ddf.manufacturerNames.push_back(device->item(RAttrManufacturerName)->toString());
ddf.modelIds.push_back(device->item(RAttrModelId)->toString());
if (ddf.manufacturerNames.last().isEmpty() || ddf.modelIds.isEmpty() || ddf.modelIds.front().isEmpty())
{
return false;
}
const auto *dd = DeviceDescriptions::instance();
for (const Resource *r : device->subDevices())
{
DeviceDescription::SubDevice sub;
sub.type = dd->stringToConstant(r->item(RAttrType)->toString());
sub.restApi = QLatin1String(r->prefix());
if (ddf.product.isEmpty())
{
const ResourceItem *productId = r->item(RAttrProductId);
if (productId && !productId->toString().isEmpty())
{
ddf.product = productId->toString();
}
}
{
QString uniqueId = r->item(RAttrUniqueId)->toString();
QStringList ls = uniqueId.split('-', SKIP_EMPTY_PARTS);
// this is a bit fugly but uniqueid template must be valid
DBG_Assert(ls.size() > 1); // lights uniqueid
if (ls.size() > 1)
{
ls[0] = QLatin1String("$address.ext");
ls[1] = "0x" + ls[1];
if (ls.size() > 2) // sensor uniqueid
{
ls[2] = "0x" + ls[2];
}
sub.uniqueId = ls;
}
else
{
return false;
}
}
if (r->prefix() == RSensors)
{
const Sensor *sensor = dynamic_cast<const Sensor*>(r);
if (sensor)
{
sub.fingerPrint = sensor->fingerPrint();
}
}
for (int i = 0; i < r->itemCount(); i++)
{
const ResourceItem *item = r->itemForIndex(i);
DeviceDescription::Item ddfItem = dd->getGenericItem(item->descriptor().suffix);
if (!ddfItem.isValid())
{
// create some
ddfItem.name = item->descriptor().suffix;
ddfItem.descriptor = item->descriptor();
}
ddfItem.isPublic = item->isPublic();
sub.items.push_back(ddfItem);
}
ddf.subDevices.push_back(sub);
}
return true;
}
bool DEV_InitDeviceBasic(Device *device)
{
{
ResourceItem *ddfPolicy = device->item(RAttrDdfPolicy);
U_ASSERT(ddfPolicy);
{
// load attr/ddf_policy and attr/ddf_hash from database if exists
std::vector<DB_ResourceItem2> dbItems2;
if (DB_LoadDeviceItems(device->deviceId(), dbItems2))
{
for (const auto &dbItem : dbItems2)
{
U_ASSERT(dbItem.valueSize != 0);
if (dbItem.valueSize == 0)
{
continue;
}
if (dbItem.name == RAttrDdfPolicy && ddfPolicy)
{
ddfPolicy->setValue(dbItem.value, (int)dbItem.valueSize);
ddfPolicy->setTimeStamps(QDateTime::fromMSecsSinceEpoch(dbItem.timestampMs));
}
else if (dbItem.name == RAttrDdfHash)
{
U_ASSERT(dbItem.valueSize == 64);
if (dbItem.valueSize == 64)
{
ResourceItem *ddfHash = device->item(RAttrDdfHash);
ddfHash->setValue(dbItem.value, (int)dbItem.valueSize);
ddfHash->setTimeStamps(QDateTime::fromMSecsSinceEpoch(dbItem.timestampMs));
}
}
}
}
}
// if no attr/ddf_policy is set, use the default
if (ddfPolicy && ddfPolicy->toLatin1String().size() == 0)
{
ddfPolicy->setValue("latest_prefer_stable", -1);
// DB_ResourceItem2 dbItem;
// if (DB_ResourceItem2DbItem(ddfPolicy, &dbItem))
// {
// if (DB_StoreDeviceItem(device->deviceId(), dbItem))
// {
// }
// }
}
}
const auto dbItems = DB_LoadSubDeviceItemsOfDevice(device->item(RAttrUniqueId)->toLatin1String());
size_t found = 0;
std::array<const char*, 2> poi = { RAttrManufacturerName, RAttrModelId };
for (const auto &dbItem : dbItems)
{
if (dbItem.name == RStateReachable || dbItem.name == RConfigReachable)
{
ResourceItem *reachable = device->item(RStateReachable);
DBG_Assert(reachable);
if (reachable)
{
reachable->setValue(dbItem.value.toBool());
reachable->setTimeStamps(QDateTime::fromMSecsSinceEpoch(dbItem.timestampMs));
}
continue;
}
for (const char *suffix: poi)
{
if (dbItem.name != suffix)
{
continue;
}
auto *item = device->item(suffix);
if (item)
{
item->setValue(dbItem.value);
item->setTimeStamps(QDateTime::fromMSecsSinceEpoch(dbItem.timestampMs));
found++;
}
break;
}
}
DB_ZclValue zclVal;
zclVal.deviceId = device->deviceId();
zclVal.endpoint = 0;
zclVal.clusterId = 0x0019; // OTA cluster
zclVal.attrId = 0x0002; // OTA current file version
zclVal.data = 0;
if (DB_LoadZclValue(&zclVal) && zclVal.data != 0)
{
ResourceItem *item = device->item(RAttrOtaVersion);
if (item && item->toNumber() != zclVal.data)
{
item->setValue(zclVal.data, ResourceItem::SourceDevice);
item->clearNeedPush();
}
}
zclVal.clusterId = 0x0500; // IAS Zone cluster
zclVal.attrId = 0x0001; // IAS Zone Type
zclVal.data = 0;
if (DB_LoadZclValue(&zclVal) && zclVal.data != 0)
{
ResourceItem *item = device->addItem(DataTypeUInt16, RAttrZoneType);
if (item && item->toNumber() != zclVal.data)
{
item->setValue(zclVal.data, ResourceItem::SourceDevice);
item->clearNeedPush();
}
}
return found == poi.size();
}