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"
28 #define LOG_PREFIX "device"
34 * Device handling in libsigrok.
38 * @defgroup grp_devices Devices
40 * Device handling in libsigrok.
46 * Allocate and initialize a new struct sr_channel and add it to sdi.
48 * @param[in] sdi The device instance the channel is connected to.
50 * @param[in] index @copydoc sr_channel::index
51 * @param[in] type @copydoc sr_channel::type
52 * @param[in] enabled @copydoc sr_channel::enabled
53 * @param[in] name @copydoc sr_channel::name
55 * @return A new struct sr_channel*.
59 SR_PRIV struct sr_channel *sr_channel_new(struct sr_dev_inst *sdi,
60 int index, int type, gboolean enabled, const char *name)
62 struct sr_channel *ch;
64 ch = g_malloc0(sizeof(struct sr_channel));
68 ch->enabled = enabled;
70 ch->name = g_strdup(name);
72 sdi->channels = g_slist_append(sdi->channels, ch);
78 * Release a previously allocated struct sr_channel.
80 * @param[in] ch Pointer to struct sr_channel.
84 SR_PRIV void sr_channel_free(struct sr_channel *ch)
94 * Wrapper around @ref sr_channel_free(), suitable for glib iterators.
96 SR_PRIV void sr_channel_free_cb(void *p)
102 * Set the name of the specified channel.
104 * If the channel already has a different name assigned to it, it will be
105 * removed, and the new name will be saved instead.
107 * @param[in] channel The channel whose name to set. Must not be NULL.
108 * @param[in] name The new name that the specified channel should get.
109 * A copy of the string is made.
111 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
115 SR_API int sr_dev_channel_name_set(struct sr_channel *channel,
121 g_free(channel->name);
122 channel->name = g_strdup(name);
128 * Enable or disable a channel.
130 * @param[in] channel The channel to enable or disable. Must not be NULL.
131 * @param[in] state TRUE to enable the channel, FALSE to disable.
133 * @return SR_OK on success or SR_ERR on failure. In case of invalid
134 * arguments, SR_ERR_ARG is returned and the channel enabled state
139 SR_API int sr_dev_channel_enable(struct sr_channel *channel, gboolean state)
142 gboolean was_enabled;
143 struct sr_dev_inst *sdi;
149 was_enabled = channel->enabled;
150 channel->enabled = state;
151 if (!state != !was_enabled && sdi->driver
152 && sdi->driver->config_channel_set) {
153 ret = sdi->driver->config_channel_set(
154 sdi, channel, SR_CHANNEL_SET_ENABLED);
155 /* Roll back change if it wasn't applicable. */
164 * Returns the next enabled channel, wrapping around if necessary.
166 * @param[in] sdi The device instance the channel is connected to.
168 * @param[in] cur_channel The current channel.
170 * @return A pointer to the next enabled channel of this device.
174 SR_PRIV struct sr_channel *sr_next_enabled_channel(const struct sr_dev_inst *sdi,
175 struct sr_channel *cur_channel)
177 struct sr_channel *next_channel;
180 next_channel = cur_channel;
182 l = g_slist_find(sdi->channels, next_channel);
184 next_channel = l->next->data;
186 next_channel = sdi->channels->data;
187 } while (!next_channel->enabled);
193 * Compare two channels, return whether they differ.
195 * The channels' names and types are checked. The enabled state is not
196 * considered a condition for difference. The test is motivated by the
197 * desire to detect changes in the configuration of acquisition setups
198 * between re-reads of an input file.
200 * @param[in] ch1 First channel.
201 * @param[in] ch2 Second channel.
203 * @return #TRUE upon differences or unexpected input, #FALSE otherwise.
207 SR_PRIV gboolean sr_channels_differ(struct sr_channel *ch1, struct sr_channel *ch2)
212 if (ch1->type != ch2->type)
214 if (strcmp(ch1->name, ch2->name))
221 * Compare two channel lists, return whether they differ.
223 * Listing the same set of channels but in a different order is considered
224 * a difference in the lists.
226 * @param[in] l1 First channel list.
227 * @param[in] l2 Second channel list.
229 * @return #TRUE upon differences or unexpected input, #FALSE otherwise.
233 SR_PRIV gboolean sr_channel_lists_differ(GSList *l1, GSList *l2)
235 struct sr_channel *ch1, *ch2;
244 if (sr_channels_differ(ch1, ch2))
246 if (ch1->index != ch2->index)
256 * Determine whether the specified device instance has the specified
259 * @param sdi Pointer to the device instance to be checked. Must not be NULL.
260 * If the device's 'driver' field is NULL (virtual device), this
261 * function will always return FALSE (virtual devices don't have
262 * a hardware capabilities list).
263 * @param[in] key The option that should be checked for is supported by the
266 * @retval TRUE Device has the specified option.
267 * @retval FALSE Device does not have the specified option, invalid input
268 * parameters or other error conditions.
272 SR_API gboolean sr_dev_has_option(const struct sr_dev_inst *sdi, int key)
279 if (!sdi || !sdi->driver || !sdi->driver->config_list)
282 if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS,
283 &gvar, sdi, NULL) != SR_OK)
287 devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t));
288 for (i = 0; i < num_opts; i++) {
289 if ((devopts[i] & SR_CONF_MASK) == key) {
294 g_variant_unref(gvar);
300 * Enumerate the configuration options of the specified item.
302 * @param driver Pointer to the driver to be checked. Must not be NULL.
303 * @param sdi Pointer to the device instance to be checked. May be NULL to
304 * check driver options.
305 * @param cg Pointer to a channel group, if a specific channel group is to
306 * be checked. Must be NULL to check device-wide options.
308 * @return A GArray * of enum sr_configkey values, or NULL on invalid
309 * arguments. The array must be freed by the caller using
314 SR_API GArray *sr_dev_options(const struct sr_dev_driver *driver,
315 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
318 const uint32_t *opts;
323 if (!driver || !driver->config_list)
326 if (sdi && sdi->driver != driver)
329 if (driver->config_list(SR_CONF_DEVICE_OPTIONS, &gvar, sdi, cg) != SR_OK)
332 opts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(uint32_t));
334 result = g_array_sized_new(FALSE, FALSE, sizeof(uint32_t), num_opts);
336 for (i = 0; i < num_opts; i++) {
337 opt = opts[i] & SR_CONF_MASK;
338 g_array_insert_val(result, i, opt);
341 g_variant_unref(gvar);
347 * Enumerate the configuration capabilities supported by a device instance
348 * for a given configuration key.
350 * @param sdi Pointer to the device instance to be checked. Must not be NULL.
351 * If the device's 'driver' field is NULL (virtual device), this
352 * function will always return FALSE (virtual devices don't have
353 * a hardware capabilities list).
354 * @param cg Pointer to a channel group, if a specific channel group is to
355 * be checked. Must be NULL to check device-wide options.
356 * @param[in] key The option that should be checked for is supported by the
359 * @retval A bitmask of enum sr_configcap values, which will be zero for
360 * invalid inputs or if the key is unsupported.
364 SR_API int sr_dev_config_capabilities_list(const struct sr_dev_inst *sdi,
365 const struct sr_channel_group *cg, const int key)
372 if (!sdi || !sdi->driver || !sdi->driver->config_list)
375 if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS,
376 &gvar, sdi, cg) != SR_OK)
380 devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t));
381 for (i = 0; i < num_opts; i++) {
382 if ((devopts[i] & SR_CONF_MASK) == key) {
383 ret = devopts[i] & ~SR_CONF_MASK;
387 g_variant_unref(gvar);
393 * Allocate and init a new user-generated device instance.
395 * @param vendor Device vendor.
396 * @param model Device model.
397 * @param version Device version.
399 * @retval struct sr_dev_inst *. Dynamically allocated, free using
400 * sr_dev_inst_free().
402 SR_API struct sr_dev_inst *sr_dev_inst_user_new(const char *vendor,
403 const char *model, const char *version)
405 struct sr_dev_inst *sdi;
407 sdi = g_malloc0(sizeof(struct sr_dev_inst));
409 sdi->vendor = g_strdup(vendor);
410 sdi->model = g_strdup(model);
411 sdi->version = g_strdup(version);
412 sdi->inst_type = SR_INST_USER;
418 * Add a new channel to the specified device instance.
420 * @param[in] index @copydoc sr_channel::index
421 * @param[in] type @copydoc sr_channel::type
422 * @param[in] name @copydoc sr_channel::name
424 * @return SR_OK Success.
425 * @return SR_OK Invalid argument.
427 SR_API int sr_dev_inst_channel_add(struct sr_dev_inst *sdi, int index, int type, const char *name)
429 if (!sdi || sdi->inst_type != SR_INST_USER || index < 0)
432 sr_channel_new(sdi, index, type, TRUE, name);
438 * Free device instance struct created by sr_dev_inst().
440 * @param sdi Device instance to free. If NULL, the function will do nothing.
444 SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
446 struct sr_channel *ch;
447 struct sr_channel_group *cg;
453 for (l = sdi->channels; l; l = l->next) {
457 g_slist_free(sdi->channels);
459 for (l = sdi->channel_groups; l; l = l->next) {
462 g_slist_free(cg->channels);
466 g_slist_free(sdi->channel_groups);
469 sr_session_dev_remove(sdi->session, sdi);
473 g_free(sdi->version);
474 g_free(sdi->serial_num);
475 g_free(sdi->connection_id);
479 #ifdef HAVE_LIBUSB_1_0
482 * Allocate and init a struct for a USB device instance.
484 * @param[in] bus @copydoc sr_usb_dev_inst::bus
485 * @param[in] address @copydoc sr_usb_dev_inst::address
486 * @param[in] hdl @copydoc sr_usb_dev_inst::devhdl
488 * @return The struct sr_usb_dev_inst * for USB device instance.
492 SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
493 uint8_t address, struct libusb_device_handle *hdl)
495 struct sr_usb_dev_inst *udi;
497 udi = g_malloc0(sizeof(struct sr_usb_dev_inst));
499 udi->address = address;
506 * Free struct sr_usb_dev_inst * allocated by sr_usb_dev_inst().
508 * @param usb The struct sr_usb_dev_inst * to free. If NULL, this
509 * function does nothing.
513 SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
520 #ifdef HAVE_LIBSERIALPORT
523 * Allocate and init a struct for a serial device instance.
525 * Both parameters are copied to newly allocated strings, and freed
526 * automatically by sr_serial_dev_inst_free().
528 * @param[in] port OS-specific serial port specification. Examples:
529 * "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1".
531 * @param[in] serialcomm A serial communication parameters string, in the form
532 * of \<speed\>/\<data bits\>\<parity\>\<stopbits\>, for example
533 * "9600/8n1" or "600/7o2". This is an optional parameter;
534 * it may be filled in later. Can be NULL.
536 * @return A pointer to a newly initialized struct sr_serial_dev_inst,
541 SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
542 const char *serialcomm)
544 struct sr_serial_dev_inst *serial;
546 serial = g_malloc0(sizeof(struct sr_serial_dev_inst));
547 serial->port = g_strdup(port);
549 serial->serialcomm = g_strdup(serialcomm);
555 * Free struct sr_serial_dev_inst * allocated by sr_serial_dev_inst().
557 * @param serial The struct sr_serial_dev_inst * to free. If NULL, this
558 * function will do nothing.
562 SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
567 g_free(serial->port);
568 g_free(serial->serialcomm);
574 SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device)
576 struct sr_usbtmc_dev_inst *usbtmc;
578 usbtmc = g_malloc0(sizeof(struct sr_usbtmc_dev_inst));
579 usbtmc->device = g_strdup(device);
586 SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc)
591 g_free(usbtmc->device);
596 * Get the list of devices/instances of the specified driver.
598 * @param driver The driver to use. Must not be NULL.
600 * @return The list of devices/instances of this driver, or NULL upon errors
601 * or if the list is empty.
605 SR_API GSList *sr_dev_list(const struct sr_dev_driver *driver)
607 if (driver && driver->dev_list)
608 return driver->dev_list(driver);
614 * Clear the list of device instances a driver knows about.
616 * @param driver The driver to use. This must be a pointer to one of
617 * the entries returned by sr_driver_list(). Must not be NULL.
619 * @retval SR_OK Success.
620 * @retval SR_ERR_ARG Invalid driver.
624 SR_API int sr_dev_clear(const struct sr_dev_driver *driver)
627 sr_err("Invalid driver.");
631 if (!driver->context) {
633 * Driver was never initialized, nothing to do.
635 * No log message since this usually gets called for all
636 * drivers, whether they were initialized or not.
641 /* No log message here, too verbose and not very useful. */
643 return driver->dev_clear(driver);
647 * Open the specified device instance.
649 * If the device instance is already open (sdi->status == SR_ST_ACTIVE),
650 * SR_ERR will be returned and no re-opening of the device will be attempted.
652 * If opening was successful, sdi->status is set to SR_ST_ACTIVE, otherwise
653 * it will be left unchanged.
655 * @param sdi Device instance to use. Must not be NULL.
657 * @retval SR_OK Success.
658 * @retval SR_ERR_ARG Invalid arguments.
659 * @retval SR_ERR Device instance was already active, or other error.
663 SR_API int sr_dev_open(struct sr_dev_inst *sdi)
667 if (!sdi || !sdi->driver || !sdi->driver->dev_open)
670 if (sdi->status == SR_ST_ACTIVE) {
671 sr_err("%s: Device instance already active, can't re-open.",
676 sr_dbg("%s: Opening device instance.", sdi->driver->name);
678 ret = sdi->driver->dev_open(sdi);
681 sdi->status = SR_ST_ACTIVE;
687 * Close the specified device instance.
689 * If the device instance is not open (sdi->status != SR_ST_ACTIVE),
690 * SR_ERR_DEV_CLOSED will be returned and no closing will be attempted.
692 * Note: sdi->status will be set to SR_ST_INACTIVE, regardless of whether
693 * there are any errors during closing of the device instance (any errors
694 * will be reported via error code and log message, though).
696 * @param sdi Device instance to use. Must not be NULL.
698 * @retval SR_OK Success.
699 * @retval SR_ERR_ARG Invalid arguments.
700 * @retval SR_ERR_DEV_CLOSED Device instance was not active.
701 * @retval SR_ERR Other error.
705 SR_API int sr_dev_close(struct sr_dev_inst *sdi)
707 if (!sdi || !sdi->driver || !sdi->driver->dev_close)
710 if (sdi->status != SR_ST_ACTIVE) {
711 sr_err("%s: Device instance not active, can't close.",
713 return SR_ERR_DEV_CLOSED;
716 sdi->status = SR_ST_INACTIVE;
718 sr_dbg("%s: Closing device instance.", sdi->driver->name);
720 return sdi->driver->dev_close(sdi);
724 * Queries a device instances' driver.
726 * @param sdi Device instance to use. Must not be NULL.
728 * @return The driver instance or NULL on error.
730 SR_API struct sr_dev_driver *sr_dev_inst_driver_get(const struct sr_dev_inst *sdi)
732 if (!sdi || !sdi->driver)
739 * Queries a device instances' vendor.
741 * @param sdi Device instance to use. Must not be NULL.
743 * @return The vendor string or NULL.
745 SR_API const char *sr_dev_inst_vendor_get(const struct sr_dev_inst *sdi)
754 * Queries a device instances' model.
756 * @param sdi Device instance to use. Must not be NULL.
758 * @return The model string or NULL.
760 SR_API const char *sr_dev_inst_model_get(const struct sr_dev_inst *sdi)
769 * Queries a device instances' version.
771 * @param sdi Device instance to use. Must not be NULL.
773 * @return The version string or NULL.
775 SR_API const char *sr_dev_inst_version_get(const struct sr_dev_inst *sdi)
784 * Queries a device instances' serial number.
786 * @param sdi Device instance to use. Must not be NULL.
788 * @return The serial number string or NULL.
790 SR_API const char *sr_dev_inst_sernum_get(const struct sr_dev_inst *sdi)
795 return sdi->serial_num;
799 * Queries a device instances' connection identifier.
801 * @param sdi Device instance to use. Must not be NULL.
803 * @return A copy of the connection ID string or NULL. The caller is responsible
804 * for g_free()ing the string when it is no longer needed.
806 SR_API const char *sr_dev_inst_connid_get(const struct sr_dev_inst *sdi)
808 #ifdef HAVE_LIBUSB_1_0
809 struct drv_context *drvc;
811 char connection_id[64];
812 struct sr_usb_dev_inst *usb;
813 struct libusb_device **devlist;
819 #ifdef HAVE_LIBSERIALPORT
820 struct sr_serial_dev_inst *serial;
822 if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_SERIAL)) {
823 /* connection_id isn't populated, let's do that here. */
826 ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(serial->port);
830 #ifdef HAVE_LIBUSB_1_0
831 if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_USB)) {
832 /* connection_id isn't populated, let's do that here. */
834 drvc = sdi->driver->context;
837 if ((cnt = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist)) < 0) {
838 sr_err("Failed to retrieve device list: %s.",
839 libusb_error_name(cnt));
843 for (i = 0; i < cnt; i++) {
844 /* Find the USB device by the logical address we know. */
845 b = libusb_get_bus_number(devlist[i]);
846 a = libusb_get_device_address(devlist[i]);
847 if (b != usb->bus || a != usb->address)
850 if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0)
853 ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(connection_id);
857 libusb_free_device_list(devlist, 1);
861 return sdi->connection_id;
865 * Queries a device instances' channel list.
867 * @param sdi Device instance to use. Must not be NULL.
869 * @return The GSList of channels or NULL.
871 SR_API GSList *sr_dev_inst_channels_get(const struct sr_dev_inst *sdi)
876 return sdi->channels;
880 * Queries a device instances' channel groups list.
882 * @param sdi Device instance to use. Must not be NULL.
884 * @return The GSList of channel groups or NULL.
886 SR_API GSList *sr_dev_inst_channel_groups_get(const struct sr_dev_inst *sdi)
891 return sdi->channel_groups;