]>
Commit | Line | Data |
---|---|---|
99eaa206 | 1 | /* |
50985c20 | 2 | * This file is part of the libsigrok project. |
99eaa206 | 3 | * |
0157808d | 4 | * Copyright (C) 2012 Petteri Aimonen <jpa@sr.mail.kapsi.fi> |
7db06394 | 5 | * Copyright (C) 2014 Bert Vermeulen <bert@biot.com> |
99eaa206 PA |
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 | ||
0157808d PA |
21 | /* The VCD input module has the following options: |
22 | * | |
ba7dd8bb | 23 | * numchannels: Maximum number of channels to use. The channels are |
0157808d PA |
24 | * detected in the same order as they are listed |
25 | * in the $var sections of the VCD file. | |
26 | * | |
27 | * skip: Allows skipping until given timestamp in the file. | |
28 | * This can speed up analyzing of long captures. | |
176d785d | 29 | * |
0157808d PA |
30 | * Value < 0: Skip until first timestamp listed in |
31 | * the file. (default) | |
32 | * | |
33 | * Value = 0: Do not skip, instead generate samples | |
34 | * beginning from timestamp 0. | |
35 | * | |
36 | * Value > 0: Start at the given timestamp. | |
37 | * | |
38 | * downsample: Divide the samplerate by the given factor. | |
39 | * This can speed up analyzing of long captures. | |
40 | * | |
6b7ace48 PA |
41 | * compress: Compress idle periods longer than this value. |
42 | * This can speed up analyzing of long captures. | |
43 | * Default 0 = don't compress. | |
44 | * | |
0157808d PA |
45 | * Based on Verilog standard IEEE Std 1364-2001 Version C |
46 | * | |
47 | * Supported features: | |
48 | * - $var with 'wire' and 'reg' types of scalar variables | |
49 | * - $timescale definition for samplerate | |
50 | * - multiple character variable identifiers | |
51 | * | |
52 | * Most important unsupported features: | |
53 | * - vector variables (bit vectors etc.) | |
54 | * - analog, integer and real number variables | |
55 | * - $dumpvars initial value declaration | |
8be87469 | 56 | * - $scope namespaces |
ba7dd8bb | 57 | * - more than 64 channels |
0157808d PA |
58 | */ |
59 | ||
6ec6c43b | 60 | #include <config.h> |
99eaa206 PA |
61 | #include <stdlib.h> |
62 | #include <glib.h> | |
63 | #include <stdio.h> | |
61a429c9 | 64 | #include <string.h> |
c1aae900 | 65 | #include <libsigrok/libsigrok.h> |
99eaa206 PA |
66 | #include "libsigrok-internal.h" |
67 | ||
3544f848 | 68 | #define LOG_PREFIX "input/vcd" |
99eaa206 | 69 | |
db0e5c99 | 70 | #define CHUNKSIZE (1024 * 1024) |
e4c8a4d7 BV |
71 | |
72 | struct context { | |
c10ef17c | 73 | gboolean started; |
7db06394 | 74 | gboolean got_header; |
f9bc17d4 | 75 | uint64_t prev_timestamp; |
e4c8a4d7 | 76 | uint64_t samplerate; |
7db06394 BV |
77 | unsigned int maxchannels; |
78 | unsigned int channelcount; | |
e4c8a4d7 BV |
79 | int downsample; |
80 | unsigned compress; | |
81 | int64_t skip; | |
7db06394 | 82 | gboolean skip_until_end; |
ba7dd8bb | 83 | GSList *channels; |
db0e5c99 PA |
84 | size_t bytes_per_sample; |
85 | size_t samples_in_buffer; | |
86 | uint8_t *buffer; | |
87 | uint8_t *current_levels; | |
e4c8a4d7 BV |
88 | }; |
89 | ||
ba7dd8bb | 90 | struct vcd_channel { |
e4c8a4d7 BV |
91 | gchar *name; |
92 | gchar *identifier; | |
93 | }; | |
94 | ||
e4c8a4d7 | 95 | /* |
7db06394 | 96 | * Reads a single VCD section from input file and parses it to name/contents. |
d9251a2c | 97 | * e.g. $timescale 1ps $end => "timescale" "1ps" |
99eaa206 | 98 | */ |
7db06394 | 99 | static gboolean parse_section(GString *buf, gchar **name, gchar **contents) |
99eaa206 | 100 | { |
7db06394 | 101 | GString *sname, *scontent; |
99eaa206 | 102 | gboolean status; |
7db06394 BV |
103 | unsigned int pos; |
104 | ||
105 | *name = *contents = NULL; | |
106 | status = FALSE; | |
107 | pos = 0; | |
cd1b0e8f | 108 | |
1f706c21 WS |
109 | /* Skip UTF8 BOM */ |
110 | if (buf->len >= 3 && !strncmp(buf->str, "\xef\xbb\xbf", 3)) | |
111 | pos = 3; | |
112 | ||
7db06394 BV |
113 | /* Skip any initial white-space. */ |
114 | while (pos < buf->len && g_ascii_isspace(buf->str[pos])) | |
115 | pos++; | |
cd1b0e8f | 116 | |
99eaa206 | 117 | /* Section tag should start with $. */ |
7db06394 | 118 | if (buf->str[pos++] != '$') |
99eaa206 | 119 | return FALSE; |
cd1b0e8f | 120 | |
99eaa206 | 121 | sname = g_string_sized_new(32); |
7db06394 BV |
122 | scontent = g_string_sized_new(128); |
123 | ||
124 | /* Read the section tag. */ | |
125 | while (pos < buf->len && !g_ascii_isspace(buf->str[pos])) | |
126 | g_string_append_c(sname, buf->str[pos++]); | |
127 | ||
128 | /* Skip whitespace before content. */ | |
129 | while (pos < buf->len && g_ascii_isspace(buf->str[pos])) | |
130 | pos++; | |
131 | ||
132 | /* Read the content. */ | |
133 | while (pos < buf->len - 4 && strncmp(buf->str + pos, "$end", 4)) | |
134 | g_string_append_c(scontent, buf->str[pos++]); | |
135 | ||
136 | if (sname->len && pos < buf->len - 4 && !strncmp(buf->str + pos, "$end", 4)) { | |
137 | status = TRUE; | |
138 | pos += 4; | |
139 | while (pos < buf->len && g_ascii_isspace(buf->str[pos])) | |
140 | pos++; | |
141 | g_string_erase(buf, 0, pos); | |
142 | } | |
99eaa206 | 143 | |
99eaa206 | 144 | *name = g_string_free(sname, !status); |
7db06394 BV |
145 | *contents = g_string_free(scontent, !status); |
146 | if (*contents) | |
147 | g_strchomp(*contents); | |
148 | ||
99eaa206 PA |
149 | return status; |
150 | } | |
151 | ||
ba7dd8bb | 152 | static void free_channel(void *data) |
db9679af | 153 | { |
ba7dd8bb UH |
154 | struct vcd_channel *vcd_ch = data; |
155 | g_free(vcd_ch->name); | |
156 | g_free(vcd_ch->identifier); | |
157 | g_free(vcd_ch); | |
db9679af ML |
158 | } |
159 | ||
99eaa206 PA |
160 | /* Remove empty parts from an array returned by g_strsplit. */ |
161 | static void remove_empty_parts(gchar **parts) | |
162 | { | |
163 | gchar **src = parts; | |
164 | gchar **dest = parts; | |
e4c8a4d7 | 165 | while (*src != NULL) { |
99eaa206 | 166 | if (**src != '\0') |
99eaa206 | 167 | *dest++ = *src; |
99eaa206 PA |
168 | src++; |
169 | } | |
cd1b0e8f | 170 | |
99eaa206 PA |
171 | *dest = NULL; |
172 | } | |
173 | ||
e4c8a4d7 BV |
174 | /* |
175 | * Parse VCD header to get values for context structure. | |
99eaa206 PA |
176 | * The context structure should be zeroed before calling this. |
177 | */ | |
7db06394 | 178 | static gboolean parse_header(const struct sr_input *in, GString *buf) |
99eaa206 | 179 | { |
ba7dd8bb | 180 | struct vcd_channel *vcd_ch; |
7db06394 BV |
181 | uint64_t p, q; |
182 | struct context *inc; | |
183 | gboolean status; | |
184 | gchar *name, *contents, **parts; | |
99eaa206 | 185 | |
7db06394 BV |
186 | inc = in->priv; |
187 | name = contents = NULL; | |
188 | status = FALSE; | |
189 | while (parse_section(buf, &name, &contents)) { | |
99eaa206 | 190 | sr_dbg("Section '%s', contents '%s'.", name, contents); |
cd1b0e8f | 191 | |
e4c8a4d7 | 192 | if (g_strcmp0(name, "enddefinitions") == 0) { |
99eaa206 PA |
193 | status = TRUE; |
194 | break; | |
e4c8a4d7 BV |
195 | } else if (g_strcmp0(name, "timescale") == 0) { |
196 | /* | |
197 | * The standard allows for values 1, 10 or 100 | |
198 | * and units s, ms, us, ns, ps and fs. | |
7db06394 | 199 | */ |
e4c8a4d7 | 200 | if (sr_parse_period(contents, &p, &q) == SR_OK) { |
7db06394 | 201 | inc->samplerate = q / p; |
e4c8a4d7 | 202 | if (q % p != 0) { |
99eaa206 | 203 | /* Does not happen unless time value is non-standard */ |
0157808d | 204 | sr_warn("Inexact rounding of samplerate, %" PRIu64 " / %" PRIu64 " to %" PRIu64 " Hz.", |
7db06394 | 205 | q, p, inc->samplerate); |
99eaa206 | 206 | } |
cd1b0e8f | 207 | |
7db06394 | 208 | sr_dbg("Samplerate: %" PRIu64, inc->samplerate); |
e4c8a4d7 | 209 | } else { |
99eaa206 PA |
210 | sr_err("Parsing timescale failed."); |
211 | } | |
e4c8a4d7 | 212 | } else if (g_strcmp0(name, "var") == 0) { |
e85e550d WS |
213 | /* Format: $var type size identifier reference [opt. index] $end */ |
214 | unsigned int length; | |
215 | ||
7db06394 | 216 | parts = g_strsplit_set(contents, " \r\n\t", 0); |
99eaa206 | 217 | remove_empty_parts(parts); |
e85e550d | 218 | length = g_strv_length(parts); |
cd1b0e8f | 219 | |
e85e550d WS |
220 | if (length != 4 && length != 5) |
221 | sr_warn("$var section should have 4 or 5 items"); | |
99eaa206 | 222 | else if (g_strcmp0(parts[0], "reg") != 0 && g_strcmp0(parts[0], "wire") != 0) |
8be87469 | 223 | sr_info("Unsupported signal type: '%s'", parts[0]); |
99eaa206 | 224 | else if (strtol(parts[1], NULL, 10) != 1) |
8be87469 | 225 | sr_info("Unsupported signal size: '%s'", parts[1]); |
3f48fc82 | 226 | else if (inc->maxchannels && inc->channelcount >= inc->maxchannels) |
e85e550d WS |
227 | sr_warn("Skipping '%s%s' because only %d channels requested.", |
228 | parts[3], parts[4] ? : "", inc->maxchannels); | |
e4c8a4d7 | 229 | else { |
ba7dd8bb UH |
230 | vcd_ch = g_malloc(sizeof(struct vcd_channel)); |
231 | vcd_ch->identifier = g_strdup(parts[2]); | |
e85e550d WS |
232 | if (length == 4) |
233 | vcd_ch->name = g_strdup(parts[3]); | |
234 | else | |
235 | vcd_ch->name = g_strconcat(parts[3], parts[4], NULL); | |
236 | ||
237 | sr_info("Channel %d is '%s' identified by '%s'.", | |
238 | inc->channelcount, vcd_ch->name, vcd_ch->identifier); | |
239 | ||
ab464eb3 | 240 | sr_channel_new(in->sdi, inc->channelcount++, SR_CHANNEL_LOGIC, TRUE, vcd_ch->name); |
7db06394 | 241 | inc->channels = g_slist_append(inc->channels, vcd_ch); |
99eaa206 | 242 | } |
cd1b0e8f | 243 | |
99eaa206 PA |
244 | g_strfreev(parts); |
245 | } | |
cd1b0e8f | 246 | |
db0e5c99 PA |
247 | g_free(name); |
248 | name = NULL; | |
249 | g_free(contents); | |
250 | contents = NULL; | |
99eaa206 | 251 | } |
99eaa206 PA |
252 | g_free(name); |
253 | g_free(contents); | |
cd1b0e8f | 254 | |
db0e5c99 PA |
255 | /* |
256 | * Compute how many bytes each sample will have and initialize the | |
257 | * current levels. The current levels will be updated whenever VCD | |
258 | * has changes. | |
259 | */ | |
260 | inc->bytes_per_sample = (inc->channelcount + 7) / 8; | |
261 | inc->current_levels = g_malloc0(inc->bytes_per_sample); | |
262 | ||
7db06394 BV |
263 | inc->got_header = status; |
264 | ||
99eaa206 PA |
265 | return status; |
266 | } | |
267 | ||
7db06394 | 268 | static int format_match(GHashTable *metadata) |
99eaa206 | 269 | { |
7db06394 | 270 | GString *buf, *tmpbuf; |
99eaa206 | 271 | gboolean status; |
7db06394 | 272 | gchar *name, *contents; |
cd1b0e8f | 273 | |
7db06394 BV |
274 | buf = g_hash_table_lookup(metadata, GINT_TO_POINTER(SR_INPUT_META_HEADER)); |
275 | tmpbuf = g_string_new_len(buf->str, buf->len); | |
99eaa206 | 276 | |
e4c8a4d7 BV |
277 | /* |
278 | * If we can parse the first section correctly, | |
99eaa206 PA |
279 | * then it is assumed to be a VCD file. |
280 | */ | |
7db06394 BV |
281 | status = parse_section(tmpbuf, &name, &contents); |
282 | g_string_free(tmpbuf, TRUE); | |
99eaa206 PA |
283 | g_free(name); |
284 | g_free(contents); | |
cd1b0e8f | 285 | |
4f979115 | 286 | return status ? SR_OK : SR_ERR; |
99eaa206 PA |
287 | } |
288 | ||
db0e5c99 PA |
289 | /* Send all accumulated bytes from inc->buffer. */ |
290 | static void send_buffer(const struct sr_input *in) | |
61a429c9 | 291 | { |
db0e5c99 | 292 | struct context *inc; |
61a429c9 PA |
293 | struct sr_datafeed_packet packet; |
294 | struct sr_datafeed_logic logic; | |
cd1b0e8f | 295 | |
db0e5c99 | 296 | inc = in->priv; |
61a429c9 | 297 | |
db0e5c99 PA |
298 | if (inc->samples_in_buffer == 0) |
299 | return; | |
cd1b0e8f | 300 | |
61a429c9 | 301 | packet.type = SR_DF_LOGIC; |
cd1b0e8f | 302 | packet.payload = &logic; |
db0e5c99 PA |
303 | logic.unitsize = inc->bytes_per_sample; |
304 | logic.data = inc->buffer; | |
305 | logic.length = inc->bytes_per_sample * inc->samples_in_buffer; | |
306 | sr_session_send(in->sdi, &packet); | |
307 | inc->samples_in_buffer = 0; | |
308 | } | |
309 | ||
310 | /* | |
311 | * Add N copies of the current sample to buffer. | |
312 | * When the buffer fills up, automatically send it. | |
313 | */ | |
314 | static void add_samples(const struct sr_input *in, size_t count) | |
315 | { | |
316 | struct context *inc; | |
317 | size_t samples_per_chunk; | |
318 | size_t space_left, i; | |
319 | uint8_t *p; | |
320 | ||
321 | inc = in->priv; | |
322 | samples_per_chunk = CHUNKSIZE / inc->bytes_per_sample; | |
cd1b0e8f | 323 | |
e4c8a4d7 | 324 | while (count) { |
db0e5c99 PA |
325 | space_left = samples_per_chunk - inc->samples_in_buffer; |
326 | ||
327 | if (space_left > count) | |
328 | space_left = count; | |
cd1b0e8f | 329 | |
db0e5c99 PA |
330 | p = inc->buffer + inc->samples_in_buffer * inc->bytes_per_sample; |
331 | for (i = 0; i < space_left; i++) { | |
332 | memcpy(p, inc->current_levels, inc->bytes_per_sample); | |
333 | p += inc->bytes_per_sample; | |
334 | inc->samples_in_buffer++; | |
335 | count--; | |
336 | } | |
cd1b0e8f | 337 | |
db0e5c99 PA |
338 | if (inc->samples_in_buffer == samples_per_chunk) |
339 | send_buffer(in); | |
61a429c9 PA |
340 | } |
341 | } | |
342 | ||
36dacf17 WS |
343 | /* Set the channel level depending on the identifier and parsed value. */ |
344 | static void process_bit(struct context *inc, char *identifier, unsigned int bit) | |
345 | { | |
346 | GSList *l; | |
347 | struct vcd_channel *vcd_ch; | |
348 | unsigned int j; | |
349 | ||
350 | for (j = 0, l = inc->channels; j < inc->channelcount && l; j++, l = l->next) { | |
351 | vcd_ch = l->data; | |
352 | if (g_strcmp0(identifier, vcd_ch->identifier) == 0) { | |
353 | /* Found our channel. */ | |
354 | size_t byte_idx = (j / 8); | |
355 | size_t bit_idx = j - 8 * byte_idx; | |
356 | if (bit) | |
357 | inc->current_levels[byte_idx] |= (uint8_t)1 << bit_idx; | |
358 | else | |
359 | inc->current_levels[byte_idx] &= ~((uint8_t)1 << bit_idx); | |
360 | break; | |
361 | } | |
362 | } | |
363 | if (j == inc->channelcount) | |
364 | sr_dbg("Did not find channel for identifier '%s'.", identifier); | |
365 | } | |
366 | ||
7db06394 BV |
367 | /* Parse a set of lines from the data section. */ |
368 | static void parse_contents(const struct sr_input *in, char *data) | |
61a429c9 | 369 | { |
7db06394 | 370 | struct context *inc; |
f9bc17d4 | 371 | uint64_t timestamp; |
36dacf17 | 372 | unsigned int bit, i; |
7db06394 | 373 | char **tokens; |
cd1b0e8f | 374 | |
7db06394 | 375 | inc = in->priv; |
cd1b0e8f | 376 | |
61a429c9 | 377 | /* Read one space-delimited token at a time. */ |
7db06394 BV |
378 | tokens = g_strsplit_set(data, " \t\r\n", 0); |
379 | remove_empty_parts(tokens); | |
380 | for (i = 0; tokens[i]; i++) { | |
381 | if (inc->skip_until_end) { | |
382 | if (!strcmp(tokens[i], "$end")) { | |
383 | /* Done with unhandled/unknown section. */ | |
384 | inc->skip_until_end = FALSE; | |
385 | break; | |
386 | } | |
387 | } | |
388 | if (tokens[i][0] == '#' && g_ascii_isdigit(tokens[i][1])) { | |
61a429c9 | 389 | /* Numeric value beginning with # is a new timestamp value */ |
7db06394 | 390 | timestamp = strtoull(tokens[i] + 1, NULL, 10); |
cd1b0e8f | 391 | |
7db06394 BV |
392 | if (inc->downsample > 1) |
393 | timestamp /= inc->downsample; | |
cd1b0e8f | 394 | |
e4c8a4d7 BV |
395 | /* |
396 | * Skip < 0 => skip until first timestamp. | |
0157808d PA |
397 | * Skip = 0 => don't skip |
398 | * Skip > 0 => skip until timestamp >= skip. | |
399 | */ | |
7db06394 BV |
400 | if (inc->skip < 0) { |
401 | inc->skip = timestamp; | |
f9bc17d4 | 402 | inc->prev_timestamp = timestamp; |
7db06394 | 403 | } else if (inc->skip > 0 && timestamp < (uint64_t)inc->skip) { |
f9bc17d4 GS |
404 | inc->prev_timestamp = inc->skip; |
405 | } else if (timestamp == inc->prev_timestamp) { | |
8be87469 | 406 | /* Ignore repeated timestamps (e.g. sigrok outputs these) */ |
7db06394 | 407 | } else { |
f9bc17d4 | 408 | if (inc->compress != 0 && timestamp - inc->prev_timestamp > inc->compress) { |
6b7ace48 | 409 | /* Compress long idle periods */ |
f9bc17d4 | 410 | inc->prev_timestamp = timestamp - inc->compress; |
6b7ace48 | 411 | } |
cd1b0e8f | 412 | |
61a429c9 | 413 | sr_dbg("New timestamp: %" PRIu64, timestamp); |
cd1b0e8f | 414 | |
61a429c9 | 415 | /* Generate samples from prev_timestamp up to timestamp - 1. */ |
f9bc17d4 GS |
416 | add_samples(in, timestamp - inc->prev_timestamp); |
417 | inc->prev_timestamp = timestamp; | |
61a429c9 | 418 | } |
7db06394 BV |
419 | } else if (tokens[i][0] == '$' && tokens[i][1] != '\0') { |
420 | /* | |
421 | * This is probably a $dumpvars, $comment or similar. | |
422 | * $dump* contain useful data. | |
423 | */ | |
424 | if (g_strcmp0(tokens[i], "$dumpvars") == 0 | |
425 | || g_strcmp0(tokens[i], "$dumpon") == 0 | |
426 | || g_strcmp0(tokens[i], "$dumpoff") == 0 | |
427 | || g_strcmp0(tokens[i], "$end") == 0) { | |
8be87469 | 428 | /* Ignore, parse contents as normally. */ |
e4c8a4d7 | 429 | } else { |
7db06394 BV |
430 | /* Ignore this and future lines until $end. */ |
431 | inc->skip_until_end = TRUE; | |
432 | break; | |
8be87469 | 433 | } |
34724ffa WS |
434 | } else if (strchr("rR", tokens[i][0]) != NULL) { |
435 | sr_dbg("Real type vector values not supported yet!"); | |
76bc28c3 WS |
436 | if (!tokens[++i]) |
437 | /* No tokens left, bail out */ | |
438 | break; | |
439 | else | |
440 | /* Process next token */ | |
441 | continue; | |
34724ffa WS |
442 | } else if (strchr("bB", tokens[i][0]) != NULL) { |
443 | bit = (tokens[i][1] == '1'); | |
444 | ||
445 | /* | |
446 | * Bail out if a) char after 'b' is NUL, or b) there is | |
447 | * a second character after 'b', or c) there is no | |
448 | * identifier. | |
449 | */ | |
450 | if (!tokens[i][1] || tokens[i][2] || !tokens[++i]) { | |
451 | sr_dbg("Unexpected vector format!"); | |
452 | break; | |
453 | } | |
454 | ||
455 | process_bit(inc, tokens[i], bit); | |
7db06394 | 456 | } else if (strchr("01xXzZ", tokens[i][0]) != NULL) { |
a66175c2 WS |
457 | char *identifier; |
458 | ||
61a429c9 | 459 | /* A new 1-bit sample value */ |
7db06394 BV |
460 | bit = (tokens[i][0] == '1'); |
461 | ||
462 | /* | |
463 | * The identifier is either the next character, or, if | |
464 | * there was whitespace after the bit, the next token. | |
465 | */ | |
466 | if (tokens[i][1] == '\0') { | |
73f052d3 WS |
467 | if (!tokens[++i]) { |
468 | sr_dbg("Identifier missing!"); | |
469 | break; | |
470 | } | |
a66175c2 | 471 | identifier = tokens[i]; |
7db06394 | 472 | } else { |
a66175c2 | 473 | identifier = tokens[i] + 1; |
61a429c9 | 474 | } |
36dacf17 | 475 | process_bit(inc, identifier, bit); |
e4c8a4d7 | 476 | } else { |
7db06394 | 477 | sr_warn("Skipping unknown token '%s'.", tokens[i]); |
8be87469 | 478 | } |
7db06394 BV |
479 | } |
480 | g_strfreev(tokens); | |
481 | } | |
482 | ||
483 | static int init(struct sr_input *in, GHashTable *options) | |
484 | { | |
7db06394 BV |
485 | struct context *inc; |
486 | ||
d0181813 | 487 | inc = in->priv = g_malloc0(sizeof(struct context)); |
cd1b0e8f | 488 | |
3f48fc82 | 489 | inc->maxchannels = g_variant_get_int32(g_hash_table_lookup(options, "numchannels")); |
7db06394 BV |
490 | inc->downsample = g_variant_get_int32(g_hash_table_lookup(options, "downsample")); |
491 | if (inc->downsample < 1) | |
492 | inc->downsample = 1; | |
493 | ||
494 | inc->compress = g_variant_get_int32(g_hash_table_lookup(options, "compress")); | |
495 | inc->skip = g_variant_get_int32(g_hash_table_lookup(options, "skip")); | |
496 | inc->skip /= inc->downsample; | |
497 | ||
aac29cc1 | 498 | in->sdi = g_malloc0(sizeof(struct sr_dev_inst)); |
7db06394 BV |
499 | in->priv = inc; |
500 | ||
db0e5c99 PA |
501 | inc->buffer = g_malloc(CHUNKSIZE); |
502 | ||
7db06394 | 503 | return SR_OK; |
61a429c9 PA |
504 | } |
505 | ||
7db06394 BV |
506 | static gboolean have_header(GString *buf) |
507 | { | |
508 | unsigned int pos; | |
509 | char *p; | |
510 | ||
511 | if (!(p = g_strstr_len(buf->str, buf->len, "$enddefinitions"))) | |
512 | return FALSE; | |
513 | pos = p - buf->str + 15; | |
514 | while (pos < buf->len - 4 && g_ascii_isspace(buf->str[pos])) | |
515 | pos++; | |
516 | if (!strncmp(buf->str + pos, "$end", 4)) | |
517 | return TRUE; | |
518 | ||
519 | return FALSE; | |
520 | } | |
521 | ||
7066fd46 | 522 | static int process_buffer(struct sr_input *in) |
99eaa206 | 523 | { |
99eaa206 | 524 | struct sr_datafeed_packet packet; |
2df1e819 BV |
525 | struct sr_datafeed_meta meta; |
526 | struct sr_config *src; | |
7db06394 | 527 | struct context *inc; |
2df1e819 | 528 | uint64_t samplerate; |
7db06394 BV |
529 | char *p; |
530 | ||
7db06394 | 531 | inc = in->priv; |
d0181813 | 532 | if (!inc->started) { |
bee2b016 | 533 | std_session_send_df_header(in->sdi); |
99eaa206 | 534 | |
7db06394 BV |
535 | packet.type = SR_DF_META; |
536 | packet.payload = &meta; | |
537 | samplerate = inc->samplerate / inc->downsample; | |
538 | src = sr_config_new(SR_CONF_SAMPLERATE, g_variant_new_uint64(samplerate)); | |
539 | meta.config = g_slist_append(NULL, src); | |
540 | sr_session_send(in->sdi, &packet); | |
c01378c9 | 541 | g_slist_free(meta.config); |
7db06394 | 542 | sr_config_free(src); |
d0181813 BV |
543 | |
544 | inc->started = TRUE; | |
99eaa206 PA |
545 | } |
546 | ||
7db06394 BV |
547 | while ((p = g_strrstr_len(in->buf->str, in->buf->len, "\n"))) { |
548 | *p = '\0'; | |
549 | g_strstrip(in->buf->str); | |
550 | if (in->buf->str[0] != '\0') | |
551 | parse_contents(in, in->buf->str); | |
552 | g_string_erase(in->buf, 0, p - in->buf->str + 1); | |
553 | } | |
99eaa206 | 554 | |
7db06394 BV |
555 | return SR_OK; |
556 | } | |
cd1b0e8f | 557 | |
7066fd46 BV |
558 | static int receive(struct sr_input *in, GString *buf) |
559 | { | |
560 | struct context *inc; | |
561 | int ret; | |
562 | ||
563 | g_string_append_len(in->buf, buf->str, buf->len); | |
564 | ||
565 | inc = in->priv; | |
566 | if (!inc->got_header) { | |
567 | if (!have_header(in->buf)) | |
568 | return SR_OK; | |
73931b7c | 569 | if (!parse_header(in, in->buf)) |
7066fd46 BV |
570 | /* There was a header in there, but it was malformed. */ |
571 | return SR_ERR; | |
572 | ||
573 | in->sdi_ready = TRUE; | |
574 | /* sdi is ready, notify frontend. */ | |
575 | return SR_OK; | |
576 | } | |
577 | ||
578 | ret = process_buffer(in); | |
579 | ||
580 | return ret; | |
581 | } | |
582 | ||
583 | static int end(struct sr_input *in) | |
7db06394 BV |
584 | { |
585 | struct context *inc; | |
7066fd46 BV |
586 | int ret; |
587 | ||
db0e5c99 PA |
588 | inc = in->priv; |
589 | ||
7066fd46 BV |
590 | if (in->sdi_ready) |
591 | ret = process_buffer(in); | |
592 | else | |
593 | ret = SR_OK; | |
99eaa206 | 594 | |
db0e5c99 PA |
595 | /* Send any samples that haven't been sent yet. */ |
596 | send_buffer(in); | |
597 | ||
3be42bc2 | 598 | if (inc->started) |
bee2b016 | 599 | std_session_send_df_end(in->sdi); |
c10ef17c | 600 | |
7066fd46 BV |
601 | return ret; |
602 | } | |
603 | ||
d5cc282f | 604 | static void cleanup(struct sr_input *in) |
7066fd46 BV |
605 | { |
606 | struct context *inc; | |
607 | ||
608 | inc = in->priv; | |
7db06394 | 609 | g_slist_free_full(inc->channels, free_channel); |
db0e5c99 PA |
610 | g_free(inc->buffer); |
611 | inc->buffer = NULL; | |
612 | g_free(inc->current_levels); | |
613 | inc->current_levels = NULL; | |
99eaa206 PA |
614 | } |
615 | ||
f4b4725b SA |
616 | static int reset(struct sr_input *in) |
617 | { | |
618 | struct context *inc = in->priv; | |
619 | ||
620 | cleanup(in); | |
621 | inc->started = FALSE; | |
622 | g_string_truncate(in->buf, 0); | |
623 | ||
624 | return SR_OK; | |
625 | } | |
626 | ||
7db06394 | 627 | static struct sr_option options[] = { |
5e83cd74 | 628 | { "numchannels", "Number of channels", "Number of channels", NULL, NULL }, |
7db06394 BV |
629 | { "skip", "Skip", "Skip until timestamp", NULL, NULL }, |
630 | { "downsample", "Downsample", "Divide samplerate by factor", NULL, NULL }, | |
631 | { "compress", "Compress", "Compress idle periods longer than this value", NULL, NULL }, | |
06ad20be | 632 | ALL_ZERO |
7db06394 BV |
633 | }; |
634 | ||
2c240774 | 635 | static const struct sr_option *get_options(void) |
7db06394 BV |
636 | { |
637 | if (!options[0].def) { | |
3f48fc82 | 638 | options[0].def = g_variant_ref_sink(g_variant_new_int32(0)); |
7db06394 BV |
639 | options[1].def = g_variant_ref_sink(g_variant_new_int32(-1)); |
640 | options[2].def = g_variant_ref_sink(g_variant_new_int32(1)); | |
641 | options[3].def = g_variant_ref_sink(g_variant_new_int32(0)); | |
642 | } | |
643 | ||
644 | return options; | |
645 | } | |
646 | ||
d4c93774 | 647 | SR_PRIV struct sr_input_module input_vcd = { |
99eaa206 | 648 | .id = "vcd", |
7db06394 | 649 | .name = "VCD", |
17bfaca6 | 650 | .desc = "Value Change Dump", |
c7bc82ff | 651 | .exts = (const char*[]){"vcd", NULL}, |
7db06394 BV |
652 | .metadata = { SR_INPUT_META_HEADER | SR_INPUT_META_REQUIRED }, |
653 | .options = get_options, | |
99eaa206 PA |
654 | .format_match = format_match, |
655 | .init = init, | |
7db06394 | 656 | .receive = receive, |
7066fd46 | 657 | .end = end, |
7db06394 | 658 | .cleanup = cleanup, |
f4b4725b | 659 | .reset = reset, |
99eaa206 | 660 | }; |