diff --git a/gui/gui_about.cpp b/gui/gui_about.cpp index bcbee0b..49ebfbb 100644 --- a/gui/gui_about.cpp +++ b/gui/gui_about.cpp @@ -69,6 +69,12 @@ mUpdateButton->Enable(false); main_sizer->Add(mUpdateButton, 0, wxALL | wxEXPAND, 5); +#ifdef DEBUG + main_sizer->AddSpacer(20); + mUpdateFromFileButton = new wxButton(this, ID_BUTTON_UPDATE_FROM_FILE, wxT("Update From File"), wxDefaultPosition, wxSize(120, 40)); + main_sizer->Add(mUpdateFromFileButton, 0, wxALL | wxEXPAND, 5); +#endif + main_sizer->SetSizeHints(this); Centre(); @@ -174,12 +180,89 @@ wxT("Confirmation"), wxOK | wxCANCEL | wxICON_QUESTION | wxSTAY_ON_TOP | wxCENTRE); if (msg.ShowModal() == wxID_OK) - { UpdateFirmware(); - } } +#ifdef DEBUG + +void GuiAbout::OnButtonUpdateFromFileClicked(wxCommandEvent &event) +{ + wxFileDialog dialog(this, wxT("Open DFU file"), "", "", "DFU files (*.bin|*.bin", wxFD_OPEN | wxFD_FILE_MUST_EXIST); + if (dialog.ShowModal() == wxID_CANCEL) + return; + + FILE* f = fopen(dialog.GetPath().mb_str(), "rb"); + if (f == nullptr) + { + wxMessageDialog msg(this, wxT("Cannot open DFU file."), wxT("Error"), wxOK | wxSTAY_ON_TOP | wxCENTRE | wxICON_ERROR); + msg.ShowModal(); + return; + } + + if (fseek(f, 0, SEEK_END) != 0) + { + fclose(f); + wxMessageDialog msg(this, wxT("Cannot open DFU file."), wxT("Error"), wxOK | wxSTAY_ON_TOP | wxCENTRE | wxICON_ERROR); + msg.ShowModal(); + return; + } + + ssize_t fsize = ftell(f); + if (fsize < 0) + { + fclose(f); + wxMessageDialog msg(this, wxT("Cannot open DFU file."), wxT("Error"), wxOK | wxSTAY_ON_TOP | wxCENTRE | wxICON_ERROR); + msg.ShowModal(); + return; + } + + if (fsize == 0) + { + wxMessageDialog msg(this, wxT("DFU file is empty."), wxT("Error"), wxOK | wxSTAY_ON_TOP | wxCENTRE | wxICON_ERROR); + msg.ShowModal(); + fclose(f); + return; + } + + if (fseek(f, 0, SEEK_SET) != 0) + { + fclose(f); + wxMessageDialog msg(this, wxT("Cannot open DFU file."), wxT("Error"), wxOK | wxSTAY_ON_TOP | wxCENTRE | wxICON_ERROR); + msg.ShowModal(); + return; + } + + mFirmwareImage.resize(fsize); + + if (fread(mFirmwareImage.data(), fsize, 1, f) != 1) + { + fclose(f); + wxMessageDialog msg(this, wxT("Cannot read DFU file."), wxT("Error"), wxOK | wxSTAY_ON_TOP | wxCENTRE | wxICON_ERROR); + msg.ShowModal(); + return; + } + + fclose(f); + + mFirmwareVersionBcd = Powermon::checkFirmwareImage(mFirmwareImage.data(), mFirmwareImage.size(), mPowermon.getLastDeviceInfo().hardware_revision_bcd); + if (mFirmwareVersionBcd == 0) + { + wxMessageDialog msg(this, wxT("Invalid DFU file."), wxT("Error"), wxOK | wxSTAY_ON_TOP | wxCENTRE | wxICON_ERROR); + msg.ShowModal(); + return; + } + + wxMessageDialog msg(this, wxT("You are about to update the firmware in your PowerMon device. \n" + "Do not disconnect the power, sit back and relax."), + wxT("Confirmation"), wxOK | wxCANCEL | wxICON_QUESTION | wxSTAY_ON_TOP | wxCENTRE); + + if (msg.ShowModal() == wxID_OK) + UpdateFirmware(); +} +#endif + + void GuiAbout::OnGuiEvent(GuiEvent &event) { event.process(); @@ -191,4 +274,8 @@ EVT_BUTTON(ID_BUTTON_UPDATE, GuiAbout::OnButtonUpdateClicked) +#ifdef DEBUG +EVT_BUTTON(ID_BUTTON_UPDATE_FROM_FILE, GuiAbout::OnButtonUpdateFromFileClicked) +#endif + END_EVENT_TABLE() diff --git a/gui/gui_about.h b/gui/gui_about.h index b536a39..eda432f 100644 --- a/gui/gui_about.h +++ b/gui/gui_about.h @@ -36,6 +36,9 @@ enum { ID_BUTTON_UPDATE = wxID_HIGHEST + 1, +#ifdef DEBUG + ID_BUTTON_UPDATE_FROM_FILE +#endif }; Powermon &mPowermon; @@ -45,7 +48,13 @@ wxStaticText* mStatusStatic; - wxButton *mUpdateButton; + wxButton* mUpdateButton; + +#ifdef DEBUG + wxButton* mUpdateFromFileButton; + + void OnButtonUpdateFromFileClicked(wxCommandEvent &event); +#endif void OnButtonUpdateClicked(wxCommandEvent &event); void OnClose(wxCloseEvent &event);