]> sigrok.org Git - libsigrok.git/blame - hardware/common/serial.c
serial: More error-checking & logging, add baudrates.
[libsigrok.git] / hardware / common / serial.c
CommitLineData
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 49static HANDLE hdl;
926b866c
UH
50#endif
51
1a081ca6 52static 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 64SR_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 102SR_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 142SR_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 169SR_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 196SR_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 229SR_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 267SR_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.",
284 strerror(errno));
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 */
302SR_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
926b866c
UH
306#ifdef _WIN32
307 /* TODO */
308#else
b19f4622
UH
309 int ret;
310
311 /* Returns 0 upon success, -1 upon failure. */
312 if ((ret = tcsetattr(fd, TCSADRAIN, (struct termios *)backup)) < 0) {
313 sr_err("FD %d: Error restoring serial parameters: %s.",
314 strerror(errno));
315 }
316
317 return ret;
926b866c 318#endif
d02a535e
BV
319}
320
b19f4622
UH
321/**
322 * Set serial parameters for the specified serial port.
323 *
324 * @param baudrate The baudrate to set.
325 * @param bits The number of data bits to use.
326 * @param parity The parity setting to use (0 = none, 1 = even, 2 = odd).
327 * @param stopbits The number of stop bits to use (1 or 2).
328 * @param flowcontrol The flow control settings to use (0 = none, 1 = RTS/CTS,
329 * 2 = XON/XOFF).
2119ab03 330 *
b19f4622 331 * @return SR_OK upon success, SR_ERR upon failure.
1ff7712c 332 */
0abee507 333SR_PRIV int serial_set_params(int fd, int baudrate, int bits, int parity,
1a081ca6 334 int stopbits, int flowcontrol)
d02a535e 335{
b19f4622
UH
336 sr_dbg("FD %d: Setting serial parameters.", fd);
337
926b866c
UH
338#ifdef _WIN32
339 DCB dcb;
340
341 if (!GetCommState(hdl, &dcb)) {
342 /* TODO: Error handling. */
e46b8fb1 343 return SR_ERR;
926b866c
UH
344 }
345
0abee507 346 switch (baudrate) {
1fdb75e1 347 /* TODO: Support for higher baud rates. */
926b866c
UH
348 case 115200:
349 dcb.BaudRate = CBR_115200;
350 break;
351 case 57600:
352 dcb.BaudRate = CBR_57600;
353 break;
354 case 38400:
355 dcb.BaudRate = CBR_38400;
356 break;
357 case 19200:
358 dcb.BaudRate = CBR_19200;
359 break;
360 case 9600:
361 dcb.BaudRate = CBR_9600;
362 break;
e8e9dcdd
AG
363 case 4800:
364 dcb.BaudRate = CBR_4800;
365 break;
0f708301
AG
366 case 2400:
367 dcb.BaudRate = CBR_2400;
368 break;
926b866c 369 default:
b19f4622
UH
370 sr_err("Unsupported baudrate: %d.", baudrate);
371 return SR_ERR;
926b866c
UH
372 }
373 dcb.ByteSize = bits;
374 dcb.Parity = NOPARITY; /* TODO: Don't hardcode. */
375 dcb.StopBits = ONESTOPBIT; /* TODO: Don't hardcode. */
376
377 if (!SetCommState(hdl, &dcb)) {
378 /* TODO: Error handling. */
e46b8fb1 379 return SR_ERR;
926b866c
UH
380 }
381#else
d02a535e 382 struct termios term;
1ff7712c 383 speed_t baud;
d02a535e 384
b19f4622
UH
385 sr_dbg("FD %d: Getting terminal settings.", fd);
386 if (tcgetattr(fd, &term) < 0) {
387 sr_err("tcgetattr() error: %ѕ.", strerror(errno));
6a6e23ab 388 return SR_ERR;
b19f4622 389 }
6a6e23ab 390
0abee507 391 switch (baudrate) {
b19f4622
UH
392 case 50:
393 baud = B50;
394 break;
395 case 75:
396 baud = B75;
397 break;
398 case 110:
399 baud = B110;
400 break;
401 case 134:
402 baud = B134;
403 break;
404 case 150:
405 baud = B150;
406 break;
407 case 200:
408 baud = B200;
409 break;
410 case 300:
411 baud = B300;
412 break;
413 case 600:
414 baud = B600;
415 break;
416 case 1200:
417 baud = B1200;
418 break;
419 case 1800:
420 baud = B1800;
421 break;
0f708301
AG
422 case 2400:
423 baud = B2400;
424 break;
e8e9dcdd
AG
425 case 4800:
426 baud = B4800;
427 break;
1ff7712c
DR
428 case 9600:
429 baud = B9600;
430 break;
b19f4622
UH
431 case 19200:
432 baud = B19200;
433 break;
1ff7712c
DR
434 case 38400:
435 baud = B38400;
436 break;
437 case 57600:
438 baud = B57600;
439 break;
440 case 115200:
441 baud = B115200;
442 break;
b19f4622
UH
443 case 230400:
444 baud = B230400;
445 break;
9a751023 446#ifndef __APPLE__
1ff7712c
DR
447 case 460800:
448 baud = B460800;
449 break;
9a751023 450#endif
1ff7712c 451 default:
b19f4622 452 sr_err("Unsupported baudrate: %d.", baudrate);
e46b8fb1 453 return SR_ERR;
1ff7712c 454 }
b19f4622
UH
455
456 sr_dbg("FD %d: Configuring output baudrate to %d (%d).",
457 fd, baudrate, baud);
458 if (cfsetospeed(&term, baud) < 0) {
459 sr_err("cfsetospeed() error: %ѕ.", strerror(errno));
e46b8fb1 460 return SR_ERR;
b19f4622
UH
461 }
462
463 sr_dbg("FD %d: Configuring input baudrate to %d (%d).",
464 fd, baudrate, baud);
465 if (cfsetispeed(&term, baud) < 0) {
466 sr_err("cfsetispeed() error: %ѕ.", strerror(errno));
e46b8fb1 467 return SR_ERR;
b19f4622 468 }
1ff7712c 469
b19f4622 470 sr_dbg("FD %d: Configuring %d data bits.", fd, bits);
d02a535e 471 term.c_cflag &= ~CSIZE;
1ff7712c
DR
472 switch (bits) {
473 case 8:
474 term.c_cflag |= CS8;
475 break;
476 case 7:
477 term.c_cflag |= CS7;
478 break;
479 default:
b19f4622 480 sr_err("Unsupported data bits number: %d.", bits);
e46b8fb1 481 return SR_ERR;
1ff7712c
DR
482 }
483
b19f4622 484 sr_dbg("FD %d: Configuring %d stop bits.", fd, stopbits);
d02a535e 485 term.c_cflag &= ~CSTOPB;
1ff7712c
DR
486 switch (stopbits) {
487 case 1:
b19f4622 488 /* Do nothing, a cleared CSTOPB entry means "1 stop bit". */
1ff7712c
DR
489 break;
490 case 2:
491 term.c_cflag |= CSTOPB;
d7c776b9 492 break;
1ff7712c 493 default:
b19f4622 494 sr_err("Unsupported stopbits number: %d.", stopbits);
e46b8fb1 495 return SR_ERR;
1ff7712c
DR
496 }
497
f38b9763
BV
498 term.c_iflag &= ~(IXON | IXOFF);
499 term.c_cflag &= ~CRTSCTS;
1ff7712c 500 switch (flowcontrol) {
f38b9763
BV
501 case 0:
502 /* No flow control. */
b19f4622 503 sr_dbg("FD %d: Configuring no flow control.", fd);
1ff7712c
DR
504 break;
505 case 1:
b19f4622 506 sr_dbg("FD %d: Configuring RTS/CTS flow control.", fd);
1ff7712c 507 term.c_cflag |= CRTSCTS;
d7c776b9 508 break;
f38b9763 509 case 2:
b19f4622 510 sr_dbg("FD %d: Configuring XON/XOFF flow control.", fd);
f38b9763
BV
511 term.c_iflag |= IXON | IXOFF;
512 break;
1ff7712c 513 default:
b19f4622 514 sr_err("Unsupported flow control setting: %d.", flowcontrol);
e46b8fb1 515 return SR_ERR;
1ff7712c
DR
516 }
517
ac4a2ea4
DR
518 term.c_iflag &= ~IGNPAR;
519 term.c_cflag &= ~(PARODD | PARENB);
1ff7712c 520 switch (parity) {
f8c1fcda 521 case SERIAL_PARITY_NONE:
b19f4622 522 sr_dbg("FD %d: Configuring no parity.", fd);
1ff7712c
DR
523 term.c_iflag |= IGNPAR;
524 break;
f8c1fcda 525 case SERIAL_PARITY_EVEN:
b19f4622 526 sr_dbg("FD %d: Configuring even parity.", fd);
ac4a2ea4 527 term.c_cflag |= PARENB;
1ff7712c 528 break;
f8c1fcda 529 case SERIAL_PARITY_ODD:
b19f4622 530 sr_dbg("FD %d: Configuring odd parity.", fd);
ac4a2ea4 531 term.c_cflag |= PARENB | PARODD;
1ff7712c
DR
532 break;
533 default:
b19f4622 534 sr_err("Unsupported parity setting: %d.", parity);
e46b8fb1 535 return SR_ERR;
1ff7712c
DR
536 }
537
b19f4622 538 /* Do NOT translate carriage return to newline on input. */
fb9d3bf9 539 term.c_iflag &= ~(ICRNL);
b19f4622
UH
540
541 /* Disable canonical mode, and don't echo input characters. */
5c51e098
BV
542 term.c_lflag &= ~(ICANON | ECHO);
543
b19f4622
UH
544 /* Write the configured settings. */
545 if (tcsetattr(fd, TCSADRAIN, &term) < 0) {
546 sr_err("tcsetattr() error: %ѕ.", strerror(errno));
e46b8fb1 547 return SR_ERR;
b19f4622 548 }
926b866c 549#endif
d02a535e 550
e46b8fb1 551 return SR_OK;
d02a535e 552}
792fc686
BV
553
554#define SERIAL_COMM_SPEC "^(\\d+)/([78])([neo])([12])$"
555SR_PRIV int serial_set_paramstr(int fd, const char *paramstr)
556{
557 GRegex *reg;
558 GMatchInfo *match;
559 int speed, databits, parity, stopbits;
560 char *mstr;
561
562 speed = databits = parity = stopbits = 0;
563 reg = g_regex_new(SERIAL_COMM_SPEC, 0, 0, NULL);
564 if (g_regex_match(reg, paramstr, 0, &match)) {
565 if ((mstr = g_match_info_fetch(match, 1)))
566 speed = strtoul(mstr, NULL, 10);
567 g_free(mstr);
568 if ((mstr = g_match_info_fetch(match, 2)))
569 databits = strtoul(mstr, NULL, 10);
570 g_free(mstr);
571 if ((mstr = g_match_info_fetch(match, 3))) {
572 switch (mstr[0]) {
573 case 'n':
574 parity = SERIAL_PARITY_NONE;
575 break;
576 case 'e':
577 parity = SERIAL_PARITY_EVEN;
578 break;
579 case 'o':
580 parity = SERIAL_PARITY_ODD;
581 break;
582 }
583 }
584 g_free(mstr);
585 if ((mstr = g_match_info_fetch(match, 4)))
586 stopbits = strtoul(mstr, NULL, 10);
587 g_free(mstr);
588 }
589 g_match_info_unref(match);
590 g_regex_unref(reg);
591
592 if (speed)
593 return serial_set_params(fd, speed, databits, parity, stopbits, 0);
594 else
595 return SR_ERR_ARG;
596}
597
6f22a8ef
UH
598SR_PRIV int serial_readline(int fd, char **buf, int *buflen,
599 uint64_t timeout_ms)
600{
601 uint64_t start;
602 int maxlen, len;
603
604 timeout_ms *= 1000;
605 start = g_get_monotonic_time();
606
607 maxlen = *buflen;
608 *buflen = len = 0;
609 while(1) {
610 len = maxlen - *buflen - 1;
611 if (len < 1)
612 break;
613 len = serial_read(fd, *buf + *buflen, 1);
614 if (len > 0) {
615 *buflen += len;
616 *(*buf + *buflen) = '\0';
617 if (*buflen > 0 && *(*buf + *buflen - 1) == '\r') {
618 /* Strip LF and terminate. */
619 *(*buf + --*buflen) = '\0';
620 break;
621 }
622 }
623 if (g_get_monotonic_time() - start > timeout_ms)
624 /* Timeout */
625 break;
626 g_usleep(2000);
627 }
628 sr_dbg("Received %d: '%s'.", *buflen, *buf);
629
630 return SR_OK;
631}