]>
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> | |
5 | * | |
6 | * This program is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 3 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
e05f1827 GS |
20 | #include "config.h" |
21 | ||
22 | #include <glib.h> | |
4a35548b MS |
23 | #include <stdlib.h> |
24 | #include <string.h> | |
e05f1827 | 25 | |
c1aae900 | 26 | #include <libsigrok/libsigrok.h> |
4a35548b MS |
27 | #include "libsigrok-internal.h" |
28 | ||
3544f848 | 29 | #define LOG_PREFIX "input/csv" |
4a35548b | 30 | |
9a4fd01a | 31 | #define CHUNK_SIZE (4 * 1024 * 1024) |
cd59e6ec | 32 | |
4a35548b MS |
33 | /* |
34 | * The CSV input module has the following options: | |
35 | * | |
36 | * single-column: Specifies the column number which stores the sample data for | |
37 | * single column mode and enables single column mode. Multi | |
38 | * column mode is used if this parameter is omitted. | |
39 | * | |
ba7dd8bb UH |
40 | * numchannels: Specifies the number of channels to use. In multi column mode |
41 | * the number of channels are the number of columns and in single | |
4a35548b | 42 | * column mode the number of bits (LSB first) beginning at |
ba7dd8bb | 43 | * 'first-channel'. |
4a35548b MS |
44 | * |
45 | * delimiter: Specifies the delimiter for columns. Must be at least one | |
46 | * character. Comma is used as default delimiter. | |
47 | * | |
48 | * format: Specifies the format of the sample data in single column mode. | |
49 | * Available formats are: 'bin', 'hex' and 'oct'. The binary | |
50 | * format is used by default. This option has no effect in multi | |
51 | * column mode. | |
52 | * | |
53 | * comment: Specifies the prefix character(s) for comments. No prefix | |
54 | * characters are used by default which disables removing of | |
55 | * comments. | |
56 | * | |
57 | * samplerate: Samplerate which the sample data was captured with. Default | |
58 | * value is 0. | |
59 | * | |
ba7dd8bb UH |
60 | * first-channel: Column number of the first channel in multi column mode and |
61 | * position of the bit for the first channel in single column mode. | |
4a35548b MS |
62 | * Default value is 0. |
63 | * | |
64 | * header: Determines if the first line should be treated as header | |
ba7dd8bb UH |
65 | * and used for channel names in multi column mode. Empty header |
66 | * names will be replaced by the channel number. If enabled in | |
4a35548b MS |
67 | * single column mode the first line will be skipped. Usage of |
68 | * header is disabled by default. | |
69 | * | |
70 | * startline: Line number to start processing sample data. Must be greater | |
71 | * than 0. The default line number to start processing is 1. | |
72 | */ | |
73 | ||
ccff468b GS |
74 | /* |
75 | * TODO | |
76 | * | |
77 | * - Determine how the text line handling can get improved, regarding | |
78 | * all of robustness and flexibility and correctness. | |
79 | * - The current implementation splits on "any run of CR and LF". Which | |
80 | * translates to: Line numbers are wrong in the presence of empty | |
de788af4 | 81 | * lines in the input stream. See below for an (expensive) fix. |
ccff468b GS |
82 | * - Dropping support for CR style end-of-line markers could improve |
83 | * the situation a lot. Code could search for and split on LF, and | |
84 | * trim optional trailing CR. This would result in proper support | |
85 | * for CRLF (Windows) as well as LF (Unix), and allow for correct | |
86 | * line number counts. | |
87 | * - When support for CR-only line termination cannot get dropped, | |
88 | * then the current implementation is inappropriate. Currently the | |
89 | * input stream is scanned for the first occurance of either of the | |
90 | * supported termination styles (which is good). For the remaining | |
91 | * session a consistent encoding of the text lines is assumed (which | |
de788af4 | 92 | * is acceptable). |
ccff468b GS |
93 | * - When line numbers need to be correct and reliable, _and_ the full |
94 | * set of previously supported line termination sequences are required, | |
95 | * and potentially more are to get added for improved compatibility | |
96 | * with more platforms or generators, then the current approach of | |
97 | * splitting on runs of termination characters needs to get replaced, | |
98 | * by the more expensive approach to scan for and count the initially | |
99 | * determined termination sequence. | |
100 | * | |
101 | * - Add support for analog input data? (optional) | |
102 | * - Needs a syntax first for user specs which channels (columns) are | |
103 | * logic and which are analog. May need heuristics(?) to guess from | |
104 | * input data in the absence of user provided specs. | |
105 | */ | |
106 | ||
4a35548b | 107 | /* Single column formats. */ |
ad6a2bee | 108 | enum single_col_format { |
4a35548b MS |
109 | FORMAT_BIN, |
110 | FORMAT_HEX, | |
ad6a2bee | 111 | FORMAT_OCT, |
4a35548b MS |
112 | }; |
113 | ||
114 | struct context { | |
41d214f6 BV |
115 | gboolean started; |
116 | ||
4a35548b MS |
117 | /* Current selected samplerate. */ |
118 | uint64_t samplerate; | |
119 | ||
ba7dd8bb | 120 | /* Number of channels. */ |
ad6a2bee | 121 | size_t num_channels; |
4a35548b MS |
122 | |
123 | /* Column delimiter character(s). */ | |
124 | GString *delimiter; | |
125 | ||
126 | /* Comment prefix character(s). */ | |
127 | GString *comment; | |
128 | ||
d9251a2c | 129 | /* Termination character(s) used in current stream. */ |
41d214f6 BV |
130 | char *termination; |
131 | ||
4a35548b MS |
132 | /* Determines if sample data is stored in multiple columns. */ |
133 | gboolean multi_column_mode; | |
134 | ||
135 | /* Column number of the sample data in single column mode. */ | |
ad6a2bee | 136 | size_t single_column; |
4a35548b MS |
137 | |
138 | /* | |
139 | * Number of the first column to parse. Equivalent to the number of the | |
ba7dd8bb | 140 | * first channel in multi column mode and the single column number in |
4a35548b MS |
141 | * single column mode. |
142 | */ | |
ad6a2bee | 143 | size_t first_column; |
4a35548b MS |
144 | |
145 | /* | |
ba7dd8bb UH |
146 | * Column number of the first channel in multi column mode and position of |
147 | * the bit for the first channel in single column mode. | |
4a35548b | 148 | */ |
ad6a2bee | 149 | size_t first_channel; |
4a35548b MS |
150 | |
151 | /* Line number to start processing. */ | |
6433156c | 152 | size_t start_line; |
4a35548b MS |
153 | |
154 | /* | |
155 | * Determines if the first line should be treated as header and used for | |
ba7dd8bb | 156 | * channel names in multi column mode. |
4a35548b MS |
157 | */ |
158 | gboolean header; | |
159 | ||
160 | /* Format sample data is stored in single column mode. */ | |
ad6a2bee | 161 | enum single_col_format format; |
4a35548b | 162 | |
cd59e6ec GS |
163 | size_t sample_unit_size; /**!< Byte count for a single sample. */ |
164 | uint8_t *sample_buffer; /**!< Buffer for a single sample. */ | |
4a35548b | 165 | |
cd59e6ec GS |
166 | uint8_t *datafeed_buffer; /**!< Queue for datafeed submission. */ |
167 | size_t datafeed_buf_size; | |
168 | size_t datafeed_buf_fill; | |
4a35548b | 169 | |
4a35548b | 170 | /* Current line number. */ |
6433156c | 171 | size_t line_number; |
affaf540 GS |
172 | |
173 | /* List of previously created sigrok channels. */ | |
174 | GSList *prev_sr_channels; | |
4a35548b MS |
175 | }; |
176 | ||
41d214f6 | 177 | static void strip_comment(char *buf, const GString *prefix) |
4a35548b MS |
178 | { |
179 | char *ptr; | |
180 | ||
181 | if (!prefix->len) | |
182 | return; | |
183 | ||
b2c4dde2 | 184 | if ((ptr = strstr(buf, prefix->str))) { |
41d214f6 | 185 | *ptr = '\0'; |
b2c4dde2 GS |
186 | g_strstrip(buf); |
187 | } | |
4a35548b MS |
188 | } |
189 | ||
41d214f6 | 190 | static int parse_binstr(const char *str, struct context *inc) |
4a35548b MS |
191 | { |
192 | gsize i, j, length; | |
193 | ||
194 | length = strlen(str); | |
195 | ||
196 | if (!length) { | |
ad6a2bee | 197 | sr_err("Column %zu in line %zu is empty.", inc->single_column, |
41d214f6 | 198 | inc->line_number); |
4a35548b MS |
199 | return SR_ERR; |
200 | } | |
201 | ||
202 | /* Clear buffer in order to set bits only. */ | |
cd59e6ec | 203 | memset(inc->sample_buffer, 0, inc->sample_unit_size); |
4a35548b | 204 | |
41d214f6 | 205 | i = inc->first_channel; |
4a35548b | 206 | |
41d214f6 | 207 | for (j = 0; i < length && j < inc->num_channels; i++, j++) { |
4a35548b | 208 | if (str[length - i - 1] == '1') { |
41d214f6 | 209 | inc->sample_buffer[j / 8] |= (1 << (j % 8)); |
4a35548b | 210 | } else if (str[length - i - 1] != '0') { |
ad6a2bee | 211 | sr_err("Invalid value '%s' in column %zu in line %zu.", |
41d214f6 | 212 | str, inc->single_column, inc->line_number); |
4a35548b MS |
213 | return SR_ERR; |
214 | } | |
215 | } | |
216 | ||
217 | return SR_OK; | |
218 | } | |
219 | ||
41d214f6 | 220 | static int parse_hexstr(const char *str, struct context *inc) |
4a35548b MS |
221 | { |
222 | gsize i, j, k, length; | |
223 | uint8_t value; | |
224 | char c; | |
225 | ||
226 | length = strlen(str); | |
227 | ||
228 | if (!length) { | |
ad6a2bee | 229 | sr_err("Column %zu in line %zu is empty.", inc->single_column, |
41d214f6 | 230 | inc->line_number); |
4a35548b MS |
231 | return SR_ERR; |
232 | } | |
233 | ||
234 | /* Clear buffer in order to set bits only. */ | |
cd59e6ec | 235 | memset(inc->sample_buffer, 0, inc->sample_unit_size); |
4a35548b MS |
236 | |
237 | /* Calculate the position of the first hexadecimal digit. */ | |
41d214f6 | 238 | i = inc->first_channel / 4; |
4a35548b | 239 | |
41d214f6 | 240 | for (j = 0; i < length && j < inc->num_channels; i++) { |
4a35548b MS |
241 | c = str[length - i - 1]; |
242 | ||
243 | if (!g_ascii_isxdigit(c)) { | |
ad6a2bee | 244 | sr_err("Invalid value '%s' in column %zu in line %zu.", |
41d214f6 | 245 | str, inc->single_column, inc->line_number); |
4a35548b MS |
246 | return SR_ERR; |
247 | } | |
248 | ||
249 | value = g_ascii_xdigit_value(c); | |
250 | ||
41d214f6 | 251 | k = (inc->first_channel + j) % 4; |
4a35548b | 252 | |
41d214f6 | 253 | for (; j < inc->num_channels && k < 4; k++) { |
4a35548b | 254 | if (value & (1 << k)) |
41d214f6 | 255 | inc->sample_buffer[j / 8] |= (1 << (j % 8)); |
4a35548b MS |
256 | |
257 | j++; | |
258 | } | |
259 | } | |
260 | ||
261 | return SR_OK; | |
262 | } | |
263 | ||
41d214f6 | 264 | static int parse_octstr(const char *str, struct context *inc) |
4a35548b MS |
265 | { |
266 | gsize i, j, k, length; | |
267 | uint8_t value; | |
268 | char c; | |
269 | ||
270 | length = strlen(str); | |
271 | ||
272 | if (!length) { | |
ad6a2bee | 273 | sr_err("Column %zu in line %zu is empty.", inc->single_column, |
41d214f6 | 274 | inc->line_number); |
4a35548b MS |
275 | return SR_ERR; |
276 | } | |
277 | ||
278 | /* Clear buffer in order to set bits only. */ | |
cd59e6ec | 279 | memset(inc->sample_buffer, 0, inc->sample_unit_size); |
4a35548b MS |
280 | |
281 | /* Calculate the position of the first octal digit. */ | |
41d214f6 | 282 | i = inc->first_channel / 3; |
4a35548b | 283 | |
41d214f6 | 284 | for (j = 0; i < length && j < inc->num_channels; i++) { |
4a35548b MS |
285 | c = str[length - i - 1]; |
286 | ||
287 | if (c < '0' || c > '7') { | |
ad6a2bee | 288 | sr_err("Invalid value '%s' in column %zu in line %zu.", |
41d214f6 | 289 | str, inc->single_column, inc->line_number); |
4a35548b MS |
290 | return SR_ERR; |
291 | } | |
292 | ||
293 | value = g_ascii_xdigit_value(c); | |
294 | ||
41d214f6 | 295 | k = (inc->first_channel + j) % 3; |
4a35548b | 296 | |
41d214f6 | 297 | for (; j < inc->num_channels && k < 3; k++) { |
4a35548b | 298 | if (value & (1 << k)) |
41d214f6 | 299 | inc->sample_buffer[j / 8] |= (1 << (j % 8)); |
4a35548b MS |
300 | |
301 | j++; | |
302 | } | |
303 | } | |
304 | ||
305 | return SR_OK; | |
306 | } | |
307 | ||
ad6a2bee GS |
308 | /** |
309 | * @brief Splits a text line into a set of columns. | |
310 | * | |
311 | * @param[in] buf The input text line to split. | |
312 | * @param[in] inc The input module's context. | |
313 | * @param[in] max_cols The maximum column count, negative to get all of them. | |
314 | * | |
315 | * @returns An array of strings, representing the columns' text. | |
316 | */ | |
317 | static char **parse_line(char *buf, struct context *inc, ssize_t max_cols) | |
4a35548b MS |
318 | { |
319 | const char *str, *remainder; | |
320 | GSList *list, *l; | |
321 | char **columns; | |
322 | char *column; | |
9eab4435 | 323 | gsize seen, taken; |
4a35548b | 324 | |
9eab4435 GS |
325 | seen = 0; |
326 | taken = 0; | |
4a35548b MS |
327 | list = NULL; |
328 | ||
41d214f6 BV |
329 | remainder = buf; |
330 | str = strstr(remainder, inc->delimiter->str); | |
ad6a2bee | 331 | while (str && max_cols) { |
9eab4435 | 332 | if (seen >= inc->first_column) { |
4a35548b MS |
333 | column = g_strndup(remainder, str - remainder); |
334 | list = g_slist_prepend(list, g_strstrip(column)); | |
335 | ||
ad6a2bee | 336 | max_cols--; |
9eab4435 | 337 | taken++; |
4a35548b MS |
338 | } |
339 | ||
41d214f6 BV |
340 | remainder = str + inc->delimiter->len; |
341 | str = strstr(remainder, inc->delimiter->str); | |
9eab4435 | 342 | seen++; |
4a35548b MS |
343 | } |
344 | ||
9eab4435 | 345 | if (buf[0] && max_cols && seen >= inc->first_column) { |
4a35548b MS |
346 | column = g_strdup(remainder); |
347 | list = g_slist_prepend(list, g_strstrip(column)); | |
9eab4435 | 348 | taken++; |
4a35548b MS |
349 | } |
350 | ||
9eab4435 | 351 | if (!(columns = g_try_new(char *, taken + 1))) |
4a35548b | 352 | return NULL; |
9eab4435 | 353 | columns[taken--] = NULL; |
4a35548b | 354 | for (l = list; l; l = l->next) |
9eab4435 | 355 | columns[taken--] = l->data; |
4a35548b MS |
356 | |
357 | g_slist_free(list); | |
358 | ||
359 | return columns; | |
360 | } | |
361 | ||
41d214f6 | 362 | static int parse_multi_columns(char **columns, struct context *inc) |
4a35548b MS |
363 | { |
364 | gsize i; | |
df0db9fd | 365 | char *column; |
4a35548b MS |
366 | |
367 | /* Clear buffer in order to set bits only. */ | |
cd59e6ec | 368 | memset(inc->sample_buffer, 0, inc->sample_unit_size); |
4a35548b | 369 | |
41d214f6 | 370 | for (i = 0; i < inc->num_channels; i++) { |
df0db9fd GS |
371 | column = columns[i]; |
372 | if (column[0] == '1') { | |
41d214f6 | 373 | inc->sample_buffer[i / 8] |= (1 << (i % 8)); |
df0db9fd | 374 | } else if (!strlen(column)) { |
4a35548b | 375 | sr_err("Column %zu in line %zu is empty.", |
41d214f6 | 376 | inc->first_channel + i, inc->line_number); |
4a35548b | 377 | return SR_ERR; |
df0db9fd | 378 | } else if (column[0] != '0') { |
4a35548b | 379 | sr_err("Invalid value '%s' in column %zu in line %zu.", |
df0db9fd | 380 | column, inc->first_channel + i, |
41d214f6 | 381 | inc->line_number); |
4a35548b MS |
382 | return SR_ERR; |
383 | } | |
384 | } | |
385 | ||
386 | return SR_OK; | |
387 | } | |
388 | ||
41d214f6 | 389 | static int parse_single_column(const char *column, struct context *inc) |
4a35548b MS |
390 | { |
391 | int res; | |
392 | ||
393 | res = SR_ERR; | |
394 | ||
0c5f2abc | 395 | switch (inc->format) { |
4a35548b | 396 | case FORMAT_BIN: |
41d214f6 | 397 | res = parse_binstr(column, inc); |
4a35548b MS |
398 | break; |
399 | case FORMAT_HEX: | |
41d214f6 | 400 | res = parse_hexstr(column, inc); |
4a35548b MS |
401 | break; |
402 | case FORMAT_OCT: | |
41d214f6 | 403 | res = parse_octstr(column, inc); |
4a35548b MS |
404 | break; |
405 | } | |
406 | ||
407 | return res; | |
408 | } | |
409 | ||
cd59e6ec | 410 | static int flush_samples(const struct sr_input *in) |
4a35548b | 411 | { |
cd59e6ec | 412 | struct context *inc; |
4a35548b MS |
413 | struct sr_datafeed_packet packet; |
414 | struct sr_datafeed_logic logic; | |
cd59e6ec | 415 | int rc; |
4a35548b | 416 | |
cd59e6ec GS |
417 | inc = in->priv; |
418 | if (!inc->datafeed_buf_fill) | |
419 | return SR_OK; | |
420 | ||
421 | memset(&packet, 0, sizeof(packet)); | |
422 | memset(&logic, 0, sizeof(logic)); | |
4a35548b MS |
423 | packet.type = SR_DF_LOGIC; |
424 | packet.payload = &logic; | |
cd59e6ec GS |
425 | logic.unitsize = inc->sample_unit_size; |
426 | logic.length = inc->datafeed_buf_fill; | |
427 | logic.data = inc->datafeed_buffer; | |
428 | ||
429 | rc = sr_session_send(in->sdi, &packet); | |
430 | if (rc != SR_OK) | |
431 | return rc; | |
4a35548b | 432 | |
cd59e6ec GS |
433 | inc->datafeed_buf_fill = 0; |
434 | return SR_OK; | |
435 | } | |
436 | ||
437 | static int queue_samples(const struct sr_input *in) | |
438 | { | |
439 | struct context *inc; | |
440 | int rc; | |
441 | ||
442 | inc = in->priv; | |
443 | ||
444 | inc->datafeed_buf_fill += inc->sample_unit_size; | |
445 | if (inc->datafeed_buf_fill == inc->datafeed_buf_size) { | |
446 | rc = flush_samples(in); | |
447 | if (rc != SR_OK) | |
448 | return rc; | |
449 | } | |
450 | inc->sample_buffer = &inc->datafeed_buffer[inc->datafeed_buf_fill]; | |
4a35548b MS |
451 | return SR_OK; |
452 | } | |
453 | ||
41d214f6 | 454 | static int init(struct sr_input *in, GHashTable *options) |
4a35548b | 455 | { |
41d214f6 BV |
456 | struct context *inc; |
457 | const char *s; | |
4a35548b | 458 | |
aac29cc1 | 459 | in->sdi = g_malloc0(sizeof(struct sr_dev_inst)); |
41d214f6 | 460 | in->priv = inc = g_malloc0(sizeof(struct context)); |
4a35548b | 461 | |
ad6a2bee | 462 | inc->single_column = g_variant_get_uint32(g_hash_table_lookup(options, "single-column")); |
41d214f6 | 463 | inc->multi_column_mode = inc->single_column == 0; |
4a35548b | 464 | |
ad6a2bee | 465 | inc->num_channels = g_variant_get_uint32(g_hash_table_lookup(options, "numchannels")); |
4a35548b | 466 | |
41d214f6 BV |
467 | inc->delimiter = g_string_new(g_variant_get_string( |
468 | g_hash_table_lookup(options, "delimiter"), NULL)); | |
469 | if (inc->delimiter->len == 0) { | |
470 | sr_err("Delimiter must be at least one character."); | |
471 | return SR_ERR_ARG; | |
4a35548b MS |
472 | } |
473 | ||
41d214f6 BV |
474 | s = g_variant_get_string(g_hash_table_lookup(options, "format"), NULL); |
475 | if (!g_ascii_strncasecmp(s, "bin", 3)) { | |
476 | inc->format = FORMAT_BIN; | |
477 | } else if (!g_ascii_strncasecmp(s, "hex", 3)) { | |
478 | inc->format = FORMAT_HEX; | |
479 | } else if (!g_ascii_strncasecmp(s, "oct", 3)) { | |
480 | inc->format = FORMAT_OCT; | |
481 | } else { | |
482 | sr_err("Invalid format: '%s'", s); | |
483 | return SR_ERR_ARG; | |
4a35548b MS |
484 | } |
485 | ||
41d214f6 BV |
486 | inc->comment = g_string_new(g_variant_get_string( |
487 | g_hash_table_lookup(options, "comment"), NULL)); | |
488 | if (g_string_equal(inc->comment, inc->delimiter)) { | |
489 | /* That's never going to work. Likely the result of the user | |
490 | * setting the delimiter to ; -- the default comment. Clearing | |
491 | * the comment setting will work in that case. */ | |
492 | g_string_truncate(inc->comment, 0); | |
4a35548b MS |
493 | } |
494 | ||
6e8d95a5 | 495 | inc->samplerate = g_variant_get_uint64(g_hash_table_lookup(options, "samplerate")); |
4a35548b | 496 | |
ad6a2bee | 497 | inc->first_channel = g_variant_get_uint32(g_hash_table_lookup(options, "first-channel")); |
4a35548b | 498 | |
41d214f6 | 499 | inc->header = g_variant_get_boolean(g_hash_table_lookup(options, "header")); |
4a35548b | 500 | |
ad6a2bee | 501 | inc->start_line = g_variant_get_uint32(g_hash_table_lookup(options, "startline")); |
41d214f6 | 502 | if (inc->start_line < 1) { |
6433156c | 503 | sr_err("Invalid start line %zu.", inc->start_line); |
41d214f6 | 504 | return SR_ERR_ARG; |
4a35548b MS |
505 | } |
506 | ||
41d214f6 BV |
507 | if (inc->multi_column_mode) |
508 | inc->first_column = inc->first_channel; | |
4a35548b | 509 | else |
41d214f6 | 510 | inc->first_column = inc->single_column; |
4a35548b | 511 | |
41d214f6 | 512 | if (!inc->multi_column_mode && !inc->num_channels) { |
ba7dd8bb | 513 | sr_err("Number of channels needs to be specified in single column mode."); |
41d214f6 | 514 | return SR_ERR_ARG; |
4a35548b MS |
515 | } |
516 | ||
41d214f6 BV |
517 | return SR_OK; |
518 | } | |
4a35548b | 519 | |
affaf540 GS |
520 | /* |
521 | * Check the channel list for consistency across file re-import. See | |
522 | * the VCD input module for more details and motivation. | |
523 | */ | |
524 | ||
525 | static void keep_header_for_reread(const struct sr_input *in) | |
526 | { | |
527 | struct context *inc; | |
528 | ||
529 | inc = in->priv; | |
530 | g_slist_free_full(inc->prev_sr_channels, sr_channel_free_cb); | |
531 | inc->prev_sr_channels = in->sdi->channels; | |
532 | in->sdi->channels = NULL; | |
533 | } | |
534 | ||
535 | static int check_header_in_reread(const struct sr_input *in) | |
536 | { | |
537 | struct context *inc; | |
538 | ||
539 | if (!in) | |
540 | return FALSE; | |
541 | inc = in->priv; | |
542 | if (!inc) | |
543 | return FALSE; | |
544 | if (!inc->prev_sr_channels) | |
545 | return TRUE; | |
546 | ||
547 | if (sr_channel_lists_differ(inc->prev_sr_channels, in->sdi->channels)) { | |
548 | sr_err("Channel list change not supported for file re-read."); | |
549 | return FALSE; | |
550 | } | |
551 | g_slist_free_full(in->sdi->channels, sr_channel_free_cb); | |
552 | in->sdi->channels = inc->prev_sr_channels; | |
553 | inc->prev_sr_channels = NULL; | |
554 | ||
555 | return TRUE; | |
556 | } | |
557 | ||
492dfa90 GS |
558 | static const char *delim_set = "\r\n"; |
559 | ||
329733d9 | 560 | static const char *get_line_termination(GString *buf) |
41d214f6 | 561 | { |
329733d9 | 562 | const char *term; |
4a35548b | 563 | |
41d214f6 BV |
564 | term = NULL; |
565 | if (g_strstr_len(buf->str, buf->len, "\r\n")) | |
566 | term = "\r\n"; | |
567 | else if (memchr(buf->str, '\n', buf->len)) | |
568 | term = "\n"; | |
569 | else if (memchr(buf->str, '\r', buf->len)) | |
570 | term = "\r"; | |
4a35548b | 571 | |
41d214f6 BV |
572 | return term; |
573 | } | |
4a35548b | 574 | |
41d214f6 BV |
575 | static int initial_parse(const struct sr_input *in, GString *buf) |
576 | { | |
577 | struct context *inc; | |
41d214f6 | 578 | GString *channel_name; |
ad6a2bee | 579 | size_t num_columns, i; |
6433156c | 580 | size_t line_number, l; |
41d214f6 | 581 | int ret; |
df0db9fd | 582 | char **lines, *line, **columns, *column; |
41d214f6 BV |
583 | |
584 | ret = SR_OK; | |
585 | inc = in->priv; | |
586 | columns = NULL; | |
587 | ||
588 | line_number = 0; | |
492dfa90 | 589 | lines = g_strsplit_set(buf->str, delim_set, 0); |
41d214f6 BV |
590 | for (l = 0; lines[l]; l++) { |
591 | line_number++; | |
df0db9fd | 592 | line = lines[l]; |
41d214f6 BV |
593 | if (inc->start_line > line_number) { |
594 | sr_spew("Line %zu skipped.", line_number); | |
4a35548b MS |
595 | continue; |
596 | } | |
df0db9fd | 597 | if (line[0] == '\0') { |
41d214f6 BV |
598 | sr_spew("Blank line %zu skipped.", line_number); |
599 | continue; | |
600 | } | |
df0db9fd GS |
601 | strip_comment(line, inc->comment); |
602 | if (line[0] == '\0') { | |
41d214f6 | 603 | sr_spew("Comment-only line %zu skipped.", line_number); |
4a35548b MS |
604 | continue; |
605 | } | |
606 | ||
41d214f6 BV |
607 | /* Reached first proper line. */ |
608 | break; | |
609 | } | |
610 | if (!lines[l]) { | |
611 | /* Not enough data for a proper line yet. */ | |
60107497 | 612 | ret = SR_ERR_NA; |
41d214f6 | 613 | goto out; |
4a35548b MS |
614 | } |
615 | ||
616 | /* | |
617 | * In order to determine the number of columns parse the current line | |
618 | * without limiting the number of columns. | |
619 | */ | |
df0db9fd GS |
620 | columns = parse_line(line, inc, -1); |
621 | if (!columns) { | |
41d214f6 BV |
622 | sr_err("Error while parsing line %zu.", line_number); |
623 | ret = SR_ERR; | |
624 | goto out; | |
4a35548b | 625 | } |
4a35548b MS |
626 | num_columns = g_strv_length(columns); |
627 | ||
628 | /* Ensure that the first column is not out of bounds. */ | |
629 | if (!num_columns) { | |
ad6a2bee | 630 | sr_err("Column %zu in line %zu is out of bounds.", |
41d214f6 BV |
631 | inc->first_column, line_number); |
632 | ret = SR_ERR; | |
633 | goto out; | |
4a35548b MS |
634 | } |
635 | ||
41d214f6 | 636 | if (inc->multi_column_mode) { |
4a35548b | 637 | /* |
ba7dd8bb | 638 | * Detect the number of channels in multi column mode |
4a35548b MS |
639 | * automatically if not specified. |
640 | */ | |
41d214f6 BV |
641 | if (!inc->num_channels) { |
642 | inc->num_channels = num_columns; | |
ad6a2bee | 643 | sr_dbg("Number of auto-detected channels: %zu.", |
41d214f6 | 644 | inc->num_channels); |
4a35548b MS |
645 | } |
646 | ||
647 | /* | |
ba7dd8bb | 648 | * Ensure that the number of channels does not exceed the number |
4a35548b MS |
649 | * of columns in multi column mode. |
650 | */ | |
41d214f6 | 651 | if (num_columns < inc->num_channels) { |
ba7dd8bb | 652 | sr_err("Not enough columns for desired number of channels in line %zu.", |
41d214f6 BV |
653 | line_number); |
654 | ret = SR_ERR; | |
655 | goto out; | |
4a35548b MS |
656 | } |
657 | } | |
658 | ||
41d214f6 BV |
659 | channel_name = g_string_sized_new(64); |
660 | for (i = 0; i < inc->num_channels; i++) { | |
df0db9fd GS |
661 | column = columns[i]; |
662 | if (inc->header && inc->multi_column_mode && column[0] != '\0') | |
663 | g_string_assign(channel_name, column); | |
4a35548b | 664 | else |
ad6a2bee | 665 | g_string_printf(channel_name, "%zu", i); |
5e23fcab | 666 | sr_channel_new(in->sdi, i, SR_CHANNEL_LOGIC, TRUE, channel_name->str); |
4a35548b | 667 | } |
41d214f6 | 668 | g_string_free(channel_name, TRUE); |
affaf540 GS |
669 | if (!check_header_in_reread(in)) { |
670 | ret = SR_ERR_DATA; | |
671 | goto out; | |
672 | } | |
4a35548b MS |
673 | |
674 | /* | |
cd59e6ec GS |
675 | * Calculate the minimum buffer size to store the set of samples |
676 | * of all channels (unit size). Determine a larger buffer size | |
677 | * for datafeed submission that is a multiple of the unit size. | |
678 | * Allocate the larger buffer, and have the "sample buffer" point | |
679 | * to a location within that large buffer. | |
4a35548b | 680 | */ |
cd59e6ec | 681 | inc->sample_unit_size = (inc->num_channels + 7) / 8; |
8bc2fa6d | 682 | inc->datafeed_buf_size = CHUNK_SIZE; |
cd59e6ec GS |
683 | inc->datafeed_buf_size *= inc->sample_unit_size; |
684 | inc->datafeed_buffer = g_malloc(inc->datafeed_buf_size); | |
685 | inc->datafeed_buf_fill = 0; | |
686 | inc->sample_buffer = &inc->datafeed_buffer[inc->datafeed_buf_fill]; | |
4a35548b | 687 | |
41d214f6 BV |
688 | out: |
689 | if (columns) | |
690 | g_strfreev(columns); | |
691 | g_strfreev(lines); | |
4a35548b | 692 | |
41d214f6 | 693 | return ret; |
4a35548b MS |
694 | } |
695 | ||
4439363a GS |
696 | /* |
697 | * Gets called from initial_receive(), which runs until the end-of-line | |
698 | * encoding of the input stream could get determined. Assumes that this | |
699 | * routine receives enough buffered initial input data to either see the | |
700 | * BOM when there is one, or that no BOM will follow when a text line | |
701 | * termination sequence was seen. Silently drops the UTF-8 BOM sequence | |
702 | * from the input buffer if one was seen. Does not care to protect | |
703 | * against multiple execution or dropping the BOM multiple times -- | |
704 | * there should be at most one in the input stream. | |
705 | */ | |
706 | static void initial_bom_check(const struct sr_input *in) | |
707 | { | |
708 | static const char *utf8_bom = "\xef\xbb\xbf"; | |
709 | ||
710 | if (in->buf->len < strlen(utf8_bom)) | |
711 | return; | |
712 | if (strncmp(in->buf->str, utf8_bom, strlen(utf8_bom)) != 0) | |
713 | return; | |
714 | g_string_erase(in->buf, 0, strlen(utf8_bom)); | |
715 | } | |
716 | ||
41d214f6 | 717 | static int initial_receive(const struct sr_input *in) |
4a35548b | 718 | { |
41d214f6 BV |
719 | struct context *inc; |
720 | GString *new_buf; | |
721 | int len, ret; | |
329733d9 UH |
722 | char *p; |
723 | const char *termination; | |
4a35548b | 724 | |
4439363a GS |
725 | initial_bom_check(in); |
726 | ||
41d214f6 | 727 | inc = in->priv; |
4a35548b | 728 | |
df0db9fd GS |
729 | termination = get_line_termination(in->buf); |
730 | if (!termination) | |
41d214f6 | 731 | /* Don't have a full line yet. */ |
d0181813 | 732 | return SR_ERR_NA; |
4a35548b | 733 | |
df0db9fd GS |
734 | p = g_strrstr_len(in->buf->str, in->buf->len, termination); |
735 | if (!p) | |
41d214f6 | 736 | /* Don't have a full line yet. */ |
d0181813 | 737 | return SR_ERR_NA; |
41d214f6 BV |
738 | len = p - in->buf->str - 1; |
739 | new_buf = g_string_new_len(in->buf->str, len); | |
740 | g_string_append_c(new_buf, '\0'); | |
4a35548b | 741 | |
41d214f6 BV |
742 | inc->termination = g_strdup(termination); |
743 | ||
744 | if (in->buf->str[0] != '\0') | |
745 | ret = initial_parse(in, new_buf); | |
746 | else | |
747 | ret = SR_OK; | |
748 | ||
749 | g_string_free(new_buf, TRUE); | |
750 | ||
751 | return ret; | |
752 | } | |
753 | ||
7f4c3a62 | 754 | static int process_buffer(struct sr_input *in, gboolean is_eof) |
41d214f6 BV |
755 | { |
756 | struct sr_datafeed_packet packet; | |
757 | struct sr_datafeed_meta meta; | |
758 | struct sr_config *src; | |
759 | struct context *inc; | |
760 | gsize num_columns; | |
761 | uint64_t samplerate; | |
ad6a2bee GS |
762 | size_t max_columns, l; |
763 | int ret; | |
df0db9fd | 764 | char *p, **lines, *line, **columns; |
41d214f6 | 765 | |
41d214f6 | 766 | inc = in->priv; |
d0181813 | 767 | if (!inc->started) { |
bee2b016 | 768 | std_session_send_df_header(in->sdi); |
41d214f6 BV |
769 | |
770 | if (inc->samplerate) { | |
771 | packet.type = SR_DF_META; | |
772 | packet.payload = &meta; | |
773 | samplerate = inc->samplerate; | |
774 | src = sr_config_new(SR_CONF_SAMPLERATE, g_variant_new_uint64(samplerate)); | |
775 | meta.config = g_slist_append(NULL, src); | |
776 | sr_session_send(in->sdi, &packet); | |
c01378c9 | 777 | g_slist_free(meta.config); |
41d214f6 BV |
778 | sr_config_free(src); |
779 | } | |
d0181813 BV |
780 | |
781 | inc->started = TRUE; | |
4a35548b MS |
782 | } |
783 | ||
f9b74861 GS |
784 | /* Limit the number of columns to parse. */ |
785 | if (inc->multi_column_mode) | |
786 | max_columns = inc->num_channels; | |
787 | else | |
788 | max_columns = 1; | |
789 | ||
4555d3bd GS |
790 | /* |
791 | * Consider empty input non-fatal. Keep accumulating input until | |
792 | * at least one full text line has become available. Grab the | |
793 | * maximum amount of accumulated data that consists of full text | |
794 | * lines, and process what has been received so far, leaving not | |
795 | * yet complete lines for the next invocation. | |
7f4c3a62 GS |
796 | * |
797 | * Enforce that all previously buffered data gets processed in | |
798 | * the "EOF" condition. Do not insist in the presence of the | |
799 | * termination sequence for the last line (may often be missing | |
800 | * on Windows). A present termination sequence will just result | |
801 | * in the "execution of an empty line", and does not harm. | |
4555d3bd GS |
802 | */ |
803 | if (!in->buf->len) | |
804 | return SR_OK; | |
7f4c3a62 GS |
805 | if (is_eof) { |
806 | p = in->buf->str + in->buf->len; | |
807 | } else { | |
808 | p = g_strrstr_len(in->buf->str, in->buf->len, inc->termination); | |
809 | if (!p) | |
810 | return SR_ERR; | |
811 | *p = '\0'; | |
812 | p += strlen(inc->termination); | |
813 | } | |
41d214f6 | 814 | g_strstrip(in->buf->str); |
4a35548b | 815 | |
18078d05 | 816 | ret = SR_OK; |
492dfa90 | 817 | lines = g_strsplit_set(in->buf->str, delim_set, 0); |
41d214f6 BV |
818 | for (l = 0; lines[l]; l++) { |
819 | inc->line_number++; | |
df0db9fd GS |
820 | line = lines[l]; |
821 | if (line[0] == '\0') { | |
41d214f6 | 822 | sr_spew("Blank line %zu skipped.", inc->line_number); |
4a35548b MS |
823 | continue; |
824 | } | |
825 | ||
826 | /* Remove trailing comment. */ | |
df0db9fd GS |
827 | strip_comment(line, inc->comment); |
828 | if (line[0] == '\0') { | |
41d214f6 | 829 | sr_spew("Comment-only line %zu skipped.", inc->line_number); |
4a35548b MS |
830 | continue; |
831 | } | |
832 | ||
160691b9 JS |
833 | /* Skip the header line, its content was used as the channel names. */ |
834 | if (inc->header) { | |
835 | sr_spew("Header line %zu skipped.", inc->line_number); | |
836 | inc->header = FALSE; | |
837 | continue; | |
838 | } | |
839 | ||
df0db9fd GS |
840 | columns = parse_line(line, inc, max_columns); |
841 | if (!columns) { | |
41d214f6 | 842 | sr_err("Error while parsing line %zu.", inc->line_number); |
2355d229 | 843 | g_strfreev(lines); |
4a35548b MS |
844 | return SR_ERR; |
845 | } | |
4a35548b | 846 | num_columns = g_strv_length(columns); |
4a35548b | 847 | if (!num_columns) { |
ad6a2bee | 848 | sr_err("Column %zu in line %zu is out of bounds.", |
41d214f6 | 849 | inc->first_column, inc->line_number); |
4a35548b | 850 | g_strfreev(columns); |
2355d229 | 851 | g_strfreev(lines); |
4a35548b MS |
852 | return SR_ERR; |
853 | } | |
4a35548b | 854 | /* |
ba7dd8bb | 855 | * Ensure that the number of channels does not exceed the number |
4a35548b MS |
856 | * of columns in multi column mode. |
857 | */ | |
41d214f6 | 858 | if (inc->multi_column_mode && num_columns < inc->num_channels) { |
ba7dd8bb | 859 | sr_err("Not enough columns for desired number of channels in line %zu.", |
41d214f6 | 860 | inc->line_number); |
4a35548b | 861 | g_strfreev(columns); |
2355d229 | 862 | g_strfreev(lines); |
4a35548b MS |
863 | return SR_ERR; |
864 | } | |
865 | ||
41d214f6 BV |
866 | if (inc->multi_column_mode) |
867 | ret = parse_multi_columns(columns, inc); | |
4a35548b | 868 | else |
41d214f6 BV |
869 | ret = parse_single_column(columns[0], inc); |
870 | if (ret != SR_OK) { | |
4a35548b | 871 | g_strfreev(columns); |
2355d229 | 872 | g_strfreev(lines); |
4a35548b MS |
873 | return SR_ERR; |
874 | } | |
875 | ||
4a35548b | 876 | /* Send sample data to the session bus. */ |
cd59e6ec | 877 | ret = queue_samples(in); |
41d214f6 | 878 | if (ret != SR_OK) { |
4a35548b | 879 | sr_err("Sending samples failed."); |
cd59e6ec | 880 | g_strfreev(columns); |
2355d229 | 881 | g_strfreev(lines); |
4a35548b MS |
882 | return SR_ERR; |
883 | } | |
cd59e6ec | 884 | |
41d214f6 BV |
885 | g_strfreev(columns); |
886 | } | |
887 | g_strfreev(lines); | |
241c386a | 888 | g_string_erase(in->buf, 0, p - in->buf->str); |
41d214f6 | 889 | |
7066fd46 | 890 | return ret; |
41d214f6 BV |
891 | } |
892 | ||
7066fd46 | 893 | static int receive(struct sr_input *in, GString *buf) |
41d214f6 BV |
894 | { |
895 | struct context *inc; | |
7066fd46 BV |
896 | int ret; |
897 | ||
898 | g_string_append_len(in->buf, buf->str, buf->len); | |
41d214f6 BV |
899 | |
900 | inc = in->priv; | |
7066fd46 | 901 | if (!inc->termination) { |
df0db9fd GS |
902 | ret = initial_receive(in); |
903 | if (ret == SR_ERR_NA) | |
7066fd46 BV |
904 | /* Not enough data yet. */ |
905 | return SR_OK; | |
906 | else if (ret != SR_OK) | |
907 | return SR_ERR; | |
908 | ||
909 | /* sdi is ready, notify frontend. */ | |
910 | in->sdi_ready = TRUE; | |
41d214f6 | 911 | return SR_OK; |
7066fd46 BV |
912 | } |
913 | ||
7f4c3a62 | 914 | ret = process_buffer(in, FALSE); |
7066fd46 BV |
915 | |
916 | return ret; | |
917 | } | |
918 | ||
919 | static int end(struct sr_input *in) | |
920 | { | |
921 | struct context *inc; | |
7066fd46 | 922 | int ret; |
41d214f6 | 923 | |
7066fd46 | 924 | if (in->sdi_ready) |
7f4c3a62 | 925 | ret = process_buffer(in, TRUE); |
7066fd46 BV |
926 | else |
927 | ret = SR_OK; | |
cd59e6ec GS |
928 | if (ret != SR_OK) |
929 | return ret; | |
930 | ||
931 | ret = flush_samples(in); | |
932 | if (ret != SR_OK) | |
933 | return ret; | |
7066fd46 BV |
934 | |
935 | inc = in->priv; | |
3be42bc2 | 936 | if (inc->started) |
bee2b016 | 937 | std_session_send_df_end(in->sdi); |
4a35548b | 938 | |
7066fd46 BV |
939 | return ret; |
940 | } | |
941 | ||
d5cc282f | 942 | static void cleanup(struct sr_input *in) |
7066fd46 BV |
943 | { |
944 | struct context *inc; | |
945 | ||
affaf540 GS |
946 | keep_header_for_reread(in); |
947 | ||
7066fd46 BV |
948 | inc = in->priv; |
949 | ||
b1f83103 | 950 | g_free(inc->termination); |
539188e5 | 951 | inc->termination = NULL; |
cd59e6ec | 952 | g_free(inc->datafeed_buffer); |
539188e5 | 953 | inc->datafeed_buffer = NULL; |
4a35548b MS |
954 | } |
955 | ||
ad93bfb0 SA |
956 | static int reset(struct sr_input *in) |
957 | { | |
958 | struct context *inc = in->priv; | |
959 | ||
960 | cleanup(in); | |
961 | inc->started = FALSE; | |
962 | g_string_truncate(in->buf, 0); | |
963 | ||
964 | return SR_OK; | |
965 | } | |
966 | ||
c6aa9870 GS |
967 | enum option_index { |
968 | OPT_SINGLE_COL, | |
969 | OPT_NUM_LOGIC, | |
970 | OPT_DELIM, | |
971 | OPT_FORMAT, | |
972 | OPT_COMMENT, | |
973 | OPT_RATE, | |
974 | OPT_FIRST_LOGIC, | |
975 | OPT_HEADER, | |
976 | OPT_START, | |
977 | OPT_MAX, | |
978 | }; | |
979 | ||
41d214f6 | 980 | static struct sr_option options[] = { |
c6aa9870 GS |
981 | [OPT_SINGLE_COL] = { "single-column", "Single column", "Enable single-column mode, using the specified column (>= 1); 0: multi-col. mode", NULL, NULL }, |
982 | [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 }, | |
983 | [OPT_DELIM] = { "delimiter", "Column delimiter", "The column delimiter (>= 1 characters)", NULL, NULL }, | |
984 | [OPT_FORMAT] = { "format", "Data format (single-col. mode)", "The numeric format of the data (single-col. mode): bin, hex, oct", NULL, NULL }, | |
985 | [OPT_COMMENT] = { "comment", "Comment character(s)", "The comment prefix character(s)", NULL, NULL }, | |
986 | [OPT_RATE] = { "samplerate", "Samplerate (Hz)", "The sample rate (used during capture) in Hz", NULL, NULL }, | |
987 | [OPT_FIRST_LOGIC] = { "first-channel", "First channel", "The column number of the first channel (multi-col. mode); bit position for the first channel (single-col. mode)", NULL, NULL }, | |
988 | [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 }, | |
989 | [OPT_START] = { "startline", "Start line", "The line number at which to start processing samples (>= 1)", NULL, NULL }, | |
990 | [OPT_MAX] = ALL_ZERO, | |
41d214f6 BV |
991 | }; |
992 | ||
2c240774 | 993 | static const struct sr_option *get_options(void) |
41d214f6 | 994 | { |
31c41782 UH |
995 | GSList *l; |
996 | ||
41d214f6 | 997 | if (!options[0].def) { |
c6aa9870 GS |
998 | options[OPT_SINGLE_COL].def = g_variant_ref_sink(g_variant_new_int32(0)); |
999 | options[OPT_NUM_LOGIC].def = g_variant_ref_sink(g_variant_new_int32(0)); | |
1000 | options[OPT_DELIM].def = g_variant_ref_sink(g_variant_new_string(",")); | |
1001 | options[OPT_FORMAT].def = g_variant_ref_sink(g_variant_new_string("bin")); | |
31c41782 UH |
1002 | l = NULL; |
1003 | l = g_slist_append(l, g_variant_ref_sink(g_variant_new_string("bin"))); | |
1004 | l = g_slist_append(l, g_variant_ref_sink(g_variant_new_string("hex"))); | |
1005 | l = g_slist_append(l, g_variant_ref_sink(g_variant_new_string("oct"))); | |
c6aa9870 GS |
1006 | options[OPT_FORMAT].values = l; |
1007 | options[OPT_COMMENT].def = g_variant_ref_sink(g_variant_new_string(";")); | |
1008 | options[OPT_RATE].def = g_variant_ref_sink(g_variant_new_uint64(0)); | |
1009 | options[OPT_FIRST_LOGIC].def = g_variant_ref_sink(g_variant_new_int32(0)); | |
1010 | options[OPT_HEADER].def = g_variant_ref_sink(g_variant_new_boolean(FALSE)); | |
1011 | options[OPT_START].def = g_variant_ref_sink(g_variant_new_int32(1)); | |
41d214f6 BV |
1012 | } |
1013 | ||
1014 | return options; | |
1015 | } | |
1016 | ||
d4c93774 | 1017 | SR_PRIV struct sr_input_module input_csv = { |
4a35548b | 1018 | .id = "csv", |
41d214f6 BV |
1019 | .name = "CSV", |
1020 | .desc = "Comma-separated values", | |
c7bc82ff | 1021 | .exts = (const char*[]){"csv", NULL}, |
41d214f6 | 1022 | .options = get_options, |
4a35548b | 1023 | .init = init, |
41d214f6 | 1024 | .receive = receive, |
7066fd46 | 1025 | .end = end, |
41d214f6 | 1026 | .cleanup = cleanup, |
ad93bfb0 | 1027 | .reset = reset, |
4a35548b | 1028 | }; |