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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
|
/** @file vehicle_gypsy.c
* @brief gypsy uses dbus signals
*
* 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.
*
* @Author Tim Niemeyer <reddog@mastersword.de>
* @date 2008-2009
*/
#include <config.h>
#include <gypsy/gypsy-device.h>
#include <gypsy/gypsy-control.h>
#include <gypsy/gypsy-course.h>
#include <gypsy/gypsy-position.h>
#include <gypsy/gypsy-satellite.h>
#ifdef USE_BINDING_DBUS
#include <dbus/dbus.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>
#endif
#ifdef HAVE_UNISTD_H
#include <sys/types.h>
#include <unistd.h>
#endif
#include <string.h>
#include <glib.h>
#include <math.h>
#include "debug.h"
#include "callback.h"
#include "plugin.h"
#include "coord.h"
#include "item.h"
#include "vehicle.h"
static struct vehicle_priv {
char *source;
GypsyControl *control;
GypsyPosition *position;
GypsyDevice *device;
GypsyCourse *course;
GypsySatellite *satellite;
char *path;
struct callback_list *cbl;
guint retry_interval;
struct coord_geo geo;
double speed;
double direction;
double height;
int fix_type;
time_t fix_time;
char fixiso8601[128];
int sats;
int sats_used;
guint retry_timer;
struct attr ** attrs;
int have_cords;
} *vehicle_last;
#define DEFAULT_RETRY_INTERVAL 10 // seconds
#define MIN_RETRY_INTERVAL 1 // seconds
/**
* @brief When the fixstatus has changed, this function get called
*
* fixstatus can be one of this:
* GYPSY_DEVICE_FIX_STATUS_INVALID = 0
* GYPSY_DEVICE_FIX_STATUS_NONE = 1
* GYPSY_DEVICE_FIX_STATUS_2D = 2
* GYPSY_DEVICE_FIX_STATUS_3D = 3
*
* Anytime this functions get called, we have to call the global
* callback.
*
* @param device The GypsyDevice
* @param fixstatus The fisstatus 0, 1, 2 or 3
* @param userdata
* @returns nothing
*/
static void
vehicle_gypsy_fixstatus_changed(GypsyDevice *device,
gint fixstatus,
gpointer userdata)
{
struct vehicle_priv *priv = vehicle_last;
if (fixstatus==GYPSY_DEVICE_FIX_STATUS_3D)
priv->fix_type = 3;
else if (fixstatus==GYPSY_DEVICE_FIX_STATUS_2D)
priv->fix_type = 1;
else
priv->fix_type = 0;
callback_list_call_attr_0(priv->cbl, attr_position_coord_geo);
}
/**
* @brief When the position has changed, this function get called
*
* The fields_set can hold:
* GYPSY_POSITION_FIELDS_NONE = 1 << 0,
* GYPSY_POSITION_FIELDS_LATITUDE = 1 << 1,
* GYPSY_POSITION_FIELDS_LONGITUDE = 1 << 2,
* GYPSY_POSITION_FIELDS_ALTITUDE = 1 << 3
*
* If we get any new information, we have to call the global
* callback.
*
* @param position The GypsyPosition
* @param fields_set Bitmask indicating what field was set
* @param timestamp the time since Unix Epoch
* @param latitude
* @param longitude
* @param altitude
* @param userdata
* @returns nothing
*/
static void
vehicle_gypsy_position_changed(GypsyPosition *position,
GypsyPositionFields fields_set, int timestamp,
double latitude, double longitude, double altitude,
gpointer userdata)
{
struct vehicle_priv *priv = vehicle_last;
int cb = FALSE;
if (timestamp > 0)
priv->fix_time = timestamp;
if (fields_set & GYPSY_POSITION_FIELDS_LATITUDE)
{
cb = TRUE;
priv->geo.lat = latitude;
}
if (fields_set & GYPSY_POSITION_FIELDS_LONGITUDE)
{
cb = TRUE;
priv->geo.lng = longitude;
}
if (fields_set & GYPSY_POSITION_FIELDS_ALTITUDE)
{
cb = TRUE;
priv->height = altitude;
}
if (cb)
{
priv->have_cords = 1;
callback_list_call_attr_0(priv->cbl, attr_position_coord_geo);
}
}
/**
* @brief Everytime any Sat-Details are changed, this function get called
*
* Going through all Sats, counting those wich are in use.
*
* Anytime this functions get called, we have to call the global
* callback.
*
* @param satellite The GypsySatellite
* @param satellites An GPtrArray wich hold information of all sats
* @param userdata
* @returns nothing
*/
static void
vehicle_gypsy_satellite_changed(GypsySatellite *satellite,
GPtrArray *satellites,
gpointer userdata)
{
struct vehicle_priv *priv = vehicle_last;
int i, sats, used=0;
sats = satellites->len;
for (i = 0; i < sats; i++) {
GypsySatelliteDetails *details = satellites->pdata[i];
if (details->in_use)
used++;
}
priv->sats_used = used;
priv->sats = sats;
callback_list_call_attr_0(priv->cbl, attr_position_coord_geo);
}
/**
* @brief When the course or speed has changed, this function get called
*
* Only speed and direction are used!
*
* If we get any new information, we have to call the global
* callback.
*
* @param course The GypsyCourse
* @param fields Bitmask indicating what field was set
* @param timestamp the time since Unix Epoch
* @param speed
* @param direction
* @param climb
* @param userdata
* @returns nothing
*/
static void
vehicle_gypsy_course_changed (GypsyCourse *course,
GypsyCourseFields fields,
int timestamp,
double speed,
double direction,
double climb,
gpointer userdata)
{
struct vehicle_priv *priv = vehicle_last;
int cb = FALSE;
if (fields & GYPSY_COURSE_FIELDS_SPEED)
{
priv->speed = speed*3.6;
cb = TRUE;
}
if (fields & GYPSY_COURSE_FIELDS_DIRECTION)
{
priv->direction = direction;
cb = TRUE;
}
if (cb)
callback_list_call_attr_0(priv->cbl, attr_position_coord_geo);
}
/**
* @brief Attempt to open the gypsy device.
*
* Tells gypsy wich functions to call when anything occours.
*
* @param data
* @returns TRUE to try again; FALSE if retry not required
*/
static gboolean
vehicle_gypsy_try_open(gpointer *data)
{
struct vehicle_priv *priv = (struct vehicle_priv *)data;
char *source = g_strdup(priv->source);
GError *error = NULL;
g_type_init();
priv->control = gypsy_control_get_default();
priv->path = gypsy_control_create(priv->control, source+8, &error);
if (priv->path == NULL) {
g_warning ("Error creating gypsy conrtol path for %s: %s", source+8, error->message);
return TRUE;
}
priv->position = gypsy_position_new(priv->path);
g_signal_connect(priv->position, "position-changed", G_CALLBACK (vehicle_gypsy_position_changed), NULL);
priv->satellite = gypsy_satellite_new(priv->path);
g_signal_connect(priv->satellite, "satellites-changed", G_CALLBACK (vehicle_gypsy_satellite_changed), NULL);
priv->course = gypsy_course_new(priv->path);
g_signal_connect(priv->course, "course-changed", G_CALLBACK (vehicle_gypsy_course_changed), NULL);
priv->device = gypsy_device_new(priv->path);
g_signal_connect(priv->device, "fix-status-changed", G_CALLBACK (vehicle_gypsy_fixstatus_changed), NULL);
gypsy_device_start(priv->device, &error);
if (error != NULL) {
g_warning ("Error starting gypsy for %s: %s", source+8, error->message);
return TRUE;
}
vehicle_last = priv;
dbg(lvl_debug,"gypsy connected to %d\n", source+8);
g_free(source);
return FALSE;
}
/**
* @brief Open a connection to gypsy. Will re-try the connection if it fails
*
* @param priv
* @returns nothing
*/
static void
vehicle_gypsy_open(struct vehicle_priv *priv)
{
priv->retry_timer=0;
if (vehicle_gypsy_try_open((gpointer *)priv)) {
priv->retry_timer = g_timeout_add(priv->retry_interval*1000, (GSourceFunc)vehicle_gypsy_try_open, (gpointer *)priv);
}
}
/**
* @brief Stop retry timer; Free alloced memory
*
* @param priv
* @returns nothing
*/
static void
vehicle_gypsy_close(struct vehicle_priv *priv)
{
if (priv->retry_timer) {
g_source_remove(priv->retry_timer);
priv->retry_timer=0;
}
if (priv->path)
g_free(priv->path);
if (priv->position)
g_free(priv->position);
if (priv->satellite)
g_free(priv->satellite);
if (priv->course)
g_free(priv->course);
if (priv->device)
g_free(priv->device);
if (priv->control)
g_object_unref(G_OBJECT (priv->control));
}
/**
* @brief Free the gypsy_vehicle
*
* @param priv
* @returns nothing
*/
static void
vehicle_gypsy_destroy(struct vehicle_priv *priv)
{
vehicle_gypsy_close(priv);
if (priv->source)
g_free(priv->source);
g_free(priv);
}
/**
* @brief Provide the outside with information
*
* @param priv
* @param type TODO: What can this be?
* @param attr
* @returns true/false
*/
static int
vehicle_gypsy_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_qual:
attr->u.num = priv->sats;
break;
case attr_position_sats_used:
attr->u.num = priv->sats_used;
break;
case attr_position_coord_geo:
attr->u.coord_geo = &priv->geo;
if (!priv->have_cords)
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;
}
case attr_active:
active = attr_search(priv->attrs,NULL,attr_active);
if(active != NULL && active->u.num == 1)
return 1;
else
return 0;
break;
default:
return 0;
}
attr->type = type;
return 1;
}
struct vehicle_methods vehicle_gypsy_methods = {
vehicle_gypsy_destroy,
vehicle_gypsy_position_attr_get,
};
/**
* @brief Create gypsy_vehicle
*
* @param meth
* @param cbl
* @param attrs
* @returns vehicle_priv
*/
static struct vehicle_priv *
vehicle_gypsy_new_gypsy(struct vehicle_methods *meth,
struct callback_list *cbl,
struct attr **attrs)
{
struct vehicle_priv *ret;
struct attr *source, *retry_int;
#if defined(USE_BINDING_DBUS) && defined(HAVE_UNISTD_H)
DBusConnection *conn;
DBusMessage *message;
dbus_uint32_t serial,pid=getpid();
struct attr *destination,*path,*interface,*method;
destination=attr_search(attrs, NULL, attr_dbus_destination);
path=attr_search(attrs, NULL, attr_dbus_path);
interface=attr_search(attrs, NULL, attr_dbus_interface);
method=attr_search(attrs, NULL, attr_dbus_method);
if (destination && path && interface && method) {
conn=dbus_bus_get(DBUS_BUS_SESSION, NULL);
if (conn) {
message=dbus_message_new_method_call(destination->u.str,path->u.str,interface->u.str,method->u.str);
dbus_message_append_args(message, DBUS_TYPE_INT32, &pid, DBUS_TYPE_INVALID);
dbus_connection_send(conn, message, &serial);
dbus_message_unref(message);
dbus_connection_unref(conn);
} else {
dbg(lvl_error,"failed to connect to session bus\n");
}
}
#endif
dbg(lvl_debug, "enter\n");
source = attr_search(attrs, NULL, attr_source);
ret = g_new0(struct vehicle_priv, 1);
ret->have_cords = 0;
ret->source = g_strdup(source->u.str);
ret->attrs = attrs;
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_error, "Retry interval not defined, setting to %d\n", DEFAULT_RETRY_INTERVAL);
ret->retry_interval = DEFAULT_RETRY_INTERVAL;
}
ret->cbl = cbl;
*meth = vehicle_gypsy_methods;
vehicle_gypsy_open(ret);
return ret;
}
/**
* @brief register vehicle_gypsy
*
* @returns nothing
*/
void
plugin_init(void)
{
dbg(lvl_debug, "enter\n");
plugin_register_vehicle_type("gypsy", vehicle_gypsy_new_gypsy);
}
|