output/csv: Use new output API.
This commit is contained in:
parent
2b36d6c64e
commit
4829d37d6a
122
output/csv.c
122
output/csv.c
|
@ -56,21 +56,13 @@ static int init(struct sr_output *o)
|
||||||
int num_probes;
|
int num_probes;
|
||||||
time_t t;
|
time_t t;
|
||||||
|
|
||||||
if (!o) {
|
if (!o)
|
||||||
sr_err("%s: o was NULL", __func__);
|
|
||||||
return SR_ERR_ARG;
|
return SR_ERR_ARG;
|
||||||
}
|
|
||||||
|
|
||||||
if (!o->sdi) {
|
if (!o->sdi)
|
||||||
sr_err("%s: o->sdi was NULL", __func__);
|
|
||||||
return SR_ERR_ARG;
|
return SR_ERR_ARG;
|
||||||
}
|
|
||||||
|
|
||||||
if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
|
|
||||||
sr_err("%s: ctx malloc failed", __func__);
|
|
||||||
return SR_ERR_MALLOC;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
ctx = g_try_malloc0(sizeof(struct context));
|
||||||
o->internal = ctx;
|
o->internal = ctx;
|
||||||
|
|
||||||
/* Get the number of probes, and the unitsize. */
|
/* Get the number of probes, and the unitsize. */
|
||||||
|
@ -121,95 +113,69 @@ static int init(struct sr_output *o)
|
||||||
return SR_OK;
|
return SR_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int event(struct sr_output *o, int event_type, uint8_t **data_out,
|
static int receive(struct sr_output *o, const struct sr_dev_inst *sdi,
|
||||||
uint64_t *length_out)
|
const struct sr_datafeed_packet *packet, GString **out)
|
||||||
{
|
{
|
||||||
|
const struct sr_datafeed_logic *logic;
|
||||||
struct context *ctx;
|
struct context *ctx;
|
||||||
|
uint64_t sample, i, j;
|
||||||
|
|
||||||
if (!o) {
|
(void)sdi;
|
||||||
sr_err("%s: o was NULL", __func__);
|
|
||||||
|
*out = NULL;
|
||||||
|
if (!o || !o->sdi)
|
||||||
return SR_ERR_ARG;
|
return SR_ERR_ARG;
|
||||||
}
|
if (!(ctx = o->internal))
|
||||||
|
|
||||||
if (!(ctx = o->internal)) {
|
|
||||||
sr_err("%s: o->internal was NULL", __func__);
|
|
||||||
return SR_ERR_ARG;
|
return SR_ERR_ARG;
|
||||||
}
|
|
||||||
|
|
||||||
if (!data_out) {
|
switch (packet->type) {
|
||||||
sr_err("%s: data_out was NULL", __func__);
|
case SR_DF_LOGIC:
|
||||||
return SR_ERR_ARG;
|
logic = packet->payload;
|
||||||
}
|
if (ctx->header) {
|
||||||
|
/*
|
||||||
|
* First data packet: prime the output with the
|
||||||
|
* previously prepared header.
|
||||||
|
*/
|
||||||
|
*out = ctx->header;
|
||||||
|
ctx->header = NULL;
|
||||||
|
} else {
|
||||||
|
*out = g_string_sized_new(512);
|
||||||
|
}
|
||||||
|
|
||||||
switch (event_type) {
|
for (i = 0; i <= logic->length - ctx->unitsize; i += ctx->unitsize) {
|
||||||
case SR_DF_TRIGGER:
|
memcpy(&sample, logic->data + i, ctx->unitsize);
|
||||||
sr_dbg("%s: SR_DF_TRIGGER event", __func__);
|
for (j = 0; j < ctx->num_enabled_probes; j++) {
|
||||||
/* TODO */
|
g_string_append_printf(*out, "%d%c",
|
||||||
*data_out = NULL;
|
(int)((sample & (1 << j)) >> j),
|
||||||
*length_out = 0;
|
ctx->separator);
|
||||||
|
}
|
||||||
|
g_string_append_printf(*out, "\n");
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case SR_DF_END:
|
case SR_DF_END:
|
||||||
sr_dbg("%s: SR_DF_END event", __func__);
|
|
||||||
/* TODO */
|
|
||||||
*data_out = NULL;
|
|
||||||
*length_out = 0;
|
|
||||||
g_free(o->internal);
|
g_free(o->internal);
|
||||||
o->internal = NULL;
|
o->internal = NULL;
|
||||||
break;
|
break;
|
||||||
default:
|
|
||||||
sr_err("%s: unsupported event type: %d", __func__, event_type);
|
|
||||||
*data_out = NULL;
|
|
||||||
*length_out = 0;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return SR_OK;
|
return SR_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int data(struct sr_output *o, const uint8_t *data_in,
|
static int cleanup(struct sr_output *o)
|
||||||
uint64_t length_in, uint8_t **data_out, uint64_t *length_out)
|
|
||||||
{
|
{
|
||||||
struct context *ctx;
|
struct context *ctx;
|
||||||
GString *outstr;
|
|
||||||
uint64_t sample, i, j;
|
|
||||||
|
|
||||||
if (!o) {
|
if (!o || !o->sdi)
|
||||||
sr_err("%s: o was NULL", __func__);
|
|
||||||
return SR_ERR_ARG;
|
return SR_ERR_ARG;
|
||||||
}
|
|
||||||
|
|
||||||
if (!(ctx = o->internal)) {
|
if (o->internal) {
|
||||||
sr_err("%s: o->internal was NULL", __func__);
|
ctx = o->internal;
|
||||||
return SR_ERR_ARG;
|
if (ctx->header)
|
||||||
|
g_string_free(ctx->header, TRUE);
|
||||||
|
g_free(o->internal);
|
||||||
|
o->internal = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!data_in) {
|
|
||||||
sr_err("%s: data_in was NULL", __func__);
|
|
||||||
return SR_ERR_ARG;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ctx->header) {
|
|
||||||
/* First data packet. */
|
|
||||||
outstr = ctx->header;
|
|
||||||
ctx->header = NULL;
|
|
||||||
} else {
|
|
||||||
outstr = g_string_sized_new(512);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
|
|
||||||
memcpy(&sample, data_in + i, ctx->unitsize);
|
|
||||||
for (j = 0; j < ctx->num_enabled_probes; j++) {
|
|
||||||
g_string_append_printf(outstr, "%d%c",
|
|
||||||
(int)((sample & (1 << j)) >> j),
|
|
||||||
ctx->separator);
|
|
||||||
}
|
|
||||||
g_string_append_printf(outstr, "\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
*data_out = (uint8_t *)outstr->str;
|
|
||||||
*length_out = outstr->len;
|
|
||||||
g_string_free(outstr, FALSE);
|
|
||||||
|
|
||||||
return SR_OK;
|
return SR_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -218,6 +184,6 @@ SR_PRIV struct sr_output_format output_csv = {
|
||||||
.description = "Comma-separated values (CSV)",
|
.description = "Comma-separated values (CSV)",
|
||||||
.df_type = SR_DF_LOGIC,
|
.df_type = SR_DF_LOGIC,
|
||||||
.init = init,
|
.init = init,
|
||||||
.data = data,
|
.receive = receive,
|
||||||
.event = event,
|
.cleanup = cleanup,
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue