]>
Commit | Line | Data |
---|---|---|
8f4e922f UH |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2014 Uwe Hermann <uwe@hermann-uwe.de> | |
9740d9bf | 5 | * Copyright (C) 2014 Matthias Heidbrink <m-sigrok@heidbrink.biz> |
8f4e922f UH |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
2ea1fdf1 | 18 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
8f4e922f UH |
19 | */ |
20 | ||
6ec6c43b | 21 | #include <config.h> |
8f4e922f UH |
22 | #include "protocol.h" |
23 | ||
b5e92647 UH |
24 | #define REQ_TIMEOUT_MS 500 |
25 | ||
9740d9bf | 26 | SR_PRIV int hcs_send_cmd(struct sr_serial_dev_inst *serial, const char *cmd, ...) |
b5e92647 UH |
27 | { |
28 | int ret; | |
9740d9bf | 29 | char cmdbuf[50]; |
b5e92647 | 30 | char *cmd_esc; |
9740d9bf MH |
31 | va_list args; |
32 | ||
33 | va_start(args, cmd); | |
34 | vsnprintf(cmdbuf, sizeof(cmdbuf), cmd, args); | |
35 | va_end(args); | |
b5e92647 | 36 | |
9740d9bf | 37 | cmd_esc = g_strescape(cmdbuf, NULL); |
b5e92647 | 38 | sr_dbg("Sending '%s'.", cmd_esc); |
9740d9bf | 39 | g_free(cmd_esc); |
b5e92647 | 40 | |
896e1a45 UH |
41 | if ((ret = serial_write_blocking(serial, cmdbuf, strlen(cmdbuf), |
42 | serial_timeout(serial, strlen(cmdbuf)))) < 0) { | |
b5e92647 | 43 | sr_err("Error sending command: %d.", ret); |
b5e92647 UH |
44 | return ret; |
45 | } | |
46 | ||
b5e92647 UH |
47 | return ret; |
48 | } | |
49 | ||
9740d9bf MH |
50 | /** |
51 | * Read data from interface into buffer blocking until @a lines number of \\r chars | |
52 | * received. | |
d9251a2c | 53 | * |
9740d9bf MH |
54 | * @param serial Previously initialized serial port structure. |
55 | * @param[in] lines Number of \\r-terminated lines to read (1-n). | |
d9251a2c | 56 | * @param buf Buffer for result. Contents is NUL-terminated on success. |
9740d9bf | 57 | * @param[in] buflen Buffer length (>0). |
d9251a2c | 58 | * |
9740d9bf MH |
59 | * @retval SR_OK Lines received and ending with "OK\r" (success). |
60 | * @retval SR_ERR Error. | |
61 | * @retval SR_ERR_ARG Invalid argument. | |
62 | */ | |
c442ffda | 63 | SR_PRIV int hcs_read_reply(struct sr_serial_dev_inst *serial, int lines, char *buf, int buflen) |
9740d9bf MH |
64 | { |
65 | int l_recv = 0; | |
66 | int bufpos = 0; | |
67 | int retc; | |
68 | ||
69 | if (!serial || (lines <= 0) || !buf || (buflen <= 0)) | |
70 | return SR_ERR_ARG; | |
71 | ||
72 | while ((l_recv < lines) && (bufpos < (buflen + 1))) { | |
eead2782 | 73 | retc = serial_read_blocking(serial, &buf[bufpos], 1, 0); |
9740d9bf MH |
74 | if (retc != 1) |
75 | return SR_ERR; | |
76 | if (buf[bufpos] == '\r') | |
77 | l_recv++; | |
78 | bufpos++; | |
79 | } | |
80 | buf[bufpos] = '\0'; | |
81 | ||
82 | if ((l_recv == lines) && (g_str_has_suffix(buf, "OK\r"))) | |
83 | return SR_OK; | |
84 | else | |
85 | return SR_ERR; | |
86 | } | |
87 | ||
88 | /** Interpret result of GETD command. */ | |
89 | SR_PRIV int hcs_parse_volt_curr_mode(struct sr_dev_inst *sdi, char **tokens) | |
b5e92647 UH |
90 | { |
91 | char *str; | |
92 | double val; | |
93 | struct dev_context *devc; | |
94 | ||
95 | devc = sdi->priv; | |
96 | ||
97 | /* Bytes 0-3: Voltage. */ | |
98 | str = g_strndup(tokens[0], 4); | |
99 | val = g_ascii_strtod(str, NULL) / 100; | |
100 | devc->voltage = val; | |
101 | g_free(str); | |
102 | ||
103 | /* Bytes 4-7: Current. */ | |
104 | str = g_strndup((tokens[0] + 4), 4); | |
105 | val = g_ascii_strtod(str, NULL) / 100; | |
106 | devc->current = val; | |
107 | g_free(str); | |
108 | ||
109 | /* Byte 8: Mode ('0' means CV, '1' means CC). */ | |
110 | devc->cc_mode = (tokens[0][8] == '1'); | |
111 | ||
9740d9bf MH |
112 | /* Output enabled? Works because voltage cannot be set to 0.0 directly. */ |
113 | devc->output_enabled = devc->voltage != 0.0; | |
114 | ||
b5e92647 UH |
115 | return SR_OK; |
116 | } | |
117 | ||
118 | static void send_sample(struct sr_dev_inst *sdi) | |
8f4e922f | 119 | { |
8f4e922f | 120 | struct dev_context *devc; |
b5e92647 | 121 | struct sr_datafeed_packet packet; |
2463ec77 UH |
122 | struct sr_datafeed_analog analog; |
123 | struct sr_analog_encoding encoding; | |
124 | struct sr_analog_meaning meaning; | |
125 | struct sr_analog_spec spec; | |
b5e92647 UH |
126 | |
127 | devc = sdi->priv; | |
128 | ||
1af428d0 | 129 | sr_analog_init(&analog, &encoding, &meaning, &spec, 2); |
2463ec77 UH |
130 | |
131 | packet.type = SR_DF_ANALOG; | |
b5e92647 | 132 | packet.payload = &analog; |
2463ec77 | 133 | analog.meaning->channels = sdi->channels; |
b5e92647 UH |
134 | analog.num_samples = 1; |
135 | ||
2463ec77 UH |
136 | analog.meaning->mq = SR_MQ_VOLTAGE; |
137 | analog.meaning->unit = SR_UNIT_VOLT; | |
138 | analog.meaning->mqflags = SR_MQFLAG_DC; | |
b5e92647 UH |
139 | analog.data = &devc->voltage; |
140 | sr_session_send(sdi, &packet); | |
141 | ||
2463ec77 UH |
142 | analog.meaning->mq = SR_MQ_CURRENT; |
143 | analog.meaning->unit = SR_UNIT_AMPERE; | |
144 | analog.meaning->mqflags = 0; | |
b5e92647 UH |
145 | analog.data = &devc->current; |
146 | sr_session_send(sdi, &packet); | |
147 | ||
a655b3fd LPC |
148 | |
149 | sr_sw_limits_update_samples_read(&devc->limits, 1); | |
b5e92647 UH |
150 | } |
151 | ||
152 | static int parse_reply(struct sr_dev_inst *sdi) | |
153 | { | |
154 | struct dev_context *devc; | |
155 | char *reply_esc, **tokens; | |
9740d9bf | 156 | int retc; |
b5e92647 UH |
157 | |
158 | devc = sdi->priv; | |
159 | ||
160 | reply_esc = g_strescape(devc->buf, NULL); | |
161 | sr_dbg("Received '%s'.", reply_esc); | |
9740d9bf | 162 | g_free(reply_esc); |
b5e92647 UH |
163 | |
164 | tokens = g_strsplit(devc->buf, "\r", 0); | |
9740d9bf MH |
165 | retc = hcs_parse_volt_curr_mode(sdi, tokens); |
166 | g_strfreev(tokens); | |
167 | if (retc < 0) | |
b5e92647 | 168 | return SR_ERR; |
b5e92647 | 169 | |
9740d9bf | 170 | send_sample(sdi); |
b5e92647 UH |
171 | |
172 | return SR_OK; | |
173 | } | |
174 | ||
175 | static int handle_new_data(struct sr_dev_inst *sdi) | |
176 | { | |
177 | int len; | |
178 | struct dev_context *devc; | |
179 | struct sr_serial_dev_inst *serial; | |
180 | ||
181 | devc = sdi->priv; | |
182 | serial = sdi->conn; | |
183 | ||
eead2782 | 184 | len = serial_read_blocking(serial, devc->buf + devc->buflen, 1, 0); |
b5e92647 UH |
185 | if (len < 1) |
186 | return SR_ERR; | |
187 | ||
188 | devc->buflen += len; | |
189 | devc->buf[devc->buflen] = '\0'; | |
190 | ||
191 | /* Wait until we received an "OK\r" (among other bytes). */ | |
192 | if (!g_str_has_suffix(devc->buf, "OK\r")) | |
193 | return SR_OK; | |
194 | ||
195 | parse_reply(sdi); | |
196 | ||
197 | devc->buf[0] = '\0'; | |
198 | devc->buflen = 0; | |
199 | ||
200 | devc->reply_pending = FALSE; | |
201 | ||
202 | return SR_OK; | |
203 | } | |
204 | ||
9740d9bf | 205 | /** Driver/serial data reception function. */ |
b5e92647 UH |
206 | SR_PRIV int hcs_receive_data(int fd, int revents, void *cb_data) |
207 | { | |
208 | struct sr_dev_inst *sdi; | |
209 | struct dev_context *devc; | |
210 | struct sr_serial_dev_inst *serial; | |
a655b3fd | 211 | uint64_t elapsed_us; |
8f4e922f UH |
212 | |
213 | (void)fd; | |
214 | ||
215 | if (!(sdi = cb_data)) | |
216 | return TRUE; | |
217 | ||
218 | if (!(devc = sdi->priv)) | |
219 | return TRUE; | |
220 | ||
b5e92647 UH |
221 | serial = sdi->conn; |
222 | ||
8f4e922f | 223 | if (revents == G_IO_IN) { |
b5e92647 UH |
224 | /* New data arrived. */ |
225 | handle_new_data(sdi); | |
226 | } else { | |
227 | /* Timeout. */ | |
228 | } | |
229 | ||
a655b3fd | 230 | if (sr_sw_limits_check(&devc->limits)) { |
d2f7c417 | 231 | sr_dev_acquisition_stop(sdi); |
b5e92647 UH |
232 | return TRUE; |
233 | } | |
234 | ||
b5e92647 UH |
235 | /* Request next packet, if required. */ |
236 | if (sdi->status == SR_ST_ACTIVE) { | |
237 | if (devc->reply_pending) { | |
238 | elapsed_us = g_get_monotonic_time() - devc->req_sent_at; | |
239 | if (elapsed_us > (REQ_TIMEOUT_MS * 1000)) | |
240 | devc->reply_pending = FALSE; | |
241 | return TRUE; | |
242 | } | |
243 | ||
244 | /* Send command to get voltage, current, and mode (CC or CV). */ | |
9740d9bf | 245 | if (hcs_send_cmd(serial, "GETD\r") < 0) |
b5e92647 UH |
246 | return TRUE; |
247 | ||
248 | devc->req_sent_at = g_get_monotonic_time(); | |
249 | devc->reply_pending = TRUE; | |
8f4e922f UH |
250 | } |
251 | ||
252 | return TRUE; | |
253 | } |