modbus plugin: Restore compatibility to libmodbus 2.0.3.
[collectd.git] / src / modbus.c
index 78c4335..6a753e1 100644 (file)
 #include "plugin.h"
 #include "configfile.h"
 
+#include <netdb.h>
+
 #include <modbus/modbus.h>
 
+#ifndef MODBUS_TCP_DEFAULT_PORT
+# ifdef MODBUS_TCP_PORT
+#  define MODBUS_TCP_DEFAULT_PORT MODBUS_TCP_PORT
+# else
+#  define MODBUS_TCP_DEFAULT_PORT 502
+# endif
+#endif
+
 /*
  * <Data "data_name">
  *   RegisterBase 1234
@@ -84,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;
@@ -757,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);
@@ -783,21 +793,18 @@ 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 (name, mb_read,
-        (interval.tv_sec > 0) ? &interval : NULL,
+    plugin_register_complex_read (/* group = */ NULL, name,
+        /* callback = */ mb_read,
+        /* interval = */ (host->interval > 0) ? &interval : NULL,
         &ud);
   }
   else
@@ -832,47 +839,6 @@ static int mb_config (oconfig_item_t *ci) /* {{{ */
 
 /* ========= */
 
-#if 0
-static int foo (void) /* {{{ */
-{
-  int status;
-  uint16_t values[2];
-  int values_num;
-
-  if (dev == NULL)
-    return (EINVAL);
-
-  printf ("mb_read (addr = %i, float = %s);\n", register_addr,
-      is_float ? "true" : "false");
-
-  memset (values, 0, sizeof (values));
-  if (is_float)
-    values_num = 2;
-  else
-    values_num = 1;
-
-  status = read_holding_registers (dev->connection,
-      /* slave = */ 1, /* start_addr = */ register_addr,
-      /* num_registers = */ values_num, /* buffer = */ values);
-  printf ("read_coil_status returned with status %i\n", status);
-  if (status <= 0)
-    return (EAGAIN);
-
-  if (is_float)
-  {
-    float value = mb_register_to_float (values[0], values[1]);
-    printf ("read_coil_status returned value %g (hi %#"PRIx16", lo %#"PRIx16")\n",
-        value, values[0], values[1]);
-  }
-  else
-  {
-    printf ("read_coil_status returned value %"PRIu16"\n", values[0]);
-  }
-
-  return (0);
-} /* }}} int foo */
-#endif
-
 static int mb_shutdown (void) /* {{{ */
 {
   data_free_all (data_definitions);