]>
Commit | Line | Data |
---|---|---|
a1bb33af UH |
1 | /* |
2 | * This file is part of the sigrok project. | |
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 BV |
23 | #include <sys/types.h> |
24 | #include <sys/stat.h> | |
25 | #include <fcntl.h> | |
26 | #include <unistd.h> | |
926b866c | 27 | #ifdef _WIN32 |
a9f54bcd | 28 | #include <windows.h> |
926b866c UH |
29 | #else |
30 | #include <glob.h> | |
d02a535e | 31 | #include <termios.h> |
0f84cda0 | 32 | #include <sys/ioctl.h> |
926b866c | 33 | #endif |
d02a535e | 34 | #include <stdlib.h> |
b19f4622 | 35 | #include <errno.h> |
a1bb33af | 36 | #include <glib.h> |
45c59c8b BV |
37 | #include "libsigrok.h" |
38 | #include "libsigrok-internal.h" | |
a1bb33af | 39 | |
b19f4622 UH |
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 | ||
926b866c UH |
49 | // FIXME: Must be moved, or rather passed as function argument. |
50 | #ifdef _WIN32 | |
a0ecd83b | 51 | static HANDLE hdl; |
926b866c UH |
52 | #endif |
53 | ||
b19f4622 UH |
54 | /** |
55 | * Open the specified serial port. | |
56 | * | |
299bdb24 | 57 | * @param serial Previously initialized serial port structure. |
a54dd31e UH |
58 | * @param flags Flags to use when opening the serial port. Possible flags |
59 | * include SERIAL_RDWR, SERIAL_RDONLY, SERIAL_NONBLOCK. | |
b19f4622 | 60 | * |
299bdb24 BV |
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. | |
b19f4622 | 65 | */ |
299bdb24 | 66 | SR_PRIV int serial_open(struct sr_serial_dev_inst *serial, int flags) |
d02a535e | 67 | { |
a54dd31e UH |
68 | int flags_local = 0; |
69 | #ifdef _WIN32 | |
70 | DWORD desired_access = 0, flags_and_attributes = 0; | |
71 | #endif | |
b19f4622 | 72 | |
299bdb24 BV |
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); | |
b19f4622 | 79 | |
926b866c | 80 | #ifdef _WIN32 |
a54dd31e UH |
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); | |
926b866c | 91 | if (hdl == INVALID_HANDLE_VALUE) { |
299bdb24 BV |
92 | sr_err("Error opening serial port '%s'.", serial->port); |
93 | return SR_ERR; | |
926b866c | 94 | } |
926b866c | 95 | #else |
a54dd31e UH |
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) { | |
299bdb24 | 105 | sr_err("Error opening serial port '%s': %s.", serial->port, |
b19f4622 | 106 | strerror(errno)); |
299bdb24 | 107 | return SR_ERR; |
a54dd31e UH |
108 | } |
109 | ||
110 | sr_spew("Opened serial port '%s' (fd %d).", serial->port, serial->fd); | |
926b866c | 111 | #endif |
299bdb24 BV |
112 | |
113 | if (serial->serialcomm) | |
114 | return serial_set_paramstr(serial, serial->serialcomm); | |
115 | else | |
116 | return SR_OK; | |
d02a535e BV |
117 | } |
118 | ||
b19f4622 UH |
119 | /** |
120 | * Close the specified serial port. | |
121 | * | |
299bdb24 | 122 | * @param serial Previously initialized serial port structure. |
b19f4622 | 123 | * |
299bdb24 | 124 | * @return SR_OK on success, SR_ERR on failure. |
2119ab03 | 125 | */ |
299bdb24 | 126 | SR_PRIV int serial_close(struct sr_serial_dev_inst *serial) |
d02a535e | 127 | { |
299bdb24 BV |
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; | |
b19f4622 | 143 | |
926b866c | 144 | #ifdef _WIN32 |
2119ab03 | 145 | /* Returns non-zero upon success, 0 upon failure. */ |
299bdb24 BV |
146 | if (CloseHandle(hdl) == 0) |
147 | ret = SR_ERR; | |
926b866c | 148 | #else |
2119ab03 | 149 | /* Returns 0 upon success, -1 upon failure. */ |
299bdb24 BV |
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; | |
b19f4622 | 154 | } |
299bdb24 BV |
155 | #endif |
156 | ||
157 | serial->fd = -1; | |
b19f4622 UH |
158 | |
159 | return ret; | |
d02a535e BV |
160 | } |
161 | ||
b19f4622 | 162 | /** |
299bdb24 | 163 | * Flush serial port buffers. |
b19f4622 | 164 | * |
299bdb24 | 165 | * @param serial Previously initialized serial port structure. |
b19f4622 | 166 | * |
299bdb24 | 167 | * @return SR_OK on success, SR_ERR on failure. |
1fdb75e1 | 168 | */ |
299bdb24 | 169 | SR_PRIV int serial_flush(struct sr_serial_dev_inst *serial) |
06d64eb8 | 170 | { |
299bdb24 BV |
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; | |
b19f4622 | 186 | |
1fdb75e1 UH |
187 | #ifdef _WIN32 |
188 | /* Returns non-zero upon success, 0 upon failure. */ | |
4da1a800 | 189 | if (PurgeComm(hdl, PURGE_RXCLEAR | PURGE_TXCLEAR) == 0) { |
299bdb24 BV |
190 | sr_err("Error flushing serial port: %s.", strerror(errno)); |
191 | ret = SR_ERR; | |
192 | } | |
1fdb75e1 UH |
193 | #else |
194 | /* Returns 0 upon success, -1 upon failure. */ | |
299bdb24 | 195 | if (tcflush(serial->fd, TCIOFLUSH) < 0) { |
b19f4622 | 196 | sr_err("Error flushing serial port: %s.", strerror(errno)); |
299bdb24 BV |
197 | ret = SR_ERR; |
198 | } | |
b19f4622 UH |
199 | |
200 | return ret; | |
1fdb75e1 | 201 | #endif |
06d64eb8 BV |
202 | } |
203 | ||
b19f4622 | 204 | /** |
2119ab03 | 205 | * Write a number of bytes to the specified serial port. |
b19f4622 | 206 | * |
299bdb24 | 207 | * @param serial Previously initialized serial port structure. |
b19f4622 UH |
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. | |
2119ab03 | 212 | */ |
299bdb24 BV |
213 | SR_PRIV int serial_write(struct sr_serial_dev_inst *serial, |
214 | const void *buf, size_t count) | |
2119ab03 | 215 | { |
299bdb24 BV |
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 | ||
2119ab03 UH |
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. */ | |
299bdb24 | 237 | ret = write(serial->fd, buf, count); |
b19f4622 | 238 | if (ret < 0) |
299bdb24 | 239 | sr_err("Write error: %s.", strerror(errno)); |
302c4b5a | 240 | else |
299bdb24 BV |
241 | sr_spew("Wrote %d/%d bytes (fd %d).", ret, count, serial->fd); |
242 | #endif | |
b19f4622 UH |
243 | |
244 | return ret; | |
2119ab03 UH |
245 | } |
246 | ||
b19f4622 | 247 | /** |
2119ab03 | 248 | * Read a number of bytes from the specified serial port. |
b19f4622 | 249 | * |
299bdb24 | 250 | * @param serial Previously initialized serial port structure. |
b19f4622 UH |
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. | |
2119ab03 | 255 | */ |
299bdb24 BV |
256 | SR_PRIV int serial_read(struct sr_serial_dev_inst *serial, void *buf, |
257 | size_t count) | |
2119ab03 | 258 | { |
299bdb24 BV |
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 | ||
2119ab03 UH |
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. */ | |
299bdb24 BV |
280 | ret = read(serial->fd, buf, count); |
281 | if (ret < 0) | |
b19f4622 UH |
282 | /* |
283 | * Should be sr_err(), but that would yield lots of | |
284 | * "Resource temporarily unavailable" messages. | |
285 | */ | |
299bdb24 BV |
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 | |
b19f4622 UH |
290 | |
291 | return ret; | |
2119ab03 UH |
292 | } |
293 | ||
b19f4622 UH |
294 | /** |
295 | * Set serial parameters for the specified serial port. | |
296 | * | |
299bdb24 | 297 | * @param serial Previously initialized serial port structure. |
b19f4622 UH |
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). | |
2119ab03 | 304 | * |
b19f4622 | 305 | * @return SR_OK upon success, SR_ERR upon failure. |
1ff7712c | 306 | */ |
299bdb24 | 307 | SR_PRIV int serial_set_params(struct sr_serial_dev_inst *serial, int baudrate, |
39e5d798 UH |
308 | int bits, int parity, int stopbits, |
309 | int flowcontrol, int rts, int dtr) | |
d02a535e | 310 | { |
299bdb24 BV |
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).", | |
39e5d798 | 318 | serial->port, serial->fd); |
299bdb24 BV |
319 | return SR_ERR; |
320 | } | |
321 | ||
322 | sr_spew("Setting serial parameters on port %s (fd %d).", serial->port, | |
39e5d798 | 323 | serial->fd); |
b19f4622 | 324 | |
926b866c UH |
325 | #ifdef _WIN32 |
326 | DCB dcb; | |
327 | ||
299bdb24 | 328 | if (!GetCommState(hdl, &dcb)) |
e46b8fb1 | 329 | return SR_ERR; |
926b866c | 330 | |
0abee507 | 331 | switch (baudrate) { |
5ae35c29 UH |
332 | /* |
333 | * The baudrates 50/75/134/150/200/1800/230400/460800 do not seem to | |
334 | * have documented CBR_* macros. | |
335 | */ | |
336 | case 110: | |
337 | dcb.BaudRate = CBR_110; | |
926b866c | 338 | break; |
5ae35c29 UH |
339 | case 300: |
340 | dcb.BaudRate = CBR_300; | |
926b866c | 341 | break; |
5ae35c29 UH |
342 | case 600: |
343 | dcb.BaudRate = CBR_600; | |
926b866c | 344 | break; |
5ae35c29 UH |
345 | case 1200: |
346 | dcb.BaudRate = CBR_1200; | |
926b866c | 347 | break; |
5ae35c29 UH |
348 | case 2400: |
349 | dcb.BaudRate = CBR_2400; | |
926b866c | 350 | break; |
e8e9dcdd AG |
351 | case 4800: |
352 | dcb.BaudRate = CBR_4800; | |
353 | break; | |
5ae35c29 UH |
354 | case 9600: |
355 | dcb.BaudRate = CBR_9600; | |
356 | break; | |
357 | case 14400: | |
358 | dcb.BaudRate = CBR_14400; /* Not available on Unix? */ | |
359 | break; | |
360 | case 19200: | |
361 | dcb.BaudRate = CBR_19200; | |
362 | break; | |
363 | case 38400: | |
364 | dcb.BaudRate = CBR_38400; | |
365 | break; | |
366 | case 57600: | |
367 | dcb.BaudRate = CBR_57600; | |
368 | break; | |
369 | case 115200: | |
370 | dcb.BaudRate = CBR_115200; | |
371 | break; | |
372 | case 128000: | |
373 | dcb.BaudRate = CBR_128000; /* Not available on Unix? */ | |
374 | break; | |
375 | case 256000: | |
376 | dcb.BaudRate = CBR_256000; /* Not available on Unix? */ | |
0f708301 | 377 | break; |
926b866c | 378 | default: |
5ae35c29 | 379 | sr_err("Unsupported baudrate: %d.", baudrate); |
b19f4622 | 380 | return SR_ERR; |
926b866c | 381 | } |
5ae35c29 UH |
382 | sr_spew("Configuring baudrate to %d (%d).", baudrate, dcb.BaudRate); |
383 | ||
926b866c UH |
384 | dcb.ByteSize = bits; |
385 | dcb.Parity = NOPARITY; /* TODO: Don't hardcode. */ | |
386 | dcb.StopBits = ONESTOPBIT; /* TODO: Don't hardcode. */ | |
387 | ||
39e5d798 UH |
388 | if (rts != -1) { |
389 | sr_spew("Setting RTS %s.", rts ? "high" : "low"); | |
390 | if (rts) | |
391 | dcb.fRtsControl = RTS_CONTROL_ENABLE; | |
392 | else | |
393 | dcb.fRtsControl = RTS_CONTROL_DISABLE; | |
394 | } | |
395 | ||
396 | if (dtr != -1) { | |
397 | sr_spew("Setting DTR %s.", dtr ? "high" : "low"); | |
398 | if (rts) | |
399 | dcb.fDtrControl = DTR_CONTROL_ENABLE; | |
400 | else | |
401 | dcb.fDtrControl = DTR_CONTROL_DISABLE; | |
402 | } | |
403 | ||
299bdb24 | 404 | if (!SetCommState(hdl, &dcb)) |
e46b8fb1 | 405 | return SR_ERR; |
926b866c | 406 | #else |
d02a535e | 407 | struct termios term; |
1ff7712c | 408 | speed_t baud; |
700dcd5c | 409 | int ret, controlbits; |
d02a535e | 410 | |
299bdb24 BV |
411 | if (tcgetattr(serial->fd, &term) < 0) { |
412 | sr_err("tcgetattr() error on port %s (fd %d): %s.", | |
413 | serial->port, serial->fd, strerror(errno)); | |
6a6e23ab | 414 | return SR_ERR; |
b19f4622 | 415 | } |
6a6e23ab | 416 | |
0abee507 | 417 | switch (baudrate) { |
b19f4622 UH |
418 | case 50: |
419 | baud = B50; | |
420 | break; | |
421 | case 75: | |
422 | baud = B75; | |
423 | break; | |
424 | case 110: | |
425 | baud = B110; | |
426 | break; | |
427 | case 134: | |
428 | baud = B134; | |
429 | break; | |
430 | case 150: | |
431 | baud = B150; | |
432 | break; | |
433 | case 200: | |
434 | baud = B200; | |
435 | break; | |
436 | case 300: | |
437 | baud = B300; | |
438 | break; | |
439 | case 600: | |
440 | baud = B600; | |
441 | break; | |
442 | case 1200: | |
443 | baud = B1200; | |
444 | break; | |
445 | case 1800: | |
446 | baud = B1800; | |
447 | break; | |
0f708301 AG |
448 | case 2400: |
449 | baud = B2400; | |
450 | break; | |
e8e9dcdd AG |
451 | case 4800: |
452 | baud = B4800; | |
453 | break; | |
1ff7712c DR |
454 | case 9600: |
455 | baud = B9600; | |
456 | break; | |
b19f4622 UH |
457 | case 19200: |
458 | baud = B19200; | |
459 | break; | |
1ff7712c DR |
460 | case 38400: |
461 | baud = B38400; | |
462 | break; | |
463 | case 57600: | |
464 | baud = B57600; | |
465 | break; | |
466 | case 115200: | |
467 | baud = B115200; | |
468 | break; | |
b19f4622 UH |
469 | case 230400: |
470 | baud = B230400; | |
471 | break; | |
9a751023 | 472 | #ifndef __APPLE__ |
1ff7712c DR |
473 | case 460800: |
474 | baud = B460800; | |
475 | break; | |
9a751023 | 476 | #endif |
1ff7712c | 477 | default: |
5ae35c29 | 478 | sr_err("Unsupported baudrate: %d.", baudrate); |
e46b8fb1 | 479 | return SR_ERR; |
1ff7712c | 480 | } |
b19f4622 | 481 | |
299bdb24 | 482 | sr_spew("Configuring output baudrate to %d (%d).", baudrate, baud); |
b19f4622 | 483 | if (cfsetospeed(&term, baud) < 0) { |
5df7b201 | 484 | sr_err("cfsetospeed() error: %s.", strerror(errno)); |
e46b8fb1 | 485 | return SR_ERR; |
b19f4622 UH |
486 | } |
487 | ||
299bdb24 | 488 | sr_spew("Configuring input baudrate to %d (%d).", baudrate, baud); |
b19f4622 | 489 | if (cfsetispeed(&term, baud) < 0) { |
5df7b201 | 490 | sr_err("cfsetispeed() error: %s.", strerror(errno)); |
e46b8fb1 | 491 | return SR_ERR; |
b19f4622 | 492 | } |
1ff7712c | 493 | |
299bdb24 | 494 | sr_spew("Configuring %d data bits.", bits); |
d02a535e | 495 | term.c_cflag &= ~CSIZE; |
1ff7712c DR |
496 | switch (bits) { |
497 | case 8: | |
498 | term.c_cflag |= CS8; | |
499 | break; | |
500 | case 7: | |
501 | term.c_cflag |= CS7; | |
502 | break; | |
503 | default: | |
299bdb24 | 504 | sr_err("Unsupported data bits number %d.", bits); |
e46b8fb1 | 505 | return SR_ERR; |
1ff7712c DR |
506 | } |
507 | ||
299bdb24 | 508 | sr_spew("Configuring %d stop bits.", stopbits); |
d02a535e | 509 | term.c_cflag &= ~CSTOPB; |
1ff7712c DR |
510 | switch (stopbits) { |
511 | case 1: | |
299bdb24 | 512 | term.c_cflag &= ~CSTOPB; |
1ff7712c DR |
513 | break; |
514 | case 2: | |
515 | term.c_cflag |= CSTOPB; | |
d7c776b9 | 516 | break; |
1ff7712c | 517 | default: |
299bdb24 | 518 | sr_err("Unsupported stopbits number %d.", stopbits); |
e46b8fb1 | 519 | return SR_ERR; |
1ff7712c DR |
520 | } |
521 | ||
f38b9763 BV |
522 | term.c_iflag &= ~(IXON | IXOFF); |
523 | term.c_cflag &= ~CRTSCTS; | |
1ff7712c | 524 | switch (flowcontrol) { |
f38b9763 BV |
525 | case 0: |
526 | /* No flow control. */ | |
299bdb24 | 527 | sr_spew("Configuring no flow control."); |
1ff7712c DR |
528 | break; |
529 | case 1: | |
299bdb24 | 530 | sr_spew("Configuring RTS/CTS flow control."); |
1ff7712c | 531 | term.c_cflag |= CRTSCTS; |
d7c776b9 | 532 | break; |
f38b9763 | 533 | case 2: |
299bdb24 | 534 | sr_spew("Configuring XON/XOFF flow control."); |
f38b9763 BV |
535 | term.c_iflag |= IXON | IXOFF; |
536 | break; | |
1ff7712c | 537 | default: |
299bdb24 | 538 | sr_err("Unsupported flow control setting %d.", flowcontrol); |
e46b8fb1 | 539 | return SR_ERR; |
1ff7712c DR |
540 | } |
541 | ||
ac4a2ea4 DR |
542 | term.c_iflag &= ~IGNPAR; |
543 | term.c_cflag &= ~(PARODD | PARENB); | |
1ff7712c | 544 | switch (parity) { |
f8c1fcda | 545 | case SERIAL_PARITY_NONE: |
299bdb24 | 546 | sr_spew("Configuring no parity."); |
1ff7712c DR |
547 | term.c_iflag |= IGNPAR; |
548 | break; | |
f8c1fcda | 549 | case SERIAL_PARITY_EVEN: |
299bdb24 | 550 | sr_spew("Configuring even parity."); |
ac4a2ea4 | 551 | term.c_cflag |= PARENB; |
1ff7712c | 552 | break; |
f8c1fcda | 553 | case SERIAL_PARITY_ODD: |
299bdb24 | 554 | sr_spew("Configuring odd parity."); |
ac4a2ea4 | 555 | term.c_cflag |= PARENB | PARODD; |
1ff7712c DR |
556 | break; |
557 | default: | |
299bdb24 | 558 | sr_err("Unsupported parity setting %d.", parity); |
e46b8fb1 | 559 | return SR_ERR; |
1ff7712c DR |
560 | } |
561 | ||
299bdb24 | 562 | /* Do not translate carriage return to newline on input. */ |
fb9d3bf9 | 563 | term.c_iflag &= ~(ICRNL); |
b19f4622 UH |
564 | |
565 | /* Disable canonical mode, and don't echo input characters. */ | |
5c51e098 BV |
566 | term.c_lflag &= ~(ICANON | ECHO); |
567 | ||
b19f4622 | 568 | /* Write the configured settings. */ |
299bdb24 BV |
569 | if (tcsetattr(serial->fd, TCSADRAIN, &term) < 0) { |
570 | sr_err("tcsetattr() error: %s.", strerror(errno)); | |
e46b8fb1 | 571 | return SR_ERR; |
b19f4622 | 572 | } |
700dcd5c | 573 | |
71caaad4 BV |
574 | if (rts != -1) { |
575 | sr_spew("Setting RTS %s.", rts ? "high" : "low"); | |
576 | controlbits = TIOCM_RTS; | |
577 | if ((ret = ioctl(serial->fd, rts ? TIOCMBIS : TIOCMBIC, | |
578 | &controlbits)) < 0) { | |
579 | sr_err("Error setting RTS: %s.", strerror(errno)); | |
580 | return SR_ERR; | |
581 | } | |
700dcd5c UH |
582 | } |
583 | ||
71caaad4 BV |
584 | if (dtr != -1) { |
585 | sr_spew("Setting DTR %s.", dtr ? "high" : "low"); | |
586 | controlbits = TIOCM_DTR; | |
587 | if ((ret = ioctl(serial->fd, dtr ? TIOCMBIS : TIOCMBIC, | |
588 | &controlbits)) < 0) { | |
589 | sr_err("Error setting DTR: %s.", strerror(errno)); | |
590 | return SR_ERR; | |
591 | } | |
700dcd5c | 592 | } |
700dcd5c | 593 | |
926b866c | 594 | #endif |
d02a535e | 595 | |
e46b8fb1 | 596 | return SR_OK; |
d02a535e | 597 | } |
792fc686 | 598 | |
299bdb24 BV |
599 | /** |
600 | * Set serial parameters for the specified serial port. | |
601 | * | |
602 | * @param serial Previously initialized serial port structure. | |
603 | * @param paramstr A serial communication parameters string, in the form | |
604 | * of <speed>/<data bits><parity><stopbits>, for example "9600/8n1" or | |
605 | * "600/7o2". | |
606 | * | |
607 | * @return SR_OK upon success, SR_ERR upon failure. | |
608 | */ | |
71caaad4 | 609 | #define SERIAL_COMM_SPEC "^(\\d+)/([78])([neo])([12])(.*)$" |
299bdb24 BV |
610 | SR_PRIV int serial_set_paramstr(struct sr_serial_dev_inst *serial, |
611 | const char *paramstr) | |
792fc686 BV |
612 | { |
613 | GRegex *reg; | |
614 | GMatchInfo *match; | |
71caaad4 BV |
615 | int speed, databits, parity, stopbits, rts, dtr, i; |
616 | char *mstr, **opts, **kv; | |
792fc686 BV |
617 | |
618 | speed = databits = parity = stopbits = 0; | |
71caaad4 | 619 | rts = dtr = -1; |
792fc686 BV |
620 | reg = g_regex_new(SERIAL_COMM_SPEC, 0, 0, NULL); |
621 | if (g_regex_match(reg, paramstr, 0, &match)) { | |
622 | if ((mstr = g_match_info_fetch(match, 1))) | |
623 | speed = strtoul(mstr, NULL, 10); | |
624 | g_free(mstr); | |
625 | if ((mstr = g_match_info_fetch(match, 2))) | |
626 | databits = strtoul(mstr, NULL, 10); | |
627 | g_free(mstr); | |
628 | if ((mstr = g_match_info_fetch(match, 3))) { | |
629 | switch (mstr[0]) { | |
630 | case 'n': | |
631 | parity = SERIAL_PARITY_NONE; | |
632 | break; | |
633 | case 'e': | |
634 | parity = SERIAL_PARITY_EVEN; | |
635 | break; | |
636 | case 'o': | |
637 | parity = SERIAL_PARITY_ODD; | |
638 | break; | |
639 | } | |
640 | } | |
641 | g_free(mstr); | |
642 | if ((mstr = g_match_info_fetch(match, 4))) | |
643 | stopbits = strtoul(mstr, NULL, 10); | |
644 | g_free(mstr); | |
71caaad4 BV |
645 | if ((mstr = g_match_info_fetch(match, 5)) && mstr[0] != '\0') { |
646 | if (mstr[0] != '/') { | |
647 | sr_dbg("missing separator before extra options"); | |
648 | speed = 0; | |
649 | } else { | |
650 | /* A set of "key=value" options separated by / */ | |
651 | opts = g_strsplit(mstr + 1, "/", 0); | |
652 | for (i = 0; opts[i]; i++) { | |
653 | kv = g_strsplit(opts[i], "=", 2); | |
654 | if (!strncmp(kv[0], "rts", 3)) { | |
655 | if (kv[1][0] == '1') | |
656 | rts = 1; | |
657 | else if (kv[1][0] == '0') | |
658 | rts = 0; | |
659 | else { | |
660 | sr_dbg("invalid value for rts: %c", kv[1][0]); | |
661 | speed = 0; | |
662 | } | |
663 | } else if (!strncmp(kv[0], "dtr", 3)) { | |
664 | if (kv[1][0] == '1') | |
665 | dtr = 1; | |
666 | else if (kv[1][0] == '0') | |
667 | dtr = 0; | |
668 | else { | |
669 | sr_dbg("invalid value for dtr: %c", kv[1][0]); | |
670 | speed = 0; | |
671 | } | |
672 | } | |
673 | g_strfreev(kv); | |
674 | } | |
675 | g_strfreev(opts); | |
676 | } | |
677 | } | |
678 | g_free(mstr); | |
792fc686 BV |
679 | } |
680 | g_match_info_unref(match); | |
681 | g_regex_unref(reg); | |
682 | ||
683 | if (speed) | |
71caaad4 BV |
684 | return serial_set_params(serial, speed, databits, parity, stopbits, |
685 | 0, rts, dtr); | |
792fc686 BV |
686 | else |
687 | return SR_ERR_ARG; | |
688 | } | |
689 | ||
299bdb24 BV |
690 | /** |
691 | * Read a line from the specified serial port. | |
692 | * | |
693 | * @param serial Previously initialized serial port structure. | |
694 | * @param buf Buffer where to store the bytes that are read. | |
695 | * @param buflen Size of the buffer. | |
696 | * @param timeout_ms How long to wait for a line to come in. | |
697 | * | |
698 | * Reading stops when CR of LR is found, which is stripped from the buffer. | |
699 | * | |
700 | * @return SR_OK on success, SR_ERR on failure. | |
701 | */ | |
702 | SR_PRIV int serial_readline(struct sr_serial_dev_inst *serial, char **buf, | |
703 | int *buflen, gint64 timeout_ms) | |
6f22a8ef | 704 | { |
b87f8504 | 705 | gint64 start; |
6f22a8ef UH |
706 | int maxlen, len; |
707 | ||
299bdb24 BV |
708 | if (!serial || serial->fd == -1) { |
709 | sr_dbg("Invalid serial port."); | |
710 | return SR_ERR; | |
711 | } | |
712 | ||
713 | if (serial->fd == -1) { | |
714 | sr_dbg("Cannot use unopened serial port %s (fd %d).", | |
715 | serial->port, serial->fd); | |
716 | return -1; | |
717 | } | |
718 | ||
6f22a8ef UH |
719 | timeout_ms *= 1000; |
720 | start = g_get_monotonic_time(); | |
721 | ||
722 | maxlen = *buflen; | |
723 | *buflen = len = 0; | |
724 | while(1) { | |
725 | len = maxlen - *buflen - 1; | |
726 | if (len < 1) | |
727 | break; | |
299bdb24 | 728 | len = serial_read(serial, *buf + *buflen, 1); |
6f22a8ef UH |
729 | if (len > 0) { |
730 | *buflen += len; | |
731 | *(*buf + *buflen) = '\0'; | |
318dd53c BV |
732 | if (*buflen > 0 && (*(*buf + *buflen - 1) == '\r' |
733 | || *(*buf + *buflen - 1) == '\n')) { | |
734 | /* Strip CR/LF and terminate. */ | |
6f22a8ef UH |
735 | *(*buf + --*buflen) = '\0'; |
736 | break; | |
737 | } | |
738 | } | |
739 | if (g_get_monotonic_time() - start > timeout_ms) | |
740 | /* Timeout */ | |
741 | break; | |
742 | g_usleep(2000); | |
743 | } | |
318dd53c BV |
744 | if (*buflen) |
745 | sr_dbg("Received %d: '%s'.", *buflen, *buf); | |
6f22a8ef UH |
746 | |
747 | return SR_OK; | |
748 | } | |
766456be UH |
749 | |
750 | /** | |
751 | * Try to find a valid packet in a serial data stream. | |
752 | * | |
753 | * @param serial Previously initialized serial port structure. | |
754 | * @param buf Buffer containing the bytes to write. | |
755 | * @param count Size of the buffer. | |
756 | * @param packet_size Size, in bytes, of a valid packet. | |
757 | * @param is_valid Callback that assesses whether the packet is valid or not. | |
758 | * @param timeout_ms The timeout after which, if no packet is detected, to | |
759 | * abort scanning. | |
760 | * @param baudrate The baudrate of the serial port. This parameter is not | |
761 | * critical, but it helps fine tune the serial port polling | |
762 | * delay. | |
763 | * | |
764 | * @return SR_OK if a valid packet is found within the given timeout, | |
765 | * SR_ERR upon failure. | |
766 | */ | |
767 | SR_PRIV int serial_stream_detect(struct sr_serial_dev_inst *serial, | |
768 | uint8_t *buf, size_t *buflen, | |
769 | size_t packet_size, packet_valid_t is_valid, | |
770 | uint64_t timeout_ms, int baudrate) | |
771 | { | |
772 | uint64_t start, time, byte_delay_us; | |
773 | size_t ibuf, i, maxlen; | |
774 | int len; | |
775 | ||
776 | maxlen = *buflen; | |
777 | ||
778 | sr_dbg("Detecting packets on FD %d (timeout = %" PRIu64 | |
779 | "ms, baudrate = %d).", serial->fd, timeout_ms, baudrate); | |
780 | ||
781 | if (maxlen < (packet_size / 2) ) { | |
782 | sr_err("Buffer size must be at least twice the packet size."); | |
783 | return SR_ERR; | |
784 | } | |
785 | ||
766456be UH |
786 | /* Assume 8n1 transmission. That is 10 bits for every byte. */ |
787 | byte_delay_us = 10 * (1000000 / baudrate); | |
788 | start = g_get_monotonic_time(); | |
789 | ||
790 | i = ibuf = len = 0; | |
791 | while (ibuf < maxlen) { | |
792 | len = serial_read(serial, &buf[ibuf], 1); | |
793 | if (len > 0) { | |
794 | ibuf += len; | |
795 | } else if (len == 0) { | |
796 | sr_spew("Error: Only read 0 bytes."); | |
797 | } else { | |
798 | /* Error reading byte, but continuing anyway. */ | |
799 | } | |
551c3d8c AG |
800 | |
801 | time = g_get_monotonic_time() - start; | |
802 | time /= 1000; | |
803 | ||
766456be UH |
804 | if ((ibuf - i) >= packet_size) { |
805 | /* We have at least a packet's worth of data. */ | |
806 | if (is_valid(&buf[i])) { | |
766456be UH |
807 | sr_spew("Found valid %d-byte packet after " |
808 | "%" PRIu64 "ms.", (ibuf - i), time); | |
809 | *buflen = ibuf; | |
810 | return SR_OK; | |
811 | } else { | |
812 | sr_spew("Got %d bytes, but not a valid " | |
813 | "packet.", (ibuf - i)); | |
814 | } | |
815 | /* Not a valid packet. Continue searching. */ | |
816 | i++; | |
817 | } | |
551c3d8c | 818 | if (time >= timeout_ms) { |
766456be | 819 | /* Timeout */ |
551c3d8c | 820 | sr_dbg("Detection timed out after %dms.", time); |
766456be UH |
821 | break; |
822 | } | |
823 | g_usleep(byte_delay_us); | |
824 | } | |
825 | ||
826 | *buflen = ibuf; | |
827 | ||
828 | sr_err("Didn't find a valid packet (read %d bytes).", *buflen); | |
829 | ||
830 | return SR_ERR; | |
831 | } |