Merge branch 'collectd-5.7' into collectd-5.8
[collectd.git] / src / modbus.c
index f2d07cc..715724d 100644 (file)
@@ -156,13 +156,13 @@ static mb_data_t *data_definitions = NULL;
 static mb_data_t *data_get_by_name(mb_data_t *src, /* {{{ */
                                    const char *name) {
   if (name == NULL)
-    return (NULL);
+    return NULL;
 
   for (mb_data_t *ptr = src; ptr != NULL; ptr = ptr->next)
     if (strcasecmp(ptr->name, name) == 0)
-      return (ptr);
+      return ptr;
 
-  return (NULL);
+  return NULL;
 } /* }}} mb_data_t *data_get_by_name */
 
 static int data_append(mb_data_t **dst, mb_data_t *src) /* {{{ */
@@ -170,13 +170,13 @@ static int data_append(mb_data_t **dst, mb_data_t *src) /* {{{ */
   mb_data_t *ptr;
 
   if ((dst == NULL) || (src == NULL))
-    return (EINVAL);
+    return EINVAL;
 
   ptr = *dst;
 
   if (ptr == NULL) {
     *dst = src;
-    return (0);
+    return 0;
   }
 
   while (ptr->next != NULL)
@@ -184,7 +184,7 @@ static int data_append(mb_data_t **dst, mb_data_t *src) /* {{{ */
 
   ptr->next = src;
 
-  return (0);
+  return 0;
 } /* }}} int data_append */
 
 /* Copy a single mb_data_t and append it to another list. */
@@ -194,11 +194,11 @@ static int data_copy(mb_data_t **dst, const mb_data_t *src) /* {{{ */
   int status;
 
   if ((dst == NULL) || (src == NULL))
-    return (EINVAL);
+    return EINVAL;
 
   tmp = malloc(sizeof(*tmp));
   if (tmp == NULL)
-    return (ENOMEM);
+    return ENOMEM;
   memcpy(tmp, src, sizeof(*tmp));
   tmp->name = NULL;
   tmp->next = NULL;
@@ -206,17 +206,17 @@ static int data_copy(mb_data_t **dst, const mb_data_t *src) /* {{{ */
   tmp->name = strdup(src->name);
   if (tmp->name == NULL) {
     sfree(tmp);
-    return (ENOMEM);
+    return ENOMEM;
   }
 
   status = data_append(dst, tmp);
   if (status != 0) {
     sfree(tmp->name);
     sfree(tmp);
-    return (status);
+    return status;
   }
 
-  return (0);
+  return 0;
 } /* }}} int data_copy */
 
 /* Lookup a single mb_data_t instance, copy it and append the copy to another
@@ -226,13 +226,13 @@ static int data_copy_by_name(mb_data_t **dst, mb_data_t *src, /* {{{ */
   mb_data_t *ptr;
 
   if ((dst == NULL) || (src == NULL) || (name == NULL))
-    return (EINVAL);
+    return EINVAL;
 
   ptr = data_get_by_name(src, name);
   if (ptr == NULL)
-    return (ENOENT);
+    return ENOENT;
 
-  return (data_copy(dst, ptr));
+  return data_copy(dst, ptr);
 } /* }}} int data_copy_by_name */
 
 /* Read functions */
@@ -242,13 +242,13 @@ static int mb_submit(mb_host_t *host, mb_slave_t *slave, /* {{{ */
   value_list_t vl = VALUE_LIST_INIT;
 
   if ((host == NULL) || (slave == NULL) || (data == NULL))
-    return (EINVAL);
+    return EINVAL;
 
   if (host->interval == 0)
     host->interval = plugin_get_interval();
 
   if (slave->instance[0] == 0)
-    ssnprintf(slave->instance, sizeof(slave->instance), "slave_%i", slave->id);
+    snprintf(slave->instance, sizeof(slave->instance), "slave_%i", slave->id);
 
   vl.values = &value;
   vl.values_len = 1;
@@ -259,7 +259,7 @@ static int mb_submit(mb_host_t *host, mb_slave_t *slave, /* {{{ */
   sstrncpy(vl.type, data->type, sizeof(vl.type));
   sstrncpy(vl.type_instance, data->instance, sizeof(vl.type_instance));
 
-  return (plugin_dispatch_values(&vl));
+  return plugin_dispatch_values(&vl);
 } /* }}} int mb_submit */
 
 static float mb_register_to_float(uint16_t hi, uint16_t lo) /* {{{ */
@@ -283,7 +283,7 @@ static float mb_register_to_float(uint16_t hi, uint16_t lo) /* {{{ */
   conv.b[0] = (hi >> 8) & 0x00ff;
 #endif
 
-  return (conv.f);
+  return conv.f;
 } /* }}} float mb_register_to_float */
 
 #if LEGACY_LIBMODBUS
