summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gps.h9
-rw-r--r--gpxlogger.c21
-rw-r--r--libgps_core.c26
-rw-r--r--libgps_dbus.c4
-rw-r--r--libgps_shm.c26
-rw-r--r--libgps_sock.c12
6 files changed, 75 insertions, 23 deletions
diff --git a/gps.h b/gps.h
index 7905be74..4441660e 100644
--- a/gps.h
+++ b/gps.h
@@ -1727,6 +1727,8 @@ extern int gps_read(/*@out@*/struct gps_data_t *);
extern int gps_unpack(char *, struct gps_data_t *);
extern bool gps_waiting(const struct gps_data_t *, int);
extern int gps_stream(struct gps_data_t *, unsigned int, /*@null@*/void *);
+extern int gps_mainloop(struct gps_data_t *, int timeout,
+ int (*)(struct gps_data_t *, bool));
extern const char /*@observer@*/ *gps_data(const struct gps_data_t *);
extern const char /*@observer@*/ *gps_errstr(const int);
@@ -1739,15 +1741,18 @@ extern bool gps_sock_waiting(const struct gps_data_t *, int);
extern int gps_sock_stream(struct gps_data_t *, unsigned int, /*@null@*/void *);
extern const char /*@observer@*/ *gps_sock_data(const struct gps_data_t *);
extern int gps_sock_mainloop(struct gps_data_t *, int timeout,
- void (*)(struct gps_data_t *));
+ int (*)(struct gps_data_t *, bool));
extern int gps_shm_open(/*@out@*/struct gps_data_t *);
extern void gps_shm_close(struct gps_data_t *);
extern int gps_shm_read(struct gps_data_t *);
+extern int gps_shm_mainloop(struct gps_data_t *, int timeout,
+ int (*)(struct gps_data_t *, bool));
extern int gps_dbus_open(struct gps_data_t *);
+extern bool gps_shm_waiting(const struct gps_data_t *, int);
extern int gps_dbus_mainloop(struct gps_data_t *, int timeout,
- void (*)(struct gps_data_t *));
+ int (*)(struct gps_data_t *, bool));
/* dependencies on struct gpsdata_t end hrere */
diff --git a/gpxlogger.c b/gpxlogger.c
index 18c128a8..f9e35bb5 100644
--- a/gpxlogger.c
+++ b/gpxlogger.c
@@ -126,7 +126,7 @@ static void print_fix(struct gps_data_t *gpsdata, double time)
(void)fflush(logfile);
}
-static void conditionally_log_fix(struct gps_data_t *gpsdata)
+static int conditionally_log_fix(struct gps_data_t *gpsdata, bool fix UNUSED)
{
static double int_time, old_int_time;
static double old_lat, old_lon;
@@ -134,14 +134,14 @@ static void conditionally_log_fix(struct gps_data_t *gpsdata)
int_time = gpsdata->fix.time;
if ((int_time == old_int_time) || gpsdata->fix.mode < MODE_2D)
- return;
+ return 0;
/* may not be worth logging if we've moved only a very short distance */
if (minmove>0 && !first && earth_distance(
gpsdata->fix.latitude,
gpsdata->fix.longitude,
old_lat, old_lon) < minmove)
- return;
+ return 0;
/*
* Make new track if the jump in time is above
@@ -170,6 +170,8 @@ static void conditionally_log_fix(struct gps_data_t *gpsdata)
old_lon = gpsdata->fix.longitude;
}
print_fix(gpsdata, int_time);
+
+ return 0;
}
static void quit_handler(int signum)
@@ -226,17 +228,8 @@ static int socket_mainloop(void)
(void)gps_stream(&gpsdata, flags, source.device);
print_gpx_header();
- for (;;) {
- if (!gps_waiting(&gpsdata, 5000000)) {
- (void)fprintf(stderr, "%s: error while waiting\n", progname);
- break;
- } else {
- (void)gps_read(&gpsdata);
- conditionally_log_fix(&gpsdata);
- }
- }
+ gps_sock_mainloop(&gpsdata, 5000000, conditionally_log_fix);
print_gpx_footer();
- (void)gps_close(&gpsdata);
return 0;
}
/*@+mustfreefresh +compdestroy@*/
@@ -267,7 +260,7 @@ static int shm_mainloop(void)
if (status == -1)
break;
if (status > 0)
- conditionally_log_fix(&gpsdata);
+ conditionally_log_fix(&gpsdata, true);
}
print_gpx_footer();
(void)gps_close(&gpsdata);
diff --git a/libgps_core.c b/libgps_core.c
index 6ea9f976..273b3fe2 100644
--- a/libgps_core.c
+++ b/libgps_core.c
@@ -173,6 +173,32 @@ int gps_stream(struct gps_data_t *gpsdata CONDITIONALLY_UNUSED,
return status;
}
+int gps_mainloop(struct gps_data_t *gpsdata, int timeout,
+ int (*hook)(struct gps_data_t *gpsdata, bool))
+/* run a socket main loop with a specified handler */
+{
+ int status;
+
+ libgps_debug_trace((DEBUG_CALLS, "gps_mainloop() begins\n"));
+
+ /*@ -usedef -compdef -uniondef @*/
+#ifdef SHM_EXPORT_ENABLE
+ if ((intptr_t)(gpsdata->gps_fd) == -1) {
+ status = gps_shm_mainloop(gpsdata, timeout, hook);
+ }
+#endif /* SHM_EXPORT_ENABLE */
+
+#ifdef SOCKET_EXPORT_ENABLE
+ if (status == -1) {
+ status = gps_sock_mainloop(gpsdata, timeout, hook);
+ }
+#endif /* SOCKET_EXPORT_ENABLE */
+ /*@ +usedef +compdef +uniondef @*/
+
+ libgps_debug_trace((DEBUG_CALLS, "gps_mainloop() -> %d\n"));
+ return status;
+}
+
const char /*@observer@*/ *gps_data(const struct gps_data_t *gpsdata CONDITIONALLY_UNUSED)
/* return the contents of the client data buffer */
{
diff --git a/libgps_dbus.c b/libgps_dbus.c
index 56ded895..f2348d6c 100644
--- a/libgps_dbus.c
+++ b/libgps_dbus.c
@@ -128,8 +128,8 @@ int gps_dbus_open(struct gps_data_t *gpsdata)
}
int gps_dbus_mainloop(struct gps_data_t *gpsdata,
- int timeout UNUSED,
- void (*hook)(struct gps_data_t *))
+ int timeout UNUSED,
+ int (*hook)(struct gps_data_t *, bool))
/* run a DBUS main loop with a specified handler */
{
GMainLoop *mainloop;
diff --git a/libgps_shm.c b/libgps_shm.c
index c14ef4ea..61690d1c 100644
--- a/libgps_shm.c
+++ b/libgps_shm.c
@@ -51,6 +51,14 @@ int gps_shm_open(/*@out@*/struct gps_data_t *gpsdata)
return 0;
}
+bool gps_shm_waiting(const struct gps_data_t *gpsdata UNUSED,
+ int timeout UNUSED)
+/* is there input waiting from the GPS? */
+{
+ /* someday, actually check a timestamp */
+ return true;
+}
+
int gps_shm_read(struct gps_data_t *gpsdata)
/* read an update from the shared-memory segment */
{
@@ -109,6 +117,24 @@ void gps_shm_close(struct gps_data_t *gpsdata)
(void)shmdt((const void *)gpsdata->privdata);
}
+
+int gps_shm_mainloop(struct gps_data_t *gpsdata, int timeout,
+ int (*hook)(struct gps_data_t *gpsdata, bool fix))
+/* run a shm main loop with a specified handler */
+{
+ for (;;) {
+ if (!gps_shm_waiting(gpsdata, timeout)) {
+ if ((*hook)(gpsdata, false) != 0)
+ break;
+ } else {
+ (void)gps_shm_read(gpsdata);
+ if ((*hook)(gpsdata, true) != 0)
+ break;
+ }
+ }
+ return 0;
+}
+
#endif /* SHM_EXPORT_ENABLE */
/* end */
diff --git a/libgps_sock.c b/libgps_sock.c
index ae4d45c3..cc4c8234 100644
--- a/libgps_sock.c
+++ b/libgps_sock.c
@@ -566,15 +566,17 @@ int gps_sock_stream(struct gps_data_t *gpsdata, unsigned int flags,
}
int gps_sock_mainloop(struct gps_data_t *gpsdata, int timeout,
- void (*hook)(struct gps_data_t *gpsdata))
+ int (*hook)(struct gps_data_t *gpsdata, bool))
/* run a socket main loop with a specified handler */
{
for (;;) {
- if (!gps_waiting(gpsdata, timeout)) {
- return -1;
+ if (!gps_sock_waiting(gpsdata, timeout)) {
+ if ((*hook)(gpsdata, false) != 0)
+ break;
} else {
- (void)gps_read(gpsdata);
- (*hook)(gpsdata);
+ (void)gps_sock_read(gpsdata);
+ if ((*hook)(gpsdata, true) != 0)
+ break;
}
}
(void)gps_close(gpsdata);