X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=hardware%2Fcommon%2Fserial.c;h=239c718125a911fb8f74d9e2e8820f0f7d987ec0;hb=a99e0d2a0c9d1bb4db5623ba50f83486238ee793;hp=651b58269c74cf6ccee09484afe83f714ec426c4;hpb=0f7083019449c1d88a5916bae766f3e51f7f8373;p=libsigrok.git diff --git a/hardware/common/serial.c b/hardware/common/serial.c index 651b5826..239c7181 100644 --- a/hardware/common/serial.c +++ b/hardware/common/serial.c @@ -2,6 +2,7 @@ * This file is part of the sigrok project. * * Copyright (C) 2010-2012 Bert Vermeulen + * Copyright (C) 2010-2012 Uwe Hermann * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -29,100 +30,129 @@ #include #endif #include +#include #include #include "libsigrok.h" #include "libsigrok-internal.h" +/* Message logging helpers with driver-specific prefix string. */ +#define DRIVER_LOG_DOMAIN "serial: " +#define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args) +#define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args) +#define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args) +#define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args) +#define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args) +#define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args) + // FIXME: Must be moved, or rather passed as function argument. #ifdef _WIN32 static HANDLE hdl; #endif -static const char *serial_port_glob[] = { - /* Linux */ - "/dev/ttyS*", - "/dev/ttyUSB*", - "/dev/ttyACM*", - /* MacOS X */ - "/dev/ttys*", - "/dev/tty.USB-*", - "/dev/tty.Modem-*", - NULL, -}; - -SR_PRIV GSList *list_serial_ports(void) +/** + * Open the specified serial port. + * + * @param pathname OS-specific serial port specification. Examples: + * "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1". + * @param flags Flags to use when opening the serial port. + * + * @return 0 upon success, -1 upon failure. + */ +SR_PRIV int serial_open(const char *pathname, int flags) { - GSList *ports; - -#ifdef _WIN32 - /* TODO */ - ports = NULL; - ports = g_slist_append(ports, g_strdup("COM1")); -#else - glob_t g; - unsigned int i, j; - - ports = NULL; - for (i = 0; serial_port_glob[i]; i++) { - if (glob(serial_port_glob[i], 0, NULL, &g)) - continue; - for (j = 0; j < g.gl_pathc; j++) - ports = g_slist_append(ports, g_strdup(g.gl_pathv[j])); - globfree(&g); - } -#endif + /* TODO: Abstract 'flags', currently they're OS-specific! */ - return ports; -} + sr_dbg("Opening serial port '%s' (flags = %d).", pathname, flags); -SR_PRIV int serial_open(const char *pathname, int flags) -{ #ifdef _WIN32 - /* FIXME: Don't hardcode COM1. */ - hdl = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, 0, + pathname = "COM1"; /* FIXME: Don't hardcode COM1. */ + + hdl = CreateFile(pathname, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); if (hdl == INVALID_HANDLE_VALUE) { - /* TODO: Error handling. */ + sr_err("Error opening serial port '%s'.", pathname); + return -1; } return 0; #else - return open(pathname, flags); + int fd; + + if ((fd = open(pathname, flags)) < 0) { + /* + * Should be sr_err(), but since some drivers try to open all + * ports on a system and see if they succeed, this would + * yield ugly output for e.g. "sigrok-cli -D". + */ + sr_dbg("Error opening serial port '%s': %s.", pathname, + strerror(errno)); + } else { + sr_dbg("Opened serial port '%s' as FD %d.", pathname, fd); + } + + return fd; #endif } -/* - * Close the serial port. - * Returns 0 upon success, -1 upon failure. +/** + * Close the specified serial port. + * + * @param fd File descriptor of the serial port. + * + * @return 0 upon success, -1 upon failure. */ SR_PRIV int serial_close(int fd) { + sr_dbg("FD %d: Closing serial port.", fd); + #ifdef _WIN32 /* Returns non-zero upon success, 0 upon failure. */ return (CloseHandle(hdl) == 0) ? -1 : 0; #else + int ret; + /* Returns 0 upon success, -1 upon failure. */ - return close(fd); + if ((ret = close(fd)) < 0) { + sr_dbg("FD %d: Error closing serial port: %s.", + fd, strerror(errno)); + } + + return ret; #endif } -/* +/** * Flush serial port buffers (if any). - * Returns 0 upon success, -1 upon failure. + * + * @param fd File descriptor of the serial port. + * + * @return 0 upon success, -1 upon failure. */ SR_PRIV int serial_flush(int fd) { + sr_dbg("FD %d: Flushing serial port.", fd); + #ifdef _WIN32 /* Returns non-zero upon success, 0 upon failure. */ return (PurgeComm(hdl, PURGE_RXCLEAR | PURGE_TXCLEAR) == 0) ? -1 : 0; #else + int ret; + /* Returns 0 upon success, -1 upon failure. */ - return tcflush(fd, TCIOFLUSH); + if ((ret = tcflush(fd, TCIOFLUSH)) < 0) + sr_err("Error flushing serial port: %s.", strerror(errno)); + + return ret; #endif } -/* +/** * Write a number of bytes to the specified serial port. - * Returns the number of bytes written, or -1 upon failure. + * + * @param fd File descriptor of the serial port. + * @param buf Buffer containing the bytes to write. + * @param count Number of bytes to write. + * + * @return The number of bytes written, or -1 upon failure. */ SR_PRIV int serial_write(int fd, const void *buf, size_t count) { @@ -133,14 +163,27 @@ SR_PRIV int serial_write(int fd, const void *buf, size_t count) /* Returns non-zero upon success, 0 upon failure. */ WriteFile(hdl, buf, count, &tmp, NULL); #else + ssize_t ret; + /* Returns the number of bytes written, or -1 upon failure. */ - return write(fd, buf, count); + ret = write(fd, buf, count); + if (ret < 0) + sr_err("FD %d: Write error: %s.", fd, strerror(errno)); + else + sr_spew("FD %d: Wrote %d/%d bytes.", fd, ret, count); + + return ret; #endif } -/* +/** * Read a number of bytes from the specified serial port. - * Returns the number of bytes read, or -1 upon failure. + * + * @param fd File descriptor of the serial port. + * @param buf Buffer where to store the bytes that are read. + * @param count The number of bytes to read. + * + * @return The number of bytes read, or -1 upon failure. */ SR_PRIV int serial_read(int fd, void *buf, size_t count) { @@ -151,48 +194,41 @@ SR_PRIV int serial_read(int fd, void *buf, size_t count) /* Returns non-zero upon success, 0 upon failure. */ return ReadFile(hdl, buf, count, &tmp, NULL); #else - /* Returns the number of bytes read, or -1 upon failure. */ - return read(fd, buf, count); -#endif -} - -SR_PRIV void *serial_backup_params(int fd) -{ -#ifdef _WIN32 - /* TODO */ -#else - struct termios *term; + ssize_t ret; - /* TODO: 'term' is never g_free()'d? */ - if (!(term = g_try_malloc(sizeof(struct termios)))) { - sr_err("serial: %s: term malloc failed", __func__); - return NULL; + /* Returns the number of bytes read, or -1 upon failure. */ + ret = read(fd, buf, count); + if (ret < 0) { + /* + * Should be sr_err(), but that would yield lots of + * "Resource temporarily unavailable" messages. + */ + sr_spew("FD %d: Read error: %s.", fd, strerror(errno)); + } else { + sr_spew("FD %d: Read %d/%d bytes.", fd, ret, count); } - tcgetattr(fd, term); - - return term; -#endif -} - -SR_PRIV void serial_restore_params(int fd, void *backup) -{ -#ifdef _WIN32 - /* TODO */ -#else - tcsetattr(fd, TCSADRAIN, (struct termios *)backup); + return ret; #endif } -/* - * Set serial parameters. +/** + * Set serial parameters for the specified serial port. + * + * @param baudrate The baudrate to set. + * @param bits The number of data bits to use. + * @param parity The parity setting to use (0 = none, 1 = even, 2 = odd). + * @param stopbits The number of stop bits to use (1 or 2). + * @param flowcontrol The flow control settings to use (0 = none, 1 = RTS/CTS, + * 2 = XON/XOFF). * - * flowcontrol: 1 = rts/cts, 2 = xon/xoff - * parity: 0 = none, 1 = even, 2 = odd + * @return SR_OK upon success, SR_ERR upon failure. */ SR_PRIV int serial_set_params(int fd, int baudrate, int bits, int parity, int stopbits, int flowcontrol) { + sr_dbg("FD %d: Setting serial parameters.", fd); + #ifdef _WIN32 DCB dcb; @@ -225,8 +261,8 @@ SR_PRIV int serial_set_params(int fd, int baudrate, int bits, int parity, dcb.BaudRate = CBR_2400; break; default: - /* TODO: Error handling. */ - break; + sr_err("Unsupported baudrate: %d.", baudrate); + return SR_ERR; } dcb.ByteSize = bits; dcb.Parity = NOPARITY; /* TODO: Don't hardcode. */ @@ -240,10 +276,43 @@ SR_PRIV int serial_set_params(int fd, int baudrate, int bits, int parity, struct termios term; speed_t baud; - if (tcgetattr(fd, &term) < 0) + sr_dbg("FD %d: Getting terminal settings.", fd); + if (tcgetattr(fd, &term) < 0) { + sr_err("tcgetattr() error: %s.", strerror(errno)); return SR_ERR; + } switch (baudrate) { + case 50: + baud = B50; + break; + case 75: + baud = B75; + break; + case 110: + baud = B110; + break; + case 134: + baud = B134; + break; + case 150: + baud = B150; + break; + case 200: + baud = B200; + break; + case 300: + baud = B300; + break; + case 600: + baud = B600; + break; + case 1200: + baud = B1200; + break; + case 1800: + baud = B1800; + break; case 2400: baud = B2400; break; @@ -253,6 +322,9 @@ SR_PRIV int serial_set_params(int fd, int baudrate, int bits, int parity, case 9600: baud = B9600; break; + case 19200: + baud = B19200; + break; case 38400: baud = B38400; break; @@ -262,19 +334,34 @@ SR_PRIV int serial_set_params(int fd, int baudrate, int bits, int parity, case 115200: baud = B115200; break; + case 230400: + baud = B230400; + break; #ifndef __APPLE__ case 460800: baud = B460800; break; #endif default: + sr_err("Unsupported baudrate: %d.", baudrate); return SR_ERR; } - if (cfsetospeed(&term, baud) < 0) + + sr_dbg("FD %d: Configuring output baudrate to %d (%d).", + fd, baudrate, baud); + if (cfsetospeed(&term, baud) < 0) { + sr_err("cfsetospeed() error: %ѕ.", strerror(errno)); return SR_ERR; - if (cfsetispeed(&term, baud) < 0) + } + + sr_dbg("FD %d: Configuring input baudrate to %d (%d).", + fd, baudrate, baud); + if (cfsetispeed(&term, baud) < 0) { + sr_err("cfsetispeed() error: %ѕ.", strerror(errno)); return SR_ERR; + } + sr_dbg("FD %d: Configuring %d data bits.", fd, bits); term.c_cflag &= ~CSIZE; switch (bits) { case 8: @@ -284,16 +371,21 @@ SR_PRIV int serial_set_params(int fd, int baudrate, int bits, int parity, term.c_cflag |= CS7; break; default: + sr_err("Unsupported data bits number: %d.", bits); return SR_ERR; } + sr_dbg("FD %d: Configuring %d stop bits.", fd, stopbits); term.c_cflag &= ~CSTOPB; switch (stopbits) { case 1: + /* Do nothing, a cleared CSTOPB entry means "1 stop bit". */ break; case 2: term.c_cflag |= CSTOPB; + break; default: + sr_err("Unsupported stopbits number: %d.", stopbits); return SR_ERR; } @@ -302,13 +394,18 @@ SR_PRIV int serial_set_params(int fd, int baudrate, int bits, int parity, switch (flowcontrol) { case 0: /* No flow control. */ + sr_dbg("FD %d: Configuring no flow control.", fd); break; case 1: + sr_dbg("FD %d: Configuring RTS/CTS flow control.", fd); term.c_cflag |= CRTSCTS; + break; case 2: + sr_dbg("FD %d: Configuring XON/XOFF flow control.", fd); term.c_iflag |= IXON | IXOFF; break; default: + sr_err("Unsupported flow control setting: %d.", flowcontrol); return SR_ERR; } @@ -316,24 +413,33 @@ SR_PRIV int serial_set_params(int fd, int baudrate, int bits, int parity, term.c_cflag &= ~(PARODD | PARENB); switch (parity) { case SERIAL_PARITY_NONE: + sr_dbg("FD %d: Configuring no parity.", fd); term.c_iflag |= IGNPAR; break; case SERIAL_PARITY_EVEN: + sr_dbg("FD %d: Configuring even parity.", fd); term.c_cflag |= PARENB; break; case SERIAL_PARITY_ODD: + sr_dbg("FD %d: Configuring odd parity.", fd); term.c_cflag |= PARENB | PARODD; break; default: + sr_err("Unsupported parity setting: %d.", parity); return SR_ERR; } - /* Some default parameters */ + /* Do NOT translate carriage return to newline on input. */ term.c_iflag &= ~(ICRNL); + + /* Disable canonical mode, and don't echo input characters. */ term.c_lflag &= ~(ICANON | ECHO); - if (tcsetattr(fd, TCSADRAIN, &term) < 0) + /* Write the configured settings. */ + if (tcsetattr(fd, TCSADRAIN, &term) < 0) { + sr_err("tcsetattr() error: %ѕ.", strerror(errno)); return SR_ERR; + } #endif return SR_OK; @@ -383,3 +489,39 @@ SR_PRIV int serial_set_paramstr(int fd, const char *paramstr) return SR_ERR_ARG; } +SR_PRIV int serial_readline(int fd, char **buf, int *buflen, + gint64 timeout_ms) +{ + gint64 start; + int maxlen, len; + + timeout_ms *= 1000; + start = g_get_monotonic_time(); + + maxlen = *buflen; + *buflen = len = 0; + while(1) { + len = maxlen - *buflen - 1; + if (len < 1) + break; + len = serial_read(fd, *buf + *buflen, 1); + if (len > 0) { + *buflen += len; + *(*buf + *buflen) = '\0'; + if (*buflen > 0 && (*(*buf + *buflen - 1) == '\r' + || *(*buf + *buflen - 1) == '\n')) { + /* Strip CR/LF and terminate. */ + *(*buf + --*buflen) = '\0'; + break; + } + } + if (g_get_monotonic_time() - start > timeout_ms) + /* Timeout */ + break; + g_usleep(2000); + } + if (*buflen) + sr_dbg("Received %d: '%s'.", *buflen, *buf); + + return SR_OK; +}