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