]> sigrok.org Git - libsigrok.git/blame - src/hardware/yokogawa-dlm/protocol.c
yokogawa-dlm: Do not block when receiving and save frame length in scope state
[libsigrok.git] / src / hardware / yokogawa-dlm / protocol.c
CommitLineData
10763937
SA
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2014 abraxa (Soeren Apel) <soeren@apelpie.net>
5 * Based on the Hameg HMO driver by poljar (Damir Jelić) <poljarinho@gmail.com>
6 *
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.
11 *
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.
16 *
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/>.
19 */
20
8ab929d6
SA
21/** @file
22 * <em>Yokogawa DL/DLM series</em> oscilloscope driver
23 * @internal
24 */
25
10763937
SA
26#include "protocol.h"
27
8ab929d6
SA
28static const int32_t dlm_hwcaps[] = {
29 SR_CONF_LOGIC_ANALYZER,
30 SR_CONF_OSCILLOSCOPE,
31 SR_CONF_TRIGGER_SLOPE,
32 SR_CONF_TRIGGER_SOURCE,
33 SR_CONF_TIMEBASE,
34 SR_CONF_NUM_TIMEBASE,
35 SR_CONF_HORIZ_TRIGGERPOS,
36};
37
38static const int32_t dlm_analog_caps[] = {
39 SR_CONF_VDIV,
40 SR_CONF_COUPLING,
41 SR_CONF_NUM_VDIV,
42};
43
44static const char *dlm_coupling_options[] = {
45 "AC",
46 "DC",
47 "DC50",
48 "GND",
49 NULL,
50};
51
52/* Note: Values must correlate to the trigger_slopes values */
53static const char *dlm_trigger_slopes[] = {
54 "r",
55 "f",
56 NULL,
57};
58
59static const char *dlm_2ch_trigger_sources[] = {
60 "1",
61 "2",
62 "LINE",
63 "EXT",
64 NULL,
65};
66
67/* TODO: Is BITx handled correctly or is Dx required? */
68static const char *dlm_4ch_trigger_sources[] = {
69 "1",
70 "2",
71 "3",
72 "4",
73 "LINE",
74 "EXT",
75 "BIT1",
76 "BIT2",
77 "BIT3",
78 "BIT4",
79 "BIT5",
80 "BIT6",
81 "BIT7",
82 "BIT8",
83 NULL,
84};
85
86static const uint64_t dlm_timebases[][2] = {
87 /* nanoseconds */
88 { 1, 1000000000 },
89 { 2, 1000000000 },
90 { 5, 1000000000 },
91 { 10, 1000000000 },
92 { 20, 1000000000 },
93 { 50, 1000000000 },
94 { 100, 1000000000 },
95 { 200, 1000000000 },
96 { 500, 1000000000 },
97 /* microseconds */
98 { 1, 1000000 },
99 { 2, 1000000 },
100 { 5, 1000000 },
101 { 10, 1000000 },
102 { 20, 1000000 },
103 { 50, 1000000 },
104 { 100, 1000000 },
105 { 200, 1000000 },
106 { 500, 1000000 },
107 /* milliseconds */
108 { 1, 1000 },
109 { 2, 1000 },
110 { 5, 1000 },
111 { 10, 1000 },
112 { 20, 1000 },
113 { 50, 1000 },
114 { 100, 1000 },
115 { 200, 1000 },
116 { 500, 1000 },
117 /* seconds */
118 { 1, 1 },
119 { 2, 1 },
120 { 5, 1 },
121 { 10, 1 },
122 { 20, 1 },
123 { 50, 1 },
124 { 100, 1 },
125 { 200, 1 },
126 { 500, 1 },
127};
128
129static const uint64_t dlm_vdivs[][2] = {
130 /* millivolts */
131 { 2, 1000 },
132 { 5, 1000 },
133 { 10, 1000 },
134 { 20, 1000 },
135 { 50, 1000 },
136 { 100, 1000 },
137 { 200, 1000 },
138 { 500, 1000 },
139 /* volts */
140 { 1, 1 },
141 { 2, 1 },
142 { 5, 1 },
143 { 10, 1 },
144 { 20, 1 },
145 { 50, 1 },
146 { 100, 1 },
147 { 200, 1 },
148 { 500, 1 },
149};
150
151static const char *scope_analog_channel_names[] = {
152 "1",
153 "2",
154 "3",
155 "4"
156};
157
158static const char *scope_digital_channel_names[] = {
159 "D0",
160 "D1",
161 "D2",
162 "D3",
163 "D4",
164 "D5",
165 "D6",
166 "D7"
167};
168
169static struct scope_config scope_models[] = {
170 {
171 .model_id = {"710105", "710115", "710125", NULL},
172 .model_name = {"DLM2022", "DLM2032", "DLM2052", NULL},
173 .analog_channels = 2,
174 .digital_channels = 0,
175 .pods = 0,
176
177 .analog_names = &scope_analog_channel_names,
178 .digital_names = &scope_digital_channel_names,
179
180 .hw_caps = &dlm_hwcaps,
181 .num_hwcaps = ARRAY_SIZE(dlm_hwcaps),
182
183 .analog_hwcaps = &dlm_analog_caps,
184 .num_analog_hwcaps = ARRAY_SIZE(dlm_analog_caps),
185
186 .coupling_options = &dlm_coupling_options,
187 .trigger_sources = &dlm_2ch_trigger_sources,
188 .trigger_slopes = &dlm_trigger_slopes,
189
190 .timebases = &dlm_timebases,
191 .num_timebases = ARRAY_SIZE(dlm_timebases),
192
193 .vdivs = &dlm_vdivs,
194 .num_vdivs = ARRAY_SIZE(dlm_vdivs),
195
196 .num_xdivs = 10,
197 .num_ydivs = 8,
198 },
199 {
200 .model_id = {"710110", "710120", "710130", NULL},
201 .model_name = {"DLM2024", "DLM2034", "DLM2054", NULL},
202 .analog_channels = 4,
203 .digital_channels = 8,
204 .pods = 1,
205
206 .analog_names = &scope_analog_channel_names,
207 .digital_names = &scope_digital_channel_names,
208
209 .hw_caps = &dlm_hwcaps,
210 .num_hwcaps = ARRAY_SIZE(dlm_hwcaps),
211
212 .analog_hwcaps = &dlm_analog_caps,
213 .num_analog_hwcaps = ARRAY_SIZE(dlm_analog_caps),
214
215 .coupling_options = &dlm_coupling_options,
216 .trigger_sources = &dlm_4ch_trigger_sources,
217 .trigger_slopes = &dlm_trigger_slopes,
218
219 .timebases = &dlm_timebases,
220 .num_timebases = ARRAY_SIZE(dlm_timebases),
221
222 .vdivs = &dlm_vdivs,
223 .num_vdivs = ARRAY_SIZE(dlm_vdivs),
224
225 .num_xdivs = 10,
226 .num_ydivs = 8,
227 },
228};
229
230/**
231 * Prints out the state of the device as we currently know it.
232 *
233 * @param config This is the scope configuration.
234 * @param state The current scope state to print.
235 */
236static void scope_state_dump(struct scope_config *config,
237 struct scope_state *state)
238{
239 unsigned int i;
240 char *tmp;
241
242 for (i = 0; i < config->analog_channels; ++i) {
243 tmp = sr_voltage_string((*config->vdivs)[state->analog_states[i].vdiv][0],
244 (*config->vdivs)[state->analog_states[i].vdiv][1]);
245 sr_info("State of analog channel %d -> %s : %s (coupling) %s (vdiv) %2.2e (offset)",
246 i + 1, state->analog_states[i].state ? "On" : "Off",
247 (*config->coupling_options)[state->analog_states[i].coupling],
248 tmp, state->analog_states[i].vertical_offset);
249 }
250
251 for (i = 0; i < config->digital_channels; ++i) {
252 sr_info("State of digital channel %d -> %s", i,
253 state->digital_states[i] ? "On" : "Off");
254 }
255
256 for (i = 0; i < config->pods; ++i) {
257 sr_info("State of digital POD %d -> %s", i,
258 state->pod_states[i] ? "On" : "Off");
259 }
260
261 tmp = sr_period_string((*config->timebases)[state->timebase][0] *
262 (*config->timebases)[state->timebase][1]);
263 sr_info("Current timebase: %s", tmp);
264 g_free(tmp);
265
266 tmp = sr_samplerate_string(state->sample_rate);
267 sr_info("Current samplerate: %s", tmp);
268 g_free(tmp);
269
af3487ec
SA
270 sr_info("Current samples per acquisition (i.e. frame): %d",
271 state->samples_per_frame);
272
8ab929d6
SA
273 sr_info("Current trigger: %s (source), %s (slope) %.2f (offset)",
274 (*config->trigger_sources)[state->trigger_source],
275 (*config->trigger_slopes)[state->trigger_slope],
276 state->horiz_triggerpos);
277}
278
279/**
280 * Searches through an array of strings and returns the index to the
281 * array where a given string is located.
282 *
283 * @param value The string to search for.
284 * @param array The array of strings.
285 * @param result The index at which value is located in array. -1 on error.
286 *
287 * @return SR_ERR when value couldn't be found, SR_OK otherwise.
288 */
289static int array_option_get(char *value, const char *(*array)[],
290 int *result)
291{
292 unsigned int i;
293
294 *result = -1;
295
296 for (i = 0; (*array)[i]; ++i)
297 if (!g_strcmp0(value, (*array)[i])) {
298 *result = i;
299 break;
300 }
301
302 if (*result == -1)
303 return SR_ERR;
304
305 return SR_OK;
306}
307
308/**
309 * This function takes a value of the form "2.000E-03", converts it to a
310 * significand / factor pair and returns the index of an array where
311 * a matching pair was found.
312 *
313 * It's a bit convoluted because of floating-point issues. The value "10.00E-09"
314 * is parsed by g_ascii_strtod() as 0.000000009999999939, for example.
315 * Therefore it's easier to break the number up into two strings and handle
316 * them separately.
317 *
318 * @param value The string to be parsed.
319 * @param array The array of s/f pairs.
320 * @param array_len The number of pairs in the array.
321 * @param result The index at which a matching pair was found.
322 *
323 * @return SR_ERR on any parsing error, SR_OK otherwise.
324 */
325static int array_float_get(gchar *value, const uint64_t array[][2],
326 int array_len, int *result)
327{
328 int i;
329 uint64_t f;
330 float s;
331 gchar ss[10], es[10];
332
333 memset(ss, 0, sizeof(ss));
334 memset(es, 0, sizeof(es));
335
336 strncpy(ss, value, 5);
337 strncpy(es, &(value[6]), 3);
338
339 if (sr_atof_ascii(ss, &s) != SR_OK)
340 return SR_ERR;
341 if (sr_atoi(es, &i) != SR_OK)
342 return SR_ERR;
343
344 /* Transform e.g. 10^-03 to 1000 as the array stores the inverse. */
345 f = pow(10, abs(i));
346
347 /* Adjust the significand/factor pair to make sure
348 * that f is a multiple of 1000.
349 */
350 while ((int)fmod(log10(f), 3) > 0) { s *= 10; f *= 10; }
351
352 /* Truncate s to circumvent rounding errors. */
353 s = (int)s;
354
355 for (i = 0; i < array_len; i++) {
356 if ( (s == array[i][0]) && (f == array[i][1]) ) {
357 *result = i;
358 return SR_OK;
359 }
360 }
361
362 return SR_ERR;
363}
364
365/**
366 * Obtains information about all analog channels from the oscilloscope.
367 * The internal state information is updated accordingly.
368 *
369 * @param scpi An open SCPI connection.
370 * @param config The device's device configuration.
371 * @param state The device's state information.
372 *
373 * @return SR_ERR on error, SR_OK otherwise.
374 */
375static int analog_channel_state_get(struct sr_scpi_dev_inst *scpi,
376 struct scope_config *config,
377 struct scope_state *state)
378{
379 int i, j;
380 gchar *response;
381
382 for (i = 0; i < config->analog_channels; ++i) {
383
384 if (dlm_analog_chan_state_get(scpi, i + 1,
385 &state->analog_states[i].state) != SR_OK)
386 return SR_ERR;
387
388 if (dlm_analog_chan_vdiv_get(scpi, i + 1, &response) != SR_OK)
389 return SR_ERR;
390
391 if (array_float_get(response, *config->vdivs, config->num_vdivs,
392 &j) != SR_OK) {
393 g_free(response);
394 return SR_ERR;
395 }
396
397 g_free(response);
398 state->analog_states[i].vdiv = j;
399
400 if (dlm_analog_chan_voffs_get(scpi, i + 1,
401 &state->analog_states[i].vertical_offset) != SR_OK)
402 return SR_ERR;
403
404 if (dlm_analog_chan_wrange_get(scpi, i + 1,
405 &state->analog_states[i].waveform_range) != SR_OK)
406 return SR_ERR;
407
408 if (dlm_analog_chan_woffs_get(scpi, i + 1,
409 &state->analog_states[i].waveform_offset) != SR_OK)
410 return SR_ERR;
411
412 if (dlm_analog_chan_coupl_get(scpi, i + 1, &response) != SR_OK) {
413 g_free(response);
414 return SR_ERR;
415 }
416
417 if (array_option_get(response, config->coupling_options,
418 &state->analog_states[i].coupling) != SR_OK) {
419 g_free(response);
420 return SR_ERR;
421 }
422 g_free(response);
423 }
424
425 return SR_OK;
426}
427
428/**
429 * Obtains information about all digital channels from the oscilloscope.
430 * The internal state information is updated accordingly.
431 *
432 * @param scpi An open SCPI connection.
433 * @param config The device's device configuration.
434 * @param state The device's state information.
435 *
436 * @return SR_ERR on error, SR_OK otherwise.
437 */
438static int digital_channel_state_get(struct sr_scpi_dev_inst *scpi,
439 struct scope_config *config,
440 struct scope_state *state)
441{
442 unsigned int i;
443
444 if (!config->digital_channels)
445 {
446 sr_warn("Tried obtaining digital channel states on a " \
447 "model without digital inputs.");
448 return SR_OK;
449 }
450
451 for (i = 0; i < config->digital_channels; ++i) {
452 if (dlm_digital_chan_state_get(scpi, i + 1,
453 &state->digital_states[i]) != SR_OK) {
454 return SR_ERR;
455 }
456 }
457
458 if (!config->pods)
459 {
460 sr_warn("Tried obtaining pod states on a model without pods.");
461 return SR_OK;
462 }
463
464 for (i = 0; i < config->pods; ++i) {
465 if (dlm_digital_pod_state_get(scpi, i + 'A',
466 &state->pod_states[i]) != SR_OK)
467 return SR_ERR;
468 }
469
470 return SR_OK;
471}
472
473/**
474 * Obtains information about the sample rate from the oscilloscope.
475 * The internal state information is updated accordingly.
476 *
477 * @param sdi The device instance.
478 *
479 * @return SR_ERR on error, SR_OK otherwise.
480 */
481SR_PRIV int dlm_sample_rate_query(const struct sr_dev_inst *sdi)
482{
483 struct dev_context *devc;
484 struct scope_state *state;
485 float tmp_float;
486
487 devc = sdi->priv;
488 state = devc->model_state;
489
490 /* No need to find an active channel to query the sample rate:
491 * querying any channel will do, so we use channel 1 all the time.
492 */
493 if (dlm_analog_chan_srate_get(sdi->conn, 1, &tmp_float) != SR_OK)
494 return SR_ERR;
495
496 state->sample_rate = tmp_float;
497
498 return SR_OK;
499}
500
501/**
502 * Obtains information about the current device state from the oscilloscope,
503 * including all analog and digital channel configurations.
504 * The internal state information is updated accordingly.
505 *
506 * @param sdi The device instance.
507 *
508 * @return SR_ERR on error, SR_OK otherwise.
509 */
510SR_PRIV int dlm_scope_state_query(struct sr_dev_inst *sdi)
511{
512 struct dev_context *devc;
513 struct scope_state *state;
514 struct scope_config *config;
515 float tmp_float;
516 gchar *response;
517 int i;
518
519 devc = sdi->priv;
520 config = devc->model_config;
521 state = devc->model_state;
522
523 if (analog_channel_state_get(sdi->conn, config, state) != SR_OK)
524 return SR_ERR;
525
526 if (digital_channel_state_get(sdi->conn, config, state) != SR_OK)
527 return SR_ERR;
528
529 if (dlm_timebase_get(sdi->conn, &response) != SR_OK)
530 return SR_ERR;
531
532 if (array_float_get(response, *config->timebases,
533 config->num_timebases, &i) != SR_OK) {
534 g_free(response);
535 return SR_ERR;
536 }
537
538 g_free(response);
539 state->timebase = i;
540
541 if (dlm_horiz_trigger_pos_get(sdi->conn, &tmp_float) != SR_OK)
542 return SR_ERR;
543
544 /* TODO: Check if the calculation makes sense for the DLM. */
545 state->horiz_triggerpos = tmp_float /
546 (((double)(*config->timebases)[state->timebase][0] /
547 (*config->timebases)[state->timebase][1]) * config->num_xdivs);
548 state->horiz_triggerpos -= 0.5;
549 state->horiz_triggerpos *= -1;
550
551 if (dlm_trigger_source_get(sdi->conn, &response) != SR_OK) {
552 g_free(response);
553 return SR_ERR;
554 }
555
556 if (array_option_get(response, config->trigger_sources,
557 &state->trigger_source) != SR_OK) {
558 g_free(response);
559 return SR_ERR;
560 }
561
562 g_free(response);
563
564 if (dlm_trigger_slope_get(sdi->conn, &i) != SR_OK)
565 return SR_ERR;
566
567 state->trigger_slope = i;
568
af3487ec
SA
569 if (dlm_acq_length_get(sdi->conn, &state->samples_per_frame) != SR_OK) {
570 sr_err("Failed to query acquisition length.");
571 return SR_ERR;
572 }
573
8ab929d6
SA
574 dlm_sample_rate_query(sdi);
575
576 scope_state_dump(config, state);
577
578 return SR_OK;
579}
580
581/**
582 * Creates a new device state structure.
583 *
584 * @param config The device configuration to use.
585 *
586 * @return The newly allocated scope_state struct or NULL on error.
587 */
588static struct scope_state *dlm_scope_state_new(struct scope_config *config)
589{
590 struct scope_state *state;
591
592 if (!(state = g_try_malloc0(sizeof(struct scope_state))))
593 return NULL;
594
595 state->analog_states = g_malloc0(config->analog_channels *
596 sizeof(struct analog_channel_state));
597
598 state->digital_states = g_malloc0(config->digital_channels *
599 sizeof(gboolean));
600
601 state->pod_states = g_malloc0(config->pods * sizeof(gboolean));
602
603 return state;
604}
605
606/**
607 * Frees the memory that was allocated by a call to dlm_scope_state_new().
608 *
609 * @param state The device state structure whose memory is to be freed.
610 */
611SR_PRIV void dlm_scope_state_destroy(struct scope_state *state)
612{
613 g_free(state->analog_states);
614 g_free(state->digital_states);
615 g_free(state->pod_states);
616 g_free(state);
617}
618
619SR_PRIV int dlm_model_get(char *model_id, char **model_name, int *model_index)
620{
621 unsigned int i, j;
622
623 *model_index = -1;
624 *model_name = NULL;
625
626 for (i = 0; i < ARRAY_SIZE(scope_models); i++) {
627 for (j = 0; scope_models[i].model_id[j]; j++) {
628 if (!strcmp(model_id, scope_models[i].model_id[j])) {
629 *model_index = i;
630 *model_name = (char *)scope_models[i].model_name[j];
631 break;
632 }
633 }
634 if (*model_index != -1)
635 break;
636 }
637
638 if (*model_index == -1) {
639 sr_err("Found unsupported DLM device with model identifier %s.",
640 model_id);
641 return SR_ERR_NA;
642 }
643
644 return SR_OK;
645}
646
647/**
648 * Attempts to initialize a DL/DLM device and prepares internal structures
649 * if a suitable device was found.
650 *
651 * @param sdi The device instance.
652 */
653SR_PRIV int dlm_device_init(struct sr_dev_inst *sdi, int model_index)
654{
655 char tmp[25];
656 int i;
657 struct sr_channel *ch;
658 struct dev_context *devc;
659
660 devc = sdi->priv;
661
662 devc->analog_groups = g_malloc0(sizeof(struct sr_channel_group*) *
663 scope_models[model_index].analog_channels);
664
665 devc->digital_groups = g_malloc0(sizeof(struct sr_channel_group*) *
666 scope_models[model_index].digital_channels);
667
668 /* Add analog channels. */
669 for (i = 0; i < scope_models[model_index].analog_channels; i++) {
670 if (!(ch = sr_channel_new(i, SR_CHANNEL_ANALOG, TRUE,
671 (*scope_models[model_index].analog_names)[i])))
672 return SR_ERR_MALLOC;
673 sdi->channels = g_slist_append(sdi->channels, ch);
674
675 devc->analog_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
676
677 devc->analog_groups[i]->name = g_strdup(
678 (char *)(*scope_models[model_index].analog_names)[i]);
679 devc->analog_groups[i]->channels = g_slist_append(NULL, ch);
680
681 sdi->channel_groups = g_slist_append(sdi->channel_groups,
682 devc->analog_groups[i]);
683 }
684
685 /* Add digital channel groups. */
686 for (i = 0; i < scope_models[model_index].pods; ++i) {
687 g_snprintf(tmp, sizeof(tmp), "POD%d", i);
688
689 devc->digital_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
690 if (!devc->digital_groups[i])
691 return SR_ERR_MALLOC;
692
693 devc->digital_groups[i]->name = g_strdup(tmp);
694 sdi->channel_groups = g_slist_append(sdi->channel_groups,
695 devc->digital_groups[i]);
696 }
697
698 /* Add digital channels. */
699 for (i = 0; i < scope_models[model_index].digital_channels; i++) {
700 if (!(ch = sr_channel_new(i, SR_CHANNEL_LOGIC, TRUE,
701 (*scope_models[model_index].digital_names)[i])))
702 return SR_ERR_MALLOC;
703 sdi->channels = g_slist_append(sdi->channels, ch);
704
705 devc->digital_groups[i / 8]->channels = g_slist_append(
706 devc->digital_groups[i / 8]->channels, ch);
707 }
708 devc->model_config = &scope_models[model_index];
709 devc->frame_limit = 0;
710
711 if (!(devc->model_state = dlm_scope_state_new(devc->model_config)))
712 return SR_ERR_MALLOC;
713
714 /* Disable non-standard response behavior. */
715 if (dlm_response_headers_set(sdi->conn, FALSE) != SR_OK)
716 return SR_ERR;
717
718 return SR_OK;
719}
720
af3487ec 721SR_PRIV int dlm_channel_data_request(const struct sr_dev_inst *sdi)
8ab929d6 722{
af3487ec
SA
723 struct dev_context *devc;
724 struct sr_channel *ch;
725 int result;
8ab929d6 726
af3487ec
SA
727 devc = sdi->priv;
728 ch = devc->current_channel->data;
8ab929d6 729
af3487ec
SA
730 switch (ch->type) {
731 case SR_CHANNEL_ANALOG:
732 result = dlm_analog_data_get(sdi->conn, ch->index + 1);
733 break;
734 case SR_CHANNEL_LOGIC:
735 result = dlm_digital_data_get(sdi->conn);
736 break;
737 default:
738 sr_err("Invalid channel type encountered (%d).",
739 ch->type);
740 result = SR_ERR;
8ab929d6
SA
741 }
742
af3487ec
SA
743 if (result == SR_OK)
744 devc->data_pending = TRUE;
745 else
746 devc->data_pending = FALSE;
747
748 return result;
8ab929d6
SA
749}
750
751/**
752 * Reads and removes the block data header from a given data input.
753 * Format is #ndddd... with n being the number of decimal digits d.
754 * The string dddd... contains the decimal-encoded length of the data.
755 * Example: #9000000013 would yield a length of 13 bytes.
756 *
757 * @param data The input data.
758 * @param len The determined input data length.
759 */
760static int dlm_block_data_header_process(GArray *data, int *len)
761{
762 int i, n;
763 gchar s[20];
764
765 if (g_array_index(data, gchar, 0) != '#')
766 return SR_ERR;
767
768 n = (uint8_t)(g_array_index(data, gchar, 1) - '0');
769
770 for (i = 0; i < n; i++)
771 s[i] = g_array_index(data, gchar, 2 + i);
772 s[i] = 0;
773
774 if (sr_atoi(s, len) != SR_OK)
775 return SR_ERR;
776
777 g_array_remove_range(data, 0, 2 + n);
778
779 return SR_OK;
780}
781
782/**
783 * Turns raw sample data into voltages and sends them off to the session bus.
784 *
785 * @param data The raw sample data.
8ab929d6
SA
786 * @ch_state Pointer to the state of the channel whose data we're processing.
787 * @sdi The device instance.
788 *
789 * @return SR_ERR when data is trucated, SR_OK otherwise.
790 */
af3487ec 791static int dlm_analog_samples_send(GArray *data,
8ab929d6
SA
792 struct analog_channel_state *ch_state,
793 struct sr_dev_inst *sdi)
794{
af3487ec 795 uint32_t i, samples;
8ab929d6
SA
796 float voltage, range, offset;
797 GArray *float_data;
798 struct dev_context *devc;
af3487ec 799 struct scope_state *model_state;
8ab929d6
SA
800 struct sr_channel *ch;
801 struct sr_datafeed_analog analog;
802 struct sr_datafeed_packet packet;
803
af3487ec
SA
804 devc = sdi->priv;
805 model_state = devc->model_state;
806 samples = model_state->samples_per_frame;
807 ch = devc->current_channel->data;
808
8ab929d6
SA
809 if (data->len < samples * sizeof(uint8_t)) {
810 sr_err("Truncated waveform data packet received.");
811 return SR_ERR;
812 }
813
8ab929d6
SA
814 range = ch_state->waveform_range;
815 offset = ch_state->waveform_offset;
816
817 /* Convert byte sample to voltage according to
818 * page 269 of the Communication Interface User's Manual.
819 */
820 float_data = g_array_new(FALSE, FALSE, sizeof(float));
821 for (i = 0; i < samples; i++) {
822 voltage = (float)g_array_index(data, int8_t, i);
823 voltage = (range * voltage /
824 DLM_DIVISION_FOR_BYTE_FORMAT) + offset;
825 g_array_append_val(float_data, voltage);
826 }
827
828 analog.channels = g_slist_append(NULL, ch);
829 analog.num_samples = float_data->len;
830 analog.data = (float*)float_data->data;
831 analog.mq = SR_MQ_VOLTAGE;
832 analog.unit = SR_UNIT_VOLT;
833 analog.mqflags = 0;
834 packet.type = SR_DF_ANALOG;
835 packet.payload = &analog;
836 sr_session_send(sdi, &packet);
837 g_slist_free(analog.channels);
838
839 g_array_free(float_data, TRUE);
840 g_array_remove_range(data, 0, samples * sizeof(uint8_t));
841
842 return SR_OK;
843}
844
845/**
846 * Sends logic sample data off to the session bus.
847 *
848 * @param data The raw sample data.
8ab929d6
SA
849 * @ch_state Pointer to the state of the channel whose data we're processing.
850 * @sdi The device instance.
851 *
852 * @return SR_ERR when data is trucated, SR_OK otherwise.
853 */
af3487ec 854static int dlm_digital_samples_send(GArray *data,
8ab929d6
SA
855 struct sr_dev_inst *sdi)
856{
af3487ec
SA
857 struct dev_context *devc;
858 struct scope_state *model_state;
859 uint32_t samples;
8ab929d6
SA
860 struct sr_datafeed_logic logic;
861 struct sr_datafeed_packet packet;
862
af3487ec
SA
863 devc = sdi->priv;
864 model_state = devc->model_state;
865 samples = model_state->samples_per_frame;
866
8ab929d6
SA
867 if (data->len < samples * sizeof(uint8_t)) {
868 sr_err("Truncated waveform data packet received.");
869 return SR_ERR;
870 }
871
872 logic.length = samples;
873 logic.unitsize = 1;
874 logic.data = data->data;
875 packet.type = SR_DF_LOGIC;
876 packet.payload = &logic;
877 sr_session_send(sdi, &packet);
878
879 g_array_remove_range(data, 0, samples * sizeof(uint8_t));
880
881 return SR_OK;
882}
883
884/**
885 * Attempts to query sample data from the oscilloscope in order to send it
886 * to the session bus for further processing.
887 *
888 * @param fd The file descriptor used as the event source.
889 * @param revents The received events.
890 * @param cb_data Callback data, in this case our device instance.
891 *
892 * @return TRUE in case of success or a recoverable error,
893 * FALSE when a fatal error was encountered.
894 */
895SR_PRIV int dlm_data_receive(int fd, int revents, void *cb_data)
896{
8ab929d6
SA
897 struct sr_dev_inst *sdi;
898 struct scope_state *model_state;
899 struct dev_context *devc;
af3487ec 900 struct sr_channel *ch;
8ab929d6 901 struct sr_datafeed_packet packet;
af3487ec
SA
902 int chunk_len, num_bytes;
903 static GArray *data = NULL;
8ab929d6
SA
904
905 (void)fd;
906 (void)revents;
907
908 if (!(sdi = cb_data))
909 return FALSE;
910
911 if (!(devc = sdi->priv))
912 return FALSE;
913
914 if (!(model_state = (struct scope_state*)devc->model_state))
915 return FALSE;
916
af3487ec
SA
917 /* Are we waiting for a response from the device? */
918 if (!devc->data_pending)
8ab929d6 919 return TRUE;
af3487ec
SA
920
921 /* Check if a new query response is coming our way. */
922 if (!data) {
923 if (sr_scpi_read_begin(sdi->conn) == SR_OK)
924 /* The 16 here accounts for the header and EOL. */
925 data = g_array_sized_new(FALSE, FALSE, sizeof(uint8_t),
926 16 + model_state->samples_per_frame);
927 else
928 return TRUE;
8ab929d6
SA
929 }
930
af3487ec
SA
931 /* Store incoming data. */
932 chunk_len = sr_scpi_read_data(sdi->conn, devc->receive_buffer,
933 RECEIVE_BUFFER_SIZE);
934 if (chunk_len < 0) {
935 sr_err("Error while reading data: %d", chunk_len);
936 goto fail;
937 }
938 g_array_append_vals(data, devc->receive_buffer, chunk_len);
8ab929d6 939
af3487ec
SA
940 /* Read the entire query response before processing. */
941 if (!sr_scpi_read_complete(sdi->conn))
942 return TRUE;
8ab929d6 943
af3487ec
SA
944 /* We finished reading and are no longer waiting for data. */
945 devc->data_pending = FALSE;
8ab929d6 946
af3487ec
SA
947 /* Signal the beginning of a new frame if this is the first channel. */
948 if (devc->current_channel == devc->enabled_channels) {
949 packet.type = SR_DF_FRAME_BEGIN;
950 sr_session_send(sdi, &packet);
951 }
8ab929d6 952
af3487ec
SA
953 if (dlm_block_data_header_process(data, &num_bytes) != SR_OK) {
954 sr_err("Encountered malformed block data header.");
955 goto fail;
956 }
8ab929d6 957
af3487ec
SA
958 if (num_bytes == 0) {
959 sr_warn("Zero-length waveform data packet received. " \
960 "Live mode not supported yet, stopping " \
961 "acquisition and retrying.");
962 /* Don't care about return value here. */
963 dlm_acquisition_stop(sdi->conn);
964 g_array_free(data, TRUE);
965 return TRUE;
966 }
8ab929d6 967
af3487ec
SA
968 ch = devc->current_channel->data;
969 switch (ch->type) {
970 case SR_CHANNEL_ANALOG:
971 if (dlm_analog_samples_send(data,
972 &model_state->analog_states[ch->index],
973 sdi) != SR_OK)
8ab929d6 974 goto fail;
af3487ec
SA
975 break;
976 case SR_CHANNEL_LOGIC:
977 if (dlm_digital_samples_send(data, sdi) != SR_OK)
978 goto fail;
979 break;
980 default:
981 sr_err("Invalid channel type encountered.");
982 break;
983 }
8ab929d6 984
af3487ec
SA
985 g_array_free(data, TRUE);
986 data = NULL;
8ab929d6 987
af3487ec
SA
988 /* Signal the end of this frame if this was the last enabled channel
989 * and set the next enabled channel. Then, request its data.
990 */
991 if (!devc->current_channel->next) {
992 packet.type = SR_DF_FRAME_END;
993 sr_session_send(sdi, &packet);
994 devc->current_channel = devc->enabled_channels;
995
996 /* As of now we only support importing the current acquisition
997 * data so we're going to stop at this point.
998 */
999 sdi->driver->dev_acquisition_stop(sdi, cb_data);
1000 return TRUE;
1001 } else
1002 devc->current_channel = devc->current_channel->next;
8ab929d6 1003
af3487ec
SA
1004 if (dlm_channel_data_request(sdi) != SR_OK) {
1005 sr_err("Failed to request aquisition data.");
1006 goto fail;
8ab929d6
SA
1007 }
1008
8ab929d6
SA
1009 return TRUE;
1010
1011fail:
af3487ec 1012 if (data) {
8ab929d6 1013 g_array_free(data, TRUE);
af3487ec
SA
1014 data = NULL;
1015 }
8ab929d6 1016
af3487ec 1017 return FALSE;
8ab929d6 1018}