2 * This file is part of the libsigrok project.
4 * Copyright (C) 2014 abraxa (Soeren Apel) <soeren@apelpie.net>
5 * Based on the Hameg HMO driver by poljar (Damir Jelić) <poljarinho@gmail.com>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 * <em>Yokogawa DL/DLM series</em> oscilloscope driver
28 static const uint32_t dlm_devopts[] = {
29 SR_CONF_LOGIC_ANALYZER,
31 SR_CONF_LIMIT_FRAMES | SR_CONF_SET,
32 SR_CONF_SAMPLERATE | SR_CONF_GET,
33 SR_CONF_TRIGGER_SLOPE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
34 SR_CONF_TRIGGER_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
35 SR_CONF_TIMEBASE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
36 SR_CONF_NUM_HDIV | SR_CONF_GET,
37 SR_CONF_HORIZ_TRIGGERPOS | SR_CONF_GET | SR_CONF_SET,
40 static const uint32_t dlm_analog_devopts[] = {
41 SR_CONF_VDIV | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
42 SR_CONF_COUPLING | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
43 SR_CONF_NUM_VDIV | SR_CONF_GET,
46 static const char *dlm_coupling_options[] = {
54 /* Note: Values must correlate to the trigger_slopes values */
55 static const char *dlm_trigger_slopes[] = {
61 static const char *dlm_2ch_trigger_sources[] = {
69 /* TODO: Is BITx handled correctly or is Dx required? */
70 static const char *dlm_4ch_trigger_sources[] = {
88 static const uint64_t dlm_timebases[][2] = {
131 static const uint64_t dlm_vdivs[][2] = {
153 static const char *scope_analog_channel_names[] = {
160 static const char *scope_digital_channel_names_8[] = {
171 static const char *scope_digital_channel_names_32[] = {
206 static const struct scope_config scope_models[] = {
208 .model_id = {"710105", "710115", "710125", NULL},
209 .model_name = {"DLM2022", "DLM2032", "DLM2052", NULL},
210 .analog_channels = 2,
211 .digital_channels = 0,
214 .analog_names = &scope_analog_channel_names,
215 .digital_names = &scope_digital_channel_names_8,
217 .devopts = &dlm_devopts,
218 .num_devopts = ARRAY_SIZE(dlm_devopts),
220 .analog_devopts = &dlm_analog_devopts,
221 .num_analog_devopts = ARRAY_SIZE(dlm_analog_devopts),
223 .coupling_options = &dlm_coupling_options,
224 .trigger_sources = &dlm_2ch_trigger_sources,
225 .trigger_slopes = &dlm_trigger_slopes,
227 .timebases = &dlm_timebases,
228 .num_timebases = ARRAY_SIZE(dlm_timebases),
231 .num_vdivs = ARRAY_SIZE(dlm_vdivs),
237 .model_id = {"710110", "710120", "710130", NULL},
238 .model_name = {"DLM2024", "DLM2034", "DLM2054", NULL},
239 .analog_channels = 4,
240 .digital_channels = 8,
243 .analog_names = &scope_analog_channel_names,
244 .digital_names = &scope_digital_channel_names_8,
246 .devopts = &dlm_devopts,
247 .num_devopts = ARRAY_SIZE(dlm_devopts),
249 .analog_devopts = &dlm_analog_devopts,
250 .num_analog_devopts = ARRAY_SIZE(dlm_analog_devopts),
252 .coupling_options = &dlm_coupling_options,
253 .trigger_sources = &dlm_4ch_trigger_sources,
254 .trigger_slopes = &dlm_trigger_slopes,
256 .timebases = &dlm_timebases,
257 .num_timebases = ARRAY_SIZE(dlm_timebases),
260 .num_vdivs = ARRAY_SIZE(dlm_vdivs),
266 .model_id = {"701307", "701308", "701310", "701311",
267 "701312", "701313", NULL},
268 .model_name = {"DL9040", "DL9040L", "DL9140", "DL9140L",
269 "DL9240", "DL9240L", NULL},
270 .analog_channels = 4,
271 .digital_channels = 0,
274 .analog_names = &scope_analog_channel_names,
275 .digital_names = NULL,
277 .devopts = &dlm_devopts,
278 .num_devopts = ARRAY_SIZE(dlm_devopts),
280 .analog_devopts = &dlm_analog_devopts,
281 .num_analog_devopts = ARRAY_SIZE(dlm_analog_devopts),
283 .coupling_options = &dlm_coupling_options,
284 .trigger_sources = &dlm_4ch_trigger_sources,
285 .trigger_slopes = &dlm_trigger_slopes,
287 .timebases = &dlm_timebases,
288 .num_timebases = ARRAY_SIZE(dlm_timebases),
291 .num_vdivs = ARRAY_SIZE(dlm_vdivs),
297 .model_id = {"701320", "701321", NULL},
298 .model_name = {"DL9505L", "DL9510L", NULL},
299 .analog_channels = 4,
300 .digital_channels = 16,
303 .analog_names = &scope_analog_channel_names,
304 .digital_names = &scope_digital_channel_names_32,
306 .devopts = &dlm_devopts,
307 .num_devopts = ARRAY_SIZE(dlm_devopts),
309 .analog_devopts = &dlm_analog_devopts,
310 .num_analog_devopts = ARRAY_SIZE(dlm_analog_devopts),
312 .coupling_options = &dlm_coupling_options,
313 .trigger_sources = &dlm_4ch_trigger_sources,
314 .trigger_slopes = &dlm_trigger_slopes,
316 .timebases = &dlm_timebases,
317 .num_timebases = ARRAY_SIZE(dlm_timebases),
320 .num_vdivs = ARRAY_SIZE(dlm_vdivs),
326 .model_id = {"701330", "701331", NULL},
327 .model_name = {"DL9705L", "DL9710L", NULL},
328 .analog_channels = 4,
329 .digital_channels = 32,
332 .analog_names = &scope_analog_channel_names,
333 .digital_names = &scope_digital_channel_names_32,
335 .devopts = &dlm_devopts,
336 .num_devopts = ARRAY_SIZE(dlm_devopts),
338 .analog_devopts = &dlm_analog_devopts,
339 .num_analog_devopts = ARRAY_SIZE(dlm_analog_devopts),
341 .coupling_options = &dlm_coupling_options,
342 .trigger_sources = &dlm_4ch_trigger_sources,
343 .trigger_slopes = &dlm_trigger_slopes,
345 .timebases = &dlm_timebases,
346 .num_timebases = ARRAY_SIZE(dlm_timebases),
349 .num_vdivs = ARRAY_SIZE(dlm_vdivs),
357 * Prints out the state of the device as we currently know it.
359 * @param config This is the scope configuration.
360 * @param state The current scope state to print.
362 static void scope_state_dump(const struct scope_config *config,
363 struct scope_state *state)
368 for (i = 0; i < config->analog_channels; ++i) {
369 tmp = sr_voltage_string((*config->vdivs)[state->analog_states[i].vdiv][0],
370 (*config->vdivs)[state->analog_states[i].vdiv][1]);
371 sr_info("State of analog channel %d -> %s : %s (coupling) %s (vdiv) %2.2e (offset)",
372 i + 1, state->analog_states[i].state ? "On" : "Off",
373 (*config->coupling_options)[state->analog_states[i].coupling],
374 tmp, state->analog_states[i].vertical_offset);
377 for (i = 0; i < config->digital_channels; ++i) {
378 sr_info("State of digital channel %d -> %s", i,
379 state->digital_states[i] ? "On" : "Off");
382 for (i = 0; i < config->pods; ++i) {
383 sr_info("State of digital POD %d -> %s", i,
384 state->pod_states[i] ? "On" : "Off");
387 tmp = sr_period_string((*config->timebases)[state->timebase][0] *
388 (*config->timebases)[state->timebase][1]);
389 sr_info("Current timebase: %s", tmp);
392 tmp = sr_samplerate_string(state->sample_rate);
393 sr_info("Current samplerate: %s", tmp);
396 sr_info("Current samples per acquisition (i.e. frame): %d",
397 state->samples_per_frame);
399 sr_info("Current trigger: %s (source), %s (slope) %.2f (offset)",
400 (*config->trigger_sources)[state->trigger_source],
401 (*config->trigger_slopes)[state->trigger_slope],
402 state->horiz_triggerpos);
406 * Searches through an array of strings and returns the index to the
407 * array where a given string is located.
409 * @param value The string to search for.
410 * @param array The array of strings.
411 * @param result The index at which value is located in array. -1 on error.
413 * @return SR_ERR when value couldn't be found, SR_OK otherwise.
415 static int array_option_get(char *value, const char *(*array)[],
422 for (i = 0; (*array)[i]; ++i)
423 if (!g_strcmp0(value, (*array)[i])) {
435 * This function takes a value of the form "2.000E-03", converts it to a
436 * significand / factor pair and returns the index of an array where
437 * a matching pair was found.
439 * It's a bit convoluted because of floating-point issues. The value "10.00E-09"
440 * is parsed by g_ascii_strtod() as 0.000000009999999939, for example.
441 * Therefore it's easier to break the number up into two strings and handle
444 * @param value The string to be parsed.
445 * @param array The array of s/f pairs.
446 * @param array_len The number of pairs in the array.
447 * @param result The index at which a matching pair was found.
449 * @return SR_ERR on any parsing error, SR_OK otherwise.
451 static int array_float_get(gchar *value, const uint64_t array[][2],
452 int array_len, int *result)
458 gchar ss[10], es[10];
460 memset(ss, 0, sizeof(ss));
461 memset(es, 0, sizeof(es));
463 strncpy(ss, value, 5);
464 strncpy(es, &(value[6]), 3);
466 if (sr_atof_ascii(ss, &s) != SR_OK)
468 if (sr_atoi(es, &i) != SR_OK)
471 /* Transform e.g. 10^-03 to 1000 as the array stores the inverse. */
474 /* Adjust the significand/factor pair to make sure
475 * that f is a multiple of 1000.
477 while ((int)fmod(log10(f), 3) > 0) { s *= 10; f *= 10; }
479 /* Truncate s to circumvent rounding errors. */
480 s_int = (unsigned int)s;
482 for (i = 0; i < array_len; i++) {
483 if ( (s_int == array[i][0]) && (f == array[i][1]) ) {
493 * Obtains information about all analog channels from the oscilloscope.
494 * The internal state information is updated accordingly.
496 * @param scpi An open SCPI connection.
497 * @param config The device's device configuration.
498 * @param state The device's state information.
500 * @return SR_ERR on error, SR_OK otherwise.
502 static int analog_channel_state_get(struct sr_scpi_dev_inst *scpi,
503 const struct scope_config *config,
504 struct scope_state *state)
509 for (i = 0; i < config->analog_channels; ++i) {
511 if (dlm_analog_chan_state_get(scpi, i + 1,
512 &state->analog_states[i].state) != SR_OK)
515 if (dlm_analog_chan_vdiv_get(scpi, i + 1, &response) != SR_OK)
518 if (array_float_get(response, *config->vdivs, config->num_vdivs,
525 state->analog_states[i].vdiv = j;
527 if (dlm_analog_chan_voffs_get(scpi, i + 1,
528 &state->analog_states[i].vertical_offset) != SR_OK)
531 if (dlm_analog_chan_wrange_get(scpi, i + 1,
532 &state->analog_states[i].waveform_range) != SR_OK)
535 if (dlm_analog_chan_woffs_get(scpi, i + 1,
536 &state->analog_states[i].waveform_offset) != SR_OK)
539 if (dlm_analog_chan_coupl_get(scpi, i + 1, &response) != SR_OK) {
544 if (array_option_get(response, config->coupling_options,
545 &state->analog_states[i].coupling) != SR_OK) {
556 * Obtains information about all digital channels from the oscilloscope.
557 * The internal state information is updated accordingly.
559 * @param scpi An open SCPI connection.
560 * @param config The device's device configuration.
561 * @param state The device's state information.
563 * @return SR_ERR on error, SR_OK otherwise.
565 static int digital_channel_state_get(struct sr_scpi_dev_inst *scpi,
566 const struct scope_config *config,
567 struct scope_state *state)
571 if (!config->digital_channels)
573 sr_warn("Tried obtaining digital channel states on a " \
574 "model without digital inputs.");
578 for (i = 0; i < config->digital_channels; ++i) {
579 if (dlm_digital_chan_state_get(scpi, i + 1,
580 &state->digital_states[i]) != SR_OK) {
587 sr_warn("Tried obtaining pod states on a model without pods.");
591 for (i = 0; i < config->pods; ++i) {
592 if (dlm_digital_pod_state_get(scpi, i + 'A',
593 &state->pod_states[i]) != SR_OK)
601 * Obtains information about the sample rate from the oscilloscope.
602 * The internal state information is updated accordingly.
604 * @param sdi The device instance.
606 * @return SR_ERR on error, SR_OK otherwise.
608 SR_PRIV int dlm_sample_rate_query(const struct sr_dev_inst *sdi)
610 struct dev_context *devc;
611 struct scope_state *state;
615 state = devc->model_state;
617 /* No need to find an active channel to query the sample rate:
618 * querying any channel will do, so we use channel 1 all the time.
620 if (dlm_analog_chan_srate_get(sdi->conn, 1, &tmp_float) != SR_OK)
623 state->sample_rate = tmp_float;
629 * Obtains information about the current device state from the oscilloscope,
630 * including all analog and digital channel configurations.
631 * The internal state information is updated accordingly.
633 * @param sdi The device instance.
635 * @return SR_ERR on error, SR_OK otherwise.
637 SR_PRIV int dlm_scope_state_query(struct sr_dev_inst *sdi)
639 struct dev_context *devc;
640 struct scope_state *state;
641 const struct scope_config *config;
647 config = devc->model_config;
648 state = devc->model_state;
650 if (analog_channel_state_get(sdi->conn, config, state) != SR_OK)
653 if (digital_channel_state_get(sdi->conn, config, state) != SR_OK)
656 if (dlm_timebase_get(sdi->conn, &response) != SR_OK)
659 if (array_float_get(response, *config->timebases,
660 config->num_timebases, &i) != SR_OK) {
668 if (dlm_horiz_trigger_pos_get(sdi->conn, &tmp_float) != SR_OK)
671 /* TODO: Check if the calculation makes sense for the DLM. */
672 state->horiz_triggerpos = tmp_float /
673 (((double)(*config->timebases)[state->timebase][0] /
674 (*config->timebases)[state->timebase][1]) * config->num_xdivs);
675 state->horiz_triggerpos -= 0.5;
676 state->horiz_triggerpos *= -1;
678 if (dlm_trigger_source_get(sdi->conn, &response) != SR_OK) {
683 if (array_option_get(response, config->trigger_sources,
684 &state->trigger_source) != SR_OK) {
691 if (dlm_trigger_slope_get(sdi->conn, &i) != SR_OK)
694 state->trigger_slope = i;
696 if (dlm_acq_length_get(sdi->conn, &state->samples_per_frame) != SR_OK) {
697 sr_err("Failed to query acquisition length.");
701 dlm_sample_rate_query(sdi);
703 scope_state_dump(config, state);
709 * Creates a new device state structure.
711 * @param config The device configuration to use.
713 * @return The newly allocated scope_state struct.
715 static struct scope_state *dlm_scope_state_new(const struct scope_config *config)
717 struct scope_state *state;
719 state = g_malloc0(sizeof(struct scope_state));
721 state->analog_states = g_malloc0(config->analog_channels *
722 sizeof(struct analog_channel_state));
724 state->digital_states = g_malloc0(config->digital_channels *
727 state->pod_states = g_malloc0(config->pods * sizeof(gboolean));
733 * Frees the memory that was allocated by a call to dlm_scope_state_new().
735 * @param state The device state structure whose memory is to be freed.
737 SR_PRIV void dlm_scope_state_destroy(struct scope_state *state)
739 g_free(state->analog_states);
740 g_free(state->digital_states);
741 g_free(state->pod_states);
745 SR_PRIV int dlm_model_get(char *model_id, char **model_name, int *model_index)
752 for (i = 0; i < ARRAY_SIZE(scope_models); i++) {
753 for (j = 0; scope_models[i].model_id[j]; j++) {
754 if (!strcmp(model_id, scope_models[i].model_id[j])) {
756 *model_name = (char *)scope_models[i].model_name[j];
760 if (*model_index != -1)
764 if (*model_index == -1) {
765 sr_err("Found unsupported DLM device with model identifier %s.",
774 * Attempts to initialize a DL/DLM device and prepares internal structures
775 * if a suitable device was found.
777 * @param sdi The device instance.
779 SR_PRIV int dlm_device_init(struct sr_dev_inst *sdi, int model_index)
783 struct sr_channel *ch;
784 struct dev_context *devc;
788 devc->analog_groups = g_malloc0(sizeof(struct sr_channel_group*) *
789 scope_models[model_index].analog_channels);
791 devc->digital_groups = g_malloc0(sizeof(struct sr_channel_group*) *
792 scope_models[model_index].pods);
794 /* Add analog channels, each in its own group. */
795 for (i = 0; i < scope_models[model_index].analog_channels; i++) {
796 ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE,
797 (*scope_models[model_index].analog_names)[i]);
799 devc->analog_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
801 devc->analog_groups[i]->name = g_strdup(
802 (char *)(*scope_models[model_index].analog_names)[i]);
803 devc->analog_groups[i]->channels = g_slist_append(NULL, ch);
805 sdi->channel_groups = g_slist_append(sdi->channel_groups,
806 devc->analog_groups[i]);
809 /* Add digital channel groups. */
810 for (i = 0; i < scope_models[model_index].pods; ++i) {
811 g_snprintf(tmp, sizeof(tmp), "POD%d", i);
813 devc->digital_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
814 if (!devc->digital_groups[i])
815 return SR_ERR_MALLOC;
817 devc->digital_groups[i]->name = g_strdup(tmp);
818 sdi->channel_groups = g_slist_append(sdi->channel_groups,
819 devc->digital_groups[i]);
822 /* Add digital channels. */
823 for (i = 0; i < scope_models[model_index].digital_channels; i++) {
824 ch = sr_channel_new(sdi, DLM_DIG_CHAN_INDEX_OFFS + i,
825 SR_CHANNEL_LOGIC, TRUE,
826 (*scope_models[model_index].digital_names)[i]);
828 devc->digital_groups[i / 8]->channels = g_slist_append(
829 devc->digital_groups[i / 8]->channels, ch);
831 devc->model_config = &scope_models[model_index];
832 devc->frame_limit = 0;
834 if (!(devc->model_state = dlm_scope_state_new(devc->model_config)))
835 return SR_ERR_MALLOC;
837 /* Disable non-standard response behavior. */
838 if (dlm_response_headers_set(sdi->conn, FALSE) != SR_OK)
844 SR_PRIV int dlm_channel_data_request(const struct sr_dev_inst *sdi)
846 struct dev_context *devc;
847 struct sr_channel *ch;
851 ch = devc->current_channel->data;
854 case SR_CHANNEL_ANALOG:
855 result = dlm_analog_data_get(sdi->conn, ch->index + 1);
857 case SR_CHANNEL_LOGIC:
858 result = dlm_digital_data_get(sdi->conn);
861 sr_err("Invalid channel type encountered (%d).",
867 devc->data_pending = TRUE;
869 devc->data_pending = FALSE;
875 * Reads and removes the block data header from a given data input.
876 * Format is #ndddd... with n being the number of decimal digits d.
877 * The string dddd... contains the decimal-encoded length of the data.
878 * Example: #9000000013 would yield a length of 13 bytes.
880 * @param data The input data.
881 * @param len The determined input data length.
883 static int dlm_block_data_header_process(GArray *data, int *len)
888 if (g_array_index(data, gchar, 0) != '#')
891 n = (uint8_t)(g_array_index(data, gchar, 1) - '0');
893 for (i = 0; i < n; i++)
894 s[i] = g_array_index(data, gchar, 2 + i);
897 if (sr_atoi(s, len) != SR_OK)
900 g_array_remove_range(data, 0, 2 + n);
906 * Turns raw sample data into voltages and sends them off to the session bus.
908 * @param data The raw sample data.
909 * @ch_state Pointer to the state of the channel whose data we're processing.
910 * @sdi The device instance.
912 * @return SR_ERR when data is trucated, SR_OK otherwise.
914 static int dlm_analog_samples_send(GArray *data,
915 struct analog_channel_state *ch_state,
916 struct sr_dev_inst *sdi)
919 float voltage, range, offset;
921 struct dev_context *devc;
922 struct scope_state *model_state;
923 struct sr_channel *ch;
924 struct sr_datafeed_analog analog;
925 struct sr_datafeed_packet packet;
928 model_state = devc->model_state;
929 samples = model_state->samples_per_frame;
930 ch = devc->current_channel->data;
932 if (data->len < samples * sizeof(uint8_t)) {
933 sr_err("Truncated waveform data packet received.");
937 range = ch_state->waveform_range;
938 offset = ch_state->waveform_offset;
940 /* Convert byte sample to voltage according to
941 * page 269 of the Communication Interface User's Manual.
943 float_data = g_array_new(FALSE, FALSE, sizeof(float));
944 for (i = 0; i < samples; i++) {
945 voltage = (float)g_array_index(data, int8_t, i);
946 voltage = (range * voltage /
947 DLM_DIVISION_FOR_BYTE_FORMAT) + offset;
948 g_array_append_val(float_data, voltage);
951 analog.channels = g_slist_append(NULL, ch);
952 analog.num_samples = float_data->len;
953 analog.data = (float*)float_data->data;
954 analog.mq = SR_MQ_VOLTAGE;
955 analog.unit = SR_UNIT_VOLT;
957 packet.type = SR_DF_ANALOG;
958 packet.payload = &analog;
959 sr_session_send(sdi, &packet);
960 g_slist_free(analog.channels);
962 g_array_free(float_data, TRUE);
963 g_array_remove_range(data, 0, samples * sizeof(uint8_t));
969 * Sends logic sample data off to the session bus.
971 * @param data The raw sample data.
972 * @ch_state Pointer to the state of the channel whose data we're processing.
973 * @sdi The device instance.
975 * @return SR_ERR when data is trucated, SR_OK otherwise.
977 static int dlm_digital_samples_send(GArray *data,
978 struct sr_dev_inst *sdi)
980 struct dev_context *devc;
981 struct scope_state *model_state;
983 struct sr_datafeed_logic logic;
984 struct sr_datafeed_packet packet;
987 model_state = devc->model_state;
988 samples = model_state->samples_per_frame;
990 if (data->len < samples * sizeof(uint8_t)) {
991 sr_err("Truncated waveform data packet received.");
995 logic.length = samples;
997 logic.data = data->data;
998 packet.type = SR_DF_LOGIC;
999 packet.payload = &logic;
1000 sr_session_send(sdi, &packet);
1002 g_array_remove_range(data, 0, samples * sizeof(uint8_t));
1008 * Attempts to query sample data from the oscilloscope in order to send it
1009 * to the session bus for further processing.
1011 * @param fd The file descriptor used as the event source.
1012 * @param revents The received events.
1013 * @param cb_data Callback data, in this case our device instance.
1015 * @return TRUE in case of success or a recoverable error,
1016 * FALSE when a fatal error was encountered.
1018 SR_PRIV int dlm_data_receive(int fd, int revents, void *cb_data)
1020 struct sr_dev_inst *sdi;
1021 struct scope_state *model_state;
1022 struct dev_context *devc;
1023 struct sr_channel *ch;
1024 struct sr_datafeed_packet packet;
1025 int chunk_len, num_bytes;
1026 static GArray *data = NULL;
1031 if (!(sdi = cb_data))
1034 if (!(devc = sdi->priv))
1037 if (!(model_state = (struct scope_state*)devc->model_state))
1040 /* Are we waiting for a response from the device? */
1041 if (!devc->data_pending)
1044 /* Check if a new query response is coming our way. */
1046 if (sr_scpi_read_begin(sdi->conn) == SR_OK)
1047 /* The 16 here accounts for the header and EOL. */
1048 data = g_array_sized_new(FALSE, FALSE, sizeof(uint8_t),
1049 16 + model_state->samples_per_frame);
1054 /* Store incoming data. */
1055 chunk_len = sr_scpi_read_data(sdi->conn, devc->receive_buffer,
1056 RECEIVE_BUFFER_SIZE);
1057 if (chunk_len < 0) {
1058 sr_err("Error while reading data: %d", chunk_len);
1061 g_array_append_vals(data, devc->receive_buffer, chunk_len);
1063 /* Read the entire query response before processing. */
1064 if (!sr_scpi_read_complete(sdi->conn))
1067 /* We finished reading and are no longer waiting for data. */
1068 devc->data_pending = FALSE;
1070 /* Signal the beginning of a new frame if this is the first channel. */
1071 if (devc->current_channel == devc->enabled_channels) {
1072 packet.type = SR_DF_FRAME_BEGIN;
1073 sr_session_send(sdi, &packet);
1076 if (dlm_block_data_header_process(data, &num_bytes) != SR_OK) {
1077 sr_err("Encountered malformed block data header.");
1081 if (num_bytes == 0) {
1082 sr_warn("Zero-length waveform data packet received. " \
1083 "Live mode not supported yet, stopping " \
1084 "acquisition and retrying.");
1085 /* Don't care about return value here. */
1086 dlm_acquisition_stop(sdi->conn);
1087 g_array_free(data, TRUE);
1088 dlm_channel_data_request(sdi);
1092 ch = devc->current_channel->data;
1094 case SR_CHANNEL_ANALOG:
1095 if (dlm_analog_samples_send(data,
1096 &model_state->analog_states[ch->index],
1100 case SR_CHANNEL_LOGIC:
1101 if (dlm_digital_samples_send(data, sdi) != SR_OK)
1105 sr_err("Invalid channel type encountered.");
1109 g_array_free(data, TRUE);
1112 /* Signal the end of this frame if this was the last enabled channel
1113 * and set the next enabled channel. Then, request its data.
1115 if (!devc->current_channel->next) {
1116 packet.type = SR_DF_FRAME_END;
1117 sr_session_send(sdi, &packet);
1118 devc->current_channel = devc->enabled_channels;
1120 /* As of now we only support importing the current acquisition
1121 * data so we're going to stop at this point.
1123 sdi->driver->dev_acquisition_stop(sdi, cb_data);
1126 devc->current_channel = devc->current_channel->next;
1128 if (dlm_channel_data_request(sdi) != SR_OK) {
1129 sr_err("Failed to request acquisition data.");
1137 g_array_free(data, TRUE);