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