]> sigrok.org Git - libsigrok.git/blame - src/hardware/scpi-pps/api.c
sr_dev_close(): Factor out SR_ERR_DEV_CLOSED check.
[libsigrok.git] / src / hardware / scpi-pps / api.c
CommitLineData
ca1a7cb5
BV
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
6ec6c43b 20#include <config.h>
9e45cd41 21#include <string.h>
ba464a12 22#include <strings.h>
91ef511d 23#include "scpi.h"
ca1a7cb5
BV
24#include "protocol.h"
25
dd5c48a6 26static struct sr_dev_driver scpi_pps_driver_info;
c2af709b 27static struct sr_dev_driver hp_ib_pps_driver_info;
9e45cd41 28
584560f1 29static const uint32_t scanopts[] = {
9e45cd41
BV
30 SR_CONF_CONN,
31 SR_CONF_SERIALCOMM,
32};
ca1a7cb5 33
9d9cf1c4 34static const uint32_t drvopts[] = {
a258204e 35 SR_CONF_POWER_SUPPLY,
a258204e
BV
36};
37
329733d9 38static const struct pps_channel_instance pci[] = {
01b0257a
BV
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" },
4264f1c0 42 { SR_MQ_FREQUENCY, SCPI_CMD_GET_MEAS_FREQUENCY, "F" },
01b0257a
BV
43};
44
c2af709b 45static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi,
d9251a2c
UH
46 int (*get_hw_id)(struct sr_scpi_dev_inst *scpi,
47 struct sr_scpi_hw_info **scpi_response))
ca1a7cb5 48{
9e45cd41
BV
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;
01b0257a 55 struct pps_channel *pch;
4a471029
BV
56 struct channel_spec *channels;
57 struct channel_group_spec *channel_groups, *cgs;
9e45cd41 58 struct pps_channel_group *pcg;
58b77c41
BV
59 GRegex *model_re;
60 GMatchInfo *model_mi;
01b0257a 61 GSList *l;
9e45cd41 62 uint64_t mask;
4a471029
BV
63 unsigned int num_channels, num_channel_groups, ch_num, ch_idx, i, j;
64 int ret;
22c18b03 65 const char *vendor;
01b0257a 66 char ch_name[16];
9e45cd41 67
c2af709b 68 if (get_hw_id(scpi, &hw_info) != SR_OK) {
9e45cd41
BV
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++) {
91ef511d 75 vendor = sr_vendor_alias(hw_info->manufacturer);
34577da6 76 if (g_ascii_strcasecmp(vendor, pps_profiles[i].vendor))
22c18b03 77 continue;
58b77c41
BV
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))
9e45cd41 80 device = &pps_profiles[i];
58b77c41
BV
81 g_match_info_unref(model_mi);
82 g_regex_unref(model_re);
83 if (device)
9e45cd41 84 break;
9e45cd41
BV
85 }
86 if (!device) {
87 sr_scpi_hw_info_free(hw_info);
88 return NULL;
89 }
90
aac29cc1 91 sdi = g_malloc0(sizeof(struct sr_dev_inst));
0af636be
UH
92 sdi->vendor = g_strdup(vendor);
93 sdi->model = g_strdup(hw_info->model);
94 sdi->version = g_strdup(hw_info->firmware_version);
9e45cd41 95 sdi->conn = scpi;
4f840ce9 96 sdi->driver = &scpi_pps_driver_info;
9e45cd41 97 sdi->inst_type = SR_INST_SCPI;
d08667c5
SA
98 sdi->serial_num = g_strdup(hw_info->serial_number);
99
9e45cd41
BV
100 devc = g_malloc0(sizeof(struct dev_context));
101 devc->device = device;
102 sdi->priv = devc;
103
4a471029
BV
104 if (device->num_channels) {
105 /* Static channels and groups. */
329733d9 106 channels = (struct channel_spec *)device->channels;
4a471029 107 num_channels = device->num_channels;
329733d9 108 channel_groups = (struct channel_group_spec *)device->channel_groups;
4a471029
BV
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
01b0257a 126 ch_idx = 0;
4a471029 127 for (ch_num = 0; ch_num < num_channels; ch_num++) {
01b0257a 128 /* Create one channel per measurable output unit. */
01b0257a 129 for (i = 0; i < ARRAY_SIZE(pci); i++) {
91ef511d 130 if (!scpi_cmd_get(devc->device->commands, pci[i].command))
01b0257a
BV
131 continue;
132 g_snprintf(ch_name, 16, "%s%s", pci[i].prefix,
4a471029 133 channels[ch_num].name);
5e23fcab
ML
134 ch = sr_channel_new(sdi, ch_idx++, SR_CHANNEL_ANALOG, TRUE,
135 ch_name);
01b0257a
BV
136 pch = g_malloc0(sizeof(struct pps_channel));
137 pch->hw_output_idx = ch_num;
4a471029 138 pch->hwname = channels[ch_num].name;
01b0257a
BV
139 pch->mq = pci[i].mq;
140 ch->priv = pch;
01b0257a 141 }
9e45cd41 142 }
ca1a7cb5 143
4a471029
BV
144 for (i = 0; i < num_channel_groups; i++) {
145 cgs = &channel_groups[i];
9e45cd41
BV
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) {
01b0257a
BV
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 }
9e45cd41
BV
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 }
ca1a7cb5 163
4a471029
BV
164 sr_scpi_hw_info_free(hw_info);
165 hw_info = NULL;
166
91ef511d 167 scpi_cmd(sdi, devc->device->commands, SCPI_CMD_LOCAL);
ca1a7cb5 168
9e45cd41
BV
169 return sdi;
170}
ca1a7cb5 171
c2af709b 172static gchar *hpib_get_revision(struct sr_scpi_dev_inst *scpi)
9e45cd41 173{
c2af709b
AG
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 */
207static 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
228static 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
233static 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
238static 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
243static GSList *scan_hpib_pps(struct sr_dev_driver *di, GSList *options)
244{
245 return sr_scpi_scan(di->context, options, probe_hpib_pps_device);
ca1a7cb5
BV
246}
247
ca1a7cb5
BV
248static int dev_open(struct sr_dev_inst *sdi)
249{
ee2860ee 250 struct dev_context *devc;
9e45cd41 251 struct sr_scpi_dev_inst *scpi;
ee2860ee 252 GVariant *beeper;
9e45cd41 253
7d4f1741 254 if (sdi->status != SR_ST_INACTIVE)
9e45cd41 255 return SR_ERR;
ca1a7cb5 256
9e45cd41
BV
257 scpi = sdi->conn;
258 if (sr_scpi_open(scpi) < 0)
259 return SR_ERR;
ca1a7cb5
BV
260
261 sdi->status = SR_ST_ACTIVE;
262
91ef511d
BV
263 devc = sdi->priv;
264 scpi_cmd(sdi, devc->device->commands, SCPI_CMD_REMOTE);
ee2860ee 265 devc->beeper_was_set = FALSE;
91ef511d
BV
266 if (scpi_cmd_resp(sdi, devc->device->commands, &beeper,
267 G_VARIANT_TYPE_BOOLEAN, SCPI_CMD_BEEPER) == SR_OK) {
ee2860ee
BV
268 if (g_variant_get_boolean(beeper)) {
269 devc->beeper_was_set = TRUE;
91ef511d 270 scpi_cmd(sdi, devc->device->commands, SCPI_CMD_BEEPER_DISABLE);
ee2860ee
BV
271 }
272 g_variant_unref(beeper);
273 }
60475cd7 274
ca1a7cb5
BV
275 return SR_OK;
276}
277
278static int dev_close(struct sr_dev_inst *sdi)
279{
9e45cd41 280 struct sr_scpi_dev_inst *scpi;
ee2860ee 281 struct dev_context *devc;
ca1a7cb5 282
ee2860ee 283 devc = sdi->priv;
9e45cd41
BV
284 scpi = sdi->conn;
285 if (scpi) {
ee2860ee 286 if (devc->beeper_was_set)
91ef511d
BV
287 scpi_cmd(sdi, devc->device->commands, SCPI_CMD_BEEPER_ENABLE);
288 scpi_cmd(sdi, devc->device->commands, SCPI_CMD_LOCAL);
9e45cd41
BV
289 sr_scpi_close(scpi);
290 sdi->status = SR_ST_INACTIVE;
291 }
ca1a7cb5
BV
292
293 return SR_OK;
294}
295
4a471029
BV
296static void clear_helper(void *priv)
297{
298 struct dev_context *devc;
299
300 devc = priv;
301 g_free(devc->channels);
302 g_free(devc->channel_groups);
303 g_free(devc);
304}
305
1e726f56 306static int dev_clear(const struct sr_dev_driver *di)
ca1a7cb5 307{
4a471029 308 return std_dev_clear(di, clear_helper);
ca1a7cb5
BV
309}
310
584560f1 311static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
ca1a7cb5
BV
312 const struct sr_channel_group *cg)
313{
9e45cd41 314 struct dev_context *devc;
478c8d92
BV
315 const GVariantType *gvtype;
316 unsigned int i;
317 int cmd, ret;
069d9f25 318 const char *s;
ca1a7cb5 319
9e45cd41
BV
320 if (!sdi)
321 return SR_ERR_ARG;
322
323 devc = sdi->priv;
ca1a7cb5 324
478c8d92 325 if (cg) {
9e45cd41
BV
326 /*
327 * These options only apply to channel groups with a single
328 * channel -- they're per-channel settings for the device.
329 */
478c8d92
BV
330
331 /*
332 * Config keys are handled below depending on whether a channel
333 * group was provided by the frontend. However some of these
334 * take a CG on one PPS but not on others. Check the device's
335 * profile for that here, and NULL out the channel group as needed.
336 */
337 for (i = 0; i < devc->device->num_devopts; i++) {
338 if (devc->device->devopts[i] == key) {
339 cg = NULL;
340 break;
341 }
342 }
478c8d92 343 }
9e45cd41 344
478c8d92
BV
345 gvtype = NULL;
346 cmd = -1;
347 switch (key) {
7a0b98b5 348 case SR_CONF_ENABLED:
478c8d92
BV
349 gvtype = G_VARIANT_TYPE_BOOLEAN;
350 cmd = SCPI_CMD_GET_OUTPUT_ENABLED;
351 break;
7a0b98b5 352 case SR_CONF_VOLTAGE:
478c8d92
BV
353 gvtype = G_VARIANT_TYPE_DOUBLE;
354 cmd = SCPI_CMD_GET_MEAS_VOLTAGE;
355 break;
7a0b98b5 356 case SR_CONF_VOLTAGE_TARGET:
478c8d92 357 gvtype = G_VARIANT_TYPE_DOUBLE;
ca95e90f 358 cmd = SCPI_CMD_GET_VOLTAGE_TARGET;
478c8d92 359 break;
4264f1c0
AG
360 case SR_CONF_OUTPUT_FREQUENCY:
361 gvtype = G_VARIANT_TYPE_DOUBLE;
362 cmd = SCPI_CMD_GET_MEAS_FREQUENCY;
363 break;
364 case SR_CONF_OUTPUT_FREQUENCY_TARGET:
365 gvtype = G_VARIANT_TYPE_DOUBLE;
366 cmd = SCPI_CMD_GET_FREQUENCY_TARGET;
367 break;
7a0b98b5 368 case SR_CONF_CURRENT:
478c8d92
BV
369 gvtype = G_VARIANT_TYPE_DOUBLE;
370 cmd = SCPI_CMD_GET_MEAS_CURRENT;
371 break;
7a0b98b5 372 case SR_CONF_CURRENT_LIMIT:
478c8d92 373 gvtype = G_VARIANT_TYPE_DOUBLE;
ca95e90f 374 cmd = SCPI_CMD_GET_CURRENT_LIMIT;
478c8d92
BV
375 break;
376 case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
377 gvtype = G_VARIANT_TYPE_BOOLEAN;
378 cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ENABLED;
379 break;
380 case SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE:
381 gvtype = G_VARIANT_TYPE_BOOLEAN;
382 cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ACTIVE;
383 break;
384 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
385 gvtype = G_VARIANT_TYPE_DOUBLE;
386 cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_THRESHOLD;
387 break;
388 case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
389 gvtype = G_VARIANT_TYPE_BOOLEAN;
390 cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ENABLED;
391 break;
392 case SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE:
393 gvtype = G_VARIANT_TYPE_BOOLEAN;
394 cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ACTIVE;
395 break;
396 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
397 gvtype = G_VARIANT_TYPE_DOUBLE;
398 cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_THRESHOLD;
399 break;
400 case SR_CONF_OVER_TEMPERATURE_PROTECTION:
401 gvtype = G_VARIANT_TYPE_BOOLEAN;
402 cmd = SCPI_CMD_GET_OVER_TEMPERATURE_PROTECTION;
403 break;
7a0b98b5 404 case SR_CONF_REGULATION:
60475cd7
BV
405 gvtype = G_VARIANT_TYPE_STRING;
406 cmd = SCPI_CMD_GET_OUTPUT_REGULATION;
478c8d92 407 }
91ef511d
BV
408 if (!gvtype)
409 return SR_ERR_NA;
410
411 if (cg)
412 select_channel(sdi, cg->channels->data);
413 ret = scpi_cmd_resp(sdi, devc->device->commands, data, gvtype, cmd);
414
415 if (cmd == SCPI_CMD_GET_OUTPUT_REGULATION) {
416 /*
417 * The Rigol DP800 series return CV/CC/UR, Philips PM2800
418 * return VOLT/CURR. We always return a GVariant string in
419 * the Rigol notation.
420 */
d66c93cc 421 s = g_variant_get_string(*data, NULL);
069d9f25
AJ
422 if (!strcmp(s, "VOLT")) {
423 g_variant_unref(*data);
91ef511d 424 *data = g_variant_new_string("CV");
069d9f25
AJ
425 } else if (!strcmp(s, "CURR")) {
426 g_variant_unref(*data);
91ef511d 427 *data = g_variant_new_string("CC");
069d9f25
AJ
428 }
429
430 s = g_variant_get_string(*data, NULL);
431 if (strcmp(s, "CV") && strcmp(s, "CC") && strcmp(s, "UR")) {
91ef511d
BV
432 sr_dbg("Unknown response to SCPI_CMD_GET_OUTPUT_REGULATION: %s", s);
433 ret = SR_ERR_DATA;
434 }
91ef511d 435 }
ca1a7cb5
BV
436
437 return ret;
438}
439
584560f1 440static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
ca1a7cb5
BV
441 const struct sr_channel_group *cg)
442{
91ef511d 443 struct dev_context *devc;
9e45cd41 444 double d;
ca1a7cb5 445 int ret;
ca1a7cb5 446
fdedbfcd
BV
447 if (!sdi)
448 return SR_ERR_ARG;
449
60475cd7 450 if (cg)
9e45cd41 451 /* Channel group specified. */
60475cd7
BV
452 select_channel(sdi, cg->channels->data);
453
91ef511d 454 devc = sdi->priv;
e57057ae 455
60475cd7 456 switch (key) {
7a0b98b5 457 case SR_CONF_ENABLED:
60475cd7 458 if (g_variant_get_boolean(data))
91ef511d
BV
459 ret = scpi_cmd(sdi, devc->device->commands,
460 SCPI_CMD_SET_OUTPUT_ENABLE);
60475cd7 461 else
91ef511d
BV
462 ret = scpi_cmd(sdi, devc->device->commands,
463 SCPI_CMD_SET_OUTPUT_DISABLE);
60475cd7 464 break;
7a0b98b5 465 case SR_CONF_VOLTAGE_TARGET:
60475cd7 466 d = g_variant_get_double(data);
91ef511d
BV
467 ret = scpi_cmd(sdi, devc->device->commands,
468 SCPI_CMD_SET_VOLTAGE_TARGET, d);
60475cd7 469 break;
4264f1c0
AG
470 case SR_CONF_OUTPUT_FREQUENCY_TARGET:
471 d = g_variant_get_double(data);
91ef511d
BV
472 ret = scpi_cmd(sdi, devc->device->commands,
473 SCPI_CMD_SET_FREQUENCY_TARGET, d);
4264f1c0 474 break;
7a0b98b5 475 case SR_CONF_CURRENT_LIMIT:
60475cd7 476 d = g_variant_get_double(data);
91ef511d
BV
477 ret = scpi_cmd(sdi, devc->device->commands,
478 SCPI_CMD_SET_CURRENT_LIMIT, d);
60475cd7
BV
479 break;
480 case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
481 if (g_variant_get_boolean(data))
91ef511d
BV
482 ret = scpi_cmd(sdi, devc->device->commands,
483 SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_ENABLE);
60475cd7 484 else
91ef511d
BV
485 ret = scpi_cmd(sdi, devc->device->commands,
486 SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_DISABLE);
60475cd7
BV
487 break;
488 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
489 d = g_variant_get_double(data);
91ef511d
BV
490 ret = scpi_cmd(sdi, devc->device->commands,
491 SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_THRESHOLD, d);
60475cd7
BV
492 break;
493 case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
494 if (g_variant_get_boolean(data))
91ef511d
BV
495 ret = scpi_cmd(sdi, devc->device->commands,
496 SCPI_CMD_SET_OVER_CURRENT_PROTECTION_ENABLE);
60475cd7 497 else
91ef511d
BV
498 ret = scpi_cmd(sdi, devc->device->commands,
499 SCPI_CMD_SET_OVER_CURRENT_PROTECTION_DISABLE);
60475cd7
BV
500 break;
501 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
502 d = g_variant_get_double(data);
91ef511d
BV
503 ret = scpi_cmd(sdi, devc->device->commands,
504 SCPI_CMD_SET_OVER_CURRENT_PROTECTION_THRESHOLD, d);
60475cd7
BV
505 break;
506 case SR_CONF_OVER_TEMPERATURE_PROTECTION:
507 if (g_variant_get_boolean(data))
91ef511d
BV
508 ret = scpi_cmd(sdi, devc->device->commands,
509 SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_ENABLE);
60475cd7 510 else
91ef511d
BV
511 ret = scpi_cmd(sdi, devc->device->commands,
512 SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_DISABLE);
60475cd7
BV
513 break;
514 default:
515 ret = SR_ERR_NA;
ca1a7cb5
BV
516 }
517
518 return ret;
519}
520
584560f1 521static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
ca1a7cb5
BV
522 const struct sr_channel_group *cg)
523{
9e45cd41
BV
524 struct dev_context *devc;
525 struct sr_channel *ch;
329733d9 526 const struct channel_spec *ch_spec;
9e45cd41
BV
527 GVariant *gvar;
528 GVariantBuilder gvb;
529 int ret, i;
530 const char *s[16];
531
532 /* Always available, even without sdi. */
533 if (key == SR_CONF_SCAN_OPTIONS) {
584560f1
BV
534 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
535 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
9e45cd41 536 return SR_OK;
a258204e
BV
537 } else if (key == SR_CONF_DEVICE_OPTIONS && !sdi) {
538 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
9d9cf1c4 539 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
a258204e 540 return SR_OK;
9e45cd41 541 }
ca1a7cb5 542
9e45cd41
BV
543 if (!sdi)
544 return SR_ERR_ARG;
545 devc = sdi->priv;
ca1a7cb5
BV
546
547 ret = SR_OK;
9e45cd41
BV
548 if (!cg) {
549 /* No channel group: global options. */
550 switch (key) {
551 case SR_CONF_DEVICE_OPTIONS:
584560f1 552 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
9e45cd41 553 devc->device->devopts, devc->device->num_devopts,
584560f1 554 sizeof(uint32_t));
9e45cd41 555 break;
7a0b98b5 556 case SR_CONF_CHANNEL_CONFIG:
478c8d92 557 /* Not used. */
9e45cd41
BV
558 i = 0;
559 if (devc->device->features & PPS_INDEPENDENT)
560 s[i++] = "Independent";
561 if (devc->device->features & PPS_SERIES)
562 s[i++] = "Series";
563 if (devc->device->features & PPS_PARALLEL)
564 s[i++] = "Parallel";
565 if (i == 0) {
566 /*
567 * Shouldn't happen: independent-only devices
568 * shouldn't advertise this option at all.
569 */
570 return SR_ERR_NA;
571 }
572 *data = g_variant_new_strv(s, i);
573 break;
574 default:
575 return SR_ERR_NA;
576 }
577 } else {
578 /* Channel group specified. */
9e45cd41
BV
579 /*
580 * Per-channel-group options depending on a channel are actually
581 * done with the first channel. Channel groups in PPS can have
582 * more than one channel, but they will typically be of equal
01b0257a 583 * specification for use in series or parallel mode.
9e45cd41 584 */
9e45cd41
BV
585 ch = cg->channels->data;
586
587 switch (key) {
588 case SR_CONF_DEVICE_OPTIONS:
584560f1 589 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
9e45cd41 590 devc->device->devopts_cg, devc->device->num_devopts_cg,
584560f1 591 sizeof(uint32_t));
9e45cd41 592 break;
7a0b98b5 593 case SR_CONF_VOLTAGE_TARGET:
9e45cd41
BV
594 ch_spec = &(devc->device->channels[ch->index]);
595 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
596 /* Min, max, write resolution. */
597 for (i = 0; i < 3; i++) {
598 gvar = g_variant_new_double(ch_spec->voltage[i]);
599 g_variant_builder_add_value(&gvb, gvar);
600 }
601 *data = g_variant_builder_end(&gvb);
602 break;
4264f1c0
AG
603 case SR_CONF_OUTPUT_FREQUENCY_TARGET:
604 ch_spec = &(devc->device->channels[ch->index]);
605 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
606 /* Min, max, write resolution. */
607 for (i = 0; i < 3; i++) {
608 gvar = g_variant_new_double(ch_spec->frequency[i]);
609 g_variant_builder_add_value(&gvb, gvar);
610 }
611 *data = g_variant_builder_end(&gvb);
612 break;
7a0b98b5 613 case SR_CONF_CURRENT_LIMIT:
9e45cd41
BV
614 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
615 /* Min, max, step. */
616 for (i = 0; i < 3; i++) {
617 ch_spec = &(devc->device->channels[ch->index]);
618 gvar = g_variant_new_double(ch_spec->current[i]);
619 g_variant_builder_add_value(&gvb, gvar);
620 }
621 *data = g_variant_builder_end(&gvb);
622 break;
623 default:
624 return SR_ERR_NA;
625 }
ca1a7cb5
BV
626 }
627
628 return ret;
629}
630
695dc859 631static int dev_acquisition_start(const struct sr_dev_inst *sdi)
ca1a7cb5 632{
9e45cd41
BV
633 struct dev_context *devc;
634 struct sr_scpi_dev_inst *scpi;
635 struct sr_channel *ch;
01b0257a
BV
636 struct pps_channel *pch;
637 int cmd, ret;
ca1a7cb5 638
9e45cd41
BV
639 devc = sdi->priv;
640 scpi = sdi->conn;
9e45cd41 641
01b0257a 642 if ((ret = sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 10,
9e45cd41
BV
643 scpi_pps_receive_data, (void *)sdi)) != SR_OK)
644 return ret;
bee2b016 645 std_session_send_df_header(sdi);
9e45cd41 646
01b0257a 647 /* Prime the pipe with the first channel's fetch. */
9c24d16a 648 ch = sr_next_enabled_channel(sdi, NULL);
01b0257a 649 pch = ch->priv;
c1603f45 650 if ((ret = select_channel(sdi, ch)) < 0)
984e4b0d 651 return ret;
01b0257a
BV
652 if (pch->mq == SR_MQ_VOLTAGE)
653 cmd = SCPI_CMD_GET_MEAS_VOLTAGE;
4264f1c0
AG
654 else if (pch->mq == SR_MQ_FREQUENCY)
655 cmd = SCPI_CMD_GET_MEAS_FREQUENCY;
01b0257a
BV
656 else if (pch->mq == SR_MQ_CURRENT)
657 cmd = SCPI_CMD_GET_MEAS_CURRENT;
658 else if (pch->mq == SR_MQ_POWER)
659 cmd = SCPI_CMD_GET_MEAS_POWER;
660 else
661 return SR_ERR;
91ef511d 662 scpi_cmd(sdi, devc->device->commands, cmd, pch->hwname);
ca1a7cb5
BV
663
664 return SR_OK;
665}
666
695dc859 667static int dev_acquisition_stop(struct sr_dev_inst *sdi)
ca1a7cb5 668{
9e45cd41
BV
669 struct sr_scpi_dev_inst *scpi;
670 float f;
671
9e45cd41
BV
672 scpi = sdi->conn;
673
674 /*
675 * A requested value is certainly on the way. Retrieve it now,
676 * to avoid leaving the device in a state where it's not expecting
677 * commands.
678 */
679 sr_scpi_get_float(scpi, NULL, &f);
680 sr_scpi_source_remove(sdi->session, scpi);
681
bee2b016 682 std_session_send_df_end(sdi);
bf48cceb 683
ca1a7cb5
BV
684 return SR_OK;
685}
686
dd5c48a6 687static struct sr_dev_driver scpi_pps_driver_info = {
ca1a7cb5
BV
688 .name = "scpi-pps",
689 .longname = "SCPI PPS",
690 .api_version = 1,
c2fdcc25 691 .init = std_init,
700d6b64 692 .cleanup = std_cleanup,
c2af709b
AG
693 .scan = scan_scpi_pps,
694 .dev_list = std_dev_list,
695 .dev_clear = dev_clear,
696 .config_get = config_get,
697 .config_set = config_set,
698 .config_list = config_list,
699 .dev_open = dev_open,
700 .dev_close = dev_close,
701 .dev_acquisition_start = dev_acquisition_start,
702 .dev_acquisition_stop = dev_acquisition_stop,
703 .context = NULL,
704};
705
706static struct sr_dev_driver hp_ib_pps_driver_info = {
707 .name = "hpib-pps",
708 .longname = "HP-IB PPS",
709 .api_version = 1,
710 .init = std_init,
711 .cleanup = std_cleanup,
712 .scan = scan_hpib_pps,
c01bf34c 713 .dev_list = std_dev_list,
ca1a7cb5
BV
714 .dev_clear = dev_clear,
715 .config_get = config_get,
716 .config_set = config_set,
717 .config_list = config_list,
718 .dev_open = dev_open,
719 .dev_close = dev_close,
720 .dev_acquisition_start = dev_acquisition_start,
721 .dev_acquisition_stop = dev_acquisition_stop,
41812aca 722 .context = NULL,
ca1a7cb5 723};
dd5c48a6 724SR_REGISTER_DEV_DRIVER(scpi_pps_driver_info);
c2af709b 725SR_REGISTER_DEV_DRIVER(hp_ib_pps_driver_info);