summaryrefslogtreecommitdiff
path: root/gpxlogger.c
diff options
context:
space:
mode:
authorEric S. Raymond <esr@thyrsus.com>2009-02-25 03:14:14 +0000
committerEric S. Raymond <esr@thyrsus.com>2009-02-25 03:14:14 +0000
commite7b6c3196ef042752dbf8da4613dc0ca79e3875f (patch)
tree380f300b6e116168c77ef239231727707629fcf2 /gpxlogger.c
parent0db635fe3c84d428ae796016a5b15b88917bdae7 (diff)
downloadgpsd-e7b6c3196ef042752dbf8da4613dc0ca79e3875f.tar.gz
Refactor in preparation for merging cgpxlogger.
Diffstat (limited to 'gpxlogger.c')
-rw-r--r--gpxlogger.c160
1 files changed, 94 insertions, 66 deletions
diff --git a/gpxlogger.c b/gpxlogger.c
index 4a3c7b55..5af43f53 100644
--- a/gpxlogger.c
+++ b/gpxlogger.c
@@ -2,21 +2,20 @@
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
+#include <stdio.h>
#include <syslog.h>
#include <math.h>
#include <time.h>
#include <signal.h>
-#include <glib.h>
-#include <dbus/dbus.h>
-#include <dbus/dbus-glib-lowlevel.h>
-#include <dbus/dbus-glib.h>
-
-#include <glib/gprintf.h>
#include "gpsd_config.h"
#include "gps.h"
-DBusConnection* connection;
+/**************************************************************************
+ *
+ * Transport-layer-independent functions
+ *
+ **************************************************************************/
static char *author = "Amaury Jacquot";
static char *copyright = "BSD or GPL v 2.0";
@@ -25,14 +24,19 @@ static bool intrack = false;
static bool first = true;
static time_t tracklimit = 5; /* seconds */
-static struct gps_fix_t gpsfix;
-static time_t int_time, old_int_time;
-static char devname[BUFSIZ];
-
-static void print_gpx_trk_start (void)
+static void print_gpx_header(void)
{
- (void)printf(" <trk>\n");
- (void)printf(" <trkseg>\n");
+ (void)printf("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
+ (void)printf("<gpx version=\"1.1\" creator=\"navsys logger\"\n");
+ (void)printf(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n");
+ (void)printf(" xmlns=\"http://www.topografix.com/GPX/1.1\"\n");
+ (void)printf(" xsi:schemaLocation=\"http://www.topografix.com/GPS/1/1\n");
+ (void)printf(" http://www.topografix.com/GPX/1/1/gpx.xsd\">\n");
+ (void)printf(" <metadata>\n");
+ (void)printf(" <name>NavSys GPS logger dump</name>\n");
+ (void)printf(" <author>%s</author>\n", author);
+ (void)printf(" <copyright>%s</copyright>\n", copyright);
+ (void)printf(" </metadata>\n");
(void)fflush(stdout);
}
@@ -43,6 +47,66 @@ static void print_gpx_trk_end (void)
(void)fflush(stdout);
}
+static void print_gpx_footer (void)
+{
+ if (intrack)
+ print_gpx_trk_end();
+ (void)printf("</gpx>\n");
+ (void)fclose(stdout);
+}
+
+static void print_gpx_trk_start (void)
+{
+ (void)printf(" <trk>\n");
+ (void)printf(" <trkseg>\n");
+ (void)fflush(stdout);
+}
+
+static void print_fix(struct gps_fix_t *fix, struct tm *time)
+{
+ (void)fprintf(stdout,
+ " <trkpt lat=\"%f\" lon=\"%f\">\n",
+ fix->latitude, fix->longitude);
+ (void)fprintf(stdout,
+ " <ele>%f</ele>\n",
+ fix->altitude);
+ (void)fprintf(stdout, " <time>%04d-%02d-%02dT%02d:%02d:%02dZ</time>\n",
+ time->tm_year+1900, time->tm_mon+1, time->tm_mday,
+ time->tm_hour, time->tm_min, time->tm_sec);
+ if (fix->mode==MODE_NO_FIX)
+ (void)fprintf (stdout, " <fix>none</fix>\n");
+ else
+ (void)fprintf (stdout, " <fix>%dd</fix>\n", fix->mode);
+ (void)fprintf(stdout, " </trkpt>\n");
+ (void)fflush (stdout);
+}
+
+static void quit_handler (int signum) {
+ syslog (LOG_INFO, "exiting, signal %d received", signum);
+ print_gpx_footer ();
+ exit (0);
+}
+
+#ifdef DBUS_ENABLE
+/**************************************************************************
+ *
+ * Doing it with D-Bus
+ *
+ **************************************************************************/
+
+#include <glib.h>
+#include <dbus/dbus.h>
+#include <dbus/dbus-glib-lowlevel.h>
+#include <dbus/dbus-glib.h>
+
+#include <glib/gprintf.h>
+
+DBusConnection* connection;
+
+static struct gps_fix_t gpsfix;
+static time_t int_time, old_int_time;
+static char devname[BUFSIZ];
+
static DBusHandlerResult handle_gps_fix (DBusMessage* message)
{
DBusError error;
@@ -94,54 +158,12 @@ static DBusHandlerResult handle_gps_fix (DBusMessage* message)
}
old_int_time = int_time;
- (void)fprintf(stdout,
- " <trkpt lat=\"%f\" lon=\"%f\">\n",
- gpsfix.latitude, gpsfix.longitude);
- (void)fprintf(stdout,
- " <ele>%f</ele>\n",
- gpsfix.altitude);
gmtime_r(&(int_time), &time);
- (void)fprintf(stdout, " <time>%04d-%02d-%02dT%02d:%02d:%02dZ</time>\n",
- time.tm_year+1900, time.tm_mon+1, time.tm_mday,
- time.tm_hour, time.tm_min, time.tm_sec);
- if (gpsfix.mode==1)
- (void)fprintf (stdout, " <fix>none</fix>\n");
- else
- (void)fprintf (stdout, " <fix>%dd</fix>\n", gpsfix.mode);
- (void)fprintf(stdout, " </trkpt>\n");
- (void)fflush (stdout);
+ print_fix(&gpsfix, &time);
}
return DBUS_HANDLER_RESULT_HANDLED;
}
-static void print_gpx_header (void) {
- (void)printf("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
- (void)printf("<gpx version=\"1.1\" creator=\"navsys logger\"\n");
- (void)printf(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n");
- (void)printf(" xmlns=\"http://www.topografix.com/GPX/1.1\"\n");
- (void)printf(" xsi:schemaLocation=\"http://www.topografix.com/GPS/1/1\n");
- (void)printf(" http://www.topografix.com/GPX/1/1/gpx.xsd\">\n");
- (void)printf(" <metadata>\n");
- (void)printf(" <name>NavSys GPS logger dump</name>\n");
- (void)printf(" <author>%s</author>\n", author);
- (void)printf(" <copyright>%s</copyright>\n", copyright);
- (void)printf(" </metadata>\n");
- (void)fflush(stdout);
-}
-
-static void print_gpx_footer (void) {
- if (intrack)
- print_gpx_trk_end();
- (void)printf("</gpx>\n");
- (void)fclose(stdout);
-}
-
-static void quit_handler (int signum) {
- syslog (LOG_INFO, "exiting, signal %d received", signum);
- print_gpx_footer ();
- exit (0);
-}
-
/*
* Message dispatching function
*
@@ -161,19 +183,11 @@ static DBusHandlerResult signal_handler (
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
-int main (int argc, char** argv)
+static int dbus_mainloop(void)
{
GMainLoop* mainloop;
DBusError error;
- /* initializes the gpsfix data structure */
- gps_clear_fix(&gpsfix);
-
- /* catch all interesting signals */
- signal (SIGTERM, quit_handler);
- signal (SIGQUIT, quit_handler);
- signal (SIGINT, quit_handler);
-
//openlog ("gpxlogger", LOG_PID | LOG_NDELAY , LOG_DAEMON);
//syslog (LOG_INFO, "---------- STARTED ----------");
@@ -204,3 +218,17 @@ int main (int argc, char** argv)
g_main_loop_run (mainloop);
return 0;
}
+#endif /* DBUS_ENABLE */
+
+int main (int argc, char** argv)
+{
+ /* initializes the gpsfix data structure */
+ gps_clear_fix(&gpsfix);
+
+ /* catch all interesting signals */
+ signal (SIGTERM, quit_handler);
+ signal (SIGQUIT, quit_handler);
+ signal (SIGINT, quit_handler);
+
+ return dbus_mainloop();
+}