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