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 #if defined(__APPLE__)
18 #include <sys/ioctl.h>
22 /* This is for FIONREAD on solaris */
24 #include <sys/filio.h>
27 /* sys/time.h (for timeval) is required when using osx 10.3 (but not 10.4) */
32 /* This is a copy of System.IO.Ports.Handshake */
37 RequestToSendXOnXOff
= 3
40 /* This is a copy of System.IO.Ports.Parity */
49 /* This is a copy of System.IO.Ports.StopBits */
57 /* This is a copy of System.IO.Ports.SerialSignal */
60 Cd
= 1, /* Carrier detect */
61 Cts
= 2, /* Clear to send */
62 Dsr
= 4, /* Data set ready */
63 Dtr
= 8, /* Data terminal ready */
64 Rts
= 16 /* Request to send */
68 open_serial (char* devfile
)
71 fd
= open (devfile
, O_RDWR
| O_NOCTTY
| O_NONBLOCK
);
77 close_serial (int unix_fd
)
79 // Linus writes: do not retry close after EINTR
80 return close (unix_fd
);
84 read_serial (int fd
, guchar
*buffer
, int offset
, int count
)
88 n
= read (fd
, buffer
+ offset
, count
);
94 write_serial (int fd
, guchar
*buffer
, int offset
, int count
, int timeout
)
100 pinfo
.events
= POLLOUT
;
101 pinfo
.revents
= POLLOUT
;
112 while ((c
= poll (&pinfo
, 1, timeout
)) == -1 && errno
== EINTR
)
119 t
= write (fd
, buffer
+ offset
, n
);
120 } while (t
== -1 && errno
== EINTR
);
133 discard_buffer (int fd
, gboolean input
)
135 return tcflush(fd
, input
? TCIFLUSH
: TCOFLUSH
);
139 get_bytes_in_buffer (int fd
, gboolean input
)
143 if (ioctl (fd
, input
? FIONREAD
: TIOCOUTQ
, &retval
) == -1) {
151 is_baud_rate_legal (int baud_rate
)
153 return setup_baud_rate (baud_rate
) != -1;
157 setup_baud_rate (int baud_rate
)
161 /*Some values are not defined on OSX and *BSD */
233 set_attributes (int fd
, int baud_rate
, MonoParity parity
, int dataBits
, MonoStopBits stopBits
, MonoHandshake handshake
)
235 struct termios newtio
;
237 if (tcgetattr (fd
, &newtio
) == -1)
240 newtio
.c_cflag
|= (CLOCAL
| CREAD
);
241 newtio
.c_lflag
&= ~(ICANON
| ECHO
| ECHOE
| ECHOK
| ECHONL
| ISIG
| IEXTEN
);
242 newtio
.c_oflag
&= ~(OPOST
);
243 newtio
.c_iflag
= IGNBRK
;
246 baud_rate
= setup_baud_rate (baud_rate
);
249 newtio
.c_cflag
&= ~CSIZE
;
253 newtio
.c_cflag
|= CS5
;
256 newtio
.c_cflag
|= CS6
;
259 newtio
.c_cflag
|= CS7
;
263 newtio
.c_cflag
|= CS8
;
274 /* do nothing, the default is one stop bit */
275 newtio
.c_cflag
&= ~CSTOPB
;
278 newtio
.c_cflag
|= CSTOPB
;
280 case OnePointFive
: /* OnePointFive */
286 newtio
.c_iflag
&= ~(INPCK
| ISTRIP
);
290 case NoneParity
: /* None */
291 newtio
.c_cflag
&= ~(PARENB
| PARODD
);
295 newtio
.c_cflag
|= PARENB
| PARODD
;
298 case Even
: /* Even */
299 newtio
.c_cflag
&= ~(PARODD
);
300 newtio
.c_cflag
|= (PARENB
);
303 case Mark
: /* Mark */
306 case Space
: /* Space */
311 newtio
.c_iflag
&= ~(IXOFF
| IXON
);
313 newtio
.c_cflag
&= ~CRTSCTS
;
314 #endif /* def CRTSCTS */
318 case NoneHandshake
: /* None */
321 case RequestToSend
: /* RequestToSend (RTS) */
323 newtio
.c_cflag
|= CRTSCTS
;
324 #endif /* def CRTSCTS */
326 case RequestToSendXOnXOff
: /* RequestToSendXOnXOff (RTS + XON/XOFF) */
328 newtio
.c_cflag
|= CRTSCTS
;
329 #endif /* def CRTSCTS */
331 case XOnXOff
: /* XOnXOff */
332 newtio
.c_iflag
|= IXOFF
| IXON
;
336 if (cfsetospeed (&newtio
, baud_rate
) < 0 || cfsetispeed (&newtio
, baud_rate
) < 0 ||
337 tcsetattr (fd
, TCSANOW
, &newtio
) < 0)
349 get_signal_code (MonoSerialSignal signal
)
370 static MonoSerialSignal
371 get_mono_signal_codes (int signals
)
373 MonoSerialSignal retval
= NoneSignal
;
375 if ((signals
& TIOCM_CAR
) != 0)
377 if ((signals
& TIOCM_CTS
) != 0)
379 if ((signals
& TIOCM_DSR
) != 0)
381 if ((signals
& TIOCM_DTR
) != 0)
383 if ((signals
& TIOCM_RTS
) != 0)
390 get_signals (int fd
, gint32
*error
)
396 if (ioctl (fd
, TIOCMGET
, &signals
) == -1) {
401 return get_mono_signal_codes (signals
);
405 set_signal (int fd
, MonoSerialSignal signal
, gboolean value
)
407 int signals
, expected
, activated
;
409 expected
= get_signal_code (signal
);
410 if (ioctl (fd
, TIOCMGET
, &signals
) == -1)
413 activated
= (signals
& expected
) != 0;
414 if (activated
== value
) /* Already set */
420 signals
&= ~expected
;
422 if (ioctl (fd
, TIOCMSET
, &signals
) == -1)
431 return tcsendbreak (fd
, 0);
435 poll_serial (int fd
, gint32
*error
, int timeout
)
442 pinfo
.events
= POLLIN
;
445 while (poll (&pinfo
, 1, timeout
) == -1 && errno
== EINTR
) {
446 /* EINTR is an OK condition, we should not throw in the upper layer an IOException */
453 return (pinfo
.revents
& POLLIN
) != 0 ? 1 : 0;
457 * mono internals should not be used here.
458 * this serial stuff needs to be implemented with icalls.
459 * make this at least compile until the code is moved elsewhere
460 * defined(linux) is wrong, too
463 list_serial_devices (void)
470 list_serial_devices (void)
474 /* Linux serial files are of the form ttyS[0-9]+ */
475 GSList
*l
, *list
= NULL
;
476 GDir
* dir
= g_dir_open ("/dev", 0, NULL
);
477 const char *filename
;
480 while ((filename
= g_dir_read_name (dir
))) {
482 if (!strncmp (filename
, "ttyS", 4))
483 list
= g_slist_append (list
, g_strconcat ("/dev/", filename
, NULL
));
489 array
= mono_array_new (mono_domain_get (), mono_get_string_class (), g_slist_length (list
));
490 for (l
= list
; l
; l
= l
->next
) {
491 mono_array_set (array
, gpointer
, i
++, mono_string_new (mono_domain_get (), (char*)l
->data
));
498 #warning "list_serial_devices isn't ported to this OS"