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