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 SR_PRIV struct sr_probe *sr_probe_new(int index, int type,
27 gboolean enabled, const char *name)
29 struct sr_probe *probe;
31 if (!(probe = g_try_malloc0(sizeof(struct sr_probe)))) {
32 sr_err("hwdriver: probe malloc failed");
38 probe->enabled = enabled;
40 probe->name = g_strdup(name);
46 * Set the name of the specified probe in the specified device.
48 * If the probe already has a different name assigned to it, it will be
49 * removed, and the new name will be saved instead.
51 * @param sdi The device instance the probe is connected to.
52 * @param probenum The number of the probe whose name to set.
53 * Note that the probe numbers start at 0.
54 * @param name The new name that the specified probe should get. A copy
55 * of the string is made.
57 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
59 SR_API int sr_dev_probe_name_set(const struct sr_dev_inst *sdi,
60 int probenum, const char *name)
63 struct sr_probe *probe;
67 sr_err("%s: sdi was NULL", __func__);
72 for (l = sdi->probes; l; l = l->next) {
74 if (probe->index == probenum) {
76 probe->name = g_strdup(name);
86 * Enable or disable a probe on the specified device.
88 * @param sdi The device instance the probe is connected to.
89 * @param probenum The probe number, starting from 0.
90 * @param state TRUE to enable the probe, FALSE to disable.
92 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
94 SR_API int sr_dev_probe_enable(const struct sr_dev_inst *sdi, int probenum,
98 struct sr_probe *probe;
105 for (l = sdi->probes; l; l = l->next) {
107 if (probe->index == probenum) {
108 probe->enabled = state;
118 * Add a trigger to the specified device (and the specified probe).
120 * If the specified probe of this device already has a trigger, it will
121 * be silently replaced.
123 * @param sdi Must not be NULL.
124 * @param probenum The probe number, starting from 0.
125 * @param trigger Trigger string, in the format used by sigrok-cli
127 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
129 SR_API int sr_dev_trigger_set(const struct sr_dev_inst *sdi, int probenum,
133 struct sr_probe *probe;
140 for (l = sdi->probes; l; l = l->next) {
142 if (probe->index == probenum) {
143 /* If the probe already has a trigger, kill it first. */
144 g_free(probe->trigger);
145 probe->trigger = g_strdup(trigger);
155 * Determine whether the specified device has the specified capability.
157 * @param dev Pointer to the device instance to be checked. Must not be NULL.
158 * If the device's 'driver' field is NULL (virtual device), this
159 * function will always return FALSE (virtual devices don't have
160 * a hardware capabilities list).
161 * @param hwcap The capability that should be checked (whether it's supported
162 * by the specified device).
164 * @return TRUE if the device has the specified capability, FALSE otherwise.
165 * FALSE is also returned upon invalid input parameters or other
168 SR_API gboolean sr_dev_has_hwcap(const struct sr_dev_inst *sdi, int hwcap)
173 if (!sdi || !sdi->driver)
176 if (sdi->driver->info_get(SR_DI_HWCAPS,
177 (const void **)&hwcaps, NULL) != SR_OK)
180 for (i = 0; hwcaps[i]; i++) {
181 if (hwcaps[i] == hwcap)
189 SR_PRIV struct sr_dev_inst *sr_dev_inst_new(int index, int status,
190 const char *vendor, const char *model, const char *version)
192 struct sr_dev_inst *sdi;
194 if (!(sdi = g_try_malloc(sizeof(struct sr_dev_inst)))) {
195 sr_err("hwdriver: %s: sdi malloc failed", __func__);
201 sdi->status = status;
203 sdi->vendor = vendor ? g_strdup(vendor) : NULL;
204 sdi->model = model ? g_strdup(model) : NULL;
205 sdi->version = version ? g_strdup(version) : NULL;
213 SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
215 struct sr_probe *probe;
218 for (l = sdi->probes; l; l = l->next) {
227 g_free(sdi->version);
232 #ifdef HAVE_LIBUSB_1_0
235 SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
236 uint8_t address, struct libusb_device_handle *hdl)
238 struct sr_usb_dev_inst *udi;
240 if (!(udi = g_try_malloc(sizeof(struct sr_usb_dev_inst)))) {
241 sr_err("hwdriver: %s: udi malloc failed", __func__);
246 udi->address = address;
253 SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
255 /* Avoid compiler warnings. */
258 /* Nothing to do for this device instance type. */
264 SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
267 struct sr_serial_dev_inst *serial;
269 if (!(serial = g_try_malloc(sizeof(struct sr_serial_dev_inst)))) {
270 sr_err("hwdriver: %s: serial malloc failed", __func__);
274 serial->port = g_strdup(port);
281 SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
283 g_free(serial->port);
286 SR_API int sr_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
291 if (!sdi || !sdi->driver || !sdi->driver->dev_config_set) {
292 sr_err("hwdriver: unable to set config option");
296 ret = sdi->driver->dev_config_set(sdi, hwcap, value);
301 SR_API GSList *sr_dev_inst_list(const struct sr_dev_driver *driver)
304 if (driver && driver->dev_list)
305 return driver->dev_list();
310 SR_API int sr_dev_inst_clear(const struct sr_dev_driver *driver)
313 if (driver && driver->dev_clear)
314 return driver->dev_clear();