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 SR_PRIV struct sr_probe *sr_probe_new(int index, int type,
51 gboolean enabled, const char *name)
53 struct sr_probe *probe;
55 if (!(probe = g_try_malloc0(sizeof(struct sr_probe)))) {
56 sr_err("Probe malloc failed.");
62 probe->enabled = enabled;
64 probe->name = g_strdup(name);
70 * Set the name of the specified probe in the specified device.
72 * If the probe already has a different name assigned to it, it will be
73 * removed, and the new name will be saved instead.
75 * @param sdi The device instance the probe is connected to.
76 * @param probenum The number of the probe whose name to set.
77 * Note that the probe numbers start at 0.
78 * @param name The new name that the specified probe should get. A copy
79 * of the string is made.
81 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
85 SR_API int sr_dev_probe_name_set(const struct sr_dev_inst *sdi,
86 int probenum, const char *name)
89 struct sr_probe *probe;
93 sr_err("%s: sdi was NULL", __func__);
98 for (l = sdi->probes; l; l = l->next) {
100 if (probe->index == probenum) {
102 probe->name = g_strdup(name);
112 * Enable or disable a probe on the specified device.
114 * @param sdi The device instance the probe is connected to.
115 * @param probenum The probe number, starting from 0.
116 * @param state TRUE to enable the probe, FALSE to disable.
118 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
122 SR_API int sr_dev_probe_enable(const struct sr_dev_inst *sdi, int probenum,
126 struct sr_probe *probe;
133 for (l = sdi->probes; l; l = l->next) {
135 if (probe->index == probenum) {
136 probe->enabled = state;
146 * Add a trigger to the specified device (and the specified probe).
148 * If the specified probe of this device already has a trigger, it will
149 * be silently replaced.
151 * @param sdi Must not be NULL.
152 * @param probenum The probe number, starting from 0.
153 * @param trigger Trigger string, in the format used by sigrok-cli
155 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
159 SR_API int sr_dev_trigger_set(const struct sr_dev_inst *sdi, int probenum,
163 struct sr_probe *probe;
170 for (l = sdi->probes; l; l = l->next) {
172 if (probe->index == probenum) {
173 /* If the probe already has a trigger, kill it first. */
174 g_free(probe->trigger);
175 probe->trigger = g_strdup(trigger);
185 * Determine whether the specified device instance has the specified
188 * @param sdi Pointer to the device instance to be checked. Must not be NULL.
189 * If the device's 'driver' field is NULL (virtual device), this
190 * function will always return FALSE (virtual devices don't have
191 * a hardware capabilities list).
192 * @param key The option that should be checked for support on the
195 * @return TRUE if the device has the specified option, FALSE otherwise.
196 * FALSE is also returned on invalid input parameters or other
201 SR_API gboolean sr_dev_has_option(const struct sr_dev_inst *sdi, int key)
208 if (!sdi || !sdi->driver || !sdi->driver->config_list)
211 if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS,
212 &gvar, NULL, NULL) != SR_OK)
216 devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t));
217 for (i = 0; i < num_opts; i++) {
218 if (devopts[i] == key) {
223 g_variant_unref(gvar);
229 SR_PRIV struct sr_dev_inst *sr_dev_inst_new(int index, int status,
230 const char *vendor, const char *model, const char *version)
232 struct sr_dev_inst *sdi;
234 if (!(sdi = g_try_malloc(sizeof(struct sr_dev_inst)))) {
235 sr_err("Device instance malloc failed.");
241 sdi->status = status;
243 sdi->vendor = vendor ? g_strdup(vendor) : NULL;
244 sdi->model = model ? g_strdup(model) : NULL;
245 sdi->version = version ? g_strdup(version) : NULL;
247 sdi->probe_groups = NULL;
255 SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
257 struct sr_probe *probe;
260 for (l = sdi->probes; l; l = l->next) {
263 g_free(probe->trigger);
266 g_slist_free(sdi->probes);
268 if (sdi->probe_groups)
269 g_slist_free(sdi->probe_groups);
273 g_free(sdi->version);
277 #ifdef HAVE_LIBUSB_1_0
280 SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
281 uint8_t address, struct libusb_device_handle *hdl)
283 struct sr_usb_dev_inst *udi;
285 if (!(udi = g_try_malloc(sizeof(struct sr_usb_dev_inst)))) {
286 sr_err("USB device instance malloc failed.");
291 udi->address = address;
298 SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
305 #ifdef HAVE_LIBSERIALPORT
310 * Both parameters are copied to newly allocated strings, and freed
311 * automatically by sr_serial_dev_inst_free().
313 * @param pathname OS-specific serial port specification. Examples:
314 * "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1".
315 * @param serialcomm A serial communication parameters string, in the form
316 * of <speed>/<data bits><parity><stopbits>, for example
317 * "9600/8n1" or "600/7o2". This is an optional parameter;
318 * it may be filled in later.
320 * @return A pointer to a newly initialized struct sr_serial_dev_inst,
323 SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
324 const char *serialcomm)
326 struct sr_serial_dev_inst *serial;
329 sr_err("Serial port required.");
333 if (!(serial = g_try_malloc0(sizeof(struct sr_serial_dev_inst)))) {
334 sr_err("Serial device instance malloc failed.");
338 serial->port = g_strdup(port);
340 serial->serialcomm = g_strdup(serialcomm);
347 SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
349 g_free(serial->port);
350 g_free(serial->serialcomm);
355 SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device)
357 struct sr_usbtmc_dev_inst *usbtmc;
360 sr_err("Device name required.");
364 if (!(usbtmc = g_try_malloc0(sizeof(struct sr_usbtmc_dev_inst)))) {
365 sr_err("USBTMC device instance malloc failed.");
369 usbtmc->device = g_strdup(device);
375 SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc)
377 g_free(usbtmc->device);
382 * Get the list of devices/instances of the specified driver.
384 * @param driver The driver to use. Must not be NULL.
386 * @return The list of devices/instances of this driver, or NULL upon errors
387 * or if the list is empty.
391 SR_API GSList *sr_dev_list(const struct sr_dev_driver *driver)
393 if (driver && driver->dev_list)
394 return driver->dev_list();
400 * Clear all devices/instances of the specified driver.
402 * @param driver The driver to use. Must not be NULL.
404 * @return SR_OK upon success, a negative error code upon errors.
408 SR_API int sr_dev_clear(const struct sr_dev_driver *driver)
410 if (driver && driver->dev_clear)
411 return driver->dev_clear();
417 * Open the specified device.
419 * @param sdi Device instance to use. Must not be NULL.
421 * @return SR_OK upon success, a negative error code upon errors.
425 SR_API int sr_dev_open(struct sr_dev_inst *sdi)
429 if (!sdi || !sdi->driver || !sdi->driver->dev_open)
432 ret = sdi->driver->dev_open(sdi);
438 * Close the specified device.
440 * @param sdi Device instance to use. Must not be NULL.
442 * @return SR_OK upon success, a negative error code upon errors.
446 SR_API int sr_dev_close(struct sr_dev_inst *sdi)
450 if (!sdi || !sdi->driver || !sdi->driver->dev_close)
453 ret = sdi->driver->dev_close(sdi);