-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
417 lines (362 loc) · 13.6 KB
/
main.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
/*
* Copyright (C) 2023 Debayan Sutradhar (rnayabed) ([email protected])
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "logger.h"
#include "serialdevice.h"
#include "xmodem.h"
enum ArgType
{
LOG_TO_FILE,
BINARY_PATH,
TARGET_PATH,
XMODEM_MAX_RETRY,
XMODEM_BLOCK_SIZE,
SERIAL_DEVICE_ARIES,
SERIAL_PARITY_YES,
SERIAL_STOP_BITS,
SERIAL_RTS_CTS_YES,
SERIAL_BITS,
SERIAL_BAUD_RATE,
SERIAL_READ_TIMEOUT,
START_AFTER_UPLOAD,
PRINT_LICENSE,
PRINT_USAGE,
INVALID
};
ArgType getArgType(char* const& arg)
{
using namespace std;
if(!string(arg).compare("-l") ||
!string(arg).compare("--log"))
return ArgType::LOG_TO_FILE;
else if (!string(arg).compare("-bp") ||
!string(arg).compare("--binary-path"))
return ArgType::BINARY_PATH;
else if (!string(arg).compare("-tp") ||
!string(arg).compare("--target-path"))
return ArgType::TARGET_PATH;
else if(!string(arg).compare("-xmr") ||
!string(arg).compare("--xmodem-max-retry"))
return ArgType::XMODEM_MAX_RETRY;
else if(!string(arg).compare("-xbs") ||
!string(arg).compare("--xmodem-block-size"))
return ArgType::XMODEM_BLOCK_SIZE;
else if(!string(arg).compare("--aries"))
return ArgType::SERIAL_DEVICE_ARIES;
else if(!string(arg).compare("-sp") ||
!string(arg).compare("--serial-parity"))
return ArgType::SERIAL_PARITY_YES;
else if(!string(arg).compare("-ssb") ||
!string(arg).compare("--serial-stop-bits"))
return ArgType::SERIAL_STOP_BITS;
else if(!string(arg).compare("-src") ||
!string(arg).compare("--serial-rts-cts"))
return ArgType::SERIAL_RTS_CTS_YES;
else if(!string(arg).compare("-sb") ||
!string(arg).compare("--serial-bits"))
return ArgType::SERIAL_BITS;
else if(!string(arg).compare("-sbr") ||
!string(arg).compare("--serial-baud-rate"))
return ArgType::SERIAL_BAUD_RATE;
else if(!string(arg).compare("-sau") ||
!string(arg).compare("--start-after-upload"))
return ArgType::START_AFTER_UPLOAD;
else if(!string(arg).compare("-srt") ||
!string(arg).compare("--serial-read-timeout"))
return ArgType::SERIAL_READ_TIMEOUT;
else if(!string(arg).compare("--license"))
return ArgType::PRINT_LICENSE;
else if(!string(arg).compare("-h") ||
!string(arg).compare("--help"))
return ArgType::PRINT_USAGE;
return ArgType::INVALID;
}
void printLicense()
{
constexpr const std::string_view license = R"(vegadude Copyright (C) 2023 Debayan Sutradhar (rnayabed) ([email protected])
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions.
To view the full license, you may visit:
)";
Logger::get() << license << LICENSE << Logger::NewLine;
}
void printUsage()
{
constexpr const std::string_view usage = R"(Usage: [-l | --log] [-bp | --binary-path]
[-tp | --target-path]
[-xmr | --xmodem-max-retry] [-xbs | --xmodem-block-size]
[--aries] [-sp | --serial-parity] [-ssb | --serial-stop-bits]
[-src | --serial-rts-cts] [-sb | --serial-bits]
[-sbr | --serial-baud-rate] [-srt | --serial-read-timeout]
[-sau | --start-after-upload] [--license] [-h | --help]
Option Summary:
-l | --log Optional. Create a log file.
-bp | --binary-path Required. Specify path to the binary file
to be uploaded.
-tp | --target-path Required. Specify path to the target board.
-xmr | --xmodem-max-retry Optional. Specify max amount of times to retry before aborting upload.
Default is 10.
-xbs | --xmodem-block-size Required if not using automatic configuration.
Specify block size of XModem data transfer.
--aries Use CDAC Aries serial port configuration.
-sp | --serial-parity Optional. Specify if target uses parity bit.
Default is false.
-ssb | --serial-stop-bits Required. Specify number of stop bits
for target.
-src | --serial-rts-cts Optional. Specify if target uses RTS/CTS
flow control.
Default is false.
-sb | --serial-bits Required. Specify the number of data bits sent
in a byte to the target.
-sbr | --serial-baud-rate Required. Specify serial baud rate of the
target.
-srt | --serial-read-timeout Optional. Specify timeout for each read in
milliseconds.
Default is 500.
-sau | --start-after-upload Optional. Immediately start running program
after uploading.
--license Print license information.
-h | --help Print this message.
NOTE: you cannot use --aries and --xmodem-block-size / --serial* arguments (except --serial-read-timeout) at the same time.)";
Logger::get() << usage << Logger::NewLine;
}
bool validateProps(const std::filesystem::path& targetPath,
const int32_t& xmodemMaxRetry,
const int32_t& xmodemBlockSize,
const int32_t& serialReadTimeout,
const SerialDevice::DeviceProperties& dp)
{
bool valid = true;
if (targetPath.empty())
{
Logger::get() << "Target path not specified." << Logger::NewLine;
valid = false;
}
if (xmodemMaxRetry == -1)
{
Logger::get() << "XMODEM Max retry invalid." << Logger::NewLine;
valid = false;
}
else if (xmodemMaxRetry < 5)
{
Logger::get() << "XMODEM Max retry needs to be atleast 5." << Logger::NewLine;
valid = false;
}
if (xmodemBlockSize == -1)
{
Logger::get() << "XMODEM Block size invalid." << Logger::NewLine;
valid = false;
}
else if (xmodemMaxRetry < 1)
{
Logger::get() << "XMODEM Block size needs to be atleast 1." << Logger::NewLine;
valid = false;
}
if (serialReadTimeout == -1)
{
Logger::get() << "Serial read timeout invalid." << Logger::NewLine;
valid = false;
}
else if (!(serialReadTimeout >= 0 && serialReadTimeout <= 25500))
{
Logger::get() << "Serial read timeout can range only in between 0 and 25500 milliseconds." << Logger::NewLine;
valid = false;
}
if (dp.stopBits == -1)
{
Logger::get() << "Stop bits invalid/not specified." << Logger::NewLine;
valid = false;
}
else if (dp.stopBits != 1 && dp.stopBits != 2)
{
Logger::get() << "There can only be 1 or 2 stop bits!" << Logger::NewLine;
valid = false;
}
if (dp.bits == -1)
{
Logger::get() << "Bits per byte invalid/not specified." << Logger::NewLine;
valid = false;
}
else if (!(dp.bits >= 5 && dp.bits <= 8))
{
Logger::get() << "There can be 5-8 bits per byte." << Logger::NewLine;
valid = false;
}
if (dp.baudRate == -1)
{
Logger::get() << "Baud rate not invalid/specified." << Logger::NewLine;
valid = false;
}
else if(dp.baudRate < 1)
{
Logger::get() << "Baud rate needs to be atleast 1." << Logger::NewLine;
valid = false;
}
return valid;
}
int stoi_e(char* str)
{
int result = -1;
try
{
result = std::stoi(str);
}
catch(const std::invalid_argument&)
{
result = -1;
}
return result;
}
int main(int argc, char** argv)
{
if (argc == 1)
{
printUsage();
return -1;
}
SerialDevice::DeviceProperties dp;
std::filesystem::path targetPath;
std::filesystem::path binaryPath;
std::filesystem::path logFilePath;
bool startAfterUpload = false;
bool isDevPropsSetManual = false;
bool isDevPropsSetAuto = false;
int32_t xmodemMaxRetry = 10;
int32_t xmodemBlockSize = -1;
int32_t serialReadTimeout = 500;
for (int32_t i = 1; i < argc; i++)
{
switch (getArgType(argv[i]))
{
case ArgType::LOG_TO_FILE:
logFilePath = argv[++i];
break;
case ArgType::BINARY_PATH:
binaryPath = argv[++i];
break;
case ArgType::TARGET_PATH:
targetPath = argv[++i];
break;
case ArgType::XMODEM_MAX_RETRY:
xmodemMaxRetry = stoi_e(argv[++i]);
break;
case ArgType::XMODEM_BLOCK_SIZE:
isDevPropsSetManual = true;
xmodemBlockSize = stoi_e(argv[++i]);
break;
case ArgType::SERIAL_DEVICE_ARIES:
isDevPropsSetAuto = true;
break;
case ArgType::SERIAL_PARITY_YES:
isDevPropsSetManual = true;
dp.parity = true;
break;
case ArgType::SERIAL_STOP_BITS:
isDevPropsSetManual = true;
dp.stopBits = stoi_e(argv[++i]);
break;
case ArgType::SERIAL_RTS_CTS_YES:
isDevPropsSetManual = true;
dp.rtsCts = true;
break;
case ArgType::SERIAL_BITS:
isDevPropsSetManual = true;
dp.bits = stoi_e(argv[++i]);
break;
case ArgType::SERIAL_BAUD_RATE:
isDevPropsSetManual = true;
dp.baudRate = stoi_e(argv[++i]);
break;
case ArgType::SERIAL_READ_TIMEOUT:
serialReadTimeout = stoi_e(argv[++i]);
break;
case ArgType::START_AFTER_UPLOAD:
startAfterUpload = true;
break;
case ArgType::PRINT_LICENSE:
printLicense();
return 0;
case ArgType::PRINT_USAGE:
printUsage();
return 0;
case ArgType::INVALID:
Logger::get() << "Invalid argument " << argv[i] << Logger::NewLine;
printUsage();
return -1;
}
}
if (isDevPropsSetAuto && isDevPropsSetManual)
{
Logger::get() << "You cannot use -aries and manually set device properties simultaneously."
<< Logger::NewLine;
return -1;
}
else if (isDevPropsSetAuto)
{
dp = SerialDevice::ARIES;
xmodemBlockSize = ARIES_XMODEM_BLOCK_SIZE;
}
if (!validateProps(targetPath, xmodemMaxRetry, xmodemBlockSize, serialReadTimeout, dp)) return -1;
if (!logFilePath.empty())
{
if(!Logger::get().setup(logFilePath))
{
Logger::get() << "Unable to setup logging!"
<< Logger::NewLine;
return -1;
}
}
if (binaryPath.empty())
{
Logger::get() << "Binary path not specified." << Logger::NewLine;
return -1;
}
Logger::get() << "vegadude " << VERSION << Logger::NewLine
<< "<" << GIT_REPOSITORY << ">" << Logger::NewLine
<< Logger::NewLine
<< "================================================" << Logger::NewLine
<< "Device Path: " << targetPath << Logger::NewLine
<< "Binary Path: " << binaryPath << Logger::NewLine
<< "Target device properties:" << Logger::NewLine
<< "Parity: " << dp.parity << Logger::NewLine
<< "Stop bits: " << dp.stopBits << Logger::NewLine
<< "RTS CTS: " << dp.rtsCts << Logger::NewLine
<< "Bits: " << dp.bits << Logger::NewLine
<< "Baud rate: " << dp.baudRate << Logger::NewLine
<< "Read Timeout (in milliseconds): " << serialReadTimeout << Logger::NewLine
<< "XMODEM Block Size " << xmodemBlockSize << Logger::NewLine
<< "XMODEM Max Retry: " << xmodemMaxRetry << Logger::NewLine
<< "================================================" << Logger::NewLine << Logger::NewLine;
SerialDevice device{targetPath, dp, serialReadTimeout};
if (!device.open())
{
Logger::get() << "Failed to setup serial device!"
<< Logger::NewLine << device.errorStr()
<< Logger::NewLine;
return -1;
}
XModem modem{device, xmodemMaxRetry, xmodemBlockSize};
if (!modem.upload(binaryPath, startAfterUpload))
{
Logger::get() << "Failed to upload file!"
<< Logger::NewLine
<< ((modem.error() == XModem::Error::DEVICE_RELATED) ? device.errorStr() : modem.errorStr())
<< Logger::NewLine;
return -1;
}
Logger::get() << (startAfterUpload ?
"Successfully uploaded and started program!"
: "Successfully uploaded program! Press enter in serial terminal to start program.");
Logger::get().close();
return 0;
}