|
@@ -12,12 +12,6 @@
|
|
|
|
|
|
int serial_setbaud_termios(struct termios *t, int baud);
|
|
|
int serial_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
|
|
|
-int ringbuf_append(struct ring_buf *buf, const char *data, unsigned int len);
|
|
|
-int ringbuf_getline(struct ring_buf *buf, char *data, unsigned int len);
|
|
|
-int ringbuf_getdata(struct ring_buf *buf, char *data, unsigned int len);
|
|
|
-void ringbuf_move(struct ring_buf *buf, unsigned int bytes);
|
|
|
-void ringbuf_reset(struct ring_buf *buf);
|
|
|
-
|
|
|
|
|
|
/* supported baud codes to use with cfsetispeed, cfsetospeed - keep sorted! */
|
|
|
|
|
@@ -181,166 +175,3 @@ int serial_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds
|
|
|
return res;
|
|
|
}
|
|
|
|
|
|
-void serial_communicate(struct serial_device *port, int infd, int outfd) {
|
|
|
- char buf[4096];
|
|
|
- int n;
|
|
|
- int bytes;
|
|
|
- int nfds;
|
|
|
- fd_set read_set;
|
|
|
- fd_set write_set;
|
|
|
- fd_set except_set;
|
|
|
-
|
|
|
- ringbuf_reset(&port->in_buf);
|
|
|
- ringbuf_reset(&port->out_buf);
|
|
|
-
|
|
|
- FD_ZERO(&read_set);
|
|
|
- FD_ZERO(&write_set);
|
|
|
- FD_ZERO(&except_set);
|
|
|
- FD_SET(port->fd, &read_set);
|
|
|
- FD_SET(infd, &read_set);
|
|
|
- FD_SET(port->fd, &except_set);
|
|
|
- FD_SET(infd, &except_set);
|
|
|
- FD_SET(outfd, &except_set);
|
|
|
-
|
|
|
- nfds = port->fd;
|
|
|
- if(infd>nfds)
|
|
|
- nfds = infd;
|
|
|
- if(outfd>nfds)
|
|
|
- nfds = outfd;
|
|
|
- nfds++;
|
|
|
-
|
|
|
- while(select(nfds, &read_set, &write_set, &except_set, NULL)>0) {
|
|
|
- if(FD_ISSET(outfd, &write_set)) {
|
|
|
- n = ringbuf_getdata(&port->in_buf, buf, sizeof(buf));
|
|
|
- n = write(outfd, buf, n);
|
|
|
- ringbuf_move(&port->in_buf, n);
|
|
|
- }
|
|
|
-
|
|
|
- if(FD_ISSET(port->fd, &write_set)) {
|
|
|
- n = ringbuf_getdata(&port->out_buf, buf, sizeof(buf));
|
|
|
- n = write(port->fd, buf, n);
|
|
|
- ringbuf_move(&port->out_buf, n);
|
|
|
- }
|
|
|
-
|
|
|
- if(FD_ISSET(infd, &read_set)) {
|
|
|
- bytes = sizeof(port->out_buf.data) - port->out_buf.size;
|
|
|
- if(bytes>0) {
|
|
|
- n = read(infd, buf, bytes);
|
|
|
- if(n==0)
|
|
|
- break;
|
|
|
- ringbuf_append(&port->out_buf, buf, n);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if(FD_ISSET(port->fd, &read_set)) {
|
|
|
- bytes = sizeof(port->in_buf.data) - port->in_buf.size;
|
|
|
- if(bytes>0) {
|
|
|
- n = read(port->fd, buf, bytes);
|
|
|
- if(n==0)
|
|
|
- break;
|
|
|
- ringbuf_append(&port->in_buf, buf, n);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- FD_ZERO(&write_set);
|
|
|
- FD_ZERO(&read_set);
|
|
|
- FD_ZERO(&except_set);
|
|
|
- FD_SET(port->fd, &read_set);
|
|
|
- FD_SET(infd, &read_set);
|
|
|
- FD_SET(port->fd, &except_set);
|
|
|
- FD_SET(infd, &except_set);
|
|
|
- FD_SET(outfd, &except_set);
|
|
|
- if(port->in_buf.size>0)
|
|
|
- FD_SET(outfd, &write_set);
|
|
|
- if(port->out_buf.size>0)
|
|
|
- FD_SET(port->fd, &write_set);
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-int ringbuf_append(struct ring_buf *buf, const char *data, unsigned int len) {
|
|
|
- unsigned int p;
|
|
|
- unsigned int i;
|
|
|
-
|
|
|
- if((buf->size + len)>sizeof(buf->data))
|
|
|
- return 1;
|
|
|
-
|
|
|
- p = buf->start + buf->size;
|
|
|
-
|
|
|
- for(i=0; i<len; i++) {
|
|
|
- if(p>=sizeof(buf->data))
|
|
|
- p -= sizeof(buf->data);
|
|
|
- buf->data[p++] = data[i];
|
|
|
- }
|
|
|
- buf->size += len;
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-int ringbuf_getline(struct ring_buf *buf, char *data, unsigned int len) {
|
|
|
- unsigned int datap;
|
|
|
- unsigned int p;
|
|
|
- unsigned int i;
|
|
|
- datap = strlen(data);
|
|
|
-
|
|
|
- p = buf->start;
|
|
|
- for(i=0; i<buf->size; i++) {
|
|
|
- if(p>=sizeof(buf->data))
|
|
|
- p -= sizeof(buf->data);
|
|
|
- if(buf->data[p]=='\r' || buf->data[p]=='\n') break;
|
|
|
- p++;
|
|
|
- }
|
|
|
-
|
|
|
- if((len-datap-1)<i) return -1;
|
|
|
- len = ringbuf_getdata(buf, &data[datap], i);
|
|
|
- data[datap+len] = 0;
|
|
|
-
|
|
|
- if(i<buf->size)
|
|
|
- i++;
|
|
|
- if(buf->data[p]=='\r') {
|
|
|
- if((i+1)<buf->size) {
|
|
|
- p++;
|
|
|
- if(p>=sizeof(buf->data))
|
|
|
- p -= sizeof(buf->data);
|
|
|
- if(buf->data[p]=='\n')
|
|
|
- i++;
|
|
|
- }
|
|
|
- }
|
|
|
- ringbuf_move(buf, i);
|
|
|
- if(buf->data[p]!='\n')
|
|
|
- return 0;
|
|
|
-
|
|
|
- return datap+len;
|
|
|
-}
|
|
|
-
|
|
|
-int ringbuf_getdata(struct ring_buf *buf, char *data, unsigned int len) {
|
|
|
- unsigned int p;
|
|
|
- unsigned int i;
|
|
|
-
|
|
|
- if(len>buf->size) len = buf->size;
|
|
|
- p = buf->start;
|
|
|
- for(i=0; i<len; i++) {
|
|
|
- if(p>=sizeof(buf->data))
|
|
|
- p -= sizeof(buf->data);
|
|
|
- data[i] = buf->data[p++];
|
|
|
- }
|
|
|
- return len;
|
|
|
-}
|
|
|
-
|
|
|
-void ringbuf_move(struct ring_buf *buf, unsigned int bytes) {
|
|
|
- unsigned int p;
|
|
|
-
|
|
|
- if(buf->size<bytes) {
|
|
|
- buf->start = 0;
|
|
|
- buf->size = 0;
|
|
|
- } else {
|
|
|
- p = buf->start + bytes;
|
|
|
- if(p>=sizeof(buf->data))
|
|
|
- p -= sizeof(buf->data);
|
|
|
- buf->start = p;
|
|
|
- buf->size -= bytes;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-void ringbuf_reset(struct ring_buf *buf) {
|
|
|
- buf->start = 0;
|
|
|
- buf->size = 0;
|
|
|
-}
|