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