]> sigrok.org Git - libsigrok.git/blame - src/hardware/yokogawa-dlm/protocol.c
Change type of SR_CONF keys to uint32_t.
[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
584560f1 28static const uint32_t dlm_hwcaps[] = {
8ab929d6
SA
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
584560f1 38static const uint32_t dlm_analog_caps[] = {
8ab929d6
SA
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],
ac10a927 244 (*config->vdivs)[state->analog_states[i].vdiv][1]);
8ab929d6 245 sr_info("State of analog channel %d -> %s : %s (coupling) %s (vdiv) %2.2e (offset)",
ac10a927
SA
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);
8ab929d6
SA
249 }
250
251 for (i = 0; i < config->digital_channels; ++i) {
252 sr_info("State of digital channel %d -> %s", i,
ac10a927 253 state->digital_states[i] ? "On" : "Off");
8ab929d6
SA
254 }
255
256 for (i = 0; i < config->pods; ++i) {
257 sr_info("State of digital POD %d -> %s", i,
ac10a927 258 state->pod_states[i] ? "On" : "Off");
8ab929d6
SA
259 }
260
261 tmp = sr_period_string((*config->timebases)[state->timebase][0] *
ac10a927 262 (*config->timebases)[state->timebase][1]);
8ab929d6
SA
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 270 sr_info("Current samples per acquisition (i.e. frame): %d",
ac10a927 271 state->samples_per_frame);
af3487ec 272
8ab929d6 273 sr_info("Current trigger: %s (source), %s (slope) %.2f (offset)",
ac10a927
SA
274 (*config->trigger_sources)[state->trigger_source],
275 (*config->trigger_slopes)[state->trigger_slope],
276 state->horiz_triggerpos);
8ab929d6
SA
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;
ac10a927 331 unsigned int s_int;
8ab929d6
SA
332 gchar ss[10], es[10];
333
334 memset(ss, 0, sizeof(ss));
335 memset(es, 0, sizeof(es));
336
337 strncpy(ss, value, 5);
338 strncpy(es, &(value[6]), 3);
339
340 if (sr_atof_ascii(ss, &s) != SR_OK)
341 return SR_ERR;
342 if (sr_atoi(es, &i) != SR_OK)
343 return SR_ERR;
344
345 /* Transform e.g. 10^-03 to 1000 as the array stores the inverse. */
346 f = pow(10, abs(i));
347
348 /* Adjust the significand/factor pair to make sure
349 * that f is a multiple of 1000.
350 */
351 while ((int)fmod(log10(f), 3) > 0) { s *= 10; f *= 10; }
352
353 /* Truncate s to circumvent rounding errors. */
ac10a927 354 s_int = (unsigned int)s;
8ab929d6
SA
355
356 for (i = 0; i < array_len; i++) {
ac10a927 357 if ( (s_int == array[i][0]) && (f == array[i][1]) ) {
8ab929d6
SA
358 *result = i;
359 return SR_OK;
360 }
361 }
362
363 return SR_ERR;
364}
365
366/**
367 * Obtains information about all analog channels from the oscilloscope.
368 * The internal state information is updated accordingly.
369 *
370 * @param scpi An open SCPI connection.
371 * @param config The device's device configuration.
372 * @param state The device's state information.
373 *
374 * @return SR_ERR on error, SR_OK otherwise.
375 */
376static int analog_channel_state_get(struct sr_scpi_dev_inst *scpi,
ac10a927
SA
377 struct scope_config *config,
378 struct scope_state *state)
8ab929d6
SA
379{
380 int i, j;
381 gchar *response;
382
383 for (i = 0; i < config->analog_channels; ++i) {
384
385 if (dlm_analog_chan_state_get(scpi, i + 1,
ac10a927 386 &state->analog_states[i].state) != SR_OK)
8ab929d6
SA
387 return SR_ERR;
388
389 if (dlm_analog_chan_vdiv_get(scpi, i + 1, &response) != SR_OK)
390 return SR_ERR;
391
392 if (array_float_get(response, *config->vdivs, config->num_vdivs,
ac10a927 393 &j) != SR_OK) {
8ab929d6
SA
394 g_free(response);
395 return SR_ERR;
396 }
397
398 g_free(response);
399 state->analog_states[i].vdiv = j;
400
401 if (dlm_analog_chan_voffs_get(scpi, i + 1,
ac10a927 402 &state->analog_states[i].vertical_offset) != SR_OK)
8ab929d6
SA
403 return SR_ERR;
404
405 if (dlm_analog_chan_wrange_get(scpi, i + 1,
ac10a927 406 &state->analog_states[i].waveform_range) != SR_OK)
8ab929d6
SA
407 return SR_ERR;
408
409 if (dlm_analog_chan_woffs_get(scpi, i + 1,
ac10a927 410 &state->analog_states[i].waveform_offset) != SR_OK)
8ab929d6
SA
411 return SR_ERR;
412
413 if (dlm_analog_chan_coupl_get(scpi, i + 1, &response) != SR_OK) {
414 g_free(response);
415 return SR_ERR;
416 }
417
418 if (array_option_get(response, config->coupling_options,
ac10a927 419 &state->analog_states[i].coupling) != SR_OK) {
8ab929d6
SA
420 g_free(response);
421 return SR_ERR;
422 }
423 g_free(response);
424 }
425
426 return SR_OK;
427}
428
429/**
430 * Obtains information about all digital channels from the oscilloscope.
431 * The internal state information is updated accordingly.
432 *
433 * @param scpi An open SCPI connection.
434 * @param config The device's device configuration.
435 * @param state The device's state information.
436 *
437 * @return SR_ERR on error, SR_OK otherwise.
438 */
439static int digital_channel_state_get(struct sr_scpi_dev_inst *scpi,
ac10a927
SA
440 struct scope_config *config,
441 struct scope_state *state)
8ab929d6
SA
442{
443 unsigned int i;
444
445 if (!config->digital_channels)
446 {
447 sr_warn("Tried obtaining digital channel states on a " \
ac10a927 448 "model without digital inputs.");
8ab929d6
SA
449 return SR_OK;
450 }
451
452 for (i = 0; i < config->digital_channels; ++i) {
453 if (dlm_digital_chan_state_get(scpi, i + 1,
454 &state->digital_states[i]) != SR_OK) {
455 return SR_ERR;
456 }
457 }
458
459 if (!config->pods)
460 {
461 sr_warn("Tried obtaining pod states on a model without pods.");
462 return SR_OK;
463 }
464
465 for (i = 0; i < config->pods; ++i) {
466 if (dlm_digital_pod_state_get(scpi, i + 'A',
467 &state->pod_states[i]) != SR_OK)
468 return SR_ERR;
469 }
470
471 return SR_OK;
472}
473
474/**
475 * Obtains information about the sample rate from the oscilloscope.
476 * The internal state information is updated accordingly.
477 *
478 * @param sdi The device instance.
479 *
480 * @return SR_ERR on error, SR_OK otherwise.
481 */
482SR_PRIV int dlm_sample_rate_query(const struct sr_dev_inst *sdi)
483{
484 struct dev_context *devc;
485 struct scope_state *state;
486 float tmp_float;
487
488 devc = sdi->priv;
489 state = devc->model_state;
490
491 /* No need to find an active channel to query the sample rate:
492 * querying any channel will do, so we use channel 1 all the time.
493 */
494 if (dlm_analog_chan_srate_get(sdi->conn, 1, &tmp_float) != SR_OK)
495 return SR_ERR;
496
497 state->sample_rate = tmp_float;
498
499 return SR_OK;
500}
501
502/**
503 * Obtains information about the current device state from the oscilloscope,
504 * including all analog and digital channel configurations.
505 * The internal state information is updated accordingly.
506 *
507 * @param sdi The device instance.
508 *
509 * @return SR_ERR on error, SR_OK otherwise.
510 */
511SR_PRIV int dlm_scope_state_query(struct sr_dev_inst *sdi)
512{
513 struct dev_context *devc;
514 struct scope_state *state;
515 struct scope_config *config;
516 float tmp_float;
517 gchar *response;
518 int i;
519
520 devc = sdi->priv;
521 config = devc->model_config;
522 state = devc->model_state;
523
524 if (analog_channel_state_get(sdi->conn, config, state) != SR_OK)
525 return SR_ERR;
526
527 if (digital_channel_state_get(sdi->conn, config, state) != SR_OK)
528 return SR_ERR;
529
530 if (dlm_timebase_get(sdi->conn, &response) != SR_OK)
531 return SR_ERR;
532
533 if (array_float_get(response, *config->timebases,
ac10a927 534 config->num_timebases, &i) != SR_OK) {
8ab929d6
SA
535 g_free(response);
536 return SR_ERR;
537 }
538
539 g_free(response);
540 state->timebase = i;
541
542 if (dlm_horiz_trigger_pos_get(sdi->conn, &tmp_float) != SR_OK)
543 return SR_ERR;
544
545 /* TODO: Check if the calculation makes sense for the DLM. */
546 state->horiz_triggerpos = tmp_float /
ac10a927
SA
547 (((double)(*config->timebases)[state->timebase][0] /
548 (*config->timebases)[state->timebase][1]) * config->num_xdivs);
8ab929d6
SA
549 state->horiz_triggerpos -= 0.5;
550 state->horiz_triggerpos *= -1;
551
552 if (dlm_trigger_source_get(sdi->conn, &response) != SR_OK) {
553 g_free(response);
554 return SR_ERR;
555 }
556
557 if (array_option_get(response, config->trigger_sources,
ac10a927 558 &state->trigger_source) != SR_OK) {
8ab929d6
SA
559 g_free(response);
560 return SR_ERR;
561 }
562
563 g_free(response);
564
565 if (dlm_trigger_slope_get(sdi->conn, &i) != SR_OK)
566 return SR_ERR;
567
568 state->trigger_slope = i;
569
af3487ec
SA
570 if (dlm_acq_length_get(sdi->conn, &state->samples_per_frame) != SR_OK) {
571 sr_err("Failed to query acquisition length.");
572 return SR_ERR;
573 }
574
8ab929d6
SA
575 dlm_sample_rate_query(sdi);
576
577 scope_state_dump(config, state);
578
579 return SR_OK;
580}
581
582/**
583 * Creates a new device state structure.
584 *
585 * @param config The device configuration to use.
586 *
ac10a927 587 * @return The newly allocated scope_state struct.
8ab929d6
SA
588 */
589static struct scope_state *dlm_scope_state_new(struct scope_config *config)
590{
591 struct scope_state *state;
592
ac10a927 593 state = g_malloc0(sizeof(struct scope_state));
8ab929d6
SA
594
595 state->analog_states = g_malloc0(config->analog_channels *
ac10a927 596 sizeof(struct analog_channel_state));
8ab929d6
SA
597
598 state->digital_states = g_malloc0(config->digital_channels *
ac10a927 599 sizeof(gboolean));
8ab929d6
SA
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.",
ac10a927 640 model_id);
8ab929d6
SA
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*) *
ac10a927 663 scope_models[model_index].analog_channels);
8ab929d6
SA
664
665 devc->digital_groups = g_malloc0(sizeof(struct sr_channel_group*) *
ac10a927 666 scope_models[model_index].digital_channels);
8ab929d6
SA
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(
ac10a927 678 (char *)(*scope_models[model_index].analog_names)[i]);
8ab929d6
SA
679 devc->analog_groups[i]->channels = g_slist_append(NULL, ch);
680
681 sdi->channel_groups = g_slist_append(sdi->channel_groups,
ac10a927 682 devc->analog_groups[i]);
8ab929d6
SA
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(
ac10a927 706 devc->digital_groups[i / 8]->channels, ch);
8ab929d6
SA
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,
ac10a927
SA
792 struct analog_channel_state *ch_state,
793 struct sr_dev_inst *sdi)
8ab929d6 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 /
ac10a927 824 DLM_DIVISION_FOR_BYTE_FORMAT) + offset;
8ab929d6
SA
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,
ac10a927 855 struct sr_dev_inst *sdi)
8ab929d6 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),
ac10a927 926 16 + model_state->samples_per_frame);
af3487ec
SA
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,
ac10a927 933 RECEIVE_BUFFER_SIZE);
af3487ec
SA
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. " \
ac10a927
SA
960 "Live mode not supported yet, stopping " \
961 "acquisition and retrying.");
af3487ec
SA
962 /* Don't care about return value here. */
963 dlm_acquisition_stop(sdi->conn);
964 g_array_free(data, TRUE);
0028d5a1 965 dlm_channel_data_request(sdi);
af3487ec
SA
966 return TRUE;
967 }
8ab929d6 968
af3487ec
SA
969 ch = devc->current_channel->data;
970 switch (ch->type) {
971 case SR_CHANNEL_ANALOG:
972 if (dlm_analog_samples_send(data,
973 &model_state->analog_states[ch->index],
974 sdi) != SR_OK)
8ab929d6 975 goto fail;
af3487ec
SA
976 break;
977 case SR_CHANNEL_LOGIC:
978 if (dlm_digital_samples_send(data, sdi) != SR_OK)
979 goto fail;
980 break;
981 default:
982 sr_err("Invalid channel type encountered.");
983 break;
984 }
8ab929d6 985
af3487ec
SA
986 g_array_free(data, TRUE);
987 data = NULL;
8ab929d6 988
af3487ec
SA
989 /* Signal the end of this frame if this was the last enabled channel
990 * and set the next enabled channel. Then, request its data.
991 */
992 if (!devc->current_channel->next) {
993 packet.type = SR_DF_FRAME_END;
994 sr_session_send(sdi, &packet);
995 devc->current_channel = devc->enabled_channels;
996
997 /* As of now we only support importing the current acquisition
998 * data so we're going to stop at this point.
999 */
1000 sdi->driver->dev_acquisition_stop(sdi, cb_data);
1001 return TRUE;
1002 } else
1003 devc->current_channel = devc->current_channel->next;
8ab929d6 1004
af3487ec
SA
1005 if (dlm_channel_data_request(sdi) != SR_OK) {
1006 sr_err("Failed to request aquisition data.");
1007 goto fail;
8ab929d6
SA
1008 }
1009
8ab929d6
SA
1010 return TRUE;
1011
1012fail:
af3487ec 1013 if (data) {
8ab929d6 1014 g_array_free(data, TRUE);
af3487ec
SA
1015 data = NULL;
1016 }
8ab929d6 1017
af3487ec 1018 return FALSE;
8ab929d6 1019}