@@ -293,7 +293,7 @@ static int mb_init_connection(mb_host_t *host) /* {{{ */
   int status;
 
   if (host == NULL)
-    return (EINVAL);
+    return EINVAL;
 
 #if COLLECT_DEBUG
   modbus_set_debug(&host->connection, 1);
@@ -325,11 +325,11 @@ static int mb_init_connection(mb_host_t *host) /* {{{ */
   if (status != 0) {
     ERROR("Modbus plugin: modbus_connect (%s, %i) failed with status %i.",
           host->node, host->port ? host->port : host->baudrate, status);
-    return (status);
+    return status;
   }
 
   host->is_connected = 1;
-  return (0);
+  return 0;
 } /* }}} int mb_init_connection */
 /* #endif LEGACY_LIBMODBUS */
 
@@ -340,10 +340,10 @@ static int mb_init_connection(mb_host_t *host) /* {{{ */
   int status;
 
   if (host == NULL)
-    return (EINVAL);
+    return EINVAL;
 
   if (host->connection != NULL)
-    return (0);
+    return 0;
 
   if (host->conntype == MBCONN_TCP) {
     if ((host->port < 1) || (host->port > 65535))
@@ -355,7 +355,7 @@ static int mb_init_connection(mb_host_t *host) /* {{{ */
     host->connection = modbus_new_tcp(host->node, host->port);
     if (host->connection == NULL) {
       ERROR("Modbus plugin: Creating new Modbus/TCP object failed.");
-      return (-1);
+      return -1;
     }
   } else {
     DEBUG("Modbus plugin: Trying to connect to \"%s\", baudrate %i.",
@@ -364,7 +364,7 @@ static int mb_init_connection(mb_host_t *host) /* {{{ */
     host->connection = modbus_new_rtu(host->node, host->baudrate, 'N', 8, 1);
     if (host->connection == NULL) {
       ERROR("Modbus plugin: Creating new Modbus/RTU object failed.");
-      return (-1);
+      return -1;
     }
   }
 
@@ -381,10 +381,10 @@ static int mb_init_connection(mb_host_t *host) /* {{{ */
           host->node, host->port ? host->port : host->baudrate, status);
     modbus_free(host->connection);
     host->connection = NULL;
-    return (status);
+    return status;
   }
 
-  return (0);
+  return 0;
 } /* }}} int mb_init_connection */
 #endif /* !LEGACY_LIBMODBUS */
 
