]> sigrok.org Git - libsigrok.git/blame - src/hardware/arachnid-labs-re-load-pro/protocol.c
arachnid-labs-re-load-pro: Change serial read in acquisition mode.
[libsigrok.git] / src / hardware / arachnid-labs-re-load-pro / protocol.c
CommitLineData
6e8d31d4
UH
1/*
2 * This file is part of the libsigrok project.
3 *
803db07a 4 * Copyright (C) 2015-2016 Uwe Hermann <uwe@hermann-uwe.de>
6e8d31d4
UH
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 2 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
2ea1fdf1 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
6e8d31d4
UH
18 */
19
20#include <config.h>
803db07a 21#include <string.h>
6e8d31d4
UH
22#include "protocol.h"
23
803db07a
UH
24#define READ_TIMEOUT_MS 1000
25
26static int send_cmd(const struct sr_dev_inst *sdi, const char *cmd,
27 char *replybuf, int replybufsize)
28{
29 char *bufptr;
30 int len, ret;
a2172899 31 struct dev_context *devc;
803db07a
UH
32 struct sr_serial_dev_inst *serial;
33
a2172899 34 devc = sdi->priv;
803db07a
UH
35 serial = sdi->conn;
36
37 /* Send the command (blocking, with timeout). */
38 if ((ret = serial_write_blocking(serial, cmd,
39 strlen(cmd), serial_timeout(serial,
40 strlen(cmd)))) < (int)strlen(cmd)) {
41 sr_err("Unable to send command.");
42 return SR_ERR;
43 }
44
a2172899
FS
45 if (!devc->acquisition_running) {
46 /* Read the reply (blocking, with timeout). */
47 memset(replybuf, 0, replybufsize);
48 bufptr = replybuf;
803db07a
UH
49 len = replybufsize;
50 ret = serial_readline(serial, &bufptr, &len, READ_TIMEOUT_MS);
803db07a 51
a2172899
FS
52 /* If we got 0 characters (possibly one \r or \n), retry once. */
53 if (len == 0) {
54 len = replybufsize;
55 ret = serial_readline(serial, &bufptr, &len, READ_TIMEOUT_MS);
56 }
57
58 if (g_str_has_prefix((const char *)&bufptr, "err ")) {
59 sr_err("Device replied with an error: '%s'.", bufptr);
60 return SR_ERR;
61 }
803db07a
UH
62 }
63
64 return ret;
65}
66
67SR_PRIV int reloadpro_set_current_limit(const struct sr_dev_inst *sdi,
68 float current)
69{
70 int ret, ma;
71 char buf[100];
72 char *cmd;
73
74 if (current < 0 || current > 6) {
75 sr_err("The current limit must be 0-6 A (was %f A).", current);
76 return SR_ERR_ARG;
77 }
78
79 /* Hardware expects current in mA, integer (0..6000). */
80 ma = (int)(current * 1000);
81
82 sr_err("Setting current limit to %f A (%d mA).", current, ma);
83
84 cmd = g_strdup_printf("set %d\n", ma);
85 if ((ret = send_cmd(sdi, cmd, (char *)&buf, sizeof(buf))) < 0) {
86 sr_err("Error sending current limit command: %d.", ret);
87 g_free(cmd);
88 return SR_ERR;
89 }
90 g_free(cmd);
91
92 return SR_OK;
93}
94
8501448c
UH
95SR_PRIV int reloadpro_set_on_off(const struct sr_dev_inst *sdi, gboolean on)
96{
97 int ret;
98 char buf[100];
99 const char *cmd;
100
101 cmd = (on) ? "on\n" : "off\n";
102 if ((ret = send_cmd(sdi, cmd, (char *)&buf, sizeof(buf))) < 0) {
103 sr_err("Error sending on/off command: %d.", ret);
104 return SR_ERR;
105 }
106
107 return SR_OK;
108}
109
803db07a
UH
110SR_PRIV int reloadpro_get_current_limit(const struct sr_dev_inst *sdi,
111 float *current)
112{
113 int ret;
114 char buf[100];
a2172899
FS
115 struct dev_context *devc;
116
117 devc = sdi->priv;
803db07a
UH
118
119 if ((ret = send_cmd(sdi, "set\n", (char *)&buf, sizeof(buf))) < 0) {
120 sr_err("Error sending current limit query: %d.", ret);
121 return SR_ERR;
122 }
123
a2172899
FS
124 if (!devc->acquisition_running) {
125 /* Hardware sends current in mA, integer (0..6000). */
126 *current = g_ascii_strtod(buf + 4, NULL) / 1000;
127 }
803db07a
UH
128
129 return SR_OK;
130}
131
132SR_PRIV int reloadpro_get_voltage_current(const struct sr_dev_inst *sdi,
133 float *voltage, float *current)
134{
135 int ret;
136 char buf[100];
137 char **tokens;
a2172899
FS
138 struct dev_context *devc;
139
140 devc = sdi->priv;
803db07a
UH
141
142 if ((ret = send_cmd(sdi, "read\n", (char *)&buf, sizeof(buf))) < 0) {
143 sr_err("Error sending voltage/current query: %d.", ret);
144 return SR_ERR;
145 }
146
a2172899
FS
147 if (!devc->acquisition_running) {
148 /* Reply: "read <current> <voltage>". */
149 tokens = g_strsplit((const char *)&buf, " ", 3);
150 if (voltage)
151 *voltage = g_ascii_strtod(tokens[2], NULL) / 1000;
152 if (current)
153 *current = g_ascii_strtod(tokens[1], NULL) / 1000;
154 g_strfreev(tokens);
155 }
803db07a
UH
156
157 return SR_OK;
158}
159
2472271e
FS
160static int send_config_update_key(const struct sr_dev_inst *sdi,
161 uint32_t key, GVariant *var)
162{
163 struct sr_config *cfg;
164 struct sr_datafeed_packet packet;
165 struct sr_datafeed_meta meta;
166 int ret;
167
168 cfg = sr_config_new(key, var);
169 if (!cfg)
170 return SR_ERR;
171
172 memset(&meta, 0, sizeof(meta));
173
174 packet.type = SR_DF_META;
175 packet.payload = &meta;
176
177 meta.config = g_slist_append(meta.config, cfg);
178
179 ret = sr_session_send(sdi, &packet);
180 sr_config_free(cfg);
181
182 return ret;
183}
184
803db07a 185static void handle_packet(const struct sr_dev_inst *sdi)
6e8d31d4 186{
803db07a
UH
187 float voltage, current;
188 struct sr_datafeed_packet packet;
6e68da51
UH
189 struct sr_datafeed_analog analog;
190 struct sr_analog_encoding encoding;
191 struct sr_analog_meaning meaning;
192 struct sr_analog_spec spec;
6e8d31d4 193 struct dev_context *devc;
803db07a
UH
194 char **tokens;
195 GSList *l;
196
197 devc = sdi->priv;
198
199 if (g_str_has_prefix((const char *)devc->buf, "overtemp")) {
2217be1d 200 sr_warn("Overtemperature condition!");
803db07a 201 devc->otp_active = TRUE;
2472271e
FS
202 send_config_update_key(sdi, SR_CONF_OVER_TEMPERATURE_PROTECTION_ACTIVE,
203 g_variant_new_boolean(TRUE));
803db07a
UH
204 return;
205 }
206
2217be1d
UH
207 if (g_str_has_prefix((const char *)devc->buf, "undervolt")) {
208 sr_warn("Undervoltage condition!");
209 devc->uvc_active = TRUE;
2472271e
FS
210 send_config_update_key(sdi, SR_CONF_UNDER_VOLTAGE_CONDITION_ACTIVE,
211 g_variant_new_boolean(TRUE));
212 return;
213 }
214
215 if (g_str_has_prefix((const char *)devc->buf, "err ")) {
216 sr_err("Device replied with an error: '%s'.", devc->buf);
217 return;
218 }
219
220 if (g_str_has_prefix((const char *)devc->buf, "set ")) {
221 tokens = g_strsplit((const char *)devc->buf, " ", 2);
222 current = g_ascii_strtod(tokens[1], NULL) / 1000;
223 g_strfreev(tokens);
224 send_config_update_key(sdi, SR_CONF_CURRENT_LIMIT,
225 g_variant_new_double(current));
2217be1d
UH
226 return;
227 }
228
803db07a
UH
229 if (!g_str_has_prefix((const char *)devc->buf, "read ")) {
230 sr_dbg("Unknown packet: '%s'.", devc->buf);
231 return;
232 }
233
234 tokens = g_strsplit((const char *)devc->buf, " ", 3);
235 voltage = g_ascii_strtod(tokens[2], NULL) / 1000;
236 current = g_ascii_strtod(tokens[1], NULL) / 1000;
237 g_strfreev(tokens);
238
803db07a
UH
239 /* Begin frame. */
240 packet.type = SR_DF_FRAME_BEGIN;
6e68da51 241 packet.payload = NULL;
803db07a
UH
242 sr_session_send(sdi, &packet);
243
6e68da51
UH
244 sr_analog_init(&analog, &encoding, &meaning, &spec, 4);
245
246 packet.type = SR_DF_ANALOG;
803db07a
UH
247 packet.payload = &analog;
248 analog.num_samples = 1;
249
250 /* Voltage */
251 l = g_slist_copy(sdi->channels);
252 l = g_slist_remove_link(l, g_slist_nth(l, 1));
6e68da51
UH
253 meaning.channels = l;
254 meaning.mq = SR_MQ_VOLTAGE;
255 meaning.mqflags = SR_MQFLAG_DC;
256 meaning.unit = SR_UNIT_VOLT;
803db07a
UH
257 analog.data = &voltage;
258 sr_session_send(sdi, &packet);
259 g_slist_free(l);
260
261 /* Current */
262 l = g_slist_copy(sdi->channels);
263 l = g_slist_remove_link(l, g_slist_nth(l, 0));
6e68da51
UH
264 meaning.channels = l;
265 meaning.mq = SR_MQ_CURRENT;
266 meaning.mqflags = SR_MQFLAG_DC;
267 meaning.unit = SR_UNIT_AMPERE;
803db07a
UH
268 analog.data = &current;
269 sr_session_send(sdi, &packet);
270 g_slist_free(l);
271
272 /* End frame. */
273 packet.type = SR_DF_FRAME_END;
6e68da51 274 packet.payload = NULL;
803db07a
UH
275 sr_session_send(sdi, &packet);
276
014c7f93 277 sr_sw_limits_update_samples_read(&devc->limits, 1);
803db07a
UH
278}
279
280static void handle_new_data(const struct sr_dev_inst *sdi)
281{
282 int len;
283 struct dev_context *devc;
284 struct sr_serial_dev_inst *serial;
a2172899 285 char *buf;
803db07a
UH
286
287 devc = sdi->priv;
288 serial = sdi->conn;
289
803db07a 290 len = RELOADPRO_BUFSIZE - devc->buflen;
a2172899
FS
291 buf = devc->buf;
292 if (serial_readline(serial, &buf, &len, 250) != SR_OK) {
293 sr_err("Err: ");
294 return;
295 }
296
803db07a
UH
297 if (len == 0)
298 return; /* No new bytes, nothing to do. */
299 if (len < 0) {
300 sr_err("Serial port read error: %d.", len);
301 return;
302 }
303 devc->buflen += len;
304
a2172899
FS
305 handle_packet(sdi);
306 memset(devc->buf, 0, RELOADPRO_BUFSIZE);
307 devc->buflen = 0;
803db07a
UH
308}
309
310SR_PRIV int reloadpro_receive_data(int fd, int revents, void *cb_data)
311{
312 struct sr_dev_inst *sdi;
313 struct dev_context *devc;
6e8d31d4
UH
314
315 (void)fd;
316
803db07a
UH
317 sdi = cb_data;
318 devc = sdi->priv;
319
320 if (revents != G_IO_IN)
6e8d31d4
UH
321 return TRUE;
322
803db07a
UH
323 handle_new_data(sdi);
324
014c7f93 325 if (sr_sw_limits_check(&devc->limits))
d2f7c417 326 sr_dev_acquisition_stop(sdi);
6e8d31d4
UH
327
328 return TRUE;
329}