]> sigrok.org Git - libsigrok.git/blame - device.c
Make more variables/functions static and non-global.
[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>
1b452b85 22#include <sigrok.h>
b08024a8 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 *
94799bc4
UH
105 * TODO: num_probes should be uint16_t.
106 * TODO: Should return int, so that we can return SR_OK, SR_ERR_* etc.
107 *
108 * It is the caller's responsibility to g_free() the allocated memory when
109 * no longer needed. TODO: Using which API function?
110 *
111 * @param plugin TODO.
112 * If 'plugin' is NULL, the created device is a "virtual" one.
113 * @param plugin_index TODO
114 * @param num_probes The number of probes (>= 1) this device has.
115 * TODO: 0 allowed?
116 *
117 * @return Pointer to the newly allocated device, or NULL upon errors.
118 */
08d4cc1d
UH
119struct sr_device *sr_device_new(const struct sr_device_plugin *plugin,
120 int plugin_index, int num_probes)
a1bb33af 121{
5c2d46d1 122 struct sr_device *device;
873080cc 123 int i;
a1bb33af 124
94799bc4
UH
125 if (!plugin) {
126 sr_err("dev: %s: plugin was NULL", __func__);
127 return NULL; /* TODO: SR_ERR_ARG */
128 }
129
130 /* TODO: Check if plugin_index valid? */
131
132 /* TODO: Check if num_probes valid? */
133
b53738ba
UH
134 if (!(device = g_try_malloc0(sizeof(struct sr_device)))) {
135 sr_err("dev: %s: device malloc failed", __func__);
136 return NULL;
137 }
138
08d4cc1d 139 device->plugin = (struct sr_device_plugin *)plugin;
a1bb33af
UH
140 device->plugin_index = plugin_index;
141 devices = g_slist_append(devices, device);
142
7d658874 143 for (i = 0; i < num_probes; i++)
94799bc4 144 sr_device_probe_add(device, NULL); /* TODO: Check return value. */
a1bb33af
UH
145
146 return device;
147}
148
94799bc4
UH
149/**
150 * Clear all probes of the specified device.
151 *
152 * This removes/clears the 'name' and 'trigger' fields of all probes of
153 * the device.
154 *
155 * The order in which the probes are cleared is not specified. The caller
156 * should not assume or rely on a specific order.
157 *
94799bc4
UH
158 * TODO: Rename to sr_device_clear_probes() or sr_device_probe_clear_all().
159 *
160 * @param device The device whose probes to clear. Must not be NULL.
161 * Note: device->probes is allowed to be NULL (in that case,
162 * there are no probes, thus none have to be cleared).
0e3b1439
UH
163 *
164 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
165 * If something other than SR_OK is returned, 'device' is unchanged.
94799bc4 166 */
0e3b1439 167int sr_device_clear(struct sr_device *device)
a1bb33af 168{
1b452b85 169 unsigned int pnum;
a1bb33af 170
94799bc4
UH
171 if (!device) {
172 sr_err("dev: %s: device was NULL", __func__);
0e3b1439 173 return SR_ERR_ARG;
94799bc4
UH
174 }
175
176 /* Note: device->probes can be NULL, this is handled correctly. */
a1bb33af 177
1b452b85 178 for (pnum = 1; pnum <= g_slist_length(device->probes); pnum++)
2bf4aca6 179 sr_device_probe_clear(device, pnum);
94799bc4 180
0e3b1439 181 return SR_OK;
a1bb33af
UH
182}
183
94799bc4
UH
184/**
185 * Clear the specified probe in the specified device.
186 *
187 * The probe itself still exists afterwards, but its 'name' and 'trigger'
188 * fields are g_free()'d and set to NULL.
189 *
94799bc4
UH
190 * @param device The device in which the specified (to be cleared) probe
191 * resides. Must not be NULL.
192 * @param probenum The number of the probe to clear.
193 * Note that the probe numbers start at 1 (not 0!).
0e3b1439
UH
194 *
195 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or SR_ERR
196 * upon other errors.
197 * If something other than SR_OK is returned, 'device' is unchanged.
94799bc4 198 */
0e3b1439 199int sr_device_probe_clear(struct sr_device *device, int probenum)
a1bb33af 200{
1afe8989 201 struct sr_probe *p;
a1bb33af 202
94799bc4
UH
203 if (!device) {
204 sr_err("dev: %s: device was NULL", __func__);
0e3b1439 205 return SR_ERR_ARG;
94799bc4
UH
206 }
207
208 /* TODO: Sanity check on 'probenum'. */
209
210 if (!(p = sr_device_probe_find(device, probenum))) {
211 sr_err("dev: %s: probe %d not found", __func__, probenum);
0e3b1439 212 return SR_ERR; /* TODO: More specific error? */
94799bc4 213 }
a1bb33af 214
94799bc4 215 /* If the probe has a name, remove it. */
1b452b85 216 if (p->name) {
a1bb33af
UH
217 g_free(p->name);
218 p->name = NULL;
219 }
220
94799bc4 221 /* If the probe has a trigger, remove it. */
1b452b85 222 if (p->trigger) {
a1bb33af
UH
223 g_free(p->trigger);
224 p->trigger = NULL;
225 }
94799bc4 226
0e3b1439 227 return SR_OK;
a1bb33af
UH
228}
229
94799bc4
UH
230/**
231 * Add a probe with the specified name to the specified device.
232 *
233 * The added probe is automatically enabled (the 'enabled' field is TRUE).
234 *
235 * The 'trigger' field of the added probe is set to NULL. A trigger can be
236 * added via sr_device_trigger_set().
237 *
94799bc4
UH
238 * TODO: Are duplicate names allowed?
239 * TODO: Do we enforce a maximum probe number for a device?
240 * TODO: Error if the max. probe number for the specific LA is reached, e.g.
241 * if the caller tries to add more probes than the device actually has.
242 *
243 * @param device The device to which to add a probe with the specified name.
244 * Must not be NULL.
245 * @param name The name of the probe to add to this device. Must not be NULL.
246 * TODO: Maximum length, allowed characters, etc.
247 *
248 * @return SR_OK upon success, SR_ERR_MALLOC upon memory allocation errors,
249 * or SR_ERR_ARG upon invalid arguments.
250 * If something other than SR_OK is returned, 'device' is unchanged.
251 */
0e3b1439 252int sr_device_probe_add(struct sr_device *device, const char *name)
a1bb33af 253{
1afe8989 254 struct sr_probe *p;
94799bc4 255 char probename[16]; /* FIXME: Don't hardcode 16? #define? */
7d658874 256 int probenum;
a1bb33af 257
94799bc4
UH
258 if (!device) {
259 sr_err("dev: %s: device was NULL", __func__);
0e3b1439 260 return SR_ERR_ARG;
94799bc4
UH
261 }
262
263 if (!name) {
264 sr_err("dev: %s: name was NULL", __func__);
0e3b1439 265 return SR_ERR_ARG;
94799bc4
UH
266 }
267
268 /* TODO: Further checks to ensure name is valid. */
269
7d658874 270 probenum = g_slist_length(device->probes) + 1;
b53738ba
UH
271
272 if (!(p = g_try_malloc0(sizeof(struct sr_probe)))) {
273 sr_err("dev: %s: p malloc failed", __func__);
0e3b1439 274 return SR_ERR_MALLOC;
b53738ba
UH
275 }
276
7d658874 277 p->index = probenum;
a1bb33af 278 p->enabled = TRUE;
7d658874
BV
279 if (name) {
280 p->name = g_strdup(name);
281 } else {
282 snprintf(probename, 16, "%d", probenum);
283 p->name = g_strdup(probename);
284 }
a1bb33af
UH
285 p->trigger = NULL;
286 device->probes = g_slist_append(device->probes, p);
94799bc4
UH
287
288 return SR_OK;
a1bb33af
UH
289}
290
94799bc4
UH
291/**
292 * Find the probe with the specified number in the specified device.
293 *
294 * TODO
295 *
296 * @param device TODO. Must not be NULL.
297 * @param probenum The number of the probe whose 'struct sr_probe' we want.
298 * Note that the probe numbers start at 1 (not 0!).
299 *
300 * TODO: Should return int.
94799bc4
UH
301 * TODO: probenum should be unsigned.
302 *
303 * @return A pointer to the requested probe's 'struct sr_probe', or NULL
304 * if the probe could not be found.
305 */
08d4cc1d
UH
306struct sr_probe *sr_device_probe_find(const struct sr_device *device,
307 int probenum)
a1bb33af
UH
308{
309 GSList *l;
1afe8989 310 struct sr_probe *p, *found_probe;
a1bb33af 311
94799bc4
UH
312 if (!device) {
313 sr_err("dev: %s: device was NULL", __func__);
314 return NULL; /* TODO: SR_ERR_ARG */
315 }
316
317 /* TODO: Sanity check on probenum. */
318
a1bb33af 319 found_probe = NULL;
1b452b85 320 for (l = device->probes; l; l = l->next) {
a1bb33af 321 p = l->data;
94799bc4 322 /* TODO: Check for p != NULL. */
1b452b85 323 if (p->index == probenum) {
a1bb33af
UH
324 found_probe = p;
325 break;
326 }
327 }
328
329 return found_probe;
330}
331
94799bc4
UH
332/**
333 * Set the name of the specified probe in the specified device.
334 *
335 * If the probe already has a different name assigned to it, it will be
336 * removed, and the new name will be saved instead.
337 *
94799bc4
UH
338 * TODO: Rename to sr_device_set_probe_name().
339 *
340 * @param device TODO
341 * @param probenum The number of the probe whose name to set.
342 * Note that the probe numbers start at 1 (not 0!).
343 * @param name The new name that the specified probe should get.
0e3b1439
UH
344 *
345 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or SR_ERR
346 * upon other errors.
347 * If something other than SR_OK is returned, 'device' is unchanged.
94799bc4 348 */
0e3b1439
UH
349int sr_device_probe_name(struct sr_device *device, int probenum,
350 const char *name)
a1bb33af 351{
1afe8989 352 struct sr_probe *p;
a1bb33af 353
94799bc4
UH
354 if (!device) {
355 sr_err("dev: %s: device was NULL", __func__);
0e3b1439 356 return SR_ERR_ARG;
94799bc4
UH
357 }
358
03dbc020 359 p = sr_device_probe_find(device, probenum);
94799bc4
UH
360 if (!p) {
361 sr_err("dev: %s: probe %d not found", __func__, probenum);
0e3b1439 362 return SR_ERR; /* TODO: More specific error? */
94799bc4
UH
363 }
364
365 /* TODO: Sanity check on 'name'. */
a1bb33af 366
94799bc4 367 /* If the probe already has a name, kill it first. */
1b452b85 368 if (p->name)
a1bb33af 369 g_free(p->name);
94799bc4 370
a1bb33af 371 p->name = g_strdup(name);
0e3b1439
UH
372
373 return SR_OK;
a1bb33af
UH
374}
375
94799bc4
UH
376/**
377 * Remove all triggers set up for the specified device.
378 *
379 * TODO: Better description.
380 *
94799bc4 381 * @param device TODO
0e3b1439
UH
382 *
383 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
384 * If something other than SR_OK is returned, 'device' is unchanged.
94799bc4 385 */
0e3b1439 386int sr_device_trigger_clear(struct sr_device *device)
a1bb33af 387{
1afe8989 388 struct sr_probe *p;
0e3b1439 389 unsigned int pnum; /* TODO: uint16_t? */
a1bb33af 390
94799bc4
UH
391 if (!device) {
392 sr_err("dev: %s: device was NULL", __func__);
0e3b1439 393 return SR_ERR_ARG;
94799bc4
UH
394 }
395
396 if (!device->probes) {
397 sr_err("dev: %s: device->probes was NULL", __func__);
0e3b1439 398 return SR_ERR_ARG;
94799bc4 399 }
a1bb33af 400
1b452b85 401 for (pnum = 1; pnum <= g_slist_length(device->probes); pnum++) {
03dbc020 402 p = sr_device_probe_find(device, pnum);
94799bc4 403 /* TODO: Silently ignore probes which cannot be found? */
1b452b85
UH
404 if (p && p->trigger) {
405 g_free(p->trigger);
406 p->trigger = NULL;
407 }
408 }
0e3b1439
UH
409
410 return SR_OK;
1b452b85 411}
a1bb33af 412
94799bc4
UH
413/**
414 * Add a trigger to the specified device.
415 *
416 * TODO: Better description.
417 * TODO: Describe valid format of the 'trigger' string.
418 *
94799bc4
UH
419 * @param device TODO. Must not be NULL.
420 * @param probenum The number of the probe. TODO.
421 * Note that the probe numbers start at 1 (not 0!).
422 * @param trigger TODO.
423 * TODO: Is NULL allowed?
0e3b1439
UH
424 *
425 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or SR_ERR
426 * upon other errors.
427 * If something other than SR_OK is returned, 'device' is unchanged.
94799bc4 428 */
0e3b1439
UH
429int sr_device_trigger_set(struct sr_device *device, int probenum,
430 const char *trigger)
a1bb33af 431{
1afe8989 432 struct sr_probe *p;
a1bb33af 433
94799bc4
UH
434 if (!device) {
435 sr_err("dev: %s: device was NULL", __func__);
0e3b1439 436 return SR_ERR_ARG;
94799bc4
UH
437 }
438
439 /* TODO: Sanity check on 'probenum'. */
440
441 /* TODO: Sanity check on 'trigger'. */
442
03dbc020 443 p = sr_device_probe_find(device, probenum);
94799bc4
UH
444 if (!p) {
445 sr_err("dev: %s: probe %d not found", __func__, probenum);
0e3b1439 446 return SR_ERR; /* TODO: More specific error? */
94799bc4 447 }
a1bb33af 448
94799bc4 449 /* If the probe already has a trigger, kill it first. */
1b452b85 450 if (p->trigger)
a1bb33af
UH
451 g_free(p->trigger);
452
453 p->trigger = g_strdup(trigger);
0e3b1439
UH
454
455 return SR_OK;
7d658874
BV
456}
457
94799bc4
UH
458/**
459 * Determine whether the specified device has the specified capability.
460 *
461 * TODO: Should return int?
94799bc4
UH
462 *
463 * @param device Pointer to the device to be checked. Must not be NULL.
464 * The device's 'plugin' field must not be NULL either.
465 * @param hwcap The capability that should be checked (whether it's supported
466 * by the specified device).
467 *
468 * @return TRUE, if the device has the specified capability, FALSE otherwise.
469 * FALSE is also returned upon invalid input parameters or other
470 * error conditions.
471 */
08d4cc1d 472gboolean sr_device_has_hwcap(const struct sr_device *device, int hwcap)
7d658874
BV
473{
474 int *capabilities, i;
475
94799bc4
UH
476 if (!device) {
477 sr_err("dev: %s: device was NULL", __func__);
478 return FALSE; /* TODO: SR_ERR_ARG. */
479 }
480
481 if (!device->plugin) {
482 sr_err("dev: %s: device->plugin was NULL", __func__);
483 return FALSE; /* TODO: SR_ERR_ARG. */
484 }
485
486 /* TODO: Sanity check on 'hwcap'. */
487
488 if (!(capabilities = device->plugin->get_capabilities())) {
489 sr_err("dev: %s: device has no capabilities", __func__);
490 return FALSE; /* TODO: SR_ERR*. */
491 }
492
493 for (i = 0; capabilities[i]; i++) {
494 if (capabilities[i] != hwcap)
495 continue;
496 sr_spew("dev: %s: found hwcap %d", __func__, hwcap);
497 return TRUE;
498 }
218557b8 499
94799bc4 500 sr_spew("dev: %s: hwcap %d not found", __func__, hwcap);
7d658874
BV
501
502 return FALSE;
a1bb33af 503}