sr: change input/output modules to use struct sr_dev_inst *

This commit is contained in:
Bert Vermeulen 2012-07-20 21:37:36 +02:00
parent 6f4b1868e8
commit 5c3c1241d2
10 changed files with 113 additions and 103 deletions

View File

@ -44,6 +44,7 @@ static int format_match(const char *filename)
static int init(struct sr_input *in)
{
struct sr_probe *probe;
int num_probes, i;
char name[SR_MAX_PROBENAME_LEN + 1];
char *param;
@ -73,13 +74,15 @@ static int init(struct sr_input *in)
}
/* Create a virtual device. */
in->vdev = sr_dev_new(NULL, 0);
in->sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, NULL, NULL, NULL);
in->internal = ctx;
for (i = 0; i < num_probes; i++) {
snprintf(name, SR_MAX_PROBENAME_LEN, "%d", i);
/* TODO: Check return value. */
sr_dev_probe_add(in->vdev, name);
if (!(probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, name)))
return SR_ERR;
in->sdi->probes = g_slist_append(in->sdi->probes, probe);
}
return SR_OK;
@ -100,21 +103,21 @@ static int loadfile(struct sr_input *in, const char *filename)
if ((fd = open(filename, O_RDONLY)) == -1)
return SR_ERR;
num_probes = g_slist_length(in->vdev->probes);
num_probes = g_slist_length(in->sdi->probes);
/* send header */
header.feed_version = 1;
gettimeofday(&header.starttime, NULL);
packet.type = SR_DF_HEADER;
packet.payload = &header;
sr_session_send(in->vdev, &packet);
sr_session_send(in->sdi, &packet);
/* Send metadata about the SR_DF_LOGIC packets to come. */
packet.type = SR_DF_META_LOGIC;
packet.payload = &meta;
meta.samplerate = ctx->samplerate;
meta.num_probes = num_probes;
sr_session_send(in->vdev, &packet);
sr_session_send(in->sdi, &packet);
/* chop up the input file into chunks and feed it into the session bus */
packet.type = SR_DF_LOGIC;
@ -123,13 +126,13 @@ static int loadfile(struct sr_input *in, const char *filename)
logic.data = buffer;
while ((size = read(fd, buffer, CHUNKSIZE)) > 0) {
logic.length = size;
sr_session_send(in->vdev, &packet);
sr_session_send(in->sdi, &packet);
}
close(fd);
/* end of stream */
packet.type = SR_DF_END;
sr_session_send(in->vdev, &packet);
sr_session_send(in->sdi, &packet);
g_free(ctx);
in->internal = NULL;

View File

@ -94,6 +94,7 @@ static int format_match(const char *filename)
static int init(struct sr_input *in)
{
struct sr_probe *probe;
int num_probes, i;
char name[SR_MAX_PROBENAME_LEN + 1];
char *param;
@ -112,12 +113,14 @@ static int init(struct sr_input *in)
}
/* Create a virtual device. */
in->vdev = sr_dev_new(NULL, 0);
in->sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, NULL, NULL, NULL);
for (i = 0; i < num_probes; i++) {
snprintf(name, SR_MAX_PROBENAME_LEN, "%d", i);
/* TODO: Check return value. */
sr_dev_probe_add(in->vdev, name);
if (!(probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, name)))
return SR_ERR;
in->sdi->probes = g_slist_append(in->sdi->probes, probe);
}
return SR_OK;
@ -139,7 +142,7 @@ static int loadfile(struct sr_input *in, const char *filename)
return SR_ERR;
}
num_probes = g_slist_length(in->vdev->probes);
num_probes = g_slist_length(in->sdi->probes);
/* Seek to the end of the file, and read the divcount byte. */
divcount = 0x00; /* TODO: Don't hardcode! */
@ -158,14 +161,14 @@ static int loadfile(struct sr_input *in, const char *filename)
packet.payload = &header;
header.feed_version = 1;
gettimeofday(&header.starttime, NULL);
sr_session_send(in->vdev, &packet);
sr_session_send(in->sdi, &packet);
/* Send metadata about the SR_DF_LOGIC packets to come. */
packet.type = SR_DF_META_LOGIC;
packet.payload = &meta;
meta.samplerate = samplerate;
meta.num_probes = num_probes;
sr_session_send(in->vdev, &packet);
sr_session_send(in->sdi, &packet);
/* TODO: Handle trigger point. */
@ -181,7 +184,7 @@ static int loadfile(struct sr_input *in, const char *filename)
/* TODO: Handle errors, handle incomplete reads. */
size = read(fd, buf, PACKET_SIZE);
logic.length = size;
sr_session_send(in->vdev, &packet);
sr_session_send(in->sdi, &packet);
}
close(fd); /* FIXME */
@ -189,7 +192,7 @@ static int loadfile(struct sr_input *in, const char *filename)
sr_dbg("la8 in: %s: sending SR_DF_END", __func__);
packet.type = SR_DF_END;
packet.payload = NULL;
sr_session_send(in->vdev, &packet);
sr_session_send(in->sdi, &packet);
return SR_OK;
}

