]> sigrok.org Git - libsigrok.git/blob - src/hardware/scpi-pps/api.c
Fix a few "value never read" scan-build warnings.
[libsigrok.git] / src / hardware / scpi-pps / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2014 Bert Vermeulen <bert@biot.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 <string.h>
22 #include <strings.h>
23 #include "scpi.h"
24 #include "protocol.h"
25
26 SR_PRIV struct sr_dev_driver scpi_pps_driver_info;
27
28 static const uint32_t scanopts[] = {
29         SR_CONF_CONN,
30         SR_CONF_SERIALCOMM,
31 };
32
33 static const uint32_t drvopts[] = {
34         SR_CONF_POWER_SUPPLY,
35 };
36
37 static const struct pps_channel_instance pci[] = {
38         { SR_MQ_VOLTAGE, SCPI_CMD_GET_MEAS_VOLTAGE, "V" },
39         { SR_MQ_CURRENT, SCPI_CMD_GET_MEAS_CURRENT, "I" },
40         { SR_MQ_POWER, SCPI_CMD_GET_MEAS_POWER, "P" },
41         { SR_MQ_FREQUENCY, SCPI_CMD_GET_MEAS_FREQUENCY, "F" },
42 };
43
44 static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
45 {
46         return std_init(sr_ctx, di, LOG_PREFIX);
47 }
48
49 static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
50 {
51         struct dev_context *devc;
52         struct sr_dev_inst *sdi;
53         struct sr_scpi_hw_info *hw_info;
54         struct sr_channel_group *cg;
55         struct sr_channel *ch;
56         const struct scpi_pps *device;
57         struct pps_channel *pch;
58         struct channel_spec *channels;
59         struct channel_group_spec *channel_groups, *cgs;
60         struct pps_channel_group *pcg;
61         GRegex *model_re;
62         GMatchInfo *model_mi;
63         GSList *l;
64         uint64_t mask;
65         unsigned int num_channels, num_channel_groups, ch_num, ch_idx, i, j;
66         int ret;
67         const char *vendor;
68         char ch_name[16];
69
70         if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
71                 sr_info("Couldn't get IDN response.");
72                 return NULL;
73         }
74
75         device = NULL;
76         for (i = 0; i < num_pps_profiles; i++) {
77                 vendor = sr_vendor_alias(hw_info->manufacturer);
78                 if (g_ascii_strcasecmp(vendor, pps_profiles[i].vendor))
79                         continue;
80                 model_re = g_regex_new(pps_profiles[i].model, 0, 0, NULL);
81                 if (g_regex_match(model_re, hw_info->model, 0, &model_mi))
82                         device = &pps_profiles[i];
83                 g_match_info_unref(model_mi);
84                 g_regex_unref(model_re);
85                 if (device)
86                         break;
87         }
88         if (!device) {
89                 sr_scpi_hw_info_free(hw_info);
90                 return NULL;
91         }
92
93         sdi = g_malloc0(sizeof(struct sr_dev_inst));
94         sdi->status = SR_ST_INACTIVE;
95         sdi->vendor = g_strdup(vendor);
96         sdi->model = g_strdup(hw_info->model);
97         sdi->version = g_strdup(hw_info->firmware_version);
98         sdi->conn = scpi;
99         sdi->driver = &scpi_pps_driver_info;
100         sdi->inst_type = SR_INST_SCPI;
101         sdi->serial_num = g_strdup(hw_info->serial_number);
102
103         devc = g_malloc0(sizeof(struct dev_context));
104         devc->device = device;
105         sdi->priv = devc;
106
107         if (device->num_channels) {
108                 /* Static channels and groups. */
109                 channels = (struct channel_spec *)device->channels;
110                 num_channels = device->num_channels;
111                 channel_groups = (struct channel_group_spec *)device->channel_groups;
112                 num_channel_groups = device->num_channel_groups;
113         } else {
114                 /* Channels and groups need to be probed. */
115                 ret = device->probe_channels(sdi, hw_info, &channels, &num_channels,
116                                 &channel_groups, &num_channel_groups);
117                 if (ret != SR_OK) {
118                         sr_err("Failed to probe for channels.");
119                         return NULL;
120                 }
121                 /*
122                  * Since these were dynamically allocated, we'll need to free them
123                  * later.
124                  */
125                 devc->channels = channels;
126                 devc->channel_groups = channel_groups;
127         }
128
129         ch_idx = 0;
130         for (ch_num = 0; ch_num < num_channels; ch_num++) {
131                 /* Create one channel per measurable output unit. */
132                 for (i = 0; i < ARRAY_SIZE(pci); i++) {
133                         if (!scpi_cmd_get(devc->device->commands, pci[i].command))
134                                 continue;
135                         g_snprintf(ch_name, 16, "%s%s", pci[i].prefix,
136                                         channels[ch_num].name);
137                         ch = sr_channel_new(sdi, ch_idx++, SR_CHANNEL_ANALOG, TRUE,
138                                         ch_name);
139                         pch = g_malloc0(sizeof(struct pps_channel));
140                         pch->hw_output_idx = ch_num;
141                         pch->hwname = channels[ch_num].name;
142                         pch->mq = pci[i].mq;
143                         ch->priv = pch;
144                 }
145         }
146
147         for (i = 0; i < num_channel_groups; i++) {
148                 cgs = &channel_groups[i];
149                 cg = g_malloc0(sizeof(struct sr_channel_group));
150                 cg->name = g_strdup(cgs->name);
151                 for (j = 0, mask = 1; j < 64; j++, mask <<= 1) {
152                         if (cgs->channel_index_mask & mask) {
153                                 for (l = sdi->channels; l; l = l->next) {
154                                         ch = l->data;
155                                         pch = ch->priv;
156                                         if (pch->hw_output_idx == j)
157                                                 cg->channels = g_slist_append(cg->channels, ch);
158                                 }
159                         }
160                 }
161                 pcg = g_malloc0(sizeof(struct pps_channel_group));
162                 pcg->features = cgs->features;
163                 cg->priv = pcg;
164                 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
165         }
166
167         sr_scpi_hw_info_free(hw_info);
168         hw_info = NULL;
169
170         scpi_cmd(sdi, devc->device->commands, SCPI_CMD_LOCAL);
171         sr_scpi_close(scpi);
172
173         return sdi;
174 }
175
176 static GSList *scan(struct sr_dev_driver *di, GSList *options)
177 {
178         return sr_scpi_scan(di->context, options, probe_device);
179 }
180
181 static GSList *dev_list(const struct sr_dev_driver *di)
182 {
183         return ((struct drv_context *)(di->context))->instances;
184 }
185
186 static int dev_clear(const struct sr_dev_driver *di)
187 {
188         return std_dev_clear(di, NULL);
189 }
190
191 static int dev_open(struct sr_dev_inst *sdi)
192 {
193         struct dev_context *devc;
194         struct sr_scpi_dev_inst *scpi;
195         GVariant *beeper;
196
197         if (sdi->status != SR_ST_INACTIVE)
198                 return SR_ERR;
199
200         scpi = sdi->conn;
201         if (sr_scpi_open(scpi) < 0)
202                 return SR_ERR;
203
204         sdi->status = SR_ST_ACTIVE;
205
206         devc = sdi->priv;
207         scpi_cmd(sdi, devc->device->commands, SCPI_CMD_REMOTE);
208         devc = sdi->priv;
209         devc->beeper_was_set = FALSE;
210         if (scpi_cmd_resp(sdi, devc->device->commands, &beeper,
211                         G_VARIANT_TYPE_BOOLEAN, SCPI_CMD_BEEPER) == SR_OK) {
212                 if (g_variant_get_boolean(beeper)) {
213                         devc->beeper_was_set = TRUE;
214                         scpi_cmd(sdi, devc->device->commands, SCPI_CMD_BEEPER_DISABLE);
215                 }
216                 g_variant_unref(beeper);
217         }
218
219         return SR_OK;
220 }
221
222 static int dev_close(struct sr_dev_inst *sdi)
223 {
224         struct sr_scpi_dev_inst *scpi;
225         struct dev_context *devc;
226
227         if (sdi->status != SR_ST_ACTIVE)
228                 return SR_ERR_DEV_CLOSED;
229
230         devc = sdi->priv;
231         scpi = sdi->conn;
232         if (scpi) {
233                 if (devc->beeper_was_set)
234                         scpi_cmd(sdi, devc->device->commands, SCPI_CMD_BEEPER_ENABLE);
235                 scpi_cmd(sdi, devc->device->commands, SCPI_CMD_LOCAL);
236                 sr_scpi_close(scpi);
237                 sdi->status = SR_ST_INACTIVE;
238         }
239
240         return SR_OK;
241 }
242
243 static void clear_helper(void *priv)
244 {
245         struct dev_context *devc;
246
247         devc = priv;
248         g_free(devc->channels);
249         g_free(devc->channel_groups);
250         g_free(devc);
251 }
252
253 static int cleanup(const struct sr_dev_driver *di)
254 {
255         return std_dev_clear(di, clear_helper);
256 }
257
258 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
259                 const struct sr_channel_group *cg)
260 {
261         struct dev_context *devc;
262         const GVariantType *gvtype;
263         unsigned int i;
264         int cmd, ret;
265         const char *s;
266
267         if (!sdi)
268                 return SR_ERR_ARG;
269
270         devc = sdi->priv;
271
272         if (cg) {
273                 /*
274                  * These options only apply to channel groups with a single
275                  * channel -- they're per-channel settings for the device.
276                  */
277
278                 /*
279                  * Config keys are handled below depending on whether a channel
280                  * group was provided by the frontend. However some of these
281                  * take a CG on one PPS but not on others. Check the device's
282                  * profile for that here, and NULL out the channel group as needed.
283                  */
284                 for (i = 0; i < devc->device->num_devopts; i++) {
285                         if (devc->device->devopts[i] == key) {
286                                 cg = NULL;
287                                 break;
288                         }
289                 }
290         }
291
292         gvtype = NULL;
293         cmd = -1;
294         switch (key) {
295         case SR_CONF_ENABLED:
296                 gvtype = G_VARIANT_TYPE_BOOLEAN;
297                 cmd = SCPI_CMD_GET_OUTPUT_ENABLED;
298                 break;
299         case SR_CONF_VOLTAGE:
300                 gvtype = G_VARIANT_TYPE_DOUBLE;
301                 cmd = SCPI_CMD_GET_MEAS_VOLTAGE;
302                 break;
303         case SR_CONF_VOLTAGE_TARGET:
304                 gvtype = G_VARIANT_TYPE_DOUBLE;
305                 cmd = SCPI_CMD_GET_VOLTAGE_TARGET;
306                 break;
307         case SR_CONF_OUTPUT_FREQUENCY:
308                 gvtype = G_VARIANT_TYPE_DOUBLE;
309                 cmd = SCPI_CMD_GET_MEAS_FREQUENCY;
310                 break;
311         case SR_CONF_OUTPUT_FREQUENCY_TARGET:
312                 gvtype = G_VARIANT_TYPE_DOUBLE;
313                 cmd = SCPI_CMD_GET_FREQUENCY_TARGET;
314                 break;
315         case SR_CONF_CURRENT:
316                 gvtype = G_VARIANT_TYPE_DOUBLE;
317                 cmd = SCPI_CMD_GET_MEAS_CURRENT;
318                 break;
319         case SR_CONF_CURRENT_LIMIT:
320                 gvtype = G_VARIANT_TYPE_DOUBLE;
321                 cmd = SCPI_CMD_GET_CURRENT_LIMIT;
322                 break;
323         case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
324                 gvtype = G_VARIANT_TYPE_BOOLEAN;
325                 cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ENABLED;
326                 break;
327         case SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE:
328                 gvtype = G_VARIANT_TYPE_BOOLEAN;
329                 cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ACTIVE;
330                 break;
331         case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
332                 gvtype = G_VARIANT_TYPE_DOUBLE;
333                 cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_THRESHOLD;
334                 break;
335         case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
336                 gvtype = G_VARIANT_TYPE_BOOLEAN;
337                 cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ENABLED;
338                 break;
339         case SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE:
340                 gvtype = G_VARIANT_TYPE_BOOLEAN;
341                 cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ACTIVE;
342                 break;
343         case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
344                 gvtype = G_VARIANT_TYPE_DOUBLE;
345                 cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_THRESHOLD;
346                 break;
347         case SR_CONF_OVER_TEMPERATURE_PROTECTION:
348                 gvtype = G_VARIANT_TYPE_BOOLEAN;
349                 cmd = SCPI_CMD_GET_OVER_TEMPERATURE_PROTECTION;
350                 break;
351         case SR_CONF_REGULATION:
352                 gvtype = G_VARIANT_TYPE_STRING;
353                 cmd = SCPI_CMD_GET_OUTPUT_REGULATION;
354         }
355         if (!gvtype)
356                 return SR_ERR_NA;
357
358         if (cg)
359                 select_channel(sdi, cg->channels->data);
360         ret = scpi_cmd_resp(sdi, devc->device->commands, data, gvtype, cmd);
361
362         if (cmd == SCPI_CMD_GET_OUTPUT_REGULATION) {
363                 /*
364                  * The Rigol DP800 series return CV/CC/UR, Philips PM2800
365                  * return VOLT/CURR. We always return a GVariant string in
366                  * the Rigol notation.
367                  */
368                 s = g_variant_get_string(*data, NULL);
369                 if (!strcmp(s, "VOLT")) {
370                         g_variant_unref(*data);
371                         *data = g_variant_new_string("CV");
372                 } else if (!strcmp(s, "CURR")) {
373                         g_variant_unref(*data);
374                         *data = g_variant_new_string("CC");
375                 }
376
377                 s = g_variant_get_string(*data, NULL);
378                 if (strcmp(s, "CV") && strcmp(s, "CC") && strcmp(s, "UR")) {
379                         sr_dbg("Unknown response to SCPI_CMD_GET_OUTPUT_REGULATION: %s", s);
380                         ret = SR_ERR_DATA;
381                 }
382         }
383
384         return ret;
385 }
386
387 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
388                 const struct sr_channel_group *cg)
389 {
390         struct dev_context *devc;
391         double d;
392         int ret;
393
394         if (!sdi)
395                 return SR_ERR_ARG;
396
397         if (sdi->status != SR_ST_ACTIVE)
398                 return SR_ERR_DEV_CLOSED;
399
400         if (cg)
401                 /* Channel group specified. */
402                 select_channel(sdi, cg->channels->data);
403
404         devc = sdi->priv;
405
406         switch (key) {
407         case SR_CONF_ENABLED:
408                 if (g_variant_get_boolean(data))
409                         ret = scpi_cmd(sdi, devc->device->commands,
410                                         SCPI_CMD_SET_OUTPUT_ENABLE);
411                 else
412                         ret = scpi_cmd(sdi, devc->device->commands,
413                                         SCPI_CMD_SET_OUTPUT_DISABLE);
414                 break;
415         case SR_CONF_VOLTAGE_TARGET:
416                 d = g_variant_get_double(data);
417                 ret = scpi_cmd(sdi, devc->device->commands,
418                                 SCPI_CMD_SET_VOLTAGE_TARGET, d);
419                 break;
420         case SR_CONF_OUTPUT_FREQUENCY_TARGET:
421                 d = g_variant_get_double(data);
422                 ret = scpi_cmd(sdi, devc->device->commands,
423                                 SCPI_CMD_SET_FREQUENCY_TARGET, d);
424                 break;
425         case SR_CONF_CURRENT_LIMIT:
426                 d = g_variant_get_double(data);
427                 ret = scpi_cmd(sdi, devc->device->commands,
428                                 SCPI_CMD_SET_CURRENT_LIMIT, d);
429                 break;
430         case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
431                 if (g_variant_get_boolean(data))
432                         ret = scpi_cmd(sdi, devc->device->commands,
433                                         SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_ENABLE);
434                 else
435                         ret = scpi_cmd(sdi, devc->device->commands,
436                                         SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_DISABLE);
437                 break;
438         case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
439                 d = g_variant_get_double(data);
440                 ret = scpi_cmd(sdi, devc->device->commands,
441                                 SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_THRESHOLD, d);
442                 break;
443         case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
444                 if (g_variant_get_boolean(data))
445                         ret = scpi_cmd(sdi, devc->device->commands,
446                                         SCPI_CMD_SET_OVER_CURRENT_PROTECTION_ENABLE);
447                 else
448                         ret = scpi_cmd(sdi, devc->device->commands,
449                                         SCPI_CMD_SET_OVER_CURRENT_PROTECTION_DISABLE);
450                 break;
451         case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
452                 d = g_variant_get_double(data);
453                 ret = scpi_cmd(sdi, devc->device->commands,
454                                 SCPI_CMD_SET_OVER_CURRENT_PROTECTION_THRESHOLD, d);
455                 break;
456         case SR_CONF_OVER_TEMPERATURE_PROTECTION:
457                 if (g_variant_get_boolean(data))
458                         ret = scpi_cmd(sdi, devc->device->commands,
459                                         SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_ENABLE);
460                 else
461                         ret = scpi_cmd(sdi, devc->device->commands,
462                                         SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_DISABLE);
463                 break;
464         default:
465                 ret = SR_ERR_NA;
466         }
467
468         return ret;
469 }
470
471 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
472                 const struct sr_channel_group *cg)
473 {
474         struct dev_context *devc;
475         struct sr_channel *ch;
476         const struct channel_spec *ch_spec;
477         GVariant *gvar;
478         GVariantBuilder gvb;
479         int ret, i;
480         const char *s[16];
481
482         /* Always available, even without sdi. */
483         if (key == SR_CONF_SCAN_OPTIONS) {
484                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
485                                 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
486                 return SR_OK;
487         } else if (key == SR_CONF_DEVICE_OPTIONS && !sdi) {
488                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
489                                 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
490                 return SR_OK;
491         }
492
493         if (!sdi)
494                 return SR_ERR_ARG;
495         devc = sdi->priv;
496
497         ret = SR_OK;
498         if (!cg) {
499                 /* No channel group: global options. */
500                 switch (key) {
501                 case SR_CONF_DEVICE_OPTIONS:
502                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
503                                         devc->device->devopts, devc->device->num_devopts,
504                                         sizeof(uint32_t));
505                         break;
506                 case SR_CONF_CHANNEL_CONFIG:
507                         /* Not used. */
508                         i = 0;
509                         if (devc->device->features & PPS_INDEPENDENT)
510                                 s[i++] = "Independent";
511                         if (devc->device->features & PPS_SERIES)
512                                 s[i++] = "Series";
513                         if (devc->device->features & PPS_PARALLEL)
514                                 s[i++] = "Parallel";
515                         if (i == 0) {
516                                 /*
517                                  * Shouldn't happen: independent-only devices
518                                  * shouldn't advertise this option at all.
519                                  */
520                                 return SR_ERR_NA;
521                         }
522                         *data = g_variant_new_strv(s, i);
523                         break;
524                 default:
525                         return SR_ERR_NA;
526                 }
527         } else {
528                 /* Channel group specified. */
529                 /*
530                  * Per-channel-group options depending on a channel are actually
531                  * done with the first channel. Channel groups in PPS can have
532                  * more than one channel, but they will typically be of equal
533                  * specification for use in series or parallel mode.
534                  */
535                 ch = cg->channels->data;
536
537                 switch (key) {
538                 case SR_CONF_DEVICE_OPTIONS:
539                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
540                                         devc->device->devopts_cg, devc->device->num_devopts_cg,
541                                         sizeof(uint32_t));
542                         break;
543                 case SR_CONF_VOLTAGE_TARGET:
544                         ch_spec = &(devc->device->channels[ch->index]);
545                         g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
546                         /* Min, max, write resolution. */
547                         for (i = 0; i < 3; i++) {
548                                 gvar = g_variant_new_double(ch_spec->voltage[i]);
549                                 g_variant_builder_add_value(&gvb, gvar);
550                         }
551                         *data = g_variant_builder_end(&gvb);
552                         break;
553                 case SR_CONF_OUTPUT_FREQUENCY_TARGET:
554                         ch_spec = &(devc->device->channels[ch->index]);
555                         g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
556                         /* Min, max, write resolution. */
557                         for (i = 0; i < 3; i++) {
558                                 gvar = g_variant_new_double(ch_spec->frequency[i]);
559                                 g_variant_builder_add_value(&gvb, gvar);
560                         }
561                         *data = g_variant_builder_end(&gvb);
562                         break;
563                 case SR_CONF_CURRENT_LIMIT:
564                         g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
565                         /* Min, max, step. */
566                         for (i = 0; i < 3; i++) {
567                                 ch_spec = &(devc->device->channels[ch->index]);
568                                 gvar = g_variant_new_double(ch_spec->current[i]);
569                                 g_variant_builder_add_value(&gvb, gvar);
570                         }
571                         *data = g_variant_builder_end(&gvb);
572                         break;
573                 default:
574                         return SR_ERR_NA;
575                 }
576         }
577
578         return ret;
579 }
580
581 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
582 {
583         struct dev_context *devc;
584         struct sr_scpi_dev_inst *scpi;
585         struct sr_channel *ch;
586         struct pps_channel *pch;
587         int cmd, ret;
588
589         if (sdi->status != SR_ST_ACTIVE)
590                 return SR_ERR_DEV_CLOSED;
591
592         devc = sdi->priv;
593         scpi = sdi->conn;
594         devc->cb_data = cb_data;
595
596         if ((ret = sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 10,
597                         scpi_pps_receive_data, (void *)sdi)) != SR_OK)
598                 return ret;
599         std_session_send_df_header(sdi, LOG_PREFIX);
600
601         /* Prime the pipe with the first channel's fetch. */
602         ch = sr_next_enabled_channel(sdi, NULL);
603         pch = ch->priv;
604         if ((ret = select_channel(sdi, ch)) < 0)
605                 return ret;
606         if (pch->mq == SR_MQ_VOLTAGE)
607                 cmd = SCPI_CMD_GET_MEAS_VOLTAGE;
608         else if (pch->mq == SR_MQ_FREQUENCY)
609                 cmd = SCPI_CMD_GET_MEAS_FREQUENCY;
610         else if (pch->mq == SR_MQ_CURRENT)
611                 cmd = SCPI_CMD_GET_MEAS_CURRENT;
612         else if (pch->mq == SR_MQ_POWER)
613                 cmd = SCPI_CMD_GET_MEAS_POWER;
614         else
615                 return SR_ERR;
616         scpi_cmd(sdi, devc->device->commands, cmd, pch->hwname);
617
618         return SR_OK;
619 }
620
621 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
622 {
623         struct sr_datafeed_packet packet;
624         struct sr_scpi_dev_inst *scpi;
625         float f;
626
627         (void)cb_data;
628
629         if (sdi->status != SR_ST_ACTIVE)
630                 return SR_ERR_DEV_CLOSED;
631
632         scpi = sdi->conn;
633
634         /*
635          * A requested value is certainly on the way. Retrieve it now,
636          * to avoid leaving the device in a state where it's not expecting
637          * commands.
638          */
639         sr_scpi_get_float(scpi, NULL, &f);
640         sr_scpi_source_remove(sdi->session, scpi);
641
642         packet.type = SR_DF_END;
643         sr_session_send(sdi, &packet);
644
645         return SR_OK;
646 }
647
648 SR_PRIV struct sr_dev_driver scpi_pps_driver_info = {
649         .name = "scpi-pps",
650         .longname = "SCPI PPS",
651         .api_version = 1,
652         .init = init,
653         .cleanup = cleanup,
654         .scan = scan,
655         .dev_list = dev_list,
656         .dev_clear = dev_clear,
657         .config_get = config_get,
658         .config_set = config_set,
659         .config_list = config_list,
660         .dev_open = dev_open,
661         .dev_close = dev_close,
662         .dev_acquisition_start = dev_acquisition_start,
663         .dev_acquisition_stop = dev_acquisition_stop,
664         .context = NULL,
665 };