]> sigrok.org Git - libsigrok.git/blobdiff - src/input/csv.c
input/csv: robustness nits in column format dispatching
[libsigrok.git] / src / input / csv.c
index 47f5c2ed51511ddd7030a56b6a47e41017bc57c7..fe62b069b60c47718b8f4bd2e54beec3f4d8a24e 100644 (file)
  *     "all remaining columns", only applicable to the last field), a format
  *     specifying character ('x' hexadecimal, 'o' octal, 'b' binary, 'l'
  *     single-bit logic), and an optional bit count (translating to: logic
- *     channels communicated in that column). This "column_formats" option
- *     is most versatile, other forms of specifying the column layout only
- *     exist for backwards compatibility.
+ *     channels communicated in that column). The 'a' format marks analog
+ *     data, an optionally following number is the digits count (resolution).
+ *     This "column_formats" option is most versatile, other forms of
+ *     specifying the column layout only exist for backwards compatibility,
+ *     and are rather limited. They exclusively support logic input data in
+ *     strictly adjacent columns, with further constraints on column layout
+ *     for multi-bit data.
  *
  * single_column: Specifies the column number which contains the logic data
  *     for single-column mode. All logic data is taken from several bits
  * - ... -I csv:start_line=20:header=yes:...
  *   Skip the first 19 text lines. Use line 20 to derive channel names.
  *   Data starts at line 21.
+ * - ... -I csv:column_formats=*a6 ...
+ *   Each column contains an analog value with six significant digits
+ *   after the decimal period.
  */
 
 /*
  * TODO
  *
- * - Extend support for analog input data? (optional)
- *   - Optionally get precision ('digits') from the column's format spec?
- *     From the position which is "bit count" for logic channels?
+ * - Extend support for analog input data.
  *   - Determine why analog samples of 'double' data type get scrambled
  *     in sigrok-cli screen output. Is analog.encoding->unitsize not
  *     handled properly? A sigrok-cli or libsigrok (src/output) issue?
+ *   - Reconsider the channel creation after format processing. Current
+ *     logic may "bleed" channel names into the analog group when logic
+ *     channels' columns follow analog columns (seen with "-,2a,x8").
+ *     Trying to sort it out, a naive change used to map logic channels'
+ *     data to incorrect bitmap positions. The whole channel numbering
+ *     needs reconsideration. Probably it's easiest to first create _all_
+ *     logic channels so that they have adjacent numbers starting at 0
+ *     (addressing logic bits), then all analog channels (again adjacent)
+ *     to simplify the calculation of their index in the sample set as
+ *     well as their sdi channel index from the "analog column index".
  * - Optionally get sample rate from timestamp column. Just best-effort
  *   approach, not necessarily reliable. Users can always specify rates.
  * - Add a test suite for input modules in general, and CSV in specific?
@@ -156,11 +171,28 @@ static const char col_format_char[] = {
        [FORMAT_ANALOG] = 'a',
 };
 
+static gboolean format_is_ignore(enum single_col_format fmt)
+{
+       return fmt == FORMAT_NONE;
+}
+
+static gboolean format_is_logic(enum single_col_format fmt)
+{
+       return fmt >= FORMAT_BIN && fmt <= FORMAT_OCT;
+}
+
+static gboolean format_is_analog(enum single_col_format fmt)
+{
+       return fmt == FORMAT_ANALOG;
+}
+
 struct column_details {
        size_t col_nr;
        enum single_col_format text_format;
        size_t channel_offset;
        size_t channel_count;
+       size_t channel_index;
+       int analog_digits;
 };
 
 struct context {
@@ -170,10 +202,9 @@ struct context {
        uint64_t samplerate;
        gboolean samplerate_sent;
 
-       /* Number of logic channels. List of names for analog datafeed. */
+       /* Number of channels. */
        size_t logic_channels;
        size_t analog_channels;
