]> sigrok.org Git - libsigrok.git/blame - output/output_text.c
output_text: always print sigrok version
[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
HE
65
66 /* Mark trigger with ^ */
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
UH
82
83 ctx = malloc(sizeof(struct context));
84 o->internal = ctx;
85 ctx->num_enabled_probes = 0;
99c1fc59
UH
86
87 for (l = o->device->probes; l; l = l->next) {
a1bb33af 88 probe = l->data;
99c1fc59 89 if (probe->enabled)
a1bb33af
UH
90 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
91 }
99c1fc59 92
a1bb33af
UH
93 ctx->probelist[ctx->num_enabled_probes] = 0;
94 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
95 ctx->line_offset = 0;
96 ctx->spl_cnt = 0;
5045c217 97 ctx->mark_trigger = -1;
99c1fc59
UH
98
99 if (o->param && o->param[0])
a1bb33af
UH
100 ctx->samples_per_line = strtoul(o->param, NULL, 10);
101 else
102 ctx->samples_per_line = default_spl;
103
d2b36a10
BV
104 ctx->header = malloc(512);
105 snprintf(ctx->header, 511, "%s\n", PACKAGE_STRING);
7aae7462 106 if (o->device->plugin) {
7aae7462
BV
107 num_probes = g_slist_length(o->device->probes);
108 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
109 o->device->plugin_index, DI_CUR_SAMPLERATE));
7aae7462 110 if ((samplerate_s = sigrok_samplerate_string(samplerate)) == NULL)
d2b36a10
BV
111 return SIGROK_ERR;
112 snprintf(ctx->header + strlen(ctx->header), 511 - strlen(ctx->header),
113 "Acquisition with %d/%d probes at %s\n", ctx->num_enabled_probes,
114 num_probes, samplerate_s);
7aae7462 115 free(samplerate_s);
7aae7462 116 }
a1bb33af
UH
117
118 ctx->linebuf_len = ctx->samples_per_line * 2;
119 ctx->linebuf = calloc(1, num_probes * ctx->linebuf_len);
120 ctx->linevalues = calloc(1, num_probes);
121
5a8fda15 122 return 0;
a1bb33af
UH
123}
124
99c1fc59
UH
125static int event(struct output *o, int event_type, char **data_out,
126 uint64_t *length_out)
a1bb33af
UH
127{
128 struct context *ctx;
129 int outsize;
130 char *outbuf;
131
132 ctx = o->internal;
99c1fc59 133 switch (event_type) {
a1bb33af 134 case DF_TRIGGER:
5045c217 135 ctx->mark_trigger = ctx->spl_cnt;
a1bb33af
UH
136 break;
137 case DF_END:
99c1fc59
UH
138 outsize = ctx->num_enabled_probes
139 * (ctx->samples_per_line + 20) + 512;
a1bb33af 140 outbuf = calloc(1, outsize);
afc8e4de 141 flush_linebufs(ctx, outbuf);
a1bb33af
UH
142 *data_out = outbuf;
143 *length_out = strlen(outbuf);
144 free(o->internal);
145 o->internal = NULL;
146 break;
147 }
148
149 return SIGROK_OK;
150}
151
02076d69 152static int init_bits(struct output *o)
a1bb33af 153{
02076d69 154 return init(o, DEFAULT_BPL_BITS);
a1bb33af
UH
155}
156
99c1fc59
UH
157static int data_bits(struct output *o, char *data_in, uint64_t length_in,
158 char **data_out, uint64_t *length_out)
a1bb33af
UH
159{
160 struct context *ctx;
afc8e4de 161 unsigned int outsize, offset, p;
a1bb33af
UH
162 uint64_t sample;
163 char *outbuf;
164
165 ctx = o->internal;
99c1fc59
UH
166 outsize = length_in / ctx->unitsize * ctx->num_enabled_probes *
167 ctx->samples_per_line + 512;
168 outbuf = calloc(1, outsize + 1);
169 if (ctx->header) {
170 /* The header is still here, this must be the first packet. */
a1bb33af
UH
171 strncpy(outbuf, ctx->header, outsize);
172 free(ctx->header);
173 ctx->header = NULL;
99c1fc59 174 } else
a1bb33af
UH
175 outbuf[0] = 0;
176
99c1fc59
UH
177 if (length_in >= ctx->unitsize) {
178 for (offset = 0; offset <= length_in - ctx->unitsize;
179 offset += ctx->unitsize) {
a1bb33af 180 memcpy(&sample, data_in + offset, ctx->unitsize);
99c1fc59
UH
181 for (p = 0; p < ctx->num_enabled_probes; p++) {
182 if (sample & ((uint64_t) 1 << p))
183 ctx->linebuf[p * ctx->linebuf_len +
184 ctx->line_offset] = '1';
a1bb33af 185 else
99c1fc59
UH
186 ctx->linebuf[p * ctx->linebuf_len +
187 ctx->line_offset] = '0';
a1bb33af
UH
188 }
189 ctx->line_offset++;
190 ctx->spl_cnt++;
191
99c1fc59
UH
192 /* Add a space every 8th bit. */
193 if ((ctx->spl_cnt & 7) == 0) {
194 for (p = 0; p < ctx->num_enabled_probes; p++)
195 ctx->linebuf[p * ctx->linebuf_len +
196 ctx->line_offset] = ' ';
a1bb33af
UH
197 ctx->line_offset++;
198 }
199
99c1fc59
UH
200 /* End of line. */
201 if (ctx->spl_cnt >= ctx->samples_per_line) {
afc8e4de 202 flush_linebufs(ctx, outbuf);
a1bb33af 203 ctx->line_offset = ctx->spl_cnt = 0;
5045c217 204 ctx->mark_trigger = -1;
a1bb33af
UH
205 }
206 }
207 } else
99c1fc59 208 g_message("short buffer (length_in=%" PRIu64 ")", length_in);
a1bb33af
UH
209
210 *data_out = outbuf;
211 *length_out = strlen(outbuf);
212
213 return SIGROK_OK;
214}
215
5a8fda15 216static int init_hex(struct output *o)
a1bb33af 217{
02076d69 218 return init(o, DEFAULT_BPL_BITS);
a1bb33af
UH
219}
220
99c1fc59
UH
221static int data_hex(struct output *o, char *data_in, uint64_t length_in,
222 char **data_out, uint64_t *length_out)
a1bb33af
UH
223{
224 struct context *ctx;
afc8e4de 225 unsigned int outsize, offset, p;
a1bb33af
UH
226 uint64_t sample;
227 char *outbuf;
228
229 ctx = o->internal;
99c1fc59
UH
230 outsize = length_in / ctx->unitsize * ctx->num_enabled_probes *
231 ctx->samples_per_line + 512;
232 outbuf = calloc(1, outsize + 1);
233 if (ctx->header) {
234 /* The header is still here, this must be the first packet. */
a1bb33af
UH
235 strncpy(outbuf, ctx->header, outsize);
236 free(ctx->header);
237 ctx->header = NULL;
99c1fc59 238 } else
a1bb33af
UH
239 outbuf[0] = 0;
240
241 ctx->line_offset = 0;
99c1fc59
UH
242 for (offset = 0; offset <= length_in - ctx->unitsize;
243 offset += ctx->unitsize) {
a1bb33af 244 memcpy(&sample, data_in + offset, ctx->unitsize);
99c1fc59 245 for (p = 0; p < ctx->num_enabled_probes; p++) {
a1bb33af 246 ctx->linevalues[p] <<= 1;
99c1fc59
UH
247 if (sample & ((uint64_t) 1 << p))
248 ctx->linevalues[p] |= 1;
249 sprintf(ctx->linebuf + (p * ctx->linebuf_len) +
250 ctx->line_offset, "%.2x", ctx->linevalues[p]);
a1bb33af
UH
251 }
252 ctx->spl_cnt++;
253
99c1fc59
UH
254 /* Add a space after every complete hex byte. */
255 if ((ctx->spl_cnt & 7) == 0) {
256 for (p = 0; p < ctx->num_enabled_probes; p++)
257 ctx->linebuf[p * ctx->linebuf_len +
258 ctx->line_offset + 2] = ' ';
a1bb33af
UH
259 ctx->line_offset += 3;
260 }
261
99c1fc59
UH
262 /* End of line. */
263 if (ctx->spl_cnt >= ctx->samples_per_line) {
afc8e4de 264 flush_linebufs(ctx, outbuf);
a1bb33af
UH
265 ctx->line_offset = ctx->spl_cnt = 0;
266 }
267 }
268
269 *data_out = outbuf;
270 *length_out = strlen(outbuf);
271
272 return SIGROK_OK;
273}
274
02076d69 275struct output_format output_text_bits = {
1c5b9d30
UH
276 "bits",
277 "Text (bits)",
02076d69
UH
278 init_bits,
279 data_bits,
99c1fc59 280 event,
a1bb33af
UH
281};
282
a1bb33af
UH
283struct output_format output_text_hex = {
284 "hex",
285 "Text (hexadecimal)",
286 init_hex,
287 data_hex,
99c1fc59 288 event,
a1bb33af 289};