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