]> sigrok.org Git - libsigrok.git/blob - src/hardware/gwinstek-gpd/protocol.c
gwinstek-gpd: Initial implementation.
[libsigrok.git] / src / hardware / gwinstek-gpd / protocol.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2018 Bastian Schmitz <bastian.schmitz@udo.edu>
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 <config.h>
21 #include <string.h>
22 #include "protocol.h"
23
24 SR_PRIV int gpd_send_cmd(struct sr_serial_dev_inst *serial, const char *cmd, ...)
25 {
26         int ret;
27         char cmdbuf[50];
28         char *cmd_esc;
29         va_list args;
30
31         va_start(args, cmd);
32         vsnprintf(cmdbuf, sizeof(cmdbuf), cmd, args);
33         va_end(args);
34
35         cmd_esc = g_strescape(cmdbuf, NULL);
36         sr_dbg("Sending '%s'.", cmd_esc);
37         g_free(cmd_esc);
38
39         ret = serial_write_blocking(serial, cmdbuf, strlen(cmdbuf),
40                                     serial_timeout(serial, strlen(cmdbuf)));
41         if (ret < 0) {
42                 sr_err("Error sending command: %d.", ret);
43                 return ret;
44         }
45
46         return ret;
47 }
48
49 SR_PRIV int gpd_receive_reply(struct sr_serial_dev_inst *serial, char *buf,
50                                 int buflen)
51 {
52         int l_recv = 0, bufpos = 0, retc, l_startpos = 0, lines = 1;
53         gint64 start, remaining;
54         const int timeout_ms = 100;
55
56         if (!serial || (lines <= 0) || !buf || (buflen <= 0))
57                 return SR_ERR_ARG;
58
59         start = g_get_monotonic_time();
60         remaining = timeout_ms;
61
62         while ((l_recv < lines) && (bufpos < (buflen + 1))) {
63                 retc = serial_read_blocking(serial, &buf[bufpos], 1, remaining);
64                 if (retc != 1)
65                         return SR_ERR;
66
67                 if (bufpos == 0 && buf[bufpos] == '\r')
68                         continue;
69                 if (bufpos == 0 && buf[bufpos] == '\n')
70                         continue;
71
72                 if (buf[bufpos] == '\n') {
73                         buf[bufpos] = '\0';
74                         sr_dbg("Received line '%s'.", &buf[l_startpos]);
75                         buf[bufpos] = '\n';
76                         l_startpos = bufpos + 1;
77                         l_recv++;
78                 }
79                 bufpos++;
80
81                 /* Reduce timeout by time elapsed. */
82                 remaining = timeout_ms - ((g_get_monotonic_time() - start) / 1000);
83                 if (remaining <= 0)
84                         return SR_ERR; /* Timeout. */
85         }
86
87         buf[bufpos] = '\0';
88
89         if (l_recv == lines)
90                 return SR_OK;
91         else
92                 return SR_ERR;
93 }
94
95 SR_PRIV int gpd_receive_data(int fd, int revents, void *cb_data)
96 {
97         struct sr_dev_inst *sdi;
98         struct dev_context *devc;
99         struct sr_serial_dev_inst *serial;
100         struct sr_datafeed_packet packet;
101         struct sr_datafeed_analog analog;
102         struct sr_analog_encoding encoding;
103         struct sr_analog_meaning meaning;
104         struct sr_analog_spec spec;
105         struct sr_channel *ch;
106         unsigned int i;
107         char reply[50];
108         char *reply_esc;
109
110         (void)fd;
111
112         if (!(sdi = cb_data))
113                 return TRUE;
114
115         if (!(devc = sdi->priv))
116                 return TRUE;
117
118         serial = sdi->conn;
119
120         if (revents == G_IO_IN) {
121                 sr_dbg("%s(G_IO_IN)", __func__);
122                 if (!devc->reply_pending) {
123                         sr_err("No reply pending.");
124                         gpd_receive_reply(serial, reply, sizeof(reply));
125                         reply_esc = g_strescape(reply, NULL);
126                         sr_err("Unexpected data '%s'.", reply_esc);
127                         g_free(reply_esc);
128                 } else {
129                         for (i = 0; i < devc->model->num_channels; i++) {
130                                 reply[0] = '\0';
131                                 gpd_receive_reply(serial, reply, sizeof(reply));
132                                 if (sscanf(reply, "%f", &devc->config[i].output_voltage_max) != 1) {
133                                         sr_err("Invalid reply to VOUT1?: '%s'.",
134                                                 reply);
135                                         return TRUE;
136                                 }
137
138                                 sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
139
140                                 /* Send the value forward. */
141                                 packet.type = SR_DF_ANALOG;
142                                 packet.payload = &analog;
143
144                                 analog.num_samples = 1;
145                                 ch = g_slist_nth_data(sdi->channels, i);
146                                 analog.meaning->channels =
147                                         g_slist_append(NULL, ch);
148                                 analog.meaning->mq = SR_MQ_CURRENT;
149                                 analog.meaning->unit = SR_UNIT_AMPERE;
150                                 analog.meaning->mqflags = 0;
151                                 analog.encoding->digits = 3;
152                                 analog.spec->spec_digits = 3;
153                                 analog.data = &devc->config[i].output_current_max;
154                                 sr_session_send(sdi, &packet);
155
156                                 reply[0] = '\0';
157                                 gpd_receive_reply(serial, reply, sizeof(reply));
158                                 if (sscanf(reply, "%f", &devc->config[i].output_voltage_max) != 1) {
159                                         sr_err("Invalid reply to VOUT1?: '%s'.",
160                                                 reply);
161                                         return TRUE;
162                                 }
163
164                                 sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
165
166                                 /* Send the value forward. */
167                                 packet.type = SR_DF_ANALOG;
168                                 packet.payload = &analog;
169
170                                 analog.num_samples = 1;
171                                 ch = g_slist_nth_data(sdi->channels, i);
172                                 analog.meaning->channels =
173                                         g_slist_append(NULL, ch);
174                                 analog.meaning->mq = SR_MQ_VOLTAGE;
175                                 analog.meaning->unit = SR_UNIT_VOLT;
176                                 analog.meaning->mqflags = SR_MQFLAG_DC;
177                                 analog.encoding->digits = 3;
178                                 analog.spec->spec_digits = 3;
179                                 analog.data = &devc->config[i].output_voltage_max;
180                                 sr_session_send(sdi, &packet);
181                         }
182
183                         devc->reply_pending = FALSE;
184                 }
185         } else {
186                 sr_dbg("%s(TIMEOUT)", __func__);
187                 if (!devc->reply_pending) {
188                         for (i = 0; i < devc->model->num_channels; i++)
189                                 gpd_send_cmd(serial, "IOUT%d?\nVOUT%d?\n",
190                                         i + 1, i + 1);
191                         devc->req_sent_at = g_get_monotonic_time();
192                         devc->reply_pending = TRUE;
193                 }
194         }
195
196         if (sr_sw_limits_check(&devc->limits)) {
197                 sr_dev_acquisition_stop(sdi);
198                 return TRUE;
199         }
200
201         return TRUE;
202 }