TargetPanel.cc 17 KB

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