-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsirfdump.c
435 lines (379 loc) · 8.98 KB
/
sirfdump.c
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
#define _GNU_SOURCE
#include <sys/types.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _MSC_VER
#include <io.h>
#include "ultragetopt.h"
#define STDIN_FILENO 0
#define ssize_t int
#else
#include <getopt.h>
#include <unistd.h>
#endif
#include "sirfdump.h"
#include "sirf_msg.h"
const char *progname = "sirfdump";
const char *revision = "$Revision: 0.4 $";
struct opts_t {
char *infile;
char *outfile;
enum {
OUTPUT_DUMP,
OUTPUT_NMEA,
OUTPUT_RINEX,
OUTPUT_RINEX_NAV,
OUTPUT_RTCM,
} output_type;
unsigned gsw230_byte_order;
};
struct input_stream_t {
int fd;
uint8_t buf[1024];
unsigned head, tail;
int last_errno;
};
struct ctx_t {
struct opts_t opts;
struct input_stream_t in;
FILE *outfh;
dumpf_t *dump_f;
void *user_ctx;
};
unsigned output_dump_use_gsw230_byte_order = 0;
static void usage(void)
{
fprintf(stdout, "\nUsage:\n %s [-h] [options]\n"
,progname);
return;
}
static void version(void)
{
fprintf(stdout,"%s %s\n",progname,revision);
}
static void help(void)
{
printf("%s - Sirf binary logger\t\t%s\n",
progname, revision);
usage();
printf(
"\nOptions:\n"
" -f, --infile Input file, default: - (stdin)\n"
" -F, --outfile Output file, default: - (stdout)\n"
" -o, --outtype Output type: dump / nmea / rinex / rinex-nav / rtcm. default: nmea\n"
" -2, --gsw230 Use alternate byte order that is used on GSW 2.3.0 - 2.9.9 firmwares\n"
" -h, --help Help\n"
" -v, --version Show version\n"
"\n"
);
return;
}
static struct ctx_t *init_ctx()
{
struct ctx_t *ctx;
ctx = malloc(sizeof(*ctx));
if (ctx == NULL) {
perror(NULL);
return NULL;
}
ctx->opts.infile = ctx->opts.outfile = NULL;
ctx->opts.output_type = OUTPUT_NMEA;
ctx->opts.gsw230_byte_order = 0;
ctx->in.fd = -1;
ctx->in.head = ctx->in.tail = 0;
ctx->in.last_errno = 0;
ctx->outfh = NULL;
ctx->user_ctx = NULL;
return ctx;
}
static void free_ctx(struct ctx_t *ctx)
{
if (ctx == NULL)
return;
free(ctx->opts.infile);
free(ctx->opts.outfile);
if (ctx->in.fd > 0 && (ctx->in.fd != STDIN_FILENO))
close(ctx->in.fd);
if (ctx->outfh && (ctx->outfh != stdout))
fclose(ctx->outfh);
free(ctx);
}
static int set_file(char **dst, const char *optarg)
{
assert(dst);
assert(optarg);
free(*dst);
if (optarg[0] == '-' && (optarg[1] == '\0')) {
*dst = NULL;
}else {
*dst = strdup(optarg);
if (*dst == NULL) {
perror(NULL);
return 1;
}
}
return 0;
}
static int read_data(struct input_stream_t *stream)
{
ssize_t l;
assert(
!(stream->tail == sizeof(stream->buf)
&& (stream->head == sizeof(stream->buf)))
);
if (stream->tail == sizeof(stream->buf)) {
memmove(stream->buf, &stream->buf[stream->head],
stream->tail - stream->head);
stream->tail = stream->tail - stream->head;
stream->head=0;
}
l = read(stream->fd, &stream->buf[stream->tail], sizeof(stream->buf) - stream->tail);
if (l<0)
stream->last_errno = errno;
else
stream->tail = stream->tail+l;
return l;
}
void *readpkt(struct input_stream_t *stream, struct transport_msg_t *res_msg)
{
unsigned start_seq_found;
unsigned payload_length;
unsigned checksum;
unsigned garbage_bytes;
unsigned p0;
size_t l;
uint8_t *res;
garbage_bytes = 0;
start_seq_found=0;
payload_length=0;
for (;;) {
while (stream->tail - stream->head < 8) {
l = read_data(stream);
if (l <= 0)
return NULL;
}
/* search for start sequence */
while (stream->head < stream->tail-1) {
if ((stream->buf[stream->head] == 0xa0)
&& (stream->buf[stream->head+1] == 0xa2)) {
start_seq_found=1;
break;
}else {
garbage_bytes++;
stream->head++;
}
}
if (!start_seq_found)
continue;
while (stream->tail - stream->head < 6) {
l = read_data(stream);
if (l <= 0)
return NULL;
}
/* get payload length */
payload_length = (0xff00 & (stream->buf[stream->head+2] << 8))
| (0xff & stream->buf[stream->head+3]);
if (payload_length >= 1023) {
stream->head++;
continue;
}
/* load payload data */
p0 = 2+2+payload_length;
while (stream->tail - stream->head < p0+2+2) {
l = read_data(stream);
if (l <= 0)
return NULL;
}
/* checksum */
checksum = (0xff00 & (stream->buf[stream->head+p0] << 8))
| (0xff & stream->buf[stream->head+p0+1]);
/* end sequence */
if (stream->buf[stream->head+p0+2] != 0xb0
|| (stream->buf[stream->head+p0+3] != 0xb3)) {
stream->head++;
continue;
}
break;
} /* for(;;) */
res = &stream->buf[stream->head];
stream->head = stream->head + 2+2+payload_length+2+2;
if (stream->head == stream->tail) {
stream->head = stream->tail = 0;
}
if (res_msg) {
res_msg->payload = &res[4];
res_msg->payload_length = payload_length;
res_msg->checksum = checksum;
res_msg->skipped_bytes = garbage_bytes;
}
return res;
}
int process(struct ctx_t *ctx)
{
uint8_t *pkt;
struct transport_msg_t msg;
while ( (pkt = readpkt(&ctx->in, &msg)) != NULL ) {
ctx->dump_f(&msg, ctx->outfh, ctx->user_ctx);
}
return ctx->in.last_errno;
}
int main(int argc, char *argv[])
{
signed char c;
struct ctx_t *ctx;
static struct option longopts[] = {
{"version", no_argument, 0, 'v'},
{"help", no_argument, 0, 'h'},
{"infile", required_argument, 0, 'f'},
{"outfile", required_argument, 0, 'F'},
{"outtype", required_argument, 0, 'o'},
{"gsw230", no_argument, 0, '2'},
{0, 0, 0, 0}
};
ctx = init_ctx();
if (ctx == NULL)
return 1;
#ifdef WIN32
#ifdef _MSC_VER
_set_output_format(_TWO_DIGIT_EXPONENT);
#else
putenv("PRINTF_EXPONENT_DIGITS=2");
#endif
#endif
while ((c = getopt_long(argc, argv, "vh?f:F:o:2",longopts,NULL)) != -1) {
switch (c) {
case 'f':
if (set_file(&ctx->opts.infile, optarg) != 0) {
free_ctx(ctx);
return 1;
}
break;
case 'F':
if (set_file(&ctx->opts.outfile, optarg) != 0) {
free_ctx(ctx);
return 1;
}
break;
case 'o':
if (strcmp(optarg, "nmea") == 0) {
ctx->opts.output_type = OUTPUT_NMEA;
}else if (strcmp(optarg, "dump") == 0) {
ctx->opts.output_type = OUTPUT_DUMP;
}else if (strcmp(optarg, "rinex") == 0) {
ctx->opts.output_type = OUTPUT_RINEX;
}else if (strcmp(optarg, "rinex-nav") == 0) {
ctx->opts.output_type = OUTPUT_RINEX_NAV;
}else if (strcmp(optarg, "rtcm") == 0) {
ctx->opts.output_type = OUTPUT_RTCM;
}else {
fputs("Wrong output type\n", stderr);
return 1;
}
break;
case '2':
ctx->opts.gsw230_byte_order = 1;
break;
case 'v':
version();
free_ctx(ctx);
exit(0);
break;
default:
help();
free_ctx(ctx);
exit(0);
break;
}
}
argc -= optind;
argv += optind;
/* infile */
if (ctx->opts.infile != NULL) {
ctx->in.fd = open(ctx->opts.infile, O_RDONLY
#ifdef O_BINARY
| O_BINARY
#endif
);
if (ctx->in.fd < 0) {
perror(NULL);
free_ctx(ctx);
return 1;
}
}else
ctx->in.fd = STDIN_FILENO;
/* outfile */
if (ctx->opts.outfile != NULL) {
ctx->outfh = fopen(ctx->opts.outfile,
#ifdef WIN32
"wb"
#else
"w"
#endif
);
if (ctx->outfh == NULL) {
perror(NULL);
free_ctx(ctx);
return 1;
}
}else
ctx->outfh = stdout;
/* output_type */
switch (ctx->opts.output_type) {
case OUTPUT_NMEA:
ctx->dump_f = &output_nmea;
break;
case OUTPUT_RINEX:
ctx->dump_f = &output_rinex;
ctx->user_ctx = new_rinex_ctx(argc, argv, ctx->opts.gsw230_byte_order);
if (ctx->user_ctx == NULL) {
perror(NULL);
free_ctx(ctx);
return 1;
}
break;
case OUTPUT_RINEX_NAV:
ctx->dump_f = &output_rinex_nav;
ctx->user_ctx = new_rinex_nav_ctx(argc, argv);
if (ctx->user_ctx == NULL) {
perror(NULL);
free_ctx(ctx);
return 1;
}
break;
case OUTPUT_RTCM:
ctx->dump_f = &output_rtcm;
ctx->user_ctx = new_rtcm_ctx(argc, argv, ctx->opts.gsw230_byte_order);
setvbuf(ctx->outfh, NULL, _IONBF, 0);
if (ctx->user_ctx == NULL) {
perror(NULL);
free_ctx(ctx);
return 1;
}
break;
case OUTPUT_DUMP:
default:
output_dump_use_gsw230_byte_order = ctx->opts.gsw230_byte_order;
ctx->dump_f = &output_dump;
break;
}
process(ctx);
switch (ctx->opts.output_type) {
case OUTPUT_RINEX:
free_rinex_ctx(ctx->user_ctx);
break;
case OUTPUT_RINEX_NAV:
free_rinex_nav_ctx(ctx->user_ctx);
break;
case OUTPUT_RTCM:
free_rtcm_ctx(ctx->user_ctx);
break;
default:
break;
}
free_ctx(ctx);
return 0;
}