+ /* Round up to nearest power of two. */
+ for (i = MIN_PACKET_SIZE; i < data_left; i *= 2)
+ ;
+ data_left_2 = i;
+
+ sr_spew("data_amount: %u (rounded to power of 2: %u)", data_left, data_left_2);
+
+ return data_left_2;
+}
+
+static void send_chunk(struct sr_dev_inst *sdi, unsigned char *buf,
+ int num_samples)
+{
+ 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;
+ struct dev_context *devc = sdi->priv;
+ int num_channels, data_offset, i;
+
+ const float ch1_bit = RANGE(0) / 255;
+ const float ch2_bit = RANGE(1) / 255;
+ const float ch1_center = RANGE(0) / 2;
+ const float ch2_center = RANGE(1) / 2;
+
+ const gboolean ch1_ena = !!devc->ch_enabled[0];
+ const gboolean ch2_ena = !!devc->ch_enabled[1];
+
+ sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
+
+ num_channels = (ch1_ena && ch2_ena) ? 2 : 1;
+ packet.type = SR_DF_ANALOG;
+ packet.payload = &analog;
+
+ analog.meaning->channels = devc->enabled_channels;
+ analog.num_samples = num_samples;
+ analog.meaning->mq = SR_MQ_VOLTAGE;
+ analog.meaning->unit = SR_UNIT_VOLT;
+ analog.meaning->mqflags = 0;
+
+ analog.data = g_try_malloc(analog.num_samples * sizeof(float) * num_channels);
+ if (!analog.data) {
+ sr_err("Analog data buffer malloc failed.");
+ devc->dev_state = STOPPING;
+ return;
+ }
+
+ data_offset = 0;
+ for (i = 0; i < num_samples; i++) {
+ /*
+ * The device always sends data for both channels. If a channel
+ * is disabled, it contains a copy of the enabled channel's
+ * data. However, we only send the requested channels to
+ * the bus.
+ *
+ * Voltage values are encoded as a value 0-255, where the
+ * value is a point in the range represented by the vdiv
+ * setting. There are 10 vertical divs, so e.g. 500mV/div
+ * represents 5V peak-to-peak where 0 = -2.5V and 255 = +2.5V.
+ */
+ if (ch1_ena)
+ ((float *)analog.data)[data_offset++] = (ch1_bit * *(buf + i * 2) - ch1_center);
+ if (ch2_ena)
+ ((float *)analog.data)[data_offset++] = (ch2_bit * *(buf + i * 2 + 1) - ch2_center);
+ }
+
+ sr_session_send(sdi, &packet);
+ g_free(analog.data);
+}
+
+static void send_data(struct sr_dev_inst *sdi, struct libusb_transfer *buf[], uint64_t samples)
+{
+ int i = 0;
+ uint64_t send = 0;
+ uint32_t chunk;
+
+ while (send < samples) {
+ chunk = MIN(samples - send, (uint64_t)(buf[i]->actual_length / NUM_CHANNELS));
+ send += chunk;
+ send_chunk(sdi, buf[i]->buffer, chunk);
+
+ /*
+ * Everything in this transfer was either copied to the buffer
+ * or sent to the session bus.
+ */
+ g_free(buf[i]->buffer);
+ libusb_free_transfer(buf[i]);
+ i++;
+ }
+}
+
+/*
+ * Called by libusb (as triggered by handle_event()) when a transfer comes in.
+ * Only channel data comes in asynchronously, and all transfers for this are
+ * queued up beforehand, so this just needs to chuck the incoming data onto
+ * the libsigrok session bus.
+ */
+static void LIBUSB_CALL receive_transfer(struct libusb_transfer *transfer)
+{
+ struct sr_dev_inst *sdi;
+ struct dev_context *devc;
+
+ sdi = transfer->user_data;
+ devc = sdi->priv;
+
+ if (devc->dev_state == FLUSH) {
+ g_free(transfer->buffer);
+ libusb_free_transfer(transfer);
+ devc->dev_state = CAPTURE;
+ devc->aq_started = g_get_monotonic_time();
+ read_channel(sdi, data_amount(sdi));
+ return;
+ }
+
+ if (devc->dev_state != CAPTURE)
+ return;
+
+ if (!devc->sample_buf) {
+ devc->sample_buf_size = 10;
+ devc->sample_buf = g_try_malloc(devc->sample_buf_size * sizeof(transfer));
+ devc->sample_buf_write = 0;
+ }
+
+ if (devc->sample_buf_write >= devc->sample_buf_size) {
+ devc->sample_buf_size += 10;
+ devc->sample_buf = g_try_realloc(devc->sample_buf,
+ devc->sample_buf_size * sizeof(transfer));
+ if (!devc->sample_buf) {
+ sr_err("Sample buffer malloc failed.");
+ devc->dev_state = STOPPING;
+ return;
+ }
+ }
+
+ devc->sample_buf[devc->sample_buf_write++] = transfer;
+ devc->samp_received += transfer->actual_length / NUM_CHANNELS;
+
+ sr_spew("receive_transfer(): calculated samplerate == %" PRIu64 "ks/s",
+ (uint64_t)(transfer->actual_length * 1000 /
+ (g_get_monotonic_time() - devc->read_start_ts + 1) /
+ NUM_CHANNELS));
+
+ sr_spew("receive_transfer(): status %s received %d bytes.",
+ libusb_error_name(transfer->status), transfer->actual_length);
+
+ if (transfer->actual_length == 0)
+ /* Nothing to send to the bus. */
+ return;
+
+ if (devc->limit_samples && devc->samp_received >= devc->limit_samples) {
+ sr_info("Requested number of samples reached, stopping. %"
+ PRIu64 " <= %" PRIu64, devc->limit_samples,
+ devc->samp_received);
+ send_data(sdi, devc->sample_buf, devc->limit_samples);
+ sdi->driver->dev_acquisition_stop(sdi);
+ } else if (devc->limit_msec && (g_get_monotonic_time() -
+ devc->aq_started) / 1000 >= devc->limit_msec) {
+ sr_info("Requested time limit reached, stopping. %d <= %d",
+ (uint32_t)devc->limit_msec,
+ (uint32_t)(g_get_monotonic_time() - devc->aq_started) / 1000);
+ send_data(sdi, devc->sample_buf, devc->samp_received);
+ g_free(devc->sample_buf);
+ devc->sample_buf = NULL;
+ sdi->driver->dev_acquisition_stop(sdi);
+ } else {
+ read_channel(sdi, data_amount(sdi));
+ }
+}
+
+static int read_channel(const struct sr_dev_inst *sdi, uint32_t amount)
+{
+ int ret;
+ struct dev_context *devc;
+
+ devc = sdi->priv;
+
+ amount = MIN(amount, MAX_PACKET_SIZE);
+ ret = hantek_6xxx_get_channeldata(sdi, receive_transfer, amount);
+ devc->read_start_ts = g_get_monotonic_time();
+ devc->read_data_amount = amount;
+