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