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) && !defined(HOST_WATCHOS) && !defined(HOST_TVOS)
41 #if defined(HAVE_IOKIT)
43 #include <IOKit/IOKitLib.h>
44 #include <IOKit/serial/IOSerialKeys.h>
45 #include <IOKit/serial/ioss.h>
46 #include <IOKit/IOBSD.h>
49 /* This is a copy of System.IO.Ports.Handshake */
54 RequestToSendXOnXOff
= 3
57 /* This is a copy of System.IO.Ports.Parity */
66 /* This is a copy of System.IO.Ports.StopBits */
74 /* This is a copy of System.IO.Ports.SerialSignal */
77 Cd
= 1, /* Carrier detect */
78 Cts
= 2, /* Clear to send */
79 Dsr
= 4, /* Data set ready */
80 Dtr
= 8, /* Data terminal ready */
81 Rts
= 16 /* Request to send */
85 * Silence the compiler, we do not need these prototypes to be public, since these are only
89 int open_serial (char *devfile
);
90 int close_serial (int unix_fd
);
91 guint32
read_serial (int fd
, guchar
*buffer
, int offset
, int count
);
92 int write_serial (int fd
, guchar
*buffer
, int offset
, int count
, int timeout
);
93 int discard_buffer (int fd
, gboolean input
);
94 gint32
get_bytes_in_buffer (int fd
, gboolean input
);
95 gboolean
is_baud_rate_legal (int baud_rate
);
96 int setup_baud_rate (int baud_rate
, gboolean
*custom_baud_rate
);
97 gboolean
set_attributes (int fd
, int baud_rate
, MonoParity parity
, int dataBits
, MonoStopBits stopBits
, MonoHandshake handshake
);
98 MonoSerialSignal
get_signals (int fd
, gint32
*error
);
99 gint32
set_signal (int fd
, MonoSerialSignal signal
, gboolean value
);
100 int breakprop (int fd
);
101 gboolean
poll_serial (int fd
, gint32
*error
, int timeout
);
102 void *list_serial_devices (void);
105 open_serial (char *devfile
)
108 fd
= open (devfile
, O_RDWR
| O_NOCTTY
| O_NONBLOCK
);
114 close_serial (int unix_fd
)
116 // Linus writes: do not retry close after EINTR
117 return close (unix_fd
);
121 read_serial (int fd
, guchar
*buffer
, int offset
, int count
)
125 n
= read (fd
, buffer
+ offset
, count
);
131 write_serial (int fd
, guchar
*buffer
, int offset
, int count
, int timeout
)
137 pinfo
.events
= POLLOUT
;
138 pinfo
.revents
= POLLOUT
;
149 while ((c
= poll (&pinfo
, 1, timeout
)) == -1 && errno
== EINTR
)
156 t
= write (fd
, buffer
+ offset
, n
);
157 } while (t
== -1 && errno
== EINTR
);
170 discard_buffer (int fd
, gboolean input
)
172 return tcflush(fd
, input
? TCIFLUSH
: TCOFLUSH
);
176 get_bytes_in_buffer (int fd
, gboolean input
)
178 #if defined(__HAIKU__)
179 /* FIXME: Haiku doesn't support TIOCOUTQ nor FIONREAD on fds */
185 if (ioctl (fd
, input
? FIONREAD
: TIOCOUTQ
, &retval
) == -1) {
193 is_baud_rate_legal (int baud_rate
)
195 gboolean ignore
= FALSE
;
196 return setup_baud_rate (baud_rate
, &ignore
) != -1;
200 setup_baud_rate (int baud_rate
, gboolean
*custom_baud_rate
)
204 /*Some values are not defined on OSX, *BSD, or AIX */
287 *custom_baud_rate
= TRUE
;
294 set_attributes (int fd
, int baud_rate
, MonoParity parity
, int dataBits
, MonoStopBits stopBits
, MonoHandshake handshake
)
296 struct termios newtio
;
297 gboolean custom_baud_rate
= FALSE
;
299 if (tcgetattr (fd
, &newtio
) == -1)
302 newtio
.c_cflag
|= (CLOCAL
| CREAD
);
303 newtio
.c_lflag
&= ~(ICANON
| ECHO
| ECHOE
| ECHOK
| ECHONL
| ISIG
| IEXTEN
);
304 newtio
.c_oflag
&= ~(OPOST
);
305 newtio
.c_iflag
= IGNBRK
;
308 baud_rate
= setup_baud_rate (baud_rate
, &custom_baud_rate
);
311 newtio
.c_cflag
&= ~CSIZE
;
315 newtio
.c_cflag
|= CS5
;
318 newtio
.c_cflag
|= CS6
;
321 newtio
.c_cflag
|= CS7
;
325 newtio
.c_cflag
|= CS8
;
336 /* do nothing, the default is one stop bit */
337 newtio
.c_cflag
&= ~CSTOPB
;
340 newtio
.c_cflag
|= CSTOPB
;
342 case OnePointFive
: /* OnePointFive */
343 /* 8250 UART handles stop bit flag as 1.5 stop bits for 5 data bits */
344 newtio
.c_cflag
|= CSTOPB
;
349 newtio
.c_iflag
&= ~(INPCK
| ISTRIP
);
353 case NoneParity
: /* None */
354 newtio
.c_cflag
&= ~(PARENB
| PARODD
);
358 newtio
.c_cflag
|= PARENB
| PARODD
;
361 case Even
: /* Even */
362 newtio
.c_cflag
&= ~(PARODD
);
363 newtio
.c_cflag
|= (PARENB
);
366 case Mark
: /* Mark */
369 case Space
: /* Space */
374 newtio
.c_iflag
&= ~(IXOFF
| IXON
);
376 newtio
.c_cflag
&= ~CRTSCTS
;
377 #endif /* def CRTSCTS */
381 case NoneHandshake
: /* None */
384 case RequestToSend
: /* RequestToSend (RTS) */
386 newtio
.c_cflag
|= CRTSCTS
;
387 #endif /* def CRTSCTS */
389 case RequestToSendXOnXOff
: /* RequestToSendXOnXOff (RTS + XON/XOFF) */
391 newtio
.c_cflag
|= CRTSCTS
;
392 #endif /* def CRTSCTS */
394 case XOnXOff
: /* XOnXOff */
395 newtio
.c_iflag
|= IXOFF
| IXON
;
399 if (custom_baud_rate
== FALSE
) {
400 if (cfsetospeed (&newtio
, baud_rate
) < 0 || cfsetispeed (&newtio
, baud_rate
) < 0)
403 #if __linux__ || defined(HAVE_IOKIT)
405 /* On Linux to set a custom baud rate, we must set the
406 * "standard" baud_rate to 38400. On Apple we set it purely
407 * so that tcsetattr has something to use (and report back later), but
408 * the Apple specific API is still opaque to these APIs, see:
409 * https://developer.apple.com/library/mac/samplecode/SerialPortSample/Listings/SerialPortSample_SerialPortSample_c.html#//apple_ref/doc/uid/DTS10000454-SerialPortSample_SerialPortSample_c-DontLinkElementID_4
411 if (cfsetospeed (&newtio
, B38400
) < 0 || cfsetispeed (&newtio
, B38400
) < 0)
416 if (tcsetattr (fd
, TCSANOW
, &newtio
) < 0)
419 if (custom_baud_rate
== TRUE
){
420 #if defined(HAVE_LINUX_SERIAL_H)
421 struct serial_struct ser
;
423 if (ioctl (fd
, TIOCGSERIAL
, &ser
) < 0)
428 ser
.custom_divisor
= ser
.baud_base
/ baud_rate
;
429 ser
.flags
&= ~ASYNC_SPD_MASK
;
430 ser
.flags
|= ASYNC_SPD_CUST
;
432 if (ioctl (fd
, TIOCSSERIAL
, &ser
) < 0)
436 #elif defined(HAVE_IOKIT)
437 speed_t speed
= baud_rate
;
438 if (ioctl(fd
, IOSSIOSPEED
, &speed
) == -1)
441 /* Don't know how to set custom baud rate on this platform. */
451 get_signal_code (MonoSerialSignal signal
)
472 static MonoSerialSignal
473 get_mono_signal_codes (int signals
)
475 MonoSerialSignal retval
= NoneSignal
;
477 if ((signals
& TIOCM_CAR
) != 0)
479 if ((signals
& TIOCM_CTS
) != 0)
481 if ((signals
& TIOCM_DSR
) != 0)
483 if ((signals
& TIOCM_DTR
) != 0)
485 if ((signals
& TIOCM_RTS
) != 0)
492 get_signals (int fd
, gint32
*error
)
498 if (ioctl (fd
, TIOCMGET
, &signals
) == -1) {
503 return get_mono_signal_codes (signals
);
507 set_signal (int fd
, MonoSerialSignal signal
, gboolean value
)
509 int signals
, expected
, activated
;
511 expected
= get_signal_code (signal
);
512 if (ioctl (fd
, TIOCMGET
, &signals
) == -1)
515 activated
= (signals
& expected
) != 0;
516 if (activated
== value
) /* Already set */
522 signals
&= ~expected
;
524 if (ioctl (fd
, TIOCMSET
, &signals
) == -1)
533 return tcsendbreak (fd
, 0);
537 poll_serial (int fd
, gint32
*error
, int timeout
)
544 pinfo
.events
= POLLIN
;
547 while (poll (&pinfo
, 1, timeout
) == -1 && errno
== EINTR
) {
548 /* EINTR is an OK condition, we should not throw in the upper layer an IOException */
555 return (pinfo
.revents
& POLLIN
) != 0 ? 1 : 0;
559 * mono internals should not be used here.
560 * this serial stuff needs to be implemented with icalls.
561 * make this at least compile until the code is moved elsewhere
562 * defined(linux) is wrong, too
565 list_serial_devices (void)
572 list_serial_devices (void)
576 /* Linux serial files are of the form ttyS[0-9]+ */
577 GSList
*l
, *list
= NULL
;
578 GDir
* dir
= g_dir_open ("/dev", 0, NULL
);
579 const char *filename
;
582 while ((filename
= g_dir_read_name (dir
))) {
584 if (!strncmp (filename
, "ttyS", 4))
585 list
= g_slist_append (list
, g_strconcat ("/dev/", filename
, NULL
));
591 array
= mono_array_new (mono_domain_get (), mono_get_string_class (), g_slist_length (list
));
592 for (l
= list
; l
; l
= l
->next
) {
593 mono_array_set (array
, gpointer
, i
++, mono_string_new (mono_domain_get (), (char*)l
->data
));
600 #warning "list_serial_devices isn't ported to this OS"