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(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 {
49 struct sr_dev_inst *sdi;
50 cdtime_t min_dispatch_interval;
51 cdtime_t last_dispatch;
55 static int cd_logger(void *cb_data, int msg_loglevel, const char *format,
60 if (msg_loglevel <= loglevel) {
61 vsnprintf(s, 512, format, args);
62 plugin_log(LOG_INFO, "sigrok: %s", s);
68 static int sigrok_config_device(oconfig_item_t *ci)
71 struct config_device *cfdev;
74 if (!(cfdev = malloc(sizeof(struct config_device)))) {
75 ERROR("malloc() failed.");
78 memset(cfdev, 0, sizeof(struct config_device));
79 if (cf_util_get_string(ci, &cfdev->name)) {
80 WARNING("Invalid device name.");
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 WARNING("Missing value for '%s'.", item->key);
91 if (!strcasecmp(item->key, "driver"))
92 cf_util_get_string(item, &cfdev->driver);
93 else if (!strcasecmp(item->key, "conn"))
94 cf_util_get_string(item, &cfdev->conn);
95 else if (!strcasecmp(item->key, "serialcomm"))
96 cf_util_get_string(item, &cfdev->serialcomm);
97 else if (!strcasecmp(item->key, "interval"))
98 cf_util_get_cdtime(item, &cfdev->min_dispatch_interval);
100 WARNING("Invalid keyword '%s'.", item->key);
103 config_devices = g_slist_append(config_devices, cfdev);
108 static int sigrok_config(oconfig_item_t *ci)
110 oconfig_item_t *item;
113 for (i = 0; i < ci->children_num; i++) {
114 item = ci->children + i;
115 if (!strcasecmp(item->key, "loglevel")) {
116 if (cf_util_get_int(item, &tmp) || tmp < 0 || tmp > 5) {
117 ERROR("Invalid loglevel");
121 } else if (!strcasecmp(item->key, "Device"))
122 sigrok_config_device(item);
124 WARNING("Invalid keyword '%s'.", item->key);
130 static void free_drvopts(struct sr_config *src)
132 g_variant_unref(src->data);
136 static void sigrok_feed_callback(const struct sr_dev_inst *sdi,
137 const struct sr_datafeed_packet *packet, void *cb_data)
139 const struct sr_datafeed_analog *analog;
140 struct config_device *cfdev;
143 value_list_t vl = VALUE_LIST_INIT;
144 int num_probes, s, p;
146 if (packet->type == SR_DF_END) {
147 /* TODO: try to restart acquisition after a delay? */
152 /* Find this device's configuration. */
154 for (l = config_devices; l; l = l->next) {
156 if (cfdev->sdi == sdi) {
163 ERROR("Unknown device instance in sigrok driver %s.", sdi->driver->name);
167 if (packet->type == SR_DF_ANALOG) {
168 if (cdtime() - cfdev->last_dispatch < cfdev->min_dispatch_interval)
171 analog = packet->payload;
172 num_probes = g_slist_length(analog->probes);
173 if (!(values = malloc(sizeof(value_t) * num_probes))) {
174 ERROR("malloc() failed.");
177 for (s = 0; s < analog->num_samples; s++) {
178 for (p = 0; p < num_probes; p++) {
179 values[s + p].gauge = analog->data[s + p];
183 vl.values_len = num_probes;
184 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
185 sstrncpy(vl.plugin, "sigrok", sizeof(vl.plugin));
186 ssnprintf(vl.plugin_instance, sizeof(vl.plugin_instance),
188 sstrncpy(vl.type, "gauge", sizeof(vl.type));
189 plugin_dispatch_values(&vl);
191 cfdev->last_dispatch = cdtime();
196 static int sigrok_init_driver(struct config_device *cfdev,
197 struct sr_dev_driver *drv)
199 struct sr_config *src;
200 GSList *devlist, *drvopts;
203 if (sr_driver_init(sr_ctx, drv) != SR_OK)
204 /* Error was logged by libsigrok. */
209 if (!(src = malloc(sizeof(struct sr_config))))
211 src->key = SR_CONF_CONN;
212 src->data = g_variant_new_string(cfdev->conn);
213 drvopts = g_slist_append(drvopts, src);
215 if (cfdev->serialcomm) {
216 if (!(src = malloc(sizeof(struct sr_config))))
218 src->key = SR_CONF_SERIALCOMM;
219 src->data = g_variant_new_string(cfdev->serialcomm);
220 drvopts = g_slist_append(drvopts, src);
222 devlist = sr_driver_scan(drv, drvopts);
223 g_slist_free_full(drvopts, (GDestroyNotify)free_drvopts);
225 /* No devices found for this driver, not an error. */
228 if (g_slist_length(devlist) > 1) {
229 INFO("sigrok: %d sigrok devices for device entry '%s': must be 1.",
230 g_slist_length(devlist), cfdev->name);
233 cfdev->sdi = devlist->data;
234 g_slist_free(devlist);
235 ssnprintf(hwident, sizeof(hwident), "%s %s %s",
236 cfdev->sdi->vendor ? cfdev->sdi->vendor : "",
237 cfdev->sdi->model ? cfdev->sdi->model : "",
238 cfdev->sdi->version ? cfdev->sdi->version : "");
239 INFO("sigrok: Device '%s' is a %s", cfdev->name, hwident);
241 if (sr_dev_open(cfdev->sdi) != SR_OK)
244 if (sr_session_dev_add(cfdev->sdi) != SR_OK)
250 static void *thread_init(void *arg __attribute__((unused)))
252 struct sr_dev_driver *drv, **drvlist;
254 struct config_device *cfdev;
257 sr_log_callback_set(cd_logger, NULL);
258 sr_log_loglevel_set(loglevel);
260 if ((ret = sr_init(&sr_ctx)) != SR_OK) {
261 ERROR("Failed to initialize libsigrok: %s.", sr_strerror(ret));
265 if (!(session = sr_session_new()))
269 drvlist = sr_driver_list();
270 for (l = config_devices; l; l = l->next) {
273 for (i = 0; drvlist[i]; i++) {
274 if (!strcmp(drvlist[i]->name, cfdev->driver)) {
280 ERROR("sigrok: Unknown driver '%s'.", cfdev->driver);
284 if ((ret = sigrok_init_driver(cfdev, drv)) < 0)
285 /* Error was already logged. */
291 if (num_devices > 0) {
292 /* Do this only when we're sure there's hardware to talk to. */
293 if (sr_session_datafeed_callback_add(sigrok_feed_callback, NULL) != SR_OK)
296 /* Start acquisition on all devices. */
297 if (sr_session_start() != SR_OK)
300 /* Main loop, runs forever. */
304 sr_session_dev_remove_all();
307 sr_session_destroy();
312 sr_thread_running = FALSE;
317 static int sigrok_init(void)
321 if (sr_thread_running) {
322 ERROR("sigrok: Thread already running.");
326 if ((status = plugin_thread_create(&sr_thread, NULL, thread_init, NULL)) != 0) {
327 ERROR("sigrok: Failed to create thread: %s.", strerror(status));
330 sr_thread_running = TRUE;
335 static int sigrok_shutdown(void)
337 struct config_device *cfdev;
340 if (sr_thread_running) {
341 pthread_cancel(sr_thread);
342 pthread_join(sr_thread, NULL);
345 for (l = config_devices; l; l = l->next) {
350 free(cfdev->serialcomm);
353 g_slist_free(config_devices);
358 void module_register(void)
361 plugin_register_complex_config("sigrok", sigrok_config);
362 plugin_register_init("sigrok", sigrok_init);
363 plugin_register_shutdown("sigrok", sigrok_shutdown);