+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)
+{
+ const struct sr_config_info *srci;
+ gsize num_opts, i;
+ GVariant *gvar_opts;
+ const uint32_t *opts;
+ uint32_t pub_opt;
+ char *suffix, *opstr;
+
+ if (sdi && cg)
+ suffix = " for this device and channel group";
+ else if (sdi)
+ suffix = " for this device";
+ else
+ suffix = "";
+
+ if (!(srci = sr_config_info_get(key))) {
+ sr_err("Invalid key %d.", key);
+ return SR_ERR_ARG;
+ }
+ opstr = op == SR_CONF_GET ? "get" : op == SR_CONF_SET ? "set" : "list";
+ sr_spew("sr_config_%s(): key %d (%s) sdi %p cg %s", opstr, key,
+ srci->id, sdi, cg ? cg->name : "NULL");
+
+ 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.", srci->id, 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;
+}
+