]> sigrok.org Git - libsigrok.git/blame - hardware/hameg-hmo/api.c
hameg-hmo: Send the DF_END packet in acquisition_stop()
[libsigrok.git] / 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
DJ
20#include <stdlib.h>
21#include <glib/gstdio.h>
06a3e78a
DJ
22#include "protocol.h"
23
13f2b9d7
DJ
24#define SERIALCOMM "115200/8n1/flow=1"
25
e9a62139
DJ
26SR_PRIV struct sr_dev_driver hameg_hmo_driver_info;
27static struct sr_dev_driver *di = &hameg_hmo_driver_info;
28
29static const char *manufacturers[] = {
30 "HAMEG",
31};
32
13f2b9d7
DJ
33static const int32_t hwopts[] = {
34 SR_CONF_CONN,
35 SR_CONF_SERIALCOMM,
36};
37
38struct usb_id_info {
39 uint16_t vendor_id;
40 uint16_t product_id;
89280b1a 41};
13f2b9d7
DJ
42
43static struct usb_id_info ho_models[] = {
89280b1a
UH
44 { 0x0403, 0xed72 }, /* HO720 */
45 { 0x0403, 0xed73 }, /* HO730 */
13f2b9d7 46};
06a3e78a 47
719eff68
UH
48enum {
49 PG_INVALID = -1,
50 PG_NONE,
51 PG_ANALOG,
52 PG_DIGITAL,
53};
54
06a3e78a
DJ
55static int init(struct sr_context *sr_ctx)
56{
57 return std_init(sr_ctx, di, LOG_PREFIX);
58}
59
13f2b9d7
DJ
60/**
61 * Find USB serial devices via the USB vendor ID and product ID.
62 *
89280b1a
UH
63 * @param vendor_id Vendor ID of the USB device.
64 * @param product_id Product ID of the USB device.
13f2b9d7 65 *
89280b1a
UH
66 * @return A GSList of strings containing the path of the serial device or
67 * NULL if no serial device is found. The returned list must be freed
68 * by the caller.
13f2b9d7 69 */
89280b1a 70static GSList *auto_find_usb(uint16_t vendor_id, uint16_t product_id)
13f2b9d7
DJ
71{
72#ifdef __linux__
73 const gchar *usb_dev;
74 const char device_tree[] = "/sys/bus/usb/devices/";
89280b1a 75 GDir *devices_dir, *device_dir;
13f2b9d7 76 GSList *l = NULL;
89280b1a 77 GSList *tty_devs;
13f2b9d7 78 GSList *matched_paths;
89280b1a
UH
79 FILE *fd;
80 char tmp[5];
81 gchar *vendor_path, *product_path, *path_copy;
82 gchar *prefix, *subdir_path, *device_path, *tty_path;
83 unsigned long read_vendor_id, read_product_id;
84 const char *file;
13f2b9d7
DJ
85
86 l = NULL;
89280b1a 87 tty_devs = NULL;
13f2b9d7
DJ
88 matched_paths = NULL;
89
90 if (!(devices_dir = g_dir_open(device_tree, 0, NULL)))
91 return NULL;
92
93 /*
94 * Find potential candidates using the vendor ID and product ID
89280b1a 95 * and store them in matched_paths.
13f2b9d7
DJ
96 */
97 while ((usb_dev = g_dir_read_name(devices_dir))) {
13f2b9d7
DJ
98 vendor_path = g_strconcat(device_tree,
99 usb_dev, "/idVendor", NULL);
100 product_path = g_strconcat(device_tree,
101 usb_dev, "/idProduct", NULL);
102
103 if (!g_file_test(vendor_path, G_FILE_TEST_EXISTS) ||
104 !g_file_test(product_path, G_FILE_TEST_EXISTS))
105 goto skip_device;
106
107 if ((fd = g_fopen(vendor_path, "r")) == NULL)
108 goto skip_device;
109
110 if (fgets(tmp, sizeof(tmp), fd) == NULL) {
111 fclose(fd);
112 goto skip_device;
113 }
114 read_vendor_id = strtoul(tmp, NULL, 16);
115
116 fclose(fd);
117
118 if ((fd = g_fopen(product_path, "r")) == NULL)
119 goto skip_device;
120
121 if (fgets(tmp, sizeof(tmp), fd) == NULL) {
122 fclose(fd);
123 goto skip_device;
124 }
125 read_product_id = strtoul(tmp, NULL, 16);
126
127 fclose(fd);
128
129 if (vendor_id == read_vendor_id &&
130 product_id == read_product_id) {
13f2b9d7
DJ
131 path_copy = g_strdup(usb_dev);
132 matched_paths = g_slist_prepend(matched_paths,
133 path_copy);
134 }
135
89280b1a 136skip_device:
13f2b9d7
DJ
137 g_free(vendor_path);
138 g_free(product_path);
139 }
140 g_dir_close(devices_dir);
141
89280b1a 142 /* For every matched device try to find a ttyUSBX subfolder. */
13f2b9d7 143 for (l = matched_paths; l; l = l->next) {
13f2b9d7
DJ
144 subdir_path = NULL;
145
146 device_path = g_strconcat(device_tree, l->data, NULL);
147
148 if (!(device_dir = g_dir_open(device_path, 0, NULL))) {
149 g_free(device_path);
150 continue;
151 }
152
153 prefix = g_strconcat(l->data, ":", NULL);
154
155 while ((file = g_dir_read_name(device_dir))) {
156 if (g_str_has_prefix(file, prefix)) {
157 subdir_path = g_strconcat(device_path,
89280b1a 158 "/", file, NULL);
13f2b9d7
DJ
159 break;
160 }
161 }
162 g_dir_close(device_dir);
163
164 g_free(prefix);
165 g_free(device_path);
166
167 if (subdir_path) {
168 if (!(device_dir = g_dir_open(subdir_path, 0, NULL))) {
169 g_free(subdir_path);
170 continue;
171 }
172 g_free(subdir_path);
173
174 while ((file = g_dir_read_name(device_dir))) {
175 if (g_str_has_prefix(file, "ttyUSB")) {
13f2b9d7
DJ
176 tty_path = g_strconcat("/dev/",
177 file, NULL);
89280b1a 178 sr_dbg("Found USB device %04x:%04x attached to %s.",
13f2b9d7 179 vendor_id, product_id, tty_path);
89280b1a
UH
180 tty_devs = g_slist_prepend(tty_devs,
181 tty_path);
13f2b9d7
DJ
182 break;
183 }
184 }
185 g_dir_close(device_dir);
186 }
187 }
188 g_slist_free_full(matched_paths, g_free);
189
89280b1a 190 return tty_devs;
13f2b9d7
DJ
191#else
192 return NULL;
193#endif
194}
195
e9a62139
DJ
196static int check_manufacturer(const char *manufacturer)
197{
198 unsigned int i;
199
200 for (i = 0; i < ARRAY_SIZE(manufacturers); ++i)
201 if (!strcmp(manufacturer, manufacturers[i]))
202 return SR_OK;
203
204 return SR_ERR;
205}
206
207static struct sr_dev_inst *hmo_probe_serial_device(const char *serial_device,
208 const char *serial_options)
209{
210 struct sr_dev_inst *sdi;
211 struct dev_context *devc;
212 struct sr_scpi_hw_info *hw_info;
213 struct sr_scpi_dev_inst *scpi;
214
215 sdi = NULL;
216 devc = NULL;
217 scpi = NULL;
218 hw_info = NULL;
219
c3515cea 220 if (!(scpi = scpi_dev_inst_new(serial_device, serial_options)))
e9a62139
DJ
221 goto fail;
222
223 sr_info("Probing %s.", serial_device);
224 if (sr_scpi_open(scpi) != SR_OK)
225 goto fail;
226
227 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
228 sr_info("Couldn't get IDN response.");
229 goto fail;
230 }
231
232 if (check_manufacturer(hw_info->manufacturer) != SR_OK)
233 goto fail;
234
235 if (!(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE,
236 hw_info->manufacturer, hw_info->model,
237 hw_info->firmware_version))) {
238 goto fail;
239 }
240 sr_scpi_hw_info_free(hw_info);
241 hw_info = NULL;
242
243 if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
244 goto fail;
245
246 sdi->driver = di;
247 sdi->priv = devc;
248 sdi->inst_type = SR_INST_SCPI;
249 sdi->conn = scpi;
250
251 if (hmo_init_device(sdi) != SR_OK)
252 goto fail;
253
68e3d070
DJ
254 sr_scpi_close(sdi->conn);
255
256 sdi->status = SR_ST_INACTIVE;
257
e9a62139
DJ
258 return sdi;
259
260fail:
261 if (hw_info)
262 sr_scpi_hw_info_free(hw_info);
263 if (scpi)
264 sr_scpi_free(scpi);
265 if (sdi)
266 sr_dev_inst_free(sdi);
267 if (devc)
268 g_free(devc);
269
270 return NULL;
271}
272
06a3e78a
DJ
273static GSList *scan(GSList *options)
274{
06a3e78a 275 GSList *devices;
13f2b9d7
DJ
276 struct drv_context *drvc;
277 struct sr_dev_inst *sdi;
89280b1a
UH
278 const char *serial_device, *serial_options;
279 GSList *l, *tty_devs;
280 unsigned int i;
06a3e78a 281
13f2b9d7
DJ
282 serial_device = NULL;
283 serial_options = SERIALCOMM;
13f2b9d7 284 sdi = NULL;
06a3e78a
DJ
285 devices = NULL;
286 drvc = di->priv;
287 drvc->instances = NULL;
288
13f2b9d7
DJ
289 if (sr_serial_extract_options(options, &serial_device,
290 &serial_options) == SR_OK) {
719eff68 291 sdi = hmo_probe_serial_device(serial_device, serial_options);
13f2b9d7
DJ
292 if (sdi != NULL) {
293 devices = g_slist_append(devices, sdi);
294 drvc->instances = g_slist_append(drvc->instances, sdi);
295 }
13f2b9d7 296 } else {
89280b1a 297 tty_devs = NULL;
13f2b9d7
DJ
298
299 for (i = 0; i < ARRAY_SIZE(ho_models); i++) {
300 if ((l = auto_find_usb(ho_models[i].vendor_id,
301 ho_models[i].product_id)) == NULL)
302 continue;
89280b1a 303 tty_devs = g_slist_concat(tty_devs, l);
13f2b9d7
DJ
304 }
305
89280b1a 306 for (l = tty_devs; l; l = l->next) {
719eff68 307 sdi = hmo_probe_serial_device(l->data, serial_options);
13f2b9d7
DJ
308 if (sdi != NULL) {
309 devices = g_slist_append(devices, sdi);
310 drvc->instances = g_slist_append(drvc->instances, sdi);
311 }
312 }
313
89280b1a 314 g_slist_free_full(tty_devs, g_free);
13f2b9d7 315 }
06a3e78a
DJ
316
317 return devices;
318}
319
320static GSList *dev_list(void)
321{
322 return ((struct drv_context *)(di->priv))->instances;
323}
324
13f2b9d7
DJ
325static void clear_helper(void *priv)
326{
327 unsigned int i;
13f2b9d7
DJ
328 struct dev_context *devc;
329 struct scope_config *model;
330
331 devc = priv;
332 model = devc->model_config;
333
719eff68 334 hmo_scope_state_free(devc->model_state);
13f2b9d7 335
89280b1a 336 for (i = 0; i < model->analog_channels; ++i)
13f2b9d7 337 g_slist_free(devc->analog_groups[i].probes);
13f2b9d7
DJ
338
339 for (i = 0; i < model->digital_pods; ++i) {
340 g_slist_free(devc->digital_groups[i].probes);
341 g_free(devc->digital_groups[i].name);
342 }
343
344 g_free(devc->analog_groups);
345 g_free(devc->digital_groups);
346
347 g_free(devc);
348}
349
06a3e78a
DJ
350static int dev_clear(void)
351{
13f2b9d7 352 return std_dev_clear(di, clear_helper);
06a3e78a
DJ
353}
354
355static int dev_open(struct sr_dev_inst *sdi)
356{
23f43dff 357 if (sdi->status != SR_ST_ACTIVE && sr_scpi_open(sdi->conn) != SR_OK)
13f2b9d7 358 return SR_ERR;
06a3e78a 359
719eff68 360 if (hmo_scope_state_get(sdi) != SR_OK)
13f2b9d7 361 return SR_ERR;
06a3e78a
DJ
362
363 sdi->status = SR_ST_ACTIVE;
364
365 return SR_OK;
366}
367
368static int dev_close(struct sr_dev_inst *sdi)
369{
13f2b9d7
DJ
370 if (sdi->status == SR_ST_INACTIVE)
371 return SR_OK;
06a3e78a 372
23f43dff 373 sr_scpi_close(sdi->conn);
06a3e78a
DJ
374
375 sdi->status = SR_ST_INACTIVE;
376
377 return SR_OK;
378}
379
380static int cleanup(void)
381{
382 dev_clear();
383
06a3e78a
DJ
384 return SR_OK;
385}
386
13f2b9d7
DJ
387static int check_probe_group(struct dev_context *devc,
388 const struct sr_probe_group *probe_group)
389{
390 unsigned int i;
391 struct scope_config *model;
392
393 model = devc->model_config;
394
395 if (!probe_group)
396 return PG_NONE;
397
398 for (i = 0; i < model->analog_channels; ++i)
399 if (probe_group == &devc->analog_groups[i])
400 return PG_ANALOG;
401
402 for (i = 0; i < model->digital_pods; ++i)
403 if (probe_group == &devc->digital_groups[i])
404 return PG_DIGITAL;
405
406 sr_err("Invalid probe group specified.");
89280b1a 407
13f2b9d7
DJ
408 return PG_INVALID;
409}
410
411static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
412 const struct sr_probe_group *probe_group)
06a3e78a 413{
89280b1a 414 int ret, pg_type;
13f2b9d7 415 unsigned int i;
13f2b9d7
DJ
416 struct dev_context *devc;
417 struct scope_config *model;
418
419 if (!sdi || !(devc = sdi->priv))
420 return SR_ERR_ARG;
421
422 if ((pg_type = check_probe_group(devc, probe_group)) == PG_INVALID)
423 return SR_ERR;
06a3e78a 424
13f2b9d7
DJ
425 ret = SR_ERR_NA;
426 model = devc->model_config;
06a3e78a 427
06a3e78a 428 switch (key) {
13f2b9d7
DJ
429 case SR_CONF_NUM_TIMEBASE:
430 *data = g_variant_new_int32(model->num_xdivs);
431 ret = SR_OK;
432 break;
13f2b9d7
DJ
433 case SR_CONF_NUM_VDIV:
434 if (pg_type == PG_NONE) {
435 sr_err("No probe group specified.");
436 return SR_ERR_PROBE_GROUP;
13f2b9d7
DJ
437 } else if (pg_type == PG_ANALOG) {
438 for (i = 0; i < model->analog_channels; ++i) {
082972e8
UH
439 if (probe_group != &devc->analog_groups[i])
440 continue;
441 *data = g_variant_new_int32(model->num_ydivs);
442 ret = SR_OK;
443 break;
13f2b9d7
DJ
444 }
445
446 } else {
447 ret = SR_ERR_NA;
448 }
449 break;
06a3e78a 450 default:
13f2b9d7 451 ret = SR_ERR_NA;
06a3e78a
DJ
452 }
453
454 return ret;
455}
456
13f2b9d7
DJ
457static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n)
458{
459 unsigned int i;
13f2b9d7
DJ
460 GVariant *rational[2];
461 GVariantBuilder gvb;
462
463 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
464
465 for (i = 0; i < n; i++) {
466 rational[0] = g_variant_new_uint64((*array)[i][0]);
467 rational[1] = g_variant_new_uint64((*array)[i][1]);
468
89280b1a 469 /* FIXME: Valgrind reports a memory leak here. */
13f2b9d7
DJ
470 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
471 }
472
473 return g_variant_builder_end(&gvb);
474}
475
476static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
477 const struct sr_probe_group *probe_group)
06a3e78a 478{
89280b1a
UH
479 int ret, pg_type;
480 unsigned int i, j;
965b463d 481 char command[MAX_COMMAND_SIZE], float_str[30];
13f2b9d7
DJ
482 struct dev_context *devc;
483 struct scope_config *model;
484 struct scope_state *state;
89280b1a
UH
485 const char *tmp;
486 uint64_t p, q, tmp_u64;
487 double tmp_d;
06a3e78a 488
13f2b9d7
DJ
489 if (!sdi || !(devc = sdi->priv))
490 return SR_ERR_ARG;
491
492 if ((pg_type = check_probe_group(devc, probe_group)) == PG_INVALID)
493 return SR_ERR;
494
495 model = devc->model_config;
496 state = devc->model_state;
497
498 ret = SR_ERR_NA;
06a3e78a 499
06a3e78a 500 switch (key) {
13f2b9d7
DJ
501 case SR_CONF_LIMIT_FRAMES:
502 devc->frame_limit = g_variant_get_uint64(data);
503 ret = SR_OK;
504 break;
13f2b9d7 505 case SR_CONF_TRIGGER_SOURCE:
13f2b9d7 506 tmp = g_variant_get_string(data, NULL);
13f2b9d7 507 for (i = 0; (*model->trigger_sources)[i]; i++) {
082972e8
UH
508 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
509 continue;
510 state->trigger_source = i;
511 g_snprintf(command, sizeof(command),
512 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
513 (*model->trigger_sources)[i]);
13f2b9d7 514
082972e8
UH
515 ret = sr_scpi_send(sdi->conn, command);
516 break;
13f2b9d7 517 }
89280b1a 518 break;
13f2b9d7 519 case SR_CONF_VDIV:
13f2b9d7
DJ
520 if (pg_type == PG_NONE) {
521 sr_err("No probe group specified.");
522 return SR_ERR_PROBE_GROUP;
523 }
524
525 g_variant_get(data, "(tt)", &p, &q);
526
527 for (i = 0; i < model->num_vdivs; i++) {
082972e8
UH
528 if (p != (*model->vdivs)[i][0] ||
529 q != (*model->vdivs)[i][1])
530 continue;
531 for (j = 1; j <= model->analog_channels; ++j) {
532 if (probe_group != &devc->analog_groups[j - 1])
533 continue;
534 state->analog_channels[j - 1].vdiv = (float) p / q;
965b463d 535 g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
082972e8
UH
536 g_snprintf(command, sizeof(command),
537 (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV],
965b463d 538 j, float_str);
082972e8
UH
539
540 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
541 sr_scpi_get_opc(sdi->conn) != SR_OK)
542 return SR_ERR;
13f2b9d7 543
13f2b9d7
DJ
544 break;
545 }
082972e8
UH
546
547 ret = SR_OK;
548 break;
13f2b9d7 549 }
89280b1a 550 break;
13f2b9d7 551 case SR_CONF_TIMEBASE:
13f2b9d7
DJ
552 g_variant_get(data, "(tt)", &p, &q);
553
554 for (i = 0; i < model->num_timebases; i++) {
082972e8
UH
555 if (p != (*model->timebases)[i][0] ||
556 q != (*model->timebases)[i][1])
557 continue;
558 state->timebase = (float) p / q;
965b463d 559 g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
082972e8
UH
560 g_snprintf(command, sizeof(command),
561 (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE],
965b463d 562 float_str);
13f2b9d7 563
082972e8
UH
564 ret = sr_scpi_send(sdi->conn, command);
565 break;
13f2b9d7 566 }
89280b1a 567 break;
13f2b9d7 568 case SR_CONF_HORIZ_TRIGGERPOS:
89280b1a 569 tmp_d = g_variant_get_double(data);
13f2b9d7 570
89280b1a 571 if (tmp_d < 0.0 || tmp_d > 1.0)
13f2b9d7
DJ
572 return SR_ERR;
573
89280b1a 574 state->horiz_triggerpos = -(tmp_d - 0.5) * state->timebase * model->num_xdivs;
13f2b9d7
DJ
575 g_snprintf(command, sizeof(command),
576 (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS],
577 state->horiz_triggerpos);
578
579 ret = sr_scpi_send(sdi->conn, command);
89280b1a 580 break;
13f2b9d7 581 case SR_CONF_TRIGGER_SLOPE:
89280b1a 582 tmp_u64 = g_variant_get_uint64(data);
13f2b9d7 583
89280b1a 584 if (tmp_u64 != 0 && tmp_u64 != 1)
13f2b9d7
DJ
585 return SR_ERR;
586
89280b1a 587 state->trigger_slope = tmp_u64;
13f2b9d7
DJ
588
589 g_snprintf(command, sizeof(command),
590 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
89280b1a 591 tmp_u64 ? "POS" : "NEG");
13f2b9d7
DJ
592
593 ret = sr_scpi_send(sdi->conn, command);
89280b1a 594 break;
13f2b9d7 595 case SR_CONF_COUPLING:
13f2b9d7
DJ
596 if (pg_type == PG_NONE) {
597 sr_err("No probe group specified.");
598 return SR_ERR_PROBE_GROUP;
599 }
600
601 tmp = g_variant_get_string(data, NULL);
602
603 for (i = 0; (*model->coupling_options)[i]; i++) {
082972e8
UH
604 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
605 continue;
606 for (j = 1; j <= model->analog_channels; ++j) {
607 if (probe_group != &devc->analog_groups[j - 1])
608 continue;
609 state->analog_channels[j-1].coupling = i;
13f2b9d7 610
082972e8
UH
611 g_snprintf(command, sizeof(command),
612 (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
613 j, tmp);
614
615 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
616 sr_scpi_get_opc(sdi->conn) != SR_OK)
617 return SR_ERR;
13f2b9d7
DJ
618 break;
619 }
082972e8
UH
620
621 ret = SR_OK;
622 break;
13f2b9d7 623 }
89280b1a 624 break;
06a3e78a
DJ
625 default:
626 ret = SR_ERR_NA;
13f2b9d7 627 break;
06a3e78a
DJ
628 }
629
13f2b9d7
DJ
630 if (ret == SR_OK)
631 ret = sr_scpi_get_opc(sdi->conn);
632
06a3e78a
DJ
633 return ret;
634}
635
13f2b9d7 636static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
89280b1a 637 const struct sr_probe_group *probe_group)
06a3e78a 638{
13f2b9d7 639 int pg_type;
13f2b9d7
DJ
640 struct dev_context *devc;
641 struct scope_config *model;
642
643 if (!sdi || !(devc = sdi->priv))
644 return SR_ERR_ARG;
06a3e78a 645
13f2b9d7
DJ
646 if ((pg_type = check_probe_group(devc, probe_group)) == PG_INVALID)
647 return SR_ERR;
648
649 model = devc->model_config;
06a3e78a 650
06a3e78a 651 switch (key) {
13f2b9d7
DJ
652 case SR_CONF_DEVICE_OPTIONS:
653 if (pg_type == PG_NONE) {
654 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
89280b1a
UH
655 model->hw_caps, model->num_hwcaps,
656 sizeof(int32_t));
13f2b9d7
DJ
657 } else if (pg_type == PG_ANALOG) {
658 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
89280b1a
UH
659 model->analog_hwcaps, model->num_analog_hwcaps,
660 sizeof(int32_t));
13f2b9d7
DJ
661 } else {
662 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
89280b1a 663 NULL, 0, sizeof(int32_t));
13f2b9d7
DJ
664 }
665 break;
13f2b9d7
DJ
666 case SR_CONF_COUPLING:
667 if (pg_type == PG_NONE)
668 return SR_ERR_PROBE_GROUP;
13f2b9d7 669 *data = g_variant_new_strv(*model->coupling_options,
89280b1a 670 g_strv_length((char **)*model->coupling_options));
13f2b9d7 671 break;
13f2b9d7
DJ
672 case SR_CONF_TRIGGER_SOURCE:
673 *data = g_variant_new_strv(*model->trigger_sources,
89280b1a 674 g_strv_length((char **)*model->trigger_sources));
13f2b9d7 675 break;
13f2b9d7
DJ
676 case SR_CONF_TIMEBASE:
677 *data = build_tuples(model->timebases, model->num_timebases);
678 break;
13f2b9d7
DJ
679 case SR_CONF_VDIV:
680 if (pg_type == PG_NONE)
681 return SR_ERR_PROBE_GROUP;
13f2b9d7
DJ
682 *data = build_tuples(model->vdivs, model->num_vdivs);
683 break;
06a3e78a
DJ
684 default:
685 return SR_ERR_NA;
686 }
687
13f2b9d7 688 return SR_OK;
06a3e78a
DJ
689}
690
13f2b9d7 691SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi)
06a3e78a 692{
13f2b9d7 693 char command[MAX_COMMAND_SIZE];
13f2b9d7
DJ
694 struct sr_probe *probe;
695 struct dev_context *devc;
696 struct scope_config *model;
697
698 devc = sdi->priv;
699 model = devc->model_config;
700
701 probe = devc->current_probe->data;
702
703 switch (probe->type) {
704 case SR_PROBE_ANALOG:
705 g_snprintf(command, sizeof(command),
706 (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA],
707 probe->index + 1);
708 break;
709 case SR_PROBE_LOGIC:
710 g_snprintf(command, sizeof(command),
711 (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA],
712 probe->index < 8 ? 1 : 2);
713 break;
714 default:
89280b1a 715 sr_err("Invalid probe type.");
13f2b9d7
DJ
716 break;
717 }
718
719 return sr_scpi_send(sdi->conn, command);
720}
721
722static int hmo_check_probes(GSList *probes)
723{
724 GSList *l;
13f2b9d7 725 struct sr_probe *probe;
89280b1a 726 gboolean enabled_pod1, enabled_pod2, enabled_chan3, enabled_chan4;
13f2b9d7 727
89280b1a 728 enabled_pod1 = enabled_pod2 = enabled_chan3 = enabled_chan4 = FALSE;
13f2b9d7
DJ
729
730 for (l = probes; l; l = l->next) {
731 probe = l->data;
13f2b9d7
DJ
732 switch (probe->type) {
733 case SR_PROBE_ANALOG:
734 if (probe->index == 2)
735 enabled_chan3 = TRUE;
736 else if (probe->index == 3)
737 enabled_chan4 = TRUE;
738 break;
13f2b9d7
DJ
739 case SR_PROBE_LOGIC:
740 if (probe->index < 8)
741 enabled_pod1 = TRUE;
742 else
743 enabled_pod2 = TRUE;
744 break;
13f2b9d7
DJ
745 default:
746 return SR_ERR;
747 }
748 }
749
750 if ((enabled_pod1 && enabled_chan3) ||
751 (enabled_pod2 && enabled_chan4))
752 return SR_ERR;
753
754 return SR_OK;
755}
756
757static int hmo_setup_probes(const struct sr_dev_inst *sdi)
758{
759 GSList *l;
760 unsigned int i;
761 gboolean *pod_enabled;
762 char command[MAX_COMMAND_SIZE];
13f2b9d7
DJ
763 struct scope_state *state;
764 struct scope_config *model;
13f2b9d7
DJ
765 struct sr_probe *probe;
766 struct dev_context *devc;
23f43dff 767 struct sr_scpi_dev_inst *scpi;
13f2b9d7
DJ
768
769 devc = sdi->priv;
23f43dff 770 scpi = sdi->conn;
13f2b9d7
DJ
771 state = devc->model_state;
772 model = devc->model_config;
773
774 pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods);
775
776 for (l = sdi->probes; l; l = l->next) {
777 probe = l->data;
13f2b9d7
DJ
778 switch (probe->type) {
779 case SR_PROBE_ANALOG:
082972e8
UH
780 if (probe->enabled == state->analog_channels[probe->index].state)
781 break;
782 g_snprintf(command, sizeof(command),
783 (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE],
784 probe->index + 1, probe->enabled);
13f2b9d7 785
23f43dff 786 if (sr_scpi_send(scpi, command) != SR_OK)
082972e8
UH
787 return SR_ERR;
788 state->analog_channels[probe->index].state = probe->enabled;
89280b1a 789 break;
13f2b9d7 790 case SR_PROBE_LOGIC:
13f2b9d7
DJ
791 /*
792 * A digital POD needs to be enabled for every group of
793 * 8 probes.
794 */
795 if (probe->enabled)
796 pod_enabled[probe->index < 8 ? 0 : 1] = TRUE;
797
082972e8
UH
798 if (probe->enabled == state->digital_channels[probe->index])
799 break;
800 g_snprintf(command, sizeof(command),
801 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE],
802 probe->index, probe->enabled);
13f2b9d7 803
23f43dff 804 if (sr_scpi_send(scpi, command) != SR_OK)
082972e8 805 return SR_ERR;
13f2b9d7 806
082972e8 807 state->digital_channels[probe->index] = probe->enabled;
89280b1a 808 break;
13f2b9d7
DJ
809 default:
810 return SR_ERR;
811 }
812 }
813
814 for (i = 1; i <= model->digital_pods; ++i) {
082972e8
UH
815 if (state->digital_pods[i - 1] == pod_enabled[i - 1])
816 continue;
817 g_snprintf(command, sizeof(command),
818 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE],
819 i, pod_enabled[i - 1]);
23f43dff 820 if (sr_scpi_send(scpi, command) != SR_OK)
082972e8
UH
821 return SR_ERR;
822 state->digital_pods[i - 1] = pod_enabled[i - 1];
13f2b9d7
DJ
823 }
824
825 g_free(pod_enabled);
826
827 return SR_OK;
828}
829
830static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
831{
832 GSList *l;
833 gboolean digital_added;
13f2b9d7
DJ
834 struct sr_probe *probe;
835 struct dev_context *devc;
23f43dff 836 struct sr_scpi_dev_inst *scpi;
06a3e78a
DJ
837
838 if (sdi->status != SR_ST_ACTIVE)
839 return SR_ERR_DEV_CLOSED;
840
23f43dff 841 scpi = sdi->conn;
13f2b9d7
DJ
842 devc = sdi->priv;
843 digital_added = FALSE;
844
845 for (l = sdi->probes; l; l = l->next) {
846 probe = l->data;
082972e8
UH
847 if (!probe->enabled)
848 continue;
849 /* Only add a single digital probe. */
850 if (probe->type != SR_PROBE_LOGIC || !digital_added) {
851 devc->enabled_probes = g_slist_append(
852 devc->enabled_probes, probe);
853 if (probe->type == SR_PROBE_LOGIC)
854 digital_added = TRUE;
13f2b9d7
DJ
855 }
856 }
06a3e78a 857
13f2b9d7
DJ
858 if (!devc->enabled_probes)
859 return SR_ERR;
860
861 if (hmo_check_probes(devc->enabled_probes) != SR_OK) {
862 sr_err("Invalid probe configuration specified!");
863 return SR_ERR_NA;
864 }
865
866 if (hmo_setup_probes(sdi) != SR_OK) {
867 sr_err("Failed to setup probe configuration!");
868 return SR_ERR;
869 }
870
23f43dff 871 sr_scpi_source_add(scpi, G_IO_IN, 50, hmo_receive_data, (void *)sdi);
13f2b9d7
DJ
872
873 /* Send header packet to the session bus. */
874 std_session_send_df_header(cb_data, LOG_PREFIX);
875
876 devc->current_probe = devc->enabled_probes;
877
878 return hmo_request_data(sdi);
06a3e78a
DJ
879}
880
881static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
882{
13f2b9d7 883 struct dev_context *devc;
23f43dff 884 struct sr_scpi_dev_inst *scpi;
66e3219d 885 struct sr_datafeed_packet packet;
13f2b9d7 886
06a3e78a
DJ
887 (void)cb_data;
888
66e3219d
DJ
889 packet.type = SR_DF_END;
890 packet.payload = NULL;
891 sr_session_send(sdi, &packet);
892
06a3e78a
DJ
893 if (sdi->status != SR_ST_ACTIVE)
894 return SR_ERR_DEV_CLOSED;
895
13f2b9d7
DJ
896 devc = sdi->priv;
897
898 g_slist_free(devc->enabled_probes);
899 devc->enabled_probes = NULL;
23f43dff
ML
900 scpi = sdi->conn;
901 sr_scpi_source_remove(scpi);
06a3e78a
DJ
902
903 return SR_OK;
904}
905
906SR_PRIV struct sr_dev_driver hameg_hmo_driver_info = {
907 .name = "hameg-hmo",
89280b1a 908 .longname = "Hameg HMO",
06a3e78a
DJ
909 .api_version = 1,
910 .init = init,
911 .cleanup = cleanup,
912 .scan = scan,
913 .dev_list = dev_list,
914 .dev_clear = dev_clear,
915 .config_get = config_get,
916 .config_set = config_set,
917 .config_list = config_list,
918 .dev_open = dev_open,
919 .dev_close = dev_close,
920 .dev_acquisition_start = dev_acquisition_start,
921 .dev_acquisition_stop = dev_acquisition_stop,
922 .priv = NULL,
923};