-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
378 lines (329 loc) · 12.1 KB
/
main.cpp
File metadata and controls
378 lines (329 loc) · 12.1 KB
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
#include "espif.h"
#include "exception.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <boost/program_options.hpp>
#include <boost/tokenizer.hpp>
enum
{
flash_sector_size = 4096,
};
namespace po = boost::program_options;
static bool option_raw = false;
static bool option_verbose = false;
static bool option_debug = false;
static bool option_no_provide_checksum = false;
static bool option_no_request_checksum = false;
static bool option_use_tcp = false;
static bool option_dontwait = false;
static unsigned int option_broadcast_group_mask = 0;
static unsigned int option_multicast_burst = 1;
int main(int argc_in, const char **argv_in)
{
po::options_description options("usage");
try
{
int current_argv_index;
const char *current_argv_pointer;
std::vector<std::string> argv;
std::vector<std::string> host_args;
std::vector<std::string> proxy_signal_ids;
std::string host;
std::string args;
std::string command_port;
std::string filename;
std::string start_string;
std::string length_string;
int start;
int image_slot;
int image_timeout;
int dim_x, dim_y, depth;
unsigned int length;
bool nocommit = false;
bool noreset = false;
bool notemp = false;
bool otawrite = false;
bool proxy_read_uart = false;
bool proxy_read_uart_hex = false;
bool cmd_write = false;
bool cmd_simulate = false;
bool cmd_verify = false;
bool cmd_benchmark = false;
bool cmd_image = false;
bool cmd_proxy = false;
bool cmd_image_epaper = false;
bool cmd_broadcast = false;
bool cmd_multicast = false;
bool cmd_read = false;
bool cmd_info = false;
unsigned int selected;
for(current_argv_index = 1; current_argv_index < argc_in; current_argv_index++)
{
current_argv_pointer = argv_in[current_argv_index];
if((strlen(current_argv_pointer) > 1) && (current_argv_pointer[0] == '@'))
{
std::string include_filename;
std::ifstream file;
std::stringstream stream;
std::string contents;
typedef boost::char_separator<char> separator_t;
separator_t separator(" \t\n");
typedef boost::tokenizer<separator_t> tokenizer_t;
tokenizer_t tokenizer(std::string(""), separator);
include_filename = std::string(¤t_argv_pointer[1]);
file.open(include_filename);
if(!file.is_open())
{
std::cerr << "warning: cannot open include file \"" << include_filename << "\"\n";
continue;
}
stream << file.rdbuf();
contents = stream.str();
tokenizer.assign(contents);
for(tokenizer_t::const_iterator token = tokenizer.begin(); token != tokenizer.end(); token++)
argv.push_back(*token);
file.close();
}
else
argv.push_back(std::string(current_argv_pointer));
}
options.add_options()
("info,i", po::bool_switch(&cmd_info)->implicit_value(true), "INFO")
("read,R", po::bool_switch(&cmd_read)->implicit_value(true), "READ")
("verify,V", po::bool_switch(&cmd_verify)->implicit_value(true), "VERIFY")
("simulate,S", po::bool_switch(&cmd_simulate)->implicit_value(true), "WRITE simulate")
("write,W", po::bool_switch(&cmd_write)->implicit_value(true), "WRITE")
("benchmark,B", po::bool_switch(&cmd_benchmark)->implicit_value(true), "BENCHMARK")
("image,I", po::bool_switch(&cmd_image)->implicit_value(true), "SEND IMAGE")
("proxy,P", po::bool_switch(&cmd_proxy)->implicit_value(true), "START PROXY")
("proxy-signal-id,q", po::value<std::vector<std::string> >(&proxy_signal_ids), "PROXY signal ids to listen to")
("proxy-read-uart,U", po::bool_switch(&proxy_read_uart)->implicit_value(true), "PROXY also frequently fetch uart data")
("proxy-read-uart-hex,H", po::bool_switch(&proxy_read_uart_hex)->implicit_value(true), "PROXY also frequently fetch uart data and return as HEX")
("epaper-image,e", po::bool_switch(&cmd_image_epaper)->implicit_value(true), "SEND EPAPER IMAGE (uc8151d connected to host)")
("broadcast,b", po::bool_switch(&cmd_broadcast)->implicit_value(true), "BROADCAST SENDER send broadcast message")
("multicast,M", po::bool_switch(&cmd_multicast)->implicit_value(true), "MULTICAST SENDER send multicast message")
("host,h", po::value<std::vector<std::string> >(&host_args)->required(), "host or broadcast address or multicast group to use")
("verbose,v", po::bool_switch(&option_verbose)->implicit_value(true), "verbose output")
("debug,D", po::bool_switch(&option_debug)->implicit_value(true), "packet trace etc.")
("tcp,t", po::bool_switch(&option_use_tcp)->implicit_value(true), "use TCP instead of UDP")
("filename,f", po::value<std::string>(&filename), "file name")
("start,s", po::value<std::string>(&start_string)->default_value("-1"), "send/receive start address (OTA is default)")
("length,l", po::value<std::string>(&length_string)->default_value("0x1000"), "read length")
("command-port,p", po::value<std::string>(&command_port)->default_value("24"), "command port to connect to")
("nocommit,n", po::bool_switch(&nocommit)->implicit_value(true), "don't commit after writing")
("noreset,N", po::bool_switch(&noreset)->implicit_value(true), "don't reset after commit")
("notemp,T", po::bool_switch(¬emp)->implicit_value(true), "don't commit temporarily, commit to flash")
("dontwait,d", po::bool_switch(&option_dontwait)->implicit_value(true), "don't wait for reply on message")
("image_slot,x", po::value<int>(&image_slot)->default_value(-1), "send image to flash slot x instead of frame buffer")
("image_timeout,y", po::value<int>(&image_timeout)->default_value(5000), "freeze frame buffer for y ms after sending")
("no-provide-checksum,1", po::bool_switch(&option_no_provide_checksum)->implicit_value(true), "do not provide checksum")
("no-request-checksum,2", po::bool_switch(&option_no_request_checksum)->implicit_value(true), "do not request checksum")
("raw,r", po::bool_switch(&option_raw)->implicit_value(true), "do not use packet encapsulation")
("broadcast-groups,g", po::value<unsigned int>(&option_broadcast_group_mask)->default_value(0), "select broadcast groups (bitfield)")
("burst,u", po::value<unsigned int>(&option_multicast_burst)->default_value(1), "burst broadcast and multicast packets multiple times");
po::positional_options_description positional_options;
positional_options.add("host", -1);
po::variables_map varmap;
auto parsed = po::command_line_parser(argv).options(options).positional(positional_options).run();
po::store(parsed, varmap);
po::notify(varmap);
auto it = host_args.begin();
host = *(it++);
auto it1 = it;
for(; it != host_args.end(); it++)
{
if(it != it1)
args.append(" ");
args.append(*it);
}
if(option_broadcast_group_mask)
cmd_broadcast = true;
selected = 0;
if(cmd_read)
selected++;
if(cmd_write)
selected++;
if(cmd_simulate)
selected++;
if(cmd_verify)
selected++;
if(cmd_benchmark)
selected++;
if(cmd_image)
selected++;
if(cmd_proxy)
selected++;
if(cmd_image_epaper)
selected++;
if(cmd_info)
selected++;
if(cmd_broadcast)
selected++;
if(cmd_multicast)
selected++;
if(selected > 1)
throw(hard_exception("specify one of write/simulate/verify/image/epaper-image/read/info"));
Espif espif(
EspifConfig
{
.host = host,
.command_port = command_port,
.use_tcp = option_use_tcp,
.broadcast = cmd_broadcast,
.multicast = cmd_multicast,
.debug = option_debug,
.verbose = option_verbose,
.dontwait = option_dontwait,
.broadcast_group_mask = option_broadcast_group_mask,
.multicast_burst = option_multicast_burst,
.raw = option_raw,
.provide_checksum = !option_no_provide_checksum,
.request_checksum = !option_no_request_checksum
}
);
if(selected == 0)
std::cout << espif.send(args);
else
{
if(cmd_broadcast || cmd_multicast)
std::cout << espif.multicast(args);
else
{
if(cmd_proxy)
espif.run_proxy(proxy_read_uart, proxy_read_uart_hex, proxy_signal_ids);
else
{
start = -1;
try
{
start = std::stoi(start_string, 0, 0);
}
catch(const std::invalid_argument &)
{
throw(hard_exception("invalid value for start argument"));
}
catch(const std::out_of_range &)
{
throw(hard_exception("invalid value for start argument"));
}
try
{
length = std::stoi(length_string, 0, 0);
}
catch(const std::invalid_argument &)
{
throw(hard_exception("invalid value for length argument"));
}
catch(const std::out_of_range &)
{
throw(hard_exception("invalid value for length argument"));
}
std::string reply;
std::vector<int> int_value;
std::vector<std::string> string_value;
unsigned int flash_slot, flash_address[2];
try
{
espif.process("flash-info", "", reply, nullptr,
"OK flash function available, slots: 2, current: ([0-9]+), sectors: \\[ ([0-9]+), ([0-9]+) \\], display: ([0-9]+)x([0-9]+)px@([0-9]+)",
&string_value, &int_value);
}
catch(const espif_exception &e)
{
throw(hard_exception(boost::format("flash incompatible image: %s") % e.what()));
}
flash_slot = int_value[0];
flash_address[0] = int_value[1];
flash_address[1] = int_value[2];
dim_x = int_value[3];
dim_y = int_value[4];
depth = int_value[5];
if(option_verbose)
std::cout <<
boost::format("flash update available, current slot: %u, address[0]: 0x%x (sector %u), address[1]: 0x%x (sector %u), display graphical dimensions: %ux%u px at depth %u") %
flash_slot % (flash_address[0] * flash_sector_size) % flash_address[0] % (flash_address[1] * flash_sector_size) % flash_address[1] % dim_x % dim_y % depth << std::endl;
if(start == -1)
{
if(cmd_write || cmd_simulate || cmd_verify || cmd_info)
{
flash_slot++;
if(flash_slot >= 2)
flash_slot = 0;
start = flash_address[flash_slot];
otawrite = true;
}
else
if(!cmd_benchmark && !cmd_image && !cmd_image_epaper && !cmd_proxy)
throw(hard_exception("start address not set"));
}
if(cmd_read)
espif.read(filename, start, length);
else
if(cmd_verify)
espif.verify(filename, start);
else
if(cmd_simulate)
espif.write(filename, start, true, false);
else
if(cmd_write)
{
espif.write(filename, start, false, otawrite);
if(otawrite && !nocommit)
espif.commit_ota(flash_slot, start, !noreset, notemp);
}
else
if(cmd_benchmark)
espif.benchmark(length);
else
if(cmd_image)
espif.image(image_slot, filename, dim_x, dim_y, depth, image_timeout);
else
if(cmd_image_epaper)
espif.image_epaper(filename);
}
}
}
}
catch(const po::error &e)
{
std::cerr << std::endl << boost::format("espif: program option exception: %s") % e.what() << std::endl << options;
return(1);
}
catch(const hard_exception &e)
{
std::cerr << std::endl << boost::format("espif: error: %s") % e.what() << std::endl;
return(1);
}
catch(const transient_exception &e)
{
std::cerr << std::endl << boost::format("espif: transient exception: %s") % e.what() << std::endl;
return(1);
}
catch(const espif_exception &e)
{
std::cerr << std::endl << boost::format("espif: unhandled espif exception: %s") % e.what() << std::endl;
return(1);
}
catch(const std::exception &e)
{
std::cerr << std::endl << boost::format("espif: unhandled generic exception: %s") % e.what() << std::endl;
return(1);
}
catch(const std::string &e)
{
std::cerr << std::endl << boost::format("espif: unhandled string exception: %s ") % e << std::endl;
return(1);
}
catch(const char *e)
{
std::cerr << std::endl << boost::format("espif: unhandled cstr exception: %s") % e << std::endl;
return(1);
}
catch(...)
{
std::cerr << std::endl << "espif: unhandled unknown exception" << std::endl;
return(1);
}
return(0);
}