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