summaryrefslogtreecommitdiff
path: root/navit/vehicle/gpsd/vehicle_gpsd.c
blob: 4526e159fdbe2a51ad006518a8ded22296ca7df7 (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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/**
 * Navit, a modular navigation system.
 * Copyright (C) 2005-2008 Navit Team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */

#include <config.h>
#include <gps.h>
#include <string.h>
#include <glib.h>
#include <math.h>
#ifdef HAVE_GPSBT
#include <gpsbt.h>
#include <errno.h>
#endif
#include "debug.h"
#include "callback.h"
#include "plugin.h"
#include "coord.h"
#include "item.h"
#include "vehicle.h"
#include "event.h"
#include "types.h"

static struct vehicle_priv {
	char *source;
	char *gpsd_query;
	struct callback_list *cbl;
	struct callback *cb, *cbt;
	struct event_watch *evwatch;
	guint retry_interval;
	struct gps_data_t *gps;
	struct coord_geo geo;
	double speed;
	double direction;
	double height;
	double hdop;
	int status;
	int fix_type;
	time_t fix_time;
	int sats;
	int sats_signal;
	int sats_used;
	char *nmea_data;
	char *nmea_data_buf;
	struct event_timeout *retry_timer2;
	struct attr ** attrs;
	char fixiso8601[128];
#ifdef HAVE_GPSBT
	gpsbt_t context;
#endif
} *vehicle_last;

#define DEFAULT_RETRY_INTERVAL 10 // seconds
#define MIN_RETRY_INTERVAL 1 // seconds

static void vehicle_gpsd_io(struct vehicle_priv *priv);

static void
#ifdef HAVE_LIBGPS19
vehicle_gpsd_callback(struct gps_data_t *data, const char *buf, size_t len)
#else
vehicle_gpsd_callback(struct gps_data_t *data, const char *buf, size_t len,
		      int level)
#endif
{
	char *pos,*nmea_data_buf;
        int i=0,sats_signal=0;
       
	struct vehicle_priv *priv = vehicle_last;
	if( len > 0 && buf[0] == '$' ) {
		char buffer[len+2];
		buffer[len+1]='\0';
		memcpy(buffer, buf, len);
		pos=strchr(buffer,'\n');
		if(!pos) {
			pos=strchr(buffer,'\r');
		}
		if (pos) {
			*pos  ='\n';
			*++pos='\0';
			if (!priv->nmea_data_buf || strlen(priv->nmea_data_buf) < 65536) {
				nmea_data_buf=g_strconcat(priv->nmea_data_buf ? priv->nmea_data_buf : "", buffer, NULL);
				g_free(priv->nmea_data_buf);
				priv->nmea_data_buf=nmea_data_buf;
			} else {
				dbg(lvl_error, "nmea buffer overflow, discarding '%s'\n", buffer);
			}
		}
	}	
	dbg(lvl_debug,"data->set="LONGLONG_HEX_FMT"\n", (unsigned long long)data->set);
	if (data->set & SPEED_SET) {
		priv->speed = data->fix.speed * 3.6;
		if(!isnan(data->fix.speed))
			callback_list_call_attr_0(priv->cbl, attr_position_speed);
		data->set &= ~SPEED_SET;
	}
	if (data->set & TRACK_SET) {
		priv->direction = data->fix.track;
		data->set &= ~TRACK_SET;
	}
	if (data->set & ALTITUDE_SET) {
		priv->height = data->fix.altitude;
		data->set &= ~ALTITUDE_SET;
	}
	if (data->set & SATELLITE_SET) {
// We cannot rely on GPSD_API_MAJOR_VERSION here because it was not
// incremented for this change :-(.
#ifdef HAVE_LIBGPS19
                if(data->satellites_visible > 0) {
#else
                if(data->satellites > 0) {
#endif
                        sats_signal=0;
#ifdef HAVE_LIBGPS19
                        for( i=0;i<data->satellites_visible;i++) {
#else
                        for( i=0;i<data->satellites;i++) {
#endif
                               if (data->ss[i] > 0)
                                        sats_signal++;
                        }
                }
#ifdef HAVE_LIBGPS19
		if (priv->sats_used != data->satellites_used || priv->sats != data->satellites_visible || priv->sats_signal != sats_signal ) {
#else
		if (priv->sats_used != data->satellites_used || priv->sats != data->satellites || priv->sats_signal != sats_signal ) {
#endif
			priv->sats_used = data->satellites_used;
#ifdef HAVE_LIBGPS19
			priv->sats = data->satellites_visible;
#else
			priv->sats = data->satellites;
#endif
                        priv->sats_signal = sats_signal;
			callback_list_call_attr_0(priv->cbl, attr_position_sats);
		}
		data->set &= ~SATELLITE_SET;
	}
	if (data->set & STATUS_SET) {
		priv->status = data->status;
		data->set &= ~STATUS_SET;
	}
	if (data->set & MODE_SET) {
		priv->fix_type = data->fix.mode - 1;
		data->set &= ~MODE_SET;
	}
	if (data->set & TIME_SET) {
		priv->fix_time = data->fix.time;
		data->set &= ~TIME_SET;
	}
#ifdef HAVE_LIBGPS19
	if (data->set & DOP_SET) {
		dbg(lvl_debug, "pdop : %g\n", data->dop.pdop);
		priv->hdop = data->dop.pdop;
		data->set &= ~DOP_SET;
#else
	if (data->set & PDOP_SET) {
		dbg(lvl_debug, "pdop : %g\n", data->pdop);
		priv->hdop = data->hdop;
		data->set &= ~PDOP_SET;
#endif
	}
	if (data->set & LATLON_SET) {
		priv->geo.lat = data->fix.latitude;
		priv->geo.lng = data->fix.longitude;
		dbg(lvl_debug,"lat=%f lng=%f\n", priv->geo.lat, priv->geo.lng);
		g_free(priv->nmea_data);
		priv->nmea_data=priv->nmea_data_buf;
		priv->nmea_data_buf=NULL;
		data->set &= ~LATLON_SET;
	}
	// If data->fix.speed is NAN, then the drawing gets jumpy.
	if (! isnan(data->fix.speed) && priv->fix_type > 0) {
		callback_list_call_attr_0(priv->cbl, attr_position_coord_geo);
	}
	dbg(lvl_info,"speed ok\n");
}

/**
 * Attempt to open the gps device.
 * Return FALSE if retry not required
 * Return TRUE to try again
 */
static int
vehicle_gpsd_try_open(struct vehicle_priv *priv)
{
	char *source = g_strdup(priv->source);
	char *colon = index(source + 7, ':');
	char *port=NULL;
	if (colon) {
		*colon = '\0';
		port=colon+1;
	}
	dbg(lvl_debug,"Trying to connect to %s:%s\n",source+7,port?port:"default");

#if GPSD_API_MAJOR_VERSION >= 5
        /* gps_open returns 0 on success */
	if (gps_open(source + 7, port, priv->gps)) {
#else
	priv->gps = gps_open(source + 7, port);
	if(!priv->gps) {
#endif
		dbg(lvl_error,"gps_open failed for '%s'. Retrying in %d seconds. Have you started gpsd?\n", priv->source, priv->retry_interval);
                g_free(source);
		return TRUE;
	}
	g_free(source);

#ifdef HAVE_LIBGPS19
	if (strchr(priv->gpsd_query,'r'))
		gps_stream(priv->gps, WATCH_ENABLE|WATCH_NMEA|WATCH_JSON, NULL);
	else
		gps_stream(priv->gps, WATCH_ENABLE|WATCH_JSON, NULL);
#else
	gps_query(priv->gps, priv->gpsd_query);
#endif

#if GPSD_API_MAJOR_VERSION < 5
	gps_set_raw_hook(priv->gps, vehicle_gpsd_callback);
#endif
	priv->cb = callback_new_1(callback_cast(vehicle_gpsd_io), priv);
	priv->cbt = callback_new_1(callback_cast(vehicle_gpsd_try_open), priv);
	priv->evwatch = event_add_watch(priv->gps->gps_fd, event_watch_cond_read, priv->cb);
	if (!priv->gps->gps_fd) {
		dbg(lvl_error,"Warning: gps_fd is 0, most likely you have used a gps.h incompatible to libgps");
	}
	dbg(lvl_debug,"Connected to gpsd fd=%d evwatch=%p\n", priv->gps->gps_fd, priv->evwatch);
	event_remove_timeout(priv->retry_timer2);
	priv->retry_timer2=NULL;
	return FALSE;
}

/**
 * Open a connection to gpsd. Will re-try the connection if it fails
 */
static void
vehicle_gpsd_open(struct vehicle_priv *priv)
{
#ifdef HAVE_GPSBT
	char errstr[256] = "";
	/* We need to start gpsd (via gpsbt) first. */
	errno = 0;
	memset(&priv->context, 0, sizeof(gpsbt_t));
	if(gpsbt_start(NULL, 0, 0, 0, errstr, sizeof(errstr),
		0, &priv->context) < 0) {
	       dbg(lvl_error,"Error connecting to GPS with gpsbt: (%d) %s (%s)\n",
		  errno, strerror(errno), errstr);
	}
	sleep(1);       /* give gpsd time to start */
	dbg(lvl_debug,"gpsbt_start: completed\n");
#endif
	priv->retry_timer2=NULL;
	if (vehicle_gpsd_try_open(priv)) 
		priv->retry_timer2=event_add_timeout(priv->retry_interval*1000, 1, priv->cbt);
}

static void
vehicle_gpsd_close(struct vehicle_priv *priv)
{
#ifdef HAVE_GPSBT
	int err;
#endif

	if (priv->retry_timer2) {
		event_remove_timeout(priv->retry_timer2);
		priv->retry_timer2=NULL;
	}
	if (priv->evwatch) {
		event_remove_watch(priv->evwatch);
		priv->evwatch = NULL;
	}
	if (priv->cb) {
		callback_destroy(priv->cb);
		priv->cb = NULL;
	}
	if (priv->cbt) {
		callback_destroy(priv->cbt);
		priv->cbt = NULL;
	}
	if (priv->gps) {
		gps_close(priv->gps);
#if GPSD_API_MAJOR_VERSION >= 5
                g_free(priv->gps);
#endif
		priv->gps = NULL;
	}
#ifdef HAVE_GPSBT
	err = gpsbt_stop(&priv->context);
	if (err < 0) {
		dbg(lvl_error,"Error %d while gpsbt_stop", err);
	}
	dbg(lvl_debug,"gpsbt_stop: completed, (%d)",err);
#endif
}

static void
vehicle_gpsd_io(struct vehicle_priv *priv)
{
	dbg(lvl_debug, "enter\n");
	if (priv->gps) {
	 	vehicle_last = priv;
#if GPSD_API_MAJOR_VERSION >= 5
                if(gps_read(priv->gps)==-1) {
                  dbg(lvl_error,"gps_poll failed\n");
                  vehicle_gpsd_close(priv);
                  vehicle_gpsd_open(priv);
                }
                else {
                  const char *buf;
                  buf = gps_data(priv->gps);
  	          vehicle_gpsd_callback(priv->gps,buf,strlen(buf));
                }
#else
                if (gps_poll(priv->gps)) {
			dbg(lvl_error,"gps_poll failed\n");
			vehicle_gpsd_close(priv);
			vehicle_gpsd_open(priv);
                }
#endif
	}
}

static void
vehicle_gpsd_destroy(struct vehicle_priv *priv)
{
	vehicle_gpsd_close(priv);
	if (priv->source)
		g_free(priv->source);
	if (priv->gpsd_query)
		g_free(priv->gpsd_query);
#if GPSD_API_MAJOR_VERSION >= 5
        g_free(priv->gps);
#endif
	g_free(priv);
}

static int
vehicle_gpsd_position_attr_get(struct vehicle_priv *priv,
			       enum attr_type type, struct attr *attr)
{
	struct attr * active=NULL;
	switch (type) {
	case attr_position_fix_type:
		attr->u.num = priv->fix_type;
		break;
	case attr_position_height:
		attr->u.numd = &priv->height;
		break;
	case attr_position_speed:
		attr->u.numd = &priv->speed;
		break;
	case attr_position_direction:
		attr->u.numd = &priv->direction;
		break;
	case attr_position_hdop:
		attr->u.numd = &priv->hdop;
		break;
	case attr_position_qual:
		attr->u.num = priv->sats;
		break;
	case attr_position_sats_signal:
		attr->u.num = priv->sats_signal;
		break;
	case attr_position_sats_used:
		attr->u.num = priv->sats_used;
		break;
	case attr_position_coord_geo:
		attr->u.coord_geo = &priv->geo;
		break;
	case attr_position_nmea:
		attr->u.str=priv->nmea_data;
		if (! attr->u.str)
			return 0;
		break;
	case attr_position_time_iso8601:
		{
		struct tm tm;
		if (!priv->fix_time)
			return 0;
		if (gmtime_r(&priv->fix_time, &tm)) {
			strftime(priv->fixiso8601, sizeof(priv->fixiso8601),
				"%Y-%m-%dT%TZ", &tm);
			attr->u.str=priv->fixiso8601;
		} else
			return 0;
		}
		break;
	case attr_active:
	  active = attr_search(priv->attrs,NULL,attr_active);
	  if(active != NULL) {
		attr->u.num=active->u.num;
	    return 1;
	  } else
	    return 0;
	       break;
	default:
		return 0;
	}
	attr->type = type;
	return 1;
}

static struct vehicle_methods vehicle_gpsd_methods = {
	vehicle_gpsd_destroy,
	vehicle_gpsd_position_attr_get,
};

static struct vehicle_priv *
vehicle_gpsd_new_gpsd(struct vehicle_methods
		      *meth, struct callback_list
		      *cbl, struct attr **attrs)
{
	struct vehicle_priv *ret;
	struct attr *source, *query, *retry_int;

	dbg(lvl_debug, "enter\n");
	source = attr_search(attrs, NULL, attr_source);
	ret = g_new0(struct vehicle_priv, 1);
#if GPSD_API_MAJOR_VERSION >= 5
	ret->gps = g_new0(struct gps_data_t, 1);
#endif
	ret->source = g_strdup(source->u.str);
	query = attr_search(attrs, NULL, attr_gpsd_query);
	if (query) {
		ret->gpsd_query = g_strconcat(query->u.str, "\n", NULL);
	} else {
		ret->gpsd_query = g_strdup("w+x\n");
	}
	dbg(lvl_debug,"Format string for gpsd_query: %s\n",ret->gpsd_query);
	retry_int = attr_search(attrs, NULL, attr_retry_interval);
	if (retry_int) {
		ret->retry_interval = retry_int->u.num;
		if (ret->retry_interval < MIN_RETRY_INTERVAL) {
			dbg(lvl_error, "Retry interval %d too small, setting to %d\n", ret->retry_interval, MIN_RETRY_INTERVAL);
			ret->retry_interval = MIN_RETRY_INTERVAL;
		}
	} else {
		dbg(lvl_debug, "Retry interval not defined, setting to %d\n", DEFAULT_RETRY_INTERVAL);
		ret->retry_interval = DEFAULT_RETRY_INTERVAL;
	}
	ret->cbl = cbl;
	*meth = vehicle_gpsd_methods;
	ret->attrs = attrs;
	vehicle_gpsd_open(ret);
	return ret;
}

void
plugin_init(void)
{
	dbg(lvl_debug, "enter\n");
	plugin_register_vehicle_type("gpsd", vehicle_gpsd_new_gpsd);
}