forked from dresden-elektronik/deconz-rest-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
firmware_update.cpp
477 lines (423 loc) · 14.1 KB
/
firmware_update.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
/*
* 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 <QApplication>
#include <QDesktopServices>
#include <QFile>
#include <QDir>
#include <QString>
#include <QProcess>
#include "de_web_plugin.h"
#include "de_web_plugin_private.h"
#define FW_IDLE_TIMEOUT (10 * 1000)
#define FW_WAIT_UPDATE_READY (2) //s
#define FW_IDLE_TIMEOUT_LONG (240 * 1000)
#define FW_WAIT_USER_TIMEOUT (120 * 1000)
/*! Inits the firmware update manager.
*/
void DeRestPluginPrivate::initFirmwareUpdate()
{
fwProcess = 0;
fwUpdateState = FW_Idle;
Q_ASSERT(apsCtrl);
apsCtrl->setParameter(deCONZ::ParamFirmwareUpdateActive, deCONZ::FirmwareUpdateIdle);
fwUpdateStartedByUser = false;
fwUpdateTimer = new QTimer(this);
fwUpdateTimer->setSingleShot(true);
connect(fwUpdateTimer, SIGNAL(timeout()),
this, SLOT(firmwareUpdateTimerFired()));
fwUpdateTimer->start(5000);
}
/*! Starts the actual firmware update process.
*/
void DeRestPluginPrivate::updateFirmware()
{
if (gwFirmwareNeedUpdate)
{
gwFirmwareNeedUpdate = false;
}
Q_ASSERT(apsCtrl);
if (apsCtrl->getParameter(deCONZ::ParamFirmwareUpdateActive) == deCONZ::FirmwareUpdateIdle ||
apsCtrl->getParameter(deCONZ::ParamDeviceConnected) == 1)
{
DBG_Printf(DBG_INFO, "GW firmware update conditions not met, abort\n");
fwUpdateState = FW_Idle;
fwUpdateTimer->start(FW_IDLE_TIMEOUT);
updateEtag(gwConfigEtag);
return;
}
QString gcfFlasherBin = qApp->applicationDirPath() + "/GCFFlasher";
#ifdef Q_OS_WIN
gcfFlasherBin.append(".exe");
QString bin = gcfFlasherBin;
#elif defined(Q_OS_LINUX) && !defined(ARCH_ARM) // on RPi a normal sudo is ok since we don't need password there
QString bin = "pkexec";
gcfFlasherBin = "/usr/bin/GCFFlasher_internal";
fwProcessArgs.prepend(gcfFlasherBin);
#elif defined(Q_OS_OSX)
// TODO
// /usr/bin/osascript -e 'do shell script "make install" with administrator privileges'
QString bin = "sudo";
fwProcessArgs.prepend(gcfFlasherBin);
#else
QString bin = "sudo";
gcfFlasherBin = "/usr/bin/GCFFlasher_internal";
fwProcessArgs.prepend(gcfFlasherBin);
#endif
if (!fwProcess)
{
fwProcess = new QProcess(this);
}
fwProcessArgs << "-f" << fwUpdateFile;
fwUpdateState = FW_UpdateWaitFinished;
updateEtag(gwConfigEtag);
fwUpdateTimer->start(250);
fwProcess->start(bin, fwProcessArgs);
}
/*! Observes the firmware update process.
*/
void DeRestPluginPrivate::updateFirmwareWaitFinished()
{
if (fwProcess)
{
if (fwProcess->bytesAvailable())
{
QByteArray data = fwProcess->readAllStandardOutput();
DBG_Printf(DBG_INFO, "%s", qPrintable(data));
if (apsCtrl->getParameter(deCONZ::ParamFirmwareUpdateActive) != deCONZ::FirmwareUpdateRunning)
{
if (data.contains("flashing"))
{
apsCtrl->setParameter(deCONZ::ParamFirmwareUpdateActive, deCONZ::FirmwareUpdateRunning);
}
}
}
if (fwProcess->state() == QProcess::Starting)
{
DBG_Printf(DBG_INFO, "GW firmware update starting ..\n");
}
else if (fwProcess->state() == QProcess::Running)
{
DBG_Printf(DBG_INFO_L2, "GW firmware update running ..\n");
}
else if (fwProcess->state() == QProcess::NotRunning)
{
if (fwProcess->exitStatus() == QProcess::NormalExit)
{
DBG_Printf(DBG_INFO, "GW firmware update exit code %d\n", fwProcess->exitCode());
}
else if (fwProcess->exitStatus() == QProcess::CrashExit)
{
DBG_Printf(DBG_INFO, "GW firmware update crashed %s\n", qPrintable(fwProcess->errorString()));
}
fwProcess->deleteLater();
fwProcess = 0;
}
}
// done
if (fwProcess == 0)
{
fwUpdateStartedByUser = false;
gwFirmwareNeedUpdate = false;
updateEtag(gwConfigEtag);
apsCtrl->setParameter(deCONZ::ParamFirmwareUpdateActive, deCONZ::FirmwareUpdateIdle);
fwUpdateState = FW_Idle;
fwUpdateTimer->start(FW_IDLE_TIMEOUT_LONG);
}
else // recheck
{
fwUpdateTimer->start(250);
}
}
/*! Starts the device disconnect so that the serial port is released.
*/
void DeRestPluginPrivate::updateFirmwareDisconnectDevice()
{
Q_ASSERT(apsCtrl);
// if (apsCtrl->getParameter(deCONZ::ParamFirmwareUpdateActive) == deCONZ::FirmwareUpdateIdle)
// {
// if (apsCtrl->getParameter(deCONZ::ParamDeviceConnected) == 1)
// {
// DBG_Printf(DBG_INFO, "GW firmware disconnect device before update\n");
// }
// }
if (apsCtrl->getParameter(deCONZ::ParamDeviceConnected) == 1)
{
fwUpdateTimer->start(100); // recheck
}
else
{
DBG_Printf(DBG_INFO, "GW firmware start update (device not connected)\n");
fwUpdateState = FW_Update;
fwUpdateTimer->start(0);
updateEtag(gwConfigEtag);
}
}
/*! Starts the firmware update.
*/
bool DeRestPluginPrivate::startUpdateFirmware()
{
fwUpdateStartedByUser = true;
if (fwUpdateState == FW_WaitUserConfirm)
{
apsCtrl->setParameter(deCONZ::ParamFirmwareUpdateActive, deCONZ::FirmwareUpdateRunning);
updateEtag(gwConfigEtag);
fwUpdateState = FW_DisconnectDevice;
fwUpdateTimer->start(100);
return true;
}
return false;
}
/*! Delayed trigger to update the firmware.
*/
void DeRestPluginPrivate::firmwareUpdateTimerFired()
{
if (otauLastBusyTimeDelta() < OTA_LOW_PRIORITY_TIME)
{
fwUpdateState = FW_Idle;
fwUpdateTimer->start(FW_IDLE_TIMEOUT);
}
else if (fwUpdateState == FW_Idle)
{
if (gwFirmwareNeedUpdate)
{
gwFirmwareNeedUpdate = false;
updateEtag(gwConfigEtag);
}
if (gwFirmwareVersion == QLatin1String("0x00000000"))
{
uint8_t devConnected = apsCtrl->getParameter(deCONZ::ParamDeviceConnected);
uint32_t fwVersion = apsCtrl->getParameter(deCONZ::ParamFirmwareVersion);
if (devConnected && fwVersion)
{
gwFirmwareVersion.sprintf("0x%08x", fwVersion);
gwConfig["fwversion"] = gwFirmwareVersion;
updateEtag(gwConfigEtag);
}
}
fwUpdateState = FW_CheckDevices;
fwUpdateTimer->start(0);
}
else if (fwUpdateState == FW_CheckDevices)
{
checkFirmwareDevices();
}
else if (fwUpdateState == FW_CheckVersion)
{
queryFirmwareVersion();
}
else if (fwUpdateState == FW_DisconnectDevice)
{
updateFirmwareDisconnectDevice();
}
else if (fwUpdateState == FW_Update)
{
updateFirmware();
}
else if (fwUpdateState == FW_UpdateWaitFinished)
{
updateFirmwareWaitFinished();
}
else if (fwUpdateState == FW_WaitUserConfirm)
{
fwUpdateState = FW_Idle;
fwUpdateTimer->start(FW_IDLE_TIMEOUT);
}
else
{
fwUpdateState = FW_Idle;
fwUpdateTimer->start(FW_IDLE_TIMEOUT);
}
}
/*! Lazy query of firmware version.
Because the device might not be connected at first optaining the
firmware version must be delayed.
If the firmware is older then the min required firmware for the platform
and a proper firmware update file exists, the API will announce that a
firmware update is available.
*/
void DeRestPluginPrivate::queryFirmwareVersion()
{
Q_ASSERT(apsCtrl);
if (!apsCtrl)
{
return;
}
{ // check for GCFFlasher binary
QString gcfFlasherBin = qApp->applicationDirPath() + "/GCFFlasher";
#ifdef Q_OS_WIN
gcfFlasherBin.append(".exe");
#elif defined(Q_OS_LINUX) && !defined(ARCH_ARM) // on RPi a normal sudo is ok since we don't need password there
gcfFlasherBin = "/usr/bin/GCFFlasher_internal";
#elif defined(Q_OS_OSX)
// TODO
#else
gcfFlasherBin = "/usr/bin/GCFFlasher_internal";
#endif
if (!QFile::exists(gcfFlasherBin))
{
DBG_Printf(DBG_INFO, "GW update firmware failed, %s doesn't exist\n", qPrintable(gcfFlasherBin));
fwUpdateState = FW_Idle;
fwUpdateTimer->start(FW_IDLE_TIMEOUT_LONG);
return;
}
}
// does the update file exist?
QString fileName;
if (fwUpdateFile.isEmpty())
{
fileName.sprintf("deCONZ_Rpi_0x%08x.bin.GCF", GW_MIN_RPI_FW_VERSION);
// search in different locations
std::vector<QString> paths;
#ifdef Q_OS_LINUX
paths.push_back(QLatin1String("/usr/share/deCONZ/firmware/"));
#endif
paths.push_back(deCONZ::getStorageLocation(deCONZ::ApplicationsDataLocation) + QLatin1String("/firmware/"));
paths.push_back(deCONZ::getStorageLocation(deCONZ::HomeLocation) + QLatin1String("/raspbee_firmware/"));
#ifdef Q_OS_OSX
QDir dir(qApp->applicationDirPath());
dir.cdUp();
dir.cd("Resources");
paths.push_back(dir.path() + "/");
#endif
std::vector<QString>::const_iterator i = paths.begin();
std::vector<QString>::const_iterator end = paths.end();
for (; i != end; ++i)
{
if (QFile::exists(*i + fileName))
{
fwUpdateFile = *i + fileName;
DBG_Printf(DBG_INFO, "GW update firmware found: %s\n", qPrintable(fwUpdateFile));
break;
}
}
}
if (fwUpdateFile.isEmpty())
{
DBG_Printf(DBG_ERROR, "GW update firmware not found: %s\n", qPrintable(fileName));
fwUpdateState = FW_Idle;
fwUpdateTimer->start(FW_IDLE_TIMEOUT);
return;
}
uint8_t devConnected = apsCtrl->getParameter(deCONZ::ParamDeviceConnected);
uint32_t fwVersion = apsCtrl->getParameter(deCONZ::ParamFirmwareVersion);
Q_ASSERT(!gwFirmwareNeedUpdate);
if (devConnected == 0 || fwVersion == 0)
{
// if even after some time no firmware was detected
// ASSUME that a device is present and reachable but might not have firmware installed
// if (getUptime() >= FW_WAIT_UPDATE_READY)
{
QString str;
str.sprintf("0x%08x", GW_MIN_RPI_FW_VERSION);
gwFirmwareVersion = "0x00000000"; // unknown
gwFirmwareVersionUpdate = str;
gwConfig["fwversion"] = gwFirmwareVersion;
gwFirmwareNeedUpdate = true;
updateEtag(gwConfigEtag);
fwUpdateState = FW_WaitUserConfirm;
fwUpdateTimer->start(FW_WAIT_USER_TIMEOUT);
apsCtrl->setParameter(deCONZ::ParamFirmwareUpdateActive, deCONZ::FirmwareUpdateReadyToStart);
if (fwUpdateStartedByUser)
{
startUpdateFirmware();
}
}
return;
}
else if (devConnected)
{
QString str;
str.sprintf("0x%08x", fwVersion);
if (gwFirmwareVersion != str)
{
gwFirmwareVersion = str;
gwConfig["fwversion"] = str;
updateEtag(gwConfigEtag);
}
DBG_Printf(DBG_INFO, "GW firmware version: %s\n", qPrintable(gwFirmwareVersion));
// if the device is detected check that the firmware version is >= min version
if ((fwVersion & FW_PLATFORM_MASK) == FW_PLATFORM_RPI)
{
if (fwVersion < GW_MIN_RPI_FW_VERSION)
{
gwFirmwareVersionUpdate.sprintf("0x%08x", GW_MIN_RPI_FW_VERSION);
gwFirmwareNeedUpdate = true;
updateEtag(gwConfigEtag);
DBG_Printf(DBG_INFO, "GW firmware version shall be updated to: 0x%08x\n", GW_MIN_RPI_FW_VERSION);
fwUpdateState = FW_WaitUserConfirm;
fwUpdateTimer->start(FW_WAIT_USER_TIMEOUT);
apsCtrl->setParameter(deCONZ::ParamFirmwareUpdateActive, deCONZ::FirmwareUpdateReadyToStart);
return;
}
else
{
DBG_Printf(DBG_INFO, "GW firmware version is up to date: 0x%08x\n", fwVersion);
fwUpdateState = FW_Idle;
fwUpdateTimer->start(FW_IDLE_TIMEOUT_LONG);
return;
}
}
if (!gwFirmwareVersionUpdate.isEmpty())
{
gwFirmwareVersionUpdate.clear();
updateEtag(gwConfigEtag);
}
}
fwUpdateState = FW_Idle;
fwUpdateTimer->start(FW_IDLE_TIMEOUT);
}
/*! Checks if devices for firmware update are present.
*/
void DeRestPluginPrivate::checkFirmwareDevices()
{
deCONZ::DeviceEnumerator devEnumerator;
fwProcessArgs.clear();
devEnumerator.listSerialPorts();
const std::vector<deCONZ::DeviceEntry> &availPorts = devEnumerator.getList();
std::vector<deCONZ::DeviceEntry>::const_iterator i = availPorts.begin();
std::vector<deCONZ::DeviceEntry>::const_iterator end = availPorts.end();
int raspBeeCount = 0;
int usbDongleCount = 0;
QString ttyPath;
for (; i != end; ++i)
{
if (i->friendlyName.contains(QLatin1String("ConBee")))
{
usbDongleCount++;
}
else if (i->friendlyName.contains(QLatin1String("RaspBee")))
{
raspBeeCount = 1;
ttyPath = i->path;
}
}
if (usbDongleCount > 1)
{
DBG_Printf(DBG_INFO_L2, "GW firmware update too many USB devices connected, abort\n");
}
else if (usbDongleCount == 1)
{
DBG_Printf(DBG_INFO_L2, "GW firmware update select USB device\n");
fwProcessArgs << "-d" << "0";
}
else if (raspBeeCount > 0 && usbDongleCount == 0 && !ttyPath.isEmpty())
{
DBG_Printf(DBG_INFO_L2, "GW firmware update select %s device\n", qPrintable(ttyPath));
fwProcessArgs << "-d" << "RaspBee";
}
if (!fwProcessArgs.isEmpty())
{
fwUpdateState = FW_CheckVersion;
fwUpdateTimer->start(0);
return;
}
fwUpdateState = FW_Idle;
fwUpdateTimer->start(FW_IDLE_TIMEOUT);
}