2 * collectd - src/sigrok.c
3 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
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(0)
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 {
48 struct sr_dev_inst *sdi;
49 cdtime_t min_dispatch_interval;
50 cdtime_t last_dispatch;
54 static int sigrok_log_callback(void*cb_data __attribute__((unused)),
55 int msg_loglevel, const char *format, va_list args)
59 if (msg_loglevel <= loglevel) {
60 vsnprintf(s, 512, format, args);
61 plugin_log(LOG_INFO, "sigrok plugin: %s", s);
67 static int sigrok_config_device(oconfig_item_t *ci)
69 struct config_device *cfdev;
72 if (!(cfdev = malloc(sizeof(struct config_device)))) {
73 ERROR("sigrok plugin: malloc() failed.");
76 memset(cfdev, 0, sizeof(*cfdev));
77 if (cf_util_get_string(ci, &cfdev->name)) {
79 WARNING("sigrok plugin: Invalid device name.");
82 cfdev->min_dispatch_interval = DEFAULT_MIN_DISPATCH_INTERVAL;
84 for (i = 0; i < ci->children_num; i++) {
85 oconfig_item_t *item = ci->children + i;
86 if (!strcasecmp(item->key, "driver"))
87 cf_util_get_string(item, &cfdev->driver);
88 else if (!strcasecmp(item->key, "conn"))
89 cf_util_get_string(item, &cfdev->conn);
90 else if (!strcasecmp(item->key, "serialcomm"))
91 cf_util_get_string(item, &cfdev->serialcomm);
92 else if (!strcasecmp(item->key, "minimuminterval"))
93 cf_util_get_cdtime(item, &cfdev->min_dispatch_interval);
95 WARNING("sigrok plugin: Invalid keyword \"%s\".",
99 config_devices = g_slist_append(config_devices, cfdev);
104 static int sigrok_config(oconfig_item_t *ci)
108 for (i = 0; i < ci->children_num; i++) {
109 oconfig_item_t *item = ci->children + i;
110 if (strcasecmp("LogLevel", item->key) == 0) {
114 status = cf_util_get_int (item, &tmp);
117 else if ((tmp < 0) || (tmp > 5)) {
118 ERROR ("sigrok plugin: The \"LogLevel\" "
119 "configuration option expects "
120 "an integer between 0 and 5 "
121 "(inclusive); you provided %i.",
126 } else if (!strcasecmp(item->key, "Device"))
127 sigrok_config_device(item);
129 WARNING("sigrok plugin: Invalid keyword \"%s\".",
136 static char *sigrok_value_type(const struct sr_datafeed_analog *analog)
140 if (analog->mq == SR_MQ_VOLTAGE)
142 else if (analog->mq == SR_MQ_CURRENT)
144 else if (analog->mq == SR_MQ_FREQUENCY)
146 else if (analog->mq == SR_MQ_POWER)
148 else if (analog->mq == SR_MQ_TEMPERATURE)
150 else if (analog->mq == SR_MQ_RELATIVE_HUMIDITY)
152 else if (analog->mq == SR_MQ_SOUND_PRESSURE_LEVEL)
160 static void sigrok_feed_callback(const struct sr_dev_inst *sdi,
161 const struct sr_datafeed_packet *packet, void *cb_data)
163 const struct sr_datafeed_analog *analog;
164 struct config_device *cfdev;
167 value_list_t vl = VALUE_LIST_INIT;
169 /* Find this device's configuration. */
171 for (l = config_devices; l; l = l->next) {
173 if (cfdev->sdi == sdi) {
181 ERROR("sigrok plugin: Received data from driver \"%s\" but "
182 "can't find a configuration / device matching "
183 "it.", sdi->driver->name);
187 if (packet->type == SR_DF_END) {
188 /* TODO: try to restart acquisition after a delay? */
189 WARNING("sigrok plugin: acquisition for \"%s\" ended.",
194 if (packet->type != SR_DF_ANALOG)
197 if ((cfdev->min_dispatch_interval != 0)
198 && ((cdtime() - cfdev->last_dispatch)
199 < cfdev->min_dispatch_interval))
202 /* Ignore all but the first sample on the first probe. */
203 analog = packet->payload;
204 value.gauge = analog->data[0];
207 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
208 sstrncpy(vl.plugin, "sigrok", sizeof(vl.plugin));
209 ssnprintf(vl.plugin_instance, sizeof(vl.plugin_instance),
211 sstrncpy(vl.type, sigrok_value_type(analog), sizeof(vl.type));
213 plugin_dispatch_values(&vl);
214 cfdev->last_dispatch = cdtime();
217 static void sigrok_free_drvopts(struct sr_config *src)
219 g_variant_unref(src->data);
223 static int sigrok_init_driver(struct config_device *cfdev,
224 struct sr_dev_driver *drv)
226 struct sr_config *src;
227 GSList *devlist, *drvopts;
230 if (sr_driver_init(sr_ctx, drv) != SR_OK)
231 /* Error was logged by libsigrok. */
236 if (!(src = malloc(sizeof(struct sr_config))))
238 src->key = SR_CONF_CONN;
239 src->data = g_variant_new_string(cfdev->conn);
240 drvopts = g_slist_append(drvopts, src);
242 if (cfdev->serialcomm) {
243 if (!(src = malloc(sizeof(struct sr_config))))
245 src->key = SR_CONF_SERIALCOMM;
246 src->data = g_variant_new_string(cfdev->serialcomm);
247 drvopts = g_slist_append(drvopts, src);
249 devlist = sr_driver_scan(drv, drvopts);
250 g_slist_free_full(drvopts, (GDestroyNotify)sigrok_free_drvopts);
252 /* Not an error, but the user should know about it. */
253 WARNING("sigrok plugin: No device found for \"%s\".",
258 if (g_slist_length(devlist) > 1) {
259 INFO("sigrok plugin: %d sigrok devices for device entry "
260 "\"%s\": must be 1.",
261 g_slist_length(devlist), cfdev->name);
264 cfdev->sdi = devlist->data;
265 g_slist_free(devlist);
266 ssnprintf(hwident, sizeof(hwident), "%s %s %s",
267 cfdev->sdi->vendor ? cfdev->sdi->vendor : "",
268 cfdev->sdi->model ? cfdev->sdi->model : "",
269 cfdev->sdi->version ? cfdev->sdi->version : "");
270 INFO("sigrok plugin: Device \"%s\" is a %s", cfdev->name, hwident);
272 if (sr_dev_open(cfdev->sdi) != SR_OK)
275 if (sr_session_dev_add(cfdev->sdi) != SR_OK)
281 static void *sigrok_read_thread(void *arg __attribute__((unused)))
283 struct sr_dev_driver *drv, **drvlist;
285 struct config_device *cfdev;
288 sr_log_callback_set(sigrok_log_callback, NULL);
289 sr_log_loglevel_set(loglevel);
291 if ((ret = sr_init(&sr_ctx)) != SR_OK) {
292 ERROR("sigrok plugin: Failed to initialize libsigrok: %s.",
297 if (!sr_session_new())
301 drvlist = sr_driver_list();
302 for (l = config_devices; l; l = l->next) {
305 for (i = 0; drvlist[i]; i++) {
306 if (!strcmp(drvlist[i]->name, cfdev->driver)) {
312 ERROR("sigrok plugin: Unknown driver \"%s\".",
317 if ((ret = sigrok_init_driver(cfdev, drv)) < 0)
318 /* Error was already logged. */
324 if (num_devices > 0) {
325 /* Do this only when we're sure there's hardware to talk to. */
326 if (sr_session_datafeed_callback_add(sigrok_feed_callback, NULL)
330 /* Start acquisition on all devices. */
331 if (sr_session_start() != SR_OK)
334 /* Main loop, runs forever. */
338 sr_session_dev_remove_all();
341 sr_session_destroy();
346 sr_thread_running = FALSE;
351 static int sigrok_init(void)
355 if (sr_thread_running) {
356 ERROR("sigrok plugin: Thread already running.");
360 if ((status = plugin_thread_create(&sr_thread, NULL, sigrok_read_thread,
362 ERROR("sigrok plugin: Failed to create thread: %s.",
366 sr_thread_running = TRUE;
371 static int sigrok_shutdown(void)
373 struct config_device *cfdev;
376 if (sr_thread_running) {
377 pthread_cancel(sr_thread);
378 pthread_join(sr_thread, NULL);
381 for (l = config_devices; l; l = l->next) {
386 free(cfdev->serialcomm);
389 g_slist_free(config_devices);
394 void module_register(void)
396 plugin_register_complex_config("sigrok", sigrok_config);
397 plugin_register_init("sigrok", sigrok_init);
398 plugin_register_shutdown("sigrok", sigrok_shutdown);