]> sigrok.org Git - libsigrok.git/blame - src/device.c
std: Rename std_dev_clear() to std_dev_clear_with_callback().
[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
9c24d16a 138/* Returns the next enabled channel, wrapping around if necessary. */
f200d59e 139/** @private */
9c24d16a 140SR_PRIV struct sr_channel *sr_next_enabled_channel(const struct sr_dev_inst *sdi,
f200d59e 141
9c24d16a
BV
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
94799bc4 159/**
9c5332d2
UH
160 * Determine whether the specified device instance has the specified
161 * capability.
94799bc4 162 *
9c5332d2 163 * @param sdi Pointer to the device instance to be checked. Must not be NULL.
8ec95d22
UH
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).
04cb9157 167 * @param[in] key The option that should be checked for is supported by the
4d15e5c9 168 * specified device.
94799bc4 169 *
2ecc745c 170 * @retval TRUE Device has the specified option.
04cb9157
MH
171 * @retval FALSE Device does not have the specified option, invalid input
172 * parameters or other error conditions.
9fb5f2df 173 *
53f05fa8 174 * @since 0.2.0
94799bc4 175 */
4d15e5c9 176SR_API gboolean sr_dev_has_option(const struct sr_dev_inst *sdi, int key)
7d658874 177{
003595ac 178 GVariant *gvar;
4d15e5c9 179 const int *devopts;
003595ac
BV
180 gsize num_opts, i;
181 int ret;
7d658874 182
003595ac 183 if (!sdi || !sdi->driver || !sdi->driver->config_list)
8ec95d22 184 return FALSE;
94799bc4 185
8f996b89 186 if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS,
92b68bb5 187 &gvar, sdi, NULL) != SR_OK)
8ec95d22 188 return FALSE;
94799bc4 189
003595ac
BV
190 ret = FALSE;
191 devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t));
192 for (i = 0; i < num_opts; i++) {
d099d880 193 if ((devopts[i] & SR_CONF_MASK) == key) {
003595ac
BV
194 ret = TRUE;
195 break;
196 }
94799bc4 197 }
003595ac 198 g_variant_unref(gvar);
218557b8 199
003595ac 200 return ret;
a1bb33af 201}
fd9836bf 202
e7136c62
ML
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.
2ecc745c
UH
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 *
e7136c62
ML
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 */
2ecc745c
UH
218SR_API GArray *sr_dev_options(const struct sr_dev_driver *driver,
219 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
e7136c62
ML
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
71e9c54d
ML
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).
2ecc745c
UH
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.
71e9c54d
ML
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 */
0c697a4b 268SR_API int sr_dev_config_capabilities_list(const struct sr_dev_inst *sdi,
71e9c54d
ML
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
e705ce3b
UH
296/**
297 * Allocate and init a new user-generated device instance.
aac29cc1 298 *
2ecc745c
UH
299 * @param vendor Device vendor.
300 * @param model Device model.
301 * @param version Device version.
aac29cc1
UH
302 *
303 * @retval struct sr_dev_inst *. Dynamically allocated, free using
304 * sr_dev_inst_free().
e705ce3b
UH
305 */
306SR_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
aac29cc1
UH
311 sdi = g_malloc0(sizeof(struct sr_dev_inst));
312
0af636be
UH
313 sdi->vendor = g_strdup(vendor);
314 sdi->model = g_strdup(model);
315 sdi->version = g_strdup(version);
e705ce3b
UH
316 sdi->inst_type = SR_INST_USER;
317
318 return sdi;
319}
320
321/**
322 * Add a new channel to the specified device instance.
2ecc745c
UH
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.
e705ce3b
UH
330 */
331SR_API int sr_dev_inst_channel_add(struct sr_dev_inst *sdi, int index, int type, const char *name)
332{
e705ce3b
UH
333 if (!sdi || sdi->inst_type != SR_INST_USER || index < 0)
334 return SR_ERR_ARG;
335
5e23fcab 336 sr_channel_new(sdi, index, type, TRUE, name);
e705ce3b
UH
337
338 return SR_OK;
339}
340
2ecc745c
UH
341/**
342 * Free device instance struct created by sr_dev_inst().
343 *
4bf93988 344 * @param sdi Device instance to free. If NULL, the function will do nothing.
2ecc745c
UH
345 *
346 * @private
04cb9157 347 */
48a486cd
BV
348SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
349{
ba7dd8bb 350 struct sr_channel *ch;
b1b1944e 351 struct sr_channel_group *cg;
d3cff734
BV
352 GSList *l;
353
4bf93988
UH
354 if (!sdi)
355 return;
356
ba7dd8bb
UH
357 for (l = sdi->channels; l; l = l->next) {
358 ch = l->data;
359 g_free(ch->name);
379d2609 360 g_free(ch->priv);
ba7dd8bb 361 g_free(ch);
d3cff734 362 }
ba7dd8bb 363 g_slist_free(sdi->channels);
d3cff734 364
b1b1944e
BV
365 for (l = sdi->channel_groups; l; l = l->next) {
366 cg = l->data;
7aebe22d
BV
367 g_free(cg->name);
368 g_slist_free(cg->channels);
b1b1944e 369 g_free(cg->priv);
7aebe22d 370 g_free(cg);
b1b1944e
BV
371 }
372 g_slist_free(sdi->channel_groups);
90c7f4e9 373
fe7b8efc
SB
374 if (sdi->session)
375 sr_session_dev_remove(sdi->session, sdi);
376
48a486cd
BV
377 g_free(sdi->vendor);
378 g_free(sdi->model);
379 g_free(sdi->version);
2fe6210a
SA
380 g_free(sdi->serial_num);
381 g_free(sdi->connection_id);
48a486cd
BV
382 g_free(sdi);
383}
384
385#ifdef HAVE_LIBUSB_1_0
386
2ecc745c
UH
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
04cb9157 393 *
2ecc745c
UH
394 * @return The struct sr_usb_dev_inst * for USB device instance.
395 *
396 * @private
04cb9157 397 */
48a486cd
BV
398SR_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
91219afc 403 udi = g_malloc0(sizeof(struct sr_usb_dev_inst));
48a486cd
BV
404 udi->bus = bus;
405 udi->address = address;
406 udi->devhdl = hdl;
407
408 return udi;
409}
410
2ecc745c
UH
411/**
412 * Free struct sr_usb_dev_inst * allocated by sr_usb_dev_inst().
413 *
5801ce78
UH
414 * @param usb The struct sr_usb_dev_inst * to free. If NULL, this
415 * function does nothing.
2ecc745c
UH
416 *
417 * @private
04cb9157 418 */
48a486cd
BV
419SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
420{
a006798b 421 g_free(usb);
48a486cd
BV
422}
423
424#endif
425
c4f2dfd0
UH
426#ifdef HAVE_LIBSERIALPORT
427
9fb5f2df 428/**
2ecc745c 429 * Allocate and init a struct for a serial device instance.
299bdb24
BV
430 *
431 * Both parameters are copied to newly allocated strings, and freed
432 * automatically by sr_serial_dev_inst_free().
9fb5f2df 433 *
04cb9157 434 * @param[in] port OS-specific serial port specification. Examples:
9fb5f2df 435 * "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1".
91219afc 436 * Must not be NULL.
04cb9157
MH
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;
91219afc 440 * it may be filled in later. Can be NULL.
9fb5f2df
UH
441 *
442 * @return A pointer to a newly initialized struct sr_serial_dev_inst,
443 * or NULL on error.
2ecc745c
UH
444 *
445 * @private
299bdb24 446 */
48a486cd 447SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
299bdb24 448 const char *serialcomm)
48a486cd
BV
449{
450 struct sr_serial_dev_inst *serial;
451
91219afc 452 serial = g_malloc0(sizeof(struct sr_serial_dev_inst));
48a486cd 453 serial->port = g_strdup(port);
299bdb24
BV
454 if (serialcomm)
455 serial->serialcomm = g_strdup(serialcomm);
48a486cd
BV
456
457 return serial;
458}
459
2ecc745c
UH
460/**
461 * Free struct sr_serial_dev_inst * allocated by sr_serial_dev_inst().
462 *
04891a99
UH
463 * @param serial The struct sr_serial_dev_inst * to free. If NULL, this
464 * function will do nothing.
2ecc745c
UH
465 *
466 * @private
04cb9157 467 */
48a486cd
BV
468SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
469{
04891a99
UH
470 if (!serial)
471 return;
472
48a486cd 473 g_free(serial->port);
299bdb24 474 g_free(serial->serialcomm);
acac8fc3 475 g_free(serial);
48a486cd 476}
c4f2dfd0
UH
477#endif
478
df823ac4 479/** @private */
ae67644f
ML
480SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device)
481{
482 struct sr_usbtmc_dev_inst *usbtmc;
483
91219afc 484 usbtmc = g_malloc0(sizeof(struct sr_usbtmc_dev_inst));
ae67644f
ML
485 usbtmc->device = g_strdup(device);
486 usbtmc->fd = -1;
487
488 return usbtmc;
489}
490
df823ac4 491/** @private */
ae67644f
ML
492SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc)
493{
ce375f2a
UH
494 if (!usbtmc)
495 return;
496
ae67644f
ML
497 g_free(usbtmc->device);
498 g_free(usbtmc);
499}
500
576ff5b0
UH
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 *
53f05fa8 509 * @since 0.2.0
576ff5b0 510 */
f99e32af 511SR_API GSList *sr_dev_list(const struct sr_dev_driver *driver)
811deee4 512{
811deee4 513 if (driver && driver->dev_list)
4f840ce9 514 return driver->dev_list(driver);
811deee4
BV
515 else
516 return NULL;
517}
518
576ff5b0 519/**
8102cee4 520 * Clear the list of device instances a driver knows about.
576ff5b0 521 *
8102cee4
BV
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.
576ff5b0 524 *
2ecc745c
UH
525 * @retval SR_OK Success.
526 * @retval SR_ERR_ARG Invalid driver.
576ff5b0
UH
527 *
528 * @since 0.2.0
529 */
f99e32af 530SR_API int sr_dev_clear(const struct sr_dev_driver *driver)
811deee4 531{
8102cee4
BV
532 int ret;
533
534 if (!driver) {
535 sr_err("Invalid driver.");
536 return SR_ERR_ARG;
537 }
538
12852b03
UH
539 if (!driver->context) {
540 /*
541 * Driver was never initialized, nothing to do.
542 *
543 * No log message since this usually gets called for all
544 * drivers, whether they were initialized or not.
545 */
546 return SR_OK;
547 }
548
91057d2f
UH
549 /* No log message here, too verbose and not very useful. */
550
8102cee4 551 if (driver->dev_clear)
4f840ce9 552 ret = driver->dev_clear(driver);
811deee4 553 else
6e43c3d5 554 ret = std_dev_clear_with_callback(driver, NULL);
8102cee4
BV
555
556 return ret;
811deee4
BV
557}
558
576ff5b0 559/**
7e463623
UH
560 * Open the specified device instance.
561 *
562 * If the device instance is already open (sdi->status == SR_ST_ACTIVE),
563 * SR_ERR will be returned and no re-opening of the device will be attempted.
564 *
565 * If opening was successful, sdi->status is set to SR_ST_ACTIVE, otherwise
566 * it will be left unchanged.
576ff5b0
UH
567 *
568 * @param sdi Device instance to use. Must not be NULL.
569 *
7e463623
UH
570 * @retval SR_OK Success.
571 * @retval SR_ERR_ARG Invalid arguments.
572 * @retval SR_ERR Device instance was already active, or other error.
576ff5b0
UH
573 *
574 * @since 0.2.0
575 */
efdecf4c
BV
576SR_API int sr_dev_open(struct sr_dev_inst *sdi)
577{
578 int ret;
579
580 if (!sdi || !sdi->driver || !sdi->driver->dev_open)
7e463623 581 return SR_ERR_ARG;
efdecf4c 582
6402c379
UH
583 if (sdi->status == SR_ST_ACTIVE) {
584 sr_err("%s: Device instance already active, can't re-open.",
585 sdi->driver->name);
586 return SR_ERR;
587 }
588
7e463623 589 sr_dbg("%s: Opening device instance.", sdi->driver->name);
6402c379 590
efdecf4c
BV
591 ret = sdi->driver->dev_open(sdi);
592
7e463623
UH
593 if (ret == SR_OK)
594 sdi->status = SR_ST_ACTIVE;
595
efdecf4c
BV
596 return ret;
597}
598
576ff5b0 599/**
f1ba6b4b
UH
600 * Close the specified device instance.
601 *
602 * If the device instance is not open (sdi->status != SR_ST_ACTIVE),
603 * SR_ERR_DEV_CLOSED will be returned and no closing will be attempted.
604 *
605 * Note: sdi->status will be set to SR_ST_INACTIVE, regardless of whether
606 * there are any errors during closing of the device instance (any errors
607 * will be reported via error code and log message, though).
576ff5b0
UH
608 *
609 * @param sdi Device instance to use. Must not be NULL.
610 *
f1ba6b4b
UH
611 * @retval SR_OK Success.
612 * @retval SR_ERR_ARG Invalid arguments.
613 * @retval SR_ERR_DEV_CLOSED Device instance was not active.
614 * @retval SR_ERR Other error.
576ff5b0
UH
615 *
616 * @since 0.2.0
617 */
efdecf4c
BV
618SR_API int sr_dev_close(struct sr_dev_inst *sdi)
619{
620 int ret;
621
622 if (!sdi || !sdi->driver || !sdi->driver->dev_close)
f1ba6b4b 623 return SR_ERR_ARG;
efdecf4c 624
093e1cba
UH
625 if (sdi->status != SR_ST_ACTIVE) {
626 sr_err("%s: Device instance not active, can't close.",
627 sdi->driver->name);
628 return SR_ERR_DEV_CLOSED;
629 }
630
f1ba6b4b
UH
631 sdi->status = SR_ST_INACTIVE;
632
633 sr_dbg("%s: Closing device instance.", sdi->driver->name);
093e1cba 634
efdecf4c
BV
635 ret = sdi->driver->dev_close(sdi);
636
637 return ret;
638}
639
0157fdce
SA
640/**
641 * Queries a device instances' driver.
642 *
643 * @param sdi Device instance to use. Must not be NULL.
644 *
645 * @return The driver instance or NULL on error.
646 */
2f5f9705 647SR_API struct sr_dev_driver *sr_dev_inst_driver_get(const struct sr_dev_inst *sdi)
0157fdce
SA
648{
649 if (!sdi || !sdi->driver)
650 return NULL;
651
652 return sdi->driver;
653}
654
655/**
656 * Queries a device instances' vendor.
657 *
658 * @param sdi Device instance to use. Must not be NULL.
659 *
660 * @return The vendor string or NULL.
661 */
2f5f9705 662SR_API const char *sr_dev_inst_vendor_get(const struct sr_dev_inst *sdi)
0157fdce
SA
663{
664 if (!sdi)
665 return NULL;
666
667 return sdi->vendor;
668}
669
670/**
671 * Queries a device instances' model.
672 *
673 * @param sdi Device instance to use. Must not be NULL.
674 *
675 * @return The model string or NULL.
676 */
2f5f9705 677SR_API const char *sr_dev_inst_model_get(const struct sr_dev_inst *sdi)
0157fdce
SA
678{
679 if (!sdi)
680 return NULL;
681
682 return sdi->model;
683}
684
685/**
686 * Queries a device instances' version.
687 *
688 * @param sdi Device instance to use. Must not be NULL.
689 *
690 * @return The version string or NULL.
691 */
2f5f9705 692SR_API const char *sr_dev_inst_version_get(const struct sr_dev_inst *sdi)
0157fdce
SA
693{
694 if (!sdi)
695 return NULL;
696
697 return sdi->version;
698}
699
700/**
701 * Queries a device instances' serial number.
702 *
703 * @param sdi Device instance to use. Must not be NULL.
704 *
705 * @return The serial number string or NULL.
706 */
2f5f9705 707SR_API const char *sr_dev_inst_sernum_get(const struct sr_dev_inst *sdi)
0157fdce
SA
708{
709 if (!sdi)
710 return NULL;
711
712 return sdi->serial_num;
713}
714
715/**
716 * Queries a device instances' connection identifier.
717 *
718 * @param sdi Device instance to use. Must not be NULL.
719 *
2ecc745c 720 * @return A copy of the connection ID string or NULL. The caller is responsible
0157fdce
SA
721 * for g_free()ing the string when it is no longer needed.
722 */
2f5f9705 723SR_API const char *sr_dev_inst_connid_get(const struct sr_dev_inst *sdi)
0157fdce 724{
468665df 725#ifdef HAVE_LIBUSB_1_0
0157fdce 726 struct drv_context *drvc;
8de8551b 727 int cnt, i, a, b;
9c6a2913 728 char connection_id[64];
0157fdce
SA
729 struct sr_usb_dev_inst *usb;
730 struct libusb_device **devlist;
9c6a2913 731#endif
0157fdce
SA
732
733 if (!sdi)
734 return NULL;
735
9c6a2913 736#ifdef HAVE_LIBSERIALPORT
e8cbb223
SA
737 struct sr_serial_dev_inst *serial;
738
739 if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_SERIAL)) {
740 /* connection_id isn't populated, let's do that here. */
741
742 serial = sdi->conn;
2f5f9705 743 ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(serial->port);
e8cbb223 744 }
9c6a2913 745#endif
e8cbb223 746
9c6a2913 747#ifdef HAVE_LIBUSB_1_0
0157fdce
SA
748 if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_USB)) {
749 /* connection_id isn't populated, let's do that here. */
750
41812aca 751 drvc = sdi->driver->context;
0157fdce
SA
752 usb = sdi->conn;
753
754 if ((cnt = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist)) < 0) {
755 sr_err("Failed to retrieve device list: %s.",
756 libusb_error_name(cnt));
757 return NULL;
758 }
759
760 for (i = 0; i < cnt; i++) {
0157fdce
SA
761 /* Find the USB device by the logical address we know. */
762 b = libusb_get_bus_number(devlist[i]);
763 a = libusb_get_device_address(devlist[i]);
764 if (b != usb->bus || a != usb->address)
765 continue;
766
767 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
2f5f9705 768 ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(connection_id);
0157fdce
SA
769 break;
770 }
771
772 libusb_free_device_list(devlist, 1);
773 }
9c6a2913 774#endif
0157fdce
SA
775
776 return sdi->connection_id;
777}
778
e437da2b
UH
779/**
780 * Queries a device instances' channel list.
781 *
782 * @param sdi Device instance to use. Must not be NULL.
783 *
784 * @return The GSList of channels or NULL.
785 */
2f5f9705 786SR_API GSList *sr_dev_inst_channels_get(const struct sr_dev_inst *sdi)
e437da2b
UH
787{
788 if (!sdi)
789 return NULL;
790
791 return sdi->channels;
792}
793
794/**
795 * Queries a device instances' channel groups list.
796 *
797 * @param sdi Device instance to use. Must not be NULL.
798 *
799 * @return The GSList of channel groups or NULL.
800 */
2f5f9705 801SR_API GSList *sr_dev_inst_channel_groups_get(const struct sr_dev_inst *sdi)
e437da2b
UH
802{
803 if (!sdi)
804 return NULL;
805
806 return sdi->channel_groups;
807}
808
7b870c38 809/** @} */