2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 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"
26 #define LOG_PREFIX "device"
31 * Device handling in libsigrok.
35 * @defgroup grp_devices Devices
37 * Device handling in libsigrok.
43 * Allocate and initialize new struct sr_probe
44 * @param[in] index @copydoc sr_probe::index
45 * @param[in] type @copydoc sr_probe::type
46 * @param[in] enabled @copydoc sr_probe::enabled
47 * @param[in] name @copydoc sr_probe::name
49 * @return NULL (failure) or new struct sr_probe*.
51 SR_PRIV struct sr_probe *sr_probe_new(int index, int type,
52 gboolean enabled, const char *name)
54 struct sr_probe *probe;
56 if (!(probe = g_try_malloc0(sizeof(struct sr_probe)))) {
57 sr_err("Probe malloc failed.");
63 probe->enabled = enabled;
65 probe->name = g_strdup(name);
71 * Set the name of the specified probe in the specified device.
73 * If the probe already has a different name assigned to it, it will be
74 * removed, and the new name will be saved instead.
76 * @param sdi The device instance the probe is connected to.
77 * @param[in] probenum The number of the probe whose name to set.
78 * Note that the probe numbers start at 0.
79 * @param[in] name The new name that the specified probe should get. A copy
80 * of the string is made.
82 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
86 SR_API int sr_dev_probe_name_set(const struct sr_dev_inst *sdi,
87 int probenum, const char *name)
90 struct sr_probe *probe;
94 sr_err("%s: sdi was NULL", __func__);
99 for (l = sdi->probes; l; l = l->next) {
101 if (probe->index == probenum) {
103 probe->name = g_strdup(name);
113 * Enable or disable a probe on the specified device.
115 * @param sdi The device instance the probe is connected to.
116 * @param probenum The probe number, starting from 0.
117 * @param state TRUE to enable the probe, FALSE to disable.
119 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
123 SR_API int sr_dev_probe_enable(const struct sr_dev_inst *sdi, int probenum,
127 struct sr_probe *probe;
134 for (l = sdi->probes; l; l = l->next) {
136 if (probe->index == probenum) {
137 probe->enabled = state;
147 * Add a trigger to the specified device (and the specified probe).
149 * If the specified probe of this device already has a trigger, it will
150 * be silently replaced.
152 * @param[in,out] sdi Pointer to the device instance; must not be NULL.
153 * @param[in] probenum Number of probe, starting at 0.
154 * @param[in] trigger Trigger string, in the format used by sigrok-cli
156 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
160 SR_API int sr_dev_trigger_set(const struct sr_dev_inst *sdi, int probenum,
164 struct sr_probe *probe;
171 for (l = sdi->probes; l; l = l->next) {
173 if (probe->index == probenum) {
174 /* If the probe already has a trigger, kill it first. */
175 g_free(probe->trigger);
176 probe->trigger = g_strdup(trigger);
186 * Determine whether the specified device instance has the specified
189 * @param sdi Pointer to the device instance to be checked. Must not be NULL.
190 * If the device's 'driver' field is NULL (virtual device), this
191 * function will always return FALSE (virtual devices don't have
192 * a hardware capabilities list).
193 * @param[in] key The option that should be checked for is supported by the
196 * @retval TRUE Device has the specified option
197 * @retval FALSE Device does not have the specified option, invalid input
198 * parameters or other error conditions.
202 SR_API gboolean sr_dev_has_option(const struct sr_dev_inst *sdi, int key)
209 if (!sdi || !sdi->driver || !sdi->driver->config_list)
212 if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS,
213 &gvar, sdi, NULL) != SR_OK)
217 devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t));
218 for (i = 0; i < num_opts; i++) {
219 if (devopts[i] == key) {
224 g_variant_unref(gvar);
230 * Allocate and init new device instance struct.
231 * @param[in] index @copydoc sr_dev_inst::index
232 * @param[in] status @copydoc sr_dev_inst::status
233 * @param[in] vendor @copydoc sr_dev_inst::vendor
234 * @param[in] model @copydoc sr_dev_inst::model
235 * @param[in] version @copydoc sr_dev_inst::version
238 * @retval struct sr_dev_inst *. Dynamically allocated, free using
239 * sr_dev_inst_free().
241 SR_PRIV struct sr_dev_inst *sr_dev_inst_new(int index, int status,
242 const char *vendor, const char *model, const char *version)
244 struct sr_dev_inst *sdi;
246 if (!(sdi = g_try_malloc(sizeof(struct sr_dev_inst)))) {
247 sr_err("Device instance malloc failed.");
253 sdi->status = status;
255 sdi->vendor = vendor ? g_strdup(vendor) : NULL;
256 sdi->model = model ? g_strdup(model) : NULL;
257 sdi->version = version ? g_strdup(version) : NULL;
259 sdi->probe_groups = NULL;
267 * Free device instance struct created by sr_dev_inst().
268 * @param sdi struct* to free.
270 SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
272 struct sr_probe *probe;
275 for (l = sdi->probes; l; l = l->next) {
278 g_free(probe->trigger);
281 g_slist_free(sdi->probes);
283 if (sdi->probe_groups)
284 g_slist_free(sdi->probe_groups);
288 g_free(sdi->version);
292 #ifdef HAVE_LIBUSB_1_0
295 * Allocate and init struct for USB device instance.
296 * @param[in] bus @copydoc sr_usb_dev_inst::bus
297 * @param[in] address @copydoc sr_usb_dev_inst::address
298 * @param[in] hdl @copydoc sr_usb_dev_inst::devhdl
301 * @retval other struct sr_usb_dev_inst * for USB device instance.
303 SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
304 uint8_t address, struct libusb_device_handle *hdl)
306 struct sr_usb_dev_inst *udi;
308 if (!(udi = g_try_malloc(sizeof(struct sr_usb_dev_inst)))) {
309 sr_err("USB device instance malloc failed.");
314 udi->address = address;
321 * Free struct * allocated by sr_usb_dev_inst().
322 * @param usb struct* to free. Must not be NULL.
324 SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
331 #ifdef HAVE_LIBSERIALPORT
336 * Both parameters are copied to newly allocated strings, and freed
337 * automatically by sr_serial_dev_inst_free().
339 * @param[in] port OS-specific serial port specification. Examples:
340 * "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1".
341 * @param[in] serialcomm A serial communication parameters string, in the form
342 * of \<speed\>/\<data bits\>\<parity\>\<stopbits\>, for example
343 * "9600/8n1" or "600/7o2". This is an optional parameter;
344 * it may be filled in later.
346 * @return A pointer to a newly initialized struct sr_serial_dev_inst,
349 SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
350 const char *serialcomm)
352 struct sr_serial_dev_inst *serial;
355 sr_err("Serial port required.");
359 if (!(serial = g_try_malloc0(sizeof(struct sr_serial_dev_inst)))) {
360 sr_err("Serial device instance malloc failed.");
364 serial->port = g_strdup(port);
366 serial->serialcomm = g_strdup(serialcomm);
372 * Free struct sr_serial_dev_inst * allocated by sr_serial_dev_inst().
373 * @param serial struct sr_serial_dev_inst * to free. Must not be NULL.
375 SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
377 g_free(serial->port);
378 g_free(serial->serialcomm);
384 SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device)
386 struct sr_usbtmc_dev_inst *usbtmc;
389 sr_err("Device name required.");
393 if (!(usbtmc = g_try_malloc0(sizeof(struct sr_usbtmc_dev_inst)))) {
394 sr_err("USBTMC device instance malloc failed.");
398 usbtmc->device = g_strdup(device);
405 SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc)
407 g_free(usbtmc->device);
412 * Get the list of devices/instances of the specified driver.
414 * @param driver The driver to use. Must not be NULL.
416 * @return The list of devices/instances of this driver, or NULL upon errors
417 * or if the list is empty.
421 SR_API GSList *sr_dev_list(const struct sr_dev_driver *driver)
423 if (driver && driver->dev_list)
424 return driver->dev_list();
430 * Clear all devices/instances of the specified driver.
432 * @param driver The driver to use. Must not be NULL.
434 * @return SR_OK upon success, a negative error code upon errors.
438 SR_API int sr_dev_clear(const struct sr_dev_driver *driver)
440 if (driver && driver->dev_clear)
441 return driver->dev_clear();
447 * Open the specified device.
449 * @param sdi Device instance to use. Must not be NULL.
451 * @return SR_OK upon success, a negative error code upon errors.
455 SR_API int sr_dev_open(struct sr_dev_inst *sdi)
459 if (!sdi || !sdi->driver || !sdi->driver->dev_open)
462 ret = sdi->driver->dev_open(sdi);
468 * Close the specified device.
470 * @param sdi Device instance to use. Must not be NULL.
472 * @return SR_OK upon success, a negative error code upon errors.
476 SR_API int sr_dev_close(struct sr_dev_inst *sdi)
480 if (!sdi || !sdi->driver || !sdi->driver->dev_close)
483 ret = sdi->driver->dev_close(sdi);