]>
Commit | Line | Data |
---|---|---|
a1bb33af UH |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
c73d2ea4 | 4 | * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com> |
a1bb33af UH |
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 | |
bb7ef793 | 25 | static GSList *devs = 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 | |
03168500 | 53 | * supported) devices can be acquired via sr_dev_list(). |
94799bc4 | 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 | */ |
03168500 | 60 | SR_API int sr_dev_scan(void) |
a1bb33af UH |
61 | { |
62 | GSList *plugins, *l; | |
bb7ef793 | 63 | struct sr_dev_plugin *plugin; |
a1bb33af | 64 | |
93a04e3b | 65 | if (!(plugins = sr_hw_list())) { |
94799bc4 | 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. */ |
93a04e3b | 78 | sr_hw_init(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 | |
03168500 | 88 | * devices -- via a call to sr_dev_scan() -- is performed first. |
94799bc4 UH |
89 | * |
90 | * TODO: Error handling? | |
91 | * | |
92 | * @return The list (GSList) of detected devices, or NULL if none were found. | |
93 | */ | |
03168500 | 94 | SR_API GSList *sr_dev_list(void) |
a1bb33af | 95 | { |
bb7ef793 | 96 | if (!devs) |
03168500 | 97 | sr_dev_scan(); |
e54bcdc5 | 98 | |
bb7ef793 | 99 | return devs; |
a1bb33af UH |
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 | |
03168500 | 109 | * use sr_dev_probe_add() to add one or more probes. |
c37d2b1b | 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 | */ | |
bb7ef793 UH |
122 | SR_API struct sr_dev *sr_dev_new(const struct sr_dev_plugin *plugin, |
123 | int plugin_index) | |
a1bb33af | 124 | { |
bb7ef793 | 125 | struct sr_dev *dev; |
94799bc4 UH |
126 | |
127 | /* TODO: Check if plugin_index valid? */ | |
128 | ||
bb7ef793 UH |
129 | if (!(dev = g_try_malloc0(sizeof(struct sr_dev)))) { |
130 | sr_err("dev: %s: dev malloc failed", __func__); | |
b53738ba UH |
131 | return NULL; |
132 | } | |
133 | ||
bb7ef793 UH |
134 | dev->plugin = (struct sr_dev_plugin *)plugin; |
135 | dev->plugin_index = plugin_index; | |
136 | devs = g_slist_append(devs, dev); | |
a1bb33af | 137 | |
bb7ef793 | 138 | return dev; |
a1bb33af UH |
139 | } |
140 | ||
94799bc4 UH |
141 | /** |
142 | * Add a probe with the specified name to the specified device. | |
143 | * | |
144 | * The added probe is automatically enabled (the 'enabled' field is TRUE). | |
145 | * | |
146 | * The 'trigger' field of the added probe is set to NULL. A trigger can be | |
03168500 | 147 | * added via sr_dev_trigger_set(). |
94799bc4 | 148 | * |
94799bc4 UH |
149 | * TODO: Are duplicate names allowed? |
150 | * TODO: Do we enforce a maximum probe number for a device? | |
151 | * TODO: Error if the max. probe number for the specific LA is reached, e.g. | |
152 | * if the caller tries to add more probes than the device actually has. | |
153 | * | |
bb7ef793 UH |
154 | * @param dev The device to which to add a probe with the specified name. |
155 | * Must not be NULL. | |
94799bc4 UH |
156 | * @param name The name of the probe to add to this device. Must not be NULL. |
157 | * TODO: Maximum length, allowed characters, etc. | |
158 | * | |
159 | * @return SR_OK upon success, SR_ERR_MALLOC upon memory allocation errors, | |
160 | * or SR_ERR_ARG upon invalid arguments. | |
bb7ef793 | 161 | * If something other than SR_OK is returned, 'dev' is unchanged. |
94799bc4 | 162 | */ |
bb7ef793 | 163 | SR_API int sr_dev_probe_add(struct sr_dev *dev, const char *name) |
a1bb33af | 164 | { |
1afe8989 | 165 | struct sr_probe *p; |
7d658874 | 166 | int probenum; |
a1bb33af | 167 | |
bb7ef793 UH |
168 | if (!dev) { |
169 | sr_err("dev: %s: dev was NULL", __func__); | |
0e3b1439 | 170 | return SR_ERR_ARG; |
94799bc4 UH |
171 | } |
172 | ||
173 | if (!name) { | |
174 | sr_err("dev: %s: name was NULL", __func__); | |
0e3b1439 | 175 | return SR_ERR_ARG; |
94799bc4 UH |
176 | } |
177 | ||
178 | /* TODO: Further checks to ensure name is valid. */ | |
179 | ||
bb7ef793 | 180 | probenum = g_slist_length(dev->probes) + 1; |
b53738ba UH |
181 | |
182 | if (!(p = g_try_malloc0(sizeof(struct sr_probe)))) { | |
183 | sr_err("dev: %s: p malloc failed", __func__); | |
0e3b1439 | 184 | return SR_ERR_MALLOC; |
b53738ba UH |
185 | } |
186 | ||
7d658874 | 187 | p->index = probenum; |
a1bb33af | 188 | p->enabled = TRUE; |
c37d2b1b | 189 | p->name = g_strdup(name); |
a1bb33af | 190 | p->trigger = NULL; |
bb7ef793 | 191 | dev->probes = g_slist_append(dev->probes, p); |
94799bc4 UH |
192 | |
193 | return SR_OK; | |
a1bb33af UH |
194 | } |
195 | ||
94799bc4 UH |
196 | /** |
197 | * Find the probe with the specified number in the specified device. | |
198 | * | |
199 | * TODO | |
200 | * | |
bb7ef793 | 201 | * @param dev TODO. Must not be NULL. |
94799bc4 UH |
202 | * @param probenum The number of the probe whose 'struct sr_probe' we want. |
203 | * Note that the probe numbers start at 1 (not 0!). | |
204 | * | |
205 | * TODO: Should return int. | |
94799bc4 UH |
206 | * TODO: probenum should be unsigned. |
207 | * | |
208 | * @return A pointer to the requested probe's 'struct sr_probe', or NULL | |
209 | * if the probe could not be found. | |
210 | */ | |
bb7ef793 UH |
211 | SR_API struct sr_probe *sr_dev_probe_find(const struct sr_dev *dev, |
212 | int probenum) | |
a1bb33af UH |
213 | { |
214 | GSList *l; | |
1afe8989 | 215 | struct sr_probe *p, *found_probe; |
a1bb33af | 216 | |
bb7ef793 UH |
217 | if (!dev) { |
218 | sr_err("dev: %s: dev was NULL", __func__); | |
94799bc4 UH |
219 | return NULL; /* TODO: SR_ERR_ARG */ |
220 | } | |
221 | ||
222 | /* TODO: Sanity check on probenum. */ | |
223 | ||
a1bb33af | 224 | found_probe = NULL; |
bb7ef793 | 225 | for (l = dev->probes; l; l = l->next) { |
a1bb33af | 226 | p = l->data; |
94799bc4 | 227 | /* TODO: Check for p != NULL. */ |
1b452b85 | 228 | if (p->index == probenum) { |
a1bb33af UH |
229 | found_probe = p; |
230 | break; | |
231 | } | |
232 | } | |
233 | ||
234 | return found_probe; | |
235 | } | |
236 | ||
94799bc4 UH |
237 | /** |
238 | * Set the name of the specified probe in the specified device. | |
239 | * | |
240 | * If the probe already has a different name assigned to it, it will be | |
241 | * removed, and the new name will be saved instead. | |
242 | * | |
bb7ef793 | 243 | * TODO: Rename to sr_dev_probe_name_set(). |
94799bc4 | 244 | * |
bb7ef793 | 245 | * @param dev TODO |
94799bc4 UH |
246 | * @param probenum The number of the probe whose name to set. |
247 | * Note that the probe numbers start at 1 (not 0!). | |
248 | * @param name The new name that the specified probe should get. | |
0e3b1439 UH |
249 | * |
250 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or SR_ERR | |
251 | * upon other errors. | |
bb7ef793 | 252 | * If something other than SR_OK is returned, 'dev' is unchanged. |
94799bc4 | 253 | */ |
bb7ef793 UH |
254 | SR_API int sr_dev_probe_name(struct sr_dev *dev, int probenum, |
255 | const char *name) | |
a1bb33af | 256 | { |
1afe8989 | 257 | struct sr_probe *p; |
a1bb33af | 258 | |
bb7ef793 UH |
259 | if (!dev) { |
260 | sr_err("dev: %s: dev was NULL", __func__); | |
0e3b1439 | 261 | return SR_ERR_ARG; |
94799bc4 UH |
262 | } |
263 | ||
bb7ef793 | 264 | p = sr_dev_probe_find(dev, probenum); |
94799bc4 UH |
265 | if (!p) { |
266 | sr_err("dev: %s: probe %d not found", __func__, probenum); | |
0e3b1439 | 267 | return SR_ERR; /* TODO: More specific error? */ |
94799bc4 UH |
268 | } |
269 | ||
270 | /* TODO: Sanity check on 'name'. */ | |
a1bb33af | 271 | |
94799bc4 | 272 | /* If the probe already has a name, kill it first. */ |
66410a86 | 273 | g_free(p->name); |
94799bc4 | 274 | |
a1bb33af | 275 | p->name = g_strdup(name); |
0e3b1439 UH |
276 | |
277 | return SR_OK; | |
a1bb33af UH |
278 | } |
279 | ||
94799bc4 UH |
280 | /** |
281 | * Remove all triggers set up for the specified device. | |
282 | * | |
283 | * TODO: Better description. | |
284 | * | |
bb7ef793 | 285 | * @param dev TODO |
0e3b1439 UH |
286 | * |
287 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments. | |
bb7ef793 | 288 | * If something other than SR_OK is returned, 'dev' is unchanged. |
94799bc4 | 289 | */ |
bb7ef793 | 290 | SR_API int sr_dev_trigger_clear(struct sr_dev *dev) |
a1bb33af | 291 | { |
1afe8989 | 292 | struct sr_probe *p; |
0e3b1439 | 293 | unsigned int pnum; /* TODO: uint16_t? */ |
a1bb33af | 294 | |
bb7ef793 UH |
295 | if (!dev) { |
296 | sr_err("dev: %s: dev was NULL", __func__); | |
0e3b1439 | 297 | return SR_ERR_ARG; |
94799bc4 UH |
298 | } |
299 | ||
bb7ef793 UH |
300 | if (!dev->probes) { |
301 | sr_err("dev: %s: dev->probes was NULL", __func__); | |
0e3b1439 | 302 | return SR_ERR_ARG; |
94799bc4 | 303 | } |
a1bb33af | 304 | |
bb7ef793 UH |
305 | for (pnum = 1; pnum <= g_slist_length(dev->probes); pnum++) { |
306 | p = sr_dev_probe_find(dev, pnum); | |
94799bc4 | 307 | /* TODO: Silently ignore probes which cannot be found? */ |
66410a86 | 308 | if (p) { |
1b452b85 UH |
309 | g_free(p->trigger); |
310 | p->trigger = NULL; | |
311 | } | |
312 | } | |
0e3b1439 UH |
313 | |
314 | return SR_OK; | |
1b452b85 | 315 | } |
a1bb33af | 316 | |
94799bc4 UH |
317 | /** |
318 | * Add a trigger to the specified device. | |
319 | * | |
320 | * TODO: Better description. | |
321 | * TODO: Describe valid format of the 'trigger' string. | |
322 | * | |
bb7ef793 | 323 | * @param dev TODO. Must not be NULL. |
94799bc4 UH |
324 | * @param probenum The number of the probe. TODO. |
325 | * Note that the probe numbers start at 1 (not 0!). | |
326 | * @param trigger TODO. | |
327 | * TODO: Is NULL allowed? | |
0e3b1439 UH |
328 | * |
329 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or SR_ERR | |
330 | * upon other errors. | |
bb7ef793 | 331 | * If something other than SR_OK is returned, 'dev' is unchanged. |
94799bc4 | 332 | */ |
bb7ef793 UH |
333 | SR_API int sr_dev_trigger_set(struct sr_dev *dev, int probenum, |
334 | const char *trigger) | |
a1bb33af | 335 | { |
1afe8989 | 336 | struct sr_probe *p; |
a1bb33af | 337 | |
bb7ef793 UH |
338 | if (!dev) { |
339 | sr_err("dev: %s: dev was NULL", __func__); | |
0e3b1439 | 340 | return SR_ERR_ARG; |
94799bc4 UH |
341 | } |
342 | ||
343 | /* TODO: Sanity check on 'probenum'. */ | |
344 | ||
345 | /* TODO: Sanity check on 'trigger'. */ | |
346 | ||
bb7ef793 | 347 | p = sr_dev_probe_find(dev, probenum); |
94799bc4 UH |
348 | if (!p) { |
349 | sr_err("dev: %s: probe %d not found", __func__, probenum); | |
0e3b1439 | 350 | return SR_ERR; /* TODO: More specific error? */ |
94799bc4 | 351 | } |
a1bb33af | 352 | |
94799bc4 | 353 | /* If the probe already has a trigger, kill it first. */ |
66410a86 | 354 | g_free(p->trigger); |
a1bb33af UH |
355 | |
356 | p->trigger = g_strdup(trigger); | |
0e3b1439 UH |
357 | |
358 | return SR_OK; | |
7d658874 BV |
359 | } |
360 | ||
94799bc4 UH |
361 | /** |
362 | * Determine whether the specified device has the specified capability. | |
363 | * | |
364 | * TODO: Should return int? | |
94799bc4 | 365 | * |
bb7ef793 UH |
366 | * @param dev Pointer to the device to be checked. Must not be NULL. |
367 | * The device's 'plugin' field must not be NULL either. | |
94799bc4 UH |
368 | * @param hwcap The capability that should be checked (whether it's supported |
369 | * by the specified device). | |
370 | * | |
371 | * @return TRUE, if the device has the specified capability, FALSE otherwise. | |
372 | * FALSE is also returned upon invalid input parameters or other | |
373 | * error conditions. | |
374 | */ | |
bb7ef793 | 375 | SR_API gboolean sr_dev_has_hwcap(const struct sr_dev *dev, int hwcap) |
7d658874 BV |
376 | { |
377 | int *capabilities, i; | |
378 | ||
bb7ef793 UH |
379 | if (!dev) { |
380 | sr_err("dev: %s: dev was NULL", __func__); | |
94799bc4 UH |
381 | return FALSE; /* TODO: SR_ERR_ARG. */ |
382 | } | |
383 | ||
bb7ef793 UH |
384 | if (!dev->plugin) { |
385 | sr_err("dev: %s: dev->plugin was NULL", __func__); | |
94799bc4 UH |
386 | return FALSE; /* TODO: SR_ERR_ARG. */ |
387 | } | |
388 | ||
389 | /* TODO: Sanity check on 'hwcap'. */ | |
390 | ||
bb7ef793 UH |
391 | if (!(capabilities = dev->plugin->get_capabilities())) { |
392 | sr_err("dev: %s: dev has no capabilities", __func__); | |
94799bc4 UH |
393 | return FALSE; /* TODO: SR_ERR*. */ |
394 | } | |
395 | ||
396 | for (i = 0; capabilities[i]; i++) { | |
397 | if (capabilities[i] != hwcap) | |
398 | continue; | |
399 | sr_spew("dev: %s: found hwcap %d", __func__, hwcap); | |
400 | return TRUE; | |
401 | } | |
218557b8 | 402 | |
94799bc4 | 403 | sr_spew("dev: %s: hwcap %d not found", __func__, hwcap); |
7d658874 BV |
404 | |
405 | return FALSE; | |
a1bb33af | 406 | } |
fd9836bf AS |
407 | |
408 | /** | |
409 | * Returns information about the given device. | |
410 | * | |
bb7ef793 UH |
411 | * @param dev Pointer to the device to be checked. Must not be NULL. |
412 | * The device's 'plugin' field must not be NULL either. | |
44dae539 UH |
413 | * @param id The type of information. |
414 | * @param data The return value. Must not be NULL. | |
fd9836bf AS |
415 | * |
416 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or SR_ERR | |
417 | * upon other errors. | |
418 | */ | |
bb7ef793 | 419 | SR_API int sr_dev_info_get(const struct sr_dev *dev, int id, const void **data) |
fd9836bf | 420 | { |
bb7ef793 | 421 | if ((dev == NULL) || (dev->plugin == NULL)) |
fd9836bf AS |
422 | return SR_ERR_ARG; |
423 | ||
424 | if (data == NULL) | |
425 | return SR_ERR_ARG; | |
426 | ||
bb7ef793 | 427 | *data = dev->plugin->get_dev_info(dev->plugin_index, id); |
fd9836bf AS |
428 | |
429 | if (*data == NULL) | |
430 | return SR_ERR; | |
431 | ||
432 | return SR_OK; | |
433 | } |