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)
70 const GpsTypes gps_types[] = {
72 { GPS_IO_RFCOMM, "Bluetooth connection (old)", TRUE, TRUE},
74 #ifdef WITH_HILDON_DBUS_BT
75 { GPS_IO_HILDON_DBUS, "Bluetooth connection (btconn)", TRUE, TRUE },
77 #ifdef WITH_BLUEZ_DBUS_BT
78 { GPS_IO_BLUEZ_DBUS, "Bluetooth connection (new)", TRUE, TRUE},
80 { GPS_IO_GPSD, "GPSD Connection", FALSE, TRUE},
81 { GPS_IO_FILE, "File or device", FALSE, TRUE},
82 { GPS_IO_TCP, "TCP Connection", FALSE, TRUE},
84 { GPS_IO_GYPSY, "Gypsy", FALSE, TRUE},
86 { GPS_IO_SIMULATION, "Simulation (random)", FALSE, FALSE},
91 gps_get_supported_types(void)
100 gps_new(GpsIOSourceType type)
104 gps=g_slice_new0(Gps);
105 gps->data.fix=FIX_NOFIX;
107 gps->io.address=NULL;
109 gps->io.conn=RCVR_OFF;
114 gps->connection_error=NULL;
115 gps->connection_retry=NULL;
116 gps->connection_progress=NULL;
117 gps->update_location=NULL;
118 gps->update_info=NULL;
119 gps->update_satellite=NULL;
120 gps_data_integerize(&gps->data);
125 gps_set_address(Gps *gps, gchar *address, guint port)
128 g_free(gps->io.address);
130 gps->io.address=g_strdup(address);
135 gps_set_type(Gps *gps, GpsIOSourceType type)
145 g_free(gps->io.address);
146 g_slice_free(Gps, gps);
150 * Convert the float lat/lon/speed/heading data into integer units.
151 * Also calculate offsets.
154 gps_data_integerize(GpsData *gpsdata)
158 tmp=(gpsdata->heading*(1.f/180.f))*G_PI;
159 latlon2unit(gpsdata->lat, gpsdata->lon, gpsdata->unitx, gpsdata->unity);
160 gpsdata->vel_offsetx=(gint)(floor(gpsdata->speed*sin(tmp)+0.5f));
161 gpsdata->vel_offsety=-(gint)(floor(gpsdata->speed*cos(tmp)+0.5f));
164 #ifdef WITH_HILDON_DBUS_BT
166 gps_connect_response(DBusGProxy *proxy, DBusGProxyCall *call_id, Gps *gps)
168 GError *error = NULL;
169 gchar *fdpath = NULL;
171 if (gps->io.conn==RCVR_DOWN && gps->io.address) {
172 if (!dbus_g_proxy_end_call(gps->io.rfcomm_req_proxy, call_id, &error, G_TYPE_STRING, &fdpath, G_TYPE_INVALID)) {
173 if (error->domain == DBUS_GERROR && error->code == DBUS_GERROR_REMOTE_EXCEPTION) {
174 /* If we're already connected, it's not an error, unless
175 * they don't give us the file descriptor path, in which
176 * case we re-connect.*/
177 if (!strcmp(BTCOND_ERROR_CONNECTED, dbus_g_error_get_name(error)) || !fdpath) {
178 g_printerr("Caught remote method exception %s: %s", dbus_g_error_get_name(error), error->message);
181 if (gps->connection_retry==NULL)
184 if (gps->connection_retry(gps, error->message)==TRUE) {
185 gps_connect_later(gps);
187 gps_conn_set_state(gps, RCVR_OFF);
193 g_printerr("Error: %s\n", error->message);
194 gps_disconnect(_gps);
195 gps_connect_later(_gps); /* Try again later. */
199 gps_connect_fd(fdpath);
201 /* else { Looks like the middle of a disconnect. Do nothing. } */
206 * Place a request to connect about 1 second after the function is called.
209 gps_connect_later(Gps *gps)
212 g_assert(gps->io.clater_sid==0);
213 gps->io.clater_sid=g_timeout_add(1000, (GSourceFunc)gps_connect_now, gps);
217 gps_connect(Gps *gps)
219 switch (gps->io.type) {
226 return gps_connect_socket(gps);
238 * Helper to open a file or device node
241 gps_connect_file(Gps *gps, gchar *file)
243 if (-1==(gps->io.fd=open(file, O_RDONLY))) {
245 gps_connect_later(gps);
253 * Helper to connect to a socket file descriptor.
257 gps_connect_socket(Gps *gps)
262 switch (gps->io.type) {
265 g_debug("RFCOMM: %d", gps->io.fd);
266 r=connect(gps->io.fd, (struct sockaddr *)&gps->io.rcvr_addr_rc, sizeof(gps->io.rcvr_addr_rc));
271 g_debug("TCP: %d", gps->io.fd);
272 r=connect(gps->io.fd, (struct sockaddr *)&gps->io.rcvr_addr_in, sizeof(gps->io.rcvr_addr_in));
278 g_debug("GPS: Error %d", e);
280 /* The socket is non blocking so handle it */
287 g_printerr("*** Connection in progress... %d %d\n", e, r);
292 g_printerr("*** Bluetooth/GPS device not found.\n");
295 set_action_activate("gps_enable", FALSE);
296 popup_error(_window, _("No bluetooth or GPS device found."));
301 /* Connection failed. Disconnect and try again later. */
302 g_printerr("### Connect failed, retrying... %d %d\n", e, r);
305 gps_connect_later(gps);
314 * Disconnect the GPS device.
315 * - Add channel callbacks are removed
316 * - Channel is close and shutdown
317 * - File descriptor is closed
318 * - Anything special for disconnecting is done
321 gps_disconnect(Gps *gps)
325 g_debug("GPS: Disconnecting from gps");
326 /* Remove watches. */
327 if (gps->io.clater_sid) {
328 g_source_remove(gps->io.clater_sid);
329 gps->io.clater_sid=0;
331 if (gps->io.error_sid) {
332 g_source_remove(gps->io.error_sid);
335 if (gps->io.connect_sid) {
336 g_source_remove(gps->io.connect_sid);
337 gps->io.connect_sid=0;
339 if (gps->io.input_sid) {
340 g_source_remove(gps->io.input_sid);
344 /* Destroy the GIOChannel object. */
345 if (gps->io.channel) {
346 g_debug("GPS: Shutting down IO channel");
347 g_io_channel_shutdown(gps->io.channel, FALSE, NULL);
348 g_io_channel_unref(gps->io.channel);
349 gps->io.channel=NULL;
352 /* Close the file descriptor. */
353 if (gps->io.fd!=-1) {
354 g_debug("GPS: Closing fd %d", gps->io.fd);
360 if (gps->io.type==GPS_IO_GPSD) {
363 g_debug("GPS: Requesting gpsd shutdown using gpsbt");
366 g_warning("Failed to stop gpsd\n");
370 #ifdef WITH_HILDON_DBUS_BT
371 if (gps->io.type==GPS_IO_HILDON_DBUS && gps->io.rfcomm_req_proxy) {
372 GError *error = NULL;
374 g_debug("GPS: Requesting rfcomm disconnection");
375 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);
377 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);
381 #ifdef WITH_BLUEZ_DBUS_BT
382 if (gps->io.type==GPS_IO_BLUEZ_DBUS && gps->io.rfcomm_req_proxy) {
383 GError *error = NULL;
391 gps_simulate_start(Gps *gps)
395 gps->io.conn=RCVR_FIXED;
396 gps->data.fix=FIX_2D;
397 gps_data_integerize(&gps->data);
401 gps_simulate_move(Gps *gps)
403 static gdouble slat=0, slon=0;
408 if (g_random_double()<0.5) {
409 slat=g_random_double_range(-0.0003, 0.0003);
410 slon=g_random_double_range(-0.0003, 0.0003);
416 BOUND(gps->data.lat, -80.0, 80.0);
417 BOUND(gps->data.lon, -80.0, 80.0);
419 g_debug("Sim: %f %f\n", gps->data.lat, gps->data.lon);
421 gps->data.speed=1+g_random_double_range(0.1, 10.0);
422 h=calculate_course(plat, plon, gps->data.lat, gps->data.lon);
423 gps->data.heading=(h<0) ? 360+h : h;
424 gps->data.time=time(NULL);
425 gps_data_integerize(&gps->data);
426 if (gps->update_location!=NULL) {
427 gps->update_location(gps);
429 gps->data.lheading=gps->data.heading;
431 return gps->io.conn==RCVR_FIXED ? TRUE : FALSE;
435 * Reset GPS read buffer
438 gps_read_buffer_prepare(Gps *gps)
440 gps->io.curr=gps->io.buffer;
441 *gps->io.curr = '\0';
442 gps->io.last=gps->io.buffer+GPS_READ_BUF_SIZE-1;
446 * Connect file descriptor to channel and add watches.
449 gps_connect_channel_connect(Gps *gps)
451 g_assert(gps->io.channel==NULL);
452 gps->io.channel=g_io_channel_unix_new(gps->io.fd);
453 g_io_channel_set_encoding(gps->io.channel, NULL, NULL);
454 g_io_channel_set_flags(gps->io.channel, G_IO_FLAG_NONBLOCK, NULL);
455 g_io_channel_set_buffered(gps->io.channel, FALSE);
456 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);
457 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);
458 gps->io.clater_sid=0;
462 * Connect to the receiver.
463 * This method assumes that fd is -1 and _channel is NULL. If unsure, call
464 * gps_disconnect() first.
465 * Since this is an idle function, this function returns whether or not it
466 * should be called again, which is always FALSE.
468 * This function prepares for the connection, the gps_connect does the "connecting" if needed.
471 gps_connect_now(Gps *gps)
473 GError *error = NULL;
476 g_debug("GPS: Connecting GPS type %d to %s:%d\n", gps->io.type, gps->io.address, gps->io.port);
478 switch (gps->io.type) {
480 if (!gps->io.address)
482 gps_connect_file(gps, gps->io.address);
487 if (gps->io.conn<=RCVR_DOWN && gps->io.address) {
488 gps->io.fd=socket(AF_INET, SOCK_STREAM, 0);
489 if (gps->io.fd==-1) {
490 g_debug("Failed to create socket\n");
491 gps_connect_later(gps);
494 g_debug("TCP: Preparing to connect to %s:%d", gps->io.address, gps->io.port);
495 gps->io.rcvr_addr_in.sin_family = AF_INET;
496 gps->io.rcvr_addr_in.sin_port = htons(gps->io.port);
497 if (inet_pton(AF_INET, gps->io.address, &gps->io.rcvr_addr_in.sin_addr.s_addr)<1) {
505 if (gps->io.type==GPS_IO_GPSD) {
508 r=gpsbt_start(NULL, 0, 0, 0, ebuf, 128, 0, &ctx);
510 g_warning("Failed to start gpsd");
518 #ifdef WITH_HILDON_DBUS_BT
519 case GPS_IO_HILDON_DBUS:
520 if (NULL == (gps->io.dbus_conn=dbus_g_bus_get(DBUS_BUS_SYSTEM, &error))) {
521 g_printerr("Failed to get D-Bus connection.");
524 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))) {
525 g_printerr("Failed to open connection to %s.\n", BTCOND_REQ_INTERFACE);
529 if (gps->io.rfcomm_req_proxy) {
530 gboolean mybool = TRUE;
531 dbus_g_proxy_begin_call(gps->io.rfcomm_req_proxy,
532 BTCOND_RFCOMM_CONNECT_REQ, (DBusGProxyCallNotify)gps_connect_response, gps, NULL,
533 G_TYPE_STRING, gps->io.address,
534 G_TYPE_STRING, "SPP",
535 G_TYPE_BOOLEAN, &mybool,
542 if (!gps->io.address)
545 if (gps->io.conn<=RCVR_DOWN && gps->io.address) {
546 gps->io.fd=socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
548 /* If file descriptor creation failed, try again later. Note that
549 * there is no need to call rcvr_disconnect() because the file
550 * descriptor creation is the first step, so if it fails, there's
551 * nothing to clean up. */
552 if (gps->io.fd==-1) {
553 g_debug("Failed to create socket\n");
554 gps_connect_later(gps);
557 gps->io.rcvr_addr_rc.rc_family=AF_BLUETOOTH;
558 gps->io.rcvr_addr_rc.rc_channel=1;
559 str2ba(gps->io.address, &gps->io.rcvr_addr_rc.rc_bdaddr);
563 case GPS_IO_SIMULATION:
564 /* Set a periodic cb to generate random movement */
565 gps_simulate_start(gps);
566 gps->io.input_sid=g_timeout_add(1000, (GSourceFunc)gps_simulate_move, gps);
570 g_printerr("Unknown GPS connection type\n");
575 gps_read_buffer_prepare(gps);
576 gps_connect_channel_connect(gps);
582 * Connection callback
585 gps_channel_cb_connect(GIOChannel *src, GIOCondition condition, gpointer data)
587 gint error, size = sizeof(error);
588 Gps *gps=(Gps *)data;
591 g_assert(gps->io.channel);
593 gps->io.curr=gps->io.buffer;
594 gps->io.last=gps->io.buffer+GPS_READ_BUF_SIZE-1;
596 switch (gps->io.type) {
600 if ((getsockopt(gps->io.fd, SOL_SOCKET, SO_ERROR, &error, &size) || error)) {
601 g_printerr("Error connecting to receiver; retrying...\n");
603 gps_connect_later(gps);
604 gps->io.connect_sid=0;
608 /* Set gpsd to NMEA mode */
609 if (gps->io.type==GPS_IO_GPSD) {
612 GError *error = NULL;
614 g_debug("GPS: Requesting NMEA mode from GPSD using cmd %s", GPSD_NMEA);
615 status=g_io_channel_write_chars(gps->io.channel, GPSD_NMEA, strlen(GPSD_NMEA), &written, &error);
616 if (status==G_IO_STATUS_NORMAL && written==strlen(GPSD_NMEA)) {
617 g_debug("GPS: NMEA mode set (%d %d)", written, status);
619 g_printerr("Failed to set gpsd to NMEA mode: %d %d %s\n",status, written, error->message);
622 gps_connect_later(gps);
623 gps->io.connect_sid=0;
633 g_warning("Connection from non-connecting source\n");
637 g_printf("Connected to receiver!\n");
638 gps_conn_set_state(gps, RCVR_UP);
639 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);
641 gps->io.connect_sid=0;
646 gps_channel_cb_error(GIOChannel *src, GIOCondition condition, gpointer data)
648 Gps *gps=(Gps *)data;
651 g_debug("GPS: Channel error %d", condition);
653 /* An error has occurred - re-connect(). */
657 if (gps->connection_error!=NULL)
658 gps->connection_error(gps, "GPS Connection error");
660 if (gps->io.conn > RCVR_OFF) {
661 gps_conn_set_state(gps, RCVR_DOWN);
662 gps_connect_later(gps);
669 gps_channel_cb_input(GIOChannel *src, GIOCondition condition, gpointer data)
674 Gps *gps=(Gps *)data;
676 g_debug("%s(%d)\n", __PRETTY_FUNCTION__, condition);
679 status=g_io_channel_read_chars(gps->io.channel, gps->io.curr, gps->io.last-gps->io.curr, &bytes_read, NULL);
681 case G_IO_STATUS_NORMAL:
682 gps->io.curr += bytes_read;
683 *gps->io.curr = '\0'; /* append a \0 so we can read as string */
684 while ((eol = strchr(gps->io.buffer, '\n'))) {
685 gchar *sptr = gps->io.buffer + 1; /* Skip the $ */
687 if (*gps->io.buffer=='$') {
688 /* This is the beginning of a sentence; okay to parse. */
689 *eol = '\0'; /* overwrite \n with \0 */
690 while (*sptr && *sptr != '*')
693 /* If we're at a \0 (meaning there is no checksum), or if the
694 * checksum is good, then parse the sentence. */
695 if (!*sptr || csum == strtol(sptr + 1, NULL, 16)) {
699 *sptr = '\0'; /* take checksum out of the buffer. */
702 gps->io.nmea=gps->io.buffer;
704 g_assert(gps->io.nmea);
705 g_debug("NMEA1 %d: (%s)", gps->io.nmea_cnt, gps->io.nmea);
709 /* There was a checksum, and it was bad. */
710 g_printerr("%s: Bad checksum in NMEA sentence:\n%s\n", __PRETTY_FUNCTION__, gps->io.buffer);
714 /* If eol is at or after (_gps_read_buf_curr - 1) */
715 if (eol >= (gps->io.curr - 1)) {
716 /* Last read was a newline - reset read buffer */
717 gps->io.curr = gps->io.buffer;
718 *gps->io.curr = '\0';
720 /* Move the next line to the front of the buffer. */
721 memmove(gps->io.buffer, eol + 1, gps->io.curr - eol); /* include terminating 0 */
722 /* Subtract _curr so that it's pointing at the new \0. */
723 gps->io.curr -= (eol - gps->io.buffer + 1);
729 case G_IO_STATUS_ERROR:
730 case G_IO_STATUS_EOF:
732 gps_connect_later(gps);
734 if (gps->io.errors>10) {
735 if (gps->connection_error==NULL)
738 gps->connection_error(gps, "GPS data read error.");
743 case G_IO_STATUS_AGAIN:
745 if (gps->io.again>20) {
747 gps_connect_later(gps);
748 if (gps->connection_error==NULL)
751 gps->connection_error(gps, "GPS connection deadlock.");
757 g_assert_not_reached();