]> sigrok.org Git - libsigrok.git/blame - device.c
Move most contrib/ stuff to libsigrok.
[libsigrok.git] / device.c
CommitLineData
a1bb33af
UH
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2010 Bert Vermeulen <bert@biot.com>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20#include <stdio.h>
21#include <glib.h>
b7f09cf8
UH
22#include "sigrok.h"
23#include "sigrok-internal.h"
a1bb33af 24
a0ecd83b 25static GSList *devices = NULL;
a1bb33af 26
94799bc4
UH
27/**
28 * Scan the system for attached logic analyzers / devices.
29 *
30 * This will try to autodetect all supported logic analyzer devices:
31 *
32 * - Those attached via USB (can be reliably detected via USB VID/PID).
33 *
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.
38 *
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...
44 *
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.
48 *
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.
51 *
52 * After the system has been scanned for devices, the list of detected (and
53 * supported) devices can be acquired via sr_device_list().
54 *
94799bc4
UH
55 * TODO: Error checks?
56 * TODO: Option to only scan for specific devices or device classes.
0e3b1439
UH
57 *
58 * @return SR_OK upon success, SR_ERR upon errors.
94799bc4 59 */
0e3b1439 60int sr_device_scan(void)
a1bb33af
UH
61{
62 GSList *plugins, *l;
5c2d46d1 63 struct sr_device_plugin *plugin;
a1bb33af 64
94799bc4
UH
65 if (!(plugins = sr_list_hwplugins())) {
66 sr_err("dev: %s: no supported devices/hwplugins", __func__);
0e3b1439 67 return SR_ERR; /* TODO: More specific error? */
94799bc4 68 }
a1bb33af 69
1b452b85
UH
70 /*
71 * Initialize all plugins first. Since the init() call may involve
a1bb33af
UH
72 * a firmware upload and associated delay, we may as well get all
73 * of these out of the way first.
74 */
1b452b85 75 for (l = plugins; l; l = l->next) {
a1bb33af 76 plugin = l->data;
94799bc4 77 /* TODO: Handle 'plugin' being NULL. */
8722c31e 78 sr_init_hwplugins(plugin);
e54bcdc5 79 }
0e3b1439
UH
80
81 return SR_OK;
a1bb33af
UH
82}
83
94799bc4
UH
84/**
85 * Return the list of logic analyzer devices libsigrok has detected.
86 *
87 * If the libsigrok-internal device list is empty, a scan for attached
88 * devices -- via a call to sr_device_scan() -- is performed first.
89 *
90 * TODO: Error handling?
91 *
92 * @return The list (GSList) of detected devices, or NULL if none were found.
93 */
2bf4aca6 94GSList *sr_device_list(void)
a1bb33af 95{
e54bcdc5 96 if (!devices)
2bf4aca6 97 sr_device_scan();
e54bcdc5 98
a1bb33af
UH
99 return devices;
100}
101
94799bc4
UH
102/**
103 * Create a new device.
104 *
c37d2b1b
UH
105 * The device is added to the (libsigrok-internal) list of devices, but
106 * additionally a pointer to the newly created device is also returned.
107 *
108 * The device has no probes attached to it yet after this call. You can
109 * use sr_device_probe_add() to add one or more probes.
110 *
94799bc4
UH
111 * TODO: Should return int, so that we can return SR_OK, SR_ERR_* etc.
112 *
113 * It is the caller's responsibility to g_free() the allocated memory when
114 * no longer needed. TODO: Using which API function?
115 *
116 * @param plugin TODO.
117 * If 'plugin' is NULL, the created device is a "virtual" one.
118 * @param plugin_index TODO
94799bc4
UH
119 *
120 * @return Pointer to the newly allocated device, or NULL upon errors.
121 */
08d4cc1d 122struct sr_device *sr_device_new(const struct sr_device_plugin *plugin,
464d12c7 123 int plugin_index)
a1bb33af 124{
5c2d46d1 125 struct sr_device *device;
94799bc4
UH
126
127 /* TODO: Check if plugin_index valid? */
128
b53738ba
UH
129 if (!(device = g_try_malloc0(sizeof(struct sr_device)))) {
130 sr_err("dev: %s: device malloc failed", __func__);
131 return NULL;
132 }
133
08d4cc1d 134 device->plugin = (struct sr_device_plugin *)plugin;
a1bb33af
UH
135 device->plugin_index = plugin_index;
136 devices = g_slist_append(devices, device);
137
a1bb33af
UH
138 return device;
139}
140
94799bc4
UH
141/**
142 * Clear all probes of the specified device.
143 *
144 * This removes/clears the 'name' and 'trigger' fields of all probes of
145 * the device.
146 *
147 * The order in which the probes are cleared is not specified. The caller
148 * should not assume or rely on a specific order.
149 *
94799bc4
UH
150 * TODO: Rename to sr_device_clear_probes() or sr_device_probe_clear_all().
151 *
152 * @param device The device whose probes to clear. Must not be NULL.
153 * Note: device->probes is allowed to be NULL (in that case,
154 * there are no probes, thus none have to be cleared).
0e3b1439
UH
155 *
156 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
157 * If something other than SR_OK is returned, 'device' is unchanged.
94799bc4 158 */
0e3b1439 159int sr_device_clear(struct sr_device *device)
a1bb33af 160{
1b452b85 161 unsigned int pnum;
a1bb33af 162
94799bc4
UH
163 if (!device) {
164 sr_err("dev: %s: device was NULL", __func__);
0e3b1439 165 return SR_ERR_ARG;
94799bc4
UH
166 }
167
168 /* Note: device->probes can be NULL, this is handled correctly. */
a1bb33af 169
1b452b85 170 for (pnum = 1; pnum <= g_slist_length(device->probes); pnum++)
2bf4aca6 171 sr_device_probe_clear(device, pnum);
94799bc4 172
0e3b1439 173 return SR_OK;
a1bb33af
UH
174}
175
94799bc4
UH
176/**
177 * Clear the specified probe in the specified device.
178 *
179 * The probe itself still exists afterwards, but its 'name' and 'trigger'
180 * fields are g_free()'d and set to NULL.
181 *
94799bc4
UH
182 * @param device The device in which the specified (to be cleared) probe
183 * resides. Must not be NULL.
184 * @param probenum The number of the probe to clear.
185 * Note that the probe numbers start at 1 (not 0!).
0e3b1439
UH
186 *
187 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or SR_ERR
188 * upon other errors.
189 * If something other than SR_OK is returned, 'device' is unchanged.
94799bc4 190 */
0e3b1439 191int sr_device_probe_clear(struct sr_device *device, int probenum)
a1bb33af 192{
1afe8989 193 struct sr_probe *p;
a1bb33af 194
94799bc4
UH
195 if (!device) {
196 sr_err("dev: %s: device was NULL", __func__);
0e3b1439 197 return SR_ERR_ARG;
94799bc4
UH
198 }
199
200 /* TODO: Sanity check on 'probenum'. */
201
202 if (!(p = sr_device_probe_find(device, probenum))) {
203 sr_err("dev: %s: probe %d not found", __func__, probenum);
0e3b1439 204 return SR_ERR; /* TODO: More specific error? */
94799bc4 205 }
a1bb33af 206
94799bc4 207 /* If the probe has a name, remove it. */
1b452b85 208 if (p->name) {
a1bb33af
UH
209 g_free(p->name);
210 p->name = NULL;
211 }
212
94799bc4 213 /* If the probe has a trigger, remove it. */
1b452b85 214 if (p->trigger) {
a1bb33af
UH
215 g_free(p->trigger);
216 p->trigger = NULL;
217 }
94799bc4 218
0e3b1439 219 return SR_OK;
a1bb33af
UH
220}
221
94799bc4
UH
222/**
223 * Add a probe with the specified name to the specified device.
224 *
225 * The added probe is automatically enabled (the 'enabled' field is TRUE).
226 *
227 * The 'trigger' field of the added probe is set to NULL. A trigger can be
228 * added via sr_device_trigger_set().
229 *
94799bc4
UH
230 * TODO: Are duplicate names allowed?
231 * TODO: Do we enforce a maximum probe number for a device?
232 * TODO: Error if the max. probe number for the specific LA is reached, e.g.
233 * if the caller tries to add more probes than the device actually has.
234 *
235 * @param device The device to which to add a probe with the specified name.
236 * Must not be NULL.
237 * @param name The name of the probe to add to this device. Must not be NULL.
238 * TODO: Maximum length, allowed characters, etc.
239 *
240 * @return SR_OK upon success, SR_ERR_MALLOC upon memory allocation errors,
241 * or SR_ERR_ARG upon invalid arguments.
242 * If something other than SR_OK is returned, 'device' is unchanged.
243 */
0e3b1439 244int sr_device_probe_add(struct sr_device *device, const char *name)
a1bb33af 245{
1afe8989 246 struct sr_probe *p;
7d658874 247 int probenum;
a1bb33af 248
94799bc4
UH
249 if (!device) {
250 sr_err("dev: %s: device was NULL", __func__);
0e3b1439 251 return SR_ERR_ARG;
94799bc4
UH
252 }
253
254 if (!name) {
255 sr_err("dev: %s: name was NULL", __func__);
0e3b1439 256 return SR_ERR_ARG;
94799bc4
UH
257 }
258
259 /* TODO: Further checks to ensure name is valid. */
260
7d658874 261 probenum = g_slist_length(device->probes) + 1;
b53738ba
UH
262
263 if (!(p = g_try_malloc0(sizeof(struct sr_probe)))) {
264 sr_err("dev: %s: p malloc failed", __func__);
0e3b1439 265 return SR_ERR_MALLOC;
b53738ba
UH
266 }
267
7d658874 268 p->index = probenum;
a1bb33af 269 p->enabled = TRUE;
c37d2b1b 270 p->name = g_strdup(name);
a1bb33af
UH
271 p->trigger = NULL;
272 device->probes = g_slist_append(device->probes, p);
94799bc4
UH
273
274 return SR_OK;
a1bb33af
UH
275}
276
94799bc4
UH
277/**
278 * Find the probe with the specified number in the specified device.
279 *
280 * TODO
281 *
282 * @param device TODO. Must not be NULL.
283 * @param probenum The number of the probe whose 'struct sr_probe' we want.
284 * Note that the probe numbers start at 1 (not 0!).
285 *
286 * TODO: Should return int.
94799bc4
UH
287 * TODO: probenum should be unsigned.
288 *
289 * @return A pointer to the requested probe's 'struct sr_probe', or NULL
290 * if the probe could not be found.
291 */
08d4cc1d
UH
292struct sr_probe *sr_device_probe_find(const struct sr_device *device,
293 int probenum)
a1bb33af
UH
294{
295 GSList *l;
1afe8989 296 struct sr_probe *p, *found_probe;
a1bb33af 297
94799bc4
UH
298 if (!device) {
299 sr_err("dev: %s: device was NULL", __func__);
300 return NULL; /* TODO: SR_ERR_ARG */
301 }
302
303 /* TODO: Sanity check on probenum. */
304
a1bb33af 305 found_probe = NULL;
1b452b85 306 for (l = device->probes; l; l = l->next) {
a1bb33af 307 p = l->data;
94799bc4 308 /* TODO: Check for p != NULL. */
1b452b85 309 if (p->index == probenum) {
a1bb33af
UH
310 found_probe = p;
311 break;
312 }
313 }
314
315 return found_probe;
316}
317
94799bc4
UH
318/**
319 * Set the name of the specified probe in the specified device.
320 *
321 * If the probe already has a different name assigned to it, it will be
322 * removed, and the new name will be saved instead.
323 *
94799bc4
UH
324 * TODO: Rename to sr_device_set_probe_name().
325 *
326 * @param device TODO
327 * @param probenum The number of the probe whose name to set.
328 * Note that the probe numbers start at 1 (not 0!).
329 * @param name The new name that the specified probe should get.
0e3b1439
UH
330 *
331 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or SR_ERR
332 * upon other errors.
333 * If something other than SR_OK is returned, 'device' is unchanged.
94799bc4 334 */
0e3b1439
UH
335int sr_device_probe_name(struct sr_device *device, int probenum,
336 const char *name)
a1bb33af 337{
1afe8989 338 struct sr_probe *p;
a1bb33af 339
94799bc4
UH
340 if (!device) {
341 sr_err("dev: %s: device was NULL", __func__);
0e3b1439 342 return SR_ERR_ARG;
94799bc4
UH
343 }
344
03dbc020 345 p = sr_device_probe_find(device, probenum);
94799bc4
UH
346 if (!p) {
347 sr_err("dev: %s: probe %d not found", __func__, probenum);
0e3b1439 348 return SR_ERR; /* TODO: More specific error? */
94799bc4
UH
349 }
350
351 /* TODO: Sanity check on 'name'. */
a1bb33af 352
94799bc4 353 /* If the probe already has a name, kill it first. */
1b452b85 354 if (p->name)
a1bb33af 355 g_free(p->name);
94799bc4 356
a1bb33af 357 p->name = g_strdup(name);
0e3b1439
UH
358
359 return SR_OK;
a1bb33af
UH
360}
361
94799bc4
UH
362/**
363 * Remove all triggers set up for the specified device.
364 *
365 * TODO: Better description.
366 *
94799bc4 367 * @param device TODO
0e3b1439
UH
368 *
369 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
370 * If something other than SR_OK is returned, 'device' is unchanged.
94799bc4 371 */
0e3b1439 372int sr_device_trigger_clear(struct sr_device *device)
a1bb33af 373{
1afe8989 374 struct sr_probe *p;
0e3b1439 375 unsigned int pnum; /* TODO: uint16_t? */
a1bb33af 376
94799bc4
UH
377 if (!device) {
378 sr_err("dev: %s: device was NULL", __func__);
0e3b1439 379 return SR_ERR_ARG;
94799bc4
UH
380 }
381
382 if (!device->probes) {
383 sr_err("dev: %s: device->probes was NULL", __func__);
0e3b1439 384 return SR_ERR_ARG;
94799bc4 385 }
a1bb33af 386
1b452b85 387 for (pnum = 1; pnum <= g_slist_length(device->probes); pnum++) {
03dbc020 388 p = sr_device_probe_find(device, pnum);
94799bc4 389 /* TODO: Silently ignore probes which cannot be found? */
1b452b85
UH
390 if (p && p->trigger) {
391 g_free(p->trigger);
392 p->trigger = NULL;
393 }
394 }
0e3b1439
UH
395
396 return SR_OK;
1b452b85 397}
a1bb33af 398
94799bc4
UH
399/**
400 * Add a trigger to the specified device.
401 *
402 * TODO: Better description.
403 * TODO: Describe valid format of the 'trigger' string.
404 *
94799bc4
UH
405 * @param device TODO. Must not be NULL.
406 * @param probenum The number of the probe. TODO.
407 * Note that the probe numbers start at 1 (not 0!).
408 * @param trigger TODO.
409 * TODO: Is NULL allowed?
0e3b1439
UH
410 *
411 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or SR_ERR
412 * upon other errors.
413 * If something other than SR_OK is returned, 'device' is unchanged.
94799bc4 414 */
0e3b1439
UH
415int sr_device_trigger_set(struct sr_device *device, int probenum,
416 const char *trigger)
a1bb33af 417{
1afe8989 418 struct sr_probe *p;
a1bb33af 419
94799bc4
UH
420 if (!device) {
421 sr_err("dev: %s: device was NULL", __func__);
0e3b1439 422 return SR_ERR_ARG;
94799bc4
UH
423 }
424
425 /* TODO: Sanity check on 'probenum'. */
426
427 /* TODO: Sanity check on 'trigger'. */
428
03dbc020 429 p = sr_device_probe_find(device, probenum);
94799bc4
UH
430 if (!p) {
431 sr_err("dev: %s: probe %d not found", __func__, probenum);
0e3b1439 432 return SR_ERR; /* TODO: More specific error? */
94799bc4 433 }
a1bb33af 434
94799bc4 435 /* If the probe already has a trigger, kill it first. */
1b452b85 436 if (p->trigger)
a1bb33af
UH
437 g_free(p->trigger);
438
439 p->trigger = g_strdup(trigger);
0e3b1439
UH
440
441 return SR_OK;
7d658874
BV
442}
443
94799bc4
UH
444/**
445 * Determine whether the specified device has the specified capability.
446 *
447 * TODO: Should return int?
94799bc4
UH
448 *
449 * @param device Pointer to the device to be checked. Must not be NULL.
450 * The device's 'plugin' field must not be NULL either.
451 * @param hwcap The capability that should be checked (whether it's supported
452 * by the specified device).
453 *
454 * @return TRUE, if the device has the specified capability, FALSE otherwise.
455 * FALSE is also returned upon invalid input parameters or other
456 * error conditions.
457 */
08d4cc1d 458gboolean sr_device_has_hwcap(const struct sr_device *device, int hwcap)
7d658874
BV
459{
460 int *capabilities, i;
461
94799bc4
UH
462 if (!device) {
463 sr_err("dev: %s: device was NULL", __func__);
464 return FALSE; /* TODO: SR_ERR_ARG. */
465 }
466
467 if (!device->plugin) {
468 sr_err("dev: %s: device->plugin was NULL", __func__);
469 return FALSE; /* TODO: SR_ERR_ARG. */
470 }
471
472 /* TODO: Sanity check on 'hwcap'. */
473
474 if (!(capabilities = device->plugin->get_capabilities())) {
475 sr_err("dev: %s: device has no capabilities", __func__);
476 return FALSE; /* TODO: SR_ERR*. */
477 }
478
479 for (i = 0; capabilities[i]; i++) {
480 if (capabilities[i] != hwcap)
481 continue;
482 sr_spew("dev: %s: found hwcap %d", __func__, hwcap);
483 return TRUE;
484 }
218557b8 485
94799bc4 486 sr_spew("dev: %s: hwcap %d not found", __func__, hwcap);
7d658874
BV
487
488 return FALSE;
a1bb33af 489}