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 /* Message logging helpers with subsystem-specific prefix string. */
27 #define LOG_PREFIX "device: "
28 #define sr_log(l, s, args...) sr_log(l, LOG_PREFIX s, ## args)
29 #define sr_spew(s, args...) sr_spew(LOG_PREFIX s, ## args)
30 #define sr_dbg(s, args...) sr_dbg(LOG_PREFIX s, ## args)
31 #define sr_info(s, args...) sr_info(LOG_PREFIX s, ## args)
32 #define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args)
33 #define sr_err(s, args...) sr_err(LOG_PREFIX s, ## args)
38 * Device handling in libsigrok.
42 * @defgroup grp_devices Devices
44 * Device handling in libsigrok.
50 * Allocate and initialize new struct sr_probe
51 * @param[in] index \copydoc sr_probe::index
52 * @param[in] type \copydoc sr_probe::type
53 * @param[in] enabled \copydoc sr_probe::enabled
54 * @param[in] name \copydoc sr_probe::name
56 * @return NULL (failure) or new struct sr_probe*.
58 SR_PRIV struct sr_probe *sr_probe_new(int index, int type,
59 gboolean enabled, const char *name)
61 struct sr_probe *probe;
63 if (!(probe = g_try_malloc0(sizeof(struct sr_probe)))) {
64 sr_err("Probe malloc failed.");
70 probe->enabled = enabled;
72 probe->name = g_strdup(name);
78 * Set the name of the specified probe in the specified device.
80 * If the probe already has a different name assigned to it, it will be
81 * removed, and the new name will be saved instead.
83 * @param sdi The device instance the probe is connected to.
84 * @param[in] probenum The number of the probe whose name to set.
85 * Note that the probe numbers start at 0.
86 * @param[in] name The new name that the specified probe should get. A copy
87 * of the string is made.
89 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
93 SR_API int sr_dev_probe_name_set(const struct sr_dev_inst *sdi,
94 int probenum, const char *name)
97 struct sr_probe *probe;
101 sr_err("%s: sdi was NULL", __func__);
106 for (l = sdi->probes; l; l = l->next) {
108 if (probe->index == probenum) {
110 probe->name = g_strdup(name);
120 * Enable or disable a probe on the specified device.
122 * @param sdi The device instance the probe is connected to.
123 * @param probenum The probe number, starting from 0.
124 * @param state TRUE to enable the probe, FALSE to disable.
126 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
130 SR_API int sr_dev_probe_enable(const struct sr_dev_inst *sdi, int probenum,
134 struct sr_probe *probe;
141 for (l = sdi->probes; l; l = l->next) {
143 if (probe->index == probenum) {
144 probe->enabled = state;
154 * Add a trigger to the specified device (and the specified probe).
156 * If the specified probe of this device already has a trigger, it will
157 * be silently replaced.
159 * @param[in,out] sdi Pointer to the device instance; must not be NULL.
160 * @param[in] probenum Number of probe, starting at 0.
161 * @param[in] trigger Trigger string, in the format used by sigrok-cli
163 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
167 SR_API int sr_dev_trigger_set(const struct sr_dev_inst *sdi, int probenum,
171 struct sr_probe *probe;
178 for (l = sdi->probes; l; l = l->next) {
180 if (probe->index == probenum) {
181 /* If the probe already has a trigger, kill it first. */
182 g_free(probe->trigger);
183 probe->trigger = g_strdup(trigger);
193 * Determine whether the specified device instance has the specified
196 * @param sdi Pointer to the device instance to be checked. Must not be NULL.
197 * If the device's 'driver' field is NULL (virtual device), this
198 * function will always return FALSE (virtual devices don't have
199 * a hardware capabilities list).
200 * @param[in] key The option that should be checked for is supported by the
203 * @retval TRUE Device has the specified option
204 * @retval FALSE Device does not have the specified option, invalid input
205 * parameters or other error conditions.
209 SR_API gboolean sr_dev_has_option(const struct sr_dev_inst *sdi, int key)
216 if (!sdi || !sdi->driver || !sdi->driver->config_list)
219 if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS,
220 &gvar, NULL, NULL) != SR_OK)
224 devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t));
225 for (i = 0; i < num_opts; i++) {
226 if (devopts[i] == key) {
231 g_variant_unref(gvar);
237 * Allocate and init new device instance struct.
238 * \param[in] index \copydoc sr_dev_inst::index
239 * \param[in] status \copydoc sr_dev_inst::status
240 * \param[in] vendor \copydoc sr_dev_inst::vendor
241 * \param[in] model \copydoc sr_dev_inst::model
242 * \param[in] version \copydoc sr_dev_inst::version
245 * \retval struct sr_dev_inst *. Dynamically allocated, free using
246 * sr_dev_inst_free().
248 SR_PRIV struct sr_dev_inst *sr_dev_inst_new(int index, int status,
249 const char *vendor, const char *model, const char *version)
251 struct sr_dev_inst *sdi;
253 if (!(sdi = g_try_malloc(sizeof(struct sr_dev_inst)))) {
254 sr_err("Device instance malloc failed.");
260 sdi->status = status;
262 sdi->vendor = vendor ? g_strdup(vendor) : NULL;
263 sdi->model = model ? g_strdup(model) : NULL;
264 sdi->version = version ? g_strdup(version) : NULL;
266 sdi->probe_groups = NULL;
274 * Free device instance struct created by sr_dev_inst().
275 * \param sdi struct* to free.
277 SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
279 struct sr_probe *probe;
282 for (l = sdi->probes; l; l = l->next) {
285 g_free(probe->trigger);
288 g_slist_free(sdi->probes);
290 if (sdi->probe_groups)
291 g_slist_free(sdi->probe_groups);
295 g_free(sdi->version);
299 #ifdef HAVE_LIBUSB_1_0
302 * Allocate and init struct for USB device instance.
303 * \param[in] bus \copydoc sr_usb_dev_inst::bus
304 * \param[in] address \copydoc sr_usb_dev_inst::address
305 * \param[in] hdl \copydoc sr_usb_dev_inst::devhdl
308 * \retval other struct sr_usb_dev_inst * for USB device instance.
310 SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
311 uint8_t address, struct libusb_device_handle *hdl)
313 struct sr_usb_dev_inst *udi;
315 if (!(udi = g_try_malloc(sizeof(struct sr_usb_dev_inst)))) {
316 sr_err("USB device instance malloc failed.");
321 udi->address = address;
328 * Free struct * allocated by sr_usb_dev_inst().
329 * \param usb struct* to free. Must not be NULL.
331 SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
338 #ifdef HAVE_LIBSERIALPORT
343 * Both parameters are copied to newly allocated strings, and freed
344 * automatically by sr_serial_dev_inst_free().
346 * @param[in] port OS-specific serial port specification. Examples:
347 * "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1".
348 * @param[in] serialcomm A serial communication parameters string, in the form
349 * of \<speed\>/\<data bits\>\<parity\>\<stopbits\>, for example
350 * "9600/8n1" or "600/7o2". This is an optional parameter;
351 * it may be filled in later.
353 * @return A pointer to a newly initialized struct sr_serial_dev_inst,
356 SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
357 const char *serialcomm)
359 struct sr_serial_dev_inst *serial;
362 sr_err("Serial port required.");
366 if (!(serial = g_try_malloc0(sizeof(struct sr_serial_dev_inst)))) {
367 sr_err("Serial device instance malloc failed.");
371 serial->port = g_strdup(port);
373 serial->serialcomm = g_strdup(serialcomm);
379 * Free struct sr_serial_dev_inst * allocated by sr_serial_dev_inst().
380 * \param serial struct sr_serial_dev_inst * to free. Must not be NULL.
382 SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
384 g_free(serial->port);
385 g_free(serial->serialcomm);
390 SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device)
392 struct sr_usbtmc_dev_inst *usbtmc;
395 sr_err("Device name required.");
399 if (!(usbtmc = g_try_malloc0(sizeof(struct sr_usbtmc_dev_inst)))) {
400 sr_err("USBTMC device instance malloc failed.");
404 usbtmc->device = g_strdup(device);
410 SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc)
412 g_free(usbtmc->device);
417 * Get the list of devices/instances of the specified driver.
419 * @param driver The driver to use. Must not be NULL.
421 * @return The list of devices/instances of this driver, or NULL upon errors
422 * or if the list is empty.
426 SR_API GSList *sr_dev_list(const struct sr_dev_driver *driver)
428 if (driver && driver->dev_list)
429 return driver->dev_list();
435 * Clear all devices/instances of the specified driver.
437 * @param driver The driver to use. Must not be NULL.
439 * @return SR_OK upon success, a negative error code upon errors.
443 SR_API int sr_dev_clear(const struct sr_dev_driver *driver)
445 if (driver && driver->dev_clear)
446 return driver->dev_clear();
452 * Open the specified device.
454 * @param sdi Device instance to use. Must not be NULL.
456 * @return SR_OK upon success, a negative error code upon errors.
460 SR_API int sr_dev_open(struct sr_dev_inst *sdi)
464 if (!sdi || !sdi->driver || !sdi->driver->dev_open)
467 ret = sdi->driver->dev_open(sdi);
473 * Close the specified device.
475 * @param sdi Device instance to use. Must not be NULL.
477 * @return SR_OK upon success, a negative error code upon errors.
481 SR_API int sr_dev_close(struct sr_dev_inst *sdi)
485 if (!sdi || !sdi->driver || !sdi->driver->dev_close)
488 ret = sdi->driver->dev_close(sdi);