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