/*
* This file is part of the sigrok project.
*
- * Copyright (C) 2010 Bert Vermeulen <bert@biot.com>
+ * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
#include <stdio.h>
#include <glib.h>
-#include <sigrok.h>
-#include <sigrok-internal.h>
+#include "libsigrok.h"
+#include "libsigrok-internal.h"
-extern struct sr_global *global;
-
-GSList *devices = NULL;
-
-void sr_device_scan(void)
-{
- GSList *plugins, *l;
- struct sr_device_plugin *plugin;
-
- plugins = sr_list_hwplugins();
-
- /*
- * Initialize all plugins first. Since the init() call may involve
- * a firmware upload and associated delay, we may as well get all
- * of these out of the way first.
- */
- for (l = plugins; l; l = l->next) {
- plugin = l->data;
- sr_device_plugin_init(plugin);
- }
-
-}
-
-int sr_device_plugin_init(struct sr_device_plugin *plugin)
-{
- int num_devices, num_probes, i;
-
- sr_info("initializing %s plugin", plugin->name);
- num_devices = plugin->init(NULL);
- for (i = 0; i < num_devices; i++) {
- num_probes = (int)plugin->get_device_info(i, SR_DI_NUM_PROBES);
- sr_device_new(plugin, i, num_probes);
- }
-
- return num_devices;
-}
-
-void sr_device_close_all(void)
+/**
+ * Set the name of the specified probe in the specified device.
+ *
+ * If the probe already has a different name assigned to it, it will be
+ * removed, and the new name will be saved instead.
+ *
+ * @param sdi The device instance the probe is connected to.
+ * @param probenum The number of the probe whose name to set.
+ * Note that the probe numbers start at 0.
+ * @param name The new name that the specified probe should get. A copy
+ * of the string is made.
+ *
+ * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
+ */
+SR_API int sr_dev_probe_name_set(const struct sr_dev_inst *sdi,
+ int probenum, const char *name)
{
+ GSList *l;
+ struct sr_probe *probe;
int ret;
- struct sr_device *device;
-
- while (devices) {
- device = devices->data;
- if (device->plugin && device->plugin->closedev) {
- ret = device->plugin->closedev(device->plugin_index);
- if (ret != SR_OK) {
- sr_err("dev: %s: could not close device %d",
- __func__, device->plugin_index);
- }
- }
- sr_device_destroy(device);
- }
-}
-
-GSList *sr_device_list(void)
-{
-
- if (!devices)
- sr_device_scan();
-
- return devices;
-}
-
-struct sr_device *sr_device_new(struct sr_device_plugin *plugin, int plugin_index,
- int num_probes)
-{
- struct sr_device *device;
- int i;
-
- if (!(device = g_try_malloc0(sizeof(struct sr_device)))) {
- sr_err("dev: %s: device malloc failed", __func__);
- return NULL;
- }
-
- device->plugin = plugin;
- device->plugin_index = plugin_index;
- devices = g_slist_append(devices, device);
-
- for (i = 0; i < num_probes; i++)
- sr_device_probe_add(device, NULL);
-
- return device;
-}
-
-void sr_device_clear(struct sr_device *device)
-{
- unsigned int pnum;
-
- /* TODO: Plugin-specific clear call? */
-
- if (!device->probes)
- return;
-
- for (pnum = 1; pnum <= g_slist_length(device->probes); pnum++)
- sr_device_probe_clear(device, pnum);
-}
-
-void sr_device_destroy(struct sr_device *device)
-{
- unsigned int pnum;
- /*
- * TODO: Plugin-specific destroy call, need to decrease refcount
- * in plugin.
- */
-
- devices = g_slist_remove(devices, device);
- if (device->probes) {
- for (pnum = 1; pnum <= g_slist_length(device->probes); pnum++)
- sr_device_probe_clear(device, pnum);
- g_slist_free(device->probes);
+ if (!sdi) {
+ sr_err("%s: sdi was NULL", __func__);
+ return SR_ERR_ARG;
}
- g_free(device);
-}
-
-void sr_device_probe_clear(struct sr_device *device, int probenum)
-{
- struct sr_probe *p;
-
- p = sr_device_probe_find(device, probenum);
- if (!p)
- return;
- if (p->name) {
- g_free(p->name);
- p->name = NULL;
+ ret = SR_ERR_ARG;
+ for (l = sdi->probes; l; l = l->next) {
+ probe = l->data;
+ if (probe->index == probenum) {
+ g_free(probe->name);
+ probe->name = g_strdup(name);
+ ret = SR_OK;
+ break;
+ }
}
- if (p->trigger) {
- g_free(p->trigger);
- p->trigger = NULL;
- }
+ return ret;
}
-void sr_device_probe_add(struct sr_device *device, const char *name)
+/**
+ * Enable or disable a probe on the specified device.
+ *
+ * @param sdi The device instance the probe is connected to.
+ * @param probenum The probe number, starting from 0.
+ * @param state TRUE to enable the probe, FALSE to disable.
+ *
+ * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
+ */
+SR_API int sr_dev_probe_enable(const struct sr_dev_inst *sdi, int probenum,
+ gboolean state)
{
- struct sr_probe *p;
- char probename[16];
- int probenum;
+ GSList *l;
+ struct sr_probe *probe;
+ int ret;
- probenum = g_slist_length(device->probes) + 1;
+ if (!sdi)
+ return SR_ERR_ARG;
- if (!(p = g_try_malloc0(sizeof(struct sr_probe)))) {
- sr_err("dev: %s: p malloc failed", __func__);
- // return SR_ERR_MALLOC;
- return; /* FIXME: should return int. */
+ ret = SR_ERR_ARG;
+ for (l = sdi->probes; l; l = l->next) {
+ probe = l->data;
+ if (probe->index == probenum) {
+ probe->enabled = state;
+ ret = SR_OK;
+ break;
+ }
}
- p->index = probenum;
- p->enabled = TRUE;
- if (name) {
- p->name = g_strdup(name);
- } else {
- snprintf(probename, 16, "%d", probenum);
- p->name = g_strdup(probename);
- }
- p->trigger = NULL;
- device->probes = g_slist_append(device->probes, p);
+ return ret;
}
-struct sr_probe *sr_device_probe_find(struct sr_device *device, int probenum)
+/**
+ * Add a trigger to the specified device (and the specified probe).
+ *
+ * If the specified probe of this device already has a trigger, it will
+ * be silently replaced.
+ *
+ * @param sdi Must not be NULL.
+ * @param probenum The probe number, starting from 0.
+ * @param trigger Trigger string, in the format used by sigrok-cli
+ *
+ * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
+ */
+SR_API int sr_dev_trigger_set(const struct sr_dev_inst *sdi, int probenum,
+ const char *trigger)
{
GSList *l;
- struct sr_probe *p, *found_probe;
+ struct sr_probe *probe;
+ int ret;
- found_probe = NULL;
- for (l = device->probes; l; l = l->next) {
- p = l->data;
- if (p->index == probenum) {
- found_probe = p;
+ if (!sdi)
+ return SR_ERR_ARG;
+
+ ret = SR_ERR_ARG;
+ for (l = sdi->probes; l; l = l->next) {
+ probe = l->data;
+ if (probe->index == probenum) {
+ /* If the probe already has a trigger, kill it first. */
+ g_free(probe->trigger);
+ probe->trigger = g_strdup(trigger);
+ ret = SR_OK;
break;
}
}
- return found_probe;
-}
-
-/* TODO: return SR_ERR if probenum not found */
-void sr_device_probe_name(struct sr_device *device, int probenum,
- const char *name)
-{
- struct sr_probe *p;
-
- p = sr_device_probe_find(device, probenum);
- if (!p)
- return;
-
- if (p->name)
- g_free(p->name);
- p->name = g_strdup(name);
-}
-
-/* TODO: return SR_ERR if probenum not found */
-void sr_device_trigger_clear(struct sr_device *device)
-{
- struct sr_probe *p;
- unsigned int pnum;
-
- if (!device->probes)
- return;
-
- for (pnum = 1; pnum <= g_slist_length(device->probes); pnum++) {
- p = sr_device_probe_find(device, pnum);
- if (p && p->trigger) {
- g_free(p->trigger);
- p->trigger = NULL;
- }
- }
+ return ret;
}
-/* TODO: return SR_ERR if probenum not found */
-void sr_device_trigger_set(struct sr_device *device, int probenum,
- const char *trigger)
+/**
+ * Determine whether the specified device has the specified capability.
+ *
+ * @param dev Pointer to the device instance to be checked. Must not be NULL.
+ * If the device's 'driver' field is NULL (virtual device), this
+ * function will always return FALSE (virtual devices don't have
+ * a hardware capabilities list).
+ * @param hwcap The capability that should be checked (whether it's supported
+ * by the specified device).
+ *
+ * @return TRUE if the device has the specified capability, FALSE otherwise.
+ * FALSE is also returned upon invalid input parameters or other
+ * error conditions.
+ */
+SR_API gboolean sr_dev_has_hwcap(const struct sr_dev_inst *sdi, int hwcap)
{
- struct sr_probe *p;
-
- p = sr_device_probe_find(device, probenum);
- if (!p)
- return;
-
- if (p->trigger)
- g_free(p->trigger);
-
- p->trigger = g_strdup(trigger);
-
-}
+ const int *hwcaps;
+ int i;
-gboolean sr_device_has_hwcap(struct sr_device *device, int hwcap)
-{
- int *capabilities, i;
+ if (!sdi || !sdi->driver)
+ return FALSE;
- if (!device || !device->plugin)
+ if (sdi->driver->info_get(SR_DI_HWCAPS,
+ (const void **)&hwcaps, NULL) != SR_OK)
return FALSE;
- if ((capabilities = device->plugin->get_capabilities()))
- for (i = 0; capabilities[i]; i++)
- if (capabilities[i] == hwcap)
- return TRUE;
+ for (i = 0; hwcaps[i]; i++) {
+ if (hwcaps[i] == hwcap)
+ return TRUE;
+ }
return FALSE;
}
+