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