summaryrefslogtreecommitdiff
path: root/vehicle/gpsd/vehicle_gpsd.c
blob: 88a50a41700b312487f50419205662b2eb6a93d7 (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
/**
 * 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"

static struct vehicle_priv {
	char *source;
	char *gpsd_query;
	struct callback_list *cbl;
	struct callback *cb;
	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;
	guint retry_timer;
	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
vehicle_gpsd_callback(struct gps_data_t *data, char *buf, size_t len,
		      int level)
{
	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='\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(0, "nmea buffer overflow, discarding '%s'\n", buffer);
			}
		}
	}	
	dbg(1,"data->set=0x%x\n", 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) {
                if(data->satellites > 0) {
                        sats_signal=0;
                        for( i=0;i<data->satellites;i++) {
                               if (data->ss[i] > 0)
                                        sats_signal++;
                        }
                }
		if (priv->sats_used != data->satellites_used || priv->sats != data->satellites || priv->sats_signal != sats_signal ) {
			priv->sats_used = data->satellites_used;
			priv->sats = data->satellites;
                        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;
	}
	if (data->set & PDOP_SET) {
		dbg(1, "pdop : %g\n", data->pdop);
		priv->hdop = data->hdop;
		data->set &= ~PDOP_SET;
	}
	if (data->set & LATLON_SET) {
		priv->geo.lat = data->fix.latitude;
		priv->geo.lng = data->fix.longitude;
		dbg(1,"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_0(priv->cbl);
	}
	dbg(2,"speed ok\n");
}

/**
 * Attempt to open the gps device.
 * Return FALSE if retry not required
 * Return TRUE to try again
 */
static gboolean
vehicle_gpsd_try_open(gpointer *data)
{
	struct vehicle_priv *priv = (struct vehicle_priv *)data;
	char *source = g_strdup(priv->source);
	char *colon = index(source + 7, ':');
	if (colon) {
		*colon = '\0';
		priv->gps = gps_open(source + 7, colon + 1);
	} else
		priv->gps = gps_open(source + 7, NULL);
	g_free(source);

	if (!priv->gps){
		dbg(0,"gps_open failed for '%s'. Retrying in %d seconds. Have you started gpsd?\n", priv->source, priv->retry_interval);
		return TRUE;
	}
	gps_query(priv->gps, priv->gpsd_query);
	gps_set_raw_hook(priv->gps, vehicle_gpsd_callback);
	priv->cb = callback_new_1(callback_cast(vehicle_gpsd_io), priv);
	priv->evwatch = event_add_watch((void *)priv->gps->gps_fd, event_watch_cond_read, priv->cb);
	if (!priv->gps->gps_fd) {
		dbg(0,"Warning: gps_fd is 0, most likely you have used a gps.h incompatible to libgps");
	}
	dbg(0,"Connected to gpsd fd=%d evwatch=%p\n", priv->gps->gps_fd, priv->evwatch);
	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(0,"Error connecting to GPS with gpsbt: (%d) %s (%s)\n",
		  errno, strerror(errno), errstr);
	}
	sleep(1);       /* give gpsd time to start */
	dbg(1,"gpsbt_start: completed\n");
#endif
	priv->retry_timer=0;
	if (vehicle_gpsd_try_open((gpointer *)priv)) {
		priv->retry_timer = g_timeout_add(priv->retry_interval*1000, (GSourceFunc)vehicle_gpsd_try_open, (gpointer *)priv);
	}
}

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

	if (priv->retry_timer) {
		g_source_remove(priv->retry_timer);
		priv->retry_timer=0;
	}
	if (priv->evwatch) {
		event_remove_watch(priv->evwatch);
		priv->evwatch = NULL;
	}
	if (priv->cb) {
		callback_destroy(priv->cb);
		priv->cb = NULL;
	}
	if (priv->gps) {
		gps_close(priv->gps);
		priv->gps = NULL;
	}
#ifdef HAVE_GPSBT
	err = gpsbt_stop(&priv->context);
	if (err < 0) {
		dbg(0,"Error %d while gpsbt_stop", err);
	}
	dbg(1,"gpsbt_stop: completed, (%d)",err);
#endif
}

static void
vehicle_gpsd_io(struct vehicle_priv *priv)
{
	dbg(1, "enter\n");
	if (priv->gps) {
	 	vehicle_last = priv;
                if (gps_poll(priv->gps)) {
			g_warning("gps_poll failed\n");
			vehicle_gpsd_close(priv);
			vehicle_gpsd_open(priv);
                }
	}
}

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);
	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;
}

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(1, "enter\n");
	source = attr_search(attrs, NULL, attr_source);
	ret = g_new0(struct vehicle_priv, 1);
	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(1,"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(0, "Retry interval %d too small, setting to %d\n", ret->retry_interval, MIN_RETRY_INTERVAL);
			ret->retry_interval = MIN_RETRY_INTERVAL;
		}
	} else {
		dbg(1, "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(1, "enter\n");
	plugin_register_vehicle_type("gpsd", vehicle_gpsd_new_gpsd);
}