]>
Commit | Line | Data |
---|---|---|
1437e893 BV |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
f3163a6c DR |
4 | * Copyright (C) 2010 Bert Vermeulen <bert@biot.com> |
5 | * Copyright (C) 2011 HÃ¥vard Espeland <gus@ping.uio.no> | |
1437e893 BV |
6 | * |
7 | * This program is free software: you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation, either version 3 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | */ | |
20 | ||
1437e893 | 21 | #include <stdlib.h> |
f3163a6c | 22 | #include <stdio.h> |
1437e893 | 23 | #include <string.h> |
f3163a6c | 24 | #include <glib.h> |
1437e893 | 25 | #include <sigrok.h> |
f3163a6c | 26 | #include "config.h" |
1437e893 | 27 | |
f3163a6c DR |
28 | #define DEFAULT_BPL_BITS 64 |
29 | #define DEFAULT_BPL_HEX 192 | |
30 | #define DEFAULT_BPL_ASCII 74 | |
1437e893 | 31 | |
f3163a6c DR |
32 | enum outputmode { |
33 | MODE_BITS = 1, | |
34 | MODE_HEX, | |
35 | MODE_ASCII, | |
36 | }; | |
1437e893 BV |
37 | |
38 | struct context { | |
1437e893 | 39 | unsigned int num_enabled_probes; |
1437e893 BV |
40 | int samples_per_line; |
41 | unsigned int unitsize; | |
42 | int line_offset; | |
43 | int linebuf_len; | |
f3163a6c | 44 | char *probelist[65]; |
1437e893 BV |
45 | char *linebuf; |
46 | int spl_cnt; | |
f3163a6c DR |
47 | uint8_t *linevalues; |
48 | char *header; | |
1437e893 | 49 | int mark_trigger; |
f3163a6c DR |
50 | uint64_t prevsample; |
51 | enum outputmode mode; | |
1437e893 BV |
52 | }; |
53 | ||
1437e893 BV |
54 | static void flush_linebufs(struct context *ctx, char *outbuf) |
55 | { | |
56 | static int max_probename_len = 0; | |
57 | int len, i; | |
58 | ||
59 | if (ctx->linebuf[0] == 0) | |
60 | return; | |
61 | ||
62 | if (max_probename_len == 0) { | |
63 | /* First time through... */ | |
64 | for (i = 0; ctx->probelist[i]; i++) { | |
65 | len = strlen(ctx->probelist[i]); | |
66 | if (len > max_probename_len) | |
67 | max_probename_len = len; | |
68 | } | |
69 | } | |
70 | ||
71 | for (i = 0; ctx->probelist[i]; i++) { | |
72 | sprintf(outbuf + strlen(outbuf), "%*s:%s\n", max_probename_len, | |
73 | ctx->probelist[i], ctx->linebuf + i * ctx->linebuf_len); | |
74 | } | |
75 | ||
76 | /* Mark trigger with a ^ character. */ | |
77 | if (ctx->mark_trigger != -1) | |
f3163a6c DR |
78 | { |
79 | int space_offset = ctx->mark_trigger / 8; | |
80 | ||
81 | if (ctx->mode == MODE_ASCII) | |
82 | space_offset = 0; | |
83 | ||
1437e893 | 84 | sprintf(outbuf + strlen(outbuf), "T:%*s^\n", |
f3163a6c DR |
85 | ctx->mark_trigger + space_offset, ""); |
86 | } | |
1437e893 BV |
87 | |
88 | memset(ctx->linebuf, 0, i * ctx->linebuf_len); | |
89 | } | |
90 | ||
f3163a6c | 91 | static int init(struct output *o, int default_spl, enum outputmode mode) |
1437e893 BV |
92 | { |
93 | struct context *ctx; | |
94 | struct probe *probe; | |
95 | GSList *l; | |
96 | uint64_t samplerate; | |
97 | int num_probes; | |
98 | char *samplerate_s; | |
99 | ||
100 | if (!(ctx = calloc(1, sizeof(struct context)))) | |
101 | return SIGROK_ERR_MALLOC; | |
102 | ||
1437e893 | 103 | o->internal = ctx; |
1437e893 | 104 | ctx->num_enabled_probes = 0; |
f3163a6c DR |
105 | |
106 | for (l = o->device->probes; l; l = l->next) { | |
107 | probe = l->data; | |
108 | if (!probe->enabled) | |
109 | continue; | |
110 | ctx->probelist[ctx->num_enabled_probes++] = probe->name; | |
111 | } | |
112 | ||
113 | ctx->probelist[ctx->num_enabled_probes] = 0; | |
114 | ctx->unitsize = (ctx->num_enabled_probes + 7) / 8; | |
115 | ctx->line_offset = 0; | |
116 | ctx->spl_cnt = 0; | |
1437e893 | 117 | ctx->mark_trigger = -1; |
f3163a6c DR |
118 | ctx->mode = mode; |
119 | ||
9b36e360 BV |
120 | if (o->param && o->param[0]) { |
121 | ctx->samples_per_line = strtoul(o->param, NULL, 10); | |
122 | if (ctx->samples_per_line < 1) | |
123 | return SIGROK_ERR; | |
124 | } else | |
f3163a6c | 125 | ctx->samples_per_line = default_spl; |
9b36e360 | 126 | |
f3163a6c DR |
127 | if (!(ctx->header = malloc(512))) { |
128 | free(ctx); | |
129 | return SIGROK_ERR_MALLOC; | |
1437e893 | 130 | } |
1437e893 BV |
131 | |
132 | snprintf(ctx->header, 511, "%s\n", PACKAGE_STRING); | |
f3163a6c | 133 | num_probes = g_slist_length(o->device->probes); |
1437e893 | 134 | if (o->device->plugin) { |
1437e893 BV |
135 | samplerate = *((uint64_t *) o->device->plugin->get_device_info( |
136 | o->device->plugin_index, DI_CUR_SAMPLERATE)); | |
137 | if (!(samplerate_s = sigrok_samplerate_string(samplerate))) { | |
138 | free(ctx->header); | |
139 | free(ctx); | |
140 | return SIGROK_ERR; | |
141 | } | |
142 | snprintf(ctx->header + strlen(ctx->header), | |
143 | 511 - strlen(ctx->header), | |
144 | "Acquisition with %d/%d probes at %s\n", | |
145 | ctx->num_enabled_probes, num_probes, samplerate_s); | |
146 | free(samplerate_s); | |
147 | } | |
148 | ||
f3163a6c DR |
149 | ctx->linebuf_len = ctx->samples_per_line * 2 + 4; |
150 | if (!(ctx->linebuf = calloc(1, num_probes * ctx->linebuf_len))) { | |
151 | free(ctx->header); | |
152 | free(ctx); | |
153 | return SIGROK_ERR_MALLOC; | |
154 | } | |
155 | if (!(ctx->linevalues = calloc(1, num_probes))) { | |
1437e893 BV |
156 | free(ctx->header); |
157 | free(ctx); | |
158 | return SIGROK_ERR_MALLOC; | |
159 | } | |
160 | ||
f3163a6c | 161 | return SIGROK_OK; |
1437e893 BV |
162 | } |
163 | ||
f3163a6c DR |
164 | static int event(struct output *o, int event_type, char **data_out, |
165 | uint64_t *length_out) | |
1437e893 BV |
166 | { |
167 | struct context *ctx; | |
f3163a6c DR |
168 | int outsize; |
169 | char *outbuf; | |
1437e893 BV |
170 | |
171 | ctx = o->internal; | |
f3163a6c DR |
172 | switch (event_type) { |
173 | case DF_TRIGGER: | |
174 | ctx->mark_trigger = ctx->spl_cnt; | |
175 | *data_out = NULL; | |
176 | *length_out = 0; | |
177 | break; | |
178 | case DF_END: | |
179 | outsize = ctx->num_enabled_probes | |
180 | * (ctx->samples_per_line + 20) + 512; | |
181 | if (!(outbuf = calloc(1, outsize))) | |
182 | return SIGROK_ERR_MALLOC; | |
183 | flush_linebufs(ctx, outbuf); | |
184 | *data_out = outbuf; | |
185 | *length_out = strlen(outbuf); | |
186 | free(o->internal); | |
187 | o->internal = NULL; | |
188 | break; | |
189 | default: | |
190 | *data_out = NULL; | |
191 | *length_out = 0; | |
192 | break; | |
193 | } | |
194 | ||
195 | return SIGROK_OK; | |
196 | } | |
197 | ||
198 | static int init_bits(struct output *o) | |
199 | { | |
200 | return init(o, DEFAULT_BPL_BITS, MODE_BITS); | |
201 | } | |
202 | ||
203 | static int data_bits(struct output *o, char *data_in, uint64_t length_in, | |
204 | char **data_out, uint64_t *length_out) | |
205 | { | |
206 | struct context *ctx; | |
207 | unsigned int outsize, offset, p; | |
208 | int max_linelen; | |
209 | uint64_t sample; | |
210 | char *outbuf, c; | |
211 | ||
212 | ctx = o->internal; | |
213 | max_linelen = MAX_PROBENAME_LEN + 3 + ctx->samples_per_line | |
1437e893 | 214 | + ctx->samples_per_line / 8; |
f3163a6c DR |
215 | /* |
216 | * Calculate space needed for probes. Set aside 512 bytes for | |
217 | * extra output, e.g. trigger. | |
218 | */ | |
219 | outsize = 512 + (1 + (length_in / ctx->unitsize) / ctx->samples_per_line) | |
220 | * (ctx->num_enabled_probes * max_linelen); | |
221 | ||
1437e893 BV |
222 | if (!(outbuf = calloc(1, outsize + 1))) |
223 | return SIGROK_ERR_MALLOC; | |
224 | ||
225 | outbuf[0] = '\0'; | |
226 | if (ctx->header) { | |
227 | /* The header is still here, this must be the first packet. */ | |
228 | strncpy(outbuf, ctx->header, outsize); | |
229 | free(ctx->header); | |
230 | ctx->header = NULL; | |
f3163a6c DR |
231 | |
232 | /* Ensure first transition. */ | |
233 | memcpy(&ctx->prevsample, data_in, ctx->unitsize); | |
234 | ctx->prevsample = ~ctx->prevsample; | |
1437e893 BV |
235 | } |
236 | ||
237 | if (length_in >= ctx->unitsize) { | |
238 | for (offset = 0; offset <= length_in - ctx->unitsize; | |
239 | offset += ctx->unitsize) { | |
f3163a6c | 240 | memcpy(&sample, data_in + offset, ctx->unitsize); |
1437e893 | 241 | for (p = 0; p < ctx->num_enabled_probes; p++) { |
f3163a6c DR |
242 | c = (sample & ((uint64_t) 1 << p)) ? '1' : '0'; |
243 | ctx->linebuf[p * ctx->linebuf_len + | |
244 | ctx->line_offset] = c; | |
245 | } | |
246 | ctx->line_offset++; | |
247 | ctx->spl_cnt++; | |
248 | ||
249 | /* Add a space every 8th bit. */ | |
250 | if ((ctx->spl_cnt & 7) == 0) { | |
251 | for (p = 0; p < ctx->num_enabled_probes; p++) | |
252 | ctx->linebuf[p * ctx->linebuf_len + | |
253 | ctx->line_offset] = ' '; | |
254 | ctx->line_offset++; | |
1437e893 BV |
255 | } |
256 | ||
257 | /* End of line. */ | |
258 | if (ctx->spl_cnt >= ctx->samples_per_line) { | |
259 | flush_linebufs(ctx, outbuf); | |
260 | ctx->line_offset = ctx->spl_cnt = 0; | |
261 | ctx->mark_trigger = -1; | |
262 | } | |
1437e893 BV |
263 | } |
264 | } else { | |
265 | g_message("short buffer (length_in=%" PRIu64 ")", length_in); | |
266 | } | |
267 | ||
268 | *data_out = outbuf; | |
269 | *length_out = strlen(outbuf); | |
270 | ||
271 | return SIGROK_OK; | |
272 | } | |
273 | ||
f3163a6c DR |
274 | static int init_hex(struct output *o) |
275 | { | |
276 | return init(o, DEFAULT_BPL_HEX, MODE_HEX); | |
277 | } | |
278 | ||
279 | static int data_hex(struct output *o, char *data_in, uint64_t length_in, | |
280 | char **data_out, uint64_t *length_out) | |
1437e893 BV |
281 | { |
282 | struct context *ctx; | |
f3163a6c DR |
283 | unsigned int outsize, offset, p; |
284 | int max_linelen; | |
285 | uint64_t sample; | |
286 | char *outbuf; | |
1437e893 BV |
287 | |
288 | ctx = o->internal; | |
f3163a6c DR |
289 | max_linelen = MAX_PROBENAME_LEN + 3 + ctx->samples_per_line |
290 | + ctx->samples_per_line / 2; | |
291 | outsize = length_in / ctx->unitsize * ctx->num_enabled_probes | |
292 | / ctx->samples_per_line * max_linelen + 512; | |
293 | ||
294 | if (!(outbuf = calloc(1, outsize + 1))) | |
295 | return SIGROK_ERR_MALLOC; | |
296 | ||
297 | outbuf[0] = '\0'; | |
298 | if (ctx->header) { | |
299 | /* The header is still here, this must be the first packet. */ | |
300 | strncpy(outbuf, ctx->header, outsize); | |
301 | free(ctx->header); | |
302 | ctx->header = NULL; | |
1437e893 BV |
303 | } |
304 | ||
f3163a6c DR |
305 | ctx->line_offset = 0; |
306 | for (offset = 0; offset <= length_in - ctx->unitsize; | |
307 | offset += ctx->unitsize) { | |
308 | memcpy(&sample, data_in + offset, ctx->unitsize); | |
309 | for (p = 0; p < ctx->num_enabled_probes; p++) { | |
310 | ctx->linevalues[p] <<= 1; | |
311 | if (sample & ((uint64_t) 1 << p)) | |
312 | ctx->linevalues[p] |= 1; | |
313 | sprintf(ctx->linebuf + (p * ctx->linebuf_len) + | |
314 | ctx->line_offset, "%.2x", ctx->linevalues[p]); | |
315 | } | |
316 | ctx->spl_cnt++; | |
317 | ||
318 | /* Add a space after every complete hex byte. */ | |
319 | if ((ctx->spl_cnt & 7) == 0) { | |
320 | for (p = 0; p < ctx->num_enabled_probes; p++) | |
321 | ctx->linebuf[p * ctx->linebuf_len + | |
322 | ctx->line_offset + 2] = ' '; | |
323 | ctx->line_offset += 3; | |
324 | } | |
325 | ||
326 | /* End of line. */ | |
327 | if (ctx->spl_cnt >= ctx->samples_per_line) { | |
328 | flush_linebufs(ctx, outbuf); | |
329 | ctx->line_offset = ctx->spl_cnt = 0; | |
330 | } | |
331 | } | |
332 | ||
333 | *data_out = outbuf; | |
334 | *length_out = strlen(outbuf); | |
1437e893 BV |
335 | |
336 | return SIGROK_OK; | |
337 | } | |
338 | ||
f3163a6c DR |
339 | static int init_ascii(struct output *o) |
340 | { | |
341 | return init(o, DEFAULT_BPL_ASCII, MODE_ASCII); | |
342 | } | |
343 | ||
344 | static int data_ascii(struct output *o, char *data_in, uint64_t length_in, | |
345 | char **data_out, uint64_t *length_out) | |
346 | { | |
347 | struct context *ctx; | |
348 | unsigned int outsize, offset, p; | |
349 | int max_linelen; | |
350 | uint64_t sample; | |
351 | char *outbuf; | |
352 | ||
353 | ctx = o->internal; | |
354 | max_linelen = MAX_PROBENAME_LEN + 3 + ctx->samples_per_line | |
355 | + ctx->samples_per_line / 8; | |
356 | /* | |
357 | * Calculate space needed for probes. Set aside 512 bytes for | |
358 | * extra output, e.g. trigger. | |
359 | */ | |
360 | outsize = 512 + (1 + (length_in / ctx->unitsize) / ctx->samples_per_line) | |
361 | * (ctx->num_enabled_probes * max_linelen); | |
362 | ||
363 | if (!(outbuf = calloc(1, outsize + 1))) | |
364 | return SIGROK_ERR_MALLOC; | |
365 | ||
366 | outbuf[0] = '\0'; | |
367 | if (ctx->header) { | |
368 | /* The header is still here, this must be the first packet. */ | |
369 | strncpy(outbuf, ctx->header, outsize); | |
370 | free(ctx->header); | |
371 | ctx->header = NULL; | |
372 | } | |
373 | ||
374 | if (length_in >= ctx->unitsize) { | |
375 | for (offset = 0; offset <= length_in - ctx->unitsize; | |
376 | offset += ctx->unitsize) { | |
377 | memcpy(&sample, data_in + offset, ctx->unitsize); | |
378 | ||
379 | char tmpval[ctx->num_enabled_probes]; | |
380 | ||
381 | for (p = 0; p < ctx->num_enabled_probes; p++) { | |
382 | uint64_t curbit = (sample & ((uint64_t) 1 << p)); | |
383 | uint64_t prevbit = (ctx->prevsample & | |
384 | ((uint64_t) 1 << p)); | |
385 | ||
386 | if (curbit < prevbit && ctx->line_offset > 0) { | |
387 | ctx->linebuf[p * ctx->linebuf_len + | |
388 | ctx->line_offset-1] = '\\'; | |
389 | } | |
390 | ||
391 | if (curbit > prevbit) { | |
392 | tmpval[p] = '/'; | |
393 | } else { | |
394 | if (curbit) | |
395 | tmpval[p] = '"'; | |
396 | else | |
397 | tmpval[p] = '.'; | |
398 | } | |
399 | } | |
400 | ||
401 | /* End of line. */ | |
402 | if (ctx->spl_cnt >= ctx->samples_per_line) { | |
403 | flush_linebufs(ctx, outbuf); | |
404 | ctx->line_offset = ctx->spl_cnt = 0; | |
405 | ctx->mark_trigger = -1; | |
406 | } | |
407 | ||
408 | for (p = 0; p < ctx->num_enabled_probes; p++) { | |
409 | ctx->linebuf[p * ctx->linebuf_len + | |
410 | ctx->line_offset] = tmpval[p]; | |
411 | } | |
412 | ||
413 | ctx->line_offset++; | |
414 | ctx->spl_cnt++; | |
415 | ||
416 | ctx->prevsample = sample; | |
417 | } | |
418 | } else { | |
419 | g_message("short buffer (length_in=%" PRIu64 ")", length_in); | |
420 | } | |
421 | ||
422 | *data_out = outbuf; | |
423 | *length_out = strlen(outbuf); | |
424 | ||
425 | return SIGROK_OK; | |
426 | } | |
427 | ||
428 | ||
429 | struct output_format output_text_bits = { | |
430 | "bits", | |
431 | "Bits (takes argument, default 64)", | |
432 | DF_LOGIC, | |
433 | init_bits, | |
434 | data_bits, | |
1437e893 BV |
435 | event, |
436 | }; | |
437 | ||
f3163a6c DR |
438 | struct output_format output_text_hex = { |
439 | "hex", | |
440 | "Hexadecimal (takes argument, default 192)", | |
441 | DF_LOGIC, | |
442 | init_hex, | |
443 | data_hex, | |
444 | event, | |
445 | }; | |
446 | ||
447 | struct output_format output_text_ascii = { | |
448 | "ascii", | |
449 | "ASCII (takes argument, default 74)", | |
450 | DF_LOGIC, | |
451 | init_ascii, | |
452 | data_ascii, | |
453 | event, | |
454 | }; |