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