summaryrefslogtreecommitdiff
path: root/gpxlogger.c
blob: 3583cc75840c756ea458c76f0ef5b00da00fd80e (plain)
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
/* $Id$ */
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <syslog.h>
#include <math.h>
#include <time.h>
#include <signal.h>
#include <getopt.h>
#include <errno.h>
#ifndef S_SPLINT_S
#include <unistd.h>
#endif /* S_SPLINT_S */

#include "gpsd_config.h"
#include "gps.h"
#include "gpsdclient.h"

/**************************************************************************
 *
 * Transport-layer-independent functions
 *
 **************************************************************************/

static char *author = "Amaury Jacquot, Chris Kuethe";
static char *license = "BSD";
static char *progname;

static time_t int_time, old_int_time;
static bool intrack = false;
static bool first = true;
static time_t timeout = 5; /* seconds */

static void print_gpx_header(void) 
{
    (void)printf("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
    (void)printf("<gpx version=\"1.1\" creator=\"navsys logger\"\n");
    (void)printf("        xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n");
    (void)printf("        xmlns=\"http://www.topografix.com/GPX/1.1\"\n");
    (void)printf("        xsi:schemaLocation=\"http://www.topografix.com/GPS/1/1\n");
    (void)printf("        http://www.topografix.com/GPX/1/1/gpx.xsd\">\n");
    (void)printf(" <metadata>\n");
    (void)printf("  <name>NavSys GPS logger dump</name>\n");
    (void)printf("  <author>%s</author>\n", author);
    (void)printf("  <copyright>%s</copyright>\n", license);
    (void)printf(" </metadata>\n");
    (void)fflush(stdout);
}

static void print_gpx_trk_end (void) 
{
    (void)printf("  </trkseg>\n");
    (void)printf(" </trk>\n");
    (void)fflush(stdout);
}

static void print_gpx_footer (void) 
{
    if (intrack)
	print_gpx_trk_end();
    (void)printf("</gpx>\n");
    (void)fclose(stdout);
}

static void print_gpx_trk_start (void)
{
    (void)printf(" <trk>\n");
    (void)printf("  <trkseg>\n");
    (void)fflush(stdout);
}

static void print_fix(struct gps_fix_t *fix, struct tm *time)
{
    (void)printf("   <trkpt lat=\"%f\" lon=\"%f\">\n", 
		 fix->latitude, fix->longitude);
    (void)printf("    <ele>%f</ele>\n",
		 fix->altitude);
    (void)printf("    <time>%04d-%02d-%02dT%02d:%02d:%02dZ</time>\n",
		 time->tm_year+1900, time->tm_mon+1, time->tm_mday,
		 time->tm_hour, time->tm_min, time->tm_sec);
    if (fix->mode==MODE_NO_FIX)
	(void)fprintf (stdout, "    <fix>none</fix>\n");
    else
	(void)fprintf (stdout, "    <fix>%dd</fix>\n", fix->mode);
#if 0
    /*
     * Can't print this more detailed report because in D-Bus mode
     * we don't necessarily have access to some of the stuff in gsdata.
     * Might mean some of this stuff should be promoted.
     */
    if ((gpsdata->status >= 2) && (gpsdata->fix.mode >= MODE_3D)){
	/* dgps or pps */
	if (gpsdata->fix.mode == 4) { /* military pps */
	    (void)printf("        <fix>pps</fix>\n");
	} else { /* civilian dgps or sbas */
	    (void)printf("        <fix>dgps</fix>\n");
	}
    } else { /* no dgps or pps */
	if (gpsdata->fix.mode == MODE_3D) {
	    (void)printf("        <fix>3d</fix>\n");
	} else if (gpsdata->fix.mode == MODE_2D) {
	    (void)printf("        <fix>2d</fix>\n");
	} else if (gpsdata->fix.mode == MODE_NOFIX) {
	    (void)printf("        <fix>none</fix>\n");
	} /* don't print anything if no fix indicator */
    }

    /* print # satellites used in fix, if reasonable to do so */
    if (gpsdata->fix.mode >= MODE_2D) {
	(void)printf("        <hdop>%.1f</hdop>\n", gpsdata->hdop);
	(void)printf("        <sat>%d</sat>\n", gpsdata->satellites_used);
    }
#endif

    (void)printf("   </trkpt>\n");
    (void)fflush (stdout);
}

static void conditionally_log_fix(struct gps_fix_t *gpsfix)
{
    int_time = floor(gpsfix->time);
    if ((int_time != old_int_time) && gpsfix->mode >= MODE_2D) {
	struct tm 	time;
	/* 
	 * 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.
	 */
	if (fabs(int_time - old_int_time) > timeout && !first) {
	    print_gpx_trk_end();
	    intrack = false;
	}

	if (!intrack) {
	    print_gpx_trk_start();
	    intrack = true;
	    if (first)
		first = false;
	}
		
	old_int_time = int_time;
	gmtime_r(&(int_time), &time);
	print_fix(gpsfix, &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 ();
    exit(0);
}

static struct gps_fix_t gpsfix;

#ifdef DBUS_ENABLE
/**************************************************************************
 *
 * Doing it with D-Bus
 *
 **************************************************************************/

#include <glib.h>
#include <dbus/dbus.h>
#include <dbus/dbus-glib-lowlevel.h>
#include <dbus/dbus-glib.h>

#include <glib/gprintf.h>

DBusConnection* connection;

static char devname[BUFSIZ];

static DBusHandlerResult handle_gps_fix (DBusMessage* message) 
{
    DBusError	error;

    dbus_error_init (&error);

    dbus_message_get_args (message,
			   &error,
			   DBUS_TYPE_DOUBLE, &gpsfix.time,
			   DBUS_TYPE_INT32,  &gpsfix.mode,
			   DBUS_TYPE_DOUBLE, &gpsfix.ept,
			   DBUS_TYPE_DOUBLE, &gpsfix.latitude,
			   DBUS_TYPE_DOUBLE, &gpsfix.longitude,
			   DBUS_TYPE_DOUBLE, &gpsfix.eph,
			   DBUS_TYPE_DOUBLE, &gpsfix.altitude,
			   DBUS_TYPE_DOUBLE, &gpsfix.epv,
			   DBUS_TYPE_DOUBLE, &gpsfix.track,
			   DBUS_TYPE_DOUBLE, &gpsfix.epd,
			   DBUS_TYPE_DOUBLE, &gpsfix.speed,
			   DBUS_TYPE_DOUBLE, &gpsfix.eps,
			   DBUS_TYPE_DOUBLE, &gpsfix.climb,
			   DBUS_TYPE_DOUBLE, &gpsfix.epc,
			   DBUS_TYPE_STRING, &devname,
			   DBUS_TYPE_INVALID);
	
    conditionally_log_fix(&gpsfix);
    return DBUS_HANDLER_RESULT_HANDLED;
}

/*
 * Message dispatching function
 *
 */
static DBusHandlerResult signal_handler (
		DBusConnection* connection, DBusMessage* message)
{
    /* dummy, need to use the variable for some reason */
    connection = NULL;
	
    if (dbus_message_is_signal (message, "org.gpsd", "fix")) 
	return handle_gps_fix (message);
    /*
     * ignore all other messages
     */
	
    return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}

static int dbus_mainloop(void)
{
    GMainLoop* mainloop;
    DBusError error;

    mainloop = g_main_loop_new (NULL, FALSE);

    dbus_error_init (&error);
    connection = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
    if (dbus_error_is_set (&error)) {
	syslog (LOG_CRIT, "%s: %s", error.name, error.message);
	return 3;
    }
	
    dbus_bus_add_match (connection, "type='signal'", &error);
    if (dbus_error_is_set (&error)) {
	syslog (LOG_CRIT, "unable to add match for signals %s: %s", error.name, error.message);
	return 4;
    }

    if (!dbus_connection_add_filter (connection, (DBusHandleMessageFunction)signal_handler, NULL, NULL)) {
	syslog (LOG_CRIT, "unable to register filter with the connection");
	return 5;
    }
	
    dbus_connection_setup_with_g_main (connection, NULL);

    g_main_loop_run (mainloop);
    return 0;
}

#endif /* DBUS_ENABLE */

/**************************************************************************
 *
 * Doing it with sockets
 *
 **************************************************************************/

struct fixsource_t source;
static int casoc = 0;

static void process(struct gps_data_t *gpsdata,
	     char *buf UNUSED, size_t len UNUSED, int level UNUSED)
{
    conditionally_log_fix(&gpsdata->fix);
}

static int socket_mainloop(void)
{
    fd_set fds;
    struct gps_data_t *gpsdata;

    gpsdata = gps_open(source.server, source.port);
    if (!gpsdata) {
	char *err_str;
	switch (errno) {
	case NL_NOSERVICE:
	    err_str = "can't get service entry";
	    break;
	case NL_NOHOST:
	    err_str = "can't get host entry";
	    break;
	case NL_NOPROTO:
	    err_str = "can't get protocol entry";
	    break;
	case NL_NOSOCK:
	    err_str = "can't create socket";
	    break;
	case NL_NOSOCKOPT:
	    err_str = "error SETSOCKOPT SO_REUSEADDR";
	    break;
	case NL_NOCONNECT:
	    err_str = "can't connect to host";
	    break;
	default:
	    err_str = "Unknown";
	    break;
	}
	fprintf(stderr,
		"%s: no gpsd running or network error: %d, %s\n",
		progname, errno, err_str);
	exit(1);
    }

    if (casoc)
	gps_query(gpsdata, "j1\n");
    if (source.device != NULL)
	gps_query(gpsdata, "f=%s\n", source.device);

    gps_set_raw_hook(gpsdata, process);

    gps_query(gpsdata, "w+x");

    for(;;){
	int data;
	struct timeval tv;

	FD_ZERO(&fds);
	FD_SET(gpsdata->gps_fd, &fds);

	tv.tv_usec = 250000;
	tv.tv_sec = 0;
	data = select(gpsdata->gps_fd + 1, &fds, NULL, NULL, &tv);

	if (data < 0) {
	    (void)fprintf(stderr,"%s\n", strerror(errno));
	    break;
	}
	else if (data)
	    gps_poll(gpsdata);
    }
    return 0;
}

/**************************************************************************
 *
 * Main sequence
 *
 **************************************************************************/

static void usage(void) 
{
    fprintf(stderr,
	    "Usage: %s [-V] [-h] [-i timeout] [-j casoc] [server[:port:[device]]]\n",
	    progname);
    fprintf(stderr,
	    "\tdefaults to '%s -i 5 -j 0 127.0.0.1:2947'\n",
	    progname);
    exit(1);
}

int main (int argc, char** argv) 
{
    int ch;

    progname = argv[0];
    while ((ch = getopt(argc, argv, "hi:j:V")) != -1){
	switch (ch) {
	case 'i':		/* set polling interfal */
	    timeout = (unsigned int)atoi(optarg);
	    if (timeout < 1)
		timeout = 1;
	    if (timeout >= 3600)
		fprintf(stderr,
			"WARNING: track timeout is an hour or more!\n");
	    break;
	case 'j':		/* set data smoothing */
	    casoc = (unsigned int)atoi(optarg);
	    casoc = casoc ? 1 : 0;
	    break;
	case 'V':
	    (void)fprintf(stderr, "SVN ID: $Id$ \n");
	    exit(0);
	default:
 	    usage();
	    /* NOTREACHED */
	}
    }

    if (optind < argc) {
	gpsd_source_spec(argv[optind], &source);
    } else
	gpsd_source_spec(NULL, &source);
#if 0
    (void)printf("<!-- server: %s port: %s  device: %s -->\n",
		 source.server, source.port, source.device);
#endif

    /* initializes the gpsfix data structure */
    gps_clear_fix(&gpsfix);

    /* catch all interesting signals */
    signal (SIGTERM, quit_handler);
    signal (SIGQUIT, quit_handler);
    signal (SIGINT, quit_handler);

    //openlog ("gpxlogger", LOG_PID | LOG_NDELAY , LOG_DAEMON);
    //syslog (LOG_INFO, "---------- STARTED ----------");
	
    print_gpx_header ();

#ifdef DBUS_ENABLE
    /* To force socket use in the default way just give a '127.0.0.1' arg */  
    if (optind < argc)
	return socket_mainloop();
    else
	return dbus_mainloop();
#else
    return socket_mainloop();
#endif
}