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
|
/*
* This file is Copyright (c) 2010 by the GPSD project
* BSD terms apply: see the file COPYING in the distribution root for details.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <syslog.h>
#include <math.h>
#include <time.h>
#include <errno.h>
#include <libgen.h>
#include <signal.h>
#include <assert.h>
#ifndef S_SPLINT_S
#include <unistd.h>
#endif /* S_SPLINT_S */
#include "gps.h"
#include "gpsd_config.h"
#include "gpsdclient.h"
#include "revision.h"
static char *progname;
static struct fixsource_t source;
/**************************************************************************
*
* Transport-layer-independent functions
*
**************************************************************************/
static struct gps_data_t gpsdata;
static FILE *logfile;
static bool intrack = false;
static time_t timeout = 5; /* seconds */
static double minmove = 0; /* meters */
#ifdef CLIENTDEBUG_ENABLE
static int debug;
#endif /* CLIENTDEBUG_ENABLE */
static void print_gpx_header(void)
{
char tbuf[CLIENT_DATE_MAX+1];
(void)fprintf(logfile,"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
(void)fprintf(logfile,"<gpx version=\"1.1\" creator=\"GPSD %s - %s\"\n", VERSION, GPSD_URL);
(void)fprintf(logfile," xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n");
(void)fprintf(logfile," xmlns=\"http://www.topografix.com/GPX/1/1\"\n");
(void)fprintf(logfile," xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1\n");
(void)fprintf(logfile," http://www.topografix.com/GPX/1/1/gpx.xsd\">\n");
(void)fprintf(logfile," <metadata>\n");
(void)fprintf(logfile," <time>%s</time>\n", unix_to_iso8601((timestamp_t)time(NULL), tbuf, sizeof(tbuf)));
(void)fprintf(logfile," </metadata>\n");
(void)fflush(logfile);
}
static void print_gpx_trk_end(void)
{
(void)fprintf(logfile," </trkseg>\n");
(void)fprintf(logfile," </trk>\n");
(void)fflush(logfile);
}
static void print_gpx_footer(void)
{
if (intrack)
print_gpx_trk_end();
(void)fprintf(logfile,"</gpx>\n");
(void)fclose(logfile);
}
static void print_gpx_trk_start(void)
{
(void)fprintf(logfile," <trk>\n");
(void)fprintf(logfile," <src>GPSD %s</src>\n", VERSION);
(void)fprintf(logfile," <trkseg>\n");
(void)fflush(logfile);
}
static void print_fix(struct gps_data_t *gpsdata, double time)
{
char tbuf[CLIENT_DATE_MAX+1];
(void)fprintf(logfile," <trkpt lat=\"%f\" lon=\"%f\">\n",
gpsdata->fix.latitude, gpsdata->fix.longitude);
if ((isnan(gpsdata->fix.altitude) == 0))
(void)fprintf(logfile," <ele>%f</ele>\n", gpsdata->fix.altitude);
(void)fprintf(logfile," <time>%s</time>\n",
unix_to_iso8601(time, tbuf, sizeof(tbuf)));
(void)fprintf(logfile," <src>GPSD tag=\"%s\"</src>\n", gpsdata->tag);
if (gpsdata->status == STATUS_DGPS_FIX)
(void)fprintf(logfile," <fix>dgps</fix>\n");
else
switch (gpsdata->fix.mode) {
case MODE_3D:
(void)fprintf(logfile," <fix>3d</fix>\n");
break;
case MODE_2D:
(void)fprintf(logfile," <fix>2d</fix>\n");
break;
case MODE_NO_FIX:
(void)fprintf(logfile," <fix>none</fix>\n");
break;
default:
/* don't print anything if no fix indicator */
break;
}
if ((gpsdata->fix.mode > MODE_NO_FIX) && (gpsdata->satellites_used > 0))
(void)fprintf(logfile," <sat>%d</sat>\n", gpsdata->satellites_used);
if (isnan(gpsdata->dop.hdop) == 0)
(void)fprintf(logfile," <hdop>%.1f</hdop>\n", gpsdata->dop.hdop);
if (isnan(gpsdata->dop.vdop) == 0)
(void)fprintf(logfile," <vdop>%.1f</vdop>\n", gpsdata->dop.vdop);
if (isnan(gpsdata->dop.pdop) == 0)
(void)fprintf(logfile," <pdop>%.1f</pdop>\n", gpsdata->dop.pdop);
(void)fprintf(logfile," </trkpt>\n");
(void)fflush(logfile);
}
static void conditionally_log_fix(struct gps_data_t *gpsdata)
{
static double int_time, old_int_time;
static double old_lat, old_lon;
static bool first = true;
int_time = gpsdata->fix.time;
if ((int_time == old_int_time) || gpsdata->fix.mode < MODE_2D)
return;
/* may not be worth logging if we've moved only a very short distance */
if (minmove>0 && !first && earth_distance(
gpsdata->fix.latitude,
gpsdata->fix.longitude,
old_lat, old_lon) < minmove)
return;
/*
* Make new track if the jump in time is above
* timeout. Handle jumps both forward and
* backwards in time. The clock sometimes jumps
* backward when gpsd is submitting junk on the
* dbus.
*/
/*@-type@*/
if (fabs(int_time - old_int_time) > timeout && !first) {
print_gpx_trk_end();
intrack = false;
}
/*@+type@*/
if (!intrack) {
print_gpx_trk_start();
intrack = true;
if (first)
first = false;
}
old_int_time = int_time;
if (minmove > 0) {
old_lat = gpsdata->fix.latitude;
old_lon = gpsdata->fix.longitude;
}
print_fix(gpsdata, int_time);
}
static void quit_handler(int signum)
{
/* don't clutter the logs on Ctrl-C */
if (signum != SIGINT)
syslog(LOG_INFO, "exiting, signal %d received", signum);
print_gpx_footer();
(void)gps_close(&gpsdata);
exit(EXIT_SUCCESS);
}
/**************************************************************************
*
* Main sequence
*
**************************************************************************/
static void usage(void)
{
/*@-nullderef@*/
fprintf(stderr,
"Usage: %s [-V] [-h] [-d] [-i timeout] [-f filename] [-m minmove]\n"
"\t[-e exportmethod] [server[:port:[device]]]\n\n"
"defaults to '%s -i 5 -e %s localhost:2947'\n",
progname, progname, export_default()->name);
/*@-nullderef@*/
exit(EXIT_FAILURE);
}
/*@-mustfreefresh -mustfreeonly -branchstate -globstate@*/
int main(int argc, char **argv)
{
int ch;
bool daemonize = false;
unsigned int flags = WATCH_ENABLE;
struct exportmethod_t *method = NULL;
progname = argv[0];
if (export_default() == NULL) {
(void)fprintf(stderr, "%s: no export methods.\n", progname);
exit(EXIT_FAILURE);
}
logfile = stdout;
while ((ch = getopt(argc, argv, "dD:e:f:hi:lm:V")) != -1) {
switch (ch) {
case 'd':
openlog(basename(progname), LOG_PID | LOG_PERROR, LOG_DAEMON);
daemonize = true;
break;
#ifdef CLIENTDEBUG_ENABLE
case 'D':
debug = atoi(optarg);
gps_enable_debug(debug, logfile);
break;
#endif /* CLIENTDEBUG_ENABLE */
case 'e':
method = export_lookup(optarg);
if (method == NULL) {
(void)fprintf(stderr,
"%s: %s is not a known export method.\n",
progname, optarg);
exit(EXIT_FAILURE);
}
break;
case 'f': /* Output file name. */
{
/*@-usedef@*/
char *fname = NULL;
time_t t;
size_t s = 0;
size_t fnamesize = strlen(optarg);
t = time(NULL);
while (s == 0) {
char *newfname = realloc(fname, fnamesize);
if (newfname == NULL) {
syslog(LOG_ERR, "realloc failed.");
goto bailout;
} else {
fnamesize += 1024;
fname = newfname;
}
s = strftime(fname, fnamesize-1, optarg, localtime(&t));
}
assert(fname != NULL); /* pacify splint */
fname[s] = '\0';;
logfile = fopen(fname, "w");
if (logfile == NULL)
syslog(LOG_ERR,
"Failed to open %s: %s, logging to stdout.",
fname, strerror(errno));
bailout:
free(fname);
break;
/*@+usedef@*/
}
case 'i': /* set polling interfal */
timeout = (time_t) atoi(optarg);
if (timeout < 1)
timeout = 1;
if (timeout >= 3600)
fprintf(stderr,
"WARNING: track timeout is an hour or more!\n");
break;
case 'l':
export_list(stderr);
exit(EXIT_SUCCESS);
case 'm':
minmove = (double )atoi(optarg);
break;
case 'V':
(void)fprintf(stderr, "%s revision " REVISION "\n", progname);
exit(EXIT_SUCCESS);
default:
usage();
/* NOTREACHED */
}
}
if (daemonize && logfile == stdout) {
syslog(LOG_ERR, "Daemon mode with no valid logfile name - exiting.");
exit(EXIT_FAILURE);
}
if (method != NULL)
if (method->magic != NULL) {
source.server = (char *)method->magic;
source.port = NULL;
}
if (optind < argc) {
gpsd_source_spec(argv[optind], &source);
} else
gpsd_source_spec(NULL, &source);
#if 0
(void)fprintf(logfile,"<!-- server: %s port: %s device: %s -->\n",
source.server, source.port, source.device);
#endif
/* catch all interesting signals */
(void)signal(SIGTERM, quit_handler);
(void)signal(SIGQUIT, quit_handler);
(void)signal(SIGINT, quit_handler);
/*@-unrecog@*/
/* might be time to daemonize */
if (daemonize) {
/* not SuS/POSIX portable, but we have our own fallback version */
if (daemon(0, 0) != 0)
(void) fprintf(stderr,"demonization failed: %s\n", strerror(errno));
}
/*@+unrecog@*/
//syslog (LOG_INFO, "---------- STARTED ----------");
if (gps_open(source.server, source.port, &gpsdata) != 0) {
(void)fprintf(stderr,
"%s: no gpsd running or network error: %d, %s\n",
progname, errno, gps_errstr(errno));
exit(EXIT_FAILURE);
}
if (source.device != NULL)
flags |= WATCH_DEVICE;
(void)gps_stream(&gpsdata, flags, source.device);
print_gpx_header();
(void)gps_mainloop(&gpsdata, 5000000, conditionally_log_fix);
print_gpx_footer();
(void)gps_close(&gpsdata);
exit(EXIT_SUCCESS);
}
/*@+mustfreefresh +mustfreeonly +branchstate +globstate@*/
|