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