]> sigrok.org Git - libsigrok.git/blob - device.c
sr: s/sr_dev_probe_name/sr_dev_probe_name_set/.
[libsigrok.git] / device.c
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2010-2012 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 *devs = 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_dev_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 SR_API int sr_dev_scan(void)
61 {
62         int i;
63         struct sr_dev_driver **drivers;
64
65         drivers = sr_hw_list();
66         if (!drivers[0]) {
67                 sr_err("dev: %s: no supported hardware drivers", __func__);
68                 return SR_ERR; /* TODO: More specific error? */
69         }
70
71         /*
72          * Initialize all drivers first. Since the init() call may involve
73          * a firmware upload and associated delay, we may as well get all
74          * of these out of the way first.
75          */
76         for (i = 0; drivers[i]; i++)
77                 sr_hw_init(drivers[i]);
78
79         return SR_OK;
80 }
81
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
86  * devices -- via a call to sr_dev_scan() -- is performed first.
87  *
88  * TODO: Error handling?
89  *
90  * @return The list (GSList) of detected devices, or NULL if none were found.
91  */
92 SR_API GSList *sr_dev_list(void)
93 {
94         if (!devs)
95                 sr_dev_scan();
96
97         return devs;
98 }
99
100 /**
101  * Create a new device.
102  *
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
107  * use sr_dev_probe_add() to add one or more probes.
108  *
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  *
114  * @param driver TODO.
115  *               If 'driver' is NULL, the created device is a "virtual" one.
116  * @param driver_index TODO
117  *
118  * @return Pointer to the newly allocated device, or NULL upon errors.
119  */
120 SR_API struct sr_dev *sr_dev_new(const struct sr_dev_driver *driver,
121                                  int driver_index)
122 {
123         struct sr_dev *dev;
124
125         /* TODO: Check if driver_index valid? */
126
127         if (!(dev = g_try_malloc0(sizeof(struct sr_dev)))) {
128                 sr_err("dev: %s: dev malloc failed", __func__);
129                 return NULL;
130         }
131
132         dev->driver = (struct sr_dev_driver *)driver;
133         dev->driver_index = driver_index;
134         devs = g_slist_append(devs, dev);
135
136         return dev;
137 }
138
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
145  * added via sr_dev_trigger_set().
146  *
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  *
152  * @param dev The device to which to add a probe with the specified name.
153  *            Must not be NULL.
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.
159  *         If something other than SR_OK is returned, 'dev' is unchanged.
160  */
161 SR_API int sr_dev_probe_add(struct sr_dev *dev, const char *name)
162 {
163         struct sr_probe *p;
164         int probenum;
165
166         if (!dev) {
167                 sr_err("dev: %s: dev was NULL", __func__);
168                 return SR_ERR_ARG;
169         }
170
171         if (!name) {
172                 sr_err("dev: %s: name was NULL", __func__);
173                 return SR_ERR_ARG;
174         }
175
176         /* TODO: Further checks to ensure name is valid. */
177
178         probenum = g_slist_length(dev->probes) + 1;
179
180         if (!(p = g_try_malloc0(sizeof(struct sr_probe)))) {
181                 sr_err("dev: %s: p malloc failed", __func__);
182                 return SR_ERR_MALLOC;
183         }
184
185         p->index = probenum;
186         p->enabled = TRUE;
187         p->name = g_strdup(name);
188         p->trigger = NULL;
189         dev->probes = g_slist_append(dev->probes, p);
190
191         return SR_OK;
192 }
193
194 /**
195  * Find the probe with the specified number in the specified device.
196  *
197  * TODO
198  *
199  * @param dev TODO. Must not be NULL.
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.
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  */
209 SR_API struct sr_probe *sr_dev_probe_find(const struct sr_dev *dev,
210                                           int probenum)
211 {
212         GSList *l;
213         struct sr_probe *p, *found_probe;
214
215         if (!dev) {
216                 sr_err("dev: %s: dev was NULL", __func__);
217                 return NULL; /* TODO: SR_ERR_ARG */
218         }
219
220         /* TODO: Sanity check on probenum. */
221
222         found_probe = NULL;
223         for (l = dev->probes; l; l = l->next) {
224                 p = l->data;
225                 /* TODO: Check for p != NULL. */
226                 if (p->index == probenum) {
227                         found_probe = p;
228                         break;
229                 }
230         }
231
232         return found_probe;
233 }
234
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  *
241  * @param dev TODO
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.
245  *
246  * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or SR_ERR
247  *         upon other errors.
248  *         If something other than SR_OK is returned, 'dev' is unchanged.
249  */
250 SR_API int sr_dev_probe_name_set(struct sr_dev *dev, int probenum,
251                                  const char *name)
252 {
253         struct sr_probe *p;
254
255         if (!dev) {
256                 sr_err("dev: %s: dev was NULL", __func__);
257                 return SR_ERR_ARG;
258         }
259
260         p = sr_dev_probe_find(dev, probenum);
261         if (!p) {
262                 sr_err("dev: %s: probe %d not found", __func__, probenum);
263                 return SR_ERR; /* TODO: More specific error? */
264         }
265
266         /* TODO: Sanity check on 'name'. */
267
268         /* If the probe already has a name, kill it first. */
269         g_free(p->name);
270
271         p->name = g_strdup(name);
272
273         return SR_OK;
274 }
275
276 /**
277  * Remove all triggers set up for the specified device.
278  *
279  * TODO: Better description.
280  *
281  * @param dev TODO
282  *
283  * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
284  *         If something other than SR_OK is returned, 'dev' is unchanged.
285  */
286 SR_API int sr_dev_trigger_clear(struct sr_dev *dev)
287 {
288         struct sr_probe *p;
289         unsigned int pnum; /* TODO: uint16_t? */
290
291         if (!dev) {
292                 sr_err("dev: %s: dev was NULL", __func__);
293                 return SR_ERR_ARG;
294         }
295
296         if (!dev->probes) {
297                 sr_err("dev: %s: dev->probes was NULL", __func__);
298                 return SR_ERR_ARG;
299         }
300
301         for (pnum = 1; pnum <= g_slist_length(dev->probes); pnum++) {
302                 p = sr_dev_probe_find(dev, pnum);
303                 /* TODO: Silently ignore probes which cannot be found? */
304                 if (p) {
305                         g_free(p->trigger);
306                         p->trigger = NULL;
307                 }
308         }
309
310         return SR_OK;
311 }
312
313 /**
314  * Add a trigger to the specified device.
315  *
316  * TODO: Better description.
317  * TODO: Describe valid format of the 'trigger' string.
318  *
319  * @param dev TODO. Must not be NULL.
320  * @param probenum The number of the probe. TODO.
321  *                 Note that the probe numbers start at 1 (not 0!).
322  * @param trigger TODO.
323  *                TODO: Is NULL allowed?
324  *
325  * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or SR_ERR
326  *         upon other errors.
327  *         If something other than SR_OK is returned, 'dev' is unchanged.
328  */
329 SR_API int sr_dev_trigger_set(struct sr_dev *dev, int probenum,
330                               const char *trigger)
331 {
332         struct sr_probe *p;
333
334         if (!dev) {
335                 sr_err("dev: %s: dev was NULL", __func__);
336                 return SR_ERR_ARG;
337         }
338
339         /* TODO: Sanity check on 'probenum'. */
340
341         /* TODO: Sanity check on 'trigger'. */
342
343         p = sr_dev_probe_find(dev, probenum);
344         if (!p) {
345                 sr_err("dev: %s: probe %d not found", __func__, probenum);
346                 return SR_ERR; /* TODO: More specific error? */
347         }
348
349         /* If the probe already has a trigger, kill it first. */
350         g_free(p->trigger);
351
352         p->trigger = g_strdup(trigger);
353
354         return SR_OK;
355 }
356
357 /**
358  * Determine whether the specified device has the specified capability.
359  *
360  * TODO: Should return int?
361  *
362  * @param dev Pointer to the device to be checked. Must not be NULL.
363  *            The device's 'driver' field must not be NULL either.
364  * @param hwcap The capability that should be checked (whether it's supported
365  *              by the specified device).
366  *
367  * @return TRUE, if the device has the specified capability, FALSE otherwise.
368  *         FALSE is also returned upon invalid input parameters or other
369  *         error conditions.
370  */
371 SR_API gboolean sr_dev_has_hwcap(const struct sr_dev *dev, int hwcap)
372 {
373         int *hwcaps, i;
374
375         if (!dev) {
376                 sr_err("dev: %s: dev was NULL", __func__);
377                 return FALSE; /* TODO: SR_ERR_ARG. */
378         }
379
380         if (!dev->driver) {
381                 sr_err("dev: %s: dev->driver was NULL", __func__);
382                 return FALSE; /* TODO: SR_ERR_ARG. */
383         }
384
385         /* TODO: Sanity check on 'hwcap'. */
386
387         if (!(hwcaps = dev->driver->hwcap_get_all())) {
388                 sr_err("dev: %s: dev has no capabilities", __func__);
389                 return FALSE; /* TODO: SR_ERR*. */
390         }
391
392         for (i = 0; hwcaps[i]; i++) {
393                 if (hwcaps[i] != hwcap)
394                         continue;
395                 sr_spew("dev: %s: found hwcap %d", __func__, hwcap);
396                 return TRUE;
397         }
398
399         sr_spew("dev: %s: hwcap %d not found", __func__, hwcap);
400
401         return FALSE;
402 }
403
404 /**
405  * Returns information about the given device.
406  *
407  * @param dev Pointer to the device to be checked. Must not be NULL.
408  *            The device's 'driver' field must not be NULL either.
409  * @param id The type of information.
410  * @param data The return value. Must not be NULL.
411  *
412  * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or SR_ERR
413  *         upon other errors.
414  */
415 SR_API int sr_dev_info_get(const struct sr_dev *dev, int id, const void **data)
416 {
417         if ((dev == NULL) || (dev->driver == NULL))
418                 return SR_ERR_ARG;
419
420         if (data == NULL)
421                 return SR_ERR_ARG;
422
423         *data = dev->driver->dev_info_get(dev->driver_index, id);
424
425         if (*data == NULL)
426                 return SR_ERR;
427
428         return SR_OK;
429 }