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