]>
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 | /** @private */ | |
140 | SR_PRIV struct sr_channel *sr_next_enabled_channel(const struct sr_dev_inst *sdi, | |
141 | ||
142 | struct sr_channel *cur_channel) | |
143 | { | |
144 | struct sr_channel *next_channel; | |
145 | GSList *l; | |
146 | ||
147 | next_channel = cur_channel; | |
148 | do { | |
149 | l = g_slist_find(sdi->channels, next_channel); | |
150 | if (l && l->next) | |
151 | next_channel = l->next->data; | |
152 | else | |
153 | next_channel = sdi->channels->data; | |
154 | } while (!next_channel->enabled); | |
155 | ||
156 | return next_channel; | |
157 | } | |
158 | ||
159 | /** | |
160 | * Determine whether the specified device instance has the specified | |
161 | * capability. | |
162 | * | |
163 | * @param sdi Pointer to the device instance to be checked. Must not be NULL. | |
164 | * If the device's 'driver' field is NULL (virtual device), this | |
165 | * function will always return FALSE (virtual devices don't have | |
166 | * a hardware capabilities list). | |
167 | * @param[in] key The option that should be checked for is supported by the | |
168 | * specified device. | |
169 | * | |
170 | * @retval TRUE Device has the specified option. | |
171 | * @retval FALSE Device does not have the specified option, invalid input | |
172 | * parameters or other error conditions. | |
173 | * | |
174 | * @since 0.2.0 | |
175 | */ | |
176 | SR_API gboolean sr_dev_has_option(const struct sr_dev_inst *sdi, int key) | |
177 | { | |
178 | GVariant *gvar; | |
179 | const int *devopts; | |
180 | gsize num_opts, i; | |
181 | int ret; | |
182 | ||
183 | if (!sdi || !sdi->driver || !sdi->driver->config_list) | |
184 | return FALSE; | |
185 | ||
186 | if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS, | |
187 | &gvar, sdi, NULL) != SR_OK) | |
188 | return FALSE; | |
189 | ||
190 | ret = FALSE; | |
191 | devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t)); | |
192 | for (i = 0; i < num_opts; i++) { | |
193 | if ((devopts[i] & SR_CONF_MASK) == key) { | |
194 | ret = TRUE; | |
195 | break; | |
196 | } | |
197 | } | |
198 | g_variant_unref(gvar); | |
199 | ||
200 | return ret; | |
201 | } | |
202 | ||
203 | /** | |
204 | * Enumerate the configuration options of the specified item. | |
205 | * | |
206 | * @param driver Pointer to the driver to be checked. Must not be NULL. | |
207 | * @param sdi Pointer to the device instance to be checked. May be NULL to | |
208 | * check driver options. | |
209 | * @param cg Pointer to a channel group, if a specific channel group is to | |
210 | * be checked. Must be NULL to check device-wide options. | |
211 | * | |
212 | * @return A GArray * of enum sr_configkey values, or NULL on invalid | |
213 | * arguments. The array must be freed by the caller using | |
214 | * g_array_free(). | |
215 | * | |
216 | * @since 0.4.0 | |
217 | */ | |
218 | SR_API GArray *sr_dev_options(const struct sr_dev_driver *driver, | |
219 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
220 | { | |
221 | GVariant *gvar; | |
222 | const uint32_t *opts; | |
223 | uint32_t opt; | |
224 | gsize num_opts, i; | |
225 | GArray *result; | |
226 | ||
227 | if (!driver || !driver->config_list) | |
228 | return NULL; | |
229 | ||
230 | if (sdi && sdi->driver != driver) | |
231 | return NULL; | |
232 | ||
233 | if (driver->config_list(SR_CONF_DEVICE_OPTIONS, &gvar, sdi, cg) != SR_OK) | |
234 | return NULL; | |
235 | ||
236 | opts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(uint32_t)); | |
237 | ||
238 | result = g_array_sized_new(FALSE, FALSE, sizeof(uint32_t), num_opts); | |
239 | ||
240 | for (i = 0; i < num_opts; i++) { | |
241 | opt = opts[i] & SR_CONF_MASK; | |
242 | g_array_insert_val(result, i, opt); | |
243 | } | |
244 | ||
245 | g_variant_unref(gvar); | |
246 | ||
247 | return result; | |
248 | } | |
249 | ||
250 | /** | |
251 | * Enumerate the configuration capabilities supported by a device instance | |
252 | * for a given configuration key. | |
253 | * | |
254 | * @param sdi Pointer to the device instance to be checked. Must not be NULL. | |
255 | * If the device's 'driver' field is NULL (virtual device), this | |
256 | * function will always return FALSE (virtual devices don't have | |
257 | * a hardware capabilities list). | |
258 | * @param cg Pointer to a channel group, if a specific channel group is to | |
259 | * be checked. Must be NULL to check device-wide options. | |
260 | * @param[in] key The option that should be checked for is supported by the | |
261 | * specified device. | |
262 | * | |
263 | * @retval A bitmask of enum sr_configcap values, which will be zero for | |
264 | * invalid inputs or if the key is unsupported. | |
265 | * | |
266 | * @since 0.4.0 | |
267 | */ | |
268 | SR_API int sr_dev_config_capabilities_list(const struct sr_dev_inst *sdi, | |
269 | const struct sr_channel_group *cg, const int key) | |
270 | { | |
271 | GVariant *gvar; | |
272 | const int *devopts; | |
273 | gsize num_opts, i; | |
274 | int ret; | |
275 | ||
276 | if (!sdi || !sdi->driver || !sdi->driver->config_list) | |
277 | return 0; | |
278 | ||
279 | if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS, | |
280 | &gvar, sdi, cg) != SR_OK) | |
281 | return 0; | |
282 | ||
283 | ret = 0; | |
284 | devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t)); | |
285 | for (i = 0; i < num_opts; i++) { | |
286 | if ((devopts[i] & SR_CONF_MASK) == key) { | |
287 | ret = devopts[i] & ~SR_CONF_MASK; | |
288 | break; | |
289 | } | |
290 | } | |
291 | g_variant_unref(gvar); | |
292 | ||
293 | return ret; | |
294 | } | |
295 | ||
296 | /** | |
297 | * Allocate and init a new user-generated device instance. | |
298 | * | |
299 | * @param vendor Device vendor. | |
300 | * @param model Device model. | |
301 | * @param version Device version. | |
302 | * | |
303 | * @retval struct sr_dev_inst *. Dynamically allocated, free using | |
304 | * sr_dev_inst_free(). | |
305 | */ | |
306 | SR_API struct sr_dev_inst *sr_dev_inst_user_new(const char *vendor, | |
307 | const char *model, const char *version) | |
308 | { | |
309 | struct sr_dev_inst *sdi; | |
310 | ||
311 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
312 | ||
313 | sdi->vendor = g_strdup(vendor); | |
314 | sdi->model = g_strdup(model); | |
315 | sdi->version = g_strdup(version); | |
316 | sdi->inst_type = SR_INST_USER; | |
317 | ||
318 | return sdi; | |
319 | } | |
320 | ||
321 | /** | |
322 | * Add a new channel to the specified device instance. | |
323 | * | |
324 | * @param[in] index @copydoc sr_channel::index | |
325 | * @param[in] type @copydoc sr_channel::type | |
326 | * @param[in] name @copydoc sr_channel::name | |
327 | * | |
328 | * @return SR_OK Success. | |
329 | * @return SR_OK Invalid argument. | |
330 | */ | |
331 | SR_API int sr_dev_inst_channel_add(struct sr_dev_inst *sdi, int index, int type, const char *name) | |
332 | { | |
333 | if (!sdi || sdi->inst_type != SR_INST_USER || index < 0) | |
334 | return SR_ERR_ARG; | |
335 | ||
336 | sr_channel_new(sdi, index, type, TRUE, name); | |
337 | ||
338 | return SR_OK; | |
339 | } | |
340 | ||
341 | /** | |
342 | * Free device instance struct created by sr_dev_inst(). | |
343 | * | |
344 | * @param sdi Device instance to free. If NULL, the function will do nothing. | |
345 | * | |
346 | * @private | |
347 | */ | |
348 | SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi) | |
349 | { | |
350 | struct sr_channel *ch; | |
351 | struct sr_channel_group *cg; | |
352 | GSList *l; | |
353 | ||
354 | if (!sdi) | |
355 | return; | |
356 | ||
357 | for (l = sdi->channels; l; l = l->next) { | |
358 | ch = l->data; | |
359 | g_free(ch->name); | |
360 | g_free(ch->priv); | |
361 | g_free(ch); | |
362 | } | |
363 | g_slist_free(sdi->channels); | |
364 | ||
365 | for (l = sdi->channel_groups; l; l = l->next) { | |
366 | cg = l->data; | |
367 | g_free(cg->name); | |
368 | g_slist_free(cg->channels); | |
369 | g_free(cg->priv); | |
370 | g_free(cg); | |
371 | } | |
372 | g_slist_free(sdi->channel_groups); | |
373 | ||
374 | if (sdi->session) | |
375 | sr_session_dev_remove(sdi->session, sdi); | |
376 | ||
377 | g_free(sdi->vendor); | |
378 | g_free(sdi->model); | |
379 | g_free(sdi->version); | |
380 | g_free(sdi->serial_num); | |
381 | g_free(sdi->connection_id); | |
382 | g_free(sdi); | |
383 | } | |
384 | ||
385 | #ifdef HAVE_LIBUSB_1_0 | |
386 | ||
387 | /** | |
388 | * Allocate and init a struct for a USB device instance. | |
389 | * | |
390 | * @param[in] bus @copydoc sr_usb_dev_inst::bus | |
391 | * @param[in] address @copydoc sr_usb_dev_inst::address | |
392 | * @param[in] hdl @copydoc sr_usb_dev_inst::devhdl | |
393 | * | |
394 | * @return The struct sr_usb_dev_inst * for USB device instance. | |
395 | * | |
396 | * @private | |
397 | */ | |
398 | SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus, | |
399 | uint8_t address, struct libusb_device_handle *hdl) | |
400 | { | |
401 | struct sr_usb_dev_inst *udi; | |
402 | ||
403 | udi = g_malloc0(sizeof(struct sr_usb_dev_inst)); | |
404 | udi->bus = bus; | |
405 | udi->address = address; | |
406 | udi->devhdl = hdl; | |
407 | ||
408 | return udi; | |
409 | } | |
410 | ||
411 | /** | |
412 | * Free struct sr_usb_dev_inst * allocated by sr_usb_dev_inst(). | |
413 | * | |
414 | * @param usb The struct sr_usb_dev_inst * to free. If NULL, this | |
415 | * function does nothing. | |
416 | * | |
417 | * @private | |
418 | */ | |
419 | SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb) | |
420 | { | |
421 | g_free(usb); | |
422 | } | |
423 | ||
424 | #endif | |
425 | ||
426 | #ifdef HAVE_LIBSERIALPORT | |
427 | ||
428 | /** | |
429 | * Allocate and init a struct for a serial device instance. | |
430 | * | |
431 | * Both parameters are copied to newly allocated strings, and freed | |
432 | * automatically by sr_serial_dev_inst_free(). | |
433 | * | |
434 | * @param[in] port OS-specific serial port specification. Examples: | |
435 | * "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1". | |
436 | * Must not be NULL. | |
437 | * @param[in] serialcomm A serial communication parameters string, in the form | |
438 | * of \<speed\>/\<data bits\>\<parity\>\<stopbits\>, for example | |
439 | * "9600/8n1" or "600/7o2". This is an optional parameter; | |
440 | * it may be filled in later. Can be NULL. | |
441 | * | |
442 | * @return A pointer to a newly initialized struct sr_serial_dev_inst, | |
443 | * or NULL on error. | |
444 | * | |
445 | * @private | |
446 | */ | |
447 | SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port, | |
448 | const char *serialcomm) | |
449 | { | |
450 | struct sr_serial_dev_inst *serial; | |
451 | ||
452 | serial = g_malloc0(sizeof(struct sr_serial_dev_inst)); | |
453 | serial->port = g_strdup(port); | |
454 | if (serialcomm) | |
455 | serial->serialcomm = g_strdup(serialcomm); | |
456 | ||
457 | return serial; | |
458 | } | |
459 | ||
460 | /** | |
461 | * Free struct sr_serial_dev_inst * allocated by sr_serial_dev_inst(). | |
462 | * | |
463 | * @param serial The struct sr_serial_dev_inst * to free. If NULL, this | |
464 | * function will do nothing. | |
465 | * | |
466 | * @private | |
467 | */ | |
468 | SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial) | |
469 | { | |
470 | if (!serial) | |
471 | return; | |
472 | ||
473 | g_free(serial->port); | |
474 | g_free(serial->serialcomm); | |
475 | g_free(serial); | |
476 | } | |
477 | #endif | |
478 | ||
479 | /** @private */ | |
480 | SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device) | |
481 | { | |
482 | struct sr_usbtmc_dev_inst *usbtmc; | |
483 | ||
484 | usbtmc = g_malloc0(sizeof(struct sr_usbtmc_dev_inst)); | |
485 | usbtmc->device = g_strdup(device); | |
486 | usbtmc->fd = -1; | |
487 | ||
488 | return usbtmc; | |
489 | } | |
490 | ||
491 | /** @private */ | |
492 | SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc) | |
493 | { | |
494 | if (!usbtmc) | |
495 | return; | |
496 | ||
497 | g_free(usbtmc->device); | |
498 | g_free(usbtmc); | |
499 | } | |
500 | ||
501 | /** | |
502 | * Get the list of devices/instances of the specified driver. | |
503 | * | |
504 | * @param driver The driver to use. Must not be NULL. | |
505 | * | |
506 | * @return The list of devices/instances of this driver, or NULL upon errors | |
507 | * or if the list is empty. | |
508 | * | |
509 | * @since 0.2.0 | |
510 | */ | |
511 | SR_API GSList *sr_dev_list(const struct sr_dev_driver *driver) | |
512 | { | |
513 | if (driver && driver->dev_list) | |
514 | return driver->dev_list(driver); | |
515 | else | |
516 | return NULL; | |
517 | } | |
518 | ||
519 | /** | |
520 | * Clear the list of device instances a driver knows about. | |
521 | * | |
522 | * @param driver The driver to use. This must be a pointer to one of | |
523 | * the entries returned by sr_driver_list(). Must not be NULL. | |
524 | * | |
525 | * @retval SR_OK Success. | |
526 | * @retval SR_ERR_ARG Invalid driver. | |
527 | * | |
528 | * @since 0.2.0 | |
529 | */ | |
530 | SR_API int sr_dev_clear(const struct sr_dev_driver *driver) | |
531 | { | |
532 | if (!driver) { | |
533 | sr_err("Invalid driver."); | |
534 | return SR_ERR_ARG; | |
535 | } | |
536 | ||
537 | if (!driver->context) { | |
538 | /* | |
539 | * Driver was never initialized, nothing to do. | |
540 | * | |
541 | * No log message since this usually gets called for all | |
542 | * drivers, whether they were initialized or not. | |
543 | */ | |
544 | return SR_OK; | |
545 | } | |
546 | ||
547 | /* No log message here, too verbose and not very useful. */ | |
548 | ||
549 | return driver->dev_clear(driver); | |
550 | } | |
551 | ||
552 | /** | |
553 | * Open the specified device instance. | |
554 | * | |
555 | * If the device instance is already open (sdi->status == SR_ST_ACTIVE), | |
556 | * SR_ERR will be returned and no re-opening of the device will be attempted. | |
557 | * | |
558 | * If opening was successful, sdi->status is set to SR_ST_ACTIVE, otherwise | |
559 | * it will be left unchanged. | |
560 | * | |
561 | * @param sdi Device instance to use. Must not be NULL. | |
562 | * | |
563 | * @retval SR_OK Success. | |
564 | * @retval SR_ERR_ARG Invalid arguments. | |
565 | * @retval SR_ERR Device instance was already active, or other error. | |
566 | * | |
567 | * @since 0.2.0 | |
568 | */ | |
569 | SR_API int sr_dev_open(struct sr_dev_inst *sdi) | |
570 | { | |
571 | int ret; | |
572 | ||
573 | if (!sdi || !sdi->driver || !sdi->driver->dev_open) | |
574 | return SR_ERR_ARG; | |
575 | ||
576 | if (sdi->status == SR_ST_ACTIVE) { | |
577 | sr_err("%s: Device instance already active, can't re-open.", | |
578 | sdi->driver->name); | |
579 | return SR_ERR; | |
580 | } | |
581 | ||
582 | sr_dbg("%s: Opening device instance.", sdi->driver->name); | |
583 | ||
584 | ret = sdi->driver->dev_open(sdi); | |
585 | ||
586 | if (ret == SR_OK) | |
587 | sdi->status = SR_ST_ACTIVE; | |
588 | ||
589 | return ret; | |
590 | } | |
591 | ||
592 | /** | |
593 | * Close the specified device instance. | |
594 | * | |
595 | * If the device instance is not open (sdi->status != SR_ST_ACTIVE), | |
596 | * SR_ERR_DEV_CLOSED will be returned and no closing will be attempted. | |
597 | * | |
598 | * Note: sdi->status will be set to SR_ST_INACTIVE, regardless of whether | |
599 | * there are any errors during closing of the device instance (any errors | |
600 | * will be reported via error code and log message, though). | |
601 | * | |
602 | * @param sdi Device instance to use. Must not be NULL. | |
603 | * | |
604 | * @retval SR_OK Success. | |
605 | * @retval SR_ERR_ARG Invalid arguments. | |
606 | * @retval SR_ERR_DEV_CLOSED Device instance was not active. | |
607 | * @retval SR_ERR Other error. | |
608 | * | |
609 | * @since 0.2.0 | |
610 | */ | |
611 | SR_API int sr_dev_close(struct sr_dev_inst *sdi) | |
612 | { | |
613 | if (!sdi || !sdi->driver || !sdi->driver->dev_close) | |
614 | return SR_ERR_ARG; | |
615 | ||
616 | if (sdi->status != SR_ST_ACTIVE) { | |
617 | sr_err("%s: Device instance not active, can't close.", | |
618 | sdi->driver->name); | |
619 | return SR_ERR_DEV_CLOSED; | |
620 | } | |
621 | ||
622 | sdi->status = SR_ST_INACTIVE; | |
623 | ||
624 | sr_dbg("%s: Closing device instance.", sdi->driver->name); | |
625 | ||
626 | return sdi->driver->dev_close(sdi); | |
627 | } | |
628 | ||
629 | /** | |
630 | * Queries a device instances' driver. | |
631 | * | |
632 | * @param sdi Device instance to use. Must not be NULL. | |
633 | * | |
634 | * @return The driver instance or NULL on error. | |
635 | */ | |
636 | SR_API struct sr_dev_driver *sr_dev_inst_driver_get(const struct sr_dev_inst *sdi) | |
637 | { | |
638 | if (!sdi || !sdi->driver) | |
639 | return NULL; | |
640 | ||
641 | return sdi->driver; | |
642 | } | |
643 | ||
644 | /** | |
645 | * Queries a device instances' vendor. | |
646 | * | |
647 | * @param sdi Device instance to use. Must not be NULL. | |
648 | * | |
649 | * @return The vendor string or NULL. | |
650 | */ | |
651 | SR_API const char *sr_dev_inst_vendor_get(const struct sr_dev_inst *sdi) | |
652 | { | |
653 | if (!sdi) | |
654 | return NULL; | |
655 | ||
656 | return sdi->vendor; | |
657 | } | |
658 | ||
659 | /** | |
660 | * Queries a device instances' model. | |
661 | * | |
662 | * @param sdi Device instance to use. Must not be NULL. | |
663 | * | |
664 | * @return The model string or NULL. | |
665 | */ | |
666 | SR_API const char *sr_dev_inst_model_get(const struct sr_dev_inst *sdi) | |
667 | { | |
668 | if (!sdi) | |
669 | return NULL; | |
670 | ||
671 | return sdi->model; | |
672 | } | |
673 | ||
674 | /** | |
675 | * Queries a device instances' version. | |
676 | * | |
677 | * @param sdi Device instance to use. Must not be NULL. | |
678 | * | |
679 | * @return The version string or NULL. | |
680 | */ | |
681 | SR_API const char *sr_dev_inst_version_get(const struct sr_dev_inst *sdi) | |
682 | { | |
683 | if (!sdi) | |
684 | return NULL; | |
685 | ||
686 | return sdi->version; | |
687 | } | |
688 | ||
689 | /** | |
690 | * Queries a device instances' serial number. | |
691 | * | |
692 | * @param sdi Device instance to use. Must not be NULL. | |
693 | * | |
694 | * @return The serial number string or NULL. | |
695 | */ | |
696 | SR_API const char *sr_dev_inst_sernum_get(const struct sr_dev_inst *sdi) | |
697 | { | |
698 | if (!sdi) | |
699 | return NULL; | |
700 | ||
701 | return sdi->serial_num; | |
702 | } | |
703 | ||
704 | /** | |
705 | * Queries a device instances' connection identifier. | |
706 | * | |
707 | * @param sdi Device instance to use. Must not be NULL. | |
708 | * | |
709 | * @return A copy of the connection ID string or NULL. The caller is responsible | |
710 | * for g_free()ing the string when it is no longer needed. | |
711 | */ | |
712 | SR_API const char *sr_dev_inst_connid_get(const struct sr_dev_inst *sdi) | |
713 | { | |
714 | #ifdef HAVE_LIBUSB_1_0 | |
715 | struct drv_context *drvc; | |
716 | int cnt, i, a, b; | |
717 | char connection_id[64]; | |
718 | struct sr_usb_dev_inst *usb; | |
719 | struct libusb_device **devlist; | |
720 | #endif | |
721 | ||
722 | if (!sdi) | |
723 | return NULL; | |
724 | ||
725 | #ifdef HAVE_LIBSERIALPORT | |
726 | struct sr_serial_dev_inst *serial; | |
727 | ||
728 | if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_SERIAL)) { | |
729 | /* connection_id isn't populated, let's do that here. */ | |
730 | ||
731 | serial = sdi->conn; | |
732 | ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(serial->port); | |
733 | } | |
734 | #endif | |
735 | ||
736 | #ifdef HAVE_LIBUSB_1_0 | |
737 | if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_USB)) { | |
738 | /* connection_id isn't populated, let's do that here. */ | |
739 | ||
740 | drvc = sdi->driver->context; | |
741 | usb = sdi->conn; | |
742 | ||
743 | if ((cnt = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist)) < 0) { | |
744 | sr_err("Failed to retrieve device list: %s.", | |
745 | libusb_error_name(cnt)); | |
746 | return NULL; | |
747 | } | |
748 | ||
749 | for (i = 0; i < cnt; i++) { | |
750 | /* Find the USB device by the logical address we know. */ | |
751 | b = libusb_get_bus_number(devlist[i]); | |
752 | a = libusb_get_device_address(devlist[i]); | |
753 | if (b != usb->bus || a != usb->address) | |
754 | continue; | |
755 | ||
756 | if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0) | |
757 | continue; | |
758 | ||
759 | ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(connection_id); | |
760 | break; | |
761 | } | |
762 | ||
763 | libusb_free_device_list(devlist, 1); | |
764 | } | |
765 | #endif | |
766 | ||
767 | return sdi->connection_id; | |
768 | } | |
769 | ||
770 | /** | |
771 | * Queries a device instances' channel list. | |
772 | * | |
773 | * @param sdi Device instance to use. Must not be NULL. | |
774 | * | |
775 | * @return The GSList of channels or NULL. | |
776 | */ | |
777 | SR_API GSList *sr_dev_inst_channels_get(const struct sr_dev_inst *sdi) | |
778 | { | |
779 | if (!sdi) | |
780 | return NULL; | |
781 | ||
782 | return sdi->channels; | |
783 | } | |
784 | ||
785 | /** | |
786 | * Queries a device instances' channel groups list. | |
787 | * | |
788 | * @param sdi Device instance to use. Must not be NULL. | |
789 | * | |
790 | * @return The GSList of channel groups or NULL. | |
791 | */ | |
792 | SR_API GSList *sr_dev_inst_channel_groups_get(const struct sr_dev_inst *sdi) | |
793 | { | |
794 | if (!sdi) | |
795 | return NULL; | |
796 | ||
797 | return sdi->channel_groups; | |
798 | } | |
799 | ||
800 | /** @} */ |