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