From 6212b671c371a8398bdaf1fa553cbb992fcc99a0 Mon Sep 17 00:00:00 2001 From: Fabrice Bellet Date: Sat, 21 Mar 2015 23:35:56 +0100 Subject: [PATCH] port-serial: add a fake mode to debug gps nmea traces --- src/mm-port-serial.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/src/mm-port-serial.c b/src/mm-port-serial.c index 3af1b8e..cb4f9ee 100644 --- a/src/mm-port-serial.c +++ b/src/mm-port-serial.c @@ -15,6 +15,7 @@ */ #define _GNU_SOURCE /* for strcasestr() */ +#define FAKE_NMEA #include #include @@ -34,6 +35,9 @@ #include #include "mm-port-serial.h" +#ifdef FAKE_NMEA +#include "mm-port-serial-gps.h" +#endif #include "mm-log.h" static gboolean port_serial_queue_process (gpointer data); @@ -873,6 +877,54 @@ parse_response (MMPortSerial *self, return MM_PORT_SERIAL_GET_CLASS (self)->parse_response (self, response, error); } +#ifdef FAKE_NMEA +static gboolean +fake_nmea (gpointer user_data) +{ + MMPortSerial *self = user_data; + static int fd = 0; + char buf[SERIAL_BUF_SIZE + 1]; + gsize bytes_read; + GError *error = NULL; + + if (fd == 0) { + fd = open (getenv("FAKE_NMEA_FILE"), O_NONBLOCK|O_RDONLY); + if (fd == -1) + fd = 0; + } + if (fd) { + bytes_read = 0; + while (read (fd, buf + bytes_read, 1) == 1) { + if (buf[bytes_read] == '\n' || buf[bytes_read] == '\r') + break; + bytes_read++; + } + if (bytes_read == 0) + return TRUE; + + buf[bytes_read++] = '\r'; + buf[bytes_read++] = '\n'; + buf[bytes_read] = 0; + + serial_debug (self, "<--", buf, bytes_read); + if (self->priv->response->len) + g_byte_array_remove_range (self->priv->response, 0, self->priv->response->len); + g_byte_array_append (self->priv->response, (const guint8 *) buf, bytes_read); + + /* Parse response. Returns TRUE either if an error is provided or if + * we really have the response to process. */ + if (parse_response (self, self->priv->response, &error)) { + /* Reset number of consecutive timeouts only here */ + self->priv->n_consecutive_timeouts = 0; + /* Process response retrieved */ + port_serial_got_response (self, error); + g_clear_error (&error); + } + } + return TRUE; +} +#endif + static gboolean common_input_available (MMPortSerial *self, GIOCondition condition) @@ -990,6 +1042,10 @@ socket_input_available (GSocket *socket, return common_input_available (MM_PORT_SERIAL (data), condition); } +#ifdef FAKE_NMEA +static guint timeout = 0; +#endif + static void data_watch_enable (MMPortSerial *self, gboolean enable) { @@ -1010,6 +1066,21 @@ data_watch_enable (MMPortSerial *self, gboolean enable) } if (enable) { +#ifdef FAKE_NMEA + if (MM_IS_PORT_SERIAL_GPS(self) && getenv("FAKE_NMEA_FILE")) { + guint interval = 10; + char *str = getenv("FAKE_NMEA_INTERVAL"); + if (str) { + interval = atoi (str); + if (interval < 10) interval = 10; + if (interval > 60000) interval = 60000; + } + if (timeout == 0) + timeout = g_timeout_add (interval, + fake_nmea, + self); + } else +#endif if (self->priv->iochannel) { self->priv->iochannel_id = g_io_add_watch (self->priv->iochannel, G_IO_IN | G_IO_ERR | G_IO_HUP, @@ -1303,6 +1374,14 @@ _close_internal (MMPortSerial *self, gboolean force) mm_port_serial_flash_cancel (self); +#ifdef FAKE_NMEA + if (MM_IS_PORT_SERIAL_GPS(self)) { + if (timeout > 0) + g_source_remove (timeout); + timeout = 0; + } +#endif + if (self->priv->iochannel || self->priv->socket) { GTimeVal tv_start, tv_end; struct serial_struct sinfo = { 0 }; -- 2.1.0