-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
484 lines (426 loc) · 15.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
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
#include "gps.h"
#include <cxxopts.hpp>
#include <fmt/format.h>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <string>
#include <algorithm>
#define POSITION_LIB_NAMESPACE_BEGIN
#define POSITION_LIB_NAMESPACE_END
#define POSITION_LIB_DETAIL_NAMESPACE_BEGIN
#define POSITION_LIB_DETAIL_NAMESPACE_REFERENCE
#define POSITION_LIB_DETAIL_NAMESPACE_END
#include "external/position.hpp"
using namespace std;
// **************************************************************** //
// //
// //
// DECLARATIONS //
// //
// //
// **************************************************************** //
struct args;
enum class position_print_format : int
{
dd,
dms,
ddm,
ddm_short,
aprs,
aprs_with_timestamp = aprs,
aprs_without_timestamp
};
struct args
{
std::string host_name = "localhost";
int port = 8888;
std::string output_file = "";
position_print_format format = position_print_format::dd;
std::string command_line_error;
bool command_line_has_errors = false;
bool help = false;
bool no_stdout = false;
bool no_gps = false;
double lat = -1;
double lon = -1;
std::string aprs_comment;
std::string aprs_symbol;
std::string aprs_symbol_table;
};
bool try_parse_command_line(int argc, char* argv[], args& args);
void print_usage();
std::string to_lower(const std::string& s);
bool try_parse_bool(const std::string& s, bool& b);
bool try_parse_double(std::string str, double& number);
position_print_format parse_position_format(const std::string& pos_str);
void print_position(position_format format, const gnss_info& gnss_info);
int write_position(const std::string& filename, const gnss_info& info);
std::string encode_aprs_position_packet_no_timestamp(const std::string& symbol, const std::string& symbol_table, const std::string& comment, double lat, double lon);
std::string encode_aprs_position_packet_no_timestamp(const args& args, double lat, double lon);
std::string encode_aprs_position_packet_no_timestamp(const args& args);
std::string encode_aprs_position_packet(const std::string& symbol, const std::string& symbol_table, const std::string& comment, const gnss_info& gnss_info);
std::string encode_aprs_position_packet(const args& args, const gnss_info& gnss_info);
void print_aprs_position_packet(const args& args, const gnss_info& gnss_info);
std::string format_two_digits_string(int number);
bool try_get_gps_info(const args& args, gnss_info& info);
int main(int argc, char* argv[]);
// **************************************************************** //
// //
// //
// //
// //
// IMPLEMENTATION //
// //
// //
// //
// //
// **************************************************************** //
bool try_parse_command_line(int argc, char* argv[], args& args)
{
cxxopts::Options options("", "");
options
.add_options()
("o,output", "", cxxopts::value<std::string>())
("f,format", "", cxxopts::value<std::string>()->default_value("dd"))
("p,port", "", cxxopts::value<int>())
("h,host-name", "", cxxopts::value<std::string>())
("aprs-comment", "", cxxopts::value<std::string>())
("aprs-symbol", "", cxxopts::value<std::string>())
("aprs-symbol-table-id", "", cxxopts::value<std::string>())
("lat", "", cxxopts::value<std::string>())
("lon", "", cxxopts::value<std::string>())
("use-gps", "", cxxopts::value<std::string>())
("no-gps", "")
("help", "")
("no-stdout", "");
cxxopts::ParseResult result;
try
{
result = options.parse(argc, argv);
}
catch (const std::exception& e)
{
args.command_line_error = fmt::format("Error parsing command line: {}\n\n", e.what());
args.command_line_has_errors = true;
return false;
}
if (result.count("output") > 0)
args.output_file = result["output"].as<std::string>();
if (result.count("format") > 0)
args.format = parse_position_format(result["format"].as<std::string>());
if (result.count("port") > 0)
args.port = result["port"].as<int>();
if (result.count("host-name") > 0)
args.host_name = result["host-name"].as<std::string>();
if (result.count("help") > 0)
args.help = true;
if (result.count("no-stdout") > 0)
args.no_stdout = true;
if (result.count("use-gps") > 0)
{
bool use_gps = false;
if (!try_parse_bool(result["use-gps"].as<std::string>(), use_gps))
{
args.command_line_has_errors = true;
return false;
}
args.no_gps = !use_gps;
}
if (result.count("no-gps") > 0)
args.no_gps = true;
if (result.count("lat") > 0)
{
if(!try_parse_double(result["lat"].as<std::string>(), args.lat))
{
args.command_line_has_errors = true;
return false;
}
}
if (result.count("lon") > 0)
{
if (!try_parse_double(result["lon"].as<std::string>(), args.lon))
{
args.command_line_has_errors = true;
return false;
}
}
if (result.count("aprs-comment") > 0)
args.aprs_comment = result["aprs-comment"].as<std::string>();
if (result.count("aprs-symbol") > 0)
args.aprs_symbol = result["aprs-symbol"].as<std::string>();
if (result.count("aprs-symbol-table-id") > 0)
args.aprs_symbol_table = result["aprs-symbol-table-id"].as<std::string>();
return true;
}
void print_usage()
{
std::string usage =
"gps_util - simple gpsd client\n"
"(C) 2023 Ion Todirel\n"
"\n"
"Usage:\n"
" gps_util [OPTION]... \n"
"\n"
"Options:\n"
" -h, --host-name <host> specify the hostname where gpsd runs on\n"
" -p, --port <port> specify the port to connect to gpsd, typically 2947\n"
" -f, --format <format> coordinates print format:\n"
" dd\n"
" ddm\n"
" dms\n"
" ddm_short\n"
" aprx\n"
" aprs\n"
" --aprs-comment <comment> APRS comment\n"
" --aprs-symbol <symbol> APRS symbol\n"
" --aprs-symbol-table-id <id> APRS symbol table\n"
" -o, --output <file> file name to write JSON GPS data to\n"
" --no-gps use fixed input lat,lon as information\n"
" --lat <lat> fixed latitude in DD format\n"
" --lon <lon> fixed longitude in DD format\n"
" --help print usage\n"
" --no-stdout no stdout\n"
"\n"
"Returns:\n"
" 0 - success\n"
" 1 - failure\n"
"\n"
"Example:\n"
" gps_util -h localhost -p 8888 -f dms -o file.json\n"
" gps_util -h localhost -p 8888 -f aprs --aprs-comment \"Downtown Bellevue fill-in Digipeater\" --aprs-symbol \"#\" --aprs-symbol-table-id \"I\"\n"
"\n"
"\n";
printf("%s", usage.c_str());
}
std::string to_lower(const std::string& s)
{
std::string lower_s = s;
std::transform(lower_s.begin(), lower_s.end(), lower_s.begin(), ::tolower);
return lower_s;
}
bool try_parse_bool(const std::string& s, bool& b)
{
std::string lower_s = to_lower(s);
if (lower_s == "true")
{
b = true;
return true;
}
else if (lower_s == "false")
{
b = false;
return true;
}
return false;
}
bool try_parse_double(std::string str, double& number)
{
if (str.empty())
return false;
double maybe_number = -1;
std::istringstream iss(str);
iss >> std::noskipws >> maybe_number;
bool result = !iss.fail() && iss.eof();
if (result)
number = maybe_number;
return result;
}
position_print_format parse_position_format(const std::string& pos_str)
{
if (pos_str == "dd")
return position_print_format::dd;
else if (pos_str == "dms")
return position_print_format::dms;
else if (pos_str == "ddm")
return position_print_format::ddm;
else if (pos_str == "ddm_short" || pos_str == "aprx")
return position_print_format::ddm_short;
else if (pos_str == "aprs" || pos_str == "aprs_with_timestamp")
return position_print_format::aprs_with_timestamp;
else if (pos_str == "aprs_without_timestamp")
return position_print_format::aprs_without_timestamp;
return position_print_format::dd;
}
void print_position(position_print_format print_fmt, const gnss_info& gnss_info)
{
position_dd dd = { gnss_info.lat, gnss_info.lon };
position_display_string pos_display;
if (print_fmt == position_print_format::dd)
{
pos_display = format(dd, position_dd_format);
}
else if (print_fmt == position_print_format::ddm)
{
pos_display = format(position_ddm(dd), position_ddm_format);
}
else if (print_fmt == position_print_format::dms)
{
pos_display = format(position_dms(dd), position_dms_format);
}
else if (print_fmt == position_print_format::ddm_short)
{
pos_display = format(position_ddm(dd), position_ddm_short_format);
}
printf("%s, %s\n", pos_display.lat.c_str(), pos_display.lon.c_str());
}
int write_position(const std::string& filename, const gnss_info& info)
{
std::string json = to_json(info);
std::ofstream output_file(filename);
if (!output_file)
{
return 1;
}
output_file << json;
output_file.close();
return 0;
}
std::string encode_aprs_position_packet_no_timestamp(const std::string& symbol, const std::string& symbol_table, const std::string& comment, double lat, double lon)
{
//
// Data Format:
//
// ! Lat Sym Lon Sym Code Comment
// =
// ------------------------------------------
// 1 8 1 9 1 0-43
//
// Examples:
//
// !4903.50N/07201.75W-Test 001234
// !4903.50N/07201.75W-Test /A=001234
// !49 . N/072 . W-
//
position_dd dd { lat, lon };
position_display_string ddm_short_display = format(position_ddm(dd), position_ddm_short_format);
std::string message;
message.append("!");
message.append(ddm_short_display.lat);
message.append(symbol_table);
message.append(ddm_short_display.lon);
message.append(symbol);
message.append(comment);
return message;
}
std::string encode_aprs_position_packet_no_timestamp(const args& args, double lat, double lon)
{
return encode_aprs_position_packet_no_timestamp(args.aprs_symbol, args.aprs_symbol_table, args.aprs_comment, lat, lon);
}
std::string encode_aprs_position_packet_no_timestamp(const args& args)
{
return encode_aprs_position_packet_no_timestamp(args.aprs_symbol, args.aprs_symbol_table, args.aprs_comment, args.lat, args.lon);
}
std::string encode_aprs_position_packet(const std::string& symbol, const std::string& symbol_table, const std::string& comment, const gnss_info& gnss_info)
{
//
// Data Format:
//
// / Time Lat Sym Lon Sym Code Comment
// @
// -----------------------------------------------
// 1 7 8 1 9 1 0-43
//
// Examples:
//
// /092345z4903.50N/07201.75W>Test1234
// @092345/4903.50N/07201.75W>Test1234
//
position_dd dd { gnss_info.lat, gnss_info.lon };
position_display_string ddm_short_display = format(position_ddm(dd), position_ddm_short_format);
std::string message;
message.append("/");
message.append(format_two_digits_string(gnss_info.time_utc.day));
message.append(format_two_digits_string(gnss_info.time_utc.hour));
message.append(format_two_digits_string(gnss_info.time_utc.minute));
message.append("z");
message.append(ddm_short_display.lat);
message.append(symbol_table);
message.append(ddm_short_display.lon);
message.append(symbol);
message.append(comment);
return message;
}
std::string encode_aprs_position_packet(const args& args, const gnss_info& gnss_info)
{
return encode_aprs_position_packet(args.aprs_symbol, args.aprs_symbol_table, args.aprs_comment, gnss_info);
}
void print_aprs_position_packet(const args& args, const gnss_info& gnss_info)
{
std::string packet = (args.no_gps || args.format == position_print_format::aprs_without_timestamp) ?
encode_aprs_position_packet_no_timestamp(args) :
encode_aprs_position_packet(args, gnss_info);
printf("%s\n", packet.c_str());
}
std::string format_two_digits_string(int number)
{
std::ostringstream oss;
oss << std::setw(2) << std::setfill('0') << number;
return oss.str();
}
bool try_get_gps_info(const args& args, gnss_info& info)
{
gpsd_client s;
bool result = false;
if (!args.no_gps)
{
if (s.open(args.host_name, args.port))
{
do
{
result = s.try_get_gps_info(info, gnss_include_info::all);
}
while (false);
s.close();
}
}
else
{
info.lat = args.lat;
info.lon = args.lon;
result = true;
}
return result;
}
int main(int argc, char* argv[])
{
args args;
try_parse_command_line(argc, argv, args);
if (args.help && !args.no_stdout)
{
print_usage();
return 1;
}
if (args.command_line_has_errors)
{
if (!args.no_stdout)
{
printf("%s\n\n", args.command_line_error.c_str());
print_usage();
}
return 1;
}
gnss_info info;
if (try_get_gps_info(args, info))
{
if (!args.no_stdout)
{
if (args.format == position_print_format::aprs_with_timestamp ||
args.format == position_print_format::aprs_without_timestamp)
{
print_aprs_position_packet(args, info);
}
else
{
print_position(args.format, info);
}
}
if (!args.output_file.empty())
{
return write_position(args.output_file, info);
}
return 0;
}
return 1;
}