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