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