2 * This file is part of the sigrok project.
4 * Copyright (C) 2010-2012 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/>.
23 #include "sigrok-internal.h"
25 static GSList *devs = NULL;
28 * Scan the system for attached logic analyzers / devices.
30 * This will try to autodetect all supported logic analyzer devices:
32 * - Those attached via USB (can be reliably detected via USB VID/PID).
34 * - Those using a (real or virtual) serial port (detected by sending
35 * device-specific commands to all OS-specific serial port devices such
36 * as /dev/ttyS*, /dev/ttyUSB*, /dev/ttyACM*, and others).
37 * The autodetection for this kind of devices can potentially be unreliable.
39 * Also, sending various bytes/commands to (all!) devices which happen to
40 * be attached to the system via a (real or virtual) serial port can be
41 * problematic. There is no way for libsigrok to know how unknown devices
42 * react to the bytes libsigrok sends. Potentially they could lead to the
43 * device getting into invalid/error states, losing/overwriting data, or...
45 * In addition to the detection, the devices that are found are also
46 * initialized automatically. On some devices, this involves a firmware upload,
47 * or other such measures.
49 * The order in which the system is scanned for devices is not specified. The
50 * caller should not assume or rely on any specific order.
52 * After the system has been scanned for devices, the list of detected (and
53 * supported) devices can be acquired via sr_dev_list().
56 * TODO: Option to only scan for specific devices or device classes.
58 * @return SR_OK upon success, SR_ERR upon errors.
60 SR_API int sr_dev_scan(void)
63 struct sr_dev_plugin **plugins;
65 plugins = sr_hw_list();
67 sr_err("dev: %s: no supported devices/hwplugins", __func__);
68 return SR_ERR; /* TODO: More specific error? */
72 * Initialize all plugins first. Since the init() call may involve
73 * a firmware upload and associated delay, we may as well get all
74 * of these out of the way first.
76 for (i = 0; plugins[i]; i++)
77 sr_hw_init(plugins[i]);
83 * Return the list of logic analyzer devices libsigrok has detected.
85 * If the libsigrok-internal device list is empty, a scan for attached
86 * devices -- via a call to sr_dev_scan() -- is performed first.
88 * TODO: Error handling?
90 * @return The list (GSList) of detected devices, or NULL if none were found.
92 SR_API GSList *sr_dev_list(void)
101 * Create a new device.
103 * The device is added to the (libsigrok-internal) list of devices, but
104 * additionally a pointer to the newly created device is also returned.
106 * The device has no probes attached to it yet after this call. You can
107 * use sr_dev_probe_add() to add one or more probes.
109 * TODO: Should return int, so that we can return SR_OK, SR_ERR_* etc.
111 * It is the caller's responsibility to g_free() the allocated memory when
112 * no longer needed. TODO: Using which API function?
114 * @param plugin TODO.
115 * If 'plugin' is NULL, the created device is a "virtual" one.
116 * @param plugin_index TODO
118 * @return Pointer to the newly allocated device, or NULL upon errors.
120 SR_API struct sr_dev *sr_dev_new(const struct sr_dev_plugin *plugin,
125 /* TODO: Check if plugin_index valid? */
127 if (!(dev = g_try_malloc0(sizeof(struct sr_dev)))) {
128 sr_err("dev: %s: dev malloc failed", __func__);
132 dev->plugin = (struct sr_dev_plugin *)plugin;
133 dev->plugin_index = plugin_index;
134 devs = g_slist_append(devs, dev);
140 * Add a probe with the specified name to the specified device.
142 * The added probe is automatically enabled (the 'enabled' field is TRUE).
144 * The 'trigger' field of the added probe is set to NULL. A trigger can be
145 * added via sr_dev_trigger_set().
147 * TODO: Are duplicate names allowed?
148 * TODO: Do we enforce a maximum probe number for a device?
149 * TODO: Error if the max. probe number for the specific LA is reached, e.g.
150 * if the caller tries to add more probes than the device actually has.
152 * @param dev The device to which to add a probe with the specified name.
154 * @param name The name of the probe to add to this device. Must not be NULL.
155 * TODO: Maximum length, allowed characters, etc.
157 * @return SR_OK upon success, SR_ERR_MALLOC upon memory allocation errors,
158 * or SR_ERR_ARG upon invalid arguments.
159 * If something other than SR_OK is returned, 'dev' is unchanged.
161 SR_API int sr_dev_probe_add(struct sr_dev *dev, const char *name)
167 sr_err("dev: %s: dev was NULL", __func__);
172 sr_err("dev: %s: name was NULL", __func__);
176 /* TODO: Further checks to ensure name is valid. */
178 probenum = g_slist_length(dev->probes) + 1;
180 if (!(p = g_try_malloc0(sizeof(struct sr_probe)))) {
181 sr_err("dev: %s: p malloc failed", __func__);
182 return SR_ERR_MALLOC;
187 p->name = g_strdup(name);
189 dev->probes = g_slist_append(dev->probes, p);
195 * Find the probe with the specified number in the specified device.
199 * @param dev TODO. Must not be NULL.
200 * @param probenum The number of the probe whose 'struct sr_probe' we want.
201 * Note that the probe numbers start at 1 (not 0!).
203 * TODO: Should return int.
204 * TODO: probenum should be unsigned.
206 * @return A pointer to the requested probe's 'struct sr_probe', or NULL
207 * if the probe could not be found.
209 SR_API struct sr_probe *sr_dev_probe_find(const struct sr_dev *dev,
213 struct sr_probe *p, *found_probe;
216 sr_err("dev: %s: dev was NULL", __func__);
217 return NULL; /* TODO: SR_ERR_ARG */
220 /* TODO: Sanity check on probenum. */
223 for (l = dev->probes; l; l = l->next) {
225 /* TODO: Check for p != NULL. */
226 if (p->index == probenum) {
236 * Set the name of the specified probe in the specified device.
238 * If the probe already has a different name assigned to it, it will be
239 * removed, and the new name will be saved instead.
241 * TODO: Rename to sr_dev_probe_name_set().
244 * @param probenum The number of the probe whose name to set.
245 * Note that the probe numbers start at 1 (not 0!).
246 * @param name The new name that the specified probe should get.
248 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or SR_ERR
250 * If something other than SR_OK is returned, 'dev' is unchanged.
252 SR_API int sr_dev_probe_name(struct sr_dev *dev, int probenum,
258 sr_err("dev: %s: dev was NULL", __func__);
262 p = sr_dev_probe_find(dev, probenum);
264 sr_err("dev: %s: probe %d not found", __func__, probenum);
265 return SR_ERR; /* TODO: More specific error? */
268 /* TODO: Sanity check on 'name'. */
270 /* If the probe already has a name, kill it first. */
273 p->name = g_strdup(name);
279 * Remove all triggers set up for the specified device.
281 * TODO: Better description.
285 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
286 * If something other than SR_OK is returned, 'dev' is unchanged.
288 SR_API int sr_dev_trigger_clear(struct sr_dev *dev)
291 unsigned int pnum; /* TODO: uint16_t? */
294 sr_err("dev: %s: dev was NULL", __func__);
299 sr_err("dev: %s: dev->probes was NULL", __func__);
303 for (pnum = 1; pnum <= g_slist_length(dev->probes); pnum++) {
304 p = sr_dev_probe_find(dev, pnum);
305 /* TODO: Silently ignore probes which cannot be found? */
316 * Add a trigger to the specified device.
318 * TODO: Better description.
319 * TODO: Describe valid format of the 'trigger' string.
321 * @param dev TODO. Must not be NULL.
322 * @param probenum The number of the probe. TODO.
323 * Note that the probe numbers start at 1 (not 0!).
324 * @param trigger TODO.
325 * TODO: Is NULL allowed?
327 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or SR_ERR
329 * If something other than SR_OK is returned, 'dev' is unchanged.
331 SR_API int sr_dev_trigger_set(struct sr_dev *dev, int probenum,
337 sr_err("dev: %s: dev was NULL", __func__);
341 /* TODO: Sanity check on 'probenum'. */
343 /* TODO: Sanity check on 'trigger'. */
345 p = sr_dev_probe_find(dev, probenum);
347 sr_err("dev: %s: probe %d not found", __func__, probenum);
348 return SR_ERR; /* TODO: More specific error? */
351 /* If the probe already has a trigger, kill it first. */
354 p->trigger = g_strdup(trigger);
360 * Determine whether the specified device has the specified capability.
362 * TODO: Should return int?
364 * @param dev Pointer to the device to be checked. Must not be NULL.
365 * The device's 'plugin' field must not be NULL either.
366 * @param hwcap The capability that should be checked (whether it's supported
367 * by the specified device).
369 * @return TRUE, if the device has the specified capability, FALSE otherwise.
370 * FALSE is also returned upon invalid input parameters or other
373 SR_API gboolean sr_dev_has_hwcap(const struct sr_dev *dev, int hwcap)
378 sr_err("dev: %s: dev was NULL", __func__);
379 return FALSE; /* TODO: SR_ERR_ARG. */
383 sr_err("dev: %s: dev->plugin was NULL", __func__);
384 return FALSE; /* TODO: SR_ERR_ARG. */
387 /* TODO: Sanity check on 'hwcap'. */
389 if (!(hwcaps = dev->plugin->hwcap_get_all())) {
390 sr_err("dev: %s: dev has no capabilities", __func__);
391 return FALSE; /* TODO: SR_ERR*. */
394 for (i = 0; hwcaps[i]; i++) {
395 if (hwcaps[i] != hwcap)
397 sr_spew("dev: %s: found hwcap %d", __func__, hwcap);
401 sr_spew("dev: %s: hwcap %d not found", __func__, hwcap);
407 * Returns information about the given device.
409 * @param dev Pointer to the device to be checked. Must not be NULL.
410 * The device's 'plugin' field must not be NULL either.
411 * @param id The type of information.
412 * @param data The return value. Must not be NULL.
414 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or SR_ERR
417 SR_API int sr_dev_info_get(const struct sr_dev *dev, int id, const void **data)
419 if ((dev == NULL) || (dev->plugin == NULL))
425 *data = dev->plugin->dev_info_get(dev->plugin_index, id);