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