]>
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 BV |
376 | /** |
377 | * Set serial parameters for the specified serial port. | |
378 | * | |
379 | * @param serial Previously initialized serial port structure. | |
04cb9157 MH |
380 | * @param[in] paramstr A serial communication parameters string, in the form |
381 | * of \<speed\>/\<data bits\>\<parity\>\<stopbits\>\<flow\>, for example | |
382 | * "9600/8n1" or "600/7o2" or "460800/8n1/flow=2" where flow is 0 for none, | |
383 | * 1 for rts/cts and 2 for xon/xoff. | |
299bdb24 | 384 | * |
04cb9157 MH |
385 | * @retval SR_OK Success. |
386 | * @retval SR_ERR Failure. | |
299bdb24 | 387 | */ |
a970522b | 388 | #define SERIAL_COMM_SPEC "^(\\d+)/([5678])([neo])([12])(.*)$" |
299bdb24 BV |
389 | SR_PRIV int serial_set_paramstr(struct sr_serial_dev_inst *serial, |
390 | const char *paramstr) | |
792fc686 BV |
391 | { |
392 | GRegex *reg; | |
393 | GMatchInfo *match; | |
26ddb5ba | 394 | int speed, databits, parity, stopbits, flow, rts, dtr, i; |
71caaad4 | 395 | char *mstr, **opts, **kv; |
792fc686 | 396 | |
26ddb5ba | 397 | speed = databits = parity = stopbits = flow = 0; |
71caaad4 | 398 | rts = dtr = -1; |
ea088bb6 | 399 | sr_spew("Parsing parameters from \"%s\".", paramstr); |
792fc686 BV |
400 | reg = g_regex_new(SERIAL_COMM_SPEC, 0, 0, NULL); |
401 | if (g_regex_match(reg, paramstr, 0, &match)) { | |
402 | if ((mstr = g_match_info_fetch(match, 1))) | |
403 | speed = strtoul(mstr, NULL, 10); | |
404 | g_free(mstr); | |
405 | if ((mstr = g_match_info_fetch(match, 2))) | |
406 | databits = strtoul(mstr, NULL, 10); | |
407 | g_free(mstr); | |
408 | if ((mstr = g_match_info_fetch(match, 3))) { | |
409 | switch (mstr[0]) { | |
410 | case 'n': | |
411 | parity = SERIAL_PARITY_NONE; | |
412 | break; | |
413 | case 'e': | |
414 | parity = SERIAL_PARITY_EVEN; | |
415 | break; | |
416 | case 'o': | |
417 | parity = SERIAL_PARITY_ODD; | |
418 | break; | |
419 | } | |
420 | } | |
421 | g_free(mstr); | |
422 | if ((mstr = g_match_info_fetch(match, 4))) | |
423 | stopbits = strtoul(mstr, NULL, 10); | |
424 | g_free(mstr); | |
71caaad4 BV |
425 | if ((mstr = g_match_info_fetch(match, 5)) && mstr[0] != '\0') { |
426 | if (mstr[0] != '/') { | |
427 | sr_dbg("missing separator before extra options"); | |
428 | speed = 0; | |
429 | } else { | |
430 | /* A set of "key=value" options separated by / */ | |
431 | opts = g_strsplit(mstr + 1, "/", 0); | |
432 | for (i = 0; opts[i]; i++) { | |
433 | kv = g_strsplit(opts[i], "=", 2); | |
434 | if (!strncmp(kv[0], "rts", 3)) { | |
435 | if (kv[1][0] == '1') | |
436 | rts = 1; | |
437 | else if (kv[1][0] == '0') | |
438 | rts = 0; | |
439 | else { | |
440 | sr_dbg("invalid value for rts: %c", kv[1][0]); | |
441 | speed = 0; | |
442 | } | |
443 | } else if (!strncmp(kv[0], "dtr", 3)) { | |
444 | if (kv[1][0] == '1') | |
445 | dtr = 1; | |
446 | else if (kv[1][0] == '0') | |
447 | dtr = 0; | |
448 | else { | |
449 | sr_dbg("invalid value for dtr: %c", kv[1][0]); | |
450 | speed = 0; | |
451 | } | |
26ddb5ba | 452 | } else if (!strncmp(kv[0], "flow", 4)) { |
453 | if (kv[1][0] == '0') | |
454 | flow = 0; | |
455 | else if (kv[1][0] == '1') | |
456 | flow = 1; | |
457 | else if (kv[1][0] == '2') | |
458 | flow = 2; | |
459 | else { | |
460 | sr_dbg("invalid value for flow: %c", kv[1][0]); | |
461 | speed = 0; | |
462 | } | |
71caaad4 BV |
463 | } |
464 | g_strfreev(kv); | |
465 | } | |
466 | g_strfreev(opts); | |
467 | } | |
468 | } | |
469 | g_free(mstr); | |
792fc686 BV |
470 | } |
471 | g_match_info_unref(match); | |
472 | g_regex_unref(reg); | |
473 | ||
ea088bb6 AG |
474 | if (speed) { |
475 | return serial_set_params(serial, speed, databits, parity, | |
26ddb5ba | 476 | stopbits, flow, rts, dtr); |
ea088bb6 AG |
477 | } else { |
478 | sr_dbg("Could not infer speed from parameter string."); | |
792fc686 | 479 | return SR_ERR_ARG; |
ea088bb6 | 480 | } |
792fc686 BV |
481 | } |
482 | ||
299bdb24 BV |
483 | /** |
484 | * Read a line from the specified serial port. | |
485 | * | |
486 | * @param serial Previously initialized serial port structure. | |
487 | * @param buf Buffer where to store the bytes that are read. | |
488 | * @param buflen Size of the buffer. | |
04cb9157 | 489 | * @param[in] timeout_ms How long to wait for a line to come in. |
299bdb24 BV |
490 | * |
491 | * Reading stops when CR of LR is found, which is stripped from the buffer. | |
492 | * | |
04cb9157 MH |
493 | * @retval SR_OK Success. |
494 | * @retval SR_ERR Failure. | |
299bdb24 BV |
495 | */ |
496 | SR_PRIV int serial_readline(struct sr_serial_dev_inst *serial, char **buf, | |
497 | int *buflen, gint64 timeout_ms) | |
6f22a8ef | 498 | { |
b87f8504 | 499 | gint64 start; |
6f22a8ef UH |
500 | int maxlen, len; |
501 | ||
64ecf7ee | 502 | if (!serial) { |
299bdb24 BV |
503 | sr_dbg("Invalid serial port."); |
504 | return SR_ERR; | |
505 | } | |
506 | ||
64ecf7ee ML |
507 | if (!serial->data) { |
508 | sr_dbg("Cannot use unopened serial port %s.", serial->port); | |
299bdb24 BV |
509 | return -1; |
510 | } | |
511 | ||
6f22a8ef UH |
512 | timeout_ms *= 1000; |
513 | start = g_get_monotonic_time(); | |
514 | ||
515 | maxlen = *buflen; | |
516 | *buflen = len = 0; | |
517 | while(1) { | |
518 | len = maxlen - *buflen - 1; | |
519 | if (len < 1) | |
520 | break; | |
299bdb24 | 521 | len = serial_read(serial, *buf + *buflen, 1); |
6f22a8ef UH |
522 | if (len > 0) { |
523 | *buflen += len; | |
524 | *(*buf + *buflen) = '\0'; | |
318dd53c BV |
525 | if (*buflen > 0 && (*(*buf + *buflen - 1) == '\r' |
526 | || *(*buf + *buflen - 1) == '\n')) { | |
527 | /* Strip CR/LF and terminate. */ | |
6f22a8ef UH |
528 | *(*buf + --*buflen) = '\0'; |
529 | break; | |
530 | } | |
531 | } | |
532 | if (g_get_monotonic_time() - start > timeout_ms) | |
533 | /* Timeout */ | |
534 | break; | |
5715e84f DT |
535 | if (len < 1) |
536 | g_usleep(2000); | |
6f22a8ef | 537 | } |
318dd53c BV |
538 | if (*buflen) |
539 | sr_dbg("Received %d: '%s'.", *buflen, *buf); | |
6f22a8ef UH |
540 | |
541 | return SR_OK; | |
542 | } | |
766456be UH |
543 | |
544 | /** | |
545 | * Try to find a valid packet in a serial data stream. | |
546 | * | |
547 | * @param serial Previously initialized serial port structure. | |
548 | * @param buf Buffer containing the bytes to write. | |
04cb9157 MH |
549 | * @param buflen Size of the buffer. |
550 | * @param[in] packet_size Size, in bytes, of a valid packet. | |
766456be | 551 | * @param is_valid Callback that assesses whether the packet is valid or not. |
04cb9157 | 552 | * @param[in] timeout_ms The timeout after which, if no packet is detected, to |
766456be | 553 | * abort scanning. |
04cb9157 | 554 | * @param[in] baudrate The baudrate of the serial port. This parameter is not |
766456be UH |
555 | * critical, but it helps fine tune the serial port polling |
556 | * delay. | |
557 | * | |
04cb9157 MH |
558 | * @retval SR_OK Valid packet was found within the given timeout |
559 | * @retval SR_ERR Failure. | |
766456be UH |
560 | */ |
561 | SR_PRIV int serial_stream_detect(struct sr_serial_dev_inst *serial, | |
562 | uint8_t *buf, size_t *buflen, | |
563 | size_t packet_size, packet_valid_t is_valid, | |
564 | uint64_t timeout_ms, int baudrate) | |
565 | { | |
566 | uint64_t start, time, byte_delay_us; | |
567 | size_t ibuf, i, maxlen; | |
568 | int len; | |
569 | ||
570 | maxlen = *buflen; | |
571 | ||
64ecf7ee ML |
572 | sr_dbg("Detecting packets on %s (timeout = %" PRIu64 |
573 | "ms, baudrate = %d).", serial->port, timeout_ms, baudrate); | |
766456be UH |
574 | |
575 | if (maxlen < (packet_size / 2) ) { | |
576 | sr_err("Buffer size must be at least twice the packet size."); | |
577 | return SR_ERR; | |
578 | } | |
579 | ||
766456be UH |
580 | /* Assume 8n1 transmission. That is 10 bits for every byte. */ |
581 | byte_delay_us = 10 * (1000000 / baudrate); | |
582 | start = g_get_monotonic_time(); | |
583 | ||
584 | i = ibuf = len = 0; | |
585 | while (ibuf < maxlen) { | |
586 | len = serial_read(serial, &buf[ibuf], 1); | |
587 | if (len > 0) { | |
588 | ibuf += len; | |
589 | } else if (len == 0) { | |
006dbe55 | 590 | /* No logging, already done in serial_read(). */ |
766456be UH |
591 | } else { |
592 | /* Error reading byte, but continuing anyway. */ | |
593 | } | |
551c3d8c AG |
594 | |
595 | time = g_get_monotonic_time() - start; | |
596 | time /= 1000; | |
597 | ||
766456be UH |
598 | if ((ibuf - i) >= packet_size) { |
599 | /* We have at least a packet's worth of data. */ | |
600 | if (is_valid(&buf[i])) { | |
766456be UH |
601 | sr_spew("Found valid %d-byte packet after " |
602 | "%" PRIu64 "ms.", (ibuf - i), time); | |
603 | *buflen = ibuf; | |
604 | return SR_OK; | |
605 | } else { | |
606 | sr_spew("Got %d bytes, but not a valid " | |
607 | "packet.", (ibuf - i)); | |
608 | } | |
609 | /* Not a valid packet. Continue searching. */ | |
610 | i++; | |
611 | } | |
551c3d8c | 612 | if (time >= timeout_ms) { |
766456be | 613 | /* Timeout */ |
551c3d8c | 614 | sr_dbg("Detection timed out after %dms.", time); |
766456be UH |
615 | break; |
616 | } | |
5715e84f DT |
617 | if (len < 1) |
618 | g_usleep(byte_delay_us); | |
766456be UH |
619 | } |
620 | ||
621 | *buflen = ibuf; | |
622 | ||
623 | sr_err("Didn't find a valid packet (read %d bytes).", *buflen); | |
624 | ||
625 | return SR_ERR; | |
626 | } | |
1bd9e678 DJ |
627 | |
628 | /** | |
629 | * Extract the serial device and options from the options linked list. | |
630 | * | |
631 | * @param options List of options passed from the command line. | |
632 | * @param serial_device Pointer where to store the exctracted serial device. | |
633 | * @param serial_options Pointer where to store the optional extracted serial | |
634 | * options. | |
635 | * | |
636 | * @return SR_OK if a serial_device is found, SR_ERR if no device is found. The | |
637 | * returned string should not be freed by the caller. | |
638 | */ | |
639 | SR_PRIV int sr_serial_extract_options(GSList *options, const char **serial_device, | |
640 | const char **serial_options) | |
641 | { | |
642 | GSList *l; | |
643 | struct sr_config *src; | |
644 | ||
645 | *serial_device = NULL; | |
646 | ||
647 | for (l = options; l; l = l->next) { | |
648 | src = l->data; | |
649 | switch (src->key) { | |
650 | case SR_CONF_CONN: | |
651 | *serial_device = g_variant_get_string(src->data, NULL); | |
652 | sr_dbg("Parsed serial device: %s", *serial_device); | |
653 | break; | |
654 | ||
655 | case SR_CONF_SERIALCOMM: | |
656 | *serial_options = g_variant_get_string(src->data, NULL); | |
657 | sr_dbg("Parsed serial options: %s", *serial_options); | |
658 | break; | |
659 | } | |
660 | } | |
661 | ||
662 | if (!*serial_device) { | |
663 | sr_dbg("No serial device specified"); | |
664 | return SR_ERR; | |
665 | } | |
666 | ||
667 | return SR_OK; | |
668 | } | |
abc4b335 | 669 | |
a0a4c0fb | 670 | #ifdef _WIN32 |
ba1949f5 | 671 | typedef HANDLE event_handle; |
a0a4c0fb | 672 | #else |
ba1949f5 | 673 | typedef int event_handle; |
a0a4c0fb | 674 | #endif |
ba1949f5 ML |
675 | |
676 | SR_PRIV int serial_source_add(struct sr_serial_dev_inst *serial, int events, | |
677 | int timeout, sr_receive_data_callback_t cb, void *cb_data) | |
678 | { | |
679 | enum sp_event mask = 0; | |
680 | unsigned int i; | |
681 | ||
682 | if (sp_new_event_set(&serial->event_set) != SP_OK) | |
683 | return SR_ERR; | |
684 | ||
685 | if (events & G_IO_IN) | |
686 | mask |= SP_EVENT_RX_READY; | |
687 | if (events & G_IO_OUT) | |
688 | mask |= SP_EVENT_TX_READY; | |
689 | if (events & G_IO_ERR) | |
690 | mask |= SP_EVENT_ERROR; | |
691 | ||
692 | if (sp_add_port_events(serial->event_set, serial->data, mask) != SP_OK) { | |
693 | sp_free_event_set(serial->event_set); | |
694 | return SR_ERR; | |
695 | } | |
696 | ||
697 | serial->pollfds = (GPollFD *) g_malloc0(sizeof(GPollFD) * serial->event_set->count); | |
698 | ||
699 | for (i = 0; i < serial->event_set->count; i++) { | |
700 | ||
701 | serial->pollfds[i].fd = ((event_handle *) serial->event_set->handles)[i]; | |
702 | ||
703 | mask = serial->event_set->masks[i]; | |
704 | ||
705 | if (mask & SP_EVENT_RX_READY) | |
706 | serial->pollfds[i].events |= G_IO_IN; | |
707 | if (mask & SP_EVENT_TX_READY) | |
708 | serial->pollfds[i].events |= G_IO_OUT; | |
709 | if (mask & SP_EVENT_ERROR) | |
710 | serial->pollfds[i].events |= G_IO_ERR; | |
711 | ||
712 | if (sr_session_source_add_pollfd(&serial->pollfds[i], | |
713 | timeout, cb, cb_data) != SR_OK) | |
714 | return SR_ERR; | |
715 | } | |
716 | ||
717 | return SR_OK; | |
abc4b335 | 718 | } |
7faa3e88 ML |
719 | |
720 | SR_PRIV int serial_source_remove(struct sr_serial_dev_inst *serial) | |
721 | { | |
ba1949f5 ML |
722 | unsigned int i; |
723 | ||
724 | for (i = 0; i < serial->event_set->count; i++) | |
725 | if (sr_session_source_remove_pollfd(&serial->pollfds[i]) != SR_OK) | |
726 | return SR_ERR; | |
727 | ||
728 | g_free(serial->pollfds); | |
729 | sp_free_event_set(serial->event_set); | |
730 | ||
731 | serial->pollfds = NULL; | |
732 | serial->event_set = NULL; | |
733 | ||
734 | return SR_OK; | |
7faa3e88 | 735 | } |