gboolean config_key_has_cap(struct sr_dev_driver *driver, const struct sr_dev_inst *sdi, struct sr_channel_group *cg, uint32_t key, uint32_t capability) { GVariant *gvar_opts; const uint32_t *opts; gsize num_opts, i; gboolean result; if (sr_config_list(driver, sdi, cg, SR_CONF_DEVICE_OPTIONS, &gvar_opts) != SR_OK) return FALSE; opts = g_variant_get_fixed_array(gvar_opts, &num_opts, sizeof(uint32_t)); result = FALSE; for (i = 0; i < num_opts; i++) { if ((opts[i] & SR_CONF_MASK) == key) { if ((opts[i] & capability) == capability) result = TRUE; else result = FALSE; break; } } g_variant_unref(gvar_opts); return result; }
int maybe_config_list(struct sr_dev_driver *driver, const struct sr_dev_inst *sdi, struct sr_channel_group *cg, uint32_t key, GVariant **gvar) { if (config_key_has_cap(driver, sdi, cg, key, SR_CONF_LIST)) return sr_config_list(driver, sdi, cg, key, gvar); return SR_ERR_NA; }
GVariant* DevInst::list_config(const sr_probe_group *group, int key) { GVariant *data = NULL; assert(_owner); sr_dev_inst *const sdi = dev_inst(); assert(sdi); if (sr_config_list(sdi->driver, sdi, group, key, &data) != SR_OK) return NULL; return data; }
static int check_options(struct sr_dev_driver *driver, GSList *options, uint32_t optlist_key, struct sr_dev_inst *sdi, struct sr_channel_group *cg) { struct sr_config *src; const struct sr_key_info *srci; GVariant *gvar_opts; GSList *l; const uint32_t *opts; gsize num_opts, i; int ret; if (sr_config_list(driver, sdi, cg, optlist_key, &gvar_opts) != SR_OK) { /* Driver publishes no options for this optlist. */ return SR_ERR; } ret = SR_OK; opts = g_variant_get_fixed_array(gvar_opts, &num_opts, sizeof(uint32_t)); for (l = options; l; l = l->next) { src = l->data; for (i = 0; i < num_opts; i++) { if (opts[i] == src->key) break; } if (i == num_opts) { if (!(srci = sr_key_info_get(SR_KEY_CONFIG, src->key))) /* Shouldn't happen. */ sr_err("Invalid option %d.", src->key); else sr_err("Invalid option '%s'.", srci->id); ret = SR_ERR_ARG; break; } if (sr_variant_type_check(src->key, src->data) != SR_OK) { ret = SR_ERR_ARG; break; } } g_variant_unref(gvar_opts); return ret; }
/** * Enumerate scan options supported by this driver. * * Before calling sr_driver_scan_options_list(), the user must have previously * initialized the driver by calling sr_driver_init(). * * @param driver The driver to enumerate options for. This must be a pointer * to one of the entries returned by sr_driver_list(). Must not * be NULL. * * @return A GArray * of uint32_t entries, or NULL on invalid arguments. Each * entry is a configuration key that is supported as a scan option. * The array must be freed by the caller using g_array_free(). * * @since 0.4.0 */ SR_API GArray *sr_driver_scan_options_list(const struct sr_dev_driver *driver) { GVariant *gvar; const uint32_t *opts; gsize num_opts; GArray *result; if (sr_config_list(driver, NULL, NULL, SR_CONF_SCAN_OPTIONS, &gvar) != SR_OK) return NULL; opts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(uint32_t)); result = g_array_sized_new(FALSE, FALSE, sizeof(uint32_t), num_opts); g_array_insert_vals(result, 0, opts, num_opts); g_variant_unref(gvar); return result; }
DeviceOptions::DeviceOptions(struct sr_dev_inst *sdi) : _sdi(sdi) { GVariant *gvar_opts, *gvar_list; gsize num_opts; if ((sr_config_list(sdi->driver, SR_CONF_DEVICE_CONFIGS, &gvar_opts, sdi) != SR_OK)) /* Driver supports no device instance options. */ return; const int *const options = (const int32_t *)g_variant_get_fixed_array( gvar_opts, &num_opts, sizeof(int32_t)); for (unsigned int i = 0; i < num_opts; i++) { const struct sr_config_info *const info = sr_config_info_get(options[i]); if (!info) continue; const int key = info->key; if(sr_config_list(_sdi->driver, key, &gvar_list, _sdi) != SR_OK) gvar_list = NULL; const QString name(info->name); switch(key) { case SR_CONF_SAMPLERATE: bind_samplerate(name, gvar_list); break; case SR_CONF_CAPTURE_RATIO: bind_int(name, key, "%", pair<int64_t, int64_t>(0, 100)); break; case SR_CONF_DEVICE_MODE: case SR_CONF_PATTERN_MODE: case SR_CONF_BUFFERSIZE: case SR_CONF_TRIGGER_SOURCE: case SR_CONF_FILTER: case SR_CONF_COUPLING: bind_enum(name, key, gvar_list); break; case SR_CONF_RLE: bind_bool(name, key); break; case SR_CONF_CLOCK_TYPE: bind_bool(name, key); break; case SR_CONF_TIMEBASE: bind_enum(name, key, gvar_list, print_timebase); break; case SR_CONF_VDIV: bind_enum(name, key, gvar_list, print_vdiv); break; } if (gvar_list) g_variant_unref(gvar_list); } g_variant_unref(gvar_opts); }
void run_session(void) { GSList *devices; GHashTable *devargs; GVariant *gvar; struct sr_dev_inst *sdi; uint64_t min_samples, max_samples; devices = device_scan(); if (!devices) { g_critical("No devices found."); return; } if (g_slist_length(devices) > 1) { g_critical("sigrok-cli only supports one device for capturing."); return; } sdi = devices->data; sr_session_new(); sr_session_datafeed_callback_add(datafeed_in, NULL); if (sr_dev_open(sdi) != SR_OK) { g_critical("Failed to open device."); return; } if (sr_session_dev_add(sdi) != SR_OK) { g_critical("Failed to add device to session."); sr_session_destroy(); return; } if (opt_config) { if ((devargs = parse_generic_arg(opt_config, FALSE))) { if (set_dev_options(sdi, devargs) != SR_OK) return; g_hash_table_destroy(devargs); } } if (select_channels(sdi) != SR_OK) { g_critical("Failed to set channels."); sr_session_destroy(); return; } if (opt_triggers) { if (!parse_triggerstring(sdi, opt_triggers)) { sr_session_destroy(); return; } } if (opt_continuous) { if (!sr_dev_has_option(sdi, SR_CONF_CONTINUOUS)) { g_critical("This device does not support continuous sampling."); sr_session_destroy(); return; } } if (opt_time) { if (set_limit_time(sdi) != SR_OK) { sr_session_destroy(); return; } } if (opt_samples) { if ((sr_parse_sizestring(opt_samples, &limit_samples) != SR_OK)) { g_critical("Invalid sample limit '%s'.", opt_samples); sr_session_destroy(); return; } if (sr_config_list(sdi->driver, sdi, NULL, SR_CONF_LIMIT_SAMPLES, &gvar) == SR_OK) { /* The device has no compression, or compression is turned * off, and publishes its sample memory size. */ g_variant_get(gvar, "(tt)", &min_samples, &max_samples); g_variant_unref(gvar); if (limit_samples < min_samples) { g_critical("The device stores at least %"PRIu64 " samples with the current settings.", min_samples); } if (limit_samples > max_samples) { g_critical("The device can store only %"PRIu64 " samples with the current settings.", max_samples); } } gvar = g_variant_new_uint64(limit_samples); if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) { g_critical("Failed to configure sample limit."); sr_session_destroy(); return; } } if (opt_frames) { if ((sr_parse_sizestring(opt_frames, &limit_frames) != SR_OK)) { g_critical("Invalid sample limit '%s'.", opt_samples); sr_session_destroy(); return; } gvar = g_variant_new_uint64(limit_frames); if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_FRAMES, gvar) != SR_OK) { g_critical("Failed to configure frame limit."); sr_session_destroy(); return; } } if (sr_session_start() != SR_OK) { g_critical("Failed to start session."); sr_session_destroy(); return; } if (opt_continuous) add_anykey(); sr_session_run(); if (opt_continuous) clear_anykey(); sr_session_datafeed_callback_remove_all(); sr_session_destroy(); g_slist_free(devices); }
DeviceOptions::DeviceOptions(struct sr_dev_inst *sdi) : _sdi(sdi) { GVariant *gvar_opts, *gvar_list; gsize num_opts; if ((sr_config_list(sdi->driver, sdi, NULL, SR_CONF_DEVICE_CONFIGS, &gvar_opts) != SR_OK)) /* Driver supports no device instance options. */ return; const int *const options = (const int32_t *)g_variant_get_fixed_array( gvar_opts, &num_opts, sizeof(int32_t)); for (unsigned int i = 0; i < num_opts; i++) { const struct sr_config_info *const info = sr_config_info_get(options[i]); if (!info) continue; const int key = info->key; if(sr_config_list(_sdi->driver, _sdi, NULL, key, &gvar_list) != SR_OK) gvar_list = NULL; const QString name(info->label); switch(key) { case SR_CONF_SAMPLERATE: bind_samplerate(name, gvar_list); break; case SR_CONF_CAPTURE_RATIO: bind_int(name, key, "%", pair<int64_t, int64_t>(0, 100)); break; case SR_CONF_PATTERN_MODE: case SR_CONF_BUFFERSIZE: case SR_CONF_TRIGGER_SOURCE: case SR_CONF_FILTER: case SR_CONF_COUPLING: case SR_CONF_EN_CH: case SR_CONF_OPERATION_MODE: case SR_CONF_THRESHOLD: case SR_CONF_ZERO: case SR_CONF_STREAM: case SR_CONF_TEST: case SR_CONF_STATUS: case SR_CONF_FACTOR: bind_enum(name, key, gvar_list); break; case SR_CONF_VTH: bind_double(name, key, "V", pair<double, double>(0.0, 5.0), 1, 0.1); break; case SR_CONF_RLE: bind_bool(name, key); break; case SR_CONF_CLOCK_TYPE: case SR_CONF_CLOCK_EDGE: case SR_CONF_INSTANT: case SR_CONF_DATALOCK: bind_bool(name, key); break; case SR_CONF_TIMEBASE: bind_enum(name, key, gvar_list, print_timebase); break; case SR_CONF_VDIV: bind_enum(name, key, gvar_list, print_vdiv); break; default: gvar_list = NULL; } if (gvar_list) g_variant_unref(gvar_list); } g_variant_unref(gvar_opts); }
static int check_key(const struct sr_dev_driver *driver, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg, uint32_t key, int op, GVariant *data) { const struct sr_key_info *srci; gsize num_opts, i; GVariant *gvar_opts; const uint32_t *opts; uint32_t pub_opt; const char *suffix; const char *opstr; if (sdi && cg) suffix = " for this device and channel group"; else if (sdi) suffix = " for this device"; else suffix = ""; if (!(srci = sr_key_info_get(SR_KEY_CONFIG, key))) { sr_err("Invalid key %d.", key); return SR_ERR_ARG; } opstr = op == SR_CONF_GET ? "get" : op == SR_CONF_SET ? "set" : "list"; switch (key) { case SR_CONF_LIMIT_MSEC: case SR_CONF_LIMIT_SAMPLES: case SR_CONF_SAMPLERATE: /* Setting any of these to 0 is not useful. */ if (op != SR_CONF_SET || !data) break; if (g_variant_get_uint64(data) == 0) { sr_err("Cannot set '%s' to 0.", srci->id); return SR_ERR_ARG; } break; } if (sr_config_list(driver, sdi, cg, SR_CONF_DEVICE_OPTIONS, &gvar_opts) != SR_OK) { /* Driver publishes no options. */ sr_err("No options available%s.", suffix); return SR_ERR_ARG; } opts = g_variant_get_fixed_array(gvar_opts, &num_opts, sizeof(uint32_t)); pub_opt = 0; for (i = 0; i < num_opts; i++) { if ((opts[i] & SR_CONF_MASK) == key) { pub_opt = opts[i]; break; } } g_variant_unref(gvar_opts); if (!pub_opt) { sr_err("Option '%s' not available%s.", srci->id, suffix); return SR_ERR_ARG; } if (!(pub_opt & op)) { sr_err("Option '%s' not available to %s%s.", srci->id, opstr, suffix); return SR_ERR_ARG; } return SR_OK; }
void show_dev_detail(void) { struct sr_dev_inst *sdi; const struct sr_config_info *srci; struct sr_channel *ch; struct sr_channel_group *channel_group, *cg; GSList *devices, *cgl, *chl; GVariant *gvar_opts, *gvar_dict, *gvar_list, *gvar; gsize num_opts, num_elements; double dlow, dhigh, dcur_low, dcur_high; const uint64_t *uint64, p, q, low, high; uint64_t cur_low, cur_high; const int32_t *int32, *opts; unsigned int num_devices, o, i; char *tmp_str; char *s, c; const char **stropts; if (!(devices = device_scan())) { g_critical("No devices found."); return; } num_devices = g_slist_length(devices); if (num_devices > 1) { g_critical("%d devices found. Use --scan to show them, " "and select one to show.", num_devices); return; } sdi = devices->data; print_dev_line(sdi); if (sr_dev_open(sdi) != SR_OK) { g_critical("Failed to open device."); return; } if ((sr_config_list(sdi->driver, NULL, NULL, SR_CONF_SCAN_OPTIONS, &gvar_opts) == SR_OK)) { opts = g_variant_get_fixed_array(gvar_opts, &num_elements, sizeof(int32_t)); printf("Supported driver options:\n"); for (i = 0; i < num_elements; i++) { if (!(srci = sr_config_info_get(opts[i]))) continue; printf(" %s\n", srci->id); } g_variant_unref(gvar_opts); } /* Selected channels and channel group may affect which options are * returned, or which values for them. */ select_channels(sdi); channel_group = select_channel_group(sdi); if ((sr_config_list(sdi->driver, sdi, channel_group, SR_CONF_DEVICE_OPTIONS, &gvar_opts)) != SR_OK) /* Driver supports no device instance options. */ return; if (sdi->channel_groups) { printf("Channel groups:\n"); for (cgl = sdi->channel_groups; cgl; cgl = cgl->next) { cg = cgl->data; printf(" %s: channel%s", cg->name, g_slist_length(cg->channels) > 1 ? "s" : ""); for (chl = cg->channels; chl; chl = chl->next) { ch = chl->data; printf(" %s", ch->name); } printf("\n"); } } printf("Supported configuration options"); if (sdi->channel_groups) { if (!channel_group) printf(" across all channel groups"); else printf(" on channel group %s", channel_group->name); } printf(":\n"); opts = g_variant_get_fixed_array(gvar_opts, &num_opts, sizeof(int32_t)); for (o = 0; o < num_opts; o++) { if (!(srci = sr_config_info_get(opts[o]))) continue; if (srci->key == SR_CONF_TRIGGER_MATCH) { if (sr_config_list(sdi->driver, sdi, channel_group, srci->key, &gvar_list) != SR_OK) { printf("\n"); continue; } int32 = g_variant_get_fixed_array(gvar_list, &num_elements, sizeof(int32_t)); printf(" Supported triggers: "); for (i = 0; i < num_elements; i++) { switch(int32[i]) { case SR_TRIGGER_ZERO: c = '0'; break; case SR_TRIGGER_ONE: c = '1'; break; case SR_TRIGGER_RISING: c = 'r'; break; case SR_TRIGGER_FALLING: c = 'f'; break; case SR_TRIGGER_EDGE: c = 'e'; break; case SR_TRIGGER_OVER: c = 'o'; break; case SR_TRIGGER_UNDER: c = 'u'; break; default: c = 0; break; } if (c) printf("%c ", c); } printf("\n"); g_variant_unref(gvar_list); } else if (srci->key == SR_CONF_LIMIT_SAMPLES) { /* If implemented in config_list(), this denotes the * maximum number of samples a device can send. This * really applies only to logic analyzers, and then * only to those that don't support compression, or * have it turned off by default. The values returned * are the low/high limits. */ if (sr_config_list(sdi->driver, sdi, channel_group, srci->key, &gvar) != SR_OK) { continue; } g_variant_get(gvar, "(tt)", &low, &high); g_variant_unref(gvar); printf(" Maximum number of samples: %"PRIu64"\n", high); } else if (srci->key == SR_CONF_SAMPLERATE) { /* Supported samplerates */ printf(" %s", srci->id); if (sr_config_list(sdi->driver, sdi, channel_group, SR_CONF_SAMPLERATE, &gvar_dict) != SR_OK) { printf("\n"); continue; } if ((gvar_list = g_variant_lookup_value(gvar_dict, "samplerates", G_VARIANT_TYPE("at")))) { uint64 = g_variant_get_fixed_array(gvar_list, &num_elements, sizeof(uint64_t)); printf(" - supported samplerates:\n"); for (i = 0; i < num_elements; i++) { if (!(s = sr_samplerate_string(uint64[i]))) continue; printf(" %s\n", s); g_free(s); } g_variant_unref(gvar_list); } else if ((gvar_list = g_variant_lookup_value(gvar_dict, "samplerate-steps", G_VARIANT_TYPE("at")))) { uint64 = g_variant_get_fixed_array(gvar_list, &num_elements, sizeof(uint64_t)); /* low */ if (!(s = sr_samplerate_string(uint64[0]))) continue; printf(" (%s", s); g_free(s); /* high */ if (!(s = sr_samplerate_string(uint64[1]))) continue; printf(" - %s", s); g_free(s); /* step */ if (!(s = sr_samplerate_string(uint64[2]))) continue; printf(" in steps of %s)\n", s); g_free(s); g_variant_unref(gvar_list); } g_variant_unref(gvar_dict); } else if (srci->key == SR_CONF_BUFFERSIZE) { /* Supported buffer sizes */ printf(" %s", srci->id); if (sr_config_list(sdi->driver, sdi, channel_group, SR_CONF_BUFFERSIZE, &gvar_list) != SR_OK) { printf("\n"); continue; } uint64 = g_variant_get_fixed_array(gvar_list, &num_elements, sizeof(uint64_t)); printf(" - supported buffer sizes:\n"); for (i = 0; i < num_elements; i++) printf(" %"PRIu64"\n", uint64[i]); g_variant_unref(gvar_list); } else if (srci->key == SR_CONF_TIMEBASE) { /* Supported time bases */ printf(" %s", srci->id); if (sr_config_list(sdi->driver, sdi, channel_group, SR_CONF_TIMEBASE, &gvar_list) != SR_OK) { printf("\n"); continue; } printf(" - supported time bases:\n"); num_elements = g_variant_n_children(gvar_list); for (i = 0; i < num_elements; i++) { gvar = g_variant_get_child_value(gvar_list, i); g_variant_get(gvar, "(tt)", &p, &q); s = sr_period_string(p * q); printf(" %s\n", s); g_free(s); } g_variant_unref(gvar_list); } else if (srci->key == SR_CONF_VDIV) { /* Supported volts/div values */ printf(" %s", srci->id); if (sr_config_list(sdi->driver, sdi, channel_group, SR_CONF_VDIV, &gvar_list) != SR_OK) { printf("\n"); continue; } printf(" - supported volts/div:\n"); num_elements = g_variant_n_children(gvar_list); for (i = 0; i < num_elements; i++) { gvar = g_variant_get_child_value(gvar_list, i); g_variant_get(gvar, "(tt)", &p, &q); s = sr_voltage_string(p, q); printf(" %s\n", s); g_free(s); } g_variant_unref(gvar_list); } else if (srci->datatype == SR_T_STRING) { printf(" %s: ", srci->id); if (sr_config_get(sdi->driver, sdi, channel_group, srci->key, &gvar) == SR_OK) { tmp_str = g_strdup(g_variant_get_string(gvar, NULL)); g_variant_unref(gvar); } else tmp_str = NULL; if (sr_config_list(sdi->driver, sdi, channel_group, srci->key, &gvar) != SR_OK) { printf("\n"); continue; } stropts = g_variant_get_strv(gvar, &num_elements); for (i = 0; i < num_elements; i++) { if (i) printf(", "); printf("%s", stropts[i]); if (tmp_str && !strcmp(tmp_str, stropts[i])) printf(" (current)"); } printf("\n"); g_free(stropts); g_free(tmp_str); g_variant_unref(gvar); } else if (srci->datatype == SR_T_UINT64_RANGE) { printf(" %s: ", srci->id); if (sr_config_list(sdi->driver, sdi, channel_group, srci->key, &gvar_list) != SR_OK) { printf("\n"); continue; } if (sr_config_get(sdi->driver, sdi, NULL, srci->key, &gvar) == SR_OK) { g_variant_get(gvar, "(tt)", &cur_low, &cur_high); g_variant_unref(gvar); } else { cur_low = 0; cur_high = 0; } num_elements = g_variant_n_children(gvar_list); for (i = 0; i < num_elements; i++) { gvar = g_variant_get_child_value(gvar_list, i); g_variant_get(gvar, "(tt)", &low, &high); g_variant_unref(gvar); if (i) printf(", "); printf("%"PRIu64"-%"PRIu64, low, high); if (low == cur_low && high == cur_high) printf(" (current)"); } printf("\n"); g_variant_unref(gvar_list); } else if (srci->datatype == SR_T_BOOL) { printf(" %s: ", srci->id); if (sr_config_get(sdi->driver, sdi, NULL, srci->key, &gvar) == SR_OK) { if (g_variant_get_boolean(gvar)) printf("on (current), off\n"); else printf("on, off (current)\n"); g_variant_unref(gvar); } else printf("on, off\n"); } else if (srci->datatype == SR_T_DOUBLE_RANGE) { printf(" %s: ", srci->id); if (sr_config_list(sdi->driver, sdi, channel_group, srci->key, &gvar_list) != SR_OK) { printf("\n"); continue; } if (sr_config_get(sdi->driver, sdi, NULL, srci->key, &gvar) == SR_OK) { g_variant_get(gvar, "(dd)", &dcur_low, &dcur_high); g_variant_unref(gvar); } else { dcur_low = 0; dcur_high = 0; } num_elements = g_variant_n_children(gvar_list); for (i = 0; i < num_elements; i++) { gvar = g_variant_get_child_value(gvar_list, i); g_variant_get(gvar, "(dd)", &dlow, &dhigh); g_variant_unref(gvar); if (i) printf(", "); printf("%.1f-%.1f", dlow, dhigh); if (dlow == dcur_low && dhigh == dcur_high) printf(" (current)"); } printf("\n"); g_variant_unref(gvar_list); } else { /* Everything else */ printf(" %s\n", srci->id); } } g_variant_unref(gvar_opts); sr_dev_close(sdi); g_slist_free(devices); }