TargetPanel.cc 19 KB

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