X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=device.c;h=528a2af11a26da317d2fd1c8e834bdcef959812e;hb=b35c829306b86dbeeeecf14de7fe30a05a88c914;hp=f5674b4ca426a1b29bc5699259185becdbc6cb92;hpb=b8c2f85f561a3e2738b621a0d42e0c066c1fcee9;p=libsigrok.git diff --git a/device.c b/device.c index f5674b4c..528a2af1 100644 --- a/device.c +++ b/device.c @@ -1,7 +1,7 @@ /* * This file is part of the sigrok project. * - * Copyright (C) 2010 Bert Vermeulen + * Copyright (C) 2010-2012 Bert Vermeulen * * 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 @@ -19,226 +19,149 @@ #include #include -#include +#include "libsigrok.h" +#include "libsigrok-internal.h" -extern struct sr_global *global; - -GSList *devices = NULL; - -void device_scan(void) -{ - GSList *plugins, *l; - struct sr_device_plugin *plugin; - - plugins = 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; - device_plugin_init(plugin); - } - -} - -int 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); - device_new(plugin, i, num_probes); + if (!sdi) { + sr_err("%s: sdi was NULL", __func__); + return SR_ERR_ARG; } - return num_devices; -} - -void 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); - 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 *device_list(void) -{ - - if (!devices) - device_scan(); - - return devices; -} - -struct sr_device *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++) - device_probe_add(device, NULL); - - return device; -} - -void 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++) - device_probe_clear(device, pnum); -} - -void 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++) - device_probe_clear(device, pnum); - g_slist_free(device->probes); - } - g_free(device); + return ret; } -void 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 probe *p; - - p = 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 device_probe_add(struct sr_device *device, char *name) -{ - struct probe *p; - char probename[16]; - int probenum; - - probenum = g_slist_length(device->probes) + 1; - p = g_malloc0(sizeof(struct 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 probe *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 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 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 probe *p; - - p = 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 device_trigger_clear(struct sr_device *device) -{ - struct 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 = 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 device_trigger_set(struct sr_device *device, int probenum, char *trigger) -{ - struct probe *p; - - p = probe_find(device, probenum); - if (!p) - return; - - if (p->trigger) - g_free(p->trigger); - - p->trigger = g_strdup(trigger); - -} - -gboolean device_has_hwcap(struct sr_device *device, int hwcap) -{ - int *capabilities, i; - - if ((capabilities = device->plugin->get_capabilities())) - for (i = 0; capabilities[i]; i++) - if (capabilities[i] == hwcap) - return TRUE; return FALSE; } +