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