+ struct dev_context *devc;
+ struct sr_serial_dev_inst *serial;
+ int ret;
+ size_t rec_idx;
+
+ devc = sdi->priv;
+ serial = sdi->conn;
+ serial_flush(serial);
+
+ /*
+ * Send an acquisition start command which depends on the
+ * currently selected data source. Enter monitor mode for
+ * Live readings, get saved or recorded data otherwise. The
+ * latter require queries for sample counts, then run chunked
+ * download sequences (single item for Save, set of samples
+ * for Recordings).
+ */
+ if (devc->data_source == DATA_SOURCE_LIVE) {
+ ret = ut181a_send_cmd_monitor(serial, TRUE);
+ } else if (devc->data_source == DATA_SOURCE_SAVE) {
+ /*
+ * There is only one sequence of saved measurements in
+ * the device, but its length is yet unknown. Determine
+ * the number of saved items, and initiate the reception
+ * of the first value. Completion of data reception will
+ * drive subsequent progress.
+ */
+ ret = ut181a_send_cmd_get_save_count(serial);
+ if (ret < 0)
+ return ret;
+ ret = ut181a_configure_waitfor(devc, FALSE, 0, 0,
+ FALSE, FALSE, TRUE, FALSE);
+ if (ret < 0)
+ return ret;
+ ret = ut181a_waitfor_response(sdi, 200);
+ if (ret < 0)
+ return ret;
+ devc->info.save_info.save_count = devc->wait_state.data_value;
+ devc->info.save_info.save_idx = 0;
+ ret = ut181a_send_cmd_get_saved_value(serial, 0);
+ } else if (devc->data_source >= DATA_SOURCE_REC_FIRST) {
+ /*
+ * When we get here, the data source got selected, which
+ * includes an update of the device's list of recordings.
+ * So the index should be good, just the number of samples
+ * in that recording is yet unknown. Get the sample count
+ * and initiate the reception of the first chunk, completed
+ * reception of a chunk advances through the sequence.
+ */
+ rec_idx = devc->data_source - DATA_SOURCE_REC_FIRST;
+ if (rec_idx >= devc->record_count)
+ return SR_ERR_DATA;
+ devc->info.rec_info.rec_count = devc->record_count;
+ devc->info.rec_info.rec_idx = rec_idx;
+ devc->info.rec_info.auto_next = 0;
+ devc->info.rec_info.auto_feed = 1;
+ ret = ut181a_send_cmd_get_rec_info(serial, rec_idx);
+ if (ret < 0)
+ return ret;
+ ret = ut181a_configure_waitfor(devc,
+ FALSE, CMD_CODE_GET_REC_INFO, 0,
+ FALSE, FALSE, FALSE, FALSE);
+ if (ret < 0)
+ return ret;
+ ret = ut181a_waitfor_response(sdi, 200);
+ if (ret < 0)
+ return ret;
+ devc->info.rec_data.samples_total = devc->wait_state.data_value;
+ devc->info.rec_data.samples_curr = 0;
+ ret = ut181a_send_cmd_get_rec_samples(serial, rec_idx, 0);
+ } else {
+ sr_err("Unhandled data source %d, programming error?",
+ (int)devc->data_source);
+ ret = SR_ERR_BUG;
+ }
+ if (ret < 0)
+ return ret;
+
+ sr_sw_limits_acquisition_start(&devc->limits);
+ devc->recv_count = 0;
+ std_session_send_df_header(sdi);