From 385375ea641acd713a04f173bbf48a69a1eb536d Mon Sep 17 00:00:00 2001 From: nanovna <54525305+nanovna@users.noreply.github.com> Date: Tue, 10 Mar 2020 10:17:46 +0000 Subject: [PATCH] fix firmware updater broken on windows * open(2) requires the non-portable O_BINARY on windows. Switch to fopen() with "b" mode instead. --- vna_qt/firmwareupdatedialog.C | 21 +++++++++++++++++---- vna_qt/firmwareupdatedialog.H | 5 ++++- vna_qt/firmwareupdater.C | 16 ++++++++++++++-- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/vna_qt/firmwareupdatedialog.C b/vna_qt/firmwareupdatedialog.C index 28a545a..f038202 100644 --- a/vna_qt/firmwareupdatedialog.C +++ b/vna_qt/firmwareupdatedialog.C @@ -22,14 +22,21 @@ FirmwareUpdateDialog::~FirmwareUpdateDialog() void FirmwareUpdateDialog::beginUploadFirmware(string dev, string file) { ui->buttonBox->setDisabled(true); - _fd = ::open(file.c_str(), O_RDONLY); + + /*_fd = ::open(file.c_str(), O_RDONLY); if(_fd < 0) + throw runtime_error(strerror(errno));*/ + + filePtr = fopen(file.c_str(), "rb"); + if(filePtr == nullptr) throw runtime_error(strerror(errno)); + updater = new FirmwareUpdater(); updater->open(dev.c_str()); updater->beginUploadFirmware(0x08004000, [this](uint8_t* buf, int len) { - return int(::read(_fd, buf, size_t(len))); + //return int(::read(_fd, buf, size_t(len))); + return int(fread(buf, 1, size_t(len), this->filePtr)); }, [this](int progress){ QMetaObject::invokeMethod(this, "updateProgress", Qt::QueuedConnection, Q_ARG(int, progress)); @@ -46,6 +53,8 @@ void FirmwareUpdateDialog::updateProgress(int progress) { delete updater; updater = nullptr; + fclose(filePtr); + if(ex != nullptr) { QString msg = "An error occurred during firmware update:\n\n"; msg += ex->what(); @@ -53,10 +62,14 @@ void FirmwareUpdateDialog::updateProgress(int progress) { QMessageBox::critical(this, "Error", msg); this->accept(); } else { - ui->l_progress->setText("Done"); + ui->l_progress->setText("Done\n" + ui->l_progress->text()); ui->buttonBox->setDisabled(false); } return; } - ui->l_progress->setText(qs(ssprintf(128, "%d KiB", progress/1024))); + string msg; + if(progress < 1024) + msg = ssprintf(128, "%d bytes", progress); + else msg = ssprintf(128, "%d KiB", progress/1024); + ui->l_progress->setText(qs(msg)); } diff --git a/vna_qt/firmwareupdatedialog.H b/vna_qt/firmwareupdatedialog.H index 73b3fc6..d214828 100644 --- a/vna_qt/firmwareupdatedialog.H +++ b/vna_qt/firmwareupdatedialog.H @@ -3,6 +3,8 @@ #include #include +#include + using namespace std; namespace Ui { class FirmwareUpdateDialog; @@ -24,7 +26,8 @@ public slots: private: Ui::FirmwareUpdateDialog *ui; FirmwareUpdater* updater = nullptr; - int _fd = -1; + //int _fd = -1; + FILE* filePtr = nullptr; }; #endif // FIRMWAREUPDATEDIALOG_H diff --git a/vna_qt/firmwareupdater.C b/vna_qt/firmwareupdater.C index e1f758b..4f10fc5 100644 --- a/vna_qt/firmwareupdater.C +++ b/vna_qt/firmwareupdater.C @@ -156,14 +156,20 @@ void* FirmwareUpdater::flashThread() { auto br = _reader(buf, bufSize); if(br < 0) goto fail; - if(br == 0) + if(br == 0) { + if(progress < 1024) { + auto ex = new runtime_error("EOF reading from file at " + to_string(progress) + " bytes"); + _cb(-1); + return ex; + } break; + } + progress += br; int res = _sendBytes(buf, int(br)); if(res < 0) goto fail; - progress += br; auto t = time(nullptr); if(t - lastNotify >= 1) { lastNotify = t; @@ -177,6 +183,12 @@ void* FirmwareUpdater::flashThread() { outstanding--; } } + if(progress == 0) { + auto ex = new runtime_error("Read 0 bytes from file!"); + _cb(-1); + return ex; + } + _cb(progress); while(outstanding > 0) { if(_waitSend() < 0) goto fail;