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
|
/* $Id$ */
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdbool.h>
#include <ctype.h>
#include "gpsd.h"
static int verbose = ISGPS_ERRLEVEL_BASE;
void gpsd_report(int errlevel, const char *fmt, ... )
/* assemble command in printf(3) style, use stderr or syslog */
{
if (errlevel <= verbose) {
char buf[BUFSIZ];
va_list ap;
(void)strlcpy(buf, "rtcmdecode: ", BUFSIZ);
va_start(ap, fmt) ;
(void)vsnprintf(buf + strlen(buf), sizeof(buf)-strlen(buf), fmt, ap);
va_end(ap);
(void)fputs(buf, stdout);
}
}
/*@ -compdestroy @*/
static void decode(FILE *fpin, FILE *fpout)
/* RTCM-104 bits on fpin to dump format on fpout */
{
int c;
struct gps_device_t device;
enum isgpsstat_t res;
off_t count;
char buf[BUFSIZ];
isgps_init(&device);
count = 0;
while ((c = fgetc(fpin)) != EOF) {
res = rtcm_decode(&device, (unsigned int)c);
if (verbose >= ISGPS_ERRLEVEL_BASE + 3)
fprintf(fpout, "%08lu: '%c' [%02x] -> %d\n",
(unsigned long)count++, (isprint(c)?c:'.'), (unsigned)(c & 0xff), res);
if (res == ISGPS_MESSAGE) {
rtcm_dump(&device, buf, sizeof(buf));
(void)fputs(buf, fpout);
}
}
}
/*@ +compdestroy @*/
/*@ -compdestroy @*/
static void pass(FILE *fpin, FILE *fpout)
/* dump format on stdin to dump format on stdout (self-inversion test) */
{
char buf[BUFSIZ];
struct gps_device_t session;
memset(&session, 0, sizeof(session));
while (fgets(buf, (int)sizeof(buf), fpin) != NULL) {
int status;
/* pass through comment lines without interpreting */
if (buf[0] == '#') {
(void)fputs(buf, fpout);
continue;
}
status = rtcm_undump(&session.gpsdata.rtcm, buf);
if (status == 0) {
(void)rtcm_repack(&session);
(void)rtcm_unpack(&session);
(void)rtcm_dump(&session, buf, sizeof(buf));
(void)fputs(buf, fpout);
memset(&session, 0, sizeof(session));
} else if (status < 0) {
(void) fprintf(stderr, "rtcmdecode: bailing out with status %d\n", status);
exit(1);
}
}
}
/*@ +compdestroy @*/
/*@ -compdestroy @*/
static void encode(FILE *fpin, FILE *fpout)
/* dump format on fpin to RTCM-104 on fpout */
{
char buf[BUFSIZ];
struct gps_device_t session;
memset(&session, 0, sizeof(session));
while (fgets(buf, (int)sizeof(buf), fpin) != NULL) {
int status;
status = rtcm_undump(&session.gpsdata.rtcm, buf);
if (status == 0) {
(void)rtcm_repack(&session);
(void)fwrite(session.driver.isgps.buf,
sizeof(isgps30bits_t),
(size_t)session.gpsdata.rtcm.length, fpout);
memset(&session, 0, sizeof(session));
} else if (status < 0) {
(void) fprintf(stderr, "rtcmdecode: bailing out with status %d\n", status);
exit(1);
}
}
}
/*@ +compdestroy @*/
int main(int argc, char **argv)
{
char buf[BUFSIZ];
int c;
bool striphdr = false;
enum {doencode, dodecode, passthrough} mode = dodecode;
while ((c = getopt(argc, argv, "dehpVv:")) != EOF) {
switch (c) {
case 'd': /* not documented, used for debugging */
mode = dodecode;
break;
case 'e': /* not documented, used for debugging */
mode = doencode;
break;
case 'h': /* not documented, used for debugging */
striphdr = true;
break;
case 'p': /* not documented, used for debugging */
mode = passthrough;
break;
case 'v': /* verbose */
verbose = ISGPS_ERRLEVEL_BASE + atoi(optarg);
break;
case 'V':
(void)fprintf(stderr, "SVN ID: $Id$ \n");
exit(0);
case '?':
default:
(void)fputs("rtcmdecode [-v]\n", stderr);
exit(1);
}
}
argc -= optind;
argv += optind;
/* strip lines with leading # */
if (striphdr) {
while ((c = getchar()) == '#')
(void)fgets(buf, (int)sizeof(buf), stdin);
(void)ungetc(c, stdin);
}
if (mode == passthrough)
pass(stdin, stdout);
else if (mode == doencode)
encode(stdin, stdout);
else
decode(stdin, stdout);
exit(0);
}
/* rtcmdecode.c ends here */
|