]>
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 | * |
91aea754 | 51 | * @return NULL (failure) or 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 | |
ba7dd8bb UH |
58 | if (!(ch = g_try_malloc0(sizeof(struct sr_channel)))) { |
59 | sr_err("Channel malloc failed."); | |
48a486cd BV |
60 | return NULL; |
61 | } | |
62 | ||
ba7dd8bb UH |
63 | ch->index = index; |
64 | ch->type = type; | |
65 | ch->enabled = enabled; | |
48a486cd | 66 | if (name) |
ba7dd8bb | 67 | ch->name = g_strdup(name); |
48a486cd | 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 | { | |
234 | struct sr_channel *ch; | |
235 | ||
236 | if (!sdi || sdi->inst_type != SR_INST_USER || index < 0) | |
237 | return SR_ERR_ARG; | |
238 | ||
239 | ch = sr_channel_new(index, type, TRUE, name); | |
240 | if (!ch) | |
241 | return SR_ERR; | |
242 | ||
243 | sdi->channels = g_slist_append(sdi->channels, ch); | |
244 | ||
245 | return SR_OK; | |
246 | } | |
247 | ||
04cb9157 MH |
248 | /** @private |
249 | * Free device instance struct created by sr_dev_inst(). | |
7aebe22d | 250 | * @param sdi device instance to free. |
04cb9157 | 251 | */ |
48a486cd BV |
252 | SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi) |
253 | { | |
ba7dd8bb | 254 | struct sr_channel *ch; |
b1b1944e | 255 | struct sr_channel_group *cg; |
d3cff734 BV |
256 | GSList *l; |
257 | ||
ba7dd8bb UH |
258 | for (l = sdi->channels; l; l = l->next) { |
259 | ch = l->data; | |
260 | g_free(ch->name); | |
379d2609 | 261 | g_free(ch->priv); |
ba7dd8bb | 262 | g_free(ch); |
d3cff734 | 263 | } |
ba7dd8bb | 264 | g_slist_free(sdi->channels); |
d3cff734 | 265 | |
b1b1944e BV |
266 | for (l = sdi->channel_groups; l; l = l->next) { |
267 | cg = l->data; | |
7aebe22d BV |
268 | g_free(cg->name); |
269 | g_slist_free(cg->channels); | |
b1b1944e | 270 | g_free(cg->priv); |
7aebe22d | 271 | g_free(cg); |
b1b1944e BV |
272 | } |
273 | g_slist_free(sdi->channel_groups); | |
90c7f4e9 | 274 | |
48a486cd BV |
275 | g_free(sdi->vendor); |
276 | g_free(sdi->model); | |
277 | g_free(sdi->version); | |
2fe6210a SA |
278 | g_free(sdi->serial_num); |
279 | g_free(sdi->connection_id); | |
48a486cd BV |
280 | g_free(sdi); |
281 | } | |
282 | ||
283 | #ifdef HAVE_LIBUSB_1_0 | |
284 | ||
04cb9157 MH |
285 | /** @private |
286 | * Allocate and init struct for USB device instance. | |
2eb84c98 UH |
287 | * @param[in] bus @copydoc sr_usb_dev_inst::bus |
288 | * @param[in] address @copydoc sr_usb_dev_inst::address | |
289 | * @param[in] hdl @copydoc sr_usb_dev_inst::devhdl | |
04cb9157 | 290 | * |
2eb84c98 UH |
291 | * @retval NULL Error |
292 | * @retval other struct sr_usb_dev_inst * for USB device instance. | |
04cb9157 | 293 | */ |
48a486cd BV |
294 | SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus, |
295 | uint8_t address, struct libusb_device_handle *hdl) | |
296 | { | |
297 | struct sr_usb_dev_inst *udi; | |
298 | ||
299 | if (!(udi = g_try_malloc(sizeof(struct sr_usb_dev_inst)))) { | |
c4227fc6 | 300 | sr_err("USB device instance malloc failed."); |
48a486cd BV |
301 | return NULL; |
302 | } | |
303 | ||
304 | udi->bus = bus; | |
305 | udi->address = address; | |
306 | udi->devhdl = hdl; | |
307 | ||
308 | return udi; | |
309 | } | |
310 | ||
04cb9157 MH |
311 | /** @private |
312 | * Free struct * allocated by sr_usb_dev_inst(). | |
2eb84c98 | 313 | * @param usb struct* to free. Must not be NULL. |
04cb9157 | 314 | */ |
48a486cd BV |
315 | SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb) |
316 | { | |
a006798b | 317 | g_free(usb); |
48a486cd BV |
318 | } |
319 | ||
320 | #endif | |
321 | ||
c4f2dfd0 UH |
322 | #ifdef HAVE_LIBSERIALPORT |
323 | ||
9fb5f2df UH |
324 | /** |
325 | * @private | |
299bdb24 BV |
326 | * |
327 | * Both parameters are copied to newly allocated strings, and freed | |
328 | * automatically by sr_serial_dev_inst_free(). | |
9fb5f2df | 329 | * |
04cb9157 | 330 | * @param[in] port OS-specific serial port specification. Examples: |
9fb5f2df | 331 | * "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1". |
04cb9157 MH |
332 | * @param[in] serialcomm A serial communication parameters string, in the form |
333 | * of \<speed\>/\<data bits\>\<parity\>\<stopbits\>, for example | |
334 | * "9600/8n1" or "600/7o2". This is an optional parameter; | |
335 | * it may be filled in later. | |
9fb5f2df UH |
336 | * |
337 | * @return A pointer to a newly initialized struct sr_serial_dev_inst, | |
338 | * or NULL on error. | |
299bdb24 | 339 | */ |
48a486cd | 340 | SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port, |
299bdb24 | 341 | const char *serialcomm) |
48a486cd BV |
342 | { |
343 | struct sr_serial_dev_inst *serial; | |
344 | ||
299bdb24 | 345 | if (!port) { |
c4227fc6 | 346 | sr_err("Serial port required."); |
299bdb24 BV |
347 | return NULL; |
348 | } | |
349 | ||
350 | if (!(serial = g_try_malloc0(sizeof(struct sr_serial_dev_inst)))) { | |
c4227fc6 | 351 | sr_err("Serial device instance malloc failed."); |
48a486cd BV |
352 | return NULL; |
353 | } | |
354 | ||
355 | serial->port = g_strdup(port); | |
299bdb24 BV |
356 | if (serialcomm) |
357 | serial->serialcomm = g_strdup(serialcomm); | |
48a486cd BV |
358 | |
359 | return serial; | |
360 | } | |
361 | ||
04cb9157 MH |
362 | /** @private |
363 | * Free struct sr_serial_dev_inst * allocated by sr_serial_dev_inst(). | |
2eb84c98 | 364 | * @param serial struct sr_serial_dev_inst * to free. Must not be NULL. |
04cb9157 | 365 | */ |
48a486cd BV |
366 | SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial) |
367 | { | |
368 | g_free(serial->port); | |
299bdb24 | 369 | g_free(serial->serialcomm); |
acac8fc3 | 370 | g_free(serial); |
48a486cd | 371 | } |
c4f2dfd0 UH |
372 | #endif |
373 | ||
df823ac4 | 374 | /** @private */ |
ae67644f ML |
375 | SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device) |
376 | { | |
377 | struct sr_usbtmc_dev_inst *usbtmc; | |
378 | ||
379 | if (!device) { | |
380 | sr_err("Device name required."); | |
381 | return NULL; | |
382 | } | |
383 | ||
384 | if (!(usbtmc = g_try_malloc0(sizeof(struct sr_usbtmc_dev_inst)))) { | |
385 | sr_err("USBTMC device instance malloc failed."); | |
386 | return NULL; | |
387 | } | |
388 | ||
389 | usbtmc->device = g_strdup(device); | |
390 | usbtmc->fd = -1; | |
391 | ||
392 | return usbtmc; | |
393 | } | |
394 | ||
df823ac4 | 395 | /** @private */ |
ae67644f ML |
396 | SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc) |
397 | { | |
398 | g_free(usbtmc->device); | |
399 | g_free(usbtmc); | |
400 | } | |
401 | ||
576ff5b0 UH |
402 | /** |
403 | * Get the list of devices/instances of the specified driver. | |
404 | * | |
405 | * @param driver The driver to use. Must not be NULL. | |
406 | * | |
407 | * @return The list of devices/instances of this driver, or NULL upon errors | |
408 | * or if the list is empty. | |
409 | * | |
53f05fa8 | 410 | * @since 0.2.0 |
576ff5b0 | 411 | */ |
f99e32af | 412 | SR_API GSList *sr_dev_list(const struct sr_dev_driver *driver) |
811deee4 | 413 | { |
811deee4 BV |
414 | if (driver && driver->dev_list) |
415 | return driver->dev_list(); | |
416 | else | |
417 | return NULL; | |
418 | } | |
419 | ||
576ff5b0 | 420 | /** |
8102cee4 | 421 | * Clear the list of device instances a driver knows about. |
576ff5b0 | 422 | * |
8102cee4 BV |
423 | * @param driver The driver to use. This must be a pointer to one of |
424 | * the entries returned by sr_driver_list(). Must not be NULL. | |
576ff5b0 | 425 | * |
8102cee4 BV |
426 | * @retval SR_OK Success |
427 | * @retval SR_ERR_ARG Invalid driver | |
576ff5b0 UH |
428 | * |
429 | * @since 0.2.0 | |
430 | */ | |
f99e32af | 431 | SR_API int sr_dev_clear(const struct sr_dev_driver *driver) |
811deee4 | 432 | { |
8102cee4 BV |
433 | int ret; |
434 | ||
435 | if (!driver) { | |
436 | sr_err("Invalid driver."); | |
437 | return SR_ERR_ARG; | |
438 | } | |
439 | ||
440 | if (driver->dev_clear) | |
441 | ret = driver->dev_clear(); | |
811deee4 | 442 | else |
8102cee4 BV |
443 | ret = std_dev_clear(driver, NULL); |
444 | ||
445 | return ret; | |
811deee4 BV |
446 | } |
447 | ||
576ff5b0 UH |
448 | /** |
449 | * Open the specified device. | |
450 | * | |
451 | * @param sdi Device instance to use. Must not be NULL. | |
452 | * | |
453 | * @return SR_OK upon success, a negative error code upon errors. | |
454 | * | |
455 | * @since 0.2.0 | |
456 | */ | |
efdecf4c BV |
457 | SR_API int sr_dev_open(struct sr_dev_inst *sdi) |
458 | { | |
459 | int ret; | |
460 | ||
461 | if (!sdi || !sdi->driver || !sdi->driver->dev_open) | |
462 | return SR_ERR; | |
463 | ||
464 | ret = sdi->driver->dev_open(sdi); | |
465 | ||
466 | return ret; | |
467 | } | |
468 | ||
576ff5b0 UH |
469 | /** |
470 | * Close the specified device. | |
471 | * | |
472 | * @param sdi Device instance to use. Must not be NULL. | |
473 | * | |
474 | * @return SR_OK upon success, a negative error code upon errors. | |
475 | * | |
476 | * @since 0.2.0 | |
477 | */ | |
efdecf4c BV |
478 | SR_API int sr_dev_close(struct sr_dev_inst *sdi) |
479 | { | |
480 | int ret; | |
481 | ||
482 | if (!sdi || !sdi->driver || !sdi->driver->dev_close) | |
483 | return SR_ERR; | |
484 | ||
485 | ret = sdi->driver->dev_close(sdi); | |
486 | ||
487 | return ret; | |
488 | } | |
489 | ||
0157fdce SA |
490 | /** |
491 | * Queries a device instances' driver. | |
492 | * | |
493 | * @param sdi Device instance to use. Must not be NULL. | |
494 | * | |
495 | * @return The driver instance or NULL on error. | |
496 | */ | |
2f5f9705 | 497 | SR_API struct sr_dev_driver *sr_dev_inst_driver_get(const struct sr_dev_inst *sdi) |
0157fdce SA |
498 | { |
499 | if (!sdi || !sdi->driver) | |
500 | return NULL; | |
501 | ||
502 | return sdi->driver; | |
503 | } | |
504 | ||
505 | /** | |
506 | * Queries a device instances' vendor. | |
507 | * | |
508 | * @param sdi Device instance to use. Must not be NULL. | |
509 | * | |
510 | * @return The vendor string or NULL. | |
511 | */ | |
2f5f9705 | 512 | SR_API const char *sr_dev_inst_vendor_get(const struct sr_dev_inst *sdi) |
0157fdce SA |
513 | { |
514 | if (!sdi) | |
515 | return NULL; | |
516 | ||
517 | return sdi->vendor; | |
518 | } | |
519 | ||
520 | /** | |
521 | * Queries a device instances' model. | |
522 | * | |
523 | * @param sdi Device instance to use. Must not be NULL. | |
524 | * | |
525 | * @return The model string or NULL. | |
526 | */ | |
2f5f9705 | 527 | SR_API const char *sr_dev_inst_model_get(const struct sr_dev_inst *sdi) |
0157fdce SA |
528 | { |
529 | if (!sdi) | |
530 | return NULL; | |
531 | ||
532 | return sdi->model; | |
533 | } | |
534 | ||
535 | /** | |
536 | * Queries a device instances' version. | |
537 | * | |
538 | * @param sdi Device instance to use. Must not be NULL. | |
539 | * | |
540 | * @return The version string or NULL. | |
541 | */ | |
2f5f9705 | 542 | SR_API const char *sr_dev_inst_version_get(const struct sr_dev_inst *sdi) |
0157fdce SA |
543 | { |
544 | if (!sdi) | |
545 | return NULL; | |
546 | ||
547 | return sdi->version; | |
548 | } | |
549 | ||
550 | /** | |
551 | * Queries a device instances' serial number. | |
552 | * | |
553 | * @param sdi Device instance to use. Must not be NULL. | |
554 | * | |
555 | * @return The serial number string or NULL. | |
556 | */ | |
2f5f9705 | 557 | SR_API const char *sr_dev_inst_sernum_get(const struct sr_dev_inst *sdi) |
0157fdce SA |
558 | { |
559 | if (!sdi) | |
560 | return NULL; | |
561 | ||
562 | return sdi->serial_num; | |
563 | } | |
564 | ||
565 | /** | |
566 | * Queries a device instances' connection identifier. | |
567 | * | |
568 | * @param sdi Device instance to use. Must not be NULL. | |
569 | * | |
570 | * @return A copy of the connection id string or NULL. The caller is responsible | |
571 | * for g_free()ing the string when it is no longer needed. | |
572 | */ | |
2f5f9705 | 573 | SR_API const char *sr_dev_inst_connid_get(const struct sr_dev_inst *sdi) |
0157fdce SA |
574 | { |
575 | struct drv_context *drvc; | |
9c6a2913 SA |
576 | int r, cnt, i, a, b; |
577 | char connection_id[64]; | |
578 | ||
579 | #ifdef HAVE_LIBUSB_1_0 | |
0157fdce SA |
580 | struct sr_usb_dev_inst *usb; |
581 | struct libusb_device **devlist; | |
582 | struct libusb_device_descriptor des; | |
9c6a2913 | 583 | #endif |
0157fdce SA |
584 | |
585 | if (!sdi) | |
586 | return NULL; | |
587 | ||
9c6a2913 | 588 | #ifdef HAVE_LIBSERIALPORT |
e8cbb223 SA |
589 | struct sr_serial_dev_inst *serial; |
590 | ||
591 | if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_SERIAL)) { | |
592 | /* connection_id isn't populated, let's do that here. */ | |
593 | ||
594 | serial = sdi->conn; | |
2f5f9705 | 595 | ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(serial->port); |
e8cbb223 | 596 | } |
9c6a2913 | 597 | #endif |
e8cbb223 SA |
598 | |
599 | ||
9c6a2913 | 600 | #ifdef HAVE_LIBUSB_1_0 |
0157fdce SA |
601 | if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_USB)) { |
602 | /* connection_id isn't populated, let's do that here. */ | |
603 | ||
604 | drvc = sdi->driver->priv; | |
605 | usb = sdi->conn; | |
606 | ||
607 | if ((cnt = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist)) < 0) { | |
608 | sr_err("Failed to retrieve device list: %s.", | |
609 | libusb_error_name(cnt)); | |
610 | return NULL; | |
611 | } | |
612 | ||
613 | for (i = 0; i < cnt; i++) { | |
614 | if ((r = libusb_get_device_descriptor(devlist[i], &des)) < 0) { | |
615 | sr_err("Failed to get device descriptor: %s.", | |
616 | libusb_error_name(r)); | |
617 | continue; | |
618 | } | |
619 | ||
620 | /* Find the USB device by the logical address we know. */ | |
621 | b = libusb_get_bus_number(devlist[i]); | |
622 | a = libusb_get_device_address(devlist[i]); | |
623 | if (b != usb->bus || a != usb->address) | |
624 | continue; | |
625 | ||
626 | usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)); | |
2f5f9705 | 627 | ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(connection_id); |
0157fdce SA |
628 | break; |
629 | } | |
630 | ||
631 | libusb_free_device_list(devlist, 1); | |
632 | } | |
9c6a2913 | 633 | #endif |
0157fdce SA |
634 | |
635 | return sdi->connection_id; | |
636 | } | |
637 | ||
e437da2b UH |
638 | /** |
639 | * Queries a device instances' channel list. | |
640 | * | |
641 | * @param sdi Device instance to use. Must not be NULL. | |
642 | * | |
643 | * @return The GSList of channels or NULL. | |
644 | */ | |
2f5f9705 | 645 | SR_API GSList *sr_dev_inst_channels_get(const struct sr_dev_inst *sdi) |
e437da2b UH |
646 | { |
647 | if (!sdi) | |
648 | return NULL; | |
649 | ||
650 | return sdi->channels; | |
651 | } | |
652 | ||
653 | /** | |
654 | * Queries a device instances' channel groups list. | |
655 | * | |
656 | * @param sdi Device instance to use. Must not be NULL. | |
657 | * | |
658 | * @return The GSList of channel groups or NULL. | |
659 | */ | |
2f5f9705 | 660 | SR_API GSList *sr_dev_inst_channel_groups_get(const struct sr_dev_inst *sdi) |
e437da2b UH |
661 | { |
662 | if (!sdi) | |
663 | return NULL; | |
664 | ||
665 | return sdi->channel_groups; | |
666 | } | |
667 | ||
7b870c38 | 668 | /** @} */ |