X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fgps.c;h=f72cef4961bfa860f2481b0663ce822a36f592ad;hb=4d370741101aeb037ae52f3529a4a0869e0dc08a;hp=86bd2d1c2aab4337529dde488ec5e2d5ae2092ba;hpb=ad2caf6750e5e584e39cf6c76a32937e1cef621c;p=collectd.git diff --git a/src/gps.c b/src/gps.c index 86bd2d1c..f72cef49 100644 --- a/src/gps.c +++ b/src/gps.c @@ -22,141 +22,203 @@ * * Authors: * Nicolas JOURDEN + * Florian octo Forster + * Marc Fournier **/ - - #include "collectd.h" #include "common.h" #include "plugin.h" +#include "utils_time.h" +#define CGPS_TRUE 1 +#define CGPS_FALSE 0 +#define CGPS_DEFAULT_HOST "localhost" +#define CGPS_DEFAULT_PORT "2947" /* DEFAULT_GPSD_PORT */ +#define CGPS_DEFAULT_TIMEOUT MS_TO_CDTIME_T (15) +#define CGPS_DEFAULT_PAUSE_CONNECT TIME_T_TO_CDTIME_T (5) +#define CGPS_MAX_ERROR 100 +#define CGPS_CONFIG "?WATCH={\"enable\":true,\"json\":true,\"nmea\":false}\r\n" -#define GPS_DEFAULT_HOST "localhost" -#define GPS_DEFAULT_PORT "2947" -#define GPS_DEFAULT_TIMEOUT 15 -#define GPS_DEFAULT_PAUSE 1 - - -#if HAVE_GPS_H #include -#endif - -#if HAVE_LIBPTHREAD #include -#endif - -typedef struct -{ +typedef struct { char *host; char *port; - int timeout; - int pause; -} gps_definition_t; -static gps_definition_t gps_data_config; - + cdtime_t timeout; + cdtime_t pause_connect; +} cgps_config_t; typedef struct { - int satellites; - double vdop; - double hdop; -} gpsdata_t; -static gpsdata_t gps_data_read; + gauge_t sats_used; + gauge_t sats_visible; + gauge_t hdop; + gauge_t vdop; +} cgps_data_t; +static cgps_config_t cgps_config_data; -static const char *config_keys[] = -{ - "Host", - "Port", - "Timeout", - "Pause" -}; -static int config_keys_num = STATIC_ARRAY_SIZE (config_keys); +static cgps_data_t cgps_data = {NAN, NAN, NAN, NAN}; + +static pthread_t cgps_thread_id; +static pthread_mutex_t cgps_data_lock = PTHREAD_MUTEX_INITIALIZER; +static pthread_mutex_t cgps_thread_lock = PTHREAD_MUTEX_INITIALIZER; +static int cgps_thread_shutdown = CGPS_FALSE; +static int cgps_thread_running = CGPS_FALSE; +/** + * Non blocking pause for the thread. + */ +static int cgps_thread_pause(cdtime_t pTime) +{ + cdtime_t now; + now = cdtime (); + struct timespec pause_th; + CDTIME_T_TO_TIMESPEC (MS_TO_CDTIME_T(10), &pause_th); + while (CGPS_TRUE) + { + if ( (cdtime () - now) > pTime ) + { + break; + } -// Thread items: -static pthread_t connector = (pthread_t) 0; -static pthread_mutex_t data_mutex = PTHREAD_MUTEX_INITIALIZER; + pthread_mutex_lock (&cgps_thread_lock); + if (cgps_thread_shutdown == CGPS_TRUE) + { + return CGPS_FALSE; + } + pthread_mutex_unlock (&cgps_thread_lock); + nanosleep (&pause_th, NULL); + } + return CGPS_TRUE; +} /** * Thread reading from gpsd. */ -static void * gps_collectd_thread (void * pData) +static void * cgps_thread (void * pData) { - struct gps_data_t gps_data; + struct gps_data_t gpsd_conn; + unsigned int err_count; + cgps_thread_running = CGPS_TRUE; - while (1) + while (CGPS_TRUE) { - if (gps_open(gps_data_config.host, gps_data_config.port, &gps_data) < 0) + pthread_mutex_lock (&cgps_thread_lock); + if (cgps_thread_shutdown == CGPS_TRUE) + { + goto quit; + } + pthread_mutex_unlock (&cgps_thread_lock); + + err_count = 0; + +#if GPSD_API_MAJOR_VERSION > 4 + int status = gps_open (cgps_config_data.host, cgps_config_data.port, &gpsd_conn); +#else + int status = gps_open_r (cgps_config_data.host, cgps_config_data.port, &gpsd_conn); +#endif + if (status < 0) { - WARNING ("gps: cannot connect to: %s:%s", gps_data_config.host, gps_data_config.port); - sleep(60); + WARNING ("gps plugin: connecting to %s:%s failed: %s", + cgps_config_data.host, cgps_config_data.port, gps_errstr (status)); + + // Here we make a pause until a new tentative to connect, we check also if + // the thread does not need to stop. + if (cgps_thread_pause(cgps_config_data.pause_connect) == CGPS_FALSE) + { + goto quit; + } + continue; } - gps_stream(&gps_data, WATCH_ENABLE | WATCH_JSON | WATCH_NEWSTYLE, NULL); - gps_send(&gps_data, "?WATCH={\"enable\":true,\"json\":true,\"nmea\":false}\r\n"); + gps_stream (&gpsd_conn, WATCH_ENABLE | WATCH_JSON | WATCH_NEWSTYLE, NULL); + gps_send (&gpsd_conn, CGPS_CONFIG); - while (1) + while (CGPS_TRUE) { - if (gps_waiting (&gps_data, gps_data_config.timeout ) ) + pthread_mutex_lock (&cgps_thread_lock); + if (cgps_thread_shutdown == CGPS_TRUE) + { + goto stop; + } + pthread_mutex_unlock (&cgps_thread_lock); + +#if GPSD_API_MAJOR_VERSION > 4 + long timeout_us = CDTIME_T_TO_US (cgps_config_data.timeout); + if (!gps_waiting (&gpsd_conn, (int) timeout_us )) +#else + if (!gps_waiting (&gpsd_conn)) +#endif + { + continue; + } + + if (gps_read (&gpsd_conn) == -1) { - DEBUG ("gps: reading\n"); + WARNING ("gps plugin: incorrect data! (err_count: %d)", err_count); + err_count++; - if (gps_read (&gps_data) == -1) + if (err_count > CGPS_MAX_ERROR) { - WARNING ("gps: incorrect data !\n"); - } - else { - pthread_mutex_lock (&data_mutex); - DEBUG ("gps: parsing\n"); - - // Dop data: - if (isnan(gps_data.dop.vdop) == 0) + // Server is not responding ... + if (gps_send (&gpsd_conn, CGPS_CONFIG) == -1) { - DEBUG ("gps: isnan(gps_data.dop.vdop) == 0 [OK]\n"); - gps_data_read.vdop = gps_data.dop.vdop; + WARNING ("gps plugin: gpsd seems to be down, reconnecting"); + gps_close (&gpsd_conn); + break; } - if (isnan(gps_data.dop.hdop) == 0) + // Server is responding ... + else { - DEBUG ("gps: isnan(gps_data.dop.hdop) == 0 [OK]\n"); - gps_data_read.hdop = gps_data.dop.hdop; + err_count = 0; } - - // Sat in view: - if ((gps_data.set & LATLON_SET)) - { - DEBUG ("gps: gps_data.set & LATLON_SET [OK] ... \n"); - gps_data_read.satellites = gps_data.satellites_used; - } - - DEBUG ("gps: raw is hdop=%1.3f, vdop=%1.3f, sat-used=%02d, lat=%02.05f, lon=%03.05f\n", - gps_data.dop.hdop, - gps_data.dop.vdop, - gps_data.satellites_used, - gps_data.fix.latitude, - gps_data.fix.longitude - ); - - pthread_mutex_unlock (&data_mutex); - sleep(gps_data_config.pause); } + + continue; + } + + pthread_mutex_lock (&cgps_data_lock); + + // Number of sats in view: + cgps_data.sats_used = (gauge_t) gpsd_conn.satellites_used; + cgps_data.sats_visible = (gauge_t) gpsd_conn.satellites_visible; + + // dilution of precision: + cgps_data.vdop = NAN; + cgps_data.hdop = NAN; + if (cgps_data.sats_used > 0) + { + cgps_data.hdop = gpsd_conn.dop.hdop; + cgps_data.vdop = gpsd_conn.dop.vdop; } + + DEBUG ("gps plugin: %.0f sats used (of %.0f visible), hdop = %.3f, vdop = %.3f", + cgps_data.sats_used, cgps_data.sats_visible, cgps_data.hdop, cgps_data.vdop); + + pthread_mutex_unlock (&cgps_data_lock); } } - gps_stream(&gps_data, WATCH_DISABLE, NULL); - gps_close(&gps_data); - - pthread_exit ((void *)0); +stop: + DEBUG ("gps plugin: thread closing gpsd connection ... "); + gps_stream (&gpsd_conn, WATCH_DISABLE, NULL); + gps_close (&gpsd_conn); +quit: + DEBUG ("gps plugin: thread shutting down ... "); + cgps_thread_running = CGPS_FALSE; + pthread_mutex_unlock (&cgps_thread_lock); + pthread_exit (NULL); } /** * Submit a piece of the data. */ -static void gps_collectd_submit (const char *type, gauge_t value, const char *type_instance) +static void cgps_submit (const char *type, gauge_t value, const char *type_instance) { value_t values[1]; value_list_t vl = VALUE_LIST_INIT; @@ -173,97 +235,117 @@ static void gps_collectd_submit (const char *type, gauge_t value, const char *ty plugin_dispatch_values (&vl); } - /** * Read the data and submit by piece. */ -static int gps_collectd_read () +static int cgps_read (void) { - pthread_mutex_lock (&data_mutex); - gps_collectd_submit("dilution_of_precision", (gauge_t) gps_data_read.hdop, "horizontal"); - gps_collectd_submit("dilution_of_precision", (gauge_t) gps_data_read.vdop, "vertical"); - gps_collectd_submit("satellites", (gauge_t) gps_data_read.satellites, "gps"); - DEBUG ("gps: hdop=%1.3f, vdop=%1.3f, sat=%02d.\n", - gps_data_read.hdop, - gps_data_read.vdop, - gps_data_read.satellites - ); - pthread_mutex_unlock (&data_mutex); + cgps_data_t data_copy; + + pthread_mutex_lock (&cgps_data_lock); + data_copy = cgps_data; + pthread_mutex_unlock (&cgps_data_lock); + + cgps_submit ("dilution_of_precision", data_copy.hdop, "horizontal"); + cgps_submit ("dilution_of_precision", data_copy.vdop, "vertical"); + cgps_submit ("satellites", data_copy.sats_used, "used"); + cgps_submit ("satellites", data_copy.sats_visible, "visible"); + return (0); } - /** * Read configuration. */ -static int gps_collectd_config (const char *key, const char *value) +static int cgps_config (oconfig_item_t *ci) { - char *endptr = NULL; + int i; - if (strcasecmp (key, "Host") == 0) + for (i = 0; i < ci->children_num; i++) { - if (gps_data_config.host != NULL) - { - free (gps_data_config.host); - } - gps_data_config.host = sstrdup (value); + oconfig_item_t *child = ci->children + i; + + if (strcasecmp ("Host", child->key) == 0) + cf_util_get_string (child, &cgps_config_data.host); + else if (strcasecmp ("Port", child->key) == 0) + cf_util_get_service (child, &cgps_config_data.port); + else if (strcasecmp ("Timeout", child->key) == 0) + cf_util_get_cdtime (child, &cgps_config_data.timeout); + else if (strcasecmp ("PauseConnect", child->key) == 0) + cf_util_get_cdtime (child, &cgps_config_data.pause_connect); + else + WARNING ("gps plugin: Ignoring unknown config option \"%s\".", child->key); } - if (strcasecmp (key, "Port") == 0) - { - if (gps_data_config.port != NULL) - { - free (gps_data_config.port); - } - gps_data_config.port = sstrdup (value); - } - if (strcasecmp (key, "Timeout") == 0) - { - gps_data_config.timeout = (int) ( strtod(value, &endptr) * 1000 ); - DEBUG ("gps: will use pause %s - %d.\n", value, gps_data_config.timeout); - } - if (strcasecmp (key, "Pause") == 0) + + // Controlling the value for timeout: + // If set too high it blocks the reading (> 5 s), too low it gets not reading (< 500 us). + // To avoid any issues we replace "out of range" value by the default value. + if ( + cgps_config_data.timeout > TIME_T_TO_CDTIME_T(5) + || + cgps_config_data.timeout < US_TO_CDTIME_T(500) + ) { - gps_data_config.pause = (int) (strtod (value, &endptr)); - DEBUG ("gps: will use pause %s - %d.\n", value, gps_data_config.pause); + WARNING ("gps plugin: timeout set to %.6f sec. setting to default (%.6f).", + CDTIME_T_TO_DOUBLE(cgps_config_data.timeout), + CDTIME_T_TO_DOUBLE(CGPS_DEFAULT_TIMEOUT) + ); + cgps_config_data.timeout = CGPS_DEFAULT_TIMEOUT; } + return (0); } - /** * Init. */ -static int gps_collectd_init (void) +static int cgps_init (void) { - int err = 0; + int status; - DEBUG ("gps: will use %s:%s, timeout %d ms, pause %d sec.\n", gps_data_config.host, gps_data_config.port, gps_data_config.timeout, gps_data_config.pause); + if (cgps_thread_running == CGPS_TRUE) + { + DEBUG ("gps plugin: error gps thread already running ... "); + return 0; + } - err = plugin_thread_create (&connector, NULL, gps_collectd_thread, NULL); + DEBUG ("gps plugin: config{host: \"%s\", port: \"%s\", timeout: %.6f sec., pause connect: %.3f sec.}", + cgps_config_data.host, cgps_config_data.port, + CDTIME_T_TO_DOUBLE (cgps_config_data.timeout), + CDTIME_T_TO_DOUBLE (cgps_config_data.pause_connect)); - if (err != 0) + status = plugin_thread_create (&cgps_thread_id, NULL, cgps_thread, NULL); + if (status != 0) { - ERROR ("gps: pthread_create() failed."); + ERROR ("gps plugin: pthread_create() failed."); return (-1); } return (0); } - /** * Shutdown. */ -static int gps_collectd_shutdown (void) +static int cgps_shutdown (void) { - if (connector != ((pthread_t) 0)) - { - pthread_kill (connector, SIGTERM); - connector = (pthread_t) 0; - } + void * res; - sfree (gps_data_config.port); - sfree (gps_data_config.host); + pthread_mutex_lock (&cgps_thread_lock); + cgps_thread_shutdown = CGPS_TRUE; + pthread_mutex_unlock (&cgps_thread_lock); + + pthread_join(cgps_thread_id, &res); + free(res); + + // Clean mutex: + pthread_mutex_unlock(&cgps_thread_lock); + pthread_mutex_destroy(&cgps_thread_lock); + pthread_mutex_unlock(&cgps_data_lock); + pthread_mutex_destroy(&cgps_data_lock); + + sfree (cgps_config_data.port); + sfree (cgps_config_data.host); return (0); } @@ -271,23 +353,15 @@ static int gps_collectd_shutdown (void) /** * Register the module. */ -void module_register (void) +void module_register (void) { - gps_data_config.host = sstrdup (GPS_DEFAULT_HOST); - gps_data_config.port = sstrdup (GPS_DEFAULT_PORT); - gps_data_config.timeout = GPS_DEFAULT_TIMEOUT; - gps_data_config.pause = GPS_DEFAULT_PAUSE; - gps_data_read.hdop = 0; - gps_data_read.vdop = 0; - gps_data_read.satellites = 0; - - // Read the config params: - plugin_register_config ("gps", gps_collectd_config, config_keys, config_keys_num); - // Create the thread: - plugin_register_init ("gps", gps_collectd_init); - // Kill the thread and stop. - plugin_register_shutdown ("gps", gps_collectd_shutdown); - // Read plugin: - plugin_register_read ("gps", gps_collectd_read); + cgps_config_data.host = sstrdup (CGPS_DEFAULT_HOST); + cgps_config_data.port = sstrdup (CGPS_DEFAULT_PORT); + cgps_config_data.timeout = CGPS_DEFAULT_TIMEOUT; + cgps_config_data.pause_connect = CGPS_DEFAULT_PAUSE_CONNECT; + + plugin_register_complex_config ("gps", cgps_config); + plugin_register_init ("gps", cgps_init); + plugin_register_read ("gps", cgps_read); + plugin_register_shutdown ("gps", cgps_shutdown); } -