2011-01-15 17:18:09 +00:00
|
|
|
/*
|
2013-04-23 20:24:30 +00:00
|
|
|
* This file is part of the libsigrok project.
|
2011-01-15 17:18:09 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2011 Uwe Hermann <uwe@hermann-uwe.de>
|
2013-03-24 10:21:00 +00:00
|
|
|
* Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
|
2011-01-15 17:18:09 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2011-02-12 05:18:16 +00:00
|
|
|
* This implements version 1.3 of the output format for the OpenBench Logic
|
|
|
|
* Sniffer "Alternative" Java client. Details:
|
|
|
|
* https://github.com/jawi/ols/wiki/OLS-data-file-format
|
2011-01-15 17:18:09 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <glib.h>
|
2012-07-04 22:55:07 +00:00
|
|
|
#include "libsigrok.h"
|
|
|
|
#include "libsigrok-internal.h"
|
2011-01-15 17:18:09 +00:00
|
|
|
|
2013-12-23 03:38:35 +00:00
|
|
|
#define LOG_PREFIX "output/ols"
|
2012-11-11 08:36:21 +00:00
|
|
|
|
2011-01-15 17:18:09 +00:00
|
|
|
struct context {
|
2013-04-27 15:29:46 +00:00
|
|
|
uint64_t samplerate;
|
2011-02-01 05:13:32 +00:00
|
|
|
uint64_t num_samples;
|
2011-01-15 17:18:09 +00:00
|
|
|
};
|
|
|
|
|
2011-02-01 05:13:32 +00:00
|
|
|
static int init(struct sr_output *o)
|
|
|
|
{
|
|
|
|
struct context *ctx;
|
2011-01-15 17:18:09 +00:00
|
|
|
|
2011-04-16 12:17:51 +00:00
|
|
|
if (!(ctx = g_try_malloc(sizeof(struct context)))) {
|
2012-11-11 08:36:21 +00:00
|
|
|
sr_err("%s: ctx malloc failed", __func__);
|
2011-02-01 05:13:32 +00:00
|
|
|
return SR_ERR_MALLOC;
|
2011-04-16 12:17:51 +00:00
|
|
|
}
|
2011-02-01 05:13:32 +00:00
|
|
|
o->internal = ctx;
|
2011-02-12 05:18:16 +00:00
|
|
|
|
2013-04-27 15:29:46 +00:00
|
|
|
ctx->samplerate = 0;
|
2011-02-01 05:13:32 +00:00
|
|
|
ctx->num_samples = 0;
|
2013-04-27 12:24:47 +00:00
|
|
|
|
|
|
|
return SR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GString *gen_header(const struct sr_dev_inst *sdi, struct context *ctx)
|
|
|
|
{
|
2014-03-20 20:58:57 +00:00
|
|
|
struct sr_channel *probe;
|
2013-04-27 12:24:47 +00:00
|
|
|
GSList *l;
|
|
|
|
GString *s;
|
|
|
|
GVariant *gvar;
|
|
|
|
int num_enabled_probes;
|
|
|
|
|
2013-04-20 00:00:49 +00:00
|
|
|
if (!ctx->samplerate && sr_config_get(sdi->driver, sdi, NULL,
|
|
|
|
SR_CONF_SAMPLERATE, &gvar) == SR_OK) {
|
2013-04-27 12:24:47 +00:00
|
|
|
ctx->samplerate = g_variant_get_uint64(gvar);
|
|
|
|
g_variant_unref(gvar);
|
|
|
|
}
|
|
|
|
|
2011-02-01 05:13:32 +00:00
|
|
|
num_enabled_probes = 0;
|
2013-04-27 12:24:47 +00:00
|
|
|
for (l = sdi->probes; l; l = l->next) {
|
2011-02-01 05:13:32 +00:00
|
|
|
probe = l->data;
|
2013-12-29 09:57:38 +00:00
|
|
|
if (probe->type != SR_PROBE_LOGIC)
|
|
|
|
continue;
|
|
|
|
if (!probe->enabled)
|
|
|
|
continue;
|
|
|
|
num_enabled_probes++;
|
2011-01-15 17:18:09 +00:00
|
|
|
}
|
|
|
|
|
2013-04-27 12:24:47 +00:00
|
|
|
s = g_string_sized_new(512);
|
|
|
|
g_string_append_printf(s, ";Rate: %"PRIu64"\n", ctx->samplerate);
|
|
|
|
g_string_append_printf(s, ";Channels: %d\n", num_enabled_probes);
|
|
|
|
g_string_append_printf(s, ";EnabledChannels: -1\n");
|
|
|
|
g_string_append_printf(s, ";Compressed: true\n");
|
|
|
|
g_string_append_printf(s, ";CursorEnabled: false\n");
|
2011-02-12 05:18:16 +00:00
|
|
|
|
2013-04-27 12:24:47 +00:00
|
|
|
return s;
|
2011-01-15 17:18:09 +00:00
|
|
|
}
|
|
|
|
|
2013-04-27 15:29:46 +00:00
|
|
|
static int receive(struct sr_output *o, const struct sr_dev_inst *sdi,
|
|
|
|
const struct sr_datafeed_packet *packet, GString **out)
|
2011-01-15 17:18:09 +00:00
|
|
|
{
|
|
|
|
struct context *ctx;
|
2013-04-27 12:24:47 +00:00
|
|
|
const struct sr_datafeed_meta *meta;
|
|
|
|
const struct sr_datafeed_logic *logic;
|
|
|
|
const struct sr_config *src;
|
|
|
|
GSList *l;
|
|
|
|
unsigned int i, j;
|
|
|
|
uint8_t c;
|
2011-01-15 17:18:09 +00:00
|
|
|
|
2013-04-27 15:29:46 +00:00
|
|
|
*out = NULL;
|
2013-04-27 12:24:47 +00:00
|
|
|
if (!o || !o->sdi)
|
2013-04-27 15:29:46 +00:00
|
|
|
return SR_ERR_ARG;
|
2011-01-15 17:18:09 +00:00
|
|
|
ctx = o->internal;
|
2011-02-01 05:13:32 +00:00
|
|
|
|
2013-04-27 12:24:47 +00:00
|
|
|
switch (packet->type) {
|
2013-04-27 15:29:46 +00:00
|
|
|
case SR_DF_META:
|
2013-04-27 12:24:47 +00:00
|
|
|
meta = packet->payload;
|
|
|
|
for (l = meta->config; l; l = l->next) {
|
|
|
|
src = l->data;
|
|
|
|
if (src->key == SR_CONF_SAMPLERATE)
|
|
|
|
ctx->samplerate = g_variant_get_uint64(src->data);
|
|
|
|
}
|
2013-04-27 15:29:46 +00:00
|
|
|
break;
|
|
|
|
case SR_DF_LOGIC:
|
2013-04-27 12:24:47 +00:00
|
|
|
logic = packet->payload;
|
|
|
|
if (ctx->num_samples == 0) {
|
|
|
|
/* First logic packet in the feed. */
|
2013-04-27 15:29:46 +00:00
|
|
|
*out = gen_header(sdi, ctx);
|
2013-04-27 12:24:47 +00:00
|
|
|
} else
|
2013-04-27 15:29:46 +00:00
|
|
|
*out = g_string_sized_new(512);
|
2013-04-27 12:24:47 +00:00
|
|
|
for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
|
|
|
|
for (j = 0; j < logic->unitsize; j++) {
|
|
|
|
/* The OLS format wants the samples presented MSB first. */
|
|
|
|
c = *((uint8_t *)logic->data + i + logic->unitsize - 1 - j);
|
2013-04-27 15:29:46 +00:00
|
|
|
g_string_append_printf(*out, "%02x", c);
|
2013-04-27 12:24:47 +00:00
|
|
|
}
|
2013-04-27 15:29:46 +00:00
|
|
|
g_string_append_printf(*out, "@%"PRIu64"\n", ctx->num_samples++);
|
2013-04-27 12:24:47 +00:00
|
|
|
}
|
|
|
|
break;
|
2011-01-15 17:18:09 +00:00
|
|
|
}
|
|
|
|
|
2013-04-27 15:29:46 +00:00
|
|
|
return SR_OK;
|
2011-01-15 17:18:09 +00:00
|
|
|
}
|
|
|
|
|
2013-04-27 12:24:47 +00:00
|
|
|
static int cleanup(struct sr_output *o)
|
2011-01-15 17:18:09 +00:00
|
|
|
{
|
|
|
|
struct context *ctx;
|
2013-04-27 12:24:47 +00:00
|
|
|
|
|
|
|
if (!o || !o->sdi)
|
|
|
|
return SR_ERR_ARG;
|
2011-01-15 17:18:09 +00:00
|
|
|
|
|
|
|
ctx = o->internal;
|
2013-04-27 15:29:46 +00:00
|
|
|
g_free(ctx);
|
|
|
|
o->internal = NULL;
|
2011-01-15 17:18:09 +00:00
|
|
|
|
2011-01-29 15:23:12 +00:00
|
|
|
return SR_OK;
|
2011-01-15 17:18:09 +00:00
|
|
|
}
|
|
|
|
|
2012-02-04 09:56:51 +00:00
|
|
|
SR_PRIV struct sr_output_format output_ols = {
|
2011-04-06 19:51:36 +00:00
|
|
|
.id = "ols",
|
2011-04-06 17:42:49 +00:00
|
|
|
.description = "OpenBench Logic Sniffer",
|
|
|
|
.df_type = SR_DF_LOGIC,
|
|
|
|
.init = init,
|
2013-04-27 15:29:46 +00:00
|
|
|
.receive = receive,
|
2013-04-27 12:24:47 +00:00
|
|
|
.cleanup = cleanup
|
2011-01-15 17:18:09 +00:00
|
|
|
};
|