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 "config.h" /* Needed for HAVE_LIBUSB_1_0 and others. */
23 #include "libsigrok.h"
24 #include "libsigrok-internal.h"
29 * Device handling in libsigrok.
33 * @defgroup grp_devices Devices
35 * Device handling in libsigrok.
41 SR_PRIV struct sr_probe *sr_probe_new(int index, int type,
42 gboolean enabled, const char *name)
44 struct sr_probe *probe;
46 if (!(probe = g_try_malloc0(sizeof(struct sr_probe)))) {
47 sr_err("hwdriver: probe malloc failed");
53 probe->enabled = enabled;
55 probe->name = g_strdup(name);
61 * Set the name of the specified probe in the specified device.
63 * If the probe already has a different name assigned to it, it will be
64 * removed, and the new name will be saved instead.
66 * @param sdi The device instance the probe is connected to.
67 * @param probenum The number of the probe whose name to set.
68 * Note that the probe numbers start at 0.
69 * @param name The new name that the specified probe should get. A copy
70 * of the string is made.
72 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
74 SR_API int sr_dev_probe_name_set(const struct sr_dev_inst *sdi,
75 int probenum, const char *name)
78 struct sr_probe *probe;
82 sr_err("%s: sdi was NULL", __func__);
87 for (l = sdi->probes; l; l = l->next) {
89 if (probe->index == probenum) {
91 probe->name = g_strdup(name);
101 * Enable or disable a probe on the specified device.
103 * @param sdi The device instance the probe is connected to.
104 * @param probenum The probe number, starting from 0.
105 * @param state TRUE to enable the probe, FALSE to disable.
107 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
109 SR_API int sr_dev_probe_enable(const struct sr_dev_inst *sdi, int probenum,
113 struct sr_probe *probe;
120 for (l = sdi->probes; l; l = l->next) {
122 if (probe->index == probenum) {
123 probe->enabled = state;
133 * Add a trigger to the specified device (and the specified probe).
135 * If the specified probe of this device already has a trigger, it will
136 * be silently replaced.
138 * @param sdi Must not be NULL.
139 * @param probenum The probe number, starting from 0.
140 * @param trigger Trigger string, in the format used by sigrok-cli
142 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
144 SR_API int sr_dev_trigger_set(const struct sr_dev_inst *sdi, int probenum,
148 struct sr_probe *probe;
155 for (l = sdi->probes; l; l = l->next) {
157 if (probe->index == probenum) {
158 /* If the probe already has a trigger, kill it first. */
159 g_free(probe->trigger);
160 probe->trigger = g_strdup(trigger);
170 * Determine whether the specified device instance has the specified
173 * @param sdi Pointer to the device instance to be checked. Must not be NULL.
174 * If the device's 'driver' field is NULL (virtual device), this
175 * function will always return FALSE (virtual devices don't have
176 * a hardware capabilities list).
177 * @param hwcap The capability that should be checked (whether it's supported
178 * by the specified device).
180 * @return TRUE if the device has the specified capability, FALSE otherwise.
181 * FALSE is also returned upon invalid input parameters or other
184 SR_API gboolean sr_dev_has_hwcap(const struct sr_dev_inst *sdi, int hwcap)
189 if (!sdi || !sdi->driver)
192 if (sdi->driver->info_get(SR_DI_HWCAPS,
193 (const void **)&hwcaps, NULL) != SR_OK)
196 for (i = 0; hwcaps[i]; i++) {
197 if (hwcaps[i] == hwcap)
205 SR_PRIV struct sr_dev_inst *sr_dev_inst_new(int index, int status,
206 const char *vendor, const char *model, const char *version)
208 struct sr_dev_inst *sdi;
210 if (!(sdi = g_try_malloc(sizeof(struct sr_dev_inst)))) {
211 sr_err("hwdriver: %s: sdi malloc failed", __func__);
217 sdi->status = status;
219 sdi->vendor = vendor ? g_strdup(vendor) : NULL;
220 sdi->model = model ? g_strdup(model) : NULL;
221 sdi->version = version ? g_strdup(version) : NULL;
229 SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
231 struct sr_probe *probe;
234 for (l = sdi->probes; l; l = l->next) {
243 g_free(sdi->version);
248 #ifdef HAVE_LIBUSB_1_0
251 SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
252 uint8_t address, struct libusb_device_handle *hdl)
254 struct sr_usb_dev_inst *udi;
256 if (!(udi = g_try_malloc(sizeof(struct sr_usb_dev_inst)))) {
257 sr_err("hwdriver: %s: udi malloc failed", __func__);
262 udi->address = address;
269 SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
271 /* Avoid compiler warnings. */
274 /* Nothing to do for this device instance type. */
280 SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
283 struct sr_serial_dev_inst *serial;
285 if (!(serial = g_try_malloc(sizeof(struct sr_serial_dev_inst)))) {
286 sr_err("hwdriver: %s: serial malloc failed", __func__);
290 serial->port = g_strdup(port);
297 SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
299 g_free(serial->port);
302 SR_API int sr_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
307 if (!sdi || !sdi->driver || !sdi->driver->dev_config_set) {
308 sr_err("hwdriver: unable to set config option");
312 ret = sdi->driver->dev_config_set(sdi, hwcap, value);
317 SR_API GSList *sr_dev_inst_list(const struct sr_dev_driver *driver)
320 if (driver && driver->dev_list)
321 return driver->dev_list();
326 SR_API int sr_dev_inst_clear(const struct sr_dev_driver *driver)
329 if (driver && driver->dev_clear)
330 return driver->dev_clear();