feat(cpn): flashing add profile check option (#6680)

This commit is contained in:
Neil Horne
2025-10-18 20:43:00 +10:00
committed by GitHub
parent 2fe2c6cbe8
commit dfe035cf51
7 changed files with 393 additions and 115 deletions
+138 -94
View File
@@ -29,27 +29,42 @@
#include "progresswidget.h"
#include "splashlibrarydialog.h"
#include "storage.h"
#include "autocollapsiblesection.h"
#include <QTimer>
FlashFirmwareDialog::FlashFirmwareDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::FlashFirmwareDialog),
fwName(g.profile[g.id()].fwName())
fwName(g.profile[g.id()].fwName()),
imageSource(FIRMWARE),
imageFile(""),
connectionMode(CONNECTION_NONE)
{
ui->setupUi(this);
ui->checkBackup->setChecked(g.backupOnFlash());
if (!g.currentProfile().splashFile().isEmpty()){
ui->advancedWidget->setTitle(tr("Advanced"));
ui->advancedWidget->setAnimationDuration(300);
QVBoxLayout *advLayout = new QVBoxLayout();
chkHWComp = new QCheckBox(tr("check hardware compatibility (recommended)"), ui->advancedWidget);
chkHWComp->setChecked(true);
advLayout->addWidget(chkHWComp);
chkProfComp = new QCheckBox(tr("check profile compatibility"), ui->advancedWidget);
chkProfComp->setChecked(true);
advLayout->addWidget(chkProfComp);
ui->advancedWidget->setContentLayout(*advLayout);
const QString splashFile(g.currentProfile().splashFile());
if (!splashFile.isEmpty() && QFile(splashFile).exists()){
imageSource = PROFILE;
imageFile = g.currentProfile().splashFile();
imageFile = splashFile;
ui->useProfileSplash->setChecked(true);
} else {
imageSource = FIRMWARE;
imageFile = "";
ui->useProfileSplash->setDisabled(true);
}
ui->checkHardwareCompatibility->setChecked(g.checkHardwareCompatibility());
ui->checkBackup->setChecked(g.backupOnFlash());
detectClicked(true);
updateUI();
@@ -60,7 +75,8 @@ FlashFirmwareDialog::FlashFirmwareDialog(QWidget *parent) :
connect(ui->useLibrarySplash, &QRadioButton::clicked, this, &FlashFirmwareDialog::useLibrarySplashClicked);
connect(ui->useExternalSplash, &QRadioButton::clicked, this, &FlashFirmwareDialog::useExternalSplashClicked);
connect(ui->writeButton, &QPushButton::clicked, this, &FlashFirmwareDialog::writeButtonClicked);
connect(ui->cancelButton, &QPushButton::clicked, [this]() { close(); } );
connect(ui->cancelButton, &QPushButton::clicked, [this] () { close(); } );
connect(ui->advancedWidget, &AutoCollapsibleSection::resized, this, &FlashFirmwareDialog::shrink);
}
FlashFirmwareDialog::~FlashFirmwareDialog()
@@ -70,62 +86,65 @@ FlashFirmwareDialog::~FlashFirmwareDialog()
void FlashFirmwareDialog::updateUI()
{
ui->firmwareFilename->setText(fwName);
if (connectionMode) {
if (connectionMode && isFileConnectionCompatible() && QFile(fwName).exists()) {
ui->firmwareFilename->setText(fwName);
ui->loadButton->setEnabled(true);
ui->writeButton->setEnabled(isFileConnectionCompatible() && QFile(fwName).exists());
} else {
ui->loadButton->setEnabled(false);
ui->writeButton->setEnabled(false);
}
ui->writeButton->setEnabled(true);
FirmwareInterface firmware(fwName);
if (firmware.isValid()) {
ui->firmwareInfoFrame->show();
ui->date->setText(firmware.getDate() + " " + firmware.getTime());
ui->version->setText(firmware.getVersion());
ui->variant->setText(firmware.getFlavour());
ui->date->setEnabled(true);
ui->version->setEnabled(true);
ui->variant->setEnabled(true);
FirmwareInterface firmware(fwName);
if (firmware.hasSplash()) {
ui->splashFrame->show();
ui->splash->setFixedSize(firmware.getSplashWidth(), firmware.getSplashHeight());
if (firmware.isValid()) {
ui->firmwareInfoFrame->show();
ui->date->setText(firmware.getDate() + " " + firmware.getTime());
ui->version->setText(firmware.getVersion());
ui->variant->setText(firmware.getFlavour());
if (firmware.hasSplash()) {
ui->splashFrame->show();
ui->splash->setFixedSize(firmware.getSplashWidth(), firmware.getSplashHeight());
QImage image;
switch (imageSource) {
case FIRMWARE:
ui->useFirmwareSplash->setChecked(true);
image = firmware.getSplash();
break;
case PROFILE:
ui->useProfileSplash->setChecked(true);
image.load(g.currentProfile().splashFile());
break;
case LIBRARY:
ui->useLibrarySplash->setChecked(true);
image.load(imageFile);
break;
case EXTERNAL:
ui->useExternalSplash->setChecked(true);
image.load(imageFile);
break;
}
if (!image.isNull())
ui->splash->setPixmap(makePixMap(image));
else
ui->splash->setPixmap(QPixmap());
} else {
ui->splashFrame->hide();
}
} else {
ui->firmwareInfoFrame->hide();
ui->splashFrame->hide();
}
} else {
imageSource = FIRMWARE;
ui->firmwareFilename->setText("");
ui->loadButton->setEnabled(false);
ui->writeButton->setEnabled(false);
ui->firmwareInfoFrame->hide();
ui->splashFrame->hide();
}
QImage image;
switch (imageSource) {
case FIRMWARE:
ui->useFirmwareSplash->setChecked(true);
image = firmware.getSplash();
break;
case PROFILE:
ui->useProfileSplash->setChecked(true);
image.load(g.currentProfile().splashFile());
break;
case LIBRARY:
ui->useLibrarySplash->setChecked(true);
image.load(imageFile);
break;
case EXTERNAL:
ui->useExternalSplash->setChecked(true);
image.load(imageFile);
break;
}
if (!image.isNull())
ui->splash->setPixmap(makePixMap(image));
QTimer::singleShot(0, [=]() { adjustSize(); });
// add resize event to queue after changes above
QTimer::singleShot(0, [=]() { shrink(); });
}
void FlashFirmwareDialog::loadClicked()
@@ -133,6 +152,7 @@ void FlashFirmwareDialog::loadClicked()
QString fileName = QFileDialog::getOpenFileName(this, tr("Open Firmware File"),
g.flashDir(),
getFirmwareFilesFilter());
if (!fileName.isEmpty()) {
if (!firmwareFileExtensions().contains(QFileInfo(fileName).suffix().toLower())) {
QMessageBox::critical(this, tr("Open Firmware File"),
@@ -141,32 +161,35 @@ void FlashFirmwareDialog::loadClicked()
return;
}
FirmwareInterface fw(fileName);
FirmwareInterface newfw(fileName);
if (!fw.isValid()) {
QMessageBox::critical(this, tr("Open Firmware File"),
tr("%1 is not a valid firmware file")
.arg(fileName));
return;
}
if (connectionMode == CONNECTION_UF2) {
const Uf2Info uf2(getUf2Info());
if (fw.getFlavour() != uf2.board) {
if (chkHWComp->isChecked()) {
if (!newfw.isValid()) {
QMessageBox::critical(this, tr("Open Firmware File"),
tr("%1 \nFirmware file is for %2 radio however connected radio is %3")
.arg(fileName)
.arg(fw.getFlavour())
.arg(uf2.board));
tr("Error - %1 is not a valid firmware file")
.arg(fileName));
return;
}
if (connectionMode == CONNECTION_UF2) {
const Uf2Info uf2(getUf2Info());
if (newfw.getFlavour() != uf2.board) {
QMessageBox::critical(this, tr("Open Firmware File"),
tr("%1 \nIncompatability - File: '%2' Connected radio: '%3'")
.arg(fileName)
.arg(newfw.getFlavour())
.arg(uf2.board));
return;
}
}
}
if (fw.getFlavour() != getCurrentFirmware()->getFlavour()) {
if (chkProfComp->isChecked() &&
newfw.getFlavour() != getCurrentFirmware()->getFlavour()) {
QMessageBox::critical(this, tr("Open Firmware File"),
tr("%1 \nFirmware file is for %2 radio however profile is for %3 radio")
tr("%1 \nIncompatability - File: '%2' Profile: '%3'")
.arg(fileName)
.arg(fw.getFlavour())
.arg(newfw.getFlavour())
.arg(getCurrentFirmware()->getFlavour()));
return;
}
@@ -184,7 +207,7 @@ void FlashFirmwareDialog::useFirmwareSplashClicked()
QMessageBox::warning(this, CPN_STR_TTL_ERROR, tr( "The firmware file is not valid." ));
} else if (!firmware.hasSplash()) {
QMessageBox::warning(this, CPN_STR_TTL_ERROR,
tr( "There is no start screen image in the firmware file." ));
tr( "The firmware file does not cntain a start screen image." ));
} else {
imageSource = FIRMWARE;
}
@@ -261,7 +284,6 @@ void FlashFirmwareDialog::writeButtonClicked()
{
g.flashDir(QFileInfo(fwName).dir().absolutePath());
g.currentProfile().fwName(fwName);
g.checkHardwareCompatibility(ui->checkHardwareCompatibility->isChecked());
g.backupOnFlash(ui->checkBackup->isChecked());
if (imageSource != FIRMWARE) {
@@ -301,35 +323,54 @@ void FlashFirmwareDialog::startWrite(const QString &filename)
CompanionIcon("write_flash.png"));
auto progress = progressDialog.progress();
bool checkHw = g.checkHardwareCompatibility();
bool checkHw = chkHWComp->isChecked();
bool checkProfile = chkProfComp->isChecked();
bool backup = g.backupOnFlash();
FirmwareInterface newfw(filename);
if (g.checkHardwareCompatibility() || backup) {
if (checkHw || checkProfile || backup) {
progress->updateInfoAndMessages(tr("Reading old firmware..."));
readFirmware(
[this, &newfw, progress, checkHw, backup](const QByteArray &_data) {
[this, &newfw, progress, checkHw, checkProfile, backup](const QByteArray &_data) {
FirmwareInterface currfw(_data);
if (!currfw.isValid()) {
QString errMsg(tr("Firmware read from radio invalid"));
progress->addMessage(errMsg, QtFatalMsg);
progress->setInfo(errMsg);
return;
}
if (backup) {
progress->addMessage(tr("Backing up old firmware"));
if (!writeFirmwareToFile(this, _data, progress)) {
qDebug() << "profile:" << getCurrentFirmware()->getFlavour()
<< "current:" << currfw.getFlavour()
<< "new:" << newfw.getFlavour();
if (checkHw) {
if (!currfw.isValid()) {
QString errMsg(tr("Firmware read from radio invalid"));
progress->addMessage(errMsg, QtFatalMsg);
progress->setInfo(errMsg);
return;
}
progress->addMessage(tr("Performing hardware compatibity check"));
if (!newfw.isHardwareCompatible(currfw)) {
QString errMsg(tr("New firmware is not compatible with current firmware"));
progress->updateInfoAndMessages(errMsg, QtFatalMsg);
return;
}
}
if (checkHw) {
progress->addMessage(tr("Performing compatibity check"));
if (!newfw.isHardwareCompatible(currfw)) {
QString errMsg(tr("New firmware is not compatible with old firmware"));
if (checkProfile) {
progress->addMessage(tr("Performing profile compatibity check"));
if (currfw.getFlavour() != getCurrentFirmware()->getFlavour()) {
QString errMsg(tr("Current firmware is not compatible with profile"));
progress->updateInfoAndMessages(errMsg, QtFatalMsg);
return;
} else if (newfw.getFlavour() != getCurrentFirmware()->getFlavour()) {
QString errMsg(tr("New firmware is not compatible with profile"));
progress->updateInfoAndMessages(errMsg, QtFatalMsg);
return;
}
}
if (backup) {
progress->addMessage(tr("Backing up current firmware"));
if (!writeFirmwareToFile(this, _data, progress)) {
return;
}
}
@@ -364,7 +405,7 @@ void FlashFirmwareDialog::detectClicked(bool atLoad)
connectionMsg = tr("Radio connection mode: DFU");
} else {
connectionMode = CONNECTION_NONE;
connectionMsg = tr("ALERT: No radio connected");
connectionMsg = tr("ALERT: No radio detected");
}
ui->connectionMode->setText(connectionMsg);
@@ -376,9 +417,6 @@ void FlashFirmwareDialog::detectClicked(bool atLoad)
tr("Note: USB mode is not suitable for flashing firmware."));
}
if (!isFileConnectionCompatible())
fwName.clear();
if (!atLoad)
updateUI();
}
@@ -395,3 +433,9 @@ bool FlashFirmwareDialog::isFileConnectionCompatible()
return false;
}
void FlashFirmwareDialog::shrink()
{
this->adjustSize();
this->resize(0, 0);
}
+15 -2
View File
@@ -50,13 +50,26 @@ class FlashFirmwareDialog : public QDialog
void startWrite(const QString &filename);
private:
enum ConnectionModes { CONNECTION_NONE, CONNECTION_UF2, CONNECTION_DFU };
enum ImageSource {FIRMWARE, PROFILE, LIBRARY, EXTERNAL};
enum ConnectionModes {
CONNECTION_NONE,
CONNECTION_UF2,
CONNECTION_DFU
};
enum ImageSource {
FIRMWARE,
PROFILE,
LIBRARY,
EXTERNAL
};
Ui::FlashFirmwareDialog *ui;
QString fwName;
ImageSource imageSource;
QString imageFile;
int connectionMode;
QCheckBox *chkHWComp;
QCheckBox *chkProfComp;
bool isFileConnectionCompatible();
void shrink();
};
+13 -18
View File
@@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>421</width>
<height>390</height>
<height>396</height>
</rect>
</property>
<property name="sizePolicy">
@@ -264,32 +264,19 @@
</layout>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkHardwareCompatibility">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string>Checks the new firmware matches the connected radio. If not the process will be aborted.</string>
</property>
<property name="text">
<string>Check hardware compatibility (recommended)</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkBackup">
<property name="toolTip">
<string>Read the connected radio firmware and write to the backup folder.</string>
</property>
<property name="text">
<string>Backup radio firmware before flashing</string>
<string>Backup current radio firmware before flashing</string>
</property>
</widget>
</item>
<item>
<widget class="AutoCollapsibleSection" name="advancedWidget" native="true"/>
</item>
<item>
<spacer name="spacer2">
<property name="orientation">
@@ -354,6 +341,14 @@
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>AutoCollapsibleSection</class>
<extends>QWidget</extends>
<header>autocollapsiblesection.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>writeButton</tabstop>
<tabstop>firmwareFilename</tabstop>