-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathnavmon.cc
451 lines (382 loc) · 9.69 KB
/
navmon.cc
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
#include "navmon.hh"
#include <errno.h>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <stdexcept>
#include "fmt/format.h"
#include "fmt/printf.h"
#include <signal.h>
#include <sys/poll.h>
#include <iostream>
#include <vector>
using namespace std;
using Clock = std::chrono::steady_clock;
static double passedMsec(const Clock::time_point& then, const Clock::time_point& now)
{
return std::chrono::duration_cast<std::chrono::microseconds>(now - then).count()/1000.0;
}
static double passedMsec(const Clock::time_point& then)
{
return passedMsec(then, Clock::now());
}
size_t readn2Timeout(int fd, void* buffer, size_t len, double* timeout)
{
size_t pos=0;
ssize_t res;
/* The plan.
Calculate the 'end time', which is now + timeout
At beginning of loop, calculate how many milliseconds this is in the future
If <= 0, set remaining *timeout to 0, throw timeout exception
Then wait that many milliseconds, if timeout, set remaining *timeout to zero, throw timeout
Otherwise only adjust *timeout
*/
auto limit = Clock::now();
if(timeout) {
// cerr<<"Called with timeout "<<*timeout<<", "<<(*timeout*1000)<<"msec for "<<len<< " bytes"<<endl;
if(*timeout < 0)
throw TimeoutError();
limit += std::chrono::milliseconds((int)(*timeout*1000));
}
for(;;) {
if(timeout) {
double msec = passedMsec(Clock::now(), limit);
// cerr<<"Need to wait "<<msec<<" msec for at most "<<(len-pos)<<" bytes"<<endl;
if(msec < 0) {
*timeout=0;
throw TimeoutError();
}
struct pollfd pfd;
memset(&pfd, 0, sizeof(pfd));
pfd.fd = fd;
pfd.events=POLLIN;
res = poll(&pfd, 1, msec);
if(res < 0)
throw runtime_error("failed in poll: "+string(strerror(errno)));
if(!res) {
*timeout=0;
throw TimeoutError();
}
// we have data!
}
res = read(fd, (char*)buffer + pos, len - pos);
if(res == 0)
throw EofException();
if(res < 0) {
if(errno == EAGAIN)
continue;
throw runtime_error("failed in readn2: "+string(strerror(errno)));
}
pos+=(size_t)res;
if(pos == len)
break;
}
if(timeout) {
// cerr<<"Spent "<<*timeout*1000 + passedMsec(limit);
*timeout -= (*timeout*1000.0 + passedMsec(limit))/1000.0;
if(*timeout < 0)
*timeout=0;
// cerr<<", "<<(*timeout*1000)<<" left " <<endl;
}
return len;
}
size_t readn2(int fd, void* buffer, size_t len)
{
size_t pos=0;
ssize_t res;
for(;;) {
res = read(fd, (char*)buffer + pos, len - pos);
if(res == 0)
throw EofException();
if(res < 0) {
throw runtime_error("failed in readn2: "+string(strerror(errno)));
}
pos+=(size_t)res;
if(pos == len)
break;
}
return len;
}
std::string humanTimeNow()
{
time_t now = time(0);
return humanTime(now);
}
std::string humanTime(time_t t)
{
static bool set_tz = false;
struct tm tm={0};
gmtime_r(&t, &tm);
if (!set_tz) {
setenv("TZ", "UTC", 1); // We think in UTC.
tzset();
set_tz = true;
}
char buffer[80];
strftime(buffer, sizeof(buffer), "%a, %d %b %Y %H:%M:%S %z", &tm);
// strftime(buffer, sizeof(buffer), "%F %T ", &tm);
return buffer;
}
std::string humanTimeShort(time_t t)
{
static bool set_tz = false;
struct tm tm={0};
gmtime_r(&t, &tm);
if (!set_tz) {
setenv("TZ", "UTC", 1); // We think in UTC.
tzset();
set_tz = true;
}
char buffer[80];
strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M", &tm);
// strftime(buffer, sizeof(buffer), "%F %T ", &tm);
return buffer;
}
// influx ascii time format, in UTC
std::string influxTime(time_t t)
{
struct tm tm={0};
gmtime_r(&t, &tm);
char buffer[80];
std::string fmt = "%Y-%m-%d %H:%M:%S";
strftime(buffer, sizeof(buffer), fmt.c_str(), &tm);
return buffer;
}
std::string humanTime(time_t t, uint32_t nanoseconds)
{
struct tm tm={0};
gmtime_r(&t, &tm);
char buffer[80];
std::string fmt = "%a, %d %b %Y %H:%M:"+fmt::sprintf("%07.4f", tm.tm_sec + nanoseconds/1000000000.0) +" %z";
strftime(buffer, sizeof(buffer), fmt.c_str(), &tm);
return buffer;
}
// truncate to x digits precision, see testrunner.cc for details
double truncPrec(double in, unsigned int digits)
{
double partial = in - trunc(in);
int factor=1;
for(; digits ; --digits)
factor *= 10;
return trunc(in) + round(partial * factor) / factor;
}
// GLONASS URA/SISA
string humanFt(uint8_t ft)
{
static const char* ret[]={"100 cm", "200 cm", "250 cm", "400 cm", "500 cm", "7 m", "10 m", "12 m", "14 m", "16 m", "32 m", "64 m", "128 m", "256 m", "512 m", "NONE"};
if(ft < 16)
return ret[ft];
return "???";
}
// GLONASS URA/SISA
double numFt(uint8_t ft)
{
static const double ret[]={1, 2, 2.5, 4, 5, 7, 10,12,14,16,32,64,128,256,512,-1};
if(ft < 16)
return ret[ft];
return -1;
}
// Galileo SISA
string humanSisa(uint8_t sisa)
{
unsigned int sval = sisa;
if(sisa < 50)
return std::to_string(sval)+" cm";
if(sisa < 75)
return std::to_string(50 + 2* (sval-50))+" cm";
if(sisa < 100)
return std::to_string(100 + 4*(sval-75))+" cm";
if(sisa < 125)
return std::to_string(200 + 16*(sval-100))+" cm";
if(sisa < 255)
return "SPARE";
return "NO SISA AVAILABLE";
}
// Galileo SISA
double numSisa(uint8_t sisa)
{
unsigned int sval = sisa;
if(sisa < 50)
return sisa/100.0;
if(sisa < 75)
return (50 + 2* (sval-50))/100.0;
if(sisa < 100)
return (100 + 4*(sval-75))/100.0;
if(sisa < 125)
return (200 + 16*(sval-100))/100.0;
return -1;
}
// GPS/BeiDou URA
string humanUra(uint8_t ura)
{
if(ura < 6)
return fmt::sprintf("%d cm", (int)(100*pow(2.0, 1.0+1.0*ura/2.0)));
else if(ura < 15)
return fmt::sprintf("%d m", (int)(pow(2, ura-2)));
return "NO URA AVAILABLE";
}
// GPS/BeiDou URA
double numUra(uint8_t ura)
{
if(ura < 6)
return pow(2.0, 1.0+1.0*ura/2.0);
else if(ura < 15)
return pow(2, ura-2);
return -1;
}
char getGNSSChar(int id)
{
if(id==0)
return 'G';
else if(id==2)
return 'E';
else if(id==3)
return 'C';
else if(id==6)
return 'R';
else if(id==255)
return '?';
else
return '0'+id;
}
std::string makeSatIDName(const SatID& satid)
{
return fmt::sprintf("%c%02d@%d", getGNSSChar(satid.gnss), satid.sv, satid.sigid);
}
std::string makeSatPartialName(const SatID& satid)
{
return fmt::sprintf("%c%02d", getGNSSChar(satid.gnss), satid.sv);
}
int g_dtLS{18}, g_dtLSBeidou{4};
void getGPSDateFromUTC(time_t t, int& wn, int& tow)
{
t -= 315964800;
t += 18; // XXXXXX LEAP SECOND
wn = t/(7*86400);
tow = t%(7*86400);
}
void getGalDateFromUTC(time_t t, int& wn, int& tow)
{
t -= 935280000;
t += 18; // XXXXXXX LEAP SECOND
wn = t/(7*86400);
tow = t%(7*86400);
}
void getBeiDouDateFromUTC(time_t t, int&wn, int& sow)
{
// Week number count started from zero at 00:00:00 on Jan. 1, 2006 of BDT
t -= 1136070000;
t+= g_dtLSBeidou;
wn = t/(7*86400);
sow = t%(7*86400);
}
uint64_t utcFromGST(int wn, int tow)
{
return (935280000 + wn * 7*86400 + tow - g_dtLS);
}
double utcFromGST(int wn, double tow)
{
return (935280000.0 + wn * 7*86400 + tow - g_dtLS);
}
double utcFromGPS(int wn, double tow)
{
return (315964800 + wn * 7*86400 + tow - g_dtLS);
}
string makeHexDump(const string& str)
{
char tmp[5];
string ret;
ret.reserve((int)(str.size()*2.2));
for(string::size_type n=0;n<str.size();++n) {
snprintf(tmp, sizeof(tmp), "%02x ", (unsigned char)str[n]);
ret+=tmp;
}
return ret;
}
string makeHexDump(const basic_string<uint8_t>& str)
{
char tmp[5];
string ret;
ret.reserve((int)(str.size()*2.2));
for(string::size_type n=0;n<str.size();++n) {
snprintf(tmp, sizeof(tmp), "%02x ", (unsigned char)str[n]);
ret+=tmp;
}
return ret;
}
std::string sbasName(int prn)
{
string sbas;
if(prn == 138 || prn == 131 || prn == 133) {
sbas = "WAAS";
}
else if(prn== 126 || prn == 136 || prn == 123 ) {
sbas = "EGNOS";
}
else if(prn == 140 || prn == 125 || prn == 141) {
sbas = "SDCM";
}
else if(prn == 127 || prn == 128 || prn == 138) {
sbas ="GAGAN";
}
else
sbas ="SBAS?";
sbas+=" " + std::to_string(prn);
return sbas;
}
size_t writen2(int fd, const void *buf, size_t count)
{
const char *ptr = (char*)buf;
const char *eptr = ptr + count;
ssize_t res;
while(ptr != eptr) {
res = ::write(fd, ptr, eptr - ptr);
if(res < 0) {
throw runtime_error("failed in writen2: "+string(strerror(errno)));
}
else if (res == 0)
throw EofException();
ptr += (size_t) res;
}
return count;
}
void unixDie(const std::string& reason)
{
throw std::runtime_error(reason+": "+strerror(errno));
}
time_t parseTime(std::string_view in)
{
time_t now=time(0);
vector<string> formats({"%Y-%m-%d %H:%M", "%Y-%m-%d %H:%M:%S", "%Y%m%d %H%M", "%H:%M", "%H%M"});
for(const auto& f : formats) {
struct tm tm;
memset(&tm, 0, sizeof(tm));
localtime_r(&now, &tm);
tm.tm_isdst = -1;
tm.tm_sec = 0;
char* res = strptime(&in[0], f.c_str(), &tm);
if(res && !*res) {
// cerr<<"Matched on "<<f<<endl;
return mktime(&tm);
}
}
string err="Can only parse on";
for(const auto& f : formats)
err += " '"+ f+"'";
throw runtime_error(err);
}
std::string string_replace(const std::string& str, const std::string& match,
const std::string& replacement, unsigned int max_replacements)
{
size_t pos = 0;
std::string newstr = str;
unsigned int replacements = 0;
while ((pos = newstr.find(match, pos)) != std::string::npos
&& replacements < max_replacements)
{
newstr.replace(pos, match.length(), replacement);
pos += replacement.length();
replacements++;
}
return newstr;
}