View File

@ -194,7 +194,7 @@ struct sr_datafeed_analog {
struct sr_input {
struct sr_input_format *format;
GHashTable *param;
struct sr_dev *vdev;
struct sr_dev_inst *sdi;
void *internal;
};
@ -208,7 +208,7 @@ struct sr_input_format {
struct sr_output {
struct sr_output_format *format;
struct sr_dev *dev;
struct sr_dev_inst *sdi;
char *param;
void *internal;
};

View File

@ -86,20 +86,20 @@ static int init(struct sr_output *o)
struct context *ctx;
struct sr_probe *probe;
GSList *l;
uint64_t samplerate;
uint64_t *samplerate;
if (!o) {
sr_warn("la8 out: %s: o was NULL", __func__);
return SR_ERR_ARG;
}
if (!o->dev) {
sr_warn("la8 out: %s: o->dev was NULL", __func__);
if (!o->sdi) {
sr_warn("la8 out: %s: o->sdi was NULL", __func__);
return SR_ERR_ARG;
}
if (!o->dev->driver) {
sr_warn("la8 out: %s: o->dev->driver was NULL", __func__);
if (!o->sdi->driver) {
sr_warn("la8 out: %s: o->sdi->driver was NULL", __func__);
return SR_ERR_ARG;
}
@ -111,8 +111,7 @@ static int init(struct sr_output *o)
o->internal = ctx;
/* Get the probe names and the unitsize. */
/* TODO: Error handling. */
for (l = o->dev->probes; l; l = l->next) {
for (l = o->sdi->probes; l; l = l->next) {
probe = l->data;
if (!probe->enabled)
continue;
@ -121,16 +120,14 @@ static int init(struct sr_output *o)
ctx->probelist[ctx->num_enabled_probes] = 0;
ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
if (sr_dev_has_hwcap(o->dev, SR_HWCAP_SAMPLERATE)) {
samplerate = *((uint64_t *) o->dev->driver->dev_info_get(
o->dev->driver_index, SR_DI_CUR_SAMPLERATE));
/* TODO: Error checks. */
} else {
samplerate = 0; /* TODO: Error or set some value? */
}
ctx->samplerate = samplerate;
if (sr_dev_has_hwcap(o->sdi, SR_HWCAP_SAMPLERATE)) {
o->sdi->driver->info_get(SR_DI_CUR_SAMPLERATE,
(const void **)&samplerate, o->sdi);
ctx->samplerate = *samplerate;
} else
ctx->samplerate = 0;
return 0; /* TODO: SR_OK? */
return SR_OK;
}
static int event(struct sr_output *o, int event_type, uint8_t **data_out,

View File

@ -52,7 +52,7 @@ static int init(struct sr_output *o)
struct sr_probe *probe;
GSList *l;
int num_probes;
uint64_t samplerate;
uint64_t *samplerate;
time_t t;
unsigned int i;
@ -61,13 +61,13 @@ static int init(struct sr_output *o)
return SR_ERR_ARG;
}
if (!o->dev) {
sr_err("csv out: %s: o->dev was NULL", __func__);
if (!o->sdi) {
sr_err("csv out: %s: o->sdi was NULL", __func__);
return SR_ERR_ARG;
}
if (!o->dev->driver) {
sr_err("csv out: %s: o->dev->driver was NULL", __func__);
if (!o->sdi->driver) {
sr_err("csv out: %s: o->sdi->driver was NULL", __func__);
return SR_ERR_ARG;
}
@ -79,8 +79,7 @@ static int init(struct sr_output *o)
o->internal = ctx;
/* Get the number of probes, their names, and the unitsize. */
/* TODO: Error handling. */
for (l = o->dev->probes; l; l = l->next) {
for (l = o->sdi->probes; l; l = l->next) {
probe = l->data;
if (!probe->enabled)
continue;
@ -89,19 +88,16 @@ static int init(struct sr_output *o)
ctx->probelist[ctx->num_enabled_probes] = 0;
ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
num_probes = g_slist_length(o->dev->probes);
num_probes = g_slist_length(o->sdi->probes);
if (sr_dev_has_hwcap(o->dev, SR_HWCAP_SAMPLERATE)) {
samplerate = *((uint64_t *) o->dev->driver->dev_info_get(
o->dev->driver_index, SR_DI_CUR_SAMPLERATE));
/* TODO: Error checks. */
} else {
samplerate = 0; /* TODO: Error or set some value? */
}
ctx->samplerate = samplerate;
if (sr_dev_has_hwcap(o->sdi, SR_HWCAP_SAMPLERATE)) {
o->sdi->driver->info_get(SR_DI_CUR_SAMPLERATE,
(const void **)&samplerate, o->sdi);
ctx->samplerate = *samplerate;
} else
ctx->samplerate = 0;
ctx->separator = ',';
ctx->header = g_string_sized_new(512);
t = time(NULL);
@ -119,7 +115,7 @@ static int init(struct sr_output *o)
g_string_append_printf(ctx->header, "%s, ", ctx->probelist[i]);
g_string_append_printf(ctx->header, "\n");
return 0; /* TODO: SR_OK? */
return SR_OK;
}
static int event(struct sr_output *o, int event_type, uint8_t **data_out,

View File

@ -38,10 +38,10 @@ static int init(struct sr_output *o)
if (!o)
return SR_ERR_ARG;
if (!o->dev)
if (!o->sdi)
return SR_ERR_ARG;
if (!o->dev->driver)
if (!o->sdi->driver)
return SR_ERR_ARG;
if (!(ctx = g_try_malloc0(sizeof(struct context))))
@ -51,7 +51,7 @@ static int init(struct sr_output *o)
/* Get the number of probes and their names. */
ctx->probelist = g_ptr_array_new();
for (l = o->dev->probes; l; l = l->next) {
for (l = o->sdi->probes; l; l = l->next) {
probe = l->data;
if (!probe || !probe->enabled)
continue;

View File

@ -54,7 +54,7 @@ static int init(struct sr_output *o)
struct context *ctx;
struct sr_probe *probe;
GSList *l;
uint64_t samplerate;
uint64_t *samplerate;
unsigned int i;
int b, num_probes;
char *c, *frequency_s;
@ -66,13 +66,13 @@ static int init(struct sr_output *o)
return SR_ERR_ARG;
}
if (!o->dev) {
sr_err("gnuplot out: %s: o->dev was NULL", __func__);
if (!o->sdi) {
sr_err("gnuplot out: %s: o->sdi was NULL", __func__);
return SR_ERR_ARG;
}
if (!o->dev->driver) {
sr_err("gnuplot out: %s: o->dev->driver was NULL", __func__);
if (!o->sdi->driver) {
sr_err("gnuplot out: %s: o->sdi->driver was NULL", __func__);
return SR_ERR_ARG;
}
@ -89,7 +89,7 @@ static int init(struct sr_output *o)
o->internal = ctx;
ctx->num_enabled_probes = 0;
for (l = o->dev->probes; l; l = l->next) {
for (l = o->sdi->probes; l; l = l->next) {
probe = l->data; /* TODO: Error checks. */
if (!probe->enabled)
continue;
@ -98,12 +98,12 @@ static int init(struct sr_output *o)
ctx->probelist[ctx->num_enabled_probes] = 0;
ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
num_probes = g_slist_length(o->dev->probes);
num_probes = g_slist_length(o->sdi->probes);
comment[0] = '\0';
if (sr_dev_has_hwcap(o->dev, SR_HWCAP_SAMPLERATE)) {
samplerate = *((uint64_t *) o->dev->driver->dev_info_get(
o->dev->driver_index, SR_DI_CUR_SAMPLERATE));
if (!(frequency_s = sr_samplerate_string(samplerate))) {
if (sr_dev_has_hwcap(o->sdi, SR_HWCAP_SAMPLERATE)) {
o->sdi->driver->info_get(SR_DI_CUR_SAMPLERATE,
(const void **)&samplerate, o->sdi);
if (!(frequency_s = sr_samplerate_string(*samplerate))) {
sr_err("gnuplot out: %s: sr_samplerate_string failed",
__func__);
g_free(ctx->header);
@ -122,7 +122,7 @@ static int init(struct sr_output *o)
sprintf(c, "# %d\t\t%s\n", i + 1, ctx->probelist[i]);
}
if (!(frequency_s = sr_period_string(samplerate))) {
if (!(frequency_s = sr_period_string(*samplerate))) {
sr_err("gnuplot out: %s: sr_period_string failed", __func__);
g_free(ctx->header);
g_free(ctx);

View File

@ -42,7 +42,7 @@ static int init(struct sr_output *o)
struct context *ctx;
struct sr_probe *probe;
GSList *l;
uint64_t samplerate;
uint64_t *samplerate, tmp;
int num_enabled_probes;
if (!(ctx = g_try_malloc(sizeof(struct context)))) {
@ -53,21 +53,23 @@ static int init(struct sr_output *o)
ctx->num_samples = 0;
num_enabled_probes = 0;
for (l = o->dev->probes; l; l = l->next) {
for (l = o->sdi->probes; l; l = l->next) {
probe = l->data;
if (probe->enabled)
num_enabled_probes++;
}
ctx->unitsize = (num_enabled_probes + 7) / 8;
if (o->dev->driver && sr_dev_has_hwcap(o->dev, SR_HWCAP_SAMPLERATE))
samplerate = *((uint64_t *) o->dev->driver->dev_info_get(
o->dev->driver_index, SR_DI_CUR_SAMPLERATE));
else
samplerate = 0;
if (o->sdi->driver && sr_dev_has_hwcap(o->sdi, SR_HWCAP_SAMPLERATE))
o->sdi->driver->info_get(SR_DI_CUR_SAMPLERATE,
(const void **)&samplerate, o->sdi);
else {
tmp = 0;
samplerate = &tmp;
}
ctx->header = g_string_sized_new(512);
g_string_append_printf(ctx->header, ";Rate: %"PRIu64"\n", samplerate);
g_string_append_printf(ctx->header, ";Rate: %"PRIu64"\n", *samplerate);
g_string_append_printf(ctx->header, ";Channels: %d\n", num_enabled_probes);
g_string_append_printf(ctx->header, ";EnabledChannels: -1\n");
g_string_append_printf(ctx->header, ";Compressed: true\n");

View File

@ -70,8 +70,8 @@ SR_PRIV int init(struct sr_output *o, int default_spl, enum outputmode mode)
struct context *ctx;
struct sr_probe *probe;
GSList *l;
uint64_t samplerate;
int num_probes;
uint64_t *samplerate;
int num_probes, ret;
char *samplerate_s;
if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
@ -82,7 +82,7 @@ SR_PRIV int init(struct sr_output *o, int default_spl, enum outputmode mode)
o->internal = ctx;
ctx->num_enabled_probes = 0;
for (l = o->dev->probes; l; l = l->next) {
for (l = o->sdi->probes; l; l = l->next) {
probe = l->data;
if (!probe->enabled)
continue;
@ -98,26 +98,29 @@ SR_PRIV int init(struct sr_output *o, int default_spl, enum outputmode mode)
if (o->param && o->param[0]) {
ctx->samples_per_line = strtoul(o->param, NULL, 10);
if (ctx->samples_per_line < 1)
return SR_ERR;
if (ctx->samples_per_line < 1) {
ret = SR_ERR;
goto err;
}
} else
ctx->samples_per_line = default_spl;
if (!(ctx->header = g_try_malloc0(512))) {
g_free(ctx);
sr_err("text out: %s: ctx->header malloc failed", __func__);
return SR_ERR_MALLOC;
ret = SR_ERR_MALLOC;
goto err;
}
snprintf(ctx->header, 511, "%s\n", PACKAGE_STRING);
num_probes = g_slist_length(o->dev->probes);
if (o->dev->driver || sr_dev_has_hwcap(o->dev, SR_HWCAP_SAMPLERATE)) {
samplerate = *((uint64_t *) o->dev->driver->dev_info_get(
o->dev->driver_index, SR_DI_CUR_SAMPLERATE));
if (!(samplerate_s = sr_samplerate_string(samplerate))) {
g_free(ctx->header);
g_free(ctx);
return SR_ERR;
num_probes = g_slist_length(o->sdi->probes);
if (o->sdi->driver || sr_dev_has_hwcap(o->sdi, SR_HWCAP_SAMPLERATE)) {
ret = o->sdi->driver->info_get(SR_DI_CUR_SAMPLERATE,
(const void **)&samplerate, o->sdi);
if (ret != SR_OK)
goto err;
if (!(samplerate_s = sr_samplerate_string(*samplerate))) {
ret = SR_ERR;
goto err;
}
snprintf(ctx->header + strlen(ctx->header),
511 - strlen(ctx->header),
@ -128,19 +131,23 @@ SR_PRIV int init(struct sr_output *o, int default_spl, enum outputmode mode)
ctx->linebuf_len = ctx->samples_per_line * 2 + 4;
if (!(ctx->linebuf = g_try_malloc0(num_probes * ctx->linebuf_len))) {
g_free(ctx->header);
g_free(ctx);
sr_err("text out: %s: ctx->linebuf malloc failed", __func__);
return SR_ERR_MALLOC;
}
if (!(ctx->linevalues = g_try_malloc0(num_probes))) {
g_free(ctx->header);
g_free(ctx);
sr_err("text out: %s: ctx->linevalues malloc failed", __func__);
return SR_ERR_MALLOC;
ret = SR_ERR_MALLOC;
goto err;
}
return SR_OK;
if (!(ctx->linevalues = g_try_malloc0(num_probes))) {
sr_err("text out: %s: ctx->linevalues malloc failed", __func__);
ret = SR_ERR_MALLOC;
}
err:
if (ret != SR_OK) {
g_free(ctx->header);
g_free(ctx);
}
return ret;
}
SR_PRIV int event(struct sr_output *o, int event_type, uint8_t **data_out,

View File

@ -45,6 +45,7 @@ static int init(struct sr_output *o)
struct context *ctx;
struct sr_probe *probe;
GSList *l;
uint64_t *samplerate;
int num_probes, i;
char *samplerate_s, *frequency_s, *timestamp;
time_t t;
@ -57,7 +58,7 @@ static int init(struct sr_output *o)
o->internal = ctx;
ctx->num_enabled_probes = 0;
for (l = o->dev->probes; l; l = l->next) {
for (l = o->sdi->probes; l; l = l->next) {
probe = l->data;
if (!probe->enabled)
continue;
@ -71,7 +72,7 @@ static int init(struct sr_output *o)
ctx->probelist[ctx->num_enabled_probes] = 0;
ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
ctx->header = g_string_sized_new(512);
num_probes = g_slist_length(o->dev->probes);
num_probes = g_slist_length(o->sdi->probes);
/* timestamp */
t = time(NULL);
@ -84,9 +85,10 @@ static int init(struct sr_output *o)
g_string_append_printf(ctx->header, "$version %s %s $end\n",
PACKAGE, PACKAGE_VERSION);
if (o->dev->driver && sr_dev_has_hwcap(o->dev, SR_HWCAP_SAMPLERATE)) {
ctx->samplerate = *((uint64_t *) o->dev->driver->dev_info_get(
o->dev->driver_index, SR_DI_CUR_SAMPLERATE));
if (o->sdi->driver && sr_dev_has_hwcap(o->sdi, SR_HWCAP_SAMPLERATE)) {
o->sdi->driver->info_get(SR_DI_CUR_SAMPLERATE,
(const void **)&samplerate, o->sdi);
ctx->samplerate = *samplerate;
if (!((samplerate_s = sr_samplerate_string(ctx->samplerate)))) {
g_string_free(ctx->header, TRUE);
g_free(ctx);