]>
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 | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
21 | #include <config.h> | |
803db07a | 22 | #include <string.h> |
6e8d31d4 UH |
23 | #include "protocol.h" |
24 | ||
803db07a UH |
25 | #define READ_TIMEOUT_MS 1000 |
26 | ||
27 | static 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; | |
32 | struct sr_serial_dev_inst *serial; | |
33 | ||
34 | serial = sdi->conn; | |
35 | ||
36 | /* Send the command (blocking, with timeout). */ | |
37 | if ((ret = serial_write_blocking(serial, cmd, | |
38 | strlen(cmd), serial_timeout(serial, | |
39 | strlen(cmd)))) < (int)strlen(cmd)) { | |
40 | sr_err("Unable to send command."); | |
41 | return SR_ERR; | |
42 | } | |
43 | ||
44 | /* Read the reply (blocking, with timeout). */ | |
45 | memset(replybuf, 0, replybufsize); | |
46 | bufptr = replybuf; | |
47 | len = replybufsize; | |
48 | ret = serial_readline(serial, &bufptr, &len, READ_TIMEOUT_MS); | |
49 | ||
50 | /* If we got 0 characters (possibly one \r or \n), retry once. */ | |
51 | if (len == 0) { | |
52 | len = replybufsize; | |
53 | ret = serial_readline(serial, &bufptr, &len, READ_TIMEOUT_MS); | |
54 | } | |
55 | ||
56 | if (g_str_has_prefix((const char *)&bufptr, "err ")) { | |
57 | sr_err("Device replied with an error: '%s'.", bufptr); | |
58 | return SR_ERR; | |
59 | } | |
60 | ||
61 | return ret; | |
62 | } | |
63 | ||
64 | SR_PRIV int reloadpro_set_current_limit(const struct sr_dev_inst *sdi, | |
65 | float current) | |
66 | { | |
67 | int ret, ma; | |
68 | char buf[100]; | |
69 | char *cmd; | |
70 | ||
71 | if (current < 0 || current > 6) { | |
72 | sr_err("The current limit must be 0-6 A (was %f A).", current); | |
73 | return SR_ERR_ARG; | |
74 | } | |
75 | ||
76 | /* Hardware expects current in mA, integer (0..6000). */ | |
77 | ma = (int)(current * 1000); | |
78 | ||
79 | sr_err("Setting current limit to %f A (%d mA).", current, ma); | |
80 | ||
81 | cmd = g_strdup_printf("set %d\n", ma); | |
82 | if ((ret = send_cmd(sdi, cmd, (char *)&buf, sizeof(buf))) < 0) { | |
83 | sr_err("Error sending current limit command: %d.", ret); | |
84 | g_free(cmd); | |
85 | return SR_ERR; | |
86 | } | |
87 | g_free(cmd); | |
88 | ||
89 | return SR_OK; | |
90 | } | |
91 | ||
8501448c UH |
92 | SR_PRIV int reloadpro_set_on_off(const struct sr_dev_inst *sdi, gboolean on) |
93 | { | |
94 | int ret; | |
95 | char buf[100]; | |
96 | const char *cmd; | |
97 | ||
98 | cmd = (on) ? "on\n" : "off\n"; | |
99 | if ((ret = send_cmd(sdi, cmd, (char *)&buf, sizeof(buf))) < 0) { | |
100 | sr_err("Error sending on/off command: %d.", ret); | |
101 | return SR_ERR; | |
102 | } | |
103 | ||
104 | return SR_OK; | |
105 | } | |
106 | ||
803db07a UH |
107 | SR_PRIV int reloadpro_get_current_limit(const struct sr_dev_inst *sdi, |
108 | float *current) | |
109 | { | |
110 | int ret; | |
111 | char buf[100]; | |
112 | ||
113 | if ((ret = send_cmd(sdi, "set\n", (char *)&buf, sizeof(buf))) < 0) { | |
114 | sr_err("Error sending current limit query: %d.", ret); | |
115 | return SR_ERR; | |
116 | } | |
117 | ||
118 | /* Hardware sends current in mA, integer (0..6000). */ | |
119 | *current = g_ascii_strtod(buf + 4, NULL) / 1000; | |
120 | ||
121 | return SR_OK; | |
122 | } | |
123 | ||
124 | SR_PRIV int reloadpro_get_voltage_current(const struct sr_dev_inst *sdi, | |
125 | float *voltage, float *current) | |
126 | { | |
127 | int ret; | |
128 | char buf[100]; | |
129 | char **tokens; | |
130 | ||
131 | if ((ret = send_cmd(sdi, "read\n", (char *)&buf, sizeof(buf))) < 0) { | |
132 | sr_err("Error sending voltage/current query: %d.", ret); | |
133 | return SR_ERR; | |
134 | } | |
135 | ||
136 | /* Reply: "read <current> <voltage>". */ | |
137 | tokens = g_strsplit((const char *)&buf, " ", 3); | |
138 | if (voltage) | |
139 | *voltage = g_ascii_strtod(tokens[2], NULL) / 1000; | |
140 | if (current) | |
141 | *current = g_ascii_strtod(tokens[1], NULL) / 1000; | |
142 | g_strfreev(tokens); | |
143 | ||
144 | return SR_OK; | |
145 | } | |
146 | ||
147 | static void handle_packet(const struct sr_dev_inst *sdi) | |
6e8d31d4 | 148 | { |
803db07a UH |
149 | float voltage, current; |
150 | struct sr_datafeed_packet packet; | |
6e68da51 UH |
151 | struct sr_datafeed_analog analog; |
152 | struct sr_analog_encoding encoding; | |
153 | struct sr_analog_meaning meaning; | |
154 | struct sr_analog_spec spec; | |
6e8d31d4 | 155 | struct dev_context *devc; |
803db07a UH |
156 | char **tokens; |
157 | GSList *l; | |
158 | ||
159 | devc = sdi->priv; | |
160 | ||
161 | if (g_str_has_prefix((const char *)devc->buf, "overtemp")) { | |
162 | sr_dbg("Overtemperature condition!"); | |
163 | devc->otp_active = TRUE; | |
164 | return; | |
165 | } | |
166 | ||
167 | if (!g_str_has_prefix((const char *)devc->buf, "read ")) { | |
168 | sr_dbg("Unknown packet: '%s'.", devc->buf); | |
169 | return; | |
170 | } | |
171 | ||
172 | tokens = g_strsplit((const char *)devc->buf, " ", 3); | |
173 | voltage = g_ascii_strtod(tokens[2], NULL) / 1000; | |
174 | current = g_ascii_strtod(tokens[1], NULL) / 1000; | |
175 | g_strfreev(tokens); | |
176 | ||
803db07a UH |
177 | /* Begin frame. */ |
178 | packet.type = SR_DF_FRAME_BEGIN; | |
6e68da51 | 179 | packet.payload = NULL; |
803db07a UH |
180 | sr_session_send(sdi, &packet); |
181 | ||
6e68da51 UH |
182 | sr_analog_init(&analog, &encoding, &meaning, &spec, 4); |
183 | ||
184 | packet.type = SR_DF_ANALOG; | |
803db07a UH |
185 | packet.payload = &analog; |
186 | analog.num_samples = 1; | |
187 | ||
188 | /* Voltage */ | |
189 | l = g_slist_copy(sdi->channels); | |
190 | l = g_slist_remove_link(l, g_slist_nth(l, 1)); | |
6e68da51 UH |
191 | meaning.channels = l; |
192 | meaning.mq = SR_MQ_VOLTAGE; | |
193 | meaning.mqflags = SR_MQFLAG_DC; | |
194 | meaning.unit = SR_UNIT_VOLT; | |
803db07a UH |
195 | analog.data = &voltage; |
196 | sr_session_send(sdi, &packet); | |
197 | g_slist_free(l); | |
198 | ||
199 | /* Current */ | |
200 | l = g_slist_copy(sdi->channels); | |
201 | l = g_slist_remove_link(l, g_slist_nth(l, 0)); | |
6e68da51 UH |
202 | meaning.channels = l; |
203 | meaning.mq = SR_MQ_CURRENT; | |
204 | meaning.mqflags = SR_MQFLAG_DC; | |
205 | meaning.unit = SR_UNIT_AMPERE; | |
803db07a UH |
206 | analog.data = ¤t; |
207 | sr_session_send(sdi, &packet); | |
208 | g_slist_free(l); | |
209 | ||
210 | /* End frame. */ | |
211 | packet.type = SR_DF_FRAME_END; | |
6e68da51 | 212 | packet.payload = NULL; |
803db07a UH |
213 | sr_session_send(sdi, &packet); |
214 | ||
215 | devc->num_samples++; | |
216 | } | |
217 | ||
218 | static void handle_new_data(const struct sr_dev_inst *sdi) | |
219 | { | |
220 | int len; | |
221 | struct dev_context *devc; | |
222 | struct sr_serial_dev_inst *serial; | |
223 | ||
224 | devc = sdi->priv; | |
225 | serial = sdi->conn; | |
226 | ||
227 | /* Try to get as much data as the buffer can hold. */ | |
228 | len = RELOADPRO_BUFSIZE - devc->buflen; | |
229 | len = serial_read_nonblocking(serial, devc->buf + devc->buflen, len); | |
230 | if (len == 0) | |
231 | return; /* No new bytes, nothing to do. */ | |
232 | if (len < 0) { | |
233 | sr_err("Serial port read error: %d.", len); | |
234 | return; | |
235 | } | |
236 | devc->buflen += len; | |
237 | ||
238 | if (g_str_has_suffix((const char *)devc->buf, "\n")) { | |
239 | handle_packet(sdi); | |
240 | memset(devc->buf, 0, RELOADPRO_BUFSIZE); | |
241 | devc->buflen = 0; | |
242 | } | |
243 | } | |
244 | ||
245 | SR_PRIV int reloadpro_receive_data(int fd, int revents, void *cb_data) | |
246 | { | |
247 | struct sr_dev_inst *sdi; | |
248 | struct dev_context *devc; | |
249 | int64_t t; | |
6e8d31d4 UH |
250 | |
251 | (void)fd; | |
252 | ||
803db07a UH |
253 | sdi = cb_data; |
254 | devc = sdi->priv; | |
255 | ||
256 | if (revents != G_IO_IN) | |
6e8d31d4 UH |
257 | return TRUE; |
258 | ||
803db07a UH |
259 | handle_new_data(sdi); |
260 | ||
261 | if (devc->limit_samples && (devc->num_samples >= devc->limit_samples)) { | |
262 | sr_info("Requested number of samples reached."); | |
263 | sdi->driver->dev_acquisition_stop(sdi, cb_data); | |
6e8d31d4 | 264 | return TRUE; |
803db07a | 265 | } |
6e8d31d4 | 266 | |
803db07a UH |
267 | if (devc->limit_msec) { |
268 | t = (g_get_monotonic_time() - devc->starttime) / 1000; | |
269 | if (t > (int64_t)devc->limit_msec) { | |
270 | sr_info("Requested time limit reached."); | |
271 | sdi->driver->dev_acquisition_stop(sdi, cb_data); | |
272 | return TRUE; | |
273 | } | |
6e8d31d4 UH |
274 | } |
275 | ||
276 | return TRUE; | |
277 | } |