TargetPanel.cc 19 KB

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