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/>.
24 #include <libsigrok/libsigrok.h>
25 #include "libsigrok-internal.h"
29 #define LOG_PREFIX "device"
35 * Device handling in libsigrok.
39 * @defgroup grp_devices Devices
41 * Device handling in libsigrok.
47 * Allocate and initialize a new struct sr_channel and add it to sdi.
49 * @param[in] sdi The device instance the channel is connected to.
51 * @param[in] index @copydoc sr_channel::index
52 * @param[in] type @copydoc sr_channel::type
53 * @param[in] enabled @copydoc sr_channel::enabled
54 * @param[in] name @copydoc sr_channel::name
56 * @return A new struct sr_channel*.
60 SR_PRIV struct sr_channel *sr_channel_new(struct sr_dev_inst *sdi,
61 int index, int type, gboolean enabled, const char *name)
63 struct sr_channel *ch;
65 ch = g_malloc0(sizeof(struct sr_channel));
69 ch->enabled = enabled;
71 ch->name = g_strdup(name);
73 sdi->channels = g_slist_append(sdi->channels, ch);
79 * Release a previously allocated struct sr_channel.
81 * @param[in] ch Pointer to struct sr_channel.
85 SR_PRIV void sr_channel_free(struct sr_channel *ch)
95 * Wrapper around @ref sr_channel_free(), suitable for glib iterators.
97 SR_PRIV void sr_channel_free_cb(void *p)
103 * Set the name of the specified channel.
105 * If the channel already has a different name assigned to it, it will be
106 * removed, and the new name will be saved instead.
108 * @param[in] channel The channel whose name to set. Must not be NULL.
109 * @param[in] name The new name that the specified channel should get.
110 * A copy of the string is made.
112 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
116 SR_API int sr_dev_channel_name_set(struct sr_channel *channel,
122 g_free(channel->name);
123 channel->name = g_strdup(name);
129 * Enable or disable a channel.
131 * @param[in] channel The channel to enable or disable. Must not be NULL.
132 * @param[in] state TRUE to enable the channel, FALSE to disable.
134 * @return SR_OK on success or SR_ERR on failure. In case of invalid
135 * arguments, SR_ERR_ARG is returned and the channel enabled state
140 SR_API int sr_dev_channel_enable(struct sr_channel *channel, gboolean state)
143 gboolean was_enabled;
144 struct sr_dev_inst *sdi;
150 was_enabled = channel->enabled;
151 channel->enabled = state;
152 if (!state != !was_enabled && sdi->driver
153 && sdi->driver->config_channel_set) {
154 ret = sdi->driver->config_channel_set(
155 sdi, channel, SR_CHANNEL_SET_ENABLED);
156 /* Roll back change if it wasn't applicable. */
165 * Returns the next enabled channel, wrapping around if necessary.
167 * @param[in] sdi The device instance the channel is connected to.
169 * @param[in] cur_channel The current channel.
171 * @return A pointer to the next enabled channel of this device.
175 SR_PRIV struct sr_channel *sr_next_enabled_channel(const struct sr_dev_inst *sdi,
176 struct sr_channel *cur_channel)
178 struct sr_channel *next_channel;
181 next_channel = cur_channel;
183 l = g_slist_find(sdi->channels, next_channel);
185 next_channel = l->next->data;
187 next_channel = sdi->channels->data;
188 } while (!next_channel->enabled);
194 * Compare two channels, return whether they differ.
196 * The channels' names and types are checked. The enabled state is not
197 * considered a condition for difference. The test is motivated by the
198 * desire to detect changes in the configuration of acquisition setups
199 * between re-reads of an input file.
201 * @param[in] ch1 First channel.
202 * @param[in] ch2 Second channel.
204 * @return #TRUE upon differences or unexpected input, #FALSE otherwise.
208 SR_PRIV gboolean sr_channels_differ(struct sr_channel *ch1, struct sr_channel *ch2)
213 if (ch1->type != ch2->type)
215 if (strcmp(ch1->name, ch2->name))
222 * Compare two channel lists, return whether they differ.
224 * Listing the same set of channels but in a different order is considered
225 * a difference in the lists.
227 * @param[in] l1 First channel list.
228 * @param[in] l2 Second channel list.
230 * @return #TRUE upon differences or unexpected input, #FALSE otherwise.
234 SR_PRIV gboolean sr_channel_lists_differ(GSList *l1, GSList *l2)
236 struct sr_channel *ch1, *ch2;
245 if (sr_channels_differ(ch1, ch2))
247 if (ch1->index != ch2->index)
257 * Determine whether the specified device instance has the specified
260 * @param sdi Pointer to the device instance to be checked. Must not be NULL.
261 * If the device's 'driver' field is NULL (virtual device), this
262 * function will always return FALSE (virtual devices don't have
263 * a hardware capabilities list).
264 * @param[in] key The option that should be checked for is supported by the
267 * @retval TRUE Device has the specified option.
268 * @retval FALSE Device does not have the specified option, invalid input
269 * parameters or other error conditions.
273 SR_API gboolean sr_dev_has_option(const struct sr_dev_inst *sdi, int key)
280 if (!sdi || !sdi->driver || !sdi->driver->config_list)
283 if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS,
284 &gvar, sdi, NULL) != SR_OK)
288 devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t));
289 for (i = 0; i < num_opts; i++) {
290 if ((devopts[i] & SR_CONF_MASK) == key) {
295 g_variant_unref(gvar);
301 * Enumerate the configuration options of the specified item.
303 * @param driver Pointer to the driver to be checked. Must not be NULL.
304 * @param sdi Pointer to the device instance to be checked. May be NULL to
305 * check driver options.
306 * @param cg Pointer to a channel group, if a specific channel group is to
307 * be checked. Must be NULL to check device-wide options.
309 * @return A GArray * of enum sr_configkey values, or NULL on invalid
310 * arguments. The array must be freed by the caller using
315 SR_API GArray *sr_dev_options(const struct sr_dev_driver *driver,
316 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
319 const uint32_t *opts;
324 if (!driver || !driver->config_list)
327 if (sdi && sdi->driver != driver)
330 if (driver->config_list(SR_CONF_DEVICE_OPTIONS, &gvar, sdi, cg) != SR_OK)
333 opts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(uint32_t));
335 result = g_array_sized_new(FALSE, FALSE, sizeof(uint32_t), num_opts);
337 for (i = 0; i < num_opts; i++) {
338 opt = opts[i] & SR_CONF_MASK;
339 g_array_insert_val(result, i, opt);
342 g_variant_unref(gvar);
348 * Enumerate the configuration capabilities supported by a device instance
349 * for a given configuration key.
351 * @param sdi Pointer to the device instance to be checked. Must not be NULL.
352 * If the device's 'driver' field is NULL (virtual device), this
353 * function will always return FALSE (virtual devices don't have
354 * a hardware capabilities list).
355 * @param cg Pointer to a channel group, if a specific channel group is to
356 * be checked. Must be NULL to check device-wide options.
357 * @param[in] key The option that should be checked for is supported by the
360 * @retval A bitmask of enum sr_configcap values, which will be zero for
361 * invalid inputs or if the key is unsupported.
365 SR_API int sr_dev_config_capabilities_list(const struct sr_dev_inst *sdi,
366 const struct sr_channel_group *cg, const int key)
373 if (!sdi || !sdi->driver || !sdi->driver->config_list)
376 if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS,
377 &gvar, sdi, cg) != SR_OK)
381 devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t));
382 for (i = 0; i < num_opts; i++) {
383 if ((devopts[i] & SR_CONF_MASK) == key) {
384 ret = devopts[i] & ~SR_CONF_MASK;
388 g_variant_unref(gvar);
394 * Allocate and init a new user-generated device instance.
396 * @param vendor Device vendor.
397 * @param model Device model.
398 * @param version Device version.
400 * @retval struct sr_dev_inst *. Dynamically allocated, free using
401 * sr_dev_inst_free().
403 SR_API struct sr_dev_inst *sr_dev_inst_user_new(const char *vendor,
404 const char *model, const char *version)
406 struct sr_dev_inst *sdi;
408 sdi = g_malloc0(sizeof(struct sr_dev_inst));
410 sdi->vendor = g_strdup(vendor);
411 sdi->model = g_strdup(model);
412 sdi->version = g_strdup(version);
413 sdi->inst_type = SR_INST_USER;
419 * Add a new channel to the specified device instance.
421 * @param[in] index @copydoc sr_channel::index
422 * @param[in] type @copydoc sr_channel::type
423 * @param[in] name @copydoc sr_channel::name
425 * @return SR_OK Success.
426 * @return SR_OK Invalid argument.
428 SR_API int sr_dev_inst_channel_add(struct sr_dev_inst *sdi, int index, int type, const char *name)
430 if (!sdi || sdi->inst_type != SR_INST_USER || index < 0)
433 sr_channel_new(sdi, index, type, TRUE, name);
439 * Free device instance struct created by sr_dev_inst().
441 * @param sdi Device instance to free. If NULL, the function will do nothing.
445 SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
447 struct sr_channel *ch;
448 struct sr_channel_group *cg;
454 for (l = sdi->channels; l; l = l->next) {
458 g_slist_free(sdi->channels);
460 for (l = sdi->channel_groups; l; l = l->next) {
463 g_slist_free(cg->channels);
467 g_slist_free(sdi->channel_groups);
470 sr_session_dev_remove(sdi->session, sdi);
474 g_free(sdi->version);
475 g_free(sdi->serial_num);
476 g_free(sdi->connection_id);
480 #ifdef HAVE_LIBUSB_1_0
483 * Allocate and init a struct for a USB device instance.
485 * @param[in] bus @copydoc sr_usb_dev_inst::bus
486 * @param[in] address @copydoc sr_usb_dev_inst::address
487 * @param[in] hdl @copydoc sr_usb_dev_inst::devhdl
489 * @return The struct sr_usb_dev_inst * for USB device instance.
493 SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
494 uint8_t address, struct libusb_device_handle *hdl)
496 struct sr_usb_dev_inst *udi;
498 udi = g_malloc0(sizeof(struct sr_usb_dev_inst));
500 udi->address = address;
507 * Free struct sr_usb_dev_inst * allocated by sr_usb_dev_inst().
509 * @param usb The struct sr_usb_dev_inst * to free. If NULL, this
510 * function does nothing.
514 SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
521 #ifdef HAVE_LIBSERIALPORT
524 * Allocate and init a struct for a serial device instance.
526 * Both parameters are copied to newly allocated strings, and freed
527 * automatically by sr_serial_dev_inst_free().
529 * @param[in] port OS-specific serial port specification. Examples:
530 * "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1".
532 * @param[in] serialcomm A serial communication parameters string, in the form
533 * of \<speed\>/\<data bits\>\<parity\>\<stopbits\>, for example
534 * "9600/8n1" or "600/7o2". This is an optional parameter;
535 * it may be filled in later. Can be NULL.
537 * @return A pointer to a newly initialized struct sr_serial_dev_inst,
542 SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
543 const char *serialcomm)
545 struct sr_serial_dev_inst *serial;
547 serial = g_malloc0(sizeof(struct sr_serial_dev_inst));
548 serial->port = g_strdup(port);
550 serial->serialcomm = g_strdup(serialcomm);
556 * Free struct sr_serial_dev_inst * allocated by sr_serial_dev_inst().
558 * @param serial The struct sr_serial_dev_inst * to free. If NULL, this
559 * function will do nothing.
563 SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
568 g_free(serial->port);
569 g_free(serial->serialcomm);
575 SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device)
577 struct sr_usbtmc_dev_inst *usbtmc;
579 usbtmc = g_malloc0(sizeof(struct sr_usbtmc_dev_inst));
580 usbtmc->device = g_strdup(device);
587 SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc)
592 g_free(usbtmc->device);
597 * Get the list of devices/instances of the specified driver.
599 * @param driver The driver to use. Must not be NULL.
601 * @return The list of devices/instances of this driver, or NULL upon errors
602 * or if the list is empty.
606 SR_API GSList *sr_dev_list(const struct sr_dev_driver *driver)
608 if (driver && driver->dev_list)
609 return driver->dev_list(driver);
615 * Clear the list of device instances a driver knows about.
617 * @param driver The driver to use. This must be a pointer to one of
618 * the entries returned by sr_driver_list(). Must not be NULL.
620 * @retval SR_OK Success.
621 * @retval SR_ERR_ARG Invalid driver.
625 SR_API int sr_dev_clear(const struct sr_dev_driver *driver)
628 sr_err("Invalid driver.");
632 if (!driver->context) {
634 * Driver was never initialized, nothing to do.
636 * No log message since this usually gets called for all
637 * drivers, whether they were initialized or not.
642 /* No log message here, too verbose and not very useful. */
644 return driver->dev_clear(driver);
648 * Open the specified device instance.
650 * If the device instance is already open (sdi->status == SR_ST_ACTIVE),
651 * SR_ERR will be returned and no re-opening of the device will be attempted.
653 * If opening was successful, sdi->status is set to SR_ST_ACTIVE, otherwise
654 * it will be left unchanged.
656 * @param sdi Device instance to use. Must not be NULL.
658 * @retval SR_OK Success.
659 * @retval SR_ERR_ARG Invalid arguments.
660 * @retval SR_ERR Device instance was already active, or other error.
664 SR_API int sr_dev_open(struct sr_dev_inst *sdi)
668 if (!sdi || !sdi->driver || !sdi->driver->dev_open)
671 if (sdi->status == SR_ST_ACTIVE) {
672 sr_err("%s: Device instance already active, can't re-open.",
677 sr_dbg("%s: Opening device instance.", sdi->driver->name);
679 ret = sdi->driver->dev_open(sdi);
682 sdi->status = SR_ST_ACTIVE;
688 * Close the specified device instance.
690 * If the device instance is not open (sdi->status != SR_ST_ACTIVE),
691 * SR_ERR_DEV_CLOSED will be returned and no closing will be attempted.
693 * Note: sdi->status will be set to SR_ST_INACTIVE, regardless of whether
694 * there are any errors during closing of the device instance (any errors
695 * will be reported via error code and log message, though).
697 * @param sdi Device instance to use. Must not be NULL.
699 * @retval SR_OK Success.
700 * @retval SR_ERR_ARG Invalid arguments.
701 * @retval SR_ERR_DEV_CLOSED Device instance was not active.
702 * @retval SR_ERR Other error.
706 SR_API int sr_dev_close(struct sr_dev_inst *sdi)
708 if (!sdi || !sdi->driver || !sdi->driver->dev_close)
711 if (sdi->status != SR_ST_ACTIVE) {
712 sr_err("%s: Device instance not active, can't close.",
714 return SR_ERR_DEV_CLOSED;
717 sdi->status = SR_ST_INACTIVE;
719 sr_dbg("%s: Closing device instance.", sdi->driver->name);
721 return sdi->driver->dev_close(sdi);
725 * Queries a device instances' driver.
727 * @param sdi Device instance to use. Must not be NULL.
729 * @return The driver instance or NULL on error.
731 SR_API struct sr_dev_driver *sr_dev_inst_driver_get(const struct sr_dev_inst *sdi)
733 if (!sdi || !sdi->driver)
740 * Queries a device instances' vendor.
742 * @param sdi Device instance to use. Must not be NULL.
744 * @return The vendor string or NULL.
746 SR_API const char *sr_dev_inst_vendor_get(const struct sr_dev_inst *sdi)
755 * Queries a device instances' model.
757 * @param sdi Device instance to use. Must not be NULL.
759 * @return The model string or NULL.
761 SR_API const char *sr_dev_inst_model_get(const struct sr_dev_inst *sdi)
770 * Queries a device instances' version.
772 * @param sdi Device instance to use. Must not be NULL.
774 * @return The version string or NULL.
776 SR_API const char *sr_dev_inst_version_get(const struct sr_dev_inst *sdi)
785 * Queries a device instances' serial number.
787 * @param sdi Device instance to use. Must not be NULL.
789 * @return The serial number string or NULL.
791 SR_API const char *sr_dev_inst_sernum_get(const struct sr_dev_inst *sdi)
796 return sdi->serial_num;
800 * Queries a device instances' connection identifier.
802 * @param sdi Device instance to use. Must not be NULL.
804 * @return A copy of the connection ID string or NULL. The caller is responsible
805 * for g_free()ing the string when it is no longer needed.
807 SR_API const char *sr_dev_inst_connid_get(const struct sr_dev_inst *sdi)
809 #ifdef HAVE_LIBUSB_1_0
810 struct drv_context *drvc;
812 char conn_id_usb[64];
813 struct sr_usb_dev_inst *usb;
814 struct libusb_device **devlist;
817 #ifdef HAVE_LIBSERIALPORT
818 struct sr_serial_dev_inst *serial;
821 struct sr_scpi_dev_inst *scpi;
827 #ifdef HAVE_LIBSERIALPORT
828 if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_SERIAL)) {
829 /* connection_id isn't populated, let's do that for serial devices. */
832 ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(serial->port);
836 #ifdef HAVE_LIBUSB_1_0
837 if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_USB)) {
838 /* connection_id isn't populated, let's do that for USB devices. */
840 drvc = sdi->driver->context;
843 if ((cnt = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist)) < 0) {
844 sr_err("Failed to retrieve device list: %s.",
845 libusb_error_name(cnt));
849 for (i = 0; i < cnt; i++) {
850 /* Find the USB device by the logical address we know. */
851 b = libusb_get_bus_number(devlist[i]);
852 a = libusb_get_device_address(devlist[i]);
853 if (b != usb->bus || a != usb->address)
856 if (usb_get_port_path(devlist[i], conn_id_usb, sizeof(conn_id_usb)) < 0)
859 ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(conn_id_usb);
863 libusb_free_device_list(devlist, 1);
867 if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_SCPI)) {
868 /* connection_id isn't populated, let's do that for SCPI devices. */
871 sr_scpi_connection_id(scpi, &conn_id_scpi);
872 ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(conn_id_scpi);
873 g_free(conn_id_scpi);
876 return sdi->connection_id;
880 * Queries a device instances' channel list.
882 * @param sdi Device instance to use. Must not be NULL.
884 * @return The GSList of channels or NULL.
886 SR_API GSList *sr_dev_inst_channels_get(const struct sr_dev_inst *sdi)
891 return sdi->channels;
895 * Queries a device instances' channel groups list.
897 * @param sdi Device instance to use. Must not be NULL.
899 * @return The GSList of channel groups or NULL.
901 SR_API GSList *sr_dev_inst_channel_groups_get(const struct sr_dev_inst *sdi)
906 return sdi->channel_groups;