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