2 * This file is part of the sigrok project.
4 * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "libsigrok.h"
23 #include "libsigrok-internal.h"
28 * Device handling in libsigrok.
32 * @defgroup grp_devices Devices
34 * Device handling in libsigrok.
40 SR_PRIV struct sr_probe *sr_probe_new(int index, int type,
41 gboolean enabled, const char *name)
43 struct sr_probe *probe;
45 if (!(probe = g_try_malloc0(sizeof(struct sr_probe)))) {
46 sr_err("hwdriver: probe malloc failed");
52 probe->enabled = enabled;
54 probe->name = g_strdup(name);
60 * Set the name of the specified probe in the specified device.
62 * If the probe already has a different name assigned to it, it will be
63 * removed, and the new name will be saved instead.
65 * @param sdi The device instance the probe is connected to.
66 * @param probenum The number of the probe whose name to set.
67 * Note that the probe numbers start at 0.
68 * @param name The new name that the specified probe should get. A copy
69 * of the string is made.
71 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
73 SR_API int sr_dev_probe_name_set(const struct sr_dev_inst *sdi,
74 int probenum, const char *name)
77 struct sr_probe *probe;
81 sr_err("%s: sdi was NULL", __func__);
86 for (l = sdi->probes; l; l = l->next) {
88 if (probe->index == probenum) {
90 probe->name = g_strdup(name);
100 * Enable or disable a probe on the specified device.
102 * @param sdi The device instance the probe is connected to.
103 * @param probenum The probe number, starting from 0.
104 * @param state TRUE to enable the probe, FALSE to disable.
106 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
108 SR_API int sr_dev_probe_enable(const struct sr_dev_inst *sdi, int probenum,
112 struct sr_probe *probe;
119 for (l = sdi->probes; l; l = l->next) {
121 if (probe->index == probenum) {
122 probe->enabled = state;
132 * Add a trigger to the specified device (and the specified probe).
134 * If the specified probe of this device already has a trigger, it will
135 * be silently replaced.
137 * @param sdi Must not be NULL.
138 * @param probenum The probe number, starting from 0.
139 * @param trigger Trigger string, in the format used by sigrok-cli
141 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
143 SR_API int sr_dev_trigger_set(const struct sr_dev_inst *sdi, int probenum,
147 struct sr_probe *probe;
154 for (l = sdi->probes; l; l = l->next) {
156 if (probe->index == probenum) {
157 /* If the probe already has a trigger, kill it first. */
158 g_free(probe->trigger);
159 probe->trigger = g_strdup(trigger);
169 * Determine whether the specified device instance has the specified
172 * @param sdi Pointer to the device instance to be checked. Must not be NULL.
173 * If the device's 'driver' field is NULL (virtual device), this
174 * function will always return FALSE (virtual devices don't have
175 * a hardware capabilities list).
176 * @param hwcap The capability that should be checked (whether it's supported
177 * by the specified device).
179 * @return TRUE if the device has the specified capability, FALSE otherwise.
180 * FALSE is also returned upon invalid input parameters or other
183 SR_API gboolean sr_dev_has_hwcap(const struct sr_dev_inst *sdi, int hwcap)
188 if (!sdi || !sdi->driver)
191 if (sdi->driver->info_get(SR_DI_HWCAPS,
192 (const void **)&hwcaps, NULL) != SR_OK)
195 for (i = 0; hwcaps[i]; i++) {
196 if (hwcaps[i] == hwcap)
204 SR_PRIV struct sr_dev_inst *sr_dev_inst_new(int index, int status,
205 const char *vendor, const char *model, const char *version)
207 struct sr_dev_inst *sdi;
209 if (!(sdi = g_try_malloc(sizeof(struct sr_dev_inst)))) {
210 sr_err("hwdriver: %s: sdi malloc failed", __func__);
216 sdi->status = status;
218 sdi->vendor = vendor ? g_strdup(vendor) : NULL;
219 sdi->model = model ? g_strdup(model) : NULL;
220 sdi->version = version ? g_strdup(version) : NULL;
228 SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
230 struct sr_probe *probe;
233 for (l = sdi->probes; l; l = l->next) {
242 g_free(sdi->version);
247 #ifdef HAVE_LIBUSB_1_0
250 SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
251 uint8_t address, struct libusb_device_handle *hdl)
253 struct sr_usb_dev_inst *udi;
255 if (!(udi = g_try_malloc(sizeof(struct sr_usb_dev_inst)))) {
256 sr_err("hwdriver: %s: udi malloc failed", __func__);
261 udi->address = address;
268 SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
270 /* Avoid compiler warnings. */
273 /* Nothing to do for this device instance type. */
279 SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
282 struct sr_serial_dev_inst *serial;
284 if (!(serial = g_try_malloc(sizeof(struct sr_serial_dev_inst)))) {
285 sr_err("hwdriver: %s: serial malloc failed", __func__);
289 serial->port = g_strdup(port);
296 SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
298 g_free(serial->port);
301 SR_API int sr_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
306 if (!sdi || !sdi->driver || !sdi->driver->dev_config_set) {
307 sr_err("hwdriver: unable to set config option");
311 ret = sdi->driver->dev_config_set(sdi, hwcap, value);
316 SR_API GSList *sr_dev_inst_list(const struct sr_dev_driver *driver)
319 if (driver && driver->dev_list)
320 return driver->dev_list();
325 SR_API int sr_dev_inst_clear(const struct sr_dev_driver *driver)
328 if (driver && driver->dev_clear)
329 return driver->dev_clear();