TargetPanel.cc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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 S2S_TargetCfg& 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(11, 3, 9, 25);
  64. fgs->Add(new wxStaticText(this, wxID_ANY, wxT("")));
  65. myEnableCtrl =
  66. new wxCheckBox(
  67. this,
  68. ID_enableCtrl,
  69. _("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, _("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, _("Device Type")));
  91. wxString deviceTypes[] =
  92. {
  93. _("Hard Drive"),
  94. _("Removable"),
  95. _("CDROM"),
  96. _("3.5\" Floppy"),
  97. _("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("SD card start sector")));
  113. wxWrapSizer* startContainer = new wxWrapSizer();
  114. myStartSDSectorCtrl =
  115. new wxTextCtrl(
  116. this,
  117. ID_startSDSectorCtrl,
  118. "0",
  119. wxDefaultPosition,
  120. wxDefaultSize,
  121. 0,
  122. *myStartSDSectorValidator);
  123. myStartSDSectorCtrl->SetToolTip(wxT("Supports multiple SCSI targets "
  124. "on a single memory card. In units of 512-byte sectors."));
  125. startContainer->Add(myStartSDSectorCtrl);
  126. myAutoStartSectorCtrl =
  127. new wxCheckBox(
  128. this,
  129. ID_autoStartSectorCtrl,
  130. wxT("Auto"));
  131. startContainer->Add(myAutoStartSectorCtrl);
  132. Bind(wxEVT_CHECKBOX, &TargetPanel::onInput<wxCommandEvent>, this, ID_autoStartSectorCtrl);
  133. fgs->Add(startContainer);
  134. myStartSDSectorMsg = new wxStaticText(this, wxID_ANY, wxT(""));
  135. fgs->Add(myStartSDSectorMsg);
  136. Bind(wxEVT_TEXT, &TargetPanel::onInput<wxCommandEvent>, this, ID_startSDSectorCtrl);
  137. fgs->Add(new wxStaticText(this, wxID_ANY, wxT("Sector size (bytes)")));
  138. mySectorSizeCtrl =
  139. new wxTextCtrl(
  140. this,
  141. ID_sectorSizeCtrl,
  142. "512",
  143. wxDefaultPosition,
  144. wxDefaultSize,
  145. 0,
  146. *mySectorSizeValidator);
  147. mySectorSizeCtrl->SetToolTip(wxT("Between 64 and 8192. Default of 512 is suitable in most cases."));
  148. fgs->Add(mySectorSizeCtrl);
  149. mySectorSizeMsg = new wxStaticText(this, wxID_ANY, wxT(""));
  150. fgs->Add(mySectorSizeMsg);
  151. Bind(wxEVT_TEXT, &TargetPanel::onSizeInput, this, ID_sectorSizeCtrl);
  152. fgs->Add(new wxStaticText(this, wxID_ANY, wxT("Sector count")));
  153. myNumSectorCtrl =
  154. new wxTextCtrl(
  155. this,
  156. ID_numSectorCtrl,
  157. "",
  158. wxDefaultPosition,
  159. wxDefaultSize,
  160. 0,
  161. *myNumSectorValidator);
  162. myNumSectorCtrl->SetToolTip(wxT("Number of sectors (device size)"));
  163. fgs->Add(myNumSectorCtrl);
  164. myNumSectorMsg = new wxStaticText(this, wxID_ANY, wxT(""));
  165. fgs->Add(myNumSectorMsg);
  166. Bind(wxEVT_TEXT, &TargetPanel::onSizeInput, this, ID_numSectorCtrl);
  167. fgs->Add(new wxStaticText(this, wxID_ANY, wxT("Device size")));
  168. wxWrapSizer* sizeContainer = new wxWrapSizer();
  169. mySizeCtrl =
  170. new wxTextCtrl(
  171. this,
  172. ID_sizeCtrl,
  173. "",
  174. wxDefaultPosition,
  175. wxDefaultSize,
  176. 0,
  177. *mySizeValidator);
  178. mySizeCtrl->SetToolTip(wxT("Device size"));
  179. sizeContainer->Add(mySizeCtrl);
  180. wxString units[] = {wxT("KB"), wxT("MB"), wxT("GB")};
  181. mySizeUnitCtrl =
  182. new wxChoice(
  183. this,
  184. ID_sizeUnitCtrl,
  185. wxDefaultPosition,
  186. wxDefaultSize,
  187. sizeof(units) / sizeof(wxString),
  188. units
  189. );
  190. sizeContainer->Add(mySizeUnitCtrl);
  191. fgs->Add(sizeContainer);
  192. fgs->Add(new wxStaticText(this, wxID_ANY, wxT("")));
  193. Bind(wxEVT_TEXT, &TargetPanel::onSizeInput, this, ID_sizeCtrl);
  194. Bind(wxEVT_CHOICE, &TargetPanel::onSizeInput, this, ID_sizeUnitCtrl);
  195. fgs->Add(new wxStaticText(this, wxID_ANY, wxT("Vendor")));
  196. myVendorCtrl =
  197. new wxTextCtrl(
  198. this,
  199. ID_vendorCtrl,
  200. wxEmptyString,
  201. wxDefaultPosition,
  202. wxSize(GetCharWidth() * 10, -1));
  203. myVendorCtrl->SetMaxLength(8);
  204. myVendorCtrl->SetToolTip(wxT("SCSI Vendor string. eg. ' codesrc'"));
  205. fgs->Add(myVendorCtrl);
  206. myVendorMsg = new wxStaticText(this, wxID_ANY, wxT(""));
  207. fgs->Add(myVendorMsg);
  208. Bind(wxEVT_TEXT, &TargetPanel::onInput<wxCommandEvent>, this, ID_vendorCtrl);
  209. fgs->Add(new wxStaticText(this, wxID_ANY, wxT("Product ID")));
  210. myProductCtrl =
  211. new wxTextCtrl(
  212. this,
  213. ID_productCtrl,
  214. wxEmptyString,
  215. wxDefaultPosition,
  216. wxSize(GetCharWidth() * 17, -1));
  217. myProductCtrl->SetMaxLength(18);
  218. myProductCtrl->SetToolTip(wxT("SCSI Product ID string. eg. 'SCSI2SD'"));
  219. fgs->Add(myProductCtrl);
  220. myProductMsg = new wxStaticText(this, wxID_ANY, wxT(""));
  221. fgs->Add(myProductMsg);
  222. Bind(wxEVT_TEXT, &TargetPanel::onInput<wxCommandEvent>, this, ID_productCtrl);
  223. fgs->Add(new wxStaticText(this, wxID_ANY, wxT("Revision")));
  224. myRevisionCtrl =
  225. new wxTextCtrl(
  226. this,
  227. ID_revisionCtrl,
  228. wxEmptyString,
  229. wxDefaultPosition,
  230. wxSize(GetCharWidth() * 6, -1));
  231. myRevisionCtrl->SetMaxLength(4);
  232. myRevisionCtrl->SetToolTip(wxT("SCSI device revision string. eg. '3.5a'"));
  233. fgs->Add(myRevisionCtrl);
  234. myRevisionMsg = new wxStaticText(this, wxID_ANY, wxT(""));
  235. fgs->Add(myRevisionMsg);
  236. Bind(wxEVT_TEXT, &TargetPanel::onInput<wxCommandEvent>, this, ID_revisionCtrl);
  237. fgs->Add(new wxStaticText(this, wxID_ANY, wxT("Serial number")));
  238. mySerialCtrl =
  239. new wxTextCtrl(
  240. this,
  241. ID_serialCtrl,
  242. wxEmptyString,
  243. wxDefaultPosition,
  244. wxSize(GetCharWidth() * 18, -1));
  245. mySerialCtrl->SetMaxLength(16);
  246. mySerialCtrl->SetToolTip(wxT("SCSI serial number. eg. '13eab5632a'"));
  247. fgs->Add(mySerialCtrl);
  248. mySerialMsg = new wxStaticText(this, wxID_ANY, wxT(""));
  249. fgs->Add(mySerialMsg);
  250. Bind(wxEVT_TEXT, &TargetPanel::onInput<wxCommandEvent>, this, ID_serialCtrl);
  251. wxBoxSizer* hbox = new wxBoxSizer(wxHORIZONTAL);
  252. hbox->Add(fgs, 1, wxALL | wxEXPAND, 15);
  253. this->SetSizer(hbox);
  254. Centre();
  255. setConfig(initialConfig);
  256. evaluate();
  257. }
  258. bool
  259. TargetPanel::evaluate()
  260. {
  261. bool valid = true;
  262. std::stringstream conv;
  263. bool enabled = myEnableCtrl->IsChecked();
  264. {
  265. myScsiIdCtrl->Enable(enabled);
  266. myDeviceTypeCtrl->Enable(enabled);
  267. myStartSDSectorCtrl->Enable(enabled && !myAutoStartSectorCtrl->IsChecked());
  268. myAutoStartSectorCtrl->Enable(enabled);
  269. mySectorSizeCtrl->Enable(enabled);
  270. myNumSectorCtrl->Enable(enabled);
  271. mySizeCtrl->Enable(enabled);
  272. mySizeUnitCtrl->Enable(enabled);
  273. myVendorCtrl->Enable(enabled);
  274. myProductCtrl->Enable(enabled);
  275. myRevisionCtrl->Enable(enabled);
  276. mySerialCtrl->Enable(enabled);
  277. }
  278. switch (myDeviceTypeCtrl->GetSelection())
  279. {
  280. case S2S_CFG_FLOPPY_14MB:
  281. mySectorSizeCtrl->ChangeValue("512");
  282. mySectorSizeCtrl->Enable(false);
  283. myNumSectorCtrl->ChangeValue("2880");
  284. myNumSectorCtrl->Enable(false);
  285. mySizeUnitCtrl->Enable(false);
  286. mySizeCtrl->Enable(false);
  287. evaluateSize();
  288. break;
  289. };
  290. if (myAutoStartSectorCtrl->IsChecked())
  291. {
  292. std::stringstream ss; ss << myAutoStartSector;
  293. myStartSDSectorCtrl->ChangeValue(ss.str());
  294. }
  295. uint32_t startSDsector;
  296. {
  297. conv << myStartSDSectorCtrl->GetValue();
  298. conv >> startSDsector;
  299. }
  300. if (!conv)
  301. // TODO check if it is beyond the current SD card.
  302. {
  303. myStartSDSectorMsg->SetLabelMarkup(wxT("<span foreground='red' weight='bold'>Number &gt;= 0</span>"));
  304. valid = false;
  305. }
  306. else
  307. {
  308. myStartSDSectorMsg->SetLabelMarkup("");
  309. }
  310. conv.str(std::string()); conv.clear();
  311. uint16_t sectorSize(CtrlGetValue<uint16_t>(mySectorSizeCtrl).first);
  312. if (sectorSize < 64 || sectorSize > 8192)
  313. {
  314. mySectorSizeMsg->SetLabelMarkup(wxT("<span foreground='red' weight='bold'>Must be between 64 and 8192</span>"));
  315. valid = false;
  316. }
  317. else
  318. {
  319. mySectorSizeMsg->SetLabelMarkup("");
  320. }
  321. conv.str(std::string()); conv.clear();
  322. std::pair<uint32_t, bool> numSectors(CtrlGetValue<uint32_t>(myNumSectorCtrl));
  323. if (!numSectors.second ||
  324. numSectors.first == 0 ||
  325. !convertUnitsToSectors().second)
  326. {
  327. myNumSectorMsg->SetLabelMarkup(wxT("<span foreground='red' weight='bold'>Invalid size</span>"));
  328. valid = false;
  329. }
  330. else
  331. {
  332. myNumSectorMsg->SetLabelMarkup("");
  333. }
  334. if (!CtrlIsAscii(myVendorCtrl))
  335. {
  336. myVendorMsg->SetLabelMarkup(wxT("<span foreground='red' weight='bold'>Invalid characters</span>"));
  337. valid = false;
  338. }
  339. else
  340. {
  341. myVendorMsg->SetLabelMarkup("");
  342. }
  343. if (!CtrlIsAscii(myProductCtrl))
  344. {
  345. myProductMsg->SetLabelMarkup(wxT("<span foreground='red' weight='bold'>Invalid characters</span>"));
  346. valid = false;
  347. }
  348. else
  349. {
  350. myProductMsg->SetLabelMarkup("");
  351. }
  352. if (!CtrlIsAscii(myRevisionCtrl))
  353. {
  354. myRevisionMsg->SetLabelMarkup(wxT("<span foreground='red' weight='bold'>Invalid characters</span>"));
  355. valid = false;
  356. }
  357. else
  358. {
  359. myRevisionMsg->SetLabelMarkup("");
  360. }
  361. if (!CtrlIsAscii(mySerialCtrl))
  362. {
  363. mySerialMsg->SetLabelMarkup(wxT("<span foreground='red' weight='bold'>Invalid characters</span>"));
  364. valid = false;
  365. }
  366. else
  367. {
  368. mySerialMsg->SetLabelMarkup("");
  369. }
  370. return valid || !enabled;
  371. }
  372. template<typename EvtType> void
  373. TargetPanel::onInput(EvtType& event)
  374. {
  375. if (event.GetId() == ID_deviceTypeCtrl)
  376. {
  377. switch (myDeviceTypeCtrl->GetSelection())
  378. {
  379. case S2S_CFG_OPTICAL:
  380. mySectorSizeCtrl->ChangeValue("2048");
  381. evaluateSize();
  382. break;
  383. }
  384. }
  385. wxCommandEvent changeEvent(ConfigChangedEvent);
  386. wxPostEvent(myParent, changeEvent);
  387. }
  388. void
  389. TargetPanel::onSizeInput(wxCommandEvent& event)
  390. {
  391. if (event.GetId() != ID_numSectorCtrl)
  392. {
  393. std::pair<uint32_t, bool> sec = convertUnitsToSectors();
  394. if (sec.second)
  395. {
  396. std::stringstream ss;
  397. ss << sec.first;
  398. myNumSectorCtrl->ChangeValue(ss.str());
  399. }
  400. }
  401. if (event.GetId() != ID_sizeCtrl)
  402. {
  403. evaluateSize();
  404. }
  405. onInput(event); // propagate
  406. }
  407. void
  408. TargetPanel::evaluateSize()
  409. {
  410. uint32_t numSectors;
  411. std::stringstream conv;
  412. conv << myNumSectorCtrl->GetValue();
  413. conv >> numSectors;
  414. conv.str(""); conv.clear();
  415. if (conv)
  416. {
  417. uint64_t bytes =
  418. uint64_t(numSectors) *
  419. CtrlGetValue<uint16_t>(mySectorSizeCtrl).first;
  420. if (bytes >= 1024 * 1024 * 1024)
  421. {
  422. conv << (bytes / (1024.0 * 1024 * 1024));
  423. mySizeUnitCtrl->SetSelection(UNIT_GB);
  424. }
  425. else if (bytes >= 1024 * 1024)
  426. {
  427. conv << (bytes / (1024.0 * 1024));
  428. mySizeUnitCtrl->SetSelection(UNIT_MB);
  429. }
  430. else
  431. {
  432. conv << (bytes / 1024.0);
  433. mySizeUnitCtrl->SetSelection(UNIT_KB);
  434. }
  435. mySizeCtrl->ChangeValue(conv.str());
  436. }
  437. }
  438. std::pair<uint32_t, bool>
  439. TargetPanel::convertUnitsToSectors() const
  440. {
  441. bool valid = true;
  442. uint64_t multiplier(0);
  443. switch (mySizeUnitCtrl->GetSelection())
  444. {
  445. case UNIT_KB: multiplier = 1024; break;
  446. case UNIT_MB: multiplier = 1024 * 1024; break;
  447. case UNIT_GB: multiplier = 1024 * 1024 * 1024; break;
  448. default: valid = false;
  449. }
  450. double size;
  451. std::stringstream conv;
  452. conv << mySizeCtrl->GetValue();
  453. conv >> size;
  454. valid = valid && conv;
  455. uint16_t sectorSize = CtrlGetValue<uint16_t>(mySectorSizeCtrl).first;
  456. uint64_t sectors = ceil(multiplier * size / sectorSize);
  457. if (sectors > std::numeric_limits<uint32_t>::max())
  458. {
  459. sectors = std::numeric_limits<uint32_t>::max();
  460. valid = false;
  461. }
  462. return std::make_pair(static_cast<uint32_t>(sectors), valid);
  463. }
  464. S2S_TargetCfg
  465. TargetPanel::getConfig() const
  466. {
  467. S2S_TargetCfg config;
  468. // Try and keep unknown/unused fields as-is to support newer firmware
  469. // versions.
  470. memcpy(&config, &myConfig, sizeof(config));
  471. bool valid = true;
  472. auto scsiId = CtrlGetValue<uint8_t>(myScsiIdCtrl);
  473. config.scsiId = scsiId.first & S2S_CFG_TARGET_ID_BITS;
  474. valid = valid && scsiId.second;
  475. if (myEnableCtrl->IsChecked())
  476. {
  477. config.scsiId = config.scsiId | S2S_CFG_TARGET_ENABLED;
  478. }
  479. config.deviceType = myDeviceTypeCtrl->GetSelection();
  480. auto startSDSector = CtrlGetValue<uint32_t>(myStartSDSectorCtrl);
  481. config.sdSectorStart = startSDSector.first;
  482. valid = valid && startSDSector.second;
  483. auto numSectors = CtrlGetValue<uint32_t>(myNumSectorCtrl);
  484. config.scsiSectors = numSectors.first;
  485. valid = valid && numSectors.second;
  486. auto sectorSize = CtrlGetValue<uint16_t>(mySectorSizeCtrl);
  487. config.bytesPerSector = sectorSize.first;
  488. valid = valid && sectorSize.second;
  489. CtrlGetFixedString(myVendorCtrl, config.vendor, sizeof(config.vendor));
  490. CtrlGetFixedString(myProductCtrl, config.prodId, sizeof(config.prodId));
  491. CtrlGetFixedString(myRevisionCtrl, config.revision, sizeof(config.revision));
  492. CtrlGetFixedString(mySerialCtrl, config.serial, sizeof(config.serial));
  493. return config;
  494. }
  495. void
  496. TargetPanel::setConfig(const S2S_TargetCfg& config)
  497. {
  498. memcpy(&myConfig, &config, sizeof(config));
  499. myScsiIdCtrl->SetValue(config.scsiId & S2S_CFG_TARGET_ID_BITS);
  500. myEnableCtrl->SetValue(config.scsiId & S2S_CFG_TARGET_ENABLED);
  501. myDeviceTypeCtrl->SetSelection(config.deviceType);
  502. {
  503. std::stringstream ss; ss << config.sdSectorStart;
  504. myStartSDSectorCtrl->ChangeValue(ss.str());
  505. myAutoStartSectorCtrl->SetValue(0);
  506. }
  507. {
  508. std::stringstream ss; ss << config.scsiSectors;
  509. myNumSectorCtrl->ChangeValue(ss.str());
  510. }
  511. {
  512. std::stringstream ss; ss << config.bytesPerSector;
  513. mySectorSizeCtrl->ChangeValue(ss.str());
  514. }
  515. myVendorCtrl->ChangeValue(std::string(config.vendor, sizeof(config.vendor)));
  516. myProductCtrl->ChangeValue(std::string(config.prodId, sizeof(config.prodId)));
  517. myRevisionCtrl->ChangeValue(std::string(config.revision, sizeof(config.revision)));
  518. mySerialCtrl->ChangeValue(std::string(config.serial, sizeof(config.serial)));
  519. // Set the size fields based on sector size, and evaluate inputs.
  520. wxCommandEvent fakeEvent(wxEVT_NULL, ID_numSectorCtrl);
  521. onSizeInput(fakeEvent);
  522. }
  523. bool
  524. TargetPanel::isEnabled() const
  525. {
  526. return myEnableCtrl->IsChecked();
  527. }
  528. uint8_t
  529. TargetPanel::getSCSIId() const
  530. {
  531. return CtrlGetValue<uint8_t>(myScsiIdCtrl).first & S2S_CFG_TARGET_ID_BITS;
  532. }
  533. std::pair<uint32_t, uint64_t>
  534. TargetPanel::getSDSectorRange() const
  535. {
  536. std::pair<uint32_t, uint64_t> result;
  537. result.first = CtrlGetValue<uint32_t>(myStartSDSectorCtrl).first;
  538. uint32_t numSCSISectors = CtrlGetValue<uint32_t>(myNumSectorCtrl).first;
  539. uint16_t scsiSectorSize = CtrlGetValue<uint16_t>(mySectorSizeCtrl).first;
  540. const int sdSector = 512; // Always 512 for SDHC/SDXC
  541. result.second = result.first +
  542. (
  543. ((uint64_t(numSCSISectors) * scsiSectorSize) + (sdSector - 1))
  544. / sdSector
  545. );
  546. return result;
  547. }
  548. void
  549. TargetPanel::setDuplicateID(bool duplicate)
  550. {
  551. if (duplicate)
  552. {
  553. myScsiIdMsg->SetLabelMarkup(wxT("<span foreground='red' weight='bold'>Duplicate ID</span>"));
  554. }
  555. else
  556. {
  557. myScsiIdMsg->SetLabelMarkup("");
  558. }
  559. }
  560. void
  561. TargetPanel::setSDSectorOverlap(bool overlap)
  562. {
  563. if (overlap)
  564. {
  565. myStartSDSectorMsg->SetLabelMarkup(wxT("<span foreground='red' weight='bold'>Overlapping data</span>"));
  566. }
  567. else
  568. {
  569. myStartSDSectorMsg->SetLabelMarkup("");
  570. }
  571. }
  572. void
  573. TargetPanel::setAutoStartSector(uint32_t start)
  574. {
  575. myAutoStartSector = start;
  576. }