1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 /* serial port functions
5 * Author: Chris Toshok <toshok@ximian.com>
13 #include <sys/ioctl.h>
18 /* This is for FIONREAD on solaris */
20 #include <sys/filio.h>
23 /* sys/time.h (for timeval) is required when using osx 10.3 (but not 10.4) */
28 /* This is a copy of System.IO.Ports.Handshake */
33 RequestToSendXOnXOff
= 3
36 /* This is a copy of System.IO.Ports.Parity */
45 /* This is a copy of System.IO.Ports.StopBits */
53 /* This is a copy of System.IO.Ports.SerialSignal */
56 Cd
= 1, /* Carrier detect */
57 Cts
= 2, /* Clear to send */
58 Dsr
= 4, /* Data set ready */
59 Dtr
= 8, /* Data terminal ready */
60 Rts
= 16 /* Request to send */
64 open_serial (char* devfile
)
67 fd
= open (devfile
, O_RDWR
| O_NOCTTY
| O_NONBLOCK
);
76 close_serial (int unix_fd
)
82 read_serial (int fd
, guchar
*buffer
, int offset
, int count
)
86 n
= read (fd
, buffer
+ offset
, count
);
92 write_serial (int fd
, guchar
*buffer
, int offset
, int count
, int timeout
)
98 pinfo
.events
= POLLOUT
;
99 pinfo
.revents
= POLLOUT
;
110 while ((c
= poll (&pinfo
, 1, timeout
)) == -1 && errno
== EINTR
)
117 t
= write (fd
, buffer
+ offset
, n
);
118 } while (t
== -1 && errno
== EINTR
);
131 discard_buffer (int fd
, gboolean input
)
133 tcflush(fd
, input
? TCIFLUSH
: TCOFLUSH
);
137 get_bytes_in_buffer (int fd
, gboolean input
)
141 if (ioctl (fd
, input
? FIONREAD
: TIOCOUTQ
, &retval
) == -1) {
149 set_attributes (int fd
, int baud_rate
, MonoParity parity
, int dataBits
, MonoStopBits stopBits
, MonoHandshake handshake
)
151 struct termios newtio
;
153 tcgetattr (fd
, &newtio
);
154 newtio
.c_cflag
|= (CLOCAL
| CREAD
);
155 newtio
.c_lflag
&= ~(ICANON
| ECHO
| ECHOE
| ECHOK
| ECHONL
| ISIG
| IEXTEN
);
156 newtio
.c_oflag
&= ~(OPOST
);
157 newtio
.c_iflag
= IGNBRK
;
221 newtio
.c_cflag
&= ~CSIZE
;
225 newtio
.c_cflag
|= CS5
;
228 newtio
.c_cflag
|= CS6
;
231 newtio
.c_cflag
|= CS7
;
235 newtio
.c_cflag
|= CS8
;
246 /* do nothing, the default is one stop bit */
247 newtio
.c_cflag
&= ~CSTOPB
;
250 newtio
.c_cflag
|= CSTOPB
;
252 case OnePointFive
: /* OnePointFive */
258 newtio
.c_iflag
&= ~(INPCK
| ISTRIP
);
262 case NoneParity
: /* None */
263 newtio
.c_cflag
&= ~(PARENB
| PARODD
);
267 newtio
.c_cflag
|= PARENB
| PARODD
;
270 case Even
: /* Even */
271 newtio
.c_cflag
&= ~(PARODD
);
274 case Mark
: /* Mark */
277 case Space
: /* Space */
282 newtio
.c_iflag
&= ~(IXOFF
| IXON
);
284 newtio
.c_cflag
&= ~CRTSCTS
;
285 #endif /* def CRTSCTS */
289 case NoneHandshake
: /* None */
292 case RequestToSend
: /* RequestToSend (RTS) */
294 newtio
.c_cflag
|= CRTSCTS
;
295 #endif /* def CRTSCTS */
297 case RequestToSendXOnXOff
: /* RequestToSendXOnXOff (RTS + XON/XOFF) */
299 newtio
.c_cflag
|= CRTSCTS
;
300 #endif /* def CRTSCTS */
302 case XOnXOff
: /* XOnXOff */
303 newtio
.c_iflag
|= IXOFF
| IXON
;
307 if (cfsetospeed (&newtio
, baud_rate
) < 0 || cfsetispeed (&newtio
, baud_rate
) < 0 ||
308 tcsetattr (fd
, TCSANOW
, &newtio
) < 0)
320 get_signal_code (MonoSerialSignal signal
)
341 static MonoSerialSignal
342 get_mono_signal_codes (int signals
)
344 MonoSerialSignal retval
= NoneSignal
;
346 if ((signals
& TIOCM_CAR
) != 0)
348 if ((signals
& TIOCM_CTS
) != 0)
350 if ((signals
& TIOCM_DSR
) != 0)
352 if ((signals
& TIOCM_DTR
) != 0)
354 if ((signals
& TIOCM_RTS
) != 0)
361 get_signals (int fd
, gint32
*error
)
367 if (ioctl (fd
, TIOCMGET
, &signals
) == -1) {
372 return get_mono_signal_codes (signals
);
376 set_signal (int fd
, MonoSerialSignal signal
, gboolean value
)
378 int signals
, expected
, activated
;
380 expected
= get_signal_code (signal
);
381 if (ioctl (fd
, TIOCMGET
, &signals
) == -1)
384 activated
= (signals
& expected
) != 0;
385 if (activated
== value
) /* Already set */
391 signals
&= ~expected
;
393 if (ioctl (fd
, TIOCMSET
, &signals
) == -1)
406 poll_serial (int fd
, gint32
*error
, int timeout
)
413 pinfo
.events
= POLLIN
;
416 while (poll (&pinfo
, 1, timeout
) == -1 && errno
== EINTR
) {
417 /* EINTR is an OK condition, we should not throw in the upper layer an IOException */
424 return (pinfo
.revents
& POLLIN
) != 0 ? 1 : 0;
428 * mono internals should not be used here.
429 * this serial stuff needs to be implemented with icalls.
430 * make this at least compile until the code is moved elsewhere
431 * defined(linux) is wrong, too
434 list_serial_devices (void)
441 list_serial_devices (void)
445 /* Linux serial files are of the form ttyS[0-9]+ */
446 GSList
*l
, *list
= NULL
;
447 GDir
* dir
= g_dir_open ("/dev", 0, NULL
);
448 const char *filename
;
451 while ((filename
= g_dir_read_name (dir
))) {
453 if (!strncmp (filename
, "ttyS", 4))
454 list
= g_slist_append (list
, g_strconcat ("/dev/", filename
, NULL
));
460 array
= mono_array_new (mono_domain_get (), mono_get_string_class (), g_slist_length (list
));
461 for (l
= list
; l
; l
= l
->next
) {
462 mono_array_set (array
, gpointer
, i
++, mono_string_new (mono_domain_get (), (char*)l
->data
));
469 #warning "list_serial_devices isn't ported to this OS"