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