2 * collectd - src/sigrok.c
3 * Copyright (C) 2013 Bert Vermeulen
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/>.
19 * Bert Vermeulen <bert at biot.com>
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 {
51 struct sr_dev_inst *sdi;
52 cdtime_t min_dispatch_interval;
53 cdtime_t last_dispatch;
56 static int sigrok_log_callback(void *cb_data __attribute__((unused)),
57 int msg_loglevel, const char *format,
61 if (msg_loglevel <= loglevel) {
62 vsnprintf(s, 512, format, args);
63 plugin_log(LOG_INFO, "sigrok plugin: %s", s);
69 static int sigrok_config_device(oconfig_item_t *ci) {
70 struct config_device *cfdev;
72 if (!(cfdev = calloc(1, sizeof(*cfdev)))) {
73 ERROR("sigrok plugin: calloc failed.");
76 if (cf_util_get_string(ci, &cfdev->name)) {
78 WARNING("sigrok plugin: Invalid device name.");
81 cfdev->min_dispatch_interval = DEFAULT_MIN_DISPATCH_INTERVAL;
83 for (int i = 0; i < ci->children_num; i++) {
84 oconfig_item_t *item = ci->children + i;
85 if (!strcasecmp(item->key, "driver"))
86 cf_util_get_string(item, &cfdev->driver);
87 else if (!strcasecmp(item->key, "conn"))
88 cf_util_get_string(item, &cfdev->conn);
89 else if (!strcasecmp(item->key, "serialcomm"))
90 cf_util_get_string(item, &cfdev->serialcomm);
91 else if (!strcasecmp(item->key, "minimuminterval"))
92 cf_util_get_cdtime(item, &cfdev->min_dispatch_interval);
94 WARNING("sigrok plugin: Invalid keyword \"%s\".", item->key);
97 config_devices = g_slist_append(config_devices, cfdev);
102 static int sigrok_config(oconfig_item_t *ci) {
103 for (int i = 0; i < ci->children_num; i++) {
104 oconfig_item_t *item = ci->children + i;
105 if (strcasecmp("LogLevel", item->key) == 0) {
109 status = cf_util_get_int(item, &tmp);
112 else if ((tmp < 0) || (tmp > 5)) {
113 ERROR("sigrok plugin: The \"LogLevel\" "
114 "configuration option expects "
115 "an integer between 0 and 5 "
116 "(inclusive); you provided %i.",
121 } else if (!strcasecmp(item->key, "Device"))
122 sigrok_config_device(item);
124 WARNING("sigrok plugin: Invalid keyword \"%s\".", item->key);
130 static const char *sigrok_value_type(const struct sr_datafeed_analog *analog) {
133 if (analog->mq == SR_MQ_VOLTAGE)
135 else if (analog->mq == SR_MQ_CURRENT)
137 else if (analog->mq == SR_MQ_FREQUENCY)
139 else if (analog->mq == SR_MQ_POWER)
141 else if (analog->mq == SR_MQ_TEMPERATURE)
143 else if (analog->mq == SR_MQ_RELATIVE_HUMIDITY)
145 else if (analog->mq == SR_MQ_SOUND_PRESSURE_LEVEL)
153 static void sigrok_feed_callback(const struct sr_dev_inst *sdi,
154 const struct sr_datafeed_packet *packet,
156 const struct sr_datafeed_analog *analog;
157 struct config_device *cfdev;
159 value_list_t vl = VALUE_LIST_INIT;
161 /* Find this device's configuration. */
163 for (GSList *l = config_devices; l; l = l->next) {
165 if (cfdev->sdi == sdi) {
173 ERROR("sigrok plugin: Received data from driver \"%s\" but "
174 "can't find a configuration / device matching "
180 if (packet->type == SR_DF_END) {
181 /* TODO: try to restart acquisition after a delay? */
182 WARNING("sigrok plugin: acquisition for \"%s\" ended.", cfdev->name);
186 if (packet->type != SR_DF_ANALOG)
189 if ((cfdev->min_dispatch_interval != 0) &&
190 ((cdtime() - cfdev->last_dispatch) < cfdev->min_dispatch_interval))
193 /* Ignore all but the first sample on the first probe. */
194 analog = packet->payload;
195 value.gauge = analog->data[0];
198 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
199 sstrncpy(vl.plugin, "sigrok", sizeof(vl.plugin));
200 ssnprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s", cfdev->name);
201 sstrncpy(vl.type, sigrok_value_type(analog), sizeof(vl.type));
203 plugin_dispatch_values(&vl);
204 cfdev->last_dispatch = cdtime();
207 static void sigrok_free_drvopts(struct sr_config *src) {
208 g_variant_unref(src->data);
212 static int sigrok_init_driver(struct config_device *cfdev,
213 struct sr_dev_driver *drv) {
214 struct sr_config *src;
215 GSList *devlist, *drvopts;
218 if (sr_driver_init(sr_ctx, drv) != SR_OK)
219 /* Error was logged by libsigrok. */
224 if (!(src = malloc(sizeof(*src))))
226 src->key = SR_CONF_CONN;
227 src->data = g_variant_new_string(cfdev->conn);
228 drvopts = g_slist_append(drvopts, src);
230 if (cfdev->serialcomm) {
231 if (!(src = malloc(sizeof(*src))))
233 src->key = SR_CONF_SERIALCOMM;
234 src->data = g_variant_new_string(cfdev->serialcomm);
235 drvopts = g_slist_append(drvopts, src);
237 devlist = sr_driver_scan(drv, drvopts);
238 g_slist_free_full(drvopts, (GDestroyNotify)sigrok_free_drvopts);
240 /* Not an error, but the user should know about it. */
241 WARNING("sigrok plugin: No device found for \"%s\".", cfdev->name);
245 if (g_slist_length(devlist) > 1) {
246 INFO("sigrok plugin: %d sigrok devices for device entry "
247 "\"%s\": must be 1.",
248 g_slist_length(devlist), cfdev->name);
251 cfdev->sdi = devlist->data;
252 g_slist_free(devlist);
253 ssnprintf(hwident, sizeof(hwident), "%s %s %s",
254 cfdev->sdi->vendor ? cfdev->sdi->vendor : "",
255 cfdev->sdi->model ? cfdev->sdi->model : "",
256 cfdev->sdi->version ? cfdev->sdi->version : "");
257 INFO("sigrok plugin: Device \"%s\" is a %s", cfdev->name, hwident);
259 if (sr_dev_open(cfdev->sdi) != SR_OK)
262 if (sr_session_dev_add(cfdev->sdi) != SR_OK)
268 static void *sigrok_read_thread(void *arg __attribute__((unused))) {
269 struct sr_dev_driver *drv, **drvlist;
271 struct config_device *cfdev;
274 sr_log_callback_set(sigrok_log_callback, NULL);
275 sr_log_loglevel_set(loglevel);
277 if ((ret = sr_init(&sr_ctx)) != SR_OK) {
278 ERROR("sigrok plugin: Failed to initialize libsigrok: %s.",
283 if (!sr_session_new())
287 drvlist = sr_driver_list();
288 for (l = config_devices; l; l = l->next) {
291 for (i = 0; drvlist[i]; i++) {
292 if (!strcmp(drvlist[i]->name, cfdev->driver)) {
298 ERROR("sigrok plugin: Unknown driver \"%s\".", cfdev->driver);
302 if ((ret = sigrok_init_driver(cfdev, drv)) < 0)
303 /* Error was already logged. */
309 if (num_devices > 0) {
310 /* Do this only when we're sure there's hardware to talk to. */
311 if (sr_session_datafeed_callback_add(sigrok_feed_callback, NULL) != SR_OK)
314 /* Start acquisition on all devices. */
315 if (sr_session_start() != SR_OK)
318 /* Main loop, runs forever. */
322 sr_session_dev_remove_all();
325 sr_session_destroy();
330 sr_thread_running = FALSE;
335 static int sigrok_init(void) {
338 if (sr_thread_running) {
339 ERROR("sigrok plugin: Thread already running.");
343 status = plugin_thread_create(&sr_thread, NULL, sigrok_read_thread, NULL);
346 ERROR("sigrok plugin: Failed to create thread: %s.",
347 sstrerror(errno, errbuf, sizeof(errbuf)));
350 sr_thread_running = TRUE;
355 static int sigrok_shutdown(void) {
356 struct config_device *cfdev;
359 if (sr_thread_running) {
360 pthread_cancel(sr_thread);
361 pthread_join(sr_thread, NULL);
364 for (l = config_devices; l; l = l->next) {
369 free(cfdev->serialcomm);
372 g_slist_free(config_devices);
377 void module_register(void) {
378 plugin_register_complex_config("sigrok", sigrok_config);
379 plugin_register_init("sigrok", sigrok_init);
380 plugin_register_shutdown("sigrok", sigrok_shutdown);