+/* Set the next event to wait for in siglent_sds_receive(). */
+static void siglent_sds_set_wait_event(struct dev_context *devc, enum wait_events event)
+{
+ if (event == WAIT_STOP) {
+ devc->wait_status = 2;
+ } else {
+ devc->wait_status = 1;
+ devc->wait_event = event;
+ }
+}
+
+/*
+ * Waiting for a event will return a timeout after 2 to 3 seconds in order
+ * to not block the application.
+ */
+static int siglent_sds_event_wait(const struct sr_dev_inst *sdi)
+{
+ char *buf;
+ long s;
+ int out;
+ struct dev_context *devc;
+ time_t start;
+
+ if (!(devc = sdi->priv))
+ return SR_ERR;
+
+ start = time(NULL);
+
+ s = 10000; /* Sleep time for status refresh. */
+ if (devc->wait_status == 1) {
+ do {
+ if (time(NULL) - start >= 3) {
+ sr_dbg("Timeout waiting for trigger.");
+ return SR_ERR_TIMEOUT;
+ }
+
+ if (sr_scpi_get_string(sdi->conn, ":INR?", &buf) != SR_OK)
+ return SR_ERR;
+ sr_atoi(buf, &out);
+ g_free(buf);
+ g_usleep(s);
+ } while (out == 0);
+
+ sr_dbg("Device triggered.");
+
+ if ((devc->timebase < 0.51) && (devc->timebase > 0.99e-6)) {
+ /*
+ * Timebase * num hor. divs * 85(%) * 1e6(usecs) / 100
+ * -> 85 percent of sweep time
+ */
+ s = (devc->timebase * devc->model->series->num_horizontal_divs * 1000);
+ sr_spew("Sleeping for %ld usecs after trigger, "
+ "to let the acq buffer in the device fill", s);
+ g_usleep(s);
+ }
+ }
+ if (devc->wait_status == 2) {
+ do {
+ if (time(NULL) - start >= 3) {
+ sr_dbg("Timeout waiting for trigger.");
+ return SR_ERR_TIMEOUT;
+ }
+ if (sr_scpi_get_string(sdi->conn, ":INR?", &buf) != SR_OK)
+ return SR_ERR;
+ sr_atoi(buf, &out);
+ g_free(buf);
+ g_usleep(s);
+ /* XXX
+ * Now this loop condition looks suspicious! A bitwise
+ * OR of a variable and a non-zero literal should be
+ * non-zero. Logical AND of several non-zero values
+ * should be non-zero. Are many parts of the condition
+ * not taking effect? Was some different condition meant
+ * to get encoded? This needs review, and adjustment.
+ */
+ } while (out != DEVICE_STATE_TRIG_RDY || out != DEVICE_STATE_DATA_TRIG_RDY || out != DEVICE_STATE_STOPPED);
+
+ sr_dbg("Device triggered.");
+
+ siglent_sds_set_wait_event(devc, WAIT_NONE);
+ }
+
+ return SR_OK;
+}
+
+static int siglent_sds_trigger_wait(const struct sr_dev_inst *sdi)
+{
+ struct dev_context *devc;
+
+ if (!(devc = sdi->priv))
+ return SR_ERR;
+ return siglent_sds_event_wait(sdi);
+}
+
+/* Wait for scope to got to "Stop" in single shot mode. */
+static int siglent_sds_stop_wait(const struct sr_dev_inst *sdi)
+{
+ return siglent_sds_event_wait(sdi);
+}
+
+/* Send a configuration setting. */
+SR_PRIV int siglent_sds_config_set(const struct sr_dev_inst *sdi, const char *format, ...)
+{
+ va_list args;
+ int ret;
+
+ va_start(args, format);
+ ret = sr_scpi_send_variadic(sdi->conn, format, args);
+ va_end(args);
+
+ return ret;
+}
+
+/* Start capturing a new frameset. */
+SR_PRIV int siglent_sds_capture_start(const struct sr_dev_inst *sdi)
+{
+ struct dev_context *devc;
+
+ if (!(devc = sdi->priv))
+ return SR_ERR;
+
+ switch (devc->model->series->protocol) {
+ case SPO_MODEL:
+ if (devc->data_source == DATA_SOURCE_SCREEN) {
+ char *buf;
+ int out;
+
+ sr_dbg("Starting data capture for active frameset %" PRIu64 " of %" PRIu64,
+ devc->num_frames + 1, devc->limit_frames);
+ if (siglent_sds_config_set(sdi, "ARM") != SR_OK)
+ return SR_ERR;
+ if (sr_scpi_get_string(sdi->conn, ":INR?", &buf) != SR_OK)
+ return SR_ERR;
+ sr_atoi(buf, &out);
+ g_free(buf);
+ if (out == DEVICE_STATE_TRIG_RDY) {
+ siglent_sds_set_wait_event(devc, WAIT_TRIGGER);
+ } else if (out == DEVICE_STATE_DATA_TRIG_RDY) {
+ sr_spew("Device triggered.");
+ siglent_sds_set_wait_event(devc, WAIT_BLOCK);
+ return SR_OK;
+ } else {
+ sr_spew("Device did not enter ARM mode.");
+ return SR_ERR;
+ }
+ } else { /* TODO: Implement history retrieval. */
+ unsigned int framecount;
+ char buf[200];
+ int ret;
+
+ sr_dbg("Starting data capture for history frameset.");
+ if (siglent_sds_config_set(sdi, "FPAR?") != SR_OK)
+ return SR_ERR;
+ ret = sr_scpi_read_data(sdi->conn, buf, 200);
+ if (ret < 0) {
+ sr_err("Read error while reading data header.");
+ return SR_ERR;
+ }
+ memcpy(&framecount, buf + 40, 4);
+ if (devc->limit_frames > framecount)
+ sr_err("Frame limit higher than frames in buffer of device!");
+ else if (devc->limit_frames == 0)
+ devc->limit_frames = framecount;
+ sr_dbg("Starting data capture for history frameset %" PRIu64 " of %" PRIu64,
+ devc->num_frames + 1, devc->limit_frames);
+ if (siglent_sds_config_set(sdi, "FRAM %i", devc->num_frames + 1) != SR_OK)
+ return SR_ERR;
+ if (siglent_sds_channel_start(sdi) != SR_OK)
+ return SR_ERR;
+ siglent_sds_set_wait_event(devc, WAIT_STOP);
+ }
+ break;
+ case ESERIES:
+ if (devc->data_source == DATA_SOURCE_SCREEN) {
+ char *buf;
+ int out;
+
+ sr_dbg("Starting data capture for active frameset %" PRIu64 " of %" PRIu64,
+ devc->num_frames + 1, devc->limit_frames);
+ if (siglent_sds_config_set(sdi, "ARM") != SR_OK)
+ return SR_ERR;
+ if (sr_scpi_get_string(sdi->conn, ":INR?", &buf) != SR_OK)
+ return SR_ERR;
+ sr_atoi(buf, &out);
+ g_free(buf);
+ if (out == DEVICE_STATE_TRIG_RDY) {
+ siglent_sds_set_wait_event(devc, WAIT_TRIGGER);
+ } else if (out == DEVICE_STATE_DATA_TRIG_RDY) {
+ sr_spew("Device triggered.");
+ siglent_sds_set_wait_event(devc, WAIT_BLOCK);
+ return SR_OK;
+ } else {
+ sr_spew("Device did not enter ARM mode.");
+ return SR_ERR;
+ }
+ } else { /* TODO: Implement history retrieval. */
+ unsigned int framecount;
+ char buf[200];
+ int ret;
+
+ sr_dbg("Starting data capture for history frameset.");
+ if (siglent_sds_config_set(sdi, "FPAR?") != SR_OK)
+ return SR_ERR;
+ ret = sr_scpi_read_data(sdi->conn, buf, 200);
+ if (ret < 0) {
+ sr_err("Read error while reading data header.");
+ return SR_ERR;
+ }
+ memcpy(&framecount, buf + 40, 4);
+ if (devc->limit_frames > framecount)
+ sr_err("Frame limit higher than frames in buffer of device!");
+ else if (devc->limit_frames == 0)
+ devc->limit_frames = framecount;
+ sr_dbg("Starting data capture for history frameset %" PRIu64 " of %" PRIu64,
+ devc->num_frames + 1, devc->limit_frames);
+ if (siglent_sds_config_set(sdi, "FRAM %i", devc->num_frames + 1) != SR_OK)
+ return SR_ERR;
+ if (siglent_sds_channel_start(sdi) != SR_OK)
+ return SR_ERR;
+ siglent_sds_set_wait_event(devc, WAIT_STOP);
+ }
+ break;
+ case NON_SPO_MODEL:
+ siglent_sds_set_wait_event(devc, WAIT_TRIGGER);
+ break;
+ }
+
+ return SR_OK;
+}
+
+/* Start reading data from the current channel. */
+SR_PRIV int siglent_sds_channel_start(const struct sr_dev_inst *sdi)
+{
+ struct dev_context *devc;
+ struct sr_channel *ch;
+ const char *s;
+
+ if (!(devc = sdi->priv))
+ return SR_ERR;
+
+ ch = devc->channel_entry->data;
+
+ sr_dbg("Start reading data from channel %s.", ch->name);
+
+ switch (devc->model->series->protocol) {
+ case NON_SPO_MODEL:
+ case SPO_MODEL:
+ s = (ch->type == SR_CHANNEL_LOGIC) ? "D%d:WF?" : "C%d:WF? ALL";
+ if (sr_scpi_send(sdi->conn, s, ch->index + 1) != SR_OK)
+ return SR_ERR;
+ siglent_sds_set_wait_event(devc, WAIT_NONE);
+ break;
+ case ESERIES:
+ if (ch->type == SR_CHANNEL_ANALOG) {
+ if (sr_scpi_send(sdi->conn, "C%d:WF? ALL",
+ ch->index + 1) != SR_OK)
+ return SR_ERR;
+ }
+ siglent_sds_set_wait_event(devc, WAIT_NONE);
+ if (sr_scpi_read_begin(sdi->conn) != SR_OK)
+ return TRUE;
+ siglent_sds_set_wait_event(devc, WAIT_BLOCK);
+ break;
+ }
+
+ devc->num_channel_bytes = 0;
+ devc->num_header_bytes = 0;
+ devc->num_block_bytes = 0;
+
+ return SR_OK;
+}
+
+/* Read the header of a data block. */
+static int siglent_sds_read_header(struct sr_dev_inst *sdi)
+{
+ struct sr_scpi_dev_inst *scpi = sdi->conn;
+ struct dev_context *devc = sdi->priv;
+ char *buf = (char *)devc->buffer;
+ int ret, desc_length;
+ int block_offset = 15; /* Offset for descriptor block. */
+ long data_length = 0;
+
+ /* Read header from device. */
+ ret = sr_scpi_read_data(scpi, buf, SIGLENT_HEADER_SIZE);
+ if (ret < SIGLENT_HEADER_SIZE) {
+ sr_err("Read error while reading data header.");
+ return SR_ERR;
+ }
+ sr_dbg("Device returned %i bytes.", ret);
+ devc->num_header_bytes += ret;
+ buf += block_offset; /* Skip to start descriptor block. */
+
+ /* Parse WaveDescriptor header. */
+ memcpy(&desc_length, buf + 36, 4); /* Descriptor block length */
+ memcpy(&data_length, buf + 60, 4); /* Data block length */
+
+ devc->block_header_size = desc_length + 15;
+ devc->num_samples = data_length;
+
+ sr_dbg("Received data block header: '%s' -> block length %d.", buf, ret);
+
+ return ret;
+}
+
+static int siglent_sds_get_digital(const struct sr_dev_inst *sdi, struct sr_channel *ch)
+{
+ struct sr_scpi_dev_inst *scpi = sdi->conn;
+ struct dev_context *devc = sdi->priv;
+ GArray *tmp_samplebuf; /* Temp buffer while iterating over the scope samples */
+ char *buf = (char *)devc->buffer; /* Buffer from scope */
+ uint8_t tmp_value; /* Holding temp value from data */
+ GArray *data_low_channels, *data_high_channels, *buffdata;
+ GSList *l;
+ gboolean low_channels; /* Lower channels enabled */
+ gboolean high_channels; /* Higher channels enabled */
+ int len, channel_index;
+ uint64_t samples_index;
+
+ len = 0;
+ channel_index = 0;
+ low_channels = FALSE;
+ high_channels = FALSE;
+ data_low_channels = g_array_new(FALSE, TRUE, sizeof(uint8_t));
+ data_high_channels = g_array_new(FALSE, TRUE, sizeof(uint8_t));
+
+ for (l = sdi->channels; l; l = l->next) {
+ ch = l->data;
+ samples_index = 0;
+ if (ch->type == SR_CHANNEL_LOGIC) {
+ if (ch->enabled) {
+ if (sr_scpi_send(sdi->conn, "D%d:WF? DAT2", ch->index) != SR_OK)
+ return SR_ERR;
+ if (sr_scpi_read_begin(scpi) != SR_OK)
+ return TRUE;
+ len = sr_scpi_read_data(scpi, buf, -1);
+ if (len < 0)
+ return TRUE;
+ len -= 15;
+ buffdata = g_array_sized_new(FALSE, FALSE, sizeof(uint8_t), len);
+ buf += 15; /* Skipping the data header. */
+ g_array_append_vals(buffdata, buf, len);
+ tmp_samplebuf = g_array_sized_new(FALSE, FALSE, sizeof(uint8_t), len); /* New temp buffer. */
+ for (uint64_t cur_sample_index = 0; cur_sample_index < devc->memory_depth_digital; cur_sample_index++) {
+ char sample = (char)g_array_index(buffdata, uint8_t, cur_sample_index);
+ for (int ii = 0; ii < 8; ii++, sample >>= 1) {
+ if (ch->index < 8) {
+ channel_index = ch->index;
+ if (data_low_channels->len <= samples_index) {
+ tmp_value = 0; /* New sample. */
+ low_channels = TRUE; /* We have at least one enabled low channel. */
+ } else {
+ /* Get previous stored sample from low channel buffer. */
+ tmp_value = g_array_index(data_low_channels, uint8_t, samples_index);
+ }
+ } else {
+ channel_index = ch->index - 8;
+ if (data_high_channels->len <= samples_index) {
+ tmp_value = 0; /* New sample. */
+ high_channels = TRUE; /* We have at least one enabled high channel. */
+ } else {
+ /* Get previous stored sample from high channel buffer. */
+ tmp_value = g_array_index(data_high_channels, uint8_t, samples_index);
+ }
+ }
+ /* Check if the current scope sample bit is set. */
+ if (sample & 0x1)
+ tmp_value |= 1UL << channel_index; /* Set current scope sample bit based on channel index. */
+ g_array_append_val(tmp_samplebuf, tmp_value);
+ samples_index++;
+ }
+ }
+
+ /* Clear the buffers to prepare for the new samples */
+ if (ch->index < 8) {
+ g_array_free(data_low_channels, FALSE);
+ data_low_channels = g_array_new(FALSE, FALSE, sizeof(uint8_t));
+ } else {
+ g_array_free(data_high_channels, FALSE);
+ data_high_channels = g_array_new(FALSE, FALSE, sizeof(uint8_t));
+ }
+
+ /* Storing the converted temp values from the the scope into the buffers. */
+ for (uint64_t index = 0; index < tmp_samplebuf->len; index++) {
+ uint8_t value = g_array_index(tmp_samplebuf, uint8_t, index);
+ if (ch->index < 8)
+ g_array_append_val(data_low_channels, value);
+ else
+ g_array_append_val(data_high_channels, value);
+ }
+ g_array_free(tmp_samplebuf, TRUE);
+ g_array_free(buffdata, TRUE);
+ }
+ }
+ }
+
+ /* Combining the lower and higher channel buffers into one buffer for sigrok. */
+ devc->dig_buffer = g_array_new(FALSE, FALSE, sizeof(uint8_t));
+ for (uint64_t index = 0; index < devc->memory_depth_digital; index++) {
+ uint8_t value;
+ if (low_channels) {
+ value = g_array_index(data_low_channels, uint8_t, index);
+ g_array_append_val(devc->dig_buffer, value);
+ } else {
+ value = 0;
+ g_array_append_val(devc->dig_buffer, value);
+ }
+ if (high_channels) {
+ value = g_array_index(data_high_channels, uint8_t, index);
+ g_array_append_val(devc->dig_buffer, value);
+ } else {
+ value = 0;
+ g_array_append_val(devc->dig_buffer, value);
+ }
+ }
+
+ g_array_free(data_low_channels, TRUE);
+ g_array_free(data_high_channels, TRUE);
+
+ return len;
+}
+
+SR_PRIV int siglent_sds_receive(int fd, int revents, void *cb_data)