]> sigrok.org Git - libsigrok.git/blame - output/output_text.c
hwplugin.c: Add missing config.h #include.
[libsigrok.git] / output / output_text.c
CommitLineData
a1bb33af
UH
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2010 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 <stdio.h>
22#include <string.h>
23#include <glib.h>
25e7d9b1 24#include <sigrok.h>
d2b36a10 25#include "config.h"
a1bb33af 26
02076d69
UH
27#define DEFAULT_BPL_BITS 64
28#define DEFAULT_BPL_HEX 256
a1bb33af
UH
29
30struct context {
afc8e4de 31 unsigned int num_enabled_probes;
a1bb33af 32 int samples_per_line;
afc8e4de 33 unsigned int unitsize;
a1bb33af
UH
34 int line_offset;
35 int linebuf_len;
36 char *probelist[65];
37 char *linebuf;
38 int spl_cnt;
39 uint8_t *linevalues;
40 char *header;
5045c217 41 int mark_trigger;
a1bb33af
UH
42};
43
afc8e4de 44static void flush_linebufs(struct context *ctx, char *outbuf)
a1bb33af
UH
45{
46 static int max_probename_len = 0;
47 int len, i;
48
99c1fc59 49 if (ctx->linebuf[0] == 0)
a1bb33af
UH
50 return;
51
99c1fc59
UH
52 if (max_probename_len == 0) {
53 /* First time through... */
54 for (i = 0; ctx->probelist[i]; i++) {
a1bb33af 55 len = strlen(ctx->probelist[i]);
99c1fc59 56 if (len > max_probename_len)
a1bb33af
UH
57 max_probename_len = len;
58 }
59 }
60
99c1fc59
UH
61 for (i = 0; ctx->probelist[i]; i++) {
62 sprintf(outbuf + strlen(outbuf), "%*s:%s\n", max_probename_len,
63 ctx->probelist[i], ctx->linebuf + i * ctx->linebuf_len);
a1bb33af 64 }
5045c217 65
e734b81a 66 /* Mark trigger with a ^ character. */
5045c217
HE
67 if (ctx->mark_trigger != -1)
68 sprintf(outbuf + strlen(outbuf), "T:%*s^\n",
69 ctx->mark_trigger + (ctx->mark_trigger / 8), "");
70
a1bb33af 71 memset(ctx->linebuf, 0, i * ctx->linebuf_len);
a1bb33af
UH
72}
73
5a8fda15 74static int init(struct output *o, int default_spl)
a1bb33af
UH
75{
76 struct context *ctx;
77 struct probe *probe;
78 GSList *l;
79 uint64_t samplerate;
80 int num_probes;
25e7d9b1 81 char *samplerate_s;
a1bb33af 82
e734b81a
UH
83 if (!(ctx = calloc(1, sizeof(struct context))))
84 return SIGROK_ERR_MALLOC;
85
a1bb33af
UH
86 o->internal = ctx;
87 ctx->num_enabled_probes = 0;
99c1fc59
UH
88
89 for (l = o->device->probes; l; l = l->next) {
a1bb33af 90 probe = l->data;
99c1fc59 91 if (probe->enabled)
a1bb33af
UH
92 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
93 }
99c1fc59 94
a1bb33af
UH
95 ctx->probelist[ctx->num_enabled_probes] = 0;
96 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
97 ctx->line_offset = 0;
98 ctx->spl_cnt = 0;
5045c217 99 ctx->mark_trigger = -1;
99c1fc59
UH
100
101 if (o->param && o->param[0])
a1bb33af
UH
102 ctx->samples_per_line = strtoul(o->param, NULL, 10);
103 else
104 ctx->samples_per_line = default_spl;
105
e734b81a
UH
106 if (!(ctx->header = malloc(512))) {
107 free(ctx);
108 return SIGROK_ERR_MALLOC;
109 }
110
d2b36a10 111 snprintf(ctx->header, 511, "%s\n", PACKAGE_STRING);
7aae7462 112 if (o->device->plugin) {
7aae7462
BV
113 num_probes = g_slist_length(o->device->probes);
114 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
115 o->device->plugin_index, DI_CUR_SAMPLERATE));
e734b81a
UH
116 if (!(samplerate_s = sigrok_samplerate_string(samplerate))) {
117 free(ctx->header);
118 free(ctx);
d2b36a10 119 return SIGROK_ERR;
e734b81a
UH
120 }
121 snprintf(ctx->header + strlen(ctx->header),
122 511 - strlen(ctx->header),
123 "Acquisition with %d/%d probes at %s\n",
124 ctx->num_enabled_probes, num_probes, samplerate_s);
7aae7462 125 free(samplerate_s);
7aae7462 126 }
a1bb33af
UH
127
128 ctx->linebuf_len = ctx->samples_per_line * 2;
e734b81a
UH
129 if (!(ctx->linebuf = calloc(1, num_probes * ctx->linebuf_len))) {
130 free(ctx->header);
131 free(ctx);
132 return SIGROK_ERR_MALLOC;
133 }
134 if (!(ctx->linevalues = calloc(1, num_probes))) {
135 free(ctx->header);
136 free(ctx);
137 return SIGROK_ERR_MALLOC;
138 }
a1bb33af 139
e734b81a 140 return SIGROK_OK;
a1bb33af
UH
141}
142
99c1fc59
UH
143static int event(struct output *o, int event_type, char **data_out,
144 uint64_t *length_out)
a1bb33af
UH
145{
146 struct context *ctx;
147 int outsize;
148 char *outbuf;
149
150 ctx = o->internal;
99c1fc59 151 switch (event_type) {
a1bb33af 152 case DF_TRIGGER:
5045c217 153 ctx->mark_trigger = ctx->spl_cnt;
a1bb33af
UH
154 break;
155 case DF_END:
99c1fc59
UH
156 outsize = ctx->num_enabled_probes
157 * (ctx->samples_per_line + 20) + 512;
e734b81a
UH
158 if (!(outbuf = calloc(1, outsize)))
159 return SIGROK_ERR_MALLOC;
afc8e4de 160 flush_linebufs(ctx, outbuf);
a1bb33af
UH
161 *data_out = outbuf;
162 *length_out = strlen(outbuf);
163 free(o->internal);
164 o->internal = NULL;
165 break;
166 }
167
168 return SIGROK_OK;
169}
170
02076d69 171static int init_bits(struct output *o)
a1bb33af 172{
02076d69 173 return init(o, DEFAULT_BPL_BITS);
a1bb33af
UH
174}
175
99c1fc59
UH
176static int data_bits(struct output *o, char *data_in, uint64_t length_in,
177 char **data_out, uint64_t *length_out)
a1bb33af
UH
178{
179 struct context *ctx;
afc8e4de 180 unsigned int outsize, offset, p;
a1bb33af
UH
181 uint64_t sample;
182 char *outbuf;
183
184 ctx = o->internal;
99c1fc59
UH
185 outsize = length_in / ctx->unitsize * ctx->num_enabled_probes *
186 ctx->samples_per_line + 512;
e734b81a
UH
187
188 if (!(outbuf = calloc(1, outsize + 1)))
189 return SIGROK_ERR_MALLOC;
190
191 outbuf[0] = '\0';
99c1fc59
UH
192 if (ctx->header) {
193 /* The header is still here, this must be the first packet. */
a1bb33af
UH
194 strncpy(outbuf, ctx->header, outsize);
195 free(ctx->header);
196 ctx->header = NULL;
e734b81a 197 }
a1bb33af 198
99c1fc59
UH
199 if (length_in >= ctx->unitsize) {
200 for (offset = 0; offset <= length_in - ctx->unitsize;
201 offset += ctx->unitsize) {
a1bb33af 202 memcpy(&sample, data_in + offset, ctx->unitsize);
99c1fc59
UH
203 for (p = 0; p < ctx->num_enabled_probes; p++) {
204 if (sample & ((uint64_t) 1 << p))
205 ctx->linebuf[p * ctx->linebuf_len +
206 ctx->line_offset] = '1';
a1bb33af 207 else
99c1fc59
UH
208 ctx->linebuf[p * ctx->linebuf_len +
209 ctx->line_offset] = '0';
a1bb33af
UH
210 }
211 ctx->line_offset++;
212 ctx->spl_cnt++;
213
99c1fc59
UH
214 /* Add a space every 8th bit. */
215 if ((ctx->spl_cnt & 7) == 0) {
216 for (p = 0; p < ctx->num_enabled_probes; p++)
217 ctx->linebuf[p * ctx->linebuf_len +
218 ctx->line_offset] = ' ';
a1bb33af
UH
219 ctx->line_offset++;
220 }
221
99c1fc59
UH
222 /* End of line. */
223 if (ctx->spl_cnt >= ctx->samples_per_line) {
afc8e4de 224 flush_linebufs(ctx, outbuf);
a1bb33af 225 ctx->line_offset = ctx->spl_cnt = 0;
5045c217 226 ctx->mark_trigger = -1;
a1bb33af
UH
227 }
228 }
e734b81a 229 } else {
99c1fc59 230 g_message("short buffer (length_in=%" PRIu64 ")", length_in);
e734b81a 231 }
a1bb33af
UH
232
233 *data_out = outbuf;
234 *length_out = strlen(outbuf);
235
236 return SIGROK_OK;
237}
238
5a8fda15 239static int init_hex(struct output *o)
a1bb33af 240{
02076d69 241 return init(o, DEFAULT_BPL_BITS);
a1bb33af
UH
242}
243
99c1fc59
UH
244static int data_hex(struct output *o, char *data_in, uint64_t length_in,
245 char **data_out, uint64_t *length_out)
a1bb33af
UH
246{
247 struct context *ctx;
afc8e4de 248 unsigned int outsize, offset, p;
a1bb33af
UH
249 uint64_t sample;
250 char *outbuf;
251
252 ctx = o->internal;
99c1fc59
UH
253 outsize = length_in / ctx->unitsize * ctx->num_enabled_probes *
254 ctx->samples_per_line + 512;
e734b81a
UH
255
256 if (!(outbuf = calloc(1, outsize + 1)))
257 return SIGROK_ERR_MALLOC;
258
259 outbuf[0] = '\0';
99c1fc59
UH
260 if (ctx->header) {
261 /* The header is still here, this must be the first packet. */
a1bb33af
UH
262 strncpy(outbuf, ctx->header, outsize);
263 free(ctx->header);
264 ctx->header = NULL;
e734b81a 265 }
a1bb33af
UH
266
267 ctx->line_offset = 0;
99c1fc59
UH
268 for (offset = 0; offset <= length_in - ctx->unitsize;
269 offset += ctx->unitsize) {
a1bb33af 270 memcpy(&sample, data_in + offset, ctx->unitsize);
99c1fc59 271 for (p = 0; p < ctx->num_enabled_probes; p++) {
a1bb33af 272 ctx->linevalues[p] <<= 1;
99c1fc59
UH
273 if (sample & ((uint64_t) 1 << p))
274 ctx->linevalues[p] |= 1;
275 sprintf(ctx->linebuf + (p * ctx->linebuf_len) +
276 ctx->line_offset, "%.2x", ctx->linevalues[p]);
a1bb33af
UH
277 }
278 ctx->spl_cnt++;
279
99c1fc59
UH
280 /* Add a space after every complete hex byte. */
281 if ((ctx->spl_cnt & 7) == 0) {
282 for (p = 0; p < ctx->num_enabled_probes; p++)
283 ctx->linebuf[p * ctx->linebuf_len +
284 ctx->line_offset + 2] = ' ';
a1bb33af
UH
285 ctx->line_offset += 3;
286 }
287
99c1fc59
UH
288 /* End of line. */
289 if (ctx->spl_cnt >= ctx->samples_per_line) {
afc8e4de 290 flush_linebufs(ctx, outbuf);
a1bb33af
UH
291 ctx->line_offset = ctx->spl_cnt = 0;
292 }
293 }
294
295 *data_out = outbuf;
296 *length_out = strlen(outbuf);
297
298 return SIGROK_OK;
299}
300
02076d69 301struct output_format output_text_bits = {
1c5b9d30
UH
302 "bits",
303 "Text (bits)",
02076d69
UH
304 init_bits,
305 data_bits,
99c1fc59 306 event,
a1bb33af
UH
307};
308
a1bb33af
UH
309struct output_format output_text_hex = {
310 "hex",
311 "Text (hexadecimal)",
312 init_hex,
313 data_hex,
99c1fc59 314 event,
a1bb33af 315};