output: Move option checks to the wrapper.
Output modules are now guaranteed: - Every option is always given, with the default value if not supplied by the user, and is the right GVariantType. - No invalid options are ever passed.
This commit is contained in:
parent
950043c30e
commit
dcc55fe91a
|
@ -30,7 +30,7 @@
|
||||||
|
|
||||||
struct context {
|
struct context {
|
||||||
unsigned int num_enabled_channels;
|
unsigned int num_enabled_channels;
|
||||||
int samples_per_line;
|
int spl;
|
||||||
int bit_cnt;
|
int bit_cnt;
|
||||||
int spl_cnt;
|
int spl_cnt;
|
||||||
int trigger;
|
int trigger;
|
||||||
|
@ -49,37 +49,15 @@ static int init(struct sr_output *o, GHashTable *options)
|
||||||
struct context *ctx;
|
struct context *ctx;
|
||||||
struct sr_channel *ch;
|
struct sr_channel *ch;
|
||||||
GSList *l;
|
GSList *l;
|
||||||
GHashTableIter iter;
|
|
||||||
gpointer key, value;
|
|
||||||
unsigned int i, j;
|
unsigned int i, j;
|
||||||
uint32_t spl;
|
|
||||||
|
|
||||||
if (!o || !o->sdi)
|
if (!o || !o->sdi)
|
||||||
return SR_ERR_ARG;
|
return SR_ERR_ARG;
|
||||||
|
|
||||||
spl = DEFAULT_SAMPLES_PER_LINE;
|
|
||||||
if (options) {
|
|
||||||
g_hash_table_iter_init(&iter, options);
|
|
||||||
while (g_hash_table_iter_next(&iter, &key, &value)) {
|
|
||||||
if (!strcmp(key, "width")) {
|
|
||||||
if (!g_variant_is_of_type(value, G_VARIANT_TYPE_UINT32)) {
|
|
||||||
sr_err("Invalid type for 'width' option.");
|
|
||||||
return SR_ERR_ARG;
|
|
||||||
}
|
|
||||||
if (!(spl = g_variant_get_uint32(value))) {
|
|
||||||
sr_err("Invalid width.");
|
|
||||||
return SR_ERR_ARG;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
sr_err("Unknown option '%s'.", key);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx = g_malloc0(sizeof(struct context));
|
ctx = g_malloc0(sizeof(struct context));
|
||||||
o->priv = ctx;
|
o->priv = ctx;
|
||||||
ctx->trigger = -1;
|
ctx->trigger = -1;
|
||||||
ctx->samples_per_line = spl;
|
ctx->spl = g_variant_get_uint32(g_hash_table_lookup(options, "width"));
|
||||||
|
|
||||||
for (l = o->sdi->channels; l; l = l->next) {
|
for (l = o->sdi->channels; l; l = l->next) {
|
||||||
ch = l->data;
|
ch = l->data;
|
||||||
|
@ -199,7 +177,7 @@ static int receive(const struct sr_output *o, const struct sr_datafeed_packet *p
|
||||||
}
|
}
|
||||||
g_string_append_c(ctx->lines[j], c);
|
g_string_append_c(ctx->lines[j], c);
|
||||||
|
|
||||||
if (ctx->spl_cnt == ctx->samples_per_line) {
|
if (ctx->spl_cnt == ctx->spl) {
|
||||||
/* Flush line buffers. */
|
/* Flush line buffers. */
|
||||||
g_string_append_len(*out, ctx->lines[j]->str, ctx->lines[j]->len);
|
g_string_append_len(*out, ctx->lines[j]->str, ctx->lines[j]->len);
|
||||||
g_string_append_c(*out, '\n');
|
g_string_append_c(*out, '\n');
|
||||||
|
@ -211,7 +189,7 @@ static int receive(const struct sr_output *o, const struct sr_datafeed_packet *p
|
||||||
g_string_printf(ctx->lines[j], "%s:", ctx->channel_names[j]);
|
g_string_printf(ctx->lines[j], "%s:", ctx->channel_names[j]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (ctx->spl_cnt == ctx->samples_per_line)
|
if (ctx->spl_cnt == ctx->spl)
|
||||||
/* Line buffers were already flushed. */
|
/* Line buffers were already flushed. */
|
||||||
ctx->spl_cnt = 0;
|
ctx->spl_cnt = 0;
|
||||||
memcpy(ctx->prev_sample, logic->data + i, logic->unitsize);
|
memcpy(ctx->prev_sample, logic->data + i, logic->unitsize);
|
||||||
|
|
|
@ -29,7 +29,7 @@
|
||||||
|
|
||||||
struct context {
|
struct context {
|
||||||
unsigned int num_enabled_channels;
|
unsigned int num_enabled_channels;
|
||||||
int samples_per_line;
|
int spl;
|
||||||
int spl_cnt;
|
int spl_cnt;
|
||||||
int trigger;
|
int trigger;
|
||||||
uint64_t samplerate;
|
uint64_t samplerate;
|
||||||
|
@ -44,37 +44,15 @@ static int init(struct sr_output *o, GHashTable *options)
|
||||||
struct context *ctx;
|
struct context *ctx;
|
||||||
struct sr_channel *ch;
|
struct sr_channel *ch;
|
||||||
GSList *l;
|
GSList *l;
|
||||||
GHashTableIter iter;
|
|
||||||
gpointer key, value;
|
|
||||||
unsigned int i, j;
|
unsigned int i, j;
|
||||||
uint32_t spl;
|
|
||||||
|
|
||||||
if (!o || !o->sdi)
|
if (!o || !o->sdi)
|
||||||
return SR_ERR_ARG;
|
return SR_ERR_ARG;
|
||||||
|
|
||||||
spl = DEFAULT_SAMPLES_PER_LINE;
|
|
||||||
if (options) {
|
|
||||||
g_hash_table_iter_init(&iter, options);
|
|
||||||
while (g_hash_table_iter_next(&iter, &key, &value)) {
|
|
||||||
if (!strcmp(key, "width")) {
|
|
||||||
if (!g_variant_is_of_type(value, G_VARIANT_TYPE_UINT32)) {
|
|
||||||
sr_err("Invalid type for 'width' option.");
|
|
||||||
return SR_ERR_ARG;
|
|
||||||
}
|
|
||||||
if (!(spl = g_variant_get_uint32(value))) {
|
|
||||||
sr_err("Invalid width.");
|
|
||||||
return SR_ERR_ARG;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
sr_err("Unknown option '%s'.", key);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx = g_malloc0(sizeof(struct context));
|
ctx = g_malloc0(sizeof(struct context));
|
||||||
o->priv = ctx;
|
o->priv = ctx;
|
||||||
ctx->trigger = -1;
|
ctx->trigger = -1;
|
||||||
ctx->samples_per_line = spl;
|
ctx->spl = g_variant_get_uint32(g_hash_table_lookup(options, "width"));
|
||||||
|
|
||||||
for (l = o->sdi->channels; l; l = l->next) {
|
for (l = o->sdi->channels; l; l = l->next) {
|
||||||
ch = l->data;
|
ch = l->data;
|
||||||
|
@ -184,7 +162,7 @@ static int receive(const struct sr_output *o, const struct sr_datafeed_packet *p
|
||||||
c = (*p & (1 << (idx % 8))) ? '1' : '0';
|
c = (*p & (1 << (idx % 8))) ? '1' : '0';
|
||||||
g_string_append_c(ctx->lines[j], c);
|
g_string_append_c(ctx->lines[j], c);
|
||||||
|
|
||||||
if (ctx->spl_cnt == ctx->samples_per_line) {
|
if (ctx->spl_cnt == ctx->spl) {
|
||||||
/* Flush line buffers. */
|
/* Flush line buffers. */
|
||||||
g_string_append_len(*out, ctx->lines[j]->str, ctx->lines[j]->len);
|
g_string_append_len(*out, ctx->lines[j]->str, ctx->lines[j]->len);
|
||||||
g_string_append_c(*out, '\n');
|
g_string_append_c(*out, '\n');
|
||||||
|
@ -199,7 +177,7 @@ static int receive(const struct sr_output *o, const struct sr_datafeed_packet *p
|
||||||
g_string_append_c(ctx->lines[j], ' ');
|
g_string_append_c(ctx->lines[j], ' ');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (ctx->spl_cnt == ctx->samples_per_line)
|
if (ctx->spl_cnt == ctx->spl)
|
||||||
/* Line buffers were already flushed. */
|
/* Line buffers were already flushed. */
|
||||||
ctx->spl_cnt = 0;
|
ctx->spl_cnt = 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,7 +29,7 @@
|
||||||
|
|
||||||
struct context {
|
struct context {
|
||||||
unsigned int num_enabled_channels;
|
unsigned int num_enabled_channels;
|
||||||
int samples_per_line;
|
int spl;
|
||||||
int bit_cnt;
|
int bit_cnt;
|
||||||
int spl_cnt;
|
int spl_cnt;
|
||||||
int trigger;
|
int trigger;
|
||||||
|
@ -47,38 +47,15 @@ static int init(struct sr_output *o, GHashTable *options)
|
||||||
struct context *ctx;
|
struct context *ctx;
|
||||||
struct sr_channel *ch;
|
struct sr_channel *ch;
|
||||||
GSList *l;
|
GSList *l;
|
||||||
GHashTableIter iter;
|
|
||||||
gpointer key, value;
|
|
||||||
unsigned int i, j;
|
unsigned int i, j;
|
||||||
uint32_t spl;
|
|
||||||
|
|
||||||
if (!o || !o->sdi)
|
if (!o || !o->sdi)
|
||||||
return SR_ERR_ARG;
|
return SR_ERR_ARG;
|
||||||
|
|
||||||
spl = DEFAULT_SAMPLES_PER_LINE;
|
|
||||||
if (options) {
|
|
||||||
g_hash_table_iter_init(&iter, options);
|
|
||||||
while (g_hash_table_iter_next(&iter, &key, &value)) {
|
|
||||||
if (!strcmp(key, "width")) {
|
|
||||||
if (!g_variant_is_of_type(value, G_VARIANT_TYPE_UINT32)) {
|
|
||||||
sr_err("Invalid type for 'width' option.");
|
|
||||||
return SR_ERR_ARG;
|
|
||||||
}
|
|
||||||
if (!(spl = g_variant_get_uint32(value))) {
|
|
||||||
sr_err("Invalid width.");
|
|
||||||
return SR_ERR_ARG;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
sr_err("Unknown option '%s'.", key);
|
|
||||||
return SR_ERR_ARG;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx = g_malloc0(sizeof(struct context));
|
ctx = g_malloc0(sizeof(struct context));
|
||||||
o->priv = ctx;
|
o->priv = ctx;
|
||||||
ctx->trigger = -1;
|
ctx->trigger = -1;
|
||||||
ctx->samples_per_line = spl;
|
ctx->spl = g_variant_get_uint32(g_hash_table_lookup(options, "width"));
|
||||||
|
|
||||||
for (l = o->sdi->channels; l; l = l->next) {
|
for (l = o->sdi->channels; l; l = l->next) {
|
||||||
ch = l->data;
|
ch = l->data;
|
||||||
|
@ -198,7 +175,7 @@ static int receive(const struct sr_output *o, const struct sr_datafeed_packet *p
|
||||||
ctx->sample_buf[j] = 0;
|
ctx->sample_buf[j] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ctx->spl_cnt == ctx->samples_per_line) {
|
if (ctx->spl_cnt == ctx->spl) {
|
||||||
/* Flush line buffers. */
|
/* Flush line buffers. */
|
||||||
g_string_append_len(*out, ctx->lines[j]->str, ctx->lines[j]->len);
|
g_string_append_len(*out, ctx->lines[j]->str, ctx->lines[j]->len);
|
||||||
g_string_append_c(*out, '\n');
|
g_string_append_c(*out, '\n');
|
||||||
|
@ -210,7 +187,7 @@ static int receive(const struct sr_output *o, const struct sr_datafeed_packet *p
|
||||||
g_string_printf(ctx->lines[j], "%s:", ctx->channel_names[j]);
|
g_string_printf(ctx->lines[j], "%s:", ctx->channel_names[j]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (ctx->spl_cnt == ctx->samples_per_line)
|
if (ctx->spl_cnt == ctx->spl)
|
||||||
/* Line buffers were already flushed. */
|
/* Line buffers were already flushed. */
|
||||||
ctx->spl_cnt = 0;
|
ctx->spl_cnt = 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -212,15 +212,59 @@ SR_API const struct sr_output *sr_output_new(const struct sr_output_module *o,
|
||||||
GHashTable *options, const struct sr_dev_inst *sdi)
|
GHashTable *options, const struct sr_dev_inst *sdi)
|
||||||
{
|
{
|
||||||
struct sr_output *op;
|
struct sr_output *op;
|
||||||
|
struct sr_option *mod_opts;
|
||||||
|
const GVariantType *gvt;
|
||||||
|
GHashTable *new_opts;
|
||||||
|
GHashTableIter iter;
|
||||||
|
gpointer key, value;
|
||||||
|
int i;
|
||||||
|
|
||||||
op = g_malloc(sizeof(struct sr_output));
|
op = g_malloc(sizeof(struct sr_output));
|
||||||
op->module = o;
|
op->module = o;
|
||||||
op->sdi = sdi;
|
op->sdi = sdi;
|
||||||
|
|
||||||
if (op->module->init && op->module->init(op, options) != SR_OK) {
|
if (options) {
|
||||||
|
new_opts = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
|
||||||
|
(GDestroyNotify)g_variant_unref);
|
||||||
|
mod_opts = o->options();
|
||||||
|
for (i = 0; mod_opts[i].id; i++) {
|
||||||
|
if (g_hash_table_lookup_extended(options, mod_opts[i].id,
|
||||||
|
&key, &value)) {
|
||||||
|
/* Option not given: insert the default value. */
|
||||||
|
gvt = g_variant_get_type(mod_opts[i].def);
|
||||||
|
if (!g_variant_is_of_type(value, gvt)) {
|
||||||
|
sr_err("Invalid type for '%s' option.", key);
|
||||||
|
g_free(op);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
g_hash_table_insert(new_opts, g_strdup(mod_opts[i].id),
|
||||||
|
g_variant_ref(value));
|
||||||
|
} else {
|
||||||
|
/* Pass option along. */
|
||||||
|
g_hash_table_insert(new_opts, g_strdup(mod_opts[i].id),
|
||||||
|
g_variant_ref(mod_opts[i].def));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Make sure no invalid options were given. */
|
||||||
|
g_hash_table_iter_init(&iter, options);
|
||||||
|
while (g_hash_table_iter_next(&iter, &key, &value)) {
|
||||||
|
if (!g_hash_table_lookup(new_opts, key)) {
|
||||||
|
sr_err("Output module '%s' has no option '%s'", o->id, key);
|
||||||
|
g_hash_table_destroy(new_opts);
|
||||||
|
g_free(op);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else
|
||||||
|
new_opts = NULL;
|
||||||
|
|
||||||
|
if (op->module->init && op->module->init(op, new_opts) != SR_OK) {
|
||||||
|
g_hash_table_destroy(new_opts);
|
||||||
g_free(op);
|
g_free(op);
|
||||||
op = NULL;
|
op = NULL;
|
||||||
}
|
}
|
||||||
|
g_hash_table_destroy(new_opts);
|
||||||
|
|
||||||
return op;
|
return op;
|
||||||
}
|
}
|
||||||
|
|
|
@ -91,28 +91,10 @@ static int init(struct sr_output *o, GHashTable *options)
|
||||||
struct out_context *outc;
|
struct out_context *outc;
|
||||||
struct sr_channel *ch;
|
struct sr_channel *ch;
|
||||||
GSList *l;
|
GSList *l;
|
||||||
GHashTableIter iter;
|
|
||||||
gpointer key, value;
|
|
||||||
|
|
||||||
outc = g_malloc0(sizeof(struct out_context));
|
outc = g_malloc0(sizeof(struct out_context));
|
||||||
o->priv = outc;
|
o->priv = outc;
|
||||||
|
outc->scale = g_variant_get_double(g_hash_table_lookup(options, "scale"));
|
||||||
outc->scale = 0.0;
|
|
||||||
if (options) {
|
|
||||||
g_hash_table_iter_init(&iter, options);
|
|
||||||
while (g_hash_table_iter_next(&iter, &key, &value)) {
|
|
||||||
if (!strcmp(key, "scale")) {
|
|
||||||
if (!g_variant_is_of_type(value, G_VARIANT_TYPE_DOUBLE)) {
|
|
||||||
sr_err("Invalid type for 'scale' option.");
|
|
||||||
return SR_ERR_ARG;
|
|
||||||
}
|
|
||||||
outc->scale = g_variant_get_double(value);
|
|
||||||
} else {
|
|
||||||
sr_err("Unknown option '%s'.", key);
|
|
||||||
return SR_ERR_ARG;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (l = o->sdi->channels; l; l = l->next) {
|
for (l = o->sdi->channels; l; l = l->next) {
|
||||||
ch = l->data;
|
ch = l->data;
|
||||||
|
@ -359,7 +341,7 @@ static struct sr_option options[] = {
|
||||||
static struct sr_option *get_options(void)
|
static struct sr_option *get_options(void)
|
||||||
{
|
{
|
||||||
if (!options[0].def) {
|
if (!options[0].def) {
|
||||||
options[0].def = g_variant_new_double(0);
|
options[0].def = g_variant_new_double(0.0);
|
||||||
g_variant_ref_sink(options[0].def);
|
g_variant_ref_sink(options[0].def);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue