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 * Set the name of the specified channel.
79 * If the channel already has a different name assigned to it, it will be
80 * removed, and the new name will be saved instead.
82 * @param[in] channel The channel whose name to set. Must not be NULL.
83 * @param[in] name The new name that the specified channel should get.
84 * A copy of the string is made.
86 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
90 SR_API int sr_dev_channel_name_set(struct sr_channel *channel,
96 g_free(channel->name);
97 channel->name = g_strdup(name);
103 * Enable or disable a channel.
105 * @param[in] channel The channel to enable or disable. Must not be NULL.
106 * @param[in] state TRUE to enable the channel, FALSE to disable.
108 * @return SR_OK on success or SR_ERR on failure. In case of invalid
109 * arguments, SR_ERR_ARG is returned and the channel enabled state
114 SR_API int sr_dev_channel_enable(struct sr_channel *channel, gboolean state)
117 gboolean was_enabled;
118 struct sr_dev_inst *sdi;
124 was_enabled = channel->enabled;
125 channel->enabled = state;
126 if (!state != !was_enabled && sdi->driver
127 && sdi->driver->config_channel_set) {
128 ret = sdi->driver->config_channel_set(
129 sdi, channel, SR_CHANNEL_SET_ENABLED);
130 /* Roll back change if it wasn't applicable. */
139 * Returns the next enabled channel, wrapping around if necessary.
141 * @param[in] sdi The device instance the channel is connected to.
143 * @param[in] cur_channel The current channel.
145 * @return A pointer to the next enabled channel of this device.
149 SR_PRIV struct sr_channel *sr_next_enabled_channel(const struct sr_dev_inst *sdi,
150 struct sr_channel *cur_channel)
152 struct sr_channel *next_channel;
155 next_channel = cur_channel;
157 l = g_slist_find(sdi->channels, next_channel);
159 next_channel = l->next->data;
161 next_channel = sdi->channels->data;
162 } while (!next_channel->enabled);
168 * Determine whether the specified device instance has the specified
171 * @param sdi Pointer to the device instance to be checked. Must not be NULL.
172 * If the device's 'driver' field is NULL (virtual device), this
173 * function will always return FALSE (virtual devices don't have
174 * a hardware capabilities list).
175 * @param[in] key The option that should be checked for is supported by the
178 * @retval TRUE Device has the specified option.
179 * @retval FALSE Device does not have the specified option, invalid input
180 * parameters or other error conditions.
184 SR_API gboolean sr_dev_has_option(const struct sr_dev_inst *sdi, int key)
191 if (!sdi || !sdi->driver || !sdi->driver->config_list)
194 if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS,
195 &gvar, sdi, NULL) != SR_OK)
199 devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t));
200 for (i = 0; i < num_opts; i++) {
201 if ((devopts[i] & SR_CONF_MASK) == key) {
206 g_variant_unref(gvar);
212 * Enumerate the configuration options of the specified item.
214 * @param driver Pointer to the driver to be checked. Must not be NULL.
215 * @param sdi Pointer to the device instance to be checked. May be NULL to
216 * check driver options.
217 * @param cg Pointer to a channel group, if a specific channel group is to
218 * be checked. Must be NULL to check device-wide options.
220 * @return A GArray * of enum sr_configkey values, or NULL on invalid
221 * arguments. The array must be freed by the caller using
226 SR_API GArray *sr_dev_options(const struct sr_dev_driver *driver,
227 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
230 const uint32_t *opts;
235 if (!driver || !driver->config_list)
238 if (sdi && sdi->driver != driver)
241 if (driver->config_list(SR_CONF_DEVICE_OPTIONS, &gvar, sdi, cg) != SR_OK)
244 opts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(uint32_t));
246 result = g_array_sized_new(FALSE, FALSE, sizeof(uint32_t), num_opts);
248 for (i = 0; i < num_opts; i++) {
249 opt = opts[i] & SR_CONF_MASK;
250 g_array_insert_val(result, i, opt);
253 g_variant_unref(gvar);
259 * Enumerate the configuration capabilities supported by a device instance
260 * for a given configuration key.
262 * @param sdi Pointer to the device instance to be checked. Must not be NULL.
263 * If the device's 'driver' field is NULL (virtual device), this
264 * function will always return FALSE (virtual devices don't have
265 * a hardware capabilities list).
266 * @param cg Pointer to a channel group, if a specific channel group is to
267 * be checked. Must be NULL to check device-wide options.
268 * @param[in] key The option that should be checked for is supported by the
271 * @retval A bitmask of enum sr_configcap values, which will be zero for
272 * invalid inputs or if the key is unsupported.
276 SR_API int sr_dev_config_capabilities_list(const struct sr_dev_inst *sdi,
277 const struct sr_channel_group *cg, const int key)
284 if (!sdi || !sdi->driver || !sdi->driver->config_list)
287 if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS,
288 &gvar, sdi, cg) != SR_OK)
292 devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t));
293 for (i = 0; i < num_opts; i++) {
294 if ((devopts[i] & SR_CONF_MASK) == key) {
295 ret = devopts[i] & ~SR_CONF_MASK;
299 g_variant_unref(gvar);
305 * Allocate and init a new user-generated device instance.
307 * @param vendor Device vendor.
308 * @param model Device model.
309 * @param version Device version.
311 * @retval struct sr_dev_inst *. Dynamically allocated, free using
312 * sr_dev_inst_free().
314 SR_API struct sr_dev_inst *sr_dev_inst_user_new(const char *vendor,
315 const char *model, const char *version)
317 struct sr_dev_inst *sdi;
319 sdi = g_malloc0(sizeof(struct sr_dev_inst));
321 sdi->vendor = g_strdup(vendor);
322 sdi->model = g_strdup(model);
323 sdi->version = g_strdup(version);
324 sdi->inst_type = SR_INST_USER;
330 * Add a new channel to the specified device instance.
332 * @param[in] index @copydoc sr_channel::index
333 * @param[in] type @copydoc sr_channel::type
334 * @param[in] name @copydoc sr_channel::name
336 * @return SR_OK Success.
337 * @return SR_OK Invalid argument.
339 SR_API int sr_dev_inst_channel_add(struct sr_dev_inst *sdi, int index, int type, const char *name)
341 if (!sdi || sdi->inst_type != SR_INST_USER || index < 0)
344 sr_channel_new(sdi, index, type, TRUE, name);
350 * Free device instance struct created by sr_dev_inst().
352 * @param sdi Device instance to free. If NULL, the function will do nothing.
356 SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
358 struct sr_channel *ch;
359 struct sr_channel_group *cg;
365 for (l = sdi->channels; l; l = l->next) {
371 g_slist_free(sdi->channels);
373 for (l = sdi->channel_groups; l; l = l->next) {
376 g_slist_free(cg->channels);
380 g_slist_free(sdi->channel_groups);
383 sr_session_dev_remove(sdi->session, sdi);
387 g_free(sdi->version);
388 g_free(sdi->serial_num);
389 g_free(sdi->connection_id);
393 #ifdef HAVE_LIBUSB_1_0
396 * Allocate and init a struct for a USB device instance.
398 * @param[in] bus @copydoc sr_usb_dev_inst::bus
399 * @param[in] address @copydoc sr_usb_dev_inst::address
400 * @param[in] hdl @copydoc sr_usb_dev_inst::devhdl
402 * @return The struct sr_usb_dev_inst * for USB device instance.
406 SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
407 uint8_t address, struct libusb_device_handle *hdl)
409 struct sr_usb_dev_inst *udi;
411 udi = g_malloc0(sizeof(struct sr_usb_dev_inst));
413 udi->address = address;
420 * Free struct sr_usb_dev_inst * allocated by sr_usb_dev_inst().
422 * @param usb The struct sr_usb_dev_inst * to free. If NULL, this
423 * function does nothing.
427 SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
434 #ifdef HAVE_LIBSERIALPORT
437 * Allocate and init a struct for a serial device instance.
439 * Both parameters are copied to newly allocated strings, and freed
440 * automatically by sr_serial_dev_inst_free().
442 * @param[in] port OS-specific serial port specification. Examples:
443 * "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1".
445 * @param[in] serialcomm A serial communication parameters string, in the form
446 * of \<speed\>/\<data bits\>\<parity\>\<stopbits\>, for example
447 * "9600/8n1" or "600/7o2". This is an optional parameter;
448 * it may be filled in later. Can be NULL.
450 * @return A pointer to a newly initialized struct sr_serial_dev_inst,
455 SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
456 const char *serialcomm)
458 struct sr_serial_dev_inst *serial;
460 serial = g_malloc0(sizeof(struct sr_serial_dev_inst));
461 serial->port = g_strdup(port);
463 serial->serialcomm = g_strdup(serialcomm);
469 * Free struct sr_serial_dev_inst * allocated by sr_serial_dev_inst().
471 * @param serial The struct sr_serial_dev_inst * to free. If NULL, this
472 * function will do nothing.
476 SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
481 g_free(serial->port);
482 g_free(serial->serialcomm);
488 SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device)
490 struct sr_usbtmc_dev_inst *usbtmc;
492 usbtmc = g_malloc0(sizeof(struct sr_usbtmc_dev_inst));
493 usbtmc->device = g_strdup(device);
500 SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc)
505 g_free(usbtmc->device);
510 * Get the list of devices/instances of the specified driver.
512 * @param driver The driver to use. Must not be NULL.
514 * @return The list of devices/instances of this driver, or NULL upon errors
515 * or if the list is empty.
519 SR_API GSList *sr_dev_list(const struct sr_dev_driver *driver)
521 if (driver && driver->dev_list)
522 return driver->dev_list(driver);
528 * Clear the list of device instances a driver knows about.
530 * @param driver The driver to use. This must be a pointer to one of
531 * the entries returned by sr_driver_list(). Must not be NULL.
533 * @retval SR_OK Success.
534 * @retval SR_ERR_ARG Invalid driver.
538 SR_API int sr_dev_clear(const struct sr_dev_driver *driver)
541 sr_err("Invalid driver.");
545 if (!driver->context) {
547 * Driver was never initialized, nothing to do.
549 * No log message since this usually gets called for all
550 * drivers, whether they were initialized or not.
555 /* No log message here, too verbose and not very useful. */
557 return driver->dev_clear(driver);
561 * Open the specified device instance.
563 * If the device instance is already open (sdi->status == SR_ST_ACTIVE),
564 * SR_ERR will be returned and no re-opening of the device will be attempted.
566 * If opening was successful, sdi->status is set to SR_ST_ACTIVE, otherwise
567 * it will be left unchanged.
569 * @param sdi Device instance to use. Must not be NULL.
571 * @retval SR_OK Success.
572 * @retval SR_ERR_ARG Invalid arguments.
573 * @retval SR_ERR Device instance was already active, or other error.
577 SR_API int sr_dev_open(struct sr_dev_inst *sdi)
581 if (!sdi || !sdi->driver || !sdi->driver->dev_open)
584 if (sdi->status == SR_ST_ACTIVE) {
585 sr_err("%s: Device instance already active, can't re-open.",
590 sr_dbg("%s: Opening device instance.", sdi->driver->name);
592 ret = sdi->driver->dev_open(sdi);
595 sdi->status = SR_ST_ACTIVE;
601 * Close the specified device instance.
603 * If the device instance is not open (sdi->status != SR_ST_ACTIVE),
604 * SR_ERR_DEV_CLOSED will be returned and no closing will be attempted.
606 * Note: sdi->status will be set to SR_ST_INACTIVE, regardless of whether
607 * there are any errors during closing of the device instance (any errors
608 * will be reported via error code and log message, though).
610 * @param sdi Device instance to use. Must not be NULL.
612 * @retval SR_OK Success.
613 * @retval SR_ERR_ARG Invalid arguments.
614 * @retval SR_ERR_DEV_CLOSED Device instance was not active.
615 * @retval SR_ERR Other error.
619 SR_API int sr_dev_close(struct sr_dev_inst *sdi)
621 if (!sdi || !sdi->driver || !sdi->driver->dev_close)
624 if (sdi->status != SR_ST_ACTIVE) {
625 sr_err("%s: Device instance not active, can't close.",
627 return SR_ERR_DEV_CLOSED;
630 sdi->status = SR_ST_INACTIVE;
632 sr_dbg("%s: Closing device instance.", sdi->driver->name);
634 return sdi->driver->dev_close(sdi);
638 * Queries a device instances' driver.
640 * @param sdi Device instance to use. Must not be NULL.
642 * @return The driver instance or NULL on error.
644 SR_API struct sr_dev_driver *sr_dev_inst_driver_get(const struct sr_dev_inst *sdi)
646 if (!sdi || !sdi->driver)
653 * Queries a device instances' vendor.
655 * @param sdi Device instance to use. Must not be NULL.
657 * @return The vendor string or NULL.
659 SR_API const char *sr_dev_inst_vendor_get(const struct sr_dev_inst *sdi)
668 * Queries a device instances' model.
670 * @param sdi Device instance to use. Must not be NULL.
672 * @return The model string or NULL.
674 SR_API const char *sr_dev_inst_model_get(const struct sr_dev_inst *sdi)
683 * Queries a device instances' version.
685 * @param sdi Device instance to use. Must not be NULL.
687 * @return The version string or NULL.
689 SR_API const char *sr_dev_inst_version_get(const struct sr_dev_inst *sdi)
698 * Queries a device instances' serial number.
700 * @param sdi Device instance to use. Must not be NULL.
702 * @return The serial number string or NULL.
704 SR_API const char *sr_dev_inst_sernum_get(const struct sr_dev_inst *sdi)
709 return sdi->serial_num;
713 * Queries a device instances' connection identifier.
715 * @param sdi Device instance to use. Must not be NULL.
717 * @return A copy of the connection ID string or NULL. The caller is responsible
718 * for g_free()ing the string when it is no longer needed.
720 SR_API const char *sr_dev_inst_connid_get(const struct sr_dev_inst *sdi)
722 #ifdef HAVE_LIBUSB_1_0
723 struct drv_context *drvc;
725 char connection_id[64];
726 struct sr_usb_dev_inst *usb;
727 struct libusb_device **devlist;
733 #ifdef HAVE_LIBSERIALPORT
734 struct sr_serial_dev_inst *serial;
736 if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_SERIAL)) {
737 /* connection_id isn't populated, let's do that here. */
740 ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(serial->port);
744 #ifdef HAVE_LIBUSB_1_0
745 if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_USB)) {
746 /* connection_id isn't populated, let's do that here. */
748 drvc = sdi->driver->context;
751 if ((cnt = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist)) < 0) {
752 sr_err("Failed to retrieve device list: %s.",
753 libusb_error_name(cnt));
757 for (i = 0; i < cnt; i++) {
758 /* Find the USB device by the logical address we know. */
759 b = libusb_get_bus_number(devlist[i]);
760 a = libusb_get_device_address(devlist[i]);
761 if (b != usb->bus || a != usb->address)
764 if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0)
767 ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(connection_id);
771 libusb_free_device_list(devlist, 1);
775 return sdi->connection_id;
779 * Queries a device instances' channel list.
781 * @param sdi Device instance to use. Must not be NULL.
783 * @return The GSList of channels or NULL.
785 SR_API GSList *sr_dev_inst_channels_get(const struct sr_dev_inst *sdi)
790 return sdi->channels;
794 * Queries a device instances' channel groups list.
796 * @param sdi Device instance to use. Must not be NULL.
798 * @return The GSList of channel groups or NULL.
800 SR_API GSList *sr_dev_inst_channel_groups_get(const struct sr_dev_inst *sdi)
805 return sdi->channel_groups;