-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.cpp
More file actions
323 lines (250 loc) · 7.86 KB
/
util.cpp
File metadata and controls
323 lines (250 loc) · 7.86 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
#include "util.h"
#include "packet.h"
#include "exception.h"
#include <string>
#include <iostream>
#include <boost/format.hpp>
#include <boost/regex.hpp>
Util::Util(GenericSocket &channel_in, const EspifConfig &config_in) noexcept
:
channel(channel_in),
config(config_in)
{
}
std::string Util::dumper(const char *id, const std::string text)
{
int ix;
char current;
std::string out;
out = (boost::format("%s[%d]: \"") % id % text.length()).str();
for(ix = 0; (ix < (int)text.length()) && (ix < 96); ix++)
{
current = text.at(ix);
if((current >= ' ') && (current <= '~'))
out.append(1, current);
else
out.append((boost::format("[%02x]") % ((unsigned int)current & 0xff)).str());
}
out.append("\"");
return(out);
}
std::string Util::sha1_hash_to_text(unsigned int length, const unsigned char *hash)
{
unsigned int current;
std::string hash_string;
for(current = 0; current < length; current++)
hash_string.append((boost::format("%02x") % (unsigned int)hash[current]).str());
return(hash_string);
}
int Util::process(const std::string &data, const std::string &oob_data, std::string &reply_data, std::string *reply_oob_data,
const char *match, std::vector<std::string> *string_value, std::vector<int> *int_value) const
{
enum { max_attempts = 8 };
unsigned int attempt;
Packet send_packet(data, oob_data);
std::string send_data;
std::string packet;
Packet receive_packet;
std::string receive_data;
boost::smatch capture;
boost::regex re(match ? match : "");
unsigned int captures;
int timeout;
if(config.debug)
std::cout << std::endl << Util::dumper("data", data) << std::endl;
packet = send_packet.encapsulate(config.raw, config.provide_checksum, config.request_checksum, config.broadcast_group_mask);
timeout = 100;
for(attempt = 0; attempt < max_attempts; attempt++)
{
try
{
send_data = packet;
while(send_data.length() > 0)
if(!channel.send(send_data))
throw(transient_exception("send failed"));
receive_packet.clear();
while(!receive_packet.complete())
{
receive_data.clear();
if(!channel.receive(receive_data))
throw(transient_exception("receive failed"));
receive_packet.append_data(receive_data);
}
if(!receive_packet.decapsulate(&reply_data, reply_oob_data, config.verbose))
throw(transient_exception("decapsulation failed"));
if(match && !boost::regex_match(reply_data, capture, re))
throw(transient_exception(boost::format("received string does not match: \"%s\" vs. \"%s\"") % Util::dumper("reply", reply_data) % match));
break;
}
catch(const transient_exception &e)
{
if(config.verbose)
std::cout << boost::format("process attempt #%u failed: %s, backoff %u ms") % attempt % e.what() % timeout << std::endl;
channel.drain(timeout);
timeout *= 2;
continue;
}
}
if(config.verbose && (attempt > 0))
std::cerr << boost::format("success at attempt %u") % attempt << std::endl;
if(attempt >= max_attempts)
throw(transient_exception("process: no more attempts"));
if(string_value || int_value)
{
if(string_value)
string_value->clear();
if(int_value)
int_value->clear();
captures = 0;
for(const auto &it : capture)
{
if(captures++ == 0)
continue;
if(string_value)
string_value->push_back(it);
if(int_value)
{
try
{
int_value->push_back(stoi(it, 0, 0));
}
catch(std::invalid_argument &)
{
int_value->push_back(0);
}
catch(std::out_of_range &)
{
int_value->push_back(0);
}
}
}
}
if(config.debug)
std::cout << std::endl << Util::dumper("reply", reply_data) << std::endl;
return(attempt);
}
int Util::read_sector(unsigned int sector_size, unsigned int sector, std::string &data) const
{
std::string reply;
std::vector<int> int_value;
std::vector<std::string> string_value;
int retries;
try
{
retries = process((boost::format("flash-read %u\n") % sector).str(), "",
reply, &data, "OK flash-read: read sector ([0-9]+)", &string_value, &int_value);
}
catch(const hard_exception &e)
{
throw(hard_exception(boost::format("read sector: hard exception: %s") % e.what()));
}
catch(const transient_exception &e)
{
throw(transient_exception(boost::format("read sector: transient exception: %s") % e.what()));
}
if(data.length() < sector_size)
{
if(config.verbose)
std::cout << boost::format("flash sector read failed: incorrect length, expected: %u, received: %u, reply: %s") %
sector_size % data.length() % reply << std::endl;
throw(transient_exception(boost::format("read_sector failed: incorrect length (%u vs. %u)") % sector_size % data.length()));
}
if(int_value[0] != (int)sector)
{
if(config.verbose)
std::cout << boost::format("flash sector read failed: local sector #%u != remote sector #%u") % sector % int_value[0] << std::endl;
throw(transient_exception(boost::format("read sector failed: incorrect sector (%u vs. %u)") % sector % int_value[0]));
}
return(retries);
}
int Util::write_sector(unsigned int sector, const std::string &data,
unsigned int &written, unsigned int &erased, unsigned int &skipped, bool simulate) const
{
std::string command;
std::string reply;
std::vector<int> int_value;
std::vector<std::string> string_value;
unsigned int attempt;
unsigned int process_tries;
command = (boost::format("flash-write %u %u") % (simulate ? 0 : 1) % sector).str();
for(attempt = 4; attempt > 0; attempt--)
{
try
{
process_tries = process(command, data,
reply, nullptr, "OK flash-write: written mode ([01]), sector ([0-9]+), same ([01]), erased ([01])", &string_value, &int_value);
if(int_value[0] != (simulate ? 0 : 1))
throw(transient_exception(boost::format("invalid mode (%u vs. %u)") % (simulate ? 0 : 1) % int_value[0]));
if(int_value[1] != (int)sector)
throw(transient_exception(boost::format("wrong sector (%u vs %u)") % sector % int_value[0]));
if(int_value[2] != 0)
skipped++;
else
written++;
if(int_value[3] != 0)
erased++;
break;
}
catch(const transient_exception &e)
{
std::cerr << std::endl << boost::format("flash sector write failed temporarily: %s, reply: %s ") % e.what() % reply << std::endl;
continue;
}
}
if(attempt == 0)
throw(hard_exception("write sector: no more attempts"));
return(process_tries);
}
void Util::get_checksum(unsigned int sector, unsigned int sectors, std::string &checksum) const
{
std::string reply;
std::vector<int> int_value;
std::vector<std::string> string_value;
try
{
process((boost::format("flash-checksum %u %u\n") % sector % sectors).str(), "",
reply, nullptr, "OK flash-checksum: checksummed ([0-9]+) sectors from sector ([0-9]+), checksum: ([0-9a-f]+)",
&string_value, &int_value);
}
catch(const transient_exception &e)
{
boost::format fmt("flash sector checksum failed temporarily: %s, reply: %s");
fmt % e.what() % reply;
if(config.verbose)
std::cout << fmt << std::endl;
throw(transient_exception(fmt));
}
catch(const hard_exception &e)
{
boost::format fmt("flash sector checksum failed: %s, reply: %s");
fmt % e.what() % reply;
if(config.verbose)
std::cout << fmt << std::endl;
throw(hard_exception(fmt));
}
if(int_value[0] != (int)sectors)
{
boost::format fmt("flash sector checksum failed: local sectors (%u) != remote sectors (%u)");
fmt % sectors % int_value[0];
if(config.verbose)
std::cout << fmt << std::endl;
throw(transient_exception(fmt));
}
if(int_value[1] != (int)sector)
{
boost::format fmt("flash sector checksum failed: local start sector (%u) != remote start sector (%u)");
fmt % sector % int_value[1];
if(config.verbose)
std::cout << fmt << std::endl;
throw(transient_exception(fmt));
}
checksum = string_value[2];
}
void Util::time_to_string(std::string &dst, const time_t &ticks)
{
struct tm tm;
char timestring[64];
localtime_r(&ticks, &tm);
strftime(timestring, sizeof(timestring), "%Y/%m/%d %H:%M:%S", &tm);
dst = timestring;
}