]>
Commit | Line | Data |
---|---|---|
a1bb33af | 1 | /* |
50985c20 | 2 | * This file is part of the libsigrok project. |
a1bb33af | 3 | * |
c73d2ea4 | 4 | * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com> |
b19f4622 | 5 | * Copyright (C) 2010-2012 Uwe Hermann <uwe@hermann-uwe.de> |
766456be | 6 | * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com> |
a1bb33af UH |
7 | * |
8 | * This program is free software: you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation, either version 3 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 | */ | |
21 | ||
54b38f64 | 22 | #include <string.h> |
d02a535e | 23 | #include <stdlib.h> |
a1bb33af | 24 | #include <glib.h> |
0d4405ce | 25 | #include <libserialport.h> |
45c59c8b BV |
26 | #include "libsigrok.h" |
27 | #include "libsigrok-internal.h" | |
a1bb33af | 28 | |
3544f848 | 29 | #define LOG_PREFIX "serial" |
b19f4622 | 30 | |
b19f4622 UH |
31 | /** |
32 | * Open the specified serial port. | |
33 | * | |
299bdb24 | 34 | * @param serial Previously initialized serial port structure. |
a54dd31e UH |
35 | * @param flags Flags to use when opening the serial port. Possible flags |
36 | * include SERIAL_RDWR, SERIAL_RDONLY, SERIAL_NONBLOCK. | |
b19f4622 | 37 | * |
299bdb24 BV |
38 | * If the serial structure contains a serialcomm string, it will be |
39 | * passed to serial_set_paramstr() after the port is opened. | |
40 | * | |
41 | * @return SR_OK on success, SR_ERR on failure. | |
b19f4622 | 42 | */ |
299bdb24 | 43 | SR_PRIV int serial_open(struct sr_serial_dev_inst *serial, int flags) |
d02a535e | 44 | { |
a9bce5a5 ML |
45 | int ret; |
46 | char *error; | |
13cd8197 | 47 | int sp_flags = 0; |
b19f4622 | 48 | |
299bdb24 BV |
49 | if (!serial) { |
50 | sr_dbg("Invalid serial port."); | |
51 | return SR_ERR; | |
52 | } | |
53 | ||
54 | sr_spew("Opening serial port '%s' (flags %d).", serial->port, flags); | |
b19f4622 | 55 | |
c9bc57b6 ML |
56 | sp_get_port_by_name(serial->port, &serial->data); |
57 | ||
13cd8197 ML |
58 | if (flags & SERIAL_RDWR) |
59 | sp_flags = (SP_MODE_READ | SP_MODE_WRITE); | |
60 | else if (flags & SERIAL_RDONLY) | |
61 | sp_flags = SP_MODE_READ; | |
9647ce69 ML |
62 | |
63 | serial->nonblocking = (flags & SERIAL_NONBLOCK) ? 1 : 0; | |
13cd8197 ML |
64 | |
65 | ret = sp_open(serial->data, sp_flags); | |
a9bce5a5 | 66 | |
a0dfaa6c UH |
67 | switch (ret) { |
68 | case SP_ERR_ARG: | |
69 | sr_err("Attempt to open serial port with invalid parameters."); | |
70 | return SR_ERR_ARG; | |
71 | case SP_ERR_FAIL: | |
72 | error = sp_last_error_message(); | |
cb7b165b UH |
73 | sr_err("Error opening port (%d): %s.", |
74 | sp_last_error_code(), error); | |
a0dfaa6c UH |
75 | sp_free_error_message(error); |
76 | return SR_ERR; | |
a54dd31e UH |
77 | } |
78 | ||
299bdb24 BV |
79 | if (serial->serialcomm) |
80 | return serial_set_paramstr(serial, serial->serialcomm); | |
81 | else | |
82 | return SR_OK; | |
d02a535e BV |
83 | } |
84 | ||
b19f4622 UH |
85 | /** |
86 | * Close the specified serial port. | |
87 | * | |
299bdb24 | 88 | * @param serial Previously initialized serial port structure. |
b19f4622 | 89 | * |
299bdb24 | 90 | * @return SR_OK on success, SR_ERR on failure. |
2119ab03 | 91 | */ |
299bdb24 | 92 | SR_PRIV int serial_close(struct sr_serial_dev_inst *serial) |
d02a535e | 93 | { |
299bdb24 | 94 | int ret; |
a9bce5a5 | 95 | char *error; |
299bdb24 BV |
96 | |
97 | if (!serial) { | |
98 | sr_dbg("Invalid serial port."); | |
99 | return SR_ERR; | |
100 | } | |
101 | ||
64ecf7ee ML |
102 | if (!serial->data) { |
103 | sr_dbg("Cannot close unopened serial port %s.", serial->port); | |
299bdb24 BV |
104 | return SR_ERR; |
105 | } | |
106 | ||
64ecf7ee | 107 | sr_spew("Closing serial port %s.", serial->port); |
b19f4622 | 108 | |
c9bc57b6 | 109 | ret = sp_close(serial->data); |
a9bce5a5 | 110 | |
a0dfaa6c UH |
111 | switch (ret) { |
112 | case SP_ERR_ARG: | |
113 | sr_err("Attempt to close an invalid serial port."); | |
114 | return SR_ERR_ARG; | |
115 | case SP_ERR_FAIL: | |
116 | error = sp_last_error_message(); | |
cb7b165b UH |
117 | sr_err("Error closing port (%d): %s.", |
118 | sp_last_error_code(), error); | |
a0dfaa6c UH |
119 | sp_free_error_message(error); |
120 | return SR_ERR; | |
b19f4622 | 121 | } |
299bdb24 | 122 | |
64ecf7ee ML |
123 | sp_free_port(serial->data); |
124 | serial->data = NULL; | |
125 | ||
6a76efeb | 126 | return SR_OK; |
d02a535e BV |
127 | } |
128 | ||
b19f4622 | 129 | /** |
299bdb24 | 130 | * Flush serial port buffers. |
b19f4622 | 131 | * |
299bdb24 | 132 | * @param serial Previously initialized serial port structure. |
b19f4622 | 133 | * |
299bdb24 | 134 | * @return SR_OK on success, SR_ERR on failure. |
1fdb75e1 | 135 | */ |
299bdb24 | 136 | SR_PRIV int serial_flush(struct sr_serial_dev_inst *serial) |
06d64eb8 | 137 | { |
299bdb24 | 138 | int ret; |
a9bce5a5 | 139 | char *error; |
299bdb24 BV |
140 | |
141 | if (!serial) { | |
142 | sr_dbg("Invalid serial port."); | |
143 | return SR_ERR; | |
144 | } | |
145 | ||
64ecf7ee ML |
146 | if (!serial->data) { |
147 | sr_dbg("Cannot flush unopened serial port %s.", serial->port); | |
299bdb24 BV |
148 | return SR_ERR; |
149 | } | |
150 | ||
64ecf7ee | 151 | sr_spew("Flushing serial port %s.", serial->port); |
b19f4622 | 152 | |
13cd8197 | 153 | ret = sp_flush(serial->data, SP_BUF_BOTH); |
a9bce5a5 | 154 | |
a0dfaa6c UH |
155 | switch (ret) { |
156 | case SP_ERR_ARG: | |
157 | sr_err("Attempt to flush an invalid serial port."); | |
158 | return SR_ERR_ARG; | |
159 | case SP_ERR_FAIL: | |
160 | error = sp_last_error_message(); | |
cb7b165b UH |
161 | sr_err("Error flushing port (%d): %s.", |
162 | sp_last_error_code(), error); | |
a0dfaa6c UH |
163 | sp_free_error_message(error); |
164 | return SR_ERR; | |
299bdb24 | 165 | } |
b19f4622 | 166 | |
6a76efeb | 167 | return SR_OK; |
06d64eb8 BV |
168 | } |
169 | ||
9a474211 ML |
170 | static int _serial_write(struct sr_serial_dev_inst *serial, |
171 | const void *buf, size_t count, int nonblocking) | |
2119ab03 | 172 | { |
299bdb24 | 173 | ssize_t ret; |
a9bce5a5 | 174 | char *error; |
299bdb24 BV |
175 | |
176 | if (!serial) { | |
177 | sr_dbg("Invalid serial port."); | |
6a76efeb | 178 | return SR_ERR; |
299bdb24 BV |
179 | } |
180 | ||
64ecf7ee ML |
181 | if (!serial->data) { |
182 | sr_dbg("Cannot use unopened serial port %s.", serial->port); | |
6a76efeb | 183 | return SR_ERR; |
299bdb24 BV |
184 | } |
185 | ||
9a474211 | 186 | if (nonblocking) |
9647ce69 ML |
187 | ret = sp_nonblocking_write(serial->data, buf, count); |
188 | else | |
189 | ret = sp_blocking_write(serial->data, buf, count, 0); | |
a9bce5a5 | 190 | |
a0dfaa6c UH |
191 | switch (ret) { |
192 | case SP_ERR_ARG: | |
193 | sr_err("Attempted serial port write with invalid arguments."); | |
194 | return SR_ERR_ARG; | |
195 | case SP_ERR_FAIL: | |
196 | error = sp_last_error_message(); | |
cb7b165b | 197 | sr_err("Write error (%d): %s.", sp_last_error_code(), error); |
a0dfaa6c UH |
198 | sp_free_error_message(error); |
199 | return SR_ERR; | |
a9bce5a5 ML |
200 | } |
201 | ||
64ecf7ee | 202 | sr_spew("Wrote %d/%d bytes.", ret, count); |
b19f4622 UH |
203 | |
204 | return ret; | |
2119ab03 UH |
205 | } |
206 | ||
b19f4622 | 207 | /** |
9a474211 | 208 | * Write a number of bytes to the specified serial port. |
b19f4622 | 209 | * |
299bdb24 | 210 | * @param serial Previously initialized serial port structure. |
9a474211 ML |
211 | * @param buf Buffer containing the bytes to write. |
212 | * @param count Number of bytes to write. | |
b19f4622 | 213 | * |
9a474211 | 214 | * @return The number of bytes written, or a negative error code upon failure. |
2119ab03 | 215 | */ |
9a474211 ML |
216 | SR_PRIV int serial_write(struct sr_serial_dev_inst *serial, |
217 | const void *buf, size_t count) | |
218 | { | |
219 | return _serial_write(serial, buf, count, serial->nonblocking); | |
220 | } | |
221 | ||
222 | SR_PRIV int serial_write_blocking(struct sr_serial_dev_inst *serial, | |
223 | const void *buf, size_t count) | |
224 | { | |
225 | return _serial_write(serial, buf, count, 0); | |
226 | } | |
227 | ||
228 | SR_PRIV int serial_write_nonblocking(struct sr_serial_dev_inst *serial, | |
229 | const void *buf, size_t count) | |
230 | { | |
231 | return _serial_write(serial, buf, count, 1); | |
232 | } | |
233 | ||
234 | static int _serial_read(struct sr_serial_dev_inst *serial, void *buf, | |
235 | size_t count, int nonblocking) | |
2119ab03 | 236 | { |
299bdb24 | 237 | ssize_t ret; |
c4d85a40 | 238 | char *error; |
299bdb24 BV |
239 | |
240 | if (!serial) { | |
241 | sr_dbg("Invalid serial port."); | |
6a76efeb | 242 | return SR_ERR; |
299bdb24 BV |
243 | } |
244 | ||
64ecf7ee ML |
245 | if (!serial->data) { |
246 | sr_dbg("Cannot use unopened serial port %s.", serial->port); | |
6a76efeb | 247 | return SR_ERR; |
299bdb24 BV |
248 | } |
249 | ||
9a474211 | 250 | if (nonblocking) |
9647ce69 ML |
251 | ret = sp_nonblocking_read(serial->data, buf, count); |
252 | else | |
253 | ret = sp_blocking_read(serial->data, buf, count, 0); | |
2119ab03 | 254 | |
a0dfaa6c UH |
255 | switch (ret) { |
256 | case SP_ERR_ARG: | |
257 | sr_err("Attempted serial port read with invalid arguments."); | |
258 | return SR_ERR_ARG; | |
c4d85a40 UH |
259 | case SP_ERR_FAIL: |
260 | error = sp_last_error_message(); | |
cb7b165b | 261 | sr_err("Read error (%d): %s.", sp_last_error_code(), error); |
c4d85a40 UH |
262 | sp_free_error_message(error); |
263 | return SR_ERR; | |
a9bce5a5 ML |
264 | } |
265 | ||
c4d85a40 | 266 | if (ret > 0) |
64ecf7ee | 267 | sr_spew("Read %d/%d bytes.", ret, count); |
b19f4622 UH |
268 | |
269 | return ret; | |
2119ab03 UH |
270 | } |
271 | ||
9a474211 ML |
272 | /** |
273 | * Read a number of bytes from the specified serial port. | |
274 | * | |
275 | * @param serial Previously initialized serial port structure. | |
276 | * @param buf Buffer where to store the bytes that are read. | |
277 | * @param count The number of bytes to read. | |
278 | * | |
279 | * @return The number of bytes read, or a negative error code upon failure. | |
280 | */ | |
281 | SR_PRIV int serial_read(struct sr_serial_dev_inst *serial, void *buf, | |
282 | size_t count) | |
283 | { | |
284 | return _serial_read(serial, buf, count, serial->nonblocking); | |
285 | } | |
286 | ||
287 | SR_PRIV int serial_read_blocking(struct sr_serial_dev_inst *serial, void *buf, | |
288 | size_t count) | |
289 | { | |
290 | return _serial_read(serial, buf, count, 0); | |
291 | } | |
292 | ||
293 | SR_PRIV int serial_read_nonblocking(struct sr_serial_dev_inst *serial, void *buf, | |
294 | size_t count) | |
295 | { | |
296 | return _serial_read(serial, buf, count, 1); | |
297 | } | |
298 | ||
b19f4622 UH |
299 | /** |
300 | * Set serial parameters for the specified serial port. | |
301 | * | |
299bdb24 | 302 | * @param serial Previously initialized serial port structure. |
04cb9157 MH |
303 | * @param[in] baudrate The baudrate to set. |
304 | * @param[in] bits The number of data bits to use (5, 6, 7 or 8). | |
305 | * @param[in] parity The parity setting to use (0 = none, 1 = even, 2 = odd). | |
306 | * @param[in] stopbits The number of stop bits to use (1 or 2). | |
307 | * @param[in] flowcontrol The flow control settings to use (0 = none, | |
308 | * 1 = RTS/CTS, 2 = XON/XOFF). | |
309 | * @param[in] rts Status of RTS line (0 or 1; required by some interfaces). | |
310 | * @param[in] dtr Status of DTR line (0 or 1; required by some interfaces). | |
2119ab03 | 311 | * |
04cb9157 MH |
312 | * @retval SR_OK Success |
313 | * @retval SR_ERR Failure. | |
1ff7712c | 314 | */ |
299bdb24 | 315 | SR_PRIV int serial_set_params(struct sr_serial_dev_inst *serial, int baudrate, |
39e5d798 UH |
316 | int bits, int parity, int stopbits, |
317 | int flowcontrol, int rts, int dtr) | |
d02a535e | 318 | { |
a9bce5a5 ML |
319 | int ret; |
320 | char *error; | |
25b66c3c | 321 | struct sp_port_config *config; |
a9bce5a5 | 322 | |
299bdb24 BV |
323 | if (!serial) { |
324 | sr_dbg("Invalid serial port."); | |
325 | return SR_ERR; | |
326 | } | |
327 | ||
64ecf7ee ML |
328 | if (!serial->data) { |
329 | sr_dbg("Cannot configure unopened serial port %s.", serial->port); | |
299bdb24 BV |
330 | return SR_ERR; |
331 | } | |
332 | ||
64ecf7ee | 333 | sr_spew("Setting serial parameters on port %s.", serial->port); |
b19f4622 | 334 | |
25b66c3c ML |
335 | sp_new_config(&config); |
336 | sp_set_config_baudrate(config, baudrate); | |
337 | sp_set_config_bits(config, bits); | |
13cd8197 ML |
338 | switch (parity) { |
339 | case 0: | |
25b66c3c | 340 | sp_set_config_parity(config, SP_PARITY_NONE); |
13cd8197 ML |
341 | break; |
342 | case 1: | |
25b66c3c | 343 | sp_set_config_parity(config, SP_PARITY_EVEN); |
13cd8197 ML |
344 | break; |
345 | case 2: | |
25b66c3c | 346 | sp_set_config_parity(config, SP_PARITY_ODD); |
13cd8197 ML |
347 | break; |
348 | default: | |
349 | return SR_ERR_ARG; | |
350 | } | |
25b66c3c ML |
351 | sp_set_config_stopbits(config, stopbits); |
352 | sp_set_config_rts(config, flowcontrol == 1 ? SP_RTS_FLOW_CONTROL : rts); | |
353 | sp_set_config_cts(config, flowcontrol == 1 ? SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE); | |
354 | sp_set_config_dtr(config, dtr); | |
355 | sp_set_config_dsr(config, SP_DSR_IGNORE); | |
356 | sp_set_config_xon_xoff(config, flowcontrol == 2 ? SP_XONXOFF_INOUT : SP_XONXOFF_DISABLED); | |
357 | ||
358 | ret = sp_set_config(serial->data, config); | |
359 | sp_free_config(config); | |
a9bce5a5 | 360 | |
a0dfaa6c UH |
361 | switch (ret) { |
362 | case SP_ERR_ARG: | |
363 | sr_err("Invalid arguments for setting serial port parameters."); | |
364 | return SR_ERR_ARG; | |
365 | case SP_ERR_FAIL: | |
366 | error = sp_last_error_message(); | |
cb7b165b UH |
367 | sr_err("Error setting serial port parameters (%d): %s.", |
368 | sp_last_error_code(), error); | |
a0dfaa6c UH |
369 | sp_free_error_message(error); |
370 | return SR_ERR; | |
700dcd5c UH |
371 | } |
372 | ||
e46b8fb1 | 373 | return SR_OK; |
d02a535e | 374 | } |
792fc686 | 375 | |
299bdb24 | 376 | /** |
48d3238e | 377 | * Set serial parameters for the specified serial port from parameter string. |
299bdb24 BV |
378 | * |
379 | * @param serial Previously initialized serial port structure. | |
48d3238e MH |
380 | * @param[in] paramstr A serial communication parameters string of the form |
381 | * "<baudrate>/<bits><parity><stopbits>{/<option>}".\n | |
382 | * Examples: "9600/8n1", "600/7o2/dtr=1/rts=0" or "460800/8n1/flow=2".\n | |
383 | * \<baudrate\>=integer Baud rate.\n | |
384 | * \<bits\>=5|6|7|8 Number of data bits.\n | |
385 | * \<parity\>=n|e|o None, even, odd.\n | |
386 | * \<stopbits\>=1|2 One or two stop bits.\n | |
387 | * Options:\n | |
388 | * dtr=0|1 Set DTR off resp. on.\n | |
389 | * flow=0|1|2 Flow control. 0 for none, 1 for RTS/CTS, 2 for XON/XOFF.\n | |
390 | * rts=0|1 Set RTS off resp. on.\n | |
391 | * Please note that values and combinations of these parameters must be | |
392 | * supported by the concrete serial interface hardware and the drivers for it. | |
04cb9157 MH |
393 | * @retval SR_OK Success. |
394 | * @retval SR_ERR Failure. | |
299bdb24 | 395 | */ |
299bdb24 BV |
396 | SR_PRIV int serial_set_paramstr(struct sr_serial_dev_inst *serial, |
397 | const char *paramstr) | |
792fc686 | 398 | { |
48d3238e MH |
399 | #define SERIAL_COMM_SPEC "^(\\d+)/([5678])([neo])([12])(.*)$" |
400 | ||
792fc686 BV |
401 | GRegex *reg; |
402 | GMatchInfo *match; | |
26ddb5ba | 403 | int speed, databits, parity, stopbits, flow, rts, dtr, i; |
71caaad4 | 404 | char *mstr, **opts, **kv; |
792fc686 | 405 | |
26ddb5ba | 406 | speed = databits = parity = stopbits = flow = 0; |
71caaad4 | 407 | rts = dtr = -1; |
ea088bb6 | 408 | sr_spew("Parsing parameters from \"%s\".", paramstr); |
792fc686 BV |
409 | reg = g_regex_new(SERIAL_COMM_SPEC, 0, 0, NULL); |
410 | if (g_regex_match(reg, paramstr, 0, &match)) { | |
411 | if ((mstr = g_match_info_fetch(match, 1))) | |
412 | speed = strtoul(mstr, NULL, 10); | |
413 | g_free(mstr); | |
414 | if ((mstr = g_match_info_fetch(match, 2))) | |
415 | databits = strtoul(mstr, NULL, 10); | |
416 | g_free(mstr); | |
417 | if ((mstr = g_match_info_fetch(match, 3))) { | |
418 | switch (mstr[0]) { | |
419 | case 'n': | |
420 | parity = SERIAL_PARITY_NONE; | |
421 | break; | |
422 | case 'e': | |
423 | parity = SERIAL_PARITY_EVEN; | |
424 | break; | |
425 | case 'o': | |
426 | parity = SERIAL_PARITY_ODD; | |
427 | break; | |
428 | } | |
429 | } | |
430 | g_free(mstr); | |
431 | if ((mstr = g_match_info_fetch(match, 4))) | |
432 | stopbits = strtoul(mstr, NULL, 10); | |
433 | g_free(mstr); | |
71caaad4 BV |
434 | if ((mstr = g_match_info_fetch(match, 5)) && mstr[0] != '\0') { |
435 | if (mstr[0] != '/') { | |
436 | sr_dbg("missing separator before extra options"); | |
437 | speed = 0; | |
438 | } else { | |
439 | /* A set of "key=value" options separated by / */ | |
440 | opts = g_strsplit(mstr + 1, "/", 0); | |
441 | for (i = 0; opts[i]; i++) { | |
442 | kv = g_strsplit(opts[i], "=", 2); | |
443 | if (!strncmp(kv[0], "rts", 3)) { | |
444 | if (kv[1][0] == '1') | |
445 | rts = 1; | |
446 | else if (kv[1][0] == '0') | |
447 | rts = 0; | |
448 | else { | |
449 | sr_dbg("invalid value for rts: %c", kv[1][0]); | |
450 | speed = 0; | |
451 | } | |
452 | } else if (!strncmp(kv[0], "dtr", 3)) { | |
453 | if (kv[1][0] == '1') | |
454 | dtr = 1; | |
455 | else if (kv[1][0] == '0') | |
456 | dtr = 0; | |
457 | else { | |
458 | sr_dbg("invalid value for dtr: %c", kv[1][0]); | |
459 | speed = 0; | |
460 | } | |
26ddb5ba | 461 | } else if (!strncmp(kv[0], "flow", 4)) { |
462 | if (kv[1][0] == '0') | |
463 | flow = 0; | |
464 | else if (kv[1][0] == '1') | |
465 | flow = 1; | |
466 | else if (kv[1][0] == '2') | |
467 | flow = 2; | |
468 | else { | |
469 | sr_dbg("invalid value for flow: %c", kv[1][0]); | |
470 | speed = 0; | |
471 | } | |
71caaad4 BV |
472 | } |
473 | g_strfreev(kv); | |
474 | } | |
475 | g_strfreev(opts); | |
476 | } | |
477 | } | |
478 | g_free(mstr); | |
792fc686 BV |
479 | } |
480 | g_match_info_unref(match); | |
481 | g_regex_unref(reg); | |
482 | ||
ea088bb6 AG |
483 | if (speed) { |
484 | return serial_set_params(serial, speed, databits, parity, | |
26ddb5ba | 485 | stopbits, flow, rts, dtr); |
ea088bb6 AG |
486 | } else { |
487 | sr_dbg("Could not infer speed from parameter string."); | |
792fc686 | 488 | return SR_ERR_ARG; |
ea088bb6 | 489 | } |
792fc686 BV |
490 | } |
491 | ||
299bdb24 BV |
492 | /** |
493 | * Read a line from the specified serial port. | |
494 | * | |
495 | * @param serial Previously initialized serial port structure. | |
496 | * @param buf Buffer where to store the bytes that are read. | |
497 | * @param buflen Size of the buffer. | |
04cb9157 | 498 | * @param[in] timeout_ms How long to wait for a line to come in. |
299bdb24 BV |
499 | * |
500 | * Reading stops when CR of LR is found, which is stripped from the buffer. | |
501 | * | |
04cb9157 MH |
502 | * @retval SR_OK Success. |
503 | * @retval SR_ERR Failure. | |
299bdb24 BV |
504 | */ |
505 | SR_PRIV int serial_readline(struct sr_serial_dev_inst *serial, char **buf, | |
506 | int *buflen, gint64 timeout_ms) | |
6f22a8ef | 507 | { |
b87f8504 | 508 | gint64 start; |
6f22a8ef UH |
509 | int maxlen, len; |
510 | ||
64ecf7ee | 511 | if (!serial) { |
299bdb24 BV |
512 | sr_dbg("Invalid serial port."); |
513 | return SR_ERR; | |
514 | } | |
515 | ||
64ecf7ee ML |
516 | if (!serial->data) { |
517 | sr_dbg("Cannot use unopened serial port %s.", serial->port); | |
299bdb24 BV |
518 | return -1; |
519 | } | |
520 | ||
6f22a8ef UH |
521 | timeout_ms *= 1000; |
522 | start = g_get_monotonic_time(); | |
523 | ||
524 | maxlen = *buflen; | |
525 | *buflen = len = 0; | |
526 | while(1) { | |
527 | len = maxlen - *buflen - 1; | |
528 | if (len < 1) | |
529 | break; | |
299bdb24 | 530 | len = serial_read(serial, *buf + *buflen, 1); |
6f22a8ef UH |
531 | if (len > 0) { |
532 | *buflen += len; | |
533 | *(*buf + *buflen) = '\0'; | |
318dd53c BV |
534 | if (*buflen > 0 && (*(*buf + *buflen - 1) == '\r' |
535 | || *(*buf + *buflen - 1) == '\n')) { | |
536 | /* Strip CR/LF and terminate. */ | |
6f22a8ef UH |
537 | *(*buf + --*buflen) = '\0'; |
538 | break; | |
539 | } | |
540 | } | |
541 | if (g_get_monotonic_time() - start > timeout_ms) | |
542 | /* Timeout */ | |
543 | break; | |
5715e84f DT |
544 | if (len < 1) |
545 | g_usleep(2000); | |
6f22a8ef | 546 | } |
318dd53c BV |
547 | if (*buflen) |
548 | sr_dbg("Received %d: '%s'.", *buflen, *buf); | |
6f22a8ef UH |
549 | |
550 | return SR_OK; | |
551 | } | |
766456be UH |
552 | |
553 | /** | |
554 | * Try to find a valid packet in a serial data stream. | |
555 | * | |
556 | * @param serial Previously initialized serial port structure. | |
557 | * @param buf Buffer containing the bytes to write. | |
04cb9157 MH |
558 | * @param buflen Size of the buffer. |
559 | * @param[in] packet_size Size, in bytes, of a valid packet. | |
766456be | 560 | * @param is_valid Callback that assesses whether the packet is valid or not. |
04cb9157 | 561 | * @param[in] timeout_ms The timeout after which, if no packet is detected, to |
766456be | 562 | * abort scanning. |
04cb9157 | 563 | * @param[in] baudrate The baudrate of the serial port. This parameter is not |
766456be UH |
564 | * critical, but it helps fine tune the serial port polling |
565 | * delay. | |
566 | * | |
04cb9157 MH |
567 | * @retval SR_OK Valid packet was found within the given timeout |
568 | * @retval SR_ERR Failure. | |
766456be UH |
569 | */ |
570 | SR_PRIV int serial_stream_detect(struct sr_serial_dev_inst *serial, | |
571 | uint8_t *buf, size_t *buflen, | |
572 | size_t packet_size, packet_valid_t is_valid, | |
573 | uint64_t timeout_ms, int baudrate) | |
574 | { | |
575 | uint64_t start, time, byte_delay_us; | |
576 | size_t ibuf, i, maxlen; | |
577 | int len; | |
578 | ||
579 | maxlen = *buflen; | |
580 | ||
64ecf7ee ML |
581 | sr_dbg("Detecting packets on %s (timeout = %" PRIu64 |
582 | "ms, baudrate = %d).", serial->port, timeout_ms, baudrate); | |
766456be UH |
583 | |
584 | if (maxlen < (packet_size / 2) ) { | |
585 | sr_err("Buffer size must be at least twice the packet size."); | |
586 | return SR_ERR; | |
587 | } | |
588 | ||
766456be UH |
589 | /* Assume 8n1 transmission. That is 10 bits for every byte. */ |
590 | byte_delay_us = 10 * (1000000 / baudrate); | |
591 | start = g_get_monotonic_time(); | |
592 | ||
593 | i = ibuf = len = 0; | |
594 | while (ibuf < maxlen) { | |
595 | len = serial_read(serial, &buf[ibuf], 1); | |
596 | if (len > 0) { | |
597 | ibuf += len; | |
598 | } else if (len == 0) { | |
006dbe55 | 599 | /* No logging, already done in serial_read(). */ |
766456be UH |
600 | } else { |
601 | /* Error reading byte, but continuing anyway. */ | |
602 | } | |
551c3d8c AG |
603 | |
604 | time = g_get_monotonic_time() - start; | |
605 | time /= 1000; | |
606 | ||
766456be UH |
607 | if ((ibuf - i) >= packet_size) { |
608 | /* We have at least a packet's worth of data. */ | |
609 | if (is_valid(&buf[i])) { | |
766456be UH |
610 | sr_spew("Found valid %d-byte packet after " |
611 | "%" PRIu64 "ms.", (ibuf - i), time); | |
612 | *buflen = ibuf; | |
613 | return SR_OK; | |
614 | } else { | |
615 | sr_spew("Got %d bytes, but not a valid " | |
616 | "packet.", (ibuf - i)); | |
617 | } | |
618 | /* Not a valid packet. Continue searching. */ | |
619 | i++; | |
620 | } | |
551c3d8c | 621 | if (time >= timeout_ms) { |
766456be | 622 | /* Timeout */ |
551c3d8c | 623 | sr_dbg("Detection timed out after %dms.", time); |
766456be UH |
624 | break; |
625 | } | |
5715e84f DT |
626 | if (len < 1) |
627 | g_usleep(byte_delay_us); | |
766456be UH |
628 | } |
629 | ||
630 | *buflen = ibuf; | |
631 | ||
632 | sr_err("Didn't find a valid packet (read %d bytes).", *buflen); | |
633 | ||
634 | return SR_ERR; | |
635 | } | |
1bd9e678 DJ |
636 | |
637 | /** | |
638 | * Extract the serial device and options from the options linked list. | |
639 | * | |
640 | * @param options List of options passed from the command line. | |
641 | * @param serial_device Pointer where to store the exctracted serial device. | |
642 | * @param serial_options Pointer where to store the optional extracted serial | |
643 | * options. | |
644 | * | |
645 | * @return SR_OK if a serial_device is found, SR_ERR if no device is found. The | |
646 | * returned string should not be freed by the caller. | |
647 | */ | |
648 | SR_PRIV int sr_serial_extract_options(GSList *options, const char **serial_device, | |
649 | const char **serial_options) | |
650 | { | |
651 | GSList *l; | |
652 | struct sr_config *src; | |
653 | ||
654 | *serial_device = NULL; | |
655 | ||
656 | for (l = options; l; l = l->next) { | |
657 | src = l->data; | |
658 | switch (src->key) { | |
659 | case SR_CONF_CONN: | |
660 | *serial_device = g_variant_get_string(src->data, NULL); | |
661 | sr_dbg("Parsed serial device: %s", *serial_device); | |
662 | break; | |
663 | ||
664 | case SR_CONF_SERIALCOMM: | |
665 | *serial_options = g_variant_get_string(src->data, NULL); | |
666 | sr_dbg("Parsed serial options: %s", *serial_options); | |
667 | break; | |
668 | } | |
669 | } | |
670 | ||
671 | if (!*serial_device) { | |
672 | sr_dbg("No serial device specified"); | |
673 | return SR_ERR; | |
674 | } | |
675 | ||
676 | return SR_OK; | |
677 | } | |
abc4b335 | 678 | |
a0a4c0fb | 679 | #ifdef _WIN32 |
ba1949f5 | 680 | typedef HANDLE event_handle; |
a0a4c0fb | 681 | #else |
ba1949f5 | 682 | typedef int event_handle; |
a0a4c0fb | 683 | #endif |
ba1949f5 ML |
684 | |
685 | SR_PRIV int serial_source_add(struct sr_serial_dev_inst *serial, int events, | |
686 | int timeout, sr_receive_data_callback_t cb, void *cb_data) | |
687 | { | |
688 | enum sp_event mask = 0; | |
689 | unsigned int i; | |
690 | ||
691 | if (sp_new_event_set(&serial->event_set) != SP_OK) | |
692 | return SR_ERR; | |
693 | ||
694 | if (events & G_IO_IN) | |
695 | mask |= SP_EVENT_RX_READY; | |
696 | if (events & G_IO_OUT) | |
697 | mask |= SP_EVENT_TX_READY; | |
698 | if (events & G_IO_ERR) | |
699 | mask |= SP_EVENT_ERROR; | |
700 | ||
701 | if (sp_add_port_events(serial->event_set, serial->data, mask) != SP_OK) { | |
702 | sp_free_event_set(serial->event_set); | |
703 | return SR_ERR; | |
704 | } | |
705 | ||
706 | serial->pollfds = (GPollFD *) g_malloc0(sizeof(GPollFD) * serial->event_set->count); | |
707 | ||
708 | for (i = 0; i < serial->event_set->count; i++) { | |
709 | ||
710 | serial->pollfds[i].fd = ((event_handle *) serial->event_set->handles)[i]; | |
711 | ||
712 | mask = serial->event_set->masks[i]; | |
713 | ||
714 | if (mask & SP_EVENT_RX_READY) | |
715 | serial->pollfds[i].events |= G_IO_IN; | |
716 | if (mask & SP_EVENT_TX_READY) | |
717 | serial->pollfds[i].events |= G_IO_OUT; | |
718 | if (mask & SP_EVENT_ERROR) | |
719 | serial->pollfds[i].events |= G_IO_ERR; | |
720 | ||
721 | if (sr_session_source_add_pollfd(&serial->pollfds[i], | |
722 | timeout, cb, cb_data) != SR_OK) | |
723 | return SR_ERR; | |
724 | } | |
725 | ||
726 | return SR_OK; | |
abc4b335 | 727 | } |
7faa3e88 ML |
728 | |
729 | SR_PRIV int serial_source_remove(struct sr_serial_dev_inst *serial) | |
730 | { | |
ba1949f5 ML |
731 | unsigned int i; |
732 | ||
733 | for (i = 0; i < serial->event_set->count; i++) | |
734 | if (sr_session_source_remove_pollfd(&serial->pollfds[i]) != SR_OK) | |
735 | return SR_ERR; | |
736 | ||
737 | g_free(serial->pollfds); | |
738 | sp_free_event_set(serial->event_set); | |
739 | ||
740 | serial->pollfds = NULL; | |
741 | serial->event_set = NULL; | |
742 | ||
743 | return SR_OK; | |
7faa3e88 | 744 | } |