]>
Commit | Line | Data |
---|---|---|
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 | ||
9e45cd41 | 20 | #include <string.h> |
ca1a7cb5 BV |
21 | #include "protocol.h" |
22 | ||
23 | SR_PRIV struct sr_dev_driver scpi_pps_driver_info; | |
24 | static struct sr_dev_driver *di = &scpi_pps_driver_info; | |
d4eabea8 BV |
25 | extern unsigned int num_pps_profiles; |
26 | extern const struct scpi_pps pps_profiles[]; | |
9e45cd41 BV |
27 | |
28 | static const int32_t scanopts[] = { | |
29 | SR_CONF_CONN, | |
30 | SR_CONF_SERIALCOMM, | |
31 | }; | |
ca1a7cb5 | 32 | |
01b0257a BV |
33 | static 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 | ||
ca1a7cb5 BV |
39 | static int init(struct sr_context *sr_ctx) |
40 | { | |
41 | return std_init(sr_ctx, di, LOG_PREFIX); | |
42 | } | |
43 | ||
9e45cd41 | 44 | static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi) |
ca1a7cb5 | 45 | { |
9e45cd41 BV |
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; | |
01b0257a | 52 | struct pps_channel *pch; |
9e45cd41 BV |
53 | const struct channel_group_spec *cgs; |
54 | struct pps_channel_group *pcg; | |
58b77c41 BV |
55 | GRegex *model_re; |
56 | GMatchInfo *model_mi; | |
01b0257a | 57 | GSList *l; |
9e45cd41 | 58 | uint64_t mask; |
01b0257a | 59 | unsigned int ch_num, ch_idx, old_idx, i, j; |
22c18b03 | 60 | const char *vendor; |
01b0257a | 61 | char ch_name[16]; |
9e45cd41 BV |
62 | |
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++) { | |
22c18b03 | 70 | vendor = get_vendor(hw_info->manufacturer); |
58b77c41 | 71 | if (strcasecmp(vendor, pps_profiles[i].vendor)) |
22c18b03 | 72 | continue; |
58b77c41 BV |
73 | 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)) | |
9e45cd41 | 75 | device = &pps_profiles[i]; |
58b77c41 BV |
76 | g_match_info_unref(model_mi); |
77 | g_regex_unref(model_re); | |
78 | if (device) | |
9e45cd41 | 79 | break; |
9e45cd41 BV |
80 | } |
81 | if (!device) { | |
82 | sr_scpi_hw_info_free(hw_info); | |
83 | return NULL; | |
84 | } | |
85 | ||
58b77c41 BV |
86 | sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, vendor, hw_info->model, |
87 | hw_info->firmware_version); | |
9e45cd41 BV |
88 | 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 | ||
01b0257a BV |
95 | 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 | } | |
9e45cd41 | 120 | } |
ca1a7cb5 | 121 | |
9e45cd41 BV |
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) { | |
01b0257a BV |
128 | 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 | } | |
9e45cd41 BV |
134 | } |
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 | } | |
ca1a7cb5 | 141 | |
9e45cd41 BV |
142 | /* SCPI devices commonly lock the panel keys when accessed remotely. */ |
143 | scpi_cmd(sdi, SCPI_CMD_KEY_UNLOCK); | |
144 | sr_scpi_close(scpi); | |
ca1a7cb5 | 145 | |
9e45cd41 BV |
146 | return sdi; |
147 | } | |
ca1a7cb5 | 148 | |
9e45cd41 BV |
149 | static GSList *scan(GSList *options) |
150 | { | |
151 | return sr_scpi_scan(di->priv, options, probe_device); | |
ca1a7cb5 BV |
152 | } |
153 | ||
154 | static GSList *dev_list(void) | |
155 | { | |
156 | return ((struct drv_context *)(di->priv))->instances; | |
157 | } | |
158 | ||
159 | static int dev_clear(void) | |
160 | { | |
161 | return std_dev_clear(di, NULL); | |
162 | } | |
163 | ||
164 | static int dev_open(struct sr_dev_inst *sdi) | |
165 | { | |
9e45cd41 BV |
166 | struct sr_scpi_dev_inst *scpi; |
167 | ||
168 | if (sdi->status != SR_ST_ACTIVE) | |
169 | return SR_ERR; | |
ca1a7cb5 | 170 | |
9e45cd41 BV |
171 | scpi = sdi->conn; |
172 | if (sr_scpi_open(scpi) < 0) | |
173 | return SR_ERR; | |
ca1a7cb5 BV |
174 | |
175 | sdi->status = SR_ST_ACTIVE; | |
176 | ||
177 | return SR_OK; | |
178 | } | |
179 | ||
180 | static int dev_close(struct sr_dev_inst *sdi) | |
181 | { | |
9e45cd41 | 182 | struct sr_scpi_dev_inst *scpi; |
ca1a7cb5 | 183 | |
9e45cd41 BV |
184 | if (sdi->status != SR_ST_ACTIVE) |
185 | return SR_ERR_DEV_CLOSED; | |
ca1a7cb5 | 186 | |
9e45cd41 BV |
187 | 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 | } | |
ca1a7cb5 BV |
193 | |
194 | return SR_OK; | |
195 | } | |
196 | ||
197 | static int cleanup(void) | |
198 | { | |
ca1a7cb5 BV |
199 | return SR_OK; |
200 | } | |
201 | ||
202 | static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi, | |
203 | const struct sr_channel_group *cg) | |
204 | { | |
9e45cd41 BV |
205 | struct dev_context *devc; |
206 | struct sr_scpi_dev_inst *scpi; | |
207 | struct sr_channel *ch; | |
01b0257a | 208 | struct pps_channel *pch; |
478c8d92 BV |
209 | const GVariantType *gvtype; |
210 | unsigned int i; | |
211 | int cmd, ret; | |
9e45cd41 | 212 | char *s; |
ca1a7cb5 | 213 | |
9e45cd41 BV |
214 | if (!sdi) |
215 | return SR_ERR_ARG; | |
216 | ||
217 | devc = sdi->priv; | |
218 | scpi = sdi->conn; | |
ca1a7cb5 | 219 | |
478c8d92 | 220 | if (cg) { |
9e45cd41 BV |
221 | /* |
222 | * These options only apply to channel groups with a single | |
223 | * channel -- they're per-channel settings for the device. | |
224 | */ | |
478c8d92 BV |
225 | |
226 | /* | |
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 | ||
9e45cd41 | 239 | ch = cg->channels->data; |
01b0257a | 240 | pch = ch->priv; |
478c8d92 | 241 | } |
9e45cd41 | 242 | |
478c8d92 BV |
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) | |
01b0257a | 297 | ret = scpi_cmd_resp(sdi, data, gvtype, cmd, pch->hwname); |
478c8d92 BV |
298 | else |
299 | ret = scpi_cmd_resp(sdi, data, gvtype, cmd); | |
300 | } else if (cg) { | |
9e45cd41 BV |
301 | switch (key) { |
302 | case SR_CONF_OUTPUT_REGULATION: | |
303 | ret = SR_ERR; | |
01b0257a | 304 | if (scpi_cmd(sdi, SCPI_CMD_GET_OUTPUT_REGULATION, pch->hwname) == SR_OK) { |
9e45cd41 BV |
305 | 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; | |
9e45cd41 | 316 | default: |
478c8d92 | 317 | ret = SR_ERR_NA; |
9e45cd41 | 318 | } |
478c8d92 BV |
319 | } else |
320 | ret = SR_ERR_NA; | |
ca1a7cb5 BV |
321 | |
322 | return ret; | |
323 | } | |
324 | ||
325 | static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi, | |
326 | const struct sr_channel_group *cg) | |
327 | { | |
9e45cd41 | 328 | struct sr_channel *ch; |
01b0257a | 329 | struct pps_channel *pch; |
9e45cd41 | 330 | double d; |
ca1a7cb5 | 331 | int ret; |
9e45cd41 | 332 | const char *s; |
ca1a7cb5 BV |
333 | |
334 | if (sdi->status != SR_ST_ACTIVE) | |
335 | return SR_ERR_DEV_CLOSED; | |
336 | ||
337 | ret = SR_OK; | |
9e45cd41 BV |
338 | if (!cg) { |
339 | switch (key) { | |
340 | /* No channel group: global options. */ | |
478c8d92 | 341 | case SR_CONF_OUTPUT_ENABLED: |
9e45cd41 | 342 | s = g_variant_get_boolean(data) ? "ON" : "OFF"; |
478c8d92 | 343 | ret = scpi_cmd(sdi, SCPI_CMD_SET_OUTPUT_ENABLED, s); |
9e45cd41 | 344 | break; |
478c8d92 BV |
345 | case SR_CONF_OUTPUT_VOLTAGE_MAX: |
346 | d = g_variant_get_double(data); | |
347 | ret = scpi_cmd(sdi, SCPI_CMD_SET_VOLTAGE_MAX, d); | |
348 | break; | |
349 | case SR_CONF_OUTPUT_CURRENT_MAX: | |
350 | d = g_variant_get_double(data); | |
351 | ret = scpi_cmd(sdi, SCPI_CMD_SET_CURRENT_MAX, d); | |
352 | break; | |
353 | case SR_CONF_OVER_TEMPERATURE_PROTECTION: | |
354 | s = g_variant_get_boolean(data) ? "ON" : "OFF"; | |
355 | ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION, s); | |
9e45cd41 BV |
356 | break; |
357 | default: | |
358 | ret = SR_ERR_NA; | |
359 | } | |
360 | } else { | |
361 | /* Channel group specified. */ | |
362 | if (!sdi) | |
363 | return SR_ERR_ARG; | |
364 | if (g_slist_length(cg->channels) > 1) | |
365 | return SR_ERR_NA; | |
366 | ch = cg->channels->data; | |
01b0257a | 367 | pch = ch->priv; |
9e45cd41 | 368 | switch (key) { |
478c8d92 BV |
369 | case SR_CONF_OUTPUT_ENABLED: |
370 | s = g_variant_get_boolean(data) ? "ON" : "OFF"; | |
01b0257a | 371 | ret = scpi_cmd(sdi, SCPI_CMD_SET_OUTPUT_ENABLED, pch->hwname, s); |
478c8d92 | 372 | break; |
9e45cd41 BV |
373 | case SR_CONF_OUTPUT_VOLTAGE_MAX: |
374 | d = g_variant_get_double(data); | |
01b0257a | 375 | ret = scpi_cmd(sdi, SCPI_CMD_SET_VOLTAGE_MAX, pch->hwname, d); |
9e45cd41 BV |
376 | break; |
377 | case SR_CONF_OUTPUT_CURRENT_MAX: | |
378 | d = g_variant_get_double(data); | |
01b0257a | 379 | ret = scpi_cmd(sdi, SCPI_CMD_SET_CURRENT_MAX, pch->hwname, d); |
9e45cd41 BV |
380 | break; |
381 | case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED: | |
382 | s = g_variant_get_boolean(data) ? "ON" : "OFF"; | |
478c8d92 | 383 | ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_ENABLED, |
01b0257a | 384 | pch->hwname, s); |
9e45cd41 BV |
385 | break; |
386 | case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD: | |
387 | d = g_variant_get_double(data); | |
478c8d92 | 388 | ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_THRESHOLD, |
01b0257a | 389 | pch->hwname, d); |
9e45cd41 BV |
390 | break; |
391 | case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED: | |
392 | s = g_variant_get_boolean(data) ? "ON" : "OFF"; | |
478c8d92 | 393 | ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_CURRENT_PROTECTION_ENABLED, |
01b0257a | 394 | pch->hwname, s); |
9e45cd41 BV |
395 | break; |
396 | case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD: | |
397 | d = g_variant_get_double(data); | |
478c8d92 | 398 | ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_CURRENT_PROTECTION_THRESHOLD, |
01b0257a | 399 | pch->hwname, d); |
9e45cd41 BV |
400 | break; |
401 | default: | |
402 | ret = SR_ERR_NA; | |
403 | } | |
ca1a7cb5 BV |
404 | } |
405 | ||
406 | return ret; | |
407 | } | |
408 | ||
409 | static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi, | |
410 | const struct sr_channel_group *cg) | |
411 | { | |
9e45cd41 BV |
412 | struct dev_context *devc; |
413 | struct sr_channel *ch; | |
414 | struct channel_spec *ch_spec; | |
415 | GVariant *gvar; | |
416 | GVariantBuilder gvb; | |
417 | int ret, i; | |
418 | const char *s[16]; | |
419 | ||
420 | /* Always available, even without sdi. */ | |
421 | if (key == SR_CONF_SCAN_OPTIONS) { | |
422 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
423 | scanopts, ARRAY_SIZE(scanopts), sizeof(int32_t)); | |
424 | return SR_OK; | |
425 | } | |
ca1a7cb5 | 426 | |
9e45cd41 BV |
427 | if (!sdi) |
428 | return SR_ERR_ARG; | |
429 | devc = sdi->priv; | |
ca1a7cb5 BV |
430 | |
431 | ret = SR_OK; | |
9e45cd41 BV |
432 | if (!cg) { |
433 | /* No channel group: global options. */ | |
434 | switch (key) { | |
435 | case SR_CONF_DEVICE_OPTIONS: | |
436 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
437 | devc->device->devopts, devc->device->num_devopts, | |
438 | sizeof(int32_t)); | |
439 | break; | |
440 | case SR_CONF_OUTPUT_CHANNEL_CONFIG: | |
478c8d92 | 441 | /* Not used. */ |
9e45cd41 BV |
442 | i = 0; |
443 | if (devc->device->features & PPS_INDEPENDENT) | |
444 | s[i++] = "Independent"; | |
445 | if (devc->device->features & PPS_SERIES) | |
446 | s[i++] = "Series"; | |
447 | if (devc->device->features & PPS_PARALLEL) | |
448 | s[i++] = "Parallel"; | |
449 | if (i == 0) { | |
450 | /* | |
451 | * Shouldn't happen: independent-only devices | |
452 | * shouldn't advertise this option at all. | |
453 | */ | |
454 | return SR_ERR_NA; | |
455 | } | |
456 | *data = g_variant_new_strv(s, i); | |
457 | break; | |
458 | default: | |
459 | return SR_ERR_NA; | |
460 | } | |
461 | } else { | |
462 | /* Channel group specified. */ | |
9e45cd41 BV |
463 | /* |
464 | * Per-channel-group options depending on a channel are actually | |
465 | * done with the first channel. Channel groups in PPS can have | |
466 | * more than one channel, but they will typically be of equal | |
01b0257a | 467 | * specification for use in series or parallel mode. |
9e45cd41 | 468 | */ |
9e45cd41 BV |
469 | ch = cg->channels->data; |
470 | ||
471 | switch (key) { | |
472 | case SR_CONF_DEVICE_OPTIONS: | |
473 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
474 | devc->device->devopts_cg, devc->device->num_devopts_cg, | |
475 | sizeof(int32_t)); | |
476 | break; | |
477 | case SR_CONF_OUTPUT_VOLTAGE_MAX: | |
478 | ch_spec = &(devc->device->channels[ch->index]); | |
479 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); | |
480 | /* Min, max, write resolution. */ | |
481 | for (i = 0; i < 3; i++) { | |
482 | gvar = g_variant_new_double(ch_spec->voltage[i]); | |
483 | g_variant_builder_add_value(&gvb, gvar); | |
484 | } | |
485 | *data = g_variant_builder_end(&gvb); | |
486 | break; | |
487 | case SR_CONF_OUTPUT_CURRENT_MAX: | |
488 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); | |
489 | /* Min, max, step. */ | |
490 | for (i = 0; i < 3; i++) { | |
491 | ch_spec = &(devc->device->channels[ch->index]); | |
492 | gvar = g_variant_new_double(ch_spec->current[i]); | |
493 | g_variant_builder_add_value(&gvb, gvar); | |
494 | } | |
495 | *data = g_variant_builder_end(&gvb); | |
496 | break; | |
497 | default: | |
498 | return SR_ERR_NA; | |
499 | } | |
ca1a7cb5 BV |
500 | } |
501 | ||
502 | return ret; | |
503 | } | |
504 | ||
505 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, | |
9e45cd41 | 506 | void *cb_data) |
ca1a7cb5 | 507 | { |
9e45cd41 BV |
508 | struct dev_context *devc; |
509 | struct sr_scpi_dev_inst *scpi; | |
510 | struct sr_channel *ch; | |
01b0257a BV |
511 | struct pps_channel *pch; |
512 | int cmd, ret; | |
ca1a7cb5 BV |
513 | |
514 | if (sdi->status != SR_ST_ACTIVE) | |
515 | return SR_ERR_DEV_CLOSED; | |
516 | ||
9e45cd41 BV |
517 | devc = sdi->priv; |
518 | scpi = sdi->conn; | |
519 | devc->cb_data = cb_data; | |
520 | ||
01b0257a | 521 | if ((ret = sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 10, |
9e45cd41 BV |
522 | scpi_pps_receive_data, (void *)sdi)) != SR_OK) |
523 | return ret; | |
524 | std_session_send_df_header(sdi, LOG_PREFIX); | |
525 | ||
01b0257a | 526 | /* Prime the pipe with the first channel's fetch. */ |
9e45cd41 | 527 | ch = sdi->channels->data; |
01b0257a | 528 | pch = ch->priv; |
9e45cd41 | 529 | devc->cur_channel = ch; |
01b0257a BV |
530 | if (pch->mq == SR_MQ_VOLTAGE) |
531 | cmd = SCPI_CMD_GET_MEAS_VOLTAGE; | |
532 | else if (pch->mq == SR_MQ_CURRENT) | |
533 | cmd = SCPI_CMD_GET_MEAS_CURRENT; | |
534 | else if (pch->mq == SR_MQ_POWER) | |
535 | cmd = SCPI_CMD_GET_MEAS_POWER; | |
536 | else | |
537 | return SR_ERR; | |
538 | scpi_cmd(sdi, cmd, pch->hwname); | |
ca1a7cb5 BV |
539 | |
540 | return SR_OK; | |
541 | } | |
542 | ||
543 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
544 | { | |
9e45cd41 BV |
545 | struct sr_scpi_dev_inst *scpi; |
546 | float f; | |
547 | ||
ca1a7cb5 BV |
548 | (void)cb_data; |
549 | ||
550 | if (sdi->status != SR_ST_ACTIVE) | |
551 | return SR_ERR_DEV_CLOSED; | |
552 | ||
9e45cd41 BV |
553 | scpi = sdi->conn; |
554 | ||
555 | /* | |
556 | * A requested value is certainly on the way. Retrieve it now, | |
557 | * to avoid leaving the device in a state where it's not expecting | |
558 | * commands. | |
559 | */ | |
560 | sr_scpi_get_float(scpi, NULL, &f); | |
561 | sr_scpi_source_remove(sdi->session, scpi); | |
562 | ||
ca1a7cb5 BV |
563 | return SR_OK; |
564 | } | |
565 | ||
566 | SR_PRIV struct sr_dev_driver scpi_pps_driver_info = { | |
567 | .name = "scpi-pps", | |
568 | .longname = "SCPI PPS", | |
569 | .api_version = 1, | |
570 | .init = init, | |
571 | .cleanup = cleanup, | |
572 | .scan = scan, | |
573 | .dev_list = dev_list, | |
574 | .dev_clear = dev_clear, | |
575 | .config_get = config_get, | |
576 | .config_set = config_set, | |
577 | .config_list = config_list, | |
578 | .dev_open = dev_open, | |
579 | .dev_close = dev_close, | |
580 | .dev_acquisition_start = dev_acquisition_start, | |
581 | .dev_acquisition_stop = dev_acquisition_stop, | |
582 | .priv = NULL, | |
583 | }; |