Added exception in case of using nonstandard baud rate on POSIX boxes, which is not...
authorKonrad M. Kruczynski <konrad.kruczynski@gmail.com>
Sun, 1 May 2011 12:20:54 +0000 (14:20 +0200)
committerKonrad M. Kruczynski <konrad.kruczynski@gmail.com>
Sun, 1 May 2011 12:20:54 +0000 (14:20 +0200)
mcs/class/System/System.IO.Ports/SerialPortStream.cs
support/serial.c

index 72494e18582a114cd0b68cc77723ccc503df7d72..0d02d64e95a0673928c60ef543413b543bbfa7f3 100644 (file)
@@ -7,6 +7,7 @@
 //
 // (c) Copyright 2006 Novell, Inc. (http://www.novell.com)
 //
+// Slightly modified by Konrad M. Kruczynski (added baud rate value checking)
 
 
 #if NET_2_0
@@ -34,9 +35,11 @@ namespace System.IO.Ports
                        fd = open_serial (portName);
                        if (fd == -1)
                                ThrowIOException ();
+                               
+                       TryBaudRate (baudRate);
                        
                        if (!set_attributes (fd, baudRate, parity, dataBits, stopBits, handshake))
-                               ThrowIOException (); // Probably Win32Exc for compatibility
+                               ThrowIOException (); // Probably Win32Exc for compatibility                     
 
                        read_timeout = readTimeout;
                        write_timeout = writeTimeout;
@@ -312,6 +315,19 @@ namespace System.IO.Ports
 
                        throw new IOException (error_message);
                }
+               
+               [DllImport ("MonoPosixHelper")]
+               static extern bool is_baud_rate_legal (int baud_rate);
+               
+               private void TryBaudRate (int baudRate)
+               {
+                       if (!is_baud_rate_legal (baudRate))
+                       {
+                               // this kind of exception to be compatible with MSDN API
+                               throw new ArgumentOutOfRangeException ("baudRate",
+                                       "Given baud rate is not supported on this platform.");
+                       }                       
+               }
        }
 }
 
index a6ad955179205d6fcdd750c3a670402f297ae3f1..6a1705fccca2dfd1945c8223f5197dba46e43bef 100644 (file)
@@ -148,19 +148,14 @@ get_bytes_in_buffer (int fd, gboolean input)
 }
 
 gboolean
-set_attributes (int fd, int baud_rate, MonoParity parity, int dataBits, MonoStopBits stopBits, MonoHandshake handshake)
+is_baud_rate_legal (int baud_rate)
 {
-       struct termios 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);
-       newtio.c_iflag = IGNBRK;
+       return setup_baud_rate (baud_rate) != -1;
+}
 
-       /* setup baudrate */
+int
+setup_baud_rate (int baud_rate)
+{
        switch (baud_rate)
        {
 /*Some values are not defined on OSX and *BSD */
@@ -228,9 +223,27 @@ set_attributes (int fd, int baud_rate, MonoParity parity, int dataBits, MonoStop
        case 50:
        case 0:
        default:
-           baud_rate = B9600;
+           baud_rate = -1;
                break;
        }
+       return baud_rate;
+}
+
+gboolean
+set_attributes (int fd, int baud_rate, MonoParity parity, int dataBits, MonoStopBits stopBits, MonoHandshake handshake)
+{
+       struct termios 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);
+       newtio.c_iflag = IGNBRK;
+
+       /* setup baudrate */
+       baud_rate = setup_baud_rate (baud_rate);
 
        /* char lenght */
        newtio.c_cflag &= ~CSIZE;