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/>.
25 static const char *coupling_options[] = {
26 "AC", "DC", "DC50", "GND",
29 static const char *trigger_sources_2ch[] = {
30 "1", "2", "LINE", "EXT",
33 /* TODO: Is BITx handled correctly or is Dx required? */
34 static const char *trigger_sources_4ch[] = {
36 "LINE", "EXT", "BIT1",
37 "BIT2", "BIT3", "BIT4", "BIT5", "BIT6", "BIT7", "BIT8",
40 /* Note: Values must correlate to the trigger_slopes values. */
41 const char *dlm_trigger_slopes[2] = {
45 const uint64_t dlm_timebases[36][2] = {
88 const uint64_t dlm_vdivs[17][2] = {
110 static const char *scope_analog_channel_names[] = {
114 static const char *scope_digital_channel_names_8[] = {
115 "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
118 static const char *scope_digital_channel_names_32[] = {
119 "A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7",
120 "B0", "B1", "B2", "B3", "B4", "B5", "B6", "B7",
121 "C0", "C1", "C2", "C3", "C4", "C5", "C6", "C7",
122 "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
125 static const struct scope_config scope_models[] = {
127 .model_id = {"710105", "710115", "710125", NULL},
128 .model_name = {"DLM2022", "DLM2032", "DLM2052", NULL},
129 .analog_channels = 2,
130 .digital_channels = 0,
133 .analog_names = &scope_analog_channel_names,
134 .digital_names = &scope_digital_channel_names_8,
136 .coupling_options = &coupling_options,
137 .num_coupling_options = ARRAY_SIZE(coupling_options),
139 .trigger_sources = &trigger_sources_2ch,
140 .num_trigger_sources = ARRAY_SIZE(trigger_sources_2ch),
146 .model_id = {"710110", "710120", "710130", NULL},
147 .model_name = {"DLM2024", "DLM2034", "DLM2054", NULL},
148 .analog_channels = 4,
149 .digital_channels = 8,
152 .analog_names = &scope_analog_channel_names,
153 .digital_names = &scope_digital_channel_names_8,
155 .coupling_options = &coupling_options,
156 .num_coupling_options = ARRAY_SIZE(coupling_options),
158 .trigger_sources = &trigger_sources_4ch,
159 .num_trigger_sources = ARRAY_SIZE(trigger_sources_4ch),
165 .model_id = {"701307", "701308", "701310", "701311",
166 "701312", "701313", NULL},
167 .model_name = {"DL9040", "DL9040L", "DL9140", "DL9140L",
168 "DL9240", "DL9240L", NULL},
169 .analog_channels = 4,
170 .digital_channels = 0,
173 .analog_names = &scope_analog_channel_names,
174 .digital_names = NULL,
176 .coupling_options = &coupling_options,
177 .num_coupling_options = ARRAY_SIZE(coupling_options),
179 .trigger_sources = &trigger_sources_4ch,
180 .num_trigger_sources = ARRAY_SIZE(trigger_sources_4ch),
186 .model_id = {"701320", "701321", NULL},
187 .model_name = {"DL9505L", "DL9510L", NULL},
188 .analog_channels = 4,
189 .digital_channels = 16,
192 .analog_names = &scope_analog_channel_names,
193 .digital_names = &scope_digital_channel_names_32,
195 .coupling_options = &coupling_options,
196 .num_coupling_options = ARRAY_SIZE(coupling_options),
198 .trigger_sources = &trigger_sources_4ch,
199 .num_trigger_sources = ARRAY_SIZE(trigger_sources_4ch),
205 .model_id = {"701330", "701331", NULL},
206 .model_name = {"DL9705L", "DL9710L", NULL},
207 .analog_channels = 4,
208 .digital_channels = 32,
211 .analog_names = &scope_analog_channel_names,
212 .digital_names = &scope_digital_channel_names_32,
214 .coupling_options = &coupling_options,
215 .num_coupling_options = ARRAY_SIZE(coupling_options),
217 .trigger_sources = &trigger_sources_4ch,
218 .num_trigger_sources = ARRAY_SIZE(trigger_sources_4ch),
226 * Prints out the state of the device as we currently know it.
228 * @param config This is the scope configuration.
229 * @param state The current scope state to print.
231 static void scope_state_dump(const struct scope_config *config,
232 struct scope_state *state)
237 for (i = 0; i < config->analog_channels; i++) {
238 tmp = sr_voltage_string(dlm_vdivs[state->analog_states[i].vdiv][0],
239 dlm_vdivs[state->analog_states[i].vdiv][1]);
240 sr_info("State of analog channel %d -> %s : %s (coupling) %s (vdiv) %2.2e (offset)",
241 i + 1, state->analog_states[i].state ? "On" : "Off",
242 (*config->coupling_options)[state->analog_states[i].coupling],
243 tmp, state->analog_states[i].vertical_offset);
246 for (i = 0; i < config->digital_channels; i++) {
247 sr_info("State of digital channel %d -> %s", i,
248 state->digital_states[i] ? "On" : "Off");
251 for (i = 0; i < config->pods; i++) {
252 sr_info("State of digital POD %d -> %s", i,
253 state->pod_states[i] ? "On" : "Off");
256 tmp = sr_period_string(dlm_timebases[state->timebase][0],
257 dlm_timebases[state->timebase][1]);
258 sr_info("Current timebase: %s", tmp);
261 tmp = sr_samplerate_string(state->sample_rate);
262 sr_info("Current samplerate: %s", tmp);
265 sr_info("Current samples per acquisition (i.e. frame): %d",
266 state->samples_per_frame);
268 sr_info("Current trigger: %s (source), %s (slope) %.2f (offset)",
269 (*config->trigger_sources)[state->trigger_source],
270 dlm_trigger_slopes[state->trigger_slope],
271 state->horiz_triggerpos);
275 * Searches through an array of strings and returns the index to the
276 * array where a given string is located.
278 * @param value The string to search for.
279 * @param array The array of strings.
280 * @param result The index at which value is located in array. -1 on error.
282 * @return SR_ERR when value couldn't be found, SR_OK otherwise.
284 static int array_option_get(char *value, const char *(*array)[],
285 unsigned int n, int *result)
291 for (i = 0; i < n; i++)
292 if (!g_strcmp0(value, (*array)[i])) {
304 * This function takes a value of the form "2.000E-03", converts it to a
305 * significand / factor pair and returns the index of an array where
306 * a matching pair was found.
308 * It's a bit convoluted because of floating-point issues. The value "10.00E-09"
309 * is parsed by g_ascii_strtod() as 0.000000009999999939, for example.
310 * Therefore it's easier to break the number up into two strings and handle
313 * @param value The string to be parsed.
314 * @param array The array of s/f pairs.
315 * @param array_len The number of pairs in the array.
316 * @param result The index at which a matching pair was found.
318 * @return SR_ERR on any parsing error, SR_OK otherwise.
320 static int array_float_get(gchar *value, const uint64_t array[][2],
321 int array_len, int *result)
328 gchar ss[10], es[10];
330 memset(ss, 0, sizeof(ss));
331 memset(es, 0, sizeof(es));
333 /* Get index of the separating 'E' character and break up the string. */
334 pos = strcspn(value, "E");
336 strncpy(ss, value, pos);
337 strncpy(es, &(value[pos+1]), 3);
339 if (sr_atof_ascii(ss, &s) != SR_OK)
341 if (sr_atoi(es, &e) != SR_OK)
344 /* Transform e.g. 10^-03 to 1000 as the array stores the inverse. */
348 * Adjust the significand/factor pair to make sure
349 * that f is a multiple of 1000.
351 while ((int)fmod(log10(f), 3) > 0) {
360 /* Truncate s to circumvent rounding errors. */
361 s_int = (unsigned int)s;
363 for (i = 0; i < array_len; i++) {
364 if ((s_int == array[i][0]) && (f == array[i][1])) {
374 * Obtains information about all analog channels from the oscilloscope.
375 * The internal state information is updated accordingly.
377 * @param sdi The device instance.
378 * @param config The device's device configuration.
379 * @param state The device's state information.
381 * @return SR_ERR on error, SR_OK otherwise.
383 static int analog_channel_state_get(const struct sr_dev_inst *sdi,
384 const struct scope_config *config,
385 struct scope_state *state)
387 struct sr_scpi_dev_inst *scpi;
390 struct sr_channel *ch;
395 for (i = 0; i < config->analog_channels; i++) {
397 if (dlm_analog_chan_state_get(scpi, i + 1,
398 &state->analog_states[i].state) != SR_OK)
401 for (l = sdi->channels; l; l = l->next) {
403 if (ch->index == i) {
404 ch->enabled = state->analog_states[i].state;
409 if (dlm_analog_chan_vdiv_get(scpi, i + 1, &response) != SR_OK)
412 if (array_float_get(response, ARRAY_AND_SIZE(dlm_vdivs),
419 state->analog_states[i].vdiv = j;
421 if (dlm_analog_chan_voffs_get(scpi, i + 1,
422 &state->analog_states[i].vertical_offset) != SR_OK)
425 if (dlm_analog_chan_wrange_get(scpi, i + 1,
426 &state->analog_states[i].waveform_range) != SR_OK)
429 if (dlm_analog_chan_woffs_get(scpi, i + 1,
430 &state->analog_states[i].waveform_offset) != SR_OK)
433 if (dlm_analog_chan_coupl_get(scpi, i + 1, &response) != SR_OK) {
438 if (array_option_get(response, config->coupling_options,
439 config->num_coupling_options,
440 &state->analog_states[i].coupling) != SR_OK) {
451 * Obtains information about all digital channels from the oscilloscope.
452 * The internal state information is updated accordingly.
454 * @param sdi The device instance.
455 * @param config The device's device configuration.
456 * @param state The device's state information.
458 * @return SR_ERR on error, SR_OK otherwise.
460 static int digital_channel_state_get(const struct sr_dev_inst *sdi,
461 const struct scope_config *config,
462 struct scope_state *state)
464 struct sr_scpi_dev_inst *scpi;
467 struct sr_channel *ch;
471 if (!config->digital_channels) {
472 sr_warn("Tried obtaining digital channel states on a " \
473 "model without digital inputs.");
477 for (i = 0; i < config->digital_channels; i++) {
478 if (dlm_digital_chan_state_get(scpi, i + 1,
479 &state->digital_states[i]) != SR_OK) {
483 for (l = sdi->channels; l; l = l->next) {
485 if (ch->index == i + DLM_DIG_CHAN_INDEX_OFFS) {
486 ch->enabled = state->digital_states[i];
493 sr_warn("Tried obtaining pod states on a model without pods.");
497 for (i = 0; i < config->pods; i++) {
498 if (dlm_digital_pod_state_get(scpi, i + 'A',
499 &state->pod_states[i]) != SR_OK)
506 SR_PRIV int dlm_channel_state_set(const struct sr_dev_inst *sdi,
507 const int ch_index, gboolean ch_state)
510 struct sr_channel *ch;
511 struct dev_context *devc = NULL;
512 struct scope_state *state;
513 const struct scope_config *model = NULL;
514 struct sr_scpi_dev_inst *scpi;
516 gboolean *pod_enabled;
523 state = devc->model_state;
524 model = devc->model_config;
527 pod_enabled = g_malloc0(sizeof(gboolean) * model->pods);
529 for (l = sdi->channels; l; l = l->next) {
533 case SR_CHANNEL_ANALOG:
534 if (ch->index == ch_index) {
535 if (dlm_analog_chan_state_set(scpi, ch->index + 1, ch_state) != SR_OK) {
540 ch->enabled = ch_state;
541 state->analog_states[ch->index].state = ch_state;
546 case SR_CHANNEL_LOGIC:
547 i = ch->index - DLM_DIG_CHAN_INDEX_OFFS;
549 if (ch->index == ch_index) {
550 if (dlm_digital_chan_state_set(scpi, i + 1, ch_state) != SR_OK) {
555 ch->enabled = ch_state;
556 state->digital_states[i] = ch_state;
559 /* The corresponding pod has to be enabled also. */
560 pod_enabled[i / 8] |= ch->enabled;
562 /* Also check all other channels. Maybe we can disable a pod. */
563 pod_enabled[i / 8] |= ch->enabled;
571 for (i = 0; i < model->pods; i++) {
572 if (state->pod_states[i] == pod_enabled[i])
575 if (dlm_digital_pod_state_set(scpi, i + 1, pod_enabled[i]) != SR_OK) {
580 state->pod_states[i] = pod_enabled[i];
585 if ((result == SR_OK) && !chan_found)
592 * Obtains information about the sample rate from the oscilloscope.
593 * The internal state information is updated accordingly.
595 * @param sdi The device instance.
597 * @return SR_ERR on error, SR_OK otherwise.
599 SR_PRIV int dlm_sample_rate_query(const struct sr_dev_inst *sdi)
601 struct dev_context *devc;
602 struct scope_state *state;
606 state = devc->model_state;
609 * No need to find an active channel to query the sample rate:
610 * querying any channel will do, so we use channel 1 all the time.
612 if (dlm_analog_chan_srate_get(sdi->conn, 1, &tmp_float) != SR_OK)
615 state->sample_rate = tmp_float;
621 * Obtains information about the current device state from the oscilloscope,
622 * including all analog and digital channel configurations.
623 * The internal state information is updated accordingly.
625 * @param sdi The device instance.
627 * @return SR_ERR on error, SR_OK otherwise.
629 SR_PRIV int dlm_scope_state_query(struct sr_dev_inst *sdi)
631 struct dev_context *devc;
632 struct scope_state *state;
633 const struct scope_config *config;
639 config = devc->model_config;
640 state = devc->model_state;
642 if (analog_channel_state_get(sdi, config, state) != SR_OK)
645 if (digital_channel_state_get(sdi, config, state) != SR_OK)
648 if (dlm_timebase_get(sdi->conn, &response) != SR_OK)
651 if (array_float_get(response, ARRAY_AND_SIZE(dlm_timebases), &i) != SR_OK) {
659 if (dlm_horiz_trigger_pos_get(sdi->conn, &tmp_float) != SR_OK)
662 /* TODO: Check if the calculation makes sense for the DLM. */
663 state->horiz_triggerpos = tmp_float /
664 (((double)dlm_timebases[state->timebase][0] /
665 dlm_timebases[state->timebase][1]) * config->num_xdivs);
666 state->horiz_triggerpos -= 0.5;
667 state->horiz_triggerpos *= -1;
669 if (dlm_trigger_source_get(sdi->conn, &response) != SR_OK) {
674 if (array_option_get(response, config->trigger_sources,
675 config->num_trigger_sources, &state->trigger_source) != SR_OK) {
682 if (dlm_trigger_slope_get(sdi->conn, &i) != SR_OK)
685 state->trigger_slope = i;
687 if (dlm_acq_length_get(sdi->conn, &state->samples_per_frame) != SR_OK) {
688 sr_err("Failed to query acquisition length.");
692 dlm_sample_rate_query(sdi);
694 scope_state_dump(config, state);
700 * Creates a new device state structure.
702 * @param config The device configuration to use.
704 * @return The newly allocated scope_state struct.
706 static struct scope_state *dlm_scope_state_new(const struct scope_config *config)
708 struct scope_state *state;
710 state = g_malloc0(sizeof(struct scope_state));
712 state->analog_states = g_malloc0(config->analog_channels *
713 sizeof(struct analog_channel_state));
715 state->digital_states = g_malloc0(config->digital_channels *
718 state->pod_states = g_malloc0(config->pods * sizeof(gboolean));
724 * Frees the memory that was allocated by a call to dlm_scope_state_new().
726 * @param state The device state structure whose memory is to be freed.
728 SR_PRIV void dlm_scope_state_destroy(struct scope_state *state)
730 g_free(state->analog_states);
731 g_free(state->digital_states);
732 g_free(state->pod_states);
736 SR_PRIV int dlm_model_get(char *model_id, char **model_name, int *model_index)
743 for (i = 0; i < ARRAY_SIZE(scope_models); i++) {
744 for (j = 0; scope_models[i].model_id[j]; j++) {
745 if (!strcmp(model_id, scope_models[i].model_id[j])) {
747 *model_name = (char *)scope_models[i].model_name[j];
751 if (*model_index != -1)
755 if (*model_index == -1) {
756 sr_err("Found unsupported DLM device with model identifier %s.",
765 * Attempts to initialize a DL/DLM device and prepares internal structures
766 * if a suitable device was found.
768 * @param sdi The device instance.
770 SR_PRIV int dlm_device_init(struct sr_dev_inst *sdi, int model_index)
773 struct sr_channel *ch;
774 struct dev_context *devc;
778 devc->analog_groups = g_malloc0(sizeof(struct sr_channel_group*) *
779 scope_models[model_index].analog_channels);
781 devc->digital_groups = g_malloc0(sizeof(struct sr_channel_group*) *
782 scope_models[model_index].pods);
784 /* Add analog channels, each in its own group. */
785 for (i = 0; i < scope_models[model_index].analog_channels; i++) {
786 ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE,
787 (*scope_models[model_index].analog_names)[i]);
789 devc->analog_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
791 devc->analog_groups[i]->name = g_strdup(
792 (char *)(*scope_models[model_index].analog_names)[i]);
793 devc->analog_groups[i]->channels = g_slist_append(NULL, ch);
795 sdi->channel_groups = g_slist_append(sdi->channel_groups,
796 devc->analog_groups[i]);
799 /* Add digital channel groups. */
800 for (i = 0; i < scope_models[model_index].pods; i++) {
801 devc->digital_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
802 if (!devc->digital_groups[i])
803 return SR_ERR_MALLOC;
804 devc->digital_groups[i]->name = g_strdup_printf("POD%d", i);
805 sdi->channel_groups = g_slist_append(sdi->channel_groups,
806 devc->digital_groups[i]);
809 /* Add digital channels. */
810 for (i = 0; i < scope_models[model_index].digital_channels; i++) {
811 ch = sr_channel_new(sdi, DLM_DIG_CHAN_INDEX_OFFS + i,
812 SR_CHANNEL_LOGIC, TRUE,
813 (*scope_models[model_index].digital_names)[i]);
815 devc->digital_groups[i / 8]->channels = g_slist_append(
816 devc->digital_groups[i / 8]->channels, ch);
818 devc->model_config = &scope_models[model_index];
819 devc->frame_limit = 0;
821 if (!(devc->model_state = dlm_scope_state_new(devc->model_config)))
822 return SR_ERR_MALLOC;
824 /* Disable non-standard response behavior. */
825 if (dlm_response_headers_set(sdi->conn, FALSE) != SR_OK)
831 SR_PRIV int dlm_channel_data_request(const struct sr_dev_inst *sdi)
833 struct dev_context *devc;
834 struct sr_channel *ch;
838 ch = devc->current_channel->data;
841 case SR_CHANNEL_ANALOG:
842 result = dlm_analog_data_get(sdi->conn, ch->index + 1);
844 case SR_CHANNEL_LOGIC:
845 result = dlm_digital_data_get(sdi->conn);
848 sr_err("Invalid channel type encountered (%d).",
854 devc->data_pending = TRUE;
856 devc->data_pending = FALSE;
862 * Reads and removes the block data header from a given data input.
863 * Format is #ndddd... with n being the number of decimal digits d.
864 * The string dddd... contains the decimal-encoded length of the data.
865 * Example: #9000000013 would yield a length of 13 bytes.
867 * @param data The input data.
868 * @param len The determined input data length.
870 static int dlm_block_data_header_process(GArray *data, int *len)
875 if (g_array_index(data, gchar, 0) != '#')
878 n = (uint8_t)(g_array_index(data, gchar, 1) - '0');
880 for (i = 0; i < n; i++)
881 s[i] = g_array_index(data, gchar, 2 + i);
884 if (sr_atoi(s, len) != SR_OK)
887 g_array_remove_range(data, 0, 2 + n);
893 * Turns raw sample data into voltages and sends them off to the session bus.
895 * @param data The raw sample data.
896 * @ch_state Pointer to the state of the channel whose data we're processing.
897 * @sdi The device instance.
899 * @return SR_ERR when data is trucated, SR_OK otherwise.
901 static int dlm_analog_samples_send(GArray *data,
902 struct analog_channel_state *ch_state,
903 struct sr_dev_inst *sdi)
906 float voltage, range, offset;
908 struct dev_context *devc;
909 struct scope_state *model_state;
910 struct sr_channel *ch;
911 struct sr_datafeed_analog analog;
912 struct sr_analog_encoding encoding;
913 struct sr_analog_meaning meaning;
914 struct sr_analog_spec spec;
915 struct sr_datafeed_packet packet;
918 model_state = devc->model_state;
919 samples = model_state->samples_per_frame;
920 ch = devc->current_channel->data;
922 if (data->len < samples * sizeof(uint8_t)) {
923 sr_err("Truncated waveform data packet received.");
927 range = ch_state->waveform_range;
928 offset = ch_state->waveform_offset;
931 * Convert byte sample to voltage according to
932 * page 269 of the Communication Interface User's Manual.
934 float_data = g_array_new(FALSE, FALSE, sizeof(float));
935 for (i = 0; i < samples; i++) {
936 voltage = (float)g_array_index(data, int8_t, i);
937 voltage = (range * voltage /
938 DLM_DIVISION_FOR_BYTE_FORMAT) + offset;
939 g_array_append_val(float_data, voltage);
942 /* TODO: Use proper 'digits' value for this device (and its modes). */
943 sr_analog_init(&analog, &encoding, &meaning, &spec, 2);
944 analog.meaning->channels = g_slist_append(NULL, ch);
945 analog.num_samples = float_data->len;
946 analog.data = (float*)float_data->data;
947 analog.meaning->mq = SR_MQ_VOLTAGE;
948 analog.meaning->unit = SR_UNIT_VOLT;
949 analog.meaning->mqflags = 0;
950 packet.type = SR_DF_ANALOG;
951 packet.payload = &analog;
952 sr_session_send(sdi, &packet);
953 g_slist_free(analog.meaning->channels);
955 g_array_free(float_data, TRUE);
956 g_array_remove_range(data, 0, samples * sizeof(uint8_t));
962 * Sends logic sample data off to the session bus.
964 * @param data The raw sample data.
965 * @ch_state Pointer to the state of the channel whose data we're processing.
966 * @sdi The device instance.
968 * @return SR_ERR when data is trucated, SR_OK otherwise.
970 static int dlm_digital_samples_send(GArray *data,
971 struct sr_dev_inst *sdi)
973 struct dev_context *devc;
974 struct scope_state *model_state;
976 struct sr_datafeed_logic logic;
977 struct sr_datafeed_packet packet;
980 model_state = devc->model_state;
981 samples = model_state->samples_per_frame;
983 if (data->len < samples * sizeof(uint8_t)) {
984 sr_err("Truncated waveform data packet received.");
988 logic.length = samples;
990 logic.data = data->data;
991 packet.type = SR_DF_LOGIC;
992 packet.payload = &logic;
993 sr_session_send(sdi, &packet);
995 g_array_remove_range(data, 0, samples * sizeof(uint8_t));
1001 * Attempts to query sample data from the oscilloscope in order to send it
1002 * to the session bus for further processing.
1004 * @param fd The file descriptor used as the event source.
1005 * @param revents The received events.
1006 * @param cb_data Callback data, in this case our device instance.
1008 * @return TRUE in case of success or a recoverable error,
1009 * FALSE when a fatal error was encountered.
1011 SR_PRIV int dlm_data_receive(int fd, int revents, void *cb_data)
1013 struct sr_dev_inst *sdi;
1014 struct scope_state *model_state;
1015 struct dev_context *devc;
1016 struct sr_channel *ch;
1017 struct sr_datafeed_packet packet;
1018 int chunk_len, num_bytes;
1019 static GArray *data = NULL;
1024 if (!(sdi = cb_data))
1027 if (!(devc = sdi->priv))
1030 if (!(model_state = (struct scope_state*)devc->model_state))
1033 /* Are we waiting for a response from the device? */
1034 if (!devc->data_pending)
1037 /* Check if a new query response is coming our way. */
1039 if (sr_scpi_read_begin(sdi->conn) == SR_OK)
1040 /* The 16 here accounts for the header and EOL. */
1041 data = g_array_sized_new(FALSE, FALSE, sizeof(uint8_t),
1042 16 + model_state->samples_per_frame);
1047 /* Store incoming data. */
1048 chunk_len = sr_scpi_read_data(sdi->conn, devc->receive_buffer,
1049 RECEIVE_BUFFER_SIZE);
1050 if (chunk_len < 0) {
1051 sr_err("Error while reading data: %d", chunk_len);
1054 g_array_append_vals(data, devc->receive_buffer, chunk_len);
1056 /* Read the entire query response before processing. */
1057 if (!sr_scpi_read_complete(sdi->conn))
1060 /* We finished reading and are no longer waiting for data. */
1061 devc->data_pending = FALSE;
1063 /* Signal the beginning of a new frame if this is the first channel. */
1064 if (devc->current_channel == devc->enabled_channels) {
1065 packet.type = SR_DF_FRAME_BEGIN;
1066 sr_session_send(sdi, &packet);
1069 if (dlm_block_data_header_process(data, &num_bytes) != SR_OK) {
1070 sr_err("Encountered malformed block data header.");
1074 if (num_bytes == 0) {
1075 sr_warn("Zero-length waveform data packet received. " \
1076 "Live mode not supported yet, stopping " \
1077 "acquisition and retrying.");
1078 /* Don't care about return value here. */
1079 dlm_acquisition_stop(sdi->conn);
1080 g_array_free(data, TRUE);
1081 dlm_channel_data_request(sdi);
1085 ch = devc->current_channel->data;
1087 case SR_CHANNEL_ANALOG:
1088 if (dlm_analog_samples_send(data,
1089 &model_state->analog_states[ch->index],
1093 case SR_CHANNEL_LOGIC:
1094 if (dlm_digital_samples_send(data, sdi) != SR_OK)
1098 sr_err("Invalid channel type encountered.");
1102 g_array_free(data, TRUE);
1106 * Signal the end of this frame if this was the last enabled channel
1107 * and set the next enabled channel. Then, request its data.
1109 if (!devc->current_channel->next) {
1110 packet.type = SR_DF_FRAME_END;
1111 sr_session_send(sdi, &packet);
1112 devc->current_channel = devc->enabled_channels;
1115 * As of now we only support importing the current acquisition
1116 * data so we're going to stop at this point.
1118 sr_dev_acquisition_stop(sdi);
1121 devc->current_channel = devc->current_channel->next;
1123 if (dlm_channel_data_request(sdi) != SR_OK) {
1124 sr_err("Failed to request acquisition data.");
1132 g_array_free(data, TRUE);