]>
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> |
a1bb33af UH |
6 | * |
7 | * This program is free software: you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation, either version 3 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | */ | |
20 | ||
54b38f64 | 21 | #include <string.h> |
d02a535e BV |
22 | #include <sys/types.h> |
23 | #include <sys/stat.h> | |
24 | #include <fcntl.h> | |
25 | #include <unistd.h> | |
926b866c | 26 | #ifdef _WIN32 |
a9f54bcd | 27 | #include <windows.h> |
926b866c UH |
28 | #else |
29 | #include <glob.h> | |
d02a535e | 30 | #include <termios.h> |
926b866c | 31 | #endif |
d02a535e | 32 | #include <stdlib.h> |
b19f4622 | 33 | #include <errno.h> |
a1bb33af | 34 | #include <glib.h> |
45c59c8b BV |
35 | #include "libsigrok.h" |
36 | #include "libsigrok-internal.h" | |
a1bb33af | 37 | |
b19f4622 UH |
38 | /* Message logging helpers with driver-specific prefix string. */ |
39 | #define DRIVER_LOG_DOMAIN "serial: " | |
40 | #define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args) | |
41 | #define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args) | |
42 | #define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args) | |
43 | #define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args) | |
44 | #define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args) | |
45 | #define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args) | |
46 | ||
926b866c UH |
47 | // FIXME: Must be moved, or rather passed as function argument. |
48 | #ifdef _WIN32 | |
a0ecd83b | 49 | static HANDLE hdl; |
926b866c UH |
50 | #endif |
51 | ||
1a081ca6 | 52 | static const char *serial_port_glob[] = { |
a1bb33af UH |
53 | /* Linux */ |
54 | "/dev/ttyS*", | |
55 | "/dev/ttyUSB*", | |
56 | "/dev/ttyACM*", | |
57 | /* MacOS X */ | |
58 | "/dev/ttys*", | |
59 | "/dev/tty.USB-*", | |
60 | "/dev/tty.Modem-*", | |
986f7270 | 61 | NULL, |
a1bb33af UH |
62 | }; |
63 | ||
1a081ca6 | 64 | SR_PRIV GSList *list_serial_ports(void) |
a1bb33af | 65 | { |
2119ab03 UH |
66 | GSList *ports; |
67 | ||
b19f4622 UH |
68 | sr_dbg("Getting list of serial ports on the system."); |
69 | ||
926b866c UH |
70 | #ifdef _WIN32 |
71 | /* TODO */ | |
2119ab03 | 72 | ports = NULL; |
133a37bf | 73 | ports = g_slist_append(ports, g_strdup("COM1")); |
926b866c | 74 | #else |
a1bb33af | 75 | glob_t g; |
afc8e4de | 76 | unsigned int i, j; |
a1bb33af UH |
77 | |
78 | ports = NULL; | |
986f7270 | 79 | for (i = 0; serial_port_glob[i]; i++) { |
71dda106 PS |
80 | if (glob(serial_port_glob[i], 0, NULL, &g)) |
81 | continue; | |
b19f4622 | 82 | for (j = 0; j < g.gl_pathc; j++) { |
133a37bf | 83 | ports = g_slist_append(ports, g_strdup(g.gl_pathv[j])); |
b19f4622 UH |
84 | sr_dbg("Found serial port '%s'.", g.gl_pathv[j]); |
85 | } | |
71dda106 | 86 | globfree(&g); |
a1bb33af | 87 | } |
2119ab03 | 88 | #endif |
a1bb33af UH |
89 | |
90 | return ports; | |
91 | } | |
92 | ||
b19f4622 UH |
93 | /** |
94 | * Open the specified serial port. | |
95 | * | |
96 | * @param pathname OS-specific serial port specification. Examples: | |
97 | * "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1". | |
98 | * @param flags Flags to use when opening the serial port. | |
99 | * | |
100 | * @return 0 upon success, -1 upon failure. | |
101 | */ | |
1a081ca6 | 102 | SR_PRIV int serial_open(const char *pathname, int flags) |
d02a535e | 103 | { |
b19f4622 UH |
104 | /* TODO: Abstract 'flags', currently they're OS-specific! */ |
105 | ||
106 | sr_dbg("Opening serial port '%s' (flags = %d).", pathname, flags); | |
107 | ||
926b866c | 108 | #ifdef _WIN32 |
b19f4622 UH |
109 | pathname = "COM1"; /* FIXME: Don't hardcode COM1. */ |
110 | ||
111 | hdl = CreateFile(pathname, GENERIC_READ | GENERIC_WRITE, 0, 0, | |
926b866c UH |
112 | OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); |
113 | if (hdl == INVALID_HANDLE_VALUE) { | |
b19f4622 UH |
114 | sr_err("Error opening serial port '%s'.", pathname); |
115 | return -1; | |
926b866c UH |
116 | } |
117 | return 0; | |
118 | #else | |
b19f4622 UH |
119 | int fd; |
120 | ||
121 | if ((fd = open(pathname, flags)) < 0) { | |
122 | /* | |
123 | * Should be sr_err(), but since some drivers try to open all | |
124 | * ports on a system and see if they succeed, this would | |
125 | * yield ugly output for e.g. "sigrok-cli -D". | |
126 | */ | |
127 | sr_dbg("Error opening serial port '%s': %s.", pathname, | |
128 | strerror(errno)); | |
129 | } | |
130 | ||
131 | return fd; | |
926b866c | 132 | #endif |
d02a535e BV |
133 | } |
134 | ||
b19f4622 UH |
135 | /** |
136 | * Close the specified serial port. | |
137 | * | |
138 | * @param fd File descriptor of the serial port. | |
139 | * | |
140 | * @return 0 upon success, -1 upon failure. | |
2119ab03 | 141 | */ |
1a081ca6 | 142 | SR_PRIV int serial_close(int fd) |
d02a535e | 143 | { |
b19f4622 UH |
144 | sr_dbg("FD %d: Closing serial port.", fd); |
145 | ||
926b866c | 146 | #ifdef _WIN32 |
2119ab03 UH |
147 | /* Returns non-zero upon success, 0 upon failure. */ |
148 | return (CloseHandle(hdl) == 0) ? -1 : 0; | |
926b866c | 149 | #else |
b19f4622 UH |
150 | int ret; |
151 | ||
2119ab03 | 152 | /* Returns 0 upon success, -1 upon failure. */ |
b19f4622 UH |
153 | if ((ret = close(fd)) < 0) { |
154 | sr_dbg("FD %d: Error closing serial port: %s.", | |
155 | fd, strerror(errno)); | |
156 | } | |
157 | ||
158 | return ret; | |
926b866c | 159 | #endif |
d02a535e BV |
160 | } |
161 | ||
b19f4622 | 162 | /** |
1fdb75e1 | 163 | * Flush serial port buffers (if any). |
b19f4622 UH |
164 | * |
165 | * @param fd File descriptor of the serial port. | |
166 | * | |
167 | * @return 0 upon success, -1 upon failure. | |
1fdb75e1 | 168 | */ |
1a081ca6 | 169 | SR_PRIV int serial_flush(int fd) |
06d64eb8 | 170 | { |
b19f4622 UH |
171 | sr_dbg("FD %d: Flushing serial port.", fd); |
172 | ||
1fdb75e1 UH |
173 | #ifdef _WIN32 |
174 | /* Returns non-zero upon success, 0 upon failure. */ | |
2119ab03 | 175 | return (PurgeComm(hdl, PURGE_RXCLEAR | PURGE_TXCLEAR) == 0) ? -1 : 0; |
1fdb75e1 | 176 | #else |
b19f4622 UH |
177 | int ret; |
178 | ||
1fdb75e1 | 179 | /* Returns 0 upon success, -1 upon failure. */ |
b19f4622 UH |
180 | if ((ret = tcflush(fd, TCIOFLUSH)) < 0) |
181 | sr_err("Error flushing serial port: %s.", strerror(errno)); | |
182 | ||
183 | return ret; | |
1fdb75e1 | 184 | #endif |
06d64eb8 BV |
185 | } |
186 | ||
b19f4622 | 187 | /** |
2119ab03 | 188 | * Write a number of bytes to the specified serial port. |
b19f4622 UH |
189 | * |
190 | * @param fd File descriptor of the serial port. | |
191 | * @param buf Buffer containing the bytes to write. | |
192 | * @param count Number of bytes to write. | |
193 | * | |
194 | * @return The number of bytes written, or -1 upon failure. | |
2119ab03 | 195 | */ |
1a081ca6 | 196 | SR_PRIV int serial_write(int fd, const void *buf, size_t count) |
2119ab03 | 197 | { |
b19f4622 UH |
198 | sr_spew("FD %d: Writing %d bytes.", fd, count); |
199 | ||
2119ab03 UH |
200 | #ifdef _WIN32 |
201 | DWORD tmp = 0; | |
202 | ||
203 | /* FIXME */ | |
204 | /* Returns non-zero upon success, 0 upon failure. */ | |
205 | WriteFile(hdl, buf, count, &tmp, NULL); | |
206 | #else | |
b19f4622 UH |
207 | ssize_t ret; |
208 | ||
2119ab03 | 209 | /* Returns the number of bytes written, or -1 upon failure. */ |
b19f4622 UH |
210 | ret = write(fd, buf, count); |
211 | if (ret < 0) | |
212 | sr_err("FD %d: Write error: %s.", fd, strerror(errno)); | |
213 | else if ((size_t)ret != count) | |
214 | sr_spew("FD %d: Only wrote %d/%d bytes.", fd, ret, count); | |
215 | ||
216 | return ret; | |
2119ab03 UH |
217 | #endif |
218 | } | |
219 | ||
b19f4622 | 220 | /** |
2119ab03 | 221 | * Read a number of bytes from the specified serial port. |
b19f4622 UH |
222 | * |
223 | * @param fd File descriptor of the serial port. | |
224 | * @param buf Buffer where to store the bytes that are read. | |
225 | * @param count The number of bytes to read. | |
226 | * | |
227 | * @return The number of bytes read, or -1 upon failure. | |
2119ab03 | 228 | */ |
1a081ca6 | 229 | SR_PRIV int serial_read(int fd, void *buf, size_t count) |
2119ab03 | 230 | { |
b19f4622 UH |
231 | sr_spew("FD %d: Reading %d bytes.", fd, count); |
232 | ||
2119ab03 UH |
233 | #ifdef _WIN32 |
234 | DWORD tmp = 0; | |
235 | ||
236 | /* FIXME */ | |
237 | /* Returns non-zero upon success, 0 upon failure. */ | |
238 | return ReadFile(hdl, buf, count, &tmp, NULL); | |
239 | #else | |
b19f4622 UH |
240 | ssize_t ret; |
241 | ||
2119ab03 | 242 | /* Returns the number of bytes read, or -1 upon failure. */ |
b19f4622 UH |
243 | ret = read(fd, buf, count); |
244 | if (ret < 0) { | |
245 | /* | |
246 | * Should be sr_err(), but that would yield lots of | |
247 | * "Resource temporarily unavailable" messages. | |
248 | */ | |
249 | sr_spew("FD %d: Read error: %s.", fd, strerror(errno)); | |
250 | } else if ((size_t)ret != count) { | |
251 | sr_spew("FD %d: Only read %d/%d bytes.", fd, ret, count); | |
252 | } | |
253 | ||
254 | return ret; | |
2119ab03 UH |
255 | #endif |
256 | } | |
257 | ||
b19f4622 UH |
258 | /** |
259 | * Create a backup of the current parameters of the specified serial port. | |
260 | * | |
261 | * @param fd File descriptor of the serial port. | |
262 | * | |
263 | * @return Pointer to a struct termios upon success, NULL upon errors. | |
264 | * It is the caller's responsibility to g_free() the pointer if no | |
265 | * longer needed. | |
266 | */ | |
1a081ca6 | 267 | SR_PRIV void *serial_backup_params(int fd) |
d02a535e | 268 | { |
b19f4622 UH |
269 | sr_dbg("FD %d: Creating serial parameters backup.", fd); |
270 | ||
926b866c UH |
271 | #ifdef _WIN32 |
272 | /* TODO */ | |
273 | #else | |
d02a535e BV |
274 | struct termios *term; |
275 | ||
c548332c | 276 | if (!(term = g_try_malloc(sizeof(struct termios)))) { |
b19f4622 UH |
277 | sr_err("termios struct malloc failed."); |
278 | return NULL; | |
c548332c UH |
279 | } |
280 | ||
b19f4622 UH |
281 | /* Returns 0 upon success, -1 upon failure. */ |
282 | if (tcgetattr(fd, term) < 0) { | |
283 | sr_err("FD %d: Error getting serial parameters: %s.", | |
c485db90 | 284 | fd, strerror(errno)); |
b19f4622 UH |
285 | g_free(term); |
286 | return NULL; | |
287 | } | |
d02a535e BV |
288 | |
289 | return term; | |
926b866c | 290 | #endif |
d02a535e BV |
291 | } |
292 | ||
b19f4622 UH |
293 | /** |
294 | * Restore serial port settings from a previously created backup. | |
295 | * | |
296 | * @param fd File descriptor of the serial port. | |
297 | * @param backup Pointer to a struct termios which contains the settings | |
298 | * to restore. | |
299 | * | |
300 | * @return 0 upon success, -1 upon failure. | |
301 | */ | |
302 | SR_PRIV int serial_restore_params(int fd, void *backup) | |
d02a535e | 303 | { |
b19f4622 UH |
304 | sr_dbg("FD %d: Restoring serial parameters from backup.", fd); |
305 | ||
20af6106 UH |
306 | if (!backup) { |
307 | sr_err("FD %d: Cannot restore serial params (NULL).", fd); | |
308 | return -1; | |
309 | } | |
310 | ||
926b866c UH |
311 | #ifdef _WIN32 |
312 | /* TODO */ | |
313 | #else | |
b19f4622 UH |
314 | int ret; |
315 | ||
316 | /* Returns 0 upon success, -1 upon failure. */ | |
317 | if ((ret = tcsetattr(fd, TCSADRAIN, (struct termios *)backup)) < 0) { | |
318 | sr_err("FD %d: Error restoring serial parameters: %s.", | |
c485db90 | 319 | fd, strerror(errno)); |
b19f4622 UH |
320 | } |
321 | ||
322 | return ret; | |
926b866c | 323 | #endif |
d02a535e BV |
324 | } |
325 | ||
b19f4622 UH |
326 | /** |
327 | * Set serial parameters for the specified serial port. | |
328 | * | |
329 | * @param baudrate The baudrate to set. | |
330 | * @param bits The number of data bits to use. | |
331 | * @param parity The parity setting to use (0 = none, 1 = even, 2 = odd). | |
332 | * @param stopbits The number of stop bits to use (1 or 2). | |
333 | * @param flowcontrol The flow control settings to use (0 = none, 1 = RTS/CTS, | |
334 | * 2 = XON/XOFF). | |
2119ab03 | 335 | * |
b19f4622 | 336 | * @return SR_OK upon success, SR_ERR upon failure. |
1ff7712c | 337 | */ |
0abee507 | 338 | SR_PRIV int serial_set_params(int fd, int baudrate, int bits, int parity, |
1a081ca6 | 339 | int stopbits, int flowcontrol) |
d02a535e | 340 | { |
b19f4622 UH |
341 | sr_dbg("FD %d: Setting serial parameters.", fd); |
342 | ||
926b866c UH |
343 | #ifdef _WIN32 |
344 | DCB dcb; | |
345 | ||
346 | if (!GetCommState(hdl, &dcb)) { | |
347 | /* TODO: Error handling. */ | |
e46b8fb1 | 348 | return SR_ERR; |
926b866c UH |
349 | } |
350 | ||
0abee507 | 351 | switch (baudrate) { |
1fdb75e1 | 352 | /* TODO: Support for higher baud rates. */ |
926b866c UH |
353 | case 115200: |
354 | dcb.BaudRate = CBR_115200; | |
355 | break; | |
356 | case 57600: | |
357 | dcb.BaudRate = CBR_57600; | |
358 | break; | |
359 | case 38400: | |
360 | dcb.BaudRate = CBR_38400; | |
361 | break; | |
362 | case 19200: | |
363 | dcb.BaudRate = CBR_19200; | |
364 | break; | |
365 | case 9600: | |
366 | dcb.BaudRate = CBR_9600; | |
367 | break; | |
e8e9dcdd AG |
368 | case 4800: |
369 | dcb.BaudRate = CBR_4800; | |
370 | break; | |
0f708301 AG |
371 | case 2400: |
372 | dcb.BaudRate = CBR_2400; | |
373 | break; | |
926b866c | 374 | default: |
b19f4622 UH |
375 | sr_err("Unsupported baudrate: %d.", baudrate); |
376 | return SR_ERR; | |
926b866c UH |
377 | } |
378 | dcb.ByteSize = bits; | |
379 | dcb.Parity = NOPARITY; /* TODO: Don't hardcode. */ | |
380 | dcb.StopBits = ONESTOPBIT; /* TODO: Don't hardcode. */ | |
381 | ||
382 | if (!SetCommState(hdl, &dcb)) { | |
383 | /* TODO: Error handling. */ | |
e46b8fb1 | 384 | return SR_ERR; |
926b866c UH |
385 | } |
386 | #else | |
d02a535e | 387 | struct termios term; |
1ff7712c | 388 | speed_t baud; |
d02a535e | 389 | |
b19f4622 UH |
390 | sr_dbg("FD %d: Getting terminal settings.", fd); |
391 | if (tcgetattr(fd, &term) < 0) { | |
20af6106 | 392 | sr_err("tcgetattr() error: %s.", strerror(errno)); |
6a6e23ab | 393 | return SR_ERR; |
b19f4622 | 394 | } |
6a6e23ab | 395 | |
0abee507 | 396 | switch (baudrate) { |
b19f4622 UH |
397 | case 50: |
398 | baud = B50; | |
399 | break; | |
400 | case 75: | |
401 | baud = B75; | |
402 | break; | |
403 | case 110: | |
404 | baud = B110; | |
405 | break; | |
406 | case 134: | |
407 | baud = B134; | |
408 | break; | |
409 | case 150: | |
410 | baud = B150; | |
411 | break; | |
412 | case 200: | |
413 | baud = B200; | |
414 | break; | |
415 | case 300: | |
416 | baud = B300; | |
417 | break; | |
418 | case 600: | |
419 | baud = B600; | |
420 | break; | |
421 | case 1200: | |
422 | baud = B1200; | |
423 | break; | |
424 | case 1800: | |
425 | baud = B1800; | |
426 | break; | |
0f708301 AG |
427 | case 2400: |
428 | baud = B2400; | |
429 | break; | |
e8e9dcdd AG |
430 | case 4800: |
431 | baud = B4800; | |
432 | break; | |
1ff7712c DR |
433 | case 9600: |
434 | baud = B9600; | |
435 | break; | |
b19f4622 UH |
436 | case 19200: |
437 | baud = B19200; | |
438 | break; | |
1ff7712c DR |
439 | case 38400: |
440 | baud = B38400; | |
441 | break; | |
442 | case 57600: | |
443 | baud = B57600; | |
444 | break; | |
445 | case 115200: | |
446 | baud = B115200; | |
447 | break; | |
b19f4622 UH |
448 | case 230400: |
449 | baud = B230400; | |
450 | break; | |
9a751023 | 451 | #ifndef __APPLE__ |
1ff7712c DR |
452 | case 460800: |
453 | baud = B460800; | |
454 | break; | |
9a751023 | 455 | #endif |
1ff7712c | 456 | default: |
b19f4622 | 457 | sr_err("Unsupported baudrate: %d.", baudrate); |
e46b8fb1 | 458 | return SR_ERR; |
1ff7712c | 459 | } |
b19f4622 UH |
460 | |
461 | sr_dbg("FD %d: Configuring output baudrate to %d (%d).", | |
462 | fd, baudrate, baud); | |
463 | if (cfsetospeed(&term, baud) < 0) { | |
464 | sr_err("cfsetospeed() error: %ѕ.", strerror(errno)); | |
e46b8fb1 | 465 | return SR_ERR; |
b19f4622 UH |
466 | } |
467 | ||
468 | sr_dbg("FD %d: Configuring input baudrate to %d (%d).", | |
469 | fd, baudrate, baud); | |
470 | if (cfsetispeed(&term, baud) < 0) { | |
471 | sr_err("cfsetispeed() error: %ѕ.", strerror(errno)); | |
e46b8fb1 | 472 | return SR_ERR; |
b19f4622 | 473 | } |
1ff7712c | 474 | |
b19f4622 | 475 | sr_dbg("FD %d: Configuring %d data bits.", fd, bits); |
d02a535e | 476 | term.c_cflag &= ~CSIZE; |
1ff7712c DR |
477 | switch (bits) { |
478 | case 8: | |
479 | term.c_cflag |= CS8; | |
480 | break; | |
481 | case 7: | |
482 | term.c_cflag |= CS7; | |
483 | break; | |
484 | default: | |
b19f4622 | 485 | sr_err("Unsupported data bits number: %d.", bits); |
e46b8fb1 | 486 | return SR_ERR; |
1ff7712c DR |
487 | } |
488 | ||
b19f4622 | 489 | sr_dbg("FD %d: Configuring %d stop bits.", fd, stopbits); |
d02a535e | 490 | term.c_cflag &= ~CSTOPB; |
1ff7712c DR |
491 | switch (stopbits) { |
492 | case 1: | |
b19f4622 | 493 | /* Do nothing, a cleared CSTOPB entry means "1 stop bit". */ |
1ff7712c DR |
494 | break; |
495 | case 2: | |
496 | term.c_cflag |= CSTOPB; | |
d7c776b9 | 497 | break; |
1ff7712c | 498 | default: |
b19f4622 | 499 | sr_err("Unsupported stopbits number: %d.", stopbits); |
e46b8fb1 | 500 | return SR_ERR; |
1ff7712c DR |
501 | } |
502 | ||
f38b9763 BV |
503 | term.c_iflag &= ~(IXON | IXOFF); |
504 | term.c_cflag &= ~CRTSCTS; | |
1ff7712c | 505 | switch (flowcontrol) { |
f38b9763 BV |
506 | case 0: |
507 | /* No flow control. */ | |
b19f4622 | 508 | sr_dbg("FD %d: Configuring no flow control.", fd); |
1ff7712c DR |
509 | break; |
510 | case 1: | |
b19f4622 | 511 | sr_dbg("FD %d: Configuring RTS/CTS flow control.", fd); |
1ff7712c | 512 | term.c_cflag |= CRTSCTS; |
d7c776b9 | 513 | break; |
f38b9763 | 514 | case 2: |
b19f4622 | 515 | sr_dbg("FD %d: Configuring XON/XOFF flow control.", fd); |
f38b9763 BV |
516 | term.c_iflag |= IXON | IXOFF; |
517 | break; | |
1ff7712c | 518 | default: |
b19f4622 | 519 | sr_err("Unsupported flow control setting: %d.", flowcontrol); |
e46b8fb1 | 520 | return SR_ERR; |
1ff7712c DR |
521 | } |
522 | ||
ac4a2ea4 DR |
523 | term.c_iflag &= ~IGNPAR; |
524 | term.c_cflag &= ~(PARODD | PARENB); | |
1ff7712c | 525 | switch (parity) { |
f8c1fcda | 526 | case SERIAL_PARITY_NONE: |
b19f4622 | 527 | sr_dbg("FD %d: Configuring no parity.", fd); |
1ff7712c DR |
528 | term.c_iflag |= IGNPAR; |
529 | break; | |
f8c1fcda | 530 | case SERIAL_PARITY_EVEN: |
b19f4622 | 531 | sr_dbg("FD %d: Configuring even parity.", fd); |
ac4a2ea4 | 532 | term.c_cflag |= PARENB; |
1ff7712c | 533 | break; |
f8c1fcda | 534 | case SERIAL_PARITY_ODD: |
b19f4622 | 535 | sr_dbg("FD %d: Configuring odd parity.", fd); |
ac4a2ea4 | 536 | term.c_cflag |= PARENB | PARODD; |
1ff7712c DR |
537 | break; |
538 | default: | |
b19f4622 | 539 | sr_err("Unsupported parity setting: %d.", parity); |
e46b8fb1 | 540 | return SR_ERR; |
1ff7712c DR |
541 | } |
542 | ||
b19f4622 | 543 | /* Do NOT translate carriage return to newline on input. */ |
fb9d3bf9 | 544 | term.c_iflag &= ~(ICRNL); |
b19f4622 UH |
545 | |
546 | /* Disable canonical mode, and don't echo input characters. */ | |
5c51e098 BV |
547 | term.c_lflag &= ~(ICANON | ECHO); |
548 | ||
b19f4622 UH |
549 | /* Write the configured settings. */ |
550 | if (tcsetattr(fd, TCSADRAIN, &term) < 0) { | |
551 | sr_err("tcsetattr() error: %ѕ.", strerror(errno)); | |
e46b8fb1 | 552 | return SR_ERR; |
b19f4622 | 553 | } |
926b866c | 554 | #endif |
d02a535e | 555 | |
e46b8fb1 | 556 | return SR_OK; |
d02a535e | 557 | } |
792fc686 BV |
558 | |
559 | #define SERIAL_COMM_SPEC "^(\\d+)/([78])([neo])([12])$" | |
560 | SR_PRIV int serial_set_paramstr(int fd, const char *paramstr) | |
561 | { | |
562 | GRegex *reg; | |
563 | GMatchInfo *match; | |
564 | int speed, databits, parity, stopbits; | |
565 | char *mstr; | |
566 | ||
567 | speed = databits = parity = stopbits = 0; | |
568 | reg = g_regex_new(SERIAL_COMM_SPEC, 0, 0, NULL); | |
569 | if (g_regex_match(reg, paramstr, 0, &match)) { | |
570 | if ((mstr = g_match_info_fetch(match, 1))) | |
571 | speed = strtoul(mstr, NULL, 10); | |
572 | g_free(mstr); | |
573 | if ((mstr = g_match_info_fetch(match, 2))) | |
574 | databits = strtoul(mstr, NULL, 10); | |
575 | g_free(mstr); | |
576 | if ((mstr = g_match_info_fetch(match, 3))) { | |
577 | switch (mstr[0]) { | |
578 | case 'n': | |
579 | parity = SERIAL_PARITY_NONE; | |
580 | break; | |
581 | case 'e': | |
582 | parity = SERIAL_PARITY_EVEN; | |
583 | break; | |
584 | case 'o': | |
585 | parity = SERIAL_PARITY_ODD; | |
586 | break; | |
587 | } | |
588 | } | |
589 | g_free(mstr); | |
590 | if ((mstr = g_match_info_fetch(match, 4))) | |
591 | stopbits = strtoul(mstr, NULL, 10); | |
592 | g_free(mstr); | |
593 | } | |
594 | g_match_info_unref(match); | |
595 | g_regex_unref(reg); | |
596 | ||
597 | if (speed) | |
598 | return serial_set_params(fd, speed, databits, parity, stopbits, 0); | |
599 | else | |
600 | return SR_ERR_ARG; | |
601 | } | |
602 | ||
6f22a8ef UH |
603 | SR_PRIV int serial_readline(int fd, char **buf, int *buflen, |
604 | uint64_t timeout_ms) | |
605 | { | |
606 | uint64_t start; | |
607 | int maxlen, len; | |
608 | ||
609 | timeout_ms *= 1000; | |
610 | start = g_get_monotonic_time(); | |
611 | ||
612 | maxlen = *buflen; | |
613 | *buflen = len = 0; | |
614 | while(1) { | |
615 | len = maxlen - *buflen - 1; | |
616 | if (len < 1) | |
617 | break; | |
618 | len = serial_read(fd, *buf + *buflen, 1); | |
619 | if (len > 0) { | |
620 | *buflen += len; | |
621 | *(*buf + *buflen) = '\0'; | |
622 | if (*buflen > 0 && *(*buf + *buflen - 1) == '\r') { | |
623 | /* Strip LF and terminate. */ | |
624 | *(*buf + --*buflen) = '\0'; | |
625 | break; | |
626 | } | |
627 | } | |
628 | if (g_get_monotonic_time() - start > timeout_ms) | |
629 | /* Timeout */ | |
630 | break; | |
631 | g_usleep(2000); | |
632 | } | |
633 | sr_dbg("Received %d: '%s'.", *buflen, *buf); | |
634 | ||
635 | return SR_OK; | |
636 | } |