X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;f=hardware%2Fcommon%2Fserial.c;h=b98d3c95e82f76d5bb573ef3bc1a58e98ab2b1f8;hb=c548332c21acc2588114214b38eaa3237fc3f8b3;hp=6bf0e40270fbb13f95235c787f4cd6d542de496f;hpb=06d64eb880876bd8a81c249e7b2f690eb03e12c1;p=libsigrok.git diff --git a/hardware/common/serial.c b/hardware/common/serial.c index 6bf0e402..b98d3c95 100644 --- a/hardware/common/serial.c +++ b/hardware/common/serial.c @@ -23,7 +23,7 @@ #include #include #ifdef _WIN32 -#include +#include #else #include #include @@ -31,13 +31,14 @@ #include #include #include +#include // FIXME: Must be moved, or rather passed as function argument. #ifdef _WIN32 HANDLE hdl; #endif -char *serial_port_glob[] = { +const char *serial_port_glob[] = { /* Linux */ "/dev/ttyS*", "/dev/ttyUSB*", @@ -51,11 +52,14 @@ char *serial_port_glob[] = { GSList *list_serial_ports(void) { + GSList *ports; + #ifdef _WIN32 /* TODO */ + ports = NULL; + ports = g_slist_append(ports, strdup("COM1")); #else glob_t g; - GSList *ports; unsigned int i, j; ports = NULL; @@ -66,9 +70,9 @@ GSList *list_serial_ports(void) ports = g_slist_append(ports, strdup(g.gl_pathv[j])); globfree(&g); } +#endif return ports; -#endif } int serial_open(const char *pathname, int flags) @@ -86,20 +90,70 @@ int serial_open(const char *pathname, int flags) #endif } +/* + * Close the serial port. + * Returns 0 upon success, -1 upon failure. + */ int serial_close(int fd) { #ifdef _WIN32 - CloseHandle(hdl); + /* Returns non-zero upon success, 0 upon failure. */ + return (CloseHandle(hdl) == 0) ? -1 : 0; #else + /* Returns 0 upon success, -1 upon failure. */ return close(fd); #endif } +/* + * Flush serial port buffers (if any). + * Returns 0 upon success, -1 upon failure. + */ int serial_flush(int fd) { +#ifdef _WIN32 + /* Returns non-zero upon success, 0 upon failure. */ + return (PurgeComm(hdl, PURGE_RXCLEAR | PURGE_TXCLEAR) == 0) ? -1 : 0; +#else + /* Returns 0 upon success, -1 upon failure. */ + return tcflush(fd, TCIOFLUSH); +#endif +} - tcflush(fd, TCIOFLUSH); +/* + * Write a number of bytes to the specified serial port. + * Returns the number of bytes written, or -1 upon failure. + */ +int serial_write(int fd, const void *buf, size_t count) +{ +#ifdef _WIN32 + DWORD tmp = 0; + + /* FIXME */ + /* Returns non-zero upon success, 0 upon failure. */ + WriteFile(hdl, buf, count, &tmp, NULL); +#else + /* Returns the number of bytes written, or -1 upon failure. */ + return write(fd, buf, count); +#endif +} + +/* + * Read a number of bytes from the specified serial port. + * Returns the number of bytes read, or -1 upon failure. + */ +int serial_read(int fd, void *buf, size_t count) +{ +#ifdef _WIN32 + DWORD tmp = 0; + /* FIXME */ + /* 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 } void *serial_backup_params(int fd) @@ -109,7 +163,12 @@ void *serial_backup_params(int fd) #else struct termios *term; - term = malloc(sizeof(struct termios)); + /* 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; + } + tcgetattr(fd, term); return term; @@ -125,7 +184,12 @@ void serial_restore_params(int fd, void *backup) #endif } -/* flowcontrol 1 = rts/cts 2 = xon/xoff */ +/* + * Set serial parameters. + * + * flowcontrol: 1 = rts/cts, 2 = xon/xoff + * parity: 0 = none, 1 = even, 2 = odd + */ int serial_set_params(int fd, int speed, int bits, int parity, int stopbits, int flowcontrol) { @@ -134,10 +198,12 @@ int serial_set_params(int fd, int speed, int bits, int parity, int stopbits, if (!GetCommState(hdl, &dcb)) { /* TODO: Error handling. */ + return SR_ERR; } /* TODO: Rename 'speed' to 'baudrate'. */ switch(speed) { + /* TODO: Support for higher baud rates. */ case 115200: dcb.BaudRate = CBR_115200; break; @@ -163,27 +229,89 @@ int serial_set_params(int fd, int speed, int bits, int parity, int stopbits, if (!SetCommState(hdl, &dcb)) { /* TODO: Error handling. */ + return SR_ERR; } #else struct termios term; + speed_t baud; - /* Only supporting what we need really, currently the OLS driver. */ - if (speed != 115200 || bits != 8 || parity != 0 || stopbits != 1 - || flowcontrol != 2) - return SIGROK_ERR; + switch (speed) { + case 9600: + baud = B9600; + break; + case 38400: + baud = B38400; + break; + case 57600: + baud = B57600; + break; + case 115200: + baud = B115200; + break; + case 460800: + baud = B460800; + break; + default: + return SR_ERR; + } if (tcgetattr(fd, &term) < 0) - return SIGROK_ERR; - if (cfsetispeed(&term, B115200) < 0) - return SIGROK_ERR; + return SR_ERR; + if (cfsetispeed(&term, baud) < 0) + return SR_ERR; + term.c_cflag &= ~CSIZE; - term.c_cflag |= CS8; + switch (bits) { + case 8: + term.c_cflag |= CS8; + break; + case 7: + term.c_cflag |= CS7; + break; + default: + return SR_ERR; + } + term.c_cflag &= ~CSTOPB; - term.c_cflag |= IXON | IXOFF; - term.c_iflag |= IGNPAR; + switch (stopbits) { + case 1: + break; + case 2: + term.c_cflag |= CSTOPB; + default: + return SR_ERR; + } + + term.c_cflag &= ~(IXON | IXOFF | CRTSCTS); + switch (flowcontrol) { + case 2: + term.c_cflag |= IXON | IXOFF; + break; + case 1: + term.c_cflag |= CRTSCTS; + default: + return SR_ERR; + } + + term.c_iflag &= ~IGNPAR; + term.c_cflag &= ~(PARODD | PARENB); + switch (parity) { + case 0: + term.c_iflag |= IGNPAR; + break; + case 1: + term.c_cflag |= PARENB; + break; + case 2: + term.c_cflag |= PARENB | PARODD; + break; + default: + return SR_ERR; + } + if (tcsetattr(fd, TCSADRAIN, &term) < 0) - return SIGROK_ERR; + return SR_ERR; #endif - return SIGROK_OK; + return SR_OK; }