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"
26 * @defgroup grp_devices Devices
28 * Device handling in libsigrok.
34 SR_PRIV struct sr_probe *sr_probe_new(int index, int type,
35 gboolean enabled, const char *name)
37 struct sr_probe *probe;
39 if (!(probe = g_try_malloc0(sizeof(struct sr_probe)))) {
40 sr_err("hwdriver: probe malloc failed");
46 probe->enabled = enabled;
48 probe->name = g_strdup(name);
54 * Set the name of the specified probe in the specified device.
56 * If the probe already has a different name assigned to it, it will be
57 * removed, and the new name will be saved instead.
59 * @param sdi The device instance the probe is connected to.
60 * @param probenum The number of the probe whose name to set.
61 * Note that the probe numbers start at 0.
62 * @param name The new name that the specified probe should get. A copy
63 * of the string is made.
65 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
67 SR_API int sr_dev_probe_name_set(const struct sr_dev_inst *sdi,
68 int probenum, const char *name)
71 struct sr_probe *probe;
75 sr_err("%s: sdi was NULL", __func__);
80 for (l = sdi->probes; l; l = l->next) {
82 if (probe->index == probenum) {
84 probe->name = g_strdup(name);
94 * Enable or disable a probe on the specified device.
96 * @param sdi The device instance the probe is connected to.
97 * @param probenum The probe number, starting from 0.
98 * @param state TRUE to enable the probe, FALSE to disable.
100 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
102 SR_API int sr_dev_probe_enable(const struct sr_dev_inst *sdi, int probenum,
106 struct sr_probe *probe;
113 for (l = sdi->probes; l; l = l->next) {
115 if (probe->index == probenum) {
116 probe->enabled = state;
126 * Add a trigger to the specified device (and the specified probe).
128 * If the specified probe of this device already has a trigger, it will
129 * be silently replaced.
131 * @param sdi Must not be NULL.
132 * @param probenum The probe number, starting from 0.
133 * @param trigger Trigger string, in the format used by sigrok-cli
135 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
137 SR_API int sr_dev_trigger_set(const struct sr_dev_inst *sdi, int probenum,
141 struct sr_probe *probe;
148 for (l = sdi->probes; l; l = l->next) {
150 if (probe->index == probenum) {
151 /* If the probe already has a trigger, kill it first. */
152 g_free(probe->trigger);
153 probe->trigger = g_strdup(trigger);
163 * Determine whether the specified device has the specified capability.
165 * @param dev Pointer to the device instance to be checked. Must not be NULL.
166 * If the device's 'driver' field is NULL (virtual device), this
167 * function will always return FALSE (virtual devices don't have
168 * a hardware capabilities list).
169 * @param hwcap The capability that should be checked (whether it's supported
170 * by the specified device).
172 * @return TRUE if the device has the specified capability, FALSE otherwise.
173 * FALSE is also returned upon invalid input parameters or other
176 SR_API gboolean sr_dev_has_hwcap(const struct sr_dev_inst *sdi, int hwcap)
181 if (!sdi || !sdi->driver)
184 if (sdi->driver->info_get(SR_DI_HWCAPS,
185 (const void **)&hwcaps, NULL) != SR_OK)
188 for (i = 0; hwcaps[i]; i++) {
189 if (hwcaps[i] == hwcap)
197 SR_PRIV struct sr_dev_inst *sr_dev_inst_new(int index, int status,
198 const char *vendor, const char *model, const char *version)
200 struct sr_dev_inst *sdi;
202 if (!(sdi = g_try_malloc(sizeof(struct sr_dev_inst)))) {
203 sr_err("hwdriver: %s: sdi malloc failed", __func__);
209 sdi->status = status;
211 sdi->vendor = vendor ? g_strdup(vendor) : NULL;
212 sdi->model = model ? g_strdup(model) : NULL;
213 sdi->version = version ? g_strdup(version) : NULL;
221 SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
223 struct sr_probe *probe;
226 for (l = sdi->probes; l; l = l->next) {
235 g_free(sdi->version);
240 #ifdef HAVE_LIBUSB_1_0
243 SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
244 uint8_t address, struct libusb_device_handle *hdl)
246 struct sr_usb_dev_inst *udi;
248 if (!(udi = g_try_malloc(sizeof(struct sr_usb_dev_inst)))) {
249 sr_err("hwdriver: %s: udi malloc failed", __func__);
254 udi->address = address;
261 SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
263 /* Avoid compiler warnings. */
266 /* Nothing to do for this device instance type. */
272 SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
275 struct sr_serial_dev_inst *serial;
277 if (!(serial = g_try_malloc(sizeof(struct sr_serial_dev_inst)))) {
278 sr_err("hwdriver: %s: serial malloc failed", __func__);
282 serial->port = g_strdup(port);
289 SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
291 g_free(serial->port);
294 SR_API int sr_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
299 if (!sdi || !sdi->driver || !sdi->driver->dev_config_set) {
300 sr_err("hwdriver: unable to set config option");
304 ret = sdi->driver->dev_config_set(sdi, hwcap, value);
309 SR_API GSList *sr_dev_inst_list(const struct sr_dev_driver *driver)
312 if (driver && driver->dev_list)
313 return driver->dev_list();
318 SR_API int sr_dev_inst_clear(const struct sr_dev_driver *driver)
321 if (driver && driver->dev_clear)
322 return driver->dev_clear();