-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArduinoEEPROMprogrammer.cpp
699 lines (629 loc) · 26.1 KB
/
ArduinoEEPROMprogrammer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
// EEPROMProgrammerConsole.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <chrono>
#include <thread>
#include <stdio.h>
#include <tchar.h>
#include <string>
#include <vector>
#include <windows.h>
#include <initguid.h>
#include <devguid.h>
#include <setupapi.h>
#include <ctype.h>
#include <conio.h>
#include <fstream>
#include <algorithm>
#pragma comment(lib, "Setupapi.lib")
#include "SerialClass.h"
#define KEY_ENTER 10
#define KEY_UP 72
#define KEY_DOWN 80
#define KEY_LEFT 75
#define KEY_RIGHT 77
#define READ 0
#define VERIFY 1
#define DUMP 2
#define WRITE 3
#define ERASE 4
#define SDP 5
#define EXIT 6
#define AT28C64B 0
#define AT28C256 1
std::vector<int> GetAvailableCOMPorts();
std::vector<std::string> GetDeviceNames();
unsigned int size = 0;
static unsigned char* data = new unsigned char[32768];
static void clear_screen() {
system("cls");
/*
COORD topLeft = { 0, 0 };
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO screen;
DWORD written;
GetConsoleScreenBufferInfo(console, &screen);
FillConsoleOutputCharacterA(
console, ' ', screen.dwSize.X * screen.dwSize.Y, topLeft, &written
);
FillConsoleOutputAttribute(
console, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE,
screen.dwSize.X * screen.dwSize.Y, topLeft, &written
);
SetConsoleCursorPosition(console, topLeft);
*/
}
static void wait_for_key_press() {
HANDLE hstdin;
DWORD mode;
hstdin = GetStdHandle(STD_INPUT_HANDLE);
GetConsoleMode(hstdin, &mode);
SetConsoleMode(hstdin, ENABLE_ECHO_INPUT | ENABLE_PROCESSED_INPUT);
int ch = 10;
while (ch == 10) ch = std::cin.get();
clear_screen();
SetConsoleMode(hstdin, mode);
}
namespace mainmenu {
static void read_into_buffer(Serial* SP) {
int arr[1] = { 0x01 };
DWORD bytesSend;
if (!WriteFile(SP->hSerial, arr, 1, &bytesSend, 0)) {
std::cerr << std::endl << "An error occured while trying to communicate with Arduino. Press any key to continue...";
wait_for_key_press();
return;
}
HANDLE hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO cbsi;
GetConsoleScreenBufferInfo(hConsoleOutput, &cbsi);
for (int i = 0; i < size; i++) {
unsigned char c = '\0';
DWORD bytesRead;
if (!ReadFile(SP->hSerial, &c, 1, &bytesRead, NULL)) {
std::cerr << "An error occured while reading EEPROM. Press any key to continue... ";
wait_for_key_press();
return;
}
data[i] = c;
std::cout << static_cast<int>(i * (100.f / size)) << "%";
SetConsoleCursorPosition(hConsoleOutput, cbsi.dwCursorPosition);
}
std::cout << "100%";
}
static DWORD write_to_serial(Serial* SP, std::string data) {
DWORD bytesSent;
if (!WriteFile(SP->hSerial, data.c_str(), data.size(), &bytesSent, 0)) {
std::cerr << "\nFATAL ERROR: Connection was interrupted during data transfer. Please restart application. Press any key to continue... ";
wait_for_key_press();
throw std::exception();
}
return bytesSent;
}
static void print(const unsigned char* data) {
clear_screen();
std::cout << std::flush;
for (int i = 0; i < size; i += 16) {
std::cout << std::hex << std::setfill('0') << "0x" << std::setw(4) << i << ": ";
for (int j = 0; j < 16; j++) {
std::cout << std::hex << std::setw(2) << static_cast<int>(data[i + j]) << ' ';
if (j == 7) std::cout << ' ';
}
std::cout << " | ";
for (int j = 0; j < 16; j++) {
char c = std::isprint(data[i + j]) ? static_cast<char>(data[i + j]) : '.';
std::cout << c;
}
std::cout << " |\n" << std::dec;
}
}
static void dump() {
OPENFILENAMEA ofn;
char szFileName[MAX_PATH];
char szFileTitle[MAX_PATH];
char filePath[MAX_PATH];
char szFile[MAX_PATH] = "contents.bin";
*szFileName = 0;
*szFileTitle = 0;
ofn.lpstrFile = szFile;
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = GetFocus();
ofn.lpstrFilter = "Binary File (*.bin)\0*.bin\0ROM File (*.rom)\0*.rom\0All Files (*.*)\0*.*\0";
ofn.lpstrCustomFilter = NULL;
ofn.nMaxCustFilter = NULL;
ofn.nFilterIndex = NULL;
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrInitialDir = "."; // Initial directory.
ofn.lpstrFileTitle = szFileTitle;
ofn.nMaxFileTitle = MAX_PATH;
ofn.lpstrTitle = "Save EEPROM contents to a file...";
ofn.lpstrDefExt = "*.bin";
ofn.Flags = OFN_OVERWRITEPROMPT;
if (!GetSaveFileNameA((LPOPENFILENAMEA)&ofn))
return;
else strcpy_s(filePath, ofn.lpstrFile);
std::ofstream in;
in.open(ofn.lpstrFile, std::ios::out | std::ios::binary | std::ofstream::trunc);
in.write(reinterpret_cast<const char*>(data), size);
std::cout << "EEPROM contents have been successfully dumped! Press any key to continue... ";
wait_for_key_press();
}
static void verify() {
OPENFILENAMEA ofn;
char szFileName[MAX_PATH];
char szFileTitle[MAX_PATH];
*szFileName = 0;
*szFileTitle = 0;
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = GetFocus();
ofn.lpstrFilter = "Binary File (*.bin)\0*.bin\0ROM File (*.rom)\0*.rom\0All Files (*.*)\0*.*\0";
ofn.lpstrCustomFilter = NULL;
ofn.nMaxCustFilter = NULL;
ofn.nFilterIndex = NULL;
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrInitialDir = "."; // Initial directory.
ofn.lpstrFileTitle = szFileTitle;
ofn.nMaxFileTitle = MAX_PATH;
ofn.lpstrTitle = "Open a file to verify with...";
ofn.lpstrDefExt = "*.bin";
ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
if (!GetOpenFileNameA((LPOPENFILENAMEA)&ofn))
return;
std::ifstream in;
in.open(ofn.lpstrFile, std::ios::in | std::ios::binary);
DWORD bytesRead;
std::vector<int> addrs;
std::string file((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
int length = file.size();
file.resize(size);
for (int i = length; i < size; i++) {
file[i] = 0x00;
}
for (int i = 0; i < size; i++) {
if (data[i] != static_cast<unsigned char>(file[i])) {
addrs.push_back(i);
}
}
if (addrs.empty()) {
std::cout << "The selected file and the EEPROM contents are identical! ";
}
else {
std::cout << "Data mismatch found in " << addrs.size() << " addresses:" << std::endl;
for (int const& a : addrs) {
printf("0x%04x, ", a);
}
std::cout << std::endl;
}
std::cout << "Press any key to continue... ";
wait_for_key_press();
}
static void write(Serial* SP) {
std::vector<int> addrs;
std::vector<int> values;
while (true) {
OPENFILENAMEA ofn;
char szFileName[MAX_PATH];
char szFileTitle[MAX_PATH];
*szFileName = 0;
*szFileTitle = 0;
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = GetFocus();
ofn.lpstrFilter = "Binary File (*.bin)\0*.bin\0ROM File (*.rom)\0*.rom\0All Files (*.*)\0*.*\0";
ofn.lpstrCustomFilter = NULL;
ofn.nMaxCustFilter = 0;
ofn.nFilterIndex = 0;
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrInitialDir = "."; // Initial directory.
ofn.lpstrFileTitle = szFileTitle;
ofn.nMaxFileTitle = MAX_PATH;
ofn.lpstrTitle = "Open a file to write to EEPROM...";
ofn.lpstrDefExt = "*.bin";
ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
if (!GetOpenFileNameA((LPOPENFILENAMEA)&ofn)) {
clear_screen();
return;
}
std::ifstream in;
in.open(ofn.lpstrFile, std::ios::in | std::ios::binary);
clear_screen();
DWORD bytesSend;
std::string file((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
int length = file.size();
file.resize(size);
for (int i = length; i < size; i++) {
file[i] = 0x00;
}
addrs.erase(addrs.begin(), addrs.end());
values.erase(values.begin(), values.end());
for (int i = 0; i < size; i++) {
if (data[i] != static_cast<unsigned char>(file[i])) {
addrs.push_back(i);
values.push_back(file[i]);
}
}
if (!addrs.size()) {
std::cout << "The contents of the file you have chosen is identical to the EEPROM contents.\n" << "Press Enter to choose another file, Space to cancel. ";
while (true) {
int c = _getch();
if (c == 32) {
clear_screen();
return;
}
else if (c == 13) {
break;
}
}
}
else {
print(reinterpret_cast<const unsigned char*>(const_cast<char*>(file.c_str()))); // ugly but it works
break;
}
}
std::cout << "\nYou're about to overwrite the data in the EEPROM with the data above.\n"
<< "Press Enter to proceed, Space to cancel. ";
while (true) {
int c = _getch();
if (c == 32) {
clear_screen();
return;
}
else if (c == 13) break;
}
clear_screen();
write_to_serial(SP, std::string(1, static_cast<char>(0x00)));
std::cout << "Writing data... ";
write_to_serial(SP, std::string(1, (unsigned char)(values.size() >> 8)));
write_to_serial(SP, std::string(1, (unsigned char)(values.size())));
DWORD bytesSent = NULL;
HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
for (size_t i = 0; i < values.size(); i++) {
write_to_serial(SP, std::string(1, (unsigned char)(addrs[i] >> 8)));
write_to_serial(SP, std::string(1, (unsigned char)(addrs[i])));
bytesSent += write_to_serial(SP, std::string(1, values[i]));
std::this_thread::sleep_for(std::chrono::milliseconds(1));
std::cout << static_cast<int>((static_cast<float>(i) / values.size()) * 100.f) << "%";
SetConsoleCursorPosition(output, { 16, 0 });
}
std::cout << "100%";
std::cout << std::dec << std::endl << bytesSent << " bytes have been written. Reading the EEPROM into the buffer... ";
read_into_buffer(SP);
std::cout << "\nSuccess. Press any key to continue... ";
wait_for_key_press();
}
static void erase(Serial* SP) {
std::cout << "\nYou're about to erase all data in the EEPROM.\n"
<< "Press Enter to proceed, Space to cancel. ";
while (true) {
int c = _getch();
if (c == 32) {
clear_screen();
return;
}
else if (c == 13) break;
}
std::cout << std::endl << "Erasing... ";
std::vector addrs { 0x5555, 0x2AAA, 0x5555, 0x5555, 0x2AAA, 0x5555 };
std::vector values{ 0xAA, 0x55, 0x80, 0xAA, 0x55, 0x10 }; // doesn't work, idk why
write_to_serial(SP, std::string(1, static_cast<char>(0x00)));
write_to_serial(SP, std::string(1, (unsigned char)(values.size() >> 8)));
write_to_serial(SP, std::string(1, (unsigned char)(values.size())));
DWORD bytesSent = NULL;
HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
for (size_t i = 0; i < values.size(); i++) {
write_to_serial(SP, std::string(1, (unsigned char)(addrs[i] >> 8)));
write_to_serial(SP, std::string(1, (unsigned char)(addrs[i])));
bytesSent += write_to_serial(SP, std::string(1, values[i]));
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
std::this_thread::sleep_for(std::chrono::milliseconds(20));
std::cout << "\nAll bytes have been set to 0x00. Reading the EEPROM into the buffer... ";
read_into_buffer(SP);
std::cout << "\nSuccess. Press any key to continue... ";
wait_for_key_press();
}
static void sdt_disable(Serial* SP) {
std::vector addrs { 0x5555, 0x2AAA, 0x5555, 0x5555, 0x2AAA, 0x5555, 0x0000, 0x0000 };
std::vector values{ 0xAA, 0x55, 0x80, 0xAA, 0x55, 0x20, 0xEA, 0xEA };
write_to_serial(SP, std::string(1, static_cast<char>(0x00)));
write_to_serial(SP, std::string(1, (unsigned char)(values.size() >> 8)));
write_to_serial(SP, std::string(1, (unsigned char)(values.size())));
DWORD bytesSent = NULL;
HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
for (size_t i = 0; i < values.size(); i++) {
write_to_serial(SP, std::string(1, (unsigned char)(addrs[i] >> 8)));
write_to_serial(SP, std::string(1, (unsigned char)(addrs[i])));
bytesSent += write_to_serial(SP, std::string(1, values[i]));
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
std::cout << "\nSDP (Software Data Protection) has been disabled. Press any key to continue... ";
wait_for_key_press();
}
static void sdt_enable(Serial* SP) {
std::vector addrs { 0x5555, 0x2AAA, 0x5555, 0x0000, 0x0000 };
std::vector values{ 0xAA, 0x55, 0xA0, 0xEA, 0xEA };
write_to_serial(SP, std::string(1, static_cast<char>(0x00)));
write_to_serial(SP, std::string(1, (unsigned char)(values.size() >> 8)));
write_to_serial(SP, std::string(1, (unsigned char)(values.size())));
DWORD bytesSent = NULL;
HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
for (size_t i = 0; i < values.size(); i++) {
write_to_serial(SP, std::string(1, (unsigned char)(addrs[i] >> 8)));
write_to_serial(SP, std::string(1, (unsigned char)(addrs[i])));
bytesSent += write_to_serial(SP, std::string(1, values[i]));
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
std::cout << "\nSDP (Software Data Protection) has been enabled. Press any key to continue... ";
wait_for_key_press();
}
static void set_sdp(Serial* SP) {
clear_screen();
int sel = 0;
std::cout << "Select operation:\n"
"> Enable SDP (Software Data Protection)\n"
" Disable SDP\n"
" Cancel\n";
unsigned int ch = 0;
bool loop = true;
while (loop) {
ch = 0;
HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
switch (ch = _getch()) {
case KEY_DOWN:
SetConsoleCursorPosition(output, { 0, (short)(1 + sel) });
std::cout << (char)(0x20) << std::flush;
((sel != 2) ? sel++ : sel = 0);
SetConsoleCursorPosition(output, { 0, (short)(1 + sel) });
std::cout << ">" << std::flush;
SetConsoleCursorPosition(output, { 0, 4 });
break;
case KEY_UP:
SetConsoleCursorPosition(output, { 0, (short)(1 + sel) });
std::cout << (char)(0x20) << std::flush;
((sel != 0) ? sel-- : sel = 2);
SetConsoleCursorPosition(output, { 0, (short)(1 + sel) });
std::cout << ">" << std::flush;
SetConsoleCursorPosition(output, { 0, 4 });
break;
case VK_RETURN:
loop = false;
break;
}
}
switch (sel) {
case 0:
mainmenu::sdt_enable(SP);
break;
case 1:
mainmenu::sdt_disable(SP);
break;
case 2:
return;
}
}
}
int main(int argc, char* argv[]) {
try {
Serial* SP;
unsigned sel;
std::vector<int> comPorts;
while (true) {
while (true) {
comPorts = GetAvailableCOMPorts();
if (comPorts.size() == 0) {
std::cerr << "No Arduino detected! Ensure proper connectivity, then press any key to retry... ";
wait_for_key_press();
continue;
}
std::vector<std::string> deviceNames = GetDeviceNames();
sel = 0;
if (comPorts.size() >= 2) {
std::cout << "Select serial port..." << std::endl;
for (int i = 0; i < comPorts.size(); i++) {
std::cout << (!i ? "> " : " ") << deviceNames[i] << std::endl;
}
unsigned int ch = 0;
bool loop = true;
while (loop) {
ch = 0;
HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
switch (ch = _getch()) {
case KEY_DOWN:
SetConsoleCursorPosition(output, { 0, (short)(1 + sel) });
std::cout << (char)(0x20) << std::flush;
((sel != comPorts.size() - 1) ? sel++ : sel = 0);
SetConsoleCursorPosition(output, { 0, (short)(1 + sel) });
std::cout << ">" << std::flush;
SetConsoleCursorPosition(output, { 0, (short)(comPorts.size() + 1) });
break;
case KEY_UP:
SetConsoleCursorPosition(output, { 0, (short)(1 + sel) });
std::cout << (char)(0x20) << std::flush;
((sel != 0) ? sel-- : sel = static_cast<unsigned int>(comPorts.size()) - 1);
SetConsoleCursorPosition(output, { 0, (short)(1 + sel) });
std::cout << ">" << std::flush;
SetConsoleCursorPosition(output, { 0, (short)(comPorts.size() + 1) });
break;
case VK_RETURN:
loop = false;
break;
}
}
}
char def[11] = "\\\\.\\COM";
char port[15]{};
sprintf_s(port, "%s%u", def, comPorts[sel]);
std::cout << "Connecting to " << std::string("COM") + std::to_string(comPorts[sel]) << "...";
SP = new Serial(port);
if (!SP->IsConnected()) {
std::cerr << std::endl << "Connection failed! Close all other instances and try again. Press any key to continue... ";
wait_for_key_press();
continue;
}
break;
}
clear_screen();
std::cout << "Connection established! Baud rate: 115200";
int chip = 0;
std::cout << "\nSelect the chip size:\n"
"> 64K (8K x 8)\n"
" 256K (32K x 8)\n";
unsigned int ch = 0;
bool loop = true;
while (loop) {
ch = 0;
HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
switch (ch = _getch()) {
case KEY_DOWN:
SetConsoleCursorPosition(output, { 0, (short)(2 + chip) });
std::cout << (char)(0x20) << std::flush;
((chip != 1) ? chip++ : chip = 0);
SetConsoleCursorPosition(output, { 0, (short)(2 + chip) });
std::cout << ">" << std::flush;
SetConsoleCursorPosition(output, { 0, 4 });
break;
case KEY_UP:
SetConsoleCursorPosition(output, { 0, (short)(2 + chip) });
std::cout << (char)(0x20) << std::flush;
((chip != 0) ? chip-- : chip = 1);
SetConsoleCursorPosition(output, { 0, (short)(2 + chip) });
std::cout << ">" << std::flush;
SetConsoleCursorPosition(output, { 0, 4 });
break;
case VK_RETURN:
loop = false;
break;
}
}
char sdata[] = { 0x02, chip };
size = chip ? 32768 : 8192;
for (size_t i = 0; i < 2; i++) {
mainmenu::write_to_serial(SP, std::string(1, sdata[i]));
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
clear_screen();
std::cout << "Reading EEPROM into buffer... ";
mainmenu::read_into_buffer(SP);
clear_screen();
while (true) {
if (!SP->IsConnected()) {
std::cerr << "Connection lost to " << std::string("COM") + std::to_string(comPorts[sel]) << ". Press any key to reconnect... ";
wait_for_key_press();
break;
}
std::cout << "EEPROMprogrammer [Version 0.1]\nCopyright (c) 2017-2023 Yilmaz Alpaslan\n\n";
std::cout << "Select an operation:\n"
"> Read\n"
" Verify\n"
" Dump to file\n"
" Write\n"
" Erase\n"
" Set SDP\n"
" Exit\n";
unsigned int ch = 0, sel = 0;
bool loop = true;
while (loop) {
ch = 0;
HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
switch (ch = _getch()) {
case KEY_DOWN:
SetConsoleCursorPosition(output, { 0, (short)(4 + sel) });
std::cout << (char)(0x20) << std::flush;
((sel != 6) ? sel++ : sel = 0);
SetConsoleCursorPosition(output, { 0, (short)(4 + sel) });
std::cout << ">" << std::flush;
SetConsoleCursorPosition(output, { 0, 11 });
break;
case KEY_UP:
SetConsoleCursorPosition(output, { 0, (short)(4 + sel) });
std::cout << (char)(0x20) << std::flush;
((sel != 0) ? sel-- : sel = 6);
SetConsoleCursorPosition(output, { 0, (short)(4 + sel) });
std::cout << ">" << std::flush;
SetConsoleCursorPosition(output, { 0, 11 });
break;
case VK_RETURN:
loop = false;
break;
}
}
switch (sel) {
case READ:
mainmenu::print(data);
std::cout << "Press any key to continue... ";
wait_for_key_press();
clear_screen();
break;
case VERIFY:
mainmenu::verify();
break;
case DUMP:
mainmenu::dump();
break;
case WRITE:
mainmenu::write(SP);
break;
case ERASE:
mainmenu::erase(SP);
break;
case SDP:
mainmenu::set_sdp(SP);
break;
case EXIT:
return 0;
}
}
}
return 0;
}
catch (const std::exception&) {
return 1;
}
}
std::vector<std::string> GetDeviceNames() {
SP_DEVINFO_DATA devInfoData = {};
devInfoData.cbSize = sizeof(devInfoData);
HDEVINFO hDeviceInfo = SetupDiGetClassDevs(&GUID_DEVCLASS_PORTS, NULL, nullptr, DIGCF_PRESENT);
if (hDeviceInfo == INVALID_HANDLE_VALUE) {
throw std::exception();
}
int nDevice = 0;
std::vector<std::string> names;
while (SetupDiEnumDeviceInfo(hDeviceInfo, nDevice++, &devInfoData)) {
DWORD regDataType;
DWORD reqSize = 0;
SetupDiGetDeviceRegistryPropertyW(hDeviceInfo, &devInfoData, SPDRP_HARDWAREID, nullptr, nullptr, 0, &reqSize);
BYTE* hardwareId = new BYTE[MAX_PATH];
if (SetupDiGetDeviceRegistryPropertyW(hDeviceInfo, &devInfoData, SPDRP_HARDWAREID, ®DataType, hardwareId, sizeof(hardwareId) * reqSize, nullptr)) {
reqSize = 0;
SetupDiGetDeviceRegistryPropertyW(hDeviceInfo, &devInfoData, SPDRP_FRIENDLYNAME, nullptr, nullptr, 0, &reqSize);
BYTE* friendlyName = new BYTE[MAX_PATH];
if (!SetupDiGetDeviceRegistryPropertyW(hDeviceInfo, &devInfoData, SPDRP_FRIENDLYNAME, nullptr, friendlyName, sizeof(friendlyName) * reqSize, nullptr)) {
memset(friendlyName, 0, reqSize > 1 ? reqSize : 1);
}
std::string deviceName;
for (int i = 0; i < MAX_PATH; i++) {
char c = *(friendlyName + i);
deviceName += c;
if (c == ')') break;
}
names.push_back(deviceName);
delete[] friendlyName;
}
delete[] hardwareId;
}
return names;
}
std::vector<int> GetAvailableCOMPorts() {
char lpTargetPath[5000];
std::vector<int> output;
for (int i = 0; i < 255; i++) {
std::string str = "COM" + std::to_string(i);
DWORD test = QueryDosDeviceA(str.c_str(), lpTargetPath, 5000);
if (test) {
output.push_back(i);
}
}
return output;
}