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