]>
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 UH |
27 | #define DEFAULT_BPL_BITS 64 |
28 | #define DEFAULT_BPL_HEX 256 | |
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; |
99c1fc59 | 91 | if (probe->enabled) |
a1bb33af UH |
92 | ctx->probelist[ctx->num_enabled_probes++] = probe->name; |
93 | } | |
99c1fc59 | 94 | |
a1bb33af UH |
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; | |
5045c217 | 99 | ctx->mark_trigger = -1; |
99c1fc59 | 100 | |
ee5f5e81 | 101 | if (o->param && o->param[0]) { |
a1bb33af | 102 | ctx->samples_per_line = strtoul(o->param, NULL, 10); |
ee5f5e81 BV |
103 | if (ctx->samples_per_line < 1) |
104 | return SIGROK_ERR; | |
105 | } else | |
a1bb33af UH |
106 | ctx->samples_per_line = default_spl; |
107 | ||
e734b81a UH |
108 | if (!(ctx->header = malloc(512))) { |
109 | free(ctx); | |
110 | return SIGROK_ERR_MALLOC; | |
111 | } | |
112 | ||
d2b36a10 | 113 | snprintf(ctx->header, 511, "%s\n", PACKAGE_STRING); |
7aae7462 | 114 | if (o->device->plugin) { |
7aae7462 BV |
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)); | |
e734b81a UH |
118 | if (!(samplerate_s = sigrok_samplerate_string(samplerate))) { |
119 | free(ctx->header); | |
120 | free(ctx); | |
d2b36a10 | 121 | return SIGROK_ERR; |
e734b81a UH |
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); | |
7aae7462 | 127 | free(samplerate_s); |
7aae7462 | 128 | } |
a1bb33af | 129 | |
ee5f5e81 | 130 | ctx->linebuf_len = ctx->samples_per_line * 2 + 4; |
e734b81a UH |
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 | } | |
a1bb33af | 141 | |
e734b81a | 142 | return SIGROK_OK; |
a1bb33af UH |
143 | } |
144 | ||
99c1fc59 UH |
145 | static int event(struct output *o, int event_type, char **data_out, |
146 | uint64_t *length_out) | |
a1bb33af UH |
147 | { |
148 | struct context *ctx; | |
149 | int outsize; | |
150 | char *outbuf; | |
151 | ||
152 | ctx = o->internal; | |
99c1fc59 | 153 | switch (event_type) { |
a1bb33af | 154 | case DF_TRIGGER: |
5045c217 | 155 | ctx->mark_trigger = ctx->spl_cnt; |
a1bb33af UH |
156 | break; |
157 | case DF_END: | |
99c1fc59 UH |
158 | outsize = ctx->num_enabled_probes |
159 | * (ctx->samples_per_line + 20) + 512; | |
e734b81a UH |
160 | if (!(outbuf = calloc(1, outsize))) |
161 | return SIGROK_ERR_MALLOC; | |
afc8e4de | 162 | flush_linebufs(ctx, outbuf); |
a1bb33af UH |
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 | ||
02076d69 | 173 | static int init_bits(struct output *o) |
a1bb33af | 174 | { |
02076d69 | 175 | return init(o, DEFAULT_BPL_BITS); |
a1bb33af UH |
176 | } |
177 | ||
99c1fc59 UH |
178 | static int data_bits(struct output *o, char *data_in, uint64_t length_in, |
179 | char **data_out, uint64_t *length_out) | |
a1bb33af UH |
180 | { |
181 | struct context *ctx; | |
afc8e4de | 182 | unsigned int outsize, offset, p; |
a1bb33af UH |
183 | uint64_t sample; |
184 | char *outbuf; | |
185 | ||
186 | ctx = o->internal; | |
99c1fc59 | 187 | outsize = length_in / ctx->unitsize * ctx->num_enabled_probes * |
ee5f5e81 | 188 | ctx->samples_per_line + 4096; |
e734b81a UH |
189 | |
190 | if (!(outbuf = calloc(1, outsize + 1))) | |
191 | return SIGROK_ERR_MALLOC; | |
192 | ||
193 | outbuf[0] = '\0'; | |
99c1fc59 UH |
194 | if (ctx->header) { |
195 | /* The header is still here, this must be the first packet. */ | |
a1bb33af UH |
196 | strncpy(outbuf, ctx->header, outsize); |
197 | free(ctx->header); | |
198 | ctx->header = NULL; | |
e734b81a | 199 | } |
a1bb33af | 200 | |
99c1fc59 UH |
201 | if (length_in >= ctx->unitsize) { |
202 | for (offset = 0; offset <= length_in - ctx->unitsize; | |
203 | offset += ctx->unitsize) { | |
a1bb33af | 204 | memcpy(&sample, data_in + offset, ctx->unitsize); |
99c1fc59 UH |
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'; | |
a1bb33af | 209 | else |
99c1fc59 UH |
210 | ctx->linebuf[p * ctx->linebuf_len + |
211 | ctx->line_offset] = '0'; | |
a1bb33af UH |
212 | } |
213 | ctx->line_offset++; | |
214 | ctx->spl_cnt++; | |
215 | ||
99c1fc59 UH |
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] = ' '; | |
a1bb33af UH |
221 | ctx->line_offset++; |
222 | } | |
223 | ||
99c1fc59 UH |
224 | /* End of line. */ |
225 | if (ctx->spl_cnt >= ctx->samples_per_line) { | |
afc8e4de | 226 | flush_linebufs(ctx, outbuf); |
a1bb33af | 227 | ctx->line_offset = ctx->spl_cnt = 0; |
5045c217 | 228 | ctx->mark_trigger = -1; |
a1bb33af UH |
229 | } |
230 | } | |
e734b81a | 231 | } else { |
99c1fc59 | 232 | g_message("short buffer (length_in=%" PRIu64 ")", length_in); |
e734b81a | 233 | } |
a1bb33af UH |
234 | |
235 | *data_out = outbuf; | |
236 | *length_out = strlen(outbuf); | |
237 | ||
238 | return SIGROK_OK; | |
239 | } | |
240 | ||
5a8fda15 | 241 | static int init_hex(struct output *o) |
a1bb33af | 242 | { |
02076d69 | 243 | return init(o, DEFAULT_BPL_BITS); |
a1bb33af UH |
244 | } |
245 | ||
99c1fc59 UH |
246 | static int data_hex(struct output *o, char *data_in, uint64_t length_in, |
247 | char **data_out, uint64_t *length_out) | |
a1bb33af UH |
248 | { |
249 | struct context *ctx; | |
afc8e4de | 250 | unsigned int outsize, offset, p; |
a1bb33af UH |
251 | uint64_t sample; |
252 | char *outbuf; | |
253 | ||
254 | ctx = o->internal; | |
99c1fc59 | 255 | outsize = length_in / ctx->unitsize * ctx->num_enabled_probes * |
ee5f5e81 | 256 | ctx->samples_per_line + 4096; |
e734b81a UH |
257 | |
258 | if (!(outbuf = calloc(1, outsize + 1))) | |
259 | return SIGROK_ERR_MALLOC; | |
260 | ||
261 | outbuf[0] = '\0'; | |
99c1fc59 UH |
262 | if (ctx->header) { |
263 | /* The header is still here, this must be the first packet. */ | |
a1bb33af UH |
264 | strncpy(outbuf, ctx->header, outsize); |
265 | free(ctx->header); | |
266 | ctx->header = NULL; | |
e734b81a | 267 | } |
a1bb33af UH |
268 | |
269 | ctx->line_offset = 0; | |
99c1fc59 UH |
270 | for (offset = 0; offset <= length_in - ctx->unitsize; |
271 | offset += ctx->unitsize) { | |
a1bb33af | 272 | memcpy(&sample, data_in + offset, ctx->unitsize); |
99c1fc59 | 273 | for (p = 0; p < ctx->num_enabled_probes; p++) { |
a1bb33af | 274 | ctx->linevalues[p] <<= 1; |
99c1fc59 UH |
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]); | |
a1bb33af UH |
279 | } |
280 | ctx->spl_cnt++; | |
281 | ||
99c1fc59 UH |
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] = ' '; | |
a1bb33af UH |
287 | ctx->line_offset += 3; |
288 | } | |
289 | ||
99c1fc59 UH |
290 | /* End of line. */ |
291 | if (ctx->spl_cnt >= ctx->samples_per_line) { | |
afc8e4de | 292 | flush_linebufs(ctx, outbuf); |
a1bb33af UH |
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 | ||
02076d69 | 303 | struct output_format output_text_bits = { |
1c5b9d30 UH |
304 | "bits", |
305 | "Text (bits)", | |
02076d69 UH |
306 | init_bits, |
307 | data_bits, | |
99c1fc59 | 308 | event, |
a1bb33af UH |
309 | }; |
310 | ||
a1bb33af UH |
311 | struct output_format output_text_hex = { |
312 | "hex", | |
313 | "Text (hexadecimal)", | |
314 | init_hex, | |
315 | data_hex, | |
99c1fc59 | 316 | event, |
a1bb33af | 317 | }; |