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. */
140 SR_PRIV struct sr_channel *sr_next_enabled_channel(const struct sr_dev_inst *sdi,
142 struct sr_channel *cur_channel)
144 struct sr_channel *next_channel;
147 next_channel = cur_channel;
149 l = g_slist_find(sdi->channels, next_channel);
151 next_channel = l->next->data;
153 next_channel = sdi->channels->data;
154 } while (!next_channel->enabled);
160 * Determine whether the specified device instance has the specified
163 * @param sdi Pointer to the device instance to be checked. Must not be NULL.
164 * If the device's 'driver' field is NULL (virtual device), this
165 * function will always return FALSE (virtual devices don't have
166 * a hardware capabilities list).
167 * @param[in] key The option that should be checked for is supported by the
170 * @retval TRUE Device has the specified option.
171 * @retval FALSE Device does not have the specified option, invalid input
172 * parameters or other error conditions.
176 SR_API gboolean sr_dev_has_option(const struct sr_dev_inst *sdi, int key)
183 if (!sdi || !sdi->driver || !sdi->driver->config_list)
186 if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS,
187 &gvar, sdi, NULL) != SR_OK)
191 devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t));
192 for (i = 0; i < num_opts; i++) {
193 if ((devopts[i] & SR_CONF_MASK) == key) {
198 g_variant_unref(gvar);
204 * Enumerate the configuration options of the specified item.
206 * @param driver Pointer to the driver to be checked. Must not be NULL.
207 * @param sdi Pointer to the device instance to be checked. May be NULL to
208 * check driver options.
209 * @param cg Pointer to a channel group, if a specific channel group is to
210 * be checked. Must be NULL to check device-wide options.
212 * @return A GArray * of enum sr_configkey values, or NULL on invalid
213 * arguments. The array must be freed by the caller using
218 SR_API GArray *sr_dev_options(const struct sr_dev_driver *driver,
219 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
222 const uint32_t *opts;
227 if (!driver || !driver->config_list)
230 if (sdi && sdi->driver != driver)
233 if (driver->config_list(SR_CONF_DEVICE_OPTIONS, &gvar, sdi, cg) != SR_OK)
236 opts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(uint32_t));
238 result = g_array_sized_new(FALSE, FALSE, sizeof(uint32_t), num_opts);
240 for (i = 0; i < num_opts; i++) {
241 opt = opts[i] & SR_CONF_MASK;
242 g_array_insert_val(result, i, opt);
245 g_variant_unref(gvar);
251 * Enumerate the configuration capabilities supported by a device instance
252 * for a given configuration key.
254 * @param sdi Pointer to the device instance to be checked. Must not be NULL.
255 * If the device's 'driver' field is NULL (virtual device), this
256 * function will always return FALSE (virtual devices don't have
257 * a hardware capabilities list).
258 * @param cg Pointer to a channel group, if a specific channel group is to
259 * be checked. Must be NULL to check device-wide options.
260 * @param[in] key The option that should be checked for is supported by the
263 * @retval A bitmask of enum sr_configcap values, which will be zero for
264 * invalid inputs or if the key is unsupported.
268 SR_API int sr_dev_config_capabilities_list(const struct sr_dev_inst *sdi,
269 const struct sr_channel_group *cg, const int key)
276 if (!sdi || !sdi->driver || !sdi->driver->config_list)
279 if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS,
280 &gvar, sdi, cg) != SR_OK)
284 devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t));
285 for (i = 0; i < num_opts; i++) {
286 if ((devopts[i] & SR_CONF_MASK) == key) {
287 ret = devopts[i] & ~SR_CONF_MASK;
291 g_variant_unref(gvar);
297 * Allocate and init a new user-generated device instance.
299 * @param vendor Device vendor.
300 * @param model Device model.
301 * @param version Device version.
303 * @retval struct sr_dev_inst *. Dynamically allocated, free using
304 * sr_dev_inst_free().
306 SR_API struct sr_dev_inst *sr_dev_inst_user_new(const char *vendor,
307 const char *model, const char *version)
309 struct sr_dev_inst *sdi;
311 sdi = g_malloc0(sizeof(struct sr_dev_inst));
313 sdi->vendor = g_strdup(vendor);
314 sdi->model = g_strdup(model);
315 sdi->version = g_strdup(version);
316 sdi->inst_type = SR_INST_USER;
322 * Add a new channel to the specified device instance.
324 * @param[in] index @copydoc sr_channel::index
325 * @param[in] type @copydoc sr_channel::type
326 * @param[in] name @copydoc sr_channel::name
328 * @return SR_OK Success.
329 * @return SR_OK Invalid argument.
331 SR_API int sr_dev_inst_channel_add(struct sr_dev_inst *sdi, int index, int type, const char *name)
333 if (!sdi || sdi->inst_type != SR_INST_USER || index < 0)
336 sr_channel_new(sdi, index, type, TRUE, name);
342 * Free device instance struct created by sr_dev_inst().
344 * @param sdi Device instance to free. If NULL, the function will do nothing.
348 SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
350 struct sr_channel *ch;
351 struct sr_channel_group *cg;
357 for (l = sdi->channels; l; l = l->next) {
363 g_slist_free(sdi->channels);
365 for (l = sdi->channel_groups; l; l = l->next) {
368 g_slist_free(cg->channels);
372 g_slist_free(sdi->channel_groups);
375 sr_session_dev_remove(sdi->session, sdi);
379 g_free(sdi->version);
380 g_free(sdi->serial_num);
381 g_free(sdi->connection_id);
385 #ifdef HAVE_LIBUSB_1_0
388 * Allocate and init a struct for a USB device instance.
390 * @param[in] bus @copydoc sr_usb_dev_inst::bus
391 * @param[in] address @copydoc sr_usb_dev_inst::address
392 * @param[in] hdl @copydoc sr_usb_dev_inst::devhdl
394 * @return The struct sr_usb_dev_inst * for USB device instance.
398 SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
399 uint8_t address, struct libusb_device_handle *hdl)
401 struct sr_usb_dev_inst *udi;
403 udi = g_malloc0(sizeof(struct sr_usb_dev_inst));
405 udi->address = address;
412 * Free struct sr_usb_dev_inst * allocated by sr_usb_dev_inst().
414 * @param usb The struct sr_usb_dev_inst * to free. If NULL, this
415 * function does nothing.
419 SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
426 #ifdef HAVE_LIBSERIALPORT
429 * Allocate and init a struct for a serial device instance.
431 * Both parameters are copied to newly allocated strings, and freed
432 * automatically by sr_serial_dev_inst_free().
434 * @param[in] port OS-specific serial port specification. Examples:
435 * "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1".
437 * @param[in] serialcomm A serial communication parameters string, in the form
438 * of \<speed\>/\<data bits\>\<parity\>\<stopbits\>, for example
439 * "9600/8n1" or "600/7o2". This is an optional parameter;
440 * it may be filled in later. Can be NULL.
442 * @return A pointer to a newly initialized struct sr_serial_dev_inst,
447 SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
448 const char *serialcomm)
450 struct sr_serial_dev_inst *serial;
452 serial = g_malloc0(sizeof(struct sr_serial_dev_inst));
453 serial->port = g_strdup(port);
455 serial->serialcomm = g_strdup(serialcomm);
461 * Free struct sr_serial_dev_inst * allocated by sr_serial_dev_inst().
463 * @param serial The struct sr_serial_dev_inst * to free. If NULL, this
464 * function will do nothing.
468 SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
473 g_free(serial->port);
474 g_free(serial->serialcomm);
480 SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device)
482 struct sr_usbtmc_dev_inst *usbtmc;
484 usbtmc = g_malloc0(sizeof(struct sr_usbtmc_dev_inst));
485 usbtmc->device = g_strdup(device);
492 SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc)
497 g_free(usbtmc->device);
502 * Get the list of devices/instances of the specified driver.
504 * @param driver The driver to use. Must not be NULL.
506 * @return The list of devices/instances of this driver, or NULL upon errors
507 * or if the list is empty.
511 SR_API GSList *sr_dev_list(const struct sr_dev_driver *driver)
513 if (driver && driver->dev_list)
514 return driver->dev_list(driver);
520 * Clear the list of device instances a driver knows about.
522 * @param driver The driver to use. This must be a pointer to one of
523 * the entries returned by sr_driver_list(). Must not be NULL.
525 * @retval SR_OK Success.
526 * @retval SR_ERR_ARG Invalid driver.
530 SR_API int sr_dev_clear(const struct sr_dev_driver *driver)
535 sr_err("Invalid driver.");
539 if (driver->dev_clear)
540 ret = driver->dev_clear(driver);
542 ret = std_dev_clear(driver, NULL);
548 * Open the specified device.
550 * @param sdi Device instance to use. Must not be NULL.
552 * @return SR_OK upon success, a negative error code upon errors.
556 SR_API int sr_dev_open(struct sr_dev_inst *sdi)
560 if (!sdi || !sdi->driver || !sdi->driver->dev_open)
563 ret = sdi->driver->dev_open(sdi);
569 * Close the specified device.
571 * @param sdi Device instance to use. Must not be NULL.
573 * @return SR_OK upon success, a negative error code upon errors.
577 SR_API int sr_dev_close(struct sr_dev_inst *sdi)
581 if (!sdi || !sdi->driver || !sdi->driver->dev_close)
584 ret = sdi->driver->dev_close(sdi);
590 * Queries a device instances' driver.
592 * @param sdi Device instance to use. Must not be NULL.
594 * @return The driver instance or NULL on error.
596 SR_API struct sr_dev_driver *sr_dev_inst_driver_get(const struct sr_dev_inst *sdi)
598 if (!sdi || !sdi->driver)
605 * Queries a device instances' vendor.
607 * @param sdi Device instance to use. Must not be NULL.
609 * @return The vendor string or NULL.
611 SR_API const char *sr_dev_inst_vendor_get(const struct sr_dev_inst *sdi)
620 * Queries a device instances' model.
622 * @param sdi Device instance to use. Must not be NULL.
624 * @return The model string or NULL.
626 SR_API const char *sr_dev_inst_model_get(const struct sr_dev_inst *sdi)
635 * Queries a device instances' version.
637 * @param sdi Device instance to use. Must not be NULL.
639 * @return The version string or NULL.
641 SR_API const char *sr_dev_inst_version_get(const struct sr_dev_inst *sdi)
650 * Queries a device instances' serial number.
652 * @param sdi Device instance to use. Must not be NULL.
654 * @return The serial number string or NULL.
656 SR_API const char *sr_dev_inst_sernum_get(const struct sr_dev_inst *sdi)
661 return sdi->serial_num;
665 * Queries a device instances' connection identifier.
667 * @param sdi Device instance to use. Must not be NULL.
669 * @return A copy of the connection ID string or NULL. The caller is responsible
670 * for g_free()ing the string when it is no longer needed.
672 SR_API const char *sr_dev_inst_connid_get(const struct sr_dev_inst *sdi)
674 #ifdef HAVE_LIBUSB_1_0
675 struct drv_context *drvc;
677 char connection_id[64];
678 struct sr_usb_dev_inst *usb;
679 struct libusb_device **devlist;
685 #ifdef HAVE_LIBSERIALPORT
686 struct sr_serial_dev_inst *serial;
688 if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_SERIAL)) {
689 /* connection_id isn't populated, let's do that here. */
692 ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(serial->port);
696 #ifdef HAVE_LIBUSB_1_0
697 if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_USB)) {
698 /* connection_id isn't populated, let's do that here. */
700 drvc = sdi->driver->context;
703 if ((cnt = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist)) < 0) {
704 sr_err("Failed to retrieve device list: %s.",
705 libusb_error_name(cnt));
709 for (i = 0; i < cnt; i++) {
710 /* Find the USB device by the logical address we know. */
711 b = libusb_get_bus_number(devlist[i]);
712 a = libusb_get_device_address(devlist[i]);
713 if (b != usb->bus || a != usb->address)
716 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
717 ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(connection_id);
721 libusb_free_device_list(devlist, 1);
725 return sdi->connection_id;
729 * Queries a device instances' channel list.
731 * @param sdi Device instance to use. Must not be NULL.
733 * @return The GSList of channels or NULL.
735 SR_API GSList *sr_dev_inst_channels_get(const struct sr_dev_inst *sdi)
740 return sdi->channels;
744 * Queries a device instances' channel groups list.
746 * @param sdi Device instance to use. Must not be NULL.
748 * @return The GSList of channel groups or NULL.
750 SR_API GSList *sr_dev_inst_channel_groups_get(const struct sr_dev_inst *sdi)
755 return sdi->channel_groups;