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