static pthread_t sr_thread;
static int sr_thread_running = FALSE;
GSList *config_devices;
-static struct sr_session *session = NULL;
static int num_devices;
static int loglevel = SR_LOG_WARN;
static struct sr_context *sr_ctx;
};
-static int cd_logger(void *cb_data, int msg_loglevel, const char *format,
- va_list args)
+static int sigrok_log_callback(void*cb_data __attribute__((unused)),
+ int msg_loglevel, const char *format, va_list args)
{
char s[512];
{
oconfig_item_t *item;
struct config_device *cfdev;
- int ret, i;
-
- if (ci->values_num != 1 || ci->values[0].type != OCONFIG_TYPE_STRING) {
- ERROR("Invalid device name.");
- return 1;
- }
+ int i;
if (!(cfdev = malloc(sizeof(struct config_device)))) {
ERROR("malloc() failed.");
return 1;
}
- memset(cfdev, 0, sizeof(struct config_device));
- cf_util_get_string(ci, &cfdev->name);
+ memset(cfdev, 0, sizeof(*cfdev));
+ if (cf_util_get_string(ci, &cfdev->name)) {
+ free(cfdev);
+ WARNING("Invalid device name.");
+ return 1;
+ }
cfdev->min_dispatch_interval = DEFAULT_MIN_DISPATCH_INTERVAL;
for (i = 0; i < ci->children_num; i++) {
item = ci->children + i;
if (item->values_num != 1) {
- ERROR("Missing value for '%s'.", item->key);
+ free(cfdev);
+ WARNING("Missing value for '%s'.", item->key);
return 1;
}
if (!strcasecmp(item->key, "driver"))
- ret = cf_util_get_string(item, &cfdev->driver);
+ cf_util_get_string(item, &cfdev->driver);
else if (!strcasecmp(item->key, "conn"))
- ret = cf_util_get_string(item, &cfdev->conn);
+ cf_util_get_string(item, &cfdev->conn);
else if (!strcasecmp(item->key, "serialcomm"))
- ret = cf_util_get_string(item, &cfdev->serialcomm);
+ cf_util_get_string(item, &cfdev->serialcomm);
else if (!strcasecmp(item->key, "interval"))
- ret = cf_util_get_cdtime(item, &cfdev->min_dispatch_interval);
- if (ret) {
- ERROR("Invalid keyword '%s'.", item->key);
- return 1;
- }
+ cf_util_get_cdtime(item, &cfdev->min_dispatch_interval);
+ else
+ WARNING("Invalid keyword '%s'.", item->key);
}
config_devices = g_slist_append(config_devices, cfdev);
if (!strcasecmp(item->key, "loglevel")) {
if (cf_util_get_int(item, &tmp) || tmp < 0 || tmp > 5) {
ERROR("Invalid loglevel");
- return 1;
+ continue;
}
loglevel = tmp;
- } else if (!strcasecmp(item->key, "Device")) {
- if (sigrok_config_device(item) != 0)
- return 1;
- } else {
- ERROR("Invalid keyword '%s'.", item->key);
- return 1;
- }
+ } else if (!strcasecmp(item->key, "Device"))
+ sigrok_config_device(item);
+ else
+ WARNING("Invalid keyword '%s'.", item->key);
}
return 0;
const struct sr_datafeed_analog *analog;
struct config_device *cfdev;
GSList *l;
- value_t *values;
+ value_t value;
value_list_t vl = VALUE_LIST_INIT;
- int num_probes, s, p;
-
- if (packet->type == SR_DF_END) {
- /* TODO: try to restart acquisition after a delay? */
- INFO("oops! ended");
- return;
- }
/* Find this device's configuration. */
cfdev = NULL;
return;
}
- if (packet->type == SR_DF_ANALOG) {
- if (cdtime() - cfdev->last_dispatch < cfdev->min_dispatch_interval)
- return;
-
- analog = packet->payload;
- num_probes = g_slist_length(analog->probes);
- if (!(values = malloc(sizeof(value_t) * num_probes))) {
- ERROR("malloc() failed.");
- return;
- }
- for (s = 0; s < analog->num_samples; s++) {
- for (p = 0; p < num_probes; p++) {
- values[s + p].gauge = analog->data[s + p];
- }
- }
- vl.values = values;
- vl.values_len = num_probes;
- sstrncpy(vl.host, hostname_g, sizeof(vl.host));
- sstrncpy(vl.plugin, "sigrok", sizeof(vl.plugin));
- ssnprintf(vl.plugin_instance, sizeof(vl.plugin_instance),
- "%s", cfdev->name);
- sstrncpy(vl.type, "gauge", sizeof(vl.type));
- plugin_dispatch_values(&vl);
-
- cfdev->last_dispatch = cdtime();
+ if (packet->type == SR_DF_END) {
+ /* TODO: try to restart acquisition after a delay? */
+ INFO("sigrok: acquisition for '%s' ended.", cfdev->name);
+ return;
}
+ if (packet->type != SR_DF_ANALOG)
+ return;
+
+ if (cdtime() - cfdev->last_dispatch < cfdev->min_dispatch_interval)
+ return;
+
+ /* Ignore all but the first sample on the first probe. */
+ analog = packet->payload;
+ value.gauge = analog->data[0];
+ vl.values = &value;
+ vl.values_len = 1;
+ sstrncpy(vl.host, hostname_g, sizeof(vl.host));
+ sstrncpy(vl.plugin, "sigrok", sizeof(vl.plugin));
+ ssnprintf(vl.plugin_instance, sizeof(vl.plugin_instance),
+ "%s", cfdev->name);
+ sstrncpy(vl.type, "gauge", sizeof(vl.type));
+ plugin_dispatch_values(&vl);
+
+ cfdev->last_dispatch = cdtime();
+
}
static int sigrok_init_driver(struct config_device *cfdev,
struct config_device *cfdev;
int ret, i;
- sr_log_callback_set(cd_logger, NULL);
+ sr_log_callback_set(sigrok_log_callback, NULL);
sr_log_loglevel_set(loglevel);
if ((ret = sr_init(&sr_ctx)) != SR_OK) {
return NULL;
}
- if (!(session = sr_session_new()))
+ if (!sr_session_new())
return NULL;
num_devices = 0;