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