modbus plugin: Restore compatibility to libmodbus 2.0.3.
[collectd.git] / src / modbus.c
index adab0d0..6a753e1 100644 (file)
@@ -94,7 +94,7 @@ struct mb_host_s /* {{{ */
   char node[NI_MAXHOST];
   /* char service[NI_MAXSERV]; */
   int port;
-  int interval;
+  cdtime_t interval;
 
   mb_slave_t *slaves;
   size_t slaves_num;
@@ -281,10 +281,8 @@ static int mb_init_connection (mb_host_t *host) /* {{{ */
 
   modbus_set_debug (&host->connection, 1);
 
-#if 0
   /* We'll do the error handling ourselves. */
   modbus_set_error_handling (&host->connection, NOP_ON_ERROR);
-#endif
 
   if ((host->port < 1) || (host->port > 65535))
     host->port = MODBUS_TCP_DEFAULT_PORT;
@@ -293,10 +291,8 @@ static int mb_init_connection (mb_host_t *host) /* {{{ */
       host->node, host->port);
 
   modbus_init_tcp (&host->connection,
-      /* host = */ host->node);
-#if 0
+      /* host = */ host->node,
       /* port = */ host->port);
-#endif
 
   status = modbus_connect (&host->connection);
   if (status != 0)
@@ -325,7 +321,7 @@ static int mb_init_connection (mb_host_t *host) /* {{{ */
 static int mb_read_data (mb_host_t *host, mb_slave_t *slave, /* {{{ */
     mb_data_t *data)
 {
-  int values[2];
+  uint16_t values[2];
   int values_num;
   const data_set_t *ds;
   int status;
@@ -771,7 +767,7 @@ static int mb_config_add_host (oconfig_item_t *ci) /* {{{ */
         status = -1;
     }
     else if (strcasecmp ("Interval", child->key) == 0)
-      status = cf_util_get_int (child, &host->interval);
+      status = cf_util_get_cdtime (child, &host->interval);
     else if (strcasecmp ("Slave", child->key) == 0)
       /* Don't set status: Gracefully continue if a slave fails. */
       mb_config_add_slave (host, child);
@@ -797,21 +793,19 @@ static int mb_config_add_host (oconfig_item_t *ci) /* {{{ */
   {
     user_data_t ud;
     char name[1024];
-    struct timespec interval;
+    struct timespec interval = { 0, 0 };
 
     ud.data = host;
     ud.free_func = host_free;
 
     ssnprintf (name, sizeof (name), "modbus-%s", host->host);
 
-    interval.tv_nsec = 0;
-    if (host->interval > 0)
-      interval.tv_sec = host->interval;
-    else
-      interval.tv_sec = 0;
+    CDTIME_T_TO_TIMESPEC (host->interval, &interval);
 
     plugin_register_complex_read (/* group = */ NULL, name,
-        mb_read, (interval.tv_sec > 0) ? &interval : NULL, &ud);
+        /* callback = */ mb_read,
+        /* interval = */ (host->interval > 0) ? &interval : NULL,
+        &ud);
   }
   else
   {