[xbuild] DirectoryScanner.HasWildcard - new method
[mono.git] / support / serial.c
index 9443e0bb92301756c8d80c3db36ed31dafcec662..a6ad955179205d6fcdd750c3a670402f297ae3f1 100644 (file)
@@ -9,9 +9,13 @@
 #include <unistd.h>
 #include <fcntl.h>
 #include <string.h>
+#include <errno.h>
+#if defined(__APPLE__)
+#include "fakepoll.h"
+#else
 #include <sys/poll.h>
+#endif
 #include <sys/ioctl.h>
-#include <errno.h>
 
 #include <glib.h>
 
@@ -66,16 +70,14 @@ open_serial (char* devfile)
        int fd;
        fd = open (devfile, O_RDWR | O_NOCTTY | O_NONBLOCK);
 
-       if (fd == -1)
-               return -1;
-
        return fd;
 }
 
-void
+int
 close_serial (int unix_fd)
 {
-       close (unix_fd);
+       // Linus writes: do not retry close after EINTR
+       return close (unix_fd);
 }
 
 guint32
@@ -91,38 +93,35 @@ read_serial (int fd, guchar *buffer, int offset, int count)
 int
 write_serial (int fd, guchar *buffer, int offset, int count, int timeout)
 {
-       struct timeval tmval;
-       fd_set writefs;
+       struct pollfd pinfo;
        guint32 n;
 
-       n = count - offset;
+       pinfo.fd = fd;
+       pinfo.events = POLLOUT;
+       pinfo.revents = POLLOUT;
+
+       n = count;
 
-       FD_SET(fd, &writefs);
-       tmval.tv_sec = timeout / 1000;
-       tmval.tv_usec = (timeout - tmval.tv_sec) * 1000;        
-       
        while (n > 0)
        {
                size_t t;
                        
-               if (timeout > 0)
-               {
-                       if (select(fd+1, NULL, &writefs, NULL, &tmval) <= 0 && errno != EINTR)
-                       {
+               if (timeout != 0) {
+                       int c;
+                       
+                       while ((c = poll (&pinfo, 1, timeout)) == -1 && errno == EINTR)
+                               ;
+                       if (c == -1)
                                return -1;
-                       }
                }               
 
-               t = write(fd, buffer + offset, count);
-               
-               if (timeout > 0)
-               {
-                       if (select(fd+1, NULL, &writefs, NULL, &tmval) <= 0  && errno != EINTR)
-                       {
-                               return -1;
-                       }
-       }
+               do {
+                       t = write (fd, buffer + offset, n);
+               } while (t == -1 && errno == EINTR);
 
+               if (t < 0)
+                       return -1;
+               
                offset += t;
                n -= t; 
        }
@@ -130,10 +129,10 @@ write_serial (int fd, guchar *buffer, int offset, int count, int timeout)
        return 0;
 }
 
-void
+int
 discard_buffer (int fd, gboolean input)
 {
-       tcflush(fd, input ? TCIFLUSH : TCOFLUSH);
+       return tcflush(fd, input ? TCIFLUSH : TCOFLUSH);
 }
 
 gint32
@@ -153,7 +152,9 @@ set_attributes (int fd, int baud_rate, MonoParity parity, int dataBits, MonoStop
 {
        struct termios newtio;
 
-       tcgetattr (fd, &newtio);
+       if (tcgetattr (fd, &newtio) == -1)
+               return FALSE;
+
        newtio.c_cflag |=  (CLOCAL | CREAD);
        newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ISIG | IEXTEN );
        newtio.c_oflag &= ~(OPOST);
@@ -162,6 +163,17 @@ set_attributes (int fd, int baud_rate, MonoParity parity, int dataBits, MonoStop
        /* setup baudrate */
        switch (baud_rate)
        {
+/*Some values are not defined on OSX and *BSD */
+#if defined(B921600)
+       case 921600:
+           baud_rate = B921600;
+           break;
+#endif
+#if defined(B460800)
+       case 460800:
+           baud_rate = B460800;
+           break;
+#endif
        case 230400: 
            baud_rate = B230400;
            break;
@@ -272,6 +284,7 @@ set_attributes (int fd, int baud_rate, MonoParity parity, int dataBits, MonoStop
            
        case Even: /* Even */
            newtio.c_cflag &= ~(PARODD);
+           newtio.c_cflag |= (PARENB);
            break;
            
        case Mark: /* Mark */
@@ -399,10 +412,10 @@ set_signal (int fd, MonoSerialSignal signal, gboolean value)
        return 1;
 }
 
-void
+int
 breakprop (int fd)
 {
-       tcsendbreak (fd, 0);
+       return tcsendbreak (fd, 0);
 }
 
 gboolean