]> sigrok.org Git - libsigrok.git/blobdiff - src/input/csv.c
input/csv: add support for timestamp columns, auto detect samplerate
[libsigrok.git] / src / input / csv.c
index 42387f72b9ca3de7eb1a46c480dd9ef4cac05640..10ddd6865a98f31dbdb2b48079420f7be41a9747 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).
+ *     The 't' format marks timestamp values, which could help in automatic
+ *     determination of the input stream's samplerate. 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.
+ * - ... -I csv:column_formats=t,2a ...
+ *   The first column contains timestamps, the next two columns contain
+ *   analog values. The capture's samplerate could get determined from
+ *   the timestamp values if not provided by the user by means of the
+ *   'samplerate' option. This assumes a mere number in units of seconds,
+ *   and equidistant rows, there is no fancy support for textual unit
+ *   suffixes nor gaps in the stream of samples nor other non-linearity,
+ *   just '-' ignore the column if the format is not supported).
  */
 
 /*
  * TODO
  *
- * - Add support for analog input data? (optional)
- *   - Extend the set of supported column types. Just grab a double
- *     value from floating point format input text.
- *   - 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?
  *   samplerates, etc).
  */
 
+typedef float csv_analog_t;    /* 'double' currently is flawed. */
+
 /* Single column formats. */
 enum single_col_format {
        FORMAT_NONE,    /* Ignore this column. */
        FORMAT_BIN,     /* Bin digits for a set of bits (or just one bit). */
        FORMAT_HEX,     /* Hex digits for a set of bits. */
        FORMAT_OCT,     /* Oct digits for a set of bits. */
+       FORMAT_ANALOG,  /* Floating point number for an analog channel. */
+       FORMAT_TIME,    /* Timestamps. */
 };
 
 static const char *col_format_text[] = {
@@ -141,6 +170,8 @@ static const char *col_format_text[] = {
        [FORMAT_BIN] = "binary",
        [FORMAT_HEX] = "hexadecimal",
        [FORMAT_OCT] = "octal",
+       [FORMAT_ANALOG] = "analog",
+       [FORMAT_TIME] = "timestamp",
 };
 
 static const char col_format_char[] = {
@@ -148,24 +179,50 @@ static const char col_format_char[] = {
        [FORMAT_BIN] = 'b',
        [FORMAT_HEX] = 'x',
        [FORMAT_OCT] = 'o',
+       [FORMAT_ANALOG] = 'a',
+       [FORMAT_TIME] = 't',
 };
 
+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;
+}
+
+static gboolean format_is_timestamp(enum single_col_format fmt)
+{
+       return fmt == FORMAT_TIME;
+}
+
 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 {
        gboolean started;
 
-       /* Current selected samplerate. */
+       /* Current samplerate, optionally determined from input data. */
        uint64_t samplerate;
+       double prev_timestamp;
        gboolean samplerate_sent;
 
-       /* Number of logic channels. */
+       /* Number of channels. */
        size_t logic_channels;
+       size_t analog_channels;
 
        /* Column delimiter (actually separator), comment leader, EOL sequence. */
        GString *delimiter;
@@ -190,10 +247,17 @@ struct context {
 
        size_t sample_unit_size;        /**!< Byte count for a single sample. */
        uint8_t *sample_buffer;         /**!< Buffer for a single sample. */
+       csv_analog_t *analog_sample_buffer;     /**!< Buffer for one set of analog values. */
 
        uint8_t *datafeed_buffer;       /**!< Queue for datafeed submission. */
        size_t datafeed_buf_size;
        size_t datafeed_buf_fill;
+       /* "Striped" layout, M samples for N channels each. */
+       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;
@@ -215,8 +279,32 @@ struct context {
  *   (when it is full, or upon EOF).
  */
 
+static int flush_samplerate(const struct sr_input *in)
+{
+       struct context *inc;
+       struct sr_datafeed_packet packet;
+       struct sr_datafeed_meta meta;
+       struct sr_config *src;
+
+       inc = in->priv;
+       if (inc->samplerate && !inc->samplerate_sent) {
+               packet.type = SR_DF_META;
+               packet.payload = &meta;
+               src = sr_config_new(SR_CONF_SAMPLERATE, g_variant_new_uint64(inc->samplerate));
+               meta.config = g_slist_append(NULL, src);
+               sr_session_send(in->sdi, &packet);
+               g_slist_free(meta.config);
+               sr_config_free(src);
+               inc->samplerate_sent = TRUE;
+       }
+
+       return SR_OK;
+}
+
 static void clear_logic_samples(struct context *inc)
 {
+       if (!inc->logic_channels)
+               return;
        inc->sample_buffer = &inc->datafeed_buffer[inc->datafeed_buf_fill];
        memset(inc->sample_buffer, 0, inc->sample_unit_size);
 }
@@ -241,9 +329,6 @@ static int flush_logic_samples(const struct sr_input *in)
 {
        struct context *inc;
        struct sr_datafeed_packet packet;
-       struct sr_datafeed_meta meta;
-       struct sr_config *src;
-       uint64_t samplerate;
        struct sr_datafeed_logic logic;
        int rc;
 
@@ -251,17 +336,9 @@ static int flush_logic_samples(const struct sr_input *in)
        if (!inc->datafeed_buf_fill)
                return SR_OK;
 
-       if (inc->samplerate && !inc->samplerate_sent) {
-               packet.type = SR_DF_META;
-               packet.payload = &meta;
-               samplerate = inc->samplerate;
-               src = sr_config_new(SR_CONF_SAMPLERATE, g_variant_new_uint64(samplerate));
-               meta.config = g_slist_append(NULL, src);
-               sr_session_send(in->sdi, &packet);
-               g_slist_free(meta.config);
-               sr_config_free(src);
-               inc->samplerate_sent = TRUE;
-       }
+       rc = flush_samplerate(in);
+       if (rc != SR_OK)
+               return rc;
 
        memset(&packet, 0, sizeof(packet));
        memset(&logic, 0, sizeof(logic));
@@ -297,6 +374,99 @@ static int queue_logic_samples(const struct sr_input *in)
        return SR_OK;
 }
 
+static void set_analog_value(struct context *inc, size_t ch_idx, csv_analog_t value);
+
+static void clear_analog_samples(struct context *inc)
+{
+       size_t idx;
+
+       if (!inc->analog_channels)
+               return;
+       inc->analog_sample_buffer = &inc->analog_datafeed_buffer[inc->analog_datafeed_buf_fill];
+       for (idx = 0; idx < inc->analog_channels; idx++)
+               set_analog_value(inc, idx, 0.0);
+}
+
+static void set_analog_value(struct context *inc, size_t ch_idx, csv_analog_t value)
+{
+       if (ch_idx >= inc->analog_channels)
+               return;
+       if (!value)
+               return;
+       inc->analog_sample_buffer[ch_idx * inc->analog_datafeed_buf_size] = value;
+}
+
+static int flush_analog_samples(const struct sr_input *in)
+{
+       struct context *inc;
+       struct sr_datafeed_packet packet;
+       struct sr_datafeed_analog analog;
+       struct sr_analog_encoding encoding;
+       struct sr_analog_meaning meaning;
+       struct sr_analog_spec spec;
+       csv_analog_t *samples;
+       size_t ch_idx;
+       int digits;
+       int rc;
+
+       inc = in->priv;
+       if (!inc->analog_datafeed_buf_fill)
+               return SR_OK;
+
+       rc = flush_samplerate(in);
+       if (rc != SR_OK)
+               return rc;
+
+       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;
+               packet.payload = &analog;
+               analog.num_samples = inc->analog_datafeed_buf_fill;
+               analog.data = samples;
+               analog.meaning->channels = inc->analog_datafeed_channels[ch_idx];
+               analog.meaning->mq = 0;
+               analog.meaning->mqflags = 0;
+               analog.meaning->unit = 0;
+               analog.encoding->unitsize = sizeof(samples[0]);
+               analog.encoding->is_signed = TRUE;
+               analog.encoding->is_float = TRUE;
+#ifdef WORDS_BIGENDIAN
+               analog.encoding->is_bigendian = TRUE;
+#else
+               analog.encoding->is_bigendian = FALSE;
+#endif
+               analog.encoding->digits = spec.spec_digits;
+               rc = sr_session_send(in->sdi, &packet);
+               if (rc != SR_OK)
+                       return rc;
+               samples += inc->analog_datafeed_buf_size;
+       }
+
+       inc->analog_datafeed_buf_fill = 0;
+       return SR_OK;
+}
+
+static int queue_analog_samples(const struct sr_input *in)
+{
+       struct context *inc;
+       int rc;
+
+       inc = in->priv;
+       if (!inc->analog_channels)
+               return SR_OK;
+
+       inc->analog_datafeed_buf_fill++;
+       if (inc->analog_datafeed_buf_fill == inc->analog_datafeed_buf_size) {
+               rc = flush_analog_samples(in);
+               if (rc != SR_OK)
+                       return rc;
+       }
+       return SR_OK;
+}
+
 /* Helpers for "column processing". */
 
 static int split_column_format(const char *spec,
@@ -312,6 +482,7 @@ static int split_column_format(const char *spec,
        /* Get the (optional, decimal, default 1) column count. Accept '*'. */
        endp = NULL;
        if (*spec == '*') {
+               /* Workaround, strtoul("*") won't always yield expected endp. */
                count = 0;
                endp = (char *)&spec[1];
        } else {
@@ -328,7 +499,7 @@ static int split_column_format(const char *spec,
        /* Get the (mandatory, single letter) type spec (-/xob/l). */
        format_char = *spec++;
        switch (format_char) {
-       case '-':       /* Might conflict with number-parsing. */
+       case '-':
        case '/':
                format_char = '-';
                format_code = FORMAT_NONE;
@@ -343,6 +514,12 @@ static int split_column_format(const char *spec,
        case 'l':
                format_code = FORMAT_BIN;
                break;
+       case 'a':
+               format_code = FORMAT_ANALOG;
+               break;
+       case 't':
+               format_code = FORMAT_TIME;
+               break;
        default:        /* includes NUL */
                return SR_ERR_ARG;
        }
@@ -355,8 +532,8 @@ static int split_column_format(const char *spec,
        if (!endp)
                return SR_ERR_ARG;
        if (endp == spec)
-               count = 1;
-       if (format_char == '-')
+               count = format_is_analog(format_code) ? 3 : 1;
+       if (format_is_ignore(format_code))
                count = 0;
        if (format_char == 'l')
                count = 1;
@@ -371,17 +548,26 @@ static int split_column_format(const char *spec,
        return SR_OK;
 }
 
-static int make_column_details_from_format(struct context *inc,
-       const char *column_format)
+static int make_column_details_from_format(const struct sr_input *in,
+       const char *column_format, char **column_texts)
 {
+       struct context *inc;
        char **formats, *format;
-       size_t format_count, column_count, bit_count;
+       size_t format_count, column_count, logic_count, analog_count;
        size_t auto_column_count;
-       size_t format_idx, c, b, column_idx, channel_idx;
+       size_t format_idx, c, b, column_idx, channel_idx, analog_idx;
        enum single_col_format f;
        struct column_details *detail;
+       GString *channel_name;
+       size_t create_idx;
+       char *column;
+       const char *caption;
+       int channel_type, channel_sdi_nr;
        int ret;
 
+       inc = in->priv;
+       inc->column_seen_count = g_strv_length(column_texts);
+
        /* Split the input spec, count involved columns and bits. */
        formats = g_strsplit(column_format, ",", 0);
        if (!formats) {
@@ -394,7 +580,7 @@ static int make_column_details_from_format(struct context *inc,
                g_strfreev(formats);
                return SR_ERR_ARG;
        }
-       column_count = bit_count = 0;
+       column_count = logic_count = analog_count = 0;
        auto_column_count = 0;
        for (format_idx = 0; format_idx < format_count; format_idx++) {
                format = formats[format_idx];
@@ -416,35 +602,107 @@ static int make_column_details_from_format(struct context *inc,
                        c = auto_column_count;
                }
                column_count += c;
-               bit_count += c * b;
+               if (format_is_analog(f))
+                       analog_count += c;
+               else if (format_is_logic(f))
+                       logic_count += c * b;
        }
-       sr_dbg("Column format %s -> %zu columns, %zu logic channels.",
-               column_format, column_count, bit_count);
+       sr_dbg("Column format %s -> %zu columns, %zu logic, %zu analog channels.",
+               column_format, column_count, logic_count, analog_count);
 
-       /* Allocate and fill in "column processing" details. */
+       /* Allocate and fill in "column processing" details. Create channels. */
        inc->column_want_count = column_count;
+       if (inc->column_seen_count < inc->column_want_count) {
+               sr_err("Insufficient input text width for desired data amount, got %zu but want %zu columns.",
+                       inc->column_seen_count, inc->column_want_count);
+               g_strfreev(formats);
+               return SR_ERR_ARG;
+       }
        inc->column_details = g_malloc0_n(column_count, sizeof(inc->column_details[0]));
-       column_idx = channel_idx = 0;
+       column_idx = channel_idx = analog_idx = 0;
+       channel_name = g_string_sized_new(64);
        for (format_idx = 0; format_idx < format_count; format_idx++) {
+               /* Process a format field, which can span multiple columns. */
                format = formats[format_idx];
                (void)split_column_format(format, &c, &f, &b);
                if (f && !c)
                        c = auto_column_count;
                while (c-- > 0) {
+                       /* Fill in a column's processing details. */
                        detail = &inc->column_details[column_idx++];
                        detail->col_nr = column_idx;
                        detail->text_format = f;
-                       if (detail->text_format) {
+                       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 (format_is_logic(detail->text_format)) {
                                detail->channel_offset = channel_idx;
                                detail->channel_count = b;
-                               channel_idx += b;
+                               channel_idx += detail->channel_count;
+                       } 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;
+                       }
+                       /*
+                        * 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.
+                        *
+                        * Manipulation of the column's caption (when a header
+                        * line is seen) is acceptable, because this header
+                        * line won't get processed another time.
+                        */
+                       column = column_texts[detail->col_nr - 1];
+                       if (inc->use_header && column && *column)
+                               caption = sr_scpi_unquote_string(column);
+                       else
+                               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);
+                               } else if (caption) {
+                                       g_string_printf(channel_name, "%s[%zu]",
+                                               caption, create_idx);
+                               } else {
+                                       g_string_printf(channel_name, "%zu",
+                                               detail->channel_offset + create_idx);
+                               }
+                               if (format_is_analog(detail->text_format)) {
+                                       channel_sdi_nr = logic_count + detail->channel_offset + create_idx;
+                                       channel_type = SR_CHANNEL_ANALOG;
+                                       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);
                        }
-                       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);
                }
        }
        inc->logic_channels = channel_idx;
+       inc->analog_channels = analog_idx;
+       g_string_free(channel_name, TRUE);
        g_strfreev(formats);
 
        return SR_OK;
@@ -586,7 +844,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_NONE:
+               default:
                        /* ShouldNotHappen(TM), but silences compiler warning. */
                        return SR_ERR;
                }
@@ -603,6 +861,144 @@ static int parse_logic(const char *column, struct context *inc,
        return SR_OK;
 }
 
+/**
+ * @brief Parse a floating point text into an analog value.
+ *
+ * @param[in] column   The input text, a floating point number.
+ * @param[in] inc      The input module's context.
+ * @param[in] details  The column processing details.
+ *
+ * @retval SR_OK       Success.
+ * @retval SR_ERR      Invalid input data (empty, or format error).
+ *
+ * This routine modifies the analog values in the current sample set,
+ * based on the text input and a user provided format spec.
+ */
+static int parse_analog(const char *column, struct context *inc,
+       const struct column_details *details)
+{
+       size_t length;
+       double dvalue; float fvalue;
+       csv_analog_t value;
+       int ret;
+
+       if (!format_is_analog(details->text_format))
+               return SR_ERR_BUG;
+
+       length = strlen(column);
+       if (!length) {
+               sr_err("Column %zu in line %zu is empty.", details->col_nr,
+                       inc->line_number);
+               return SR_ERR;
+       }
+       if (sizeof(value) == sizeof(double)) {
+               ret = sr_atod_ascii(column, &dvalue);
+               value = dvalue;
+       } else if (sizeof(value) == sizeof(float)) {
+               ret = sr_atof_ascii(column, &fvalue);
+               value = fvalue;
+       } else {
+               ret = SR_ERR_BUG;
+       }
+       if (ret != SR_OK) {
+               sr_err("Cannot parse analog text %s in column %zu in line %zu.",
+                       column, details->col_nr, inc->line_number);
+               return SR_ERR_DATA;
+       }
+       set_analog_value(inc, details->channel_offset, value);
+
+       return SR_OK;
+}
+
+/**
+ * @brief Parse a timestamp text, auto-determine samplerate.
+ *
+ * @param[in] column   The input text, a floating point number.
+ * @param[in] inc      The input module's context.
+ * @param[in] details  The column processing details.
+ *
+ * @retval SR_OK       Success.
+ * @retval SR_ERR      Invalid input data (empty, or format error).
+ *
+ * This routine attempts to automatically determine the input data's
+ * samplerate from text rows' timestamp values. Only simple formats are
+ * supported, user provided values always take precedence.
+ */
+static int parse_timestamp(const char *column, struct context *inc,
+       const struct column_details *details)
+{
+       double ts, rate;
+       int ret;
+
+       if (!format_is_timestamp(details->text_format))
+               return SR_ERR_BUG;
+
+       /*
+        * Implementor's notes on timestamp interpretation. Use a simple
+        * approach for improved maintainability which covers most cases
+        * of input data. There is not much gain in adding complexity,
+        * users can easily provide the rate when auto-detection fails.
+        * - Bail out if samplerate is known already.
+        * - Try to interpret the timestamp (simple float conversion).
+        *   If conversion fails then clear all previous knowledge and
+        *   bail out (non-fatal, perhaps warn). Silently ignore values
+        *   of zero since those could be silent fails -- assume that
+        *   genuine data contains at least two adjacent rows with useful
+        *   timestamps for the feature to work reliably. Annoying users
+        *   with "failed to detect" messages is acceptable here, since
+        *   users expecting the feature to work should provide useful
+        *   data, and there are easy ways to disable the detection or
+        *   ignore the column.
+        * - If there is no previous timestamp, keep the current value
+        *   for later reference and bail out.
+        * - If a previous timestamp was seen, determine the difference
+        *   between them, and derive the samplerate. Update internal
+        *   state (the value automatically gets sent to the datafeed),
+        *   and clear previous knowledge. Subsequent calls will ignore
+        *   following input data (see above, rate is known).
+        *
+        * TODO Potential future improvements:
+        * - Prefer rationals over floats for improved precision and
+        *   reduced rounding errors which result in odd rates.
+        * - Support other formats ("2 ms" or similar)?
+        */
+       if (inc->samplerate)
+               return SR_OK;
+       ret = sr_atod_ascii(column, &ts);
+       if (ret != SR_OK)
+               ts = 0.0;
+       if (!ts) {
+               sr_warn("Cannot convert timestamp text %s in line %zu (or zero value).",
+                       column, inc->line_number);
+               inc->prev_timestamp = 0.0;
+               return SR_OK;
+       }
+       if (!inc->prev_timestamp) {
+               sr_dbg("First timestamp value %g in line %zu.",
+                       ts, inc->line_number);
+               inc->prev_timestamp = ts;
+               return SR_OK;
+       }
+       sr_dbg("Second timestamp value %g in line %zu.", ts, inc->line_number);
+       ts -= inc->prev_timestamp;
+       sr_dbg("Timestamp difference %g in line %zu.",
+               ts, inc->line_number);
+       if (!ts) {
+               sr_warn("Zero timestamp difference in line %zu.",
+                       inc->line_number);
+               inc->prev_timestamp = ts;
+               return SR_OK;
+       }
+       rate = 1.0 / ts;
+       rate += 0.5;
+       rate = (uint64_t)rate;
+       sr_dbg("Rate from timestamp %g in line %zu.", rate, inc->line_number);
+       inc->samplerate = rate;
+       inc->prev_timestamp = 0.0;
+
+       return SR_OK;
+}
+
 /**
  * @brief Parse routine which ignores the input text.
  *
@@ -626,6 +1022,8 @@ static const col_parse_cb col_parse_funcs[] = {
        [FORMAT_BIN] = parse_logic,
        [FORMAT_OCT] = parse_logic,
        [FORMAT_HEX] = parse_logic,
+       [FORMAT_ANALOG] = parse_analog,
+       [FORMAT_TIME] = parse_timestamp,
 };
 
 static int init(struct sr_input *in, GHashTable *options)
@@ -640,16 +1038,13 @@ static int init(struct sr_input *in, GHashTable *options)
        in->priv = inc = g_malloc0(sizeof(*inc));
 
        single_column = g_variant_get_uint32(g_hash_table_lookup(options, "single_column"));
-
        logic_channels = g_variant_get_uint32(g_hash_table_lookup(options, "logic_channels"));
-
        inc->delimiter = g_string_new(g_variant_get_string(
                        g_hash_table_lookup(options, "column_separator"), NULL));
        if (!inc->delimiter->len) {
                sr_err("Column separator cannot be empty.");
                return SR_ERR_ARG;
        }
-
        s = g_variant_get_string(g_hash_table_lookup(options, "single_format"), NULL);
        if (g_ascii_strncasecmp(s, "bin", 3) == 0) {
                format = FORMAT_BIN;
@@ -661,7 +1056,6 @@ static int init(struct sr_input *in, GHashTable *options)
                sr_err("Invalid single-column format: '%s'", s);
                return SR_ERR_ARG;
        }
-
        inc->comment = g_string_new(g_variant_get_string(
                        g_hash_table_lookup(options, "comment_leader"), NULL));
        if (g_string_equal(inc->comment, inc->delimiter)) {
@@ -674,13 +1068,9 @@ static int init(struct sr_input *in, GHashTable *options)
                sr_warn("Comment leader and column separator conflict, disabling comment support.");
                g_string_truncate(inc->comment, 0);
        }
-
        inc->samplerate = g_variant_get_uint64(g_hash_table_lookup(options, "samplerate"));
-
        first_column = g_variant_get_uint32(g_hash_table_lookup(options, "first_column"));
-
        inc->use_header = g_variant_get_boolean(g_hash_table_lookup(options, "header"));
-
        inc->start_line = g_variant_get_uint32(g_hash_table_lookup(options, "start_line"));
        if (inc->start_line < 1) {
                sr_err("Invalid start line %zu.", inc->start_line);
@@ -792,19 +1182,16 @@ static const char *get_line_termination(GString *buf)
 static int initial_parse(const struct sr_input *in, GString *buf)
 {
        struct context *inc;
-       GString *channel_name;
-       size_t num_columns, ch_idx, ch_name_idx, col_idx, col_nr;
+       size_t num_columns;
        size_t line_number, line_idx;
        int ret;
-       char **lines, *line, **columns, *column;
-       const char *col_caption;
-       gboolean got_caption;
-       const struct column_details *detail;
+       char **lines, *line, **columns;
 
        ret = SR_OK;
        inc = in->priv;
        columns = NULL;
 
+       /* Search for the first line to process (header or data). */
        line_number = 0;
        if (inc->termination)
                lines = g_strsplit(buf->str, inc->termination, 0);
@@ -835,7 +1222,7 @@ static int initial_parse(const struct sr_input *in, GString *buf)
                goto out;
        }
 
-       /* See how many columns the current line has. */
+       /* Get the number of columns in the line. */
        columns = split_line(line, inc);
        if (!columns) {
                sr_err("Error while parsing line %zu.", line_number);
@@ -851,83 +1238,81 @@ static int initial_parse(const struct sr_input *in, GString *buf)
        sr_dbg("DIAG Got %zu columns in text line: %s.", num_columns, line);
 
        /*
-        * Track the observed number of columns in the input file. Do
-        * process the previously gathered columns format spec now that
-        * automatic channel count can be dealt with.
+        * Interpret the user provided column format specs. This might
+        * involve inspection of the now received input text, to support
+        * e.g. automatic detection of channel counts in the absence of
+        * user provided specs. Optionally a header line is used to get
+        * channels' names.
+        *
+        * Check the then created channels for consistency across .reset
+        * and .receive sequences (file re-load).
         */
-       inc->column_seen_count = num_columns;
-       ret = make_column_details_from_format(inc, inc->column_formats);
+       ret = make_column_details_from_format(in, inc->column_formats, columns);
        if (ret != SR_OK) {
                sr_err("Cannot parse columns format using line %zu.", line_number);
                goto out;
        }
-
-       /*
-        * Assume all lines have equal length (column count). Bail out
-        * early on suspicious or insufficient input data (check input
-        * which became available here against previous user specs or
-        * auto-determined properties, regardless of layout variant).
-        */
-       if (num_columns < inc->column_want_count) {
-               sr_err("Insufficient input text width for desired data amount, got %zu but want %zu columns.",
-                       num_columns, inc->column_want_count);
-               ret = SR_ERR;
-               goto out;
-       }
-
-       /*
-        * Determine channel names. Optionally use text from a header
-        * line (when requested by the user, and only works in multi
-        * column mode). In the absence of header text, or in single
-        * column mode, channels are assigned rather generic names.
-        *
-        * Manipulation of the column's caption is acceptable here, the
-        * header line will never get processed another time.
-        */
-       channel_name = g_string_sized_new(64);
-       for (col_idx = 0; col_idx < inc->column_want_count; col_idx++) {
-
-               col_nr = col_idx + 1;
-               detail = lookup_column_details(inc, col_nr);
-               if (detail->text_format == FORMAT_NONE)
-                       continue;
-               column = columns[col_idx];
-               col_caption = sr_scpi_unquote_string(column);
-               got_caption = inc->use_header && *col_caption;
-               sr_dbg("DIAG col %zu, ch count %zu, text %s.",
-                       col_nr, detail->channel_count, col_caption);
-               for (ch_idx = 0; ch_idx < detail->channel_count; ch_idx++) {
-                       ch_name_idx = detail->channel_offset + ch_idx;
-                       if (got_caption && detail->channel_count == 1)
-                               g_string_assign(channel_name, col_caption);
-                       else if (got_caption)
-                               g_string_printf(channel_name, "%s[%zu]",
-                                       col_caption, ch_idx);
-                       else
-                               g_string_printf(channel_name, "%zu", ch_name_idx);
-                       sr_dbg("DIAG ch idx %zu, name %s.", ch_name_idx, channel_name->str);
-                       sr_channel_new(in->sdi, ch_name_idx, SR_CHANNEL_LOGIC, TRUE,
-                               channel_name->str);
-               }
-       }
-       g_string_free(channel_name, TRUE);
        if (!check_header_in_reread(in)) {
                ret = SR_ERR_DATA;
                goto out;
        }
 
        /*
+        * Allocate buffer memory for datafeed submission of sample data.
         * Calculate the minimum buffer size to store the set of samples
         * of all channels (unit size). Determine a larger buffer size
         * 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.
         */
-       inc->sample_unit_size = (inc->logic_channels + 7) / 8;
-       inc->datafeed_buf_size = CHUNK_SIZE;
-       inc->datafeed_buf_size *= inc->sample_unit_size;
-       inc->datafeed_buffer = g_malloc(inc->datafeed_buf_size);
-       inc->datafeed_buf_fill = 0;
+       if (inc->logic_channels) {
+               inc->sample_unit_size = (inc->logic_channels + 7) / 8;
+               inc->datafeed_buf_size = CHUNK_SIZE;
+               inc->datafeed_buf_size *= inc->sample_unit_size;
+               inc->datafeed_buffer = g_malloc(inc->datafeed_buf_size);
+               if (!inc->datafeed_buffer) {
+                       sr_err("Cannot allocate datafeed send buffer (logic).");
+                       ret = SR_ERR_MALLOC;
+                       goto out;
+               }
+               inc->datafeed_buf_fill = 0;
+       }
+
+       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;
+               inc->analog_datafeed_buf_size /= inc->analog_channels;
+               sample_count = inc->analog_channels * inc->analog_datafeed_buf_size;
+               inc->analog_datafeed_buffer = g_malloc0(sample_count * sample_size);
+               if (!inc->analog_datafeed_buffer) {
+                       sr_err("Cannot allocate datafeed send buffer (analog).");
+                       ret = SR_ERR_MALLOC;
+                       goto out;
+               }
+               inc->analog_datafeed_buf_fill = 0;
+               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;
+               }
+       }
 
 out:
        if (columns)
@@ -1082,6 +1467,7 @@ static int process_buffer(struct sr_input *in, gboolean is_eof)
 
                /* Have the columns of the current text line processed. */
                clear_logic_samples(inc);
+               clear_analog_samples(inc);
                for (col_idx = 0; col_idx < inc->column_want_count; col_idx++) {
                        column = columns[col_idx];
                        col_nr = col_idx + 1;
@@ -1101,6 +1487,7 @@ static int process_buffer(struct sr_input *in, gboolean is_eof)
 
                /* Send sample data to the session bus (buffered). */
                ret = queue_logic_samples(in);
+               ret += queue_analog_samples(in);
                if (ret != SR_OK) {
                        sr_err("Sending samples failed.");
                        g_strfreev(columns);
@@ -1155,6 +1542,7 @@ static int end(struct sr_input *in)
                return ret;
 
        ret = flush_logic_samples(in);
+       ret += flush_analog_samples(in);
        if (ret != SR_OK)
                return ret;
 
@@ -1177,6 +1565,8 @@ static void cleanup(struct sr_input *in)
        inc->termination = NULL;
        g_free(inc->datafeed_buffer);
        inc->datafeed_buffer = NULL;
+       g_free(inc->analog_datafeed_buffer);
+       inc->analog_datafeed_buffer = NULL;
 }
 
 static int reset(struct sr_input *in)
@@ -1207,17 +1597,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, t for timestamps.",
                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] = {
@@ -1227,7 +1617,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] = {
@@ -1237,12 +1627,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] = {
@@ -1252,7 +1642,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,