]> sigrok.org Git - libsigrok.git/blob - src/hardware/gwinstek-gpd/protocol.c
e69995902cfe82a4ee95c50800d456dfadf1f065
[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 = 250;
55
56         if (!serial || !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' || buf[bufpos] == '\r') {
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                 if (!devc->reply_pending) {
122                         sr_err("No reply pending.");
123                         gpd_receive_reply(serial, reply, sizeof(reply));
124                         reply_esc = g_strescape(reply, NULL);
125                         sr_err("Unexpected data '%s'.", reply_esc);
126                         g_free(reply_esc);
127                 } else {
128                         for (i = 0; i < devc->model->num_channels; i++) {
129                                 packet.type = SR_DF_ANALOG;
130                                 packet.payload = &analog;
131
132                                 reply[0] = '\0';
133                                 gpd_receive_reply(serial, reply, sizeof(reply));
134                                 if (sscanf(reply, "%f", &devc->config[i].output_voltage_max) != 1) {
135                                         sr_err("Invalid reply to VOUT1?: '%s'.",
136                                                 reply);
137                                         return TRUE;
138                                 }
139
140                                 /* Send the value forward. */
141                                 sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
142                                 analog.num_samples = 1;
143                                 ch = g_slist_nth_data(sdi->channels, i);
144                                 analog.meaning->channels =
145                                         g_slist_append(NULL, ch);
146                                 analog.meaning->mq = SR_MQ_CURRENT;
147                                 analog.meaning->unit = SR_UNIT_AMPERE;
148                                 analog.meaning->mqflags = 0;
149                                 analog.encoding->digits = 3;
150                                 analog.spec->spec_digits = 3;
151                                 analog.data = &devc->config[i].output_current_max;
152                                 sr_session_send(sdi, &packet);
153
154                                 reply[0] = '\0';
155                                 gpd_receive_reply(serial, reply, sizeof(reply));
156                                 if (sscanf(reply, "%f", &devc->config[i].output_voltage_max) != 1) {
157                                         sr_err("Invalid reply to VOUT1?: '%s'.",
158                                                 reply);
159                                         return TRUE;
160                                 }
161
162                                 /* Send the value forward. */
163                                 sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
164                                 analog.num_samples = 1;
165                                 ch = g_slist_nth_data(sdi->channels, i);
166                                 analog.meaning->channels =
167                                         g_slist_append(NULL, ch);
168                                 analog.meaning->mq = SR_MQ_VOLTAGE;
169                                 analog.meaning->unit = SR_UNIT_VOLT;
170                                 analog.meaning->mqflags = SR_MQFLAG_DC;
171                                 analog.encoding->digits = 3;
172                                 analog.spec->spec_digits = 3;
173                                 analog.data = &devc->config[i].output_voltage_max;
174                                 sr_session_send(sdi, &packet);
175                         }
176
177                         devc->reply_pending = FALSE;
178                         sr_sw_limits_update_samples_read(&devc->limits, 1);
179                 }
180         } else {
181                 if (!devc->reply_pending) {
182                         for (i = 0; i < devc->model->num_channels; i++)
183                                 gpd_send_cmd(serial, "IOUT%d?\nVOUT%d?\n",
184                                         i + 1, i + 1);
185                         devc->req_sent_at = g_get_monotonic_time();
186                         devc->reply_pending = TRUE;
187                 }
188         }
189
190         if (sr_sw_limits_check(&devc->limits)) {
191                 sr_dev_acquisition_stop(sdi);
192                 return TRUE;
193         }
194
195         return TRUE;
196 }