Automatic OSTC firmware update: download file and try to install

This successfully downloads the hex file.
The actuall update of the OSTC fails before it gets started with a crash
when the ConfigureDiveComputer object is created.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2014-12-28 10:22:46 -08:00
parent 0be0cdb046
commit 003f5d18b6
3 changed files with 73 additions and 23 deletions

View file

@ -1,17 +1,21 @@
#include "configuredivecomputerdialog.h"
#include "../divecomputer.h"
#include "../libdivecomputer.h"
#include "../helpers.h"
#include "../display.h"
#include "../divelist.h"
#include "divecomputer.h"
#include "libdivecomputer.h"
#include "helpers.h"
#include "display.h"
#include "divelist.h"
#include "configuredivecomputer.h"
#include "mainwindow.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QSettings>
#include <QWebElement>
#include <QWebFrame>
#include <QWebPage>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QDebug>
struct product {
@ -193,29 +197,37 @@ ConfigureDiveComputerDialog::ConfigureDiveComputerDialog(QWidget *parent) :
OstcFirmwareCheck::OstcFirmwareCheck(QString product)
{
QUrl url;
if (product == "OSTC 3")
if (product == "OSTC 3") {
url = QUrl("http://www.heinrichsweikamp.net/autofirmware/ostc3_changelog.txt");
else if (product == "OSTC Sport")
latestFirmwareHexFile = QString("http://www.heinrichsweikamp.net/autofirmware/ostc3_firmware.hex");
} else if (product == "OSTC Sport") {
url = QUrl("http://www.heinrichsweikamp.net/autofirmware/ostc_sport_changelog.txt");
else // not one of the known dive computers
latestFirmwareHexFile = QString("http://www.heinrichsweikamp.net/autofirmware/ostc_sport_firmware.hex");
} else { // not one of the known dive computers
return;
hwVersionPage.mainFrame()->load(url);
connect(&hwVersionPage, SIGNAL(loadFinished(bool)), this, SLOT(parseOstcFwVersion()));
}
connect(&manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(parseOstcFwVersion(QNetworkReply *)));
QNetworkRequest download(url);
manager.get(download);
}
void OstcFirmwareCheck::parseOstcFwVersion()
void OstcFirmwareCheck::parseOstcFwVersion(QNetworkReply *reply)
{
QString parse = hwVersionPage.mainFrame()->toPlainText();
QString parse = reply->readAll();
int firstOpenBracket = parse.indexOf('[');
int firstCloseBracket = parse.indexOf(']');
latestFirmwareAvailable = parse.mid(firstOpenBracket + 1, firstCloseBracket - firstOpenBracket -1);
qDebug() << "latest firmware available" << latestFirmwareAvailable;
disconnect(&manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(parseOstcFwVersion(QNetworkReply*)));
}
void OstcFirmwareCheck::checkLatest(QWidget *parent, uint32_t firmwareOnDevice)
void OstcFirmwareCheck::checkLatest(QWidget *_parent, device_data_t *data)
{
devData = *data;
parent = _parent;
// for now libdivecomputer gives us the firmware on device undecoded as integer
// for the OSTC that means highbyte.lowbyte is the version number
int firmwareOnDevice = devData.libdc_firmware;
QString firmware;
firmware = QString("%1.%2").arg(firmwareOnDevice / 256). arg(firmwareOnDevice % 256);
if (!latestFirmwareAvailable.isEmpty() && latestFirmwareAvailable != firmware) {
@ -229,14 +241,44 @@ void OstcFirmwareCheck::checkLatest(QWidget *parent, uint32_t firmwareOnDevice)
response.setIcon(QMessageBox::Question);
response.setWindowModality(Qt::WindowModal);
int ret = response.exec();
if (ret == QMessageBox::Accepted) {
qDebug() << "go to firmware upgrade";
} else {
qDebug() << "no upgrade";
}
if (ret == QMessageBox::Accepted)
upgradeFirmware();
}
}
void OstcFirmwareCheck::upgradeFirmware()
{
// start download of latestFirmwareHexFile
QString saveFileName = latestFirmwareHexFile;
saveFileName.replace("http://www.heinrichsweikamp.net/autofirmware/", "");
saveFileName.replace("firmware", latestFirmwareAvailable);
QString filename = existing_filename ?: prefs.default_filename;
QFileInfo fi(filename);
filename = fi.absolutePath().append(QDir::separator()).append(saveFileName);
storeFirmware = QFileDialog::getSaveFileName(parent, tr("Save the downloaded firmware as"),
filename, tr("HEX files (*.hex)"));
if (storeFirmware.isEmpty())
return;
connect(&manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(saveOstcFirmware(QNetworkReply *)));
QNetworkRequest download(latestFirmwareHexFile);
manager.get(download);
}
void OstcFirmwareCheck::saveOstcFirmware(QNetworkReply *reply)
{
// firmware is downloaded
// call config->startFirmwareUpdate() with that file and the device data
QByteArray firmwareData = reply->readAll();
QFile file(storeFirmware);
file.open(QIODevice::WriteOnly);
file.write(firmwareData);
file.close();
ConfigureDiveComputer *config = new ConfigureDiveComputer(MainWindow::instance());
config->startFirmwareUpdate(storeFirmware, &devData);
}
ConfigureDiveComputerDialog::~ConfigureDiveComputerDialog()
{
QSettings settings;
@ -1023,6 +1065,8 @@ void ConfigureDiveComputerDialog::on_updateFirmwareButton_clicked()
}
}
void ConfigureDiveComputerDialog::on_DiveComputerList_currentRowChanged(int currentRow)
{
// Disable the buttons to do operations on this data

View file

@ -7,7 +7,7 @@
#include "../libdivecomputer.h"
#include "configuredivecomputer.h"
#include <QStyledItemDelegate>
#include <QWebPage>
#include <QNetworkAccessManager>
class GasSpinBoxItemDelegate : public QStyledItemDelegate
{
@ -107,13 +107,19 @@ class OstcFirmwareCheck : QObject
Q_OBJECT
public:
explicit OstcFirmwareCheck(QString product);
void checkLatest(QWidget *parent, uint32_t firmwareOnDevice);
void checkLatest(QWidget *parent, device_data_t *data);
public
slots:
void parseOstcFwVersion();
void parseOstcFwVersion(QNetworkReply *reply);
void saveOstcFirmware(QNetworkReply * reply);
private:
QWebPage hwVersionPage;
void upgradeFirmware();
device_data_t devData;
QString latestFirmwareAvailable;
QString latestFirmwareHexFile;
QString storeFirmware;
QWidget *parent;
QNetworkAccessManager manager;
};
#endif // CONFIGUREDIVECOMPUTERDIALOG_H

View file

@ -404,7 +404,7 @@ void DownloadFromDCWidget::onDownloadThreadFinished()
MainWindow::instance()->dive_list()->selectDive(idx, true);
QString dcName = data.devname;
if (ostcFirmwareCheck)
ostcFirmwareCheck->checkLatest(this, data.libdc_firmware);
ostcFirmwareCheck->checkLatest(this, &data);
}
} else if (currentState == CANCELLING || currentState == CANCELLED) {
if (import_thread_cancelled) {