Code

snmp plugin: res can't be NULL here
[collectd.git] / src / sigrok.c
1 /*
2  * collectd - src/sigrok.c
3  * Copyright (C) 2013 Bert Vermeulen
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  * Authors:
19  *   Bert Vermeulen <bert at biot.com>
20  */
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <time.h>
30 #include <pthread.h>
32 #include <glib.h>
33 #include <libsigrok/libsigrok.h>
35 /* Minimum interval between dispatches coming from this plugin. The RRD
36  * plugin, at least, complains when written to with sub-second intervals.*/
37 #define DEFAULT_MIN_DISPATCH_INTERVAL TIME_T_TO_CDTIME_T(0)
39 static pthread_t sr_thread;
40 static int sr_thread_running = FALSE;
41 GSList *config_devices;
42 static int num_devices;
43 static int loglevel = SR_LOG_WARN;
44 static struct sr_context *sr_ctx;
46 struct config_device {
47         char *name;
48         char *driver;
49         char *conn;
50         char *serialcomm;
51         struct sr_dev_inst *sdi;
52         cdtime_t min_dispatch_interval;
53         cdtime_t last_dispatch;
54 };
57 static int sigrok_log_callback(void*cb_data __attribute__((unused)),
58                 int msg_loglevel, const char *format, va_list args)
59 {
60         char s[512];
62         if (msg_loglevel <= loglevel) {
63                 vsnprintf(s, 512, format, args);
64                 plugin_log(LOG_INFO, "sigrok plugin: %s", s);
65         }
67         return 0;
68 }
70 static int sigrok_config_device(oconfig_item_t *ci)
71 {
72         struct config_device *cfdev;
73         int i;
75         if (!(cfdev = calloc(1, sizeof(*cfdev)))) {
76                 ERROR("sigrok plugin: calloc failed.");
77                 return -1;
78         }
79         if (cf_util_get_string(ci, &cfdev->name)) {
80                 free(cfdev);
81                 WARNING("sigrok plugin: Invalid device name.");
82                 return -1;
83         }
84         cfdev->min_dispatch_interval = DEFAULT_MIN_DISPATCH_INTERVAL;
86         for (i = 0; i < ci->children_num; i++) {
87                 oconfig_item_t *item = ci->children + i;
88                 if (!strcasecmp(item->key, "driver"))
89                         cf_util_get_string(item, &cfdev->driver);
90                 else if (!strcasecmp(item->key, "conn"))
91                         cf_util_get_string(item, &cfdev->conn);
92                 else if (!strcasecmp(item->key, "serialcomm"))
93                         cf_util_get_string(item, &cfdev->serialcomm);
94                 else if (!strcasecmp(item->key, "minimuminterval"))
95                         cf_util_get_cdtime(item, &cfdev->min_dispatch_interval);
96                 else
97                         WARNING("sigrok plugin: Invalid keyword \"%s\".",
98                                         item->key);
99         }
101         config_devices = g_slist_append(config_devices, cfdev);
103         return 0;
106 static int sigrok_config(oconfig_item_t *ci)
108         int i;
110         for (i = 0; i < ci->children_num; i++) {
111                 oconfig_item_t *item = ci->children + i;
112                 if (strcasecmp("LogLevel", item->key) == 0) {
113                         int status;
114                         int tmp = -1;
116                         status = cf_util_get_int (item, &tmp);
117                         if (status != 0)
118                                 continue;
119                         else if ((tmp < 0) || (tmp > 5)) {
120                                 ERROR ("sigrok plugin: The \"LogLevel\" "
121                                                 "configuration option expects "
122                                                 "an integer between 0 and 5 "
123                                                 "(inclusive); you provided %i.",
124                                                 tmp);
125                                 continue;
126                         }
127                         loglevel = tmp;
128                 } else if (!strcasecmp(item->key, "Device"))
129                         sigrok_config_device(item);
130                 else
131                         WARNING("sigrok plugin: Invalid keyword \"%s\".",
132                                         item->key);
133         }
135         return 0;
138 static const char *sigrok_value_type(const struct sr_datafeed_analog *analog)
140         const char *s;
142         if (analog->mq == SR_MQ_VOLTAGE)
143                 s = "voltage";
144         else if (analog->mq == SR_MQ_CURRENT)
145                 s = "current";
146         else if (analog->mq == SR_MQ_FREQUENCY)
147                 s = "frequency";
148         else if (analog->mq == SR_MQ_POWER)
149                 s = "power";
150         else if (analog->mq == SR_MQ_TEMPERATURE)
151                 s = "temperature";
152         else if (analog->mq == SR_MQ_RELATIVE_HUMIDITY)
153                 s = "humidity";
154         else if (analog->mq == SR_MQ_SOUND_PRESSURE_LEVEL)
155                 s = "spl";
156         else
157                 s = "gauge";
159         return s;
162 static void sigrok_feed_callback(const struct sr_dev_inst *sdi,
163                 const struct sr_datafeed_packet *packet, void *cb_data)
165         const struct sr_datafeed_analog *analog;
166         struct config_device *cfdev;
167         GSList *l;
168         value_t value;
169         value_list_t vl = VALUE_LIST_INIT;
171         /* Find this device's configuration. */
172         cfdev = NULL;
173         for (l = config_devices; l; l = l->next) {
174                 cfdev = l->data;
175                 if (cfdev->sdi == sdi) {
176                         /* Found it. */
177                         break;
178                 }
179                 cfdev = NULL;
180         }
182         if (!cfdev) {
183                 ERROR("sigrok plugin: Received data from driver \"%s\" but "
184                                 "can't find a configuration / device matching "
185                                 "it.", sdi->driver->name);
186                 return;
187         }
189         if (packet->type == SR_DF_END) {
190                 /* TODO: try to restart acquisition after a delay? */
191                 WARNING("sigrok plugin: acquisition for \"%s\" ended.",
192                                 cfdev->name);
193                 return;
194         }
196         if (packet->type != SR_DF_ANALOG)
197                 return;
199         if ((cfdev->min_dispatch_interval != 0)
200                         && ((cdtime() - cfdev->last_dispatch)
201                                 < cfdev->min_dispatch_interval))
202                 return;
204         /* Ignore all but the first sample on the first probe. */
205         analog = packet->payload;
206         value.gauge = analog->data[0];
207         vl.values = &value;
208         vl.values_len = 1;
209         sstrncpy(vl.host, hostname_g, sizeof(vl.host));
210         sstrncpy(vl.plugin, "sigrok", sizeof(vl.plugin));
211         ssnprintf(vl.plugin_instance, sizeof(vl.plugin_instance),
212                         "%s", cfdev->name);
213         sstrncpy(vl.type, sigrok_value_type(analog), sizeof(vl.type));
215         plugin_dispatch_values(&vl);
216         cfdev->last_dispatch = cdtime();
219 static void sigrok_free_drvopts(struct sr_config *src)
221         g_variant_unref(src->data);
222         g_free(src);
225 static int sigrok_init_driver(struct config_device *cfdev,
226                 struct sr_dev_driver *drv)
228         struct sr_config *src;
229         GSList *devlist, *drvopts;
230         char hwident[512];
232         if (sr_driver_init(sr_ctx, drv) != SR_OK)
233                 /* Error was logged by libsigrok. */
234                 return -1;
236         drvopts = NULL;
237         if (cfdev->conn) {
238                 if (!(src = malloc(sizeof(*src))))
239                         return -1;
240                 src->key = SR_CONF_CONN;
241                 src->data = g_variant_new_string(cfdev->conn);
242                 drvopts = g_slist_append(drvopts, src);
243         }
244         if (cfdev->serialcomm) {
245                 if (!(src = malloc(sizeof(*src))))
246                         return -1;
247                 src->key = SR_CONF_SERIALCOMM;
248                 src->data = g_variant_new_string(cfdev->serialcomm);
249                 drvopts = g_slist_append(drvopts, src);
250         }
251         devlist = sr_driver_scan(drv, drvopts);
252         g_slist_free_full(drvopts, (GDestroyNotify)sigrok_free_drvopts);
253         if (!devlist) {
254                 /* Not an error, but the user should know about it. */
255                 WARNING("sigrok plugin: No device found for \"%s\".",
256                                 cfdev->name);
257                 return 0;
258         }
260         if (g_slist_length(devlist) > 1) {
261                 INFO("sigrok plugin: %d sigrok devices for device entry "
262                                 "\"%s\": must be 1.",
263                                 g_slist_length(devlist), cfdev->name);
264                 return -1;
265         }
266         cfdev->sdi = devlist->data;
267         g_slist_free(devlist);
268         ssnprintf(hwident, sizeof(hwident), "%s %s %s",
269                         cfdev->sdi->vendor ? cfdev->sdi->vendor : "",
270                         cfdev->sdi->model ? cfdev->sdi->model : "",
271                         cfdev->sdi->version ? cfdev->sdi->version : "");
272         INFO("sigrok plugin: Device \"%s\" is a %s", cfdev->name, hwident);
274         if (sr_dev_open(cfdev->sdi) != SR_OK)
275                 return -1;
277         if (sr_session_dev_add(cfdev->sdi) != SR_OK)
278                 return -1;
280         return 1;
283 static void *sigrok_read_thread(void *arg __attribute__((unused)))
285         struct sr_dev_driver *drv, **drvlist;
286         GSList *l;
287         struct config_device *cfdev;
288         int ret, i;
290         sr_log_callback_set(sigrok_log_callback, NULL);
291         sr_log_loglevel_set(loglevel);
293         if ((ret = sr_init(&sr_ctx)) != SR_OK) {
294                 ERROR("sigrok plugin: Failed to initialize libsigrok: %s.",
295                                 sr_strerror(ret));
296                 return NULL;
297         }
299         if (!sr_session_new())
300                 return NULL;
302         num_devices = 0;
303         drvlist = sr_driver_list();
304         for (l = config_devices; l; l = l->next) {
305                 cfdev = l->data;
306                 drv = NULL;
307                 for (i = 0; drvlist[i]; i++) {
308                         if (!strcmp(drvlist[i]->name, cfdev->driver)) {
309                                 drv = drvlist[i];
310                                 break;
311                         }
312                 }
313                 if (!drv) {
314                         ERROR("sigrok plugin: Unknown driver \"%s\".",
315                                         cfdev->driver);
316                         return NULL;
317                 }
319                 if ((ret = sigrok_init_driver(cfdev, drv)) < 0)
320                         /* Error was already logged. */
321                         return NULL;
323                 num_devices += ret;
324         }
326         if (num_devices > 0) {
327                 /* Do this only when we're sure there's hardware to talk to. */
328                 if (sr_session_datafeed_callback_add(sigrok_feed_callback, NULL)
329                                 != SR_OK)
330                         return NULL;
332                 /* Start acquisition on all devices. */
333                 if (sr_session_start() != SR_OK)
334                         return NULL;
336                 /* Main loop, runs forever. */
337                 sr_session_run();
339                 sr_session_stop();
340                 sr_session_dev_remove_all();
341         }
343         sr_session_destroy();
345         sr_exit(sr_ctx);
347         pthread_exit(NULL);
348         sr_thread_running = FALSE;
350         return NULL;
353 static int sigrok_init(void)
355         int status;
357         if (sr_thread_running) {
358                 ERROR("sigrok plugin: Thread already running.");
359                 return -1;
360         }
362         status = plugin_thread_create(&sr_thread, NULL, sigrok_read_thread,
363                         NULL);
364         if (status != 0)
365         {
366                 char errbuf[1024];
367                 ERROR("sigrok plugin: Failed to create thread: %s.",
368                                 sstrerror (errno, errbuf, sizeof (errbuf)));
369                 return -1;
370         }
371         sr_thread_running = TRUE;
373         return 0;
376 static int sigrok_shutdown(void)
378         struct config_device *cfdev;
379         GSList *l;
381         if (sr_thread_running) {
382                 pthread_cancel(sr_thread);
383                 pthread_join(sr_thread, NULL);
384         }
386         for (l = config_devices; l; l = l->next) {
387                 cfdev = l->data;
388                 free(cfdev->name);
389                 free(cfdev->driver);
390                 free(cfdev->conn);
391                 free(cfdev->serialcomm);
392                 free(cfdev);
393         }
394         g_slist_free(config_devices);
396         return 0;
399 void module_register(void)
401         plugin_register_complex_config("sigrok", sigrok_config);
402         plugin_register_init("sigrok", sigrok_init);
403         plugin_register_shutdown("sigrok", sigrok_shutdown);