25 #include "libsigrok-internal.h"
29 #define LOG_PREFIX "device"
65 ch = g_malloc0(
sizeof(*ch));
71 ch->
name = g_strdup(name);
73 sdi->channels = g_slist_append(sdi->channels, ch);
122 g_free(channel->
name);
123 channel->
name = g_strdup(name);
143 gboolean was_enabled;
144 struct sr_dev_inst *sdi;
150 was_enabled = channel->
enabled;
152 if (!state != !was_enabled && sdi->driver
153 && sdi->driver->config_channel_set) {
154 ret = sdi->driver->config_channel_set(
155 sdi, channel, SR_CHANNEL_SET_ENABLED);
175 SR_PRIV struct sr_channel *sr_next_enabled_channel(
const struct sr_dev_inst *sdi,
181 next_channel = cur_channel;
183 l = g_slist_find(sdi->channels, next_channel);
185 next_channel = l->next->data;
187 next_channel = sdi->channels->data;
188 }
while (!next_channel->
enabled);
280 if (!sdi || !sdi->driver || !sdi->driver->config_list)
283 if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS,
284 &gvar, sdi, NULL) !=
SR_OK)
288 devopts = g_variant_get_fixed_array(gvar, &num_opts,
sizeof(int32_t));
289 for (i = 0; i < num_opts; i++) {
290 if ((devopts[i] & SR_CONF_MASK) == key) {
295 g_variant_unref(gvar);
319 const uint32_t *opts;
327 if (sdi && sdi->driver != driver)
333 opts = g_variant_get_fixed_array(gvar, &num_opts,
sizeof(uint32_t));
335 result = g_array_sized_new(FALSE, FALSE,
sizeof(uint32_t), num_opts);
337 for (i = 0; i < num_opts; i++) {
338 opt = opts[i] & SR_CONF_MASK;
339 g_array_insert_val(result, i, opt);
342 g_variant_unref(gvar);
373 if (!sdi || !sdi->driver || !sdi->driver->config_list)
376 if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS,
377 &gvar, sdi, cg) !=
SR_OK)
381 devopts = g_variant_get_fixed_array(gvar, &num_opts,
sizeof(int32_t));
382 for (i = 0; i < num_opts; i++) {
383 if ((devopts[i] & SR_CONF_MASK) == key) {
384 ret = devopts[i] & ~SR_CONF_MASK;
388 g_variant_unref(gvar);
404 const char *model,
const char *version)
406 struct sr_dev_inst *sdi;
408 sdi = g_malloc0(
sizeof(*sdi));
410 sdi->vendor = g_strdup(vendor);
411 sdi->model = g_strdup(model);
412 sdi->version = g_strdup(version);
430 if (!sdi || sdi->inst_type !=
SR_INST_USER || index < 0)
433 sr_channel_new(sdi, index, type, TRUE, name);
445 SR_PRIV void sr_dev_inst_free(
struct sr_dev_inst *sdi)
454 for (l = sdi->channels; l; l = l->next) {
458 g_slist_free(sdi->channels);
460 for (l = sdi->channel_groups; l; l = l->next) {
467 g_slist_free(sdi->channel_groups);
474 g_free(sdi->version);
475 g_free(sdi->serial_num);
476 g_free(sdi->connection_id);
480 #ifdef HAVE_LIBUSB_1_0
493 SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
494 uint8_t address,
struct libusb_device_handle *hdl)
496 struct sr_usb_dev_inst *udi;
498 udi = g_malloc0(
sizeof(*udi));
500 udi->address = address;
514 SR_PRIV void sr_usb_dev_inst_free(
struct sr_usb_dev_inst *usb)
521 #ifdef HAVE_SERIAL_COMM
542 SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(
const char *port,
543 const char *serialcomm)
545 struct sr_serial_dev_inst *serial;
547 serial = g_malloc0(
sizeof(*serial));
548 serial->port = g_strdup(port);
550 serial->serialcomm = g_strdup(serialcomm);
563 SR_PRIV void sr_serial_dev_inst_free(
struct sr_serial_dev_inst *serial)
568 g_free(serial->port);
569 g_free(serial->serialcomm);
575 SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(
const char *device)
577 struct sr_usbtmc_dev_inst *usbtmc;
579 usbtmc = g_malloc0(
sizeof(*usbtmc));
580 usbtmc->device = g_strdup(device);
587 SR_PRIV void sr_usbtmc_dev_inst_free(
struct sr_usbtmc_dev_inst *usbtmc)
592 g_free(usbtmc->device);
628 sr_err(
"Invalid driver.");
668 if (!sdi || !sdi->driver || !sdi->driver->dev_open)
672 sr_err(
"%s: Device instance already active, can't re-open.",
677 sr_dbg(
"%s: Opening device instance.", sdi->driver->name);
679 ret = sdi->driver->dev_open(sdi);
708 if (!sdi || !sdi->driver || !sdi->driver->dev_close)
712 sr_err(
"%s: Device instance not active, can't close.",
719 sr_dbg(
"%s: Closing device instance.", sdi->driver->name);
721 return sdi->driver->dev_close(sdi);
733 if (!sdi || !sdi->driver)
796 return sdi->serial_num;
809 #ifdef HAVE_LIBUSB_1_0
810 struct drv_context *drvc;
812 char conn_id_usb[64];
813 struct sr_usb_dev_inst *usb;
814 struct libusb_device **devlist;
817 #ifdef HAVE_SERIAL_COMM
818 struct sr_serial_dev_inst *serial;
821 struct sr_scpi_dev_inst *scpi;
827 #ifdef HAVE_SERIAL_COMM
828 if ((!sdi->connection_id) && (sdi->inst_type ==
SR_INST_SERIAL)) {
832 ((
struct sr_dev_inst *)sdi)->connection_id = g_strdup(serial->port);
836 #ifdef HAVE_LIBUSB_1_0
837 if ((!sdi->connection_id) && (sdi->inst_type ==
SR_INST_USB)) {
840 drvc = sdi->driver->context;
843 if ((cnt = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist)) < 0) {
844 sr_err(
"Failed to retrieve device list: %s.",
845 libusb_error_name(cnt));
849 for (i = 0; i < cnt; i++) {
851 b = libusb_get_bus_number(devlist[i]);
852 a = libusb_get_device_address(devlist[i]);
853 if (b != usb->bus || a != usb->address)
856 if (usb_get_port_path(devlist[i], conn_id_usb,
sizeof(conn_id_usb)) < 0)
859 ((
struct sr_dev_inst *)sdi)->connection_id = g_strdup(conn_id_usb);
863 libusb_free_device_list(devlist, 1);
867 if ((!sdi->connection_id) && (sdi->inst_type ==
SR_INST_SCPI)) {
871 sr_scpi_connection_id(scpi, &conn_id_scpi);
872 ((
struct sr_dev_inst *)sdi)->connection_id = g_strdup(conn_id_scpi);
873 g_free(conn_id_scpi);
876 return sdi->connection_id;
891 return sdi->channels;
906 return sdi->channel_groups;
Generic/unspecified error.
SR_PRIV void sr_channel_free_cb(void *p)
Wrapper around sr_channel_free(), suitable for glib iterators.
const char * sr_dev_inst_version_get(const struct sr_dev_inst *sdi)
Queries a device instances' version.
int sr_dev_config_capabilities_list(const struct sr_dev_inst *sdi, const struct sr_channel_group *cg, const int key)
Enumerate the configuration capabilities supported by a device instance for a given configuration key...
void * priv
Private data for driver use.
const char * sr_dev_inst_vendor_get(const struct sr_dev_inst *sdi)
Queries a device instances' vendor.
int(* config_list)(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
List all possible values for a configuration key in a device instance.
SR_PRIV gboolean sr_channel_lists_differ(GSList *l1, GSList *l2)
Compare two channel lists, return whether they differ.
gboolean enabled
Is this channel enabled?
GArray * sr_dev_options(const struct sr_dev_driver *driver, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
Enumerate the configuration options of the specified item.
struct sr_dev_driver * sr_dev_inst_driver_get(const struct sr_dev_inst *sdi)
Queries a device instances' driver.
int sr_dev_channel_name_set(struct sr_channel *channel, const char *name)
Set the name of the specified channel.
const char * sr_dev_inst_connid_get(const struct sr_dev_inst *sdi)
Queries a device instances' connection identifier.
GSList * sr_dev_inst_channel_groups_get(const struct sr_dev_inst *sdi)
Queries a device instances' channel groups list.
The public libsigrok header file to be used by frontends.
GSList * channels
List of sr_channel structs of the channels belonging to this group.
int sr_session_dev_remove(struct sr_session *session, struct sr_dev_inst *sdi)
Remove a device instance from a session.
Device instance type for SCPI devices.
GSList * sr_dev_list(const struct sr_dev_driver *driver)
Get the list of devices/instances of the specified driver.
gboolean sr_dev_has_option(const struct sr_dev_inst *sdi, int key)
Determine whether the specified device instance has the specified capability.
void * priv
Private data for driver use.
Device-instance type for user-created "devices".
The device instance is actively in use in a session.
const char * sr_dev_inst_model_get(const struct sr_dev_inst *sdi)
Queries a device instances' model.
int sr_dev_channel_enable(struct sr_channel *channel, gboolean state)
Enable or disable a channel.
Structure for groups of channels that have common properties.
Device instance type for serial port devices.
int sr_dev_open(struct sr_dev_inst *sdi)
Open the specified device instance.
int type
Channel type (SR_CHANNEL_LOGIC, ...)
struct sr_dev_inst * sr_dev_inst_user_new(const char *vendor, const char *model, const char *version)
Allocate and init a new user-generated device instance.
Device is closed, but must be open.
int sr_dev_close(struct sr_dev_inst *sdi)
Close the specified device instance.
char * name
Name of the channel group.
char * name
Name of channel.
GSList * sr_dev_inst_channels_get(const struct sr_dev_inst *sdi)
Queries a device instances' channel list.
void * context
Device driver context, considered private.
struct sr_dev_inst * sdi
The device this channel is attached to.
const char * sr_dev_inst_sernum_get(const struct sr_dev_inst *sdi)
Queries a device instances' serial number.
GSList *(* dev_list)(const struct sr_dev_driver *driver)
Get list of device instances the driver knows about.
int(* dev_clear)(const struct sr_dev_driver *driver)
Clear list of devices the driver knows about.
int sr_dev_clear(const struct sr_dev_driver *driver)
Clear the list of device instances a driver knows about.
SR_PRIV gboolean sr_channels_differ(struct sr_channel *ch1, struct sr_channel *ch2)
Compare two channels, return whether they differ.
int index
The index of this channel, starting at 0.
Device instance type for USB devices.
The device instance is live, but not in use.
Information on single channel.
int sr_dev_inst_channel_add(struct sr_dev_inst *sdi, int index, int type, const char *name)
Add a new channel to the specified device instance.