2 * This file is part of the libsigrok project.
4 * Copyright (C) 2011 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 * This implements version 1.3 of the output format for the OpenBench Logic
24 * Sniffer "Alternative" Java client. Details:
25 * https://github.com/jawi/ols/wiki/OLS-data-file-format
32 #include <libsigrok/libsigrok.h>
33 #include "libsigrok-internal.h"
35 #define LOG_PREFIX "output/ols"
42 static int init(struct sr_output *o, GHashTable *options)
48 ctx = g_malloc0(sizeof(struct context));
56 static GString *gen_header(const struct sr_dev_inst *sdi, struct context *ctx)
58 struct sr_channel *ch;
62 int num_enabled_channels;
64 if (!ctx->samplerate && sr_config_get(sdi->driver, sdi, NULL,
65 SR_CONF_SAMPLERATE, &gvar) == SR_OK) {
66 ctx->samplerate = g_variant_get_uint64(gvar);
67 g_variant_unref(gvar);
70 num_enabled_channels = 0;
71 for (l = sdi->channels; l; l = l->next) {
73 if (ch->type != SR_CHANNEL_LOGIC)
77 num_enabled_channels++;
80 s = g_string_sized_new(512);
81 g_string_append_printf(s, ";Rate: %"PRIu64"\n", ctx->samplerate);
82 g_string_append_printf(s, ";Channels: %d\n", num_enabled_channels);
83 g_string_append_printf(s, ";EnabledChannels: -1\n");
84 g_string_append_printf(s, ";Compressed: true\n");
85 g_string_append_printf(s, ";CursorEnabled: false\n");
90 static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet,
94 const struct sr_datafeed_meta *meta;
95 const struct sr_datafeed_logic *logic;
96 const struct sr_config *src;
106 switch (packet->type) {
108 meta = packet->payload;
109 for (l = meta->config; l; l = l->next) {
111 if (src->key == SR_CONF_SAMPLERATE)
112 ctx->samplerate = g_variant_get_uint64(src->data);
116 logic = packet->payload;
117 if (ctx->num_samples == 0) {
118 /* First logic packet in the feed. */
119 *out = gen_header(o->sdi, ctx);
121 *out = g_string_sized_new(512);
122 for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
123 for (j = 0; j < logic->unitsize; j++) {
124 /* The OLS format wants the samples presented MSB first. */
125 c = *((uint8_t *)logic->data + i + logic->unitsize - 1 - j);
126 g_string_append_printf(*out, "%02x", c);
128 g_string_append_printf(*out, "@%"PRIu64"\n", ctx->num_samples++);
136 static int cleanup(struct sr_output *o)
150 SR_PRIV struct sr_output_module output_ols = {
153 .desc = "OpenBench Logic Sniffer",
154 .exts = (const char*[]){"ols", NULL},