Repository navigation
Expand file tree
/
Copy pathManufacturerSpecificDB.cpp
More file actions
471 lines (419 loc) · 14.6 KB
/
Copy pathManufacturerSpecificDB.cpp
File metadata and controls
471 lines (419 loc) · 14.6 KB
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
//-----------------------------------------------------------------------------
//
// ManufacturerSpecificDB.cpp
//
// Interface for Handling Device Configuration Files.
//
// Copyright (c) 2016 Justin Hammond <justin@dynam.ac>
//
// SOFTWARE NOTICE AND LICENSE
//
// This file is part of OpenZWave.
//
// OpenZWave is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// OpenZWave is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenZWave. If not, see <http://www.gnu.org/licenses/>.
//
//-----------------------------------------------------------------------------
#include "ManufacturerSpecificDB.h"
#include "tinyxml.h"
#include "Options.h"
#include "Driver.h"
#include "platform/Log.h"
#include "platform/FileOps.h"
#include "platform/Mutex.h"
#include "Notification.h"
#include "Utils.h"
namespace OpenZWave
{
namespace Internal
{
ManufacturerSpecificDB *ManufacturerSpecificDB::s_instance = NULL;
std::map<uint16, std::string> ManufacturerSpecificDB::s_manufacturerMap;
std::map<int64, std::shared_ptr<ProductDescriptor> > ManufacturerSpecificDB::s_productMap;
bool ManufacturerSpecificDB::s_bXmlLoaded = false;
ManufacturerSpecificDB *ManufacturerSpecificDB::Create()
{
if ( NULL == s_instance)
{
s_instance = new ManufacturerSpecificDB();
}
return s_instance;
}
void ManufacturerSpecificDB::Destroy()
{
delete s_instance;
s_instance = NULL;
}
ManufacturerSpecificDB::ManufacturerSpecificDB() :
m_MfsMutex(new Internal::Platform::Mutex()), m_revision(0), m_latestRevision(0), m_initializing(true)
{
// Ensure the singleton instance is set
s_instance = this;
if (!s_bXmlLoaded)
LoadProductXML();
}
ManufacturerSpecificDB::~ManufacturerSpecificDB()
{
if (!s_bXmlLoaded)
UnloadProductXML();
}
//-----------------------------------------------------------------------------
// <ManufacturerSpecificDB::LoadConfigFileRevision>
// Load the Config File Revision from each config file specified in our
// ManufacturerSpecific.xml file
//-----------------------------------------------------------------------------
void ManufacturerSpecificDB::LoadConfigFileRevision(ProductDescriptor *product)
{
// Parse the Z-Wave manufacturer and product XML file.
string configPath;
Options::Get()->GetOptionAsString("ConfigPath", &configPath);
if (product->GetConfigPath().size() > 0)
{
string path = configPath + product->GetConfigPath();
TiXmlDocument* pDoc = new TiXmlDocument();
if (!pDoc->LoadFile(path.c_str(), TIXML_ENCODING_UTF8))
{
delete pDoc;
Log::Write(LogLevel_Info, "Unable to load config file %s", path.c_str());
return;
}
pDoc->SetUserData((void *) path.c_str());
TiXmlElement const* root = pDoc->RootElement();
char const *str = root->Value();
if (str && !strcmp(str, "Product"))
{
str = root->Attribute("xmlns");
if (str && strcmp(str, "https://github.com/OpenZWave/open-zwave"))
{
Log::Write(LogLevel_Info, "Product Config File % has incorrect xml Namespace", path.c_str());
delete pDoc;
return;
}
// Read in the revision attributes
str = root->Attribute("Revision");
if (!str)
{
Log::Write(LogLevel_Info, "Error in Product Config file at line %d - missing Revision attribute", root->Row());
delete pDoc;
return;
}
product->SetConfigRevision(atol(str));
}
delete pDoc;
}
}
//-----------------------------------------------------------------------------
// <ManufacturerSpecificDB::LoadProductXML>
// Load the XML that maps manufacturer and product IDs to human-readable names
//-----------------------------------------------------------------------------
bool ManufacturerSpecificDB::LoadProductXML()
{
LockGuard LG(m_MfsMutex);
// Parse the Z-Wave manufacturer and product XML file.
string configPath;
Options::Get()->GetOptionAsString("ConfigPath", &configPath);
string filename = configPath + "manufacturer_specific.xml";
TiXmlDocument* pDoc = new TiXmlDocument();
if (!pDoc->LoadFile(filename.c_str(), TIXML_ENCODING_UTF8))
{
delete pDoc;
Log::Write(LogLevel_Info, "Unable to load %s", filename.c_str());
return false;
}
pDoc->SetUserData((void *) filename.c_str());
TiXmlElement const* root = pDoc->RootElement();
char const* str;
char* pStopChar;
str = root->Attribute("Revision");
if (str)
{
Log::Write(LogLevel_Info, "Manufacturer_Specific.xml file Revision is %s", str);
m_revision = atoi(str);
}
else
{
Log::Write(LogLevel_Warning, "Manufacturer_Specific.xml file has no Revision");
m_revision = 0;
}
TiXmlElement const* manufacturerElement = root->FirstChildElement();
while (manufacturerElement)
{
str = manufacturerElement->Value();
if (str && !strcmp(str, "Manufacturer"))
{
// Read in the manufacturer attributes
str = manufacturerElement->Attribute("id");
if (!str)
{
Log::Write(LogLevel_Info, "Error in manufacturer_specific.xml at line %d - missing manufacturer id attribute", manufacturerElement->Row());
delete pDoc;
return false;
}
uint16 manufacturerId = (uint16) strtol(str, &pStopChar, 16);
str = manufacturerElement->Attribute("name");
if (!str)
{
Log::Write(LogLevel_Info, "Error in manufacturer_specific.xml at line %d - missing manufacturer name attribute", manufacturerElement->Row());
delete pDoc;
return false;
}
// Add this manufacturer to the map
s_manufacturerMap[manufacturerId] = str;
// Parse all the products for this manufacturer
TiXmlElement const* productElement = manufacturerElement->FirstChildElement();
while (productElement)
{
str = productElement->Value();
if (str && !strcmp(str, "Product"))
{
str = productElement->Attribute("type");
if (!str)
{
Log::Write(LogLevel_Info, "Error in manufacturer_specific.xml at line %d - missing product type attribute", productElement->Row());
delete pDoc;
return false;
}
uint16 productType = (uint16) strtol(str, &pStopChar, 16);
str = productElement->Attribute("id");
if (!str)
{
Log::Write(LogLevel_Info, "Error in manufacturer_specific.xml at line %d - missing product id attribute", productElement->Row());
delete pDoc;
return false;
}
uint16 productId = (uint16) strtol(str, &pStopChar, 16);
str = productElement->Attribute("name");
if (!str)
{
Log::Write(LogLevel_Info, "Error in manufacturer_specific.xml at line %d - missing product name attribute", productElement->Row());
delete pDoc;
return false;
}
string productName = str;
// Optional config path
string dconfigPath;
str = productElement->Attribute("config");
if (str)
{
dconfigPath = str;
}
// Add the product to the map
ProductDescriptor* product = new ProductDescriptor(manufacturerId, productType, productId, productName, s_manufacturerMap[manufacturerId], dconfigPath);
if (s_productMap[product->GetKey()] != NULL)
{
std::shared_ptr<ProductDescriptor> c = s_productMap[product->GetKey()];
Log::Write(LogLevel_Info, "Product name collision: %s type %x id %x manufacturerid %x, collides with %s, type %x id %x manufacturerid %x", productName.c_str(), productType, productId, manufacturerId, c->GetProductName().c_str(), c->GetProductType(), c->GetProductId(), c->GetManufacturerId());
delete product;
}
else
{
LoadConfigFileRevision(product);
s_productMap[product->GetKey()] = std::shared_ptr<ProductDescriptor>(product);
}
}
// Move on to the next product.
productElement = productElement->NextSiblingElement();
}
}
// Move on to the next manufacturer.
manufacturerElement = manufacturerElement->NextSiblingElement();
}
s_bXmlLoaded = true;
delete pDoc;
return true;
}
//-----------------------------------------------------------------------------
// <ManufacturerSpecific::UnloadProductXML>
// Free the XML that maps manufacturer and product IDs
//-----------------------------------------------------------------------------
void ManufacturerSpecificDB::UnloadProductXML()
{
LockGuard LG(m_MfsMutex);
if (s_bXmlLoaded)
{
map<int64, std::shared_ptr<ProductDescriptor> >::iterator pit = s_productMap.begin();
while (!s_productMap.empty())
{
s_productMap.erase(pit);
pit = s_productMap.begin();
}
map<uint16, string>::iterator mit = s_manufacturerMap.begin();
while (!s_manufacturerMap.empty())
{
s_manufacturerMap.erase(mit);
mit = s_manufacturerMap.begin();
}
s_bXmlLoaded = false;
}
}
void ManufacturerSpecificDB::checkConfigFiles(Driver *driver)
{
LockGuard LG(m_MfsMutex);
if (!s_bXmlLoaded)
LoadProductXML();
string configPath;
Options::Get()->GetOptionAsString("ConfigPath", &configPath);
map<int64, std::shared_ptr<ProductDescriptor> >::iterator pit;
for (pit = s_productMap.begin(); pit != s_productMap.end(); pit++)
{
std::shared_ptr<ProductDescriptor> c = pit->second;
if (c->GetConfigPath().size() > 0)
{
string path = configPath + c->GetConfigPath();
/* check if we are downloading already */
std::list<string>::iterator iter = std::find(m_downloading.begin(), m_downloading.end(), path);
/* check if the file exists */
if (iter == m_downloading.end() && !Internal::Platform::FileOps::Create()->FileExists(path))
{
Log::Write(LogLevel_Warning, "Config File for %s does not exist - %s", c->GetProductName().c_str(), path.c_str());
/* try to download it */
if (driver->startConfigDownload(c->GetManufacturerId(), c->GetProductType(), c->GetProductId(), path))
{
m_downloading.push_back(path);
}
else
{
Log::Write(LogLevel_Warning, "Can't download file %s", path.c_str());
Notification* notification = new Notification(Notification::Type_UserAlerts);
notification->SetUserAlertNotification(Notification::Alert_ConfigFileDownloadFailed);
driver->QueueNotification(notification);
}
}
else if (iter != m_downloading.end())
{
Log::Write(LogLevel_Debug, "Config file for %s already queued", c->GetProductName().c_str());
}
}
}
checkInitialized();
}
void ManufacturerSpecificDB::configDownloaded(Driver *driver, string file, uint8 node, bool success)
{
/* check if we are downloading already */
std::list<string>::iterator iter = std::find(m_downloading.begin(), m_downloading.end(), file);
if (iter != m_downloading.end())
{
m_downloading.erase(iter);
if ((node > 0) && success)
{
driver->refreshNodeConfig(node);
}
else
{
checkInitialized();
}
}
else
{
Log::Write(LogLevel_Warning, "File is not in the list of downloading files: %s", file.c_str());
checkInitialized();
}
}
void ManufacturerSpecificDB::mfsConfigDownloaded(Driver *driver, string file, bool success)
{
/* check if we are downloading already */
std::list<string>::iterator iter = std::find(m_downloading.begin(), m_downloading.end(), file);
if (iter != m_downloading.end())
{
m_downloading.erase(iter);
if (success)
{
UnloadProductXML();
LoadProductXML();
checkConfigFiles(driver);
}
}
else
{
Log::Write(LogLevel_Warning, "File is not in the list of downloading files: %s", file.c_str());
}
checkInitialized();
}
bool ManufacturerSpecificDB::isReady()
{
if (!m_initializing && (m_downloading.size() == 0))
return true;
return false;
}
void ManufacturerSpecificDB::checkInitialized()
{
if (!m_initializing)
return;
Log::Write(LogLevel_Debug, "Downloads Remaining: %d", m_downloading.size());
if (m_downloading.size() == 0)
{
Log::Write(LogLevel_Info, "ManufacturerSpecificDB Initialized");
m_initializing = false;
}
}
std::shared_ptr<ProductDescriptor> ManufacturerSpecificDB::getProduct(uint16 _manufacturerId, uint16 _productType, uint16 _productId)
{
if (!s_bXmlLoaded)
LoadProductXML();
// Try to get the real manufacturer and product names
map<uint16, string>::iterator mit = s_manufacturerMap.find(_manufacturerId);
if (mit != s_manufacturerMap.end())
{
// Get the product
map<int64, std::shared_ptr<ProductDescriptor> >::iterator pit = s_productMap.find(ProductDescriptor::GetKey(_manufacturerId, _productType, _productId));
if (pit != s_productMap.end())
{
return pit->second;
}
}
return NULL;
}
bool ManufacturerSpecificDB::updateConfigFile(Driver *driver, Node *node)
{
string configPath;
bool ret = false;
Options::Get()->GetOptionAsString("ConfigPath", &configPath);
string path = configPath + node->getConfigPath();
if (driver->startConfigDownload(node->GetManufacturerId(), node->GetProductType(), node->GetProductId(), path, node->GetNodeId()))
{
m_downloading.push_back(path);
ret = true;
}
else
{
Log::Write(LogLevel_Warning, "Can't download Config file %s", node->getConfigPath().c_str());
Notification* notification = new Notification(Notification::Type_UserAlerts);
notification->SetUserAlertNotification(Notification::Alert_ConfigFileDownloadFailed);
driver->QueueNotification(notification);
}
checkInitialized();
return ret;
}
bool ManufacturerSpecificDB::updateMFSConfigFile(Driver *driver)
{
bool ret = false;
string configPath;
Options::Get()->GetOptionAsString("ConfigPath", &configPath);
string path = configPath + "manufacturer_specific.xml";
if (driver->startMFSDownload(path))
{
m_downloading.push_back(path);
ret = true;
}
else
{
Log::Write(LogLevel_Warning, "Can't download ManufacturerSpecifix.xml Config file");
Notification* notification = new Notification(Notification::Type_UserAlerts);
notification->SetUserAlertNotification(Notification::Alert_ConfigFileDownloadFailed);
driver->QueueNotification(notification);
}
checkInitialized();
return ret;
}
} // namespace Internal
} // namespace OpenZWave