Fixed time to ms instead of us, added a possibility to reconnect if gpsd server resta...
[collectd.git] / src / gps.c
index b4a273e..944932a 100644 (file)
--- a/src/gps.c
+++ b/src/gps.c
 
 #define GPS_DEFAULT_HOST    "localhost"
 #define GPS_DEFAULT_PORT    "2947"
-#define GPS_DEFAULT_TIMEOUT 15
-#define GPS_DEFAULT_PAUSE   1
+#define GPS_DEFAULT_TIMEOUT TIME_T_TO_CDTIME_T (15)
+#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 <gps.h>
 #include <pthread.h>
@@ -41,8 +43,8 @@
 typedef struct {
   char *host;
   char *port;
-  int timeout;
-  int pause;
+  cdtime_t timeout;
+  cdtime_t pause;
 } cgps_config_t;
 
 typedef struct {
@@ -52,15 +54,6 @@ typedef struct {
   gauge_t vdop;
 } cgps_data_t;
 
-static const char *config_keys[] =
-{
-  "Host",
-  "Port",
-  "Timeout",
-  "Pause"
-};
-static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
-
 // Thread items:
 static pthread_t connector = (pthread_t) 0;
 
@@ -72,12 +65,15 @@ static pthread_mutex_t  data_lock = PTHREAD_MUTEX_INITIALIZER;
 /**
  * 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)
   {
+    err_count = 0;
+
     int status = gps_open (config.host, config.port, &conn);
     if (status < 0)
     {
@@ -88,19 +84,39 @@ static void * gps_collectd_thread (void * pData)
     }
 
     gps_stream (&conn, WATCH_ENABLE | WATCH_JSON | WATCH_NEWSTYLE, NULL);
-    gps_send (&conn, "?WATCH={\"enable\":true,\"json\":true,\"nmea\":false}\r\n");
+    gps_send (&conn, GPS_CONFIG);
 
     while (1)
     {
-      if (!gps_waiting (&conn, config.timeout))
+      long timeout_ms = CDTIME_T_TO_MS (config.timeout);
+      if (!gps_waiting (&conn, (int) timeout_ms))
       {
-        sleep (config.pause);
+        struct timespec pause_ns;
+        CDTIME_T_TO_TIMESPEC (config.pause, &pause_ns);
+        nanosleep (&pause_ns, NULL);
         continue;
       }
 
       if (gps_read (&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 (&conn, GPS_CONFIG) == -1)
+          {
+            WARNING ("gps plugin: gpsd seems to be done, reconnecting");
+            break;
+          }
+          // Server is responding ...
+          else
+          {
+            err_count = 0;
+          }
+        }
+
         continue;
       }
 
@@ -174,30 +190,27 @@ static int cgps_read ()
 /**
  * Read configuration.
  */
-static int cgps_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)
-  {
-    free (config.host);
-    config.host = sstrdup (value);
-  }
-  else if (strcasecmp (key, "Port") == 0)
+  for (i = 0; i < ci->children_num; i++)
   {
-    free (config.port);
-    config.port = sstrdup (value);
-  }
-  else if (strcasecmp (key, "Timeout") == 0)
-  {
-    config.timeout = (int) (strtod(value, &endptr) * 1000);
-  }
-  else if (strcasecmp (key, "Pause") == 0)
-  {
-    config.pause = (int) (strtod (value, &endptr));
+    oconfig_item_t *child = ci->children + i;
+
+    if (strcasecmp ("Host", child->key) == 0)
+      cf_util_get_string (child, &config.host);
+    else if (strcasecmp ("Port", child->key) == 0)
+      cf_util_get_service (child, &config.port);
+    else if (strcasecmp ("Timeout", child->key) == 0)
+      cf_util_get_cdtime (child, &config.timeout);
+    else if (strcasecmp ("Pause", child->key) == 0)
+      cf_util_get_cdtime (child, &config.pause);
+    else
+      WARNING ("gps plugin: Ignoring unknown config option \"%s\".", child->key);
   }
 
-  return (0);
+  return 0;
 }
 
 /**
@@ -207,10 +220,11 @@ static int cgps_init (void)
 {
   int status;
 
-  DEBUG ("gps plugin: config{host: \"%s\", port: \"%s\", timeout: %d, pause: %d}",
-         config.host, config.port, config.timeout, config.pause);
+  DEBUG ("gps plugin: config{host: \"%s\", port: \"%s\", timeout: %.3f, pause: %.3f}",
+         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.");
@@ -247,7 +261,7 @@ void module_register (void)
   config.timeout = GPS_DEFAULT_TIMEOUT;
   config.pause = GPS_DEFAULT_PAUSE;
 
-  plugin_register_config ("gps", cgps_config, config_keys, config_keys_num);
+  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);