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(*ch));
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 sr_channel_free(), suitable for glib iterators.
99 SR_PRIV void sr_channel_free_cb(void *p)
105 * Set the name of the specified channel.
107 * If the channel already has a different name assigned to it, it will be
108 * removed, and the new name will be saved instead.
110 * @param[in] channel The channel whose name to set. Must not be NULL.
111 * @param[in] name The new name that the specified channel should get.
112 * A copy of the string is made.
114 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
118 SR_API int sr_dev_channel_name_set(struct sr_channel *channel,
124 g_free(channel->name);
125 channel->name = g_strdup(name);
131 * Enable or disable a channel.
133 * @param[in] channel The channel to enable or disable. Must not be NULL.
134 * @param[in] state TRUE to enable the channel, FALSE to disable.
136 * @return SR_OK on success or SR_ERR on failure. In case of invalid
137 * arguments, SR_ERR_ARG is returned and the channel enabled state
142 SR_API int sr_dev_channel_enable(struct sr_channel *channel, gboolean state)
145 gboolean was_enabled;
146 struct sr_dev_inst *sdi;
152 was_enabled = channel->enabled;
153 channel->enabled = state;
154 if (!state != !was_enabled && sdi->driver
155 && sdi->driver->config_channel_set) {
156 ret = sdi->driver->config_channel_set(
157 sdi, channel, SR_CHANNEL_SET_ENABLED);
158 /* Roll back change if it wasn't applicable. */
167 * Returns the next enabled channel, wrapping around if necessary.
169 * @param[in] sdi The device instance the channel is connected to.
171 * @param[in] cur_channel The current channel.
173 * @return A pointer to the next enabled channel of this device.
177 SR_PRIV struct sr_channel *sr_next_enabled_channel(const struct sr_dev_inst *sdi,
178 struct sr_channel *cur_channel)
180 struct sr_channel *next_channel;
183 next_channel = cur_channel;
185 l = g_slist_find(sdi->channels, next_channel);
187 next_channel = l->next->data;
189 next_channel = sdi->channels->data;
190 } while (!next_channel->enabled);
196 * Compare two channels, return whether they differ.
198 * The channels' names and types are checked. The enabled state is not
199 * considered a condition for difference. The test is motivated by the
200 * desire to detect changes in the configuration of acquisition setups
201 * between re-reads of an input file.
203 * @param[in] ch1 First channel.
204 * @param[in] ch2 Second channel.
206 * @return TRUE upon differences or unexpected input, FALSE otherwise.
210 SR_PRIV gboolean sr_channels_differ(struct sr_channel *ch1, struct sr_channel *ch2)
215 if (ch1->type != ch2->type)
217 if (strcmp(ch1->name, ch2->name))
224 * Compare two channel lists, return whether they differ.
226 * Listing the same set of channels but in a different order is considered
227 * a difference in the lists.
229 * @param[in] l1 First channel list.
230 * @param[in] l2 Second channel list.
232 * @return TRUE upon differences or unexpected input, FALSE otherwise.
236 SR_PRIV gboolean sr_channel_lists_differ(GSList *l1, GSList *l2)
238 struct sr_channel *ch1, *ch2;
247 if (sr_channels_differ(ch1, ch2))
249 if (ch1->index != ch2->index)
259 * Allocate and initialize a new channel group, and add it to sdi.
261 * @param[in] sdi The device instance the channel group is connected to.
262 * Optional, can be NULL.
263 * @param[in] name @copydoc sr_channel_group::name
264 * @param[in] priv @copydoc sr_channel_group::priv
266 * @return A pointer to a new struct sr_channel_group, NULL upon error.
270 SR_PRIV struct sr_channel_group *sr_channel_group_new(struct sr_dev_inst *sdi,
271 const char *name, void *priv)
273 struct sr_channel_group *cg;
275 cg = g_malloc0(sizeof(*cg));
277 cg->name = g_strdup(name);
281 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
287 * Release a previously allocated struct sr_channel_group.
289 * @param[in] cg Pointer to struct sr_channel_group.
293 SR_PRIV void sr_channel_group_free(struct sr_channel_group *cg)
299 g_slist_free(cg->channels);
305 * Wrapper around sr_channel_group_free(), suitable for glib iterators.
309 SR_PRIV void sr_channel_group_free_cb(void *cg)
311 return sr_channel_group_free(cg);
315 * Determine whether the specified device instance has the specified
318 * @param sdi Pointer to the device instance to be checked. Must not be NULL.
319 * If the device's 'driver' field is NULL (virtual device), this
320 * function will always return FALSE (virtual devices don't have
321 * a hardware capabilities list).
322 * @param[in] key The option that should be checked for is supported by the
325 * @retval TRUE Device has the specified option.
326 * @retval FALSE Device does not have the specified option, invalid input
327 * parameters or other error conditions.
331 SR_API gboolean sr_dev_has_option(const struct sr_dev_inst *sdi, int key)
338 if (!sdi || !sdi->driver || !sdi->driver->config_list)
341 if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS,
342 &gvar, sdi, NULL) != SR_OK)
346 devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t));
347 for (i = 0; i < num_opts; i++) {
348 if ((devopts[i] & SR_CONF_MASK) == key) {
353 g_variant_unref(gvar);
359 * Enumerate the configuration options of the specified item.
361 * @param driver Pointer to the driver to be checked. Must not be NULL.
362 * @param sdi Pointer to the device instance to be checked. May be NULL to
363 * check driver options.
364 * @param cg Pointer to a channel group, if a specific channel group is to
365 * be checked. Must be NULL to check device-wide options.
367 * @return A GArray * of enum sr_configkey values, or NULL on invalid
368 * arguments. The array must be freed by the caller using
373 SR_API GArray *sr_dev_options(const struct sr_dev_driver *driver,
374 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
377 const uint32_t *opts;
382 if (!driver || !driver->config_list)
385 if (sdi && sdi->driver != driver)
388 if (driver->config_list(SR_CONF_DEVICE_OPTIONS, &gvar, sdi, cg) != SR_OK)
391 opts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(uint32_t));
393 result = g_array_sized_new(FALSE, FALSE, sizeof(uint32_t), num_opts);
395 for (i = 0; i < num_opts; i++) {
396 opt = opts[i] & SR_CONF_MASK;
397 g_array_insert_val(result, i, opt);
400 g_variant_unref(gvar);
406 * Enumerate the configuration capabilities supported by a device instance
407 * for a given configuration key.
409 * @param sdi Pointer to the device instance to be checked. Must not be NULL.
410 * If the device's 'driver' field is NULL (virtual device), this
411 * function will always return FALSE (virtual devices don't have
412 * a hardware capabilities list).
413 * @param cg Pointer to a channel group, if a specific channel group is to
414 * be checked. Must be NULL to check device-wide options.
415 * @param[in] key The option that should be checked for is supported by the
418 * @retval A bitmask of enum sr_configcap values, which will be zero for
419 * invalid inputs or if the key is unsupported.
423 SR_API int sr_dev_config_capabilities_list(const struct sr_dev_inst *sdi,
424 const struct sr_channel_group *cg, const int key)
431 if (!sdi || !sdi->driver || !sdi->driver->config_list)
434 if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS,
435 &gvar, sdi, cg) != SR_OK)
439 devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t));
440 for (i = 0; i < num_opts; i++) {
441 if ((devopts[i] & SR_CONF_MASK) == key) {
442 ret = devopts[i] & ~SR_CONF_MASK;
446 g_variant_unref(gvar);
452 * Allocate and init a new user-generated device instance.
454 * @param vendor Device vendor.
455 * @param model Device model.
456 * @param version Device version.
458 * @retval struct sr_dev_inst *. Dynamically allocated, free using
459 * sr_dev_inst_free().
461 SR_API struct sr_dev_inst *sr_dev_inst_user_new(const char *vendor,
462 const char *model, const char *version)
464 struct sr_dev_inst *sdi;
466 sdi = g_malloc0(sizeof(*sdi));
468 sdi->vendor = g_strdup(vendor);
469 sdi->model = g_strdup(model);
470 sdi->version = g_strdup(version);
471 sdi->inst_type = SR_INST_USER;
477 * Add a new channel to the specified device instance.
479 * @param[in] sdi Device instance to use. Must not be NULL.
480 * @param[in] index @copydoc sr_channel::index
481 * @param[in] type @copydoc sr_channel::type
482 * @param[in] name @copydoc sr_channel::name
484 * @return SR_OK Success.
485 * @return SR_OK Invalid argument.
487 SR_API int sr_dev_inst_channel_add(struct sr_dev_inst *sdi, int index, int type, const char *name)
489 if (!sdi || sdi->inst_type != SR_INST_USER || index < 0)
492 sr_channel_new(sdi, index, type, TRUE, name);
498 * Free device instance struct created by sr_dev_inst().
500 * @param sdi Device instance to free. If NULL, the function will do nothing.
504 SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
506 struct sr_channel *ch;
512 for (l = sdi->channels; l; l = l->next) {
516 g_slist_free(sdi->channels);
517 g_slist_free_full(sdi->channel_groups, sr_channel_group_free_cb);
520 sr_session_dev_remove(sdi->session, sdi);
524 g_free(sdi->version);
525 g_free(sdi->serial_num);
526 g_free(sdi->connection_id);
530 #ifdef HAVE_LIBUSB_1_0
533 * Allocate and init a struct for a USB device instance.
535 * @param[in] bus @copydoc sr_usb_dev_inst::bus
536 * @param[in] address @copydoc sr_usb_dev_inst::address
537 * @param[in] hdl @copydoc sr_usb_dev_inst::devhdl
539 * @return The struct sr_usb_dev_inst * for USB device instance.
543 SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
544 uint8_t address, struct libusb_device_handle *hdl)
546 struct sr_usb_dev_inst *udi;
548 udi = g_malloc0(sizeof(*udi));
550 udi->address = address;
557 * Free struct sr_usb_dev_inst * allocated by sr_usb_dev_inst().
559 * @param usb The struct sr_usb_dev_inst * to free. If NULL, this
560 * function does nothing.
564 SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
570 * Wrapper for g_slist_free_full() convenience.
574 SR_PRIV void sr_usb_dev_inst_free_cb(gpointer p)
576 sr_usb_dev_inst_free(p);
580 #ifdef HAVE_SERIAL_COMM
583 * Allocate and init a struct for a serial device instance.
585 * Both parameters are copied to newly allocated strings, and freed
586 * automatically by sr_serial_dev_inst_free().
588 * @param[in] port OS-specific serial port specification. Examples:
589 * "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1".
591 * @param[in] serialcomm A serial communication parameters string, in the form
592 * of \<speed\>/\<data bits\>\<parity\>\<stopbits\>, for example
593 * "9600/8n1" or "600/7o2". This is an optional parameter;
594 * it may be filled in later. Can be NULL.
596 * @return A pointer to a newly initialized struct sr_serial_dev_inst,
601 SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
602 const char *serialcomm)
604 struct sr_serial_dev_inst *serial;
606 serial = g_malloc0(sizeof(*serial));
607 serial->port = g_strdup(port);
609 serial->serialcomm = g_strdup(serialcomm);
615 * Free struct sr_serial_dev_inst * allocated by sr_serial_dev_inst().
617 * @param serial The struct sr_serial_dev_inst * to free. If NULL, this
618 * function will do nothing.
622 SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
627 g_free(serial->port);
628 g_free(serial->serialcomm);
634 SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device)
636 struct sr_usbtmc_dev_inst *usbtmc;
638 usbtmc = g_malloc0(sizeof(*usbtmc));
639 usbtmc->device = g_strdup(device);
646 SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc)
651 g_free(usbtmc->device);
656 * Get the list of devices/instances of the specified driver.
658 * @param driver The driver to use. Must not be NULL.
660 * @return The list of devices/instances of this driver, or NULL upon errors
661 * or if the list is empty.
665 SR_API GSList *sr_dev_list(const struct sr_dev_driver *driver)
667 if (driver && driver->dev_list)
668 return driver->dev_list(driver);
674 * Clear the list of device instances a driver knows about.
676 * @param driver The driver to use. This must be a pointer to one of
677 * the entries returned by sr_driver_list(). Must not be NULL.
679 * @retval SR_OK Success.
680 * @retval SR_ERR_ARG Invalid driver.
684 SR_API int sr_dev_clear(const struct sr_dev_driver *driver)
687 sr_err("Invalid driver.");
691 if (!driver->context) {
693 * Driver was never initialized, nothing to do.
695 * No log message since this usually gets called for all
696 * drivers, whether they were initialized or not.
701 /* No log message here, too verbose and not very useful. */
703 return driver->dev_clear(driver);
707 * Open the specified device instance.
709 * If the device instance is already open (sdi->status == SR_ST_ACTIVE),
710 * SR_ERR will be returned and no re-opening of the device will be attempted.
712 * If opening was successful, sdi->status is set to SR_ST_ACTIVE, otherwise
713 * it will be left unchanged.
715 * @param sdi Device instance to use. Must not be NULL.
717 * @retval SR_OK Success.
718 * @retval SR_ERR_ARG Invalid arguments.
719 * @retval SR_ERR Device instance was already active, or other error.
723 SR_API int sr_dev_open(struct sr_dev_inst *sdi)
727 if (!sdi || !sdi->driver || !sdi->driver->dev_open)
730 if (sdi->status == SR_ST_ACTIVE) {
731 sr_err("%s: Device instance already active, can't re-open.",
736 sr_dbg("%s: Opening device instance.", sdi->driver->name);
738 ret = sdi->driver->dev_open(sdi);
741 sdi->status = SR_ST_ACTIVE;
747 * Close the specified device instance.
749 * If the device instance is not open (sdi->status != SR_ST_ACTIVE),
750 * SR_ERR_DEV_CLOSED will be returned and no closing will be attempted.
752 * Note: sdi->status will be set to SR_ST_INACTIVE, regardless of whether
753 * there are any errors during closing of the device instance (any errors
754 * will be reported via error code and log message, though).
756 * @param sdi Device instance to use. Must not be NULL.
758 * @retval SR_OK Success.
759 * @retval SR_ERR_ARG Invalid arguments.
760 * @retval SR_ERR_DEV_CLOSED Device instance was not active.
761 * @retval SR_ERR Other error.
765 SR_API int sr_dev_close(struct sr_dev_inst *sdi)
767 if (!sdi || !sdi->driver || !sdi->driver->dev_close)
770 if (sdi->status != SR_ST_ACTIVE) {
771 sr_err("%s: Device instance not active, can't close.",
773 return SR_ERR_DEV_CLOSED;
776 sdi->status = SR_ST_INACTIVE;
778 sr_dbg("%s: Closing device instance.", sdi->driver->name);
780 return sdi->driver->dev_close(sdi);
784 * Queries a device instances' driver.
786 * @param sdi Device instance to use. Must not be NULL.
788 * @return The driver instance or NULL on error.
790 SR_API struct sr_dev_driver *sr_dev_inst_driver_get(const struct sr_dev_inst *sdi)
792 if (!sdi || !sdi->driver)
799 * Queries a device instances' vendor.
801 * @param sdi Device instance to use. Must not be NULL.
803 * @return The vendor string or NULL.
805 SR_API const char *sr_dev_inst_vendor_get(const struct sr_dev_inst *sdi)
814 * Queries a device instances' model.
816 * @param sdi Device instance to use. Must not be NULL.
818 * @return The model string or NULL.
820 SR_API const char *sr_dev_inst_model_get(const struct sr_dev_inst *sdi)
829 * Queries a device instances' version.
831 * @param sdi Device instance to use. Must not be NULL.
833 * @return The version string or NULL.
835 SR_API const char *sr_dev_inst_version_get(const struct sr_dev_inst *sdi)
844 * Queries a device instances' serial number.
846 * @param sdi Device instance to use. Must not be NULL.
848 * @return The serial number string or NULL.
850 SR_API const char *sr_dev_inst_sernum_get(const struct sr_dev_inst *sdi)
855 return sdi->serial_num;
859 * Queries a device instances' connection identifier.
861 * @param sdi Device instance to use. Must not be NULL.
863 * @return A copy of the connection ID string or NULL. The caller is responsible
864 * for g_free()ing the string when it is no longer needed.
866 SR_API const char *sr_dev_inst_connid_get(const struct sr_dev_inst *sdi)
868 #ifdef HAVE_LIBUSB_1_0
869 struct drv_context *drvc;
871 char conn_id_usb[64];
872 struct sr_usb_dev_inst *usb;
873 struct libusb_device **devlist;
876 #ifdef HAVE_SERIAL_COMM
877 struct sr_serial_dev_inst *serial;
880 struct sr_scpi_dev_inst *scpi;
886 #ifdef HAVE_SERIAL_COMM
887 if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_SERIAL)) {
888 /* connection_id isn't populated, let's do that for serial devices. */
891 ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(serial->port);
895 #ifdef HAVE_LIBUSB_1_0
896 if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_USB)) {
897 /* connection_id isn't populated, let's do that for USB devices. */
899 drvc = sdi->driver->context;
902 if ((cnt = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist)) < 0) {
903 sr_err("Failed to retrieve device list: %s.",
904 libusb_error_name(cnt));
908 for (i = 0; i < cnt; i++) {
909 /* Find the USB device by the logical address we know. */
910 b = libusb_get_bus_number(devlist[i]);
911 a = libusb_get_device_address(devlist[i]);
912 if (b != usb->bus || a != usb->address)
915 if (usb_get_port_path(devlist[i], conn_id_usb, sizeof(conn_id_usb)) < 0)
918 ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(conn_id_usb);
922 libusb_free_device_list(devlist, 1);
926 if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_SCPI)) {
927 /* connection_id isn't populated, let's do that for SCPI devices. */
930 sr_scpi_connection_id(scpi, &conn_id_scpi);
931 ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(conn_id_scpi);
932 g_free(conn_id_scpi);
935 return sdi->connection_id;
939 * Queries a device instances' channel list.
941 * @param sdi Device instance to use. Must not be NULL.
943 * @return The GSList of channels or NULL.
945 SR_API GSList *sr_dev_inst_channels_get(const struct sr_dev_inst *sdi)
950 return sdi->channels;
954 * Queries a device instances' channel groups list.
956 * @param sdi Device instance to use. Must not be NULL.
958 * @return The GSList of channel groups or NULL.
960 SR_API GSList *sr_dev_inst_channel_groups_get(const struct sr_dev_inst *sdi)
965 return sdi->channel_groups;