]> sigrok.org Git - libsigrok.git/blob - src/device.c
device: introduce common helpers for channel group allocation
[libsigrok.git] / src / device.c
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 <glib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <libsigrok/libsigrok.h>
25 #include "libsigrok-internal.h"
26 #include "scpi.h"
27
28 /** @cond PRIVATE */
29 #define LOG_PREFIX "device"
30 /** @endcond */
31
32 /**
33  * @file
34  *
35  * Device handling in libsigrok.
36  */
37
38 /**
39  * @defgroup grp_devices Devices
40  *
41  * Device handling in libsigrok.
42  *
43  * @{
44  */
45
46 /**
47  * Allocate and initialize a new struct sr_channel and add it to sdi.
48  *
49  * @param[in] sdi The device instance the channel is connected to.
50  *                Must not be NULL.
51  * @param[in] index @copydoc sr_channel::index
52  * @param[in] type @copydoc sr_channel::type
53  * @param[in] enabled @copydoc sr_channel::enabled
54  * @param[in] name @copydoc sr_channel::name
55  *
56  * @return A new struct sr_channel*.
57  *
58  * @private
59  */
60 SR_PRIV struct sr_channel *sr_channel_new(struct sr_dev_inst *sdi,
61                 int index, int type, gboolean enabled, const char *name)
62 {
63         struct sr_channel *ch;
64
65         ch = g_malloc0(sizeof(*ch));
66         ch->sdi = sdi;
67         ch->index = index;
68         ch->type = type;
69         ch->enabled = enabled;
70         if (name)
71                 ch->name = g_strdup(name);
72
73         sdi->channels = g_slist_append(sdi->channels, ch);
74
75         return ch;
76 }
77
78 /**
79  * Release a previously allocated struct sr_channel.
80  *
81  * @param[in] ch Pointer to struct sr_channel.
82  *
83  * @private
84  */
85 SR_PRIV void sr_channel_free(struct sr_channel *ch)
86 {
87         if (!ch)
88                 return;
89         g_free(ch->name);
90         g_free(ch->priv);
91         g_free(ch);
92 }
93
94 /**
95  * Wrapper around sr_channel_free(), suitable for glib iterators.
96  *
97  * @private
98  */
99 SR_PRIV void sr_channel_free_cb(void *p)
100 {
101         sr_channel_free(p);
102 }
103
104 /**
105  * Set the name of the specified channel.
106  *
107  * If the channel already has a different name assigned to it, it will be
108  * removed, and the new name will be saved instead.
109  *
110  * @param[in] channel The channel whose name to set. Must not be NULL.
111  * @param[in] name The new name that the specified channel should get.
112  *                 A copy of the string is made.
113  *
114  * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
115  *
116  * @since 0.3.0
117  */
118 SR_API int sr_dev_channel_name_set(struct sr_channel *channel,
119                 const char *name)
120 {
121         if (!channel)
122                 return SR_ERR_ARG;
123
124         g_free(channel->name);
125         channel->name = g_strdup(name);
126
127         return SR_OK;
128 }
129
130 /**
131  * Enable or disable a channel.
132  *
133  * @param[in] channel The channel to enable or disable. Must not be NULL.
134  * @param[in] state TRUE to enable the channel, FALSE to disable.
135  *
136  * @return SR_OK on success or SR_ERR on failure. In case of invalid
137  *         arguments, SR_ERR_ARG is returned and the channel enabled state
138  *         remains unchanged.
139  *
140  * @since 0.3.0
141  */
142 SR_API int sr_dev_channel_enable(struct sr_channel *channel, gboolean state)
143 {
144         int ret;
145         gboolean was_enabled;
146         struct sr_dev_inst *sdi;
147
148         if (!channel)
149                 return SR_ERR_ARG;
150
151         sdi = channel->sdi;
152         was_enabled = channel->enabled;
153         channel->enabled = state;
154         if (!state != !was_enabled && sdi->driver
155                         && sdi->driver->config_channel_set) {
156                 ret = sdi->driver->config_channel_set(
157                         sdi, channel, SR_CHANNEL_SET_ENABLED);
158                 /* Roll back change if it wasn't applicable. */
159                 if (ret != SR_OK)
160                         return ret;
161         }
162
163         return SR_OK;
164 }
165
166 /**
167  * Returns the next enabled channel, wrapping around if necessary.
168  *
169  * @param[in] sdi The device instance the channel is connected to.
170  *                Must not be NULL.
171  * @param[in] cur_channel The current channel.
172  *
173  * @return A pointer to the next enabled channel of this device.
174  *
175  * @private
176  */
177 SR_PRIV struct sr_channel *sr_next_enabled_channel(const struct sr_dev_inst *sdi,
178                 struct sr_channel *cur_channel)
179 {
180         struct sr_channel *next_channel;
181         GSList *l;
182
183         next_channel = cur_channel;
184         do {
185                 l = g_slist_find(sdi->channels, next_channel);
186                 if (l && l->next)
187                         next_channel = l->next->data;
188                 else
189                         next_channel = sdi->channels->data;
190         } while (!next_channel->enabled);
191
192         return next_channel;
193 }
194
195 /**
196  * Compare two channels, return whether they differ.
197  *
198  * The channels' names and types are checked. The enabled state is not
199  * considered a condition for difference. The test is motivated by the
200  * desire to detect changes in the configuration of acquisition setups
201  * between re-reads of an input file.
202  *
203  * @param[in] ch1 First channel.
204  * @param[in] ch2 Second channel.
205  *
206  * @return TRUE upon differences or unexpected input, FALSE otherwise.
207  *
208  * @private
209  */
210 SR_PRIV gboolean sr_channels_differ(struct sr_channel *ch1, struct sr_channel *ch2)
211 {
212         if (!ch1 || !ch2)
213                 return TRUE;
214
215         if (ch1->type != ch2->type)
216                 return TRUE;
217         if (strcmp(ch1->name, ch2->name))
218                 return TRUE;
219
220         return FALSE;
221 }
222
223 /**
224  * Compare two channel lists, return whether they differ.
225  *
226  * Listing the same set of channels but in a different order is considered
227  * a difference in the lists.
228  *
229  * @param[in] l1 First channel list.
230  * @param[in] l2 Second channel list.
231  *
232  * @return TRUE upon differences or unexpected input, FALSE otherwise.
233  *
234  * @private
235  */
236 SR_PRIV gboolean sr_channel_lists_differ(GSList *l1, GSList *l2)
237 {
238         struct sr_channel *ch1, *ch2;
239
240         while (l1 && l2) {
241                 ch1 = l1->data;
242                 ch2 = l2->data;
243                 l1 = l1->next;
244                 l2 = l2->next;
245                 if (!ch1 || !ch2)
246                         return TRUE;
247                 if (sr_channels_differ(ch1, ch2))
248                         return TRUE;
249                 if (ch1->index != ch2->index)
250                         return TRUE;
251         }
252         if (l1 || l2)
253                 return TRUE;
254
255         return FALSE;
256 }
257
258 /**
259  * Allocate and initialize a new channel group, and add it to sdi.
260  *
261  * @param[in] sdi The device instance the channel group is connected to.
262  *                Optional, can be NULL.
263  * @param[in] name @copydoc sr_channel_group::name
264  * @param[in] priv @copydoc sr_channel_group::priv
265  *
266  * @return A pointer to a new struct sr_channel_group, NULL upon error.
267  *
268  * @private
269  */
270 SR_PRIV struct sr_channel_group *sr_channel_group_new(struct sr_dev_inst *sdi,
271         const char *name, void *priv)
272 {
273         struct sr_channel_group *cg;
274
275         cg = g_malloc0(sizeof(*cg));
276         if (name && *name)
277                 cg->name = g_strdup(name);
278         cg->priv = priv;
279
280         if (sdi)
281                 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
282
283         return cg;
284 }
285
286 /**
287  * Release a previously allocated struct sr_channel_group.
288  *
289  * @param[in] cg Pointer to struct sr_channel_group.
290  *
291  * @private
292  */
293 SR_PRIV void sr_channel_group_free(struct sr_channel_group *cg)
294 {
295         if (!cg)
296                 return;
297
298         g_free(cg->name);
299         g_slist_free(cg->channels);
300         g_free(cg->priv);
301         g_free(cg);
302 }
303
304 /**
305  * Wrapper around sr_channel_group_free(), suitable for glib iterators.
306  *
307  * @private
308  */
309 SR_PRIV void sr_channel_group_free_cb(void *cg)
310 {
311         return sr_channel_group_free(cg);
312 }
313
314 /**
315  * Determine whether the specified device instance has the specified
316  * capability.
317  *
318  * @param sdi Pointer to the device instance to be checked. Must not be NULL.
319  *            If the device's 'driver' field is NULL (virtual device), this
320  *            function will always return FALSE (virtual devices don't have
321  *            a hardware capabilities list).
322  * @param[in] key The option that should be checked for is supported by the
323  *            specified device.
324  *
325  * @retval TRUE Device has the specified option.
326  * @retval FALSE Device does not have the specified option, invalid input
327  *         parameters or other error conditions.
328  *
329  * @since 0.2.0
330  */
331 SR_API gboolean sr_dev_has_option(const struct sr_dev_inst *sdi, int key)
332 {
333         GVariant *gvar;
334         const int *devopts;
335         gsize num_opts, i;
336         int ret;
337
338         if (!sdi || !sdi->driver || !sdi->driver->config_list)
339                 return FALSE;
340
341         if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS,
342                                 &gvar, sdi, NULL) != SR_OK)
343                 return FALSE;
344
345         ret = FALSE;
346         devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t));
347         for (i = 0; i < num_opts; i++) {
348                 if ((devopts[i] & SR_CONF_MASK) == key) {
349                         ret = TRUE;
350                         break;
351                 }
352         }
353         g_variant_unref(gvar);
354
355         return ret;
356 }
357
358 /**
359  * Enumerate the configuration options of the specified item.
360  *
361  * @param driver Pointer to the driver to be checked. Must not be NULL.
362  * @param sdi Pointer to the device instance to be checked. May be NULL to
363  *            check driver options.
364  * @param cg Pointer to a channel group, if a specific channel group is to
365  *           be checked. Must be NULL to check device-wide options.
366  *
367  * @return A GArray * of enum sr_configkey values, or NULL on invalid
368  *         arguments. The array must be freed by the caller using
369  *         g_array_free().
370  *
371  * @since 0.4.0
372  */
373 SR_API GArray *sr_dev_options(const struct sr_dev_driver *driver,
374         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
375 {
376         GVariant *gvar;
377         const uint32_t *opts;
378         uint32_t opt;
379         gsize num_opts, i;
380         GArray *result;
381
382         if (!driver || !driver->config_list)
383                 return NULL;
384
385         if (sdi && sdi->driver != driver)
386                 return NULL;
387
388         if (driver->config_list(SR_CONF_DEVICE_OPTIONS, &gvar, sdi, cg) != SR_OK)
389                 return NULL;
390
391         opts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(uint32_t));
392
393         result = g_array_sized_new(FALSE, FALSE, sizeof(uint32_t), num_opts);
394
395         for (i = 0; i < num_opts; i++) {
396                 opt = opts[i] & SR_CONF_MASK;
397                 g_array_insert_val(result, i, opt);
398         }
399
400         g_variant_unref(gvar);
401
402         return result;
403 }
404
405 /**
406  * Enumerate the configuration capabilities supported by a device instance
407  * for a given configuration key.
408  *
409  * @param sdi Pointer to the device instance to be checked. Must not be NULL.
410  *            If the device's 'driver' field is NULL (virtual device), this
411  *            function will always return FALSE (virtual devices don't have
412  *            a hardware capabilities list).
413  * @param cg Pointer to a channel group, if a specific channel group is to
414  *           be checked. Must be NULL to check device-wide options.
415  * @param[in] key The option that should be checked for is supported by the
416  *            specified device.
417  *
418  * @retval A bitmask of enum sr_configcap values, which will be zero for
419  *         invalid inputs or if the key is unsupported.
420  *
421  * @since 0.4.0
422  */
423 SR_API int sr_dev_config_capabilities_list(const struct sr_dev_inst *sdi,
424                 const struct sr_channel_group *cg, const int key)
425 {
426         GVariant *gvar;
427         const int *devopts;
428         gsize num_opts, i;
429         int ret;
430
431         if (!sdi || !sdi->driver || !sdi->driver->config_list)
432                 return 0;
433
434         if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS,
435                                 &gvar, sdi, cg) != SR_OK)
436                 return 0;
437
438         ret = 0;
439         devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t));
440         for (i = 0; i < num_opts; i++) {
441                 if ((devopts[i] & SR_CONF_MASK) == key) {
442                         ret = devopts[i] & ~SR_CONF_MASK;
443                         break;
444                 }
445         }
446         g_variant_unref(gvar);
447
448         return ret;
449 }
450
451 /**
452  * Allocate and init a new user-generated device instance.
453  *
454  * @param vendor Device vendor.
455  * @param model Device model.
456  * @param version Device version.
457  *
458  * @retval struct sr_dev_inst *. Dynamically allocated, free using
459  *         sr_dev_inst_free().
460  */
461 SR_API struct sr_dev_inst *sr_dev_inst_user_new(const char *vendor,
462                 const char *model, const char *version)
463 {
464         struct sr_dev_inst *sdi;
465
466         sdi = g_malloc0(sizeof(*sdi));
467
468         sdi->vendor = g_strdup(vendor);
469         sdi->model = g_strdup(model);
470         sdi->version = g_strdup(version);
471         sdi->inst_type = SR_INST_USER;
472
473         return sdi;
474 }
475
476 /**
477  * Add a new channel to the specified device instance.
478  *
479  * @param[in] sdi Device instance to use. Must not be NULL.
480  * @param[in] index @copydoc sr_channel::index
481  * @param[in] type @copydoc sr_channel::type
482  * @param[in] name @copydoc sr_channel::name
483  *
484  * @return SR_OK Success.
485  * @return SR_OK Invalid argument.
486  */
487 SR_API int sr_dev_inst_channel_add(struct sr_dev_inst *sdi, int index, int type, const char *name)
488 {
489         if (!sdi || sdi->inst_type != SR_INST_USER || index < 0)
490                 return SR_ERR_ARG;
491
492         sr_channel_new(sdi, index, type, TRUE, name);
493
494         return SR_OK;
495 }
496
497 /**
498  * Free device instance struct created by sr_dev_inst().
499  *
500  * @param sdi Device instance to free. If NULL, the function will do nothing.
501  *
502  * @private
503  */
504 SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
505 {
506         struct sr_channel *ch;
507         GSList *l;
508
509         if (!sdi)
510                 return;
511
512         for (l = sdi->channels; l; l = l->next) {
513                 ch = l->data;
514                 sr_channel_free(ch);
515         }
516         g_slist_free(sdi->channels);
517         g_slist_free_full(sdi->channel_groups, sr_channel_group_free_cb);
518
519         if (sdi->session)
520                 sr_session_dev_remove(sdi->session, sdi);
521
522         g_free(sdi->vendor);
523         g_free(sdi->model);
524         g_free(sdi->version);
525         g_free(sdi->serial_num);
526         g_free(sdi->connection_id);
527         g_free(sdi);
528 }
529
530 #ifdef HAVE_LIBUSB_1_0
531
532 /**
533  * Allocate and init a struct for a USB device instance.
534  *
535  * @param[in] bus @copydoc sr_usb_dev_inst::bus
536  * @param[in] address @copydoc sr_usb_dev_inst::address
537  * @param[in] hdl @copydoc sr_usb_dev_inst::devhdl
538  *
539  * @return The struct sr_usb_dev_inst * for USB device instance.
540  *
541  * @private
542  */
543 SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
544                         uint8_t address, struct libusb_device_handle *hdl)
545 {
546         struct sr_usb_dev_inst *udi;
547
548         udi = g_malloc0(sizeof(*udi));
549         udi->bus = bus;
550         udi->address = address;
551         udi->devhdl = hdl;
552
553         return udi;
554 }
555
556 /**
557  * Free struct sr_usb_dev_inst * allocated by sr_usb_dev_inst().
558  *
559  * @param usb The struct sr_usb_dev_inst * to free. If NULL, this
560  *            function does nothing.
561  *
562  * @private
563  */
564 SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
565 {
566         g_free(usb);
567 }
568
569 #endif
570
571 #ifdef HAVE_SERIAL_COMM
572
573 /**
574  * Allocate and init a struct for a serial device instance.
575  *
576  * Both parameters are copied to newly allocated strings, and freed
577  * automatically by sr_serial_dev_inst_free().
578  *
579  * @param[in] port OS-specific serial port specification. Examples:
580  *                 "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1".
581  *                 Must not be NULL.
582  * @param[in] serialcomm A serial communication parameters string, in the form
583  *              of \<speed\>/\<data bits\>\<parity\>\<stopbits\>, for example
584  *              "9600/8n1" or "600/7o2". This is an optional parameter;
585  *              it may be filled in later. Can be NULL.
586  *
587  * @return A pointer to a newly initialized struct sr_serial_dev_inst,
588  *         or NULL on error.
589  *
590  * @private
591  */
592 SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
593                 const char *serialcomm)
594 {
595         struct sr_serial_dev_inst *serial;
596
597         serial = g_malloc0(sizeof(*serial));
598         serial->port = g_strdup(port);
599         if (serialcomm)
600                 serial->serialcomm = g_strdup(serialcomm);
601
602         return serial;
603 }
604
605 /**
606  * Free struct sr_serial_dev_inst * allocated by sr_serial_dev_inst().
607  *
608  * @param serial The struct sr_serial_dev_inst * to free. If NULL, this
609  *               function will do nothing.
610  *
611  * @private
612  */
613 SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
614 {
615         if (!serial)
616                 return;
617
618         g_free(serial->port);
619         g_free(serial->serialcomm);
620         g_free(serial);
621 }
622 #endif
623
624 /** @private */
625 SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device)
626 {
627         struct sr_usbtmc_dev_inst *usbtmc;
628
629         usbtmc = g_malloc0(sizeof(*usbtmc));
630         usbtmc->device = g_strdup(device);
631         usbtmc->fd = -1;
632
633         return usbtmc;
634 }
635
636 /** @private */
637 SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc)
638 {
639         if (!usbtmc)
640                 return;
641
642         g_free(usbtmc->device);
643         g_free(usbtmc);
644 }
645
646 /**
647  * Get the list of devices/instances of the specified driver.
648  *
649  * @param driver The driver to use. Must not be NULL.
650  *
651  * @return The list of devices/instances of this driver, or NULL upon errors
652  *         or if the list is empty.
653  *
654  * @since 0.2.0
655  */
656 SR_API GSList *sr_dev_list(const struct sr_dev_driver *driver)
657 {
658         if (driver && driver->dev_list)
659                 return driver->dev_list(driver);
660         else
661                 return NULL;
662 }
663
664 /**
665  * Clear the list of device instances a driver knows about.
666  *
667  * @param driver The driver to use. This must be a pointer to one of
668  *               the entries returned by sr_driver_list(). Must not be NULL.
669  *
670  * @retval SR_OK Success.
671  * @retval SR_ERR_ARG Invalid driver.
672  *
673  * @since 0.2.0
674  */
675 SR_API int sr_dev_clear(const struct sr_dev_driver *driver)
676 {
677         if (!driver) {
678                 sr_err("Invalid driver.");
679                 return SR_ERR_ARG;
680         }
681
682         if (!driver->context) {
683                 /*
684                  * Driver was never initialized, nothing to do.
685                  *
686                  * No log message since this usually gets called for all
687                  * drivers, whether they were initialized or not.
688                  */
689                 return SR_OK;
690         }
691
692         /* No log message here, too verbose and not very useful. */
693
694         return driver->dev_clear(driver);
695 }
696
697 /**
698  * Open the specified device instance.
699  *
700  * If the device instance is already open (sdi->status == SR_ST_ACTIVE),
701  * SR_ERR will be returned and no re-opening of the device will be attempted.
702  *
703  * If opening was successful, sdi->status is set to SR_ST_ACTIVE, otherwise
704  * it will be left unchanged.
705  *
706  * @param sdi Device instance to use. Must not be NULL.
707  *
708  * @retval SR_OK Success.
709  * @retval SR_ERR_ARG Invalid arguments.
710  * @retval SR_ERR Device instance was already active, or other error.
711  *
712  * @since 0.2.0
713  */
714 SR_API int sr_dev_open(struct sr_dev_inst *sdi)
715 {
716         int ret;
717
718         if (!sdi || !sdi->driver || !sdi->driver->dev_open)
719                 return SR_ERR_ARG;
720
721         if (sdi->status == SR_ST_ACTIVE) {
722                 sr_err("%s: Device instance already active, can't re-open.",
723                         sdi->driver->name);
724                 return SR_ERR;
725         }
726
727         sr_dbg("%s: Opening device instance.", sdi->driver->name);
728
729         ret = sdi->driver->dev_open(sdi);
730
731         if (ret == SR_OK)
732                 sdi->status = SR_ST_ACTIVE;
733
734         return ret;
735 }
736
737 /**
738  * Close the specified device instance.
739  *
740  * If the device instance is not open (sdi->status != SR_ST_ACTIVE),
741  * SR_ERR_DEV_CLOSED will be returned and no closing will be attempted.
742  *
743  * Note: sdi->status will be set to SR_ST_INACTIVE, regardless of whether
744  * there are any errors during closing of the device instance (any errors
745  * will be reported via error code and log message, though).
746  *
747  * @param sdi Device instance to use. Must not be NULL.
748  *
749  * @retval SR_OK Success.
750  * @retval SR_ERR_ARG Invalid arguments.
751  * @retval SR_ERR_DEV_CLOSED Device instance was not active.
752  * @retval SR_ERR Other error.
753  *
754  * @since 0.2.0
755  */
756 SR_API int sr_dev_close(struct sr_dev_inst *sdi)
757 {
758         if (!sdi || !sdi->driver || !sdi->driver->dev_close)
759                 return SR_ERR_ARG;
760
761         if (sdi->status != SR_ST_ACTIVE) {
762                 sr_err("%s: Device instance not active, can't close.",
763                         sdi->driver->name);
764                 return SR_ERR_DEV_CLOSED;
765         }
766
767         sdi->status = SR_ST_INACTIVE;
768
769         sr_dbg("%s: Closing device instance.", sdi->driver->name);
770
771         return sdi->driver->dev_close(sdi);
772 }
773
774 /**
775  * Queries a device instances' driver.
776  *
777  * @param sdi Device instance to use. Must not be NULL.
778  *
779  * @return The driver instance or NULL on error.
780  */
781 SR_API struct sr_dev_driver *sr_dev_inst_driver_get(const struct sr_dev_inst *sdi)
782 {
783         if (!sdi || !sdi->driver)
784                 return NULL;
785
786         return sdi->driver;
787 }
788
789 /**
790  * Queries a device instances' vendor.
791  *
792  * @param sdi Device instance to use. Must not be NULL.
793  *
794  * @return The vendor string or NULL.
795  */
796 SR_API const char *sr_dev_inst_vendor_get(const struct sr_dev_inst *sdi)
797 {
798         if (!sdi)
799                 return NULL;
800
801         return sdi->vendor;
802 }
803
804 /**
805  * Queries a device instances' model.
806  *
807  * @param sdi Device instance to use. Must not be NULL.
808  *
809  * @return The model string or NULL.
810  */
811 SR_API const char *sr_dev_inst_model_get(const struct sr_dev_inst *sdi)
812 {
813         if (!sdi)
814                 return NULL;
815
816         return sdi->model;
817 }
818
819 /**
820  * Queries a device instances' version.
821  *
822  * @param sdi Device instance to use. Must not be NULL.
823  *
824  * @return The version string or NULL.
825  */
826 SR_API const char *sr_dev_inst_version_get(const struct sr_dev_inst *sdi)
827 {
828         if (!sdi)
829                 return NULL;
830
831         return sdi->version;
832 }
833
834 /**
835  * Queries a device instances' serial number.
836  *
837  * @param sdi Device instance to use. Must not be NULL.
838  *
839  * @return The serial number string or NULL.
840  */
841 SR_API const char *sr_dev_inst_sernum_get(const struct sr_dev_inst *sdi)
842 {
843         if (!sdi)
844                 return NULL;
845
846         return sdi->serial_num;
847 }
848
849 /**
850  * Queries a device instances' connection identifier.
851  *
852  * @param sdi Device instance to use. Must not be NULL.
853  *
854  * @return A copy of the connection ID string or NULL. The caller is responsible
855  *         for g_free()ing the string when it is no longer needed.
856  */
857 SR_API const char *sr_dev_inst_connid_get(const struct sr_dev_inst *sdi)
858 {
859 #ifdef HAVE_LIBUSB_1_0
860         struct drv_context *drvc;
861         int cnt, i, a, b;
862         char conn_id_usb[64];
863         struct sr_usb_dev_inst *usb;
864         struct libusb_device **devlist;
865 #endif
866
867 #ifdef HAVE_SERIAL_COMM
868         struct sr_serial_dev_inst *serial;
869 #endif
870
871         struct sr_scpi_dev_inst *scpi;
872         char *conn_id_scpi;
873
874         if (!sdi)
875                 return NULL;
876
877 #ifdef HAVE_SERIAL_COMM
878         if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_SERIAL)) {
879                 /* connection_id isn't populated, let's do that for serial devices. */
880
881                 serial = sdi->conn;
882                 ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(serial->port);
883         }
884 #endif
885
886 #ifdef HAVE_LIBUSB_1_0
887         if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_USB)) {
888                 /* connection_id isn't populated, let's do that for USB devices. */
889
890                 drvc = sdi->driver->context;
891                 usb = sdi->conn;
892
893                 if ((cnt = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist)) < 0) {
894                         sr_err("Failed to retrieve device list: %s.",
895                                libusb_error_name(cnt));
896                         return NULL;
897                 }
898
899                 for (i = 0; i < cnt; i++) {
900                         /* Find the USB device by the logical address we know. */
901                         b = libusb_get_bus_number(devlist[i]);
902                         a = libusb_get_device_address(devlist[i]);
903                         if (b != usb->bus || a != usb->address)
904                                 continue;
905
906                         if (usb_get_port_path(devlist[i], conn_id_usb, sizeof(conn_id_usb)) < 0)
907                                 continue;
908
909                         ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(conn_id_usb);
910                         break;
911                 }
912
913                 libusb_free_device_list(devlist, 1);
914         }
915 #endif
916
917         if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_SCPI)) {
918                 /* connection_id isn't populated, let's do that for SCPI devices. */
919
920                 scpi = sdi->conn;
921                 sr_scpi_connection_id(scpi, &conn_id_scpi);
922                 ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(conn_id_scpi);
923                 g_free(conn_id_scpi);
924         }
925
926         return sdi->connection_id;
927 }
928
929 /**
930  * Queries a device instances' channel list.
931  *
932  * @param sdi Device instance to use. Must not be NULL.
933  *
934  * @return The GSList of channels or NULL.
935  */
936 SR_API GSList *sr_dev_inst_channels_get(const struct sr_dev_inst *sdi)
937 {
938         if (!sdi)
939                 return NULL;
940
941         return sdi->channels;
942 }
943
944 /**
945  * Queries a device instances' channel groups list.
946  *
947  * @param sdi Device instance to use. Must not be NULL.
948  *
949  * @return The GSList of channel groups or NULL.
950  */
951 SR_API GSList *sr_dev_inst_channel_groups_get(const struct sr_dev_inst *sdi)
952 {
953         if (!sdi)
954                 return NULL;
955
956         return sdi->channel_groups;
957 }
958
959 /** @} */