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>
16 #if defined(HAVE_POLL_H)
18 #elif defined(HAVE_SYS_POLL_H)
21 #include <sys/ioctl.h>
23 /* This is for ASYNC_*, serial_struct on linux */
24 #if defined(HAVE_LINUX_SERIAL_H)
25 #include <linux/serial.h>
30 /* This is for FIONREAD on solaris */
32 #include <sys/filio.h>
35 /* sys/time.h (for timeval) is required when using osx 10.3 (but not 10.4) */
36 /* IOKit is a private framework in iOS, so exclude there */
37 #if defined(__APPLE__) && !defined(HOST_IOS)
39 #include <IOKit/IOKitLib.h>
40 #include <IOKit/serial/IOSerialKeys.h>
41 #include <IOKit/serial/ioss.h>
42 #include <IOKit/IOBSD.h>
45 /* This is a copy of System.IO.Ports.Handshake */
50 RequestToSendXOnXOff
= 3
53 /* This is a copy of System.IO.Ports.Parity */
62 /* This is a copy of System.IO.Ports.StopBits */
70 /* This is a copy of System.IO.Ports.SerialSignal */
73 Cd
= 1, /* Carrier detect */
74 Cts
= 2, /* Clear to send */
75 Dsr
= 4, /* Data set ready */
76 Dtr
= 8, /* Data terminal ready */
77 Rts
= 16 /* Request to send */
81 * Silence the compiler, we do not need these prototypes to be public, since these are only
85 int open_serial (char *devfile
);
86 int close_serial (int unix_fd
);
87 guint32
read_serial (int fd
, guchar
*buffer
, int offset
, int count
);
88 int write_serial (int fd
, guchar
*buffer
, int offset
, int count
, int timeout
);
89 int discard_buffer (int fd
, gboolean input
);
90 gint32
get_bytes_in_buffer (int fd
, gboolean input
);
91 gboolean
is_baud_rate_legal (int baud_rate
);
92 int setup_baud_rate (int baud_rate
, gboolean
*custom_baud_rate
);
93 gboolean
set_attributes (int fd
, int baud_rate
, MonoParity parity
, int dataBits
, MonoStopBits stopBits
, MonoHandshake handshake
);
94 MonoSerialSignal
get_signals (int fd
, gint32
*error
);
95 gint32
set_signal (int fd
, MonoSerialSignal signal
, gboolean value
);
96 int breakprop (int fd
);
97 gboolean
poll_serial (int fd
, gint32
*error
, int timeout
);
98 void *list_serial_devices (void);
101 open_serial (char *devfile
)
104 fd
= open (devfile
, O_RDWR
| O_NOCTTY
| O_NONBLOCK
);
110 close_serial (int unix_fd
)
112 // Linus writes: do not retry close after EINTR
113 return close (unix_fd
);
117 read_serial (int fd
, guchar
*buffer
, int offset
, int count
)
121 n
= read (fd
, buffer
+ offset
, count
);
127 write_serial (int fd
, guchar
*buffer
, int offset
, int count
, int timeout
)
133 pinfo
.events
= POLLOUT
;
134 pinfo
.revents
= POLLOUT
;
145 while ((c
= poll (&pinfo
, 1, timeout
)) == -1 && errno
== EINTR
)
152 t
= write (fd
, buffer
+ offset
, n
);
153 } while (t
== -1 && errno
== EINTR
);
166 discard_buffer (int fd
, gboolean input
)
168 return tcflush(fd
, input
? TCIFLUSH
: TCOFLUSH
);
172 get_bytes_in_buffer (int fd
, gboolean input
)
176 if (ioctl (fd
, input
? FIONREAD
: TIOCOUTQ
, &retval
) == -1) {
184 is_baud_rate_legal (int baud_rate
)
186 gboolean ignore
= FALSE
;
187 return setup_baud_rate (baud_rate
, &ignore
) != -1;
191 setup_baud_rate (int baud_rate
, gboolean
*custom_baud_rate
)
195 /*Some values are not defined on OSX and *BSD */
272 *custom_baud_rate
= TRUE
;
279 set_attributes (int fd
, int baud_rate
, MonoParity parity
, int dataBits
, MonoStopBits stopBits
, MonoHandshake handshake
)
281 struct termios newtio
;
282 gboolean custom_baud_rate
= FALSE
;
284 if (tcgetattr (fd
, &newtio
) == -1)
287 newtio
.c_cflag
|= (CLOCAL
| CREAD
);
288 newtio
.c_lflag
&= ~(ICANON
| ECHO
| ECHOE
| ECHOK
| ECHONL
| ISIG
| IEXTEN
);
289 newtio
.c_oflag
&= ~(OPOST
);
290 newtio
.c_iflag
= IGNBRK
;
293 baud_rate
= setup_baud_rate (baud_rate
, &custom_baud_rate
);
296 newtio
.c_cflag
&= ~CSIZE
;
300 newtio
.c_cflag
|= CS5
;
303 newtio
.c_cflag
|= CS6
;
306 newtio
.c_cflag
|= CS7
;
310 newtio
.c_cflag
|= CS8
;
321 /* do nothing, the default is one stop bit */
322 newtio
.c_cflag
&= ~CSTOPB
;
325 newtio
.c_cflag
|= CSTOPB
;
327 case OnePointFive
: /* OnePointFive */
333 newtio
.c_iflag
&= ~(INPCK
| ISTRIP
);
337 case NoneParity
: /* None */
338 newtio
.c_cflag
&= ~(PARENB
| PARODD
);
342 newtio
.c_cflag
|= PARENB
| PARODD
;
345 case Even
: /* Even */
346 newtio
.c_cflag
&= ~(PARODD
);
347 newtio
.c_cflag
|= (PARENB
);
350 case Mark
: /* Mark */
353 case Space
: /* Space */
358 newtio
.c_iflag
&= ~(IXOFF
| IXON
);
360 newtio
.c_cflag
&= ~CRTSCTS
;
361 #endif /* def CRTSCTS */
365 case NoneHandshake
: /* None */
368 case RequestToSend
: /* RequestToSend (RTS) */
370 newtio
.c_cflag
|= CRTSCTS
;
371 #endif /* def CRTSCTS */
373 case RequestToSendXOnXOff
: /* RequestToSendXOnXOff (RTS + XON/XOFF) */
375 newtio
.c_cflag
|= CRTSCTS
;
376 #endif /* def CRTSCTS */
378 case XOnXOff
: /* XOnXOff */
379 newtio
.c_iflag
|= IXOFF
| IXON
;
383 if (custom_baud_rate
== FALSE
) {
384 if (cfsetospeed (&newtio
, baud_rate
) < 0 || cfsetispeed (&newtio
, baud_rate
) < 0)
387 #if __linux__ || (defined(__APPLE__) && !defined(HOST_IOS))
389 /* On Linux to set a custom baud rate, we must set the
390 * "standard" baud_rate to 38400. On Apple we set it purely
391 * so that tcsetattr has something to use (and report back later), but
392 * the Apple specific API is still opaque to these APIs, see:
393 * https://developer.apple.com/library/mac/samplecode/SerialPortSample/Listings/SerialPortSample_SerialPortSample_c.html#//apple_ref/doc/uid/DTS10000454-SerialPortSample_SerialPortSample_c-DontLinkElementID_4
395 if (cfsetospeed (&newtio
, B38400
) < 0 || cfsetispeed (&newtio
, B38400
) < 0)
400 if (tcsetattr (fd
, TCSANOW
, &newtio
) < 0)
403 if (custom_baud_rate
== TRUE
){
404 #if defined(HAVE_LINUX_SERIAL_H)
405 struct serial_struct ser
;
407 if (ioctl (fd
, TIOCGSERIAL
, &ser
) < 0)
412 ser
.custom_divisor
= ser
.baud_base
/ baud_rate
;
413 ser
.flags
&= ~ASYNC_SPD_MASK
;
414 ser
.flags
|= ASYNC_SPD_CUST
;
416 if (ioctl (fd
, TIOCSSERIAL
, &ser
) < 0)
420 #elif defined(__APPLE__) && !defined(HOST_IOS)
421 speed_t speed
= baud_rate
;
422 if (ioctl(fd
, IOSSIOSPEED
, &speed
) == -1)
425 /* Don't know how to set custom baud rate on this platform. */
435 get_signal_code (MonoSerialSignal signal
)
456 static MonoSerialSignal
457 get_mono_signal_codes (int signals
)
459 MonoSerialSignal retval
= NoneSignal
;
461 if ((signals
& TIOCM_CAR
) != 0)
463 if ((signals
& TIOCM_CTS
) != 0)
465 if ((signals
& TIOCM_DSR
) != 0)
467 if ((signals
& TIOCM_DTR
) != 0)
469 if ((signals
& TIOCM_RTS
) != 0)
476 get_signals (int fd
, gint32
*error
)
482 if (ioctl (fd
, TIOCMGET
, &signals
) == -1) {
487 return get_mono_signal_codes (signals
);
491 set_signal (int fd
, MonoSerialSignal signal
, gboolean value
)
493 int signals
, expected
, activated
;
495 expected
= get_signal_code (signal
);
496 if (ioctl (fd
, TIOCMGET
, &signals
) == -1)
499 activated
= (signals
& expected
) != 0;
500 if (activated
== value
) /* Already set */
506 signals
&= ~expected
;
508 if (ioctl (fd
, TIOCMSET
, &signals
) == -1)
517 return tcsendbreak (fd
, 0);
521 poll_serial (int fd
, gint32
*error
, int timeout
)
528 pinfo
.events
= POLLIN
;
531 while (poll (&pinfo
, 1, timeout
) == -1 && errno
== EINTR
) {
532 /* EINTR is an OK condition, we should not throw in the upper layer an IOException */
539 return (pinfo
.revents
& POLLIN
) != 0 ? 1 : 0;
543 * mono internals should not be used here.
544 * this serial stuff needs to be implemented with icalls.
545 * make this at least compile until the code is moved elsewhere
546 * defined(linux) is wrong, too
549 list_serial_devices (void)
556 list_serial_devices (void)
560 /* Linux serial files are of the form ttyS[0-9]+ */
561 GSList
*l
, *list
= NULL
;
562 GDir
* dir
= g_dir_open ("/dev", 0, NULL
);
563 const char *filename
;
566 while ((filename
= g_dir_read_name (dir
))) {
568 if (!strncmp (filename
, "ttyS", 4))
569 list
= g_slist_append (list
, g_strconcat ("/dev/", filename
, NULL
));
575 array
= mono_array_new (mono_domain_get (), mono_get_string_class (), g_slist_length (list
));
576 for (l
= list
; l
; l
= l
->next
) {
577 mono_array_set (array
, gpointer
, i
++, mono_string_new (mono_domain_get (), (char*)l
->data
));
584 #warning "list_serial_devices isn't ported to this OS"