]> err.no Git - mapper/commitdiff
Make the TCP connection work for real. Tested to work with gpsd now.
authorKaj-Michael Lang <milang@onion.tal.org>
Fri, 1 Feb 2008 17:18:44 +0000 (19:18 +0200)
committerKaj-Michael Lang <milang@onion.tal.org>
Fri, 1 Feb 2008 17:18:44 +0000 (19:18 +0200)
src/gps.c

index 3e4c63cc64fe5788e6530fe7025d5e5a3f7c4c51..bdfa799d432603f8e0555f83fb049998c55f971a 100644 (file)
--- a/src/gps.c
+++ b/src/gps.c
@@ -38,6 +38,9 @@
 #include <glib/gstdio.h>
 #include <dbus/dbus.h>
 #include <dbus/dbus-glib.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
 #include <fcntl.h>
 
 #include "gps.h"
@@ -206,7 +209,7 @@ void
 gps_connect_later(Gps *gps)
 {
 g_assert(gps);
-g_assert(gps->io.clater_sid!=0);
+g_assert(gps->io.clater_sid==0);
 gps->io.clater_sid=g_timeout_add(1000, (GSourceFunc)gps_connect_now, gps);
 }
 
@@ -319,6 +322,7 @@ gps_disconnect(Gps *gps)
 {
 g_assert(gps);
 
+g_debug("GPS: Disconnecting from gps");
 /* Remove watches. */
 if (gps->io.clater_sid) {
        g_source_remove(gps->io.clater_sid);
@@ -339,6 +343,7 @@ if (gps->io.input_sid) {
 
 /* Destroy the GIOChannel object. */
 if (gps->io.channel) {
+       g_debug("GPS: Shutting down IO channel");
        g_io_channel_shutdown(gps->io.channel, FALSE, NULL);
        g_io_channel_unref(gps->io.channel);
        gps->io.channel=NULL;
@@ -346,6 +351,7 @@ if (gps->io.channel) {
 
 /* Close the file descriptor. */
 if (gps->io.fd!=-1) {
+       g_debug("GPS: Closing fd %d", gps->io.fd);
        close(gps->io.fd);
        gps->io.fd=-1;
 }
@@ -353,6 +359,8 @@ if (gps->io.fd!=-1) {
 #ifdef WITH_GPSBT
 if (gps->io.type==GPS_IO_GPSD) {
        gint r;
+
+       g_debug("GPS: Requesting gpsd shutdown using gpsbt");
        r=gpsbt_stop(&ctx);
        if (r!=0)
                g_warning("Failed to stop gpsd\n");
@@ -363,6 +371,7 @@ if (gps->io.type==GPS_IO_GPSD) {
 if (gps->io.type==GPS_IO_HILDON_DBUS && gps->io.rfcomm_req_proxy) {
        GError *error = NULL;
 
+       g_debug("GPS: Requesting rfcomm disconnection");
        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);
        error = NULL;
        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);
@@ -437,12 +446,14 @@ gps->io.last=gps->io.buffer+GPS_READ_BUF_SIZE-1;
  * Connect file descriptor to channel and add watches. 
  */
 static void
-gps_read_channel_connect(Gps *gps)
+gps_connect_channel_connect(Gps *gps)
 {
+g_assert(gps->io.channel==NULL);
 gps->io.channel=g_io_channel_unix_new(gps->io.fd);
 g_io_channel_set_encoding(gps->io.channel, NULL, NULL);
 g_io_channel_set_flags(gps->io.channel, G_IO_FLAG_NONBLOCK, NULL);
-gps->io.error_sid=g_io_add_watch_full(gps->io.channel, G_PRIORITY_HIGH_IDLE, G_IO_ERR | G_IO_HUP, gps_channel_cb_error, gps, NULL);
+g_io_channel_set_buffered(gps->io.channel, FALSE);
+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);
 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);
 gps->io.clater_sid=0;
 }
@@ -480,9 +491,13 @@ switch (gps->io.type) {
                                gps_connect_later(gps);
                                return FALSE;
                        }
-                       memcpy(&gps->io.rcvr_addr_in.sin_addr.s_addr, gps->io.address, strlen(gps->io.address));
+                       g_debug("TCP: Preparing to connect to %s:%d", gps->io.address, gps->io.port);
                        gps->io.rcvr_addr_in.sin_family = AF_INET;
                        gps->io.rcvr_addr_in.sin_port = htons(gps->io.port);
+                       if (inet_pton(AF_INET, gps->io.address, &gps->io.rcvr_addr_in.sin_addr.s_addr)<1) {
+                               perror("TCP:");
+                               return FALSE;
+                       }
                } else {
                        return FALSE;
                }
@@ -494,6 +509,8 @@ switch (gps->io.type) {
                        if (r!=0) {
                                g_warning("Failed to start gpsd");
                                return FALSE;
+                       } else {
+                               /* */
                        }
                }
 #endif
@@ -556,7 +573,7 @@ switch (gps->io.type) {
 }
 
 gps_read_buffer_prepare(gps);
-gps_read_channel_connect(gps);
+gps_connect_channel_connect(gps);
 gps_connect(gps);
 return FALSE;
 }
@@ -565,12 +582,13 @@ return FALSE;
  * Connection callback
  */
 static gboolean
-gps_channel_cb_connect(GIOChannel * src, GIOCondition condition, gpointer data)
+gps_channel_cb_connect(GIOChannel *src, GIOCondition condition, gpointer data)
 {
 gint error, size = sizeof(error);
 Gps *gps=(Gps *)data;
 
 g_assert(data);
+g_assert(gps->io.channel);
 
 gps->io.curr=gps->io.buffer;
 gps->io.last=gps->io.buffer+GPS_READ_BUF_SIZE-1;
@@ -591,9 +609,15 @@ switch (gps->io.type) {
                if (gps->io.type==GPS_IO_GPSD) {
                        GIOStatus status;
                        gsize written;
-                       status=g_io_channel_write_chars(gps->io.channel, GPSD_NMEA, -1, &written, NULL);
-                       if (status!=G_IO_STATUS_NORMAL || written!=sizeof(GPSD_NMEA)) {
-                               g_printerr("Failed to set gpsd to NMEA mode\n");
+                       GError *error = NULL;
+
+                       g_debug("GPS: Requesting NMEA mode from GPSD using cmd %s", GPSD_NMEA);
+                       status=g_io_channel_write_chars(gps->io.channel, GPSD_NMEA, strlen(GPSD_NMEA), &written, &error);
+                       if (status==G_IO_STATUS_NORMAL && written==strlen(GPSD_NMEA)) {
+                               g_debug("GPS: NMEA mode set (%d %d)", written, status);
+                       } else {
+                               g_printerr("Failed to set gpsd to NMEA mode: %d %d %s\n",status, written, error->message);
+                               g_error_free(error);
                                gps_disconnect(gps);
                                gps_connect_later(gps);
                                gps->io.connect_sid=0;
@@ -624,9 +648,12 @@ gps_channel_cb_error(GIOChannel *src, GIOCondition condition, gpointer data)
 Gps *gps=(Gps *)data;
 
 g_assert(data);
+g_debug("GPS: Channel error %d", condition);
+
 /* An error has occurred - re-connect(). */
 gps_disconnect(gps);
 
+/* Do error cb */
 if (gps->connection_error!=NULL)
        gps->connection_error(gps, "GPS Connection error");
 
@@ -634,6 +661,7 @@ if (gps->io.conn > RCVR_OFF) {
        gps_conn_set_state(gps, RCVR_DOWN);
        gps_connect_later(gps);
 }
+
 return FALSE;
 }