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