Code

9cb5e30aa96e5016f91cc71f9238337525245cbc
[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 struct sr_session *session = NULL;
40 static int num_devices;
41 static int loglevel = SR_LOG_WARN;
42 static struct sr_context *sr_ctx;
44 struct config_device {
45         char *name;
46         char *driver;
47         char *conn;
48         char *serialcomm;
49         struct sr_dev_inst *sdi;
50         cdtime_t min_dispatch_interval;
51         cdtime_t last_dispatch;
52 };
55 static int cd_logger(void *cb_data, int msg_loglevel, const char *format,
56                 va_list args)
57 {
58         char s[512];
60         if (msg_loglevel <= loglevel) {
61                 vsnprintf(s, 512, format, args);
62                 plugin_log(LOG_INFO, "sigrok: %s", s);
63         }
65         return 0;
66 }
68 static int sigrok_config_device(oconfig_item_t *ci)
69 {
70         oconfig_item_t *item;
71         struct config_device *cfdev;
72         int ret, i;
74         if (ci->values_num != 1 || ci->values[0].type != OCONFIG_TYPE_STRING) {
75                 ERROR("Invalid device name.");
76                 return 1;
77         }
79         if (!(cfdev = malloc(sizeof(struct config_device)))) {
80                 ERROR("malloc() failed.");
81                 return 1;
82         }
83         memset(cfdev, 0, sizeof(struct config_device));
84         cf_util_get_string(ci, &cfdev->name);
85         cfdev->min_dispatch_interval = DEFAULT_MIN_DISPATCH_INTERVAL;
87         for (i = 0; i < ci->children_num; i++) {
88                 item = ci->children + i;
89                 if (item->values_num != 1) {
90                         ERROR("Missing value for '%s'.", item->key);
91                         return 1;
92                 }
93                 if (!strcasecmp(item->key, "driver"))
94                         ret = cf_util_get_string(item, &cfdev->driver);
95                 else if (!strcasecmp(item->key, "conn"))
96                         ret = cf_util_get_string(item, &cfdev->conn);
97                 else if (!strcasecmp(item->key, "serialcomm"))
98                         ret = cf_util_get_string(item, &cfdev->serialcomm);
99                 else if (!strcasecmp(item->key, "interval"))
100                         ret = cf_util_get_cdtime(item, &cfdev->min_dispatch_interval);
101                 if (ret) {
102                         ERROR("Invalid keyword '%s'.", item->key);
103                         return 1;
104                 }
105         }
107         config_devices = g_slist_append(config_devices, cfdev);
109         return 0;
112 static int sigrok_config(oconfig_item_t *ci)
114         oconfig_item_t *item;
115         int tmp, i;
117         for (i = 0; i < ci->children_num; i++) {
118                 item = ci->children + i;
119                 if (!strcasecmp(item->key, "loglevel")) {
120                         if (cf_util_get_int(item, &tmp) || tmp < 0 || tmp > 5) {
121                                 ERROR("Invalid loglevel");
122                                 return 1;
123                         }
124                         loglevel = tmp;
125                 } else if (!strcasecmp(item->key, "Device")) {
126                         if (sigrok_config_device(item) != 0)
127                                 return 1;
128                 } else {
129                         ERROR("Invalid keyword '%s'.", item->key);
130                         return 1;
131                 }
132         }
134         return 0;
137 static void free_drvopts(struct sr_config *src)
139         g_variant_unref(src->data);
140         g_free(src);
143 static void sigrok_feed_callback(const struct sr_dev_inst *sdi,
144                 const struct sr_datafeed_packet *packet, void *cb_data)
146         const struct sr_datafeed_analog *analog;
147         struct config_device *cfdev;
148         GSList *l;
149         value_t *values;
150         value_list_t vl = VALUE_LIST_INIT;
151         int num_probes, s, p;
153         if (packet->type == SR_DF_END) {
154                 /* TODO: try to restart acquisition after a delay? */
155                 INFO("oops! ended");
156                 return;
157         }
159         /* Find this device's configuration. */
160         cfdev = NULL;
161         for (l = config_devices; l; l = l->next) {
162                 cfdev = l->data;
163                 if (cfdev->sdi == sdi) {
164                         /* Found it. */
165                         break;
166                 }
167                 cfdev = NULL;
168         }
169         if (!cfdev) {
170                 ERROR("Unknown device instance in sigrok driver %s.", sdi->driver->name);
171                 return;
172         }
174         if (packet->type == SR_DF_ANALOG) {
175                 if (cdtime() - cfdev->last_dispatch < cfdev->min_dispatch_interval)
176                         return;
178                 analog = packet->payload;
179                 num_probes = g_slist_length(analog->probes);
180                 if (!(values = malloc(sizeof(value_t) * num_probes))) {
181                         ERROR("malloc() failed.");
182                         return;
183                 }
184                 for (s = 0; s < analog->num_samples; s++) {
185                         for (p = 0; p < num_probes; p++) {
186                                 values[s + p].gauge = analog->data[s + p];
187                         }
188                 }
189                 vl.values = values;
190                 vl.values_len = num_probes;
191                 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
192                 sstrncpy(vl.plugin, "sigrok", sizeof(vl.plugin));
193                 ssnprintf(vl.plugin_instance, sizeof(vl.plugin_instance),
194                                 "%s", cfdev->name);
195                 sstrncpy(vl.type, "gauge", sizeof(vl.type));
196                 plugin_dispatch_values(&vl);
198                 cfdev->last_dispatch = cdtime();
199         }
203 static void *thread_init(void *arg)
205         struct sr_dev_driver *drv, **drvlist;
206         struct sr_config *src;
207         GSList *devlist, *drvopts, *l;
208         struct config_device *cfdev;
209         int ret, i;
210         char hwident[512];
212         (void)arg;
214         sr_log_callback_set(cd_logger, NULL);
215         sr_log_loglevel_set(loglevel);
217         if ((ret = sr_init(&sr_ctx)) != SR_OK) {
218                 ERROR("Failed to initialize libsigrok: %s.", sr_strerror(ret));
219                 return NULL;
220         }
222         if (!(session = sr_session_new()))
223                 return NULL;
225         num_devices = 0;
226         drvlist = sr_driver_list();
227         for (l = config_devices; l; l = l->next) {
228                 cfdev = l->data;
229                 drv = NULL;
230                 for (i = 0; drvlist[i]; i++) {
231                         if (!strcmp(drvlist[i]->name, cfdev->driver)) {
232                                 drv = drvlist[i];
233                                 break;
234                         }
235                 }
236                 if (!drv) {
237                         ERROR("sigrok: Unknown driver '%s'.", cfdev->driver);
238                         return NULL;
239                 }
241                 if (sr_driver_init(sr_ctx, drv) != SR_OK)
242                         return NULL;
244                 drvopts = NULL;
245                 if (cfdev->conn) {
246                         if (!(src = malloc(sizeof(struct sr_config))))
247                                 return NULL;
248                         src->key = SR_CONF_CONN;
249                         src->data = g_variant_new_string(cfdev->conn);
250                         drvopts = g_slist_append(drvopts, src);
251                 }
252                 if (cfdev->serialcomm) {
253                         if (!(src = malloc(sizeof(struct sr_config))))
254                                 return NULL;
255                         src->key = SR_CONF_SERIALCOMM;
256                         src->data = g_variant_new_string(cfdev->serialcomm);
257                         drvopts = g_slist_append(drvopts, src);
258                 }
259                 devlist = sr_driver_scan(drv, drvopts);
260                 g_slist_free_full(drvopts, (GDestroyNotify)free_drvopts);
261                 if (!devlist)
262                         /* No devices found for this driver. */
263                         continue;
265                 if (g_slist_length(devlist) > 1) {
266                         INFO("sigrok: %d sigrok devices for device entry '%s': must be 1.",
267                                         g_slist_length(devlist), cfdev->name);
268                         return NULL;
269                 }
270                 cfdev->sdi = devlist->data;
271                 g_slist_free(devlist);
272                 ssnprintf(hwident, sizeof(hwident), "%s %s %s",
273                                 cfdev->sdi->vendor ? cfdev->sdi->vendor : "",
274                                 cfdev->sdi->model ? cfdev->sdi->model : "",
275                                 cfdev->sdi->version ? cfdev->sdi->version : "");
276                 INFO("sigrok: Device '%s' is a %s.", cfdev->name, hwident);
278                 if (sr_dev_open(cfdev->sdi) != SR_OK)
279                         return NULL;
281                 if (sr_session_dev_add(cfdev->sdi) != SR_OK)
282                         return NULL;
284                 num_devices++;
285         }
287         if (num_devices > 0) {
288                 /* Do this only when we're sure there's hardware to talk to. */
289                 if (sr_session_datafeed_callback_add(sigrok_feed_callback, NULL) != SR_OK)
290                         return NULL;
292                 /* Start acquisition on all devices. */
293                 if (sr_session_start() != SR_OK)
294                         return NULL;
296                 /* Main loop, runs forever. */
297                 sr_session_run();
299                 sr_session_stop();
300                 sr_session_dev_remove_all();
301         }
303         sr_session_destroy();
305         sr_exit(sr_ctx);
307         pthread_exit(NULL);
308         sr_thread_running = FALSE;
310         return NULL;
313 static int sigrok_init(void)
315         int status;
317         if (sr_thread_running) {
318                 ERROR("sigrok: Thread already running.");
319                 return -1;
320         }
322         if ((status = plugin_thread_create(&sr_thread, NULL, thread_init, NULL)) != 0) {
323                 ERROR("sigrok: Failed to create thread: %s.", strerror(status));
324                 return -1;
325         }
326         sr_thread_running = TRUE;
328         return 0;
331 static int sigrok_shutdown(void)
333         struct config_device *cfdev;
334         GSList *l;
336         if (sr_thread_running) {
337                 pthread_cancel(sr_thread);
338                 pthread_join(sr_thread, NULL);
339         }
341         for (l = config_devices; l; l = l->next) {
342                 cfdev = l->data;
343                 free(cfdev->name);
344                 free(cfdev->driver);
345                 free(cfdev->conn);
346                 free(cfdev->serialcomm);
347                 free(cfdev);
348         }
349         g_slist_free(config_devices);
351         return 0;
354 void module_register(void)
357         plugin_register_complex_config("sigrok", sigrok_config);
358         plugin_register_init("sigrok", sigrok_init);
359         plugin_register_shutdown("sigrok", sigrok_shutdown);