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. */
138 /* Returns the next enabled channel, wrapping around if necessary. */
139 SR_PRIV struct sr_channel *sr_next_enabled_channel(const struct sr_dev_inst *sdi,
140 struct sr_channel *cur_channel)
142 struct sr_channel *next_channel;
145 next_channel = cur_channel;
147 l = g_slist_find(sdi->channels, next_channel);
149 next_channel = l->next->data;
151 next_channel = sdi->channels->data;
152 } while (!next_channel->enabled);
158 * Determine whether the specified device instance has the specified
161 * @param sdi Pointer to the device instance to be checked. Must not be NULL.
162 * If the device's 'driver' field is NULL (virtual device), this
163 * function will always return FALSE (virtual devices don't have
164 * a hardware capabilities list).
165 * @param[in] key The option that should be checked for is supported by the
168 * @retval TRUE Device has the specified option.
169 * @retval FALSE Device does not have the specified option, invalid input
170 * parameters or other error conditions.
174 SR_API gboolean sr_dev_has_option(const struct sr_dev_inst *sdi, int key)
181 if (!sdi || !sdi->driver || !sdi->driver->config_list)
184 if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS,
185 &gvar, sdi, NULL) != SR_OK)
189 devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t));
190 for (i = 0; i < num_opts; i++) {
191 if ((devopts[i] & SR_CONF_MASK) == key) {
196 g_variant_unref(gvar);
202 * Enumerate the configuration options of the specified item.
204 * @param driver Pointer to the driver to be checked. Must not be NULL.
205 * @param sdi Pointer to the device instance to be checked. May be NULL to
206 * check driver options.
207 * @param cg Pointer to a channel group, if a specific channel group is to
208 * be checked. Must be NULL to check device-wide options.
210 * @return A GArray * of enum sr_configkey values, or NULL on invalid
211 * arguments. The array must be freed by the caller using
216 SR_API GArray *sr_dev_options(const struct sr_dev_driver *driver,
217 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
220 const uint32_t *opts;
225 if (!driver || !driver->config_list)
228 if (sdi && sdi->driver != driver)
231 if (driver->config_list(SR_CONF_DEVICE_OPTIONS, &gvar, sdi, cg) != SR_OK)
234 opts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(uint32_t));
236 result = g_array_sized_new(FALSE, FALSE, sizeof(uint32_t), num_opts);
238 for (i = 0; i < num_opts; i++) {
239 opt = opts[i] & SR_CONF_MASK;
240 g_array_insert_val(result, i, opt);
243 g_variant_unref(gvar);
249 * Enumerate the configuration capabilities supported by a device instance
250 * for a given configuration key.
252 * @param sdi Pointer to the device instance to be checked. Must not be NULL.
253 * If the device's 'driver' field is NULL (virtual device), this
254 * function will always return FALSE (virtual devices don't have
255 * a hardware capabilities list).
256 * @param cg Pointer to a channel group, if a specific channel group is to
257 * be checked. Must be NULL to check device-wide options.
258 * @param[in] key The option that should be checked for is supported by the
261 * @retval A bitmask of enum sr_configcap values, which will be zero for
262 * invalid inputs or if the key is unsupported.
266 SR_API int sr_dev_config_capabilities_list(const struct sr_dev_inst *sdi,
267 const struct sr_channel_group *cg, const int key)
274 if (!sdi || !sdi->driver || !sdi->driver->config_list)
277 if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS,
278 &gvar, sdi, cg) != SR_OK)
282 devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t));
283 for (i = 0; i < num_opts; i++) {
284 if ((devopts[i] & SR_CONF_MASK) == key) {
285 ret = devopts[i] & ~SR_CONF_MASK;
289 g_variant_unref(gvar);
295 * Allocate and init a new user-generated device instance.
297 * @param vendor Device vendor.
298 * @param model Device model.
299 * @param version Device version.
301 * @retval struct sr_dev_inst *. Dynamically allocated, free using
302 * sr_dev_inst_free().
304 SR_API struct sr_dev_inst *sr_dev_inst_user_new(const char *vendor,
305 const char *model, const char *version)
307 struct sr_dev_inst *sdi;
309 sdi = g_malloc0(sizeof(struct sr_dev_inst));
311 sdi->vendor = g_strdup(vendor);
312 sdi->model = g_strdup(model);
313 sdi->version = g_strdup(version);
314 sdi->inst_type = SR_INST_USER;
320 * Add a new channel to the specified device instance.
322 * @param[in] index @copydoc sr_channel::index
323 * @param[in] type @copydoc sr_channel::type
324 * @param[in] name @copydoc sr_channel::name
326 * @return SR_OK Success.
327 * @return SR_OK Invalid argument.
329 SR_API int sr_dev_inst_channel_add(struct sr_dev_inst *sdi, int index, int type, const char *name)
331 if (!sdi || sdi->inst_type != SR_INST_USER || index < 0)
334 sr_channel_new(sdi, index, type, TRUE, name);
340 * Free device instance struct created by sr_dev_inst().
342 * @param sdi Device instance to free. If NULL, the function will do nothing.
346 SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
348 struct sr_channel *ch;
349 struct sr_channel_group *cg;
355 for (l = sdi->channels; l; l = l->next) {
361 g_slist_free(sdi->channels);
363 for (l = sdi->channel_groups; l; l = l->next) {
366 g_slist_free(cg->channels);
370 g_slist_free(sdi->channel_groups);
373 sr_session_dev_remove(sdi->session, sdi);
377 g_free(sdi->version);
378 g_free(sdi->serial_num);
379 g_free(sdi->connection_id);
383 #ifdef HAVE_LIBUSB_1_0
386 * Allocate and init a struct for a USB device instance.
388 * @param[in] bus @copydoc sr_usb_dev_inst::bus
389 * @param[in] address @copydoc sr_usb_dev_inst::address
390 * @param[in] hdl @copydoc sr_usb_dev_inst::devhdl
392 * @return The struct sr_usb_dev_inst * for USB device instance.
396 SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
397 uint8_t address, struct libusb_device_handle *hdl)
399 struct sr_usb_dev_inst *udi;
401 udi = g_malloc0(sizeof(struct sr_usb_dev_inst));
403 udi->address = address;
410 * Free struct sr_usb_dev_inst * allocated by sr_usb_dev_inst().
412 * @param usb The struct sr_usb_dev_inst * to free. If NULL, this
413 * function does nothing.
417 SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
424 #ifdef HAVE_LIBSERIALPORT
427 * Allocate and init a struct for a serial device instance.
429 * Both parameters are copied to newly allocated strings, and freed
430 * automatically by sr_serial_dev_inst_free().
432 * @param[in] port OS-specific serial port specification. Examples:
433 * "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1".
435 * @param[in] serialcomm A serial communication parameters string, in the form
436 * of \<speed\>/\<data bits\>\<parity\>\<stopbits\>, for example
437 * "9600/8n1" or "600/7o2". This is an optional parameter;
438 * it may be filled in later. Can be NULL.
440 * @return A pointer to a newly initialized struct sr_serial_dev_inst,
445 SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
446 const char *serialcomm)
448 struct sr_serial_dev_inst *serial;
450 serial = g_malloc0(sizeof(struct sr_serial_dev_inst));
451 serial->port = g_strdup(port);
453 serial->serialcomm = g_strdup(serialcomm);
459 * Free struct sr_serial_dev_inst * allocated by sr_serial_dev_inst().
461 * @param serial The struct sr_serial_dev_inst * to free. If NULL, this
462 * function will do nothing.
466 SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
471 g_free(serial->port);
472 g_free(serial->serialcomm);
478 SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device)
480 struct sr_usbtmc_dev_inst *usbtmc;
482 usbtmc = g_malloc0(sizeof(struct sr_usbtmc_dev_inst));
483 usbtmc->device = g_strdup(device);
490 SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc)
495 g_free(usbtmc->device);
500 * Get the list of devices/instances of the specified driver.
502 * @param driver The driver to use. Must not be NULL.
504 * @return The list of devices/instances of this driver, or NULL upon errors
505 * or if the list is empty.
509 SR_API GSList *sr_dev_list(const struct sr_dev_driver *driver)
511 if (driver && driver->dev_list)
512 return driver->dev_list(driver);
518 * Clear the list of device instances a driver knows about.
520 * @param driver The driver to use. This must be a pointer to one of
521 * the entries returned by sr_driver_list(). Must not be NULL.
523 * @retval SR_OK Success.
524 * @retval SR_ERR_ARG Invalid driver.
528 SR_API int sr_dev_clear(const struct sr_dev_driver *driver)
533 sr_err("Invalid driver.");
537 if (driver->dev_clear)
538 ret = driver->dev_clear(driver);
540 ret = std_dev_clear(driver, NULL);
546 * Open the specified device.
548 * @param sdi Device instance to use. Must not be NULL.
550 * @return SR_OK upon success, a negative error code upon errors.
554 SR_API int sr_dev_open(struct sr_dev_inst *sdi)
558 if (!sdi || !sdi->driver || !sdi->driver->dev_open)
561 ret = sdi->driver->dev_open(sdi);
567 * Close the specified device.
569 * @param sdi Device instance to use. Must not be NULL.
571 * @return SR_OK upon success, a negative error code upon errors.
575 SR_API int sr_dev_close(struct sr_dev_inst *sdi)
579 if (!sdi || !sdi->driver || !sdi->driver->dev_close)
582 ret = sdi->driver->dev_close(sdi);
588 * Queries a device instances' driver.
590 * @param sdi Device instance to use. Must not be NULL.
592 * @return The driver instance or NULL on error.
594 SR_API struct sr_dev_driver *sr_dev_inst_driver_get(const struct sr_dev_inst *sdi)
596 if (!sdi || !sdi->driver)
603 * Queries a device instances' vendor.
605 * @param sdi Device instance to use. Must not be NULL.
607 * @return The vendor string or NULL.
609 SR_API const char *sr_dev_inst_vendor_get(const struct sr_dev_inst *sdi)
618 * Queries a device instances' model.
620 * @param sdi Device instance to use. Must not be NULL.
622 * @return The model string or NULL.
624 SR_API const char *sr_dev_inst_model_get(const struct sr_dev_inst *sdi)
633 * Queries a device instances' version.
635 * @param sdi Device instance to use. Must not be NULL.
637 * @return The version string or NULL.
639 SR_API const char *sr_dev_inst_version_get(const struct sr_dev_inst *sdi)
648 * Queries a device instances' serial number.
650 * @param sdi Device instance to use. Must not be NULL.
652 * @return The serial number string or NULL.
654 SR_API const char *sr_dev_inst_sernum_get(const struct sr_dev_inst *sdi)
659 return sdi->serial_num;
663 * Queries a device instances' connection identifier.
665 * @param sdi Device instance to use. Must not be NULL.
667 * @return A copy of the connection ID string or NULL. The caller is responsible
668 * for g_free()ing the string when it is no longer needed.
670 SR_API const char *sr_dev_inst_connid_get(const struct sr_dev_inst *sdi)
672 #ifdef HAVE_LIBUSB_1_0
673 struct drv_context *drvc;
675 char connection_id[64];
676 struct sr_usb_dev_inst *usb;
677 struct libusb_device **devlist;
683 #ifdef HAVE_LIBSERIALPORT
684 struct sr_serial_dev_inst *serial;
686 if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_SERIAL)) {
687 /* connection_id isn't populated, let's do that here. */
690 ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(serial->port);
694 #ifdef HAVE_LIBUSB_1_0
695 if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_USB)) {
696 /* connection_id isn't populated, let's do that here. */
698 drvc = sdi->driver->context;
701 if ((cnt = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist)) < 0) {
702 sr_err("Failed to retrieve device list: %s.",
703 libusb_error_name(cnt));
707 for (i = 0; i < cnt; i++) {
708 /* Find the USB device by the logical address we know. */
709 b = libusb_get_bus_number(devlist[i]);
710 a = libusb_get_device_address(devlist[i]);
711 if (b != usb->bus || a != usb->address)
714 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
715 ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(connection_id);
719 libusb_free_device_list(devlist, 1);
723 return sdi->connection_id;
727 * Queries a device instances' channel list.
729 * @param sdi Device instance to use. Must not be NULL.
731 * @return The GSList of channels or NULL.
733 SR_API GSList *sr_dev_inst_channels_get(const struct sr_dev_inst *sdi)
738 return sdi->channels;
742 * Queries a device instances' channel groups list.
744 * @param sdi Device instance to use. Must not be NULL.
746 * @return The GSList of channel groups or NULL.
748 SR_API GSList *sr_dev_inst_channel_groups_get(const struct sr_dev_inst *sdi)
753 return sdi->channel_groups;