Automatically re-open gpsd connection after an inactivity timeout
authorBenoit Papillault <benoit.papillault@free.fr>
Tue, 28 Jun 2011 14:12:53 +0000 (16:12 +0200)
committerBenoit Papillault <benoit.papillault@free.fr>
Tue, 28 Jun 2011 14:12:53 +0000 (16:12 +0200)
Conditionnal compilation for several libgps version (since the API has changed)

mygps.c

diff --git a/mygps.c b/mygps.c
index c996c99..d5e5e97 100644 (file)
--- a/mygps.c
+++ b/mygps.c
@@ -8,8 +8,11 @@
 */
 
 #include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 #include <gps.h>
 #include <errno.h>
+#include <sys/select.h>
 
 struct timeval tv_now;
 
@@ -175,15 +178,15 @@ struct gps_handler_t {
        { ATTITUDE_SET,         NULL },
        { POLICY_SET,           gps_policy_set },
        { SATELLITE_SET,        gps_satellite_set },
-       { RAW_SET,              NULL },
-       { USED_SET,             NULL },
+       /*{ RAW_SET,            NULL },*/
+       /*{ USED_SET,           NULL },*/
        { SPEEDERR_SET,         gps_speederr_set },
        { TRACKERR_SET,         gps_trackerr_set },
        { CLIMBERR_SET,         gps_climberr_set },
        { DEVICE_SET,           gps_device_set },
        { DEVICELIST_SET,       gps_devicelist_set },
        { DEVICEID_SET,         NULL },
-       { ERROR_SET,            NULL },
+       /*{ ERROR_SET,          NULL },*/
        { RTCM2_SET,            NULL },
        { RTCM3_SET,            NULL },
        { AIS_SET,              NULL },
@@ -192,49 +195,97 @@ struct gps_handler_t {
 
 #define sizetab(tab) (sizeof(tab)/sizeof(tab[0]))
 
-int main() {
-       struct gps_data_t *gps;
-
-       printf("Compiled libgps API version %d.%d\n",
-              GPSD_API_MAJOR_VERSION, GPSD_API_MINOR_VERSION);
+void usage() {
+       fprintf(stderr,"usage: mygps [--data]\n");
+       exit (-1);
+}
 
-       /* NULL, NULL matches localhost and the default GPSD port */
-       if ((gps = gps_open(NULL, NULL)) == NULL) {
-               fprintf(stderr, "gps_open: %s\n", gps_errstr(errno));
-               return -1;
+int main(int argc, const char *argv[]) {
+       int i;
+       struct gps_data_t gpsdata, *gps = &gpsdata;
+       int data_mode = 0;
+
+       for (i=1; i<argc; i++) {
+               if (strcmp(argv[i], "--data")==0) {
+                       data_mode = 1;
+               } else {
+                       usage();
+               }
        }
 
-       gps_stream(gps, WATCH_ENABLE, NULL);
+       printf("Compiled libgps API version %d.%d\n",
+              GPSD_API_MAJOR_VERSION, GPSD_API_MINOR_VERSION);
 
        for (;;) {
-               int i;
 
-               /* Depending on the version of libgps, you need to call gps_read or gps_poll */
-               if (gps_poll(gps) < 0) {
-                       perror("gps_read");
-                       break;
+               /* NULL, NULL matches localhost and the default GPSD port */
+#if GPS_API_MAJOR_VERSION < 5
+               if ((gps = gps_open(NULL, NULL)) == NULL) {
+#endif
+#if GPSD_API_MAJOR_VERSION == 5
+               if (gps_open(NULL, NULL, &gpsdata) != 0) {
+#endif
+                       fprintf(stderr, "gps_open: %s\n", gps_errstr(errno));
+                       return -1;
+               }
+
+               if (gps_stream(gps, WATCH_ENABLE, NULL) != 0) {
+                       fprintf(stderr, "gps_stream() failed\n");
+                       return -1;
                }
 
-               gettimeofday(&tv_now, NULL);
-               printf("--- %lu.%06lu ---\n", 
-                      tv_now.tv_sec, tv_now.tv_usec);
+               for (;;) {
+                       int i, r;
+                       fd_set fdr;
+                       struct timeval tv = { 10, 0 };
+                       gps_mask_t set;
+
+                       /* wait for data of gps->gps_fd */
+
+                       FD_ZERO(&fdr);
+                       FD_SET(gps->gps_fd, &fdr);
 
-               for (i=0; i<sizetab(gps_handlers); i++) {
-                       if (gps->set & gps_handlers[i].set &&
-                           gps_handlers[i].handler) {
-                               gps->set &= ~gps_handlers[i].set;
-                               gps_handlers[i].handler(gps);
+                       r = select(FD_SETSIZE, &fdr, NULL, NULL, &tv);
+                       if (r == 0) {
+                               printf("Time-out reading from GPS daemon\n");
+                               break;
                        }
-               }
-               fflush(stdout);
 
-               if (gps->set) {
-                       printf("=> gps->set not decoded : 0x%x\n", gps->set);
-               }
-       }
+                       /* Depending on the version of libgps, you need to
+                          call gps_read() or gps_poll() */
+                       if (gps_read(gps) < 0) {
+                               perror("gps_read");
+                               break;
+                       }
+
+                       gettimeofday(&tv_now, NULL);
+                       printf("--- %lu.%06lu ---\n", 
+                              tv_now.tv_sec, tv_now.tv_usec);
+
+                       set = gps->set;
+                       for (i=0; i<sizetab(gps_handlers); i++) {
+                               if (set & gps_handlers[i].set &&
+                                   gps_handlers[i].handler) {
+                                       set &= ~gps_handlers[i].set;
+                                       gps_handlers[i].handler(gps);
+                               }
+                       }
 
+                       if (data_mode) {
+                               printf("data %u.%06lu\n",
+                                      tv_now.tv_sec, tv_now.tv_usec);
+                       }
+
+                       fflush(stdout);
+
+                       if (set) {
+                               printf("=> gps->set not decoded : 0x%x\n",
+                                      set);
+                       }
+               }
 
-       gps_close(gps);
+               gps_close(gps);
+       }
 
        return 0;
 }