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