struct context {
unsigned int num_enabled_channels;
- unsigned int unitsize;
uint64_t samplerate;
GString *header;
char separator;
+ int *channel_index;
};
/*
struct sr_channel *ch;
GSList *l;
GVariant *gvar;
- int num_channels;
+ int num_channels, i, j;
time_t t;
if (!o)
if (!o->sdi)
return SR_ERR_ARG;
- ctx = g_try_malloc0(sizeof(struct context));
+ ctx = g_malloc0(sizeof(struct context));
o->internal = ctx;
/* Get the number of channels, and the unitsize. */
continue;
ctx->num_enabled_channels++;
}
-
- ctx->unitsize = (ctx->num_enabled_channels + 7) / 8;
+ ctx->channel_index = g_malloc(sizeof(int) * ctx->num_enabled_channels);
num_channels = g_slist_length(o->sdi->channels);
/* Columns / channels */
g_string_append_printf(ctx->header, "; Channels (%d/%d):",
ctx->num_enabled_channels, num_channels);
- for (l = o->sdi->channels; l; l = l->next) {
+ for (i = 0, j = 0, l = o->sdi->channels; l; l = l->next, i++) {
ch = l->data;
if (ch->type != SR_CHANNEL_LOGIC)
continue;
if (!ch->enabled)
continue;
g_string_append_printf(ctx->header, " %s,", ch->name);
+ /* Remember the enabled channel's index while we're at it. */
+ ctx->channel_index[j++] = i;
}
if (o->sdi->channels)
/* Drop last separator. */
{
const struct sr_datafeed_logic *logic;
struct context *ctx;
+ int idx;
uint64_t i, j;
gchar *p, c;
*out = g_string_sized_new(512);
}
- for (i = 0; i <= logic->length - ctx->unitsize; i += ctx->unitsize) {
+ for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
for (j = 0; j < ctx->num_enabled_channels; j++) {
- p = logic->data + i + j / 8;
- c = *p & (1 << (j % 8));
+ idx = ctx->channel_index[j];
+ p = logic->data + i + idx / 8;
+ c = *p & (1 << (idx % 8));
g_string_append_c(*out, c ? '1' : '0');
g_string_append_c(*out, ctx->separator);
}
g_string_append_printf(*out, "\n");
}
break;
- case SR_DF_END:
- g_free(o->internal);
- o->internal = NULL;
- break;
}
return SR_OK;
ctx = o->internal;
if (ctx->header)
g_string_free(ctx->header, TRUE);
+ g_free(ctx->channel_index);
g_free(o->internal);
o->internal = NULL;
}