]> sigrok.org Git - libsigrok.git/blame - src/output/hex.c
Build: Set local include directories in Makefile.am
[libsigrok.git] / src / output / hex.c
CommitLineData
8d3af2e8
BV
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2014 Bert Vermeulen <bert@biot.com>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <stdlib.h>
21#include <string.h>
22#include <glib.h>
c1aae900 23#include <libsigrok/libsigrok.h>
8d3af2e8
BV
24#include "libsigrok-internal.h"
25
26#define LOG_PREFIX "output/hex"
27
28#define DEFAULT_SAMPLES_PER_LINE 192
29
30struct context {
31 unsigned int num_enabled_channels;
dcc55fe9 32 int spl;
8d3af2e8
BV
33 int bit_cnt;
34 int spl_cnt;
35 int trigger;
7bcbbfae 36 uint64_t samplerate;
8d3af2e8
BV
37 int *channel_index;
38 char **channel_names;
39 char **line_values;
40 uint8_t *sample_buf;
7bcbbfae 41 gboolean header_done;
8d3af2e8 42 GString **lines;
8d3af2e8
BV
43};
44
a755b0e1 45static int init(struct sr_output *o, GHashTable *options)
8d3af2e8
BV
46{
47 struct context *ctx;
48 struct sr_channel *ch;
49 GSList *l;
8d3af2e8 50 unsigned int i, j;
8d3af2e8
BV
51
52 if (!o || !o->sdi)
53 return SR_ERR_ARG;
dba3e682 54
8d3af2e8 55 ctx = g_malloc0(sizeof(struct context));
d686c5ec 56 o->priv = ctx;
8d3af2e8 57 ctx->trigger = -1;
dcc55fe9 58 ctx->spl = g_variant_get_uint32(g_hash_table_lookup(options, "width"));
8d3af2e8
BV
59
60 for (l = o->sdi->channels; l; l = l->next) {
61 ch = l->data;
62 if (ch->type != SR_CHANNEL_LOGIC)
63 continue;
64 if (!ch->enabled)
65 continue;
66 ctx->num_enabled_channels++;
67 }
68 ctx->channel_index = g_malloc(sizeof(int) * ctx->num_enabled_channels);
69 ctx->channel_names = g_malloc(sizeof(char *) * ctx->num_enabled_channels);
70 ctx->lines = g_malloc(sizeof(GString *) * ctx->num_enabled_channels);
71 ctx->sample_buf = g_malloc(ctx->num_enabled_channels);
72
73 j = 0;
74 for (i = 0, l = o->sdi->channels; l; l = l->next, i++) {
75 ch = l->data;
76 if (ch->type != SR_CHANNEL_LOGIC)
77 continue;
78 if (!ch->enabled)
79 continue;
80 ctx->channel_index[j] = ch->index;
81 ctx->channel_names[j] = ch->name;
82 ctx->lines[j] = g_string_sized_new(80);
83 ctx->sample_buf[j] = 0;
84 g_string_printf(ctx->lines[j], "%s:", ch->name);
85 j++;
86 }
87
7bcbbfae
BV
88 return SR_OK;
89}
90
a755b0e1 91static GString *gen_header(const struct sr_output *o)
7bcbbfae
BV
92{
93 struct context *ctx;
94 GVariant *gvar;
95 GString *header;
96 int num_channels;
97 char *samplerate_s;
98
d686c5ec 99 ctx = o->priv;
7bcbbfae
BV
100 if (ctx->samplerate == 0) {
101 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
102 &gvar) == SR_OK) {
103 ctx->samplerate = g_variant_get_uint64(gvar);
104 g_variant_unref(gvar);
105 }
106 }
107
108 header = g_string_sized_new(512);
109 g_string_printf(header, "%s\n", PACKAGE_STRING);
8d3af2e8 110 num_channels = g_slist_length(o->sdi->channels);
7bcbbfae
BV
111 g_string_append_printf(header, "Acquisition with %d/%d channels",
112 ctx->num_enabled_channels, num_channels);
113 if (ctx->samplerate != 0) {
114 samplerate_s = sr_samplerate_string(ctx->samplerate);
115 g_string_append_printf(header, " at %s", samplerate_s);
8d3af2e8 116 g_free(samplerate_s);
8d3af2e8 117 }
7bcbbfae 118 g_string_append_printf(header, "\n");
8d3af2e8 119
7bcbbfae 120 return header;
8d3af2e8
BV
121}
122
a755b0e1 123static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet,
dba3e682 124 GString **out)
8d3af2e8 125{
7bcbbfae 126 const struct sr_datafeed_meta *meta;
8d3af2e8 127 const struct sr_datafeed_logic *logic;
7bcbbfae
BV
128 const struct sr_config *src;
129 GSList *l;
8d3af2e8
BV
130 struct context *ctx;
131 int idx, pos, offset;
132 uint64_t i, j;
133 gchar *p;
134
8d3af2e8
BV
135 *out = NULL;
136 if (!o || !o->sdi)
137 return SR_ERR_ARG;
d686c5ec 138 if (!(ctx = o->priv))
8d3af2e8
BV
139 return SR_ERR_ARG;
140
141 switch (packet->type) {
7bcbbfae
BV
142 case SR_DF_META:
143 meta = packet->payload;
144 for (l = meta->config; l; l = l->next) {
145 src = l->data;
146 if (src->key != SR_CONF_SAMPLERATE)
147 continue;
148 ctx->samplerate = g_variant_get_uint64(src->data);
149 }
150 break;
8d3af2e8
BV
151 case SR_DF_TRIGGER:
152 ctx->trigger = ctx->spl_cnt;
153 break;
154 case SR_DF_LOGIC:
7bcbbfae
BV
155 if (!ctx->header_done) {
156 *out = gen_header(o);
157 ctx->header_done = TRUE;
8d3af2e8
BV
158 } else
159 *out = g_string_sized_new(512);
160
161 logic = packet->payload;
162 for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
163 ctx->spl_cnt++;
164 pos = ctx->spl_cnt & 7;
165 for (j = 0; j < ctx->num_enabled_channels; j++) {
166 idx = ctx->channel_index[j];
167 p = logic->data + i + idx / 8;
168 ctx->sample_buf[j] <<= 1;
169 if (*p & (1 << (idx % 8)))
170 ctx->sample_buf[j] |= 1;
171 if (ctx->spl_cnt && pos == 0) {
172 /* Buffered a byte's worth, output hex. */
173 g_string_append_printf(ctx->lines[j], "%.2x ",
174 ctx->sample_buf[j]);
175 ctx->sample_buf[j] = 0;
176 }
177
dcc55fe9 178 if (ctx->spl_cnt == ctx->spl) {
8d3af2e8
BV
179 /* Flush line buffers. */
180 g_string_append_len(*out, ctx->lines[j]->str, ctx->lines[j]->len);
181 g_string_append_c(*out, '\n');
182 if (j == ctx->num_enabled_channels - 1 && ctx->trigger > -1) {
183 offset = ctx->trigger + ctx->trigger / 8;
184 g_string_append_printf(*out, "T:%*s^ %d\n", offset, "", ctx->trigger);
185 ctx->trigger = -1;
186 }
187 g_string_printf(ctx->lines[j], "%s:", ctx->channel_names[j]);
188 }
189 }
dcc55fe9 190 if (ctx->spl_cnt == ctx->spl)
8d3af2e8
BV
191 /* Line buffers were already flushed. */
192 ctx->spl_cnt = 0;
193 }
194 break;
195 case SR_DF_END:
196 if (ctx->spl_cnt) {
197 /* Line buffers need flushing. */
198 *out = g_string_sized_new(512);
199 for (i = 0; i < ctx->num_enabled_channels; i++) {
200 if (ctx->spl_cnt & 7)
201 g_string_append_printf(ctx->lines[i], "%.2x ",
202 ctx->sample_buf[i] << (8 - (ctx->spl_cnt & 7)));
203 g_string_append_len(*out, ctx->lines[i]->str, ctx->lines[i]->len);
204 g_string_append_c(*out, '\n');
205 }
206 }
207 break;
208 }
209
210 return SR_OK;
211}
212
213static int cleanup(struct sr_output *o)
214{
215 struct context *ctx;
216 unsigned int i;
217
218 if (!o)
219 return SR_ERR_ARG;
220
d686c5ec 221 if (!(ctx = o->priv))
8d3af2e8
BV
222 return SR_OK;
223
8d3af2e8
BV
224 g_free(ctx->channel_index);
225 g_free(ctx->sample_buf);
226 g_free(ctx->channel_names);
227 for (i = 0; i < ctx->num_enabled_channels; i++)
228 g_string_free(ctx->lines[i], TRUE);
229 g_free(ctx->lines);
8d3af2e8 230 g_free(ctx);
d686c5ec 231 o->priv = NULL;
8d3af2e8
BV
232
233 return SR_OK;
234}
235
a755b0e1
BV
236static struct sr_option options[] = {
237 { "width", "Width", "Number of samples per line", NULL, NULL },
1685c276 238 ALL_ZERO
a755b0e1
BV
239};
240
af7d656d 241static const struct sr_option *get_options(void)
a755b0e1 242{
950043c3
BV
243 if (!options[0].def) {
244 options[0].def = g_variant_new_uint32(DEFAULT_SAMPLES_PER_LINE);
245 g_variant_ref_sink(options[0].def);
246 }
a755b0e1
BV
247
248 return options;
249}
250
251SR_PRIV struct sr_output_module output_hex = {
8d3af2e8 252 .id = "hex",
a755b0e1
BV
253 .name = "Hexadecimal",
254 .desc = "Hexadecimal digits",
8a174d23 255 .exts = (const char*[]){"txt", NULL},
3cd4b381 256 .flags = 0,
a755b0e1 257 .options = get_options,
8d3af2e8
BV
258 .init = init,
259 .receive = receive,
260 .cleanup = cleanup,
261};