]> sigrok.org Git - libsigrok.git/blobdiff - device.c
sr: change sr_dev_probe_name_set() to use sdi
[libsigrok.git] / device.c
index bf87e811e90c6ffb9530214b0fcf381215bde610..cfa5492f179a7ec5e3f2a14fc2ec997917b47849 100644 (file)
--- a/device.c
+++ b/device.c
@@ -1,7 +1,7 @@
 /*
  * 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 "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)
+/**
+ * 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)
 {
-       int num_devices, num_probes, i;
+       GSList *l;
+       struct sr_probe *probe;
+       int ret;
 
-       g_message("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);
+       if (!sdi) {
+               sr_err("%s: sdi was NULL", __func__);
+               return SR_ERR_ARG;
        }
 
-       return num_devices;
-}
-
-void sr_device_close_all(void)
-{
-       struct sr_device *device;
-
-       while (devices) {
-               device = devices->data;
-               if (device->plugin && device->plugin->close)
-                       device->plugin->close(device->plugin_index);
-               sr_device_destroy(device);
+       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;
+               }
        }
-}
-
-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;
-
-       device = g_malloc0(sizeof(struct sr_device));
-       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);
+       return ret;
 }
 
-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);
-       }
-       g_free(device);
-}
-
-void sr_device_probe_clear(struct sr_device *device, int probenum)
+/**
+ * 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;
-
-       p = sr_device_probe_find(device, probenum);
-       if (!p)
-               return;
-
-       if (p->name) {
-               g_free(p->name);
-               p->name = NULL;
-       }
-
-       if (p->trigger) {
-               g_free(p->trigger);
-               p->trigger = NULL;
+       GSList *l;
+       struct sr_probe *probe;
+       int ret;
+
+       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) {
+                       probe->enabled = state;
+                       ret = SR_OK;
+                       break;
+               }
        }
-}
 
-void sr_device_probe_add(struct sr_device *device, char *name)
-{
-       struct sr_probe *p;
-       char probename[16];
-       int probenum;
-
-       probenum = g_slist_length(device->probes) + 1;
-       p = g_malloc0(sizeof(struct sr_probe));
-       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 number of the probe.
+ *                 Note that the probe numbers start at 1 (not 0!).
+ * @param trigger trigger string, in the format used by sigrok-cli
+ *
+ * @return SR_OK upon success, SR_ERR_ARG upon 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;
-
-       found_probe = NULL;
-       for (l = device->probes; l; l = l->next) {
-               p = l->data;
-               if (p->index == probenum) {
-                       found_probe = p;
+       struct sr_probe *probe;
+       int ret;
+
+       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;
+       return ret;
 }
 
-/* TODO: return SIGROK_ERR if probenum not found */
-void sr_device_probe_name(struct sr_device *device, int probenum, char *name)
+/**
+ * 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->name)
-               g_free(p->name);
-       p->name = g_strdup(name);
-}
+       const int *hwcaps;
+       int i;
 
-/* TODO: return SIGROK_ERR if probenum not found */
-void sr_device_trigger_clear(struct sr_device *device)
-{
-       struct sr_probe *p;
-       unsigned int pnum;
+       if (!sdi || !sdi->driver)
+               return FALSE;
 
-       if (!device->probes)
-               return;
+       if (sdi->driver->info_get(SR_DI_HWCAPS,
+                       (const void **)&hwcaps, NULL) != SR_OK)
+               return FALSE;
 
-       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;
-               }
+       for (i = 0; hwcaps[i]; i++) {
+               if (hwcaps[i] == hwcap)
+                       return TRUE;
        }
-}
-
-/* TODO: return SIGROK_ERR if probenum not found */
-void sr_device_trigger_set(struct sr_device *device, int probenum, char *trigger)
-{
-       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);
-
-}
-
-gboolean sr_device_has_hwcap(struct sr_device *device, int hwcap)
-{
-       int *capabilities, i;
-
-       if (!device || !device->plugin)
-               return;
-
-       if ((capabilities = device->plugin->get_capabilities()))
-               for (i = 0; capabilities[i]; i++)
-                       if (capabilities[i] == hwcap)
-                               return TRUE;
 
        return FALSE;
 }
+