]>
Commit | Line | Data |
---|---|---|
a1bb33af UH |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2010 Bert Vermeulen <bert@biot.com> | |
d4f228d0 | 5 | * Copyright (C) 2011 Håvard Espeland <gus@ping.uio.no> |
a1bb33af UH |
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 | ||
21 | #include <stdlib.h> | |
22 | #include <stdio.h> | |
23 | #include <string.h> | |
24 | #include <glib.h> | |
25e7d9b1 | 25 | #include <sigrok.h> |
d2b36a10 | 26 | #include "config.h" |
a1bb33af | 27 | |
02076d69 | 28 | #define DEFAULT_BPL_BITS 64 |
f7606f9b | 29 | #define DEFAULT_BPL_HEX 192 |
d4f228d0 | 30 | #define DEFAULT_BPL_ASCII 74 |
a1bb33af | 31 | |
afa8f844 HE |
32 | enum outputmode { |
33 | MODE_BITS = 1, | |
34 | MODE_HEX, | |
35 | MODE_ASCII, | |
36 | }; | |
37 | ||
a1bb33af | 38 | struct context { |
afc8e4de | 39 | unsigned int num_enabled_probes; |
a1bb33af | 40 | int samples_per_line; |
afc8e4de | 41 | unsigned int unitsize; |
a1bb33af UH |
42 | int line_offset; |
43 | int linebuf_len; | |
44 | char *probelist[65]; | |
45 | char *linebuf; | |
46 | int spl_cnt; | |
47 | uint8_t *linevalues; | |
48 | char *header; | |
5045c217 | 49 | int mark_trigger; |
d4f228d0 | 50 | uint64_t prevsample; |
afa8f844 | 51 | enum outputmode mode; |
a1bb33af UH |
52 | }; |
53 | ||
afc8e4de | 54 | static void flush_linebufs(struct context *ctx, char *outbuf) |
a1bb33af UH |
55 | { |
56 | static int max_probename_len = 0; | |
57 | int len, i; | |
58 | ||
99c1fc59 | 59 | if (ctx->linebuf[0] == 0) |
a1bb33af UH |
60 | return; |
61 | ||
99c1fc59 UH |
62 | if (max_probename_len == 0) { |
63 | /* First time through... */ | |
64 | for (i = 0; ctx->probelist[i]; i++) { | |
a1bb33af | 65 | len = strlen(ctx->probelist[i]); |
99c1fc59 | 66 | if (len > max_probename_len) |
a1bb33af UH |
67 | max_probename_len = len; |
68 | } | |
69 | } | |
70 | ||
99c1fc59 UH |
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); | |
a1bb33af | 74 | } |
5045c217 | 75 | |
e734b81a | 76 | /* Mark trigger with a ^ character. */ |
5045c217 | 77 | if (ctx->mark_trigger != -1) |
afa8f844 HE |
78 | { |
79 | int space_offset = ctx->mark_trigger / 8; | |
80 | ||
81 | if (ctx->mode == MODE_ASCII) | |
82 | space_offset = 0; | |
83 | ||
5045c217 | 84 | sprintf(outbuf + strlen(outbuf), "T:%*s^\n", |
afa8f844 HE |
85 | ctx->mark_trigger + space_offset, ""); |
86 | } | |
5045c217 | 87 | |
a1bb33af | 88 | memset(ctx->linebuf, 0, i * ctx->linebuf_len); |
a1bb33af UH |
89 | } |
90 | ||
afa8f844 | 91 | static int init(struct output *o, int default_spl, enum outputmode mode) |
a1bb33af UH |
92 | { |
93 | struct context *ctx; | |
94 | struct probe *probe; | |
95 | GSList *l; | |
96 | uint64_t samplerate; | |
97 | int num_probes; | |
25e7d9b1 | 98 | char *samplerate_s; |
a1bb33af | 99 | |
e734b81a UH |
100 | if (!(ctx = calloc(1, sizeof(struct context)))) |
101 | return SIGROK_ERR_MALLOC; | |
102 | ||
a1bb33af UH |
103 | o->internal = ctx; |
104 | ctx->num_enabled_probes = 0; | |
99c1fc59 UH |
105 | |
106 | for (l = o->device->probes; l; l = l->next) { | |
a1bb33af | 107 | probe = l->data; |
757b8c62 UH |
108 | if (!probe->enabled) |
109 | continue; | |
110 | ctx->probelist[ctx->num_enabled_probes++] = probe->name; | |
a1bb33af | 111 | } |
99c1fc59 | 112 | |
a1bb33af UH |
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; | |
5045c217 | 117 | ctx->mark_trigger = -1; |
afa8f844 | 118 | ctx->mode = mode; |
99c1fc59 | 119 | |
ee5f5e81 | 120 | if (o->param && o->param[0]) { |
a1bb33af | 121 | ctx->samples_per_line = strtoul(o->param, NULL, 10); |
ee5f5e81 BV |
122 | if (ctx->samples_per_line < 1) |
123 | return SIGROK_ERR; | |
124 | } else | |
a1bb33af UH |
125 | ctx->samples_per_line = default_spl; |
126 | ||
e734b81a UH |
127 | if (!(ctx->header = malloc(512))) { |
128 | free(ctx); | |
129 | return SIGROK_ERR_MALLOC; | |
130 | } | |
131 | ||
d2b36a10 | 132 | snprintf(ctx->header, 511, "%s\n", PACKAGE_STRING); |
78ed6420 | 133 | num_probes = g_slist_length(o->device->probes); |
7aae7462 | 134 | if (o->device->plugin) { |
7aae7462 BV |
135 | samplerate = *((uint64_t *) o->device->plugin->get_device_info( |
136 | o->device->plugin_index, DI_CUR_SAMPLERATE)); | |
e734b81a UH |
137 | if (!(samplerate_s = sigrok_samplerate_string(samplerate))) { |
138 | free(ctx->header); | |
139 | free(ctx); | |
d2b36a10 | 140 | return SIGROK_ERR; |
e734b81a UH |
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); | |
7aae7462 | 146 | free(samplerate_s); |
7aae7462 | 147 | } |
a1bb33af | 148 | |
ee5f5e81 | 149 | ctx->linebuf_len = ctx->samples_per_line * 2 + 4; |
e734b81a UH |
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))) { | |
156 | free(ctx->header); | |
157 | free(ctx); | |
158 | return SIGROK_ERR_MALLOC; | |
159 | } | |
a1bb33af | 160 | |
e734b81a | 161 | return SIGROK_OK; |
a1bb33af UH |
162 | } |
163 | ||
99c1fc59 UH |
164 | static int event(struct output *o, int event_type, char **data_out, |
165 | uint64_t *length_out) | |
a1bb33af UH |
166 | { |
167 | struct context *ctx; | |
168 | int outsize; | |
169 | char *outbuf; | |
170 | ||
171 | ctx = o->internal; | |
99c1fc59 | 172 | switch (event_type) { |
a1bb33af | 173 | case DF_TRIGGER: |
5045c217 | 174 | ctx->mark_trigger = ctx->spl_cnt; |
78ed6420 BV |
175 | *data_out = NULL; |
176 | *length_out = 0; | |
a1bb33af UH |
177 | break; |
178 | case DF_END: | |
99c1fc59 UH |
179 | outsize = ctx->num_enabled_probes |
180 | * (ctx->samples_per_line + 20) + 512; | |
e734b81a UH |
181 | if (!(outbuf = calloc(1, outsize))) |
182 | return SIGROK_ERR_MALLOC; | |
afc8e4de | 183 | flush_linebufs(ctx, outbuf); |
a1bb33af UH |
184 | *data_out = outbuf; |
185 | *length_out = strlen(outbuf); | |
186 | free(o->internal); | |
187 | o->internal = NULL; | |
188 | break; | |
78ed6420 BV |
189 | default: |
190 | *data_out = NULL; | |
191 | *length_out = 0; | |
192 | break; | |
a1bb33af UH |
193 | } |
194 | ||
195 | return SIGROK_OK; | |
196 | } | |
197 | ||
02076d69 | 198 | static int init_bits(struct output *o) |
a1bb33af | 199 | { |
afa8f844 | 200 | return init(o, DEFAULT_BPL_BITS, MODE_BITS); |
a1bb33af UH |
201 | } |
202 | ||
99c1fc59 UH |
203 | static int data_bits(struct output *o, char *data_in, uint64_t length_in, |
204 | char **data_out, uint64_t *length_out) | |
a1bb33af UH |
205 | { |
206 | struct context *ctx; | |
afc8e4de | 207 | unsigned int outsize, offset, p; |
33972913 | 208 | int max_linelen; |
a1bb33af | 209 | uint64_t sample; |
757b8c62 | 210 | char *outbuf, c; |
a1bb33af UH |
211 | |
212 | ctx = o->internal; | |
757b8c62 UH |
213 | max_linelen = MAX_PROBENAME_LEN + 3 + ctx->samples_per_line |
214 | + ctx->samples_per_line / 8; | |
9d7ab9ba HE |
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) | |
3aa403e8 | 220 | * (ctx->num_enabled_probes * max_linelen); |
e734b81a UH |
221 | |
222 | if (!(outbuf = calloc(1, outsize + 1))) | |
223 | return SIGROK_ERR_MALLOC; | |
224 | ||
225 | outbuf[0] = '\0'; | |
99c1fc59 UH |
226 | if (ctx->header) { |
227 | /* The header is still here, this must be the first packet. */ | |
a1bb33af UH |
228 | strncpy(outbuf, ctx->header, outsize); |
229 | free(ctx->header); | |
230 | ctx->header = NULL; | |
d4f228d0 HE |
231 | |
232 | /* Ensure first transition. */ | |
233 | memcpy(&ctx->prevsample, data_in, ctx->unitsize); | |
234 | ctx->prevsample = ~ctx->prevsample; | |
e734b81a | 235 | } |
a1bb33af | 236 | |
99c1fc59 UH |
237 | if (length_in >= ctx->unitsize) { |
238 | for (offset = 0; offset <= length_in - ctx->unitsize; | |
239 | offset += ctx->unitsize) { | |
a1bb33af | 240 | memcpy(&sample, data_in + offset, ctx->unitsize); |
99c1fc59 | 241 | for (p = 0; p < ctx->num_enabled_probes; p++) { |
757b8c62 UH |
242 | c = (sample & ((uint64_t) 1 << p)) ? '1' : '0'; |
243 | ctx->linebuf[p * ctx->linebuf_len + | |
244 | ctx->line_offset] = c; | |
a1bb33af UH |
245 | } |
246 | ctx->line_offset++; | |
247 | ctx->spl_cnt++; | |
248 | ||
99c1fc59 UH |
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] = ' '; | |
a1bb33af UH |
254 | ctx->line_offset++; |
255 | } | |
256 | ||
99c1fc59 UH |
257 | /* End of line. */ |
258 | if (ctx->spl_cnt >= ctx->samples_per_line) { | |
afc8e4de | 259 | flush_linebufs(ctx, outbuf); |
a1bb33af | 260 | ctx->line_offset = ctx->spl_cnt = 0; |
5045c217 | 261 | ctx->mark_trigger = -1; |
a1bb33af UH |
262 | } |
263 | } | |
e734b81a | 264 | } else { |
99c1fc59 | 265 | g_message("short buffer (length_in=%" PRIu64 ")", length_in); |
e734b81a | 266 | } |
a1bb33af UH |
267 | |
268 | *data_out = outbuf; | |
269 | *length_out = strlen(outbuf); | |
270 | ||
271 | return SIGROK_OK; | |
272 | } | |
273 | ||
5a8fda15 | 274 | static int init_hex(struct output *o) |
a1bb33af | 275 | { |
afa8f844 | 276 | return init(o, DEFAULT_BPL_HEX, MODE_HEX); |
a1bb33af UH |
277 | } |
278 | ||
99c1fc59 UH |
279 | static int data_hex(struct output *o, char *data_in, uint64_t length_in, |
280 | char **data_out, uint64_t *length_out) | |
a1bb33af UH |
281 | { |
282 | struct context *ctx; | |
afc8e4de | 283 | unsigned int outsize, offset, p; |
33972913 | 284 | int max_linelen; |
a1bb33af UH |
285 | uint64_t sample; |
286 | char *outbuf; | |
287 | ||
288 | ctx = o->internal; | |
757b8c62 UH |
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; | |
e734b81a UH |
293 | |
294 | if (!(outbuf = calloc(1, outsize + 1))) | |
295 | return SIGROK_ERR_MALLOC; | |
296 | ||
297 | outbuf[0] = '\0'; | |
99c1fc59 UH |
298 | if (ctx->header) { |
299 | /* The header is still here, this must be the first packet. */ | |
a1bb33af UH |
300 | strncpy(outbuf, ctx->header, outsize); |
301 | free(ctx->header); | |
302 | ctx->header = NULL; | |
e734b81a | 303 | } |
a1bb33af UH |
304 | |
305 | ctx->line_offset = 0; | |
99c1fc59 UH |
306 | for (offset = 0; offset <= length_in - ctx->unitsize; |
307 | offset += ctx->unitsize) { | |
a1bb33af | 308 | memcpy(&sample, data_in + offset, ctx->unitsize); |
99c1fc59 | 309 | for (p = 0; p < ctx->num_enabled_probes; p++) { |
a1bb33af | 310 | ctx->linevalues[p] <<= 1; |
99c1fc59 UH |
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]); | |
a1bb33af UH |
315 | } |
316 | ctx->spl_cnt++; | |
317 | ||
99c1fc59 UH |
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] = ' '; | |
a1bb33af UH |
323 | ctx->line_offset += 3; |
324 | } | |
325 | ||
99c1fc59 UH |
326 | /* End of line. */ |
327 | if (ctx->spl_cnt >= ctx->samples_per_line) { | |
afc8e4de | 328 | flush_linebufs(ctx, outbuf); |
a1bb33af UH |
329 | ctx->line_offset = ctx->spl_cnt = 0; |
330 | } | |
331 | } | |
332 | ||
333 | *data_out = outbuf; | |
334 | *length_out = strlen(outbuf); | |
335 | ||
336 | return SIGROK_OK; | |
337 | } | |
338 | ||
d4f228d0 HE |
339 | static int init_ascii(struct output *o) |
340 | { | |
afa8f844 | 341 | return init(o, DEFAULT_BPL_ASCII, MODE_ASCII); |
d4f228d0 HE |
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; | |
6ef7a8cb | 351 | char *outbuf; |
d4f228d0 HE |
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 | ||
6ef7a8cb HE |
379 | char tmpval[ctx->num_enabled_probes]; |
380 | ||
d4f228d0 HE |
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 | ||
6ef7a8cb | 386 | if (curbit < prevbit && ctx->line_offset > 0) { |
d4f228d0 HE |
387 | ctx->linebuf[p * ctx->linebuf_len + |
388 | ctx->line_offset-1] = '\\'; | |
389 | } | |
390 | ||
6ef7a8cb HE |
391 | if (curbit > prevbit) { |
392 | tmpval[p] = '/'; | |
393 | } else { | |
d4f228d0 | 394 | if (curbit) |
6ef7a8cb | 395 | tmpval[p] = '"'; |
d4f228d0 | 396 | else |
6ef7a8cb | 397 | tmpval[p] = '.'; |
d4f228d0 | 398 | } |
d4f228d0 | 399 | } |
d4f228d0 HE |
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 | ||
6ef7a8cb HE |
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 | ||
d4f228d0 HE |
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 | ||
02076d69 | 429 | struct output_format output_text_bits = { |
1c5b9d30 | 430 | "bits", |
655756e0 | 431 | "Bits (takes argument, default 64)", |
f0411b1d | 432 | DF_LOGIC, |
02076d69 UH |
433 | init_bits, |
434 | data_bits, | |
99c1fc59 | 435 | event, |
a1bb33af UH |
436 | }; |
437 | ||
a1bb33af UH |
438 | struct output_format output_text_hex = { |
439 | "hex", | |
f7606f9b | 440 | "Hexadecimal (takes argument, default 192)", |
f0411b1d | 441 | DF_LOGIC, |
a1bb33af UH |
442 | init_hex, |
443 | data_hex, | |
99c1fc59 | 444 | event, |
a1bb33af | 445 | }; |
d4f228d0 HE |
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 | }; |