2 * This file is part of mapper
4 * Copyright (C) 2007 Kaj-Michael Lang
5 * Copyright (C) 2006-2007 John Costigan.
7 * POI and GPS-Info code originally written by Cezary Jackiewicz.
9 * Default map data provided by http://www.openstreetmap.org/
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
38 #include <glib/gstdio.h>
39 #include <dbus/dbus.h>
40 #include <dbus/dbus-glib.h>
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <arpa/inet.h>
50 #include "gps-nmea-parse.h"
52 #ifdef WITH_HILDON_DBUS_BT
58 static gpsbt_t ctx = {0};
61 static gboolean gps_channel_cb_error(GIOChannel *src, GIOCondition condition, gpointer data);
62 static gboolean gps_channel_cb_input(GIOChannel *src, GIOCondition condition, gpointer data);
63 static gboolean gps_channel_cb_connect(GIOChannel * src, GIOCondition condition, gpointer data);
64 static gboolean gps_connect_socket(Gps *gps);
65 static gboolean gps_connect_file(Gps *gps, gchar *file);
67 #define GPSD_NMEA "r+\r\n"
68 #define GPSD_PORT (2947)
69 #define LOCALHOST "127.0.0.1"
70 #define GPS_DEVICE_FILE "/dev/ttyS0"
72 const GpsTypes gps_types[] = {
74 { GPS_IO_RFCOMM, "Bluetooth connection (old)", TRUE, TRUE, FALSE},
76 #ifdef WITH_HILDON_DBUS_BT
77 { GPS_IO_HILDON_DBUS, "Bluetooth connection (btconn)", TRUE, TRUE, FALSE },
79 #ifdef WITH_BLUEZ_DBUS_BT
80 { GPS_IO_BLUEZ_DBUS, "Bluetooth connection (new)", TRUE, TRUE, FALSE},
82 { GPS_IO_GPSD, "GPSD Connection", FALSE, TRUE, TRUE, GPSD_PORT, LOCALHOST },
83 { GPS_IO_FILE, "File or device", FALSE, TRUE, FALSE, 0, GPS_DEVICE_FILE },
84 { GPS_IO_TCP, "TCP Connection", FALSE, TRUE, TRUE, 1024, LOCALHOST },
86 { GPS_IO_GYPSY, "Gypsy", FALSE, TRUE, FALSE },
88 { GPS_IO_SIMULATION, "Simulation (random)", FALSE, FALSE, FALSE},
93 gps_get_supported_types(void)
102 gps_new(GpsIOSourceType type)
106 gps=g_slice_new0(Gps);
107 gps->data.fix=FIX_NOFIX;
109 gps->io.address=NULL;
111 gps->io.conn=RCVR_OFF;
116 gps->connection_error=NULL;
117 gps->connection_retry=NULL;
118 gps->connection_progress=NULL;
119 gps->update_location=NULL;
120 gps->update_info=NULL;
121 gps->update_satellite=NULL;
122 gps_data_integerize(&gps->data);
127 gps_set_address(Gps *gps, gchar *address, guint port)
130 g_free(gps->io.address);
132 gps->io.address=g_strdup(address);
137 gps_set_type(Gps *gps, GpsIOSourceType type)
147 g_free(gps->io.address);
148 g_slice_free(Gps, gps);
152 * Convert the float lat/lon/speed/heading data into integer units.
153 * Also calculate offsets.
156 gps_data_integerize(GpsData *gpsdata)
160 tmp=(gpsdata->heading*(1.f/180.f))*G_PI;
161 latlon2unit(gpsdata->lat, gpsdata->lon, gpsdata->unitx, gpsdata->unity);
162 gpsdata->vel_offsetx=(gint)(floor(gpsdata->speed*sin(tmp)+0.5f));
163 gpsdata->vel_offsety=-(gint)(floor(gpsdata->speed*cos(tmp)+0.5f));
166 #ifdef WITH_HILDON_DBUS_BT
168 gps_connect_response(DBusGProxy *proxy, DBusGProxyCall *call_id, Gps *gps)
170 GError *error = NULL;
171 gchar *fdpath = NULL;
173 if (gps->io.conn==RCVR_DOWN && gps->io.address) {
174 if (!dbus_g_proxy_end_call(gps->io.rfcomm_req_proxy, call_id, &error, G_TYPE_STRING, &fdpath, G_TYPE_INVALID)) {
175 if (error->domain == DBUS_GERROR && error->code == DBUS_GERROR_REMOTE_EXCEPTION) {
176 /* If we're already connected, it's not an error, unless
177 * they don't give us the file descriptor path, in which
178 * case we re-connect.*/
179 if (!strcmp(BTCOND_ERROR_CONNECTED, dbus_g_error_get_name(error)) || !fdpath) {
180 g_printerr("Caught remote method exception %s: %s", dbus_g_error_get_name(error), error->message);
183 if (gps->connection_retry==NULL)
186 if (gps->connection_retry(gps, error->message)==TRUE) {
187 gps_connect_later(gps);
189 gps_conn_set_state(gps, RCVR_OFF);
195 g_printerr("Error: %s\n", error->message);
197 gps_connect_later(gps); /* Try again later. */
201 gps_connect_file(gps, fdpath);
203 /* else { Looks like the middle of a disconnect. Do nothing. } */
208 * Place a request to connect about 1 second after the function is called.
211 gps_connect_later(Gps *gps)
214 g_assert(gps->io.clater_sid==0);
215 gps->io.clater_sid=g_timeout_add(1000, (GSourceFunc)gps_connect_now, gps);
219 gps_connect(Gps *gps)
221 switch (gps->io.type) {
228 return gps_connect_socket(gps);
240 * Helper to open a file or device node
243 gps_connect_file(Gps *gps, gchar *file)
245 if (-1==(gps->io.fd=open(file, O_RDONLY))) {
247 gps_connect_later(gps);
255 * Helper to connect to a socket file descriptor.
259 gps_connect_socket(Gps *gps)
264 switch (gps->io.type) {
267 g_debug("RFCOMM: %d", gps->io.fd);
268 r=connect(gps->io.fd, (struct sockaddr *)&gps->io.rcvr_addr_rc, sizeof(gps->io.rcvr_addr_rc));
273 g_debug("TCP: %d", gps->io.fd);
274 r=connect(gps->io.fd, (struct sockaddr *)&gps->io.rcvr_addr_in, sizeof(gps->io.rcvr_addr_in));
280 g_debug("GPS: Error %d", e);
282 /* The socket is non blocking so handle it */
289 g_printerr("*** Connection in progress... %d %d\n", e, r);
294 g_printerr("*** Bluetooth/GPS device not found.\n");
297 set_action_activate("gps_enable", FALSE);
298 popup_error(_window, _("No bluetooth or GPS device found."));
303 /* Connection failed. Disconnect and try again later. */
304 g_printerr("### Connect failed, retrying... %d %d\n", e, r);
307 gps_connect_later(gps);
316 * Disconnect the GPS device.
317 * - Add channel callbacks are removed
318 * - Channel is close and shutdown
319 * - File descriptor is closed
320 * - Anything special for disconnecting is done
323 gps_disconnect(Gps *gps)
327 g_debug("GPS: Disconnecting from gps");
328 /* Remove watches. */
329 if (gps->io.clater_sid) {
330 g_source_remove(gps->io.clater_sid);
331 gps->io.clater_sid=0;
333 if (gps->io.error_sid) {
334 g_source_remove(gps->io.error_sid);
337 if (gps->io.connect_sid) {
338 g_source_remove(gps->io.connect_sid);
339 gps->io.connect_sid=0;
341 if (gps->io.input_sid) {
342 g_source_remove(gps->io.input_sid);
346 /* Destroy the GIOChannel object. */
347 if (gps->io.channel) {
348 g_debug("GPS: Shutting down IO channel");
349 g_io_channel_shutdown(gps->io.channel, FALSE, NULL);
350 g_io_channel_unref(gps->io.channel);
351 gps->io.channel=NULL;
354 /* Close the file descriptor. */
355 if (gps->io.fd!=-1) {
356 g_debug("GPS: Closing fd %d", gps->io.fd);
362 if (gps->io.type==GPS_IO_GPSD) {
365 g_debug("GPS: Requesting gpsd shutdown using gpsbt");
368 g_warning("Failed to stop gpsd\n");
372 #ifdef WITH_HILDON_DBUS_BT
373 if (gps->io.type==GPS_IO_HILDON_DBUS && gps->io.rfcomm_req_proxy) {
374 GError *error = NULL;
376 g_debug("GPS: Requesting rfcomm disconnection");
377 dbus_g_proxy_call(gps->io.rfcomm_req_proxy, BTCOND_RFCOMM_CANCEL_CONNECT_REQ, &error, G_TYPE_STRING, gps->io.address, G_TYPE_STRING, "SPP", G_TYPE_INVALID, G_TYPE_INVALID);
379 dbus_g_proxy_call(gps->io.rfcomm_req_proxy, BTCOND_RFCOMM_DISCONNECT_REQ, &error,G_TYPE_STRING, gps->io.address, G_TYPE_STRING, "SPP", G_TYPE_INVALID, G_TYPE_INVALID);
383 #ifdef WITH_BLUEZ_DBUS_BT
384 if (gps->io.type==GPS_IO_BLUEZ_DBUS && gps->io.rfcomm_req_proxy) {
385 GError *error = NULL;
393 gps_simulate_start(Gps *gps)
397 gps->io.conn=RCVR_FIXED;
398 gps->data.fix=FIX_2D;
399 gps_data_integerize(&gps->data);
403 gps_simulate_move(Gps *gps)
405 static gdouble slat=0, slon=0;
410 if (g_random_double()<0.5) {
411 slat=g_random_double_range(-0.0003, 0.0003);
412 slon=g_random_double_range(-0.0003, 0.0003);
418 BOUND(gps->data.lat, -80.0, 80.0);
419 BOUND(gps->data.lon, -80.0, 80.0);
421 g_debug("Sim: %f %f\n", gps->data.lat, gps->data.lon);
423 gps->data.speed=1+g_random_double_range(0.1, 10.0);
424 h=calculate_course(plat, plon, gps->data.lat, gps->data.lon);
425 gps->data.heading=(h<0) ? 360+h : h;
426 gps->data.time=time(NULL);
427 gps_data_integerize(&gps->data);
428 if (gps->update_location!=NULL) {
429 gps->update_location(gps);
431 gps->data.lheading=gps->data.heading;
433 return gps->io.conn==RCVR_FIXED ? TRUE : FALSE;
437 * Reset GPS read buffer
440 gps_read_buffer_prepare(Gps *gps)
442 gps->io.curr=gps->io.buffer;
443 *gps->io.curr = '\0';
444 gps->io.last=gps->io.buffer+GPS_READ_BUF_SIZE-1;
448 * Connect file descriptor to channel and add watches.
451 gps_connect_channel_connect(Gps *gps)
453 g_assert(gps->io.channel==NULL);
454 gps->io.channel=g_io_channel_unix_new(gps->io.fd);
455 g_io_channel_set_encoding(gps->io.channel, NULL, NULL);
456 g_io_channel_set_flags(gps->io.channel, G_IO_FLAG_NONBLOCK, NULL);
457 g_io_channel_set_buffered(gps->io.channel, FALSE);
458 gps->io.error_sid=g_io_add_watch_full(gps->io.channel, G_PRIORITY_HIGH_IDLE, G_IO_ERR | G_IO_HUP | G_IO_NVAL, gps_channel_cb_error, gps, NULL);
459 gps->io.connect_sid=g_io_add_watch_full(gps->io.channel, G_PRIORITY_HIGH_IDLE, G_IO_OUT, gps_channel_cb_connect, gps, NULL);
460 gps->io.clater_sid=0;
464 * Connect to the receiver.
465 * This method assumes that fd is -1 and _channel is NULL. If unsure, call
466 * gps_disconnect() first.
467 * Since this is an idle function, this function returns whether or not it
468 * should be called again, which is always FALSE.
470 * This function prepares for the connection, the gps_connect does the "connecting" if needed.
473 gps_connect_now(Gps *gps)
475 GError *error = NULL;
478 g_debug("GPS: Connecting GPS type %d to %s:%d\n", gps->io.type, gps->io.address, gps->io.port);
480 switch (gps->io.type) {
482 if (!gps->io.address)
484 gps_connect_file(gps, gps->io.address);
489 if (gps->io.conn<=RCVR_DOWN && gps->io.address) {
490 gps->io.fd=socket(AF_INET, SOCK_STREAM, 0);
491 if (gps->io.fd==-1) {
492 g_debug("Failed to create socket\n");
493 gps_connect_later(gps);
496 g_debug("TCP: Preparing to connect to %s:%d", gps->io.address, gps->io.port);
497 gps->io.rcvr_addr_in.sin_family = AF_INET;
498 gps->io.rcvr_addr_in.sin_port = htons(gps->io.port);
499 if (inet_pton(AF_INET, gps->io.address, &gps->io.rcvr_addr_in.sin_addr.s_addr)<1) {
507 if (gps->io.type==GPS_IO_GPSD) {
510 r=gpsbt_start(NULL, 0, 0, 0, ebuf, 128, 0, &ctx);
512 g_warning("Failed to start gpsd");
520 #ifdef WITH_HILDON_DBUS_BT
521 case GPS_IO_HILDON_DBUS:
522 if (NULL == (gps->io.dbus_conn=dbus_g_bus_get(DBUS_BUS_SYSTEM, &error))) {
523 g_printerr("Failed to get D-Bus connection.");
526 if (NULL == (gps->io.rfcomm_req_proxy = dbus_g_proxy_new_for_name(gps->io.dbus_conn, BTCOND_SERVICE, BTCOND_REQ_PATH, BTCOND_REQ_INTERFACE))) {
527 g_printerr("Failed to open connection to %s.\n", BTCOND_REQ_INTERFACE);
531 if (gps->io.rfcomm_req_proxy) {
532 gboolean mybool = TRUE;
533 dbus_g_proxy_begin_call(gps->io.rfcomm_req_proxy,
534 BTCOND_RFCOMM_CONNECT_REQ, (DBusGProxyCallNotify)gps_connect_response, gps, NULL,
535 G_TYPE_STRING, gps->io.address,
536 G_TYPE_STRING, "SPP",
537 G_TYPE_BOOLEAN, &mybool,
544 if (!gps->io.address)
547 if (gps->io.conn<=RCVR_DOWN && gps->io.address) {
548 gps->io.fd=socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
550 /* If file descriptor creation failed, try again later. Note that
551 * there is no need to call rcvr_disconnect() because the file
552 * descriptor creation is the first step, so if it fails, there's
553 * nothing to clean up. */
554 if (gps->io.fd==-1) {
555 g_debug("Failed to create socket\n");
556 gps_connect_later(gps);
559 gps->io.rcvr_addr_rc.rc_family=AF_BLUETOOTH;
560 gps->io.rcvr_addr_rc.rc_channel=1;
561 str2ba(gps->io.address, &gps->io.rcvr_addr_rc.rc_bdaddr);
565 case GPS_IO_SIMULATION:
566 /* Set a periodic cb to generate random movement */
567 gps_simulate_start(gps);
568 gps->io.input_sid=g_timeout_add(1000, (GSourceFunc)gps_simulate_move, gps);
572 g_printerr("Unknown GPS connection type\n");
577 gps_read_buffer_prepare(gps);
578 gps_connect_channel_connect(gps);
584 * Connection callback
587 gps_channel_cb_connect(GIOChannel *src, GIOCondition condition, gpointer data)
589 gint error, size = sizeof(error);
590 Gps *gps=(Gps *)data;
593 g_assert(gps->io.channel);
595 gps->io.curr=gps->io.buffer;
596 gps->io.last=gps->io.buffer+GPS_READ_BUF_SIZE-1;
598 switch (gps->io.type) {
602 if ((getsockopt(gps->io.fd, SOL_SOCKET, SO_ERROR, &error, &size) || error)) {
603 g_printerr("Error connecting to receiver; retrying...\n");
605 gps_connect_later(gps);
606 gps->io.connect_sid=0;
610 /* Set gpsd to NMEA mode */
611 if (gps->io.type==GPS_IO_GPSD) {
614 GError *error = NULL;
616 g_debug("GPS: Requesting NMEA mode from GPSD using cmd %s", GPSD_NMEA);
617 status=g_io_channel_write_chars(gps->io.channel, GPSD_NMEA, strlen(GPSD_NMEA), &written, &error);
618 if (status==G_IO_STATUS_NORMAL && written==strlen(GPSD_NMEA)) {
619 g_debug("GPS: NMEA mode set (%d %d)", (gint)written, (gint)status);
621 g_printerr("Failed to set gpsd to NMEA mode: %d %d %s\n",(gint)status,(gint)written, error->message);
624 gps_connect_later(gps);
625 gps->io.connect_sid=0;
635 g_warning("Connection from non-connecting source\n");
639 g_printf("Connected to receiver!\n");
640 gps_conn_set_state(gps, RCVR_UP);
641 gps->io.input_sid=g_io_add_watch_full(gps->io.channel, G_PRIORITY_HIGH_IDLE, G_IO_IN | G_IO_PRI, gps_channel_cb_input, gps, NULL);
643 gps->io.connect_sid=0;
648 gps_channel_cb_error(GIOChannel *src, GIOCondition condition, gpointer data)
650 Gps *gps=(Gps *)data;
653 g_debug("GPS: Channel error %d", condition);
655 /* An error has occurred - re-connect(). */
659 if (gps->connection_error!=NULL)
660 gps->connection_error(gps, "GPS Connection error");
662 if (gps->io.conn > RCVR_OFF) {
663 gps_conn_set_state(gps, RCVR_DOWN);
664 gps_connect_later(gps);
671 gps_channel_cb_input(GIOChannel *src, GIOCondition condition, gpointer data)
676 Gps *gps=(Gps *)data;
678 g_debug("%s(%d)\n", __PRETTY_FUNCTION__, condition);
681 status=g_io_channel_read_chars(gps->io.channel, gps->io.curr, gps->io.last-gps->io.curr, &bytes_read, NULL);
683 case G_IO_STATUS_NORMAL:
684 gps->io.curr += bytes_read;
685 *gps->io.curr = '\0'; /* append a \0 so we can read as string */
686 while ((eol = strchr(gps->io.buffer, '\n'))) {
687 gchar *sptr = gps->io.buffer + 1; /* Skip the $ */
689 if (*gps->io.buffer=='$') {
690 /* This is the beginning of a sentence; okay to parse. */
691 *eol = '\0'; /* overwrite \n with \0 */
692 while (*sptr && *sptr != '*')
695 /* If we're at a \0 (meaning there is no checksum), or if the
696 * checksum is good, then parse the sentence. */
697 if (!*sptr || csum == strtol(sptr + 1, NULL, 16)) {
701 *sptr = '\0'; /* take checksum out of the buffer. */
704 gps->io.nmea=gps->io.buffer;
706 g_assert(gps->io.nmea);
707 g_debug("NMEA1 %d: (%s)", gps->io.nmea_cnt, gps->io.nmea);
711 /* There was a checksum, and it was bad. */
712 g_printerr("%s: Bad checksum in NMEA sentence:\n%s\n", __PRETTY_FUNCTION__, gps->io.buffer);
716 /* If eol is at or after (_gps_read_buf_curr - 1) */
717 if (eol >= (gps->io.curr - 1)) {
718 /* Last read was a newline - reset read buffer */
719 gps->io.curr = gps->io.buffer;
720 *gps->io.curr = '\0';
722 /* Move the next line to the front of the buffer. */
723 memmove(gps->io.buffer, eol + 1, gps->io.curr - eol); /* include terminating 0 */
724 /* Subtract _curr so that it's pointing at the new \0. */
725 gps->io.curr -= (eol - gps->io.buffer + 1);
731 case G_IO_STATUS_ERROR:
732 case G_IO_STATUS_EOF:
734 gps_connect_later(gps);
736 if (gps->io.errors>10) {
737 if (gps->connection_error==NULL)
740 gps->connection_error(gps, "GPS data read error.");
745 case G_IO_STATUS_AGAIN:
747 if (gps->io.again>20) {
749 gps_connect_later(gps);
750 if (gps->connection_error==NULL)
753 gps->connection_error(gps, "GPS connection deadlock.");
759 g_assert_not_reached();