Re: [Mono-dev] [PATCH] Cache errno for serial port

2008-02-19 Thread Leszek Ciesielski
2008/2/18 Carlos Alberto Cortez [EMAIL PROTECTED]:
 Hey,

 The attached patch adds a 'serial_errno' variable in
 mono/support/serial.c, to cache the last error related to serial port
 functions.

 At first I tried to just create a function directly retrieving errno,
 but it was getting a different value from the original (probably some
 code modified it after the pinvoke call?).

 This is needed to a simple but better error reporting in the
 System.IO.Ports.SerialPort class.

The variable is static. What about cases when multiple serial ports are used?
___
Mono-devel-list mailing list
Mono-devel-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-devel-list


Re: [Mono-dev] [PATCH] Cache errno for serial port

2008-02-19 Thread Miguel de Icaza
Hello Carlos,

 The attached patch adds a 'serial_errno' variable in
 mono/support/serial.c, to cache the last error related to serial port
 functions.
 
 At first I tried to just create a function directly retrieving errno,
 but it was getting a different value from the original (probably some
 code modified it after the pinvoke call?).
 
 This is needed to a simple but better error reporting in the
 System.IO.Ports.SerialPort class.

I think you can use DllImport's built-in SetLastError/GetLastError
framework for this.

Just add this to the DllImport definitions:

[DllImport (something, SetLastError=true)]

And then in the calling code, call:

Marshal.GetLastWin32Error ()

This will contain the errno value.
___
Mono-devel-list mailing list
Mono-devel-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-devel-list


Re: [Mono-dev] [PATCH] Cache errno for serial port

2008-02-19 Thread Carlos Alberto Cortez
Hey Miguel,

Somehow I had this wrong idea that GetLastError would retrieve
windows-specific codes (not the ones errno gets).

Thanks,
Carlos.


El lun, 18-02-2008 a las 20:19 -0500, Miguel de Icaza escribió:
 Hello Carlos,
 
  The attached patch adds a 'serial_errno' variable in
  mono/support/serial.c, to cache the last error related to serial port
  functions.
  
  At first I tried to just create a function directly retrieving errno,
  but it was getting a different value from the original (probably some
  code modified it after the pinvoke call?).
  
  This is needed to a simple but better error reporting in the
  System.IO.Ports.SerialPort class.
 
 I think you can use DllImport's built-in SetLastError/GetLastError
 framework for this.
 
 Just add this to the DllImport definitions:
 
   [DllImport (something, SetLastError=true)]
 
 And then in the calling code, call:
 
   Marshal.GetLastWin32Error ()
 
 This will contain the errno value.

___
Mono-devel-list mailing list
Mono-devel-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-devel-list


[Mono-dev] [PATCH] Cache errno for serial port

2008-02-17 Thread Carlos Alberto Cortez
Hey,

The attached patch adds a 'serial_errno' variable in
mono/support/serial.c, to cache the last error related to serial port
functions.

At first I tried to just create a function directly retrieving errno,
but it was getting a different value from the original (probably some
code modified it after the pinvoke call?).

This is needed to a simple but better error reporting in the
System.IO.Ports.SerialPort class.
 1. 
Carlos.
Index: serial.c
===
--- serial.c	(revisión: 96026)
+++ serial.c	(copia de trabajo)
@@ -60,15 +60,16 @@
 	Rts = 16  /* Request to send */
 } MonoSerialSignal;
 
+/* Last error related to serial port */
+static int serial_errno;
+
 int
 open_serial (char* devfile)
 {
 	int fd;
 	fd = open (devfile, O_RDWR | O_NOCTTY | O_NONBLOCK);
+	serial_errno = errno;
 
-	if (fd == -1)
-		return -1;
-
 	return fd;
 }
 
@@ -79,11 +80,18 @@
 }
 
 guint32
-read_serial (int fd, guchar *buffer, int offset, int count)
+read_serial (int fd, guchar *buffer, int offset, int count, int timeout)
 {
 	guint32 n;
+	struct pollfd ufd;
+	ufd.events = POLLHUP | POLLIN | POLLERR;
+
+	poll (ufd, 1, timeout);
+	if ((ufd.revents  POLLIN) != POLLIN)
+		return -1;
  
 	n = read (fd, buffer + offset, count);
+	serial_errno = errno;
 
 	return (guint32) n;
 }
@@ -114,6 +122,7 @@
 		}		
 
  		t = write(fd, buffer + offset, count);
+		serial_errno = errno;
 		
 		if (timeout  0)
 		{
@@ -142,6 +151,7 @@
 	gint32 retval;
 
 	if (ioctl (fd, input ? FIONREAD : TIOCOUTQ, retval) == -1) {
+		serial_errno = errno;
 		return -1;
 	}
 
@@ -310,6 +320,7 @@
 	if (cfsetospeed (newtio, baud_rate)  0 || cfsetispeed (newtio, baud_rate)  0 ||
 	tcsetattr (fd, TCSANOW, newtio)  0)
 	{
+		serial_errno = errno;
 		return FALSE;
 	}
 	else
@@ -368,6 +379,7 @@
 	*error = 0;
 	
 	if (ioctl (fd, TIOCMGET, signals) == -1) {
+		serial_errno = errno;
 		*error = -1;
 		return NoneSignal;
 	}
@@ -393,8 +405,10 @@
 	else
 		signals = ~expected;
 	
-	if (ioctl (fd, TIOCMSET, signals) == -1)
+	if (ioctl (fd, TIOCMSET, signals) == -1) {
+		serial_errno = errno;
 		return -1;
+	}
 	
 	return 1;
 }
@@ -419,6 +433,7 @@
 	while (poll (pinfo, 1, timeout) == -1  errno == EINTR) {
 		/* EINTR is an OK condition, we should not throw in the upper layer an IOException */
 		if (errno != EINTR){
+			serial_errno = errno;
 			*error = -1;
 			return FALSE;
 		}
@@ -427,6 +442,12 @@
 	return (pinfo.revents  POLLIN) != 0 ? 1 : 0;
 }
 
+gint32
+get_last_serial_error ()
+{
+	return serial_errno;
+}
+
 /*
  * mono internals should not be used here.
  * this serial stuff needs to be implemented with icalls.
___
Mono-devel-list mailing list
Mono-devel-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-devel-list