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
46 * @param[in] index @copydoc sr_channel::index
47 * @param[in] type @copydoc sr_channel::type
48 * @param[in] enabled @copydoc sr_channel::enabled
49 * @param[in] name @copydoc sr_channel::name
51 * @return NULL (failure) or new struct sr_channel*.
53 SR_PRIV struct sr_channel *sr_channel_new(int index, int type,
54 gboolean enabled, const char *name)
56 struct sr_channel *ch;
58 if (!(ch = g_try_malloc0(sizeof(struct sr_channel)))) {
59 sr_err("Channel malloc failed.");
65 ch->enabled = enabled;
67 ch->name = g_strdup(name);
73 * Set the name of the specified channel in the specified device.
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 sdi The device instance the channel is connected to.
79 * @param[in] channelnum The number of the channel whose name to set.
80 * Note that the channel numbers start at 0.
81 * @param[in] name The new name that the specified channel should get. A copy
82 * of the string is made.
84 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
88 SR_API int sr_dev_channel_name_set(const struct sr_dev_inst *sdi,
89 int channelnum, const char *name)
92 struct sr_channel *ch;
96 sr_err("%s: sdi was NULL", __func__);
101 for (l = sdi->channels; l; l = l->next) {
103 if (ch->index == channelnum) {
105 ch->name = g_strdup(name);
115 * Enable or disable a channel on the specified device.
117 * @param sdi The device instance the channel is connected to.
118 * @param channelnum The channel number, starting from 0.
119 * @param state TRUE to enable the channel, FALSE to disable.
121 * @return SR_OK on success or SR_ERR on failure. In case of invalid
122 * arguments, SR_ERR_ARG is returned and the channel enabled state
127 SR_API int sr_dev_channel_enable(const struct sr_dev_inst *sdi, int channelnum,
131 struct sr_channel *ch;
133 gboolean was_enabled;
139 for (l = sdi->channels; l; l = l->next) {
141 if (ch->index == channelnum) {
142 was_enabled = ch->enabled;
145 if (!state != !was_enabled && sdi->driver
146 && sdi->driver->config_channel_set) {
147 ret = sdi->driver->config_channel_set(
148 sdi, ch, SR_CHANNEL_SET_ENABLED);
149 /* Roll back change if it wasn't applicable. */
150 if (ret == SR_ERR_ARG)
151 ch->enabled = was_enabled;
161 * Determine whether the specified device instance has the specified
164 * @param sdi Pointer to the device instance to be checked. Must not be NULL.
165 * If the device's 'driver' field is NULL (virtual device), this
166 * function will always return FALSE (virtual devices don't have
167 * a hardware capabilities list).
168 * @param[in] key The option that should be checked for is supported by the
171 * @retval TRUE Device has the specified option
172 * @retval FALSE Device does not have the specified option, invalid input
173 * parameters or other error conditions.
177 SR_API gboolean sr_dev_has_option(const struct sr_dev_inst *sdi, int key)
184 if (!sdi || !sdi->driver || !sdi->driver->config_list)
187 if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS,
188 &gvar, sdi, NULL) != SR_OK)
192 devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t));
193 for (i = 0; i < num_opts; i++) {
194 if ((devopts[i] & SR_CONF_MASK) == key) {
199 g_variant_unref(gvar);
205 * Allocate and init a new user-generated device instance.
207 * @param vendor Device vendor
208 * @param model Device model
209 * @param version Device version
211 * @retval struct sr_dev_inst *. Dynamically allocated, free using
212 * sr_dev_inst_free().
214 SR_API struct sr_dev_inst *sr_dev_inst_user_new(const char *vendor,
215 const char *model, const char *version)
217 struct sr_dev_inst *sdi;
219 sdi = g_malloc0(sizeof(struct sr_dev_inst));
221 sdi->vendor = g_strdup(vendor);
222 sdi->model = g_strdup(model);
223 sdi->version = g_strdup(version);
224 sdi->inst_type = SR_INST_USER;
230 * Add a new channel to the specified device instance.
232 SR_API int sr_dev_inst_channel_add(struct sr_dev_inst *sdi, int index, int type, const char *name)
234 struct sr_channel *ch;
236 if (!sdi || sdi->inst_type != SR_INST_USER || index < 0)
239 ch = sr_channel_new(index, type, TRUE, name);
243 sdi->channels = g_slist_append(sdi->channels, ch);
249 * Free device instance struct created by sr_dev_inst().
250 * @param sdi device instance to free.
252 SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
254 struct sr_channel *ch;
255 struct sr_channel_group *cg;
258 for (l = sdi->channels; l; l = l->next) {
264 g_slist_free(sdi->channels);
266 for (l = sdi->channel_groups; l; l = l->next) {
269 g_slist_free(cg->channels);
273 g_slist_free(sdi->channel_groups);
277 g_free(sdi->version);
278 g_free(sdi->serial_num);
279 g_free(sdi->connection_id);
283 #ifdef HAVE_LIBUSB_1_0
286 * Allocate and init struct for USB device instance.
287 * @param[in] bus @copydoc sr_usb_dev_inst::bus
288 * @param[in] address @copydoc sr_usb_dev_inst::address
289 * @param[in] hdl @copydoc sr_usb_dev_inst::devhdl
292 * @retval other struct sr_usb_dev_inst * for USB device instance.
294 SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
295 uint8_t address, struct libusb_device_handle *hdl)
297 struct sr_usb_dev_inst *udi;
299 if (!(udi = g_try_malloc(sizeof(struct sr_usb_dev_inst)))) {
300 sr_err("USB device instance malloc failed.");
305 udi->address = address;
312 * Free struct * allocated by sr_usb_dev_inst().
313 * @param usb struct* to free. Must not be NULL.
315 SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
322 #ifdef HAVE_LIBSERIALPORT
327 * Both parameters are copied to newly allocated strings, and freed
328 * automatically by sr_serial_dev_inst_free().
330 * @param[in] port OS-specific serial port specification. Examples:
331 * "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1".
332 * @param[in] serialcomm A serial communication parameters string, in the form
333 * of \<speed\>/\<data bits\>\<parity\>\<stopbits\>, for example
334 * "9600/8n1" or "600/7o2". This is an optional parameter;
335 * it may be filled in later.
337 * @return A pointer to a newly initialized struct sr_serial_dev_inst,
340 SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
341 const char *serialcomm)
343 struct sr_serial_dev_inst *serial;
346 sr_err("Serial port required.");
350 if (!(serial = g_try_malloc0(sizeof(struct sr_serial_dev_inst)))) {
351 sr_err("Serial device instance malloc failed.");
355 serial->port = g_strdup(port);
357 serial->serialcomm = g_strdup(serialcomm);
363 * Free struct sr_serial_dev_inst * allocated by sr_serial_dev_inst().
364 * @param serial struct sr_serial_dev_inst * to free. Must not be NULL.
366 SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
368 g_free(serial->port);
369 g_free(serial->serialcomm);
375 SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device)
377 struct sr_usbtmc_dev_inst *usbtmc;
380 sr_err("Device name required.");
384 if (!(usbtmc = g_try_malloc0(sizeof(struct sr_usbtmc_dev_inst)))) {
385 sr_err("USBTMC device instance malloc failed.");
389 usbtmc->device = g_strdup(device);
396 SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc)
398 g_free(usbtmc->device);
403 * Get the list of devices/instances of the specified driver.
405 * @param driver The driver to use. Must not be NULL.
407 * @return The list of devices/instances of this driver, or NULL upon errors
408 * or if the list is empty.
412 SR_API GSList *sr_dev_list(const struct sr_dev_driver *driver)
414 if (driver && driver->dev_list)
415 return driver->dev_list();
421 * Clear the list of device instances a driver knows about.
423 * @param driver The driver to use. This must be a pointer to one of
424 * the entries returned by sr_driver_list(). Must not be NULL.
426 * @retval SR_OK Success
427 * @retval SR_ERR_ARG Invalid driver
431 SR_API int sr_dev_clear(const struct sr_dev_driver *driver)
436 sr_err("Invalid driver.");
440 if (driver->dev_clear)
441 ret = driver->dev_clear();
443 ret = std_dev_clear(driver, NULL);
449 * Open the specified device.
451 * @param sdi Device instance to use. Must not be NULL.
453 * @return SR_OK upon success, a negative error code upon errors.
457 SR_API int sr_dev_open(struct sr_dev_inst *sdi)
461 if (!sdi || !sdi->driver || !sdi->driver->dev_open)
464 ret = sdi->driver->dev_open(sdi);
470 * Close the specified device.
472 * @param sdi Device instance to use. Must not be NULL.
474 * @return SR_OK upon success, a negative error code upon errors.
478 SR_API int sr_dev_close(struct sr_dev_inst *sdi)
482 if (!sdi || !sdi->driver || !sdi->driver->dev_close)
485 ret = sdi->driver->dev_close(sdi);
491 * Queries a device instances' driver.
493 * @param sdi Device instance to use. Must not be NULL.
495 * @return The driver instance or NULL on error.
497 SR_API struct sr_dev_driver *sr_dev_inst_driver_get(const struct sr_dev_inst *sdi)
499 if (!sdi || !sdi->driver)
506 * Queries a device instances' vendor.
508 * @param sdi Device instance to use. Must not be NULL.
510 * @return The vendor string or NULL.
512 SR_API const char *sr_dev_inst_vendor_get(const struct sr_dev_inst *sdi)
521 * Queries a device instances' model.
523 * @param sdi Device instance to use. Must not be NULL.
525 * @return The model string or NULL.
527 SR_API const char *sr_dev_inst_model_get(const struct sr_dev_inst *sdi)
536 * Queries a device instances' version.
538 * @param sdi Device instance to use. Must not be NULL.
540 * @return The version string or NULL.
542 SR_API const char *sr_dev_inst_version_get(const struct sr_dev_inst *sdi)
551 * Queries a device instances' serial number.
553 * @param sdi Device instance to use. Must not be NULL.
555 * @return The serial number string or NULL.
557 SR_API const char *sr_dev_inst_sernum_get(const struct sr_dev_inst *sdi)
562 return sdi->serial_num;
566 * Queries a device instances' connection identifier.
568 * @param sdi Device instance to use. Must not be NULL.
570 * @return A copy of the connection id string or NULL. The caller is responsible
571 * for g_free()ing the string when it is no longer needed.
573 SR_API const char *sr_dev_inst_connid_get(const struct sr_dev_inst *sdi)
575 struct drv_context *drvc;
577 char connection_id[64];
579 #ifdef HAVE_LIBUSB_1_0
580 struct sr_usb_dev_inst *usb;
581 struct libusb_device **devlist;
582 struct libusb_device_descriptor des;
588 #ifdef HAVE_LIBSERIALPORT
589 struct sr_serial_dev_inst *serial;
591 if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_SERIAL)) {
592 /* connection_id isn't populated, let's do that here. */
595 ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(serial->port);
600 #ifdef HAVE_LIBUSB_1_0
601 if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_USB)) {
602 /* connection_id isn't populated, let's do that here. */
604 drvc = sdi->driver->priv;
607 if ((cnt = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist)) < 0) {
608 sr_err("Failed to retrieve device list: %s.",
609 libusb_error_name(cnt));
613 for (i = 0; i < cnt; i++) {
614 if ((r = libusb_get_device_descriptor(devlist[i], &des)) < 0) {
615 sr_err("Failed to get device descriptor: %s.",
616 libusb_error_name(r));
620 /* Find the USB device by the logical address we know. */
621 b = libusb_get_bus_number(devlist[i]);
622 a = libusb_get_device_address(devlist[i]);
623 if (b != usb->bus || a != usb->address)
626 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
627 ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(connection_id);
631 libusb_free_device_list(devlist, 1);
635 return sdi->connection_id;
639 * Queries a device instances' channel list.
641 * @param sdi Device instance to use. Must not be NULL.
643 * @return The GSList of channels or NULL.
645 SR_API GSList *sr_dev_inst_channels_get(const struct sr_dev_inst *sdi)
650 return sdi->channels;
654 * Queries a device instances' channel groups list.
656 * @param sdi Device instance to use. Must not be NULL.
658 * @return The GSList of channel groups or NULL.
660 SR_API GSList *sr_dev_inst_channel_groups_get(const struct sr_dev_inst *sdi)
665 return sdi->channel_groups;