TargetPanel.cc 18 KB

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