#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"
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);
}
{
g_assert(gps);
+g_debug("GPS: Disconnecting from gps");
/* Remove watches. */
if (gps->io.clater_sid) {
g_source_remove(gps->io.clater_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;
/* 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;
}
#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");
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);
* 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;
}
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;
}
if (r!=0) {
g_warning("Failed to start gpsd");
return FALSE;
+ } else {
+ /* */
}
}
#endif
}
gps_read_buffer_prepare(gps);
-gps_read_channel_connect(gps);
+gps_connect_channel_connect(gps);
gps_connect(gps);
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;
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;
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");
gps_conn_set_state(gps, RCVR_DOWN);
gps_connect_later(gps);
}
+
return FALSE;
}