-       GSList **analog_datafeed_channels;
 
        /* Column delimiter (actually separator), comment leader, EOL sequence. */
        GString *delimiter;
@@ -207,6 +238,8 @@ struct context {
        csv_analog_t *analog_datafeed_buffer;   /**!< Queue for analog datafeed. */
        size_t analog_datafeed_buf_size;
        size_t analog_datafeed_buf_fill;
+       GSList **analog_datafeed_channels;
+       int *analog_datafeed_digits;
 
        /* Current line number. */
        size_t line_number;
@@ -347,9 +380,6 @@ static void set_analog_value(struct context *inc, size_t ch_idx, csv_analog_t va
 
 static int flush_analog_samples(const struct sr_input *in)
 {
-       /* TODO Use proper 'digits' value for this input module. */
-       static const int digits = 3;
-
        struct context *inc;
        struct sr_datafeed_packet packet;
        struct sr_datafeed_analog analog;
@@ -358,6 +388,7 @@ static int flush_analog_samples(const struct sr_input *in)
        struct sr_analog_spec spec;
        csv_analog_t *samples;
        size_t ch_idx;
+       int digits;
        int rc;
 
        inc = in->priv;
@@ -370,6 +401,7 @@ static int flush_analog_samples(const struct sr_input *in)
 
        samples = inc->analog_datafeed_buffer;
        for (ch_idx = 0; ch_idx < inc->analog_channels; ch_idx++) {
+               digits = inc->analog_datafeed_digits[ch_idx];
                sr_analog_init(&analog, &encoding, &meaning, &spec, digits);
                memset(&packet, 0, sizeof(packet));
                packet.type = SR_DF_ANALOG;
@@ -479,13 +511,11 @@ static int split_column_format(const char *spec,
        if (!endp)
                return SR_ERR_ARG;
        if (endp == spec)
-               count = 1;
-       if (!format_code)
+               count = format_is_analog(format_code) ? 3 : 1;
+       if (format_is_ignore(format_code))
                count = 0;
        if (format_char == 'l')
                count = 1;
-       if (format_code == FORMAT_ANALOG)
-               count = 1;
        if (bit_count)
                *bit_count = count;
        spec = endp;
@@ -551,9 +581,9 @@ static int make_column_details_from_format(const struct sr_input *in,
                        c = auto_column_count;
                }
                column_count += c;
-               if (f == FORMAT_ANALOG)
+               if (format_is_analog(f))
                        analog_count += c;
-               else if (f)
+               else if (format_is_logic(f))
                        logic_count += c * b;
        }
        sr_dbg("Column format %s -> %zu columns, %zu logic, %zu analog channels.",
@@ -581,22 +611,27 @@ static int make_column_details_from_format(const struct sr_input *in,
                        detail = &inc->column_details[column_idx++];
                        detail->col_nr = column_idx;
                        detail->text_format = f;
-                       if (detail->text_format == FORMAT_ANALOG) {
+                       if (format_is_analog(detail->text_format)) {
                                detail->channel_offset = analog_idx;
                                detail->channel_count = 1;
+                               detail->analog_digits = b;
                                analog_idx += detail->channel_count;
-                       } else if (detail->text_format) {
+                       } else if (format_is_logic(detail->text_format)) {
                                detail->channel_offset = channel_idx;
                                detail->channel_count = b;
                                channel_idx += detail->channel_count;
-                       }
-                       sr_dbg("detail -> col %zu, fmt %s, ch off/cnt %zu/%zu",
-                               detail->col_nr, col_format_text[detail->text_format],
-                               detail->channel_offset, detail->channel_count);
-                       if (!detail->text_format)
+                       } else if (format_is_ignore(detail->text_format)) {
+                               /* EMPTY */
                                continue;
+                       } else {
+                               /*
+                                * Neither logic nor analog data, nor ignore.
+                                * Format was noted. No channel creation involved.
+                                */
+                               continue;
+                       }
                        /*
-                        * Create channels with appropriate names. Optionally
+                        * Pick most appropriate channel names. Optionally
                         * use text from a header line (when requested by the
                         * user). In the absence of header text, channels are
                         * assigned rather generic names.
@@ -612,6 +647,13 @@ static int make_column_details_from_format(const struct sr_input *in,
                                caption = NULL;
                        if (!caption || !*caption)
                                caption = NULL;
+                       /*
+                        * TODO Need we first create _all_ logic channels,
+                        * before creating analog channels? Just store the
+                        * parameters here (index, type, name) and have the
+                        * creation sequence done outside of the format
+                        * spec parse loop.
+                        */
                        for (create_idx = 0; create_idx < detail->channel_count; create_idx++) {
                                if (caption && detail->channel_count == 1) {
                                        g_string_assign(channel_name, caption);
@@ -622,12 +664,15 @@ static int make_column_details_from_format(const struct sr_input *in,
                                        g_string_printf(channel_name, "%zu",
                                                detail->channel_offset + create_idx);
                                }
-                               if (detail->text_format == FORMAT_ANALOG) {
+                               if (format_is_analog(detail->text_format)) {
                                        channel_sdi_nr = logic_count + detail->channel_offset + create_idx;
                                        channel_type = SR_CHANNEL_ANALOG;
-                               } else {
+                                       detail->channel_index = g_slist_length(in->sdi->channels);
+                               } else if (format_is_logic(detail->text_format)) {
                                        channel_sdi_nr = detail->channel_offset + create_idx;
                                        channel_type = SR_CHANNEL_LOGIC;
+                               } else {
+                                       continue;
                                }
                                sr_channel_new(in->sdi, channel_sdi_nr,
                                        channel_type, TRUE, channel_name->str);
@@ -778,8 +823,7 @@ static int parse_logic(const char *column, struct context *inc,
                        ch_rem--;
                        set_logic_level(inc, ch_idx + 0, bits & (1 << 0));
                        break;
-               case FORMAT_ANALOG:
-               case FORMAT_NONE:
+               default:
                        /* ShouldNotHappen(TM), but silences compiler warning. */
                        return SR_ERR;
                }
@@ -817,7 +861,7 @@ static int parse_analog(const char *column, struct context *inc,
        csv_analog_t value;
        int ret;
 
-       if (details->text_format != FORMAT_ANALOG)
+       if (!format_is_analog(details->text_format))
                return SR_ERR_BUG;
 
        length = strlen(column);
@@ -1028,7 +1072,7 @@ static int initial_parse(const struct sr_input *in, GString *buf)
 {
        struct context *inc;
        size_t num_columns;
-       size_t line_number, line_idx, ch_idx;
+       size_t line_number, line_idx;
        int ret;
        char **lines, *line, **columns;
 
@@ -1109,6 +1153,11 @@ static int initial_parse(const struct sr_input *in, GString *buf)
         * for datafeed submission that is a multiple of the unit size.
         * Allocate the larger buffer, the "sample buffer" will point
         * to a location within that large buffer later.
+        *
+        * TODO Move channel creation here, and just store required
+        * parameters in the format parser above? Could simplify the
+        * arrangement that logic and analog channels get created in
+        * strict sequence in their respective groups.
         */
        if (inc->logic_channels) {
                inc->sample_unit_size = (inc->logic_channels + 7) / 8;
@@ -1125,6 +1174,10 @@ static int initial_parse(const struct sr_input *in, GString *buf)
 
        if (inc->analog_channels) {
                size_t sample_size, sample_count;
+               size_t detail_idx;
+               struct column_details *detail;
+               int *digits_item;
+               void *channel;
                sample_size = sizeof(inc->analog_datafeed_buffer[0]);
                inc->analog_datafeed_buf_size = CHUNK_SIZE;
                inc->analog_datafeed_buf_size /= sample_size;
@@ -1137,11 +1190,16 @@ static int initial_parse(const struct sr_input *in, GString *buf)
                        goto out;
                }
                inc->analog_datafeed_buf_fill = 0;
-               inc->analog_datafeed_channels = g_malloc0_n(inc->analog_channels, sizeof(inc->analog_datafeed_channels[0]));
-               for (ch_idx = 0; ch_idx < inc->analog_channels; ch_idx++) {
-                       void *channel;
-                       channel = g_slist_nth_data(in->sdi->channels, inc->logic_channels + ch_idx);
-                       inc->analog_datafeed_channels[ch_idx] = g_slist_append(NULL, channel);
+               inc->analog_datafeed_channels = g_malloc0(inc->analog_channels * sizeof(inc->analog_datafeed_channels[0]));
+               inc->analog_datafeed_digits = g_malloc0(inc->analog_channels * sizeof(inc->analog_datafeed_digits[0]));
+               digits_item = inc->analog_datafeed_digits;
+               for (detail_idx = 0; detail_idx < inc->column_want_count; detail_idx++) {
+                       detail = &inc->column_details[detail_idx];
+                       if (!format_is_analog(detail->text_format))
+                               continue;
+                       channel = g_slist_nth_data(in->sdi->channels, detail->channel_index);
+                       inc->analog_datafeed_channels[detail->channel_offset] = g_slist_append(NULL, channel);
+                       *digits_item++ = detail->analog_digits;
                }
        }
 
@@ -1428,17 +1486,17 @@ enum option_index {
 static struct sr_option options[] = {
        [OPT_COL_FMTS] = {
                "column_formats", "Column format specs",
-               "Specifies text columns data types: comma separated list of [<cols>]<fmt>[<bits>], with -/x/o/b/l format specifiers.",
+               "Specifies text columns data types: A comma separated list of [<cols>]<fmt>[<bits>] items, with - to ignore columns, x/o/b/l for logic data, a (and resolution) for analog data.",
                NULL, NULL,
        },
        [OPT_SINGLE_COL] = {
                "single_column", "Single column",
-               "Enable single-column mode, exclusively use text from the specified column (number starting at 1).",
+               "Enable single-column mode, exclusively use text from the specified column (number starting at 1). Obsoleted by 'column_formats'.",
                NULL, NULL,
        },
        [OPT_FIRST_COL] = {
                "first_column", "First column",
-               "Number of the first column with logic data in simple multi-column mode (number starting at 1, default 1).",
+               "Number of the first column with logic data in simple multi-column mode (number starting at 1, default 1). Obsoleted by 'column_formats'.",
                NULL, NULL,
        },
        [OPT_NUM_LOGIC] = {
@@ -1448,7 +1506,7 @@ static struct sr_option options[] = {
        },
        [OPT_FORMAT] = {
                "single_format", "Data format for simple single-column mode.",
-               "The number format of single-column mode input data: bin, hex, oct.",
+               "The number format of single-column mode input data: bin, hex, oct. Obsoleted by 'column_formats'.",
                NULL, NULL,
        },
        [OPT_START] = {
@@ -1458,12 +1516,12 @@ static struct sr_option options[] = {
        },
        [OPT_HEADER] = {
                "header", "Get channel names from first line.",
-               "Use the first processed line's column captions (when available) as channel names.",
+               "Use the first processed line's column captions (when available) as channel names. Off by default",
                NULL, NULL,
        },
        [OPT_RATE] = {
                "samplerate", "Samplerate (Hz)",
-               "The input data's sample rate in Hz.",
+               "The input data's sample rate in Hz. No default value.",
                NULL, NULL,
        },
        [OPT_DELIM] = {
@@ -1473,7 +1531,7 @@ static struct sr_option options[] = {
        },
        [OPT_COMMENT] = {
                "comment_leader", "Comment leader character",
-               "The text which starts comments at the end of text lines.",
+               "The text which starts comments at the end of text lines, semicolon by default.",
                NULL, NULL,
        },
        [OPT_MAX] = ALL_ZERO,