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