]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com> | |
5 | * Copyright (C) 2010-2012 Uwe Hermann <uwe@hermann-uwe.de> | |
6 | * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com> | |
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 | ||
22 | #include <string.h> | |
23 | #include <sys/types.h> | |
24 | #include <sys/stat.h> | |
25 | #include <fcntl.h> | |
26 | #include <unistd.h> | |
27 | #ifdef _WIN32 | |
28 | #include <windows.h> | |
29 | #else | |
30 | #include <glob.h> | |
31 | #include <termios.h> | |
32 | #include <sys/ioctl.h> | |
33 | #endif | |
34 | #include <stdlib.h> | |
35 | #include <errno.h> | |
36 | #include <glib.h> | |
37 | #include "libsigrok.h" | |
38 | #include "libsigrok-internal.h" | |
39 | ||
40 | /* Message logging helpers with driver-specific prefix string. */ | |
41 | #define DRIVER_LOG_DOMAIN "serial: " | |
42 | #define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args) | |
43 | #define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args) | |
44 | #define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args) | |
45 | #define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args) | |
46 | #define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args) | |
47 | #define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args) | |
48 | ||
49 | // FIXME: Must be moved, or rather passed as function argument. | |
50 | #ifdef _WIN32 | |
51 | static HANDLE hdl; | |
52 | #endif | |
53 | ||
54 | /** | |
55 | * Open the specified serial port. | |
56 | * | |
57 | * @param serial Previously initialized serial port structure. | |
58 | * @param flags Flags to use when opening the serial port. Possible flags | |
59 | * include SERIAL_RDWR, SERIAL_RDONLY, SERIAL_NONBLOCK. | |
60 | * | |
61 | * If the serial structure contains a serialcomm string, it will be | |
62 | * passed to serial_set_paramstr() after the port is opened. | |
63 | * | |
64 | * @return SR_OK on success, SR_ERR on failure. | |
65 | */ | |
66 | SR_PRIV int serial_open(struct sr_serial_dev_inst *serial, int flags) | |
67 | { | |
68 | int flags_local = 0; | |
69 | #ifdef _WIN32 | |
70 | DWORD desired_access = 0, flags_and_attributes = 0; | |
71 | #endif | |
72 | ||
73 | if (!serial) { | |
74 | sr_dbg("Invalid serial port."); | |
75 | return SR_ERR; | |
76 | } | |
77 | ||
78 | sr_spew("Opening serial port '%s' (flags %d).", serial->port, flags); | |
79 | ||
80 | #ifdef _WIN32 | |
81 | /* Map 'flags' to the OS-specific settings. */ | |
82 | desired_access |= GENERIC_READ; | |
83 | flags_and_attributes = FILE_ATTRIBUTE_NORMAL; | |
84 | if (flags & SERIAL_RDWR) | |
85 | desired_access |= GENERIC_WRITE; | |
86 | if (flags & SERIAL_NONBLOCK) | |
87 | flags_and_attributes |= FILE_FLAG_OVERLAPPED; | |
88 | ||
89 | hdl = CreateFile(serial->port, desired_access, 0, 0, | |
90 | OPEN_EXISTING, flags_and_attributes, 0); | |
91 | if (hdl == INVALID_HANDLE_VALUE) { | |
92 | sr_err("Error opening serial port '%s'.", serial->port); | |
93 | return SR_ERR; | |
94 | } | |
95 | #else | |
96 | /* Map 'flags' to the OS-specific settings. */ | |
97 | if (flags & SERIAL_RDWR) | |
98 | flags_local |= O_RDWR; | |
99 | if (flags & SERIAL_RDONLY) | |
100 | flags_local |= O_RDONLY; | |
101 | if (flags & SERIAL_NONBLOCK) | |
102 | flags_local |= O_NONBLOCK; | |
103 | ||
104 | if ((serial->fd = open(serial->port, flags_local)) < 0) { | |
105 | sr_err("Error opening serial port '%s': %s.", serial->port, | |
106 | strerror(errno)); | |
107 | return SR_ERR; | |
108 | } | |
109 | ||
110 | sr_spew("Opened serial port '%s' (fd %d).", serial->port, serial->fd); | |
111 | #endif | |
112 | ||
113 | if (serial->serialcomm) | |
114 | return serial_set_paramstr(serial, serial->serialcomm); | |
115 | else | |
116 | return SR_OK; | |
117 | } | |
118 | ||
119 | /** | |
120 | * Close the specified serial port. | |
121 | * | |
122 | * @param serial Previously initialized serial port structure. | |
123 | * | |
124 | * @return SR_OK on success, SR_ERR on failure. | |
125 | */ | |
126 | SR_PRIV int serial_close(struct sr_serial_dev_inst *serial) | |
127 | { | |
128 | int ret; | |
129 | ||
130 | if (!serial) { | |
131 | sr_dbg("Invalid serial port."); | |
132 | return SR_ERR; | |
133 | } | |
134 | ||
135 | if (serial->fd == -1) { | |
136 | sr_dbg("Cannot close unopened serial port %s (fd %d).", | |
137 | serial->port, serial->fd); | |
138 | return SR_ERR; | |
139 | } | |
140 | ||
141 | sr_spew("Closing serial port %s (fd %d).", serial->port, serial->fd); | |
142 | ret = SR_OK; | |
143 | ||
144 | #ifdef _WIN32 | |
145 | /* Returns non-zero upon success, 0 upon failure. */ | |
146 | if (CloseHandle(hdl) == 0) | |
147 | ret = SR_ERR; | |
148 | #else | |
149 | /* Returns 0 upon success, -1 upon failure. */ | |
150 | if (close(serial->fd) < 0) { | |
151 | sr_err("Error closing serial port: %s (fd %d).", strerror(errno), | |
152 | serial->fd); | |
153 | ret = SR_ERR; | |
154 | } | |
155 | #endif | |
156 | ||
157 | serial->fd = -1; | |
158 | ||
159 | return ret; | |
160 | } | |
161 | ||
162 | /** | |
163 | * Flush serial port buffers. | |
164 | * | |
165 | * @param serial Previously initialized serial port structure. | |
166 | * | |
167 | * @return SR_OK on success, SR_ERR on failure. | |
168 | */ | |
169 | SR_PRIV int serial_flush(struct sr_serial_dev_inst *serial) | |
170 | { | |
171 | int ret; | |
172 | ||
173 | if (!serial) { | |
174 | sr_dbg("Invalid serial port."); | |
175 | return SR_ERR; | |
176 | } | |
177 | ||
178 | if (serial->fd == -1) { | |
179 | sr_dbg("Cannot flush unopened serial port %s (fd %d).", | |
180 | serial->port, serial->fd); | |
181 | return SR_ERR; | |
182 | } | |
183 | ||
184 | sr_spew("Flushing serial port %s (fd %d).", serial->port, serial->fd); | |
185 | ret = SR_OK; | |
186 | ||
187 | #ifdef _WIN32 | |
188 | /* Returns non-zero upon success, 0 upon failure. */ | |
189 | if (PurgeComm(hdl, PURGE_RXCLEAR | PURGE_TXCLEAR) == 0) { | |
190 | sr_err("Error flushing serial port: %s.", strerror(errno)); | |
191 | ret = SR_ERR; | |
192 | } | |
193 | #else | |
194 | /* Returns 0 upon success, -1 upon failure. */ | |
195 | if (tcflush(serial->fd, TCIOFLUSH) < 0) { | |
196 | sr_err("Error flushing serial port: %s.", strerror(errno)); | |
197 | ret = SR_ERR; | |
198 | } | |
199 | ||
200 | return ret; | |
201 | #endif | |
202 | } | |
203 | ||
204 | /** | |
205 | * Write a number of bytes to the specified serial port. | |
206 | * | |
207 | * @param serial Previously initialized serial port structure. | |
208 | * @param buf Buffer containing the bytes to write. | |
209 | * @param count Number of bytes to write. | |
210 | * | |
211 | * @return The number of bytes written, or -1 upon failure. | |
212 | */ | |
213 | SR_PRIV int serial_write(struct sr_serial_dev_inst *serial, | |
214 | const void *buf, size_t count) | |
215 | { | |
216 | ssize_t ret; | |
217 | ||
218 | if (!serial) { | |
219 | sr_dbg("Invalid serial port."); | |
220 | return -1; | |
221 | } | |
222 | ||
223 | if (serial->fd == -1) { | |
224 | sr_dbg("Cannot use unopened serial port %s (fd %d).", | |
225 | serial->port, serial->fd); | |
226 | return -1; | |
227 | } | |
228 | ||
229 | #ifdef _WIN32 | |
230 | DWORD tmp = 0; | |
231 | ||
232 | /* FIXME */ | |
233 | /* Returns non-zero upon success, 0 upon failure. */ | |
234 | WriteFile(hdl, buf, count, &tmp, NULL); | |
235 | #else | |
236 | /* Returns the number of bytes written, or -1 upon failure. */ | |
237 | ret = write(serial->fd, buf, count); | |
238 | if (ret < 0) | |
239 | sr_err("Write error: %s.", strerror(errno)); | |
240 | else | |
241 | sr_spew("Wrote %d/%d bytes (fd %d).", ret, count, serial->fd); | |
242 | #endif | |
243 | ||
244 | return ret; | |
245 | } | |
246 | ||
247 | /** | |
248 | * Read a number of bytes from the specified serial port. | |
249 | * | |
250 | * @param serial Previously initialized serial port structure. | |
251 | * @param buf Buffer where to store the bytes that are read. | |
252 | * @param count The number of bytes to read. | |
253 | * | |
254 | * @return The number of bytes read, or -1 upon failure. | |
255 | */ | |
256 | SR_PRIV int serial_read(struct sr_serial_dev_inst *serial, void *buf, | |
257 | size_t count) | |
258 | { | |
259 | ssize_t ret; | |
260 | ||
261 | if (!serial) { | |
262 | sr_dbg("Invalid serial port."); | |
263 | return -1; | |
264 | } | |
265 | ||
266 | if (serial->fd == -1) { | |
267 | sr_dbg("Cannot use unopened serial port %s (fd %d).", | |
268 | serial->port, serial->fd); | |
269 | return -1; | |
270 | } | |
271 | ||
272 | #ifdef _WIN32 | |
273 | DWORD tmp = 0; | |
274 | ||
275 | /* FIXME */ | |
276 | /* Returns non-zero upon success, 0 upon failure. */ | |
277 | return ReadFile(hdl, buf, count, &tmp, NULL); | |
278 | #else | |
279 | /* Returns the number of bytes read, or -1 upon failure. */ | |
280 | ret = read(serial->fd, buf, count); | |
281 | if (ret < 0) | |
282 | /* | |
283 | * Should be sr_err(), but that would yield lots of | |
284 | * "Resource temporarily unavailable" messages. | |
285 | */ | |
286 | sr_spew("Read error: %s (fd %d).", strerror(errno), serial->fd); | |
287 | else | |
288 | sr_spew("Read %d/%d bytes (fd %d).", ret, count, serial->fd); | |
289 | #endif | |
290 | ||
291 | return ret; | |
292 | } | |
293 | ||
294 | /** | |
295 | * Set serial parameters for the specified serial port. | |
296 | * | |
297 | * @param serial Previously initialized serial port structure. | |
298 | * @param baudrate The baudrate to set. | |
299 | * @param bits The number of data bits to use. | |
300 | * @param parity The parity setting to use (0 = none, 1 = even, 2 = odd). | |
301 | * @param stopbits The number of stop bits to use (1 or 2). | |
302 | * @param flowcontrol The flow control settings to use (0 = none, 1 = RTS/CTS, | |
303 | * 2 = XON/XOFF). | |
304 | * | |
305 | * @return SR_OK upon success, SR_ERR upon failure. | |
306 | */ | |
307 | SR_PRIV int serial_set_params(struct sr_serial_dev_inst *serial, int baudrate, | |
308 | int bits, int parity, int stopbits, | |
309 | int flowcontrol, int rts, int dtr) | |
310 | { | |
311 | if (!serial) { | |
312 | sr_dbg("Invalid serial port."); | |
313 | return SR_ERR; | |
314 | } | |
315 | ||
316 | if (serial->fd == -1) { | |
317 | sr_dbg("Cannot configure unopened serial port %s (fd %d).", | |
318 | serial->port, serial->fd); | |
319 | return SR_ERR; | |
320 | } | |
321 | ||
322 | sr_spew("Setting serial parameters on port %s (fd %d).", serial->port, | |
323 | serial->fd); | |
324 | ||
325 | #ifdef _WIN32 | |
326 | DCB dcb; | |
327 | ||
328 | if (!GetCommState(hdl, &dcb)) { | |
329 | sr_err("Failed to get comm state on port %s (fd %d): %d.", | |
330 | serial->port, serial->fd, GetLastError()); | |
331 | return SR_ERR; | |
332 | } | |
333 | ||
334 | switch (baudrate) { | |
335 | /* | |
336 | * The baudrates 50/75/134/150/200/1800/230400/460800 do not seem to | |
337 | * have documented CBR_* macros. | |
338 | */ | |
339 | case 110: | |
340 | dcb.BaudRate = CBR_110; | |
341 | break; | |
342 | case 300: | |
343 | dcb.BaudRate = CBR_300; | |
344 | break; | |
345 | case 600: | |
346 | dcb.BaudRate = CBR_600; | |
347 | break; | |
348 | case 1200: | |
349 | dcb.BaudRate = CBR_1200; | |
350 | break; | |
351 | case 2400: | |
352 | dcb.BaudRate = CBR_2400; | |
353 | break; | |
354 | case 4800: | |
355 | dcb.BaudRate = CBR_4800; | |
356 | break; | |
357 | case 9600: | |
358 | dcb.BaudRate = CBR_9600; | |
359 | break; | |
360 | case 14400: | |
361 | dcb.BaudRate = CBR_14400; /* Not available on Unix? */ | |
362 | break; | |
363 | case 19200: | |
364 | dcb.BaudRate = CBR_19200; | |
365 | break; | |
366 | case 38400: | |
367 | dcb.BaudRate = CBR_38400; | |
368 | break; | |
369 | case 57600: | |
370 | dcb.BaudRate = CBR_57600; | |
371 | break; | |
372 | case 115200: | |
373 | dcb.BaudRate = CBR_115200; | |
374 | break; | |
375 | case 128000: | |
376 | dcb.BaudRate = CBR_128000; /* Not available on Unix? */ | |
377 | break; | |
378 | case 256000: | |
379 | dcb.BaudRate = CBR_256000; /* Not available on Unix? */ | |
380 | break; | |
381 | default: | |
382 | sr_err("Unsupported baudrate: %d.", baudrate); | |
383 | return SR_ERR; | |
384 | } | |
385 | sr_spew("Configuring baudrate to %d (%d).", baudrate, dcb.BaudRate); | |
386 | ||
387 | sr_spew("Configuring %d data bits.", bits); | |
388 | dcb.ByteSize = bits; | |
389 | ||
390 | sr_spew("Configuring %d stop bits.", stopbits); | |
391 | switch (stopbits) { | |
392 | /* Note: There's also ONE5STOPBITS == 1.5 (unneeded so far). */ | |
393 | case 1: | |
394 | dcb.StopBits = ONESTOPBIT; | |
395 | break; | |
396 | case 2: | |
397 | dcb.StopBits = TWOSTOPBITS; | |
398 | break; | |
399 | default: | |
400 | sr_err("Unsupported stopbits number: %d.", stopbits); | |
401 | return SR_ERR; | |
402 | } | |
403 | ||
404 | switch (parity) { | |
405 | /* Note: There's also SPACEPARITY, MARKPARITY (unneeded so far). */ | |
406 | case SERIAL_PARITY_NONE: | |
407 | sr_spew("Configuring no parity."); | |
408 | dcb.Parity = NOPARITY; | |
409 | break; | |
410 | case SERIAL_PARITY_EVEN: | |
411 | sr_spew("Configuring even parity."); | |
412 | dcb.Parity = EVENPARITY; | |
413 | break; | |
414 | case SERIAL_PARITY_ODD: | |
415 | sr_spew("Configuring odd parity."); | |
416 | dcb.Parity = ODDPARITY; | |
417 | break; | |
418 | default: | |
419 | sr_err("Unsupported parity setting: %d.", parity); | |
420 | return SR_ERR; | |
421 | } | |
422 | ||
423 | if (rts != -1) { | |
424 | sr_spew("Setting RTS %s.", rts ? "high" : "low"); | |
425 | if (rts) | |
426 | dcb.fRtsControl = RTS_CONTROL_ENABLE; | |
427 | else | |
428 | dcb.fRtsControl = RTS_CONTROL_DISABLE; | |
429 | } | |
430 | ||
431 | if (dtr != -1) { | |
432 | sr_spew("Setting DTR %s.", dtr ? "high" : "low"); | |
433 | if (rts) | |
434 | dcb.fDtrControl = DTR_CONTROL_ENABLE; | |
435 | else | |
436 | dcb.fDtrControl = DTR_CONTROL_DISABLE; | |
437 | } | |
438 | ||
439 | if (!SetCommState(hdl, &dcb)) { | |
440 | sr_err("Failed to set comm state on port %s (fd %d): %d.", | |
441 | serial->port, serial->fd, GetLastError()); | |
442 | return SR_ERR; | |
443 | } | |
444 | #else | |
445 | struct termios term; | |
446 | speed_t baud; | |
447 | int ret, controlbits; | |
448 | ||
449 | if (tcgetattr(serial->fd, &term) < 0) { | |
450 | sr_err("tcgetattr() error on port %s (fd %d): %s.", | |
451 | serial->port, serial->fd, strerror(errno)); | |
452 | return SR_ERR; | |
453 | } | |
454 | ||
455 | switch (baudrate) { | |
456 | case 50: | |
457 | baud = B50; | |
458 | break; | |
459 | case 75: | |
460 | baud = B75; | |
461 | break; | |
462 | case 110: | |
463 | baud = B110; | |
464 | break; | |
465 | case 134: | |
466 | baud = B134; | |
467 | break; | |
468 | case 150: | |
469 | baud = B150; | |
470 | break; | |
471 | case 200: | |
472 | baud = B200; | |
473 | break; | |
474 | case 300: | |
475 | baud = B300; | |
476 | break; | |
477 | case 600: | |
478 | baud = B600; | |
479 | break; | |
480 | case 1200: | |
481 | baud = B1200; | |
482 | break; | |
483 | case 1800: | |
484 | baud = B1800; | |
485 | break; | |
486 | case 2400: | |
487 | baud = B2400; | |
488 | break; | |
489 | case 4800: | |
490 | baud = B4800; | |
491 | break; | |
492 | case 9600: | |
493 | baud = B9600; | |
494 | break; | |
495 | case 19200: | |
496 | baud = B19200; | |
497 | break; | |
498 | case 38400: | |
499 | baud = B38400; | |
500 | break; | |
501 | case 57600: | |
502 | baud = B57600; | |
503 | break; | |
504 | case 115200: | |
505 | baud = B115200; | |
506 | break; | |
507 | case 230400: | |
508 | baud = B230400; | |
509 | break; | |
510 | #ifndef __APPLE__ | |
511 | case 460800: | |
512 | baud = B460800; | |
513 | break; | |
514 | #endif | |
515 | default: | |
516 | sr_err("Unsupported baudrate: %d.", baudrate); | |
517 | return SR_ERR; | |
518 | } | |
519 | ||
520 | sr_spew("Configuring output baudrate to %d (%d).", baudrate, baud); | |
521 | if (cfsetospeed(&term, baud) < 0) { | |
522 | sr_err("cfsetospeed() error: %s.", strerror(errno)); | |
523 | return SR_ERR; | |
524 | } | |
525 | ||
526 | sr_spew("Configuring input baudrate to %d (%d).", baudrate, baud); | |
527 | if (cfsetispeed(&term, baud) < 0) { | |
528 | sr_err("cfsetispeed() error: %s.", strerror(errno)); | |
529 | return SR_ERR; | |
530 | } | |
531 | ||
532 | sr_spew("Configuring %d data bits.", bits); | |
533 | term.c_cflag &= ~CSIZE; | |
534 | switch (bits) { | |
535 | case 8: | |
536 | term.c_cflag |= CS8; | |
537 | break; | |
538 | case 7: | |
539 | term.c_cflag |= CS7; | |
540 | break; | |
541 | default: | |
542 | sr_err("Unsupported data bits number %d.", bits); | |
543 | return SR_ERR; | |
544 | } | |
545 | ||
546 | sr_spew("Configuring %d stop bits.", stopbits); | |
547 | term.c_cflag &= ~CSTOPB; | |
548 | switch (stopbits) { | |
549 | case 1: | |
550 | term.c_cflag &= ~CSTOPB; | |
551 | break; | |
552 | case 2: | |
553 | term.c_cflag |= CSTOPB; | |
554 | break; | |
555 | default: | |
556 | sr_err("Unsupported stopbits number %d.", stopbits); | |
557 | return SR_ERR; | |
558 | } | |
559 | ||
560 | term.c_iflag &= ~(IXON | IXOFF); | |
561 | term.c_cflag &= ~CRTSCTS; | |
562 | switch (flowcontrol) { | |
563 | case 0: | |
564 | /* No flow control. */ | |
565 | sr_spew("Configuring no flow control."); | |
566 | break; | |
567 | case 1: | |
568 | sr_spew("Configuring RTS/CTS flow control."); | |
569 | term.c_cflag |= CRTSCTS; | |
570 | break; | |
571 | case 2: | |
572 | sr_spew("Configuring XON/XOFF flow control."); | |
573 | term.c_iflag |= IXON | IXOFF; | |
574 | break; | |
575 | default: | |
576 | sr_err("Unsupported flow control setting %d.", flowcontrol); | |
577 | return SR_ERR; | |
578 | } | |
579 | ||
580 | term.c_iflag &= ~IGNPAR; | |
581 | term.c_cflag &= ~(PARODD | PARENB); | |
582 | switch (parity) { | |
583 | case SERIAL_PARITY_NONE: | |
584 | sr_spew("Configuring no parity."); | |
585 | term.c_iflag |= IGNPAR; | |
586 | break; | |
587 | case SERIAL_PARITY_EVEN: | |
588 | sr_spew("Configuring even parity."); | |
589 | term.c_cflag |= PARENB; | |
590 | break; | |
591 | case SERIAL_PARITY_ODD: | |
592 | sr_spew("Configuring odd parity."); | |
593 | term.c_cflag |= PARENB | PARODD; | |
594 | break; | |
595 | default: | |
596 | sr_err("Unsupported parity setting %d.", parity); | |
597 | return SR_ERR; | |
598 | } | |
599 | ||
600 | /* Do not translate carriage return to newline on input. */ | |
601 | term.c_iflag &= ~(ICRNL); | |
602 | ||
603 | /* Disable canonical mode, and don't echo input characters. */ | |
604 | term.c_lflag &= ~(ICANON | ECHO); | |
605 | ||
606 | /* Write the configured settings. */ | |
607 | if (tcsetattr(serial->fd, TCSADRAIN, &term) < 0) { | |
608 | sr_err("tcsetattr() error: %s.", strerror(errno)); | |
609 | return SR_ERR; | |
610 | } | |
611 | ||
612 | if (rts != -1) { | |
613 | sr_spew("Setting RTS %s.", rts ? "high" : "low"); | |
614 | controlbits = TIOCM_RTS; | |
615 | if ((ret = ioctl(serial->fd, rts ? TIOCMBIS : TIOCMBIC, | |
616 | &controlbits)) < 0) { | |
617 | sr_err("Error setting RTS: %s.", strerror(errno)); | |
618 | return SR_ERR; | |
619 | } | |
620 | } | |
621 | ||
622 | if (dtr != -1) { | |
623 | sr_spew("Setting DTR %s.", dtr ? "high" : "low"); | |
624 | controlbits = TIOCM_DTR; | |
625 | if ((ret = ioctl(serial->fd, dtr ? TIOCMBIS : TIOCMBIC, | |
626 | &controlbits)) < 0) { | |
627 | sr_err("Error setting DTR: %s.", strerror(errno)); | |
628 | return SR_ERR; | |
629 | } | |
630 | } | |
631 | ||
632 | #endif | |
633 | ||
634 | return SR_OK; | |
635 | } | |
636 | ||
637 | /** | |
638 | * Set serial parameters for the specified serial port. | |
639 | * | |
640 | * @param serial Previously initialized serial port structure. | |
641 | * @param paramstr A serial communication parameters string, in the form | |
642 | * of <speed>/<data bits><parity><stopbits>, for example "9600/8n1" or | |
643 | * "600/7o2". | |
644 | * | |
645 | * @return SR_OK upon success, SR_ERR upon failure. | |
646 | */ | |
647 | #define SERIAL_COMM_SPEC "^(\\d+)/([78])([neo])([12])(.*)$" | |
648 | SR_PRIV int serial_set_paramstr(struct sr_serial_dev_inst *serial, | |
649 | const char *paramstr) | |
650 | { | |
651 | GRegex *reg; | |
652 | GMatchInfo *match; | |
653 | int speed, databits, parity, stopbits, rts, dtr, i; | |
654 | char *mstr, **opts, **kv; | |
655 | ||
656 | speed = databits = parity = stopbits = 0; | |
657 | rts = dtr = -1; | |
658 | sr_spew("Parsing parameters from \"%s\".", paramstr); | |
659 | reg = g_regex_new(SERIAL_COMM_SPEC, 0, 0, NULL); | |
660 | if (g_regex_match(reg, paramstr, 0, &match)) { | |
661 | if ((mstr = g_match_info_fetch(match, 1))) | |
662 | speed = strtoul(mstr, NULL, 10); | |
663 | g_free(mstr); | |
664 | if ((mstr = g_match_info_fetch(match, 2))) | |
665 | databits = strtoul(mstr, NULL, 10); | |
666 | g_free(mstr); | |
667 | if ((mstr = g_match_info_fetch(match, 3))) { | |
668 | switch (mstr[0]) { | |
669 | case 'n': | |
670 | parity = SERIAL_PARITY_NONE; | |
671 | break; | |
672 | case 'e': | |
673 | parity = SERIAL_PARITY_EVEN; | |
674 | break; | |
675 | case 'o': | |
676 | parity = SERIAL_PARITY_ODD; | |
677 | break; | |
678 | } | |
679 | } | |
680 | g_free(mstr); | |
681 | if ((mstr = g_match_info_fetch(match, 4))) | |
682 | stopbits = strtoul(mstr, NULL, 10); | |
683 | g_free(mstr); | |
684 | if ((mstr = g_match_info_fetch(match, 5)) && mstr[0] != '\0') { | |
685 | if (mstr[0] != '/') { | |
686 | sr_dbg("missing separator before extra options"); | |
687 | speed = 0; | |
688 | } else { | |
689 | /* A set of "key=value" options separated by / */ | |
690 | opts = g_strsplit(mstr + 1, "/", 0); | |
691 | for (i = 0; opts[i]; i++) { | |
692 | kv = g_strsplit(opts[i], "=", 2); | |
693 | if (!strncmp(kv[0], "rts", 3)) { | |
694 | if (kv[1][0] == '1') | |
695 | rts = 1; | |
696 | else if (kv[1][0] == '0') | |
697 | rts = 0; | |
698 | else { | |
699 | sr_dbg("invalid value for rts: %c", kv[1][0]); | |
700 | speed = 0; | |
701 | } | |
702 | } else if (!strncmp(kv[0], "dtr", 3)) { | |
703 | if (kv[1][0] == '1') | |
704 | dtr = 1; | |
705 | else if (kv[1][0] == '0') | |
706 | dtr = 0; | |
707 | else { | |
708 | sr_dbg("invalid value for dtr: %c", kv[1][0]); | |
709 | speed = 0; | |
710 | } | |
711 | } | |
712 | g_strfreev(kv); | |
713 | } | |
714 | g_strfreev(opts); | |
715 | } | |
716 | } | |
717 | g_free(mstr); | |
718 | } | |
719 | g_match_info_unref(match); | |
720 | g_regex_unref(reg); | |
721 | ||
722 | if (speed) { | |
723 | return serial_set_params(serial, speed, databits, parity, | |
724 | stopbits, 0, rts, dtr); | |
725 | } else { | |
726 | sr_dbg("Could not infer speed from parameter string."); | |
727 | return SR_ERR_ARG; | |
728 | } | |
729 | } | |
730 | ||
731 | /** | |
732 | * Read a line from the specified serial port. | |
733 | * | |
734 | * @param serial Previously initialized serial port structure. | |
735 | * @param buf Buffer where to store the bytes that are read. | |
736 | * @param buflen Size of the buffer. | |
737 | * @param timeout_ms How long to wait for a line to come in. | |
738 | * | |
739 | * Reading stops when CR of LR is found, which is stripped from the buffer. | |
740 | * | |
741 | * @return SR_OK on success, SR_ERR on failure. | |
742 | */ | |
743 | SR_PRIV int serial_readline(struct sr_serial_dev_inst *serial, char **buf, | |
744 | int *buflen, gint64 timeout_ms) | |
745 | { | |
746 | gint64 start; | |
747 | int maxlen, len; | |
748 | ||
749 | if (!serial || serial->fd == -1) { | |
750 | sr_dbg("Invalid serial port."); | |
751 | return SR_ERR; | |
752 | } | |
753 | ||
754 | if (serial->fd == -1) { | |
755 | sr_dbg("Cannot use unopened serial port %s (fd %d).", | |
756 | serial->port, serial->fd); | |
757 | return -1; | |
758 | } | |
759 | ||
760 | timeout_ms *= 1000; | |
761 | start = g_get_monotonic_time(); | |
762 | ||
763 | maxlen = *buflen; | |
764 | *buflen = len = 0; | |
765 | while(1) { | |
766 | len = maxlen - *buflen - 1; | |
767 | if (len < 1) | |
768 | break; | |
769 | len = serial_read(serial, *buf + *buflen, 1); | |
770 | if (len > 0) { | |
771 | *buflen += len; | |
772 | *(*buf + *buflen) = '\0'; | |
773 | if (*buflen > 0 && (*(*buf + *buflen - 1) == '\r' | |
774 | || *(*buf + *buflen - 1) == '\n')) { | |
775 | /* Strip CR/LF and terminate. */ | |
776 | *(*buf + --*buflen) = '\0'; | |
777 | break; | |
778 | } | |
779 | } | |
780 | if (g_get_monotonic_time() - start > timeout_ms) | |
781 | /* Timeout */ | |
782 | break; | |
783 | g_usleep(2000); | |
784 | } | |
785 | if (*buflen) | |
786 | sr_dbg("Received %d: '%s'.", *buflen, *buf); | |
787 | ||
788 | return SR_OK; | |
789 | } | |
790 | ||
791 | /** | |
792 | * Try to find a valid packet in a serial data stream. | |
793 | * | |
794 | * @param serial Previously initialized serial port structure. | |
795 | * @param buf Buffer containing the bytes to write. | |
796 | * @param count Size of the buffer. | |
797 | * @param packet_size Size, in bytes, of a valid packet. | |
798 | * @param is_valid Callback that assesses whether the packet is valid or not. | |
799 | * @param timeout_ms The timeout after which, if no packet is detected, to | |
800 | * abort scanning. | |
801 | * @param baudrate The baudrate of the serial port. This parameter is not | |
802 | * critical, but it helps fine tune the serial port polling | |
803 | * delay. | |
804 | * | |
805 | * @return SR_OK if a valid packet is found within the given timeout, | |
806 | * SR_ERR upon failure. | |
807 | */ | |
808 | SR_PRIV int serial_stream_detect(struct sr_serial_dev_inst *serial, | |
809 | uint8_t *buf, size_t *buflen, | |
810 | size_t packet_size, packet_valid_t is_valid, | |
811 | uint64_t timeout_ms, int baudrate) | |
812 | { | |
813 | uint64_t start, time, byte_delay_us; | |
814 | size_t ibuf, i, maxlen; | |
815 | int len; | |
816 | ||
817 | maxlen = *buflen; | |
818 | ||
819 | sr_dbg("Detecting packets on FD %d (timeout = %" PRIu64 | |
820 | "ms, baudrate = %d).", serial->fd, timeout_ms, baudrate); | |
821 | ||
822 | if (maxlen < (packet_size / 2) ) { | |
823 | sr_err("Buffer size must be at least twice the packet size."); | |
824 | return SR_ERR; | |
825 | } | |
826 | ||
827 | /* Assume 8n1 transmission. That is 10 bits for every byte. */ | |
828 | byte_delay_us = 10 * (1000000 / baudrate); | |
829 | start = g_get_monotonic_time(); | |
830 | ||
831 | i = ibuf = len = 0; | |
832 | while (ibuf < maxlen) { | |
833 | len = serial_read(serial, &buf[ibuf], 1); | |
834 | if (len > 0) { | |
835 | ibuf += len; | |
836 | } else if (len == 0) { | |
837 | /* No logging, already done in serial_read(). */ | |
838 | } else { | |
839 | /* Error reading byte, but continuing anyway. */ | |
840 | } | |
841 | ||
842 | time = g_get_monotonic_time() - start; | |
843 | time /= 1000; | |
844 | ||
845 | if ((ibuf - i) >= packet_size) { | |
846 | /* We have at least a packet's worth of data. */ | |
847 | if (is_valid(&buf[i])) { | |
848 | sr_spew("Found valid %d-byte packet after " | |
849 | "%" PRIu64 "ms.", (ibuf - i), time); | |
850 | *buflen = ibuf; | |
851 | return SR_OK; | |
852 | } else { | |
853 | sr_spew("Got %d bytes, but not a valid " | |
854 | "packet.", (ibuf - i)); | |
855 | } | |
856 | /* Not a valid packet. Continue searching. */ | |
857 | i++; | |
858 | } | |
859 | if (time >= timeout_ms) { | |
860 | /* Timeout */ | |
861 | sr_dbg("Detection timed out after %dms.", time); | |
862 | break; | |
863 | } | |
864 | g_usleep(byte_delay_us); | |
865 | } | |
866 | ||
867 | *buflen = ibuf; | |
868 | ||
869 | sr_err("Didn't find a valid packet (read %d bytes).", *buflen); | |
870 | ||
871 | return SR_ERR; | |
872 | } |