modbus: avoid enabling libmodbus's debug flag by default
[collectd.git] / src / modbus.c
index f40258d..7349dc5 100644 (file)
@@ -27,7 +27,7 @@
 
 #include <netdb.h>
 
-#include <modbus/modbus.h>
+#include <modbus.h>
 
 #ifndef LIBMODBUS_VERSION_CHECK
 /* Assume version 2.0.3 */
@@ -288,7 +288,9 @@ static int mb_init_connection (mb_host_t *host) /* {{{ */
   if (host == NULL)
     return (EINVAL);
 
+#if COLLECT_DEBUG
   modbus_set_debug (&host->connection, 1);
+#endif
 
   /* We'll do the error handling ourselves. */
   modbus_set_error_handling (&host->connection, NOP_ON_ERROR);
@@ -341,7 +343,9 @@ static int mb_init_connection (mb_host_t *host) /* {{{ */
     return (-1);
   }
 
+#if COLLECT_DEBUG
   modbus_set_debug (host->connection, 1);
+#endif
 
   /* We'll do the error handling ourselves. */
   modbus_set_error_recovery (host->connection, 0);
@@ -377,9 +381,7 @@ static int mb_read_data (mb_host_t *host, mb_slave_t *slave, /* {{{ */
   uint16_t values[2];
   int values_num;
   const data_set_t *ds;
-  struct sockaddr sockaddr;
-  socklen_t saddrlen = sizeof(sockaddr);
-  int status;
+  int status = 0;
 
   if ((host == NULL) || (slave == NULL) || (data == NULL))
     return (EINVAL);
@@ -416,32 +418,40 @@ static int mb_read_data (mb_host_t *host, mb_slave_t *slave, /* {{{ */
   else
     values_num = 1;
 
-  if(host->connection == NULL)
-    goto try_connect;
-
-  if(getpeername(modbus_get_socket(host->connection),
-                 &sockaddr, &saddrlen) != 0)
-    switch(errno){
-    case EBADF:
-    case ENOTSOCK:
-    case ENOTCONN:
-    try_connect:
-      status = mb_init_connection (host);
-      if (status != 0)
-      {
-        ERROR ("Modbus plugin: mb_init_connection (%s/%s) failed. ",
-           host->host, host->node);
-        host->is_connected = 0;
-        host->connection = NULL;
-        return (-1);
-      }
-    break;
-    default:
+  if (host->connection == NULL)
+  {
+    status = EBADF;
+  }
+  else
+  {
+    struct sockaddr sockaddr;
+    socklen_t saddrlen = sizeof (sockaddr);
+
+    status = getpeername (modbus_get_socket (host->connection),
+        &sockaddr, &saddrlen);
+    if (status != 0)
+      status = errno;
+  }
+
+  if ((status == EBADF) || (status == ENOTSOCK) || (status == ENOTCONN))
+  {
+    status = mb_init_connection (host);
+    if (status != 0)
+    {
+      ERROR ("Modbus plugin: mb_init_connection (%s/%s) failed. ",
+          host->host, host->node);
+      host->is_connected = 0;
+      host->connection = NULL;
+      return (-1);
+    }
+  }
+  else if (status != 0)
+  {
 #if LEGACY_LIBMODBUS
-      modbus_close (&host->connection);
+    modbus_close (&host->connection);
 #else
-      modbus_close (host->connection);
-      modbus_free (host->connection);
+    modbus_close (host->connection);
+    modbus_free (host->connection);
 #endif
   }
  
@@ -670,7 +680,6 @@ static int mb_config_add_data (oconfig_item_t *ci) /* {{{ */
   for (i = 0; i < ci->children_num; i++)
   {
     oconfig_item_t *child = ci->children + i;
-    status = 0;
 
     if (strcasecmp ("Type", child->key) == 0)
       status = cf_util_get_string_buffer (child,
@@ -809,7 +818,6 @@ static int mb_config_add_slave (mb_host_t *host, oconfig_item_t *ci) /* {{{ */
   for (i = 0; i < ci->children_num; i++)
   {
     oconfig_item_t *child = ci->children + i;
-    status = 0;
 
     if (strcasecmp ("Instance", child->key) == 0)
       status = cf_util_get_string_buffer (child,
@@ -860,9 +868,15 @@ static int mb_config_add_host (oconfig_item_t *ci) /* {{{ */
 
   status = cf_util_get_string_buffer (ci, host->host, sizeof (host->host));
   if (status != 0)
+  {
+    sfree (host);
     return (status);
+  }
   if (host->host[0] == 0)
+  {
+    sfree (host);
     return (EINVAL);
+  }
 
   for (i = 0; i < ci->children_num; i++)
   {