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