]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013 Bert Vermeulen <bert@biot.com> | |
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 <config.h> | |
21 | #include <stdio.h> | |
22 | #include <glib.h> | |
23 | #include <libsigrok/libsigrok.h> | |
24 | #include "libsigrok-internal.h" | |
25 | ||
26 | /** @cond PRIVATE */ | |
27 | #define LOG_PREFIX "device" | |
28 | /** @endcond */ | |
29 | ||
30 | /** | |
31 | * @file | |
32 | * | |
33 | * Device handling in libsigrok. | |
34 | */ | |
35 | ||
36 | /** | |
37 | * @defgroup grp_devices Devices | |
38 | * | |
39 | * Device handling in libsigrok. | |
40 | * | |
41 | * @{ | |
42 | */ | |
43 | ||
44 | /** | |
45 | * Allocate and initialize a new struct sr_channel and add it to sdi. | |
46 | * | |
47 | * @param[in] sdi The device instance the channel is connected to. | |
48 | * Must not be NULL. | |
49 | * @param[in] index @copydoc sr_channel::index | |
50 | * @param[in] type @copydoc sr_channel::type | |
51 | * @param[in] enabled @copydoc sr_channel::enabled | |
52 | * @param[in] name @copydoc sr_channel::name | |
53 | * | |
54 | * @return A new struct sr_channel*. | |
55 | * | |
56 | * @private | |
57 | */ | |
58 | SR_PRIV struct sr_channel *sr_channel_new(struct sr_dev_inst *sdi, | |
59 | int index, int type, gboolean enabled, const char *name) | |
60 | { | |
61 | struct sr_channel *ch; | |
62 | ||
63 | ch = g_malloc0(sizeof(struct sr_channel)); | |
64 | ch->sdi = sdi; | |
65 | ch->index = index; | |
66 | ch->type = type; | |
67 | ch->enabled = enabled; | |
68 | if (name) | |
69 | ch->name = g_strdup(name); | |
70 | ||
71 | sdi->channels = g_slist_append(sdi->channels, ch); | |
72 | ||
73 | return ch; | |
74 | } | |
75 | ||
76 | /** | |
77 | * Set the name of the specified channel. | |
78 | * | |
79 | * If the channel already has a different name assigned to it, it will be | |
80 | * removed, and the new name will be saved instead. | |
81 | * | |
82 | * @param[in] channel The channel whose name to set. Must not be NULL. | |
83 | * @param[in] name The new name that the specified channel should get. | |
84 | * A copy of the string is made. | |
85 | * | |
86 | * @return SR_OK on success, or SR_ERR_ARG on invalid arguments. | |
87 | * | |
88 | * @since 0.3.0 | |
89 | */ | |
90 | SR_API int sr_dev_channel_name_set(struct sr_channel *channel, | |
91 | const char *name) | |
92 | { | |
93 | if (!channel) | |
94 | return SR_ERR_ARG; | |
95 | ||
96 | g_free(channel->name); | |
97 | channel->name = g_strdup(name); | |
98 | ||
99 | return SR_OK; | |
100 | } | |
101 | ||
102 | /** | |
103 | * Enable or disable a channel. | |
104 | * | |
105 | * @param[in] channel The channel to enable or disable. Must not be NULL. | |
106 | * @param[in] state TRUE to enable the channel, FALSE to disable. | |
107 | * | |
108 | * @return SR_OK on success or SR_ERR on failure. In case of invalid | |
109 | * arguments, SR_ERR_ARG is returned and the channel enabled state | |
110 | * remains unchanged. | |
111 | * | |
112 | * @since 0.3.0 | |
113 | */ | |
114 | SR_API int sr_dev_channel_enable(struct sr_channel *channel, gboolean state) | |
115 | { | |
116 | int ret; | |
117 | gboolean was_enabled; | |
118 | struct sr_dev_inst *sdi; | |
119 | ||
120 | if (!channel) | |
121 | return SR_ERR_ARG; | |
122 | ||
123 | sdi = channel->sdi; | |
124 | was_enabled = channel->enabled; | |
125 | channel->enabled = state; | |
126 | if (!state != !was_enabled && sdi->driver | |
127 | && sdi->driver->config_channel_set) { | |
128 | ret = sdi->driver->config_channel_set( | |
129 | sdi, channel, SR_CHANNEL_SET_ENABLED); | |
130 | /* Roll back change if it wasn't applicable. */ | |
131 | if (ret != SR_OK) | |
132 | return ret; | |
133 | } | |
134 | ||
135 | return SR_OK; | |
136 | } | |
137 | ||
138 | /* Returns the next enabled channel, wrapping around if necessary. */ | |
139 | SR_PRIV struct sr_channel *sr_next_enabled_channel(const struct sr_dev_inst *sdi, | |
140 | struct sr_channel *cur_channel) | |
141 | { | |
142 | struct sr_channel *next_channel; | |
143 | GSList *l; | |
144 | ||
145 | next_channel = cur_channel; | |
146 | do { | |
147 | l = g_slist_find(sdi->channels, next_channel); | |
148 | if (l && l->next) | |
149 | next_channel = l->next->data; | |
150 | else | |
151 | next_channel = sdi->channels->data; | |
152 | } while (!next_channel->enabled); | |
153 | ||
154 | return next_channel; | |
155 | } | |
156 | ||
157 | /** | |
158 | * Determine whether the specified device instance has the specified | |
159 | * capability. | |
160 | * | |
161 | * @param sdi Pointer to the device instance to be checked. Must not be NULL. | |
162 | * If the device's 'driver' field is NULL (virtual device), this | |
163 | * function will always return FALSE (virtual devices don't have | |
164 | * a hardware capabilities list). | |
165 | * @param[in] key The option that should be checked for is supported by the | |
166 | * specified device. | |
167 | * | |
168 | * @retval TRUE Device has the specified option. | |
169 | * @retval FALSE Device does not have the specified option, invalid input | |
170 | * parameters or other error conditions. | |
171 | * | |
172 | * @since 0.2.0 | |
173 | */ | |
174 | SR_API gboolean sr_dev_has_option(const struct sr_dev_inst *sdi, int key) | |
175 | { | |
176 | GVariant *gvar; | |
177 | const int *devopts; | |
178 | gsize num_opts, i; | |
179 | int ret; | |
180 | ||
181 | if (!sdi || !sdi->driver || !sdi->driver->config_list) | |
182 | return FALSE; | |
183 | ||
184 | if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS, | |
185 | &gvar, sdi, NULL) != SR_OK) | |
186 | return FALSE; | |
187 | ||
188 | ret = FALSE; | |
189 | devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t)); | |
190 | for (i = 0; i < num_opts; i++) { | |
191 | if ((devopts[i] & SR_CONF_MASK) == key) { | |
192 | ret = TRUE; | |
193 | break; | |
194 | } | |
195 | } | |
196 | g_variant_unref(gvar); | |
197 | ||
198 | return ret; | |
199 | } | |
200 | ||
201 | /** | |
202 | * Enumerate the configuration options of the specified item. | |
203 | * | |
204 | * @param driver Pointer to the driver to be checked. Must not be NULL. | |
205 | * @param sdi Pointer to the device instance to be checked. May be NULL to | |
206 | * check driver options. | |
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 | * | |
210 | * @return A GArray * of enum sr_configkey values, or NULL on invalid | |
211 | * arguments. The array must be freed by the caller using | |
212 | * g_array_free(). | |
213 | * | |
214 | * @since 0.4.0 | |
215 | */ | |
216 | SR_API GArray *sr_dev_options(const struct sr_dev_driver *driver, | |
217 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
218 | { | |
219 | GVariant *gvar; | |
220 | const uint32_t *opts; | |
221 | uint32_t opt; | |
222 | gsize num_opts, i; | |
223 | GArray *result; | |
224 | ||
225 | if (!driver || !driver->config_list) | |
226 | return NULL; | |
227 | ||
228 | if (sdi && sdi->driver != driver) | |
229 | return NULL; | |
230 | ||
231 | if (driver->config_list(SR_CONF_DEVICE_OPTIONS, &gvar, sdi, cg) != SR_OK) | |
232 | return NULL; | |
233 | ||
234 | opts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(uint32_t)); | |
235 | ||
236 | result = g_array_sized_new(FALSE, FALSE, sizeof(uint32_t), num_opts); | |
237 | ||
238 | for (i = 0; i < num_opts; i++) { | |
239 | opt = opts[i] & SR_CONF_MASK; | |
240 | g_array_insert_val(result, i, opt); | |
241 | } | |
242 | ||
243 | g_variant_unref(gvar); | |
244 | ||
245 | return result; | |
246 | } | |
247 | ||
248 | /** | |
249 | * Enumerate the configuration capabilities supported by a device instance | |
250 | * for a given configuration key. | |
251 | * | |
252 | * @param sdi Pointer to the device instance to be checked. Must not be NULL. | |
253 | * If the device's 'driver' field is NULL (virtual device), this | |
254 | * function will always return FALSE (virtual devices don't have | |
255 | * a hardware capabilities list). | |
256 | * @param cg Pointer to a channel group, if a specific channel group is to | |
257 | * be checked. Must be NULL to check device-wide options. | |
258 | * @param[in] key The option that should be checked for is supported by the | |
259 | * specified device. | |
260 | * | |
261 | * @retval A bitmask of enum sr_configcap values, which will be zero for | |
262 | * invalid inputs or if the key is unsupported. | |
263 | * | |
264 | * @since 0.4.0 | |
265 | */ | |
266 | SR_API int sr_dev_config_capabilities_list(const struct sr_dev_inst *sdi, | |
267 | const struct sr_channel_group *cg, const int key) | |
268 | { | |
269 | GVariant *gvar; | |
270 | const int *devopts; | |
271 | gsize num_opts, i; | |
272 | int ret; | |
273 | ||
274 | if (!sdi || !sdi->driver || !sdi->driver->config_list) | |
275 | return 0; | |
276 | ||
277 | if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS, | |
278 | &gvar, sdi, cg) != SR_OK) | |
279 | return 0; | |
280 | ||
281 | ret = 0; | |
282 | devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t)); | |
283 | for (i = 0; i < num_opts; i++) { | |
284 | if ((devopts[i] & SR_CONF_MASK) == key) { | |
285 | ret = devopts[i] & ~SR_CONF_MASK; | |
286 | break; | |
287 | } | |
288 | } | |
289 | g_variant_unref(gvar); | |
290 | ||
291 | return ret; | |
292 | } | |
293 | ||
294 | /** | |
295 | * Allocate and init a new user-generated device instance. | |
296 | * | |
297 | * @param vendor Device vendor. | |
298 | * @param model Device model. | |
299 | * @param version Device version. | |
300 | * | |
301 | * @retval struct sr_dev_inst *. Dynamically allocated, free using | |
302 | * sr_dev_inst_free(). | |
303 | */ | |
304 | SR_API struct sr_dev_inst *sr_dev_inst_user_new(const char *vendor, | |
305 | const char *model, const char *version) | |
306 | { | |
307 | struct sr_dev_inst *sdi; | |
308 | ||
309 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
310 | ||
311 | sdi->vendor = g_strdup(vendor); | |
312 | sdi->model = g_strdup(model); | |
313 | sdi->version = g_strdup(version); | |
314 | sdi->inst_type = SR_INST_USER; | |
315 | ||
316 | return sdi; | |
317 | } | |
318 | ||
319 | /** | |
320 | * Add a new channel to the specified device instance. | |
321 | * | |
322 | * @param[in] index @copydoc sr_channel::index | |
323 | * @param[in] type @copydoc sr_channel::type | |
324 | * @param[in] name @copydoc sr_channel::name | |
325 | * | |
326 | * @return SR_OK Success. | |
327 | * @return SR_OK Invalid argument. | |
328 | */ | |
329 | SR_API int sr_dev_inst_channel_add(struct sr_dev_inst *sdi, int index, int type, const char *name) | |
330 | { | |
331 | if (!sdi || sdi->inst_type != SR_INST_USER || index < 0) | |
332 | return SR_ERR_ARG; | |
333 | ||
334 | sr_channel_new(sdi, index, type, TRUE, name); | |
335 | ||
336 | return SR_OK; | |
337 | } | |
338 | ||
339 | /** | |
340 | * Free device instance struct created by sr_dev_inst(). | |
341 | * | |
342 | * @param sdi Device instance to free. Must not be NULL. | |
343 | * | |
344 | * @private | |
345 | */ | |
346 | SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi) | |
347 | { | |
348 | struct sr_channel *ch; | |
349 | struct sr_channel_group *cg; | |
350 | GSList *l; | |
351 | ||
352 | for (l = sdi->channels; l; l = l->next) { | |
353 | ch = l->data; | |
354 | g_free(ch->name); | |
355 | g_free(ch->priv); | |
356 | g_free(ch); | |
357 | } | |
358 | g_slist_free(sdi->channels); | |
359 | ||
360 | for (l = sdi->channel_groups; l; l = l->next) { | |
361 | cg = l->data; | |
362 | g_free(cg->name); | |
363 | g_slist_free(cg->channels); | |
364 | g_free(cg->priv); | |
365 | g_free(cg); | |
366 | } | |
367 | g_slist_free(sdi->channel_groups); | |
368 | ||
369 | if (sdi->session) | |
370 | sr_session_dev_remove(sdi->session, sdi); | |
371 | ||
372 | g_free(sdi->vendor); | |
373 | g_free(sdi->model); | |
374 | g_free(sdi->version); | |
375 | g_free(sdi->serial_num); | |
376 | g_free(sdi->connection_id); | |
377 | g_free(sdi); | |
378 | } | |
379 | ||
380 | #ifdef HAVE_LIBUSB_1_0 | |
381 | ||
382 | /** | |
383 | * Allocate and init a struct for a USB device instance. | |
384 | * | |
385 | * @param[in] bus @copydoc sr_usb_dev_inst::bus | |
386 | * @param[in] address @copydoc sr_usb_dev_inst::address | |
387 | * @param[in] hdl @copydoc sr_usb_dev_inst::devhdl | |
388 | * | |
389 | * @return The struct sr_usb_dev_inst * for USB device instance. | |
390 | * | |
391 | * @private | |
392 | */ | |
393 | SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus, | |
394 | uint8_t address, struct libusb_device_handle *hdl) | |
395 | { | |
396 | struct sr_usb_dev_inst *udi; | |
397 | ||
398 | udi = g_malloc0(sizeof(struct sr_usb_dev_inst)); | |
399 | udi->bus = bus; | |
400 | udi->address = address; | |
401 | udi->devhdl = hdl; | |
402 | ||
403 | return udi; | |
404 | } | |
405 | ||
406 | /** | |
407 | * Free struct sr_usb_dev_inst * allocated by sr_usb_dev_inst(). | |
408 | * | |
409 | * @param usb The struct sr_usb_dev_inst * to free. Must not be NULL. | |
410 | * | |
411 | * @private | |
412 | */ | |
413 | SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb) | |
414 | { | |
415 | g_free(usb); | |
416 | } | |
417 | ||
418 | #endif | |
419 | ||
420 | #ifdef HAVE_LIBSERIALPORT | |
421 | ||
422 | /** | |
423 | * Allocate and init a struct for a serial device instance. | |
424 | * | |
425 | * Both parameters are copied to newly allocated strings, and freed | |
426 | * automatically by sr_serial_dev_inst_free(). | |
427 | * | |
428 | * @param[in] port OS-specific serial port specification. Examples: | |
429 | * "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1". | |
430 | * Must not be NULL. | |
431 | * @param[in] serialcomm A serial communication parameters string, in the form | |
432 | * of \<speed\>/\<data bits\>\<parity\>\<stopbits\>, for example | |
433 | * "9600/8n1" or "600/7o2". This is an optional parameter; | |
434 | * it may be filled in later. Can be NULL. | |
435 | * | |
436 | * @return A pointer to a newly initialized struct sr_serial_dev_inst, | |
437 | * or NULL on error. | |
438 | * | |
439 | * @private | |
440 | */ | |
441 | SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port, | |
442 | const char *serialcomm) | |
443 | { | |
444 | struct sr_serial_dev_inst *serial; | |
445 | ||
446 | serial = g_malloc0(sizeof(struct sr_serial_dev_inst)); | |
447 | serial->port = g_strdup(port); | |
448 | if (serialcomm) | |
449 | serial->serialcomm = g_strdup(serialcomm); | |
450 | ||
451 | return serial; | |
452 | } | |
453 | ||
454 | /** | |
455 | * Free struct sr_serial_dev_inst * allocated by sr_serial_dev_inst(). | |
456 | * | |
457 | * @param serial The struct sr_serial_dev_inst * to free. Must not be NULL. | |
458 | * | |
459 | * @private | |
460 | */ | |
461 | SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial) | |
462 | { | |
463 | g_free(serial->port); | |
464 | g_free(serial->serialcomm); | |
465 | g_free(serial); | |
466 | } | |
467 | #endif | |
468 | ||
469 | /** @private */ | |
470 | SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device) | |
471 | { | |
472 | struct sr_usbtmc_dev_inst *usbtmc; | |
473 | ||
474 | usbtmc = g_malloc0(sizeof(struct sr_usbtmc_dev_inst)); | |
475 | usbtmc->device = g_strdup(device); | |
476 | usbtmc->fd = -1; | |
477 | ||
478 | return usbtmc; | |
479 | } | |
480 | ||
481 | /** @private */ | |
482 | SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc) | |
483 | { | |
484 | g_free(usbtmc->device); | |
485 | g_free(usbtmc); | |
486 | } | |
487 | ||
488 | /** | |
489 | * Get the list of devices/instances of the specified driver. | |
490 | * | |
491 | * @param driver The driver to use. Must not be NULL. | |
492 | * | |
493 | * @return The list of devices/instances of this driver, or NULL upon errors | |
494 | * or if the list is empty. | |
495 | * | |
496 | * @since 0.2.0 | |
497 | */ | |
498 | SR_API GSList *sr_dev_list(const struct sr_dev_driver *driver) | |
499 | { | |
500 | if (driver && driver->dev_list) | |
501 | return driver->dev_list(driver); | |
502 | else | |
503 | return NULL; | |
504 | } | |
505 | ||
506 | /** | |
507 | * Clear the list of device instances a driver knows about. | |
508 | * | |
509 | * @param driver The driver to use. This must be a pointer to one of | |
510 | * the entries returned by sr_driver_list(). Must not be NULL. | |
511 | * | |
512 | * @retval SR_OK Success. | |
513 | * @retval SR_ERR_ARG Invalid driver. | |
514 | * | |
515 | * @since 0.2.0 | |
516 | */ | |
517 | SR_API int sr_dev_clear(const struct sr_dev_driver *driver) | |
518 | { | |
519 | int ret; | |
520 | ||
521 | if (!driver) { | |
522 | sr_err("Invalid driver."); | |
523 | return SR_ERR_ARG; | |
524 | } | |
525 | ||
526 | if (driver->dev_clear) | |
527 | ret = driver->dev_clear(driver); | |
528 | else | |
529 | ret = std_dev_clear(driver, NULL); | |
530 | ||
531 | return ret; | |
532 | } | |
533 | ||
534 | /** | |
535 | * Open the specified device. | |
536 | * | |
537 | * @param sdi Device instance to use. Must not be NULL. | |
538 | * | |
539 | * @return SR_OK upon success, a negative error code upon errors. | |
540 | * | |
541 | * @since 0.2.0 | |
542 | */ | |
543 | SR_API int sr_dev_open(struct sr_dev_inst *sdi) | |
544 | { | |
545 | int ret; | |
546 | ||
547 | if (!sdi || !sdi->driver || !sdi->driver->dev_open) | |
548 | return SR_ERR; | |
549 | ||
550 | ret = sdi->driver->dev_open(sdi); | |
551 | ||
552 | return ret; | |
553 | } | |
554 | ||
555 | /** | |
556 | * Close the specified device. | |
557 | * | |
558 | * @param sdi Device instance to use. Must not be NULL. | |
559 | * | |
560 | * @return SR_OK upon success, a negative error code upon errors. | |
561 | * | |
562 | * @since 0.2.0 | |
563 | */ | |
564 | SR_API int sr_dev_close(struct sr_dev_inst *sdi) | |
565 | { | |
566 | int ret; | |
567 | ||
568 | if (!sdi || !sdi->driver || !sdi->driver->dev_close) | |
569 | return SR_ERR; | |
570 | ||
571 | ret = sdi->driver->dev_close(sdi); | |
572 | ||
573 | return ret; | |
574 | } | |
575 | ||
576 | /** | |
577 | * Queries a device instances' driver. | |
578 | * | |
579 | * @param sdi Device instance to use. Must not be NULL. | |
580 | * | |
581 | * @return The driver instance or NULL on error. | |
582 | */ | |
583 | SR_API struct sr_dev_driver *sr_dev_inst_driver_get(const struct sr_dev_inst *sdi) | |
584 | { | |
585 | if (!sdi || !sdi->driver) | |
586 | return NULL; | |
587 | ||
588 | return sdi->driver; | |
589 | } | |
590 | ||
591 | /** | |
592 | * Queries a device instances' vendor. | |
593 | * | |
594 | * @param sdi Device instance to use. Must not be NULL. | |
595 | * | |
596 | * @return The vendor string or NULL. | |
597 | */ | |
598 | SR_API const char *sr_dev_inst_vendor_get(const struct sr_dev_inst *sdi) | |
599 | { | |
600 | if (!sdi) | |
601 | return NULL; | |
602 | ||
603 | return sdi->vendor; | |
604 | } | |
605 | ||
606 | /** | |
607 | * Queries a device instances' model. | |
608 | * | |
609 | * @param sdi Device instance to use. Must not be NULL. | |
610 | * | |
611 | * @return The model string or NULL. | |
612 | */ | |
613 | SR_API const char *sr_dev_inst_model_get(const struct sr_dev_inst *sdi) | |
614 | { | |
615 | if (!sdi) | |
616 | return NULL; | |
617 | ||
618 | return sdi->model; | |
619 | } | |
620 | ||
621 | /** | |
622 | * Queries a device instances' version. | |
623 | * | |
624 | * @param sdi Device instance to use. Must not be NULL. | |
625 | * | |
626 | * @return The version string or NULL. | |
627 | */ | |
628 | SR_API const char *sr_dev_inst_version_get(const struct sr_dev_inst *sdi) | |
629 | { | |
630 | if (!sdi) | |
631 | return NULL; | |
632 | ||
633 | return sdi->version; | |
634 | } | |
635 | ||
636 | /** | |
637 | * Queries a device instances' serial number. | |
638 | * | |
639 | * @param sdi Device instance to use. Must not be NULL. | |
640 | * | |
641 | * @return The serial number string or NULL. | |
642 | */ | |
643 | SR_API const char *sr_dev_inst_sernum_get(const struct sr_dev_inst *sdi) | |
644 | { | |
645 | if (!sdi) | |
646 | return NULL; | |
647 | ||
648 | return sdi->serial_num; | |
649 | } | |
650 | ||
651 | /** | |
652 | * Queries a device instances' connection identifier. | |
653 | * | |
654 | * @param sdi Device instance to use. Must not be NULL. | |
655 | * | |
656 | * @return A copy of the connection ID string or NULL. The caller is responsible | |
657 | * for g_free()ing the string when it is no longer needed. | |
658 | */ | |
659 | SR_API const char *sr_dev_inst_connid_get(const struct sr_dev_inst *sdi) | |
660 | { | |
661 | #ifdef HAVE_LIBUSB_1_0 | |
662 | struct drv_context *drvc; | |
663 | int cnt, i, a, b; | |
664 | char connection_id[64]; | |
665 | struct sr_usb_dev_inst *usb; | |
666 | struct libusb_device **devlist; | |
667 | #endif | |
668 | ||
669 | if (!sdi) | |
670 | return NULL; | |
671 | ||
672 | #ifdef HAVE_LIBSERIALPORT | |
673 | struct sr_serial_dev_inst *serial; | |
674 | ||
675 | if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_SERIAL)) { | |
676 | /* connection_id isn't populated, let's do that here. */ | |
677 | ||
678 | serial = sdi->conn; | |
679 | ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(serial->port); | |
680 | } | |
681 | #endif | |
682 | ||
683 | #ifdef HAVE_LIBUSB_1_0 | |
684 | if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_USB)) { | |
685 | /* connection_id isn't populated, let's do that here. */ | |
686 | ||
687 | drvc = sdi->driver->context; | |
688 | usb = sdi->conn; | |
689 | ||
690 | if ((cnt = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist)) < 0) { | |
691 | sr_err("Failed to retrieve device list: %s.", | |
692 | libusb_error_name(cnt)); | |
693 | return NULL; | |
694 | } | |
695 | ||
696 | for (i = 0; i < cnt; i++) { | |
697 | /* Find the USB device by the logical address we know. */ | |
698 | b = libusb_get_bus_number(devlist[i]); | |
699 | a = libusb_get_device_address(devlist[i]); | |
700 | if (b != usb->bus || a != usb->address) | |
701 | continue; | |
702 | ||
703 | usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)); | |
704 | ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(connection_id); | |
705 | break; | |
706 | } | |
707 | ||
708 | libusb_free_device_list(devlist, 1); | |
709 | } | |
710 | #endif | |
711 | ||
712 | return sdi->connection_id; | |
713 | } | |
714 | ||
715 | /** | |
716 | * Queries a device instances' channel list. | |
717 | * | |
718 | * @param sdi Device instance to use. Must not be NULL. | |
719 | * | |
720 | * @return The GSList of channels or NULL. | |
721 | */ | |
722 | SR_API GSList *sr_dev_inst_channels_get(const struct sr_dev_inst *sdi) | |
723 | { | |
724 | if (!sdi) | |
725 | return NULL; | |
726 | ||
727 | return sdi->channels; | |
728 | } | |
729 | ||
730 | /** | |
731 | * Queries a device instances' channel groups list. | |
732 | * | |
733 | * @param sdi Device instance to use. Must not be NULL. | |
734 | * | |
735 | * @return The GSList of channel groups or NULL. | |
736 | */ | |
737 | SR_API GSList *sr_dev_inst_channel_groups_get(const struct sr_dev_inst *sdi) | |
738 | { | |
739 | if (!sdi) | |
740 | return NULL; | |
741 | ||
742 | return sdi->channel_groups; | |
743 | } | |
744 | ||
745 | /** @} */ |