]> sigrok.org Git - libsigrok.git/blame - src/hardware/gwinstek-gpd/protocol.c
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / src / hardware / gwinstek-gpd / protocol.c
CommitLineData
b872ab7d
BS
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
24SR_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
49SR_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;
13726d30 54 const int timeout_ms = 250;
b872ab7d 55
3e33089b 56 if (!serial || !buf || (buflen <= 0))
b872ab7d
BS
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
13726d30 72 if (buf[bufpos] == '\n' || buf[bufpos] == '\r') {
b872ab7d
BS
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
95SR_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) {
b872ab7d
BS
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++) {
380d3b2a
UH
129 packet.type = SR_DF_ANALOG;
130 packet.payload = &analog;
131
b872ab7d
BS
132 reply[0] = '\0';
133 gpd_receive_reply(serial, reply, sizeof(reply));
1145ceaa
RA
134 if (sscanf(reply, "%f", &devc->config[i].output_current_last) != 1) {
135 sr_err("Invalid reply to IOUT1?: '%s'.",
b872ab7d
BS
136 reply);
137 return TRUE;
138 }
139
b872ab7d 140 /* Send the value forward. */
380d3b2a 141 sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
b872ab7d
BS
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;
1145ceaa 151 analog.data = &devc->config[i].output_current_last;
b872ab7d
BS
152 sr_session_send(sdi, &packet);
153
154 reply[0] = '\0';
155 gpd_receive_reply(serial, reply, sizeof(reply));
1145ceaa 156 if (sscanf(reply, "%f", &devc->config[i].output_voltage_last) != 1) {
b872ab7d
BS
157 sr_err("Invalid reply to VOUT1?: '%s'.",
158 reply);
159 return TRUE;
160 }
161
b872ab7d 162 /* Send the value forward. */
380d3b2a 163 sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
b872ab7d
BS
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;
1145ceaa 173 analog.data = &devc->config[i].output_voltage_last;
b872ab7d
BS
174 sr_session_send(sdi, &packet);
175 }
176
177 devc->reply_pending = FALSE;
b81cfbc3 178 sr_sw_limits_update_samples_read(&devc->limits, 1);
b872ab7d
BS
179 }
180 } else {
b872ab7d
BS
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}