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