]> sigrok.org Git - libsigrok.git/blame - src/hardware/hameg-hmo/api.c
Introduce standard implementation of the dev_list() callback
[libsigrok.git] / src / hardware / hameg-hmo / api.c
CommitLineData
06a3e78a
DJ
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2013 poljar (Damir Jelić) <poljarinho@gmail.com>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
6ec6c43b 20#include <config.h>
13f2b9d7 21#include <stdlib.h>
5a1afc09 22#include "scpi.h"
06a3e78a
DJ
23#include "protocol.h"
24
13f2b9d7
DJ
25#define SERIALCOMM "115200/8n1/flow=1"
26
e9a62139 27SR_PRIV struct sr_dev_driver hameg_hmo_driver_info;
e9a62139
DJ
28
29static const char *manufacturers[] = {
30 "HAMEG",
da1726cc 31 "Rohde&Schwarz",
e9a62139
DJ
32};
33
6ec3ef9b
AJ
34static const uint32_t drvopts[] = {
35 SR_CONF_OSCILLOSCOPE,
36};
37
584560f1 38static const uint32_t scanopts[] = {
13f2b9d7
DJ
39 SR_CONF_CONN,
40 SR_CONF_SERIALCOMM,
41};
42
719eff68 43enum {
61e77667
UH
44 CG_INVALID = -1,
45 CG_NONE,
46 CG_ANALOG,
47 CG_DIGITAL,
719eff68
UH
48};
49
4f840ce9 50static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
06a3e78a
DJ
51{
52 return std_init(sr_ctx, di, LOG_PREFIX);
53}
54
e9a62139
DJ
55static int check_manufacturer(const char *manufacturer)
56{
57 unsigned int i;
58
0a1f7b09 59 for (i = 0; i < ARRAY_SIZE(manufacturers); i++)
e9a62139
DJ
60 if (!strcmp(manufacturer, manufacturers[i]))
61 return SR_OK;
62
63 return SR_ERR;
64}
65
ca28abd6 66static struct sr_dev_inst *hmo_probe_serial_device(struct sr_scpi_dev_inst *scpi)
e9a62139
DJ
67{
68 struct sr_dev_inst *sdi;
69 struct dev_context *devc;
70 struct sr_scpi_hw_info *hw_info;
e9a62139
DJ
71
72 sdi = NULL;
73 devc = NULL;
e9a62139
DJ
74 hw_info = NULL;
75
e9a62139
DJ
76 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
77 sr_info("Couldn't get IDN response.");
78 goto fail;
79 }
80
81 if (check_manufacturer(hw_info->manufacturer) != SR_OK)
82 goto fail;
83
aac29cc1 84 sdi = g_malloc0(sizeof(struct sr_dev_inst));
0af636be
UH
85 sdi->vendor = g_strdup(hw_info->manufacturer);
86 sdi->model = g_strdup(hw_info->model);
87 sdi->version = g_strdup(hw_info->firmware_version);
88 sdi->serial_num = g_strdup(hw_info->serial_number);
4f840ce9 89 sdi->driver = &hameg_hmo_driver_info;
b33db61c
SA
90 sdi->inst_type = SR_INST_SCPI;
91 sdi->conn = scpi;
b33db61c 92
e9a62139
DJ
93 sr_scpi_hw_info_free(hw_info);
94 hw_info = NULL;
95
f57d8ffe 96 devc = g_malloc0(sizeof(struct dev_context));
e9a62139 97
e9a62139 98 sdi->priv = devc;
e9a62139
DJ
99
100 if (hmo_init_device(sdi) != SR_OK)
101 goto fail;
102
103 return sdi;
104
105fail:
106 if (hw_info)
107 sr_scpi_hw_info_free(hw_info);
e9a62139
DJ
108 if (sdi)
109 sr_dev_inst_free(sdi);
b1f83103 110 g_free(devc);
e9a62139
DJ
111
112 return NULL;
113}
114
4f840ce9 115static GSList *scan(struct sr_dev_driver *di, GSList *options)
06a3e78a 116{
41812aca 117 return sr_scpi_scan(di->context, options, hmo_probe_serial_device);
06a3e78a
DJ
118}
119
13f2b9d7
DJ
120static void clear_helper(void *priv)
121{
13f2b9d7 122 struct dev_context *devc;
13f2b9d7
DJ
123
124 devc = priv;
13f2b9d7 125
719eff68 126 hmo_scope_state_free(devc->model_state);
13f2b9d7 127
13f2b9d7
DJ
128 g_free(devc->analog_groups);
129 g_free(devc->digital_groups);
130
131 g_free(devc);
132}
133
4f840ce9 134static int dev_clear(const struct sr_dev_driver *di)
06a3e78a 135{
13f2b9d7 136 return std_dev_clear(di, clear_helper);
06a3e78a
DJ
137}
138
139static int dev_open(struct sr_dev_inst *sdi)
140{
23f43dff 141 if (sdi->status != SR_ST_ACTIVE && sr_scpi_open(sdi->conn) != SR_OK)
13f2b9d7 142 return SR_ERR;
06a3e78a 143
719eff68 144 if (hmo_scope_state_get(sdi) != SR_OK)
13f2b9d7 145 return SR_ERR;
06a3e78a
DJ
146
147 sdi->status = SR_ST_ACTIVE;
148
149 return SR_OK;
150}
151
152static int dev_close(struct sr_dev_inst *sdi)
153{
13f2b9d7
DJ
154 if (sdi->status == SR_ST_INACTIVE)
155 return SR_OK;
06a3e78a 156
23f43dff 157 sr_scpi_close(sdi->conn);
06a3e78a
DJ
158
159 sdi->status = SR_ST_INACTIVE;
160
161 return SR_OK;
162}
163
660e398f 164static int check_channel_group(struct dev_context *devc,
53b4680f 165 const struct sr_channel_group *cg)
13f2b9d7
DJ
166{
167 unsigned int i;
329733d9 168 const struct scope_config *model;
13f2b9d7
DJ
169
170 model = devc->model_config;
171
53b4680f 172 if (!cg)
61e77667 173 return CG_NONE;
13f2b9d7 174
0a1f7b09 175 for (i = 0; i < model->analog_channels; i++)
562b7ae5 176 if (cg == devc->analog_groups[i])
61e77667 177 return CG_ANALOG;
13f2b9d7 178
0a1f7b09 179 for (i = 0; i < model->digital_pods; i++)
562b7ae5 180 if (cg == devc->digital_groups[i])
61e77667 181 return CG_DIGITAL;
13f2b9d7 182
660e398f 183 sr_err("Invalid channel group specified.");
89280b1a 184
61e77667 185 return CG_INVALID;
13f2b9d7
DJ
186}
187
584560f1 188static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 189 const struct sr_channel_group *cg)
06a3e78a 190{
61e77667 191 int ret, cg_type;
13f2b9d7 192 unsigned int i;
13f2b9d7 193 struct dev_context *devc;
329733d9 194 const struct scope_config *model;
14a2f74d 195 struct scope_state *state;
13f2b9d7
DJ
196
197 if (!sdi || !(devc = sdi->priv))
198 return SR_ERR_ARG;
199
61e77667 200 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
13f2b9d7 201 return SR_ERR;
06a3e78a 202
13f2b9d7
DJ
203 ret = SR_ERR_NA;
204 model = devc->model_config;
14a2f74d 205 state = devc->model_state;
06a3e78a 206
06a3e78a 207 switch (key) {
bf622e6d 208 case SR_CONF_NUM_HDIV:
13f2b9d7
DJ
209 *data = g_variant_new_int32(model->num_xdivs);
210 ret = SR_OK;
211 break;
3fd2dca2
DJ
212 case SR_CONF_TIMEBASE:
213 *data = g_variant_new("(tt)", (*model->timebases)[state->timebase][0],
214 (*model->timebases)[state->timebase][1]);
215 ret = SR_OK;
216 break;
13f2b9d7 217 case SR_CONF_NUM_VDIV:
61e77667 218 if (cg_type == CG_NONE) {
660e398f
UH
219 sr_err("No channel group specified.");
220 return SR_ERR_CHANNEL_GROUP;
61e77667 221 } else if (cg_type == CG_ANALOG) {
0a1f7b09 222 for (i = 0; i < model->analog_channels; i++) {
562b7ae5 223 if (cg != devc->analog_groups[i])
082972e8
UH
224 continue;
225 *data = g_variant_new_int32(model->num_ydivs);
226 ret = SR_OK;
227 break;
13f2b9d7
DJ
228 }
229
3fd2dca2
DJ
230 } else {
231 ret = SR_ERR_NA;
232 }
233 break;
234 case SR_CONF_VDIV:
61e77667
UH
235 if (cg_type == CG_NONE) {
236 sr_err("No channel group specified.");
237 return SR_ERR_CHANNEL_GROUP;
238 } else if (cg_type == CG_ANALOG) {
0a1f7b09 239 for (i = 0; i < model->analog_channels; i++) {
562b7ae5 240 if (cg != devc->analog_groups[i])
3fd2dca2
DJ
241 continue;
242 *data = g_variant_new("(tt)",
243 (*model->vdivs)[state->analog_channels[i].vdiv][0],
244 (*model->vdivs)[state->analog_channels[i].vdiv][1]);
245 ret = SR_OK;
246 break;
247 }
248
ccf14618
DJ
249 } else {
250 ret = SR_ERR_NA;
251 }
252 break;
253 case SR_CONF_TRIGGER_SOURCE:
254 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
255 ret = SR_OK;
256 break;
3fd2dca2
DJ
257 case SR_CONF_TRIGGER_SLOPE:
258 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
259 ret = SR_OK;
260 break;
261 case SR_CONF_HORIZ_TRIGGERPOS:
262 *data = g_variant_new_double(state->horiz_triggerpos);
263 ret = SR_OK;
264 break;
ccf14618 265 case SR_CONF_COUPLING:
61e77667 266 if (cg_type == CG_NONE) {
660e398f
UH
267 sr_err("No channel group specified.");
268 return SR_ERR_CHANNEL_GROUP;
61e77667 269 } else if (cg_type == CG_ANALOG) {
0a1f7b09 270 for (i = 0; i < model->analog_channels; i++) {
562b7ae5 271 if (cg != devc->analog_groups[i])
ccf14618
DJ
272 continue;
273 *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[i].coupling]);
274 ret = SR_OK;
275 break;
276 }
277
13f2b9d7
DJ
278 } else {
279 ret = SR_ERR_NA;
280 }
281 break;
14a2f74d
DJ
282 case SR_CONF_SAMPLERATE:
283 *data = g_variant_new_uint64(state->sample_rate);
284 ret = SR_OK;
285 break;
06a3e78a 286 default:
13f2b9d7 287 ret = SR_ERR_NA;
06a3e78a
DJ
288 }
289
290 return ret;
291}
292
13f2b9d7
DJ
293static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n)
294{
295 unsigned int i;
13f2b9d7
DJ
296 GVariant *rational[2];
297 GVariantBuilder gvb;
298
299 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
300
301 for (i = 0; i < n; i++) {
302 rational[0] = g_variant_new_uint64((*array)[i][0]);
303 rational[1] = g_variant_new_uint64((*array)[i][1]);
304
89280b1a 305 /* FIXME: Valgrind reports a memory leak here. */
13f2b9d7
DJ
306 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
307 }
308
309 return g_variant_builder_end(&gvb);
310}
311
584560f1 312static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
53b4680f 313 const struct sr_channel_group *cg)
06a3e78a 314{
61e77667 315 int ret, cg_type;
89280b1a 316 unsigned int i, j;
965b463d 317 char command[MAX_COMMAND_SIZE], float_str[30];
13f2b9d7 318 struct dev_context *devc;
329733d9 319 const struct scope_config *model;
13f2b9d7 320 struct scope_state *state;
89280b1a 321 const char *tmp;
ca9b9f48 322 uint64_t p, q;
89280b1a 323 double tmp_d;
23e1ea7a 324 gboolean update_sample_rate;
06a3e78a 325
13f2b9d7
DJ
326 if (!sdi || !(devc = sdi->priv))
327 return SR_ERR_ARG;
328
61e77667 329 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
13f2b9d7
DJ
330 return SR_ERR;
331
332 model = devc->model_config;
333 state = devc->model_state;
23e1ea7a 334 update_sample_rate = FALSE;
13f2b9d7
DJ
335
336 ret = SR_ERR_NA;
06a3e78a 337
06a3e78a 338 switch (key) {
13f2b9d7
DJ
339 case SR_CONF_LIMIT_FRAMES:
340 devc->frame_limit = g_variant_get_uint64(data);
341 ret = SR_OK;
342 break;
13f2b9d7 343 case SR_CONF_TRIGGER_SOURCE:
13f2b9d7 344 tmp = g_variant_get_string(data, NULL);
13f2b9d7 345 for (i = 0; (*model->trigger_sources)[i]; i++) {
082972e8
UH
346 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
347 continue;
348 state->trigger_source = i;
349 g_snprintf(command, sizeof(command),
350 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
351 (*model->trigger_sources)[i]);
13f2b9d7 352
082972e8
UH
353 ret = sr_scpi_send(sdi->conn, command);
354 break;
13f2b9d7 355 }
89280b1a 356 break;
13f2b9d7 357 case SR_CONF_VDIV:
61e77667 358 if (cg_type == CG_NONE) {
660e398f
UH
359 sr_err("No channel group specified.");
360 return SR_ERR_CHANNEL_GROUP;
13f2b9d7
DJ
361 }
362
363 g_variant_get(data, "(tt)", &p, &q);
364
365 for (i = 0; i < model->num_vdivs; i++) {
082972e8
UH
366 if (p != (*model->vdivs)[i][0] ||
367 q != (*model->vdivs)[i][1])
368 continue;
0a1f7b09 369 for (j = 1; j <= model->analog_channels; j++) {
562b7ae5 370 if (cg != devc->analog_groups[j - 1])
082972e8 371 continue;
8de2dc3b 372 state->analog_channels[j - 1].vdiv = i;
965b463d 373 g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
082972e8
UH
374 g_snprintf(command, sizeof(command),
375 (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV],
965b463d 376 j, float_str);
082972e8
UH
377
378 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
379 sr_scpi_get_opc(sdi->conn) != SR_OK)
380 return SR_ERR;
13f2b9d7 381
13f2b9d7
DJ
382 break;
383 }
082972e8
UH
384
385 ret = SR_OK;
386 break;
13f2b9d7 387 }
89280b1a 388 break;
13f2b9d7 389 case SR_CONF_TIMEBASE:
13f2b9d7
DJ
390 g_variant_get(data, "(tt)", &p, &q);
391
392 for (i = 0; i < model->num_timebases; i++) {
082972e8
UH
393 if (p != (*model->timebases)[i][0] ||
394 q != (*model->timebases)[i][1])
395 continue;
8de2dc3b 396 state->timebase = i;
965b463d 397 g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
082972e8
UH
398 g_snprintf(command, sizeof(command),
399 (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE],
965b463d 400 float_str);
13f2b9d7 401
082972e8 402 ret = sr_scpi_send(sdi->conn, command);
23e1ea7a 403 update_sample_rate = TRUE;
082972e8 404 break;
13f2b9d7 405 }
89280b1a 406 break;
13f2b9d7 407 case SR_CONF_HORIZ_TRIGGERPOS:
89280b1a 408 tmp_d = g_variant_get_double(data);
13f2b9d7 409
89280b1a 410 if (tmp_d < 0.0 || tmp_d > 1.0)
13f2b9d7
DJ
411 return SR_ERR;
412
422a1c0d
DJ
413 state->horiz_triggerpos = tmp_d;
414 tmp_d = -(tmp_d - 0.5) *
415 ((double) (*model->timebases)[state->timebase][0] /
416 (*model->timebases)[state->timebase][1])
417 * model->num_xdivs;
418
419 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
13f2b9d7
DJ
420 g_snprintf(command, sizeof(command),
421 (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS],
422a1c0d 422 float_str);
13f2b9d7
DJ
423
424 ret = sr_scpi_send(sdi->conn, command);
89280b1a 425 break;
13f2b9d7 426 case SR_CONF_TRIGGER_SLOPE:
ca9b9f48 427 tmp = g_variant_get_string(data, NULL);
e3abd15d
SB
428 for (i = 0; (*model->trigger_slopes)[i]; i++) {
429 if (g_strcmp0(tmp, (*model->trigger_slopes)[i]) != 0)
430 continue;
431 state->trigger_slope = i;
432 g_snprintf(command, sizeof(command),
433 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
434 (*model->trigger_slopes)[i]);
13f2b9d7 435
e3abd15d
SB
436 ret = sr_scpi_send(sdi->conn, command);
437 break;
438 }
89280b1a 439 break;
13f2b9d7 440 case SR_CONF_COUPLING:
61e77667 441 if (cg_type == CG_NONE) {
660e398f
UH
442 sr_err("No channel group specified.");
443 return SR_ERR_CHANNEL_GROUP;
13f2b9d7
DJ
444 }
445
446 tmp = g_variant_get_string(data, NULL);
447
448 for (i = 0; (*model->coupling_options)[i]; i++) {
082972e8
UH
449 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
450 continue;
0a1f7b09 451 for (j = 1; j <= model->analog_channels; j++) {
562b7ae5 452 if (cg != devc->analog_groups[j - 1])
082972e8
UH
453 continue;
454 state->analog_channels[j-1].coupling = i;
13f2b9d7 455
082972e8
UH
456 g_snprintf(command, sizeof(command),
457 (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
458 j, tmp);
459
460 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
461 sr_scpi_get_opc(sdi->conn) != SR_OK)
462 return SR_ERR;
13f2b9d7
DJ
463 break;
464 }
082972e8
UH
465
466 ret = SR_OK;
467 break;
13f2b9d7 468 }
89280b1a 469 break;
06a3e78a
DJ
470 default:
471 ret = SR_ERR_NA;
13f2b9d7 472 break;
06a3e78a
DJ
473 }
474
13f2b9d7
DJ
475 if (ret == SR_OK)
476 ret = sr_scpi_get_opc(sdi->conn);
477
23e1ea7a
DJ
478 if (ret == SR_OK && update_sample_rate)
479 ret = hmo_update_sample_rate(sdi);
480
06a3e78a
DJ
481 return ret;
482}
483
584560f1 484static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 485 const struct sr_channel_group *cg)
06a3e78a 486{
6ec3ef9b
AJ
487 int cg_type = CG_NONE;
488 struct dev_context *devc = NULL;
329733d9 489 const struct scope_config *model = NULL;
13f2b9d7 490
6ec3ef9b
AJ
491 if (sdi && (devc = sdi->priv)) {
492 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
493 return SR_ERR;
13f2b9d7 494
6ec3ef9b
AJ
495 model = devc->model_config;
496 }
06a3e78a 497
06a3e78a 498 switch (key) {
226363c4 499 case SR_CONF_SCAN_OPTIONS:
584560f1
BV
500 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
501 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
226363c4 502 break;
13f2b9d7 503 case SR_CONF_DEVICE_OPTIONS:
61e77667 504 if (cg_type == CG_NONE) {
6ec3ef9b
AJ
505 if (model)
506 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
507 model->devopts, model->num_devopts, sizeof(uint32_t));
508 else
509 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
510 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
61e77667 511 } else if (cg_type == CG_ANALOG) {
584560f1 512 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
f254bc4b 513 model->analog_devopts, model->num_analog_devopts,
584560f1 514 sizeof(uint32_t));
13f2b9d7 515 } else {
584560f1
BV
516 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
517 NULL, 0, sizeof(uint32_t));
13f2b9d7
DJ
518 }
519 break;
13f2b9d7 520 case SR_CONF_COUPLING:
61e77667 521 if (cg_type == CG_NONE)
660e398f 522 return SR_ERR_CHANNEL_GROUP;
13f2b9d7 523 *data = g_variant_new_strv(*model->coupling_options,
89280b1a 524 g_strv_length((char **)*model->coupling_options));
13f2b9d7 525 break;
13f2b9d7 526 case SR_CONF_TRIGGER_SOURCE:
6ec3ef9b
AJ
527 if (!model)
528 return SR_ERR_ARG;
13f2b9d7 529 *data = g_variant_new_strv(*model->trigger_sources,
89280b1a 530 g_strv_length((char **)*model->trigger_sources));
13f2b9d7 531 break;
bbabdaf1 532 case SR_CONF_TRIGGER_SLOPE:
6ec3ef9b
AJ
533 if (!model)
534 return SR_ERR_ARG;
bbabdaf1
DJ
535 *data = g_variant_new_strv(*model->trigger_slopes,
536 g_strv_length((char **)*model->trigger_slopes));
537 break;
13f2b9d7 538 case SR_CONF_TIMEBASE:
6ec3ef9b
AJ
539 if (!model)
540 return SR_ERR_ARG;
13f2b9d7
DJ
541 *data = build_tuples(model->timebases, model->num_timebases);
542 break;
13f2b9d7 543 case SR_CONF_VDIV:
61e77667 544 if (cg_type == CG_NONE)
660e398f 545 return SR_ERR_CHANNEL_GROUP;
13f2b9d7
DJ
546 *data = build_tuples(model->vdivs, model->num_vdivs);
547 break;
06a3e78a
DJ
548 default:
549 return SR_ERR_NA;
550 }
551
13f2b9d7 552 return SR_OK;
06a3e78a
DJ
553}
554
13f2b9d7 555SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi)
06a3e78a 556{
13f2b9d7 557 char command[MAX_COMMAND_SIZE];
ba7dd8bb 558 struct sr_channel *ch;
13f2b9d7 559 struct dev_context *devc;
329733d9 560 const struct scope_config *model;
13f2b9d7
DJ
561
562 devc = sdi->priv;
563 model = devc->model_config;
564
ba7dd8bb 565 ch = devc->current_channel->data;
13f2b9d7 566
ba7dd8bb 567 switch (ch->type) {
3f239f08 568 case SR_CHANNEL_ANALOG:
13f2b9d7
DJ
569 g_snprintf(command, sizeof(command),
570 (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA],
ba7dd8bb 571 ch->index + 1);
13f2b9d7 572 break;
3f239f08 573 case SR_CHANNEL_LOGIC:
13f2b9d7
DJ
574 g_snprintf(command, sizeof(command),
575 (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA],
ba7dd8bb 576 ch->index < 8 ? 1 : 2);
13f2b9d7
DJ
577 break;
578 default:
ba7dd8bb 579 sr_err("Invalid channel type.");
13f2b9d7
DJ
580 break;
581 }
582
583 return sr_scpi_send(sdi->conn, command);
584}
585
ba7dd8bb 586static int hmo_check_channels(GSList *channels)
13f2b9d7
DJ
587{
588 GSList *l;
ba7dd8bb 589 struct sr_channel *ch;
89280b1a 590 gboolean enabled_pod1, enabled_pod2, enabled_chan3, enabled_chan4;
13f2b9d7 591
89280b1a 592 enabled_pod1 = enabled_pod2 = enabled_chan3 = enabled_chan4 = FALSE;
13f2b9d7 593
ba7dd8bb
UH
594 for (l = channels; l; l = l->next) {
595 ch = l->data;
596 switch (ch->type) {
3f239f08 597 case SR_CHANNEL_ANALOG:
ba7dd8bb 598 if (ch->index == 2)
13f2b9d7 599 enabled_chan3 = TRUE;
ba7dd8bb 600 else if (ch->index == 3)
13f2b9d7
DJ
601 enabled_chan4 = TRUE;
602 break;
3f239f08 603 case SR_CHANNEL_LOGIC:
ba7dd8bb 604 if (ch->index < 8)
13f2b9d7
DJ
605 enabled_pod1 = TRUE;
606 else
607 enabled_pod2 = TRUE;
608 break;
13f2b9d7
DJ
609 default:
610 return SR_ERR;
611 }
612 }
613
614 if ((enabled_pod1 && enabled_chan3) ||
615 (enabled_pod2 && enabled_chan4))
616 return SR_ERR;
617
618 return SR_OK;
619}
620
ba7dd8bb 621static int hmo_setup_channels(const struct sr_dev_inst *sdi)
13f2b9d7
DJ
622{
623 GSList *l;
624 unsigned int i;
23e1ea7a 625 gboolean *pod_enabled, setup_changed;
13f2b9d7 626 char command[MAX_COMMAND_SIZE];
13f2b9d7 627 struct scope_state *state;
329733d9 628 const struct scope_config *model;
ba7dd8bb 629 struct sr_channel *ch;
13f2b9d7 630 struct dev_context *devc;
23f43dff 631 struct sr_scpi_dev_inst *scpi;
13f2b9d7
DJ
632
633 devc = sdi->priv;
23f43dff 634 scpi = sdi->conn;
13f2b9d7
DJ
635 state = devc->model_state;
636 model = devc->model_config;
23e1ea7a 637 setup_changed = FALSE;
13f2b9d7
DJ
638
639 pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods);
640
ba7dd8bb
UH
641 for (l = sdi->channels; l; l = l->next) {
642 ch = l->data;
643 switch (ch->type) {
3f239f08 644 case SR_CHANNEL_ANALOG:
ba7dd8bb 645 if (ch->enabled == state->analog_channels[ch->index].state)
082972e8
UH
646 break;
647 g_snprintf(command, sizeof(command),
648 (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE],
ba7dd8bb 649 ch->index + 1, ch->enabled);
13f2b9d7 650
23f43dff 651 if (sr_scpi_send(scpi, command) != SR_OK)
082972e8 652 return SR_ERR;
ba7dd8bb 653 state->analog_channels[ch->index].state = ch->enabled;
23e1ea7a 654 setup_changed = TRUE;
89280b1a 655 break;
3f239f08 656 case SR_CHANNEL_LOGIC:
13f2b9d7
DJ
657 /*
658 * A digital POD needs to be enabled for every group of
ba7dd8bb 659 * 8 channels.
13f2b9d7 660 */
ba7dd8bb
UH
661 if (ch->enabled)
662 pod_enabled[ch->index < 8 ? 0 : 1] = TRUE;
13f2b9d7 663
ba7dd8bb 664 if (ch->enabled == state->digital_channels[ch->index])
082972e8
UH
665 break;
666 g_snprintf(command, sizeof(command),
667 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE],
ba7dd8bb 668 ch->index, ch->enabled);
13f2b9d7 669
23f43dff 670 if (sr_scpi_send(scpi, command) != SR_OK)
082972e8 671 return SR_ERR;
13f2b9d7 672
ba7dd8bb 673 state->digital_channels[ch->index] = ch->enabled;
23e1ea7a 674 setup_changed = TRUE;
89280b1a 675 break;
13f2b9d7
DJ
676 default:
677 return SR_ERR;
678 }
679 }
680
0a1f7b09 681 for (i = 1; i <= model->digital_pods; i++) {
082972e8
UH
682 if (state->digital_pods[i - 1] == pod_enabled[i - 1])
683 continue;
684 g_snprintf(command, sizeof(command),
685 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE],
686 i, pod_enabled[i - 1]);
23f43dff 687 if (sr_scpi_send(scpi, command) != SR_OK)
082972e8
UH
688 return SR_ERR;
689 state->digital_pods[i - 1] = pod_enabled[i - 1];
23e1ea7a 690 setup_changed = TRUE;
13f2b9d7
DJ
691 }
692
693 g_free(pod_enabled);
694
23e1ea7a
DJ
695 if (setup_changed && hmo_update_sample_rate(sdi) != SR_OK)
696 return SR_ERR;
697
13f2b9d7
DJ
698 return SR_OK;
699}
700
695dc859 701static int dev_acquisition_start(const struct sr_dev_inst *sdi)
13f2b9d7
DJ
702{
703 GSList *l;
704 gboolean digital_added;
ba7dd8bb 705 struct sr_channel *ch;
13f2b9d7 706 struct dev_context *devc;
23f43dff 707 struct sr_scpi_dev_inst *scpi;
06a3e78a
DJ
708
709 if (sdi->status != SR_ST_ACTIVE)
710 return SR_ERR_DEV_CLOSED;
711
23f43dff 712 scpi = sdi->conn;
13f2b9d7
DJ
713 devc = sdi->priv;
714 digital_added = FALSE;
715
db81fbb5
SA
716 g_slist_free(devc->enabled_channels);
717 devc->enabled_channels = NULL;
718
ba7dd8bb
UH
719 for (l = sdi->channels; l; l = l->next) {
720 ch = l->data;
721 if (!ch->enabled)
082972e8 722 continue;
ba7dd8bb 723 /* Only add a single digital channel. */
3f239f08 724 if (ch->type != SR_CHANNEL_LOGIC || !digital_added) {
ba7dd8bb
UH
725 devc->enabled_channels = g_slist_append(
726 devc->enabled_channels, ch);
3f239f08 727 if (ch->type == SR_CHANNEL_LOGIC)
082972e8 728 digital_added = TRUE;
13f2b9d7
DJ
729 }
730 }
06a3e78a 731
ba7dd8bb 732 if (!devc->enabled_channels)
13f2b9d7
DJ
733 return SR_ERR;
734
ba7dd8bb
UH
735 if (hmo_check_channels(devc->enabled_channels) != SR_OK) {
736 sr_err("Invalid channel configuration specified!");
13f2b9d7
DJ
737 return SR_ERR_NA;
738 }
739
ba7dd8bb
UH
740 if (hmo_setup_channels(sdi) != SR_OK) {
741 sr_err("Failed to setup channel configuration!");
13f2b9d7
DJ
742 return SR_ERR;
743 }
744
102f1239
BV
745 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
746 hmo_receive_data, (void *)sdi);
13f2b9d7 747
695dc859 748 std_session_send_df_header(sdi, LOG_PREFIX);
13f2b9d7 749
ba7dd8bb 750 devc->current_channel = devc->enabled_channels;
13f2b9d7
DJ
751
752 return hmo_request_data(sdi);
06a3e78a
DJ
753}
754
695dc859 755static int dev_acquisition_stop(struct sr_dev_inst *sdi)
06a3e78a 756{
13f2b9d7 757 struct dev_context *devc;
23f43dff 758 struct sr_scpi_dev_inst *scpi;
13f2b9d7 759
3be42bc2 760 std_session_send_df_end(sdi, LOG_PREFIX);
66e3219d 761
06a3e78a
DJ
762 if (sdi->status != SR_ST_ACTIVE)
763 return SR_ERR_DEV_CLOSED;
764
13f2b9d7
DJ
765 devc = sdi->priv;
766
ef1a346b 767 devc->num_frames = 0;
ba7dd8bb
UH
768 g_slist_free(devc->enabled_channels);
769 devc->enabled_channels = NULL;
23f43dff 770 scpi = sdi->conn;
102f1239 771 sr_scpi_source_remove(sdi->session, scpi);
06a3e78a
DJ
772
773 return SR_OK;
774}
775
776SR_PRIV struct sr_dev_driver hameg_hmo_driver_info = {
777 .name = "hameg-hmo",
89280b1a 778 .longname = "Hameg HMO",
06a3e78a
DJ
779 .api_version = 1,
780 .init = init,
700d6b64 781 .cleanup = std_cleanup,
06a3e78a 782 .scan = scan,
c01bf34c 783 .dev_list = std_dev_list,
06a3e78a
DJ
784 .dev_clear = dev_clear,
785 .config_get = config_get,
786 .config_set = config_set,
787 .config_list = config_list,
788 .dev_open = dev_open,
789 .dev_close = dev_close,
790 .dev_acquisition_start = dev_acquisition_start,
791 .dev_acquisition_stop = dev_acquisition_stop,
41812aca 792 .context = NULL,
06a3e78a 793};