Skip to content

Commit

Permalink
fix firmware updater broken on windows
Browse files Browse the repository at this point in the history
* open(2) requires the non-portable O_BINARY on windows.
Switch to fopen() with "b" mode instead.
  • Loading branch information
nanovna committed Mar 10, 2020
1 parent 1d8021d commit 385375e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
21 changes: 17 additions & 4 deletions vna_qt/firmwareupdatedialog.C
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -46,17 +53,23 @@ 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();
msg += "\n\nPlease retry the update.\nYou can re-enter DFU mode by holding down the JOG LEFT button and power cycling the device.";
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));
}
5 changes: 4 additions & 1 deletion vna_qt/firmwareupdatedialog.H
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include <QDialog>
#include <string>
#include <stdio.h>

using namespace std;
namespace Ui {
class FirmwareUpdateDialog;
Expand All @@ -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
16 changes: 14 additions & 2 deletions vna_qt/firmwareupdater.C
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 385375e

Please sign in to comment.