From 8ac97072428a4aa86a3e26dff314e9baff6078c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20K=C3=A4berich?= Date: Fri, 17 Feb 2023 17:30:22 +0100 Subject: [PATCH] Safeguard against writing a wrong firmware to a different hardware version --- .../Device/LibreVNA/firmwareupdatedialog.cpp | 2 +- .../Device/LibreVNA/librevnadriver.cpp | 24 +++++++++++++++++-- .../Device/LibreVNA/librevnadriver.h | 4 ++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/Software/PC_Application/LibreVNA-GUI/Device/LibreVNA/firmwareupdatedialog.cpp b/Software/PC_Application/LibreVNA-GUI/Device/LibreVNA/firmwareupdatedialog.cpp index 042cd9a..a6f73fc 100644 --- a/Software/PC_Application/LibreVNA-GUI/Device/LibreVNA/firmwareupdatedialog.cpp +++ b/Software/PC_Application/LibreVNA-GUI/Device/LibreVNA/firmwareupdatedialog.cpp @@ -59,7 +59,7 @@ void FirmwareUpdateDialog::on_bStart_clicked() } char header[24]; file->read(header, sizeof(header)); - if(strncmp(header, "VNA!", 4)) { + if(strncmp(header, dev->getFirmwareMagicString().toStdString().c_str(), 4)) { abortWithError("Invalid magic header constant"); return; } diff --git a/Software/PC_Application/LibreVNA-GUI/Device/LibreVNA/librevnadriver.cpp b/Software/PC_Application/LibreVNA-GUI/Device/LibreVNA/librevnadriver.cpp index 70e00b8..512a3a4 100644 --- a/Software/PC_Application/LibreVNA-GUI/Device/LibreVNA/librevnadriver.cpp +++ b/Software/PC_Application/LibreVNA-GUI/Device/LibreVNA/librevnadriver.cpp @@ -108,6 +108,7 @@ LibreVNADriver::LibreVNADriver() connected = false; skipOwnPacketHandling = false; SApoints = 0; + hardwareVersion = 0; setSynchronization(Synchronization::Disabled, false); auto manual = new QAction("Manual Control"); @@ -182,7 +183,7 @@ std::set LibreVNADriver::getFlags() QString LibreVNADriver::getStatus() { QString ret; - ret.append("HW Rev."); + ret.append("HW "); ret.append(info.hardware_version); ret.append(" FW "+info.firmware_version); ret.append(" Temps: "+QString::number(lastStatus.temp_source)+"°C/"+QString::number(lastStatus.temp_LO1)+"°C/"+QString::number(lastStatus.temp_MCU)+"°C"); @@ -555,8 +556,9 @@ void LibreVNADriver::handleReceivedPacket(const Protocol::PacketInfo &packet) } } + hardwareVersion = packet.info.hardware_version; info.firmware_version = QString::number(packet.info.FW_major)+"."+QString::number(packet.info.FW_minor)+"."+QString::number(packet.info.FW_patch); - info.hardware_version = QString::number(packet.info.hardware_version)+QString(packet.info.HW_Revision); + info.hardware_version = hardwareVersionToString(packet.info.hardware_version)+" Rev."+QString(packet.info.HW_Revision); info.supportedFeatures = { Feature::VNA, Feature::VNAFrequencySweep, Feature::VNALogSweep, Feature::VNAPowerSweep, Feature::VNAZeroSpan, Feature::Generator, @@ -658,11 +660,29 @@ void LibreVNADriver::updateIFFrequencies() SendPacket(p); } +QString LibreVNADriver::hardwareVersionToString(uint8_t version) +{ + switch(version) { + case 1: return "1"; + case 255: return "PT"; + default: return "Unknown"; + } +} + unsigned int LibreVNADriver::getMaxAmplitudePoints() const { return limits_maxAmplitudePoints; } +QString LibreVNADriver::getFirmwareMagicString() +{ + switch(hardwareVersion) { + case 1: return "VNA!"; + case 255: return "VNPT"; + default: return "XXXX"; + } +} + bool LibreVNADriver::sendWithoutPayload(Protocol::PacketType type, std::function cb) { Protocol::PacketInfo p; diff --git a/Software/PC_Application/LibreVNA-GUI/Device/LibreVNA/librevnadriver.h b/Software/PC_Application/LibreVNA-GUI/Device/LibreVNA/librevnadriver.h index 3a4167a..44bfdfa 100644 --- a/Software/PC_Application/LibreVNA-GUI/Device/LibreVNA/librevnadriver.h +++ b/Software/PC_Application/LibreVNA-GUI/Device/LibreVNA/librevnadriver.h @@ -182,6 +182,8 @@ public: unsigned int getMaxAmplitudePoints() const; + QString getFirmwareMagicString(); + signals: void receivedAnswer(const LibreVNADriver::TransmissionResult &result); void receivedPacket(const Protocol::PacketInfo& packet); @@ -190,10 +192,12 @@ protected slots: void handleReceivedPacket(const Protocol::PacketInfo& packet); protected: void updateIFFrequencies(); + QString hardwareVersionToString(uint8_t version); bool connected; QString serial; Info info; + uint8_t hardwareVersion; unsigned int limits_maxAmplitudePoints; Protocol::DeviceStatusV1 lastStatus;