]>
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)); | |
83e3c368 UH |
129 | } else { |
130 | sr_dbg("Opened serial port '%s' as FD %d.", pathname, fd); | |
b19f4622 UH |
131 | } |
132 | ||
133 | return fd; | |
926b866c | 134 | #endif |
d02a535e BV |
135 | } |
136 | ||
b19f4622 UH |
137 | /** |
138 | * Close the specified serial port. | |
139 | * | |
140 | * @param fd File descriptor of the serial port. | |
141 | * | |
142 | * @return 0 upon success, -1 upon failure. | |
2119ab03 | 143 | */ |
1a081ca6 | 144 | SR_PRIV int serial_close(int fd) |
d02a535e | 145 | { |
b19f4622 UH |
146 | sr_dbg("FD %d: Closing serial port.", fd); |
147 | ||
926b866c | 148 | #ifdef _WIN32 |
2119ab03 UH |
149 | /* Returns non-zero upon success, 0 upon failure. */ |
150 | return (CloseHandle(hdl) == 0) ? -1 : 0; | |
926b866c | 151 | #else |
b19f4622 UH |
152 | int ret; |
153 | ||
2119ab03 | 154 | /* Returns 0 upon success, -1 upon failure. */ |
b19f4622 UH |
155 | if ((ret = close(fd)) < 0) { |
156 | sr_dbg("FD %d: Error closing serial port: %s.", | |
157 | fd, strerror(errno)); | |
158 | } | |
159 | ||
160 | return ret; | |
926b866c | 161 | #endif |
d02a535e BV |
162 | } |
163 | ||
b19f4622 | 164 | /** |
1fdb75e1 | 165 | * Flush serial port buffers (if any). |
b19f4622 UH |
166 | * |
167 | * @param fd File descriptor of the serial port. | |
168 | * | |
169 | * @return 0 upon success, -1 upon failure. | |
1fdb75e1 | 170 | */ |
1a081ca6 | 171 | SR_PRIV int serial_flush(int fd) |
06d64eb8 | 172 | { |
b19f4622 UH |
173 | sr_dbg("FD %d: Flushing serial port.", fd); |
174 | ||
1fdb75e1 UH |
175 | #ifdef _WIN32 |
176 | /* Returns non-zero upon success, 0 upon failure. */ | |
2119ab03 | 177 | return (PurgeComm(hdl, PURGE_RXCLEAR | PURGE_TXCLEAR) == 0) ? -1 : 0; |
1fdb75e1 | 178 | #else |
b19f4622 UH |
179 | int ret; |
180 | ||
1fdb75e1 | 181 | /* Returns 0 upon success, -1 upon failure. */ |
b19f4622 UH |
182 | if ((ret = tcflush(fd, TCIOFLUSH)) < 0) |
183 | sr_err("Error flushing serial port: %s.", strerror(errno)); | |
184 | ||
185 | return ret; | |
1fdb75e1 | 186 | #endif |
06d64eb8 BV |
187 | } |
188 | ||
b19f4622 | 189 | /** |
2119ab03 | 190 | * Write a number of bytes to the specified serial port. |
b19f4622 UH |
191 | * |
192 | * @param fd File descriptor of the serial port. | |
193 | * @param buf Buffer containing the bytes to write. | |
194 | * @param count Number of bytes to write. | |
195 | * | |
196 | * @return The number of bytes written, or -1 upon failure. | |
2119ab03 | 197 | */ |
1a081ca6 | 198 | SR_PRIV int serial_write(int fd, const void *buf, size_t count) |
2119ab03 | 199 | { |
b19f4622 UH |
200 | sr_spew("FD %d: Writing %d bytes.", fd, count); |
201 | ||
2119ab03 UH |
202 | #ifdef _WIN32 |
203 | DWORD tmp = 0; | |
204 | ||
205 | /* FIXME */ | |
206 | /* Returns non-zero upon success, 0 upon failure. */ | |
207 | WriteFile(hdl, buf, count, &tmp, NULL); | |
208 | #else | |
b19f4622 UH |
209 | ssize_t ret; |
210 | ||
2119ab03 | 211 | /* Returns the number of bytes written, or -1 upon failure. */ |
b19f4622 UH |
212 | ret = write(fd, buf, count); |
213 | if (ret < 0) | |
214 | sr_err("FD %d: Write error: %s.", fd, strerror(errno)); | |
215 | else if ((size_t)ret != count) | |
216 | sr_spew("FD %d: Only wrote %d/%d bytes.", fd, ret, count); | |
217 | ||
218 | return ret; | |
2119ab03 UH |
219 | #endif |
220 | } | |
221 | ||
b19f4622 | 222 | /** |
2119ab03 | 223 | * Read a number of bytes from the specified serial port. |
b19f4622 UH |
224 | * |
225 | * @param fd File descriptor of the serial port. | |
226 | * @param buf Buffer where to store the bytes that are read. | |
227 | * @param count The number of bytes to read. | |
228 | * | |
229 | * @return The number of bytes read, or -1 upon failure. | |
2119ab03 | 230 | */ |
1a081ca6 | 231 | SR_PRIV int serial_read(int fd, void *buf, size_t count) |
2119ab03 | 232 | { |
b19f4622 UH |
233 | sr_spew("FD %d: Reading %d bytes.", fd, count); |
234 | ||
2119ab03 UH |
235 | #ifdef _WIN32 |
236 | DWORD tmp = 0; | |
237 | ||
238 | /* FIXME */ | |
239 | /* Returns non-zero upon success, 0 upon failure. */ | |
240 | return ReadFile(hdl, buf, count, &tmp, NULL); | |
241 | #else | |
b19f4622 UH |
242 | ssize_t ret; |
243 | ||
2119ab03 | 244 | /* Returns the number of bytes read, or -1 upon failure. */ |
b19f4622 UH |
245 | ret = read(fd, buf, count); |
246 | if (ret < 0) { | |
247 | /* | |
248 | * Should be sr_err(), but that would yield lots of | |
249 | * "Resource temporarily unavailable" messages. | |
250 | */ | |
251 | sr_spew("FD %d: Read error: %s.", fd, strerror(errno)); | |
252 | } else if ((size_t)ret != count) { | |
253 | sr_spew("FD %d: Only read %d/%d bytes.", fd, ret, count); | |
254 | } | |
255 | ||
256 | return ret; | |
2119ab03 UH |
257 | #endif |
258 | } | |
259 | ||
b19f4622 UH |
260 | /** |
261 | * Create a backup of the current parameters of the specified serial port. | |
262 | * | |
263 | * @param fd File descriptor of the serial port. | |
264 | * | |
265 | * @return Pointer to a struct termios upon success, NULL upon errors. | |
266 | * It is the caller's responsibility to g_free() the pointer if no | |
267 | * longer needed. | |
268 | */ | |
1a081ca6 | 269 | SR_PRIV void *serial_backup_params(int fd) |
d02a535e | 270 | { |
b19f4622 UH |
271 | sr_dbg("FD %d: Creating serial parameters backup.", fd); |
272 | ||
926b866c UH |
273 | #ifdef _WIN32 |
274 | /* TODO */ | |
275 | #else | |
d02a535e BV |
276 | struct termios *term; |
277 | ||
c548332c | 278 | if (!(term = g_try_malloc(sizeof(struct termios)))) { |
b19f4622 UH |
279 | sr_err("termios struct malloc failed."); |
280 | return NULL; | |
c548332c UH |
281 | } |
282 | ||
b19f4622 UH |
283 | /* Returns 0 upon success, -1 upon failure. */ |
284 | if (tcgetattr(fd, term) < 0) { | |
285 | sr_err("FD %d: Error getting serial parameters: %s.", | |
c485db90 | 286 | fd, strerror(errno)); |
b19f4622 UH |
287 | g_free(term); |
288 | return NULL; | |
289 | } | |
d02a535e BV |
290 | |
291 | return term; | |
926b866c | 292 | #endif |
d02a535e BV |
293 | } |
294 | ||
b19f4622 UH |
295 | /** |
296 | * Restore serial port settings from a previously created backup. | |
297 | * | |
298 | * @param fd File descriptor of the serial port. | |
299 | * @param backup Pointer to a struct termios which contains the settings | |
300 | * to restore. | |
301 | * | |
302 | * @return 0 upon success, -1 upon failure. | |
303 | */ | |
304 | SR_PRIV int serial_restore_params(int fd, void *backup) | |
d02a535e | 305 | { |
b19f4622 UH |
306 | sr_dbg("FD %d: Restoring serial parameters from backup.", fd); |
307 | ||
20af6106 UH |
308 | if (!backup) { |
309 | sr_err("FD %d: Cannot restore serial params (NULL).", fd); | |
310 | return -1; | |
311 | } | |
312 | ||
926b866c UH |
313 | #ifdef _WIN32 |
314 | /* TODO */ | |
315 | #else | |
b19f4622 UH |
316 | int ret; |
317 | ||
318 | /* Returns 0 upon success, -1 upon failure. */ | |
319 | if ((ret = tcsetattr(fd, TCSADRAIN, (struct termios *)backup)) < 0) { | |
320 | sr_err("FD %d: Error restoring serial parameters: %s.", | |
c485db90 | 321 | fd, strerror(errno)); |
b19f4622 UH |
322 | } |
323 | ||
324 | return ret; | |
926b866c | 325 | #endif |
d02a535e BV |
326 | } |
327 | ||
b19f4622 UH |
328 | /** |
329 | * Set serial parameters for the specified serial port. | |
330 | * | |
331 | * @param baudrate The baudrate to set. | |
332 | * @param bits The number of data bits to use. | |
333 | * @param parity The parity setting to use (0 = none, 1 = even, 2 = odd). | |
334 | * @param stopbits The number of stop bits to use (1 or 2). | |
335 | * @param flowcontrol The flow control settings to use (0 = none, 1 = RTS/CTS, | |
336 | * 2 = XON/XOFF). | |
2119ab03 | 337 | * |
b19f4622 | 338 | * @return SR_OK upon success, SR_ERR upon failure. |
1ff7712c | 339 | */ |
0abee507 | 340 | SR_PRIV int serial_set_params(int fd, int baudrate, int bits, int parity, |
1a081ca6 | 341 | int stopbits, int flowcontrol) |
d02a535e | 342 | { |
b19f4622 UH |
343 | sr_dbg("FD %d: Setting serial parameters.", fd); |
344 | ||
926b866c UH |
345 | #ifdef _WIN32 |
346 | DCB dcb; | |
347 | ||
348 | if (!GetCommState(hdl, &dcb)) { | |
349 | /* TODO: Error handling. */ | |
e46b8fb1 | 350 | return SR_ERR; |
926b866c UH |
351 | } |
352 | ||
0abee507 | 353 | switch (baudrate) { |
1fdb75e1 | 354 | /* TODO: Support for higher baud rates. */ |
926b866c UH |
355 | case 115200: |
356 | dcb.BaudRate = CBR_115200; | |
357 | break; | |
358 | case 57600: | |
359 | dcb.BaudRate = CBR_57600; | |
360 | break; | |
361 | case 38400: | |
362 | dcb.BaudRate = CBR_38400; | |
363 | break; | |
364 | case 19200: | |
365 | dcb.BaudRate = CBR_19200; | |
366 | break; | |
367 | case 9600: | |
368 | dcb.BaudRate = CBR_9600; | |
369 | break; | |
e8e9dcdd AG |
370 | case 4800: |
371 | dcb.BaudRate = CBR_4800; | |
372 | break; | |
0f708301 AG |
373 | case 2400: |
374 | dcb.BaudRate = CBR_2400; | |
375 | break; | |
926b866c | 376 | default: |
b19f4622 UH |
377 | sr_err("Unsupported baudrate: %d.", baudrate); |
378 | return SR_ERR; | |
926b866c UH |
379 | } |
380 | dcb.ByteSize = bits; | |
381 | dcb.Parity = NOPARITY; /* TODO: Don't hardcode. */ | |
382 | dcb.StopBits = ONESTOPBIT; /* TODO: Don't hardcode. */ | |
383 | ||
384 | if (!SetCommState(hdl, &dcb)) { | |
385 | /* TODO: Error handling. */ | |
e46b8fb1 | 386 | return SR_ERR; |
926b866c UH |
387 | } |
388 | #else | |
d02a535e | 389 | struct termios term; |
1ff7712c | 390 | speed_t baud; |
d02a535e | 391 | |
b19f4622 UH |
392 | sr_dbg("FD %d: Getting terminal settings.", fd); |
393 | if (tcgetattr(fd, &term) < 0) { | |
20af6106 | 394 | sr_err("tcgetattr() error: %s.", strerror(errno)); |
6a6e23ab | 395 | return SR_ERR; |
b19f4622 | 396 | } |
6a6e23ab | 397 | |
0abee507 | 398 | switch (baudrate) { |
b19f4622 UH |
399 | case 50: |
400 | baud = B50; | |
401 | break; | |
402 | case 75: | |
403 | baud = B75; | |
404 | break; | |
405 | case 110: | |
406 | baud = B110; | |
407 | break; | |
408 | case 134: | |
409 | baud = B134; | |
410 | break; | |
411 | case 150: | |
412 | baud = B150; | |
413 | break; | |
414 | case 200: | |
415 | baud = B200; | |
416 | break; | |
417 | case 300: | |
418 | baud = B300; | |
419 | break; | |
420 | case 600: | |
421 | baud = B600; | |
422 | break; | |
423 | case 1200: | |
424 | baud = B1200; | |
425 | break; | |
426 | case 1800: | |
427 | baud = B1800; | |
428 | break; | |
0f708301 AG |
429 | case 2400: |
430 | baud = B2400; | |
431 | break; | |
e8e9dcdd AG |
432 | case 4800: |
433 | baud = B4800; | |
434 | break; | |
1ff7712c DR |
435 | case 9600: |
436 | baud = B9600; | |
437 | break; | |
b19f4622 UH |
438 | case 19200: |
439 | baud = B19200; | |
440 | break; | |
1ff7712c DR |
441 | case 38400: |
442 | baud = B38400; | |
443 | break; | |
444 | case 57600: | |
445 | baud = B57600; | |
446 | break; | |
447 | case 115200: | |
448 | baud = B115200; | |
449 | break; | |
b19f4622 UH |
450 | case 230400: |
451 | baud = B230400; | |
452 | break; | |
9a751023 | 453 | #ifndef __APPLE__ |
1ff7712c DR |
454 | case 460800: |
455 | baud = B460800; | |
456 | break; | |
9a751023 | 457 | #endif |
1ff7712c | 458 | default: |
b19f4622 | 459 | sr_err("Unsupported baudrate: %d.", baudrate); |
e46b8fb1 | 460 | return SR_ERR; |
1ff7712c | 461 | } |
b19f4622 UH |
462 | |
463 | sr_dbg("FD %d: Configuring output baudrate to %d (%d).", | |
464 | fd, baudrate, baud); | |
465 | if (cfsetospeed(&term, baud) < 0) { | |
466 | sr_err("cfsetospeed() error: %ѕ.", strerror(errno)); | |
e46b8fb1 | 467 | return SR_ERR; |
b19f4622 UH |
468 | } |
469 | ||
470 | sr_dbg("FD %d: Configuring input baudrate to %d (%d).", | |
471 | fd, baudrate, baud); | |
472 | if (cfsetispeed(&term, baud) < 0) { | |
473 | sr_err("cfsetispeed() error: %ѕ.", strerror(errno)); | |
e46b8fb1 | 474 | return SR_ERR; |
b19f4622 | 475 | } |
1ff7712c | 476 | |
b19f4622 | 477 | sr_dbg("FD %d: Configuring %d data bits.", fd, bits); |
d02a535e | 478 | term.c_cflag &= ~CSIZE; |
1ff7712c DR |
479 | switch (bits) { |
480 | case 8: | |
481 | term.c_cflag |= CS8; | |
482 | break; | |
483 | case 7: | |
484 | term.c_cflag |= CS7; | |
485 | break; | |
486 | default: | |
b19f4622 | 487 | sr_err("Unsupported data bits number: %d.", bits); |
e46b8fb1 | 488 | return SR_ERR; |
1ff7712c DR |
489 | } |
490 | ||
b19f4622 | 491 | sr_dbg("FD %d: Configuring %d stop bits.", fd, stopbits); |
d02a535e | 492 | term.c_cflag &= ~CSTOPB; |
1ff7712c DR |
493 | switch (stopbits) { |
494 | case 1: | |
b19f4622 | 495 | /* Do nothing, a cleared CSTOPB entry means "1 stop bit". */ |
1ff7712c DR |
496 | break; |
497 | case 2: | |
498 | term.c_cflag |= CSTOPB; | |
d7c776b9 | 499 | break; |
1ff7712c | 500 | default: |
b19f4622 | 501 | sr_err("Unsupported stopbits number: %d.", stopbits); |
e46b8fb1 | 502 | return SR_ERR; |
1ff7712c DR |
503 | } |
504 | ||
f38b9763 BV |
505 | term.c_iflag &= ~(IXON | IXOFF); |
506 | term.c_cflag &= ~CRTSCTS; | |
1ff7712c | 507 | switch (flowcontrol) { |
f38b9763 BV |
508 | case 0: |
509 | /* No flow control. */ | |
b19f4622 | 510 | sr_dbg("FD %d: Configuring no flow control.", fd); |
1ff7712c DR |
511 | break; |
512 | case 1: | |
b19f4622 | 513 | sr_dbg("FD %d: Configuring RTS/CTS flow control.", fd); |
1ff7712c | 514 | term.c_cflag |= CRTSCTS; |
d7c776b9 | 515 | break; |
f38b9763 | 516 | case 2: |
b19f4622 | 517 | sr_dbg("FD %d: Configuring XON/XOFF flow control.", fd); |
f38b9763 BV |
518 | term.c_iflag |= IXON | IXOFF; |
519 | break; | |
1ff7712c | 520 | default: |
b19f4622 | 521 | sr_err("Unsupported flow control setting: %d.", flowcontrol); |
e46b8fb1 | 522 | return SR_ERR; |
1ff7712c DR |
523 | } |
524 | ||
ac4a2ea4 DR |
525 | term.c_iflag &= ~IGNPAR; |
526 | term.c_cflag &= ~(PARODD | PARENB); | |
1ff7712c | 527 | switch (parity) { |
f8c1fcda | 528 | case SERIAL_PARITY_NONE: |
b19f4622 | 529 | sr_dbg("FD %d: Configuring no parity.", fd); |
1ff7712c DR |
530 | term.c_iflag |= IGNPAR; |
531 | break; | |
f8c1fcda | 532 | case SERIAL_PARITY_EVEN: |
b19f4622 | 533 | sr_dbg("FD %d: Configuring even parity.", fd); |
ac4a2ea4 | 534 | term.c_cflag |= PARENB; |
1ff7712c | 535 | break; |
f8c1fcda | 536 | case SERIAL_PARITY_ODD: |
b19f4622 | 537 | sr_dbg("FD %d: Configuring odd parity.", fd); |
ac4a2ea4 | 538 | term.c_cflag |= PARENB | PARODD; |
1ff7712c DR |
539 | break; |
540 | default: | |
b19f4622 | 541 | sr_err("Unsupported parity setting: %d.", parity); |
e46b8fb1 | 542 | return SR_ERR; |
1ff7712c DR |
543 | } |
544 | ||
b19f4622 | 545 | /* Do NOT translate carriage return to newline on input. */ |
fb9d3bf9 | 546 | term.c_iflag &= ~(ICRNL); |
b19f4622 UH |
547 | |
548 | /* Disable canonical mode, and don't echo input characters. */ | |
5c51e098 BV |
549 | term.c_lflag &= ~(ICANON | ECHO); |
550 | ||
b19f4622 UH |
551 | /* Write the configured settings. */ |
552 | if (tcsetattr(fd, TCSADRAIN, &term) < 0) { | |
553 | sr_err("tcsetattr() error: %ѕ.", strerror(errno)); | |
e46b8fb1 | 554 | return SR_ERR; |
b19f4622 | 555 | } |
926b866c | 556 | #endif |
d02a535e | 557 | |
e46b8fb1 | 558 | return SR_OK; |
d02a535e | 559 | } |
792fc686 BV |
560 | |
561 | #define SERIAL_COMM_SPEC "^(\\d+)/([78])([neo])([12])$" | |
562 | SR_PRIV int serial_set_paramstr(int fd, const char *paramstr) | |
563 | { | |
564 | GRegex *reg; | |
565 | GMatchInfo *match; | |
566 | int speed, databits, parity, stopbits; | |
567 | char *mstr; | |
568 | ||
569 | speed = databits = parity = stopbits = 0; | |
570 | reg = g_regex_new(SERIAL_COMM_SPEC, 0, 0, NULL); | |
571 | if (g_regex_match(reg, paramstr, 0, &match)) { | |
572 | if ((mstr = g_match_info_fetch(match, 1))) | |
573 | speed = strtoul(mstr, NULL, 10); | |
574 | g_free(mstr); | |
575 | if ((mstr = g_match_info_fetch(match, 2))) | |
576 | databits = strtoul(mstr, NULL, 10); | |
577 | g_free(mstr); | |
578 | if ((mstr = g_match_info_fetch(match, 3))) { | |
579 | switch (mstr[0]) { | |
580 | case 'n': | |
581 | parity = SERIAL_PARITY_NONE; | |
582 | break; | |
583 | case 'e': | |
584 | parity = SERIAL_PARITY_EVEN; | |
585 | break; | |
586 | case 'o': | |
587 | parity = SERIAL_PARITY_ODD; | |
588 | break; | |
589 | } | |
590 | } | |
591 | g_free(mstr); | |
592 | if ((mstr = g_match_info_fetch(match, 4))) | |
593 | stopbits = strtoul(mstr, NULL, 10); | |
594 | g_free(mstr); | |
595 | } | |
596 | g_match_info_unref(match); | |
597 | g_regex_unref(reg); | |
598 | ||
599 | if (speed) | |
600 | return serial_set_params(fd, speed, databits, parity, stopbits, 0); | |
601 | else | |
602 | return SR_ERR_ARG; | |
603 | } | |
604 | ||
6f22a8ef | 605 | SR_PRIV int serial_readline(int fd, char **buf, int *buflen, |
b87f8504 | 606 | gint64 timeout_ms) |
6f22a8ef | 607 | { |
b87f8504 | 608 | gint64 start; |
6f22a8ef UH |
609 | int maxlen, len; |
610 | ||
611 | timeout_ms *= 1000; | |
612 | start = g_get_monotonic_time(); | |
613 | ||
614 | maxlen = *buflen; | |
615 | *buflen = len = 0; | |
616 | while(1) { | |
617 | len = maxlen - *buflen - 1; | |
618 | if (len < 1) | |
619 | break; | |
620 | len = serial_read(fd, *buf + *buflen, 1); | |
621 | if (len > 0) { | |
622 | *buflen += len; | |
623 | *(*buf + *buflen) = '\0'; | |
624 | if (*buflen > 0 && *(*buf + *buflen - 1) == '\r') { | |
625 | /* Strip LF and terminate. */ | |
626 | *(*buf + --*buflen) = '\0'; | |
627 | break; | |
628 | } | |
629 | } | |
630 | if (g_get_monotonic_time() - start > timeout_ms) | |
631 | /* Timeout */ | |
632 | break; | |
633 | g_usleep(2000); | |
634 | } | |
635 | sr_dbg("Received %d: '%s'.", *buflen, *buf); | |
636 | ||
637 | return SR_OK; | |
638 | } |