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