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