summaryrefslogtreecommitdiff
path: root/providers/gpsd/geoclue-gpsd.c
blob: 0b23e14630d2e1fb05ffc9ba2a61a5f24d0bedb5 (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
/*
 * Geoclue
 * geoclue-gpsd.c - Geoclue Position backend for gpsd
 *
 * Authors: Jussi Kukkonen <jku@o-hand.com>
 * Copyright 2007 by Garmin Ltd. or its subsidiaries
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 */

/* TODO:
 *
 * 	call to gps_set_callback blocks for a long time if
 * 	BT device is not present.
 *
 **/

#include <config.h>

#include <glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <gps.h>

#include <geoclue/geoclue-error.h>
#include <geoclue/gc-provider.h>
#include <geoclue/gc-iface-position.h>
#include <geoclue/gc-iface-velocity.h>

typedef struct gps_data_t gps_data;
typedef struct gps_fix_t gps_fix;

/* only listing used tags */
typedef enum {
	NMEA_NONE,
	NMEA_GSA,
	NMEA_GGA,
	NMEA_GSV,
	NMEA_RMC
} NmeaTag;

typedef struct {
	GcProvider parent;

	char *host;
	char *port;

	gps_data *gpsdata;

	gps_fix *last_fix;

	GeoclueStatus last_status;
	GeocluePositionFields last_pos_fields;
	GeoclueAccuracy *last_accuracy;
	GeoclueVelocityFields last_velo_fields;

	GMainLoop *loop;
} GeoclueGpsd;

typedef struct {
	GcProviderClass parent_class;
} GeoclueGpsdClass;

static void geoclue_gpsd_position_init(GcIfacePositionClass * iface);
static void geoclue_gpsd_velocity_init(GcIfaceVelocityClass * iface);

#define GEOCLUE_TYPE_GPSD (geoclue_gpsd_get_type ())
#define GEOCLUE_GPSD(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GEOCLUE_TYPE_GPSD, GeoclueGpsd))

G_DEFINE_TYPE_WITH_CODE(GeoclueGpsd, geoclue_gpsd, GC_TYPE_PROVIDER,
			G_IMPLEMENT_INTERFACE(GC_TYPE_IFACE_POSITION, geoclue_gpsd_position_init)
			G_IMPLEMENT_INTERFACE(GC_TYPE_IFACE_VELOCITY, geoclue_gpsd_velocity_init))

static void geoclue_gpsd_stop_gpsd(GeoclueGpsd * self);
static gboolean geoclue_gpsd_start_gpsd(GeoclueGpsd * self);

/* defining global GeoclueGpsd because gpsd does not support "user_data"
 * pointers in callbacks */
GeoclueGpsd *gpsd;

/* Geoclue interface */
static gboolean get_status(GcIfaceGeoclue * gc, GeoclueStatus * status, GError ** error)
{
	GeoclueGpsd *gpsd = GEOCLUE_GPSD(gc);

	*status = gpsd->last_status;
	return TRUE;
}

static void shutdown(GcProvider * provider)
{
	GeoclueGpsd *gpsd = GEOCLUE_GPSD(provider);

	g_main_loop_quit(gpsd->loop);
}

static void geoclue_gpsd_set_status(GeoclueGpsd * self, GeoclueStatus status)
{
	if (status != self->last_status) {
		self->last_status = status;

		/* make position and velocity invalid if no fix */
		if (status != GEOCLUE_STATUS_AVAILABLE) {
			self->last_pos_fields = GEOCLUE_POSITION_FIELDS_NONE;
			self->last_velo_fields = GEOCLUE_VELOCITY_FIELDS_NONE;
		}
		g_debug("status changed [%d]", status);
		gc_iface_geoclue_emit_status_changed(GC_IFACE_GEOCLUE(self), status);
	}
}

static gboolean set_options(GcIfaceGeoclue * gc, GHashTable * options, GError ** error)
{
	GeoclueGpsd *gpsd = GEOCLUE_GPSD(gc);
	char *port, *host;
	gboolean changed = FALSE;

	host = g_hash_table_lookup(options, "org.freedesktop.Geoclue.GPSHost");
	port = g_hash_table_lookup(options, "org.freedesktop.Geoclue.GPSPort");

	if (port == NULL) {
		port = DEFAULT_GPSD_PORT;
	}

	/* new values? */
	if (g_strcmp0(host, gpsd->host) != 0 || g_strcmp0(port, gpsd->port) != 0) {
		changed = TRUE;
	}

	if (!changed) {
		return TRUE;
	}

	/* update private values with new ones, restart gpsd */
	g_free(gpsd->port);
	gpsd->port = NULL;
	g_free(gpsd->host);
	gpsd->host = NULL;

	geoclue_gpsd_stop_gpsd(gpsd);

	if (host == NULL) {
		return TRUE;
	}

	gpsd->port = g_strdup(port);
	gpsd->host = g_strdup(host);
	if (!geoclue_gpsd_start_gpsd(gpsd)) {
		geoclue_gpsd_set_status(gpsd, GEOCLUE_STATUS_ERROR);
		g_set_error(error, GEOCLUE_ERROR, GEOCLUE_ERROR_FAILED, "Gpsd not found");
		return FALSE;
	}
	return TRUE;
}

static void finalize(GObject * object)
{
	GeoclueGpsd *gpsd = GEOCLUE_GPSD(object);

	geoclue_gpsd_stop_gpsd(gpsd);
	g_free(gpsd->last_fix);
	geoclue_accuracy_free(gpsd->last_accuracy);

	g_free(gpsd->port);
	if (gpsd->host) {
		g_free(gpsd->host);
	}

	((GObjectClass *) geoclue_gpsd_parent_class)->finalize(object);
}

static void geoclue_gpsd_class_init(GeoclueGpsdClass * klass)
{
	GObjectClass *o_class = (GObjectClass *) klass;
	GcProviderClass *p_class = (GcProviderClass *) klass;

	o_class->finalize = finalize;

	p_class->get_status = get_status;
	p_class->set_options = set_options;
	p_class->shutdown = shutdown;
}

static gboolean equal_or_nan(double a, double b)
{
	if (isnan(a) && isnan(b)) {
		return TRUE;
	}
	return a == b;
}

static void geoclue_gpsd_update_position(GeoclueGpsd * gpsd)
{
	if (gpsd->last_status != GEOCLUE_STATUS_AVAILABLE)
		return;

	gps_fix *fix = &gpsd->gpsdata->fix;
	gps_fix *last_fix = gpsd->last_fix;

	if (isnan(fix->time) == 0) {
		last_fix->time = fix->time;
	}
	if (equal_or_nan(fix->latitude, last_fix->latitude) && equal_or_nan(fix->longitude, last_fix->longitude)
	    && equal_or_nan(fix->altitude, last_fix->altitude)) {
		return;
	}
	/* save values */
	if (fix->mode >= MODE_2D && isnan(fix->latitude) == 0) {
		last_fix->latitude = fix->latitude;
	}
	if (fix->mode >= MODE_2D && isnan(fix->longitude) == 0) {
		last_fix->longitude = fix->longitude;
	}
	if (fix->mode == MODE_3D && isnan(fix->altitude) == 0) {
		last_fix->altitude = fix->altitude;
	}

	if (isnan(fix->epx) == 0) {
		last_fix->epx = fix->epx;
	}
	if (isnan(fix->epy) == 0) {
		last_fix->epy = fix->epy;
	}
	if (isnan(fix->epv) == 0) {
		last_fix->epv = fix->epv;
	}
	geoclue_accuracy_set_details(gpsd->last_accuracy,
				     GEOCLUE_ACCURACY_LEVEL_DETAILED,
				     sqrt(pow(last_fix->epx, 2) + pow(last_fix->epy, 2)), fix->epv);
	gpsd->last_pos_fields = GEOCLUE_POSITION_FIELDS_NONE;
	gpsd->last_pos_fields |= (isnan(fix->latitude)) ? 0 : GEOCLUE_POSITION_FIELDS_LATITUDE;
	gpsd->last_pos_fields |= (isnan(fix->longitude)) ? 0 : GEOCLUE_POSITION_FIELDS_LONGITUDE;
	gpsd->last_pos_fields |= (isnan(fix->altitude)) ? 0 : GEOCLUE_POSITION_FIELDS_ALTITUDE;

	g_debug
	    ("Update position: %lf, %lf, %lf, fields:0x%x, Accuracy level: %d, hori:%lf vert:%lf",
	     last_fix->latitude, last_fix->longitude, last_fix->altitude,
	     gpsd->last_pos_fields, GEOCLUE_ACCURACY_LEVEL_DETAILED,
	     sqrt(pow(last_fix->epx, 2) + pow(last_fix->epy, 2)), fix->epv);
	gc_iface_position_emit_position_changed(GC_IFACE_POSITION(gpsd),
						gpsd->last_pos_fields,
						(int)(last_fix->time + 0.5),
						last_fix->latitude,
						last_fix->longitude, last_fix->altitude, gpsd->last_accuracy);

}

static void geoclue_gpsd_update_velocity(GeoclueGpsd * gpsd)
{
	if (gpsd->last_status != GEOCLUE_STATUS_AVAILABLE)
		return;

	gps_fix *fix = &gpsd->gpsdata->fix;
	gps_fix *last_fix = gpsd->last_fix;

	if (isnan(fix->time) == 0) {
		last_fix->time = fix->time;
	}
	if (equal_or_nan(fix->track, last_fix->track) && equal_or_nan(fix->speed, last_fix->speed)
	    && equal_or_nan(fix->climb, last_fix->climb)) {
		return;
	}
	if (fix->mode >= MODE_2D && isnan(fix->track) == 0) {
		last_fix->track = fix->track;
	}
	if (fix->mode >= MODE_2D && isnan(fix->speed) == 0) {
		last_fix->speed = fix->speed;
	}
	if (fix->mode >= MODE_3D && isnan(fix->climb) == 0) {
		last_fix->climb = fix->climb;
	}

	g_debug("Update velocity: %lf, %lf, %lf", last_fix->track, last_fix->speed, last_fix->climb);
	gpsd->last_velo_fields = GEOCLUE_VELOCITY_FIELDS_NONE;
	gpsd->last_velo_fields |= (isnan(last_fix->track)) ? 0 : GEOCLUE_VELOCITY_FIELDS_DIRECTION;
	gpsd->last_velo_fields |= (isnan(last_fix->speed)) ? 0 : GEOCLUE_VELOCITY_FIELDS_SPEED;
	gpsd->last_velo_fields |= (isnan(last_fix->climb)) ? 0 : GEOCLUE_VELOCITY_FIELDS_CLIMB;

	gc_iface_velocity_emit_velocity_changed
	    (GC_IFACE_VELOCITY(gpsd), gpsd->last_velo_fields,
	     (int)(last_fix->time + 0.5), last_fix->speed, last_fix->track, last_fix->climb);
}

static void geoclue_gpsd_update_status(GeoclueGpsd * gpsd)
{
	GeoclueStatus status;

	/* gpsdata->online is supposedly always up-to-date */
	if (gpsd->gpsdata->online <= 0) {
		status = GEOCLUE_STATUS_UNAVAILABLE;
	} else if (gpsd->gpsdata->set & STATUS_SET) {
		gpsd->gpsdata->set &= ~(STATUS_SET);
		if (gpsd->gpsdata->status > 0) {
			status = GEOCLUE_STATUS_AVAILABLE;
		} else {
			status = GEOCLUE_STATUS_ACQUIRING;
		}
	} else {
		status = GEOCLUE_STATUS_AVAILABLE;
		return;
	}

	geoclue_gpsd_set_status(gpsd, status);
}

static void gpsd_raw_hook(gps_data * gpsdata, char *message, size_t len)
{
	if (gpsdata == NULL)
		return;

	geoclue_gpsd_update_status(gpsd);
	geoclue_gpsd_update_position(gpsd);
	geoclue_gpsd_update_velocity(gpsd);
}

static void geoclue_gpsd_stop_gpsd(GeoclueGpsd * self)
{
	if (self->gpsdata) {
		gps_close(self->gpsdata);
		self->gpsdata = NULL;
	}
}

static gboolean geoclue_gpsd_start_gpsd(GeoclueGpsd * self)
{
	self->gpsdata = gps_open(self->host, self->port);
	if (self->gpsdata) {
		gps_stream(self->gpsdata, WATCH_ENABLE | WATCH_NEWSTYLE, NULL);
		gps_set_raw_hook(self->gpsdata, gpsd_raw_hook);
		return TRUE;
	} else {
		g_warning("gps_open() failed, is gpsd running (host=%s,port=%s)?", self->host, self->port);
		return FALSE;
	}
}

gboolean gpsd_poll(gpointer data)
{
	GeoclueGpsd *self = (GeoclueGpsd *) data;
	if (self->gpsdata) {
		if (gps_poll(self->gpsdata) < 0) {
			geoclue_gpsd_set_status(self, GEOCLUE_STATUS_ERROR);
			geoclue_gpsd_stop_gpsd(self);
			return FALSE;
		}
	}
	return TRUE;
}

static void geoclue_gpsd_init(GeoclueGpsd * self)
{
	self->gpsdata = NULL;
	self->last_fix = g_new0(gps_fix, 1);

	self->last_pos_fields = GEOCLUE_POSITION_FIELDS_NONE;
	self->last_velo_fields = GEOCLUE_VELOCITY_FIELDS_NONE;
	self->last_accuracy = geoclue_accuracy_new(GEOCLUE_ACCURACY_LEVEL_NONE, 0, 0);

	gc_provider_set_details(GC_PROVIDER(self),
				"org.freedesktop.Geoclue.Providers.Gpsd",
				"/org/freedesktop/Geoclue/Providers/Gpsd", "Gpsd", "Gpsd provider");

	self->port = g_strdup(DEFAULT_GPSD_PORT);
	self->host = g_strdup("localhost");

	geoclue_gpsd_set_status(self, GEOCLUE_STATUS_ACQUIRING);
	if (!geoclue_gpsd_start_gpsd(self)) {
		geoclue_gpsd_set_status(self, GEOCLUE_STATUS_ERROR);
	}
}

static gboolean
get_position(GcIfacePosition * gc,
	     GeocluePositionFields * fields,
	     int *timestamp,
	     double *latitude, double *longitude, double *altitude, GeoclueAccuracy ** accuracy, GError ** error)
{
	GeoclueGpsd *gpsd = GEOCLUE_GPSD(gc);

	*timestamp = (int)(gpsd->last_fix->time + 0.5);
	*latitude = gpsd->last_fix->latitude;
	*longitude = gpsd->last_fix->longitude;
	*altitude = gpsd->last_fix->altitude;
	*fields = gpsd->last_pos_fields;
	*accuracy = geoclue_accuracy_copy(gpsd->last_accuracy);

	return TRUE;
}

static void geoclue_gpsd_position_init(GcIfacePositionClass * iface)
{
	iface->get_position = get_position;
}

static gboolean
get_velocity(GcIfaceVelocity * gc,
	     GeoclueVelocityFields * fields, int *timestamp, double *speed, double *direction, double *climb, GError ** error)
{
	GeoclueGpsd *gpsd = GEOCLUE_GPSD(gc);

	*timestamp = (int)(gpsd->last_fix->time + 0.5);
	*speed = gpsd->last_fix->speed;
	*direction = gpsd->last_fix->track;
	*climb = gpsd->last_fix->climb;
	*fields = gpsd->last_velo_fields;

	return TRUE;
}

static void geoclue_gpsd_velocity_init(GcIfaceVelocityClass * iface)
{
	iface->get_velocity = get_velocity;
}

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

	gpsd = g_object_new(GEOCLUE_TYPE_GPSD, NULL);

	gpsd->loop = g_main_loop_new(NULL, TRUE);
	g_timeout_add(500, gpsd_poll, (gpointer) gpsd);

	g_main_loop_run(gpsd->loop);

	g_main_loop_unref(gpsd->loop);
	g_object_unref(gpsd);

	return 0;
}