]> sigrok.org Git - libsigrok.git/blame_incremental - src/output/ols.c
binary helpers: add "invalid" enum item, improve readability
[libsigrok.git] / src / output / ols.c
... / ...
CommitLineData
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2011 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
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, see <http://www.gnu.org/licenses/>.
19 */
20
21/*
22 * This implements version 1.3 of the output format for the OpenBench Logic
23 * Sniffer "Alternative" Java client. Details:
24 * https://github.com/jawi/ols/wiki/OLS-data-file-format
25 */
26
27#include <config.h>
28#include <stdlib.h>
29#include <string.h>
30#include <glib.h>
31#include <libsigrok/libsigrok.h>
32#include "libsigrok-internal.h"
33
34#define LOG_PREFIX "output/ols"
35
36struct context {
37 uint64_t samplerate;
38 uint64_t num_samples;
39};
40
41static int init(struct sr_output *o, GHashTable *options)
42{
43 struct context *ctx;
44
45 (void)options;
46
47 ctx = g_malloc0(sizeof(struct context));
48 o->priv = ctx;
49 ctx->samplerate = 0;
50 ctx->num_samples = 0;
51
52 return SR_OK;
53}
54
55static GString *gen_header(const struct sr_dev_inst *sdi, struct context *ctx)
56{
57 struct sr_channel *ch;
58 GSList *l;
59 GString *s;
60 GVariant *gvar;
61 int num_enabled_channels;
62
63 if (!ctx->samplerate && sr_config_get(sdi->driver, sdi, NULL,
64 SR_CONF_SAMPLERATE, &gvar) == SR_OK) {
65 ctx->samplerate = g_variant_get_uint64(gvar);
66 g_variant_unref(gvar);
67 }
68
69 num_enabled_channels = 0;
70 for (l = sdi->channels; l; l = l->next) {
71 ch = l->data;
72 if (ch->type != SR_CHANNEL_LOGIC)
73 continue;
74 if (!ch->enabled)
75 continue;
76 num_enabled_channels++;
77 }
78
79 s = g_string_sized_new(512);
80 g_string_append_printf(s, ";Rate: %"PRIu64"\n", ctx->samplerate);
81 g_string_append_printf(s, ";Channels: %d\n", num_enabled_channels);
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");
85
86 return s;
87}
88
89static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet,
90 GString **out)
91{
92 struct context *ctx;
93 const struct sr_datafeed_meta *meta;
94 const struct sr_datafeed_logic *logic;
95 const struct sr_config *src;
96 GSList *l;
97 unsigned int i, j;
98 uint8_t c;
99
100 *out = NULL;
101 if (!o || !o->sdi)
102 return SR_ERR_ARG;
103 ctx = o->priv;
104
105 switch (packet->type) {
106 case SR_DF_META:
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 }
113 break;
114 case SR_DF_LOGIC:
115 logic = packet->payload;
116 if (ctx->num_samples == 0) {
117 /* First logic packet in the feed. */
118 *out = gen_header(o->sdi, ctx);
119 } else
120 *out = g_string_sized_new(512);
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);
125 g_string_append_printf(*out, "%02x", c);
126 }
127 g_string_append_printf(*out, "@%"PRIu64"\n", ctx->num_samples++);
128 }
129 break;
130 }
131
132 return SR_OK;
133}
134
135static int cleanup(struct sr_output *o)
136{
137 struct context *ctx;
138
139 if (!o || !o->sdi)
140 return SR_ERR_ARG;
141
142 ctx = o->priv;
143 g_free(ctx);
144 o->priv = NULL;
145
146 return SR_OK;
147}
148
149SR_PRIV struct sr_output_module output_ols = {
150 .id = "ols",
151 .name = "OLS",
152 .desc = "OpenBench Logic Sniffer data",
153 .exts = (const char*[]){"ols", NULL},
154 .flags = 0,
155 .options = NULL,
156 .init = init,
157 .receive = receive,
158 .cleanup = cleanup
159};