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"
24 #include "common.h"
25 #include "plugin.h"
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <time.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;
74 if (!(cfdev = calloc(1, sizeof(*cfdev)))) {
75 ERROR("sigrok plugin: calloc failed.");
76 return -1;
77 }
78 if (cf_util_get_string(ci, &cfdev->name)) {
79 free(cfdev);
80 WARNING("sigrok plugin: Invalid device name.");
81 return -1;
82 }
83 cfdev->min_dispatch_interval = DEFAULT_MIN_DISPATCH_INTERVAL;
85 for (int i = 0; i < ci->children_num; i++) {
86 oconfig_item_t *item = ci->children + i;
87 if (!strcasecmp(item->key, "driver"))
88 cf_util_get_string(item, &cfdev->driver);
89 else if (!strcasecmp(item->key, "conn"))
90 cf_util_get_string(item, &cfdev->conn);
91 else if (!strcasecmp(item->key, "serialcomm"))
92 cf_util_get_string(item, &cfdev->serialcomm);
93 else if (!strcasecmp(item->key, "minimuminterval"))
94 cf_util_get_cdtime(item, &cfdev->min_dispatch_interval);
95 else
96 WARNING("sigrok plugin: Invalid keyword \"%s\".",
97 item->key);
98 }
100 config_devices = g_slist_append(config_devices, cfdev);
102 return 0;
103 }
105 static int sigrok_config(oconfig_item_t *ci)
106 {
107 for (int i = 0; i < ci->children_num; i++) {
108 oconfig_item_t *item = ci->children + i;
109 if (strcasecmp("LogLevel", item->key) == 0) {
110 int status;
111 int tmp = -1;
113 status = cf_util_get_int (item, &tmp);
114 if (status != 0)
115 continue;
116 else if ((tmp < 0) || (tmp > 5)) {
117 ERROR ("sigrok plugin: The \"LogLevel\" "
118 "configuration option expects "
119 "an integer between 0 and 5 "
120 "(inclusive); you provided %i.",
121 tmp);
122 continue;
123 }
124 loglevel = tmp;
125 } else if (!strcasecmp(item->key, "Device"))
126 sigrok_config_device(item);
127 else
128 WARNING("sigrok plugin: Invalid keyword \"%s\".",
129 item->key);
130 }
132 return 0;
133 }
135 static const char *sigrok_value_type(const struct sr_datafeed_analog *analog)
136 {
137 const char *s;
139 if (analog->mq == SR_MQ_VOLTAGE)
140 s = "voltage";
141 else if (analog->mq == SR_MQ_CURRENT)
142 s = "current";
143 else if (analog->mq == SR_MQ_FREQUENCY)
144 s = "frequency";
145 else if (analog->mq == SR_MQ_POWER)
146 s = "power";
147 else if (analog->mq == SR_MQ_TEMPERATURE)
148 s = "temperature";
149 else if (analog->mq == SR_MQ_RELATIVE_HUMIDITY)
150 s = "humidity";
151 else if (analog->mq == SR_MQ_SOUND_PRESSURE_LEVEL)
152 s = "spl";
153 else
154 s = "gauge";
156 return s;
157 }
159 static void sigrok_feed_callback(const struct sr_dev_inst *sdi,
160 const struct sr_datafeed_packet *packet, void *cb_data)
161 {
162 const struct sr_datafeed_analog *analog;
163 struct config_device *cfdev;
164 value_t value;
165 value_list_t vl = VALUE_LIST_INIT;
167 /* Find this device's configuration. */
168 cfdev = NULL;
169 for (GSList *l = config_devices; l; l = l->next) {
170 cfdev = l->data;
171 if (cfdev->sdi == sdi) {
172 /* Found it. */
173 break;
174 }
175 cfdev = NULL;
176 }
178 if (!cfdev) {
179 ERROR("sigrok plugin: Received data from driver \"%s\" but "
180 "can't find a configuration / device matching "
181 "it.", sdi->driver->name);
182 return;
183 }
185 if (packet->type == SR_DF_END) {
186 /* TODO: try to restart acquisition after a delay? */
187 WARNING("sigrok plugin: acquisition for \"%s\" ended.",
188 cfdev->name);
189 return;
190 }
192 if (packet->type != SR_DF_ANALOG)
193 return;
195 if ((cfdev->min_dispatch_interval != 0)
196 && ((cdtime() - cfdev->last_dispatch)
197 < cfdev->min_dispatch_interval))
198 return;
200 /* Ignore all but the first sample on the first probe. */
201 analog = packet->payload;
202 value.gauge = analog->data[0];
203 vl.values = &value;
204 vl.values_len = 1;
205 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
206 sstrncpy(vl.plugin, "sigrok", sizeof(vl.plugin));
207 ssnprintf(vl.plugin_instance, sizeof(vl.plugin_instance),
208 "%s", cfdev->name);
209 sstrncpy(vl.type, sigrok_value_type(analog), sizeof(vl.type));
211 plugin_dispatch_values(&vl);
212 cfdev->last_dispatch = cdtime();
213 }
215 static void sigrok_free_drvopts(struct sr_config *src)
216 {
217 g_variant_unref(src->data);
218 g_free(src);
219 }
221 static int sigrok_init_driver(struct config_device *cfdev,
222 struct sr_dev_driver *drv)
223 {
224 struct sr_config *src;
225 GSList *devlist, *drvopts;
226 char hwident[512];
228 if (sr_driver_init(sr_ctx, drv) != SR_OK)
229 /* Error was logged by libsigrok. */
230 return -1;
232 drvopts = NULL;
233 if (cfdev->conn) {
234 if (!(src = malloc(sizeof(*src))))
235 return -1;
236 src->key = SR_CONF_CONN;
237 src->data = g_variant_new_string(cfdev->conn);
238 drvopts = g_slist_append(drvopts, src);
239 }
240 if (cfdev->serialcomm) {
241 if (!(src = malloc(sizeof(*src))))
242 return -1;
243 src->key = SR_CONF_SERIALCOMM;
244 src->data = g_variant_new_string(cfdev->serialcomm);
245 drvopts = g_slist_append(drvopts, src);
246 }
247 devlist = sr_driver_scan(drv, drvopts);
248 g_slist_free_full(drvopts, (GDestroyNotify)sigrok_free_drvopts);
249 if (!devlist) {
250 /* Not an error, but the user should know about it. */
251 WARNING("sigrok plugin: No device found for \"%s\".",
252 cfdev->name);
253 return 0;
254 }
256 if (g_slist_length(devlist) > 1) {
257 INFO("sigrok plugin: %d sigrok devices for device entry "
258 "\"%s\": must be 1.",
259 g_slist_length(devlist), cfdev->name);
260 return -1;
261 }
262 cfdev->sdi = devlist->data;
263 g_slist_free(devlist);
264 ssnprintf(hwident, sizeof(hwident), "%s %s %s",
265 cfdev->sdi->vendor ? cfdev->sdi->vendor : "",
266 cfdev->sdi->model ? cfdev->sdi->model : "",
267 cfdev->sdi->version ? cfdev->sdi->version : "");
268 INFO("sigrok plugin: Device \"%s\" is a %s", cfdev->name, hwident);
270 if (sr_dev_open(cfdev->sdi) != SR_OK)
271 return -1;
273 if (sr_session_dev_add(cfdev->sdi) != SR_OK)
274 return -1;
276 return 1;
277 }
279 static void *sigrok_read_thread(void *arg __attribute__((unused)))
280 {
281 struct sr_dev_driver *drv, **drvlist;
282 GSList *l;
283 struct config_device *cfdev;
284 int ret, i;
286 sr_log_callback_set(sigrok_log_callback, NULL);
287 sr_log_loglevel_set(loglevel);
289 if ((ret = sr_init(&sr_ctx)) != SR_OK) {
290 ERROR("sigrok plugin: Failed to initialize libsigrok: %s.",
291 sr_strerror(ret));
292 return NULL;
293 }
295 if (!sr_session_new())
296 return NULL;
298 num_devices = 0;
299 drvlist = sr_driver_list();
300 for (l = config_devices; l; l = l->next) {
301 cfdev = l->data;
302 drv = NULL;
303 for (i = 0; drvlist[i]; i++) {
304 if (!strcmp(drvlist[i]->name, cfdev->driver)) {
305 drv = drvlist[i];
306 break;
307 }
308 }
309 if (!drv) {
310 ERROR("sigrok plugin: Unknown driver \"%s\".",
311 cfdev->driver);
312 return NULL;
313 }
315 if ((ret = sigrok_init_driver(cfdev, drv)) < 0)
316 /* Error was already logged. */
317 return NULL;
319 num_devices += ret;
320 }
322 if (num_devices > 0) {
323 /* Do this only when we're sure there's hardware to talk to. */
324 if (sr_session_datafeed_callback_add(sigrok_feed_callback, NULL)
325 != SR_OK)
326 return NULL;
328 /* Start acquisition on all devices. */
329 if (sr_session_start() != SR_OK)
330 return NULL;
332 /* Main loop, runs forever. */
333 sr_session_run();
335 sr_session_stop();
336 sr_session_dev_remove_all();
337 }
339 sr_session_destroy();
341 sr_exit(sr_ctx);
343 pthread_exit(NULL);
344 sr_thread_running = FALSE;
346 return NULL;
347 }
349 static int sigrok_init(void)
350 {
351 int status;
353 if (sr_thread_running) {
354 ERROR("sigrok plugin: Thread already running.");
355 return -1;
356 }
358 status = plugin_thread_create(&sr_thread, NULL, sigrok_read_thread,
359 NULL);
360 if (status != 0)
361 {
362 char errbuf[1024];
363 ERROR("sigrok plugin: Failed to create thread: %s.",
364 sstrerror (errno, errbuf, sizeof (errbuf)));
365 return -1;
366 }
367 sr_thread_running = TRUE;
369 return 0;
370 }
372 static int sigrok_shutdown(void)
373 {
374 struct config_device *cfdev;
375 GSList *l;
377 if (sr_thread_running) {
378 pthread_cancel(sr_thread);
379 pthread_join(sr_thread, NULL);
380 }
382 for (l = config_devices; l; l = l->next) {
383 cfdev = l->data;
384 free(cfdev->name);
385 free(cfdev->driver);
386 free(cfdev->conn);
387 free(cfdev->serialcomm);
388 free(cfdev);
389 }
390 g_slist_free(config_devices);
392 return 0;
393 }
395 void module_register(void)
396 {
397 plugin_register_complex_config("sigrok", sigrok_config);
398 plugin_register_init("sigrok", sigrok_init);
399 plugin_register_shutdown("sigrok", sigrok_shutdown);
400 }