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