@@ -408,19 +408,19 @@ static int mb_read_data(mb_host_t *host, mb_slave_t *slave, /* {{{ */
   int status = 0;
 
   if ((host == NULL) || (slave == NULL) || (data == NULL))
-    return (EINVAL);
+    return EINVAL;
 
   ds = plugin_get_ds(data->type);
   if (ds == NULL) {
     ERROR("Modbus plugin: Type \"%s\" is not defined.", data->type);
-    return (-1);
+    return -1;
   }
 
   if (ds->ds_num != 1) {
     ERROR("Modbus plugin: The type \"%s\" has %zu data sources. "
           "I can only handle data sets with only one data source.",
           data->type, ds->ds_num);
-    return (-1);
+    return -1;
   }
 
   if ((ds->ds[0].type != DS_TYPE_GAUGE) &&
@@ -443,6 +443,8 @@ static int mb_read_data(mb_host_t *host, mb_slave_t *slave, /* {{{ */
   if (host->connection == NULL) {
     status = EBADF;
   } else if (host->conntype == MBCONN_TCP) {
+    /* getpeername() is used only to determine if the socket is connected, not
+     * because we're really interested in the peer's IP address. */
     if (getpeername(modbus_get_socket(host->connection),
                     (void *)&(struct sockaddr_storage){0},
                     &(socklen_t){sizeof(struct sockaddr_storage)}) != 0)
@@ -456,7 +458,7 @@ static int mb_read_data(mb_host_t *host, mb_slave_t *slave, /* {{{ */
             host->node);
       host->is_connected = 0;
       host->connection = NULL;
-      return (-1);
+      return -1;
     }
   } else if (status != 0) {
 #if LEGACY_LIBMODBUS
@@ -478,7 +480,7 @@ static int mb_read_data(mb_host_t *host, mb_slave_t *slave, /* {{{ */
   if (status != 0) {
     ERROR("Modbus plugin: modbus_set_slave (%i) failed with status %i.",
           slave->id, status);
-    return (-1);
+    return -1;
   }
 #endif
   if (data->modbus_register_type == MREG_INPUT) {
@@ -503,7 +505,7 @@ static int mb_read_data(mb_host_t *host, mb_slave_t *slave, /* {{{ */
     modbus_free(host->connection);
 #endif
     host->connection = NULL;
-    return (-1);
+    return -1;
   }
 
   DEBUG("Modbus plugin: mb_read_data: Success! "
@@ -573,7 +575,7 @@ static int mb_read_data(mb_host_t *host, mb_slave_t *slave, /* {{{ */
     mb_submit(host, slave, data, vt);
   }
 
-  return (0);
+  return 0;
 } /* }}} int mb_read_data */
 
 static int mb_read_slave(mb_host_t *host, mb_slave_t *slave) /* {{{ */
@@ -582,7 +584,7 @@ static int mb_read_slave(mb_host_t *host, mb_slave_t *slave) /* {{{ */
   int status;
 
   if ((host == NULL) || (slave == NULL))
-    return (EINVAL);
+    return EINVAL;
 
   success = 0;
   for (mb_data_t *data = slave->collect; data != NULL; data = data->next) {
@@ -592,9 +594,9 @@ static int mb_read_slave(mb_host_t *host, mb_slave_t *slave) /* {{{ */
   }
 
   if (success == 0)
-    return (-1);
+    return -1;
   else
-    return (0);
+    return 0;
 } /* }}} int mb_read_slave */
 
 static int mb_read(user_data_t *user_data) /* {{{ */
@@ -604,7 +606,7 @@ static int mb_read(user_data_t *user_data) /* {{{ */
   int status;
 
   if ((user_data == NULL) || (user_data->data == NULL))
-    return (EINVAL);
+    return EINVAL;
 
   host = user_data->data;
 
@@ -616,9 +618,9 @@ static int mb_read(user_data_t *user_data) /* {{{ */
   }
 
   if (success == 0)
-    return (-1);
+    return -1;
   else
-    return (0);
+    return 0;
 } /* }}} int mb_read */
 
 /* Free functions */
@@ -679,7 +681,7 @@ static int mb_config_add_data(oconfig_item_t *ci) /* {{{ */
 
   status = cf_util_get_string(ci, &data.name);
   if (status != 0)
-    return (status);
+    return status;
 
   for (int i = 0; i < ci->children_num; i++) {
     oconfig_item_t *child = ci->children + i;
@@ -750,7 +752,7 @@ static int mb_config_add_data(oconfig_item_t *ci) /* {{{ */
 
   sfree(data.name);
 
-  return (status);
+  return status;
 } /* }}} int mb_config_add_data */
 
 static int mb_config_set_host_address(mb_host_t *host, /* {{{ */
@@ -759,7 +761,7 @@ static int mb_config_set_host_address(mb_host_t *host, /* {{{ */
   int status;
 
   if ((host == NULL) || (address == NULL))
-    return (EINVAL);
+    return EINVAL;
 
   struct addrinfo ai_hints = {
       /* XXX: libmodbus can only handle IPv4 addresses. */
@@ -772,7 +774,7 @@ static int mb_config_set_host_address(mb_host_t *host, /* {{{ */
     ERROR("Modbus plugin: getaddrinfo failed: %s",
           (status == EAI_SYSTEM) ? sstrerror(errno, errbuf, sizeof(errbuf))
                                  : gai_strerror(status));
-    return (status);
+    return status;
   }
 
   for (struct addrinfo *ai_ptr = ai_list; ai_ptr != NULL;
@@ -795,7 +797,7 @@ static int mb_config_set_host_address(mb_host_t *host, /* {{{ */
           host->node);
   }
 
-  return (status);
+  return status;
 } /* }}} int mb_config_set_host_address */
 
 static int mb_config_add_slave(mb_host_t *host, oconfig_item_t *ci) /* {{{ */
@@ -804,11 +806,11 @@ static int mb_config_add_slave(mb_host_t *host, oconfig_item_t *ci) /* {{{ */
   int status;
 
   if ((host == NULL) || (ci == NULL))
-    return (EINVAL);
+    return EINVAL;
 
   slave = realloc(host->slaves, sizeof(*slave) * (host->slaves_num + 1));
   if (slave == NULL)
-    return (ENOMEM);
+    return ENOMEM;
   host->slaves = slave;
   slave = host->slaves + host->slaves_num;
   memset(slave, 0, sizeof(*slave));
@@ -816,7 +818,7 @@ static int mb_config_add_slave(mb_host_t *host, oconfig_item_t *ci) /* {{{ */
 
   status = cf_util_get_int(ci, &slave->id);
   if (status != 0)
-    return (status);
+    return status;
 
   for (int i = 0; i < ci->children_num; i++) {
     oconfig_item_t *child = ci->children + i;
@@ -850,7 +852,7 @@ static int mb_config_add_slave(mb_host_t *host, oconfig_item_t *ci) /* {{{ */
   else /* if (status != 0) */
     data_free_all(slave->collect);
 
-  return (status);
+  return status;
 } /* }}} int mb_config_add_slave */
 
 static int mb_config_add_host(oconfig_item_t *ci) /* {{{ */
@@ -860,17 +862,17 @@ static int mb_config_add_host(oconfig_item_t *ci) /* {{{ */
 
   host = calloc(1, sizeof(*host));
   if (host == NULL)
-    return (ENOMEM);
+    return ENOMEM;
   host->slaves = NULL;
 
   status = cf_util_get_string_buffer(ci, host->host, sizeof(host->host));
   if (status != 0) {
     sfree(host);
-    return (status);
+    return status;
   }
   if (host->host[0] == 0) {
     sfree(host);
-    return (EINVAL);
+    return EINVAL;
   }
 
   for (int i = 0; i < ci->children_num; i++) {
@@ -930,28 +932,27 @@ static int mb_config_add_host(oconfig_item_t *ci) /* {{{ */
   }
 
   if (status == 0) {
-    user_data_t ud;
     char name[1024];
 
-    ud.data = host;
-    ud.free_func = host_free;
-
-    ssnprintf(name, sizeof(name), "modbus-%s", host->host);
+    snprintf(name, sizeof(name), "modbus-%s", host->host);
 
     plugin_register_complex_read(/* group = */ NULL, name,
                                  /* callback = */ mb_read,
-                                 /* interval = */ host->interval, &ud);
+                                 /* interval = */ host->interval,
+                                 &(user_data_t){
+                                     .data = host, .free_func = host_free,
+                                 });
   } else {
     host_free(host);
   }
 
-  return (status);
+  return status;
 } /* }}} int mb_config_add_host */
 
 static int mb_config(oconfig_item_t *ci) /* {{{ */
 {
   if (ci == NULL)
-    return (EINVAL);
+    return EINVAL;
 
   for (int i = 0; i < ci->children_num; i++) {
     oconfig_item_t *child = ci->children + i;
@@ -964,7 +965,7 @@ static int mb_config(oconfig_item_t *ci) /* {{{ */
       ERROR("Modbus plugin: Unknown configuration option: %s", child->key);
   }
 
-  return (0);
+  return 0;
 } /* }}} int mb_config */
 
 /* ========= */
@@ -974,12 +975,10 @@ static int mb_shutdown(void) /* {{{ */
   data_free_all(data_definitions);
   data_definitions = NULL;
 
-  return (0);
+  return 0;
 } /* }}} int mb_shutdown */
 
 void module_register(void) {
   plugin_register_complex_config("modbus", mb_config);
   plugin_register_shutdown("modbus", mb_shutdown);
 } /* void module_register */
-
-/* vim: set sw=2 sts=2 et fdm=marker : */