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