]> sigrok.org Git - libsigrok.git/blame - src/hardware/hameg-hmo/api.c
output/csv: use intermediate time_t var, silence compiler warning
[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>
e131be0a 5 * Copyright (C) 2018 Guido Trentalancia <guido@trentalancia.com>
06a3e78a
DJ
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
6ec6c43b 21#include <config.h>
13f2b9d7 22#include <stdlib.h>
5a1afc09 23#include "scpi.h"
06a3e78a
DJ
24#include "protocol.h"
25
dd5c48a6 26static struct sr_dev_driver hameg_hmo_driver_info;
e9a62139
DJ
27
28static const char *manufacturers[] = {
29 "HAMEG",
da1726cc 30 "Rohde&Schwarz",
e9a62139
DJ
31};
32
584560f1 33static const uint32_t scanopts[] = {
13f2b9d7
DJ
34 SR_CONF_CONN,
35 SR_CONF_SERIALCOMM,
36};
37
55fb76b3
UH
38static const uint32_t drvopts[] = {
39 SR_CONF_OSCILLOSCOPE,
3a247d03 40 SR_CONF_LOGIC_ANALYZER,
55fb76b3
UH
41};
42
719eff68 43enum {
61e77667
UH
44 CG_INVALID = -1,
45 CG_NONE,
46 CG_ANALOG,
47 CG_DIGITAL,
719eff68
UH
48};
49
373e92a4 50static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
e9a62139
DJ
51{
52 struct sr_dev_inst *sdi;
53 struct dev_context *devc;
54 struct sr_scpi_hw_info *hw_info;
e9a62139
DJ
55
56 sdi = NULL;
57 devc = NULL;
e9a62139
DJ
58 hw_info = NULL;
59
e9a62139
DJ
60 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
61 sr_info("Couldn't get IDN response.");
62 goto fail;
63 }
64
697fb6dd 65 if (std_str_idx_s(hw_info->manufacturer, ARRAY_AND_SIZE(manufacturers)) < 0)
e9a62139
DJ
66 goto fail;
67
aac29cc1 68 sdi = g_malloc0(sizeof(struct sr_dev_inst));
0af636be
UH
69 sdi->vendor = g_strdup(hw_info->manufacturer);
70 sdi->model = g_strdup(hw_info->model);
71 sdi->version = g_strdup(hw_info->firmware_version);
72 sdi->serial_num = g_strdup(hw_info->serial_number);
4f840ce9 73 sdi->driver = &hameg_hmo_driver_info;
b33db61c
SA
74 sdi->inst_type = SR_INST_SCPI;
75 sdi->conn = scpi;
b33db61c 76
e9a62139
DJ
77 sr_scpi_hw_info_free(hw_info);
78 hw_info = NULL;
79
f57d8ffe 80 devc = g_malloc0(sizeof(struct dev_context));
e9a62139 81
e9a62139 82 sdi->priv = devc;
e9a62139
DJ
83
84 if (hmo_init_device(sdi) != SR_OK)
85 goto fail;
86
87 return sdi;
88
89fail:
7b365c47 90 sr_scpi_hw_info_free(hw_info);
4bf93988 91 sr_dev_inst_free(sdi);
b1f83103 92 g_free(devc);
e9a62139
DJ
93
94 return NULL;
95}
96
4f840ce9 97static GSList *scan(struct sr_dev_driver *di, GSList *options)
06a3e78a 98{
373e92a4 99 return sr_scpi_scan(di->context, options, probe_device);
06a3e78a
DJ
100}
101
3553451f 102static void clear_helper(struct dev_context *devc)
13f2b9d7 103{
719eff68 104 hmo_scope_state_free(devc->model_state);
13f2b9d7
DJ
105 g_free(devc->analog_groups);
106 g_free(devc->digital_groups);
13f2b9d7
DJ
107}
108
4f840ce9 109static int dev_clear(const struct sr_dev_driver *di)
06a3e78a 110{
3553451f 111 return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
06a3e78a
DJ
112}
113
114static int dev_open(struct sr_dev_inst *sdi)
115{
6402c379 116 if (sr_scpi_open(sdi->conn) != SR_OK)
13f2b9d7 117 return SR_ERR;
06a3e78a 118
719eff68 119 if (hmo_scope_state_get(sdi) != SR_OK)
13f2b9d7 120 return SR_ERR;
06a3e78a 121
06a3e78a
DJ
122 return SR_OK;
123}
124
125static int dev_close(struct sr_dev_inst *sdi)
126{
f1ba6b4b 127 return sr_scpi_close(sdi->conn);
06a3e78a
DJ
128}
129
660e398f 130static int check_channel_group(struct dev_context *devc,
53b4680f 131 const struct sr_channel_group *cg)
13f2b9d7 132{
329733d9 133 const struct scope_config *model;
13f2b9d7
DJ
134
135 model = devc->model_config;
136
53b4680f 137 if (!cg)
61e77667 138 return CG_NONE;
13f2b9d7 139
fcd6a8bd
UH
140 if (std_cg_idx(cg, devc->analog_groups, model->analog_channels) >= 0)
141 return CG_ANALOG;
13f2b9d7 142
fcd6a8bd
UH
143 if (std_cg_idx(cg, devc->digital_groups, model->digital_pods) >= 0)
144 return CG_DIGITAL;
13f2b9d7 145
660e398f 146 sr_err("Invalid channel group specified.");
89280b1a 147
61e77667 148 return CG_INVALID;
13f2b9d7
DJ
149}
150
dd7a72ea
UH
151static int config_get(uint32_t key, GVariant **data,
152 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
06a3e78a 153{
aac30633 154 int cg_type, idx, i;
13f2b9d7 155 struct dev_context *devc;
329733d9 156 const struct scope_config *model;
14a2f74d 157 struct scope_state *state;
13f2b9d7 158
709468ba 159 if (!sdi)
13f2b9d7
DJ
160 return SR_ERR_ARG;
161
709468ba
UH
162 devc = sdi->priv;
163
61e77667 164 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
13f2b9d7 165 return SR_ERR;
06a3e78a 166
13f2b9d7 167 model = devc->model_config;
14a2f74d 168 state = devc->model_state;
06a3e78a 169
06a3e78a 170 switch (key) {
bf622e6d 171 case SR_CONF_NUM_HDIV:
13f2b9d7 172 *data = g_variant_new_int32(model->num_xdivs);
13f2b9d7 173 break;
3fd2dca2
DJ
174 case SR_CONF_TIMEBASE:
175 *data = g_variant_new("(tt)", (*model->timebases)[state->timebase][0],
176 (*model->timebases)[state->timebase][1]);
3fd2dca2 177 break;
13f2b9d7 178 case SR_CONF_NUM_VDIV:
3782e571 179 if (!cg)
660e398f 180 return SR_ERR_CHANNEL_GROUP;
3782e571 181 if (cg_type != CG_ANALOG)
758906aa 182 return SR_ERR_NA;
3782e571
UH
183 if (std_cg_idx(cg, devc->analog_groups, model->analog_channels) < 0)
184 return SR_ERR_ARG;
185 *data = g_variant_new_int32(model->num_ydivs);
3fd2dca2
DJ
186 break;
187 case SR_CONF_VDIV:
3782e571 188 if (!cg)
61e77667 189 return SR_ERR_CHANNEL_GROUP;
3782e571 190 if (cg_type != CG_ANALOG)
758906aa 191 return SR_ERR_NA;
3782e571
UH
192 if ((idx = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
193 return SR_ERR_ARG;
194 *data = g_variant_new("(tt)",
195 (*model->vdivs)[state->analog_channels[idx].vdiv][0],
196 (*model->vdivs)[state->analog_channels[idx].vdiv][1]);
ccf14618
DJ
197 break;
198 case SR_CONF_TRIGGER_SOURCE:
199 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
ccf14618 200 break;
3fd2dca2
DJ
201 case SR_CONF_TRIGGER_SLOPE:
202 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
3fd2dca2 203 break;
4fa4db2c
GT
204 case SR_CONF_TRIGGER_PATTERN:
205 *data = g_variant_new_string(state->trigger_pattern);
206 break;
97a00074
GT
207 case SR_CONF_HIGH_RESOLUTION:
208 *data = g_variant_new_boolean(state->high_resolution);
209 break;
210 case SR_CONF_PEAK_DETECTION:
211 *data = g_variant_new_boolean(state->peak_detection);
212 break;
3fd2dca2
DJ
213 case SR_CONF_HORIZ_TRIGGERPOS:
214 *data = g_variant_new_double(state->horiz_triggerpos);
3fd2dca2 215 break;
ccf14618 216 case SR_CONF_COUPLING:
3782e571 217 if (!cg)
660e398f 218 return SR_ERR_CHANNEL_GROUP;
3782e571 219 if (cg_type != CG_ANALOG)
758906aa 220 return SR_ERR_NA;
3782e571
UH
221 if ((idx = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
222 return SR_ERR_ARG;
223 *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[idx].coupling]);
13f2b9d7 224 break;
14a2f74d
DJ
225 case SR_CONF_SAMPLERATE:
226 *data = g_variant_new_uint64(state->sample_rate);
14a2f74d 227 break;
e131be0a
GT
228 case SR_CONF_LOGIC_THRESHOLD:
229 if (!cg)
230 return SR_ERR_CHANNEL_GROUP;
231 if (cg_type != CG_DIGITAL)
232 return SR_ERR_NA;
233 if (!model)
234 return SR_ERR_ARG;
235 if ((idx = std_cg_idx(cg, devc->digital_groups, model->digital_pods)) < 0)
236 return SR_ERR_ARG;
237 *data = g_variant_new_string((*model->logic_threshold)[state->digital_pods[idx].threshold]);
238 break;
239 case SR_CONF_LOGIC_THRESHOLD_CUSTOM:
240 if (!cg)
241 return SR_ERR_CHANNEL_GROUP;
242 if (cg_type != CG_DIGITAL)
243 return SR_ERR_NA;
244 if (!model)
245 return SR_ERR_ARG;
246 if ((idx = std_cg_idx(cg, devc->digital_groups, model->digital_pods)) < 0)
247 return SR_ERR_ARG;
aac30633
GT
248 /* Check if the oscilloscope is currently in custom threshold mode. */
249 for (i = 0; i < model->num_logic_threshold; i++) {
250 if (!strcmp("USER2", (*model->logic_threshold)[i]))
251 if (strcmp("USER2", (*model->logic_threshold)[state->digital_pods[idx].threshold]))
252 return SR_ERR_NA;
253 if (!strcmp("USER", (*model->logic_threshold)[i]))
254 if (strcmp("USER", (*model->logic_threshold)[state->digital_pods[idx].threshold]))
255 return SR_ERR_NA;
256 if (!strcmp("MAN", (*model->logic_threshold)[i]))
257 if (strcmp("MAN", (*model->logic_threshold)[state->digital_pods[idx].threshold]))
258 return SR_ERR_NA;
259 }
e131be0a
GT
260 *data = g_variant_new_double(state->digital_pods[idx].user_threshold);
261 break;
98c7ef37
VO
262 case SR_CONF_LIMIT_FRAMES:
263 *data = g_variant_new_uint64(devc->frame_limit);
264 break;
06a3e78a 265 default:
758906aa 266 return SR_ERR_NA;
06a3e78a
DJ
267 }
268
758906aa 269 return SR_OK;
06a3e78a
DJ
270}
271
dd7a72ea
UH
272static int config_set(uint32_t key, GVariant *data,
273 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
06a3e78a 274{
aac30633
GT
275 int ret, cg_type, idx, i, j;
276 char command[MAX_COMMAND_SIZE], command2[MAX_COMMAND_SIZE];
277 char float_str[30], *tmp_str;
13f2b9d7 278 struct dev_context *devc;
329733d9 279 const struct scope_config *model;
13f2b9d7 280 struct scope_state *state;
1203acc7 281 double tmp_d, tmp_d2;
97a00074 282 gboolean update_sample_rate, tmp_bool;
06a3e78a 283
b0baddef 284 if (!sdi)
13f2b9d7
DJ
285 return SR_ERR_ARG;
286
b0baddef
UH
287 devc = sdi->priv;
288
61e77667 289 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
13f2b9d7
DJ
290 return SR_ERR;
291
292 model = devc->model_config;
293 state = devc->model_state;
23e1ea7a 294 update_sample_rate = FALSE;
13f2b9d7 295
06a3e78a 296 switch (key) {
d779dcac
GT
297 case SR_CONF_LIMIT_SAMPLES:
298 devc->samples_limit = g_variant_get_uint64(data);
299 ret = SR_OK;
300 break;
13f2b9d7
DJ
301 case SR_CONF_LIMIT_FRAMES:
302 devc->frame_limit = g_variant_get_uint64(data);
303 ret = SR_OK;
304 break;
13f2b9d7 305 case SR_CONF_VDIV:
61233697 306 if (!cg)
660e398f 307 return SR_ERR_CHANNEL_GROUP;
697fb6dd
UH
308 if ((idx = std_u64_tuple_idx(data, *model->vdivs, model->num_vdivs)) < 0)
309 return SR_ERR_ARG;
fcd6a8bd
UH
310 if ((j = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
311 return SR_ERR_ARG;
fcd6a8bd
UH
312 g_ascii_formatd(float_str, sizeof(float_str), "%E",
313 (float) (*model->vdivs)[idx][0] / (*model->vdivs)[idx][1]);
314 g_snprintf(command, sizeof(command),
66ddc22a 315 (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_SCALE],
fcd6a8bd
UH
316 j + 1, float_str);
317 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
318 sr_scpi_get_opc(sdi->conn) != SR_OK)
319 return SR_ERR;
1203acc7 320 state->analog_channels[j].vdiv = idx;
697fb6dd 321 ret = SR_OK;
89280b1a 322 break;
13f2b9d7 323 case SR_CONF_TIMEBASE:
697fb6dd
UH
324 if ((idx = std_u64_tuple_idx(data, *model->timebases, model->num_timebases)) < 0)
325 return SR_ERR_ARG;
697fb6dd
UH
326 g_ascii_formatd(float_str, sizeof(float_str), "%E",
327 (float) (*model->timebases)[idx][0] / (*model->timebases)[idx][1]);
328 g_snprintf(command, sizeof(command),
329 (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE],
330 float_str);
38839344
GT
331 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
332 sr_scpi_get_opc(sdi->conn) != SR_OK)
333 return SR_ERR;
1203acc7 334 state->timebase = idx;
38839344 335 ret = SR_OK;
697fb6dd 336 update_sample_rate = TRUE;
89280b1a 337 break;
13f2b9d7 338 case SR_CONF_HORIZ_TRIGGERPOS:
89280b1a 339 tmp_d = g_variant_get_double(data);
89280b1a 340 if (tmp_d < 0.0 || tmp_d > 1.0)
13f2b9d7 341 return SR_ERR;
1203acc7 342 tmp_d2 = -(tmp_d - 0.5) *
422a1c0d
DJ
343 ((double) (*model->timebases)[state->timebase][0] /
344 (*model->timebases)[state->timebase][1])
345 * model->num_xdivs;
1203acc7 346 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d2);
13f2b9d7
DJ
347 g_snprintf(command, sizeof(command),
348 (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS],
422a1c0d 349 float_str);
38839344
GT
350 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
351 sr_scpi_get_opc(sdi->conn) != SR_OK)
352 return SR_ERR;
1203acc7 353 state->horiz_triggerpos = tmp_d;
38839344 354 ret = SR_OK;
89280b1a 355 break;
97a00074
GT
356 case SR_CONF_TRIGGER_SOURCE:
357 if ((idx = std_str_idx(data, *model->trigger_sources, model->num_trigger_sources)) < 0)
358 return SR_ERR_ARG;
359 g_snprintf(command, sizeof(command),
360 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
361 (*model->trigger_sources)[idx]);
362 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
363 sr_scpi_get_opc(sdi->conn) != SR_OK)
364 return SR_ERR;
365 state->trigger_source = idx;
366 ret = SR_OK;
367 break;
13f2b9d7 368 case SR_CONF_TRIGGER_SLOPE:
bd633efa
UH
369 if ((idx = std_str_idx(data, *model->trigger_slopes, model->num_trigger_slopes)) < 0)
370 return SR_ERR_ARG;
bd633efa
UH
371 g_snprintf(command, sizeof(command),
372 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
373 (*model->trigger_slopes)[idx]);
38839344
GT
374 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
375 sr_scpi_get_opc(sdi->conn) != SR_OK)
376 return SR_ERR;
1203acc7 377 state->trigger_slope = idx;
38839344 378 ret = SR_OK;
89280b1a 379 break;
4fa4db2c
GT
380 case SR_CONF_TRIGGER_PATTERN:
381 tmp_str = (char *)g_variant_get_string(data, 0);
382 idx = strlen(tmp_str);
383 if (idx == 0 || idx > model->analog_channels + model->digital_channels)
384 return SR_ERR_ARG;
385 g_snprintf(command, sizeof(command),
386 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_PATTERN],
387 tmp_str);
388 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
389 sr_scpi_get_opc(sdi->conn) != SR_OK)
390 return SR_ERR;
396af5ad
GT
391 strncpy(state->trigger_pattern,
392 tmp_str,
393 MAX_ANALOG_CHANNEL_COUNT + MAX_DIGITAL_CHANNEL_COUNT);
4fa4db2c
GT
394 ret = SR_OK;
395 break;
97a00074
GT
396 case SR_CONF_HIGH_RESOLUTION:
397 tmp_bool = g_variant_get_boolean(data);
398 g_snprintf(command, sizeof(command),
399 (*model->scpi_dialect)[SCPI_CMD_SET_HIGH_RESOLUTION],
400 tmp_bool ? "AUTO" : "OFF");
401 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
402 sr_scpi_get_opc(sdi->conn) != SR_OK)
403 return SR_ERR;
404 /* High Resolution mode automatically switches off Peak Detection. */
405 if (tmp_bool) {
406 g_snprintf(command, sizeof(command),
407 (*model->scpi_dialect)[SCPI_CMD_SET_PEAK_DETECTION],
408 "OFF");
409 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
410 sr_scpi_get_opc(sdi->conn) != SR_OK)
411 return SR_ERR;
412 state->peak_detection = FALSE;
413 }
414 state->high_resolution = tmp_bool;
415 ret = SR_OK;
416 break;
417 case SR_CONF_PEAK_DETECTION:
418 tmp_bool = g_variant_get_boolean(data);
419 g_snprintf(command, sizeof(command),
420 (*model->scpi_dialect)[SCPI_CMD_SET_PEAK_DETECTION],
421 tmp_bool ? "AUTO" : "OFF");
422 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
423 sr_scpi_get_opc(sdi->conn) != SR_OK)
424 return SR_ERR;
425 /* Peak Detection automatically switches off High Resolution mode. */
426 if (tmp_bool) {
427 g_snprintf(command, sizeof(command),
428 (*model->scpi_dialect)[SCPI_CMD_SET_HIGH_RESOLUTION],
429 "OFF");
430 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
431 sr_scpi_get_opc(sdi->conn) != SR_OK)
432 return SR_ERR;
433 state->high_resolution = FALSE;
434 }
435 state->peak_detection = tmp_bool;
436 ret = SR_OK;
437 break;
13f2b9d7 438 case SR_CONF_COUPLING:
61233697 439 if (!cg)
660e398f 440 return SR_ERR_CHANNEL_GROUP;
bd633efa
UH
441 if ((idx = std_str_idx(data, *model->coupling_options, model->num_coupling_options)) < 0)
442 return SR_ERR_ARG;
fcd6a8bd
UH
443 if ((j = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
444 return SR_ERR_ARG;
fcd6a8bd
UH
445 g_snprintf(command, sizeof(command),
446 (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
447 j + 1, (*model->coupling_options)[idx]);
448 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
449 sr_scpi_get_opc(sdi->conn) != SR_OK)
450 return SR_ERR;
1203acc7 451 state->analog_channels[j].coupling = idx;
bd633efa 452 ret = SR_OK;
89280b1a 453 break;
e131be0a
GT
454 case SR_CONF_LOGIC_THRESHOLD:
455 if (!cg)
456 return SR_ERR_CHANNEL_GROUP;
457 if (cg_type != CG_DIGITAL)
458 return SR_ERR_NA;
459 if (!model)
460 return SR_ERR_ARG;
461 if ((idx = std_str_idx(data, *model->logic_threshold, model->num_logic_threshold)) < 0)
462 return SR_ERR_ARG;
463 if ((j = std_cg_idx(cg, devc->digital_groups, model->digital_pods)) < 0)
464 return SR_ERR_ARG;
aac30633
GT
465 /* Check if the threshold command is based on the POD or digital channel index. */
466 if (model->logic_threshold_for_pod)
467 i = j + 1;
468 else
a12456f1 469 i = j * DIGITAL_CHANNELS_PER_POD;
e131be0a
GT
470 g_snprintf(command, sizeof(command),
471 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_THRESHOLD],
aac30633 472 i, (*model->logic_threshold)[idx]);
e131be0a
GT
473 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
474 sr_scpi_get_opc(sdi->conn) != SR_OK)
475 return SR_ERR;
476 state->digital_pods[j].threshold = idx;
477 ret = SR_OK;
478 break;
479 case SR_CONF_LOGIC_THRESHOLD_CUSTOM:
480 if (!cg)
481 return SR_ERR_CHANNEL_GROUP;
482 if (cg_type != CG_DIGITAL)
483 return SR_ERR_NA;
484 if (!model)
485 return SR_ERR_ARG;
486 if ((j = std_cg_idx(cg, devc->digital_groups, model->digital_pods)) < 0)
487 return SR_ERR_ARG;
488 tmp_d = g_variant_get_double(data);
489 if (tmp_d < -2.0 || tmp_d > 8.0)
490 return SR_ERR;
491 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
aac30633
GT
492 /* Check if the threshold command is based on the POD or digital channel index. */
493 if (model->logic_threshold_for_pod)
494 idx = j + 1;
495 else
a12456f1 496 idx = j * DIGITAL_CHANNELS_PER_POD;
aac30633
GT
497 /* Try to support different dialects exhaustively. */
498 for (i = 0; i < model->num_logic_threshold; i++) {
499 if (!strcmp("USER2", (*model->logic_threshold)[i])) {
500 g_snprintf(command, sizeof(command),
501 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_USER_THRESHOLD],
502 idx, 2, float_str); /* USER2 */
503 g_snprintf(command2, sizeof(command2),
504 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_THRESHOLD],
505 idx, "USER2");
506 break;
507 }
508 if (!strcmp("USER", (*model->logic_threshold)[i])) {
509 g_snprintf(command, sizeof(command),
510 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_USER_THRESHOLD],
511 idx, float_str);
512 g_snprintf(command2, sizeof(command2),
513 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_THRESHOLD],
514 idx, "USER");
515 break;
516 }
517 if (!strcmp("MAN", (*model->logic_threshold)[i])) {
518 g_snprintf(command, sizeof(command),
519 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_USER_THRESHOLD],
520 idx, float_str);
521 g_snprintf(command2, sizeof(command2),
522 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_THRESHOLD],
523 idx, "MAN");
524 break;
525 }
526 }
e131be0a
GT
527 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
528 sr_scpi_get_opc(sdi->conn) != SR_OK)
529 return SR_ERR;
aac30633 530 if (sr_scpi_send(sdi->conn, command2) != SR_OK ||
e131be0a
GT
531 sr_scpi_get_opc(sdi->conn) != SR_OK)
532 return SR_ERR;
533 state->digital_pods[j].user_threshold = tmp_d;
534 ret = SR_OK;
535 break;
06a3e78a
DJ
536 default:
537 ret = SR_ERR_NA;
13f2b9d7 538 break;
06a3e78a
DJ
539 }
540
23e1ea7a
DJ
541 if (ret == SR_OK && update_sample_rate)
542 ret = hmo_update_sample_rate(sdi);
543
06a3e78a
DJ
544 return ret;
545}
546
dd7a72ea
UH
547static int config_list(uint32_t key, GVariant **data,
548 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
06a3e78a 549{
6ec3ef9b
AJ
550 int cg_type = CG_NONE;
551 struct dev_context *devc = NULL;
329733d9 552 const struct scope_config *model = NULL;
13f2b9d7 553
4d399734
UH
554 if (sdi) {
555 devc = sdi->priv;
6ec3ef9b
AJ
556 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
557 return SR_ERR;
13f2b9d7 558
6ec3ef9b
AJ
559 model = devc->model_config;
560 }
06a3e78a 561
06a3e78a 562 switch (key) {
226363c4 563 case SR_CONF_SCAN_OPTIONS:
53012da6 564 *data = std_gvar_array_u32(ARRAY_AND_SIZE(scanopts));
226363c4 565 break;
13f2b9d7 566 case SR_CONF_DEVICE_OPTIONS:
61233697 567 if (!cg) {
6ec3ef9b 568 if (model)
769561cb 569 *data = std_gvar_array_u32(*model->devopts, model->num_devopts);
6ec3ef9b 570 else
53012da6 571 *data = std_gvar_array_u32(ARRAY_AND_SIZE(drvopts));
61e77667 572 } else if (cg_type == CG_ANALOG) {
769561cb 573 *data = std_gvar_array_u32(*model->devopts_cg_analog, model->num_devopts_cg_analog);
e131be0a
GT
574 } else if (cg_type == CG_DIGITAL) {
575 *data = std_gvar_array_u32(*model->devopts_cg_digital, model->num_devopts_cg_digital);
13f2b9d7 576 } else {
105df674 577 *data = std_gvar_array_u32(NULL, 0);
13f2b9d7
DJ
578 }
579 break;
13f2b9d7 580 case SR_CONF_COUPLING:
61233697 581 if (!cg)
660e398f 582 return SR_ERR_CHANNEL_GROUP;
b0e80e9a
GS
583 if (!model)
584 return SR_ERR_ARG;
692716f5 585 *data = g_variant_new_strv(*model->coupling_options, model->num_coupling_options);
13f2b9d7 586 break;
13f2b9d7 587 case SR_CONF_TRIGGER_SOURCE:
6ec3ef9b
AJ
588 if (!model)
589 return SR_ERR_ARG;
692716f5 590 *data = g_variant_new_strv(*model->trigger_sources, model->num_trigger_sources);
13f2b9d7 591 break;
bbabdaf1 592 case SR_CONF_TRIGGER_SLOPE:
6ec3ef9b
AJ
593 if (!model)
594 return SR_ERR_ARG;
692716f5 595 *data = g_variant_new_strv(*model->trigger_slopes, model->num_trigger_slopes);
bbabdaf1 596 break;
13f2b9d7 597 case SR_CONF_TIMEBASE:
6ec3ef9b
AJ
598 if (!model)
599 return SR_ERR_ARG;
58ffcf97 600 *data = std_gvar_tuple_array(*model->timebases, model->num_timebases);
13f2b9d7 601 break;
13f2b9d7 602 case SR_CONF_VDIV:
61233697 603 if (!cg)
660e398f 604 return SR_ERR_CHANNEL_GROUP;
b0e80e9a
GS
605 if (!model)
606 return SR_ERR_ARG;
58ffcf97 607 *data = std_gvar_tuple_array(*model->vdivs, model->num_vdivs);
13f2b9d7 608 break;
e131be0a
GT
609 case SR_CONF_LOGIC_THRESHOLD:
610 if (!cg)
611 return SR_ERR_CHANNEL_GROUP;
612 if (!model)
613 return SR_ERR_ARG;
614 *data = g_variant_new_strv(*model->logic_threshold, model->num_logic_threshold);
615 break;
06a3e78a
DJ
616 default:
617 return SR_ERR_NA;
618 }
619
13f2b9d7 620 return SR_OK;
06a3e78a
DJ
621}
622
13f2b9d7 623SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi)
06a3e78a 624{
13f2b9d7 625 char command[MAX_COMMAND_SIZE];
ba7dd8bb 626 struct sr_channel *ch;
13f2b9d7 627 struct dev_context *devc;
329733d9 628 const struct scope_config *model;
13f2b9d7
DJ
629
630 devc = sdi->priv;
631 model = devc->model_config;
632
ba7dd8bb 633 ch = devc->current_channel->data;
13f2b9d7 634
ba7dd8bb 635 switch (ch->type) {
3f239f08 636 case SR_CHANNEL_ANALOG:
13f2b9d7
DJ
637 g_snprintf(command, sizeof(command),
638 (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA],
65a6794e
GS
639#ifdef WORDS_BIGENDIAN
640 "MSBF",
641#else
642 "LSBF",
643#endif
ba7dd8bb 644 ch->index + 1);
13f2b9d7 645 break;
3f239f08 646 case SR_CHANNEL_LOGIC:
13f2b9d7
DJ
647 g_snprintf(command, sizeof(command),
648 (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA],
a12456f1 649 ch->index / DIGITAL_CHANNELS_PER_POD + 1);
13f2b9d7
DJ
650 break;
651 default:
ba7dd8bb 652 sr_err("Invalid channel type.");
13f2b9d7
DJ
653 break;
654 }
655
656 return sr_scpi_send(sdi->conn, command);
657}
658
ba7dd8bb 659static int hmo_check_channels(GSList *channels)
13f2b9d7
DJ
660{
661 GSList *l;
ba7dd8bb 662 struct sr_channel *ch;
4889acef
GS
663 gboolean enabled_chan[MAX_ANALOG_CHANNEL_COUNT];
664 gboolean enabled_pod[MAX_DIGITAL_GROUP_COUNT];
665 size_t idx;
666
667 /* Preset "not enabled" for all channels / pods. */
668 for (idx = 0; idx < ARRAY_SIZE(enabled_chan); idx++)
669 enabled_chan[idx] = FALSE;
670 for (idx = 0; idx < ARRAY_SIZE(enabled_pod); idx++)
671 enabled_pod[idx] = FALSE;
672
673 /*
674 * Determine which channels / pods are required for the caller's
675 * specified configuration.
676 */
ba7dd8bb
UH
677 for (l = channels; l; l = l->next) {
678 ch = l->data;
679 switch (ch->type) {
3f239f08 680 case SR_CHANNEL_ANALOG:
4889acef
GS
681 idx = ch->index;
682 if (idx < ARRAY_SIZE(enabled_chan))
683 enabled_chan[idx] = TRUE;
13f2b9d7 684 break;
3f239f08 685 case SR_CHANNEL_LOGIC:
a12456f1 686 idx = ch->index / DIGITAL_CHANNELS_PER_POD;
4889acef
GS
687 if (idx < ARRAY_SIZE(enabled_pod))
688 enabled_pod[idx] = TRUE;
13f2b9d7 689 break;
13f2b9d7
DJ
690 default:
691 return SR_ERR;
692 }
693 }
694
4889acef
GS
695 /*
696 * Check for resource conflicts. Some channels can be either
697 * analog or digital, but never both at the same time.
698 *
699 * Note that the constraints might depend on the specific model.
700 * These tests might need some adjustment when support for more
701 * models gets added to the driver.
702 */
703 if (enabled_pod[0] && enabled_chan[2])
704 return SR_ERR;
705 if (enabled_pod[1] && enabled_chan[3])
13f2b9d7 706 return SR_ERR;
13f2b9d7
DJ
707 return SR_OK;
708}
709
ba7dd8bb 710static int hmo_setup_channels(const struct sr_dev_inst *sdi)
13f2b9d7
DJ
711{
712 GSList *l;
713 unsigned int i;
23e1ea7a 714 gboolean *pod_enabled, setup_changed;
13f2b9d7 715 char command[MAX_COMMAND_SIZE];
13f2b9d7 716 struct scope_state *state;
329733d9 717 const struct scope_config *model;
ba7dd8bb 718 struct sr_channel *ch;
13f2b9d7 719 struct dev_context *devc;
23f43dff 720 struct sr_scpi_dev_inst *scpi;
addbb09b 721 int ret;
13f2b9d7
DJ
722
723 devc = sdi->priv;
23f43dff 724 scpi = sdi->conn;
13f2b9d7
DJ
725 state = devc->model_state;
726 model = devc->model_config;
23e1ea7a 727 setup_changed = FALSE;
13f2b9d7
DJ
728
729 pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods);
730
ba7dd8bb
UH
731 for (l = sdi->channels; l; l = l->next) {
732 ch = l->data;
733 switch (ch->type) {
3f239f08 734 case SR_CHANNEL_ANALOG:
ba7dd8bb 735 if (ch->enabled == state->analog_channels[ch->index].state)
082972e8
UH
736 break;
737 g_snprintf(command, sizeof(command),
738 (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE],
ba7dd8bb 739 ch->index + 1, ch->enabled);
13f2b9d7 740
4fc4b8e7
UH
741 if (sr_scpi_send(scpi, command) != SR_OK) {
742 g_free(pod_enabled);
082972e8 743 return SR_ERR;
4fc4b8e7 744 }
ba7dd8bb 745 state->analog_channels[ch->index].state = ch->enabled;
23e1ea7a 746 setup_changed = TRUE;
89280b1a 747 break;
3f239f08 748 case SR_CHANNEL_LOGIC:
13f2b9d7
DJ
749 /*
750 * A digital POD needs to be enabled for every group of
a12456f1 751 * DIGITAL_CHANNELS_PER_POD channels.
13f2b9d7 752 */
ba7dd8bb 753 if (ch->enabled)
a12456f1 754 pod_enabled[ch->index / DIGITAL_CHANNELS_PER_POD] = TRUE;
13f2b9d7 755
ba7dd8bb 756 if (ch->enabled == state->digital_channels[ch->index])
082972e8
UH
757 break;
758 g_snprintf(command, sizeof(command),
759 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE],
ba7dd8bb 760 ch->index, ch->enabled);
13f2b9d7 761
4fc4b8e7
UH
762 if (sr_scpi_send(scpi, command) != SR_OK) {
763 g_free(pod_enabled);
082972e8 764 return SR_ERR;
4fc4b8e7 765 }
13f2b9d7 766
ba7dd8bb 767 state->digital_channels[ch->index] = ch->enabled;
23e1ea7a 768 setup_changed = TRUE;
89280b1a 769 break;
13f2b9d7 770 default:
b0e80e9a 771 g_free(pod_enabled);
13f2b9d7
DJ
772 return SR_ERR;
773 }
774 }
775
addbb09b 776 ret = SR_OK;
d1ac53cc 777 for (i = 0; i < model->digital_pods; i++) {
e131be0a 778 if (state->digital_pods[i].state == pod_enabled[i])
082972e8
UH
779 continue;
780 g_snprintf(command, sizeof(command),
781 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE],
d1ac53cc 782 i + 1, pod_enabled[i]);
addbb09b
GS
783 if (sr_scpi_send(scpi, command) != SR_OK) {
784 ret = SR_ERR;
785 break;
786 }
e131be0a 787 state->digital_pods[i].state = pod_enabled[i];
23e1ea7a 788 setup_changed = TRUE;
13f2b9d7 789 }
13f2b9d7 790 g_free(pod_enabled);
addbb09b
GS
791 if (ret != SR_OK)
792 return ret;
13f2b9d7 793
23e1ea7a
DJ
794 if (setup_changed && hmo_update_sample_rate(sdi) != SR_OK)
795 return SR_ERR;
796
13f2b9d7
DJ
797 return SR_OK;
798}
799
695dc859 800static int dev_acquisition_start(const struct sr_dev_inst *sdi)
13f2b9d7
DJ
801{
802 GSList *l;
1b0f62df 803 gboolean digital_added[MAX_DIGITAL_GROUP_COUNT];
e06875b2 804 size_t group, pod_count;
ba7dd8bb 805 struct sr_channel *ch;
13f2b9d7 806 struct dev_context *devc;
23f43dff 807 struct sr_scpi_dev_inst *scpi;
40a75c8e 808 int ret;
06a3e78a 809
23f43dff 810 scpi = sdi->conn;
13f2b9d7 811 devc = sdi->priv;
13f2b9d7 812
d779dcac
GT
813 devc->num_samples = 0;
814 devc->num_frames = 0;
815
1b0f62df
GS
816 /* Preset empty results. */
817 for (group = 0; group < ARRAY_SIZE(digital_added); group++)
818 digital_added[group] = FALSE;
db81fbb5
SA
819 g_slist_free(devc->enabled_channels);
820 devc->enabled_channels = NULL;
821
1b0f62df 822 /*
e06875b2
GS
823 * Contruct the list of enabled channels. Determine the highest
824 * number of digital pods involved in the acquisition.
1b0f62df 825 */
e06875b2 826 pod_count = 0;
ba7dd8bb
UH
827 for (l = sdi->channels; l; l = l->next) {
828 ch = l->data;
829 if (!ch->enabled)
082972e8 830 continue;
1b0f62df 831 /* Only add a single digital channel per group (pod). */
a12456f1 832 group = ch->index / DIGITAL_CHANNELS_PER_POD;
1b0f62df 833 if (ch->type != SR_CHANNEL_LOGIC || !digital_added[group]) {
ba7dd8bb
UH
834 devc->enabled_channels = g_slist_append(
835 devc->enabled_channels, ch);
e06875b2 836 if (ch->type == SR_CHANNEL_LOGIC) {
1b0f62df 837 digital_added[group] = TRUE;
e06875b2
GS
838 if (pod_count < group + 1)
839 pod_count = group + 1;
840 }
13f2b9d7
DJ
841 }
842 }
ba7dd8bb 843 if (!devc->enabled_channels)
13f2b9d7 844 return SR_ERR;
e06875b2
GS
845 devc->pod_count = pod_count;
846 devc->logic_data = NULL;
13f2b9d7 847
1b0f62df
GS
848 /*
849 * Check constraints. Some channels can be either analog or
850 * digital, but not both at the same time.
851 */
ba7dd8bb
UH
852 if (hmo_check_channels(devc->enabled_channels) != SR_OK) {
853 sr_err("Invalid channel configuration specified!");
40a75c8e
GS
854 ret = SR_ERR_NA;
855 goto free_enabled;
13f2b9d7
DJ
856 }
857
1b0f62df
GS
858 /*
859 * Configure the analog and digital channels and the
860 * corresponding digital pods.
861 */
ba7dd8bb
UH
862 if (hmo_setup_channels(sdi) != SR_OK) {
863 sr_err("Failed to setup channel configuration!");
40a75c8e
GS
864 ret = SR_ERR;
865 goto free_enabled;
13f2b9d7
DJ
866 }
867
1b0f62df
GS
868 /*
869 * Start acquisition on the first enabled channel. The
870 * receive routine will continue driving the acquisition.
871 */
102f1239
BV
872 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
873 hmo_receive_data, (void *)sdi);
13f2b9d7 874
bee2b016 875 std_session_send_df_header(sdi);
13f2b9d7 876
ba7dd8bb 877 devc->current_channel = devc->enabled_channels;
13f2b9d7
DJ
878
879 return hmo_request_data(sdi);
40a75c8e
GS
880
881free_enabled:
882 g_slist_free(devc->enabled_channels);
883 devc->enabled_channels = NULL;
884 return ret;
06a3e78a
DJ
885}
886
695dc859 887static int dev_acquisition_stop(struct sr_dev_inst *sdi)
06a3e78a 888{
13f2b9d7 889 struct dev_context *devc;
23f43dff 890 struct sr_scpi_dev_inst *scpi;
13f2b9d7 891
bee2b016 892 std_session_send_df_end(sdi);
66e3219d 893
13f2b9d7
DJ
894 devc = sdi->priv;
895
d779dcac 896 devc->num_samples = 0;
ef1a346b 897 devc->num_frames = 0;
ba7dd8bb
UH
898 g_slist_free(devc->enabled_channels);
899 devc->enabled_channels = NULL;
23f43dff 900 scpi = sdi->conn;
102f1239 901 sr_scpi_source_remove(sdi->session, scpi);
06a3e78a
DJ
902
903 return SR_OK;
904}
905
dd5c48a6 906static struct sr_dev_driver hameg_hmo_driver_info = {
06a3e78a 907 .name = "hameg-hmo",
89280b1a 908 .longname = "Hameg HMO",
06a3e78a 909 .api_version = 1,
c2fdcc25 910 .init = std_init,
700d6b64 911 .cleanup = std_cleanup,
06a3e78a 912 .scan = scan,
c01bf34c 913 .dev_list = std_dev_list,
06a3e78a
DJ
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,
41812aca 922 .context = NULL,
06a3e78a 923};
dd5c48a6 924SR_REGISTER_DEV_DRIVER(hameg_hmo_driver_info);