1 // Copyright (C) 2014 Michael McMaster <michael@codesrc.com>
3 // This file is part of SCSI2SD.
5 // SCSI2SD is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // SCSI2SD is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with SCSI2SD. If not, see <http://www.gnu.org/licenses/>.
19 // For compilers that support precompilation, includes "wx/wx.h".
20 #include <wx/wxprec.h>
25 #include <wx/filedlg.h>
26 #include <wx/notebook.h>
27 #include <wx/progdlg.h>
29 #include <wx/windowptr.h>
30 #include <wx/thread.h>
32 #include "ConfigUtil.hh"
33 #include "TargetPanel.hh"
34 #include "SCSI2SD_Bootloader.hh"
35 #include "SCSI2SD_HID.hh"
36 #include "Firmware.hh"
43 #if __cplusplus >= 201103L
46 using std::shared_ptr;
50 using std::tr1::shared_ptr;
54 using namespace SCSI2SD;
59 void setProgressDialog(
60 const wxWindowPtr<wxGenericProgressDialog>& dlg,
63 myProgressDialog = dlg;
68 void clearProgressDialog()
70 myProgressDialog.reset();
73 void update(unsigned char arrayId, unsigned short rowNum)
75 if (!myProgressDialog) return;
80 ss << "Writing flash array " <<
81 static_cast<int>(arrayId) << " row " <<
82 static_cast<int>(rowNum);
83 std::clog << ss.str() << std::endl;
84 myProgressDialog->Update(myNumRows, ss.str());
88 wxWindowPtr<wxGenericProgressDialog> myProgressDialog;
92 static ProgressWrapper TheProgressWrapper;
95 void ProgressUpdate(unsigned char arrayId, unsigned short rowNum)
97 TheProgressWrapper.update(arrayId, rowNum);
106 TimerLock(wxTimer* timer) :
108 myInterval(myTimer->GetInterval())
115 if (myTimer && myInterval > 0)
117 myTimer->Start(myInterval);
125 class AppFrame : public wxFrame
129 wxFrame(NULL, wxID_ANY, "scsi2sd-util", wxPoint(50, 50), wxSize(600, 650)),
130 myInitialConfig(false),
133 wxMenu *menuFile = new wxMenu();
137 "Load default configuration options.");
140 "&Upgrade Firmware...",
141 "Upgrade or inspect device firmware version.");
142 menuFile->AppendSeparator();
143 menuFile->Append(wxID_EXIT);
144 wxMenu *menuHelp = new wxMenu();
145 menuHelp->Append(wxID_ABOUT);
146 wxMenuBar *menuBar = new wxMenuBar();
147 menuBar->Append( menuFile, "&File" );
148 menuBar->Append( menuHelp, "&Help" );
149 SetMenuBar( menuBar );
152 SetStatusText( "Searching for SCSI2SD" );
155 wxPanel* cfgPanel = new wxPanel(this);
156 wxFlexGridSizer *fgs = new wxFlexGridSizer(2, 1, 5, 5);
157 wxNotebook* tabs = new wxNotebook(cfgPanel, ID_Notebook);
159 for (int i = 0; i < MAX_SCSI_TARGETS; ++i)
161 TargetPanel* target =
162 new TargetPanel(tabs, ConfigUtil::Default(i));
163 myTargets.push_back(target);
164 std::stringstream ss;
165 ss << "Device " << (i + 1);
166 tabs->AddPage(target, ss.str());
171 wxPanel* btnPanel = new wxPanel(cfgPanel);
172 wxFlexGridSizer *btnFgs = new wxFlexGridSizer(1, 2, 5, 5);
173 btnFgs->Add(new wxButton(btnPanel, ID_BtnLoad, wxT("Load from device")));
175 new wxButton(btnPanel, ID_BtnSave, wxT("Save to device"));
176 btnFgs->Add(mySaveButton);
178 wxBoxSizer* hbox = new wxBoxSizer(wxHORIZONTAL);
179 hbox->Add(btnFgs, 1, wxALL | wxEXPAND, 15);
180 btnPanel->SetSizer(hbox);
185 wxBoxSizer* hbox = new wxBoxSizer(wxHORIZONTAL);
186 hbox->Add(fgs, 1, wxALL | wxEXPAND, 15);
187 cfgPanel->SetSizer(hbox);
191 myTimer = new wxTimer(this, ID_Timer);
192 myTimer->Start(1000); //ms
196 std::vector<TargetPanel*> myTargets;
197 wxButton* mySaveButton;
199 shared_ptr<HID> myHID;
200 shared_ptr<Bootloader> myBootloader;
201 bool myInitialConfig;
203 uint8_t myTickCounter;
205 void onConfigChanged(wxCommandEvent& event)
214 // Check for duplicate SCSI IDs
215 std::set<uint8_t> enabledID;
217 // Check for overlapping SD sectors.
218 std::vector<std::pair<uint32_t, uint64_t> > sdSectors;
220 bool isTargetEnabled = false; // Need at least one enabled
222 for (size_t i = 0; i < myTargets.size(); ++i)
224 valid = myTargets[i]->evaluate() && valid;
226 if (myTargets[i]->isEnabled())
228 isTargetEnabled = true;
229 uint8_t scsiID = myTargets[i]->getSCSIId();
230 if (enabledID.find(scsiID) != enabledID.end())
232 myTargets[i]->setDuplicateID(true);
237 enabledID.insert(scsiID);
238 myTargets[i]->setDuplicateID(false);
241 auto sdSectorRange = myTargets[i]->getSDSectorRange();
242 for (auto it(sdSectors.begin()); it != sdSectors.end(); ++it)
244 if (sdSectorRange.first < it->second &&
245 sdSectorRange.second > it->first)
248 myTargets[i]->setSDSectorOverlap(true);
252 myTargets[i]->setSDSectorOverlap(false);
255 sdSectors.push_back(sdSectorRange);
259 myTargets[i]->setDuplicateID(false);
260 myTargets[i]->setSDSectorOverlap(false);
264 valid = valid && isTargetEnabled; // Need at least one.
266 mySaveButton->Enable(valid && myHID);
272 ID_ConfigDefaults = wxID_HIGHEST + 1,
280 void OnID_ConfigDefaults(wxCommandEvent& event)
282 for (size_t i = 0; i < myTargets.size(); ++i)
284 myTargets[i]->setConfig(ConfigUtil::Default(i));
288 void OnID_Firmware(wxCommandEvent& event)
290 TimerLock lock(myTimer);
294 void doFirmwareUpdate()
298 "Load firmware file",
301 "SCSI2SD Firmware files (*.cyacd)|*.cyacd",
302 wxFD_OPEN | wxFD_FILE_MUST_EXIST);
303 if (dlg.ShowModal() == wxID_CANCEL) return;
305 std::string filename(dlg.GetPath());
309 if (!myHID) myHID.reset(HID::Open());
312 std::clog << "Resetting SCSI2SD into bootloader" << std::endl;
314 myHID->enterBootloader();
320 // Verify the USB HID connection is valid
321 if (!myBootloader->ping())
323 myBootloader.reset();
327 for (int i = 0; !myBootloader && (i < 10); ++i)
329 std::clog << "Searching for bootloader" << std::endl;
330 myBootloader.reset(Bootloader::Open());
334 catch (std::exception& e)
336 std::clog << e.what() << std::endl;
338 SetStatusText(e.what());
340 myBootloader.reset();
348 wxOK | wxICON_ERROR);
353 if (!myBootloader->isCorrectFirmware(filename))
355 // TODO allow "force" option
359 wxOK | wxICON_ERROR);
363 int totalFlashRows = 0;
366 Firmware firmware(filename);
367 totalFlashRows = firmware.totalFlashRows();
369 catch (std::exception& e)
371 SetStatusText(e.what());
372 std::stringstream msg;
373 msg << "Could not open firmware file: " << e.what();
377 wxOK | wxICON_ERROR);
382 wxWindowPtr<wxGenericProgressDialog> progress(
383 new wxGenericProgressDialog(
390 TheProgressWrapper.setProgressDialog(progress, totalFlashRows);
393 std::clog << "Upgrading firmware from file: " << filename << std::endl;
397 myBootloader->load(filename, &ProgressUpdate);
398 TheProgressWrapper.clearProgressDialog();
401 "Firmware update successful",
404 SetStatusText("Firmware update successful");
408 myBootloader.reset();
410 catch (std::exception& e)
412 TheProgressWrapper.clearProgressDialog();
413 SetStatusText(e.what());
415 myBootloader.reset();
418 "Firmware Update Failed",
420 wxOK | wxICON_ERROR);
424 void OnID_Timer(wxTimerEvent& event)
426 // Check if we are connected to the HID device.
427 // AND/or bootloader device.
432 // Verify the USB HID connection is valid
433 if (!myBootloader->ping())
435 myBootloader.reset();
441 myBootloader.reset(Bootloader::Open());
445 SetStatusText(wxT("SCSI2SD Bootloader Ready"));
451 // Verify the USB HID connection is valid
460 myHID.reset(HID::Open());
463 uint16_t version = myHID->getFirmwareVersion();
466 // Oh dear, old firmware
467 SetStatusText(wxT("Firmware update required"));
472 SetStatusText(wxT("SCSI2SD Ready"));
474 if (!myInitialConfig)
476 wxCommandEvent loadEvent(wxEVT_NULL, ID_BtnLoad);
477 GetEventHandler()->AddPendingEvent(loadEvent);
483 char ticks[] = {'/', '-', '\\', '|'};
484 std::stringstream ss;
485 ss << "Searching for SCSI2SD device " << ticks[myTickCounter % sizeof(ticks)];
487 SetStatusText(ss.str());
491 catch (std::runtime_error& e)
493 std::clog << e.what() << std::endl;
494 SetStatusText(e.what());
500 void doLoad(wxCommandEvent& event)
502 TimerLock lock(myTimer);
505 std::clog << "Loading configuration" << std::endl;
507 wxWindowPtr<wxGenericProgressDialog> progress(
508 new wxGenericProgressDialog(
509 "Load config settings",
510 "Loading config settings",
513 wxPD_APP_MODAL | wxPD_CAN_ABORT)
516 int flashRow = SCSI_CONFIG_0_ROW;
517 int currentProgress = 0;
518 int totalProgress = myTargets.size() * SCSI_CONFIG_ROWS;
520 i < myTargets.size();
521 ++i, flashRow += SCSI_CONFIG_ROWS)
523 std::vector<uint8_t> raw(sizeof(TargetConfig));
525 for (size_t j = 0; j < SCSI_CONFIG_ROWS; ++j)
527 std::stringstream ss;
528 ss << "Reading flash array " << SCSI_CONFIG_ARRAY <<
529 " row " << (flashRow + j);
530 SetStatusText(ss.str());
531 std::clog << ss.str() << std::endl;
532 currentProgress += 1;
533 if (!progress->Update(
534 (100 * currentProgress) / totalProgress,
542 std::vector<uint8_t> flashData;
547 SCSI_CONFIG_ARRAY, flashRow + j, flashData);
550 catch (std::runtime_error& e)
552 SetStatusText(e.what());
559 &raw[j * SCSI_CONFIG_ROW_SIZE]);
561 myTargets[i]->setConfig(ConfigUtil::fromBytes(&raw[0]));
564 myInitialConfig = true;
565 SetStatusText("Load Complete");
566 std::clog << "Load Complete" << std::endl;
567 while (progress->Update(100, "Load Complete"))
569 // Wait for the user to click "Close"
575 SetStatusText("Load failed");
576 std::clog << "Load failed" << std::endl;
577 while (progress->Update(100, "Load failed"))
579 // Wait for the user to click "Close"
585 SetStatusText("Load Aborted");
586 std::clog << "Load Aborted" << std::endl;
592 void doSave(wxCommandEvent& event)
594 TimerLock lock(myTimer);
597 std::clog << "Saving configuration" << std::endl;
599 wxWindowPtr<wxGenericProgressDialog> progress(
600 new wxGenericProgressDialog(
601 "Save config settings",
602 "Saving config settings",
605 wxPD_APP_MODAL | wxPD_CAN_ABORT)
608 int flashRow = SCSI_CONFIG_0_ROW;
609 int currentProgress = 0;
610 int totalProgress = myTargets.size() * SCSI_CONFIG_ROWS;
612 i < myTargets.size();
613 ++i, flashRow += SCSI_CONFIG_ROWS)
615 TargetConfig config(myTargets[i]->getConfig());
616 std::vector<uint8_t> raw(ConfigUtil::toBytes(config));
618 for (size_t j = 0; j < SCSI_CONFIG_ROWS; ++j)
620 std::stringstream ss;
621 ss << "Programming flash array " << SCSI_CONFIG_ARRAY <<
622 " row " << (flashRow + j);
623 SetStatusText(ss.str());
624 std::clog << ss.str() << std::endl;
625 currentProgress += 1;
626 if (!progress->Update(
627 (100 * currentProgress) / totalProgress,
635 std::vector<uint8_t> flashData(SCSI_CONFIG_ROW_SIZE, 0);
637 &raw[j * SCSI_CONFIG_ROW_SIZE],
638 &raw[(1+j) * SCSI_CONFIG_ROW_SIZE],
642 myHID->writeFlashRow(
643 SCSI_CONFIG_ARRAY, flashRow + j, flashData);
645 catch (std::runtime_error& e)
647 std::clog << e.what() << std::endl;
648 SetStatusText(e.what());
654 // Reboot so new settings take effect.
655 myHID->enterBootloader();
658 SetStatusText("Save Complete");
659 std::clog << "Save Complete" << std::endl;
660 while (progress->Update(100, "Save Complete"))
662 // Wait for the user to click "Close"
668 SetStatusText("Save failed");
669 std::clog << "Save failed" << std::endl;
670 while (progress->Update(100, "Save failed"))
672 // Wait for the user to click "Close"
678 SetStatusText("Save Aborted");
679 std::clog << "Save Aborted" << std::endl;
682 (void) true; // empty statement.
685 void OnExit(wxCommandEvent& event)
689 void OnAbout(wxCommandEvent& event)
692 "SCSI2SD (scsi2sd-util)\n"
693 "Copyright (C) 2014 Michael McMaster <michael@codesrc.com>\n"
695 "This program is free software: you can redistribute it and/or modify\n"
696 "it under the terms of the GNU General Public License as published by\n"
697 "the Free Software Foundation, either version 3 of the License, or\n"
698 "(at your option) any later version.\n"
700 "This program is distributed in the hope that it will be useful,\n"
701 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
702 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
703 "GNU General Public License for more details.\n"
705 "You should have received a copy of the GNU General Public License\n"
706 "along with this program. If not, see <http://www.gnu.org/licenses/>.\n",
708 "About scsi2sd-util", wxOK | wxICON_INFORMATION );
711 wxDECLARE_EVENT_TABLE();
714 wxBEGIN_EVENT_TABLE(AppFrame, wxFrame)
715 EVT_MENU(AppFrame::ID_ConfigDefaults, AppFrame::OnID_ConfigDefaults)
716 EVT_MENU(AppFrame::ID_Firmware, AppFrame::OnID_Firmware)
717 EVT_MENU(wxID_EXIT, AppFrame::OnExit)
718 EVT_MENU(wxID_ABOUT, AppFrame::OnAbout)
720 EVT_TIMER(AppFrame::ID_Timer, AppFrame::OnID_Timer)
722 EVT_COMMAND(wxID_ANY, ConfigChangedEvent, AppFrame::onConfigChanged)
724 EVT_BUTTON(ID_BtnSave, AppFrame::doSave)
725 EVT_BUTTON(ID_BtnLoad, AppFrame::doLoad)
732 class App : public wxApp
735 virtual bool OnInit()
737 AppFrame* frame = new AppFrame();
746 wxIMPLEMENT_APP(App);