scpi-pps: don't break SCPI devices when scanning for HP-IB devices
[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  * Copyright (C) 2017,2019 Frank Stettner <frank-stettner@gmx.net>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <config.h>
22 #include <string.h>
23 #include <strings.h>
24 #include "scpi.h"
25 #include "protocol.h"
26
27 static struct sr_dev_driver scpi_pps_driver_info;
28 static struct sr_dev_driver hp_ib_pps_driver_info;
29
30 static const uint32_t scanopts[] = {
31         SR_CONF_CONN,
32         SR_CONF_SERIALCOMM,
33 };
34
35 static const uint32_t drvopts[] = {
36         SR_CONF_POWER_SUPPLY,
37 };
38
39 static const struct pps_channel_instance pci[] = {
40         { SR_MQ_VOLTAGE, SCPI_CMD_GET_MEAS_VOLTAGE, "V" },
41         { SR_MQ_CURRENT, SCPI_CMD_GET_MEAS_CURRENT, "I" },
42         { SR_MQ_POWER, SCPI_CMD_GET_MEAS_POWER, "P" },
43         { SR_MQ_FREQUENCY, SCPI_CMD_GET_MEAS_FREQUENCY, "F" },
44 };
45
46 static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi,
47                 int (*get_hw_id)(struct sr_scpi_dev_inst *scpi,
48                 struct sr_scpi_hw_info **scpi_response))
49 {
50         struct dev_context *devc;
51         struct sr_dev_inst *sdi;
52         struct sr_scpi_hw_info *hw_info;
53         struct sr_channel_group *cg;
54         struct sr_channel *ch;
55         const struct scpi_pps *device;
56         struct pps_channel *pch;
57         struct channel_spec *channels;
58         struct channel_group_spec *channel_groups, *cgs;
59         struct pps_channel_group *pcg;
60         GRegex *model_re;
61         GMatchInfo *model_mi;
62         GSList *l;
63         uint64_t mask;
64         unsigned int num_channels, num_channel_groups, ch_num, ch_idx, i, j;
65         int ret;
66         const char *vendor;
67         char ch_name[16];
68
69         if (get_hw_id(scpi, &hw_info) != SR_OK) {
70                 sr_info("Couldn't get IDN response.");
71                 return NULL;
72         }
73
74         device = NULL;
75         for (i = 0; i < num_pps_profiles; i++) {
76                 vendor = sr_vendor_alias(hw_info->manufacturer);
77                 if (g_ascii_strcasecmp(vendor, pps_profiles[i].vendor))
78                         continue;
79                 model_re = g_regex_new(pps_profiles[i].model, 0, 0, NULL);
80                 if (g_regex_match(model_re, hw_info->model, 0, &model_mi))
81                         device = &pps_profiles[i];
82                 g_match_info_unref(model_mi);
83                 g_regex_unref(model_re);
84                 if (device)
85                         break;
86         }
87         if (!device) {
88                 sr_scpi_hw_info_free(hw_info);
89                 return NULL;
90         }
91
92         sdi = g_malloc0(sizeof(struct sr_dev_inst));
93         sdi->vendor = g_strdup(vendor);
94         sdi->model = g_strdup(hw_info->model);
95         sdi->version = g_strdup(hw_info->firmware_version);
96         sdi->conn = scpi;
97         sdi->driver = &scpi_pps_driver_info;
98         sdi->inst_type = SR_INST_SCPI;
99         sdi->serial_num = g_strdup(hw_info->serial_number);
100
101         devc = g_malloc0(sizeof(struct dev_context));
102         devc->device = device;
103         sr_sw_limits_init(&devc->limits);
104         sdi->priv = devc;
105
106         if (device->num_channels) {
107                 /* Static channels and groups. */
108                 channels = (struct channel_spec *)device->channels;
109                 num_channels = device->num_channels;
110                 channel_groups = (struct channel_group_spec *)device->channel_groups;
111                 num_channel_groups = device->num_channel_groups;
112         } else {
113                 /* Channels and groups need to be probed. */
114                 ret = device->probe_channels(sdi, hw_info, &channels, &num_channels,
115                                 &channel_groups, &num_channel_groups);
116                 if (ret != SR_OK) {
117                         sr_err("Failed to probe for channels.");
118                         return NULL;
119                 }
120                 /*
121                  * Since these were dynamically allocated, we'll need to free them
122                  * later.
123                  */
124                 devc->channels = channels;
125                 devc->channel_groups = channel_groups;
126         }
127
128         ch_idx = 0;
129         for (ch_num = 0; ch_num < num_channels; ch_num++) {
130                 /* Create one channel per measurable output unit. */
131                 for (i = 0; i < ARRAY_SIZE(pci); i++) {
132                         if (!sr_scpi_cmd_get(devc->device->commands, pci[i].command))
133                                 continue;
134                         g_snprintf(ch_name, 16, "%s%s", pci[i].prefix,
135                                         channels[ch_num].name);
136                         ch = sr_channel_new(sdi, ch_idx++, SR_CHANNEL_ANALOG, TRUE,
137                                         ch_name);
138                         pch = g_malloc0(sizeof(struct pps_channel));
139                         pch->hw_output_idx = ch_num;
140                         pch->hwname = channels[ch_num].name;
141                         pch->mq = pci[i].mq;
142                         ch->priv = pch;
143                 }
144         }
145
146         for (i = 0; i < num_channel_groups; i++) {
147                 cgs = &channel_groups[i];
148                 cg = g_malloc0(sizeof(struct sr_channel_group));
149                 cg->name = g_strdup(cgs->name);
150                 for (j = 0, mask = 1; j < 64; j++, mask <<= 1) {
151                         if (cgs->channel_index_mask & mask) {
152                                 for (l = sdi->channels; l; l = l->next) {
153                                         ch = l->data;
154                                         pch = ch->priv;
155                                         /* Add mqflags from channel_group_spec only to voltage
156                                          * and current channels.
157                                          */
158                                         if (pch->mq == SR_MQ_VOLTAGE || pch->mq == SR_MQ_CURRENT)
159                                                 pch->mqflags = cgs->mqflags;
160                                         else
161                                                 pch->mqflags = 0;
162                                         if (pch->hw_output_idx == j)
163                                                 cg->channels = g_slist_append(cg->channels, ch);
164                                 }
165                         }
166                 }
167                 pcg = g_malloc0(sizeof(struct pps_channel_group));
168                 pcg->features = cgs->features;
169                 cg->priv = pcg;
170                 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
171         }
172
173         sr_scpi_hw_info_free(hw_info);
174         hw_info = NULL;
175
176         /* Don't send SCPI_CMD_LOCAL for HP 66xxB using SCPI over GPIB. */
177         if (!(devc->device->dialect == SCPI_DIALECT_HP_66XXB &&
178                         scpi->transport == SCPI_TRANSPORT_LIBGPIB))
179                 sr_scpi_cmd(sdi, devc->device->commands, 0, NULL, SCPI_CMD_LOCAL);
180
181         return sdi;
182 }
183
184 static gchar *hpib_get_revision(struct sr_scpi_dev_inst *scpi)
185 {
186         int ret;
187         gboolean matches;
188         char *response;
189         GRegex *version_regex;
190
191         ret = sr_scpi_get_string(scpi, "ROM?", &response);
192         if (ret != SR_OK && !response)
193                 return NULL;
194
195         /* Example version string: "B01 B01" */
196         version_regex = g_regex_new("[A-Z][0-9]{2} [A-Z][0-9]{2}", 0, 0, NULL);
197         matches = g_regex_match(version_regex, response, 0, NULL);
198         g_regex_unref(version_regex);
199
200         if (!matches) {
201                 /* Not a valid version string. Ignore it. */
202                 g_free(response);
203                 response = NULL;
204         } else {
205                 /* Replace space with dot. */
206                 response[3] = '.';
207         }
208
209         return response;
210 }
211
212 /*
213  * This function assumes the response is in the form "HP<model_number>"
214  *
215  * HP made many GPIB (then called HP-IB) instruments before the SCPI command
216  * set was introduced into the standard. We haven't seen any non-HP instruments
217  * which respond to the "ID?" query, so assume all are HP for now.
218  */
219 static int hpib_get_hw_id(struct sr_scpi_dev_inst *scpi,
220                           struct sr_scpi_hw_info **scpi_response)
221 {
222         int ret;
223         char *response;
224         struct sr_scpi_hw_info *hw_info;
225
226         ret = sr_scpi_get_string(scpi, "ID?", &response);
227         if ((ret != SR_OK) || !response)
228                 return SR_ERR;
229
230         hw_info = g_malloc0(sizeof(struct sr_scpi_hw_info));
231
232         *scpi_response = hw_info;
233         hw_info->model = response;
234         hw_info->firmware_version = hpib_get_revision(scpi);
235         hw_info->manufacturer = g_strdup("HP");
236
237         return SR_OK;
238 }
239
240 static struct sr_dev_inst *probe_scpi_pps_device(struct sr_scpi_dev_inst *scpi)
241 {
242         return probe_device(scpi, sr_scpi_get_hw_id);
243 }
244
245 static struct sr_dev_inst *probe_hpib_pps_device(struct sr_scpi_dev_inst *scpi)
246 {
247         return probe_device(scpi, hpib_get_hw_id);
248 }
249
250 static GSList *scan_scpi_pps(struct sr_dev_driver *di, GSList *options)
251 {
252         return sr_scpi_scan(di->context, options, probe_scpi_pps_device);
253 }
254
255 static GSList *scan_hpib_pps(struct sr_dev_driver *di, GSList *options)
256 {
257         const char *conn;
258
259         /*
260          * Only scan for HP-IB devices when conn= was specified, to not
261          * break SCPI devices' operation.
262          */
263         conn = NULL;
264         (void)sr_serial_extract_options(options, &conn, NULL);
265         if (!conn)
266                 return NULL;
267
268         return sr_scpi_scan(di->context, options, probe_hpib_pps_device);
269 }
270
271 static int dev_open(struct sr_dev_inst *sdi)
272 {
273         struct dev_context *devc;
274         struct sr_scpi_dev_inst *scpi;
275         GVariant *beeper;
276
277         scpi = sdi->conn;
278         if (sr_scpi_open(scpi) < 0)
279                 return SR_ERR;
280
281         devc = sdi->priv;
282
283         /* Don't send SCPI_CMD_REMOTE for HP 66xxB using SCPI over GPIB. */
284         if (!(devc->device->dialect == SCPI_DIALECT_HP_66XXB &&
285                         scpi->transport == SCPI_TRANSPORT_LIBGPIB))
286                 sr_scpi_cmd(sdi, devc->device->commands, 0, NULL, SCPI_CMD_REMOTE);
287
288         devc->beeper_was_set = FALSE;
289         if (sr_scpi_cmd_resp(sdi, devc->device->commands, 0, NULL,
290                         &beeper, G_VARIANT_TYPE_BOOLEAN, SCPI_CMD_BEEPER) == SR_OK) {
291                 if (g_variant_get_boolean(beeper)) {
292                         devc->beeper_was_set = TRUE;
293                         sr_scpi_cmd(sdi, devc->device->commands,
294                                 0, NULL, SCPI_CMD_BEEPER_DISABLE);
295                 }
296                 g_variant_unref(beeper);
297         }
298
299         return SR_OK;
300 }
301
302 static int dev_close(struct sr_dev_inst *sdi)
303 {
304         struct sr_scpi_dev_inst *scpi;
305         struct dev_context *devc;
306
307         devc = sdi->priv;
308         scpi = sdi->conn;
309
310         if (!scpi)
311                 return SR_ERR_BUG;
312
313         if (devc->beeper_was_set)
314                 sr_scpi_cmd(sdi, devc->device->commands,
315                         0, NULL, SCPI_CMD_BEEPER_ENABLE);
316
317         /* Don't send SCPI_CMD_LOCAL for HP 66xxB using SCPI over GPIB. */
318         if (!(devc->device->dialect == SCPI_DIALECT_HP_66XXB &&
319                         scpi->transport == SCPI_TRANSPORT_LIBGPIB))
320                 sr_scpi_cmd(sdi, devc->device->commands, 0, NULL, SCPI_CMD_LOCAL);
321
322         return sr_scpi_close(scpi);
323 }
324
325 static void clear_helper(struct dev_context *devc)
326 {
327         g_free(devc->channels);
328         g_free(devc->channel_groups);
329 }
330
331 static int dev_clear(const struct sr_dev_driver *di)
332 {
333         return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
334 }
335
336 static int config_get(uint32_t key, GVariant **data,
337         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
338 {
339         struct dev_context *devc;
340         const GVariantType *gvtype;
341         unsigned int i;
342         int channel_group_cmd;
343         char *channel_group_name;
344         int cmd, ret;
345         const char *s;
346         int reg;
347         gboolean is_hmp_sqii;
348
349         if (!sdi)
350                 return SR_ERR_ARG;
351
352         devc = sdi->priv;
353
354         if (cg) {
355                 /*
356                  * These options only apply to channel groups with a single
357                  * channel -- they're per-channel settings for the device.
358                  */
359
360                 /*
361                  * Config keys are handled below depending on whether a channel
362                  * group was provided by the frontend. However some of these
363                  * take a CG on one PPS but not on others. Check the device's
364                  * profile for that here, and NULL out the channel group as needed.
365                  */
366                 for (i = 0; i < devc->device->num_devopts; i++) {
367                         if (devc->device->devopts[i] == key) {
368                                 cg = NULL;
369                                 break;
370                         }
371                 }
372         }
373
374         gvtype = NULL;
375         cmd = -1;
376         switch (key) {
377         case SR_CONF_ENABLED:
378                 gvtype = G_VARIANT_TYPE_BOOLEAN;
379                 cmd = SCPI_CMD_GET_OUTPUT_ENABLED;
380                 break;
381         case SR_CONF_VOLTAGE:
382                 gvtype = G_VARIANT_TYPE_DOUBLE;
383                 cmd = SCPI_CMD_GET_MEAS_VOLTAGE;
384                 break;
385         case SR_CONF_VOLTAGE_TARGET:
386                 gvtype = G_VARIANT_TYPE_DOUBLE;
387                 cmd = SCPI_CMD_GET_VOLTAGE_TARGET;
388                 break;
389         case SR_CONF_OUTPUT_FREQUENCY:
390                 gvtype = G_VARIANT_TYPE_DOUBLE;
391                 cmd = SCPI_CMD_GET_MEAS_FREQUENCY;
392                 break;
393         case SR_CONF_OUTPUT_FREQUENCY_TARGET:
394                 gvtype = G_VARIANT_TYPE_DOUBLE;
395                 cmd = SCPI_CMD_GET_FREQUENCY_TARGET;
396                 break;
397         case SR_CONF_CURRENT:
398                 gvtype = G_VARIANT_TYPE_DOUBLE;
399                 cmd = SCPI_CMD_GET_MEAS_CURRENT;
400                 break;
401         case SR_CONF_CURRENT_LIMIT:
402                 gvtype = G_VARIANT_TYPE_DOUBLE;
403                 cmd = SCPI_CMD_GET_CURRENT_LIMIT;
404                 break;
405         case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
406                 if (devc->device->dialect == SCPI_DIALECT_HMP) {
407                         /* OVP is always enabled. */
408                         *data = g_variant_new_boolean(TRUE);
409                         return 0;
410                 }
411                 gvtype = G_VARIANT_TYPE_BOOLEAN;
412                 cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ENABLED;
413                 break;
414         case SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE:
415                 if (devc->device->dialect == SCPI_DIALECT_HP_66XXB ||
416                         devc->device->dialect == SCPI_DIALECT_HP_COMP)
417                         gvtype = G_VARIANT_TYPE_STRING;
418                 else
419                         gvtype = G_VARIANT_TYPE_BOOLEAN;
420                 cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ACTIVE;
421                 break;
422         case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
423                 gvtype = G_VARIANT_TYPE_DOUBLE;
424                 cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_THRESHOLD;
425                 break;
426         case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
427                 gvtype = G_VARIANT_TYPE_BOOLEAN;
428                 cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ENABLED;
429                 break;
430         case SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE:
431                 if (devc->device->dialect == SCPI_DIALECT_HP_66XXB ||
432                         devc->device->dialect == SCPI_DIALECT_HP_COMP)
433                         gvtype = G_VARIANT_TYPE_STRING;
434                 else
435                         gvtype = G_VARIANT_TYPE_BOOLEAN;
436                 cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ACTIVE;
437                 break;
438         case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
439                 gvtype = G_VARIANT_TYPE_DOUBLE;
440                 cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_THRESHOLD;
441                 break;
442         case SR_CONF_OVER_TEMPERATURE_PROTECTION:
443                 if (devc->device->dialect == SCPI_DIALECT_HMP) {
444                         /* OTP is always enabled. */
445                         *data = g_variant_new_boolean(TRUE);
446                         return 0;
447                 }
448                 gvtype = G_VARIANT_TYPE_BOOLEAN;
449                 cmd = SCPI_CMD_GET_OVER_TEMPERATURE_PROTECTION;
450                 break;
451         case SR_CONF_OVER_TEMPERATURE_PROTECTION_ACTIVE:
452                 if (devc->device->dialect == SCPI_DIALECT_HP_66XXB ||
453                         devc->device->dialect == SCPI_DIALECT_HP_COMP ||
454                         devc->device->dialect == SCPI_DIALECT_HMP)
455                         gvtype = G_VARIANT_TYPE_STRING;
456                 else
457                         gvtype = G_VARIANT_TYPE_BOOLEAN;
458                 cmd = SCPI_CMD_GET_OVER_TEMPERATURE_PROTECTION_ACTIVE;
459                 break;
460         case SR_CONF_REGULATION:
461                 gvtype = G_VARIANT_TYPE_STRING;
462                 cmd = SCPI_CMD_GET_OUTPUT_REGULATION;
463                 break;
464         default:
465                 return sr_sw_limits_config_get(&devc->limits, key, data);
466         }
467         if (!gvtype)
468                 return SR_ERR_NA;
469
470         channel_group_cmd = 0;
471         channel_group_name = NULL;
472         if (cg) {
473                 channel_group_cmd = SCPI_CMD_SELECT_CHANNEL;
474                 channel_group_name = g_strdup(cg->name);
475         }
476
477         is_hmp_sqii = FALSE;
478         is_hmp_sqii |= cmd == SCPI_CMD_GET_OUTPUT_REGULATION;
479         is_hmp_sqii |= cmd == SCPI_CMD_GET_OVER_TEMPERATURE_PROTECTION_ACTIVE;
480         is_hmp_sqii &= devc->device->dialect == SCPI_DIALECT_HMP;
481         if (is_hmp_sqii) {
482                 if (!cg) {
483                         /* STAT:QUES:INST:ISUMx query requires channel spec. */
484                         sr_err("Need a channel group for regulation or OTP-active query.");
485                         return SR_ERR_NA;
486                 }
487                 ret = sr_scpi_cmd_resp(sdi, devc->device->commands,
488                         0, NULL, data, gvtype, cmd, channel_group_name);
489         } else {
490                 ret = sr_scpi_cmd_resp(sdi, devc->device->commands,
491                         channel_group_cmd, channel_group_name, data, gvtype, cmd);
492         }
493         g_free(channel_group_name);
494
495         /*
496          * Handle special cases
497          */
498
499         if (cmd == SCPI_CMD_GET_OUTPUT_REGULATION) {
500                 if (devc->device->dialect == SCPI_DIALECT_PHILIPS) {
501                         /*
502                         * The Philips PM2800 series returns VOLT/CURR. We always return
503                         * a GVariant string in the Rigol notation (CV/CC/UR).
504                         */
505                         s = g_variant_get_string(*data, NULL);
506                         if (!g_strcmp0(s, "VOLT")) {
507                                 g_variant_unref(*data);
508                                 *data = g_variant_new_string("CV");
509                         } else if (!g_strcmp0(s, "CURR")) {
510                                 g_variant_unref(*data);
511                                 *data = g_variant_new_string("CC");
512                         }
513                 }
514                 if (devc->device->dialect == SCPI_DIALECT_HP_COMP) {
515                         /* Evaluate Status Register from a HP 66xx in COMP mode. */
516                         s = g_variant_get_string(*data, NULL);
517                         sr_atoi(s, &reg);
518                         g_variant_unref(*data);
519                         if (reg & (1 << 0))
520                                 *data = g_variant_new_string("CV");
521                         else if (reg & (1 << 1))
522                                 *data = g_variant_new_string("CC");
523                         else if (reg & (1 << 2))
524                                 *data = g_variant_new_string("UR");
525                         else if (reg & (1 << 9))
526                                 *data = g_variant_new_string("CC-");
527                         else
528                                 *data = g_variant_new_string("");
529                 }
530                 if (devc->device->dialect == SCPI_DIALECT_HP_66XXB) {
531                         /* Evaluate Operational Status Register from a HP 66xxB. */
532                         s = g_variant_get_string(*data, NULL);
533                         sr_atoi(s, &reg);
534                         g_variant_unref(*data);
535                         if (reg & (1 << 8))
536                                 *data = g_variant_new_string("CV");
537                         else if (reg & (1 << 10))
538                                 *data = g_variant_new_string("CC");
539                         else if (reg & (1 << 11))
540                                 *data = g_variant_new_string("CC-");
541                         else
542                                 *data = g_variant_new_string("UR");
543                 }
544                 if (devc->device->dialect == SCPI_DIALECT_HMP) {
545                         /* Evaluate Condition Status Register from a HMP series device. */
546                         s = g_variant_get_string(*data, NULL);
547                         sr_atoi(s, &reg);
548                         g_variant_unref(*data);
549                         if (reg & (1 << 0))
550                                 *data = g_variant_new_string("CC");
551                         else if (reg & (1 << 1))
552                                 *data = g_variant_new_string("CV");
553                         else
554                                 *data = g_variant_new_string("UR");
555                 }
556
557                 s = g_variant_get_string(*data, NULL);
558                 if (g_strcmp0(s, "CV") && g_strcmp0(s, "CC") && g_strcmp0(s, "CC-") &&
559                         g_strcmp0(s, "UR") && g_strcmp0(s, "")) {
560
561                         sr_err("Unknown response to SCPI_CMD_GET_OUTPUT_REGULATION: %s", s);
562                         ret = SR_ERR_DATA;
563                 }
564         }
565
566         if (cmd == SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ACTIVE) {
567                 if (devc->device->dialect == SCPI_DIALECT_HP_COMP) {
568                         /* Evaluate Status Register from a HP 66xx in COMP mode. */
569                         s = g_variant_get_string(*data, NULL);
570                         sr_atoi(s, &reg);
571                         g_variant_unref(*data);
572                         *data = g_variant_new_boolean(reg & (1 << 3));
573                 }
574                 if (devc->device->dialect == SCPI_DIALECT_HP_66XXB) {
575                         /* Evaluate Questionable Status Register bit 0 from a HP 66xxB. */
576                         s = g_variant_get_string(*data, NULL);
577                         sr_atoi(s, &reg);
578                         g_variant_unref(*data);
579                         *data = g_variant_new_boolean(reg & (1 << 0));
580                 }
581         }
582
583         if (cmd == SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ACTIVE) {
584                 if (devc->device->dialect == SCPI_DIALECT_HP_COMP) {
585                         /* Evaluate Status Register from a HP 66xx in COMP mode. */
586                         s = g_variant_get_string(*data, NULL);
587                         sr_atoi(s, &reg);
588                         g_variant_unref(*data);
589                         *data = g_variant_new_boolean(reg & (1 << 6));
590                 }
591                 if (devc->device->dialect == SCPI_DIALECT_HP_66XXB) {
592                         /* Evaluate Questionable Status Register bit 1 from a HP 66xxB. */
593                         s = g_variant_get_string(*data, NULL);
594                         sr_atoi(s, &reg);
595                         g_variant_unref(*data);
596                         *data = g_variant_new_boolean(reg & (1 << 1));
597                 }
598         }
599
600         if (cmd == SCPI_CMD_GET_OVER_TEMPERATURE_PROTECTION_ACTIVE) {
601                 if (devc->device->dialect == SCPI_DIALECT_HP_COMP) {
602                         /* Evaluate Status Register from a HP 66xx in COMP mode. */
603                         s = g_variant_get_string(*data, NULL);
604                         sr_atoi(s, &reg);
605                         g_variant_unref(*data);
606                         *data = g_variant_new_boolean(reg & (1 << 4));
607                 }
608                 if (devc->device->dialect == SCPI_DIALECT_HP_66XXB ||
609                     devc->device->dialect == SCPI_DIALECT_HMP) {
610                         /* Evaluate Questionable Status Register bit 4 from a HP 66xxB. */
611                         s = g_variant_get_string(*data, NULL);
612                         sr_atoi(s, &reg);
613                         g_variant_unref(*data);
614                         *data = g_variant_new_boolean(reg & (1 << 4));
615                 }
616         }
617
618         return ret;
619 }
620
621 static int config_set(uint32_t key, GVariant *data,
622         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
623 {
624         struct dev_context *devc;
625         double d;
626         int channel_group_cmd;
627         char *channel_group_name;
628         int ret;
629
630         if (!sdi)
631                 return SR_ERR_ARG;
632
633         channel_group_cmd = 0;
634         channel_group_name = NULL;
635         if (cg) {
636                 channel_group_cmd = SCPI_CMD_SELECT_CHANNEL;
637                 channel_group_name = g_strdup(cg->name);
638         }
639
640         devc = sdi->priv;
641
642         switch (key) {
643         case SR_CONF_ENABLED:
644                 if (g_variant_get_boolean(data))
645                         ret = sr_scpi_cmd(sdi, devc->device->commands,
646                                         channel_group_cmd, channel_group_name,
647                                         SCPI_CMD_SET_OUTPUT_ENABLE);
648                 else
649                         ret = sr_scpi_cmd(sdi, devc->device->commands,
650                                         channel_group_cmd, channel_group_name,
651                                         SCPI_CMD_SET_OUTPUT_DISABLE);
652                 break;
653         case SR_CONF_VOLTAGE_TARGET:
654                 d = g_variant_get_double(data);
655                 ret = sr_scpi_cmd(sdi, devc->device->commands,
656                                 channel_group_cmd, channel_group_name,
657                                 SCPI_CMD_SET_VOLTAGE_TARGET, d);
658                 break;
659         case SR_CONF_OUTPUT_FREQUENCY_TARGET:
660                 d = g_variant_get_double(data);
661                 ret = sr_scpi_cmd(sdi, devc->device->commands,
662                                 channel_group_cmd, channel_group_name,
663                                 SCPI_CMD_SET_FREQUENCY_TARGET, d);
664                 break;
665         case SR_CONF_CURRENT_LIMIT:
666                 d = g_variant_get_double(data);
667                 ret = sr_scpi_cmd(sdi, devc->device->commands,
668                                 channel_group_cmd, channel_group_name,
669                                 SCPI_CMD_SET_CURRENT_LIMIT, d);
670                 break;
671         case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
672                 if (g_variant_get_boolean(data))
673                         ret = sr_scpi_cmd(sdi, devc->device->commands,
674                                         channel_group_cmd, channel_group_name,
675                                         SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_ENABLE);
676                 else
677                         ret = sr_scpi_cmd(sdi, devc->device->commands,
678                                         channel_group_cmd, channel_group_name,
679                                         SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_DISABLE);
680                 break;
681         case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
682                 d = g_variant_get_double(data);
683                 ret = sr_scpi_cmd(sdi, devc->device->commands,
684                                 channel_group_cmd, channel_group_name,
685                                 SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_THRESHOLD, d);
686                 break;
687         case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
688                 if (g_variant_get_boolean(data))
689                         ret = sr_scpi_cmd(sdi, devc->device->commands,
690                                         channel_group_cmd, channel_group_name,
691                                         SCPI_CMD_SET_OVER_CURRENT_PROTECTION_ENABLE);
692                 else
693                         ret = sr_scpi_cmd(sdi, devc->device->commands,
694                                         channel_group_cmd, channel_group_name,
695                                         SCPI_CMD_SET_OVER_CURRENT_PROTECTION_DISABLE);
696                 break;
697         case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
698                 d = g_variant_get_double(data);
699                 ret = sr_scpi_cmd(sdi, devc->device->commands,
700                                 channel_group_cmd, channel_group_name,
701                                 SCPI_CMD_SET_OVER_CURRENT_PROTECTION_THRESHOLD, d);
702                 break;
703         case SR_CONF_OVER_TEMPERATURE_PROTECTION:
704                 if (g_variant_get_boolean(data))
705                         ret = sr_scpi_cmd(sdi, devc->device->commands,
706                                         channel_group_cmd, channel_group_name,
707                                         SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_ENABLE);
708                 else
709                         ret = sr_scpi_cmd(sdi, devc->device->commands,
710                                         channel_group_cmd, channel_group_name,
711                                         SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_DISABLE);
712                 break;
713         default:
714                 ret = sr_sw_limits_config_set(&devc->limits, key, data);
715         }
716
717         g_free(channel_group_name);
718
719         return ret;
720 }
721
722 static int config_list(uint32_t key, GVariant **data,
723         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
724 {
725         struct dev_context *devc;
726         struct sr_channel *ch;
727         struct pps_channel *pch;
728         const struct channel_spec *ch_spec;
729         int i;
730         const char *s[16];
731
732         devc = (sdi) ? sdi->priv : NULL;
733
734         if (!cg) {
735                 switch (key) {
736                 case SR_CONF_SCAN_OPTIONS:
737                 case SR_CONF_DEVICE_OPTIONS:
738                         return std_opts_config_list(key, data, sdi, cg,
739                                 ARRAY_AND_SIZE(scanopts),
740                                 ARRAY_AND_SIZE(drvopts),
741                                 (devc && devc->device) ? devc->device->devopts : NULL,
742                                 (devc && devc->device) ? devc->device->num_devopts : 0);
743                         break;
744                 case SR_CONF_CHANNEL_CONFIG:
745                         if (!devc || !devc->device)
746                                 return SR_ERR_ARG;
747                         /* Not used. */
748                         i = 0;
749                         if (devc->device->features & PPS_INDEPENDENT)
750                                 s[i++] = "Independent";
751                         if (devc->device->features & PPS_SERIES)
752                                 s[i++] = "Series";
753                         if (devc->device->features & PPS_PARALLEL)
754                                 s[i++] = "Parallel";
755                         if (i == 0) {
756                                 /*
757                                  * Shouldn't happen: independent-only devices
758                                  * shouldn't advertise this option at all.
759                                  */
760                                 return SR_ERR_NA;
761                         }
762                         *data = g_variant_new_strv(s, i);
763                         break;
764                 default:
765                         return SR_ERR_NA;
766                 }
767         } else {
768                 /*
769                  * Per-channel-group options depending on a channel are actually
770                  * done with the first channel. Channel groups in PPS can have
771                  * more than one channel, but they will typically be of equal
772                  * specification for use in series or parallel mode.
773                  */
774                 ch = cg->channels->data;
775                 pch = ch->priv;
776                 if (!devc || !devc->device)
777                         return SR_ERR_ARG;
778                 ch_spec = &(devc->device->channels[pch->hw_output_idx]);
779
780                 switch (key) {
781                 case SR_CONF_DEVICE_OPTIONS:
782                         *data = std_gvar_array_u32(devc->device->devopts_cg, devc->device->num_devopts_cg);
783                         break;
784                 case SR_CONF_VOLTAGE_TARGET:
785                         *data = std_gvar_min_max_step_array(ch_spec->voltage);
786                         break;
787                 case SR_CONF_OUTPUT_FREQUENCY_TARGET:
788                         *data = std_gvar_min_max_step_array(ch_spec->frequency);
789                         break;
790                 case SR_CONF_CURRENT_LIMIT:
791                         *data = std_gvar_min_max_step_array(ch_spec->current);
792                         break;
793                 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
794                         *data = std_gvar_min_max_step_array(ch_spec->ovp);
795                         break;
796                 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
797                         *data = std_gvar_min_max_step_array(ch_spec->ocp);
798                         break;
799                 default:
800                         return SR_ERR_NA;
801                 }
802         }
803
804         return SR_OK;
805 }
806
807 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
808 {
809         struct dev_context *devc;
810         struct sr_scpi_dev_inst *scpi;
811         int ret;
812
813         devc = sdi->priv;
814         scpi = sdi->conn;
815
816         /* Prime the pipe with the first channel. */
817         devc->cur_acquisition_channel = sr_next_enabled_channel(sdi, NULL);
818
819         /* Device specific initialization before acquisition starts. */
820         if (devc->device->init_acquisition)
821                 devc->device->init_acquisition(sdi);
822
823         if ((ret = sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 10,
824                         scpi_pps_receive_data, (void *)sdi)) != SR_OK)
825                 return ret;
826         std_session_send_df_header(sdi);
827         sr_sw_limits_acquisition_start(&devc->limits);
828
829         return SR_OK;
830 }
831
832 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
833 {
834         struct sr_scpi_dev_inst *scpi;
835
836         scpi = sdi->conn;
837
838         sr_scpi_source_remove(sdi->session, scpi);
839
840         std_session_send_df_end(sdi);
841
842         return SR_OK;
843 }
844
845 static struct sr_dev_driver scpi_pps_driver_info = {
846         .name = "scpi-pps",
847         .longname = "SCPI PPS",
848         .api_version = 1,
849         .init = std_init,
850         .cleanup = std_cleanup,
851         .scan = scan_scpi_pps,
852         .dev_list = std_dev_list,
853         .dev_clear = dev_clear,
854         .config_get = config_get,
855         .config_set = config_set,
856         .config_list = config_list,
857         .dev_open = dev_open,
858         .dev_close = dev_close,
859         .dev_acquisition_start = dev_acquisition_start,
860         .dev_acquisition_stop = dev_acquisition_stop,
861         .context = NULL,
862 };
863
864 static struct sr_dev_driver hp_ib_pps_driver_info = {
865         .name = "hpib-pps",
866         .longname = "HP-IB PPS",
867         .api_version = 1,
868         .init = std_init,
869         .cleanup = std_cleanup,
870         .scan = scan_hpib_pps,
871         .dev_list = std_dev_list,
872         .dev_clear = dev_clear,
873         .config_get = config_get,
874         .config_set = config_set,
875         .config_list = config_list,
876         .dev_open = dev_open,
877         .dev_close = dev_close,
878         .dev_acquisition_start = dev_acquisition_start,
879         .dev_acquisition_stop = dev_acquisition_stop,
880         .context = NULL,
881 };
882 SR_REGISTER_DEV_DRIVER(scpi_pps_driver_info);
883 SR_REGISTER_DEV_DRIVER(hp_ib_pps_driver_info);