]>
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> | |
45c59c8b BV |
22 | #include "libsigrok.h" |
23 | #include "libsigrok-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 | 57 | * |
0abee507 | 58 | * @return SR_OK upon success, SR_ERR_BUG upon internal errors. |
94799bc4 | 59 | */ |
03168500 | 60 | SR_API int sr_dev_scan(void) |
a1bb33af | 61 | { |
050e9219 | 62 | int i; |
c09f0b57 | 63 | struct sr_dev_driver **drivers; |
a1bb33af | 64 | |
cfe064d8 | 65 | drivers = sr_driver_list(); |
c09f0b57 UH |
66 | if (!drivers[0]) { |
67 | sr_err("dev: %s: no supported hardware drivers", __func__); | |
0abee507 | 68 | return SR_ERR_BUG; |
94799bc4 | 69 | } |
a1bb33af | 70 | |
1b452b85 | 71 | /* |
c09f0b57 | 72 | * Initialize all drivers first. Since the init() call may involve |
a1bb33af UH |
73 | * a firmware upload and associated delay, we may as well get all |
74 | * of these out of the way first. | |
75 | */ | |
c09f0b57 | 76 | for (i = 0; drivers[i]; i++) |
cfe064d8 | 77 | sr_driver_init(drivers[i]); |
0e3b1439 UH |
78 | |
79 | return SR_OK; | |
a1bb33af UH |
80 | } |
81 | ||
94799bc4 UH |
82 | /** |
83 | * Return the list of logic analyzer devices libsigrok has detected. | |
84 | * | |
85 | * If the libsigrok-internal device list is empty, a scan for attached | |
03168500 | 86 | * devices -- via a call to sr_dev_scan() -- is performed first. |
94799bc4 UH |
87 | * |
88 | * TODO: Error handling? | |
89 | * | |
90 | * @return The list (GSList) of detected devices, or NULL if none were found. | |
91 | */ | |
03168500 | 92 | SR_API GSList *sr_dev_list(void) |
a1bb33af | 93 | { |
bb7ef793 | 94 | if (!devs) |
03168500 | 95 | sr_dev_scan(); |
e54bcdc5 | 96 | |
bb7ef793 | 97 | return devs; |
a1bb33af UH |
98 | } |
99 | ||
94799bc4 UH |
100 | /** |
101 | * Create a new device. | |
102 | * | |
c37d2b1b UH |
103 | * The device is added to the (libsigrok-internal) list of devices, but |
104 | * additionally a pointer to the newly created device is also returned. | |
105 | * | |
106 | * The device has no probes attached to it yet after this call. You can | |
03168500 | 107 | * use sr_dev_probe_add() to add one or more probes. |
c37d2b1b | 108 | * |
94799bc4 UH |
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 | * | |
c09f0b57 UH |
114 | * @param driver TODO. |
115 | * If 'driver' is NULL, the created device is a "virtual" one. | |
116 | * @param driver_index TODO | |
94799bc4 UH |
117 | * |
118 | * @return Pointer to the newly allocated device, or NULL upon errors. | |
119 | */ | |
c09f0b57 UH |
120 | SR_API struct sr_dev *sr_dev_new(const struct sr_dev_driver *driver, |
121 | int driver_index) | |
a1bb33af | 122 | { |
bb7ef793 | 123 | struct sr_dev *dev; |
94799bc4 | 124 | |
c09f0b57 | 125 | /* TODO: Check if driver_index valid? */ |
94799bc4 | 126 | |
bb7ef793 UH |
127 | if (!(dev = g_try_malloc0(sizeof(struct sr_dev)))) { |
128 | sr_err("dev: %s: dev malloc failed", __func__); | |
b53738ba UH |
129 | return NULL; |
130 | } | |
131 | ||
c09f0b57 UH |
132 | dev->driver = (struct sr_dev_driver *)driver; |
133 | dev->driver_index = driver_index; | |
bb7ef793 | 134 | devs = g_slist_append(devs, dev); |
a1bb33af | 135 | |
bb7ef793 | 136 | return dev; |
a1bb33af UH |
137 | } |
138 | ||
94799bc4 UH |
139 | /** |
140 | * Add a probe with the specified name to the specified device. | |
141 | * | |
142 | * The added probe is automatically enabled (the 'enabled' field is TRUE). | |
143 | * | |
144 | * The 'trigger' field of the added probe is set to NULL. A trigger can be | |
03168500 | 145 | * added via sr_dev_trigger_set(). |
94799bc4 | 146 | * |
94799bc4 UH |
147 | * TODO: Are duplicate names allowed? |
148 | * TODO: Do we enforce a maximum probe number for a device? | |
149 | * TODO: Error if the max. probe number for the specific LA is reached, e.g. | |
150 | * if the caller tries to add more probes than the device actually has. | |
151 | * | |
bb7ef793 UH |
152 | * @param dev The device to which to add a probe with the specified name. |
153 | * Must not be NULL. | |
94799bc4 UH |
154 | * @param name The name of the probe to add to this device. Must not be NULL. |
155 | * TODO: Maximum length, allowed characters, etc. | |
156 | * | |
157 | * @return SR_OK upon success, SR_ERR_MALLOC upon memory allocation errors, | |
158 | * or SR_ERR_ARG upon invalid arguments. | |
bb7ef793 | 159 | * If something other than SR_OK is returned, 'dev' is unchanged. |
94799bc4 | 160 | */ |
bb7ef793 | 161 | SR_API int sr_dev_probe_add(struct sr_dev *dev, const char *name) |
a1bb33af | 162 | { |
1afe8989 | 163 | struct sr_probe *p; |
7d658874 | 164 | int probenum; |
a1bb33af | 165 | |
bb7ef793 UH |
166 | if (!dev) { |
167 | sr_err("dev: %s: dev was NULL", __func__); | |
0e3b1439 | 168 | return SR_ERR_ARG; |
94799bc4 UH |
169 | } |
170 | ||
171 | if (!name) { | |
172 | sr_err("dev: %s: name was NULL", __func__); | |
0e3b1439 | 173 | return SR_ERR_ARG; |
94799bc4 UH |
174 | } |
175 | ||
176 | /* TODO: Further checks to ensure name is valid. */ | |
177 | ||
bb7ef793 | 178 | probenum = g_slist_length(dev->probes) + 1; |
b53738ba UH |
179 | |
180 | if (!(p = g_try_malloc0(sizeof(struct sr_probe)))) { | |
181 | sr_err("dev: %s: p malloc failed", __func__); | |
0e3b1439 | 182 | return SR_ERR_MALLOC; |
b53738ba UH |
183 | } |
184 | ||
7d658874 | 185 | p->index = probenum; |
a1bb33af | 186 | p->enabled = TRUE; |
c37d2b1b | 187 | p->name = g_strdup(name); |
a1bb33af | 188 | p->trigger = NULL; |
bb7ef793 | 189 | dev->probes = g_slist_append(dev->probes, p); |
94799bc4 UH |
190 | |
191 | return SR_OK; | |
a1bb33af UH |
192 | } |
193 | ||
94799bc4 UH |
194 | /** |
195 | * Find the probe with the specified number in the specified device. | |
196 | * | |
197 | * TODO | |
198 | * | |
bb7ef793 | 199 | * @param dev TODO. Must not be NULL. |
94799bc4 UH |
200 | * @param probenum The number of the probe whose 'struct sr_probe' we want. |
201 | * Note that the probe numbers start at 1 (not 0!). | |
202 | * | |
203 | * TODO: Should return int. | |
94799bc4 UH |
204 | * TODO: probenum should be unsigned. |
205 | * | |
206 | * @return A pointer to the requested probe's 'struct sr_probe', or NULL | |
207 | * if the probe could not be found. | |
208 | */ | |
bb7ef793 UH |
209 | SR_API struct sr_probe *sr_dev_probe_find(const struct sr_dev *dev, |
210 | int probenum) | |
a1bb33af UH |
211 | { |
212 | GSList *l; | |
1afe8989 | 213 | struct sr_probe *p, *found_probe; |
a1bb33af | 214 | |
bb7ef793 UH |
215 | if (!dev) { |
216 | sr_err("dev: %s: dev was NULL", __func__); | |
94799bc4 UH |
217 | return NULL; /* TODO: SR_ERR_ARG */ |
218 | } | |
219 | ||
220 | /* TODO: Sanity check on probenum. */ | |
221 | ||
a1bb33af | 222 | found_probe = NULL; |
bb7ef793 | 223 | for (l = dev->probes; l; l = l->next) { |
a1bb33af | 224 | p = l->data; |
94799bc4 | 225 | /* TODO: Check for p != NULL. */ |
1b452b85 | 226 | if (p->index == probenum) { |
a1bb33af UH |
227 | found_probe = p; |
228 | break; | |
229 | } | |
230 | } | |
231 | ||
232 | return found_probe; | |
233 | } | |
234 | ||
94799bc4 UH |
235 | /** |
236 | * Set the name of the specified probe in the specified device. | |
237 | * | |
238 | * If the probe already has a different name assigned to it, it will be | |
239 | * removed, and the new name will be saved instead. | |
240 | * | |
bb7ef793 | 241 | * @param dev TODO |
94799bc4 UH |
242 | * @param probenum The number of the probe whose name to set. |
243 | * Note that the probe numbers start at 1 (not 0!). | |
244 | * @param name The new name that the specified probe should get. | |
0e3b1439 UH |
245 | * |
246 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or SR_ERR | |
247 | * upon other errors. | |
bb7ef793 | 248 | * If something other than SR_OK is returned, 'dev' is unchanged. |
94799bc4 | 249 | */ |
2f8cf274 UH |
250 | SR_API int sr_dev_probe_name_set(struct sr_dev *dev, int probenum, |
251 | const char *name) | |
a1bb33af | 252 | { |
1afe8989 | 253 | struct sr_probe *p; |
a1bb33af | 254 | |
bb7ef793 UH |
255 | if (!dev) { |
256 | sr_err("dev: %s: dev was NULL", __func__); | |
0e3b1439 | 257 | return SR_ERR_ARG; |
94799bc4 UH |
258 | } |
259 | ||
bb7ef793 | 260 | p = sr_dev_probe_find(dev, probenum); |
94799bc4 UH |
261 | if (!p) { |
262 | sr_err("dev: %s: probe %d not found", __func__, probenum); | |
0e3b1439 | 263 | return SR_ERR; /* TODO: More specific error? */ |
94799bc4 UH |
264 | } |
265 | ||
266 | /* TODO: Sanity check on 'name'. */ | |
a1bb33af | 267 | |
94799bc4 | 268 | /* If the probe already has a name, kill it first. */ |
66410a86 | 269 | g_free(p->name); |
94799bc4 | 270 | |
a1bb33af | 271 | p->name = g_strdup(name); |
0e3b1439 UH |
272 | |
273 | return SR_OK; | |
a1bb33af UH |
274 | } |
275 | ||
be5bf44d BV |
276 | /** |
277 | * Enable or disable a probe on the specified device. | |
278 | * | |
279 | * @param sdi The device instance the probe is connected to. | |
280 | * @param probenum The probe number, starting from 0. | |
281 | * @param state TRUE to enable the probe, FALSE to disable. | |
282 | * | |
283 | * @return SR_OK on success, or SR_ERR_ARG on invalid arguments. | |
284 | */ | |
285 | SR_API int sr_dev_probe_enable(const struct sr_dev_inst *sdi, int probenum, | |
286 | gboolean state) | |
287 | { | |
288 | GSList *l; | |
289 | struct sr_probe *probe; | |
290 | int ret; | |
291 | ||
292 | if (!sdi) | |
293 | return SR_ERR_ARG; | |
294 | ||
295 | ret = SR_ERR_ARG; | |
296 | for (l = sdi->probes; l; l = l->next) { | |
297 | probe = l->data; | |
298 | if (probe->index == probenum) { | |
299 | probe->enabled = state; | |
300 | ret = SR_OK; | |
301 | break; | |
302 | } | |
303 | } | |
304 | ||
305 | return ret; | |
306 | } | |
307 | ||
94799bc4 UH |
308 | /** |
309 | * Remove all triggers set up for the specified device. | |
310 | * | |
311 | * TODO: Better description. | |
312 | * | |
bb7ef793 | 313 | * @param dev TODO |
0e3b1439 UH |
314 | * |
315 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments. | |
bb7ef793 | 316 | * If something other than SR_OK is returned, 'dev' is unchanged. |
94799bc4 | 317 | */ |
01c3e9db | 318 | SR_API int sr_dev_trigger_remove_all(struct sr_dev *dev) |
a1bb33af | 319 | { |
1afe8989 | 320 | struct sr_probe *p; |
0e3b1439 | 321 | unsigned int pnum; /* TODO: uint16_t? */ |
a1bb33af | 322 | |
bb7ef793 UH |
323 | if (!dev) { |
324 | sr_err("dev: %s: dev was NULL", __func__); | |
0e3b1439 | 325 | return SR_ERR_ARG; |
94799bc4 UH |
326 | } |
327 | ||
bb7ef793 UH |
328 | if (!dev->probes) { |
329 | sr_err("dev: %s: dev->probes was NULL", __func__); | |
0e3b1439 | 330 | return SR_ERR_ARG; |
94799bc4 | 331 | } |
a1bb33af | 332 | |
bb7ef793 UH |
333 | for (pnum = 1; pnum <= g_slist_length(dev->probes); pnum++) { |
334 | p = sr_dev_probe_find(dev, pnum); | |
94799bc4 | 335 | /* TODO: Silently ignore probes which cannot be found? */ |
66410a86 | 336 | if (p) { |
1b452b85 UH |
337 | g_free(p->trigger); |
338 | p->trigger = NULL; | |
339 | } | |
340 | } | |
0e3b1439 UH |
341 | |
342 | return SR_OK; | |
1b452b85 | 343 | } |
a1bb33af | 344 | |
94799bc4 | 345 | /** |
01c3e9db UH |
346 | * Add a trigger to the specified device (and the specified probe). |
347 | * | |
348 | * If the specified probe of this device already has a trigger, it will | |
349 | * be silently replaced. | |
94799bc4 UH |
350 | * |
351 | * TODO: Better description. | |
352 | * TODO: Describe valid format of the 'trigger' string. | |
353 | * | |
bb7ef793 | 354 | * @param dev TODO. Must not be NULL. |
94799bc4 UH |
355 | * @param probenum The number of the probe. TODO. |
356 | * Note that the probe numbers start at 1 (not 0!). | |
357 | * @param trigger TODO. | |
358 | * TODO: Is NULL allowed? | |
0e3b1439 UH |
359 | * |
360 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or SR_ERR | |
361 | * upon other errors. | |
bb7ef793 | 362 | * If something other than SR_OK is returned, 'dev' is unchanged. |
94799bc4 | 363 | */ |
58453e58 BV |
364 | SR_API int sr_dev_trigger_set(const struct sr_dev_inst *sdi, int probenum, |
365 | const char *trigger) | |
a1bb33af | 366 | { |
58453e58 BV |
367 | GSList *l; |
368 | struct sr_probe *probe; | |
369 | int ret; | |
a1bb33af | 370 | |
58453e58 | 371 | if (!sdi) |
0e3b1439 | 372 | return SR_ERR_ARG; |
94799bc4 | 373 | |
58453e58 BV |
374 | ret = SR_ERR_ARG; |
375 | for (l = sdi->probes; l; l = l->next) { | |
376 | probe = l->data; | |
377 | if (probe->index == probenum) { | |
378 | /* If the probe already has a trigger, kill it first. */ | |
379 | g_free(probe->trigger); | |
380 | probe->trigger = g_strdup(trigger); | |
381 | ret = SR_OK; | |
382 | break; | |
383 | } | |
94799bc4 | 384 | } |
a1bb33af | 385 | |
58453e58 | 386 | return ret; |
7d658874 BV |
387 | } |
388 | ||
94799bc4 UH |
389 | /** |
390 | * Determine whether the specified device has the specified capability. | |
391 | * | |
a5b35a16 | 392 | * @param dev Pointer to the device instance to be checked. Must not be NULL. |
8ec95d22 UH |
393 | * If the device's 'driver' field is NULL (virtual device), this |
394 | * function will always return FALSE (virtual devices don't have | |
395 | * a hardware capabilities list). | |
94799bc4 UH |
396 | * @param hwcap The capability that should be checked (whether it's supported |
397 | * by the specified device). | |
398 | * | |
399 | * @return TRUE, if the device has the specified capability, FALSE otherwise. | |
400 | * FALSE is also returned upon invalid input parameters or other | |
401 | * error conditions. | |
402 | */ | |
a5b35a16 | 403 | SR_API gboolean sr_dev_has_hwcap(const struct sr_dev_inst *sdi, int hwcap) |
7d658874 | 404 | { |
915f7cc8 JH |
405 | const int *hwcaps; |
406 | int i; | |
7d658874 | 407 | |
a5b35a16 | 408 | if (!sdi || !sdi->driver) |
8ec95d22 | 409 | return FALSE; |
94799bc4 | 410 | |
a5b35a16 BV |
411 | if (sdi->driver->info_get(SR_DI_HWCAPS, |
412 | (const void **)&hwcaps, NULL) != SR_OK) | |
8ec95d22 | 413 | return FALSE; |
94799bc4 | 414 | |
ffedd0bf | 415 | for (i = 0; hwcaps[i]; i++) { |
a5b35a16 BV |
416 | if (hwcaps[i] == hwcap) |
417 | return TRUE; | |
94799bc4 | 418 | } |
218557b8 | 419 | |
7d658874 | 420 | return FALSE; |
a1bb33af | 421 | } |
fd9836bf AS |
422 | |
423 | /** | |
424 | * Returns information about the given device. | |
425 | * | |
bb7ef793 | 426 | * @param dev Pointer to the device to be checked. Must not be NULL. |
c09f0b57 | 427 | * The device's 'driver' field must not be NULL either. |
44dae539 UH |
428 | * @param id The type of information. |
429 | * @param data The return value. Must not be NULL. | |
fd9836bf AS |
430 | * |
431 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or SR_ERR | |
432 | * upon other errors. | |
433 | */ | |
bb7ef793 | 434 | SR_API int sr_dev_info_get(const struct sr_dev *dev, int id, const void **data) |
fd9836bf | 435 | { |
c09f0b57 | 436 | if ((dev == NULL) || (dev->driver == NULL)) |
fd9836bf AS |
437 | return SR_ERR_ARG; |
438 | ||
439 | if (data == NULL) | |
440 | return SR_ERR_ARG; | |
441 | ||
c09f0b57 | 442 | *data = dev->driver->dev_info_get(dev->driver_index, id); |
fd9836bf AS |
443 | |
444 | if (*data == NULL) | |
445 | return SR_ERR; | |
446 | ||
447 | return SR_OK; | |
448 | } |