]>
Commit | Line | Data |
---|---|---|
4a35548b MS |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013 Marc Schink <sigrok-dev@marcschink.de> | |
e53f32d2 | 5 | * Copyright (C) 2019 Gerhard Sittig <gerhard.sittig@gmx.net> |
4a35548b MS |
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 | ||
e05f1827 GS |
21 | #include "config.h" |
22 | ||
23 | #include <glib.h> | |
4a35548b MS |
24 | #include <stdlib.h> |
25 | #include <string.h> | |
e05f1827 | 26 | |
c1aae900 | 27 | #include <libsigrok/libsigrok.h> |
4a35548b | 28 | #include "libsigrok-internal.h" |
f6dcb320 | 29 | #include "scpi.h" /* String un-quote for channel name from header line. */ |
4a35548b | 30 | |
3544f848 | 31 | #define LOG_PREFIX "input/csv" |
4a35548b | 32 | |
9a4fd01a | 33 | #define CHUNK_SIZE (4 * 1024 * 1024) |
cd59e6ec | 34 | |
4a35548b MS |
35 | /* |
36 | * The CSV input module has the following options: | |
37 | * | |
38 | * single-column: Specifies the column number which stores the sample data for | |
39 | * single column mode and enables single column mode. Multi | |
40 | * column mode is used if this parameter is omitted. | |
41 | * | |
ba7dd8bb UH |
42 | * numchannels: Specifies the number of channels to use. In multi column mode |
43 | * the number of channels are the number of columns and in single | |
4a35548b | 44 | * column mode the number of bits (LSB first) beginning at |
ba7dd8bb | 45 | * 'first-channel'. |
4a35548b MS |
46 | * |
47 | * delimiter: Specifies the delimiter for columns. Must be at least one | |
48 | * character. Comma is used as default delimiter. | |
49 | * | |
50 | * format: Specifies the format of the sample data in single column mode. | |
51 | * Available formats are: 'bin', 'hex' and 'oct'. The binary | |
52 | * format is used by default. This option has no effect in multi | |
53 | * column mode. | |
54 | * | |
55 | * comment: Specifies the prefix character(s) for comments. No prefix | |
56 | * characters are used by default which disables removing of | |
57 | * comments. | |
58 | * | |
59 | * samplerate: Samplerate which the sample data was captured with. Default | |
60 | * value is 0. | |
61 | * | |
ba7dd8bb UH |
62 | * first-channel: Column number of the first channel in multi column mode and |
63 | * position of the bit for the first channel in single column mode. | |
4a35548b MS |
64 | * Default value is 0. |
65 | * | |
66 | * header: Determines if the first line should be treated as header | |
ba7dd8bb UH |
67 | * and used for channel names in multi column mode. Empty header |
68 | * names will be replaced by the channel number. If enabled in | |
4a35548b MS |
69 | * single column mode the first line will be skipped. Usage of |
70 | * header is disabled by default. | |
71 | * | |
72 | * startline: Line number to start processing sample data. Must be greater | |
73 | * than 0. The default line number to start processing is 1. | |
74 | */ | |
75 | ||
ccff468b GS |
76 | /* |
77 | * TODO | |
78 | * | |
79 | * - Determine how the text line handling can get improved, regarding | |
80 | * all of robustness and flexibility and correctness. | |
81 | * - The current implementation splits on "any run of CR and LF". Which | |
82 | * translates to: Line numbers are wrong in the presence of empty | |
de788af4 | 83 | * lines in the input stream. See below for an (expensive) fix. |
ccff468b GS |
84 | * - Dropping support for CR style end-of-line markers could improve |
85 | * the situation a lot. Code could search for and split on LF, and | |
86 | * trim optional trailing CR. This would result in proper support | |
87 | * for CRLF (Windows) as well as LF (Unix), and allow for correct | |
88 | * line number counts. | |
89 | * - When support for CR-only line termination cannot get dropped, | |
90 | * then the current implementation is inappropriate. Currently the | |
91 | * input stream is scanned for the first occurance of either of the | |
92 | * supported termination styles (which is good). For the remaining | |
93 | * session a consistent encoding of the text lines is assumed (which | |
de788af4 | 94 | * is acceptable). |
ccff468b GS |
95 | * - When line numbers need to be correct and reliable, _and_ the full |
96 | * set of previously supported line termination sequences are required, | |
97 | * and potentially more are to get added for improved compatibility | |
98 | * with more platforms or generators, then the current approach of | |
99 | * splitting on runs of termination characters needs to get replaced, | |
100 | * by the more expensive approach to scan for and count the initially | |
101 | * determined termination sequence. | |
102 | * | |
103 | * - Add support for analog input data? (optional) | |
104 | * - Needs a syntax first for user specs which channels (columns) are | |
105 | * logic and which are analog. May need heuristics(?) to guess from | |
106 | * input data in the absence of user provided specs. | |
107 | */ | |
108 | ||
4a35548b | 109 | /* Single column formats. */ |
ad6a2bee | 110 | enum single_col_format { |
e53f32d2 GS |
111 | FORMAT_NONE, /* Ignore this column. */ |
112 | FORMAT_BIN, /* Bin digits for a set of bits (or just one bit). */ | |
113 | FORMAT_HEX, /* Hex digits for a set of bits. */ | |
114 | FORMAT_OCT, /* Oct digits for a set of bits. */ | |
115 | }; | |
116 | ||
117 | static const char *col_format_text[] = { | |
118 | [FORMAT_NONE] = "unknown", | |
119 | [FORMAT_BIN] = "binary", | |
120 | [FORMAT_HEX] = "hexadecimal", | |
121 | [FORMAT_OCT] = "octal", | |
122 | }; | |
123 | ||
1a920e33 GS |
124 | static const char col_format_char[] = { |
125 | [FORMAT_NONE] = '?', | |
126 | [FORMAT_BIN] = 'b', | |
127 | [FORMAT_HEX] = 'x', | |
128 | [FORMAT_OCT] = 'o', | |
129 | }; | |
130 | ||
e53f32d2 GS |
131 | struct column_details { |
132 | size_t col_nr; | |
133 | enum single_col_format text_format; | |
134 | size_t channel_offset; | |
135 | size_t channel_count; | |
4a35548b MS |
136 | }; |
137 | ||
138 | struct context { | |
41d214f6 BV |
139 | gboolean started; |
140 | ||
4a35548b MS |
141 | /* Current selected samplerate. */ |
142 | uint64_t samplerate; | |
246aca5f | 143 | gboolean samplerate_sent; |
4a35548b | 144 | |
836fac9c GS |
145 | /* Number of logic channels. */ |
146 | size_t logic_channels; | |
4a35548b | 147 | |
836fac9c | 148 | /* Column delimiter (actually separator), comment leader, EOL sequence. */ |
4a35548b | 149 | GString *delimiter; |
4a35548b | 150 | GString *comment; |
41d214f6 BV |
151 | char *termination; |
152 | ||
1a920e33 GS |
153 | /* Format specs for input columns, and processing state. */ |
154 | size_t column_seen_count; | |
155 | const char *column_formats; | |
e53f32d2 GS |
156 | size_t column_want_count; |
157 | struct column_details *column_details; | |
158 | ||
4a35548b | 159 | /* Line number to start processing. */ |
6433156c | 160 | size_t start_line; |
4a35548b MS |
161 | |
162 | /* | |
163 | * Determines if the first line should be treated as header and used for | |
ba7dd8bb | 164 | * channel names in multi column mode. |
4a35548b | 165 | */ |
de8fe3b5 GS |
166 | gboolean use_header; |
167 | gboolean header_seen; | |
4a35548b | 168 | |
cd59e6ec GS |
169 | size_t sample_unit_size; /**!< Byte count for a single sample. */ |
170 | uint8_t *sample_buffer; /**!< Buffer for a single sample. */ | |
4a35548b | 171 | |
cd59e6ec GS |
172 | uint8_t *datafeed_buffer; /**!< Queue for datafeed submission. */ |
173 | size_t datafeed_buf_size; | |
174 | size_t datafeed_buf_fill; | |
4a35548b | 175 | |
4a35548b | 176 | /* Current line number. */ |
6433156c | 177 | size_t line_number; |
affaf540 GS |
178 | |
179 | /* List of previously created sigrok channels. */ | |
180 | GSList *prev_sr_channels; | |
4a35548b MS |
181 | }; |
182 | ||
626c388a GS |
183 | /* |
184 | * Primitive operations to handle sample sets: | |
185 | * - Keep a buffer for datafeed submission, capable of holding many | |
186 | * samples (reduces call overhead, improves throughput). | |
187 | * - Have a "current sample set" pointer reference one position in that | |
188 | * large samples buffer. | |
189 | * - Clear the current sample set before text line inspection, then set | |
190 | * the bits which are found active in the current line of text input. | |
191 | * Phrase the API such that call sites can be kept simple. Advance to | |
192 | * the next sample set between lines, flush the larger buffer as needed | |
193 | * (when it is full, or upon EOF). | |
194 | */ | |
195 | ||
196 | static void clear_logic_samples(struct context *inc) | |
197 | { | |
198 | inc->sample_buffer = &inc->datafeed_buffer[inc->datafeed_buf_fill]; | |
199 | memset(inc->sample_buffer, 0, inc->sample_unit_size); | |
200 | } | |
201 | ||
202 | static void set_logic_level(struct context *inc, size_t ch_idx, int on) | |
203 | { | |
204 | size_t byte_idx, bit_idx; | |
205 | uint8_t bit_mask; | |
206 | ||
836fac9c | 207 | if (ch_idx >= inc->logic_channels) |
626c388a GS |
208 | return; |
209 | if (!on) | |
210 | return; | |
211 | ||
212 | byte_idx = ch_idx / 8; | |
213 | bit_idx = ch_idx % 8; | |
214 | bit_mask = 1 << bit_idx; | |
215 | inc->sample_buffer[byte_idx] |= bit_mask; | |
216 | } | |
217 | ||
218 | static int flush_logic_samples(const struct sr_input *in) | |
219 | { | |
220 | struct context *inc; | |
221 | struct sr_datafeed_packet packet; | |
246aca5f GS |
222 | struct sr_datafeed_meta meta; |
223 | struct sr_config *src; | |
224 | uint64_t samplerate; | |
626c388a GS |
225 | struct sr_datafeed_logic logic; |
226 | int rc; | |
227 | ||
228 | inc = in->priv; | |
229 | if (!inc->datafeed_buf_fill) | |
230 | return SR_OK; | |
231 | ||
246aca5f GS |
232 | if (inc->samplerate && !inc->samplerate_sent) { |
233 | packet.type = SR_DF_META; | |
234 | packet.payload = &meta; | |
235 | samplerate = inc->samplerate; | |
236 | src = sr_config_new(SR_CONF_SAMPLERATE, g_variant_new_uint64(samplerate)); | |
237 | meta.config = g_slist_append(NULL, src); | |
238 | sr_session_send(in->sdi, &packet); | |
239 | g_slist_free(meta.config); | |
240 | sr_config_free(src); | |
241 | inc->samplerate_sent = TRUE; | |
242 | } | |
243 | ||
626c388a GS |
244 | memset(&packet, 0, sizeof(packet)); |
245 | memset(&logic, 0, sizeof(logic)); | |
246 | packet.type = SR_DF_LOGIC; | |
247 | packet.payload = &logic; | |
248 | logic.unitsize = inc->sample_unit_size; | |
249 | logic.length = inc->datafeed_buf_fill; | |
250 | logic.data = inc->datafeed_buffer; | |
251 | ||
252 | rc = sr_session_send(in->sdi, &packet); | |
253 | if (rc != SR_OK) | |
254 | return rc; | |
255 | ||
256 | inc->datafeed_buf_fill = 0; | |
257 | return SR_OK; | |
258 | } | |
259 | ||
260 | static int queue_logic_samples(const struct sr_input *in) | |
261 | { | |
262 | struct context *inc; | |
263 | int rc; | |
264 | ||
265 | inc = in->priv; | |
836fac9c GS |
266 | if (!inc->logic_channels) |
267 | return SR_OK; | |
626c388a GS |
268 | |
269 | inc->datafeed_buf_fill += inc->sample_unit_size; | |
270 | if (inc->datafeed_buf_fill == inc->datafeed_buf_size) { | |
271 | rc = flush_logic_samples(in); | |
272 | if (rc != SR_OK) | |
273 | return rc; | |
274 | } | |
275 | return SR_OK; | |
276 | } | |
277 | ||
2142a79b GS |
278 | /* Helpers for "column processing". */ |
279 | ||
280 | static int split_column_format(const char *spec, | |
281 | size_t *column_count, enum single_col_format *format, size_t *bit_count) | |
282 | { | |
283 | size_t count; | |
284 | char *endp, format_char; | |
285 | enum single_col_format format_code; | |
286 | ||
287 | if (!spec || !*spec) | |
288 | return SR_ERR_ARG; | |
289 | ||
1a920e33 | 290 | /* Get the (optional, decimal, default 1) column count. Accept '*'. */ |
2142a79b | 291 | endp = NULL; |
1a920e33 GS |
292 | if (*spec == '*') { |
293 | count = 0; | |
294 | endp = (char *)&spec[1]; | |
295 | } else { | |
296 | count = strtoul(spec, &endp, 10); | |
297 | } | |
2142a79b GS |
298 | if (!endp) |
299 | return SR_ERR_ARG; | |
300 | if (endp == spec) | |
301 | count = 1; | |
302 | if (column_count) | |
303 | *column_count = count; | |
304 | spec = endp; | |
305 | ||
306 | /* Get the (mandatory, single letter) type spec (-/xob/l). */ | |
307 | format_char = *spec++; | |
308 | switch (format_char) { | |
309 | case '-': /* Might conflict with number-parsing. */ | |
310 | case '/': | |
311 | format_char = '-'; | |
312 | format_code = FORMAT_NONE; | |
313 | break; | |
314 | case 'x': | |
315 | format_code = FORMAT_HEX; | |
316 | break; | |
317 | case 'o': | |
318 | format_code = FORMAT_OCT; | |
319 | break; | |
320 | case 'b': | |
321 | case 'l': | |
322 | format_code = FORMAT_BIN; | |
323 | break; | |
324 | default: /* includes NUL */ | |
325 | return SR_ERR_ARG; | |
326 | } | |
327 | if (format) | |
328 | *format = format_code; | |
329 | ||
330 | /* Get the (optional, decimal, default 1) bit count. */ | |
331 | endp = NULL; | |
332 | count = strtoul(spec, &endp, 10); | |
333 | if (!endp) | |
334 | return SR_ERR_ARG; | |
335 | if (endp == spec) | |
336 | count = 1; | |
337 | if (format_char == '-') | |
338 | count = 0; | |
339 | if (format_char == 'l') | |
340 | count = 1; | |
341 | if (bit_count) | |
342 | *bit_count = count; | |
343 | spec = endp; | |
344 | ||
345 | /* Input spec must have been exhausted. */ | |
346 | if (*spec) | |
347 | return SR_ERR_ARG; | |
348 | ||
349 | return SR_OK; | |
350 | } | |
351 | ||
352 | static int make_column_details_from_format(struct context *inc, | |
353 | const char *column_format) | |
354 | { | |
355 | char **formats, *format; | |
356 | size_t format_count, column_count, bit_count; | |
1a920e33 | 357 | size_t auto_column_count; |
2142a79b GS |
358 | size_t format_idx, c, b, column_idx, channel_idx; |
359 | enum single_col_format f; | |
360 | struct column_details *detail; | |
361 | int ret; | |
362 | ||
2142a79b GS |
363 | /* Split the input spec, count involved columns and bits. */ |
364 | formats = g_strsplit(column_format, ",", 0); | |
365 | if (!formats) { | |
366 | sr_err("Cannot parse columns format %s (comma split).", column_format); | |
367 | return SR_ERR_ARG; | |
368 | } | |
369 | format_count = g_strv_length(formats); | |
370 | if (!format_count) { | |
371 | sr_err("Cannot parse columns format %s (field count).", column_format); | |
372 | g_strfreev(formats); | |
373 | return SR_ERR_ARG; | |
374 | } | |
375 | column_count = bit_count = 0; | |
1a920e33 | 376 | auto_column_count = 0; |
2142a79b GS |
377 | for (format_idx = 0; format_idx < format_count; format_idx++) { |
378 | format = formats[format_idx]; | |
379 | ret = split_column_format(format, &c, &f, &b); | |
380 | sr_dbg("fmt %s -> %zu cols, %s fmt, %zu bits, rc %d", format, c, col_format_text[f], b, ret); | |
381 | if (ret != SR_OK) { | |
382 | sr_err("Cannot parse columns format %s (field split, %s).", column_format, format); | |
383 | g_strfreev(formats); | |
384 | return SR_ERR_ARG; | |
385 | } | |
1a920e33 GS |
386 | if (f && !c) { |
387 | /* User requested "auto-count", must be last format. */ | |
388 | if (formats[format_idx + 1]) { | |
389 | sr_err("Auto column count must be last format field."); | |
390 | g_strfreev(formats); | |
391 | return SR_ERR_ARG; | |
392 | } | |
393 | auto_column_count = inc->column_seen_count - column_count; | |
394 | c = auto_column_count; | |
395 | } | |
2142a79b GS |
396 | column_count += c; |
397 | bit_count += c * b; | |
398 | } | |
399 | sr_dbg("Column format %s -> %zu columns, %zu logic channels.", | |
400 | column_format, column_count, bit_count); | |
401 | ||
402 | /* Allocate and fill in "column processing" details. */ | |
403 | inc->column_want_count = column_count; | |
404 | inc->column_details = g_malloc0_n(column_count, sizeof(inc->column_details[0])); | |
405 | column_idx = channel_idx = 0; | |
406 | for (format_idx = 0; format_idx < format_count; format_idx++) { | |
407 | format = formats[format_idx]; | |
408 | (void)split_column_format(format, &c, &f, &b); | |
1a920e33 GS |
409 | if (f && !c) |
410 | c = auto_column_count; | |
2142a79b GS |
411 | while (c-- > 0) { |
412 | detail = &inc->column_details[column_idx++]; | |
413 | detail->col_nr = column_idx; | |
414 | detail->text_format = f; | |
415 | if (detail->text_format) { | |
416 | detail->channel_offset = channel_idx; | |
417 | detail->channel_count = b; | |
418 | channel_idx += b; | |
419 | } | |
420 | sr_dbg("detail -> col %zu, fmt %s, ch off/cnt %zu/%zu", | |
421 | detail->col_nr, col_format_text[detail->text_format], | |
422 | detail->channel_offset, detail->channel_count); | |
423 | } | |
424 | } | |
425 | inc->logic_channels = channel_idx; | |
426 | g_strfreev(formats); | |
427 | ||
428 | return SR_OK; | |
429 | } | |
430 | ||
e53f32d2 GS |
431 | static const struct column_details *lookup_column_details(struct context *inc, size_t nr) |
432 | { | |
433 | if (!inc || !inc->column_details) | |
434 | return NULL; | |
435 | if (!nr || nr > inc->column_want_count) | |
436 | return NULL; | |
437 | return &inc->column_details[nr - 1]; | |
438 | } | |
439 | ||
19267272 GS |
440 | /* |
441 | * Primitive operations for text input: Strip comments off text lines. | |
442 | * Split text lines into columns. Process input text for individual | |
443 | * columns. | |
444 | */ | |
445 | ||
41d214f6 | 446 | static void strip_comment(char *buf, const GString *prefix) |
4a35548b MS |
447 | { |
448 | char *ptr; | |
449 | ||
450 | if (!prefix->len) | |
451 | return; | |
452 | ||
b2c4dde2 | 453 | if ((ptr = strstr(buf, prefix->str))) { |
41d214f6 | 454 | *ptr = '\0'; |
b2c4dde2 GS |
455 | g_strstrip(buf); |
456 | } | |
4a35548b MS |
457 | } |
458 | ||
19267272 | 459 | /** |
e53f32d2 | 460 | * @brief Splits a text line into a set of columns. |
19267272 | 461 | * |
e53f32d2 | 462 | * @param[in] buf The input text line to split. |
19267272 GS |
463 | * @param[in] inc The input module's context. |
464 | * | |
e53f32d2 | 465 | * @returns An array of strings, representing the columns' text. |
19267272 | 466 | * |
e53f32d2 | 467 | * This routine splits a text line on previously determined separators. |
19267272 | 468 | */ |
e53f32d2 | 469 | static char **split_line(char *buf, struct context *inc) |
4a35548b | 470 | { |
e53f32d2 | 471 | return g_strsplit(buf, inc->delimiter->str, 0); |
4a35548b MS |
472 | } |
473 | ||
19267272 | 474 | /** |
e53f32d2 | 475 | * @brief Parse a multi-bit field into several logic channels. |
19267272 | 476 | * |
e53f32d2 | 477 | * @param[in] column The input text, a run of bin/hex/oct digits. |
19267272 | 478 | * @param[in] inc The input module's context. |
836fac9c | 479 | * @param[in] details The column processing details. |
19267272 GS |
480 | * |
481 | * @retval SR_OK Success. | |
482 | * @retval SR_ERR Invalid input data (empty, or format error). | |
483 | * | |
484 | * This routine modifies the logic levels in the current sample set, | |
e53f32d2 | 485 | * based on the text input and a user provided format spec. |
19267272 | 486 | */ |
836fac9c GS |
487 | static int parse_logic(const char *column, struct context *inc, |
488 | const struct column_details *details) | |
4a35548b | 489 | { |
e53f32d2 GS |
490 | size_t length, ch_rem, ch_idx, ch_inc; |
491 | const char *rdptr; | |
4a35548b | 492 | char c; |
e53f32d2 GS |
493 | gboolean valid; |
494 | const char *type_text; | |
495 | uint8_t bits; | |
496 | ||
e53f32d2 GS |
497 | /* |
498 | * Prepare to read the digits from the text end towards the start. | |
499 | * A digit corresponds to a variable number of channels (depending | |
500 | * on the value's radix). Prepare the mapping of text digits to | |
501 | * (a number of) logic channels. | |
502 | */ | |
503 | length = strlen(column); | |
4a35548b | 504 | if (!length) { |
836fac9c | 505 | sr_err("Column %zu in line %zu is empty.", details->col_nr, |
41d214f6 | 506 | inc->line_number); |
4a35548b MS |
507 | return SR_ERR; |
508 | } | |
e53f32d2 | 509 | rdptr = &column[length]; |
836fac9c GS |
510 | ch_idx = details->channel_offset; |
511 | ch_rem = details->channel_count; | |
4a35548b | 512 | |
e53f32d2 GS |
513 | /* |
514 | * Get another digit and derive up to four logic channels' state from | |
515 | * it. Make sure to not process more bits than the column has channels | |
516 | * associated with it. | |
517 | */ | |
518 | while (rdptr > column && ch_rem) { | |
519 | /* Check for valid digits according to the input radix. */ | |
520 | c = *(--rdptr); | |
836fac9c | 521 | switch (details->text_format) { |
e53f32d2 GS |
522 | case FORMAT_BIN: |
523 | valid = g_ascii_isxdigit(c) && c < '2'; | |
524 | ch_inc = 1; | |
525 | break; | |
526 | case FORMAT_OCT: | |
527 | valid = g_ascii_isxdigit(c) && c < '8'; | |
528 | ch_inc = 3; | |
529 | break; | |
530 | case FORMAT_HEX: | |
531 | valid = g_ascii_isxdigit(c); | |
532 | ch_inc = 4; | |
533 | break; | |
534 | default: | |
535 | valid = FALSE; | |
536 | break; | |
4a35548b | 537 | } |
e53f32d2 | 538 | if (!valid) { |
836fac9c | 539 | type_text = col_format_text[details->text_format]; |
e53f32d2 | 540 | sr_err("Invalid text '%s' in %s type column %zu in line %zu.", |
836fac9c | 541 | column, type_text, details->col_nr, inc->line_number); |
4a35548b | 542 | return SR_ERR; |
e53f32d2 GS |
543 | } |
544 | /* Use the digit's bits for logic channels' data. */ | |
545 | bits = g_ascii_xdigit_value(c); | |
836fac9c | 546 | switch (details->text_format) { |
e53f32d2 GS |
547 | case FORMAT_HEX: |
548 | if (ch_rem >= 4) { | |
549 | ch_rem--; | |
550 | set_logic_level(inc, ch_idx + 3, bits & (1 << 3)); | |
551 | } | |
552 | /* FALLTHROUGH */ | |
553 | case FORMAT_OCT: | |
554 | if (ch_rem >= 3) { | |
555 | ch_rem--; | |
556 | set_logic_level(inc, ch_idx + 2, bits & (1 << 2)); | |
557 | } | |
558 | if (ch_rem >= 2) { | |
559 | ch_rem--; | |
560 | set_logic_level(inc, ch_idx + 1, bits & (1 << 1)); | |
561 | } | |
562 | /* FALLTHROUGH */ | |
563 | case FORMAT_BIN: | |
564 | ch_rem--; | |
565 | set_logic_level(inc, ch_idx + 0, bits & (1 << 0)); | |
566 | break; | |
836fac9c GS |
567 | case FORMAT_NONE: |
568 | /* ShouldNotHappen(TM), but silences compiler warning. */ | |
569 | return SR_ERR; | |
4a35548b | 570 | } |
e53f32d2 | 571 | ch_idx += ch_inc; |
4a35548b | 572 | } |
e53f32d2 GS |
573 | /* |
574 | * TODO Determine whether the availability of extra input data | |
575 | * for unhandled logic channels is worth warning here. In this | |
576 | * implementation users are in control, and can have the more | |
577 | * significant bits ignored (which can be considered a feature | |
578 | * and not really a limitation). | |
579 | */ | |
4a35548b MS |
580 | |
581 | return SR_OK; | |
582 | } | |
583 | ||
836fac9c GS |
584 | /** |
585 | * @brief Parse routine which ignores the input text. | |
586 | * | |
587 | * This routine exists to unify dispatch code paths, mapping input file | |
588 | * columns' data types to their respective parse routines. | |
589 | */ | |
590 | static int parse_ignore(const char *column, struct context *inc, | |
591 | const struct column_details *details) | |
592 | { | |
593 | (void)column; | |
594 | (void)inc; | |
595 | (void)details; | |
596 | return SR_OK; | |
597 | } | |
598 | ||
599 | typedef int (*col_parse_cb)(const char *column, struct context *inc, | |
600 | const struct column_details *details); | |
601 | ||
602 | static const col_parse_cb col_parse_funcs[] = { | |
603 | [FORMAT_NONE] = parse_ignore, | |
604 | [FORMAT_BIN] = parse_logic, | |
605 | [FORMAT_OCT] = parse_logic, | |
606 | [FORMAT_HEX] = parse_logic, | |
607 | }; | |
608 | ||
41d214f6 | 609 | static int init(struct sr_input *in, GHashTable *options) |
4a35548b | 610 | { |
41d214f6 | 611 | struct context *inc; |
1a920e33 | 612 | size_t single_column, first_column, logic_channels; |
41d214f6 | 613 | const char *s; |
836fac9c | 614 | enum single_col_format format; |
1a920e33 | 615 | char format_char; |
4a35548b | 616 | |
836fac9c GS |
617 | in->sdi = g_malloc0(sizeof(*in->sdi)); |
618 | in->priv = inc = g_malloc0(sizeof(*inc)); | |
4a35548b | 619 | |
836fac9c | 620 | single_column = g_variant_get_uint32(g_hash_table_lookup(options, "single-column")); |
4a35548b | 621 | |
1a920e33 | 622 | logic_channels = g_variant_get_uint32(g_hash_table_lookup(options, "numchannels")); |
4a35548b | 623 | |
41d214f6 BV |
624 | inc->delimiter = g_string_new(g_variant_get_string( |
625 | g_hash_table_lookup(options, "delimiter"), NULL)); | |
836fac9c | 626 | if (!inc->delimiter->len) { |
e53f32d2 | 627 | sr_err("Column delimiter cannot be empty."); |
41d214f6 | 628 | return SR_ERR_ARG; |
4a35548b MS |
629 | } |
630 | ||
41d214f6 | 631 | s = g_variant_get_string(g_hash_table_lookup(options, "format"), NULL); |
836fac9c GS |
632 | if (g_ascii_strncasecmp(s, "bin", 3) == 0) { |
633 | format = FORMAT_BIN; | |
634 | } else if (g_ascii_strncasecmp(s, "hex", 3) == 0) { | |
635 | format = FORMAT_HEX; | |
636 | } else if (g_ascii_strncasecmp(s, "oct", 3) == 0) { | |
637 | format = FORMAT_OCT; | |
41d214f6 BV |
638 | } else { |
639 | sr_err("Invalid format: '%s'", s); | |
640 | return SR_ERR_ARG; | |
4a35548b MS |
641 | } |
642 | ||
41d214f6 BV |
643 | inc->comment = g_string_new(g_variant_get_string( |
644 | g_hash_table_lookup(options, "comment"), NULL)); | |
645 | if (g_string_equal(inc->comment, inc->delimiter)) { | |
e53f32d2 GS |
646 | /* |
647 | * Using the same sequence as comment leader and column | |
648 | * delimiter won't work. The user probably specified ';' | |
649 | * as the column delimiter but did not adjust the comment | |
650 | * leader. Try DWIM, drop comment strippin support here. | |
651 | */ | |
652 | sr_warn("Comment leader and column delimiter conflict, disabling comment support."); | |
41d214f6 | 653 | g_string_truncate(inc->comment, 0); |
4a35548b MS |
654 | } |
655 | ||
6e8d95a5 | 656 | inc->samplerate = g_variant_get_uint64(g_hash_table_lookup(options, "samplerate")); |
4a35548b | 657 | |
1a920e33 | 658 | first_column = g_variant_get_uint32(g_hash_table_lookup(options, "first-column")); |
4a35548b | 659 | |
de8fe3b5 | 660 | inc->use_header = g_variant_get_boolean(g_hash_table_lookup(options, "header")); |
4a35548b | 661 | |
ad6a2bee | 662 | inc->start_line = g_variant_get_uint32(g_hash_table_lookup(options, "startline")); |
41d214f6 | 663 | if (inc->start_line < 1) { |
6433156c | 664 | sr_err("Invalid start line %zu.", inc->start_line); |
41d214f6 | 665 | return SR_ERR_ARG; |
4a35548b MS |
666 | } |
667 | ||
e53f32d2 | 668 | /* |
1a920e33 GS |
669 | * Scan flexible, to get prefered format specs which describe |
670 | * the input file's data formats. As well as some simple specs | |
671 | * for backwards compatibility and user convenience. | |
672 | * | |
673 | * This logic ends up with a copy of the format string, either | |
674 | * user provided or internally derived. Actual creation of the | |
675 | * column processing details gets deferred until the first line | |
676 | * of input data was seen. To support automatic determination of | |
677 | * e.g. channel counts from column counts. | |
e53f32d2 | 678 | */ |
2142a79b GS |
679 | s = g_variant_get_string(g_hash_table_lookup(options, "column-formats"), NULL); |
680 | if (s && *s) { | |
1a920e33 GS |
681 | inc->column_formats = g_strdup(s); |
682 | sr_dbg("User specified column-formats: %s.", s); | |
683 | } else if (single_column && logic_channels) { | |
684 | format_char = col_format_char[format]; | |
685 | if (single_column == 1) { | |
686 | inc->column_formats = g_strdup_printf("%c%zu", | |
687 | format_char, logic_channels); | |
e53f32d2 | 688 | } else { |
1a920e33 GS |
689 | inc->column_formats = g_strdup_printf("%zu-,%c%zu", |
690 | single_column - 1, | |
691 | format_char, logic_channels); | |
e53f32d2 | 692 | } |
1a920e33 GS |
693 | sr_dbg("Backwards compat single-column, col %zu, fmt %s, bits %zu -> %s.", |
694 | single_column, col_format_text[format], logic_channels, | |
695 | inc->column_formats); | |
696 | } else if (!single_column) { | |
697 | if (first_column > 1) { | |
698 | inc->column_formats = g_strdup_printf("%zu-,%zul", | |
699 | first_column - 1, logic_channels); | |
700 | } else { | |
701 | inc->column_formats = g_strdup_printf("%zul", | |
702 | logic_channels); | |
703 | } | |
704 | sr_dbg("Backwards compat multi-column, col %zu, chans %zu -> %s.", | |
705 | first_column, logic_channels, | |
706 | inc->column_formats); | |
e53f32d2 | 707 | } else { |
1a920e33 GS |
708 | sr_warn("Unknown or unsupported format spec, assuming trivial multi-column."); |
709 | inc->column_formats = g_strdup("*l"); | |
4a35548b MS |
710 | } |
711 | ||
41d214f6 BV |
712 | return SR_OK; |
713 | } | |
4a35548b | 714 | |
affaf540 GS |
715 | /* |
716 | * Check the channel list for consistency across file re-import. See | |
717 | * the VCD input module for more details and motivation. | |
718 | */ | |
719 | ||
720 | static void keep_header_for_reread(const struct sr_input *in) | |
721 | { | |
722 | struct context *inc; | |
723 | ||
724 | inc = in->priv; | |
725 | g_slist_free_full(inc->prev_sr_channels, sr_channel_free_cb); | |
726 | inc->prev_sr_channels = in->sdi->channels; | |
727 | in->sdi->channels = NULL; | |
728 | } | |
729 | ||
730 | static int check_header_in_reread(const struct sr_input *in) | |
731 | { | |
732 | struct context *inc; | |
733 | ||
734 | if (!in) | |
735 | return FALSE; | |
736 | inc = in->priv; | |
737 | if (!inc) | |
738 | return FALSE; | |
739 | if (!inc->prev_sr_channels) | |
740 | return TRUE; | |
741 | ||
742 | if (sr_channel_lists_differ(inc->prev_sr_channels, in->sdi->channels)) { | |
743 | sr_err("Channel list change not supported for file re-read."); | |
744 | return FALSE; | |
745 | } | |
746 | g_slist_free_full(in->sdi->channels, sr_channel_free_cb); | |
747 | in->sdi->channels = inc->prev_sr_channels; | |
748 | inc->prev_sr_channels = NULL; | |
749 | ||
750 | return TRUE; | |
751 | } | |
752 | ||
492dfa90 GS |
753 | static const char *delim_set = "\r\n"; |
754 | ||
329733d9 | 755 | static const char *get_line_termination(GString *buf) |
41d214f6 | 756 | { |
329733d9 | 757 | const char *term; |
4a35548b | 758 | |
41d214f6 BV |
759 | term = NULL; |
760 | if (g_strstr_len(buf->str, buf->len, "\r\n")) | |
761 | term = "\r\n"; | |
762 | else if (memchr(buf->str, '\n', buf->len)) | |
763 | term = "\n"; | |
764 | else if (memchr(buf->str, '\r', buf->len)) | |
765 | term = "\r"; | |
4a35548b | 766 | |
41d214f6 BV |
767 | return term; |
768 | } | |
4a35548b | 769 | |
41d214f6 BV |
770 | static int initial_parse(const struct sr_input *in, GString *buf) |
771 | { | |
772 | struct context *inc; | |
41d214f6 | 773 | GString *channel_name; |
e53f32d2 GS |
774 | size_t num_columns, ch_idx, ch_name_idx, col_idx, col_nr; |
775 | size_t line_number, line_idx; | |
41d214f6 | 776 | int ret; |
df0db9fd | 777 | char **lines, *line, **columns, *column; |
f6dcb320 GS |
778 | const char *col_caption; |
779 | gboolean got_caption; | |
e53f32d2 | 780 | const struct column_details *detail; |
41d214f6 BV |
781 | |
782 | ret = SR_OK; | |
783 | inc = in->priv; | |
784 | columns = NULL; | |
785 | ||
786 | line_number = 0; | |
ef0b9935 GS |
787 | if (inc->termination) |
788 | lines = g_strsplit(buf->str, inc->termination, 0); | |
789 | else | |
790 | lines = g_strsplit_set(buf->str, delim_set, 0); | |
e53f32d2 | 791 | for (line_idx = 0; (line = lines[line_idx]); line_idx++) { |
41d214f6 BV |
792 | line_number++; |
793 | if (inc->start_line > line_number) { | |
e53f32d2 | 794 | sr_spew("Line %zu skipped (before start).", line_number); |
4a35548b MS |
795 | continue; |
796 | } | |
df0db9fd | 797 | if (line[0] == '\0') { |
41d214f6 BV |
798 | sr_spew("Blank line %zu skipped.", line_number); |
799 | continue; | |
800 | } | |
df0db9fd GS |
801 | strip_comment(line, inc->comment); |
802 | if (line[0] == '\0') { | |
41d214f6 | 803 | sr_spew("Comment-only line %zu skipped.", line_number); |
4a35548b MS |
804 | continue; |
805 | } | |
806 | ||
41d214f6 BV |
807 | /* Reached first proper line. */ |
808 | break; | |
809 | } | |
e53f32d2 | 810 | if (!line) { |
41d214f6 | 811 | /* Not enough data for a proper line yet. */ |
60107497 | 812 | ret = SR_ERR_NA; |
41d214f6 | 813 | goto out; |
4a35548b MS |
814 | } |
815 | ||
e53f32d2 GS |
816 | /* See how many columns the current line has. */ |
817 | columns = split_line(line, inc); | |
df0db9fd | 818 | if (!columns) { |
41d214f6 BV |
819 | sr_err("Error while parsing line %zu.", line_number); |
820 | ret = SR_ERR; | |
821 | goto out; | |
4a35548b | 822 | } |
4a35548b | 823 | num_columns = g_strv_length(columns); |
4a35548b | 824 | if (!num_columns) { |
e53f32d2 | 825 | sr_err("Error while parsing line %zu.", line_number); |
41d214f6 BV |
826 | ret = SR_ERR; |
827 | goto out; | |
4a35548b | 828 | } |
e53f32d2 GS |
829 | sr_dbg("DIAG Got %zu columns in text line: %s.", num_columns, line); |
830 | ||
1a920e33 GS |
831 | /* |
832 | * Track the observed number of columns in the input file. Do | |
833 | * process the previously gathered columns format spec now that | |
834 | * automatic channel count can be dealt with. | |
835 | */ | |
836 | inc->column_seen_count = num_columns; | |
837 | ret = make_column_details_from_format(inc, inc->column_formats); | |
838 | if (ret != SR_OK) { | |
839 | sr_err("Cannot parse columns format using line %zu.", line_number); | |
840 | goto out; | |
4a35548b MS |
841 | } |
842 | ||
e53f32d2 GS |
843 | /* |
844 | * Assume all lines have equal length (column count). Bail out | |
845 | * early on suspicious or insufficient input data (check input | |
846 | * which became available here against previous user specs or | |
847 | * auto-determined properties, regardless of layout variant). | |
848 | */ | |
849 | if (num_columns < inc->column_want_count) { | |
850 | sr_err("Insufficient input text width for desired data amount, got %zu but want %zu columns.", | |
851 | num_columns, inc->column_want_count); | |
852 | ret = SR_ERR; | |
853 | goto out; | |
854 | } | |
855 | ||
856 | /* | |
857 | * Determine channel names. Optionally use text from a header | |
858 | * line (when requested by the user, and only works in multi | |
859 | * column mode). In the absence of header text, or in single | |
860 | * column mode, channels are assigned rather generic names. | |
f6dcb320 GS |
861 | * |
862 | * Manipulation of the column's caption is acceptable here, the | |
863 | * header line will never get processed another time. | |
e53f32d2 | 864 | */ |
41d214f6 | 865 | channel_name = g_string_sized_new(64); |
e53f32d2 | 866 | for (col_idx = 0; col_idx < inc->column_want_count; col_idx++) { |
f6dcb320 | 867 | |
e53f32d2 GS |
868 | col_nr = col_idx + 1; |
869 | detail = lookup_column_details(inc, col_nr); | |
870 | if (detail->text_format == FORMAT_NONE) | |
871 | continue; | |
872 | column = columns[col_idx]; | |
f6dcb320 GS |
873 | col_caption = sr_scpi_unquote_string(column); |
874 | got_caption = inc->use_header && *col_caption; | |
e53f32d2 | 875 | sr_dbg("DIAG col %zu, ch count %zu, text %s.", |
f6dcb320 | 876 | col_nr, detail->channel_count, col_caption); |
e53f32d2 GS |
877 | for (ch_idx = 0; ch_idx < detail->channel_count; ch_idx++) { |
878 | ch_name_idx = detail->channel_offset + ch_idx; | |
f6dcb320 GS |
879 | if (got_caption && detail->channel_count == 1) |
880 | g_string_assign(channel_name, col_caption); | |
881 | else if (got_caption) | |
882 | g_string_printf(channel_name, "%s[%zu]", | |
883 | col_caption, ch_idx); | |
e53f32d2 GS |
884 | else |
885 | g_string_printf(channel_name, "%zu", ch_name_idx); | |
886 | sr_dbg("DIAG ch idx %zu, name %s.", ch_name_idx, channel_name->str); | |
887 | sr_channel_new(in->sdi, ch_name_idx, SR_CHANNEL_LOGIC, TRUE, | |
888 | channel_name->str); | |
889 | } | |
4a35548b | 890 | } |
41d214f6 | 891 | g_string_free(channel_name, TRUE); |
affaf540 GS |
892 | if (!check_header_in_reread(in)) { |
893 | ret = SR_ERR_DATA; | |
894 | goto out; | |
895 | } | |
4a35548b MS |
896 | |
897 | /* | |
cd59e6ec GS |
898 | * Calculate the minimum buffer size to store the set of samples |
899 | * of all channels (unit size). Determine a larger buffer size | |
900 | * for datafeed submission that is a multiple of the unit size. | |
626c388a GS |
901 | * Allocate the larger buffer, the "sample buffer" will point |
902 | * to a location within that large buffer later. | |
4a35548b | 903 | */ |
836fac9c | 904 | inc->sample_unit_size = (inc->logic_channels + 7) / 8; |
8bc2fa6d | 905 | inc->datafeed_buf_size = CHUNK_SIZE; |
cd59e6ec GS |
906 | inc->datafeed_buf_size *= inc->sample_unit_size; |
907 | inc->datafeed_buffer = g_malloc(inc->datafeed_buf_size); | |
908 | inc->datafeed_buf_fill = 0; | |
4a35548b | 909 | |
41d214f6 BV |
910 | out: |
911 | if (columns) | |
912 | g_strfreev(columns); | |
913 | g_strfreev(lines); | |
4a35548b | 914 | |
41d214f6 | 915 | return ret; |
4a35548b MS |
916 | } |
917 | ||
4439363a GS |
918 | /* |
919 | * Gets called from initial_receive(), which runs until the end-of-line | |
920 | * encoding of the input stream could get determined. Assumes that this | |
921 | * routine receives enough buffered initial input data to either see the | |
922 | * BOM when there is one, or that no BOM will follow when a text line | |
923 | * termination sequence was seen. Silently drops the UTF-8 BOM sequence | |
924 | * from the input buffer if one was seen. Does not care to protect | |
925 | * against multiple execution or dropping the BOM multiple times -- | |
926 | * there should be at most one in the input stream. | |
927 | */ | |
928 | static void initial_bom_check(const struct sr_input *in) | |
929 | { | |
930 | static const char *utf8_bom = "\xef\xbb\xbf"; | |
931 | ||
932 | if (in->buf->len < strlen(utf8_bom)) | |
933 | return; | |
934 | if (strncmp(in->buf->str, utf8_bom, strlen(utf8_bom)) != 0) | |
935 | return; | |
936 | g_string_erase(in->buf, 0, strlen(utf8_bom)); | |
937 | } | |
938 | ||
41d214f6 | 939 | static int initial_receive(const struct sr_input *in) |
4a35548b | 940 | { |
41d214f6 BV |
941 | struct context *inc; |
942 | GString *new_buf; | |
943 | int len, ret; | |
329733d9 UH |
944 | char *p; |
945 | const char *termination; | |
4a35548b | 946 | |
4439363a GS |
947 | initial_bom_check(in); |
948 | ||
41d214f6 | 949 | inc = in->priv; |
4a35548b | 950 | |
df0db9fd GS |
951 | termination = get_line_termination(in->buf); |
952 | if (!termination) | |
41d214f6 | 953 | /* Don't have a full line yet. */ |
d0181813 | 954 | return SR_ERR_NA; |
4a35548b | 955 | |
df0db9fd GS |
956 | p = g_strrstr_len(in->buf->str, in->buf->len, termination); |
957 | if (!p) | |
41d214f6 | 958 | /* Don't have a full line yet. */ |
d0181813 | 959 | return SR_ERR_NA; |
41d214f6 BV |
960 | len = p - in->buf->str - 1; |
961 | new_buf = g_string_new_len(in->buf->str, len); | |
962 | g_string_append_c(new_buf, '\0'); | |
4a35548b | 963 | |
41d214f6 BV |
964 | inc->termination = g_strdup(termination); |
965 | ||
966 | if (in->buf->str[0] != '\0') | |
967 | ret = initial_parse(in, new_buf); | |
968 | else | |
969 | ret = SR_OK; | |
970 | ||
971 | g_string_free(new_buf, TRUE); | |
972 | ||
973 | return ret; | |
974 | } | |
975 | ||
7f4c3a62 | 976 | static int process_buffer(struct sr_input *in, gboolean is_eof) |
41d214f6 | 977 | { |
41d214f6 BV |
978 | struct context *inc; |
979 | gsize num_columns; | |
e53f32d2 | 980 | size_t line_idx, col_idx, col_nr; |
836fac9c GS |
981 | const struct column_details *details; |
982 | col_parse_cb parse_func; | |
ad6a2bee | 983 | int ret; |
e53f32d2 | 984 | char *p, **lines, *line, **columns, *column; |
41d214f6 | 985 | |
41d214f6 | 986 | inc = in->priv; |
d0181813 | 987 | if (!inc->started) { |
bee2b016 | 988 | std_session_send_df_header(in->sdi); |
d0181813 | 989 | inc->started = TRUE; |
4a35548b MS |
990 | } |
991 | ||
4555d3bd GS |
992 | /* |
993 | * Consider empty input non-fatal. Keep accumulating input until | |
994 | * at least one full text line has become available. Grab the | |
995 | * maximum amount of accumulated data that consists of full text | |
996 | * lines, and process what has been received so far, leaving not | |
997 | * yet complete lines for the next invocation. | |
7f4c3a62 GS |
998 | * |
999 | * Enforce that all previously buffered data gets processed in | |
1000 | * the "EOF" condition. Do not insist in the presence of the | |
1001 | * termination sequence for the last line (may often be missing | |
1002 | * on Windows). A present termination sequence will just result | |
1003 | * in the "execution of an empty line", and does not harm. | |
4555d3bd GS |
1004 | */ |
1005 | if (!in->buf->len) | |
1006 | return SR_OK; | |
7f4c3a62 GS |
1007 | if (is_eof) { |
1008 | p = in->buf->str + in->buf->len; | |
1009 | } else { | |
1010 | p = g_strrstr_len(in->buf->str, in->buf->len, inc->termination); | |
1011 | if (!p) | |
1012 | return SR_ERR; | |
1013 | *p = '\0'; | |
1014 | p += strlen(inc->termination); | |
1015 | } | |
41d214f6 | 1016 | g_strstrip(in->buf->str); |
4a35548b | 1017 | |
18078d05 | 1018 | ret = SR_OK; |
ef0b9935 | 1019 | lines = g_strsplit(in->buf->str, inc->termination, 0); |
e53f32d2 | 1020 | for (line_idx = 0; (line = lines[line_idx]); line_idx++) { |
41d214f6 | 1021 | inc->line_number++; |
ef0b9935 GS |
1022 | if (inc->line_number < inc->start_line) { |
1023 | sr_spew("Line %zu skipped (before start).", inc->line_number); | |
1024 | continue; | |
1025 | } | |
df0db9fd | 1026 | if (line[0] == '\0') { |
41d214f6 | 1027 | sr_spew("Blank line %zu skipped.", inc->line_number); |
4a35548b MS |
1028 | continue; |
1029 | } | |
1030 | ||
1031 | /* Remove trailing comment. */ | |
df0db9fd GS |
1032 | strip_comment(line, inc->comment); |
1033 | if (line[0] == '\0') { | |
41d214f6 | 1034 | sr_spew("Comment-only line %zu skipped.", inc->line_number); |
4a35548b MS |
1035 | continue; |
1036 | } | |
1037 | ||
160691b9 | 1038 | /* Skip the header line, its content was used as the channel names. */ |
de8fe3b5 | 1039 | if (inc->use_header && !inc->header_seen) { |
160691b9 | 1040 | sr_spew("Header line %zu skipped.", inc->line_number); |
de8fe3b5 | 1041 | inc->header_seen = TRUE; |
160691b9 JS |
1042 | continue; |
1043 | } | |
1044 | ||
e53f32d2 GS |
1045 | /* Split the line into columns, check for minimum length. */ |
1046 | columns = split_line(line, inc); | |
df0db9fd | 1047 | if (!columns) { |
41d214f6 | 1048 | sr_err("Error while parsing line %zu.", inc->line_number); |
2355d229 | 1049 | g_strfreev(lines); |
4a35548b MS |
1050 | return SR_ERR; |
1051 | } | |
4a35548b | 1052 | num_columns = g_strv_length(columns); |
e53f32d2 GS |
1053 | if (num_columns < inc->column_want_count) { |
1054 | sr_err("Insufficient column count %zu in line %zu.", | |
1055 | num_columns, inc->line_number); | |
4a35548b | 1056 | g_strfreev(columns); |
2355d229 | 1057 | g_strfreev(lines); |
4a35548b MS |
1058 | return SR_ERR; |
1059 | } | |
1060 | ||
836fac9c | 1061 | /* Have the columns of the current text line processed. */ |
626c388a | 1062 | clear_logic_samples(inc); |
e53f32d2 GS |
1063 | for (col_idx = 0; col_idx < inc->column_want_count; col_idx++) { |
1064 | column = columns[col_idx]; | |
1065 | col_nr = col_idx + 1; | |
836fac9c GS |
1066 | details = lookup_column_details(inc, col_nr); |
1067 | if (!details || !details->text_format) | |
1068 | continue; | |
1069 | parse_func = col_parse_funcs[details->text_format]; | |
1070 | if (!parse_func) | |
1071 | continue; | |
1072 | ret = parse_func(column, inc, details); | |
e53f32d2 GS |
1073 | if (ret != SR_OK) { |
1074 | g_strfreev(columns); | |
1075 | g_strfreev(lines); | |
1076 | return SR_ERR; | |
1077 | } | |
4a35548b MS |
1078 | } |
1079 | ||
626c388a GS |
1080 | /* Send sample data to the session bus (buffered). */ |
1081 | ret = queue_logic_samples(in); | |
41d214f6 | 1082 | if (ret != SR_OK) { |
4a35548b | 1083 | sr_err("Sending samples failed."); |
cd59e6ec | 1084 | g_strfreev(columns); |
2355d229 | 1085 | g_strfreev(lines); |
4a35548b MS |
1086 | return SR_ERR; |
1087 | } | |
cd59e6ec | 1088 | |
41d214f6 BV |
1089 | g_strfreev(columns); |
1090 | } | |
1091 | g_strfreev(lines); | |
241c386a | 1092 | g_string_erase(in->buf, 0, p - in->buf->str); |
41d214f6 | 1093 | |
7066fd46 | 1094 | return ret; |
41d214f6 BV |
1095 | } |
1096 | ||
7066fd46 | 1097 | static int receive(struct sr_input *in, GString *buf) |
41d214f6 BV |
1098 | { |
1099 | struct context *inc; | |
7066fd46 BV |
1100 | int ret; |
1101 | ||
1102 | g_string_append_len(in->buf, buf->str, buf->len); | |
41d214f6 BV |
1103 | |
1104 | inc = in->priv; | |
1a920e33 | 1105 | if (!inc->column_seen_count) { |
df0db9fd GS |
1106 | ret = initial_receive(in); |
1107 | if (ret == SR_ERR_NA) | |
7066fd46 BV |
1108 | /* Not enough data yet. */ |
1109 | return SR_OK; | |
1110 | else if (ret != SR_OK) | |
1111 | return SR_ERR; | |
1112 | ||
1113 | /* sdi is ready, notify frontend. */ | |
1114 | in->sdi_ready = TRUE; | |
41d214f6 | 1115 | return SR_OK; |
7066fd46 BV |
1116 | } |
1117 | ||
7f4c3a62 | 1118 | ret = process_buffer(in, FALSE); |
7066fd46 BV |
1119 | |
1120 | return ret; | |
1121 | } | |
1122 | ||
1123 | static int end(struct sr_input *in) | |
1124 | { | |
1125 | struct context *inc; | |
7066fd46 | 1126 | int ret; |
41d214f6 | 1127 | |
7066fd46 | 1128 | if (in->sdi_ready) |
7f4c3a62 | 1129 | ret = process_buffer(in, TRUE); |
7066fd46 BV |
1130 | else |
1131 | ret = SR_OK; | |
cd59e6ec GS |
1132 | if (ret != SR_OK) |
1133 | return ret; | |
1134 | ||
626c388a | 1135 | ret = flush_logic_samples(in); |
cd59e6ec GS |
1136 | if (ret != SR_OK) |
1137 | return ret; | |
7066fd46 BV |
1138 | |
1139 | inc = in->priv; | |
3be42bc2 | 1140 | if (inc->started) |
bee2b016 | 1141 | std_session_send_df_end(in->sdi); |
4a35548b | 1142 | |
7066fd46 BV |
1143 | return ret; |
1144 | } | |
1145 | ||
d5cc282f | 1146 | static void cleanup(struct sr_input *in) |
7066fd46 BV |
1147 | { |
1148 | struct context *inc; | |
1149 | ||
affaf540 GS |
1150 | keep_header_for_reread(in); |
1151 | ||
7066fd46 BV |
1152 | inc = in->priv; |
1153 | ||
b1f83103 | 1154 | g_free(inc->termination); |
539188e5 | 1155 | inc->termination = NULL; |
cd59e6ec | 1156 | g_free(inc->datafeed_buffer); |
539188e5 | 1157 | inc->datafeed_buffer = NULL; |
4a35548b MS |
1158 | } |
1159 | ||
ad93bfb0 SA |
1160 | static int reset(struct sr_input *in) |
1161 | { | |
1162 | struct context *inc = in->priv; | |
1163 | ||
1164 | cleanup(in); | |
1165 | inc->started = FALSE; | |
1166 | g_string_truncate(in->buf, 0); | |
1167 | ||
1168 | return SR_OK; | |
1169 | } | |
1170 | ||
c6aa9870 | 1171 | enum option_index { |
2142a79b | 1172 | OPT_COL_FMTS, |
c6aa9870 GS |
1173 | OPT_SINGLE_COL, |
1174 | OPT_NUM_LOGIC, | |
1175 | OPT_DELIM, | |
1176 | OPT_FORMAT, | |
1177 | OPT_COMMENT, | |
1178 | OPT_RATE, | |
e53f32d2 | 1179 | OPT_FIRST_COL, |
c6aa9870 GS |
1180 | OPT_HEADER, |
1181 | OPT_START, | |
1182 | OPT_MAX, | |
1183 | }; | |
1184 | ||
41d214f6 | 1185 | static struct sr_option options[] = { |
2142a79b | 1186 | [OPT_COL_FMTS] = { "column-formats", "Column format specs", "Specifies text columns data types: comma separated list of [<cols>]<fmt>[<bits>]", NULL, NULL }, |
c6aa9870 GS |
1187 | [OPT_SINGLE_COL] = { "single-column", "Single column", "Enable single-column mode, using the specified column (>= 1); 0: multi-col. mode", NULL, NULL }, |
1188 | [OPT_NUM_LOGIC] = { "numchannels", "Number of logic channels", "The number of (logic) channels (single-col. mode: number of bits beginning at 'first channel', LSB-first)", NULL, NULL }, | |
1189 | [OPT_DELIM] = { "delimiter", "Column delimiter", "The column delimiter (>= 1 characters)", NULL, NULL }, | |
1190 | [OPT_FORMAT] = { "format", "Data format (single-col. mode)", "The numeric format of the data (single-col. mode): bin, hex, oct", NULL, NULL }, | |
1191 | [OPT_COMMENT] = { "comment", "Comment character(s)", "The comment prefix character(s)", NULL, NULL }, | |
1192 | [OPT_RATE] = { "samplerate", "Samplerate (Hz)", "The sample rate (used during capture) in Hz", NULL, NULL }, | |
e53f32d2 | 1193 | [OPT_FIRST_COL] = { "first-column", "First column", "The column number of the first channel (multi-col. mode)", NULL, NULL }, |
c6aa9870 GS |
1194 | [OPT_HEADER] = { "header", "Interpret first line as header (multi-col. mode)", "Treat the first line as header with channel names (multi-col. mode)", NULL, NULL }, |
1195 | [OPT_START] = { "startline", "Start line", "The line number at which to start processing samples (>= 1)", NULL, NULL }, | |
1196 | [OPT_MAX] = ALL_ZERO, | |
41d214f6 BV |
1197 | }; |
1198 | ||
2c240774 | 1199 | static const struct sr_option *get_options(void) |
41d214f6 | 1200 | { |
31c41782 UH |
1201 | GSList *l; |
1202 | ||
41d214f6 | 1203 | if (!options[0].def) { |
1a920e33 | 1204 | options[OPT_COL_FMTS].def = g_variant_ref_sink(g_variant_new_string("")); |
e53f32d2 GS |
1205 | options[OPT_SINGLE_COL].def = g_variant_ref_sink(g_variant_new_uint32(0)); |
1206 | options[OPT_NUM_LOGIC].def = g_variant_ref_sink(g_variant_new_uint32(0)); | |
c6aa9870 GS |
1207 | options[OPT_DELIM].def = g_variant_ref_sink(g_variant_new_string(",")); |
1208 | options[OPT_FORMAT].def = g_variant_ref_sink(g_variant_new_string("bin")); | |
31c41782 UH |
1209 | l = NULL; |
1210 | l = g_slist_append(l, g_variant_ref_sink(g_variant_new_string("bin"))); | |
1211 | l = g_slist_append(l, g_variant_ref_sink(g_variant_new_string("hex"))); | |
1212 | l = g_slist_append(l, g_variant_ref_sink(g_variant_new_string("oct"))); | |
c6aa9870 GS |
1213 | options[OPT_FORMAT].values = l; |
1214 | options[OPT_COMMENT].def = g_variant_ref_sink(g_variant_new_string(";")); | |
1215 | options[OPT_RATE].def = g_variant_ref_sink(g_variant_new_uint64(0)); | |
e53f32d2 | 1216 | options[OPT_FIRST_COL].def = g_variant_ref_sink(g_variant_new_uint32(1)); |
c6aa9870 | 1217 | options[OPT_HEADER].def = g_variant_ref_sink(g_variant_new_boolean(FALSE)); |
e53f32d2 | 1218 | options[OPT_START].def = g_variant_ref_sink(g_variant_new_uint32(1)); |
41d214f6 BV |
1219 | } |
1220 | ||
1221 | return options; | |
1222 | } | |
1223 | ||
d4c93774 | 1224 | SR_PRIV struct sr_input_module input_csv = { |
4a35548b | 1225 | .id = "csv", |
41d214f6 BV |
1226 | .name = "CSV", |
1227 | .desc = "Comma-separated values", | |
c7bc82ff | 1228 | .exts = (const char*[]){"csv", NULL}, |
41d214f6 | 1229 | .options = get_options, |
4a35548b | 1230 | .init = init, |
41d214f6 | 1231 | .receive = receive, |
7066fd46 | 1232 | .end = end, |
41d214f6 | 1233 | .cleanup = cleanup, |
ad93bfb0 | 1234 | .reset = reset, |
4a35548b | 1235 | }; |