Bladeren bron

Don't fork, remove ring buffers and pipes

Use direct communication  with the serial port - no ring buffers and no pipes. This makes also creating separate process unnecessary.
Mateusz Bugdalski 13 jaren geleden
bovenliggende
commit
3b724bacbd
3 gewijzigde bestanden met toevoegingen van 7 en 194 verwijderingen
  1. 0 169
      serial.c
  2. 0 2
      serial.h
  3. 7 23
      sprog.c

+ 0 - 169
serial.c

@@ -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;
-}

+ 0 - 2
serial.h

@@ -29,7 +29,5 @@ int serial_setbaud(struct serial_device *port, int baud);
 void serial_setline(struct serial_device *port, int line, int state);
 int serial_read(struct serial_device *port, char *buf, int len, int timeout);
 void serial_write(struct serial_device *port, const char *text);
-void serial_communicate(struct serial_device *port, int infd, int outfd);
-
 
 #endif

+ 7 - 23
sprog.c

@@ -22,33 +22,12 @@ void sprog_sleep(int msec) {
 }
 
 void sprog_communicate(const struct sprog_family *fam, const char *port, int baud) {
-  int pstdin[2];
-  int pstdout[2];
-  int pid;
   void *arg;
   struct serial_device dev;
   
   serial_open(&dev, port, baud);
   arg = fam->setup(&dev);
-  pipe(pstdin);
-  pipe(pstdout);
-  pid = fork();
-  if(pid<0)
-    sprog_error("Unable to fork: %s\n", strerror(errno));
-  else {
-    if(pid!=0) {
-      close(pstdin[0]);
-      close(pstdout[1]);
-      /* serial_communicate(&dev, pstdout[0], pstdin[1]); */
-      waitpid(pid, NULL, 0);
-      fam->close(arg);
-    } else {
-      close(pstdin[1]);
-      close(pstdout[0]);
-      /* sprog_process(fam, arg, pstdin[0], pstdout[1]); */
-      sprog_process(fam, arg, dev.fd, dev.fd);
-    }
-  }
+  sprog_process(fam, arg, dev.fd, dev.fd);
 }
 
 int sprog_waitdata(int timeout) {
@@ -64,8 +43,13 @@ int sprog_waitdata(int timeout) {
 }
 
 void sprog_process(const struct sprog_family *fam, void *arg, int nstdin, int nstdout) {
+  int oldstdin;
+  int oldstdout;
+  oldstdin = dup(0);
+  oldstdout = dup(1);
   dup2(nstdin, 0);
   dup2(nstdout, 1);
   fam->init(arg);
-  exit(0);
+  dup2(oldstdin, 0);
+  dup2(oldstdout, 1);
 }