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