]>
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 UH |
31 | |
32 | struct context { | |
afc8e4de | 33 | unsigned int num_enabled_probes; |
a1bb33af | 34 | int samples_per_line; |
afc8e4de | 35 | unsigned int unitsize; |
a1bb33af UH |
36 | int line_offset; |
37 | int linebuf_len; | |
38 | char *probelist[65]; | |
39 | char *linebuf; | |
40 | int spl_cnt; | |
41 | uint8_t *linevalues; | |
42 | char *header; | |
5045c217 | 43 | int mark_trigger; |
d4f228d0 | 44 | uint64_t prevsample; |
a1bb33af UH |
45 | }; |
46 | ||
afc8e4de | 47 | static void flush_linebufs(struct context *ctx, char *outbuf) |
a1bb33af UH |
48 | { |
49 | static int max_probename_len = 0; | |
50 | int len, i; | |
51 | ||
99c1fc59 | 52 | if (ctx->linebuf[0] == 0) |
a1bb33af UH |
53 | return; |
54 | ||
99c1fc59 UH |
55 | if (max_probename_len == 0) { |
56 | /* First time through... */ | |
57 | for (i = 0; ctx->probelist[i]; i++) { | |
a1bb33af | 58 | len = strlen(ctx->probelist[i]); |
99c1fc59 | 59 | if (len > max_probename_len) |
a1bb33af UH |
60 | max_probename_len = len; |
61 | } | |
62 | } | |
63 | ||
99c1fc59 UH |
64 | for (i = 0; ctx->probelist[i]; i++) { |
65 | sprintf(outbuf + strlen(outbuf), "%*s:%s\n", max_probename_len, | |
66 | ctx->probelist[i], ctx->linebuf + i * ctx->linebuf_len); | |
a1bb33af | 67 | } |
5045c217 | 68 | |
e734b81a | 69 | /* Mark trigger with a ^ character. */ |
5045c217 HE |
70 | if (ctx->mark_trigger != -1) |
71 | sprintf(outbuf + strlen(outbuf), "T:%*s^\n", | |
72 | ctx->mark_trigger + (ctx->mark_trigger / 8), ""); | |
73 | ||
a1bb33af | 74 | memset(ctx->linebuf, 0, i * ctx->linebuf_len); |
a1bb33af UH |
75 | } |
76 | ||
5a8fda15 | 77 | static int init(struct output *o, int default_spl) |
a1bb33af UH |
78 | { |
79 | struct context *ctx; | |
80 | struct probe *probe; | |
81 | GSList *l; | |
82 | uint64_t samplerate; | |
83 | int num_probes; | |
25e7d9b1 | 84 | char *samplerate_s; |
a1bb33af | 85 | |
e734b81a UH |
86 | if (!(ctx = calloc(1, sizeof(struct context)))) |
87 | return SIGROK_ERR_MALLOC; | |
88 | ||
a1bb33af UH |
89 | o->internal = ctx; |
90 | ctx->num_enabled_probes = 0; | |
99c1fc59 UH |
91 | |
92 | for (l = o->device->probes; l; l = l->next) { | |
a1bb33af | 93 | probe = l->data; |
757b8c62 UH |
94 | if (!probe->enabled) |
95 | continue; | |
96 | ctx->probelist[ctx->num_enabled_probes++] = probe->name; | |
a1bb33af | 97 | } |
99c1fc59 | 98 | |
a1bb33af UH |
99 | ctx->probelist[ctx->num_enabled_probes] = 0; |
100 | ctx->unitsize = (ctx->num_enabled_probes + 7) / 8; | |
101 | ctx->line_offset = 0; | |
102 | ctx->spl_cnt = 0; | |
5045c217 | 103 | ctx->mark_trigger = -1; |
99c1fc59 | 104 | |
ee5f5e81 | 105 | if (o->param && o->param[0]) { |
a1bb33af | 106 | ctx->samples_per_line = strtoul(o->param, NULL, 10); |
ee5f5e81 BV |
107 | if (ctx->samples_per_line < 1) |
108 | return SIGROK_ERR; | |
109 | } else | |
a1bb33af UH |
110 | ctx->samples_per_line = default_spl; |
111 | ||
e734b81a UH |
112 | if (!(ctx->header = malloc(512))) { |
113 | free(ctx); | |
114 | return SIGROK_ERR_MALLOC; | |
115 | } | |
116 | ||
d2b36a10 | 117 | snprintf(ctx->header, 511, "%s\n", PACKAGE_STRING); |
78ed6420 | 118 | num_probes = g_slist_length(o->device->probes); |
7aae7462 | 119 | if (o->device->plugin) { |
7aae7462 BV |
120 | samplerate = *((uint64_t *) o->device->plugin->get_device_info( |
121 | o->device->plugin_index, DI_CUR_SAMPLERATE)); | |
e734b81a UH |
122 | if (!(samplerate_s = sigrok_samplerate_string(samplerate))) { |
123 | free(ctx->header); | |
124 | free(ctx); | |
d2b36a10 | 125 | return SIGROK_ERR; |
e734b81a UH |
126 | } |
127 | snprintf(ctx->header + strlen(ctx->header), | |
128 | 511 - strlen(ctx->header), | |
129 | "Acquisition with %d/%d probes at %s\n", | |
130 | ctx->num_enabled_probes, num_probes, samplerate_s); | |
7aae7462 | 131 | free(samplerate_s); |
7aae7462 | 132 | } |
a1bb33af | 133 | |
ee5f5e81 | 134 | ctx->linebuf_len = ctx->samples_per_line * 2 + 4; |
e734b81a UH |
135 | if (!(ctx->linebuf = calloc(1, num_probes * ctx->linebuf_len))) { |
136 | free(ctx->header); | |
137 | free(ctx); | |
138 | return SIGROK_ERR_MALLOC; | |
139 | } | |
140 | if (!(ctx->linevalues = calloc(1, num_probes))) { | |
141 | free(ctx->header); | |
142 | free(ctx); | |
143 | return SIGROK_ERR_MALLOC; | |
144 | } | |
a1bb33af | 145 | |
e734b81a | 146 | return SIGROK_OK; |
a1bb33af UH |
147 | } |
148 | ||
99c1fc59 UH |
149 | static int event(struct output *o, int event_type, char **data_out, |
150 | uint64_t *length_out) | |
a1bb33af UH |
151 | { |
152 | struct context *ctx; | |
153 | int outsize; | |
154 | char *outbuf; | |
155 | ||
156 | ctx = o->internal; | |
99c1fc59 | 157 | switch (event_type) { |
a1bb33af | 158 | case DF_TRIGGER: |
5045c217 | 159 | ctx->mark_trigger = ctx->spl_cnt; |
78ed6420 BV |
160 | *data_out = NULL; |
161 | *length_out = 0; | |
a1bb33af UH |
162 | break; |
163 | case DF_END: | |
99c1fc59 UH |
164 | outsize = ctx->num_enabled_probes |
165 | * (ctx->samples_per_line + 20) + 512; | |
e734b81a UH |
166 | if (!(outbuf = calloc(1, outsize))) |
167 | return SIGROK_ERR_MALLOC; | |
afc8e4de | 168 | flush_linebufs(ctx, outbuf); |
a1bb33af UH |
169 | *data_out = outbuf; |
170 | *length_out = strlen(outbuf); | |
171 | free(o->internal); | |
172 | o->internal = NULL; | |
173 | break; | |
78ed6420 BV |
174 | default: |
175 | *data_out = NULL; | |
176 | *length_out = 0; | |
177 | break; | |
a1bb33af UH |
178 | } |
179 | ||
180 | return SIGROK_OK; | |
181 | } | |
182 | ||
02076d69 | 183 | static int init_bits(struct output *o) |
a1bb33af | 184 | { |
02076d69 | 185 | return init(o, DEFAULT_BPL_BITS); |
a1bb33af UH |
186 | } |
187 | ||
99c1fc59 UH |
188 | static int data_bits(struct output *o, char *data_in, uint64_t length_in, |
189 | char **data_out, uint64_t *length_out) | |
a1bb33af UH |
190 | { |
191 | struct context *ctx; | |
afc8e4de | 192 | unsigned int outsize, offset, p; |
33972913 | 193 | int max_linelen; |
a1bb33af | 194 | uint64_t sample; |
757b8c62 | 195 | char *outbuf, c; |
a1bb33af UH |
196 | |
197 | ctx = o->internal; | |
757b8c62 UH |
198 | max_linelen = MAX_PROBENAME_LEN + 3 + ctx->samples_per_line |
199 | + ctx->samples_per_line / 8; | |
9d7ab9ba HE |
200 | /* |
201 | * Calculate space needed for probes. Set aside 512 bytes for | |
202 | * extra output, e.g. trigger. | |
203 | */ | |
204 | outsize = 512 + (1 + (length_in / ctx->unitsize) / ctx->samples_per_line) | |
3aa403e8 | 205 | * (ctx->num_enabled_probes * max_linelen); |
e734b81a UH |
206 | |
207 | if (!(outbuf = calloc(1, outsize + 1))) | |
208 | return SIGROK_ERR_MALLOC; | |
209 | ||
210 | outbuf[0] = '\0'; | |
99c1fc59 UH |
211 | if (ctx->header) { |
212 | /* The header is still here, this must be the first packet. */ | |
a1bb33af UH |
213 | strncpy(outbuf, ctx->header, outsize); |
214 | free(ctx->header); | |
215 | ctx->header = NULL; | |
d4f228d0 HE |
216 | |
217 | /* Ensure first transition. */ | |
218 | memcpy(&ctx->prevsample, data_in, ctx->unitsize); | |
219 | ctx->prevsample = ~ctx->prevsample; | |
e734b81a | 220 | } |
a1bb33af | 221 | |
99c1fc59 UH |
222 | if (length_in >= ctx->unitsize) { |
223 | for (offset = 0; offset <= length_in - ctx->unitsize; | |
224 | offset += ctx->unitsize) { | |
a1bb33af | 225 | memcpy(&sample, data_in + offset, ctx->unitsize); |
99c1fc59 | 226 | for (p = 0; p < ctx->num_enabled_probes; p++) { |
757b8c62 UH |
227 | c = (sample & ((uint64_t) 1 << p)) ? '1' : '0'; |
228 | ctx->linebuf[p * ctx->linebuf_len + | |
229 | ctx->line_offset] = c; | |
a1bb33af UH |
230 | } |
231 | ctx->line_offset++; | |
232 | ctx->spl_cnt++; | |
233 | ||
99c1fc59 UH |
234 | /* Add a space every 8th bit. */ |
235 | if ((ctx->spl_cnt & 7) == 0) { | |
236 | for (p = 0; p < ctx->num_enabled_probes; p++) | |
237 | ctx->linebuf[p * ctx->linebuf_len + | |
238 | ctx->line_offset] = ' '; | |
a1bb33af UH |
239 | ctx->line_offset++; |
240 | } | |
241 | ||
99c1fc59 UH |
242 | /* End of line. */ |
243 | if (ctx->spl_cnt >= ctx->samples_per_line) { | |
afc8e4de | 244 | flush_linebufs(ctx, outbuf); |
a1bb33af | 245 | ctx->line_offset = ctx->spl_cnt = 0; |
5045c217 | 246 | ctx->mark_trigger = -1; |
a1bb33af UH |
247 | } |
248 | } | |
e734b81a | 249 | } else { |
99c1fc59 | 250 | g_message("short buffer (length_in=%" PRIu64 ")", length_in); |
e734b81a | 251 | } |
a1bb33af UH |
252 | |
253 | *data_out = outbuf; | |
254 | *length_out = strlen(outbuf); | |
255 | ||
256 | return SIGROK_OK; | |
257 | } | |
258 | ||
5a8fda15 | 259 | static int init_hex(struct output *o) |
a1bb33af | 260 | { |
f7606f9b | 261 | return init(o, DEFAULT_BPL_HEX); |
a1bb33af UH |
262 | } |
263 | ||
99c1fc59 UH |
264 | static int data_hex(struct output *o, char *data_in, uint64_t length_in, |
265 | char **data_out, uint64_t *length_out) | |
a1bb33af UH |
266 | { |
267 | struct context *ctx; | |
afc8e4de | 268 | unsigned int outsize, offset, p; |
33972913 | 269 | int max_linelen; |
a1bb33af UH |
270 | uint64_t sample; |
271 | char *outbuf; | |
272 | ||
273 | ctx = o->internal; | |
757b8c62 UH |
274 | max_linelen = MAX_PROBENAME_LEN + 3 + ctx->samples_per_line |
275 | + ctx->samples_per_line / 2; | |
276 | outsize = length_in / ctx->unitsize * ctx->num_enabled_probes | |
277 | / ctx->samples_per_line * max_linelen + 512; | |
e734b81a UH |
278 | |
279 | if (!(outbuf = calloc(1, outsize + 1))) | |
280 | return SIGROK_ERR_MALLOC; | |
281 | ||
282 | outbuf[0] = '\0'; | |
99c1fc59 UH |
283 | if (ctx->header) { |
284 | /* The header is still here, this must be the first packet. */ | |
a1bb33af UH |
285 | strncpy(outbuf, ctx->header, outsize); |
286 | free(ctx->header); | |
287 | ctx->header = NULL; | |
e734b81a | 288 | } |
a1bb33af UH |
289 | |
290 | ctx->line_offset = 0; | |
99c1fc59 UH |
291 | for (offset = 0; offset <= length_in - ctx->unitsize; |
292 | offset += ctx->unitsize) { | |
a1bb33af | 293 | memcpy(&sample, data_in + offset, ctx->unitsize); |
99c1fc59 | 294 | for (p = 0; p < ctx->num_enabled_probes; p++) { |
a1bb33af | 295 | ctx->linevalues[p] <<= 1; |
99c1fc59 UH |
296 | if (sample & ((uint64_t) 1 << p)) |
297 | ctx->linevalues[p] |= 1; | |
298 | sprintf(ctx->linebuf + (p * ctx->linebuf_len) + | |
299 | ctx->line_offset, "%.2x", ctx->linevalues[p]); | |
a1bb33af UH |
300 | } |
301 | ctx->spl_cnt++; | |
302 | ||
99c1fc59 UH |
303 | /* Add a space after every complete hex byte. */ |
304 | if ((ctx->spl_cnt & 7) == 0) { | |
305 | for (p = 0; p < ctx->num_enabled_probes; p++) | |
306 | ctx->linebuf[p * ctx->linebuf_len + | |
307 | ctx->line_offset + 2] = ' '; | |
a1bb33af UH |
308 | ctx->line_offset += 3; |
309 | } | |
310 | ||
99c1fc59 UH |
311 | /* End of line. */ |
312 | if (ctx->spl_cnt >= ctx->samples_per_line) { | |
afc8e4de | 313 | flush_linebufs(ctx, outbuf); |
a1bb33af UH |
314 | ctx->line_offset = ctx->spl_cnt = 0; |
315 | } | |
316 | } | |
317 | ||
318 | *data_out = outbuf; | |
319 | *length_out = strlen(outbuf); | |
320 | ||
321 | return SIGROK_OK; | |
322 | } | |
323 | ||
d4f228d0 HE |
324 | static int init_ascii(struct output *o) |
325 | { | |
326 | return init(o, DEFAULT_BPL_ASCII); | |
327 | } | |
328 | ||
329 | static int data_ascii(struct output *o, char *data_in, uint64_t length_in, | |
330 | char **data_out, uint64_t *length_out) | |
331 | { | |
332 | struct context *ctx; | |
333 | unsigned int outsize, offset, p; | |
334 | int max_linelen; | |
335 | uint64_t sample; | |
336 | char *outbuf, c; | |
337 | ||
338 | ctx = o->internal; | |
339 | max_linelen = MAX_PROBENAME_LEN + 3 + ctx->samples_per_line | |
340 | + ctx->samples_per_line / 8; | |
341 | /* | |
342 | * Calculate space needed for probes. Set aside 512 bytes for | |
343 | * extra output, e.g. trigger. | |
344 | */ | |
345 | outsize = 512 + (1 + (length_in / ctx->unitsize) / ctx->samples_per_line) | |
346 | * (ctx->num_enabled_probes * max_linelen); | |
347 | ||
348 | if (!(outbuf = calloc(1, outsize + 1))) | |
349 | return SIGROK_ERR_MALLOC; | |
350 | ||
351 | outbuf[0] = '\0'; | |
352 | if (ctx->header) { | |
353 | /* The header is still here, this must be the first packet. */ | |
354 | strncpy(outbuf, ctx->header, outsize); | |
355 | free(ctx->header); | |
356 | ctx->header = NULL; | |
357 | } | |
358 | ||
359 | if (length_in >= ctx->unitsize) { | |
360 | for (offset = 0; offset <= length_in - ctx->unitsize; | |
361 | offset += ctx->unitsize) { | |
362 | memcpy(&sample, data_in + offset, ctx->unitsize); | |
363 | ||
364 | for (p = 0; p < ctx->num_enabled_probes; p++) { | |
365 | uint64_t curbit = (sample & ((uint64_t) 1 << p)); | |
366 | uint64_t prevbit = (ctx->prevsample & | |
367 | ((uint64_t) 1 << p)); | |
368 | ||
369 | if (curbit < prevbit) { | |
370 | /* XXX: Does not draw \ at EOL. */ | |
371 | ctx->linebuf[p * ctx->linebuf_len + | |
372 | ctx->line_offset-1] = '\\'; | |
373 | } | |
374 | ||
375 | if (curbit > prevbit) | |
376 | c = '/'; | |
377 | else | |
378 | { | |
379 | if (curbit) | |
380 | c = '"'; | |
381 | else | |
382 | c = '.'; | |
383 | } | |
384 | ||
385 | ctx->linebuf[p * ctx->linebuf_len + | |
386 | ctx->line_offset] = c; | |
387 | } | |
388 | ctx->line_offset++; | |
389 | ctx->spl_cnt++; | |
390 | ||
391 | /* End of line. */ | |
392 | if (ctx->spl_cnt >= ctx->samples_per_line) { | |
393 | flush_linebufs(ctx, outbuf); | |
394 | ctx->line_offset = ctx->spl_cnt = 0; | |
395 | ctx->mark_trigger = -1; | |
396 | } | |
397 | ||
398 | ctx->prevsample = sample; | |
399 | } | |
400 | } else { | |
401 | g_message("short buffer (length_in=%" PRIu64 ")", length_in); | |
402 | } | |
403 | ||
404 | *data_out = outbuf; | |
405 | *length_out = strlen(outbuf); | |
406 | ||
407 | return SIGROK_OK; | |
408 | } | |
409 | ||
410 | ||
02076d69 | 411 | struct output_format output_text_bits = { |
1c5b9d30 | 412 | "bits", |
655756e0 | 413 | "Bits (takes argument, default 64)", |
f0411b1d | 414 | DF_LOGIC, |
02076d69 UH |
415 | init_bits, |
416 | data_bits, | |
99c1fc59 | 417 | event, |
a1bb33af UH |
418 | }; |
419 | ||
a1bb33af UH |
420 | struct output_format output_text_hex = { |
421 | "hex", | |
f7606f9b | 422 | "Hexadecimal (takes argument, default 192)", |
f0411b1d | 423 | DF_LOGIC, |
a1bb33af UH |
424 | init_hex, |
425 | data_hex, | |
99c1fc59 | 426 | event, |
a1bb33af | 427 | }; |
d4f228d0 HE |
428 | |
429 | struct output_format output_text_ascii = { | |
430 | "ascii", | |
431 | "ASCII (takes argument, default 74)", | |
432 | DF_LOGIC, | |
433 | init_ascii, | |
434 | data_ascii, | |
435 | event, | |
436 | }; |