]> sigrok.org Git - libsigrok.git/blame - src/output/ols.c
output: Rename instance private storage pointer to priv.
[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
28#include <stdlib.h>
29#include <string.h>
30#include <glib.h>
45c59c8b
BV
31#include "libsigrok.h"
32#include "libsigrok-internal.h"
ab224f7b 33
3544f848 34#define LOG_PREFIX "output/ols"
a944a84b 35
ab224f7b 36struct context {
17f63de6 37 uint64_t samplerate;
6e738600 38 uint64_t num_samples;
ab224f7b
UH
39};
40
a755b0e1 41static int init(struct sr_output *o, GHashTable *options)
6e738600
BV
42{
43 struct context *ctx;
ab224f7b 44
a755b0e1
BV
45 (void)options;
46
b53738ba 47 if (!(ctx = g_try_malloc(sizeof(struct context)))) {
a944a84b 48 sr_err("%s: ctx malloc failed", __func__);
6e738600 49 return SR_ERR_MALLOC;
b53738ba 50 }
d686c5ec 51 o->priv = ctx;
305bde4d 52
17f63de6 53 ctx->samplerate = 0;
6e738600 54 ctx->num_samples = 0;
3292535c
BV
55
56 return SR_OK;
57}
58
59static GString *gen_header(const struct sr_dev_inst *sdi, struct context *ctx)
60{
ba7dd8bb 61 struct sr_channel *ch;
3292535c
BV
62 GSList *l;
63 GString *s;
64 GVariant *gvar;
ba7dd8bb 65 int num_enabled_channels;
3292535c 66
8f996b89
ML
67 if (!ctx->samplerate && sr_config_get(sdi->driver, sdi, NULL,
68 SR_CONF_SAMPLERATE, &gvar) == SR_OK) {
3292535c
BV
69 ctx->samplerate = g_variant_get_uint64(gvar);
70 g_variant_unref(gvar);
71 }
72
ba7dd8bb
UH
73 num_enabled_channels = 0;
74 for (l = sdi->channels; l; l = l->next) {
75 ch = l->data;
3f239f08 76 if (ch->type != SR_CHANNEL_LOGIC)
3699a8a1 77 continue;
ba7dd8bb 78 if (!ch->enabled)
3699a8a1 79 continue;
ba7dd8bb 80 num_enabled_channels++;
ab224f7b
UH
81 }
82
3292535c
BV
83 s = g_string_sized_new(512);
84 g_string_append_printf(s, ";Rate: %"PRIu64"\n", ctx->samplerate);
ba7dd8bb 85 g_string_append_printf(s, ";Channels: %d\n", num_enabled_channels);
3292535c
BV
86 g_string_append_printf(s, ";EnabledChannels: -1\n");
87 g_string_append_printf(s, ";Compressed: true\n");
88 g_string_append_printf(s, ";CursorEnabled: false\n");
305bde4d 89
3292535c 90 return s;
ab224f7b
UH
91}
92
a755b0e1 93static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet,
dba3e682 94 GString **out)
ab224f7b
UH
95{
96 struct context *ctx;
3292535c
BV
97 const struct sr_datafeed_meta *meta;
98 const struct sr_datafeed_logic *logic;
99 const struct sr_config *src;
100 GSList *l;
3292535c
BV
101 unsigned int i, j;
102 uint8_t c;
ab224f7b 103
17f63de6 104 *out = NULL;
3292535c 105 if (!o || !o->sdi)
17f63de6 106 return SR_ERR_ARG;
d686c5ec 107 ctx = o->priv;
6e738600 108
3292535c 109 switch (packet->type) {
17f63de6 110 case SR_DF_META:
3292535c
BV
111 meta = packet->payload;
112 for (l = meta->config; l; l = l->next) {
113 src = l->data;
114 if (src->key == SR_CONF_SAMPLERATE)
115 ctx->samplerate = g_variant_get_uint64(src->data);
116 }
17f63de6
BV
117 break;
118 case SR_DF_LOGIC:
3292535c
BV
119 logic = packet->payload;
120 if (ctx->num_samples == 0) {
121 /* First logic packet in the feed. */
dba3e682 122 *out = gen_header(o->sdi, ctx);
3292535c 123 } else
17f63de6 124 *out = g_string_sized_new(512);
3292535c
BV
125 for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
126 for (j = 0; j < logic->unitsize; j++) {
127 /* The OLS format wants the samples presented MSB first. */
128 c = *((uint8_t *)logic->data + i + logic->unitsize - 1 - j);
17f63de6 129 g_string_append_printf(*out, "%02x", c);
3292535c 130 }
17f63de6 131 g_string_append_printf(*out, "@%"PRIu64"\n", ctx->num_samples++);
3292535c
BV
132 }
133 break;
ab224f7b
UH
134 }
135
17f63de6 136 return SR_OK;
ab224f7b
UH
137}
138
3292535c 139static int cleanup(struct sr_output *o)
ab224f7b
UH
140{
141 struct context *ctx;
3292535c
BV
142
143 if (!o || !o->sdi)
144 return SR_ERR_ARG;
ab224f7b 145
d686c5ec 146 ctx = o->priv;
17f63de6 147 g_free(ctx);
d686c5ec 148 o->priv = NULL;
ab224f7b 149
e46b8fb1 150 return SR_OK;
ab224f7b
UH
151}
152
a755b0e1 153SR_PRIV struct sr_output_module output_ols = {
cdb3573c 154 .id = "ols",
a755b0e1
BV
155 .name = "OLS",
156 .desc = "OpenBench Logic Sniffer",
157 .options = NULL,
d494a4aa 158 .init = init,
17f63de6 159 .receive = receive,
3292535c 160 .cleanup = cleanup
ab224f7b 161};