]>
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 MS |
28 | |
29 | /* | |
30 | * The CSV input module has the following options: | |
31 | * | |
32 | * single-column: Specifies the column number which stores the sample data for | |
33 | * single column mode and enables single column mode. Multi | |
34 | * column mode is used if this parameter is omitted. | |
35 | * | |
ba7dd8bb UH |
36 | * numchannels: Specifies the number of channels to use. In multi column mode |
37 | * the number of channels are the number of columns and in single | |
4a35548b | 38 | * column mode the number of bits (LSB first) beginning at |
ba7dd8bb | 39 | * 'first-channel'. |
4a35548b MS |
40 | * |
41 | * delimiter: Specifies the delimiter for columns. Must be at least one | |
42 | * character. Comma is used as default delimiter. | |
43 | * | |
44 | * format: Specifies the format of the sample data in single column mode. | |
45 | * Available formats are: 'bin', 'hex' and 'oct'. The binary | |
46 | * format is used by default. This option has no effect in multi | |
47 | * column mode. | |
48 | * | |
49 | * comment: Specifies the prefix character(s) for comments. No prefix | |
50 | * characters are used by default which disables removing of | |
51 | * comments. | |
52 | * | |
53 | * samplerate: Samplerate which the sample data was captured with. Default | |
54 | * value is 0. | |
55 | * | |
ba7dd8bb UH |
56 | * first-channel: Column number of the first channel in multi column mode and |
57 | * position of the bit for the first channel in single column mode. | |
4a35548b MS |
58 | * Default value is 0. |
59 | * | |
60 | * header: Determines if the first line should be treated as header | |
ba7dd8bb UH |
61 | * and used for channel names in multi column mode. Empty header |
62 | * names will be replaced by the channel number. If enabled in | |
4a35548b MS |
63 | * single column mode the first line will be skipped. Usage of |
64 | * header is disabled by default. | |
65 | * | |
66 | * startline: Line number to start processing sample data. Must be greater | |
67 | * than 0. The default line number to start processing is 1. | |
68 | */ | |
69 | ||
70 | /* Single column formats. */ | |
71 | enum { | |
72 | FORMAT_BIN, | |
73 | FORMAT_HEX, | |
74 | FORMAT_OCT | |
75 | }; | |
76 | ||
77 | struct context { | |
41d214f6 BV |
78 | gboolean started; |
79 | ||
4a35548b MS |
80 | /* Current selected samplerate. */ |
81 | uint64_t samplerate; | |
82 | ||
ba7dd8bb | 83 | /* Number of channels. */ |
6433156c | 84 | unsigned int num_channels; |
4a35548b MS |
85 | |
86 | /* Column delimiter character(s). */ | |
87 | GString *delimiter; | |
88 | ||
89 | /* Comment prefix character(s). */ | |
90 | GString *comment; | |
91 | ||
41d214f6 BV |
92 | /* Termination character(s) used in current stream. */ |
93 | char *termination; | |
94 | ||
4a35548b MS |
95 | /* Determines if sample data is stored in multiple columns. */ |
96 | gboolean multi_column_mode; | |
97 | ||
98 | /* Column number of the sample data in single column mode. */ | |
6433156c | 99 | unsigned int single_column; |
4a35548b MS |
100 | |
101 | /* | |
102 | * Number of the first column to parse. Equivalent to the number of the | |
ba7dd8bb | 103 | * first channel in multi column mode and the single column number in |
4a35548b MS |
104 | * single column mode. |
105 | */ | |
6433156c | 106 | unsigned int first_column; |
4a35548b MS |
107 | |
108 | /* | |
ba7dd8bb UH |
109 | * Column number of the first channel in multi column mode and position of |
110 | * the bit for the first channel in single column mode. | |
4a35548b | 111 | */ |
6433156c | 112 | unsigned int first_channel; |
4a35548b MS |
113 | |
114 | /* Line number to start processing. */ | |
6433156c | 115 | size_t start_line; |
4a35548b MS |
116 | |
117 | /* | |
118 | * Determines if the first line should be treated as header and used for | |
ba7dd8bb | 119 | * channel names in multi column mode. |
4a35548b MS |
120 | */ |
121 | gboolean header; | |
122 | ||
123 | /* Format sample data is stored in single column mode. */ | |
124 | int format; | |
125 | ||
126 | /* Size of the sample buffer. */ | |
6433156c | 127 | size_t sample_buffer_size; |
4a35548b MS |
128 | |
129 | /* Buffer to store sample data. */ | |
130 | uint8_t *sample_buffer; | |
131 | ||
4a35548b | 132 | /* Current line number. */ |
6433156c | 133 | size_t line_number; |
4a35548b MS |
134 | }; |
135 | ||
41d214f6 | 136 | static int format_match(GHashTable *metadata) |
4a35548b | 137 | { |
41d214f6 | 138 | char *buf; |
4a35548b | 139 | |
41d214f6 BV |
140 | buf = g_hash_table_lookup(metadata, GINT_TO_POINTER(SR_INPUT_META_MIMETYPE)); |
141 | if (!strcmp(buf, "text/csv")) | |
4f979115 | 142 | return SR_OK; |
4a35548b | 143 | |
4f979115 | 144 | return SR_ERR; |
4a35548b MS |
145 | } |
146 | ||
41d214f6 | 147 | static void strip_comment(char *buf, const GString *prefix) |
4a35548b MS |
148 | { |
149 | char *ptr; | |
150 | ||
151 | if (!prefix->len) | |
152 | return; | |
153 | ||
41d214f6 BV |
154 | if ((ptr = strstr(buf, prefix->str))) |
155 | *ptr = '\0'; | |
4a35548b MS |
156 | } |
157 | ||
41d214f6 | 158 | static int parse_binstr(const char *str, struct context *inc) |
4a35548b MS |
159 | { |
160 | gsize i, j, length; | |
161 | ||
162 | length = strlen(str); | |
163 | ||
164 | if (!length) { | |
6433156c | 165 | sr_err("Column %u in line %zu is empty.", inc->single_column, |
41d214f6 | 166 | inc->line_number); |
4a35548b MS |
167 | return SR_ERR; |
168 | } | |
169 | ||
170 | /* Clear buffer in order to set bits only. */ | |
41d214f6 | 171 | memset(inc->sample_buffer, 0, (inc->num_channels + 7) >> 3); |
4a35548b | 172 | |
41d214f6 | 173 | i = inc->first_channel; |
4a35548b | 174 | |
41d214f6 | 175 | for (j = 0; i < length && j < inc->num_channels; i++, j++) { |
4a35548b | 176 | if (str[length - i - 1] == '1') { |
41d214f6 | 177 | inc->sample_buffer[j / 8] |= (1 << (j % 8)); |
4a35548b | 178 | } else if (str[length - i - 1] != '0') { |
6433156c | 179 | sr_err("Invalid value '%s' in column %u in line %zu.", |
41d214f6 | 180 | str, inc->single_column, inc->line_number); |
4a35548b MS |
181 | return SR_ERR; |
182 | } | |
183 | } | |
184 | ||
185 | return SR_OK; | |
186 | } | |
187 | ||
41d214f6 | 188 | static int parse_hexstr(const char *str, struct context *inc) |
4a35548b MS |
189 | { |
190 | gsize i, j, k, length; | |
191 | uint8_t value; | |
192 | char c; | |
193 | ||
194 | length = strlen(str); | |
195 | ||
196 | if (!length) { | |
6433156c | 197 | sr_err("Column %u 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. */ | |
41d214f6 | 203 | memset(inc->sample_buffer, 0, (inc->num_channels + 7) >> 3); |
4a35548b MS |
204 | |
205 | /* Calculate the position of the first hexadecimal digit. */ | |
41d214f6 | 206 | i = inc->first_channel / 4; |
4a35548b | 207 | |
41d214f6 | 208 | for (j = 0; i < length && j < inc->num_channels; i++) { |
4a35548b MS |
209 | c = str[length - i - 1]; |
210 | ||
211 | if (!g_ascii_isxdigit(c)) { | |
6433156c | 212 | sr_err("Invalid value '%s' in column %u in line %zu.", |
41d214f6 | 213 | str, inc->single_column, inc->line_number); |
4a35548b MS |
214 | return SR_ERR; |
215 | } | |
216 | ||
217 | value = g_ascii_xdigit_value(c); | |
218 | ||
41d214f6 | 219 | k = (inc->first_channel + j) % 4; |
4a35548b | 220 | |
41d214f6 | 221 | for (; j < inc->num_channels && k < 4; k++) { |
4a35548b | 222 | if (value & (1 << k)) |
41d214f6 | 223 | inc->sample_buffer[j / 8] |= (1 << (j % 8)); |
4a35548b MS |
224 | |
225 | j++; | |
226 | } | |
227 | } | |
228 | ||
229 | return SR_OK; | |
230 | } | |
231 | ||
41d214f6 | 232 | static int parse_octstr(const char *str, struct context *inc) |
4a35548b MS |
233 | { |
234 | gsize i, j, k, length; | |
235 | uint8_t value; | |
236 | char c; | |
237 | ||
238 | length = strlen(str); | |
239 | ||
240 | if (!length) { | |
6433156c | 241 | sr_err("Column %u in line %zu is empty.", inc->single_column, |
41d214f6 | 242 | inc->line_number); |
4a35548b MS |
243 | return SR_ERR; |
244 | } | |
245 | ||
246 | /* Clear buffer in order to set bits only. */ | |
41d214f6 | 247 | memset(inc->sample_buffer, 0, (inc->num_channels + 7) >> 3); |
4a35548b MS |
248 | |
249 | /* Calculate the position of the first octal digit. */ | |
41d214f6 | 250 | i = inc->first_channel / 3; |
4a35548b | 251 | |
41d214f6 | 252 | for (j = 0; i < length && j < inc->num_channels; i++) { |
4a35548b MS |
253 | c = str[length - i - 1]; |
254 | ||
255 | if (c < '0' || c > '7') { | |
6433156c | 256 | sr_err("Invalid value '%s' in column %u in line %zu.", |
41d214f6 | 257 | str, inc->single_column, inc->line_number); |
4a35548b MS |
258 | return SR_ERR; |
259 | } | |
260 | ||
261 | value = g_ascii_xdigit_value(c); | |
262 | ||
41d214f6 | 263 | k = (inc->first_channel + j) % 3; |
4a35548b | 264 | |
41d214f6 | 265 | for (; j < inc->num_channels && k < 3; k++) { |
4a35548b | 266 | if (value & (1 << k)) |
41d214f6 | 267 | inc->sample_buffer[j / 8] |= (1 << (j % 8)); |
4a35548b MS |
268 | |
269 | j++; | |
270 | } | |
271 | } | |
272 | ||
273 | return SR_OK; | |
274 | } | |
275 | ||
41d214f6 | 276 | static char **parse_line(char *buf, struct context *inc, int max_columns) |
4a35548b MS |
277 | { |
278 | const char *str, *remainder; | |
279 | GSList *list, *l; | |
280 | char **columns; | |
281 | char *column; | |
282 | gsize n, k; | |
283 | ||
284 | n = 0; | |
285 | k = 0; | |
286 | list = NULL; | |
287 | ||
41d214f6 BV |
288 | remainder = buf; |
289 | str = strstr(remainder, inc->delimiter->str); | |
4a35548b MS |
290 | |
291 | while (str && max_columns) { | |
41d214f6 | 292 | if (n >= inc->first_column) { |
4a35548b MS |
293 | column = g_strndup(remainder, str - remainder); |
294 | list = g_slist_prepend(list, g_strstrip(column)); | |
295 | ||
296 | max_columns--; | |
297 | k++; | |
298 | } | |
299 | ||
41d214f6 BV |
300 | remainder = str + inc->delimiter->len; |
301 | str = strstr(remainder, inc->delimiter->str); | |
4a35548b MS |
302 | n++; |
303 | } | |
304 | ||
41d214f6 | 305 | if (buf[0] && max_columns && n >= inc->first_column) { |
4a35548b MS |
306 | column = g_strdup(remainder); |
307 | list = g_slist_prepend(list, g_strstrip(column)); | |
308 | k++; | |
309 | } | |
310 | ||
311 | if (!(columns = g_try_new(char *, k + 1))) | |
312 | return NULL; | |
313 | ||
314 | columns[k--] = NULL; | |
315 | ||
316 | for (l = list; l; l = l->next) | |
317 | columns[k--] = l->data; | |
318 | ||
319 | g_slist_free(list); | |
320 | ||
321 | return columns; | |
322 | } | |
323 | ||
41d214f6 | 324 | static int parse_multi_columns(char **columns, struct context *inc) |
4a35548b MS |
325 | { |
326 | gsize i; | |
327 | ||
328 | /* Clear buffer in order to set bits only. */ | |
41d214f6 | 329 | memset(inc->sample_buffer, 0, (inc->num_channels + 7) >> 3); |
4a35548b | 330 | |
41d214f6 | 331 | for (i = 0; i < inc->num_channels; i++) { |
4a35548b | 332 | if (columns[i][0] == '1') { |
41d214f6 | 333 | inc->sample_buffer[i / 8] |= (1 << (i % 8)); |
4a35548b MS |
334 | } else if (!strlen(columns[i])) { |
335 | sr_err("Column %zu in line %zu is empty.", | |
41d214f6 | 336 | inc->first_channel + i, inc->line_number); |
4a35548b MS |
337 | return SR_ERR; |
338 | } else if (columns[i][0] != '0') { | |
339 | sr_err("Invalid value '%s' in column %zu in line %zu.", | |
41d214f6 BV |
340 | columns[i], inc->first_channel + i, |
341 | inc->line_number); | |
4a35548b MS |
342 | return SR_ERR; |
343 | } | |
344 | } | |
345 | ||
346 | return SR_OK; | |
347 | } | |
348 | ||
41d214f6 | 349 | static int parse_single_column(const char *column, struct context *inc) |
4a35548b MS |
350 | { |
351 | int res; | |
352 | ||
353 | res = SR_ERR; | |
354 | ||
0c5f2abc | 355 | switch (inc->format) { |
4a35548b | 356 | case FORMAT_BIN: |
41d214f6 | 357 | res = parse_binstr(column, inc); |
4a35548b MS |
358 | break; |
359 | case FORMAT_HEX: | |
41d214f6 | 360 | res = parse_hexstr(column, inc); |
4a35548b MS |
361 | break; |
362 | case FORMAT_OCT: | |
41d214f6 | 363 | res = parse_octstr(column, inc); |
4a35548b MS |
364 | break; |
365 | } | |
366 | ||
367 | return res; | |
368 | } | |
369 | ||
370 | static int send_samples(const struct sr_dev_inst *sdi, uint8_t *buffer, | |
41d214f6 | 371 | gsize buffer_size, gsize count) |
4a35548b | 372 | { |
4a35548b MS |
373 | struct sr_datafeed_packet packet; |
374 | struct sr_datafeed_logic logic; | |
41d214f6 | 375 | int res; |
4a35548b MS |
376 | gsize i; |
377 | ||
378 | packet.type = SR_DF_LOGIC; | |
379 | packet.payload = &logic; | |
380 | logic.unitsize = buffer_size; | |
381 | logic.length = buffer_size; | |
382 | logic.data = buffer; | |
383 | ||
384 | for (i = 0; i < count; i++) { | |
385 | if ((res = sr_session_send(sdi, &packet)) != SR_OK) | |
386 | return res; | |
387 | } | |
388 | ||
389 | return SR_OK; | |
390 | } | |
391 | ||
41d214f6 | 392 | static int init(struct sr_input *in, GHashTable *options) |
4a35548b | 393 | { |
41d214f6 BV |
394 | struct context *inc; |
395 | const char *s; | |
4a35548b | 396 | |
aac29cc1 | 397 | in->sdi = g_malloc0(sizeof(struct sr_dev_inst)); |
41d214f6 | 398 | in->priv = inc = g_malloc0(sizeof(struct context)); |
4a35548b | 399 | |
41d214f6 BV |
400 | inc->single_column = g_variant_get_int32(g_hash_table_lookup(options, "single-column")); |
401 | inc->multi_column_mode = inc->single_column == 0; | |
4a35548b | 402 | |
41d214f6 | 403 | inc->num_channels = g_variant_get_int32(g_hash_table_lookup(options, "numchannels")); |
4a35548b | 404 | |
41d214f6 BV |
405 | inc->delimiter = g_string_new(g_variant_get_string( |
406 | g_hash_table_lookup(options, "delimiter"), NULL)); | |
407 | if (inc->delimiter->len == 0) { | |
408 | sr_err("Delimiter must be at least one character."); | |
409 | return SR_ERR_ARG; | |
4a35548b MS |
410 | } |
411 | ||
41d214f6 BV |
412 | s = g_variant_get_string(g_hash_table_lookup(options, "format"), NULL); |
413 | if (!g_ascii_strncasecmp(s, "bin", 3)) { | |
414 | inc->format = FORMAT_BIN; | |
415 | } else if (!g_ascii_strncasecmp(s, "hex", 3)) { | |
416 | inc->format = FORMAT_HEX; | |
417 | } else if (!g_ascii_strncasecmp(s, "oct", 3)) { | |
418 | inc->format = FORMAT_OCT; | |
419 | } else { | |
420 | sr_err("Invalid format: '%s'", s); | |
421 | return SR_ERR_ARG; | |
4a35548b MS |
422 | } |
423 | ||
41d214f6 BV |
424 | inc->comment = g_string_new(g_variant_get_string( |
425 | g_hash_table_lookup(options, "comment"), NULL)); | |
426 | if (g_string_equal(inc->comment, inc->delimiter)) { | |
427 | /* That's never going to work. Likely the result of the user | |
428 | * setting the delimiter to ; -- the default comment. Clearing | |
429 | * the comment setting will work in that case. */ | |
430 | g_string_truncate(inc->comment, 0); | |
4a35548b MS |
431 | } |
432 | ||
6e8d95a5 | 433 | inc->samplerate = g_variant_get_uint64(g_hash_table_lookup(options, "samplerate")); |
4a35548b | 434 | |
41d214f6 | 435 | inc->first_channel = g_variant_get_int32(g_hash_table_lookup(options, "first-channel")); |
4a35548b | 436 | |
41d214f6 | 437 | inc->header = g_variant_get_boolean(g_hash_table_lookup(options, "header")); |
4a35548b | 438 | |
41d214f6 BV |
439 | inc->start_line = g_variant_get_int32(g_hash_table_lookup(options, "startline")); |
440 | if (inc->start_line < 1) { | |
6433156c | 441 | sr_err("Invalid start line %zu.", inc->start_line); |
41d214f6 | 442 | return SR_ERR_ARG; |
4a35548b MS |
443 | } |
444 | ||
41d214f6 BV |
445 | if (inc->multi_column_mode) |
446 | inc->first_column = inc->first_channel; | |
4a35548b | 447 | else |
41d214f6 | 448 | inc->first_column = inc->single_column; |
4a35548b | 449 | |
41d214f6 | 450 | if (!inc->multi_column_mode && !inc->num_channels) { |
ba7dd8bb | 451 | sr_err("Number of channels needs to be specified in single column mode."); |
41d214f6 | 452 | return SR_ERR_ARG; |
4a35548b MS |
453 | } |
454 | ||
41d214f6 BV |
455 | return SR_OK; |
456 | } | |
4a35548b | 457 | |
329733d9 | 458 | static const char *get_line_termination(GString *buf) |
41d214f6 | 459 | { |
329733d9 | 460 | const char *term; |
4a35548b | 461 | |
41d214f6 BV |
462 | term = NULL; |
463 | if (g_strstr_len(buf->str, buf->len, "\r\n")) | |
464 | term = "\r\n"; | |
465 | else if (memchr(buf->str, '\n', buf->len)) | |
466 | term = "\n"; | |
467 | else if (memchr(buf->str, '\r', buf->len)) | |
468 | term = "\r"; | |
4a35548b | 469 | |
41d214f6 BV |
470 | return term; |
471 | } | |
4a35548b | 472 | |
41d214f6 BV |
473 | static int initial_parse(const struct sr_input *in, GString *buf) |
474 | { | |
475 | struct context *inc; | |
41d214f6 | 476 | GString *channel_name; |
6433156c DE |
477 | unsigned int num_columns, i; |
478 | size_t line_number, l; | |
41d214f6 BV |
479 | int ret; |
480 | char **lines, **columns; | |
481 | ||
482 | ret = SR_OK; | |
483 | inc = in->priv; | |
484 | columns = NULL; | |
485 | ||
486 | line_number = 0; | |
487 | lines = g_strsplit_set(buf->str, "\r\n", 0); | |
488 | for (l = 0; lines[l]; l++) { | |
489 | line_number++; | |
490 | if (inc->start_line > line_number) { | |
491 | sr_spew("Line %zu skipped.", line_number); | |
4a35548b MS |
492 | continue; |
493 | } | |
41d214f6 BV |
494 | if (lines[l][0] == '\0') { |
495 | sr_spew("Blank line %zu skipped.", line_number); | |
496 | continue; | |
497 | } | |
498 | strip_comment(lines[l], inc->comment); | |
499 | if (lines[l][0] == '\0') { | |
500 | sr_spew("Comment-only line %zu skipped.", line_number); | |
4a35548b MS |
501 | continue; |
502 | } | |
503 | ||
41d214f6 BV |
504 | /* Reached first proper line. */ |
505 | break; | |
506 | } | |
507 | if (!lines[l]) { | |
508 | /* Not enough data for a proper line yet. */ | |
60107497 | 509 | ret = SR_ERR_NA; |
41d214f6 | 510 | goto out; |
4a35548b MS |
511 | } |
512 | ||
513 | /* | |
514 | * In order to determine the number of columns parse the current line | |
515 | * without limiting the number of columns. | |
516 | */ | |
41d214f6 BV |
517 | if (!(columns = parse_line(lines[l], inc, -1))) { |
518 | sr_err("Error while parsing line %zu.", line_number); | |
519 | ret = SR_ERR; | |
520 | goto out; | |
4a35548b | 521 | } |
4a35548b MS |
522 | num_columns = g_strv_length(columns); |
523 | ||
524 | /* Ensure that the first column is not out of bounds. */ | |
525 | if (!num_columns) { | |
6433156c | 526 | sr_err("Column %u in line %zu is out of bounds.", |
41d214f6 BV |
527 | inc->first_column, line_number); |
528 | ret = SR_ERR; | |
529 | goto out; | |
4a35548b MS |
530 | } |
531 | ||
41d214f6 | 532 | if (inc->multi_column_mode) { |
4a35548b | 533 | /* |
ba7dd8bb | 534 | * Detect the number of channels in multi column mode |
4a35548b MS |
535 | * automatically if not specified. |
536 | */ | |
41d214f6 BV |
537 | if (!inc->num_channels) { |
538 | inc->num_channels = num_columns; | |
6433156c | 539 | sr_dbg("Number of auto-detected channels: %u.", |
41d214f6 | 540 | inc->num_channels); |
4a35548b MS |
541 | } |
542 | ||
543 | /* | |
ba7dd8bb | 544 | * Ensure that the number of channels does not exceed the number |
4a35548b MS |
545 | * of columns in multi column mode. |
546 | */ | |
41d214f6 | 547 | if (num_columns < inc->num_channels) { |
ba7dd8bb | 548 | sr_err("Not enough columns for desired number of channels in line %zu.", |
41d214f6 BV |
549 | line_number); |
550 | ret = SR_ERR; | |
551 | goto out; | |
4a35548b MS |
552 | } |
553 | } | |
554 | ||
41d214f6 BV |
555 | channel_name = g_string_sized_new(64); |
556 | for (i = 0; i < inc->num_channels; i++) { | |
6433156c | 557 | if (inc->header && inc->multi_column_mode && columns[i][0] != '\0') |
41d214f6 | 558 | g_string_assign(channel_name, columns[i]); |
4a35548b | 559 | else |
6433156c | 560 | g_string_printf(channel_name, "%u", i); |
5e23fcab | 561 | sr_channel_new(in->sdi, i, SR_CHANNEL_LOGIC, TRUE, channel_name->str); |
4a35548b | 562 | } |
41d214f6 | 563 | g_string_free(channel_name, TRUE); |
4a35548b MS |
564 | |
565 | /* | |
566 | * Calculate the minimum buffer size to store the sample data of the | |
ba7dd8bb | 567 | * channels. |
4a35548b | 568 | */ |
41d214f6 BV |
569 | inc->sample_buffer_size = (inc->num_channels + 7) >> 3; |
570 | inc->sample_buffer = g_malloc(inc->sample_buffer_size); | |
4a35548b | 571 | |
41d214f6 BV |
572 | out: |
573 | if (columns) | |
574 | g_strfreev(columns); | |
575 | g_strfreev(lines); | |
4a35548b | 576 | |
41d214f6 | 577 | return ret; |
4a35548b MS |
578 | } |
579 | ||
41d214f6 | 580 | static int initial_receive(const struct sr_input *in) |
4a35548b | 581 | { |
41d214f6 BV |
582 | struct context *inc; |
583 | GString *new_buf; | |
584 | int len, ret; | |
329733d9 UH |
585 | char *p; |
586 | const char *termination; | |
4a35548b | 587 | |
41d214f6 | 588 | inc = in->priv; |
4a35548b | 589 | |
41d214f6 BV |
590 | if (!(termination = get_line_termination(in->buf))) |
591 | /* Don't have a full line yet. */ | |
d0181813 | 592 | return SR_ERR_NA; |
4a35548b | 593 | |
41d214f6 BV |
594 | if (!(p = g_strrstr_len(in->buf->str, in->buf->len, termination))) |
595 | /* Don't have a full line yet. */ | |
d0181813 | 596 | return SR_ERR_NA; |
41d214f6 BV |
597 | len = p - in->buf->str - 1; |
598 | new_buf = g_string_new_len(in->buf->str, len); | |
599 | g_string_append_c(new_buf, '\0'); | |
4a35548b | 600 | |
41d214f6 BV |
601 | inc->termination = g_strdup(termination); |
602 | ||
603 | if (in->buf->str[0] != '\0') | |
604 | ret = initial_parse(in, new_buf); | |
605 | else | |
606 | ret = SR_OK; | |
607 | ||
608 | g_string_free(new_buf, TRUE); | |
609 | ||
610 | return ret; | |
611 | } | |
612 | ||
7066fd46 | 613 | static int process_buffer(struct sr_input *in) |
41d214f6 BV |
614 | { |
615 | struct sr_datafeed_packet packet; | |
616 | struct sr_datafeed_meta meta; | |
617 | struct sr_config *src; | |
618 | struct context *inc; | |
619 | gsize num_columns; | |
620 | uint64_t samplerate; | |
621 | int max_columns, ret, l; | |
622 | char *p, **lines, **columns; | |
623 | ||
41d214f6 | 624 | inc = in->priv; |
d0181813 | 625 | if (!inc->started) { |
41d214f6 BV |
626 | std_session_send_df_header(in->sdi, LOG_PREFIX); |
627 | ||
628 | if (inc->samplerate) { | |
629 | packet.type = SR_DF_META; | |
630 | packet.payload = &meta; | |
631 | samplerate = inc->samplerate; | |
632 | src = sr_config_new(SR_CONF_SAMPLERATE, g_variant_new_uint64(samplerate)); | |
633 | meta.config = g_slist_append(NULL, src); | |
634 | sr_session_send(in->sdi, &packet); | |
635 | sr_config_free(src); | |
636 | } | |
d0181813 BV |
637 | |
638 | inc->started = TRUE; | |
4a35548b MS |
639 | } |
640 | ||
41d214f6 BV |
641 | if (!(p = g_strrstr_len(in->buf->str, in->buf->len, inc->termination))) |
642 | /* Don't have a full line. */ | |
643 | return SR_ERR; | |
644 | ||
645 | *p = '\0'; | |
646 | g_strstrip(in->buf->str); | |
4a35548b MS |
647 | |
648 | /* Limit the number of columns to parse. */ | |
41d214f6 BV |
649 | if (inc->multi_column_mode) |
650 | max_columns = inc->num_channels; | |
4a35548b MS |
651 | else |
652 | max_columns = 1; | |
653 | ||
18078d05 | 654 | ret = SR_OK; |
41d214f6 BV |
655 | lines = g_strsplit_set(in->buf->str, "\r\n", 0); |
656 | for (l = 0; lines[l]; l++) { | |
657 | inc->line_number++; | |
658 | if (lines[l][0] == '\0') { | |
659 | sr_spew("Blank line %zu skipped.", inc->line_number); | |
4a35548b MS |
660 | continue; |
661 | } | |
662 | ||
663 | /* Remove trailing comment. */ | |
41d214f6 BV |
664 | strip_comment(lines[l], inc->comment); |
665 | if (lines[l][0] == '\0') { | |
666 | sr_spew("Comment-only line %zu skipped.", inc->line_number); | |
4a35548b MS |
667 | continue; |
668 | } | |
669 | ||
160691b9 JS |
670 | /* Skip the header line, its content was used as the channel names. */ |
671 | if (inc->header) { | |
672 | sr_spew("Header line %zu skipped.", inc->line_number); | |
673 | inc->header = FALSE; | |
674 | continue; | |
675 | } | |
676 | ||
41d214f6 BV |
677 | if (!(columns = parse_line(lines[l], inc, max_columns))) { |
678 | sr_err("Error while parsing line %zu.", inc->line_number); | |
4a35548b MS |
679 | return SR_ERR; |
680 | } | |
4a35548b | 681 | num_columns = g_strv_length(columns); |
4a35548b | 682 | if (!num_columns) { |
6433156c | 683 | sr_err("Column %u in line %zu is out of bounds.", |
41d214f6 | 684 | inc->first_column, inc->line_number); |
4a35548b | 685 | g_strfreev(columns); |
4a35548b MS |
686 | return SR_ERR; |
687 | } | |
4a35548b | 688 | /* |
ba7dd8bb | 689 | * Ensure that the number of channels does not exceed the number |
4a35548b MS |
690 | * of columns in multi column mode. |
691 | */ | |
41d214f6 | 692 | if (inc->multi_column_mode && num_columns < inc->num_channels) { |
ba7dd8bb | 693 | sr_err("Not enough columns for desired number of channels in line %zu.", |
41d214f6 | 694 | inc->line_number); |
4a35548b | 695 | g_strfreev(columns); |
4a35548b MS |
696 | return SR_ERR; |
697 | } | |
698 | ||
41d214f6 BV |
699 | if (inc->multi_column_mode) |
700 | ret = parse_multi_columns(columns, inc); | |
4a35548b | 701 | else |
41d214f6 BV |
702 | ret = parse_single_column(columns[0], inc); |
703 | if (ret != SR_OK) { | |
4a35548b | 704 | g_strfreev(columns); |
4a35548b MS |
705 | return SR_ERR; |
706 | } | |
707 | ||
4a35548b | 708 | /* Send sample data to the session bus. */ |
41d214f6 BV |
709 | ret = send_samples(in->sdi, inc->sample_buffer, |
710 | inc->sample_buffer_size, 1); | |
711 | if (ret != SR_OK) { | |
4a35548b | 712 | sr_err("Sending samples failed."); |
4a35548b MS |
713 | return SR_ERR; |
714 | } | |
41d214f6 BV |
715 | g_strfreev(columns); |
716 | } | |
717 | g_strfreev(lines); | |
718 | g_string_erase(in->buf, 0, p - in->buf->str + 1); | |
719 | ||
7066fd46 | 720 | return ret; |
41d214f6 BV |
721 | } |
722 | ||
7066fd46 | 723 | static int receive(struct sr_input *in, GString *buf) |
41d214f6 BV |
724 | { |
725 | struct context *inc; | |
7066fd46 BV |
726 | int ret; |
727 | ||
728 | g_string_append_len(in->buf, buf->str, buf->len); | |
41d214f6 BV |
729 | |
730 | inc = in->priv; | |
7066fd46 BV |
731 | if (!inc->termination) { |
732 | if ((ret = initial_receive(in)) == SR_ERR_NA) | |
733 | /* Not enough data yet. */ | |
734 | return SR_OK; | |
735 | else if (ret != SR_OK) | |
736 | return SR_ERR; | |
737 | ||
738 | /* sdi is ready, notify frontend. */ | |
739 | in->sdi_ready = TRUE; | |
41d214f6 | 740 | return SR_OK; |
7066fd46 BV |
741 | } |
742 | ||
743 | ret = process_buffer(in); | |
744 | ||
745 | return ret; | |
746 | } | |
747 | ||
748 | static int end(struct sr_input *in) | |
749 | { | |
750 | struct context *inc; | |
751 | struct sr_datafeed_packet packet; | |
752 | int ret; | |
41d214f6 | 753 | |
7066fd46 BV |
754 | if (in->sdi_ready) |
755 | ret = process_buffer(in); | |
756 | else | |
757 | ret = SR_OK; | |
758 | ||
759 | inc = in->priv; | |
41d214f6 BV |
760 | if (inc->started) { |
761 | /* End of stream. */ | |
762 | packet.type = SR_DF_END; | |
763 | sr_session_send(in->sdi, &packet); | |
4a35548b MS |
764 | } |
765 | ||
7066fd46 BV |
766 | return ret; |
767 | } | |
768 | ||
d5cc282f | 769 | static void cleanup(struct sr_input *in) |
7066fd46 BV |
770 | { |
771 | struct context *inc; | |
772 | ||
773 | inc = in->priv; | |
774 | ||
41d214f6 BV |
775 | if (inc->delimiter) |
776 | g_string_free(inc->delimiter, TRUE); | |
777 | ||
778 | if (inc->comment) | |
779 | g_string_free(inc->comment, TRUE); | |
4a35548b | 780 | |
b1f83103 UH |
781 | g_free(inc->termination); |
782 | g_free(inc->sample_buffer); | |
4a35548b MS |
783 | } |
784 | ||
41d214f6 BV |
785 | static struct sr_option options[] = { |
786 | { "single-column", "Single column", "Enable/specify single column", NULL, NULL }, | |
787 | { "numchannels", "Max channels", "Number of channels", NULL, NULL }, | |
788 | { "delimiter", "Delimiter", "Column delimiter", NULL, NULL }, | |
789 | { "format", "Format", "Numeric format", NULL, NULL }, | |
790 | { "comment", "Comment", "Comment prefix character", NULL, NULL }, | |
791 | { "samplerate", "Samplerate", "Samplerate used during capture", NULL, NULL }, | |
792 | { "first-channel", "First channel", "Column number of first channel", NULL, NULL }, | |
793 | { "header", "Header", "Treat first line as header with channel names", NULL, NULL }, | |
794 | { "startline", "Start line", "Line number at which to start processing samples", NULL, NULL }, | |
06ad20be | 795 | ALL_ZERO |
41d214f6 BV |
796 | }; |
797 | ||
798 | static struct sr_option *get_options(void) | |
799 | { | |
800 | if (!options[0].def) { | |
801 | options[0].def = g_variant_ref_sink(g_variant_new_int32(0)); | |
802 | options[1].def = g_variant_ref_sink(g_variant_new_int32(0)); | |
803 | options[2].def = g_variant_ref_sink(g_variant_new_string(",")); | |
804 | options[3].def = g_variant_ref_sink(g_variant_new_string("bin")); | |
805 | options[4].def = g_variant_ref_sink(g_variant_new_string(";")); | |
6e8d95a5 | 806 | options[5].def = g_variant_ref_sink(g_variant_new_uint64(0)); |
41d214f6 BV |
807 | options[6].def = g_variant_ref_sink(g_variant_new_int32(0)); |
808 | options[7].def = g_variant_ref_sink(g_variant_new_boolean(FALSE)); | |
809 | options[8].def = g_variant_ref_sink(g_variant_new_int32(1)); | |
810 | } | |
811 | ||
812 | return options; | |
813 | } | |
814 | ||
d4c93774 | 815 | SR_PRIV struct sr_input_module input_csv = { |
4a35548b | 816 | .id = "csv", |
41d214f6 BV |
817 | .name = "CSV", |
818 | .desc = "Comma-separated values", | |
c7bc82ff | 819 | .exts = (const char*[]){"csv", NULL}, |
41d214f6 BV |
820 | .metadata = { SR_INPUT_META_MIMETYPE }, |
821 | .options = get_options, | |
4a35548b MS |
822 | .format_match = format_match, |
823 | .init = init, | |
41d214f6 | 824 | .receive = receive, |
7066fd46 | 825 | .end = end, |
41d214f6 | 826 | .cleanup = cleanup, |
4a35548b | 827 | }; |