]>
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 | |
2ad1deb8 | 26 | /** @cond PRIVATE */ |
3544f848 | 27 | #define LOG_PREFIX "device" |
2ad1deb8 | 28 | /** @endcond */ |
a885ce3e | 29 | |
393fb9cb UH |
30 | /** |
31 | * @file | |
32 | * | |
33 | * Device handling in libsigrok. | |
34 | */ | |
35 | ||
7b870c38 UH |
36 | /** |
37 | * @defgroup grp_devices Devices | |
38 | * | |
39 | * Device handling in libsigrok. | |
40 | * | |
41 | * @{ | |
42 | */ | |
43 | ||
04cb9157 | 44 | /** @private |
91aea754 UH |
45 | * Allocate and initialize new struct sr_channel |
46 | * @param[in] index @copydoc sr_channel::index | |
47 | * @param[in] type @copydoc sr_channel::type | |
48 | * @param[in] enabled @copydoc sr_channel::enabled | |
49 | * @param[in] name @copydoc sr_channel::name | |
04cb9157 | 50 | * |
c368e6f3 | 51 | * @return A new struct sr_channel*. |
04cb9157 | 52 | */ |
56d0d245 | 53 | SR_PRIV struct sr_channel *sr_channel_new(int index, int type, |
48a486cd BV |
54 | gboolean enabled, const char *name) |
55 | { | |
ba7dd8bb | 56 | struct sr_channel *ch; |
48a486cd | 57 | |
c368e6f3 | 58 | ch = g_malloc0(sizeof(struct sr_channel)); |
ba7dd8bb UH |
59 | ch->index = index; |
60 | ch->type = type; | |
61 | ch->enabled = enabled; | |
48a486cd | 62 | if (name) |
ba7dd8bb | 63 | ch->name = g_strdup(name); |
48a486cd | 64 | |
ba7dd8bb | 65 | return ch; |
48a486cd BV |
66 | } |
67 | ||
94799bc4 | 68 | /** |
ba7dd8bb | 69 | * Set the name of the specified channel in the specified device. |
94799bc4 | 70 | * |
ba7dd8bb | 71 | * If the channel already has a different name assigned to it, it will be |
94799bc4 UH |
72 | * removed, and the new name will be saved instead. |
73 | * | |
ba7dd8bb UH |
74 | * @param sdi The device instance the channel is connected to. |
75 | * @param[in] channelnum The number of the channel whose name to set. | |
76 | * Note that the channel numbers start at 0. | |
77 | * @param[in] name The new name that the specified channel should get. A copy | |
37e8b4c4 | 78 | * of the string is made. |
0e3b1439 | 79 | * |
37e8b4c4 | 80 | * @return SR_OK on success, or SR_ERR_ARG on invalid arguments. |
9fb5f2df | 81 | * |
47117241 | 82 | * @since 0.3.0 |
94799bc4 | 83 | */ |
f3ca73ed | 84 | SR_API int sr_dev_channel_name_set(const struct sr_dev_inst *sdi, |
ba7dd8bb | 85 | int channelnum, const char *name) |
a1bb33af | 86 | { |
37e8b4c4 | 87 | GSList *l; |
ba7dd8bb | 88 | struct sr_channel *ch; |
37e8b4c4 | 89 | int ret; |
a1bb33af | 90 | |
37e8b4c4 BV |
91 | if (!sdi) { |
92 | sr_err("%s: sdi was NULL", __func__); | |
0e3b1439 | 93 | return SR_ERR_ARG; |
94799bc4 UH |
94 | } |
95 | ||
37e8b4c4 | 96 | ret = SR_ERR_ARG; |
ba7dd8bb UH |
97 | for (l = sdi->channels; l; l = l->next) { |
98 | ch = l->data; | |
99 | if (ch->index == channelnum) { | |
100 | g_free(ch->name); | |
101 | ch->name = g_strdup(name); | |
37e8b4c4 BV |
102 | ret = SR_OK; |
103 | break; | |
104 | } | |
94799bc4 UH |
105 | } |
106 | ||
37e8b4c4 | 107 | return ret; |
a1bb33af UH |
108 | } |
109 | ||
be5bf44d | 110 | /** |
ba7dd8bb | 111 | * Enable or disable a channel on the specified device. |
be5bf44d | 112 | * |
ba7dd8bb UH |
113 | * @param sdi The device instance the channel is connected to. |
114 | * @param channelnum The channel number, starting from 0. | |
115 | * @param state TRUE to enable the channel, FALSE to disable. | |
be5bf44d | 116 | * |
2a854d71 | 117 | * @return SR_OK on success or SR_ERR on failure. In case of invalid |
ba7dd8bb | 118 | * arguments, SR_ERR_ARG is returned and the channel enabled state |
2a854d71 | 119 | * remains unchanged. |
9fb5f2df | 120 | * |
47117241 | 121 | * @since 0.3.0 |
be5bf44d | 122 | */ |
f3ca73ed | 123 | SR_API int sr_dev_channel_enable(const struct sr_dev_inst *sdi, int channelnum, |
be5bf44d BV |
124 | gboolean state) |
125 | { | |
126 | GSList *l; | |
ba7dd8bb | 127 | struct sr_channel *ch; |
be5bf44d | 128 | int ret; |
2a854d71 | 129 | gboolean was_enabled; |
be5bf44d BV |
130 | |
131 | if (!sdi) | |
132 | return SR_ERR_ARG; | |
133 | ||
134 | ret = SR_ERR_ARG; | |
ba7dd8bb UH |
135 | for (l = sdi->channels; l; l = l->next) { |
136 | ch = l->data; | |
137 | if (ch->index == channelnum) { | |
138 | was_enabled = ch->enabled; | |
139 | ch->enabled = state; | |
be5bf44d | 140 | ret = SR_OK; |
2a854d71 | 141 | if (!state != !was_enabled && sdi->driver |
f3ca73ed UH |
142 | && sdi->driver->config_channel_set) { |
143 | ret = sdi->driver->config_channel_set( | |
3f239f08 | 144 | sdi, ch, SR_CHANNEL_SET_ENABLED); |
2a854d71 DE |
145 | /* Roll back change if it wasn't applicable. */ |
146 | if (ret == SR_ERR_ARG) | |
ba7dd8bb | 147 | ch->enabled = was_enabled; |
2a854d71 | 148 | } |
be5bf44d BV |
149 | break; |
150 | } | |
151 | } | |
152 | ||
153 | return ret; | |
154 | } | |
155 | ||
94799bc4 | 156 | /** |
9c5332d2 UH |
157 | * Determine whether the specified device instance has the specified |
158 | * capability. | |
94799bc4 | 159 | * |
9c5332d2 | 160 | * @param sdi Pointer to the device instance to be checked. Must not be NULL. |
8ec95d22 UH |
161 | * If the device's 'driver' field is NULL (virtual device), this |
162 | * function will always return FALSE (virtual devices don't have | |
163 | * a hardware capabilities list). | |
04cb9157 | 164 | * @param[in] key The option that should be checked for is supported by the |
4d15e5c9 | 165 | * specified device. |
94799bc4 | 166 | * |
04cb9157 MH |
167 | * @retval TRUE Device has the specified option |
168 | * @retval FALSE Device does not have the specified option, invalid input | |
169 | * parameters or other error conditions. | |
9fb5f2df | 170 | * |
53f05fa8 | 171 | * @since 0.2.0 |
94799bc4 | 172 | */ |
4d15e5c9 | 173 | SR_API gboolean sr_dev_has_option(const struct sr_dev_inst *sdi, int key) |
7d658874 | 174 | { |
003595ac | 175 | GVariant *gvar; |
4d15e5c9 | 176 | const int *devopts; |
003595ac BV |
177 | gsize num_opts, i; |
178 | int ret; | |
7d658874 | 179 | |
003595ac | 180 | if (!sdi || !sdi->driver || !sdi->driver->config_list) |
8ec95d22 | 181 | return FALSE; |
94799bc4 | 182 | |
8f996b89 | 183 | if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS, |
92b68bb5 | 184 | &gvar, sdi, NULL) != SR_OK) |
8ec95d22 | 185 | return FALSE; |
94799bc4 | 186 | |
003595ac BV |
187 | ret = FALSE; |
188 | devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t)); | |
189 | for (i = 0; i < num_opts; i++) { | |
d099d880 | 190 | if ((devopts[i] & SR_CONF_MASK) == key) { |
003595ac BV |
191 | ret = TRUE; |
192 | break; | |
193 | } | |
94799bc4 | 194 | } |
003595ac | 195 | g_variant_unref(gvar); |
218557b8 | 196 | |
003595ac | 197 | return ret; |
a1bb33af | 198 | } |
fd9836bf | 199 | |
e705ce3b UH |
200 | /** |
201 | * Allocate and init a new user-generated device instance. | |
aac29cc1 UH |
202 | * |
203 | * @param vendor Device vendor | |
204 | * @param model Device model | |
205 | * @param version Device version | |
206 | * | |
207 | * @retval struct sr_dev_inst *. Dynamically allocated, free using | |
208 | * sr_dev_inst_free(). | |
e705ce3b UH |
209 | */ |
210 | SR_API struct sr_dev_inst *sr_dev_inst_user_new(const char *vendor, | |
211 | const char *model, const char *version) | |
212 | { | |
213 | struct sr_dev_inst *sdi; | |
214 | ||
aac29cc1 UH |
215 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); |
216 | ||
0af636be UH |
217 | sdi->vendor = g_strdup(vendor); |
218 | sdi->model = g_strdup(model); | |
219 | sdi->version = g_strdup(version); | |
e705ce3b UH |
220 | sdi->inst_type = SR_INST_USER; |
221 | ||
222 | return sdi; | |
223 | } | |
224 | ||
225 | /** | |
226 | * Add a new channel to the specified device instance. | |
227 | */ | |
228 | SR_API int sr_dev_inst_channel_add(struct sr_dev_inst *sdi, int index, int type, const char *name) | |
229 | { | |
230 | struct sr_channel *ch; | |
231 | ||
232 | if (!sdi || sdi->inst_type != SR_INST_USER || index < 0) | |
233 | return SR_ERR_ARG; | |
234 | ||
235 | ch = sr_channel_new(index, type, TRUE, name); | |
e705ce3b UH |
236 | sdi->channels = g_slist_append(sdi->channels, ch); |
237 | ||
238 | return SR_OK; | |
239 | } | |
240 | ||
04cb9157 MH |
241 | /** @private |
242 | * Free device instance struct created by sr_dev_inst(). | |
7aebe22d | 243 | * @param sdi device instance to free. |
04cb9157 | 244 | */ |
48a486cd BV |
245 | SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi) |
246 | { | |
ba7dd8bb | 247 | struct sr_channel *ch; |
b1b1944e | 248 | struct sr_channel_group *cg; |
d3cff734 BV |
249 | GSList *l; |
250 | ||
ba7dd8bb UH |
251 | for (l = sdi->channels; l; l = l->next) { |
252 | ch = l->data; | |
253 | g_free(ch->name); | |
379d2609 | 254 | g_free(ch->priv); |
ba7dd8bb | 255 | g_free(ch); |
d3cff734 | 256 | } |
ba7dd8bb | 257 | g_slist_free(sdi->channels); |
d3cff734 | 258 | |
b1b1944e BV |
259 | for (l = sdi->channel_groups; l; l = l->next) { |
260 | cg = l->data; | |
7aebe22d BV |
261 | g_free(cg->name); |
262 | g_slist_free(cg->channels); | |
b1b1944e | 263 | g_free(cg->priv); |
7aebe22d | 264 | g_free(cg); |
b1b1944e BV |
265 | } |
266 | g_slist_free(sdi->channel_groups); | |
90c7f4e9 | 267 | |
48a486cd BV |
268 | g_free(sdi->vendor); |
269 | g_free(sdi->model); | |
270 | g_free(sdi->version); | |
2fe6210a SA |
271 | g_free(sdi->serial_num); |
272 | g_free(sdi->connection_id); | |
48a486cd BV |
273 | g_free(sdi); |
274 | } | |
275 | ||
276 | #ifdef HAVE_LIBUSB_1_0 | |
277 | ||
04cb9157 MH |
278 | /** @private |
279 | * Allocate and init struct for USB device instance. | |
2eb84c98 UH |
280 | * @param[in] bus @copydoc sr_usb_dev_inst::bus |
281 | * @param[in] address @copydoc sr_usb_dev_inst::address | |
282 | * @param[in] hdl @copydoc sr_usb_dev_inst::devhdl | |
04cb9157 | 283 | * |
2eb84c98 UH |
284 | * @retval NULL Error |
285 | * @retval other struct sr_usb_dev_inst * for USB device instance. | |
04cb9157 | 286 | */ |
48a486cd BV |
287 | SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus, |
288 | uint8_t address, struct libusb_device_handle *hdl) | |
289 | { | |
290 | struct sr_usb_dev_inst *udi; | |
291 | ||
292 | if (!(udi = g_try_malloc(sizeof(struct sr_usb_dev_inst)))) { | |
c4227fc6 | 293 | sr_err("USB device instance malloc failed."); |
48a486cd BV |
294 | return NULL; |
295 | } | |
296 | ||
297 | udi->bus = bus; | |
298 | udi->address = address; | |
299 | udi->devhdl = hdl; | |
300 | ||
301 | return udi; | |
302 | } | |
303 | ||
04cb9157 MH |
304 | /** @private |
305 | * Free struct * allocated by sr_usb_dev_inst(). | |
2eb84c98 | 306 | * @param usb struct* to free. Must not be NULL. |
04cb9157 | 307 | */ |
48a486cd BV |
308 | SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb) |
309 | { | |
a006798b | 310 | g_free(usb); |
48a486cd BV |
311 | } |
312 | ||
313 | #endif | |
314 | ||
c4f2dfd0 UH |
315 | #ifdef HAVE_LIBSERIALPORT |
316 | ||
9fb5f2df UH |
317 | /** |
318 | * @private | |
299bdb24 BV |
319 | * |
320 | * Both parameters are copied to newly allocated strings, and freed | |
321 | * automatically by sr_serial_dev_inst_free(). | |
9fb5f2df | 322 | * |
04cb9157 | 323 | * @param[in] port OS-specific serial port specification. Examples: |
9fb5f2df | 324 | * "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1". |
04cb9157 MH |
325 | * @param[in] serialcomm A serial communication parameters string, in the form |
326 | * of \<speed\>/\<data bits\>\<parity\>\<stopbits\>, for example | |
327 | * "9600/8n1" or "600/7o2". This is an optional parameter; | |
328 | * it may be filled in later. | |
9fb5f2df UH |
329 | * |
330 | * @return A pointer to a newly initialized struct sr_serial_dev_inst, | |
331 | * or NULL on error. | |
299bdb24 | 332 | */ |
48a486cd | 333 | SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port, |
299bdb24 | 334 | const char *serialcomm) |
48a486cd BV |
335 | { |
336 | struct sr_serial_dev_inst *serial; | |
337 | ||
299bdb24 | 338 | if (!port) { |
c4227fc6 | 339 | sr_err("Serial port required."); |
299bdb24 BV |
340 | return NULL; |
341 | } | |
342 | ||
343 | if (!(serial = g_try_malloc0(sizeof(struct sr_serial_dev_inst)))) { | |
c4227fc6 | 344 | sr_err("Serial device instance malloc failed."); |
48a486cd BV |
345 | return NULL; |
346 | } | |
347 | ||
348 | serial->port = g_strdup(port); | |
299bdb24 BV |
349 | if (serialcomm) |
350 | serial->serialcomm = g_strdup(serialcomm); | |
48a486cd BV |
351 | |
352 | return serial; | |
353 | } | |
354 | ||
04cb9157 MH |
355 | /** @private |
356 | * Free struct sr_serial_dev_inst * allocated by sr_serial_dev_inst(). | |
2eb84c98 | 357 | * @param serial struct sr_serial_dev_inst * to free. Must not be NULL. |
04cb9157 | 358 | */ |
48a486cd BV |
359 | SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial) |
360 | { | |
361 | g_free(serial->port); | |
299bdb24 | 362 | g_free(serial->serialcomm); |
acac8fc3 | 363 | g_free(serial); |
48a486cd | 364 | } |
c4f2dfd0 UH |
365 | #endif |
366 | ||
df823ac4 | 367 | /** @private */ |
ae67644f ML |
368 | SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device) |
369 | { | |
370 | struct sr_usbtmc_dev_inst *usbtmc; | |
371 | ||
372 | if (!device) { | |
373 | sr_err("Device name required."); | |
374 | return NULL; | |
375 | } | |
376 | ||
377 | if (!(usbtmc = g_try_malloc0(sizeof(struct sr_usbtmc_dev_inst)))) { | |
378 | sr_err("USBTMC device instance malloc failed."); | |
379 | return NULL; | |
380 | } | |
381 | ||
382 | usbtmc->device = g_strdup(device); | |
383 | usbtmc->fd = -1; | |
384 | ||
385 | return usbtmc; | |
386 | } | |
387 | ||
df823ac4 | 388 | /** @private */ |
ae67644f ML |
389 | SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc) |
390 | { | |
391 | g_free(usbtmc->device); | |
392 | g_free(usbtmc); | |
393 | } | |
394 | ||
576ff5b0 UH |
395 | /** |
396 | * Get the list of devices/instances of the specified driver. | |
397 | * | |
398 | * @param driver The driver to use. Must not be NULL. | |
399 | * | |
400 | * @return The list of devices/instances of this driver, or NULL upon errors | |
401 | * or if the list is empty. | |
402 | * | |
53f05fa8 | 403 | * @since 0.2.0 |
576ff5b0 | 404 | */ |
f99e32af | 405 | SR_API GSList *sr_dev_list(const struct sr_dev_driver *driver) |
811deee4 | 406 | { |
811deee4 BV |
407 | if (driver && driver->dev_list) |
408 | return driver->dev_list(); | |
409 | else | |
410 | return NULL; | |
411 | } | |
412 | ||
576ff5b0 | 413 | /** |
8102cee4 | 414 | * Clear the list of device instances a driver knows about. |
576ff5b0 | 415 | * |
8102cee4 BV |
416 | * @param driver The driver to use. This must be a pointer to one of |
417 | * the entries returned by sr_driver_list(). Must not be NULL. | |
576ff5b0 | 418 | * |
8102cee4 BV |
419 | * @retval SR_OK Success |
420 | * @retval SR_ERR_ARG Invalid driver | |
576ff5b0 UH |
421 | * |
422 | * @since 0.2.0 | |
423 | */ | |
f99e32af | 424 | SR_API int sr_dev_clear(const struct sr_dev_driver *driver) |
811deee4 | 425 | { |
8102cee4 BV |
426 | int ret; |
427 | ||
428 | if (!driver) { | |
429 | sr_err("Invalid driver."); | |
430 | return SR_ERR_ARG; | |
431 | } | |
432 | ||
433 | if (driver->dev_clear) | |
434 | ret = driver->dev_clear(); | |
811deee4 | 435 | else |
8102cee4 BV |
436 | ret = std_dev_clear(driver, NULL); |
437 | ||
438 | return ret; | |
811deee4 BV |
439 | } |
440 | ||
576ff5b0 UH |
441 | /** |
442 | * Open the specified device. | |
443 | * | |
444 | * @param sdi Device instance to use. Must not be NULL. | |
445 | * | |
446 | * @return SR_OK upon success, a negative error code upon errors. | |
447 | * | |
448 | * @since 0.2.0 | |
449 | */ | |
efdecf4c BV |
450 | SR_API int sr_dev_open(struct sr_dev_inst *sdi) |
451 | { | |
452 | int ret; | |
453 | ||
454 | if (!sdi || !sdi->driver || !sdi->driver->dev_open) | |
455 | return SR_ERR; | |
456 | ||
457 | ret = sdi->driver->dev_open(sdi); | |
458 | ||
459 | return ret; | |
460 | } | |
461 | ||
576ff5b0 UH |
462 | /** |
463 | * Close the specified device. | |
464 | * | |
465 | * @param sdi Device instance to use. Must not be NULL. | |
466 | * | |
467 | * @return SR_OK upon success, a negative error code upon errors. | |
468 | * | |
469 | * @since 0.2.0 | |
470 | */ | |
efdecf4c BV |
471 | SR_API int sr_dev_close(struct sr_dev_inst *sdi) |
472 | { | |
473 | int ret; | |
474 | ||
475 | if (!sdi || !sdi->driver || !sdi->driver->dev_close) | |
476 | return SR_ERR; | |
477 | ||
478 | ret = sdi->driver->dev_close(sdi); | |
479 | ||
480 | return ret; | |
481 | } | |
482 | ||
0157fdce SA |
483 | /** |
484 | * Queries a device instances' driver. | |
485 | * | |
486 | * @param sdi Device instance to use. Must not be NULL. | |
487 | * | |
488 | * @return The driver instance or NULL on error. | |
489 | */ | |
2f5f9705 | 490 | SR_API struct sr_dev_driver *sr_dev_inst_driver_get(const struct sr_dev_inst *sdi) |
0157fdce SA |
491 | { |
492 | if (!sdi || !sdi->driver) | |
493 | return NULL; | |
494 | ||
495 | return sdi->driver; | |
496 | } | |
497 | ||
498 | /** | |
499 | * Queries a device instances' vendor. | |
500 | * | |
501 | * @param sdi Device instance to use. Must not be NULL. | |
502 | * | |
503 | * @return The vendor string or NULL. | |
504 | */ | |
2f5f9705 | 505 | SR_API const char *sr_dev_inst_vendor_get(const struct sr_dev_inst *sdi) |
0157fdce SA |
506 | { |
507 | if (!sdi) | |
508 | return NULL; | |
509 | ||
510 | return sdi->vendor; | |
511 | } | |
512 | ||
513 | /** | |
514 | * Queries a device instances' model. | |
515 | * | |
516 | * @param sdi Device instance to use. Must not be NULL. | |
517 | * | |
518 | * @return The model string or NULL. | |
519 | */ | |
2f5f9705 | 520 | SR_API const char *sr_dev_inst_model_get(const struct sr_dev_inst *sdi) |
0157fdce SA |
521 | { |
522 | if (!sdi) | |
523 | return NULL; | |
524 | ||
525 | return sdi->model; | |
526 | } | |
527 | ||
528 | /** | |
529 | * Queries a device instances' version. | |
530 | * | |
531 | * @param sdi Device instance to use. Must not be NULL. | |
532 | * | |
533 | * @return The version string or NULL. | |
534 | */ | |
2f5f9705 | 535 | SR_API const char *sr_dev_inst_version_get(const struct sr_dev_inst *sdi) |
0157fdce SA |
536 | { |
537 | if (!sdi) | |
538 | return NULL; | |
539 | ||
540 | return sdi->version; | |
541 | } | |
542 | ||
543 | /** | |
544 | * Queries a device instances' serial number. | |
545 | * | |
546 | * @param sdi Device instance to use. Must not be NULL. | |
547 | * | |
548 | * @return The serial number string or NULL. | |
549 | */ | |
2f5f9705 | 550 | SR_API const char *sr_dev_inst_sernum_get(const struct sr_dev_inst *sdi) |
0157fdce SA |
551 | { |
552 | if (!sdi) | |
553 | return NULL; | |
554 | ||
555 | return sdi->serial_num; | |
556 | } | |
557 | ||
558 | /** | |
559 | * Queries a device instances' connection identifier. | |
560 | * | |
561 | * @param sdi Device instance to use. Must not be NULL. | |
562 | * | |
563 | * @return A copy of the connection id string or NULL. The caller is responsible | |
564 | * for g_free()ing the string when it is no longer needed. | |
565 | */ | |
2f5f9705 | 566 | SR_API const char *sr_dev_inst_connid_get(const struct sr_dev_inst *sdi) |
0157fdce SA |
567 | { |
568 | struct drv_context *drvc; | |
9c6a2913 SA |
569 | int r, cnt, i, a, b; |
570 | char connection_id[64]; | |
571 | ||
572 | #ifdef HAVE_LIBUSB_1_0 | |
0157fdce SA |
573 | struct sr_usb_dev_inst *usb; |
574 | struct libusb_device **devlist; | |
575 | struct libusb_device_descriptor des; | |
9c6a2913 | 576 | #endif |
0157fdce SA |
577 | |
578 | if (!sdi) | |
579 | return NULL; | |
580 | ||
9c6a2913 | 581 | #ifdef HAVE_LIBSERIALPORT |
e8cbb223 SA |
582 | struct sr_serial_dev_inst *serial; |
583 | ||
584 | if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_SERIAL)) { | |
585 | /* connection_id isn't populated, let's do that here. */ | |
586 | ||
587 | serial = sdi->conn; | |
2f5f9705 | 588 | ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(serial->port); |
e8cbb223 | 589 | } |
9c6a2913 | 590 | #endif |
e8cbb223 SA |
591 | |
592 | ||
9c6a2913 | 593 | #ifdef HAVE_LIBUSB_1_0 |
0157fdce SA |
594 | if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_USB)) { |
595 | /* connection_id isn't populated, let's do that here. */ | |
596 | ||
597 | drvc = sdi->driver->priv; | |
598 | usb = sdi->conn; | |
599 | ||
600 | if ((cnt = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist)) < 0) { | |
601 | sr_err("Failed to retrieve device list: %s.", | |
602 | libusb_error_name(cnt)); | |
603 | return NULL; | |
604 | } | |
605 | ||
606 | for (i = 0; i < cnt; i++) { | |
607 | if ((r = libusb_get_device_descriptor(devlist[i], &des)) < 0) { | |
608 | sr_err("Failed to get device descriptor: %s.", | |
609 | libusb_error_name(r)); | |
610 | continue; | |
611 | } | |
612 | ||
613 | /* Find the USB device by the logical address we know. */ | |
614 | b = libusb_get_bus_number(devlist[i]); | |
615 | a = libusb_get_device_address(devlist[i]); | |
616 | if (b != usb->bus || a != usb->address) | |
617 | continue; | |
618 | ||
619 | usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)); | |
2f5f9705 | 620 | ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(connection_id); |
0157fdce SA |
621 | break; |
622 | } | |
623 | ||
624 | libusb_free_device_list(devlist, 1); | |
625 | } | |
9c6a2913 | 626 | #endif |
0157fdce SA |
627 | |
628 | return sdi->connection_id; | |
629 | } | |
630 | ||
e437da2b UH |
631 | /** |
632 | * Queries a device instances' channel list. | |
633 | * | |
634 | * @param sdi Device instance to use. Must not be NULL. | |
635 | * | |
636 | * @return The GSList of channels or NULL. | |
637 | */ | |
2f5f9705 | 638 | SR_API GSList *sr_dev_inst_channels_get(const struct sr_dev_inst *sdi) |
e437da2b UH |
639 | { |
640 | if (!sdi) | |
641 | return NULL; | |
642 | ||
643 | return sdi->channels; | |
644 | } | |
645 | ||
646 | /** | |
647 | * Queries a device instances' channel groups list. | |
648 | * | |
649 | * @param sdi Device instance to use. Must not be NULL. | |
650 | * | |
651 | * @return The GSList of channel groups or NULL. | |
652 | */ | |
2f5f9705 | 653 | SR_API GSList *sr_dev_inst_channel_groups_get(const struct sr_dev_inst *sdi) |
e437da2b UH |
654 | { |
655 | if (!sdi) | |
656 | return NULL; | |
657 | ||
658 | return sdi->channel_groups; | |
659 | } | |
660 | ||
7b870c38 | 661 | /** @} */ |