]> sigrok.org Git - libsigrok.git/blame - src/hardware/yokogawa-dlm/api.c
yokogawa-dlm: Config get/set/list handler updates
[libsigrok.git] / src / hardware / yokogawa-dlm / api.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
21#include <stdlib.h>
22#include "protocol.h"
23
8ab929d6 24SR_PRIV struct sr_dev_driver yokogawa_dlm_driver_info;
8ab929d6 25
329733d9
UH
26static const char *MANUFACTURER_ID = "YOKOGAWA";
27static const char *MANUFACTURER_NAME = "Yokogawa";
8ab929d6 28
f3c60fb6
SA
29static const uint32_t dlm_scanopts[] = {
30 SR_CONF_CONN,
31};
32
33static const uint32_t dlm_drvopts[] = {
cf0280fa
AJ
34 SR_CONF_LOGIC_ANALYZER,
35 SR_CONF_OSCILLOSCOPE,
36};
37
f3c60fb6
SA
38static const uint32_t dlm_devopts[] = {
39 SR_CONF_LIMIT_FRAMES | SR_CONF_GET | SR_CONF_SET,
40 SR_CONF_SAMPLERATE | SR_CONF_GET,
41 SR_CONF_TIMEBASE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
42 SR_CONF_NUM_HDIV | SR_CONF_GET,
43 SR_CONF_HORIZ_TRIGGERPOS | SR_CONF_GET | SR_CONF_SET,
44 SR_CONF_TRIGGER_SLOPE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
45 SR_CONF_TRIGGER_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
46};
47
48static const uint32_t dlm_analog_devopts[] = {
49 SR_CONF_COUPLING | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
50 SR_CONF_VDIV | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
51 SR_CONF_NUM_VDIV | SR_CONF_GET,
52};
53
54static const uint32_t dlm_digital_devopts[] = {
55};
56
8ab929d6
SA
57enum {
58 CG_INVALID = -1,
59 CG_NONE,
60 CG_ANALOG,
61 CG_DIGITAL,
62};
63
4f840ce9 64static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
8ab929d6
SA
65{
66 return std_init(sr_ctx, di, LOG_PREFIX);
67}
68
69static struct sr_dev_inst *probe_usbtmc_device(struct sr_scpi_dev_inst *scpi)
70{
71 struct sr_dev_inst *sdi;
72 struct dev_context *devc;
73 struct sr_scpi_hw_info *hw_info;
74 char *model_name;
75 int model_index;
76
77 sdi = NULL;
78 devc = NULL;
79 hw_info = NULL;
80
81 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
82 sr_info("Couldn't get IDN response.");
83 goto fail;
84 }
85
86 if (strcmp(hw_info->manufacturer, MANUFACTURER_ID) != 0)
87 goto fail;
88
89 if (dlm_model_get(hw_info->model, &model_name, &model_index) != SR_OK)
90 goto fail;
91
aac29cc1 92 sdi = g_malloc0(sizeof(struct sr_dev_inst));
0af636be
UH
93 sdi->status = SR_ST_ACTIVE;
94 sdi->vendor = g_strdup(MANUFACTURER_NAME);
95 sdi->model = g_strdup(model_name);
96 sdi->version = g_strdup(hw_info->firmware_version);
8ab929d6 97
d1314831
SA
98 sdi->serial_num = g_strdup(hw_info->serial_number);
99
8ab929d6
SA
100 sr_scpi_hw_info_free(hw_info);
101 hw_info = NULL;
102
ac10a927 103 devc = g_malloc0(sizeof(struct dev_context));
8ab929d6 104
4f840ce9 105 sdi->driver = &yokogawa_dlm_driver_info;
8ab929d6
SA
106 sdi->priv = devc;
107 sdi->inst_type = SR_INST_SCPI;
108 sdi->conn = scpi;
109
110 if (dlm_device_init(sdi, model_index) != SR_OK)
111 goto fail;
112
113 sr_scpi_close(sdi->conn);
114
115 sdi->status = SR_ST_INACTIVE;
116 return sdi;
117
118fail:
119 if (hw_info)
120 sr_scpi_hw_info_free(hw_info);
121 if (sdi)
122 sr_dev_inst_free(sdi);
b1f83103 123 g_free(devc);
8ab929d6
SA
124
125 return NULL;
126}
127
4f840ce9 128static GSList *scan(struct sr_dev_driver *di, GSList *options)
8ab929d6
SA
129{
130 return sr_scpi_scan(di->priv, options, probe_usbtmc_device);
131}
132
4f840ce9 133static GSList *dev_list(const struct sr_dev_driver *di)
8ab929d6
SA
134{
135 return ((struct drv_context *)(di->priv))->instances;
136}
137
138static void clear_helper(void *priv)
139{
140 struct dev_context *devc;
141
142 devc = priv;
143
144 dlm_scope_state_destroy(devc->model_state);
145
146 g_free(devc->analog_groups);
147 g_free(devc->digital_groups);
148 g_free(devc);
149}
150
4f840ce9 151static int dev_clear(const struct sr_dev_driver *di)
8ab929d6
SA
152{
153 return std_dev_clear(di, clear_helper);
154}
155
156static int dev_open(struct sr_dev_inst *sdi)
157{
158 if (sdi->status != SR_ST_ACTIVE && sr_scpi_open(sdi->conn) != SR_OK)
159 return SR_ERR;
160
161 if (dlm_scope_state_query(sdi) != SR_OK)
162 return SR_ERR;
163
164 sdi->status = SR_ST_ACTIVE;
165
166 return SR_OK;
167}
168
169static int dev_close(struct sr_dev_inst *sdi)
170{
171 if (sdi->status == SR_ST_INACTIVE)
172 return SR_OK;
173
174 sr_scpi_close(sdi->conn);
175
176 sdi->status = SR_ST_INACTIVE;
177
178 return SR_OK;
179}
180
4f840ce9 181static int cleanup(const struct sr_dev_driver *di)
8ab929d6 182{
4f840ce9 183 dev_clear(di);
8ab929d6
SA
184
185 return SR_OK;
186}
187
188/**
189 * Check which category a given channel group belongs to.
190 *
191 * @param devc Our internal device context.
192 * @param cg The channel group to check.
193 *
194 * @retval CG_NONE cg is NULL
195 * @retval CG_ANALOG cg is an analog group
196 * @retval CG_DIGITAL cg is a digital group
197 * @retval CG_INVALID cg is something else
198 */
199static int check_channel_group(struct dev_context *devc,
ac10a927 200 const struct sr_channel_group *cg)
8ab929d6
SA
201{
202 unsigned int i;
329733d9 203 const struct scope_config *model;
8ab929d6
SA
204
205 model = devc->model_config;
206
207 if (!cg)
208 return CG_NONE;
209
210 for (i = 0; i < model->analog_channels; ++i)
211 if (cg == devc->analog_groups[i])
212 return CG_ANALOG;
213
214 for (i = 0; i < model->pods; ++i)
215 if (cg == devc->digital_groups[i])
216 return CG_DIGITAL;
217
218 sr_err("Invalid channel group specified.");
219 return CG_INVALID;
220}
221
584560f1 222static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
ac10a927 223 const struct sr_channel_group *cg)
8ab929d6
SA
224{
225 int ret, cg_type;
226 unsigned int i;
227 struct dev_context *devc;
329733d9 228 const struct scope_config *model;
8ab929d6
SA
229 struct scope_state *state;
230
231 if (!sdi || !(devc = sdi->priv))
232 return SR_ERR_ARG;
233
234 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
235 return SR_ERR;
236
237 ret = SR_ERR_NA;
238 model = devc->model_config;
239 state = devc->model_state;
240
241 switch (key) {
bf622e6d 242 case SR_CONF_NUM_HDIV:
8ab929d6
SA
243 *data = g_variant_new_int32(model->num_xdivs);
244 ret = SR_OK;
245 break;
246 case SR_CONF_TIMEBASE:
247 *data = g_variant_new("(tt)",
f3c60fb6
SA
248 dlm_timebases[state->timebase][0],
249 dlm_timebases[state->timebase][1]);
8ab929d6
SA
250 ret = SR_OK;
251 break;
252 case SR_CONF_NUM_VDIV:
253 if (cg_type == CG_NONE) {
254 sr_err("No channel group specified.");
255 return SR_ERR_CHANNEL_GROUP;
256 } else if (cg_type == CG_ANALOG) {
257 *data = g_variant_new_int32(model->num_ydivs);
258 ret = SR_OK;
259 break;
260 } else {
261 ret = SR_ERR_NA;
262 }
263 break;
264 case SR_CONF_VDIV:
265 ret = SR_ERR_NA;
266 if (cg_type == CG_NONE) {
267 sr_err("No channel group specified.");
268 return SR_ERR_CHANNEL_GROUP;
269 } else if (cg_type != CG_ANALOG)
270 break;
271
272 for (i = 0; i < model->analog_channels; ++i) {
273 if (cg != devc->analog_groups[i])
274 continue;
275 *data = g_variant_new("(tt)",
f3c60fb6
SA
276 dlm_vdivs[state->analog_states[i].vdiv][0],
277 dlm_vdivs[state->analog_states[i].vdiv][1]);
8ab929d6
SA
278 ret = SR_OK;
279 break;
280 }
281 break;
282 case SR_CONF_TRIGGER_SOURCE:
283 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
284 ret = SR_OK;
285 break;
286 case SR_CONF_TRIGGER_SLOPE:
f3c60fb6 287 *data = g_variant_new_string(dlm_trigger_slopes[state->trigger_slope]);
8ab929d6
SA
288 ret = SR_OK;
289 break;
290 case SR_CONF_HORIZ_TRIGGERPOS:
291 *data = g_variant_new_double(state->horiz_triggerpos);
292 ret = SR_OK;
293 break;
294 case SR_CONF_COUPLING:
295 ret = SR_ERR_NA;
296 if (cg_type == CG_NONE) {
297 sr_err("No channel group specified.");
298 return SR_ERR_CHANNEL_GROUP;
299 } else if (cg_type != CG_ANALOG)
300 break;
301
302 for (i = 0; i < model->analog_channels; ++i) {
303 if (cg != devc->analog_groups[i])
304 continue;
305 *data = g_variant_new_string((*model->coupling_options)[state->analog_states[i].coupling]);
306 ret = SR_OK;
307 break;
308 }
309 break;
310 case SR_CONF_SAMPLERATE:
311 *data = g_variant_new_uint64(state->sample_rate);
312 ret = SR_OK;
313 break;
314 default:
315 ret = SR_ERR_NA;
316 }
317
318 return ret;
319}
320
321static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n)
322{
323 unsigned int i;
324 GVariant *rational[2];
325 GVariantBuilder gvb;
326
327 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
328
329 for (i = 0; i < n; i++) {
330 rational[0] = g_variant_new_uint64((*array)[i][0]);
331 rational[1] = g_variant_new_uint64((*array)[i][1]);
332
333 /* FIXME: Valgrind reports a memory leak here. */
334 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
335 }
336
337 return g_variant_builder_end(&gvb);
338}
339
584560f1 340static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
ac10a927 341 const struct sr_channel_group *cg)
8ab929d6
SA
342{
343 int ret, cg_type;
344 unsigned int i, j;
345 char float_str[30];
346 struct dev_context *devc;
329733d9 347 const struct scope_config *model;
8ab929d6
SA
348 struct scope_state *state;
349 const char *tmp;
350 uint64_t p, q;
351 double tmp_d;
352 gboolean update_sample_rate;
353
354 if (!sdi || !(devc = sdi->priv))
355 return SR_ERR_ARG;
356
357 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
358 return SR_ERR;
359
360 model = devc->model_config;
361 state = devc->model_state;
362 update_sample_rate = FALSE;
363
364 ret = SR_ERR_NA;
365
366 switch (key) {
367 case SR_CONF_LIMIT_FRAMES:
368 devc->frame_limit = g_variant_get_uint64(data);
369 ret = SR_OK;
370 break;
371 case SR_CONF_TRIGGER_SOURCE:
372 tmp = g_variant_get_string(data, NULL);
373 for (i = 0; (*model->trigger_sources)[i]; i++) {
374 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
375 continue;
376 state->trigger_source = i;
377 /* TODO: A and B trigger support possible? */
378 ret = dlm_trigger_source_set(sdi->conn, (*model->trigger_sources)[i]);
379 break;
380 }
381 break;
382 case SR_CONF_VDIV:
383 if (cg_type == CG_NONE) {
384 sr_err("No channel group specified.");
385 return SR_ERR_CHANNEL_GROUP;
386 }
387
388 g_variant_get(data, "(tt)", &p, &q);
389
f3c60fb6
SA
390 for (i = 0; i < ARRAY_SIZE(dlm_vdivs); i++) {
391 if (p != dlm_vdivs[i][0] ||
392 q != dlm_vdivs[i][1])
8ab929d6
SA
393 continue;
394 for (j = 1; j <= model->analog_channels; ++j) {
395 if (cg != devc->analog_groups[j - 1])
396 continue;
397 state->analog_states[j - 1].vdiv = i;
398 g_ascii_formatd(float_str, sizeof(float_str),
399 "%E", (float) p / q);
400 if (dlm_analog_chan_vdiv_set(sdi->conn, j, float_str) != SR_OK ||
ac10a927 401 sr_scpi_get_opc(sdi->conn) != SR_OK)
8ab929d6
SA
402 return SR_ERR;
403
404 break;
405 }
406
407 ret = SR_OK;
408 break;
409 }
410 break;
411 case SR_CONF_TIMEBASE:
412 g_variant_get(data, "(tt)", &p, &q);
413
f3c60fb6
SA
414 for (i = 0; i < ARRAY_SIZE(dlm_timebases); i++) {
415 if (p != dlm_timebases[i][0] ||
416 q != dlm_timebases[i][1])
8ab929d6
SA
417 continue;
418 state->timebase = i;
419 g_ascii_formatd(float_str, sizeof(float_str),
420 "%E", (float) p / q);
421 ret = dlm_timebase_set(sdi->conn, float_str);
422 update_sample_rate = TRUE;
423 break;
424 }
425 break;
426 case SR_CONF_HORIZ_TRIGGERPOS:
427 tmp_d = g_variant_get_double(data);
428
429 /* TODO: Check if the calculation makes sense for the DLM. */
430 if (tmp_d < 0.0 || tmp_d > 1.0)
431 return SR_ERR;
432
433 state->horiz_triggerpos = tmp_d;
434 tmp_d = -(tmp_d - 0.5) *
f3c60fb6
SA
435 ((double) dlm_timebases[state->timebase][0] /
436 dlm_timebases[state->timebase][1])
ac10a927 437 * model->num_xdivs;
8ab929d6
SA
438
439 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
440 ret = dlm_horiz_trigger_pos_set(sdi->conn, float_str);
441 break;
442 case SR_CONF_TRIGGER_SLOPE:
443 tmp = g_variant_get_string(data, NULL);
444
445 if (!tmp || !(tmp[0] == 'f' || tmp[0] == 'r'))
446 return SR_ERR_ARG;
447
448 /* Note: See dlm_trigger_slopes[] in protocol.c. */
449 state->trigger_slope = (tmp[0] == 'r') ?
450 SLOPE_POSITIVE : SLOPE_NEGATIVE;
451
452 ret = dlm_trigger_slope_set(sdi->conn, state->trigger_slope);
453 break;
454 case SR_CONF_COUPLING:
455 if (cg_type == CG_NONE) {
456 sr_err("No channel group specified.");
457 return SR_ERR_CHANNEL_GROUP;
458 }
459
460 tmp = g_variant_get_string(data, NULL);
461
462 for (i = 0; (*model->coupling_options)[i]; i++) {
463 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
464 continue;
465 for (j = 1; j <= model->analog_channels; ++j) {
466 if (cg != devc->analog_groups[j - 1])
467 continue;
468 state->analog_states[j-1].coupling = i;
469
470 if (dlm_analog_chan_coupl_set(sdi->conn, j, tmp) != SR_OK ||
ac10a927 471 sr_scpi_get_opc(sdi->conn) != SR_OK)
8ab929d6
SA
472 return SR_ERR;
473 break;
474 }
475
476 ret = SR_OK;
477 break;
478 }
479 break;
480 default:
481 ret = SR_ERR_NA;
482 break;
483 }
484
485 if (ret == SR_OK)
486 ret = sr_scpi_get_opc(sdi->conn);
487
488 if (ret == SR_OK && update_sample_rate)
489 ret = dlm_sample_rate_query(sdi);
490
491 return ret;
492}
493
584560f1 494static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
ac10a927 495 const struct sr_channel_group *cg)
8ab929d6 496{
cf0280fa
AJ
497 int cg_type = CG_NONE;
498 struct dev_context *devc = NULL;
329733d9 499 const struct scope_config *model = NULL;
8ab929d6 500
f3c60fb6
SA
501 /* SR_CONF_SCAN_OPTIONS is always valid, regardless of sdi or probe group. */
502 if (key == SR_CONF_SCAN_OPTIONS) {
503 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
504 dlm_scanopts, ARRAY_SIZE(dlm_scanopts), sizeof(uint32_t));
505 return SR_OK;
506 }
8ab929d6 507
f3c60fb6
SA
508 /* If sdi is NULL, nothing except SR_CONF_DEVICE_OPTIONS can be provided. */
509 if (key == SR_CONF_DEVICE_OPTIONS && !sdi) {
510 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
511 dlm_drvopts, ARRAY_SIZE(dlm_drvopts), sizeof(uint32_t));
512 return SR_OK;
cf0280fa 513 }
8ab929d6 514
f3c60fb6
SA
515 if (!sdi)
516 return SR_ERR_ARG;
517
518 devc = sdi->priv;
519 model = devc->model_config;
520
521 /* If cg is NULL, only the SR_CONF_DEVICE_OPTIONS that are not
522 * specific to a probe group must be returned. */
523 if (!cg) {
524 switch (key) {
525 case SR_CONF_DEVICE_OPTIONS:
526 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
527 dlm_devopts, ARRAY_SIZE(dlm_devopts), sizeof(uint32_t));
528 return SR_OK;
529 case SR_CONF_TIMEBASE:
530 *data = build_tuples(&dlm_timebases, ARRAY_SIZE(dlm_timebases));
531 return SR_OK;
532 case SR_CONF_TRIGGER_SOURCE:
533 if (!model)
534 return SR_ERR_ARG;
535 *data = g_variant_new_strv(*model->trigger_sources,
536 g_strv_length((char **)*model->trigger_sources));
537 return SR_OK;
538 case SR_CONF_TRIGGER_SLOPE:
539 *data = g_variant_new_strv(dlm_trigger_slopes,
540 g_strv_length((char **)dlm_trigger_slopes));
541 return SR_OK;
542 case SR_CONF_NUM_HDIV:
543 *data = g_variant_new_uint32(ARRAY_SIZE(dlm_timebases));
544 return SR_OK;
545 default:
546 return SR_ERR_NA;
547 }
548 }
549
550 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
551 return SR_ERR;
552
8ab929d6 553 switch (key) {
8ab929d6 554 case SR_CONF_DEVICE_OPTIONS:
f3c60fb6 555 if (cg_type == CG_ANALOG) {
584560f1 556 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
f3c60fb6
SA
557 dlm_analog_devopts, ARRAY_SIZE(dlm_analog_devopts), sizeof(uint32_t));
558 } else if (cg_type == CG_DIGITAL) {
559 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
560 dlm_digital_devopts, ARRAY_SIZE(dlm_digital_devopts), sizeof(uint32_t));
8ab929d6 561 } else {
584560f1
BV
562 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
563 NULL, 0, sizeof(uint32_t));
8ab929d6
SA
564 }
565 break;
566 case SR_CONF_COUPLING:
567 if (cg_type == CG_NONE)
568 return SR_ERR_CHANNEL_GROUP;
569 *data = g_variant_new_strv(*model->coupling_options,
ac10a927 570 g_strv_length((char **)*model->coupling_options));
8ab929d6 571 break;
8ab929d6
SA
572 case SR_CONF_VDIV:
573 if (cg_type == CG_NONE)
574 return SR_ERR_CHANNEL_GROUP;
f3c60fb6 575 *data = build_tuples(&dlm_vdivs, ARRAY_SIZE(dlm_vdivs));
8ab929d6
SA
576 break;
577 default:
578 return SR_ERR_NA;
579 }
580
581 return SR_OK;
582}
583
584static int dlm_check_channels(GSList *channels)
585{
586 GSList *l;
587 struct sr_channel *ch;
588 gboolean enabled_pod1, enabled_chan4;
589
590 enabled_pod1 = enabled_chan4 = FALSE;
591
592 /* Note: On the DLM2000, CH4 and Logic are shared. */
593 /* TODO Handle non-DLM2000 models. */
594 for (l = channels; l; l = l->next) {
595 ch = l->data;
596 switch (ch->type) {
597 case SR_CHANNEL_ANALOG:
598 if (ch->index == 3)
599 enabled_chan4 = TRUE;
600 break;
601 case SR_CHANNEL_LOGIC:
602 enabled_pod1 = TRUE;
603 break;
604 default:
605 return SR_ERR;
606 }
607 }
608
609 if (enabled_pod1 && enabled_chan4)
610 return SR_ERR;
611
612 return SR_OK;
613}
614
615static int dlm_setup_channels(const struct sr_dev_inst *sdi)
616{
617 GSList *l;
618 unsigned int i;
619 gboolean *pod_enabled, setup_changed;
620 struct scope_state *state;
329733d9 621 const struct scope_config *model;
8ab929d6
SA
622 struct sr_channel *ch;
623 struct dev_context *devc;
624 struct sr_scpi_dev_inst *scpi;
625
626 devc = sdi->priv;
627 scpi = sdi->conn;
628 state = devc->model_state;
629 model = devc->model_config;
630 setup_changed = FALSE;
631
ac10a927 632 pod_enabled = g_malloc0(sizeof(gboolean) * model->pods);
8ab929d6
SA
633
634 for (l = sdi->channels; l; l = l->next) {
635 ch = l->data;
636 switch (ch->type) {
637 case SR_CHANNEL_ANALOG:
638 if (ch->enabled == state->analog_states[ch->index].state)
639 break;
640
641 if (dlm_analog_chan_state_set(scpi, ch->index + 1,
ac10a927 642 ch->enabled) != SR_OK)
8ab929d6
SA
643 return SR_ERR;
644
645 state->analog_states[ch->index].state = ch->enabled;
646 setup_changed = TRUE;
647 break;
648 case SR_CHANNEL_LOGIC:
6fd78a9f 649 i = ch->index - DLM_DIG_CHAN_INDEX_OFFS;
8ab929d6 650 if (ch->enabled)
6fd78a9f 651 pod_enabled[i / 8] = TRUE;
8ab929d6 652
6fd78a9f 653 if (ch->enabled == state->digital_states[i])
8ab929d6
SA
654 break;
655
6fd78a9f 656 if (dlm_digital_chan_state_set(scpi, i + 1,
ac10a927 657 ch->enabled) != SR_OK)
8ab929d6
SA
658 return SR_ERR;
659
6fd78a9f 660 state->digital_states[i] = ch->enabled;
8ab929d6
SA
661 setup_changed = TRUE;
662 break;
663 default:
664 return SR_ERR;
665 }
666 }
667
668 for (i = 1; i <= model->pods; ++i) {
669 if (state->pod_states[i - 1] == pod_enabled[i - 1])
670 continue;
671
672 if (dlm_digital_pod_state_set(scpi, i,
ac10a927 673 pod_enabled[i - 1]) != SR_OK)
8ab929d6
SA
674 return SR_ERR;
675
676 state->pod_states[i - 1] = pod_enabled[i - 1];
677 setup_changed = TRUE;
678 }
679
680 g_free(pod_enabled);
681
682 if (setup_changed && dlm_sample_rate_query(sdi) != SR_OK)
683 return SR_ERR;
684
685 return SR_OK;
686}
687
688static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
689{
690 GSList *l;
691 gboolean digital_added;
692 struct sr_channel *ch;
693 struct dev_context *devc;
694 struct sr_scpi_dev_inst *scpi;
695
696 (void)cb_data;
697
ac10a927
SA
698 if (sdi->status != SR_ST_ACTIVE)
699 return SR_ERR_DEV_CLOSED;
8ab929d6
SA
700
701 scpi = sdi->conn;
702 devc = sdi->priv;
703 digital_added = FALSE;
704
705 g_slist_free(devc->enabled_channels);
706 devc->enabled_channels = NULL;
707
708 for (l = sdi->channels; l; l = l->next) {
709 ch = l->data;
710 if (!ch->enabled)
711 continue;
712 /* Only add a single digital channel. */
713 if (ch->type != SR_CHANNEL_LOGIC || !digital_added) {
714 devc->enabled_channels = g_slist_append(
ac10a927
SA
715 devc->enabled_channels, ch);
716 if (ch->type == SR_CHANNEL_LOGIC)
717 digital_added = TRUE;
8ab929d6
SA
718 }
719 }
720
721 if (!devc->enabled_channels)
722 return SR_ERR;
723
724 if (dlm_check_channels(devc->enabled_channels) != SR_OK) {
725 sr_err("Invalid channel configuration specified!");
726 return SR_ERR_NA;
727 }
728
729 if (dlm_setup_channels(sdi) != SR_OK) {
730 sr_err("Failed to setup channel configuration!");
731 return SR_ERR;
732 }
733
af3487ec
SA
734 /* Request data for the first enabled channel. */
735 devc->current_channel = devc->enabled_channels;
736 dlm_channel_data_request(sdi);
737
0028d5a1
SA
738 /* Call our callback when data comes in or after 5ms. */
739 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 5,
8ab929d6
SA
740 dlm_data_receive, (void *)sdi);
741
742 return SR_OK;
743}
744
745static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
746{
747 struct dev_context *devc;
8ab929d6
SA
748 struct sr_datafeed_packet packet;
749
750 (void)cb_data;
751
752 packet.type = SR_DF_END;
753 packet.payload = NULL;
754 sr_session_send(sdi, &packet);
755
756 if (sdi->status != SR_ST_ACTIVE)
757 return SR_ERR_DEV_CLOSED;
758
759 devc = sdi->priv;
760
761 devc->num_frames = 0;
762 g_slist_free(devc->enabled_channels);
763 devc->enabled_channels = NULL;
af3487ec
SA
764
765 sr_scpi_source_remove(sdi->session, sdi->conn);
8ab929d6
SA
766
767 return SR_OK;
768}
10763937
SA
769
770SR_PRIV struct sr_dev_driver yokogawa_dlm_driver_info = {
771 .name = "yokogawa-dlm",
ac10a927 772 .longname = "Yokogawa DL/DLM",
10763937 773 .api_version = 1,
8ab929d6
SA
774 .init = init,
775 .cleanup = cleanup,
776 .scan = scan,
777 .dev_list = dev_list,
778 .dev_clear = dev_clear,
779 .config_get = config_get,
780 .config_set = config_set,
781 .config_list = config_list,
782 .dev_open = dev_open,
783 .dev_close = dev_close,
784 .dev_acquisition_start = dev_acquisition_start,
785 .dev_acquisition_stop = dev_acquisition_stop,
10763937
SA
786 .priv = NULL,
787};