]>
Commit | Line | Data |
---|---|---|
a1bb33af | 1 | /* |
50985c20 | 2 | * This file is part of the libsigrok project. |
a1bb33af | 3 | * |
13d8e03c | 4 | * Copyright (C) 2013 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> | |
545f9786 | 22 | #include "config.h" /* Needed for HAVE_LIBUSB_1_0 and others. */ |
45c59c8b BV |
23 | #include "libsigrok.h" |
24 | #include "libsigrok-internal.h" | |
a1bb33af | 25 | |
3544f848 | 26 | #define LOG_PREFIX "device" |
a885ce3e | 27 | |
393fb9cb UH |
28 | /** |
29 | * @file | |
30 | * | |
31 | * Device handling in libsigrok. | |
32 | */ | |
33 | ||
7b870c38 UH |
34 | /** |
35 | * @defgroup grp_devices Devices | |
36 | * | |
37 | * Device handling in libsigrok. | |
38 | * | |
39 | * @{ | |
40 | */ | |
41 | ||
04cb9157 MH |
42 | /** @private |
43 | * Allocate and initialize new struct sr_probe | |
2eb84c98 UH |
44 | * @param[in] index @copydoc sr_probe::index |
45 | * @param[in] type @copydoc sr_probe::type | |
46 | * @param[in] enabled @copydoc sr_probe::enabled | |
47 | * @param[in] name @copydoc sr_probe::name | |
04cb9157 MH |
48 | * |
49 | * @return NULL (failure) or new struct sr_probe*. | |
50 | */ | |
48a486cd BV |
51 | SR_PRIV struct sr_probe *sr_probe_new(int index, int type, |
52 | gboolean enabled, const char *name) | |
53 | { | |
54 | struct sr_probe *probe; | |
55 | ||
56 | if (!(probe = g_try_malloc0(sizeof(struct sr_probe)))) { | |
a885ce3e | 57 | sr_err("Probe malloc failed."); |
48a486cd BV |
58 | return NULL; |
59 | } | |
60 | ||
61 | probe->index = index; | |
62 | probe->type = type; | |
63 | probe->enabled = enabled; | |
64 | if (name) | |
65 | probe->name = g_strdup(name); | |
66 | ||
67 | return probe; | |
68 | } | |
69 | ||
94799bc4 UH |
70 | /** |
71 | * Set the name of the specified probe in the specified device. | |
72 | * | |
73 | * If the probe already has a different name assigned to it, it will be | |
74 | * removed, and the new name will be saved instead. | |
75 | * | |
37e8b4c4 | 76 | * @param sdi The device instance the probe is connected to. |
04cb9157 | 77 | * @param[in] probenum The number of the probe whose name to set. |
37e8b4c4 | 78 | * Note that the probe numbers start at 0. |
04cb9157 | 79 | * @param[in] name The new name that the specified probe should get. A copy |
37e8b4c4 | 80 | * of the string is made. |
0e3b1439 | 81 | * |
37e8b4c4 | 82 | * @return SR_OK on success, or SR_ERR_ARG on invalid arguments. |
9fb5f2df | 83 | * |
53f05fa8 | 84 | * @since 0.2.0 |
94799bc4 | 85 | */ |
37e8b4c4 BV |
86 | SR_API int sr_dev_probe_name_set(const struct sr_dev_inst *sdi, |
87 | int probenum, const char *name) | |
a1bb33af | 88 | { |
37e8b4c4 BV |
89 | GSList *l; |
90 | struct sr_probe *probe; | |
91 | int ret; | |
a1bb33af | 92 | |
37e8b4c4 BV |
93 | if (!sdi) { |
94 | sr_err("%s: sdi was NULL", __func__); | |
0e3b1439 | 95 | return SR_ERR_ARG; |
94799bc4 UH |
96 | } |
97 | ||
37e8b4c4 BV |
98 | ret = SR_ERR_ARG; |
99 | for (l = sdi->probes; l; l = l->next) { | |
100 | probe = l->data; | |
101 | if (probe->index == probenum) { | |
102 | g_free(probe->name); | |
103 | probe->name = g_strdup(name); | |
104 | ret = SR_OK; | |
105 | break; | |
106 | } | |
94799bc4 UH |
107 | } |
108 | ||
37e8b4c4 | 109 | return ret; |
a1bb33af UH |
110 | } |
111 | ||
be5bf44d BV |
112 | /** |
113 | * Enable or disable a probe on the specified device. | |
114 | * | |
115 | * @param sdi The device instance the probe is connected to. | |
116 | * @param probenum The probe number, starting from 0. | |
117 | * @param state TRUE to enable the probe, FALSE to disable. | |
118 | * | |
119 | * @return SR_OK on success, or SR_ERR_ARG on invalid arguments. | |
9fb5f2df UH |
120 | * |
121 | * @since 0.2.0 | |
be5bf44d BV |
122 | */ |
123 | SR_API int sr_dev_probe_enable(const struct sr_dev_inst *sdi, int probenum, | |
124 | gboolean state) | |
125 | { | |
126 | GSList *l; | |
127 | struct sr_probe *probe; | |
128 | int ret; | |
129 | ||
130 | if (!sdi) | |
131 | return SR_ERR_ARG; | |
132 | ||
133 | ret = SR_ERR_ARG; | |
134 | for (l = sdi->probes; l; l = l->next) { | |
135 | probe = l->data; | |
136 | if (probe->index == probenum) { | |
137 | probe->enabled = state; | |
138 | ret = SR_OK; | |
139 | break; | |
140 | } | |
141 | } | |
142 | ||
143 | return ret; | |
144 | } | |
145 | ||
94799bc4 | 146 | /** |
01c3e9db UH |
147 | * Add a trigger to the specified device (and the specified probe). |
148 | * | |
149 | * If the specified probe of this device already has a trigger, it will | |
150 | * be silently replaced. | |
94799bc4 | 151 | * |
04cb9157 MH |
152 | * @param[in,out] sdi Pointer to the device instance; must not be NULL. |
153 | * @param[in] probenum Number of probe, starting at 0. | |
154 | * @param[in] trigger Trigger string, in the format used by sigrok-cli | |
0e3b1439 | 155 | * |
a5f2e707 | 156 | * @return SR_OK on success, or SR_ERR_ARG on invalid arguments. |
9fb5f2df | 157 | * |
53f05fa8 | 158 | * @since 0.2.0 |
94799bc4 | 159 | */ |
58453e58 BV |
160 | SR_API int sr_dev_trigger_set(const struct sr_dev_inst *sdi, int probenum, |
161 | const char *trigger) | |
a1bb33af | 162 | { |
58453e58 BV |
163 | GSList *l; |
164 | struct sr_probe *probe; | |
165 | int ret; | |
a1bb33af | 166 | |
58453e58 | 167 | if (!sdi) |
0e3b1439 | 168 | return SR_ERR_ARG; |
94799bc4 | 169 | |
58453e58 BV |
170 | ret = SR_ERR_ARG; |
171 | for (l = sdi->probes; l; l = l->next) { | |
172 | probe = l->data; | |
173 | if (probe->index == probenum) { | |
174 | /* If the probe already has a trigger, kill it first. */ | |
175 | g_free(probe->trigger); | |
176 | probe->trigger = g_strdup(trigger); | |
177 | ret = SR_OK; | |
178 | break; | |
179 | } | |
94799bc4 | 180 | } |
a1bb33af | 181 | |
58453e58 | 182 | return ret; |
7d658874 BV |
183 | } |
184 | ||
94799bc4 | 185 | /** |
9c5332d2 UH |
186 | * Determine whether the specified device instance has the specified |
187 | * capability. | |
94799bc4 | 188 | * |
9c5332d2 | 189 | * @param sdi Pointer to the device instance to be checked. Must not be NULL. |
8ec95d22 UH |
190 | * If the device's 'driver' field is NULL (virtual device), this |
191 | * function will always return FALSE (virtual devices don't have | |
192 | * a hardware capabilities list). | |
04cb9157 | 193 | * @param[in] key The option that should be checked for is supported by the |
4d15e5c9 | 194 | * specified device. |
94799bc4 | 195 | * |
04cb9157 MH |
196 | * @retval TRUE Device has the specified option |
197 | * @retval FALSE Device does not have the specified option, invalid input | |
198 | * parameters or other error conditions. | |
9fb5f2df | 199 | * |
53f05fa8 | 200 | * @since 0.2.0 |
94799bc4 | 201 | */ |
4d15e5c9 | 202 | SR_API gboolean sr_dev_has_option(const struct sr_dev_inst *sdi, int key) |
7d658874 | 203 | { |
003595ac | 204 | GVariant *gvar; |
4d15e5c9 | 205 | const int *devopts; |
003595ac BV |
206 | gsize num_opts, i; |
207 | int ret; | |
7d658874 | 208 | |
003595ac | 209 | if (!sdi || !sdi->driver || !sdi->driver->config_list) |
8ec95d22 | 210 | return FALSE; |
94799bc4 | 211 | |
8f996b89 | 212 | if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS, |
92b68bb5 | 213 | &gvar, sdi, NULL) != SR_OK) |
8ec95d22 | 214 | return FALSE; |
94799bc4 | 215 | |
003595ac BV |
216 | ret = FALSE; |
217 | devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t)); | |
218 | for (i = 0; i < num_opts; i++) { | |
219 | if (devopts[i] == key) { | |
220 | ret = TRUE; | |
221 | break; | |
222 | } | |
94799bc4 | 223 | } |
003595ac | 224 | g_variant_unref(gvar); |
218557b8 | 225 | |
003595ac | 226 | return ret; |
a1bb33af | 227 | } |
fd9836bf | 228 | |
04cb9157 MH |
229 | /** @private |
230 | * Allocate and init new device instance struct. | |
2eb84c98 UH |
231 | * @param[in] index @copydoc sr_dev_inst::index |
232 | * @param[in] status @copydoc sr_dev_inst::status | |
233 | * @param[in] vendor @copydoc sr_dev_inst::vendor | |
234 | * @param[in] model @copydoc sr_dev_inst::model | |
235 | * @param[in] version @copydoc sr_dev_inst::version | |
04cb9157 | 236 | * |
2eb84c98 UH |
237 | * @retval NULL Error |
238 | * @retval struct sr_dev_inst *. Dynamically allocated, free using | |
04cb9157 MH |
239 | * sr_dev_inst_free(). |
240 | */ | |
48a486cd BV |
241 | SR_PRIV struct sr_dev_inst *sr_dev_inst_new(int index, int status, |
242 | const char *vendor, const char *model, const char *version) | |
243 | { | |
244 | struct sr_dev_inst *sdi; | |
245 | ||
246 | if (!(sdi = g_try_malloc(sizeof(struct sr_dev_inst)))) { | |
c4227fc6 | 247 | sr_err("Device instance malloc failed."); |
48a486cd BV |
248 | return NULL; |
249 | } | |
250 | ||
e8d3d6c8 | 251 | sdi->driver = NULL; |
48a486cd BV |
252 | sdi->index = index; |
253 | sdi->status = status; | |
254 | sdi->inst_type = -1; | |
255 | sdi->vendor = vendor ? g_strdup(vendor) : NULL; | |
256 | sdi->model = model ? g_strdup(model) : NULL; | |
257 | sdi->version = version ? g_strdup(version) : NULL; | |
258 | sdi->probes = NULL; | |
909cc050 | 259 | sdi->probe_groups = NULL; |
9e2e9864 | 260 | sdi->conn = NULL; |
48a486cd BV |
261 | sdi->priv = NULL; |
262 | ||
263 | return sdi; | |
264 | } | |
265 | ||
04cb9157 MH |
266 | /** @private |
267 | * Free device instance struct created by sr_dev_inst(). | |
2eb84c98 | 268 | * @param sdi struct* to free. |
04cb9157 | 269 | */ |
48a486cd BV |
270 | SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi) |
271 | { | |
d3cff734 BV |
272 | struct sr_probe *probe; |
273 | GSList *l; | |
274 | ||
275 | for (l = sdi->probes; l; l = l->next) { | |
276 | probe = l->data; | |
277 | g_free(probe->name); | |
a006798b | 278 | g_free(probe->trigger); |
d3cff734 BV |
279 | g_free(probe); |
280 | } | |
a006798b | 281 | g_slist_free(sdi->probes); |
d3cff734 | 282 | |
90c7f4e9 DJ |
283 | if (sdi->probe_groups) |
284 | g_slist_free(sdi->probe_groups); | |
285 | ||
48a486cd BV |
286 | g_free(sdi->vendor); |
287 | g_free(sdi->model); | |
288 | g_free(sdi->version); | |
289 | g_free(sdi); | |
290 | } | |
291 | ||
292 | #ifdef HAVE_LIBUSB_1_0 | |
293 | ||
04cb9157 MH |
294 | /** @private |
295 | * Allocate and init struct for USB device instance. | |
2eb84c98 UH |
296 | * @param[in] bus @copydoc sr_usb_dev_inst::bus |
297 | * @param[in] address @copydoc sr_usb_dev_inst::address | |
298 | * @param[in] hdl @copydoc sr_usb_dev_inst::devhdl | |
04cb9157 | 299 | * |
2eb84c98 UH |
300 | * @retval NULL Error |
301 | * @retval other struct sr_usb_dev_inst * for USB device instance. | |
04cb9157 | 302 | */ |
48a486cd BV |
303 | SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus, |
304 | uint8_t address, struct libusb_device_handle *hdl) | |
305 | { | |
306 | struct sr_usb_dev_inst *udi; | |
307 | ||
308 | if (!(udi = g_try_malloc(sizeof(struct sr_usb_dev_inst)))) { | |
c4227fc6 | 309 | sr_err("USB device instance malloc failed."); |
48a486cd BV |
310 | return NULL; |
311 | } | |
312 | ||
313 | udi->bus = bus; | |
314 | udi->address = address; | |
315 | udi->devhdl = hdl; | |
316 | ||
317 | return udi; | |
318 | } | |
319 | ||
04cb9157 MH |
320 | /** @private |
321 | * Free struct * allocated by sr_usb_dev_inst(). | |
2eb84c98 | 322 | * @param usb struct* to free. Must not be NULL. |
04cb9157 | 323 | */ |
48a486cd BV |
324 | SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb) |
325 | { | |
a006798b | 326 | g_free(usb); |
48a486cd BV |
327 | } |
328 | ||
329 | #endif | |
330 | ||
c4f2dfd0 UH |
331 | #ifdef HAVE_LIBSERIALPORT |
332 | ||
9fb5f2df UH |
333 | /** |
334 | * @private | |
299bdb24 BV |
335 | * |
336 | * Both parameters are copied to newly allocated strings, and freed | |
337 | * automatically by sr_serial_dev_inst_free(). | |
9fb5f2df | 338 | * |
04cb9157 | 339 | * @param[in] port OS-specific serial port specification. Examples: |
9fb5f2df | 340 | * "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1". |
04cb9157 MH |
341 | * @param[in] serialcomm A serial communication parameters string, in the form |
342 | * of \<speed\>/\<data bits\>\<parity\>\<stopbits\>, for example | |
343 | * "9600/8n1" or "600/7o2". This is an optional parameter; | |
344 | * it may be filled in later. | |
9fb5f2df UH |
345 | * |
346 | * @return A pointer to a newly initialized struct sr_serial_dev_inst, | |
347 | * or NULL on error. | |
299bdb24 | 348 | */ |
48a486cd | 349 | SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port, |
299bdb24 | 350 | const char *serialcomm) |
48a486cd BV |
351 | { |
352 | struct sr_serial_dev_inst *serial; | |
353 | ||
299bdb24 | 354 | if (!port) { |
c4227fc6 | 355 | sr_err("Serial port required."); |
299bdb24 BV |
356 | return NULL; |
357 | } | |
358 | ||
359 | if (!(serial = g_try_malloc0(sizeof(struct sr_serial_dev_inst)))) { | |
c4227fc6 | 360 | sr_err("Serial device instance malloc failed."); |
48a486cd BV |
361 | return NULL; |
362 | } | |
363 | ||
364 | serial->port = g_strdup(port); | |
299bdb24 BV |
365 | if (serialcomm) |
366 | serial->serialcomm = g_strdup(serialcomm); | |
48a486cd BV |
367 | |
368 | return serial; | |
369 | } | |
370 | ||
04cb9157 MH |
371 | /** @private |
372 | * Free struct sr_serial_dev_inst * allocated by sr_serial_dev_inst(). | |
2eb84c98 | 373 | * @param serial struct sr_serial_dev_inst * to free. Must not be NULL. |
04cb9157 | 374 | */ |
48a486cd BV |
375 | SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial) |
376 | { | |
377 | g_free(serial->port); | |
299bdb24 | 378 | g_free(serial->serialcomm); |
acac8fc3 | 379 | g_free(serial); |
48a486cd | 380 | } |
c4f2dfd0 UH |
381 | #endif |
382 | ||
df823ac4 | 383 | /** @private */ |
ae67644f ML |
384 | SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device) |
385 | { | |
386 | struct sr_usbtmc_dev_inst *usbtmc; | |
387 | ||
388 | if (!device) { | |
389 | sr_err("Device name required."); | |
390 | return NULL; | |
391 | } | |
392 | ||
393 | if (!(usbtmc = g_try_malloc0(sizeof(struct sr_usbtmc_dev_inst)))) { | |
394 | sr_err("USBTMC device instance malloc failed."); | |
395 | return NULL; | |
396 | } | |
397 | ||
398 | usbtmc->device = g_strdup(device); | |
399 | usbtmc->fd = -1; | |
400 | ||
401 | return usbtmc; | |
402 | } | |
403 | ||
df823ac4 | 404 | /** @private */ |
ae67644f ML |
405 | SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc) |
406 | { | |
407 | g_free(usbtmc->device); | |
408 | g_free(usbtmc); | |
409 | } | |
410 | ||
576ff5b0 UH |
411 | /** |
412 | * Get the list of devices/instances of the specified driver. | |
413 | * | |
414 | * @param driver The driver to use. Must not be NULL. | |
415 | * | |
416 | * @return The list of devices/instances of this driver, or NULL upon errors | |
417 | * or if the list is empty. | |
418 | * | |
53f05fa8 | 419 | * @since 0.2.0 |
576ff5b0 | 420 | */ |
f99e32af | 421 | SR_API GSList *sr_dev_list(const struct sr_dev_driver *driver) |
811deee4 | 422 | { |
811deee4 BV |
423 | if (driver && driver->dev_list) |
424 | return driver->dev_list(); | |
425 | else | |
426 | return NULL; | |
427 | } | |
428 | ||
576ff5b0 UH |
429 | /** |
430 | * Clear all devices/instances of the specified driver. | |
431 | * | |
432 | * @param driver The driver to use. Must not be NULL. | |
433 | * | |
434 | * @return SR_OK upon success, a negative error code upon errors. | |
435 | * | |
436 | * @since 0.2.0 | |
437 | */ | |
f99e32af | 438 | SR_API int sr_dev_clear(const struct sr_dev_driver *driver) |
811deee4 | 439 | { |
811deee4 BV |
440 | if (driver && driver->dev_clear) |
441 | return driver->dev_clear(); | |
442 | else | |
443 | return SR_OK; | |
444 | } | |
445 | ||
576ff5b0 UH |
446 | /** |
447 | * Open the specified device. | |
448 | * | |
449 | * @param sdi Device instance to use. Must not be NULL. | |
450 | * | |
451 | * @return SR_OK upon success, a negative error code upon errors. | |
452 | * | |
453 | * @since 0.2.0 | |
454 | */ | |
efdecf4c BV |
455 | SR_API int sr_dev_open(struct sr_dev_inst *sdi) |
456 | { | |
457 | int ret; | |
458 | ||
459 | if (!sdi || !sdi->driver || !sdi->driver->dev_open) | |
460 | return SR_ERR; | |
461 | ||
462 | ret = sdi->driver->dev_open(sdi); | |
463 | ||
464 | return ret; | |
465 | } | |
466 | ||
576ff5b0 UH |
467 | /** |
468 | * Close the specified device. | |
469 | * | |
470 | * @param sdi Device instance to use. Must not be NULL. | |
471 | * | |
472 | * @return SR_OK upon success, a negative error code upon errors. | |
473 | * | |
474 | * @since 0.2.0 | |
475 | */ | |
efdecf4c BV |
476 | SR_API int sr_dev_close(struct sr_dev_inst *sdi) |
477 | { | |
478 | int ret; | |
479 | ||
480 | if (!sdi || !sdi->driver || !sdi->driver->dev_close) | |
481 | return SR_ERR; | |
482 | ||
483 | ret = sdi->driver->dev_close(sdi); | |
484 | ||
485 | return ret; | |
486 | } | |
487 | ||
7b870c38 | 488 | /** @} */ |