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