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
|
/* $Id$ */
/*
* gpspipe
*
* a simple program to connect to a gpsd daemon and dump the received data
* to stdout
*
* This will dump the raw NMEA from gpsd to stdout
* gpspipe -r
*
* This will dump the super-raw data (gps binary) from gpsd to stdout
* gpspipe -R
*
* This will dump the GPSD sentences from gpsd to stdout
* gpspipe -w
*
* This will dump the GPSD and the NMEA sentences from gpsd to stdout
* gpspipe -wr
*
* Original code by: Gary E. Miller <gem@rellim.com>. Cleanup by ESR.
* All rights given to the gpsd project to release under whatever open source
* license they use. A thank you would be nice if you use this code.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <fcntl.h>
#include <termios.h>
#include "gpsd_config.h"
#include "gpsd.h"
static int fd_out = 1; /* output initially goes to standard output */
static void spinner(unsigned int, unsigned int);
/* NMEA-0183 standard baud rate */
#define BAUDRATE B4800
/* Serial port variables */
static struct termios oldtio, newtio;
static char serbuf[255];
/* open the serial port and set it up */
static void open_serial(char* device)
{
/*
* Open modem device for reading and writing and not as controlling
* tty.
*/
if ((fd_out = open(device, O_RDWR|O_NOCTTY)) < 0) {
fprintf(stderr, "gpspipe: error opening serial port\n");
exit(1);
}
/* Save current serial port settings for later */
if (tcgetattr(fd_out, &oldtio) != 0) {
fprintf(stderr, "gpspipe: error reading serial port settings\n");
exit(1);
}
/* Clear struct for new port settings. */
/*@i@*/bzero(&newtio, sizeof(newtio));
/* make it raw */
(void)cfmakeraw(&newtio);
/* set speed */
/*@i@*/(void)cfsetospeed(&newtio, BAUDRATE);
/* Clear the modem line and activate the settings for the port. */
(void)tcflush(fd_out,TCIFLUSH);
if (tcsetattr(fd_out,TCSANOW,&newtio) != 0) {
(void)fprintf(stderr, "gspipe: error configuring serial port\n");
exit(1);
}
}
static void usage(void)
{
(void)fprintf(stderr, "Usage: gpspipe [OPTIONS] [server[:port[:device]]]\n\n"
"SVN ID: $Id$ \n"
"-h Show this help.\n"
"-r Dump raw NMEA.\n"
"-R Dump super-raw mode (GPS binary).\n"
"-w Dump gpsd native data.\n"
"-j Turn on server-side buffering.\n"
"-t Time stamp the data.\n"
"-s [serial dev] emulate a 4800bps NMEA GPS on serial port (use with '-r').\n"
"-n [count] exit after count packets.\n"
"-v Print a little spinner.\n"
"-V Print version and exit.\n\n"
"You must specify one, or both, of -r/-w.\n"
);
}
int main( int argc, char **argv)
{
int sock = 0;
char buf[4096];
ssize_t wrote = 0;
bool timestamp = false;
bool new_line = true;
long count = -1;
int option;
unsigned int vflag = 0, l = 0;
char *arg = NULL, *colon1, *colon2, *device = NULL;
char *port = DEFAULT_GPSD_PORT, *server = "127.0.0.1";
char *serialport = NULL;
buf[0] = '\0';
while ((option = getopt(argc, argv, "?hrRwjtvVn:s:")) != -1) {
switch (option) {
case 'n':
count = strtol(optarg, 0, 0);
break;
case 'r':
(void)strlcat(buf, "r=1;", sizeof(buf));
break;
case 'R':
(void)strlcat(buf, "r=2;", sizeof(buf));
break;
case 't':
timestamp = true;
break;
case 'v':
vflag++;
break;
case 'w':
(void)strlcat(buf, "w=1;", sizeof(buf));
break;
case 'j':
(void)strlcat(buf, "j=1;", sizeof(buf));
break;
case 'V':
(void)fprintf(stderr, "%s: SVN ID: $Id$ \n", argv[0]);
exit(0);
case 's':
serialport = optarg;
break;
case '?':
case 'h':
default:
usage();
exit(1);
}
}
if (serialport!=NULL && strstr(buf, "r=1")==NULL) {
(void)fprintf(stderr, "gpsipipe: use of '-s' requires '-r'.\n");
exit(1);
}
if (strstr(buf, "r=")==NULL && strstr(buf, "w=1")==NULL) {
(void)fprintf(stderr, "gpspipe: one of '-R', '-r' or '-w' is required.\n");
exit(1);
}
/* Grok the server, port, and device. */
/*@ -branchstate @*/
if (optind < argc) {
arg = strdup(argv[optind]);
/*@i@*/colon1 = strchr(arg, ':');
server = arg;
if (colon1 != NULL) {
if (colon1 == arg) {
server = NULL;
} else {
*colon1 = '\0';
}
port = colon1 + 1;
colon2 = strchr(port, ':');
if (colon2 != NULL) {
if (colon2 == port) {
port = NULL;
} else {
*colon2 = '\0';
}
device = colon2 + 1;
(void)snprintf(buf, sizeof(buf), "%sF=%s\n", buf, device);
}
}
colon1 = colon2 = NULL;
}
/*@ +branchstate @*/
/* Open the serial port and set it up. */
if (serialport)
open_serial(serialport);
/*@ -nullpass @*/
sock = netlib_connectsock( server, port, "tcp");
if (sock < 0) {
(void)fprintf(stderr,
"gpspipe: could not connect to gpsd %s:%s, %s(%d)\n",
server, port, strerror(errno), errno);
exit(1);
}
/*@ +nullpass @*/
/* ship the assembled options */
wrote = write(sock, buf, strlen(buf));
if ((ssize_t)strlen(buf) != wrote) {
(void)fprintf(stderr, "gpspipe: write error, %s(%d)\n",
strerror(errno), errno);
exit(1);
}
if (isatty(STDERR_FILENO) == 0)
vflag = 0;
for(;;) {
int i = 0;
int j = 0;
int readbytes = 0;
if (vflag)
spinner(vflag, l++);
readbytes = (int)read(sock, buf, sizeof(buf));
if (readbytes > 0) {
for (i = 0 ; i < readbytes ; i++) {
char c = buf[i];
if (j < (int)(sizeof(serbuf) - 1)) {
serbuf[j++] = buf[i];
}
if (new_line && timestamp) {
time_t now = time(NULL);
new_line = 0;
if (fprintf(stdout, "%.24s :", ctime(&now)) <= 0) {
(void)fprintf(stderr,
"gpspipe: write error, %s(%d)\n",
strerror(errno), errno);
exit(1);
}
}
if (fputc(c, stdout) == EOF) {
fprintf( stderr, "gpspipe: Write Error, %s(%d)\n",
strerror(errno), errno);
exit(1);
}
if (c == '\n') {
if (serialport != NULL) {
if (write(fd_out, serbuf, (size_t)j) == -1) {
fprintf(stderr,
"gpspipe: Serial port write Error, %s(%d)\n",
strerror(errno), errno);
exit(1);
}
j = 0;
}
new_line = true;
/* flush after every good line */
if (fflush(stdout)) {
(void)fprintf(stderr, "gpspipe: fflush Error, %s(%d)\n",
strerror(errno), errno);
exit(1);
}
if (count > 0) {
if (0 >= --count) {
/* completed count */
exit(0);
}
}
}
}
} else if (readbytes < 0) {
(void) fprintf(stderr, "gpspipe: read error %s(%d)\n",
strerror(errno), errno);
exit(1);
}
}
#ifdef __UNUSED__
if (serialport != NULL) {
/* Restore the old serial port settings. */
if (tcsetattr(fd, TCSANOW, &oldtio) != 0) {
(void)fprintf(stderr, "Error restoring serial port settings\n");
exit(1);
}
}
exit(0);
#endif /* __UNUSED__ */
}
static void spinner (unsigned int v, unsigned int num) {
char *spin = "|/-\\";
(void)fprintf(stderr, "\010%c", spin[(num/(1<<(v-1))) % 4]);
(void)fflush(stderr);
return;
}
|