X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Funix%2Fserial.c;h=22978e42ed246e0c0e73e33a7151f496ce891b25;hb=fa8d935b2ba4e1fddee7404dc6fd68de18d8c649;hp=fe8aeb31744194accd3be82cc8c6984f9e4f3cee;hpb=b592e337adbb150e09d3e256a4066734698df3cf;p=smouse diff --git a/src/unix/serial.c b/src/unix/serial.c index fe8aeb3..22978e4 100644 --- a/src/unix/serial.c +++ b/src/unix/serial.c @@ -22,11 +22,10 @@ int ser_open(const char *port, int baud, unsigned int mode) return -1; } - if((fd = open(port, O_RDWR | O_NONBLOCK | O_NOCTTY)) == -1) { + if((fd = open(port, O_RDWR | O_NOCTTY)) == -1) { return -1; } - /*memset(&term, 0, sizeof term);*/ tcgetattr(fd, &term); term.c_oflag = 0; @@ -34,10 +33,13 @@ int ser_open(const char *port, int baud, unsigned int mode) term.c_cc[VMIN] = 0; term.c_cc[VTIME] = 0; - term.c_cflag = CLOCAL | CREAD | CS8 | HUPCL | CRTSCTS; + term.c_cflag = CLOCAL | CREAD | CS8 | HUPCL; if(mode & SER_8N2) { term.c_cflag |= CSTOPB; } + if(mode & SER_HWFLOW) { + term.c_cflag |= CRTSCTS; + } term.c_iflag = IGNBRK | IGNPAR; @@ -50,7 +52,9 @@ int ser_open(const char *port, int baud, unsigned int mode) return -1; } - if(mode & SER_HWFLOW) { +#ifdef TIOCM_RTS + /* assert DTR/RTS lines */ + { int st; if(ioctl(fd, TIOCMGET, &st) == -1) { perror("ser_open: failed to get modem status"); @@ -64,6 +68,7 @@ int ser_open(const char *port, int baud, unsigned int mode) return -1; } } +#endif return fd; } @@ -73,6 +78,16 @@ void ser_close(int fd) close(fd); } +int ser_block(int fd) +{ + return fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) & ~O_NONBLOCK); +} + +int ser_nonblock(int fd) +{ + return fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK); +} + int ser_pending(int fd) { static struct timeval tv_zero; @@ -136,8 +151,7 @@ char *ser_getline(int fd, char *buf, int bsz) { static char linebuf[512]; static int widx; - int i, rd, size; - char *ptr; + int i, rd, size, offs; size = sizeof linebuf - widx; while(size && (rd = read(fd, linebuf + widx, size)) > 0) { @@ -145,22 +159,18 @@ char *ser_getline(int fd, char *buf, int bsz) size -= rd; } - ptr = linebuf; - for(i=0; i= bsz ? bsz - 1 : widx; + for(i=0; i= bsz ? bsz - 1 : i; memcpy(buf, linebuf, size); buf[size] = 0; - memmove(linebuf, linebuf + widx, sizeof linebuf - widx); + offs = i + 1; + memmove(linebuf, linebuf + offs, widx - offs); + widx -= offs; return buf; - } else { - ++ptr; } } return 0;