]> sigrok.org Git - libsigrok.git/blob - src/hardware/lecroy-xstream/api.c
sr_dev_inst_free(): Allow NULL as argument.
[libsigrok.git] / src / hardware / lecroy-xstream / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2017 Sven Schnelle <svens@stackframe.org>
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 static struct sr_dev_driver lecroy_xstream_driver_info;
26
27 static const char *manufacturers[] = {
28         "LECROY",
29 };
30
31 static const uint32_t scanopts[] = {
32         SR_CONF_CONN,
33 };
34
35 static const uint32_t drvopts[] = {
36         SR_CONF_OSCILLOSCOPE,
37 };
38
39 static const uint32_t devopts[] = {
40         SR_CONF_LIMIT_FRAMES | SR_CONF_GET | SR_CONF_SET,
41         SR_CONF_SAMPLERATE | SR_CONF_GET,
42         SR_CONF_TIMEBASE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
43         SR_CONF_NUM_HDIV | SR_CONF_GET,
44         SR_CONF_HORIZ_TRIGGERPOS | SR_CONF_GET | SR_CONF_SET,
45         SR_CONF_TRIGGER_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
46         SR_CONF_TRIGGER_SLOPE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
47 };
48
49 static const uint32_t analog_devopts[] = {
50         SR_CONF_NUM_VDIV | SR_CONF_GET,
51         SR_CONF_VDIV | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
52         SR_CONF_COUPLING | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
53 };
54
55 static int check_manufacturer(const char *manufacturer)
56 {
57         unsigned int i;
58
59         for (i = 0; i < ARRAY_SIZE(manufacturers); i++)
60                 if (!strcmp(manufacturer, manufacturers[i]))
61                         return SR_OK;
62
63         return SR_ERR;
64 }
65
66 static struct sr_dev_inst *probe_serial_device(struct sr_scpi_dev_inst *scpi)
67 {
68         struct sr_dev_inst *sdi;
69         struct dev_context *devc;
70         struct sr_scpi_hw_info *hw_info;
71
72         sdi = NULL;
73         devc = NULL;
74         hw_info = NULL;
75
76         if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
77                 sr_info("Couldn't get IDN response.");
78                 goto fail;
79         }
80
81         if (check_manufacturer(hw_info->manufacturer) != SR_OK)
82                 goto fail;
83
84         sdi = g_malloc0(sizeof(struct sr_dev_inst));
85         sdi->vendor = g_strdup(hw_info->manufacturer);
86         sdi->model = g_strdup(hw_info->model);
87         sdi->version = g_strdup(hw_info->firmware_version);
88         sdi->serial_num = g_strdup(hw_info->serial_number);
89         sdi->driver = &lecroy_xstream_driver_info;
90         sdi->inst_type = SR_INST_SCPI;
91         sdi->conn = scpi;
92
93         sr_scpi_hw_info_free(hw_info);
94         hw_info = NULL;
95
96         devc = g_malloc0(sizeof(struct dev_context));
97
98         sdi->priv = devc;
99
100         if (lecroy_xstream_init_device(sdi) != SR_OK)
101                 goto fail;
102
103         return sdi;
104
105 fail:
106         sr_scpi_hw_info_free(hw_info);
107         sr_dev_inst_free(sdi);
108         g_free(devc);
109
110         return NULL;
111 }
112
113 static GSList *scan(struct sr_dev_driver *di, GSList *options)
114 {
115         return sr_scpi_scan(di->context, options, probe_serial_device);
116 }
117
118 static void clear_helper(void *priv)
119 {
120         struct dev_context *devc;
121
122         devc = priv;
123
124         lecroy_xstream_state_free(devc->model_state);
125
126         g_free(devc->analog_groups);
127
128         g_free(devc);
129 }
130
131 static int dev_clear(const struct sr_dev_driver *di)
132 {
133         return std_dev_clear(di, clear_helper);
134 }
135
136 static int dev_open(struct sr_dev_inst *sdi)
137 {
138         if (sdi->status != SR_ST_ACTIVE && sr_scpi_open(sdi->conn) != SR_OK)
139                 return SR_ERR;
140
141         if (lecroy_xstream_state_get(sdi) != SR_OK)
142                 return SR_ERR;
143
144         sdi->status = SR_ST_ACTIVE;
145
146         return SR_OK;
147 }
148
149 static int dev_close(struct sr_dev_inst *sdi)
150 {
151         if (sdi->status == SR_ST_INACTIVE)
152                 return SR_OK;
153
154         sr_scpi_close(sdi->conn);
155
156         sdi->status = SR_ST_INACTIVE;
157
158         return SR_OK;
159 }
160
161 static int config_get(uint32_t key, GVariant **data,
162         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
163 {
164         int ret;
165         unsigned int i;
166         struct dev_context *devc;
167         const struct scope_config *model;
168         struct scope_state *state;
169
170         if (!sdi)
171                 return SR_ERR_ARG;
172
173         devc = sdi->priv;
174
175         ret = SR_ERR_NA;
176         model = devc->model_config;
177         state = devc->model_state;
178         *data = NULL;
179
180         switch (key) {
181         case SR_CONF_NUM_HDIV:
182                 *data = g_variant_new_int32(model->num_xdivs);
183                 ret = SR_OK;
184                 break;
185         case SR_CONF_TIMEBASE:
186                 *data = g_variant_new("(tt)",
187                                 model->timebases[state->timebase].p,
188                                 model->timebases[state->timebase].q);
189                 ret = SR_OK;
190                 break;
191         case SR_CONF_NUM_VDIV:
192                 for (i = 0; i < model->analog_channels; i++) {
193                         if (cg != devc->analog_groups[i])
194                                 continue;
195                         *data = g_variant_new_int32(model->num_ydivs);
196                         ret = SR_OK;
197                 }
198                 break;
199         case SR_CONF_VDIV:
200                 for (i = 0; i < model->analog_channels; i++) {
201                         if (cg != devc->analog_groups[i])
202                                 continue;
203                         *data = g_variant_new("(tt)",
204                                 model->vdivs[state->analog_channels[i].vdiv].p,
205                                 model->vdivs[state->analog_channels[i].vdiv].q);
206                         ret = SR_OK;
207                 }
208                 break;
209         case SR_CONF_TRIGGER_SOURCE:
210                 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
211                 ret = SR_OK;
212                 break;
213         case SR_CONF_TRIGGER_SLOPE:
214                 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
215                 ret = SR_OK;
216                 break;
217         case SR_CONF_HORIZ_TRIGGERPOS:
218                 *data = g_variant_new_double(state->horiz_triggerpos);
219                 ret = SR_OK;
220                 break;
221         case SR_CONF_COUPLING:
222                 for (i = 0; i < model->analog_channels; i++) {
223                         if (cg != devc->analog_groups[i])
224                                 continue;
225                         *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[i].coupling]);
226                         ret = SR_OK;
227                 }
228                 break;
229         case SR_CONF_SAMPLERATE:
230                 *data = g_variant_new_uint64(state->sample_rate);
231                 ret = SR_OK;
232                 break;
233         case SR_CONF_ENABLED:
234                 *data = g_variant_new_boolean(FALSE);
235                 ret = SR_OK;
236                 break;
237         default:
238                 ret = SR_ERR_NA;
239         }
240
241         return ret;
242 }
243
244 static GVariant *build_tuples(const struct sr_rational *array, unsigned int n)
245 {
246         unsigned int i;
247         GVariant *rational[2];
248         GVariantBuilder gvb;
249
250         g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
251
252         for (i = 0; i < n; i++) {
253                 rational[0] = g_variant_new_uint64(array[i].p);
254                 rational[1] = g_variant_new_uint64(array[i].q);
255
256                 /* FIXME: Valgrind reports a memory leak here. */
257                 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
258         }
259
260         return g_variant_builder_end(&gvb);
261 }
262
263 static int config_set(uint32_t key, GVariant *data,
264         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
265 {
266         int ret;
267         unsigned int i, j;
268         char command[MAX_COMMAND_SIZE];
269         struct dev_context *devc;
270         const struct scope_config *model;
271         struct scope_state *state;
272         const char *tmp;
273         int64_t p;
274         uint64_t q;
275         double tmp_d;
276         gboolean update_sample_rate;
277
278         if (!sdi)
279                 return SR_ERR_ARG;
280
281         devc = sdi->priv;
282
283         model = devc->model_config;
284         state = devc->model_state;
285         update_sample_rate = FALSE;
286
287         ret = SR_ERR_NA;
288
289         switch (key) {
290         case SR_CONF_LIMIT_FRAMES:
291                 devc->frame_limit = g_variant_get_uint64(data);
292                 ret = SR_OK;
293                 break;
294         case SR_CONF_TRIGGER_SOURCE:
295                 tmp = g_variant_get_string(data, NULL);
296                 for (i = 0; (*model->trigger_sources)[i]; i++) {
297                         if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
298                                 continue;
299                         state->trigger_source = i;
300                         g_snprintf(command, sizeof(command),
301                                         "SET TRIGGER SOURCE %s",
302                                         (*model->trigger_sources)[i]);
303
304                         ret = sr_scpi_send(sdi->conn, command);
305                         break;
306                 }
307                 break;
308         case SR_CONF_VDIV:
309                 g_variant_get(data, "(tt)", &p, &q);
310
311                 for (i = 0; i < model->num_vdivs; i++) {
312                         if (p != model->vdivs[i].p || q != model->vdivs[i].q)
313                                 continue;
314                         for (j = 1; j <= model->analog_channels; j++) {
315                                 if (cg != devc->analog_groups[j - 1])
316                                         continue;
317                                 state->analog_channels[j - 1].vdiv = i;
318                                 g_snprintf(command, sizeof(command),
319                                                 "C%d:VDIV %E", j, (float)p/q);
320
321                                 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
322                                     sr_scpi_get_opc(sdi->conn) != SR_OK)
323                                         return SR_ERR;
324
325                                 break;
326                         }
327
328                         ret = SR_OK;
329                         break;
330                 }
331                 break;
332         case SR_CONF_TIMEBASE:
333                 g_variant_get(data, "(tt)", &p, &q);
334
335                 for (i = 0; i < model->num_timebases; i++) {
336                         if (p != model->timebases[i].p ||
337                             q != model->timebases[i].q)
338                                 continue;
339                         state->timebase = i;
340                         g_snprintf(command, sizeof(command),
341                                         "TIME_DIV %E", (float)p/q);
342
343                         ret = sr_scpi_send(sdi->conn, command);
344                         update_sample_rate = TRUE;
345                         break;
346                 }
347                 break;
348         case SR_CONF_HORIZ_TRIGGERPOS:
349                 tmp_d = g_variant_get_double(data);
350
351                 if (tmp_d < 0.0 || tmp_d > 1.0)
352                         return SR_ERR;
353
354                 state->horiz_triggerpos = tmp_d;
355                 tmp_d = -(tmp_d - 0.5) *
356                         ((double)model->timebases[state->timebase].p /
357                          model->timebases[state->timebase].q)
358                          * model->num_xdivs;
359
360                 g_snprintf(command, sizeof(command), "TRIG POS %e S", tmp_d);
361
362                 ret = sr_scpi_send(sdi->conn, command);
363                 break;
364         case SR_CONF_TRIGGER_SLOPE:
365                 tmp = g_variant_get_string(data, NULL);
366                 for (i = 0; (*model->trigger_slopes)[i]; i++) {
367                         if (g_strcmp0(tmp, (*model->trigger_slopes)[i]) != 0)
368                                 continue;
369                         state->trigger_slope = i;
370                         g_snprintf(command, sizeof(command),
371                                         "SET TRIGGER SLOPE %s",
372                                         (*model->trigger_slopes)[i]);
373
374                         ret = sr_scpi_send(sdi->conn, command);
375                         break;
376                 }
377                 break;
378         case SR_CONF_COUPLING:
379                 tmp = g_variant_get_string(data, NULL);
380
381                 for (i = 0; (*model->coupling_options)[i]; i++) {
382                         if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
383                                 continue;
384                         for (j = 1; j <= model->analog_channels; j++) {
385                                 if (cg != devc->analog_groups[j - 1])
386                                         continue;
387                                 state->analog_channels[j - 1].coupling = i;
388
389                                 g_snprintf(command, sizeof(command),
390                                                 "C%d:COUPLING %s", j, tmp);
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                                 break;
396                         }
397
398                         ret = SR_OK;
399                         break;
400                 }
401                 break;
402         default:
403                 ret = SR_ERR_NA;
404                 break;
405         }
406
407         if (ret == SR_OK)
408                 ret = sr_scpi_get_opc(sdi->conn);
409
410         if (ret == SR_OK && update_sample_rate)
411                 ret = lecroy_xstream_update_sample_rate(sdi);
412
413         return ret;
414 }
415
416 static int config_list(uint32_t key, GVariant **data,
417         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
418 {
419         struct dev_context *devc = NULL;
420         const struct scope_config *model = NULL;
421
422         (void)cg;
423
424         /* SR_CONF_SCAN_OPTIONS is always valid, regardless of sdi or channel group. */
425         if (key == SR_CONF_SCAN_OPTIONS) {
426                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
427                                 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
428                 return SR_OK;
429         }
430
431         /* If sdi is NULL, nothing except SR_CONF_DEVICE_OPTIONS can be provided. */
432         if (key == SR_CONF_DEVICE_OPTIONS && !sdi) {
433                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
434                                 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
435                 return SR_OK;
436         }
437
438         /* Every other option requires a valid device instance. */
439         if (!sdi)
440                 return SR_ERR_ARG;
441
442         devc = sdi->priv;
443         model = devc->model_config;
444
445         switch (key) {
446         case SR_CONF_DEVICE_OPTIONS:
447                 if (!cg) {
448                         /* If cg is NULL, only the SR_CONF_DEVICE_OPTIONS that are not
449                          * specific to a channel group must be returned. */
450                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
451                                         devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
452                         return SR_OK;
453                 }
454                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
455                         analog_devopts, ARRAY_SIZE(analog_devopts),
456                         sizeof(uint32_t));
457                 break;
458         case SR_CONF_COUPLING:
459                 *data = g_variant_new_strv(*model->coupling_options,
460                            g_strv_length((char **)*model->coupling_options));
461                 break;
462         case SR_CONF_TRIGGER_SOURCE:
463                 if (!model)
464                         return SR_ERR_ARG;
465                 *data = g_variant_new_strv(*model->trigger_sources,
466                            g_strv_length((char **)*model->trigger_sources));
467                 break;
468         case SR_CONF_TRIGGER_SLOPE:
469                 if (!model)
470                         return SR_ERR_ARG;
471                 *data = g_variant_new_strv(*model->trigger_slopes,
472                            g_strv_length((char **)*model->trigger_slopes));
473                 break;
474         case SR_CONF_TIMEBASE:
475                 if (!model)
476                         return SR_ERR_ARG;
477                 *data = build_tuples(model->timebases, model->num_timebases);
478                 break;
479         case SR_CONF_VDIV:
480                 if (!model)
481                         return SR_ERR_ARG;
482                 *data = build_tuples(model->vdivs, model->num_vdivs);
483                 break;
484         default:
485                 return SR_ERR_NA;
486         }
487         return SR_OK;
488 }
489
490 SR_PRIV int lecroy_xstream_request_data(const struct sr_dev_inst *sdi)
491 {
492         char command[MAX_COMMAND_SIZE];
493         struct sr_channel *ch;
494         struct dev_context *devc;
495
496         devc = sdi->priv;
497
498         ch = devc->current_channel->data;
499
500         if (ch->type != SR_CHANNEL_ANALOG)
501                 return SR_ERR;
502
503         g_snprintf(command, sizeof(command),
504                 "COMM_FORMAT DEF9,WORD,BIN;C%d:WAVEFORM?", ch->index + 1);
505         return sr_scpi_send(sdi->conn, command);
506 }
507
508 static int setup_channels(const struct sr_dev_inst *sdi)
509 {
510         GSList *l;
511         gboolean setup_changed;
512         char command[MAX_COMMAND_SIZE];
513         struct scope_state *state;
514         struct sr_channel *ch;
515         struct dev_context *devc;
516         struct sr_scpi_dev_inst *scpi;
517
518         devc = sdi->priv;
519         scpi = sdi->conn;
520         state = devc->model_state;
521         setup_changed = FALSE;
522
523         for (l = sdi->channels; l; l = l->next) {
524                 ch = l->data;
525                 switch (ch->type) {
526                 case SR_CHANNEL_ANALOG:
527                         if (ch->enabled == state->analog_channels[ch->index].state)
528                                 break;
529                         g_snprintf(command, sizeof(command), "C%d:TRACE %s",
530                                    ch->index + 1, ch->enabled ? "ON" : "OFF");
531
532                         if (sr_scpi_send(scpi, command) != SR_OK)
533                                 return SR_ERR;
534                         state->analog_channels[ch->index].state = ch->enabled;
535                         setup_changed = TRUE;
536                         break;
537                 default:
538                         return SR_ERR;
539                 }
540         }
541
542         if (setup_changed && lecroy_xstream_update_sample_rate(sdi) != SR_OK)
543                 return SR_ERR;
544
545         return SR_OK;
546 }
547
548 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
549 {
550         GSList *l;
551         struct sr_channel *ch;
552         struct dev_context *devc;
553         int ret;
554         struct sr_scpi_dev_inst *scpi;
555
556         if (sdi->status != SR_ST_ACTIVE)
557                 return SR_ERR_DEV_CLOSED;
558
559         devc = sdi->priv;
560         scpi = sdi->conn;
561         /* Preset empty results. */
562         g_slist_free(devc->enabled_channels);
563         devc->enabled_channels = NULL;
564
565         /*
566          * Contruct the list of enabled channels. Determine the highest
567          * number of digital pods involved in the acquisition.
568          */
569
570         for (l = sdi->channels; l; l = l->next) {
571                 ch = l->data;
572                 if (!ch->enabled)
573                         continue;
574                 /* Only add a single digital channel per group (pod). */
575                 devc->enabled_channels = g_slist_append(
576                         devc->enabled_channels, ch);
577         }
578
579         if (!devc->enabled_channels)
580                 return SR_ERR;
581
582         /*
583          * Configure the analog channels and the
584          * corresponding digital pods.
585          */
586         if (setup_channels(sdi) != SR_OK) {
587                 sr_err("Failed to setup channel configuration!");
588                 ret = SR_ERR;
589                 goto free_enabled;
590         }
591
592         /*
593          * Start acquisition on the first enabled channel. The
594          * receive routine will continue driving the acquisition.
595          */
596         sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
597                         lecroy_xstream_receive_data, (void *)sdi);
598
599         std_session_send_df_header(sdi);
600
601         devc->current_channel = devc->enabled_channels;
602
603         return lecroy_xstream_request_data(sdi);
604
605 free_enabled:
606         g_slist_free(devc->enabled_channels);
607         devc->enabled_channels = NULL;
608
609         return ret;
610 }
611
612 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
613 {
614         struct dev_context *devc;
615         struct sr_scpi_dev_inst *scpi;
616
617         std_session_send_df_end(sdi);
618
619         if (sdi->status != SR_ST_ACTIVE)
620                 return SR_ERR_DEV_CLOSED;
621
622         devc = sdi->priv;
623
624         devc->num_frames = 0;
625         g_slist_free(devc->enabled_channels);
626         devc->enabled_channels = NULL;
627         scpi = sdi->conn;
628         sr_scpi_source_remove(sdi->session, scpi);
629
630         return SR_OK;
631 }
632
633 static struct sr_dev_driver lecroy_xstream_driver_info = {
634         .name = "lecroy-xstream",
635         .longname = "LeCroy X-Stream",
636         .api_version = 1,
637         .init = std_init,
638         .cleanup = std_cleanup,
639         .scan = scan,
640         .dev_list = std_dev_list,
641         .dev_clear = dev_clear,
642         .config_get = config_get,
643         .config_set = config_set,
644         .config_list = config_list,
645         .dev_open = dev_open,
646         .dev_close = dev_close,
647         .dev_acquisition_start = dev_acquisition_start,
648         .dev_acquisition_stop = dev_acquisition_stop,
649         .context = NULL,
650 };
651 SR_REGISTER_DEV_DRIVER(lecroy_xstream_driver_info);