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"
26 #define LOG_PREFIX "device"
31 * Device handling in libsigrok.
35 * @defgroup grp_devices Devices
37 * Device handling in libsigrok.
43 * Allocate and initialize new struct sr_channel
44 * @param[in] index @copydoc sr_channel::index
45 * @param[in] type @copydoc sr_channel::type
46 * @param[in] enabled @copydoc sr_channel::enabled
47 * @param[in] name @copydoc sr_channel::name
49 * @return NULL (failure) or new struct sr_channel*.
51 SR_PRIV struct sr_channel *sr_channel_new(int index, int type,
52 gboolean enabled, const char *name)
54 struct sr_channel *ch;
56 if (!(ch = g_try_malloc0(sizeof(struct sr_channel)))) {
57 sr_err("Channel malloc failed.");
63 ch->enabled = enabled;
65 ch->name = g_strdup(name);
71 * Set the name of the specified channel in the specified device.
73 * If the channel already has a different name assigned to it, it will be
74 * removed, and the new name will be saved instead.
76 * @param sdi The device instance the channel is connected to.
77 * @param[in] channelnum The number of the channel whose name to set.
78 * Note that the channel numbers start at 0.
79 * @param[in] name The new name that the specified channel should get. A copy
80 * 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(const struct sr_dev_inst *sdi,
87 int channelnum, const char *name)
90 struct sr_channel *ch;
94 sr_err("%s: sdi was NULL", __func__);
99 for (l = sdi->channels; l; l = l->next) {
101 if (ch->index == channelnum) {
103 ch->name = g_strdup(name);
113 * Enable or disable a channel on the specified device.
115 * @param sdi The device instance the channel is connected to.
116 * @param channelnum The channel number, starting from 0.
117 * @param state TRUE to enable the channel, FALSE to disable.
119 * @return SR_OK on success or SR_ERR on failure. In case of invalid
120 * arguments, SR_ERR_ARG is returned and the channel enabled state
125 SR_API int sr_dev_channel_enable(const struct sr_dev_inst *sdi, int channelnum,
129 struct sr_channel *ch;
131 gboolean was_enabled;
137 for (l = sdi->channels; l; l = l->next) {
139 if (ch->index == channelnum) {
140 was_enabled = ch->enabled;
143 if (!state != !was_enabled && sdi->driver
144 && sdi->driver->config_channel_set) {
145 ret = sdi->driver->config_channel_set(
146 sdi, ch, SR_CHANNEL_SET_ENABLED);
147 /* Roll back change if it wasn't applicable. */
148 if (ret == SR_ERR_ARG)
149 ch->enabled = was_enabled;
159 * Add a trigger to the specified device (and the specified channel).
161 * If the specified channel of this device already has a trigger, it will
162 * be silently replaced.
164 * @param[in,out] sdi Pointer to the device instance; must not be NULL.
165 * @param[in] channelnum Number of channel, starting at 0.
166 * @param[in] trigger Trigger string, in the format used by sigrok-cli
168 * @return SR_OK on success or SR_ERR on failure. In case of invalid
169 * arguments, SR_ERR_ARG is returned and the trigger settings
174 SR_API int sr_dev_trigger_set(const struct sr_dev_inst *sdi, int channelnum,
178 struct sr_channel *ch;
186 for (l = sdi->channels; l; l = l->next) {
188 if (ch->index == channelnum) {
189 old_trigger = ch->trigger;
191 if (g_strcmp0(trigger, old_trigger) == 0)
193 /* Set new trigger if it has changed. */
194 ch->trigger = g_strdup(trigger);
196 if (sdi->driver && sdi->driver->config_channel_set) {
197 ret = sdi->driver->config_channel_set(
198 sdi, ch, SR_CHANNEL_SET_TRIGGER);
199 /* Roll back change if it wasn't applicable. */
200 if (ret == SR_ERR_ARG) {
202 ch->trigger = old_trigger;
215 * Determine whether the specified device instance has the specified
218 * @param sdi Pointer to the device instance to be checked. Must not be NULL.
219 * If the device's 'driver' field is NULL (virtual device), this
220 * function will always return FALSE (virtual devices don't have
221 * a hardware capabilities list).
222 * @param[in] key The option that should be checked for is supported by the
225 * @retval TRUE Device has the specified option
226 * @retval FALSE Device does not have the specified option, invalid input
227 * parameters or other error conditions.
231 SR_API gboolean sr_dev_has_option(const struct sr_dev_inst *sdi, int key)
238 if (!sdi || !sdi->driver || !sdi->driver->config_list)
241 if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS,
242 &gvar, sdi, NULL) != SR_OK)
246 devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t));
247 for (i = 0; i < num_opts; i++) {
248 if (devopts[i] == key) {
253 g_variant_unref(gvar);
259 * Allocate and init new device instance struct.
260 * @param[in] index @copydoc sr_dev_inst::index
261 * @param[in] status @copydoc sr_dev_inst::status
262 * @param[in] vendor @copydoc sr_dev_inst::vendor
263 * @param[in] model @copydoc sr_dev_inst::model
264 * @param[in] version @copydoc sr_dev_inst::version
267 * @retval struct sr_dev_inst *. Dynamically allocated, free using
268 * sr_dev_inst_free().
270 SR_PRIV struct sr_dev_inst *sr_dev_inst_new(int index, int status,
271 const char *vendor, const char *model, const char *version)
273 struct sr_dev_inst *sdi;
275 if (!(sdi = g_try_malloc(sizeof(struct sr_dev_inst)))) {
276 sr_err("Device instance malloc failed.");
282 sdi->status = status;
284 sdi->vendor = vendor ? g_strdup(vendor) : NULL;
285 sdi->model = model ? g_strdup(model) : NULL;
286 sdi->version = version ? g_strdup(version) : NULL;
287 sdi->channels = NULL;
288 sdi->channel_groups = NULL;
296 * Free device instance struct created by sr_dev_inst().
297 * @param sdi struct* to free.
299 SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
301 struct sr_channel *ch;
304 for (l = sdi->channels; l; l = l->next) {
310 g_slist_free(sdi->channels);
312 if (sdi->channel_groups)
313 g_slist_free(sdi->channel_groups);
317 g_free(sdi->version);
321 #ifdef HAVE_LIBUSB_1_0
324 * Allocate and init struct for USB device instance.
325 * @param[in] bus @copydoc sr_usb_dev_inst::bus
326 * @param[in] address @copydoc sr_usb_dev_inst::address
327 * @param[in] hdl @copydoc sr_usb_dev_inst::devhdl
330 * @retval other struct sr_usb_dev_inst * for USB device instance.
332 SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
333 uint8_t address, struct libusb_device_handle *hdl)
335 struct sr_usb_dev_inst *udi;
337 if (!(udi = g_try_malloc(sizeof(struct sr_usb_dev_inst)))) {
338 sr_err("USB device instance malloc failed.");
343 udi->address = address;
350 * Free struct * allocated by sr_usb_dev_inst().
351 * @param usb struct* to free. Must not be NULL.
353 SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
360 #ifdef HAVE_LIBSERIALPORT
365 * Both parameters are copied to newly allocated strings, and freed
366 * automatically by sr_serial_dev_inst_free().
368 * @param[in] port OS-specific serial port specification. Examples:
369 * "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1".
370 * @param[in] serialcomm A serial communication parameters string, in the form
371 * of \<speed\>/\<data bits\>\<parity\>\<stopbits\>, for example
372 * "9600/8n1" or "600/7o2". This is an optional parameter;
373 * it may be filled in later.
375 * @return A pointer to a newly initialized struct sr_serial_dev_inst,
378 SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
379 const char *serialcomm)
381 struct sr_serial_dev_inst *serial;
384 sr_err("Serial port required.");
388 if (!(serial = g_try_malloc0(sizeof(struct sr_serial_dev_inst)))) {
389 sr_err("Serial device instance malloc failed.");
393 serial->port = g_strdup(port);
395 serial->serialcomm = g_strdup(serialcomm);
401 * Free struct sr_serial_dev_inst * allocated by sr_serial_dev_inst().
402 * @param serial struct sr_serial_dev_inst * to free. Must not be NULL.
404 SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
406 g_free(serial->port);
407 g_free(serial->serialcomm);
413 SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device)
415 struct sr_usbtmc_dev_inst *usbtmc;
418 sr_err("Device name required.");
422 if (!(usbtmc = g_try_malloc0(sizeof(struct sr_usbtmc_dev_inst)))) {
423 sr_err("USBTMC device instance malloc failed.");
427 usbtmc->device = g_strdup(device);
434 SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc)
436 g_free(usbtmc->device);
441 * Get the list of devices/instances of the specified driver.
443 * @param driver The driver to use. Must not be NULL.
445 * @return The list of devices/instances of this driver, or NULL upon errors
446 * or if the list is empty.
450 SR_API GSList *sr_dev_list(const struct sr_dev_driver *driver)
452 if (driver && driver->dev_list)
453 return driver->dev_list();
459 * Clear the list of device instances a driver knows about.
461 * @param driver The driver to use. This must be a pointer to one of
462 * the entries returned by sr_driver_list(). Must not be NULL.
464 * @retval SR_OK Success
465 * @retval SR_ERR_ARG Invalid driver
469 SR_API int sr_dev_clear(const struct sr_dev_driver *driver)
474 sr_err("Invalid driver.");
478 if (driver->dev_clear)
479 ret = driver->dev_clear();
481 ret = std_dev_clear(driver, NULL);
487 * Open the specified device.
489 * @param sdi Device instance to use. Must not be NULL.
491 * @return SR_OK upon success, a negative error code upon errors.
495 SR_API int sr_dev_open(struct sr_dev_inst *sdi)
499 if (!sdi || !sdi->driver || !sdi->driver->dev_open)
502 ret = sdi->driver->dev_open(sdi);
508 * Close the specified device.
510 * @param sdi Device instance to use. Must not be NULL.
512 * @return SR_OK upon success, a negative error code upon errors.
516 SR_API int sr_dev_close(struct sr_dev_inst *sdi)
520 if (!sdi || !sdi->driver || !sdi->driver->dev_close)
523 ret = sdi->driver->dev_close(sdi);