X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fgps.c;h=1218e192f7b7b92058893d24ed91fddcbccffd6b;hb=33ee0bb4ae03ee848f04e4763474bba67fbbc530;hp=e793cfa6c744d1920b1c7a0598d6892d5ee6022e;hpb=621fa8259ea9df412a9b45c3e65ddcf34dd9d9d5;p=collectd.git diff --git a/src/gps.c b/src/gps.c index e793cfa6..1218e192 100644 --- a/src/gps.c +++ b/src/gps.c @@ -32,8 +32,10 @@ #define GPS_DEFAULT_HOST "localhost" #define GPS_DEFAULT_PORT "2947" -#define GPS_DEFAULT_TIMEOUT TIME_T_TO_CDTIME_T (15) +#define GPS_DEFAULT_TIMEOUT TIME_T_TO_CDTIME_T (0.015) #define GPS_DEFAULT_PAUSE TIME_T_TO_CDTIME_T (1) +#define GPS_MAX_ERROR 100 +#define GPS_CONFIG "?WATCH={\"enable\":true,\"json\":true,\"nmea\":false}\r\n" #include #include @@ -59,32 +61,43 @@ static cgps_config_t config; static cgps_data_t data = {NAN, NAN, NAN, NAN}; static pthread_mutex_t data_lock = PTHREAD_MUTEX_INITIALIZER; +static struct gps_data_t gpsd_conn; /** * Thread reading from gpsd. */ -static void * gps_collectd_thread (void * pData) +static void * cgps_thread (void * pData) { - struct gps_data_t conn; + int err_count; while (1) { - int status = gps_open (config.host, config.port, &conn); + err_count = 0; + +#if GPSD_API_MAJOR_VERSION > 4 + int status = gps_open (config.host, config.port, &gpsd_conn); +#else + int status = gps_open_r (config.host, config.port, &gpsd_conn); +#endif if (status < 0) { - WARNING ("gps plugin: Connecting to %s:%s failed: %s", + WARNING ("gps plugin: connecting to %s:%s failed: %s", config.host, config.port, gps_errstr (status)); sleep (60); continue; } - gps_stream (&conn, WATCH_ENABLE | WATCH_JSON | WATCH_NEWSTYLE, NULL); - gps_send (&conn, "?WATCH={\"enable\":true,\"json\":true,\"nmea\":false}\r\n"); + gps_stream (&gpsd_conn, WATCH_ENABLE | WATCH_JSON | WATCH_NEWSTYLE, NULL); + gps_send (&gpsd_conn, GPS_CONFIG); while (1) { +#if GPSD_API_MAJOR_VERSION > 4 long timeout_us = CDTIME_T_TO_US (config.timeout); - if (!gps_waiting (&conn, (int) timeout_us)) + if (!gps_waiting (&gpsd_conn, (int) timeout_us )) +#else + if (!gps_waiting (&gpsd_conn)) +#endif { struct timespec pause_ns; CDTIME_T_TO_TIMESPEC (config.pause, &pause_ns); @@ -92,24 +105,42 @@ static void * gps_collectd_thread (void * pData) continue; } - if (gps_read (&conn) == -1) + if (gps_read (&gpsd_conn) == -1) { - WARNING ("gps plugin: incorrect data!"); + WARNING ("gps plugin: incorrect data! (err_count: %d)", err_count); + err_count++; + + if (err_count > GPS_MAX_ERROR) + { + // Server is not responding ... + if (gps_send (&gpsd_conn, GPS_CONFIG) == -1) + { + WARNING ("gps plugin: gpsd seems to be down, reconnecting"); + gps_close (&gpsd_conn); + break; + } + // Server is responding ... + else + { + err_count = 0; + } + } + continue; } pthread_mutex_lock (&data_lock); // Number of sats in view: - data.sats_used = (gauge_t) conn.satellites_used; - data.sats_visible = (gauge_t) conn.satellites_visible; + data.sats_used = (gauge_t) gpsd_conn.satellites_used; + data.sats_visible = (gauge_t) gpsd_conn.satellites_visible; // dilution of precision: data.vdop = NAN; data.hdop = NAN; if (data.sats_used > 0) { - data.hdop = conn.dop.hdop; - data.vdop = conn.dop.vdop; + data.hdop = gpsd_conn.dop.hdop; + data.vdop = gpsd_conn.dop.vdop; } @@ -120,8 +151,8 @@ static void * gps_collectd_thread (void * pData) } } - gps_stream (&conn, WATCH_DISABLE, /* data = */ NULL); - gps_close (&conn); + gps_stream (&gpsd_conn, WATCH_DISABLE, /* data = */ NULL); + gps_close (&gpsd_conn); pthread_exit ((void *) 0); } @@ -198,11 +229,11 @@ static int cgps_init (void) { int status; - DEBUG ("gps plugin: config{host: \"%s\", port: \"%s\", timeout: %.3f, pause: %.3f}", + DEBUG ("gps plugin: config{host: \"%s\", port: \"%s\", timeout: %.6f sec., pause: %.3f sec.}", config.host, config.port, CDTIME_T_TO_DOUBLE (config.timeout), CDTIME_T_TO_DOUBLE (config.pause)); - status = plugin_thread_create (&connector, NULL, gps_collectd_thread, NULL); + status = plugin_thread_create (&connector, NULL, cgps_thread, NULL); if (status != 0) { ERROR ("gps plugin: pthread_create() failed."); @@ -223,6 +254,8 @@ static int cgps_shutdown (void) connector = (pthread_t) 0; } + gps_close (&gpsd_conn); + sfree (config.port); sfree (config.host);