Code

9dc8831781f067c9216c5084fcd1c3b1b82bfbe5
[collectd.git] / src / sigrok.c
1 /*
2  * collectd - src/sigrok.c
3  * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
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  */
19 #include "collectd.h"
20 #include "common.h"
21 #include "plugin.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <time.h>
27 #include <pthread.h>
29 #include <glib.h>
30 #include <libsigrok/libsigrok.h>
32 /* Minimum interval between dispatches coming from this plugin. The RRD
33  * plugin, at least, complains when written to with sub-second intervals.*/
34 #define DEFAULT_MIN_DISPATCH_INTERVAL TIME_T_TO_CDTIME_T(1)
36 static pthread_t sr_thread;
37 static int sr_thread_running = FALSE;
38 GSList *config_devices;
39 static int num_devices;
40 static int loglevel = SR_LOG_WARN;
41 static struct sr_context *sr_ctx;
43 struct config_device {
44         char *name;
45         char *driver;
46         char *conn;
47         char *serialcomm;
48         struct sr_dev_inst *sdi;
49         cdtime_t min_dispatch_interval;
50         cdtime_t last_dispatch;
51 };
54 static int sigrok_log_callback(void*cb_data __attribute__((unused)),
55                 int msg_loglevel, const char *format, va_list args)
56 {
57         char s[512];
59         if (msg_loglevel <= loglevel) {
60                 vsnprintf(s, 512, format, args);
61                 plugin_log(LOG_INFO, "sigrok: %s", s);
62         }
64         return 0;
65 }
67 static int sigrok_config_device(oconfig_item_t *ci)
68 {
69         oconfig_item_t *item;
70         struct config_device *cfdev;
71         int i;
73         if (!(cfdev = malloc(sizeof(struct config_device)))) {
74                 ERROR("malloc() failed.");
75                 return 1;
76         }
77         memset(cfdev, 0, sizeof(*cfdev));
78         if (cf_util_get_string(ci, &cfdev->name)) {
79                 free(cfdev);
80                 WARNING("Invalid device name.");
81                 return 1;
82         }
83         cfdev->min_dispatch_interval = DEFAULT_MIN_DISPATCH_INTERVAL;
85         for (i = 0; i < ci->children_num; i++) {
86                 item = ci->children + i;
87                 if (item->values_num != 1) {
88                         free(cfdev);
89                         WARNING("Missing value for '%s'.", item->key);
90                         return 1;
91                 }
92                 if (!strcasecmp(item->key, "driver"))
93                         cf_util_get_string(item, &cfdev->driver);
94                 else if (!strcasecmp(item->key, "conn"))
95                         cf_util_get_string(item, &cfdev->conn);
96                 else if (!strcasecmp(item->key, "serialcomm"))
97                         cf_util_get_string(item, &cfdev->serialcomm);
98                 else if (!strcasecmp(item->key, "interval"))
99                         cf_util_get_cdtime(item, &cfdev->min_dispatch_interval);
100                 else
101                         WARNING("Invalid keyword '%s'.", item->key);
102         }
104         config_devices = g_slist_append(config_devices, cfdev);
106         return 0;
109 static int sigrok_config(oconfig_item_t *ci)
111         oconfig_item_t *item;
112         int tmp, i;
114         for (i = 0; i < ci->children_num; i++) {
115                 item = ci->children + i;
116                 if (!strcasecmp(item->key, "loglevel")) {
117                         if (cf_util_get_int(item, &tmp) || tmp < 0 || tmp > 5) {
118                                 ERROR("Invalid loglevel");
119                                 continue;
120                         }
121                         loglevel = tmp;
122                 } else if (!strcasecmp(item->key, "Device"))
123                         sigrok_config_device(item);
124                 else
125                         WARNING("Invalid keyword '%s'.", item->key);
126         }
128         return 0;
131 static void free_drvopts(struct sr_config *src)
133         g_variant_unref(src->data);
134         g_free(src);
137 static void sigrok_feed_callback(const struct sr_dev_inst *sdi,
138                 const struct sr_datafeed_packet *packet, void *cb_data)
140         const struct sr_datafeed_analog *analog;
141         struct config_device *cfdev;
142         GSList *l;
143         value_t value;
144         value_list_t vl = VALUE_LIST_INIT;
146         /* Find this device's configuration. */
147         cfdev = NULL;
148         for (l = config_devices; l; l = l->next) {
149                 cfdev = l->data;
150                 if (cfdev->sdi == sdi) {
151                         /* Found it. */
152                         break;
153                 }
154                 cfdev = NULL;
155         }
156         if (!cfdev) {
157                 ERROR("Unknown device instance in sigrok driver %s.", sdi->driver->name);
158                 return;
159         }
161         if (packet->type == SR_DF_END) {
162                 /* TODO: try to restart acquisition after a delay? */
163                 INFO("sigrok: acquisition for '%s' ended.", cfdev->name);
164                 return;
165         }
167         if (packet->type != SR_DF_ANALOG)
168                 return;
170         if (cdtime() - cfdev->last_dispatch < cfdev->min_dispatch_interval)
171                 return;
173         /* Ignore all but the first sample on the first probe. */
174         analog = packet->payload;
175         value.gauge = analog->data[0];
176         vl.values = &value;
177         vl.values_len = 1;
178         sstrncpy(vl.host, hostname_g, sizeof(vl.host));
179         sstrncpy(vl.plugin, "sigrok", sizeof(vl.plugin));
180         ssnprintf(vl.plugin_instance, sizeof(vl.plugin_instance),
181                         "%s", cfdev->name);
182         sstrncpy(vl.type, "gauge", sizeof(vl.type));
183         plugin_dispatch_values(&vl);
185         cfdev->last_dispatch = cdtime();
189 static int sigrok_init_driver(struct config_device *cfdev,
190                 struct sr_dev_driver *drv)
192         struct sr_config *src;
193         GSList *devlist, *drvopts;
194         char hwident[512];
196         if (sr_driver_init(sr_ctx, drv) != SR_OK)
197                 /* Error was logged by libsigrok. */
198                 return -1;
200         drvopts = NULL;
201         if (cfdev->conn) {
202                 if (!(src = malloc(sizeof(struct sr_config))))
203                         return -1;
204                 src->key = SR_CONF_CONN;
205                 src->data = g_variant_new_string(cfdev->conn);
206                 drvopts = g_slist_append(drvopts, src);
207         }
208         if (cfdev->serialcomm) {
209                 if (!(src = malloc(sizeof(struct sr_config))))
210                         return -1;
211                 src->key = SR_CONF_SERIALCOMM;
212                 src->data = g_variant_new_string(cfdev->serialcomm);
213                 drvopts = g_slist_append(drvopts, src);
214         }
215         devlist = sr_driver_scan(drv, drvopts);
216         g_slist_free_full(drvopts, (GDestroyNotify)free_drvopts);
217         if (!devlist)
218                 /* No devices found for this driver, not an error. */
219                 return 0;
221         if (g_slist_length(devlist) > 1) {
222                 INFO("sigrok: %d sigrok devices for device entry '%s': must be 1.",
223                                 g_slist_length(devlist), cfdev->name);
224                 return -1;
225         }
226         cfdev->sdi = devlist->data;
227         g_slist_free(devlist);
228         ssnprintf(hwident, sizeof(hwident), "%s %s %s",
229                         cfdev->sdi->vendor ? cfdev->sdi->vendor : "",
230                         cfdev->sdi->model ? cfdev->sdi->model : "",
231                         cfdev->sdi->version ? cfdev->sdi->version : "");
232         INFO("sigrok: Device '%s' is a %s", cfdev->name, hwident);
234         if (sr_dev_open(cfdev->sdi) != SR_OK)
235                 return -1;
237         if (sr_session_dev_add(cfdev->sdi) != SR_OK)
238                 return -1;
240         return 1;
243 static void *thread_init(void *arg __attribute__((unused)))
245         struct sr_dev_driver *drv, **drvlist;
246         GSList *l;
247         struct config_device *cfdev;
248         int ret, i;
250         sr_log_callback_set(sigrok_log_callback, NULL);
251         sr_log_loglevel_set(loglevel);
253         if ((ret = sr_init(&sr_ctx)) != SR_OK) {
254                 ERROR("Failed to initialize libsigrok: %s.", sr_strerror(ret));
255                 return NULL;
256         }
258         if (!sr_session_new())
259                 return NULL;
261         num_devices = 0;
262         drvlist = sr_driver_list();
263         for (l = config_devices; l; l = l->next) {
264                 cfdev = l->data;
265                 drv = NULL;
266                 for (i = 0; drvlist[i]; i++) {
267                         if (!strcmp(drvlist[i]->name, cfdev->driver)) {
268                                 drv = drvlist[i];
269                                 break;
270                         }
271                 }
272                 if (!drv) {
273                         ERROR("sigrok: Unknown driver '%s'.", cfdev->driver);
274                         return NULL;
275                 }
277                 if ((ret = sigrok_init_driver(cfdev, drv)) < 0)
278                         /* Error was already logged. */
279                         return NULL;
281                 num_devices += ret;
282         }
284         if (num_devices > 0) {
285                 /* Do this only when we're sure there's hardware to talk to. */
286                 if (sr_session_datafeed_callback_add(sigrok_feed_callback, NULL) != SR_OK)
287                         return NULL;
289                 /* Start acquisition on all devices. */
290                 if (sr_session_start() != SR_OK)
291                         return NULL;
293                 /* Main loop, runs forever. */
294                 sr_session_run();
296                 sr_session_stop();
297                 sr_session_dev_remove_all();
298         }
300         sr_session_destroy();
302         sr_exit(sr_ctx);
304         pthread_exit(NULL);
305         sr_thread_running = FALSE;
307         return NULL;
310 static int sigrok_init(void)
312         int status;
314         if (sr_thread_running) {
315                 ERROR("sigrok: Thread already running.");
316                 return -1;
317         }
319         if ((status = plugin_thread_create(&sr_thread, NULL, thread_init, NULL)) != 0) {
320                 ERROR("sigrok: Failed to create thread: %s.", strerror(status));
321                 return -1;
322         }
323         sr_thread_running = TRUE;
325         return 0;
328 static int sigrok_shutdown(void)
330         struct config_device *cfdev;
331         GSList *l;
333         if (sr_thread_running) {
334                 pthread_cancel(sr_thread);
335                 pthread_join(sr_thread, NULL);
336         }
338         for (l = config_devices; l; l = l->next) {
339                 cfdev = l->data;
340                 free(cfdev->name);
341                 free(cfdev->driver);
342                 free(cfdev->conn);
343                 free(cfdev->serialcomm);
344                 free(cfdev);
345         }
346         g_slist_free(config_devices);
348         return 0;
351 void module_register(void)
354         plugin_register_complex_config("sigrok", sigrok_config);
355         plugin_register_init("sigrok", sigrok_init);
356         plugin_register_shutdown("sigrok", sigrok_shutdown);