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/>.
23 #include <libsigrok/libsigrok.h>
24 #include "libsigrok-internal.h"
27 #define LOG_PREFIX "device"
33 * Device handling in libsigrok.
37 * @defgroup grp_devices Devices
39 * Device handling in libsigrok.
45 * Allocate and initialize a new struct sr_channel and add it to sdi.
47 * @param[in] sdi The device instance the channel is connected to.
49 * @param[in] index @copydoc sr_channel::index
50 * @param[in] type @copydoc sr_channel::type
51 * @param[in] enabled @copydoc sr_channel::enabled
52 * @param[in] name @copydoc sr_channel::name
54 * @return A new struct sr_channel*.
58 SR_PRIV struct sr_channel *sr_channel_new(struct sr_dev_inst *sdi,
59 int index, int type, gboolean enabled, const char *name)
61 struct sr_channel *ch;
63 ch = g_malloc0(sizeof(struct sr_channel));
67 ch->enabled = enabled;
69 ch->name = g_strdup(name);
71 sdi->channels = g_slist_append(sdi->channels, ch);
77 * Release a previously allocated struct sr_channel.
79 * @param[in] ch Pointer to struct sr_channel.
83 SR_PRIV void sr_channel_free(struct sr_channel *ch)
93 * Wrapper around @ref sr_channel_free(), suitable for glib iterators.
95 SR_PRIV void sr_channel_free_cb(void *p)
101 * Set the name of the specified channel.
103 * If the channel already has a different name assigned to it, it will be
104 * removed, and the new name will be saved instead.
106 * @param[in] channel The channel whose name to set. Must not be NULL.
107 * @param[in] name The new name that the specified channel should get.
108 * A copy of the string is made.
110 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
114 SR_API int sr_dev_channel_name_set(struct sr_channel *channel,
120 g_free(channel->name);
121 channel->name = g_strdup(name);
127 * Enable or disable a channel.
129 * @param[in] channel The channel to enable or disable. Must not be NULL.
130 * @param[in] state TRUE to enable the channel, FALSE to disable.
132 * @return SR_OK on success or SR_ERR on failure. In case of invalid
133 * arguments, SR_ERR_ARG is returned and the channel enabled state
138 SR_API int sr_dev_channel_enable(struct sr_channel *channel, gboolean state)
141 gboolean was_enabled;
142 struct sr_dev_inst *sdi;
148 was_enabled = channel->enabled;
149 channel->enabled = state;
150 if (!state != !was_enabled && sdi->driver
151 && sdi->driver->config_channel_set) {
152 ret = sdi->driver->config_channel_set(
153 sdi, channel, SR_CHANNEL_SET_ENABLED);
154 /* Roll back change if it wasn't applicable. */
163 * Returns the next enabled channel, wrapping around if necessary.
165 * @param[in] sdi The device instance the channel is connected to.
167 * @param[in] cur_channel The current channel.
169 * @return A pointer to the next enabled channel of this device.
173 SR_PRIV struct sr_channel *sr_next_enabled_channel(const struct sr_dev_inst *sdi,
174 struct sr_channel *cur_channel)
176 struct sr_channel *next_channel;
179 next_channel = cur_channel;
181 l = g_slist_find(sdi->channels, next_channel);
183 next_channel = l->next->data;
185 next_channel = sdi->channels->data;
186 } while (!next_channel->enabled);
192 * Determine whether the specified device instance has the specified
195 * @param sdi Pointer to the device instance to be checked. Must not be NULL.
196 * If the device's 'driver' field is NULL (virtual device), this
197 * function will always return FALSE (virtual devices don't have
198 * a hardware capabilities list).
199 * @param[in] key The option that should be checked for is supported by the
202 * @retval TRUE Device has the specified option.
203 * @retval FALSE Device does not have the specified option, invalid input
204 * parameters or other error conditions.
208 SR_API gboolean sr_dev_has_option(const struct sr_dev_inst *sdi, int key)
215 if (!sdi || !sdi->driver || !sdi->driver->config_list)
218 if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS,
219 &gvar, sdi, NULL) != SR_OK)
223 devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t));
224 for (i = 0; i < num_opts; i++) {
225 if ((devopts[i] & SR_CONF_MASK) == key) {
230 g_variant_unref(gvar);
236 * Enumerate the configuration options of the specified item.
238 * @param driver Pointer to the driver to be checked. Must not be NULL.
239 * @param sdi Pointer to the device instance to be checked. May be NULL to
240 * check driver options.
241 * @param cg Pointer to a channel group, if a specific channel group is to
242 * be checked. Must be NULL to check device-wide options.
244 * @return A GArray * of enum sr_configkey values, or NULL on invalid
245 * arguments. The array must be freed by the caller using
250 SR_API GArray *sr_dev_options(const struct sr_dev_driver *driver,
251 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
254 const uint32_t *opts;
259 if (!driver || !driver->config_list)
262 if (sdi && sdi->driver != driver)
265 if (driver->config_list(SR_CONF_DEVICE_OPTIONS, &gvar, sdi, cg) != SR_OK)
268 opts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(uint32_t));
270 result = g_array_sized_new(FALSE, FALSE, sizeof(uint32_t), num_opts);
272 for (i = 0; i < num_opts; i++) {
273 opt = opts[i] & SR_CONF_MASK;
274 g_array_insert_val(result, i, opt);
277 g_variant_unref(gvar);
283 * Enumerate the configuration capabilities supported by a device instance
284 * for a given configuration key.
286 * @param sdi Pointer to the device instance to be checked. Must not be NULL.
287 * If the device's 'driver' field is NULL (virtual device), this
288 * function will always return FALSE (virtual devices don't have
289 * a hardware capabilities list).
290 * @param cg Pointer to a channel group, if a specific channel group is to
291 * be checked. Must be NULL to check device-wide options.
292 * @param[in] key The option that should be checked for is supported by the
295 * @retval A bitmask of enum sr_configcap values, which will be zero for
296 * invalid inputs or if the key is unsupported.
300 SR_API int sr_dev_config_capabilities_list(const struct sr_dev_inst *sdi,
301 const struct sr_channel_group *cg, const int key)
308 if (!sdi || !sdi->driver || !sdi->driver->config_list)
311 if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS,
312 &gvar, sdi, cg) != SR_OK)
316 devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t));
317 for (i = 0; i < num_opts; i++) {
318 if ((devopts[i] & SR_CONF_MASK) == key) {
319 ret = devopts[i] & ~SR_CONF_MASK;
323 g_variant_unref(gvar);
329 * Allocate and init a new user-generated device instance.
331 * @param vendor Device vendor.
332 * @param model Device model.
333 * @param version Device version.
335 * @retval struct sr_dev_inst *. Dynamically allocated, free using
336 * sr_dev_inst_free().
338 SR_API struct sr_dev_inst *sr_dev_inst_user_new(const char *vendor,
339 const char *model, const char *version)
341 struct sr_dev_inst *sdi;
343 sdi = g_malloc0(sizeof(struct sr_dev_inst));
345 sdi->vendor = g_strdup(vendor);
346 sdi->model = g_strdup(model);
347 sdi->version = g_strdup(version);
348 sdi->inst_type = SR_INST_USER;
354 * Add a new channel to the specified device instance.
356 * @param[in] index @copydoc sr_channel::index
357 * @param[in] type @copydoc sr_channel::type
358 * @param[in] name @copydoc sr_channel::name
360 * @return SR_OK Success.
361 * @return SR_OK Invalid argument.
363 SR_API int sr_dev_inst_channel_add(struct sr_dev_inst *sdi, int index, int type, const char *name)
365 if (!sdi || sdi->inst_type != SR_INST_USER || index < 0)
368 sr_channel_new(sdi, index, type, TRUE, name);
374 * Free device instance struct created by sr_dev_inst().
376 * @param sdi Device instance to free. If NULL, the function will do nothing.
380 SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
382 struct sr_channel *ch;
383 struct sr_channel_group *cg;
389 for (l = sdi->channels; l; l = l->next) {
393 g_slist_free(sdi->channels);
395 for (l = sdi->channel_groups; l; l = l->next) {
398 g_slist_free(cg->channels);
402 g_slist_free(sdi->channel_groups);
405 sr_session_dev_remove(sdi->session, sdi);
409 g_free(sdi->version);
410 g_free(sdi->serial_num);
411 g_free(sdi->connection_id);
415 #ifdef HAVE_LIBUSB_1_0
418 * Allocate and init a struct for a USB device instance.
420 * @param[in] bus @copydoc sr_usb_dev_inst::bus
421 * @param[in] address @copydoc sr_usb_dev_inst::address
422 * @param[in] hdl @copydoc sr_usb_dev_inst::devhdl
424 * @return The struct sr_usb_dev_inst * for USB device instance.
428 SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
429 uint8_t address, struct libusb_device_handle *hdl)
431 struct sr_usb_dev_inst *udi;
433 udi = g_malloc0(sizeof(struct sr_usb_dev_inst));
435 udi->address = address;
442 * Free struct sr_usb_dev_inst * allocated by sr_usb_dev_inst().
444 * @param usb The struct sr_usb_dev_inst * to free. If NULL, this
445 * function does nothing.
449 SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
456 #ifdef HAVE_LIBSERIALPORT
459 * Allocate and init a struct for a serial device instance.
461 * Both parameters are copied to newly allocated strings, and freed
462 * automatically by sr_serial_dev_inst_free().
464 * @param[in] port OS-specific serial port specification. Examples:
465 * "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1".
467 * @param[in] serialcomm A serial communication parameters string, in the form
468 * of \<speed\>/\<data bits\>\<parity\>\<stopbits\>, for example
469 * "9600/8n1" or "600/7o2". This is an optional parameter;
470 * it may be filled in later. Can be NULL.
472 * @return A pointer to a newly initialized struct sr_serial_dev_inst,
477 SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
478 const char *serialcomm)
480 struct sr_serial_dev_inst *serial;
482 serial = g_malloc0(sizeof(struct sr_serial_dev_inst));
483 serial->port = g_strdup(port);
485 serial->serialcomm = g_strdup(serialcomm);
491 * Free struct sr_serial_dev_inst * allocated by sr_serial_dev_inst().
493 * @param serial The struct sr_serial_dev_inst * to free. If NULL, this
494 * function will do nothing.
498 SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
503 g_free(serial->port);
504 g_free(serial->serialcomm);
510 SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device)
512 struct sr_usbtmc_dev_inst *usbtmc;
514 usbtmc = g_malloc0(sizeof(struct sr_usbtmc_dev_inst));
515 usbtmc->device = g_strdup(device);
522 SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc)
527 g_free(usbtmc->device);
532 * Get the list of devices/instances of the specified driver.
534 * @param driver The driver to use. Must not be NULL.
536 * @return The list of devices/instances of this driver, or NULL upon errors
537 * or if the list is empty.
541 SR_API GSList *sr_dev_list(const struct sr_dev_driver *driver)
543 if (driver && driver->dev_list)
544 return driver->dev_list(driver);
550 * Clear the list of device instances a driver knows about.
552 * @param driver The driver to use. This must be a pointer to one of
553 * the entries returned by sr_driver_list(). Must not be NULL.
555 * @retval SR_OK Success.
556 * @retval SR_ERR_ARG Invalid driver.
560 SR_API int sr_dev_clear(const struct sr_dev_driver *driver)
563 sr_err("Invalid driver.");
567 if (!driver->context) {
569 * Driver was never initialized, nothing to do.
571 * No log message since this usually gets called for all
572 * drivers, whether they were initialized or not.
577 /* No log message here, too verbose and not very useful. */
579 return driver->dev_clear(driver);
583 * Open the specified device instance.
585 * If the device instance is already open (sdi->status == SR_ST_ACTIVE),
586 * SR_ERR will be returned and no re-opening of the device will be attempted.
588 * If opening was successful, sdi->status is set to SR_ST_ACTIVE, otherwise
589 * it will be left unchanged.
591 * @param sdi Device instance to use. Must not be NULL.
593 * @retval SR_OK Success.
594 * @retval SR_ERR_ARG Invalid arguments.
595 * @retval SR_ERR Device instance was already active, or other error.
599 SR_API int sr_dev_open(struct sr_dev_inst *sdi)
603 if (!sdi || !sdi->driver || !sdi->driver->dev_open)
606 if (sdi->status == SR_ST_ACTIVE) {
607 sr_err("%s: Device instance already active, can't re-open.",
612 sr_dbg("%s: Opening device instance.", sdi->driver->name);
614 ret = sdi->driver->dev_open(sdi);
617 sdi->status = SR_ST_ACTIVE;
623 * Close the specified device instance.
625 * If the device instance is not open (sdi->status != SR_ST_ACTIVE),
626 * SR_ERR_DEV_CLOSED will be returned and no closing will be attempted.
628 * Note: sdi->status will be set to SR_ST_INACTIVE, regardless of whether
629 * there are any errors during closing of the device instance (any errors
630 * will be reported via error code and log message, though).
632 * @param sdi Device instance to use. Must not be NULL.
634 * @retval SR_OK Success.
635 * @retval SR_ERR_ARG Invalid arguments.
636 * @retval SR_ERR_DEV_CLOSED Device instance was not active.
637 * @retval SR_ERR Other error.
641 SR_API int sr_dev_close(struct sr_dev_inst *sdi)
643 if (!sdi || !sdi->driver || !sdi->driver->dev_close)
646 if (sdi->status != SR_ST_ACTIVE) {
647 sr_err("%s: Device instance not active, can't close.",
649 return SR_ERR_DEV_CLOSED;
652 sdi->status = SR_ST_INACTIVE;
654 sr_dbg("%s: Closing device instance.", sdi->driver->name);
656 return sdi->driver->dev_close(sdi);
660 * Queries a device instances' driver.
662 * @param sdi Device instance to use. Must not be NULL.
664 * @return The driver instance or NULL on error.
666 SR_API struct sr_dev_driver *sr_dev_inst_driver_get(const struct sr_dev_inst *sdi)
668 if (!sdi || !sdi->driver)
675 * Queries a device instances' vendor.
677 * @param sdi Device instance to use. Must not be NULL.
679 * @return The vendor string or NULL.
681 SR_API const char *sr_dev_inst_vendor_get(const struct sr_dev_inst *sdi)
690 * Queries a device instances' model.
692 * @param sdi Device instance to use. Must not be NULL.
694 * @return The model string or NULL.
696 SR_API const char *sr_dev_inst_model_get(const struct sr_dev_inst *sdi)
705 * Queries a device instances' version.
707 * @param sdi Device instance to use. Must not be NULL.
709 * @return The version string or NULL.
711 SR_API const char *sr_dev_inst_version_get(const struct sr_dev_inst *sdi)
720 * Queries a device instances' serial number.
722 * @param sdi Device instance to use. Must not be NULL.
724 * @return The serial number string or NULL.
726 SR_API const char *sr_dev_inst_sernum_get(const struct sr_dev_inst *sdi)
731 return sdi->serial_num;
735 * Queries a device instances' connection identifier.
737 * @param sdi Device instance to use. Must not be NULL.
739 * @return A copy of the connection ID string or NULL. The caller is responsible
740 * for g_free()ing the string when it is no longer needed.
742 SR_API const char *sr_dev_inst_connid_get(const struct sr_dev_inst *sdi)
744 #ifdef HAVE_LIBUSB_1_0
745 struct drv_context *drvc;
747 char connection_id[64];
748 struct sr_usb_dev_inst *usb;
749 struct libusb_device **devlist;
755 #ifdef HAVE_LIBSERIALPORT
756 struct sr_serial_dev_inst *serial;
758 if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_SERIAL)) {
759 /* connection_id isn't populated, let's do that here. */
762 ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(serial->port);
766 #ifdef HAVE_LIBUSB_1_0
767 if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_USB)) {
768 /* connection_id isn't populated, let's do that here. */
770 drvc = sdi->driver->context;
773 if ((cnt = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist)) < 0) {
774 sr_err("Failed to retrieve device list: %s.",
775 libusb_error_name(cnt));
779 for (i = 0; i < cnt; i++) {
780 /* Find the USB device by the logical address we know. */
781 b = libusb_get_bus_number(devlist[i]);
782 a = libusb_get_device_address(devlist[i]);
783 if (b != usb->bus || a != usb->address)
786 if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0)
789 ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(connection_id);
793 libusb_free_device_list(devlist, 1);
797 return sdi->connection_id;
801 * Queries a device instances' channel list.
803 * @param sdi Device instance to use. Must not be NULL.
805 * @return The GSList of channels or NULL.
807 SR_API GSList *sr_dev_inst_channels_get(const struct sr_dev_inst *sdi)
812 return sdi->channels;
816 * Queries a device instances' channel groups list.
818 * @param sdi Device instance to use. Must not be NULL.
820 * @return The GSList of channel groups or NULL.
822 SR_API GSList *sr_dev_inst_channel_groups_get(const struct sr_dev_inst *sdi)
827 return sdi->channel_groups;