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