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