]> sigrok.org Git - libsigrok.git/blob - src/hardware/scpi-pps/api.c
4c5e9afc96f547ee09e5919b5d01469f64d9df9a
[libsigrok.git] / src / hardware / scpi-pps / api.c
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
20 #include <string.h>
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;
25 extern unsigned int num_pps_profiles;
26 extern const struct scpi_pps pps_profiles[];
27
28 static const uint32_t scanopts[] = {
29         SR_CONF_CONN,
30         SR_CONF_SERIALCOMM,
31 };
32
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
39 static int init(struct sr_context *sr_ctx)
40 {
41         return std_init(sr_ctx, di, LOG_PREFIX);
42 }
43
44 static 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;
52         struct pps_channel *pch;
53         const struct channel_group_spec *cgs;
54         struct pps_channel_group *pcg;
55         GRegex *model_re;
56         GMatchInfo *model_mi;
57         GSList *l;
58         uint64_t mask;
59         unsigned int ch_num, ch_idx, i, j;
60         const char *vendor;
61         char ch_name[16];
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++) {
70                 vendor = get_vendor(hw_info->manufacturer);
71                 if (strcasecmp(vendor, pps_profiles[i].vendor))
72                         continue;
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))
75                         device = &pps_profiles[i];
76                 g_match_info_unref(model_mi);
77                 g_regex_unref(model_re);
78                 if (device)
79                         break;
80         }
81         if (!device) {
82                 sr_scpi_hw_info_free(hw_info);
83                 return NULL;
84         }
85
86         sdi = sr_dev_inst_new(SR_ST_ACTIVE, vendor, hw_info->model,
87                         hw_info->firmware_version);
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
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                 for (i = 0; i < ARRAY_SIZE(pci); i++) {
99                         if (!scpi_cmd_get(sdi, pci[i].command))
100                                 continue;
101                         g_snprintf(ch_name, 16, "%s%s", pci[i].prefix,
102                                         device->channels[ch_num].name);
103                         ch = sr_channel_new(ch_idx++, SR_CHANNEL_ANALOG, TRUE, ch_name);
104                         pch = g_malloc0(sizeof(struct pps_channel));
105                         pch->hw_output_idx = ch_num;
106                         pch->hwname = device->channels[ch_num].name;
107                         pch->mq = pci[i].mq;
108                         ch->priv = pch;
109                         sdi->channels = g_slist_append(sdi->channels, ch);
110                 }
111         }
112
113         for (i = 0; i < device->num_channel_groups; i++) {
114                 cgs = &device->channel_groups[i];
115                 cg = g_malloc0(sizeof(struct sr_channel_group));
116                 cg->name = g_strdup(cgs->name);
117                 for (j = 0, mask = 1; j < 64; j++, mask <<= 1) {
118                         if (cgs->channel_index_mask & mask) {
119                                 for (l = sdi->channels; l; l = l->next) {
120                                         ch = l->data;
121                                         pch = ch->priv;
122                                         if (pch->hw_output_idx == j)
123                                                 cg->channels = g_slist_append(cg->channels, ch);
124                                 }
125                         }
126                 }
127                 pcg = g_malloc0(sizeof(struct pps_channel_group));
128                 pcg->features = cgs->features;
129                 cg->priv = pcg;
130                 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
131         }
132
133         /* SCPI devices commonly lock the panel keys when accessed remotely. */
134         scpi_cmd(sdi, SCPI_CMD_KEY_UNLOCK);
135         sr_scpi_close(scpi);
136
137         return sdi;
138 }
139
140 static GSList *scan(GSList *options)
141 {
142         return sr_scpi_scan(di->priv, options, probe_device);
143 }
144
145 static GSList *dev_list(void)
146 {
147         return ((struct drv_context *)(di->priv))->instances;
148 }
149
150 static int dev_clear(void)
151 {
152         return std_dev_clear(di, NULL);
153 }
154
155 static int dev_open(struct sr_dev_inst *sdi)
156 {
157         struct sr_scpi_dev_inst *scpi;
158
159         if (sdi->status != SR_ST_ACTIVE)
160                 return SR_ERR;
161
162         scpi = sdi->conn;
163         if (sr_scpi_open(scpi) < 0)
164                 return SR_ERR;
165
166         sdi->status = SR_ST_ACTIVE;
167
168         return SR_OK;
169 }
170
171 static int dev_close(struct sr_dev_inst *sdi)
172 {
173         struct sr_scpi_dev_inst *scpi;
174
175         if (sdi->status != SR_ST_ACTIVE)
176                 return SR_ERR_DEV_CLOSED;
177
178         scpi = sdi->conn;
179         if (scpi) {
180                 scpi_cmd(sdi, SCPI_CMD_KEY_UNLOCK);
181                 sr_scpi_close(scpi);
182                 sdi->status = SR_ST_INACTIVE;
183         }
184
185         return SR_OK;
186 }
187
188 static int cleanup(void)
189 {
190         return SR_OK;
191 }
192
193 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
194                 const struct sr_channel_group *cg)
195 {
196         struct dev_context *devc;
197         struct sr_scpi_dev_inst *scpi;
198         struct sr_channel *ch;
199         struct pps_channel *pch;
200         const GVariantType *gvtype;
201         unsigned int i;
202         int cmd, ret;
203         char *s;
204
205         if (!sdi)
206                 return SR_ERR_ARG;
207
208         devc = sdi->priv;
209         scpi = sdi->conn;
210
211         if (cg) {
212                 /*
213                  * These options only apply to channel groups with a single
214                  * channel -- they're per-channel settings for the device.
215                  */
216
217                 /*
218                  * Config keys are handled below depending on whether a channel
219                  * group was provided by the frontend. However some of these
220                  * take a CG on one PPS but not on others. Check the device's
221                  * profile for that here, and NULL out the channel group as needed.
222                  */
223                 for (i = 0; i < devc->device->num_devopts; i++) {
224                         if (devc->device->devopts[i] == key) {
225                                 cg = NULL;
226                                 break;
227                         }
228                 }
229
230                 ch = cg->channels->data;
231                 pch = ch->priv;
232         }
233
234         gvtype = NULL;
235         cmd = -1;
236         switch (key) {
237         case SR_CONF_OUTPUT_ENABLED:
238                 gvtype = G_VARIANT_TYPE_BOOLEAN;
239                 cmd = SCPI_CMD_GET_OUTPUT_ENABLED;
240                 break;
241         case SR_CONF_OUTPUT_VOLTAGE:
242                 gvtype = G_VARIANT_TYPE_DOUBLE;
243                 cmd = SCPI_CMD_GET_MEAS_VOLTAGE;
244                 break;
245         case SR_CONF_OUTPUT_VOLTAGE_MAX:
246                 gvtype = G_VARIANT_TYPE_DOUBLE;
247                 cmd = SCPI_CMD_GET_VOLTAGE_MAX;
248                 break;
249         case SR_CONF_OUTPUT_CURRENT:
250                 gvtype = G_VARIANT_TYPE_DOUBLE;
251                 cmd = SCPI_CMD_GET_MEAS_CURRENT;
252                 break;
253         case SR_CONF_OUTPUT_CURRENT_MAX:
254                 gvtype = G_VARIANT_TYPE_DOUBLE;
255                 cmd = SCPI_CMD_GET_CURRENT_MAX;
256                 break;
257         case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
258                 gvtype = G_VARIANT_TYPE_BOOLEAN;
259                 cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ENABLED;
260                 break;
261         case SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE:
262                 gvtype = G_VARIANT_TYPE_BOOLEAN;
263                 cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ACTIVE;
264                 break;
265         case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
266                 gvtype = G_VARIANT_TYPE_DOUBLE;
267                 cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_THRESHOLD;
268                 break;
269         case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
270                 gvtype = G_VARIANT_TYPE_BOOLEAN;
271                 cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ENABLED;
272                 break;
273         case SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE:
274                 gvtype = G_VARIANT_TYPE_BOOLEAN;
275                 cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ACTIVE;
276                 break;
277         case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
278                 gvtype = G_VARIANT_TYPE_DOUBLE;
279                 cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_THRESHOLD;
280                 break;
281         case SR_CONF_OVER_TEMPERATURE_PROTECTION:
282                 gvtype = G_VARIANT_TYPE_BOOLEAN;
283                 cmd = SCPI_CMD_GET_OVER_TEMPERATURE_PROTECTION;
284                 break;
285         }
286         if (gvtype) {
287                 if (cg)
288                         ret = scpi_cmd_resp(sdi, data, gvtype, cmd, pch->hwname);
289                 else
290                         ret = scpi_cmd_resp(sdi, data, gvtype, cmd);
291         } else if (cg) {
292                 switch (key) {
293                 case SR_CONF_OUTPUT_REGULATION:
294                         ret = SR_ERR;
295                         if (scpi_cmd(sdi, SCPI_CMD_GET_OUTPUT_REGULATION, pch->hwname) == SR_OK) {
296                                 if (sr_scpi_get_string(scpi, NULL, &s) == SR_OK) {
297                                         if (strcmp(s, "CC") && strcmp(s, "CV") && strcmp(s, "UR")) {
298                                                 sr_dbg("Unknown response to SCPI_CMD_GET_OUTPUT_REGULATION: %s", s);
299                                         } else {
300                                                 *data = g_variant_new_string(s);
301                                                 g_free(s);
302                                                 ret = SR_OK;
303                                         }
304                                 }
305                         }
306                         break;
307                 default:
308                         ret = SR_ERR_NA;
309                 }
310         } else
311                 ret = SR_ERR_NA;
312
313         return ret;
314 }
315
316 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
317                 const struct sr_channel_group *cg)
318 {
319         struct sr_channel *ch;
320         struct pps_channel *pch;
321         double d;
322         int ret;
323
324         if (!sdi)
325                 return SR_ERR_ARG;
326
327         if (sdi->status != SR_ST_ACTIVE)
328                 return SR_ERR_DEV_CLOSED;
329
330         ret = SR_OK;
331         if (!cg) {
332                 switch (key) {
333                 /* No channel group: global options. */
334                 case SR_CONF_OUTPUT_ENABLED:
335                         if (g_variant_get_boolean(data))
336                                 ret = scpi_cmd(sdi, SCPI_CMD_SET_OUTPUT_ENABLE);
337                         else
338                                 ret = scpi_cmd(sdi, SCPI_CMD_SET_OUTPUT_DISABLE);
339                         break;
340                 case SR_CONF_OUTPUT_VOLTAGE_MAX:
341                         d = g_variant_get_double(data);
342                         ret = scpi_cmd(sdi, SCPI_CMD_SET_VOLTAGE_MAX, d);
343                         break;
344                 case SR_CONF_OUTPUT_CURRENT_MAX:
345                         d = g_variant_get_double(data);
346                         ret = scpi_cmd(sdi, SCPI_CMD_SET_CURRENT_MAX, d);
347                         break;
348                 case SR_CONF_OVER_TEMPERATURE_PROTECTION:
349                         if (g_variant_get_boolean(data))
350                                 ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_ENABLE);
351                         else
352                                 ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_DISABLE);
353                         break;
354                 default:
355                         ret = SR_ERR_NA;
356                 }
357         } else {
358                 /* Channel group specified. */
359                 ch = cg->channels->data;
360                 pch = ch->priv;
361                 switch (key) {
362                 case SR_CONF_OUTPUT_ENABLED:
363                         if (g_variant_get_boolean(data))
364                                 ret = scpi_cmd(sdi, SCPI_CMD_SET_OUTPUT_ENABLE, pch->hwname);
365                         else
366                                 ret = scpi_cmd(sdi, SCPI_CMD_SET_OUTPUT_DISABLE, pch->hwname);
367                         break;
368                 case SR_CONF_OUTPUT_VOLTAGE_MAX:
369                         d = g_variant_get_double(data);
370                         ret = scpi_cmd(sdi, SCPI_CMD_SET_VOLTAGE_MAX, pch->hwname, d);
371                         break;
372                 case SR_CONF_OUTPUT_CURRENT_MAX:
373                         d = g_variant_get_double(data);
374                         ret = scpi_cmd(sdi, SCPI_CMD_SET_CURRENT_MAX, pch->hwname, d);
375                         break;
376                 case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
377                         if (g_variant_get_boolean(data))
378                                 ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_ENABLE,
379                                                 pch->hwname);
380                         else
381                                 ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_DISABLE,
382                                                 pch->hwname);
383                         break;
384                 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
385                         d = g_variant_get_double(data);
386                         ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_THRESHOLD,
387                                         pch->hwname, d);
388                         break;
389                 case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
390                         if (g_variant_get_boolean(data))
391                                 ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_CURRENT_PROTECTION_ENABLE,
392                                                 pch->hwname);
393                         else
394                                 ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_CURRENT_PROTECTION_DISABLE,
395                                                 pch->hwname);
396                         break;
397                 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
398                         d = g_variant_get_double(data);
399                         ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_CURRENT_PROTECTION_THRESHOLD,
400                                         pch->hwname, d);
401                         break;
402                 default:
403                         ret = SR_ERR_NA;
404                 }
405         }
406
407         return ret;
408 }
409
410 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
411                 const struct sr_channel_group *cg)
412 {
413         struct dev_context *devc;
414         struct sr_channel *ch;
415         struct channel_spec *ch_spec;
416         GVariant *gvar;
417         GVariantBuilder gvb;
418         int ret, i;
419         const char *s[16];
420
421         /* Always available, even without sdi. */
422         if (key == SR_CONF_SCAN_OPTIONS) {
423                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
424                                 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
425                 return SR_OK;
426         }
427
428         if (!sdi)
429                 return SR_ERR_ARG;
430         devc = sdi->priv;
431
432         ret = SR_OK;
433         if (!cg) {
434                 /* No channel group: global options. */
435                 switch (key) {
436                 case SR_CONF_DEVICE_OPTIONS:
437                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
438                                         devc->device->devopts, devc->device->num_devopts,
439                                         sizeof(uint32_t));
440                         break;
441                 case SR_CONF_OUTPUT_CHANNEL_CONFIG:
442                         /* Not used. */
443                         i = 0;
444                         if (devc->device->features & PPS_INDEPENDENT)
445                                 s[i++] = "Independent";
446                         if (devc->device->features & PPS_SERIES)
447                                 s[i++] = "Series";
448                         if (devc->device->features & PPS_PARALLEL)
449                                 s[i++] = "Parallel";
450                         if (i == 0) {
451                                 /*
452                                  * Shouldn't happen: independent-only devices
453                                  * shouldn't advertise this option at all.
454                                  */
455                                 return SR_ERR_NA;
456                         }
457                         *data = g_variant_new_strv(s, i);
458                         break;
459                 default:
460                         return SR_ERR_NA;
461                 }
462         } else {
463                 /* Channel group specified. */
464                 /*
465                  * Per-channel-group options depending on a channel are actually
466                  * done with the first channel. Channel groups in PPS can have
467                  * more than one channel, but they will typically be of equal
468                  * specification for use in series or parallel mode.
469                  */
470                 ch = cg->channels->data;
471
472                 switch (key) {
473                 case SR_CONF_DEVICE_OPTIONS:
474                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
475                                         devc->device->devopts_cg, devc->device->num_devopts_cg,
476                                         sizeof(uint32_t));
477                         break;
478                 case SR_CONF_OUTPUT_VOLTAGE_MAX:
479                         ch_spec = &(devc->device->channels[ch->index]);
480                         g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
481                         /* Min, max, write resolution. */
482                         for (i = 0; i < 3; i++) {
483                                 gvar = g_variant_new_double(ch_spec->voltage[i]);
484                                 g_variant_builder_add_value(&gvb, gvar);
485                         }
486                         *data = g_variant_builder_end(&gvb);
487                         break;
488                 case SR_CONF_OUTPUT_CURRENT_MAX:
489                         g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
490                         /* Min, max, step. */
491                         for (i = 0; i < 3; i++) {
492                                 ch_spec = &(devc->device->channels[ch->index]);
493                                 gvar = g_variant_new_double(ch_spec->current[i]);
494                                 g_variant_builder_add_value(&gvb, gvar);
495                         }
496                         *data = g_variant_builder_end(&gvb);
497                         break;
498                 default:
499                         return SR_ERR_NA;
500                 }
501         }
502
503         return ret;
504 }
505
506 static int dev_acquisition_start(const struct sr_dev_inst *sdi,
507                 void *cb_data)
508 {
509         struct dev_context *devc;
510         struct sr_scpi_dev_inst *scpi;
511         struct sr_channel *ch;
512         struct pps_channel *pch;
513         int cmd, ret;
514
515         if (sdi->status != SR_ST_ACTIVE)
516                 return SR_ERR_DEV_CLOSED;
517
518         devc = sdi->priv;
519         scpi = sdi->conn;
520         devc->cb_data = cb_data;
521
522         if ((ret = sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 10,
523                         scpi_pps_receive_data, (void *)sdi)) != SR_OK)
524                 return ret;
525         std_session_send_df_header(sdi, LOG_PREFIX);
526
527         /* Prime the pipe with the first channel's fetch. */
528         ch = sdi->channels->data;
529         pch = ch->priv;
530         devc->cur_channel = ch;
531         if (pch->mq == SR_MQ_VOLTAGE)
532                 cmd = SCPI_CMD_GET_MEAS_VOLTAGE;
533         else if (pch->mq == SR_MQ_CURRENT)
534                 cmd = SCPI_CMD_GET_MEAS_CURRENT;
535         else if (pch->mq == SR_MQ_POWER)
536                 cmd = SCPI_CMD_GET_MEAS_POWER;
537         else
538                 return SR_ERR;
539         scpi_cmd(sdi, cmd, pch->hwname);
540
541         return SR_OK;
542 }
543
544 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
545 {
546         struct sr_scpi_dev_inst *scpi;
547         float f;
548
549         (void)cb_data;
550
551         if (sdi->status != SR_ST_ACTIVE)
552                 return SR_ERR_DEV_CLOSED;
553
554         scpi = sdi->conn;
555
556         /*
557          * A requested value is certainly on the way. Retrieve it now,
558          * to avoid leaving the device in a state where it's not expecting
559          * commands.
560          */
561         sr_scpi_get_float(scpi, NULL, &f);
562         sr_scpi_source_remove(sdi->session, scpi);
563
564         return SR_OK;
565 }
566
567 SR_PRIV struct sr_dev_driver scpi_pps_driver_info = {
568         .name = "scpi-pps",
569         .longname = "SCPI PPS",
570         .api_version = 1,
571         .init = init,
572         .cleanup = cleanup,
573         .scan = scan,
574         .dev_list = dev_list,
575         .dev_clear = dev_clear,
576         .config_get = config_get,
577         .config_set = config_set,
578         .config_list = config_list,
579         .dev_open = dev_open,
580         .dev_close = dev_close,
581         .dev_acquisition_start = dev_acquisition_start,
582         .dev_acquisition_stop = dev_acquisition_stop,
583         .priv = NULL,
584 };