TargetPanel.cc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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(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 CONFIG_OPTICAL:
  281. mySectorSizeCtrl->ChangeValue("2048");
  282. mySectorSizeCtrl->Enable(false);
  283. break;
  284. case CONFIG_FLOPPY_14MB:
  285. mySectorSizeCtrl->ChangeValue("512");
  286. mySectorSizeCtrl->Enable(false);
  287. myNumSectorCtrl->ChangeValue("2880");
  288. myNumSectorCtrl->Enable(false);
  289. mySizeUnitCtrl->Enable(false);
  290. mySizeCtrl->Enable(false);
  291. break;
  292. };
  293. evaluateSize();
  294. if (myAutoStartSectorCtrl->IsChecked())
  295. {
  296. std::stringstream ss; ss << myAutoStartSector;
  297. myStartSDSectorCtrl->ChangeValue(ss.str());
  298. }
  299. uint32_t startSDsector;
  300. {
  301. conv << myStartSDSectorCtrl->GetValue();
  302. conv >> startSDsector;
  303. }
  304. if (!conv)
  305. // TODO check if it is beyond the current SD card.
  306. {
  307. myStartSDSectorMsg->SetLabelMarkup(wxT("<span foreground='red' weight='bold'>Number &gt;= 0</span>"));
  308. valid = false;
  309. }
  310. else
  311. {
  312. myStartSDSectorMsg->SetLabelMarkup("");
  313. }
  314. conv.str(std::string()); conv.clear();
  315. uint16_t sectorSize(CtrlGetValue<uint16_t>(mySectorSizeCtrl).first);
  316. if (sectorSize < 64 || sectorSize > 8192)
  317. {
  318. mySectorSizeMsg->SetLabelMarkup(wxT("<span foreground='red' weight='bold'>Must be between 64 and 8192</span>"));
  319. valid = false;
  320. }
  321. else
  322. {
  323. mySectorSizeMsg->SetLabelMarkup("");
  324. }
  325. conv.str(std::string()); conv.clear();
  326. std::pair<uint32_t, bool> numSectors(CtrlGetValue<uint32_t>(myNumSectorCtrl));
  327. if (!numSectors.second ||
  328. numSectors.first == 0 ||
  329. !convertUnitsToSectors().second)
  330. {
  331. myNumSectorMsg->SetLabelMarkup(wxT("<span foreground='red' weight='bold'>Invalid size</span>"));
  332. valid = false;
  333. }
  334. else
  335. {
  336. myNumSectorMsg->SetLabelMarkup("");
  337. }
  338. if (!CtrlIsAscii(myVendorCtrl))
  339. {
  340. myVendorMsg->SetLabelMarkup(wxT("<span foreground='red' weight='bold'>Invalid characters</span>"));
  341. valid = false;
  342. }
  343. else
  344. {
  345. myVendorMsg->SetLabelMarkup("");
  346. }
  347. if (!CtrlIsAscii(myProductCtrl))
  348. {
  349. myProductMsg->SetLabelMarkup(wxT("<span foreground='red' weight='bold'>Invalid characters</span>"));
  350. valid = false;
  351. }
  352. else
  353. {
  354. myProductMsg->SetLabelMarkup("");
  355. }
  356. if (!CtrlIsAscii(myRevisionCtrl))
  357. {
  358. myRevisionMsg->SetLabelMarkup(wxT("<span foreground='red' weight='bold'>Invalid characters</span>"));
  359. valid = false;
  360. }
  361. else
  362. {
  363. myRevisionMsg->SetLabelMarkup("");
  364. }
  365. if (!CtrlIsAscii(mySerialCtrl))
  366. {
  367. mySerialMsg->SetLabelMarkup(wxT("<span foreground='red' weight='bold'>Invalid characters</span>"));
  368. valid = false;
  369. }
  370. else
  371. {
  372. mySerialMsg->SetLabelMarkup("");
  373. }
  374. return valid || !enabled;
  375. }
  376. template<typename EvtType> void
  377. TargetPanel::onInput(EvtType& event)
  378. {
  379. wxCommandEvent changeEvent(ConfigChangedEvent);
  380. wxPostEvent(myParent, changeEvent);
  381. }
  382. void
  383. TargetPanel::onSizeInput(wxCommandEvent& event)
  384. {
  385. if (event.GetId() != ID_numSectorCtrl)
  386. {
  387. std::stringstream ss;
  388. ss << convertUnitsToSectors().first;
  389. myNumSectorCtrl->ChangeValue(ss.str());
  390. }
  391. evaluateSize();
  392. onInput(event); // propagate
  393. }
  394. void
  395. TargetPanel::evaluateSize()
  396. {
  397. uint32_t numSectors;
  398. std::stringstream conv;
  399. conv << myNumSectorCtrl->GetValue();
  400. conv >> numSectors;
  401. conv.str(""); conv.clear();
  402. if (conv)
  403. {
  404. uint64_t bytes =
  405. uint64_t(numSectors) *
  406. CtrlGetValue<uint16_t>(mySectorSizeCtrl).first;
  407. if (bytes >= 1024 * 1024 * 1024)
  408. {
  409. conv << (bytes / (1024.0 * 1024 * 1024));
  410. mySizeUnitCtrl->SetSelection(UNIT_GB);
  411. }
  412. else if (bytes >= 1024 * 1024)
  413. {
  414. conv << (bytes / (1024.0 * 1024));
  415. mySizeUnitCtrl->SetSelection(UNIT_MB);
  416. }
  417. else
  418. {
  419. conv << (bytes / 1024.0);
  420. mySizeUnitCtrl->SetSelection(UNIT_KB);
  421. }
  422. mySizeCtrl->ChangeValue(conv.str());
  423. }
  424. }
  425. std::pair<uint32_t, bool>
  426. TargetPanel::convertUnitsToSectors() const
  427. {
  428. bool valid = true;
  429. uint64_t multiplier(0);
  430. switch (mySizeUnitCtrl->GetSelection())
  431. {
  432. case UNIT_KB: multiplier = 1024; break;
  433. case UNIT_MB: multiplier = 1024 * 1024; break;
  434. case UNIT_GB: multiplier = 1024 * 1024 * 1024; break;
  435. default: valid = false;
  436. }
  437. double size;
  438. std::stringstream conv;
  439. conv << mySizeCtrl->GetValue();
  440. conv >> size;
  441. valid = valid && conv;
  442. uint16_t sectorSize = CtrlGetValue<uint16_t>(mySectorSizeCtrl).first;
  443. uint64_t sectors = ceil(multiplier * size / sectorSize);
  444. if (sectors > std::numeric_limits<uint32_t>::max())
  445. {
  446. sectors = std::numeric_limits<uint32_t>::max();
  447. valid = false;
  448. }
  449. return std::make_pair(static_cast<uint32_t>(sectors), valid);
  450. }
  451. TargetConfig
  452. TargetPanel::getConfig() const
  453. {
  454. TargetConfig config;
  455. // Try and keep unknown/unused fields as-is to support newer firmware
  456. // versions.
  457. memcpy(&config, &myConfig, sizeof(config));
  458. bool valid = true;
  459. auto scsiId = CtrlGetValue<uint8_t>(myScsiIdCtrl);
  460. config.scsiId = scsiId.first & CONFIG_TARGET_ID_BITS;
  461. valid = valid && scsiId.second;
  462. if (myEnableCtrl->IsChecked())
  463. {
  464. config.scsiId = config.scsiId | CONFIG_TARGET_ENABLED;
  465. }
  466. config.deviceType = myDeviceTypeCtrl->GetSelection();
  467. auto startSDSector = CtrlGetValue<uint32_t>(myStartSDSectorCtrl);
  468. config.sdSectorStart = startSDSector.first;
  469. valid = valid && startSDSector.second;
  470. auto numSectors = CtrlGetValue<uint32_t>(myNumSectorCtrl);
  471. config.scsiSectors = numSectors.first;
  472. valid = valid && numSectors.second;
  473. auto sectorSize = CtrlGetValue<uint16_t>(mySectorSizeCtrl);
  474. config.bytesPerSector = sectorSize.first;
  475. valid = valid && sectorSize.second;
  476. CtrlGetFixedString(myVendorCtrl, config.vendor, sizeof(config.vendor));
  477. CtrlGetFixedString(myProductCtrl, config.prodId, sizeof(config.prodId));
  478. CtrlGetFixedString(myRevisionCtrl, config.revision, sizeof(config.revision));
  479. CtrlGetFixedString(mySerialCtrl, config.serial, sizeof(config.serial));
  480. return config;
  481. }
  482. void
  483. TargetPanel::setConfig(const TargetConfig& config)
  484. {
  485. memcpy(&myConfig, &config, sizeof(config));
  486. myScsiIdCtrl->SetValue(config.scsiId & CONFIG_TARGET_ID_BITS);
  487. myEnableCtrl->SetValue(config.scsiId & CONFIG_TARGET_ENABLED);
  488. myDeviceTypeCtrl->SetSelection(config.deviceType);
  489. {
  490. std::stringstream ss; ss << config.sdSectorStart;
  491. myStartSDSectorCtrl->ChangeValue(ss.str());
  492. myAutoStartSectorCtrl->SetValue(0);
  493. }
  494. {
  495. std::stringstream ss; ss << config.scsiSectors;
  496. myNumSectorCtrl->ChangeValue(ss.str());
  497. }
  498. {
  499. std::stringstream ss; ss << config.bytesPerSector;
  500. mySectorSizeCtrl->ChangeValue(ss.str());
  501. }
  502. myVendorCtrl->ChangeValue(std::string(config.vendor, sizeof(config.vendor)));
  503. myProductCtrl->ChangeValue(std::string(config.prodId, sizeof(config.prodId)));
  504. myRevisionCtrl->ChangeValue(std::string(config.revision, sizeof(config.revision)));
  505. mySerialCtrl->ChangeValue(std::string(config.serial, sizeof(config.serial)));
  506. // Set the size fields based on sector size, and evaluate inputs.
  507. wxCommandEvent fakeEvent(wxEVT_NULL, ID_numSectorCtrl);
  508. onSizeInput(fakeEvent);
  509. }
  510. bool
  511. TargetPanel::isEnabled() const
  512. {
  513. return myEnableCtrl->IsChecked();
  514. }
  515. uint8_t
  516. TargetPanel::getSCSIId() const
  517. {
  518. return CtrlGetValue<uint8_t>(myScsiIdCtrl).first & CONFIG_TARGET_ID_BITS;
  519. }
  520. std::pair<uint32_t, uint64_t>
  521. TargetPanel::getSDSectorRange() const
  522. {
  523. std::pair<uint32_t, uint64_t> result;
  524. result.first = CtrlGetValue<uint32_t>(myStartSDSectorCtrl).first;
  525. uint32_t numSCSISectors = CtrlGetValue<uint32_t>(myNumSectorCtrl).first;
  526. uint16_t scsiSectorSize = CtrlGetValue<uint16_t>(mySectorSizeCtrl).first;
  527. const int sdSector = 512; // Always 512 for SDHC/SDXC
  528. result.second = result.first +
  529. (
  530. ((uint64_t(numSCSISectors) * scsiSectorSize) + (sdSector - 1))
  531. / sdSector
  532. );
  533. return result;
  534. }
  535. void
  536. TargetPanel::setDuplicateID(bool duplicate)
  537. {
  538. if (duplicate)
  539. {
  540. myScsiIdMsg->SetLabelMarkup(wxT("<span foreground='red' weight='bold'>Duplicate ID</span>"));
  541. }
  542. else
  543. {
  544. myScsiIdMsg->SetLabelMarkup("");
  545. }
  546. }
  547. void
  548. TargetPanel::setSDSectorOverlap(bool overlap)
  549. {
  550. if (overlap)
  551. {
  552. myStartSDSectorMsg->SetLabelMarkup(wxT("<span foreground='red' weight='bold'>Overlapping data</span>"));
  553. }
  554. else
  555. {
  556. myStartSDSectorMsg->SetLabelMarkup("");
  557. }
  558. }
  559. void
  560. TargetPanel::setAutoStartSector(uint32_t start)
  561. {
  562. myAutoStartSector = start;
  563. }