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