Add support to set the thread name.
[collectd.git] / src / sigrok.c
index ad5c70c..6a8003c 100644 (file)
@@ -20,6 +20,7 @@
  */
 
 #include "collectd.h"
+
 #include "common.h"
 #include "plugin.h"
 
@@ -27,7 +28,6 @@
 #include <stdlib.h>
 #include <string.h>
 #include <time.h>
-#include <pthread.h>
 
 #include <glib.h>
 #include <libsigrok/libsigrok.h>
@@ -70,13 +70,11 @@ static int sigrok_log_callback(void*cb_data __attribute__((unused)),
 static int sigrok_config_device(oconfig_item_t *ci)
 {
        struct config_device *cfdev;
-       int i;
 
-       if (!(cfdev = malloc(sizeof(struct config_device)))) {
-               ERROR("sigrok plugin: malloc() failed.");
+       if (!(cfdev = calloc(1, sizeof(*cfdev)))) {
+               ERROR("sigrok plugin: calloc failed.");
                return -1;
        }
-       memset(cfdev, 0, sizeof(*cfdev));
        if (cf_util_get_string(ci, &cfdev->name)) {
                free(cfdev);
                WARNING("sigrok plugin: Invalid device name.");
@@ -84,7 +82,7 @@ static int sigrok_config_device(oconfig_item_t *ci)
        }
        cfdev->min_dispatch_interval = DEFAULT_MIN_DISPATCH_INTERVAL;
 
-       for (i = 0; i < ci->children_num; i++) {
+       for (int i = 0; i < ci->children_num; i++) {
                oconfig_item_t *item = ci->children + i;
                if (!strcasecmp(item->key, "driver"))
                        cf_util_get_string(item, &cfdev->driver);
@@ -106,9 +104,7 @@ static int sigrok_config_device(oconfig_item_t *ci)
 
 static int sigrok_config(oconfig_item_t *ci)
 {
-       int i;
-
-       for (i = 0; i < ci->children_num; i++) {
+       for (int i = 0; i < ci->children_num; i++) {
                oconfig_item_t *item = ci->children + i;
                if (strcasecmp("LogLevel", item->key) == 0) {
                        int status;
@@ -165,13 +161,11 @@ static void sigrok_feed_callback(const struct sr_dev_inst *sdi,
 {
        const struct sr_datafeed_analog *analog;
        struct config_device *cfdev;
-       GSList *l;
-       value_t value;
        value_list_t vl = VALUE_LIST_INIT;
 
        /* Find this device's configuration. */
        cfdev = NULL;
-       for (l = config_devices; l; l = l->next) {
+       for (GSList *l = config_devices; l; l = l->next) {
                cfdev = l->data;
                if (cfdev->sdi == sdi) {
                        /* Found it. */
@@ -204,13 +198,10 @@ static void sigrok_feed_callback(const struct sr_dev_inst *sdi,
 
        /* Ignore all but the first sample on the first probe. */
        analog = packet->payload;
-       value.gauge = analog->data[0];
-       vl.values = &value;
+       vl.values = &(value_t) { .gauge = analog->data[0] };
        vl.values_len = 1;
-       sstrncpy(vl.host, hostname_g, sizeof(vl.host));
        sstrncpy(vl.plugin, "sigrok", sizeof(vl.plugin));
-       ssnprintf(vl.plugin_instance, sizeof(vl.plugin_instance),
-                       "%s", cfdev->name);
+       sstrncpy(vl.plugin_instance, cfdev->name, sizeof(vl.plugin_instance));
        sstrncpy(vl.type, sigrok_value_type(analog), sizeof(vl.type));
 
        plugin_dispatch_values(&vl);
@@ -236,14 +227,14 @@ static int sigrok_init_driver(struct config_device *cfdev,
 
        drvopts = NULL;
        if (cfdev->conn) {
-               if (!(src = malloc(sizeof(struct sr_config))))
+               if (!(src = malloc(sizeof(*src))))
                        return -1;
                src->key = SR_CONF_CONN;
                src->data = g_variant_new_string(cfdev->conn);
                drvopts = g_slist_append(drvopts, src);
        }
        if (cfdev->serialcomm) {
-               if (!(src = malloc(sizeof(struct sr_config))))
+               if (!(src = malloc(sizeof(*src))))
                        return -1;
                src->key = SR_CONF_SERIALCOMM;
                src->data = g_variant_new_string(cfdev->serialcomm);
@@ -361,7 +352,7 @@ static int sigrok_init(void)
        }
 
        status = plugin_thread_create(&sr_thread, NULL, sigrok_read_thread,
-                       NULL);
+                       NULL, "sigrok read");
        if (status != 0)
        {
                char errbuf[1024];