TargetPanel.cc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. // Copyright (C) 2014 Michael McMaster <michael@codesrc.com>
  2. //
  3. // This file is part of SCSI2SD.
  4. //
  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.
  9. //
  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.
  14. //
  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/>.
  17. // For compilers that support precompilation, includes "wx/wx.h".
  18. #include <wx/wxprec.h>
  19. #ifndef WX_PRECOMP
  20. #include <wx/wx.h>
  21. #endif
  22. #include <wx/wrapsizer.h>
  23. #include "ConfigUtil.hh"
  24. #include "TargetPanel.hh"
  25. #include <limits>
  26. #include <sstream>
  27. #include <math.h>
  28. #include <string.h>
  29. using namespace SCSI2SD;
  30. wxDEFINE_EVENT(SCSI2SD::ConfigChangedEvent, wxCommandEvent);
  31. namespace
  32. {
  33. template<typename IntType, class WXCTRL> std::pair<IntType, bool>
  34. CtrlGetValue(WXCTRL* ctrl)
  35. {
  36. IntType value;
  37. std::stringstream conv;
  38. conv << ctrl->GetValue();
  39. conv >> value;
  40. return std::make_pair(value, static_cast<bool>(conv));
  41. }
  42. void CtrlGetFixedString(wxTextEntry* ctrl, char* dest, size_t len)
  43. {
  44. memset(dest, ' ', len);
  45. std::string str(ctrl->GetValue().ToAscii());
  46. // Don't use strncpy - we need to avoid NULL's
  47. memcpy(dest, str.c_str(), std::min(len, str.size()));
  48. }
  49. bool CtrlIsAscii(wxTextEntry* ctrl)
  50. {
  51. return ctrl->GetValue().IsAscii();
  52. }
  53. }
  54. TargetPanel::TargetPanel(wxWindow* parent, const TargetConfig& initialConfig) :
  55. wxPanel(parent),
  56. myParent(parent),
  57. myAutoStartSector(0),
  58. myStartSDSectorValidator(new wxIntegerValidator<uint32_t>),
  59. mySectorSizeValidator(new wxIntegerValidator<uint16_t>),
  60. myNumSectorValidator(new wxIntegerValidator<uint32_t>),
  61. mySizeValidator(new wxFloatingPointValidator<float>(2))
  62. {
  63. wxFlexGridSizer *fgs = new wxFlexGridSizer(14, 3, 9, 25);
  64. fgs->Add(new wxStaticText(this, wxID_ANY, wxT("")));
  65. myEnableCtrl =
  66. new wxCheckBox(
  67. this,
  68. ID_enableCtrl,
  69. wxT("Enable SCSI Target"));
  70. fgs->Add(myEnableCtrl);
  71. // Set a non-visible string to leave room in the column for future messages
  72. fgs->Add(new wxStaticText(this, wxID_ANY, wxT(" ")));
  73. Bind(wxEVT_CHECKBOX, &TargetPanel::onInput<wxCommandEvent>, this, ID_enableCtrl);
  74. fgs->Add(new wxStaticText(this, wxID_ANY, wxT("SCSI ID")));
  75. myScsiIdCtrl =
  76. new wxSpinCtrl
  77. (this,
  78. ID_scsiIdCtrl,
  79. wxEmptyString,
  80. wxDefaultPosition,
  81. wxDefaultSize,
  82. wxSP_WRAP | wxSP_ARROW_KEYS,
  83. 0,
  84. 7,
  85. 0);
  86. fgs->Add(myScsiIdCtrl);
  87. myScsiIdMsg = new wxStaticText(this, wxID_ANY, wxT(""));
  88. fgs->Add(myScsiIdMsg);
  89. Bind(wxEVT_SPINCTRL, &TargetPanel::onInput<wxSpinEvent>, this, ID_scsiIdCtrl);
  90. fgs->Add(new wxStaticText(this, wxID_ANY, wxT("Device Type")));
  91. wxString deviceTypes[] =
  92. {
  93. wxT("Hard Drive"),
  94. wxT("Removable"),
  95. wxT("CDROM"),
  96. wxT("3.5\" Floppy"),
  97. wxT("Magneto optical")
  98. };
  99. myDeviceTypeCtrl =
  100. new wxChoice(
  101. this,
  102. ID_deviceTypeCtrl,
  103. wxDefaultPosition,
  104. wxDefaultSize,
  105. sizeof(deviceTypes) / sizeof(wxString),
  106. deviceTypes
  107. );
  108. myDeviceTypeCtrl->SetSelection(0);
  109. fgs->Add(myDeviceTypeCtrl);
  110. fgs->Add(new wxStaticText(this, wxID_ANY, wxT("")));
  111. Bind(wxEVT_CHOICE, &TargetPanel::onInput<wxCommandEvent>, this, ID_deviceTypeCtrl);
  112. fgs->Add(new wxStaticText(this, wxID_ANY, wxT("")));
  113. myParityCtrl =
  114. new wxCheckBox(
  115. this,
  116. ID_parityCtrl,
  117. wxT("Enable Parity"));
  118. myParityCtrl->SetToolTip(wxT("Enable to require valid SCSI parity bits when receiving data. Some hosts don't provide parity. SCSI2SD always outputs valid parity bits."));
  119. fgs->Add(myParityCtrl);
  120. fgs->Add(new wxStaticText(this, wxID_ANY, wxT("")));
  121. Bind(wxEVT_CHECKBOX, &TargetPanel::onInput<wxCommandEvent>, this, ID_parityCtrl);
  122. fgs->Add(new wxStaticText(this, wxID_ANY, wxT("")));
  123. myUnitAttCtrl =
  124. new wxCheckBox(
  125. this,
  126. ID_unitAttCtrl,
  127. wxT("Enable Unit Attention"));
  128. myUnitAttCtrl->SetToolTip(wxT("Enable this to inform the host of changes after hot-swapping SD cards. Causes problems with Mac Plus."));
  129. fgs->Add(myUnitAttCtrl);
  130. fgs->Add(new wxStaticText(this, wxID_ANY, wxT("")));
  131. Bind(wxEVT_CHECKBOX, &TargetPanel::onInput<wxCommandEvent>, this, ID_unitAttCtrl);
  132. fgs->Add(new wxStaticText(this, wxID_ANY, wxT("")));
  133. myScsi2Ctrl =
  134. new wxCheckBox(
  135. this,
  136. ID_scsi2Ctrl,
  137. wxT("Enable SCSI2 Mode"));
  138. myScsi2Ctrl->SetToolTip(wxT("Enable high-performance mode. May cause problems with SASI/SCSI1 hosts."));
  139. fgs->Add(myScsi2Ctrl);
  140. fgs->Add(new wxStaticText(this, wxID_ANY, wxT("")));
  141. Bind(wxEVT_CHECKBOX, &TargetPanel::onInput<wxCommandEvent>, this, ID_scsi2Ctrl);
  142. fgs->Add(new wxStaticText(this, wxID_ANY, wxT("SD card start sector")));
  143. wxWrapSizer* startContainer = new wxWrapSizer();
  144. myStartSDSectorCtrl =
  145. new wxTextCtrl(
  146. this,
  147. ID_startSDSectorCtrl,
  148. "0",
  149. wxDefaultPosition,
  150. wxDefaultSize,
  151. 0,
  152. *myStartSDSectorValidator);
  153. myStartSDSectorCtrl->SetToolTip(wxT("Supports multiple SCSI targets "
  154. "on a single memory card. In units of 512-byte sectors."));
  155. startContainer->Add(myStartSDSectorCtrl);
  156. myAutoStartSectorCtrl =
  157. new wxCheckBox(
  158. this,
  159. ID_autoStartSectorCtrl,
  160. wxT("Auto"));
  161. startContainer->Add(myAutoStartSectorCtrl);
  162. Bind(wxEVT_CHECKBOX, &TargetPanel::onInput<wxCommandEvent>, this, ID_autoStartSectorCtrl);
  163. fgs->Add(startContainer);
  164. myStartSDSectorMsg = new wxStaticText(this, wxID_ANY, wxT(""));
  165. fgs->Add(myStartSDSectorMsg);
  166. Bind(wxEVT_TEXT, &TargetPanel::onInput<wxCommandEvent>, this, ID_startSDSectorCtrl);
  167. fgs->Add(new wxStaticText(this, wxID_ANY, wxT("Sector size (bytes)")));
  168. mySectorSizeCtrl =
  169. new wxTextCtrl(
  170. this,
  171. ID_sectorSizeCtrl,
  172. "512",
  173. wxDefaultPosition,
  174. wxDefaultSize,
  175. 0,
  176. *mySectorSizeValidator);
  177. mySectorSizeCtrl->SetToolTip(wxT("Between 64 and 8192. Default of 512 is suitable in most cases."));
  178. fgs->Add(mySectorSizeCtrl);
  179. mySectorSizeMsg = new wxStaticText(this, wxID_ANY, wxT(""));
  180. fgs->Add(mySectorSizeMsg);
  181. Bind(wxEVT_TEXT, &TargetPanel::onSizeInput, this, ID_sectorSizeCtrl);
  182. fgs->Add(new wxStaticText(this, wxID_ANY, wxT("Sector count")));
  183. myNumSectorCtrl =
  184. new wxTextCtrl(
  185. this,
  186. ID_numSectorCtrl,
  187. "",
  188. wxDefaultPosition,
  189. wxDefaultSize,
  190. 0,
  191. *myNumSectorValidator);
  192. myNumSectorCtrl->SetToolTip(wxT("Number of sectors (device size)"));
  193. fgs->Add(myNumSectorCtrl);
  194. myNumSectorMsg = new wxStaticText(this, wxID_ANY, wxT(""));
  195. fgs->Add(myNumSectorMsg);
  196. Bind(wxEVT_TEXT, &TargetPanel::onSizeInput, this, ID_numSectorCtrl);
  197. fgs->Add(new wxStaticText(this, wxID_ANY, wxT("Device size")));
  198. wxWrapSizer* sizeContainer = new wxWrapSizer();
  199. mySizeCtrl =
  200. new wxTextCtrl(
  201. this,
  202. ID_sizeCtrl,
  203. "",
  204. wxDefaultPosition,
  205. wxDefaultSize,
  206. 0,
  207. *mySizeValidator);
  208. mySizeCtrl->SetToolTip(wxT("Device size"));
  209. sizeContainer->Add(mySizeCtrl);
  210. wxString units[] = {wxT("KB"), wxT("MB"), wxT("GB")};
  211. mySizeUnitCtrl =
  212. new wxChoice(
  213. this,
  214. ID_sizeUnitCtrl,
  215. wxDefaultPosition,
  216. wxDefaultSize,
  217. sizeof(units) / sizeof(wxString),
  218. units
  219. );
  220. sizeContainer->Add(mySizeUnitCtrl);
  221. fgs->Add(sizeContainer);
  222. fgs->Add(new wxStaticText(this, wxID_ANY, wxT("")));
  223. Bind(wxEVT_TEXT, &TargetPanel::onSizeInput, this, ID_sizeCtrl);
  224. Bind(wxEVT_CHOICE, &TargetPanel::onSizeInput, this, ID_sizeUnitCtrl);
  225. fgs->Add(new wxStaticText(this, wxID_ANY, wxT("Vendor")));
  226. myVendorCtrl =
  227. new wxTextCtrl(
  228. this,
  229. ID_vendorCtrl,
  230. wxEmptyString,
  231. wxDefaultPosition,
  232. wxSize(GetCharWidth() * 10, -1));
  233. myVendorCtrl->SetMaxLength(8);
  234. myVendorCtrl->SetToolTip(wxT("SCSI Vendor string. eg. ' codesrc'"));
  235. fgs->Add(myVendorCtrl);
  236. myVendorMsg = new wxStaticText(this, wxID_ANY, wxT(""));
  237. fgs->Add(myVendorMsg);
  238. Bind(wxEVT_TEXT, &TargetPanel::onInput<wxCommandEvent>, this, ID_vendorCtrl);
  239. fgs->Add(new wxStaticText(this, wxID_ANY, wxT("Product ID")));
  240. myProductCtrl =
  241. new wxTextCtrl(
  242. this,
  243. ID_productCtrl,
  244. wxEmptyString,
  245. wxDefaultPosition,
  246. wxSize(GetCharWidth() * 17, -1));
  247. myProductCtrl->SetMaxLength(18);
  248. myProductCtrl->SetToolTip(wxT("SCSI Product ID string. eg. 'SCSI2SD'"));
  249. fgs->Add(myProductCtrl);
  250. myProductMsg = new wxStaticText(this, wxID_ANY, wxT(""));
  251. fgs->Add(myProductMsg);
  252. Bind(wxEVT_TEXT, &TargetPanel::onInput<wxCommandEvent>, this, ID_productCtrl);
  253. fgs->Add(new wxStaticText(this, wxID_ANY, wxT("Revision")));
  254. myRevisionCtrl =
  255. new wxTextCtrl(
  256. this,
  257. ID_revisionCtrl,
  258. wxEmptyString,
  259. wxDefaultPosition,
  260. wxSize(GetCharWidth() * 6, -1));
  261. myRevisionCtrl->SetMaxLength(4);
  262. myRevisionCtrl->SetToolTip(wxT("SCSI device revision string. eg. '3.5a'"));
  263. fgs->Add(myRevisionCtrl);
  264. myRevisionMsg = new wxStaticText(this, wxID_ANY, wxT(""));
  265. fgs->Add(myRevisionMsg);
  266. Bind(wxEVT_TEXT, &TargetPanel::onInput<wxCommandEvent>, this, ID_revisionCtrl);
  267. fgs->Add(new wxStaticText(this, wxID_ANY, wxT("Serial number")));
  268. mySerialCtrl =
  269. new wxTextCtrl(
  270. this,
  271. ID_serialCtrl,
  272. wxEmptyString,
  273. wxDefaultPosition,
  274. wxSize(GetCharWidth() * 18, -1));
  275. mySerialCtrl->SetMaxLength(16);
  276. mySerialCtrl->SetToolTip(wxT("SCSI serial number. eg. '13eab5632a'"));
  277. fgs->Add(mySerialCtrl);
  278. mySerialMsg = new wxStaticText(this, wxID_ANY, wxT(""));
  279. fgs->Add(mySerialMsg);
  280. Bind(wxEVT_TEXT, &TargetPanel::onInput<wxCommandEvent>, this, ID_serialCtrl);
  281. wxBoxSizer* hbox = new wxBoxSizer(wxHORIZONTAL);
  282. hbox->Add(fgs, 1, wxALL | wxEXPAND, 15);
  283. this->SetSizer(hbox);
  284. Centre();
  285. setConfig(initialConfig);
  286. evaluate();
  287. }
  288. bool
  289. TargetPanel::evaluate()
  290. {
  291. bool valid = true;
  292. std::stringstream conv;
  293. bool enabled = myEnableCtrl->IsChecked();
  294. {
  295. myScsiIdCtrl->Enable(enabled);
  296. myDeviceTypeCtrl->Enable(enabled);
  297. myParityCtrl->Enable(enabled);
  298. myUnitAttCtrl->Enable(enabled);
  299. myScsi2Ctrl->Enable(enabled);
  300. myStartSDSectorCtrl->Enable(enabled && !myAutoStartSectorCtrl->IsChecked());
  301. myAutoStartSectorCtrl->Enable(enabled);
  302. mySectorSizeCtrl->Enable(enabled);
  303. myNumSectorCtrl->Enable(enabled);
  304. mySizeCtrl->Enable(enabled);
  305. mySizeUnitCtrl->Enable(enabled);
  306. myVendorCtrl->Enable(enabled);
  307. myProductCtrl->Enable(enabled);
  308. myRevisionCtrl->Enable(enabled);
  309. mySerialCtrl->Enable(enabled);
  310. }
  311. switch (myDeviceTypeCtrl->GetSelection())
  312. {
  313. case CONFIG_OPTICAL:
  314. mySectorSizeCtrl->ChangeValue("2048");
  315. mySectorSizeCtrl->Enable(false);
  316. break;
  317. case CONFIG_FLOPPY_14MB:
  318. mySectorSizeCtrl->ChangeValue("512");
  319. mySectorSizeCtrl->Enable(false);
  320. myNumSectorCtrl->ChangeValue("2880");
  321. myNumSectorCtrl->Enable(false);
  322. mySizeUnitCtrl->Enable(false);
  323. mySizeCtrl->Enable(false);
  324. break;
  325. };
  326. evaluateSize();
  327. if (myAutoStartSectorCtrl->IsChecked())
  328. {
  329. std::stringstream ss; ss << myAutoStartSector;
  330. myStartSDSectorCtrl->ChangeValue(ss.str());
  331. }
  332. uint32_t startSDsector;
  333. {
  334. conv << myStartSDSectorCtrl->GetValue();
  335. conv >> startSDsector;
  336. }
  337. if (!conv)
  338. // TODO check if it is beyond the current SD card.
  339. {
  340. myStartSDSectorMsg->SetLabelMarkup(wxT("<span foreground='red' weight='bold'>Number &gt;= 0</span>"));
  341. valid = false;
  342. }
  343. else
  344. {
  345. myStartSDSectorMsg->SetLabelMarkup("");
  346. }
  347. conv.str(std::string()); conv.clear();
  348. uint16_t sectorSize(CtrlGetValue<uint16_t>(mySectorSizeCtrl).first);
  349. if (sectorSize < 64 || sectorSize > 8192)
  350. {
  351. mySectorSizeMsg->SetLabelMarkup(wxT("<span foreground='red' weight='bold'>Must be between 64 and 8192</span>"));
  352. valid = false;
  353. }
  354. else
  355. {
  356. mySectorSizeMsg->SetLabelMarkup("");
  357. }
  358. conv.str(std::string()); conv.clear();
  359. std::pair<uint32_t, bool> numSectors(CtrlGetValue<uint32_t>(myNumSectorCtrl));
  360. if (!numSectors.second ||
  361. numSectors.first == 0 ||
  362. !convertUnitsToSectors().second)
  363. {
  364. myNumSectorMsg->SetLabelMarkup(wxT("<span foreground='red' weight='bold'>Invalid size</span>"));
  365. valid = false;
  366. }
  367. else
  368. {
  369. myNumSectorMsg->SetLabelMarkup("");
  370. }
  371. if (!CtrlIsAscii(myVendorCtrl))
  372. {
  373. myVendorMsg->SetLabelMarkup(wxT("<span foreground='red' weight='bold'>Invalid characters</span>"));
  374. valid = false;
  375. }
  376. else
  377. {
  378. myVendorMsg->SetLabelMarkup("");
  379. }
  380. if (!CtrlIsAscii(myProductCtrl))
  381. {
  382. myProductMsg->SetLabelMarkup(wxT("<span foreground='red' weight='bold'>Invalid characters</span>"));
  383. valid = false;
  384. }
  385. else
  386. {
  387. myProductMsg->SetLabelMarkup("");
  388. }
  389. if (!CtrlIsAscii(myRevisionCtrl))
  390. {
  391. myRevisionMsg->SetLabelMarkup(wxT("<span foreground='red' weight='bold'>Invalid characters</span>"));
  392. valid = false;
  393. }
  394. else
  395. {
  396. myRevisionMsg->SetLabelMarkup("");
  397. }
  398. if (!CtrlIsAscii(mySerialCtrl))
  399. {
  400. mySerialMsg->SetLabelMarkup(wxT("<span foreground='red' weight='bold'>Invalid characters</span>"));
  401. valid = false;
  402. }
  403. else
  404. {
  405. mySerialMsg->SetLabelMarkup("");
  406. }
  407. return valid || !enabled;
  408. }
  409. template<typename EvtType> void
  410. TargetPanel::onInput(EvtType& event)
  411. {
  412. wxCommandEvent changeEvent(ConfigChangedEvent);
  413. wxPostEvent(myParent, changeEvent);
  414. }
  415. void
  416. TargetPanel::onSizeInput(wxCommandEvent& event)
  417. {
  418. if (event.GetId() != ID_numSectorCtrl)
  419. {
  420. std::stringstream ss;
  421. ss << convertUnitsToSectors().first;
  422. myNumSectorCtrl->ChangeValue(ss.str());
  423. }
  424. evaluateSize();
  425. onInput(event); // propagate
  426. }
  427. void
  428. TargetPanel::evaluateSize()
  429. {
  430. uint32_t numSectors;
  431. std::stringstream conv;
  432. conv << myNumSectorCtrl->GetValue();
  433. conv >> numSectors;
  434. conv.str(""); conv.clear();
  435. if (conv)
  436. {
  437. uint64_t bytes =
  438. uint64_t(numSectors) *
  439. CtrlGetValue<uint16_t>(mySectorSizeCtrl).first;
  440. if (bytes >= 1024 * 1024 * 1024)
  441. {
  442. conv << (bytes / (1024.0 * 1024 * 1024));
  443. mySizeUnitCtrl->SetSelection(UNIT_GB);
  444. }
  445. else if (bytes >= 1024 * 1024)
  446. {
  447. conv << (bytes / (1024.0 * 1024));
  448. mySizeUnitCtrl->SetSelection(UNIT_MB);
  449. }
  450. else
  451. {
  452. conv << (bytes / 1024.0);
  453. mySizeUnitCtrl->SetSelection(UNIT_KB);
  454. }
  455. mySizeCtrl->ChangeValue(conv.str());
  456. }
  457. }
  458. std::pair<uint32_t, bool>
  459. TargetPanel::convertUnitsToSectors() const
  460. {
  461. bool valid = true;
  462. uint64_t multiplier(0);
  463. switch (mySizeUnitCtrl->GetSelection())
  464. {
  465. case UNIT_KB: multiplier = 1024; break;
  466. case UNIT_MB: multiplier = 1024 * 1024; break;
  467. case UNIT_GB: multiplier = 1024 * 1024 * 1024; break;
  468. default: valid = false;
  469. }
  470. double size;
  471. std::stringstream conv;
  472. conv << mySizeCtrl->GetValue();
  473. conv >> size;
  474. valid = valid && conv;
  475. uint16_t sectorSize = CtrlGetValue<uint16_t>(mySectorSizeCtrl).first;
  476. uint64_t sectors = ceil(multiplier * size / sectorSize);
  477. if (sectors > std::numeric_limits<uint32_t>::max())
  478. {
  479. sectors = std::numeric_limits<uint32_t>::max();
  480. valid = false;
  481. }
  482. return std::make_pair(static_cast<uint32_t>(sectors), valid);
  483. }
  484. TargetConfig
  485. TargetPanel::getConfig() const
  486. {
  487. TargetConfig config;
  488. // Try and keep unknown/unused fields as-is to support newer firmware
  489. // versions.
  490. memcpy(&config, &myConfig, sizeof(config));
  491. bool valid = true;
  492. auto scsiId = CtrlGetValue<uint8_t>(myScsiIdCtrl);
  493. config.scsiId = scsiId.first & CONFIG_TARGET_ID_BITS;
  494. valid = valid && scsiId.second;
  495. if (myEnableCtrl->IsChecked())
  496. {
  497. config.scsiId = config.scsiId | CONFIG_TARGET_ENABLED;
  498. }
  499. config.deviceType = myDeviceTypeCtrl->GetSelection();
  500. config.flags =
  501. (myParityCtrl->IsChecked() ? CONFIG_ENABLE_PARITY : 0) |
  502. (myUnitAttCtrl->IsChecked() ? CONFIG_ENABLE_UNIT_ATTENTION : 0) |
  503. (myScsi2Ctrl->IsChecked() ? CONFIG_ENABLE_SCSI2 : 0);
  504. auto startSDSector = CtrlGetValue<uint32_t>(myStartSDSectorCtrl);
  505. config.sdSectorStart = startSDSector.first;
  506. valid = valid && startSDSector.second;
  507. auto numSectors = CtrlGetValue<uint32_t>(myNumSectorCtrl);
  508. config.scsiSectors = numSectors.first;
  509. valid = valid && numSectors.second;
  510. auto sectorSize = CtrlGetValue<uint16_t>(mySectorSizeCtrl);
  511. config.bytesPerSector = sectorSize.first;
  512. valid = valid && sectorSize.second;
  513. CtrlGetFixedString(myVendorCtrl, config.vendor, sizeof(config.vendor));
  514. CtrlGetFixedString(myProductCtrl, config.prodId, sizeof(config.prodId));
  515. CtrlGetFixedString(myRevisionCtrl, config.revision, sizeof(config.revision));
  516. CtrlGetFixedString(mySerialCtrl, config.serial, sizeof(config.serial));
  517. return config;
  518. }
  519. void
  520. TargetPanel::setConfig(const TargetConfig& config)
  521. {
  522. memcpy(&myConfig, &config, sizeof(config));
  523. myScsiIdCtrl->SetValue(config.scsiId & CONFIG_TARGET_ID_BITS);
  524. myEnableCtrl->SetValue(config.scsiId & CONFIG_TARGET_ENABLED);
  525. myDeviceTypeCtrl->SetSelection(config.deviceType);
  526. myParityCtrl->SetValue(config.flags & CONFIG_ENABLE_PARITY);
  527. myUnitAttCtrl->SetValue(config.flags & CONFIG_ENABLE_UNIT_ATTENTION);
  528. myScsi2Ctrl->SetValue(config.flags & CONFIG_ENABLE_SCSI2);
  529. {
  530. std::stringstream ss; ss << config.sdSectorStart;
  531. myStartSDSectorCtrl->ChangeValue(ss.str());
  532. myAutoStartSectorCtrl->SetValue(0);
  533. }
  534. {
  535. std::stringstream ss; ss << config.scsiSectors;
  536. myNumSectorCtrl->ChangeValue(ss.str());
  537. }
  538. {
  539. std::stringstream ss; ss << config.bytesPerSector;
  540. mySectorSizeCtrl->ChangeValue(ss.str());
  541. }
  542. myVendorCtrl->ChangeValue(std::string(config.vendor, sizeof(config.vendor)));
  543. myProductCtrl->ChangeValue(std::string(config.prodId, sizeof(config.prodId)));
  544. myRevisionCtrl->ChangeValue(std::string(config.revision, sizeof(config.revision)));
  545. mySerialCtrl->ChangeValue(std::string(config.serial, sizeof(config.serial)));
  546. // Set the size fields based on sector size, and evaluate inputs.
  547. wxCommandEvent fakeEvent(wxEVT_NULL, ID_numSectorCtrl);
  548. onSizeInput(fakeEvent);
  549. }
  550. bool
  551. TargetPanel::isEnabled() const
  552. {
  553. return myEnableCtrl->IsChecked();
  554. }
  555. uint8_t
  556. TargetPanel::getSCSIId() const
  557. {
  558. return CtrlGetValue<uint8_t>(myScsiIdCtrl).first & CONFIG_TARGET_ID_BITS;
  559. }
  560. std::pair<uint32_t, uint64_t>
  561. TargetPanel::getSDSectorRange() const
  562. {
  563. std::pair<uint32_t, uint64_t> result;
  564. result.first = CtrlGetValue<uint32_t>(myStartSDSectorCtrl).first;
  565. uint32_t numSCSISectors = CtrlGetValue<uint32_t>(myNumSectorCtrl).first;
  566. uint16_t scsiSectorSize = CtrlGetValue<uint16_t>(mySectorSizeCtrl).first;
  567. const int sdSector = 512; // Always 512 for SDHC/SDXC
  568. result.second = result.first +
  569. (
  570. ((uint64_t(numSCSISectors) * scsiSectorSize) + (sdSector - 1))
  571. / sdSector
  572. );
  573. return result;
  574. }
  575. void
  576. TargetPanel::setDuplicateID(bool duplicate)
  577. {
  578. if (duplicate)
  579. {
  580. myScsiIdMsg->SetLabelMarkup(wxT("<span foreground='red' weight='bold'>Duplicate ID</span>"));
  581. }
  582. else
  583. {
  584. myScsiIdMsg->SetLabelMarkup("");
  585. }
  586. }
  587. void
  588. TargetPanel::setSDSectorOverlap(bool overlap)
  589. {
  590. if (overlap)
  591. {
  592. myStartSDSectorMsg->SetLabelMarkup(wxT("<span foreground='red' weight='bold'>Overlapping data</span>"));
  593. }
  594. else
  595. {
  596. myStartSDSectorMsg->SetLabelMarkup("");
  597. }
  598. }
  599. void
  600. TargetPanel::setAutoStartSector(uint32_t start)
  601. {
  602. myAutoStartSector = start;
  603. }