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 A 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 ch = g_malloc0(sizeof(struct sr_channel));
61 ch->enabled = enabled;
63 ch->name = g_strdup(name);
69 * Set the name of the specified channel in the specified device.
71 * If the channel already has a different name assigned to it, it will be
72 * removed, and the new name will be saved instead.
74 * @param sdi The device instance the channel is connected to.
75 * @param[in] channelnum The number of the channel whose name to set.
76 * Note that the channel numbers start at 0.
77 * @param[in] name The new name that the specified channel should get. A copy
78 * of the string is made.
80 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
84 SR_API int sr_dev_channel_name_set(const struct sr_dev_inst *sdi,
85 int channelnum, const char *name)
88 struct sr_channel *ch;
92 sr_err("%s: sdi was NULL", __func__);
97 for (l = sdi->channels; l; l = l->next) {
99 if (ch->index == channelnum) {
101 ch->name = g_strdup(name);
111 * Enable or disable a channel on the specified device.
113 * @param sdi The device instance the channel is connected to.
114 * @param channelnum The channel number, starting from 0.
115 * @param state TRUE to enable the channel, FALSE to disable.
117 * @return SR_OK on success or SR_ERR on failure. In case of invalid
118 * arguments, SR_ERR_ARG is returned and the channel enabled state
123 SR_API int sr_dev_channel_enable(const struct sr_dev_inst *sdi, int channelnum,
127 struct sr_channel *ch;
129 gboolean was_enabled;
135 for (l = sdi->channels; l; l = l->next) {
137 if (ch->index == channelnum) {
138 was_enabled = ch->enabled;
141 if (!state != !was_enabled && sdi->driver
142 && sdi->driver->config_channel_set) {
143 ret = sdi->driver->config_channel_set(
144 sdi, ch, SR_CHANNEL_SET_ENABLED);
145 /* Roll back change if it wasn't applicable. */
146 if (ret == SR_ERR_ARG)
147 ch->enabled = was_enabled;
157 * Determine whether the specified device instance has the specified
160 * @param sdi Pointer to the device instance to be checked. Must not be NULL.
161 * If the device's 'driver' field is NULL (virtual device), this
162 * function will always return FALSE (virtual devices don't have
163 * a hardware capabilities list).
164 * @param[in] key The option that should be checked for is supported by the
167 * @retval TRUE Device has the specified option
168 * @retval FALSE Device does not have the specified option, invalid input
169 * parameters or other error conditions.
173 SR_API gboolean sr_dev_has_option(const struct sr_dev_inst *sdi, int key)
180 if (!sdi || !sdi->driver || !sdi->driver->config_list)
183 if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS,
184 &gvar, sdi, NULL) != SR_OK)
188 devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t));
189 for (i = 0; i < num_opts; i++) {
190 if ((devopts[i] & SR_CONF_MASK) == key) {
195 g_variant_unref(gvar);
201 * Allocate and init a new user-generated device instance.
203 * @param vendor Device vendor
204 * @param model Device model
205 * @param version Device version
207 * @retval struct sr_dev_inst *. Dynamically allocated, free using
208 * sr_dev_inst_free().
210 SR_API struct sr_dev_inst *sr_dev_inst_user_new(const char *vendor,
211 const char *model, const char *version)
213 struct sr_dev_inst *sdi;
215 sdi = g_malloc0(sizeof(struct sr_dev_inst));
217 sdi->vendor = g_strdup(vendor);
218 sdi->model = g_strdup(model);
219 sdi->version = g_strdup(version);
220 sdi->inst_type = SR_INST_USER;
226 * Add a new channel to the specified device instance.
228 SR_API int sr_dev_inst_channel_add(struct sr_dev_inst *sdi, int index, int type, const char *name)
230 struct sr_channel *ch;
232 if (!sdi || sdi->inst_type != SR_INST_USER || index < 0)
235 ch = sr_channel_new(index, type, TRUE, name);
236 sdi->channels = g_slist_append(sdi->channels, ch);
242 * Free device instance struct created by sr_dev_inst().
243 * @param sdi device instance to free.
245 SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
247 struct sr_channel *ch;
248 struct sr_channel_group *cg;
251 for (l = sdi->channels; l; l = l->next) {
257 g_slist_free(sdi->channels);
259 for (l = sdi->channel_groups; l; l = l->next) {
262 g_slist_free(cg->channels);
266 g_slist_free(sdi->channel_groups);
270 g_free(sdi->version);
271 g_free(sdi->serial_num);
272 g_free(sdi->connection_id);
276 #ifdef HAVE_LIBUSB_1_0
279 * Allocate and init struct for USB device instance.
280 * @param[in] bus @copydoc sr_usb_dev_inst::bus
281 * @param[in] address @copydoc sr_usb_dev_inst::address
282 * @param[in] hdl @copydoc sr_usb_dev_inst::devhdl
285 * @retval other struct sr_usb_dev_inst * for USB device instance.
287 SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
288 uint8_t address, struct libusb_device_handle *hdl)
290 struct sr_usb_dev_inst *udi;
292 if (!(udi = g_try_malloc(sizeof(struct sr_usb_dev_inst)))) {
293 sr_err("USB device instance malloc failed.");
298 udi->address = address;
305 * Free struct * allocated by sr_usb_dev_inst().
306 * @param usb struct* to free. Must not be NULL.
308 SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
315 #ifdef HAVE_LIBSERIALPORT
320 * Both parameters are copied to newly allocated strings, and freed
321 * automatically by sr_serial_dev_inst_free().
323 * @param[in] port OS-specific serial port specification. Examples:
324 * "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1".
325 * @param[in] serialcomm A serial communication parameters string, in the form
326 * of \<speed\>/\<data bits\>\<parity\>\<stopbits\>, for example
327 * "9600/8n1" or "600/7o2". This is an optional parameter;
328 * it may be filled in later.
330 * @return A pointer to a newly initialized struct sr_serial_dev_inst,
333 SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
334 const char *serialcomm)
336 struct sr_serial_dev_inst *serial;
339 sr_err("Serial port required.");
343 if (!(serial = g_try_malloc0(sizeof(struct sr_serial_dev_inst)))) {
344 sr_err("Serial device instance malloc failed.");
348 serial->port = g_strdup(port);
350 serial->serialcomm = g_strdup(serialcomm);
356 * Free struct sr_serial_dev_inst * allocated by sr_serial_dev_inst().
357 * @param serial struct sr_serial_dev_inst * to free. Must not be NULL.
359 SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
361 g_free(serial->port);
362 g_free(serial->serialcomm);
368 SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device)
370 struct sr_usbtmc_dev_inst *usbtmc;
373 sr_err("Device name required.");
377 if (!(usbtmc = g_try_malloc0(sizeof(struct sr_usbtmc_dev_inst)))) {
378 sr_err("USBTMC device instance malloc failed.");
382 usbtmc->device = g_strdup(device);
389 SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc)
391 g_free(usbtmc->device);
396 * Get the list of devices/instances of the specified driver.
398 * @param driver The driver to use. Must not be NULL.
400 * @return The list of devices/instances of this driver, or NULL upon errors
401 * or if the list is empty.
405 SR_API GSList *sr_dev_list(const struct sr_dev_driver *driver)
407 if (driver && driver->dev_list)
408 return driver->dev_list();
414 * Clear the list of device instances a driver knows about.
416 * @param driver The driver to use. This must be a pointer to one of
417 * the entries returned by sr_driver_list(). Must not be NULL.
419 * @retval SR_OK Success
420 * @retval SR_ERR_ARG Invalid driver
424 SR_API int sr_dev_clear(const struct sr_dev_driver *driver)
429 sr_err("Invalid driver.");
433 if (driver->dev_clear)
434 ret = driver->dev_clear();
436 ret = std_dev_clear(driver, NULL);
442 * Open the specified device.
444 * @param sdi Device instance to use. Must not be NULL.
446 * @return SR_OK upon success, a negative error code upon errors.
450 SR_API int sr_dev_open(struct sr_dev_inst *sdi)
454 if (!sdi || !sdi->driver || !sdi->driver->dev_open)
457 ret = sdi->driver->dev_open(sdi);
463 * Close the specified device.
465 * @param sdi Device instance to use. Must not be NULL.
467 * @return SR_OK upon success, a negative error code upon errors.
471 SR_API int sr_dev_close(struct sr_dev_inst *sdi)
475 if (!sdi || !sdi->driver || !sdi->driver->dev_close)
478 ret = sdi->driver->dev_close(sdi);
484 * Queries a device instances' driver.
486 * @param sdi Device instance to use. Must not be NULL.
488 * @return The driver instance or NULL on error.
490 SR_API struct sr_dev_driver *sr_dev_inst_driver_get(const struct sr_dev_inst *sdi)
492 if (!sdi || !sdi->driver)
499 * Queries a device instances' vendor.
501 * @param sdi Device instance to use. Must not be NULL.
503 * @return The vendor string or NULL.
505 SR_API const char *sr_dev_inst_vendor_get(const struct sr_dev_inst *sdi)
514 * Queries a device instances' model.
516 * @param sdi Device instance to use. Must not be NULL.
518 * @return The model string or NULL.
520 SR_API const char *sr_dev_inst_model_get(const struct sr_dev_inst *sdi)
529 * Queries a device instances' version.
531 * @param sdi Device instance to use. Must not be NULL.
533 * @return The version string or NULL.
535 SR_API const char *sr_dev_inst_version_get(const struct sr_dev_inst *sdi)
544 * Queries a device instances' serial number.
546 * @param sdi Device instance to use. Must not be NULL.
548 * @return The serial number string or NULL.
550 SR_API const char *sr_dev_inst_sernum_get(const struct sr_dev_inst *sdi)
555 return sdi->serial_num;
559 * Queries a device instances' connection identifier.
561 * @param sdi Device instance to use. Must not be NULL.
563 * @return A copy of the connection id string or NULL. The caller is responsible
564 * for g_free()ing the string when it is no longer needed.
566 SR_API const char *sr_dev_inst_connid_get(const struct sr_dev_inst *sdi)
568 struct drv_context *drvc;
570 char connection_id[64];
572 #ifdef HAVE_LIBUSB_1_0
573 struct sr_usb_dev_inst *usb;
574 struct libusb_device **devlist;
575 struct libusb_device_descriptor des;
581 #ifdef HAVE_LIBSERIALPORT
582 struct sr_serial_dev_inst *serial;
584 if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_SERIAL)) {
585 /* connection_id isn't populated, let's do that here. */
588 ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(serial->port);
593 #ifdef HAVE_LIBUSB_1_0
594 if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_USB)) {
595 /* connection_id isn't populated, let's do that here. */
597 drvc = sdi->driver->priv;
600 if ((cnt = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist)) < 0) {
601 sr_err("Failed to retrieve device list: %s.",
602 libusb_error_name(cnt));
606 for (i = 0; i < cnt; i++) {
607 if ((r = libusb_get_device_descriptor(devlist[i], &des)) < 0) {
608 sr_err("Failed to get device descriptor: %s.",
609 libusb_error_name(r));
613 /* Find the USB device by the logical address we know. */
614 b = libusb_get_bus_number(devlist[i]);
615 a = libusb_get_device_address(devlist[i]);
616 if (b != usb->bus || a != usb->address)
619 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
620 ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(connection_id);
624 libusb_free_device_list(devlist, 1);
628 return sdi->connection_id;
632 * Queries a device instances' channel list.
634 * @param sdi Device instance to use. Must not be NULL.
636 * @return The GSList of channels or NULL.
638 SR_API GSList *sr_dev_inst_channels_get(const struct sr_dev_inst *sdi)
643 return sdi->channels;
647 * Queries a device instances' channel groups list.
649 * @param sdi Device instance to use. Must not be NULL.
651 * @return The GSList of channel groups or NULL.
653 SR_API GSList *sr_dev_inst_channel_groups_get(const struct sr_dev_inst *sdi)
658 return sdi->channel_groups;