TargetPanel.cc 18 KB

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