]> sigrok.org Git - libsigrok.git/blob - src/hardware/scpi-pps/protocol.c
86ec387693a4d3b58072643a752dd6b60526116d
[libsigrok.git] / src / hardware / scpi-pps / protocol.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 <stdarg.h>
22 #include "protocol.h"
23
24 SR_PRIV char *scpi_cmd_get(const struct sr_dev_inst *sdi, int command)
25 {
26         struct dev_context *devc;
27         unsigned int i;
28         char *cmd;
29
30         devc = sdi->priv;
31         cmd = NULL;
32         for (i = 0; i < devc->device->num_commands; i++) {
33                 if (devc->device->commands[i].command == command) {
34                         cmd = devc->device->commands[i].string;
35                         break;
36                 }
37         }
38
39         return cmd;
40 }
41
42 SR_PRIV int scpi_cmd(const struct sr_dev_inst *sdi, int command, ...)
43 {
44         struct sr_scpi_dev_inst *scpi;
45         va_list args;
46         int ret;
47         char *cmd;
48
49         if (!(cmd = scpi_cmd_get(sdi, command))) {
50                 /* Device does not implement this command, that's OK. */
51                 return SR_OK_CONTINUE;
52         }
53
54         scpi = sdi->conn;
55         va_start(args, command);
56         ret = sr_scpi_send_variadic(scpi, cmd, args);
57         va_end(args);
58
59         return ret;
60 }
61
62 SR_PRIV int scpi_cmd_resp(const struct sr_dev_inst *sdi, GVariant **gvar,
63                 const GVariantType *gvtype, int command, ...)
64 {
65         struct sr_scpi_dev_inst *scpi;
66         va_list args;
67         double d;
68         int ret;
69         char *cmd, *s;
70
71         if (!(cmd = scpi_cmd_get(sdi, command))) {
72                 /* Device does not implement this command, that's OK. */
73                 return SR_OK_CONTINUE;
74         }
75
76         scpi = sdi->conn;
77         va_start(args, command);
78         ret = sr_scpi_send_variadic(scpi, cmd, args);
79         va_end(args);
80         if (ret != SR_OK)
81                 return ret;
82
83         /* Non-standard data type responses. */
84         if (command == SCPI_CMD_GET_OUTPUT_REGULATION) {
85                 /*
86                  * The Rigol DP800 series return CV/CC/UR, Philips PM2800
87                  * return VOLT/CURR. We always return a GVariant string in
88                  * the Rigol notation.
89                  */
90                 if ((ret = sr_scpi_get_string(scpi, NULL, &s)) != SR_OK)
91                         return ret;
92                 if (!strcmp(s, "CV") || !strcmp(s, "VOLT")) {
93                         *gvar = g_variant_new_string("CV");
94                 } else if (!strcmp(s, "CC") || !strcmp(s, "CURR")) {
95                         *gvar = g_variant_new_string("CC");
96                 } else if (!strcmp(s, "UR")) {
97                         *gvar = g_variant_new_string("UR");
98                 } else {
99                         sr_dbg("Unknown response to SCPI_CMD_GET_OUTPUT_REGULATION: %s", s);
100                         ret = SR_ERR_DATA;
101                 }
102                 g_free(s);
103         } else {
104                 /* Straight SCPI getters to GVariant types. */
105                 if (g_variant_type_equal(gvtype, G_VARIANT_TYPE_BOOLEAN)) {
106                         if ((ret = sr_scpi_get_string(scpi, NULL, &s)) != SR_OK)
107                                 return ret;
108                         if (!strcasecmp(s, "ON") || !strcasecmp(s, "1") || !strcasecmp(s, "YES"))
109                                 *gvar = g_variant_new_boolean(TRUE);
110                         else if (!strcasecmp(s, "OFF") || !strcasecmp(s, "0") || !strcasecmp(s, "NO"))
111                                 *gvar = g_variant_new_boolean(FALSE);
112                         else
113                                 ret = SR_ERR;
114                         g_free(s);
115                 } if (g_variant_type_equal(gvtype, G_VARIANT_TYPE_DOUBLE)) {
116                         if ((ret = sr_scpi_get_double(scpi, NULL, &d)) == SR_OK)
117                                 *gvar = g_variant_new_double(d);
118                 } if (g_variant_type_equal(gvtype, G_VARIANT_TYPE_STRING)) {
119                         if ((ret = sr_scpi_get_string(scpi, NULL, &s)) == SR_OK)
120                                 *gvar = g_variant_new_string(s);
121                 }
122         }
123
124         return ret;
125 }
126
127 SR_PRIV int select_channel(const struct sr_dev_inst *sdi, struct sr_channel *ch)
128 {
129         struct dev_context *devc;
130         struct pps_channel *cur_pch, *new_pch;
131         int ret;
132
133         if (g_slist_length(sdi->channels) == 1)
134                 return SR_OK;
135
136         devc = sdi->priv;
137         if (ch == devc->cur_channel)
138                 return SR_OK;
139
140         new_pch = ch->priv;
141         if (devc->cur_channel) {
142                 cur_pch = devc->cur_channel->priv;
143                 if (cur_pch->hw_output_idx == new_pch->hw_output_idx) {
144                         /* Same underlying output channel. */
145                         devc->cur_channel = ch;
146                         return SR_OK;
147                 }
148         }
149
150         if ((ret = scpi_cmd(sdi, SCPI_CMD_SELECT_CHANNEL, new_pch->hwname)) == SR_OK)
151                 devc->cur_channel = ch;
152
153         return ret;
154 }
155
156 SR_PRIV struct sr_channel *next_enabled_channel(const struct sr_dev_inst *sdi,
157                 struct sr_channel *cur_channel)
158 {
159         struct sr_channel *next_channel;
160         GSList *l;
161
162         next_channel = cur_channel;
163         do {
164                 l = g_slist_find(sdi->channels, next_channel);
165                 if (l && l->next)
166                         next_channel = l->next->data;
167                 else
168                         next_channel = sdi->channels->data;
169         } while (!next_channel->enabled);
170
171         return next_channel;
172 }
173
174 SR_PRIV int scpi_pps_receive_data(int fd, int revents, void *cb_data)
175 {
176         struct dev_context *devc;
177         struct sr_datafeed_packet packet;
178         struct sr_datafeed_analog analog;
179         const struct sr_dev_inst *sdi;
180         struct sr_channel *next_channel;
181         struct sr_scpi_dev_inst *scpi;
182         struct pps_channel *pch;
183         float f;
184         int cmd;
185
186         (void)fd;
187         (void)revents;
188
189         if (!(sdi = cb_data))
190                 return TRUE;
191
192         if (!(devc = sdi->priv))
193                 return TRUE;
194
195         scpi = sdi->conn;
196
197         /* Retrieve requested value for this state. */
198         if (sr_scpi_get_float(scpi, NULL, &f) == SR_OK) {
199                 pch = devc->cur_channel->priv;
200                 packet.type = SR_DF_ANALOG;
201                 packet.payload = &analog;
202                 analog.channels = g_slist_append(NULL, devc->cur_channel);
203                 analog.num_samples = 1;
204                 analog.mq = pch->mq;
205                 if (pch->mq == SR_MQ_VOLTAGE)
206                         analog.unit = SR_UNIT_VOLT;
207                 else if (pch->mq == SR_MQ_CURRENT)
208                         analog.unit = SR_UNIT_AMPERE;
209                 else if (pch->mq == SR_MQ_POWER)
210                         analog.unit = SR_UNIT_WATT;
211                 analog.mqflags = SR_MQFLAG_DC;
212                 analog.data = &f;
213                 sr_session_send(sdi, &packet);
214                 g_slist_free(analog.channels);
215         }
216
217         if (g_slist_length(sdi->channels) > 1) {
218                 next_channel = next_enabled_channel(sdi, devc->cur_channel);
219                 if (select_channel(sdi, next_channel) != SR_OK) {
220                         sr_err("Failed to select channel %s", next_channel->name);
221                         return FALSE;
222                 }
223         }
224
225         pch = devc->cur_channel->priv;
226         if (pch->mq == SR_MQ_VOLTAGE)
227                 cmd = SCPI_CMD_GET_MEAS_VOLTAGE;
228         else if (pch->mq == SR_MQ_CURRENT)
229                 cmd = SCPI_CMD_GET_MEAS_CURRENT;
230         else if (pch->mq == SR_MQ_POWER)
231                 cmd = SCPI_CMD_GET_MEAS_POWER;
232         else
233                 return SR_ERR;
234         scpi_cmd(sdi, cmd);
235
236         return TRUE;
237 }