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/>.
22 #include "config.h" /* Needed for HAVE_LIBUSB_1_0 and others. */
23 #include "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 new struct sr_channel and add to sdi.
46 * @param[in] sdi The device instance the channel is connected to.
47 * @param[in] index @copydoc sr_channel::index
48 * @param[in] type @copydoc sr_channel::type
49 * @param[in] enabled @copydoc sr_channel::enabled
50 * @param[in] name @copydoc sr_channel::name
52 * @return A new struct sr_channel*.
54 SR_PRIV struct sr_channel *sr_channel_new(struct sr_dev_inst *sdi,
55 int index, int type, gboolean enabled, const char *name)
57 struct sr_channel *ch;
59 ch = g_malloc0(sizeof(struct sr_channel));
63 ch->enabled = enabled;
65 ch->name = g_strdup(name);
67 sdi->channels = g_slist_append(sdi->channels, ch);
73 * Set the name of the specified channel.
75 * If the channel already has a different name assigned to it, it will be
76 * removed, and the new name will be saved instead.
78 * @param[in] channel The channel whose name to set.
79 * @param[in] name The new name that the specified channel should get. A
80 * copy of the string is made.
82 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
86 SR_API int sr_dev_channel_name_set(struct sr_channel *channel,
90 sr_err("%s: channel was NULL", __func__);
94 g_free(channel->name);
95 channel->name = g_strdup(name);
100 * Enable or disable a channel.
102 * @param[in] channel The channel to enable or disable.
103 * @param[in] state TRUE to enable the channel, FALSE to disable.
105 * @return SR_OK on success or SR_ERR on failure. In case of invalid
106 * arguments, SR_ERR_ARG is returned and the channel enabled state
111 SR_API int sr_dev_channel_enable(struct sr_channel *channel,
115 gboolean was_enabled;
116 struct sr_dev_inst *sdi;
122 was_enabled = channel->enabled;
123 channel->enabled = state;
124 if (!state != !was_enabled && sdi->driver
125 && sdi->driver->config_channel_set) {
126 ret = sdi->driver->config_channel_set(
127 sdi, channel, SR_CHANNEL_SET_ENABLED);
128 /* Roll back change if it wasn't applicable. */
137 * Determine whether the specified device instance has the specified
140 * @param sdi Pointer to the device instance to be checked. Must not be NULL.
141 * If the device's 'driver' field is NULL (virtual device), this
142 * function will always return FALSE (virtual devices don't have
143 * a hardware capabilities list).
144 * @param[in] key The option that should be checked for is supported by the
147 * @retval TRUE Device has the specified option
148 * @retval FALSE Device does not have the specified option, invalid input
149 * parameters or other error conditions.
153 SR_API gboolean sr_dev_has_option(const struct sr_dev_inst *sdi, int key)
160 if (!sdi || !sdi->driver || !sdi->driver->config_list)
163 if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS,
164 &gvar, sdi, NULL) != SR_OK)
168 devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t));
169 for (i = 0; i < num_opts; i++) {
170 if ((devopts[i] & SR_CONF_MASK) == key) {
175 g_variant_unref(gvar);
181 * Allocate and init a new user-generated device instance.
183 * @param vendor Device vendor
184 * @param model Device model
185 * @param version Device version
187 * @retval struct sr_dev_inst *. Dynamically allocated, free using
188 * sr_dev_inst_free().
190 SR_API struct sr_dev_inst *sr_dev_inst_user_new(const char *vendor,
191 const char *model, const char *version)
193 struct sr_dev_inst *sdi;
195 sdi = g_malloc0(sizeof(struct sr_dev_inst));
197 sdi->vendor = g_strdup(vendor);
198 sdi->model = g_strdup(model);
199 sdi->version = g_strdup(version);
200 sdi->inst_type = SR_INST_USER;
206 * Add a new channel to the specified device instance.
208 SR_API int sr_dev_inst_channel_add(struct sr_dev_inst *sdi, int index, int type, const char *name)
210 if (!sdi || sdi->inst_type != SR_INST_USER || index < 0)
213 sr_channel_new(sdi, index, type, TRUE, name);
219 * Free device instance struct created by sr_dev_inst().
220 * @param sdi device instance to free.
222 SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
224 struct sr_channel *ch;
225 struct sr_channel_group *cg;
228 for (l = sdi->channels; l; l = l->next) {
234 g_slist_free(sdi->channels);
236 for (l = sdi->channel_groups; l; l = l->next) {
239 g_slist_free(cg->channels);
243 g_slist_free(sdi->channel_groups);
247 g_free(sdi->version);
248 g_free(sdi->serial_num);
249 g_free(sdi->connection_id);
253 #ifdef HAVE_LIBUSB_1_0
256 * Allocate and init struct for USB device instance.
257 * @param[in] bus @copydoc sr_usb_dev_inst::bus
258 * @param[in] address @copydoc sr_usb_dev_inst::address
259 * @param[in] hdl @copydoc sr_usb_dev_inst::devhdl
261 * @retval other struct sr_usb_dev_inst * for USB device instance.
263 SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
264 uint8_t address, struct libusb_device_handle *hdl)
266 struct sr_usb_dev_inst *udi;
268 udi = g_malloc0(sizeof(struct sr_usb_dev_inst));
270 udi->address = address;
277 * Free struct * allocated by sr_usb_dev_inst().
278 * @param usb struct* to free. Must not be NULL.
280 SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
287 #ifdef HAVE_LIBSERIALPORT
292 * Both parameters are copied to newly allocated strings, and freed
293 * automatically by sr_serial_dev_inst_free().
295 * @param[in] port OS-specific serial port specification. Examples:
296 * "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1".
298 * @param[in] serialcomm A serial communication parameters string, in the form
299 * of \<speed\>/\<data bits\>\<parity\>\<stopbits\>, for example
300 * "9600/8n1" or "600/7o2". This is an optional parameter;
301 * it may be filled in later. Can be NULL.
303 * @return A pointer to a newly initialized struct sr_serial_dev_inst,
306 SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
307 const char *serialcomm)
309 struct sr_serial_dev_inst *serial;
311 serial = g_malloc0(sizeof(struct sr_serial_dev_inst));
312 serial->port = g_strdup(port);
314 serial->serialcomm = g_strdup(serialcomm);
320 * Free struct sr_serial_dev_inst * allocated by sr_serial_dev_inst().
321 * @param serial struct sr_serial_dev_inst * to free. Must not be NULL.
323 SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
325 g_free(serial->port);
326 g_free(serial->serialcomm);
332 SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device)
334 struct sr_usbtmc_dev_inst *usbtmc;
336 usbtmc = g_malloc0(sizeof(struct sr_usbtmc_dev_inst));
337 usbtmc->device = g_strdup(device);
344 SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc)
346 g_free(usbtmc->device);
351 * Get the list of devices/instances of the specified driver.
353 * @param driver The driver to use. Must not be NULL.
355 * @return The list of devices/instances of this driver, or NULL upon errors
356 * or if the list is empty.
360 SR_API GSList *sr_dev_list(const struct sr_dev_driver *driver)
362 if (driver && driver->dev_list)
363 return driver->dev_list(driver);
369 * Clear the list of device instances a driver knows about.
371 * @param driver The driver to use. This must be a pointer to one of
372 * the entries returned by sr_driver_list(). Must not be NULL.
374 * @retval SR_OK Success
375 * @retval SR_ERR_ARG Invalid driver
379 SR_API int sr_dev_clear(const struct sr_dev_driver *driver)
384 sr_err("Invalid driver.");
388 if (driver->dev_clear)
389 ret = driver->dev_clear(driver);
391 ret = std_dev_clear(driver, NULL);
397 * Open the specified device.
399 * @param sdi Device instance to use. Must not be NULL.
401 * @return SR_OK upon success, a negative error code upon errors.
405 SR_API int sr_dev_open(struct sr_dev_inst *sdi)
409 if (!sdi || !sdi->driver || !sdi->driver->dev_open)
412 ret = sdi->driver->dev_open(sdi);
418 * Close the specified device.
420 * @param sdi Device instance to use. Must not be NULL.
422 * @return SR_OK upon success, a negative error code upon errors.
426 SR_API int sr_dev_close(struct sr_dev_inst *sdi)
430 if (!sdi || !sdi->driver || !sdi->driver->dev_close)
433 ret = sdi->driver->dev_close(sdi);
439 * Queries a device instances' driver.
441 * @param sdi Device instance to use. Must not be NULL.
443 * @return The driver instance or NULL on error.
445 SR_API struct sr_dev_driver *sr_dev_inst_driver_get(const struct sr_dev_inst *sdi)
447 if (!sdi || !sdi->driver)
454 * Queries a device instances' vendor.
456 * @param sdi Device instance to use. Must not be NULL.
458 * @return The vendor string or NULL.
460 SR_API const char *sr_dev_inst_vendor_get(const struct sr_dev_inst *sdi)
469 * Queries a device instances' model.
471 * @param sdi Device instance to use. Must not be NULL.
473 * @return The model string or NULL.
475 SR_API const char *sr_dev_inst_model_get(const struct sr_dev_inst *sdi)
484 * Queries a device instances' version.
486 * @param sdi Device instance to use. Must not be NULL.
488 * @return The version string or NULL.
490 SR_API const char *sr_dev_inst_version_get(const struct sr_dev_inst *sdi)
499 * Queries a device instances' serial number.
501 * @param sdi Device instance to use. Must not be NULL.
503 * @return The serial number string or NULL.
505 SR_API const char *sr_dev_inst_sernum_get(const struct sr_dev_inst *sdi)
510 return sdi->serial_num;
514 * Queries a device instances' connection identifier.
516 * @param sdi Device instance to use. Must not be NULL.
518 * @return A copy of the connection id string or NULL. The caller is responsible
519 * for g_free()ing the string when it is no longer needed.
521 SR_API const char *sr_dev_inst_connid_get(const struct sr_dev_inst *sdi)
523 #ifdef HAVE_LIBUSB_1_0
524 struct drv_context *drvc;
526 char connection_id[64];
527 struct sr_usb_dev_inst *usb;
528 struct libusb_device **devlist;
529 struct libusb_device_descriptor des;
535 #ifdef HAVE_LIBSERIALPORT
536 struct sr_serial_dev_inst *serial;
538 if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_SERIAL)) {
539 /* connection_id isn't populated, let's do that here. */
542 ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(serial->port);
546 #ifdef HAVE_LIBUSB_1_0
547 if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_USB)) {
548 /* connection_id isn't populated, let's do that here. */
550 drvc = sdi->driver->priv;
553 if ((cnt = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist)) < 0) {
554 sr_err("Failed to retrieve device list: %s.",
555 libusb_error_name(cnt));
559 for (i = 0; i < cnt; i++) {
560 if ((r = libusb_get_device_descriptor(devlist[i], &des)) < 0) {
561 sr_err("Failed to get device descriptor: %s.",
562 libusb_error_name(r));
566 /* Find the USB device by the logical address we know. */
567 b = libusb_get_bus_number(devlist[i]);
568 a = libusb_get_device_address(devlist[i]);
569 if (b != usb->bus || a != usb->address)
572 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
573 ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(connection_id);
577 libusb_free_device_list(devlist, 1);
581 return sdi->connection_id;
585 * Queries a device instances' channel list.
587 * @param sdi Device instance to use. Must not be NULL.
589 * @return The GSList of channels or NULL.
591 SR_API GSList *sr_dev_inst_channels_get(const struct sr_dev_inst *sdi)
596 return sdi->channels;
600 * Queries a device instances' channel groups list.
602 * @param sdi Device instance to use. Must not be NULL.
604 * @return The GSList of channel groups or NULL.
606 SR_API GSList *sr_dev_inst_channel_groups_get(const struct sr_dev_inst *sdi)
611 return sdi->channel_groups;