blob: 5b64f4b622e9b4a5f788b14a00f03f61e56bc620 [file] [log] [blame]
Bert Vermeulenca1a7cb2014-08-31 17:20:39 +02001/*
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
Bert Vermeulen9e45cd42014-09-05 03:20:32 +020020#include <string.h>
Bert Vermeulenca1a7cb2014-08-31 17:20:39 +020021#include "protocol.h"
22
23SR_PRIV struct sr_dev_driver scpi_pps_driver_info;
24static struct sr_dev_driver *di = &scpi_pps_driver_info;
Bert Vermeulend4eabea2014-09-05 03:23:32 +020025extern unsigned int num_pps_profiles;
26extern const struct scpi_pps pps_profiles[];
Bert Vermeulen9e45cd42014-09-05 03:20:32 +020027
28static const int32_t scanopts[] = {
29 SR_CONF_CONN,
30 SR_CONF_SERIALCOMM,
31};
Bert Vermeulenca1a7cb2014-08-31 17:20:39 +020032
Bert Vermeulen01b02572014-09-08 03:26:19 +020033static struct pps_channel_instance pci[] = {
34 { SR_MQ_VOLTAGE, SCPI_CMD_GET_MEAS_VOLTAGE, "V" },
35 { SR_MQ_CURRENT, SCPI_CMD_GET_MEAS_CURRENT, "I" },
36 { SR_MQ_POWER, SCPI_CMD_GET_MEAS_POWER, "P" },
37};
38
Bert Vermeulenca1a7cb2014-08-31 17:20:39 +020039static int init(struct sr_context *sr_ctx)
40{
41 return std_init(sr_ctx, di, LOG_PREFIX);
42}
43
Bert Vermeulen9e45cd42014-09-05 03:20:32 +020044static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
45{
46 struct dev_context *devc;
47 struct sr_dev_inst *sdi;
48 struct sr_scpi_hw_info *hw_info;
49 struct sr_channel_group *cg;
50 struct sr_channel *ch;
51 const struct scpi_pps *device;
Bert Vermeulen01b02572014-09-08 03:26:19 +020052 struct pps_channel *pch;
Bert Vermeulen9e45cd42014-09-05 03:20:32 +020053 const struct channel_group_spec *cgs;
54 struct pps_channel_group *pcg;
Bert Vermeulen58b77c42014-09-05 12:50:07 +020055 GRegex *model_re;
56 GMatchInfo *model_mi;
Bert Vermeulen01b02572014-09-08 03:26:19 +020057 GSList *l;
Bert Vermeulen9e45cd42014-09-05 03:20:32 +020058 uint64_t mask;
Bert Vermeulen01b02572014-09-08 03:26:19 +020059 unsigned int ch_num, ch_idx, old_idx, i, j;
Bert Vermeulen22c18b02014-09-05 03:49:25 +020060 const char *vendor;
Bert Vermeulen01b02572014-09-08 03:26:19 +020061 char ch_name[16];
Bert Vermeulen9e45cd42014-09-05 03:20:32 +020062
63 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
64 sr_info("Couldn't get IDN response.");
65 return NULL;
66 }
67
68 device = NULL;
69 for (i = 0; i < num_pps_profiles; i++) {
Bert Vermeulen22c18b02014-09-05 03:49:25 +020070 vendor = get_vendor(hw_info->manufacturer);
Bert Vermeulen58b77c42014-09-05 12:50:07 +020071 if (strcasecmp(vendor, pps_profiles[i].vendor))
Bert Vermeulen22c18b02014-09-05 03:49:25 +020072 continue;
Bert Vermeulen58b77c42014-09-05 12:50:07 +020073 model_re = g_regex_new(pps_profiles[i].model, 0, 0, NULL);
74 if (g_regex_match(model_re, hw_info->model, 0, &model_mi))
Bert Vermeulen9e45cd42014-09-05 03:20:32 +020075 device = &pps_profiles[i];
Bert Vermeulen58b77c42014-09-05 12:50:07 +020076 g_match_info_unref(model_mi);
77 g_regex_unref(model_re);
78 if (device)
Bert Vermeulen9e45cd42014-09-05 03:20:32 +020079 break;
Bert Vermeulen9e45cd42014-09-05 03:20:32 +020080 }
81 if (!device) {
82 sr_scpi_hw_info_free(hw_info);
83 return NULL;
84 }
85
Bert Vermeulen58b77c42014-09-05 12:50:07 +020086 sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, vendor, hw_info->model,
87 hw_info->firmware_version);
Bert Vermeulen9e45cd42014-09-05 03:20:32 +020088 sdi->conn = scpi;
89 sdi->driver = di;
90 sdi->inst_type = SR_INST_SCPI;
91 devc = g_malloc0(sizeof(struct dev_context));
92 devc->device = device;
93 sdi->priv = devc;
94
Bert Vermeulen01b02572014-09-08 03:26:19 +020095 ch_idx = 0;
96 for (ch_num = 0; ch_num < device->num_channels; ch_num++) {
97 /* Create one channel per measurable output unit. */
98 old_idx = ch_idx;
99 for (i = 0; i < ARRAY_SIZE(pci); i++) {
100 if (!scpi_cmd_get(sdi, pci[i].command))
101 continue;
102 g_snprintf(ch_name, 16, "%s%s", pci[i].prefix,
103 device->channels[ch_num].name);
104 ch = sr_channel_new(ch_idx++, SR_CHANNEL_ANALOG, TRUE, ch_name);
105 pch = g_malloc0(sizeof(struct pps_channel));
106 pch->hw_output_idx = ch_num;
107 pch->hwname = device->channels[ch_num].name;
108 pch->mq = pci[i].mq;
109 ch->priv = pch;
110 sdi->channels = g_slist_append(sdi->channels, ch);
111 }
112 if (ch_idx == old_idx) {
113 /*
114 * Didn't create any channels for this hardware output.
115 * This can happen if the device has no measurement capability.
116 */
117 g_free(pch);
118 continue;
119 }
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200120 }
121
122 for (i = 0; i < device->num_channel_groups; i++) {
123 cgs = &device->channel_groups[i];
124 cg = g_malloc0(sizeof(struct sr_channel_group));
125 cg->name = g_strdup(cgs->name);
126 for (j = 0, mask = 1; j < 64; j++, mask <<= 1) {
127 if (cgs->channel_index_mask & mask) {
Bert Vermeulen01b02572014-09-08 03:26:19 +0200128 for (l = sdi->channels; l; l = l->next) {
129 ch = l->data;
130 pch = ch->priv;
131 if (pch->hw_output_idx == j)
132 cg->channels = g_slist_append(cg->channels, ch);
133 }
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200134 }
135 }
136 pcg = g_malloc0(sizeof(struct pps_channel_group));
137 pcg->features = cgs->features;
138 cg->priv = pcg;
139 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
140 }
141
142 /* SCPI devices commonly lock the panel keys when accessed remotely. */
143 scpi_cmd(sdi, SCPI_CMD_KEY_UNLOCK);
144 sr_scpi_close(scpi);
145
146 return sdi;
147}
148
Bert Vermeulenca1a7cb2014-08-31 17:20:39 +0200149static GSList *scan(GSList *options)
150{
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200151 return sr_scpi_scan(di->priv, options, probe_device);
Bert Vermeulenca1a7cb2014-08-31 17:20:39 +0200152}
153
154static GSList *dev_list(void)
155{
156 return ((struct drv_context *)(di->priv))->instances;
157}
158
159static int dev_clear(void)
160{
161 return std_dev_clear(di, NULL);
162}
163
164static int dev_open(struct sr_dev_inst *sdi)
165{
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200166 struct sr_scpi_dev_inst *scpi;
Bert Vermeulenca1a7cb2014-08-31 17:20:39 +0200167
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200168 if (sdi->status != SR_ST_ACTIVE)
169 return SR_ERR;
170
171 scpi = sdi->conn;
172 if (sr_scpi_open(scpi) < 0)
173 return SR_ERR;
Bert Vermeulenca1a7cb2014-08-31 17:20:39 +0200174
175 sdi->status = SR_ST_ACTIVE;
176
177 return SR_OK;
178}
179
180static int dev_close(struct sr_dev_inst *sdi)
181{
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200182 struct sr_scpi_dev_inst *scpi;
Bert Vermeulenca1a7cb2014-08-31 17:20:39 +0200183
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200184 if (sdi->status != SR_ST_ACTIVE)
185 return SR_ERR_DEV_CLOSED;
Bert Vermeulenca1a7cb2014-08-31 17:20:39 +0200186
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200187 scpi = sdi->conn;
188 if (scpi) {
189 scpi_cmd(sdi, SCPI_CMD_KEY_UNLOCK);
190 sr_scpi_close(scpi);
191 sdi->status = SR_ST_INACTIVE;
192 }
Bert Vermeulenca1a7cb2014-08-31 17:20:39 +0200193
194 return SR_OK;
195}
196
197static int cleanup(void)
198{
Bert Vermeulenca1a7cb2014-08-31 17:20:39 +0200199 return SR_OK;
200}
201
202static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
203 const struct sr_channel_group *cg)
204{
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200205 struct dev_context *devc;
206 struct sr_scpi_dev_inst *scpi;
207 struct sr_channel *ch;
Bert Vermeulen01b02572014-09-08 03:26:19 +0200208 struct pps_channel *pch;
Bert Vermeulen478c8d92014-09-08 00:02:03 +0200209 const GVariantType *gvtype;
210 unsigned int i;
211 int cmd, ret;
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200212 char *s;
Bert Vermeulenca1a7cb2014-08-31 17:20:39 +0200213
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200214 if (!sdi)
215 return SR_ERR_ARG;
216
217 devc = sdi->priv;
218 scpi = sdi->conn;
Bert Vermeulenca1a7cb2014-08-31 17:20:39 +0200219
Bert Vermeulen478c8d92014-09-08 00:02:03 +0200220 if (cg) {
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200221 /*
222 * These options only apply to channel groups with a single
223 * channel -- they're per-channel settings for the device.
224 */
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200225
Bert Vermeulen478c8d92014-09-08 00:02:03 +0200226 /*
227 * Config keys are handled below depending on whether a channel
228 * group was provided by the frontend. However some of these
229 * take a CG on one PPS but not on others. Check the device's
230 * profile for that here, and NULL out the channel group as needed.
231 */
232 for (i = 0; i < devc->device->num_devopts; i++) {
233 if (devc->device->devopts[i] == key) {
234 cg = NULL;
235 break;
236 }
237 }
238
239 ch = cg->channels->data;
Bert Vermeulen01b02572014-09-08 03:26:19 +0200240 pch = ch->priv;
Bert Vermeulen478c8d92014-09-08 00:02:03 +0200241 }
242
243 gvtype = NULL;
244 cmd = -1;
245 switch (key) {
246 case SR_CONF_OUTPUT_ENABLED:
247 gvtype = G_VARIANT_TYPE_BOOLEAN;
248 cmd = SCPI_CMD_GET_OUTPUT_ENABLED;
249 break;
250 case SR_CONF_OUTPUT_VOLTAGE:
251 gvtype = G_VARIANT_TYPE_DOUBLE;
252 cmd = SCPI_CMD_GET_MEAS_VOLTAGE;
253 break;
254 case SR_CONF_OUTPUT_VOLTAGE_MAX:
255 gvtype = G_VARIANT_TYPE_DOUBLE;
256 cmd = SCPI_CMD_GET_VOLTAGE_MAX;
257 break;
258 case SR_CONF_OUTPUT_CURRENT:
259 gvtype = G_VARIANT_TYPE_DOUBLE;
260 cmd = SCPI_CMD_GET_MEAS_CURRENT;
261 break;
262 case SR_CONF_OUTPUT_CURRENT_MAX:
263 gvtype = G_VARIANT_TYPE_DOUBLE;
264 cmd = SCPI_CMD_GET_CURRENT_MAX;
265 break;
266 case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
267 gvtype = G_VARIANT_TYPE_BOOLEAN;
268 cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ENABLED;
269 break;
270 case SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE:
271 gvtype = G_VARIANT_TYPE_BOOLEAN;
272 cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ACTIVE;
273 break;
274 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
275 gvtype = G_VARIANT_TYPE_DOUBLE;
276 cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_THRESHOLD;
277 break;
278 case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
279 gvtype = G_VARIANT_TYPE_BOOLEAN;
280 cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ENABLED;
281 break;
282 case SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE:
283 gvtype = G_VARIANT_TYPE_BOOLEAN;
284 cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ACTIVE;
285 break;
286 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
287 gvtype = G_VARIANT_TYPE_DOUBLE;
288 cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_THRESHOLD;
289 break;
290 case SR_CONF_OVER_TEMPERATURE_PROTECTION:
291 gvtype = G_VARIANT_TYPE_BOOLEAN;
292 cmd = SCPI_CMD_GET_OVER_TEMPERATURE_PROTECTION;
293 break;
294 }
295 if (gvtype) {
296 if (cg)
Bert Vermeulen01b02572014-09-08 03:26:19 +0200297 ret = scpi_cmd_resp(sdi, data, gvtype, cmd, pch->hwname);
Bert Vermeulen478c8d92014-09-08 00:02:03 +0200298 else
299 ret = scpi_cmd_resp(sdi, data, gvtype, cmd);
300 } else if (cg) {
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200301 switch (key) {
302 case SR_CONF_OUTPUT_REGULATION:
303 ret = SR_ERR;
Bert Vermeulen01b02572014-09-08 03:26:19 +0200304 if (scpi_cmd(sdi, SCPI_CMD_GET_OUTPUT_REGULATION, pch->hwname) == SR_OK) {
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200305 if (sr_scpi_get_string(scpi, NULL, &s) == SR_OK) {
306 if (strcmp(s, "CC") && strcmp(s, "CV") && strcmp(s, "UR")) {
307 sr_dbg("Unknown response to SCPI_CMD_GET_OUTPUT_REGULATION: %s", s);
308 } else {
309 *data = g_variant_new_string(s);
310 g_free(s);
311 ret = SR_OK;
312 }
313 }
314 }
315 break;
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200316 default:
Bert Vermeulen478c8d92014-09-08 00:02:03 +0200317 ret = SR_ERR_NA;
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200318 }
Bert Vermeulen478c8d92014-09-08 00:02:03 +0200319 } else
320 ret = SR_ERR_NA;
Bert Vermeulenca1a7cb2014-08-31 17:20:39 +0200321
322 return ret;
323}
324
325static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
326 const struct sr_channel_group *cg)
327{
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200328 struct sr_channel *ch;
Bert Vermeulen01b02572014-09-08 03:26:19 +0200329 struct pps_channel *pch;
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200330 double d;
Bert Vermeulenca1a7cb2014-08-31 17:20:39 +0200331 int ret;
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200332 const char *s;
Bert Vermeulenca1a7cb2014-08-31 17:20:39 +0200333
Bert Vermeulenfdedbfc2014-09-08 23:30:21 +0200334 if (!sdi)
335 return SR_ERR_ARG;
336
Bert Vermeulenca1a7cb2014-08-31 17:20:39 +0200337 if (sdi->status != SR_ST_ACTIVE)
338 return SR_ERR_DEV_CLOSED;
339
340 ret = SR_OK;
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200341 if (!cg) {
342 switch (key) {
343 /* No channel group: global options. */
Bert Vermeulen478c8d92014-09-08 00:02:03 +0200344 case SR_CONF_OUTPUT_ENABLED:
345 s = g_variant_get_boolean(data) ? "ON" : "OFF";
346 ret = scpi_cmd(sdi, SCPI_CMD_SET_OUTPUT_ENABLED, s);
347 break;
348 case SR_CONF_OUTPUT_VOLTAGE_MAX:
349 d = g_variant_get_double(data);
350 ret = scpi_cmd(sdi, SCPI_CMD_SET_VOLTAGE_MAX, d);
351 break;
352 case SR_CONF_OUTPUT_CURRENT_MAX:
353 d = g_variant_get_double(data);
354 ret = scpi_cmd(sdi, SCPI_CMD_SET_CURRENT_MAX, d);
355 break;
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200356 case SR_CONF_OVER_TEMPERATURE_PROTECTION:
357 s = g_variant_get_boolean(data) ? "ON" : "OFF";
Bert Vermeulen478c8d92014-09-08 00:02:03 +0200358 ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION, s);
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200359 break;
360 default:
361 ret = SR_ERR_NA;
362 }
363 } else {
364 /* Channel group specified. */
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200365 ch = cg->channels->data;
Bert Vermeulen01b02572014-09-08 03:26:19 +0200366 pch = ch->priv;
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200367 switch (key) {
Bert Vermeulen478c8d92014-09-08 00:02:03 +0200368 case SR_CONF_OUTPUT_ENABLED:
369 s = g_variant_get_boolean(data) ? "ON" : "OFF";
Bert Vermeulen01b02572014-09-08 03:26:19 +0200370 ret = scpi_cmd(sdi, SCPI_CMD_SET_OUTPUT_ENABLED, pch->hwname, s);
Bert Vermeulen478c8d92014-09-08 00:02:03 +0200371 break;
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200372 case SR_CONF_OUTPUT_VOLTAGE_MAX:
373 d = g_variant_get_double(data);
Bert Vermeulen01b02572014-09-08 03:26:19 +0200374 ret = scpi_cmd(sdi, SCPI_CMD_SET_VOLTAGE_MAX, pch->hwname, d);
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200375 break;
376 case SR_CONF_OUTPUT_CURRENT_MAX:
377 d = g_variant_get_double(data);
Bert Vermeulen01b02572014-09-08 03:26:19 +0200378 ret = scpi_cmd(sdi, SCPI_CMD_SET_CURRENT_MAX, pch->hwname, d);
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200379 break;
380 case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
381 s = g_variant_get_boolean(data) ? "ON" : "OFF";
Bert Vermeulen478c8d92014-09-08 00:02:03 +0200382 ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_ENABLED,
Bert Vermeulen01b02572014-09-08 03:26:19 +0200383 pch->hwname, s);
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200384 break;
385 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
386 d = g_variant_get_double(data);
Bert Vermeulen478c8d92014-09-08 00:02:03 +0200387 ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_THRESHOLD,
Bert Vermeulen01b02572014-09-08 03:26:19 +0200388 pch->hwname, d);
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200389 break;
390 case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
391 s = g_variant_get_boolean(data) ? "ON" : "OFF";
Bert Vermeulen478c8d92014-09-08 00:02:03 +0200392 ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_CURRENT_PROTECTION_ENABLED,
Bert Vermeulen01b02572014-09-08 03:26:19 +0200393 pch->hwname, s);
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200394 break;
395 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
396 d = g_variant_get_double(data);
Bert Vermeulen478c8d92014-09-08 00:02:03 +0200397 ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_CURRENT_PROTECTION_THRESHOLD,
Bert Vermeulen01b02572014-09-08 03:26:19 +0200398 pch->hwname, d);
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200399 break;
400 default:
401 ret = SR_ERR_NA;
402 }
Bert Vermeulenca1a7cb2014-08-31 17:20:39 +0200403 }
404
405 return ret;
406}
407
408static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
409 const struct sr_channel_group *cg)
410{
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200411 struct dev_context *devc;
412 struct sr_channel *ch;
413 struct channel_spec *ch_spec;
414 GVariant *gvar;
415 GVariantBuilder gvb;
416 int ret, i;
417 const char *s[16];
Bert Vermeulenca1a7cb2014-08-31 17:20:39 +0200418
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200419 /* Always available, even without sdi. */
420 if (key == SR_CONF_SCAN_OPTIONS) {
421 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
422 scanopts, ARRAY_SIZE(scanopts), sizeof(int32_t));
423 return SR_OK;
424 }
425
426 if (!sdi)
427 return SR_ERR_ARG;
428 devc = sdi->priv;
Bert Vermeulenca1a7cb2014-08-31 17:20:39 +0200429
430 ret = SR_OK;
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200431 if (!cg) {
432 /* No channel group: global options. */
433 switch (key) {
434 case SR_CONF_DEVICE_OPTIONS:
435 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
436 devc->device->devopts, devc->device->num_devopts,
437 sizeof(int32_t));
438 break;
439 case SR_CONF_OUTPUT_CHANNEL_CONFIG:
Bert Vermeulen478c8d92014-09-08 00:02:03 +0200440 /* Not used. */
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200441 i = 0;
442 if (devc->device->features & PPS_INDEPENDENT)
443 s[i++] = "Independent";
444 if (devc->device->features & PPS_SERIES)
445 s[i++] = "Series";
446 if (devc->device->features & PPS_PARALLEL)
447 s[i++] = "Parallel";
448 if (i == 0) {
449 /*
450 * Shouldn't happen: independent-only devices
451 * shouldn't advertise this option at all.
452 */
453 return SR_ERR_NA;
454 }
455 *data = g_variant_new_strv(s, i);
456 break;
457 default:
458 return SR_ERR_NA;
459 }
460 } else {
461 /* Channel group specified. */
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200462 /*
463 * Per-channel-group options depending on a channel are actually
464 * done with the first channel. Channel groups in PPS can have
465 * more than one channel, but they will typically be of equal
Bert Vermeulen01b02572014-09-08 03:26:19 +0200466 * specification for use in series or parallel mode.
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200467 */
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200468 ch = cg->channels->data;
469
470 switch (key) {
471 case SR_CONF_DEVICE_OPTIONS:
472 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
473 devc->device->devopts_cg, devc->device->num_devopts_cg,
474 sizeof(int32_t));
475 break;
476 case SR_CONF_OUTPUT_VOLTAGE_MAX:
477 ch_spec = &(devc->device->channels[ch->index]);
478 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
479 /* Min, max, write resolution. */
480 for (i = 0; i < 3; i++) {
481 gvar = g_variant_new_double(ch_spec->voltage[i]);
482 g_variant_builder_add_value(&gvb, gvar);
483 }
484 *data = g_variant_builder_end(&gvb);
485 break;
486 case SR_CONF_OUTPUT_CURRENT_MAX:
487 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
488 /* Min, max, step. */
489 for (i = 0; i < 3; i++) {
490 ch_spec = &(devc->device->channels[ch->index]);
491 gvar = g_variant_new_double(ch_spec->current[i]);
492 g_variant_builder_add_value(&gvb, gvar);
493 }
494 *data = g_variant_builder_end(&gvb);
495 break;
496 default:
497 return SR_ERR_NA;
498 }
Bert Vermeulenca1a7cb2014-08-31 17:20:39 +0200499 }
500
501 return ret;
502}
503
504static int dev_acquisition_start(const struct sr_dev_inst *sdi,
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200505 void *cb_data)
Bert Vermeulenca1a7cb2014-08-31 17:20:39 +0200506{
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200507 struct dev_context *devc;
508 struct sr_scpi_dev_inst *scpi;
509 struct sr_channel *ch;
Bert Vermeulen01b02572014-09-08 03:26:19 +0200510 struct pps_channel *pch;
511 int cmd, ret;
Bert Vermeulenca1a7cb2014-08-31 17:20:39 +0200512
513 if (sdi->status != SR_ST_ACTIVE)
514 return SR_ERR_DEV_CLOSED;
515
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200516 devc = sdi->priv;
517 scpi = sdi->conn;
518 devc->cb_data = cb_data;
519
Bert Vermeulen01b02572014-09-08 03:26:19 +0200520 if ((ret = sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 10,
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200521 scpi_pps_receive_data, (void *)sdi)) != SR_OK)
522 return ret;
523 std_session_send_df_header(sdi, LOG_PREFIX);
524
Bert Vermeulen01b02572014-09-08 03:26:19 +0200525 /* Prime the pipe with the first channel's fetch. */
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200526 ch = sdi->channels->data;
Bert Vermeulen01b02572014-09-08 03:26:19 +0200527 pch = ch->priv;
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200528 devc->cur_channel = ch;
Bert Vermeulen01b02572014-09-08 03:26:19 +0200529 if (pch->mq == SR_MQ_VOLTAGE)
530 cmd = SCPI_CMD_GET_MEAS_VOLTAGE;
531 else if (pch->mq == SR_MQ_CURRENT)
532 cmd = SCPI_CMD_GET_MEAS_CURRENT;
533 else if (pch->mq == SR_MQ_POWER)
534 cmd = SCPI_CMD_GET_MEAS_POWER;
535 else
536 return SR_ERR;
537 scpi_cmd(sdi, cmd, pch->hwname);
Bert Vermeulenca1a7cb2014-08-31 17:20:39 +0200538
539 return SR_OK;
540}
541
542static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
543{
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200544 struct sr_scpi_dev_inst *scpi;
545 float f;
546
Bert Vermeulenca1a7cb2014-08-31 17:20:39 +0200547 (void)cb_data;
548
549 if (sdi->status != SR_ST_ACTIVE)
550 return SR_ERR_DEV_CLOSED;
551
Bert Vermeulen9e45cd42014-09-05 03:20:32 +0200552 scpi = sdi->conn;
553
554 /*
555 * A requested value is certainly on the way. Retrieve it now,
556 * to avoid leaving the device in a state where it's not expecting
557 * commands.
558 */
559 sr_scpi_get_float(scpi, NULL, &f);
560 sr_scpi_source_remove(sdi->session, scpi);
561
Bert Vermeulenca1a7cb2014-08-31 17:20:39 +0200562 return SR_OK;
563}
564
565SR_PRIV struct sr_dev_driver scpi_pps_driver_info = {
566 .name = "scpi-pps",
567 .longname = "SCPI PPS",
568 .api_version = 1,
569 .init = init,
570 .cleanup = cleanup,
571 .scan = scan,
572 .dev_list = dev_list,
573 .dev_clear = dev_clear,
574 .config_get = config_get,
575 .config_set = config_set,
576 .config_list = config_list,
577 .dev_open = dev_open,
578 .dev_close = dev_close,
579 .dev_acquisition_start = dev_acquisition_start,
580 .dev_acquisition_stop = dev_acquisition_stop,
581 .priv = NULL,
582};