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