]> sigrok.org Git - libsigrok.git/blame - src/hardware/hameg-hmo/api.c
scpi: Move closing of discovered devices to sr_scpi_scan_resource().
[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",
31};
32
6ec3ef9b
AJ
33static const uint32_t drvopts[] = {
34 SR_CONF_OSCILLOSCOPE,
35};
36
584560f1 37static const uint32_t scanopts[] = {
13f2b9d7
DJ
38 SR_CONF_CONN,
39 SR_CONF_SERIALCOMM,
40};
41
719eff68 42enum {
61e77667
UH
43 CG_INVALID = -1,
44 CG_NONE,
45 CG_ANALOG,
46 CG_DIGITAL,
719eff68
UH
47};
48
4f840ce9 49static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
06a3e78a
DJ
50{
51 return std_init(sr_ctx, di, LOG_PREFIX);
52}
53
e9a62139
DJ
54static int check_manufacturer(const char *manufacturer)
55{
56 unsigned int i;
57
58 for (i = 0; i < ARRAY_SIZE(manufacturers); ++i)
59 if (!strcmp(manufacturer, manufacturers[i]))
60 return SR_OK;
61
62 return SR_ERR;
63}
64
ca28abd6 65static struct sr_dev_inst *hmo_probe_serial_device(struct sr_scpi_dev_inst *scpi)
e9a62139
DJ
66{
67 struct sr_dev_inst *sdi;
68 struct dev_context *devc;
69 struct sr_scpi_hw_info *hw_info;
e9a62139
DJ
70
71 sdi = NULL;
72 devc = NULL;
e9a62139
DJ
73 hw_info = NULL;
74
e9a62139
DJ
75 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
76 sr_info("Couldn't get IDN response.");
77 goto fail;
78 }
79
80 if (check_manufacturer(hw_info->manufacturer) != SR_OK)
81 goto fail;
82
aac29cc1 83 sdi = g_malloc0(sizeof(struct sr_dev_inst));
0af636be
UH
84 sdi->vendor = g_strdup(hw_info->manufacturer);
85 sdi->model = g_strdup(hw_info->model);
86 sdi->version = g_strdup(hw_info->firmware_version);
87 sdi->serial_num = g_strdup(hw_info->serial_number);
4f840ce9 88 sdi->driver = &hameg_hmo_driver_info;
b33db61c
SA
89 sdi->inst_type = SR_INST_SCPI;
90 sdi->conn = scpi;
b33db61c 91
e9a62139
DJ
92 sr_scpi_hw_info_free(hw_info);
93 hw_info = NULL;
94
f57d8ffe 95 devc = g_malloc0(sizeof(struct dev_context));
e9a62139 96
e9a62139 97 sdi->priv = devc;
e9a62139
DJ
98
99 if (hmo_init_device(sdi) != SR_OK)
100 goto fail;
101
102 return sdi;
103
104fail:
105 if (hw_info)
106 sr_scpi_hw_info_free(hw_info);
e9a62139
DJ
107 if (sdi)
108 sr_dev_inst_free(sdi);
b1f83103 109 g_free(devc);
e9a62139
DJ
110
111 return NULL;
112}
113
4f840ce9 114static GSList *scan(struct sr_dev_driver *di, GSList *options)
06a3e78a 115{
41812aca 116 return sr_scpi_scan(di->context, options, hmo_probe_serial_device);
06a3e78a
DJ
117}
118
4f840ce9 119static GSList *dev_list(const struct sr_dev_driver *di)
06a3e78a 120{
41812aca 121 return ((struct drv_context *)(di->context))->instances;
06a3e78a
DJ
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
4f840ce9 138static int dev_clear(const struct sr_dev_driver *di)
06a3e78a 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
4f840ce9 168static int cleanup(const struct sr_dev_driver *di)
06a3e78a 169{
4f840ce9 170 dev_clear(di);
06a3e78a 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;
329733d9 179 const struct scope_config *model;
13f2b9d7
DJ
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 204 struct dev_context *devc;
329733d9 205 const 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) {
bf622e6d 219 case SR_CONF_NUM_HDIV:
13f2b9d7
DJ
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 329 struct dev_context *devc;
329733d9 330 const struct scope_config *model;
13f2b9d7 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{
6ec3ef9b
AJ
498 int cg_type = CG_NONE;
499 struct dev_context *devc = NULL;
329733d9 500 const struct scope_config *model = NULL;
13f2b9d7 501
6ec3ef9b
AJ
502 if (sdi && (devc = sdi->priv)) {
503 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
504 return SR_ERR;
13f2b9d7 505
6ec3ef9b
AJ
506 model = devc->model_config;
507 }
06a3e78a 508
06a3e78a 509 switch (key) {
226363c4 510 case SR_CONF_SCAN_OPTIONS:
584560f1
BV
511 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
512 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
226363c4 513 break;
13f2b9d7 514 case SR_CONF_DEVICE_OPTIONS:
61e77667 515 if (cg_type == CG_NONE) {
6ec3ef9b
AJ
516 if (model)
517 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
518 model->devopts, model->num_devopts, sizeof(uint32_t));
519 else
520 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
521 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
61e77667 522 } else if (cg_type == CG_ANALOG) {
584560f1 523 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
f254bc4b 524 model->analog_devopts, model->num_analog_devopts,
584560f1 525 sizeof(uint32_t));
13f2b9d7 526 } else {
584560f1
BV
527 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
528 NULL, 0, sizeof(uint32_t));
13f2b9d7
DJ
529 }
530 break;
13f2b9d7 531 case SR_CONF_COUPLING:
61e77667 532 if (cg_type == CG_NONE)
660e398f 533 return SR_ERR_CHANNEL_GROUP;
13f2b9d7 534 *data = g_variant_new_strv(*model->coupling_options,
89280b1a 535 g_strv_length((char **)*model->coupling_options));
13f2b9d7 536 break;
13f2b9d7 537 case SR_CONF_TRIGGER_SOURCE:
6ec3ef9b
AJ
538 if (!model)
539 return SR_ERR_ARG;
13f2b9d7 540 *data = g_variant_new_strv(*model->trigger_sources,
89280b1a 541 g_strv_length((char **)*model->trigger_sources));
13f2b9d7 542 break;
bbabdaf1 543 case SR_CONF_TRIGGER_SLOPE:
6ec3ef9b
AJ
544 if (!model)
545 return SR_ERR_ARG;
bbabdaf1
DJ
546 *data = g_variant_new_strv(*model->trigger_slopes,
547 g_strv_length((char **)*model->trigger_slopes));
548 break;
13f2b9d7 549 case SR_CONF_TIMEBASE:
6ec3ef9b
AJ
550 if (!model)
551 return SR_ERR_ARG;
13f2b9d7
DJ
552 *data = build_tuples(model->timebases, model->num_timebases);
553 break;
13f2b9d7 554 case SR_CONF_VDIV:
61e77667 555 if (cg_type == CG_NONE)
660e398f 556 return SR_ERR_CHANNEL_GROUP;
13f2b9d7
DJ
557 *data = build_tuples(model->vdivs, model->num_vdivs);
558 break;
06a3e78a
DJ
559 default:
560 return SR_ERR_NA;
561 }
562
13f2b9d7 563 return SR_OK;
06a3e78a
DJ
564}
565
13f2b9d7 566SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi)
06a3e78a 567{
13f2b9d7 568 char command[MAX_COMMAND_SIZE];
ba7dd8bb 569 struct sr_channel *ch;
13f2b9d7 570 struct dev_context *devc;
329733d9 571 const struct scope_config *model;
13f2b9d7
DJ
572
573 devc = sdi->priv;
574 model = devc->model_config;
575
ba7dd8bb 576 ch = devc->current_channel->data;
13f2b9d7 577
ba7dd8bb 578 switch (ch->type) {
3f239f08 579 case SR_CHANNEL_ANALOG:
13f2b9d7
DJ
580 g_snprintf(command, sizeof(command),
581 (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA],
ba7dd8bb 582 ch->index + 1);
13f2b9d7 583 break;
3f239f08 584 case SR_CHANNEL_LOGIC:
13f2b9d7
DJ
585 g_snprintf(command, sizeof(command),
586 (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA],
ba7dd8bb 587 ch->index < 8 ? 1 : 2);
13f2b9d7
DJ
588 break;
589 default:
ba7dd8bb 590 sr_err("Invalid channel type.");
13f2b9d7
DJ
591 break;
592 }
593
594 return sr_scpi_send(sdi->conn, command);
595}
596
ba7dd8bb 597static int hmo_check_channels(GSList *channels)
13f2b9d7
DJ
598{
599 GSList *l;
ba7dd8bb 600 struct sr_channel *ch;
89280b1a 601 gboolean enabled_pod1, enabled_pod2, enabled_chan3, enabled_chan4;
13f2b9d7 602
89280b1a 603 enabled_pod1 = enabled_pod2 = enabled_chan3 = enabled_chan4 = FALSE;
13f2b9d7 604
ba7dd8bb
UH
605 for (l = channels; l; l = l->next) {
606 ch = l->data;
607 switch (ch->type) {
3f239f08 608 case SR_CHANNEL_ANALOG:
ba7dd8bb 609 if (ch->index == 2)
13f2b9d7 610 enabled_chan3 = TRUE;
ba7dd8bb 611 else if (ch->index == 3)
13f2b9d7
DJ
612 enabled_chan4 = TRUE;
613 break;
3f239f08 614 case SR_CHANNEL_LOGIC:
ba7dd8bb 615 if (ch->index < 8)
13f2b9d7
DJ
616 enabled_pod1 = TRUE;
617 else
618 enabled_pod2 = TRUE;
619 break;
13f2b9d7
DJ
620 default:
621 return SR_ERR;
622 }
623 }
624
625 if ((enabled_pod1 && enabled_chan3) ||
626 (enabled_pod2 && enabled_chan4))
627 return SR_ERR;
628
629 return SR_OK;
630}
631
ba7dd8bb 632static int hmo_setup_channels(const struct sr_dev_inst *sdi)
13f2b9d7
DJ
633{
634 GSList *l;
635 unsigned int i;
23e1ea7a 636 gboolean *pod_enabled, setup_changed;
13f2b9d7 637 char command[MAX_COMMAND_SIZE];
13f2b9d7 638 struct scope_state *state;
329733d9 639 const struct scope_config *model;
ba7dd8bb 640 struct sr_channel *ch;
13f2b9d7 641 struct dev_context *devc;
23f43dff 642 struct sr_scpi_dev_inst *scpi;
13f2b9d7
DJ
643
644 devc = sdi->priv;
23f43dff 645 scpi = sdi->conn;
13f2b9d7
DJ
646 state = devc->model_state;
647 model = devc->model_config;
23e1ea7a 648 setup_changed = FALSE;
13f2b9d7
DJ
649
650 pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods);
651
ba7dd8bb
UH
652 for (l = sdi->channels; l; l = l->next) {
653 ch = l->data;
654 switch (ch->type) {
3f239f08 655 case SR_CHANNEL_ANALOG:
ba7dd8bb 656 if (ch->enabled == state->analog_channels[ch->index].state)
082972e8
UH
657 break;
658 g_snprintf(command, sizeof(command),
659 (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE],
ba7dd8bb 660 ch->index + 1, ch->enabled);
13f2b9d7 661
23f43dff 662 if (sr_scpi_send(scpi, command) != SR_OK)
082972e8 663 return SR_ERR;
ba7dd8bb 664 state->analog_channels[ch->index].state = ch->enabled;
23e1ea7a 665 setup_changed = TRUE;
89280b1a 666 break;
3f239f08 667 case SR_CHANNEL_LOGIC:
13f2b9d7
DJ
668 /*
669 * A digital POD needs to be enabled for every group of
ba7dd8bb 670 * 8 channels.
13f2b9d7 671 */
ba7dd8bb
UH
672 if (ch->enabled)
673 pod_enabled[ch->index < 8 ? 0 : 1] = TRUE;
13f2b9d7 674
ba7dd8bb 675 if (ch->enabled == state->digital_channels[ch->index])
082972e8
UH
676 break;
677 g_snprintf(command, sizeof(command),
678 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE],
ba7dd8bb 679 ch->index, ch->enabled);
13f2b9d7 680
23f43dff 681 if (sr_scpi_send(scpi, command) != SR_OK)
082972e8 682 return SR_ERR;
13f2b9d7 683
ba7dd8bb 684 state->digital_channels[ch->index] = ch->enabled;
23e1ea7a 685 setup_changed = TRUE;
89280b1a 686 break;
13f2b9d7
DJ
687 default:
688 return SR_ERR;
689 }
690 }
691
692 for (i = 1; i <= model->digital_pods; ++i) {
082972e8
UH
693 if (state->digital_pods[i - 1] == pod_enabled[i - 1])
694 continue;
695 g_snprintf(command, sizeof(command),
696 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE],
697 i, pod_enabled[i - 1]);
23f43dff 698 if (sr_scpi_send(scpi, command) != SR_OK)
082972e8
UH
699 return SR_ERR;
700 state->digital_pods[i - 1] = pod_enabled[i - 1];
23e1ea7a 701 setup_changed = TRUE;
13f2b9d7
DJ
702 }
703
704 g_free(pod_enabled);
705
23e1ea7a
DJ
706 if (setup_changed && hmo_update_sample_rate(sdi) != SR_OK)
707 return SR_ERR;
708
13f2b9d7
DJ
709 return SR_OK;
710}
711
712static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
713{
714 GSList *l;
715 gboolean digital_added;
ba7dd8bb 716 struct sr_channel *ch;
13f2b9d7 717 struct dev_context *devc;
23f43dff 718 struct sr_scpi_dev_inst *scpi;
06a3e78a
DJ
719
720 if (sdi->status != SR_ST_ACTIVE)
721 return SR_ERR_DEV_CLOSED;
722
23f43dff 723 scpi = sdi->conn;
13f2b9d7
DJ
724 devc = sdi->priv;
725 digital_added = FALSE;
726
db81fbb5
SA
727 g_slist_free(devc->enabled_channels);
728 devc->enabled_channels = NULL;
729
ba7dd8bb
UH
730 for (l = sdi->channels; l; l = l->next) {
731 ch = l->data;
732 if (!ch->enabled)
082972e8 733 continue;
ba7dd8bb 734 /* Only add a single digital channel. */
3f239f08 735 if (ch->type != SR_CHANNEL_LOGIC || !digital_added) {
ba7dd8bb
UH
736 devc->enabled_channels = g_slist_append(
737 devc->enabled_channels, ch);
3f239f08 738 if (ch->type == SR_CHANNEL_LOGIC)
082972e8 739 digital_added = TRUE;
13f2b9d7
DJ
740 }
741 }
06a3e78a 742
ba7dd8bb 743 if (!devc->enabled_channels)
13f2b9d7
DJ
744 return SR_ERR;
745
ba7dd8bb
UH
746 if (hmo_check_channels(devc->enabled_channels) != SR_OK) {
747 sr_err("Invalid channel configuration specified!");
13f2b9d7
DJ
748 return SR_ERR_NA;
749 }
750
ba7dd8bb
UH
751 if (hmo_setup_channels(sdi) != SR_OK) {
752 sr_err("Failed to setup channel configuration!");
13f2b9d7
DJ
753 return SR_ERR;
754 }
755
102f1239
BV
756 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
757 hmo_receive_data, (void *)sdi);
13f2b9d7
DJ
758
759 /* Send header packet to the session bus. */
760 std_session_send_df_header(cb_data, LOG_PREFIX);
761
ba7dd8bb 762 devc->current_channel = devc->enabled_channels;
13f2b9d7
DJ
763
764 return hmo_request_data(sdi);
06a3e78a
DJ
765}
766
767static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
768{
13f2b9d7 769 struct dev_context *devc;
23f43dff 770 struct sr_scpi_dev_inst *scpi;
66e3219d 771 struct sr_datafeed_packet packet;
13f2b9d7 772
06a3e78a
DJ
773 (void)cb_data;
774
66e3219d
DJ
775 packet.type = SR_DF_END;
776 packet.payload = NULL;
777 sr_session_send(sdi, &packet);
778
06a3e78a
DJ
779 if (sdi->status != SR_ST_ACTIVE)
780 return SR_ERR_DEV_CLOSED;
781
13f2b9d7
DJ
782 devc = sdi->priv;
783
ef1a346b 784 devc->num_frames = 0;
ba7dd8bb
UH
785 g_slist_free(devc->enabled_channels);
786 devc->enabled_channels = NULL;
23f43dff 787 scpi = sdi->conn;
102f1239 788 sr_scpi_source_remove(sdi->session, scpi);
06a3e78a
DJ
789
790 return SR_OK;
791}
792
793SR_PRIV struct sr_dev_driver hameg_hmo_driver_info = {
794 .name = "hameg-hmo",
89280b1a 795 .longname = "Hameg HMO",
06a3e78a
DJ
796 .api_version = 1,
797 .init = init,
798 .cleanup = cleanup,
799 .scan = scan,
800 .dev_list = dev_list,
801 .dev_clear = dev_clear,
802 .config_get = config_get,
803 .config_set = config_set,
804 .config_list = config_list,
805 .dev_open = dev_open,
806 .dev_close = dev_close,
807 .dev_acquisition_start = dev_acquisition_start,
808 .dev_acquisition_stop = dev_acquisition_stop,
41812aca 809 .context = NULL,
06a3e78a 810};