]> sigrok.org Git - libsigrok.git/blame - src/output/ols.c
asyc-ii: Rephrase "exponent" logic when parsing packets
[libsigrok.git] / src / output / ols.c
CommitLineData
ab224f7b 1/*
50985c20 2 * This file is part of the libsigrok project.
ab224f7b
UH
3 *
4 * Copyright (C) 2011 Uwe Hermann <uwe@hermann-uwe.de>
13d8e03c 5 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
ab224f7b
UH
6 *
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.
11 *
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.
16 *
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
20 */
21
22/*
305bde4d
BV
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
ab224f7b
UH
26 */
27
6ec6c43b 28#include <config.h>
ab224f7b
UH
29#include <stdlib.h>
30#include <string.h>
31#include <glib.h>
c1aae900 32#include <libsigrok/libsigrok.h>
45c59c8b 33#include "libsigrok-internal.h"
ab224f7b 34
3544f848 35#define LOG_PREFIX "output/ols"
a944a84b 36
ab224f7b 37struct context {
17f63de6 38 uint64_t samplerate;
6e738600 39 uint64_t num_samples;
ab224f7b
UH
40};
41
a755b0e1 42static int init(struct sr_output *o, GHashTable *options)
6e738600
BV
43{
44 struct context *ctx;
ab224f7b 45
a755b0e1
BV
46 (void)options;
47
91219afc 48 ctx = g_malloc0(sizeof(struct context));
d686c5ec 49 o->priv = ctx;
17f63de6 50 ctx->samplerate = 0;
6e738600 51 ctx->num_samples = 0;
3292535c
BV
52
53 return SR_OK;
54}
55
56static GString *gen_header(const struct sr_dev_inst *sdi, struct context *ctx)
57{
ba7dd8bb 58 struct sr_channel *ch;
3292535c
BV
59 GSList *l;
60 GString *s;
61 GVariant *gvar;
ba7dd8bb 62 int num_enabled_channels;
3292535c 63
8f996b89
ML
64 if (!ctx->samplerate && sr_config_get(sdi->driver, sdi, NULL,
65 SR_CONF_SAMPLERATE, &gvar) == SR_OK) {
3292535c
BV
66 ctx->samplerate = g_variant_get_uint64(gvar);
67 g_variant_unref(gvar);
68 }
69
ba7dd8bb
UH
70 num_enabled_channels = 0;
71 for (l = sdi->channels; l; l = l->next) {
72 ch = l->data;
3f239f08 73 if (ch->type != SR_CHANNEL_LOGIC)
3699a8a1 74 continue;
ba7dd8bb 75 if (!ch->enabled)
3699a8a1 76 continue;
ba7dd8bb 77 num_enabled_channels++;
ab224f7b
UH
78 }
79
3292535c
BV
80 s = g_string_sized_new(512);
81 g_string_append_printf(s, ";Rate: %"PRIu64"\n", ctx->samplerate);
ba7dd8bb 82 g_string_append_printf(s, ";Channels: %d\n", num_enabled_channels);
3292535c
BV
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");
305bde4d 86
3292535c 87 return s;
ab224f7b
UH
88}
89
a755b0e1 90static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet,
dba3e682 91 GString **out)
ab224f7b
UH
92{
93 struct context *ctx;
3292535c
BV
94 const struct sr_datafeed_meta *meta;
95 const struct sr_datafeed_logic *logic;
96 const struct sr_config *src;
97 GSList *l;
3292535c
BV
98 unsigned int i, j;
99 uint8_t c;
ab224f7b 100
17f63de6 101 *out = NULL;
3292535c 102 if (!o || !o->sdi)
17f63de6 103 return SR_ERR_ARG;
d686c5ec 104 ctx = o->priv;
6e738600 105
3292535c 106 switch (packet->type) {
17f63de6 107 case SR_DF_META:
3292535c
BV
108 meta = packet->payload;
109 for (l = meta->config; l; l = l->next) {
110 src = l->data;
111 if (src->key == SR_CONF_SAMPLERATE)
112 ctx->samplerate = g_variant_get_uint64(src->data);
113 }
17f63de6
BV
114 break;
115 case SR_DF_LOGIC:
3292535c
BV
116 logic = packet->payload;
117 if (ctx->num_samples == 0) {
118 /* First logic packet in the feed. */
dba3e682 119 *out = gen_header(o->sdi, ctx);
3292535c 120 } else
17f63de6 121 *out = g_string_sized_new(512);
3292535c
BV
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);
17f63de6 126 g_string_append_printf(*out, "%02x", c);
3292535c 127 }
17f63de6 128 g_string_append_printf(*out, "@%"PRIu64"\n", ctx->num_samples++);
3292535c
BV
129 }
130 break;
ab224f7b
UH
131 }
132
17f63de6 133 return SR_OK;
ab224f7b
UH
134}
135
3292535c 136static int cleanup(struct sr_output *o)
ab224f7b
UH
137{
138 struct context *ctx;
3292535c
BV
139
140 if (!o || !o->sdi)
141 return SR_ERR_ARG;
ab224f7b 142
d686c5ec 143 ctx = o->priv;
17f63de6 144 g_free(ctx);
d686c5ec 145 o->priv = NULL;
ab224f7b 146
e46b8fb1 147 return SR_OK;
ab224f7b
UH
148}
149
a755b0e1 150SR_PRIV struct sr_output_module output_ols = {
cdb3573c 151 .id = "ols",
a755b0e1
BV
152 .name = "OLS",
153 .desc = "OpenBench Logic Sniffer",
8a174d23 154 .exts = (const char*[]){"ols", NULL},
3cd4b381 155 .flags = 0,
a755b0e1 156 .options = NULL,
d494a4aa 157 .init = init,
17f63de6 158 .receive = receive,
3292535c 159 .cleanup = cleanup
ab224f7b 160};