]>
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 | * | |
2a854d71 DE |
119 | * @return SR_OK on success or SR_ERR on failure. In case of invalid |
120 | * arguments, SR_ERR_ARG is returned and the probe enabled state | |
121 | * remains unchanged. | |
9fb5f2df UH |
122 | * |
123 | * @since 0.2.0 | |
be5bf44d BV |
124 | */ |
125 | SR_API int sr_dev_probe_enable(const struct sr_dev_inst *sdi, int probenum, | |
126 | gboolean state) | |
127 | { | |
128 | GSList *l; | |
129 | struct sr_probe *probe; | |
130 | int ret; | |
2a854d71 | 131 | gboolean was_enabled; |
be5bf44d BV |
132 | |
133 | if (!sdi) | |
134 | return SR_ERR_ARG; | |
135 | ||
136 | ret = SR_ERR_ARG; | |
137 | for (l = sdi->probes; l; l = l->next) { | |
138 | probe = l->data; | |
139 | if (probe->index == probenum) { | |
2a854d71 | 140 | was_enabled = probe->enabled; |
be5bf44d BV |
141 | probe->enabled = state; |
142 | ret = SR_OK; | |
2a854d71 DE |
143 | if (!state != !was_enabled && sdi->driver |
144 | && sdi->driver->config_probe_set) { | |
145 | ret = sdi->driver->config_probe_set( | |
146 | sdi, probe, SR_PROBE_SET_ENABLED); | |
147 | /* Roll back change if it wasn't applicable. */ | |
148 | if (ret == SR_ERR_ARG) | |
149 | probe->enabled = was_enabled; | |
150 | } | |
be5bf44d BV |
151 | break; |
152 | } | |
153 | } | |
154 | ||
155 | return ret; | |
156 | } | |
157 | ||
94799bc4 | 158 | /** |
01c3e9db UH |
159 | * Add a trigger to the specified device (and the specified probe). |
160 | * | |
161 | * If the specified probe of this device already has a trigger, it will | |
162 | * be silently replaced. | |
94799bc4 | 163 | * |
04cb9157 MH |
164 | * @param[in,out] sdi Pointer to the device instance; must not be NULL. |
165 | * @param[in] probenum Number of probe, starting at 0. | |
166 | * @param[in] trigger Trigger string, in the format used by sigrok-cli | |
0e3b1439 | 167 | * |
2a854d71 DE |
168 | * @return SR_OK on success or SR_ERR on failure. In case of invalid |
169 | * arguments, SR_ERR_ARG is returned and the trigger settings | |
170 | * remain unchanged. | |
9fb5f2df | 171 | * |
53f05fa8 | 172 | * @since 0.2.0 |
94799bc4 | 173 | */ |
58453e58 BV |
174 | SR_API int sr_dev_trigger_set(const struct sr_dev_inst *sdi, int probenum, |
175 | const char *trigger) | |
a1bb33af | 176 | { |
58453e58 BV |
177 | GSList *l; |
178 | struct sr_probe *probe; | |
2a854d71 | 179 | char *old_trigger; |
58453e58 | 180 | int ret; |
a1bb33af | 181 | |
58453e58 | 182 | if (!sdi) |
0e3b1439 | 183 | return SR_ERR_ARG; |
94799bc4 | 184 | |
58453e58 BV |
185 | ret = SR_ERR_ARG; |
186 | for (l = sdi->probes; l; l = l->next) { | |
187 | probe = l->data; | |
188 | if (probe->index == probenum) { | |
2a854d71 | 189 | old_trigger = probe->trigger; |
58453e58 | 190 | ret = SR_OK; |
2a854d71 DE |
191 | if (g_strcmp0(trigger, old_trigger) == 0) |
192 | break; | |
193 | /* Set new trigger if it has changed. */ | |
194 | probe->trigger = g_strdup(trigger); | |
195 | ||
196 | if (sdi->driver && sdi->driver->config_probe_set) { | |
197 | ret = sdi->driver->config_probe_set( | |
198 | sdi, probe, SR_PROBE_SET_TRIGGER); | |
199 | /* Roll back change if it wasn't applicable. */ | |
200 | if (ret == SR_ERR_ARG) { | |
201 | g_free(probe->trigger); | |
202 | probe->trigger = old_trigger; | |
203 | break; | |
204 | } | |
205 | } | |
206 | g_free(old_trigger); | |
58453e58 BV |
207 | break; |
208 | } | |
94799bc4 | 209 | } |
a1bb33af | 210 | |
58453e58 | 211 | return ret; |
7d658874 BV |
212 | } |
213 | ||
94799bc4 | 214 | /** |
9c5332d2 UH |
215 | * Determine whether the specified device instance has the specified |
216 | * capability. | |
94799bc4 | 217 | * |
9c5332d2 | 218 | * @param sdi Pointer to the device instance to be checked. Must not be NULL. |
8ec95d22 UH |
219 | * If the device's 'driver' field is NULL (virtual device), this |
220 | * function will always return FALSE (virtual devices don't have | |
221 | * a hardware capabilities list). | |
04cb9157 | 222 | * @param[in] key The option that should be checked for is supported by the |
4d15e5c9 | 223 | * specified device. |
94799bc4 | 224 | * |
04cb9157 MH |
225 | * @retval TRUE Device has the specified option |
226 | * @retval FALSE Device does not have the specified option, invalid input | |
227 | * parameters or other error conditions. | |
9fb5f2df | 228 | * |
53f05fa8 | 229 | * @since 0.2.0 |
94799bc4 | 230 | */ |
4d15e5c9 | 231 | SR_API gboolean sr_dev_has_option(const struct sr_dev_inst *sdi, int key) |
7d658874 | 232 | { |
003595ac | 233 | GVariant *gvar; |
4d15e5c9 | 234 | const int *devopts; |
003595ac BV |
235 | gsize num_opts, i; |
236 | int ret; | |
7d658874 | 237 | |
003595ac | 238 | if (!sdi || !sdi->driver || !sdi->driver->config_list) |
8ec95d22 | 239 | return FALSE; |
94799bc4 | 240 | |
8f996b89 | 241 | if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS, |
92b68bb5 | 242 | &gvar, sdi, NULL) != SR_OK) |
8ec95d22 | 243 | return FALSE; |
94799bc4 | 244 | |
003595ac BV |
245 | ret = FALSE; |
246 | devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t)); | |
247 | for (i = 0; i < num_opts; i++) { | |
248 | if (devopts[i] == key) { | |
249 | ret = TRUE; | |
250 | break; | |
251 | } | |
94799bc4 | 252 | } |
003595ac | 253 | g_variant_unref(gvar); |
218557b8 | 254 | |
003595ac | 255 | return ret; |
a1bb33af | 256 | } |
fd9836bf | 257 | |
04cb9157 MH |
258 | /** @private |
259 | * Allocate and init new device instance struct. | |
2eb84c98 UH |
260 | * @param[in] index @copydoc sr_dev_inst::index |
261 | * @param[in] status @copydoc sr_dev_inst::status | |
262 | * @param[in] vendor @copydoc sr_dev_inst::vendor | |
263 | * @param[in] model @copydoc sr_dev_inst::model | |
264 | * @param[in] version @copydoc sr_dev_inst::version | |
04cb9157 | 265 | * |
2eb84c98 UH |
266 | * @retval NULL Error |
267 | * @retval struct sr_dev_inst *. Dynamically allocated, free using | |
04cb9157 MH |
268 | * sr_dev_inst_free(). |
269 | */ | |
48a486cd BV |
270 | SR_PRIV struct sr_dev_inst *sr_dev_inst_new(int index, int status, |
271 | const char *vendor, const char *model, const char *version) | |
272 | { | |
273 | struct sr_dev_inst *sdi; | |
274 | ||
275 | if (!(sdi = g_try_malloc(sizeof(struct sr_dev_inst)))) { | |
c4227fc6 | 276 | sr_err("Device instance malloc failed."); |
48a486cd BV |
277 | return NULL; |
278 | } | |
279 | ||
e8d3d6c8 | 280 | sdi->driver = NULL; |
48a486cd BV |
281 | sdi->index = index; |
282 | sdi->status = status; | |
283 | sdi->inst_type = -1; | |
284 | sdi->vendor = vendor ? g_strdup(vendor) : NULL; | |
285 | sdi->model = model ? g_strdup(model) : NULL; | |
286 | sdi->version = version ? g_strdup(version) : NULL; | |
287 | sdi->probes = NULL; | |
909cc050 | 288 | sdi->probe_groups = NULL; |
9e2e9864 | 289 | sdi->conn = NULL; |
48a486cd BV |
290 | sdi->priv = NULL; |
291 | ||
292 | return sdi; | |
293 | } | |
294 | ||
04cb9157 MH |
295 | /** @private |
296 | * Free device instance struct created by sr_dev_inst(). | |
2eb84c98 | 297 | * @param sdi struct* to free. |
04cb9157 | 298 | */ |
48a486cd BV |
299 | SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi) |
300 | { | |
d3cff734 BV |
301 | struct sr_probe *probe; |
302 | GSList *l; | |
303 | ||
304 | for (l = sdi->probes; l; l = l->next) { | |
305 | probe = l->data; | |
306 | g_free(probe->name); | |
a006798b | 307 | g_free(probe->trigger); |
d3cff734 BV |
308 | g_free(probe); |
309 | } | |
a006798b | 310 | g_slist_free(sdi->probes); |
d3cff734 | 311 | |
90c7f4e9 DJ |
312 | if (sdi->probe_groups) |
313 | g_slist_free(sdi->probe_groups); | |
314 | ||
48a486cd BV |
315 | g_free(sdi->vendor); |
316 | g_free(sdi->model); | |
317 | g_free(sdi->version); | |
318 | g_free(sdi); | |
319 | } | |
320 | ||
321 | #ifdef HAVE_LIBUSB_1_0 | |
322 | ||
04cb9157 MH |
323 | /** @private |
324 | * Allocate and init struct for USB device instance. | |
2eb84c98 UH |
325 | * @param[in] bus @copydoc sr_usb_dev_inst::bus |
326 | * @param[in] address @copydoc sr_usb_dev_inst::address | |
327 | * @param[in] hdl @copydoc sr_usb_dev_inst::devhdl | |
04cb9157 | 328 | * |
2eb84c98 UH |
329 | * @retval NULL Error |
330 | * @retval other struct sr_usb_dev_inst * for USB device instance. | |
04cb9157 | 331 | */ |
48a486cd BV |
332 | SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus, |
333 | uint8_t address, struct libusb_device_handle *hdl) | |
334 | { | |
335 | struct sr_usb_dev_inst *udi; | |
336 | ||
337 | if (!(udi = g_try_malloc(sizeof(struct sr_usb_dev_inst)))) { | |
c4227fc6 | 338 | sr_err("USB device instance malloc failed."); |
48a486cd BV |
339 | return NULL; |
340 | } | |
341 | ||
342 | udi->bus = bus; | |
343 | udi->address = address; | |
344 | udi->devhdl = hdl; | |
345 | ||
346 | return udi; | |
347 | } | |
348 | ||
04cb9157 MH |
349 | /** @private |
350 | * Free struct * allocated by sr_usb_dev_inst(). | |
2eb84c98 | 351 | * @param usb struct* to free. Must not be NULL. |
04cb9157 | 352 | */ |
48a486cd BV |
353 | SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb) |
354 | { | |
a006798b | 355 | g_free(usb); |
48a486cd BV |
356 | } |
357 | ||
358 | #endif | |
359 | ||
c4f2dfd0 UH |
360 | #ifdef HAVE_LIBSERIALPORT |
361 | ||
9fb5f2df UH |
362 | /** |
363 | * @private | |
299bdb24 BV |
364 | * |
365 | * Both parameters are copied to newly allocated strings, and freed | |
366 | * automatically by sr_serial_dev_inst_free(). | |
9fb5f2df | 367 | * |
04cb9157 | 368 | * @param[in] port OS-specific serial port specification. Examples: |
9fb5f2df | 369 | * "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1". |
04cb9157 MH |
370 | * @param[in] serialcomm A serial communication parameters string, in the form |
371 | * of \<speed\>/\<data bits\>\<parity\>\<stopbits\>, for example | |
372 | * "9600/8n1" or "600/7o2". This is an optional parameter; | |
373 | * it may be filled in later. | |
9fb5f2df UH |
374 | * |
375 | * @return A pointer to a newly initialized struct sr_serial_dev_inst, | |
376 | * or NULL on error. | |
299bdb24 | 377 | */ |
48a486cd | 378 | SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port, |
299bdb24 | 379 | const char *serialcomm) |
48a486cd BV |
380 | { |
381 | struct sr_serial_dev_inst *serial; | |
382 | ||
299bdb24 | 383 | if (!port) { |
c4227fc6 | 384 | sr_err("Serial port required."); |
299bdb24 BV |
385 | return NULL; |
386 | } | |
387 | ||
388 | if (!(serial = g_try_malloc0(sizeof(struct sr_serial_dev_inst)))) { | |
c4227fc6 | 389 | sr_err("Serial device instance malloc failed."); |
48a486cd BV |
390 | return NULL; |
391 | } | |
392 | ||
393 | serial->port = g_strdup(port); | |
299bdb24 BV |
394 | if (serialcomm) |
395 | serial->serialcomm = g_strdup(serialcomm); | |
48a486cd BV |
396 | |
397 | return serial; | |
398 | } | |
399 | ||
04cb9157 MH |
400 | /** @private |
401 | * Free struct sr_serial_dev_inst * allocated by sr_serial_dev_inst(). | |
2eb84c98 | 402 | * @param serial struct sr_serial_dev_inst * to free. Must not be NULL. |
04cb9157 | 403 | */ |
48a486cd BV |
404 | SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial) |
405 | { | |
406 | g_free(serial->port); | |
299bdb24 | 407 | g_free(serial->serialcomm); |
acac8fc3 | 408 | g_free(serial); |
48a486cd | 409 | } |
c4f2dfd0 UH |
410 | #endif |
411 | ||
df823ac4 | 412 | /** @private */ |
ae67644f ML |
413 | SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device) |
414 | { | |
415 | struct sr_usbtmc_dev_inst *usbtmc; | |
416 | ||
417 | if (!device) { | |
418 | sr_err("Device name required."); | |
419 | return NULL; | |
420 | } | |
421 | ||
422 | if (!(usbtmc = g_try_malloc0(sizeof(struct sr_usbtmc_dev_inst)))) { | |
423 | sr_err("USBTMC device instance malloc failed."); | |
424 | return NULL; | |
425 | } | |
426 | ||
427 | usbtmc->device = g_strdup(device); | |
428 | usbtmc->fd = -1; | |
429 | ||
430 | return usbtmc; | |
431 | } | |
432 | ||
df823ac4 | 433 | /** @private */ |
ae67644f ML |
434 | SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc) |
435 | { | |
436 | g_free(usbtmc->device); | |
437 | g_free(usbtmc); | |
438 | } | |
439 | ||
576ff5b0 UH |
440 | /** |
441 | * Get the list of devices/instances of the specified driver. | |
442 | * | |
443 | * @param driver The driver to use. Must not be NULL. | |
444 | * | |
445 | * @return The list of devices/instances of this driver, or NULL upon errors | |
446 | * or if the list is empty. | |
447 | * | |
53f05fa8 | 448 | * @since 0.2.0 |
576ff5b0 | 449 | */ |
f99e32af | 450 | SR_API GSList *sr_dev_list(const struct sr_dev_driver *driver) |
811deee4 | 451 | { |
811deee4 BV |
452 | if (driver && driver->dev_list) |
453 | return driver->dev_list(); | |
454 | else | |
455 | return NULL; | |
456 | } | |
457 | ||
576ff5b0 | 458 | /** |
8102cee4 | 459 | * Clear the list of device instances a driver knows about. |
576ff5b0 | 460 | * |
8102cee4 BV |
461 | * @param driver The driver to use. This must be a pointer to one of |
462 | * the entries returned by sr_driver_list(). Must not be NULL. | |
576ff5b0 | 463 | * |
8102cee4 BV |
464 | * @retval SR_OK Success |
465 | * @retval SR_ERR_ARG Invalid driver | |
576ff5b0 UH |
466 | * |
467 | * @since 0.2.0 | |
468 | */ | |
f99e32af | 469 | SR_API int sr_dev_clear(const struct sr_dev_driver *driver) |
811deee4 | 470 | { |
8102cee4 BV |
471 | int ret; |
472 | ||
473 | if (!driver) { | |
474 | sr_err("Invalid driver."); | |
475 | return SR_ERR_ARG; | |
476 | } | |
477 | ||
478 | if (driver->dev_clear) | |
479 | ret = driver->dev_clear(); | |
811deee4 | 480 | else |
8102cee4 BV |
481 | ret = std_dev_clear(driver, NULL); |
482 | ||
483 | return ret; | |
811deee4 BV |
484 | } |
485 | ||
576ff5b0 UH |
486 | /** |
487 | * Open the specified device. | |
488 | * | |
489 | * @param sdi Device instance to use. Must not be NULL. | |
490 | * | |
491 | * @return SR_OK upon success, a negative error code upon errors. | |
492 | * | |
493 | * @since 0.2.0 | |
494 | */ | |
efdecf4c BV |
495 | SR_API int sr_dev_open(struct sr_dev_inst *sdi) |
496 | { | |
497 | int ret; | |
498 | ||
499 | if (!sdi || !sdi->driver || !sdi->driver->dev_open) | |
500 | return SR_ERR; | |
501 | ||
502 | ret = sdi->driver->dev_open(sdi); | |
503 | ||
504 | return ret; | |
505 | } | |
506 | ||
576ff5b0 UH |
507 | /** |
508 | * Close the specified device. | |
509 | * | |
510 | * @param sdi Device instance to use. Must not be NULL. | |
511 | * | |
512 | * @return SR_OK upon success, a negative error code upon errors. | |
513 | * | |
514 | * @since 0.2.0 | |
515 | */ | |
efdecf4c BV |
516 | SR_API int sr_dev_close(struct sr_dev_inst *sdi) |
517 | { | |
518 | int ret; | |
519 | ||
520 | if (!sdi || !sdi->driver || !sdi->driver->dev_close) | |
521 | return SR_ERR; | |
522 | ||
523 | ret = sdi->driver->dev_close(sdi); | |
524 | ||
525 | return ret; | |
526 | } | |
527 | ||
7b870c38 | 528 | /** @} */ |