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_APPLETVOS)
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 and *BSD */
281 *custom_baud_rate = TRUE;
288 set_attributes (int fd, int baud_rate, MonoParity parity, int dataBits, MonoStopBits stopBits, MonoHandshake handshake)
290 struct termios newtio;
291 gboolean custom_baud_rate = FALSE;
293 if (tcgetattr (fd, &newtio) == -1)
296 newtio.c_cflag |= (CLOCAL | CREAD);
297 newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ISIG | IEXTEN );
298 newtio.c_oflag &= ~(OPOST);
299 newtio.c_iflag = IGNBRK;
302 baud_rate = setup_baud_rate (baud_rate, &custom_baud_rate);
305 newtio.c_cflag &= ~CSIZE;
309 newtio.c_cflag |= CS5;
312 newtio.c_cflag |= CS6;
315 newtio.c_cflag |= CS7;
319 newtio.c_cflag |= CS8;
330 /* do nothing, the default is one stop bit */
331 newtio.c_cflag &= ~CSTOPB;
334 newtio.c_cflag |= CSTOPB;
336 case OnePointFive: /* OnePointFive */
342 newtio.c_iflag &= ~(INPCK | ISTRIP );
346 case NoneParity: /* None */
347 newtio.c_cflag &= ~(PARENB | PARODD);
351 newtio.c_cflag |= PARENB | PARODD;
354 case Even: /* Even */
355 newtio.c_cflag &= ~(PARODD);
356 newtio.c_cflag |= (PARENB);
359 case Mark: /* Mark */
362 case Space: /* Space */
367 newtio.c_iflag &= ~(IXOFF | IXON);
369 newtio.c_cflag &= ~CRTSCTS;
370 #endif /* def CRTSCTS */
374 case NoneHandshake: /* None */
377 case RequestToSend: /* RequestToSend (RTS) */
379 newtio.c_cflag |= CRTSCTS;
380 #endif /* def CRTSCTS */
382 case RequestToSendXOnXOff: /* RequestToSendXOnXOff (RTS + XON/XOFF) */
384 newtio.c_cflag |= CRTSCTS;
385 #endif /* def CRTSCTS */
387 case XOnXOff: /* XOnXOff */
388 newtio.c_iflag |= IXOFF | IXON;
392 if (custom_baud_rate == FALSE) {
393 if (cfsetospeed (&newtio, baud_rate) < 0 || cfsetispeed (&newtio, baud_rate) < 0)
396 #if __linux__ || defined(HAVE_IOKIT)
398 /* On Linux to set a custom baud rate, we must set the
399 * "standard" baud_rate to 38400. On Apple we set it purely
400 * so that tcsetattr has something to use (and report back later), but
401 * the Apple specific API is still opaque to these APIs, see:
402 * https://developer.apple.com/library/mac/samplecode/SerialPortSample/Listings/SerialPortSample_SerialPortSample_c.html#//apple_ref/doc/uid/DTS10000454-SerialPortSample_SerialPortSample_c-DontLinkElementID_4
404 if (cfsetospeed (&newtio, B38400) < 0 || cfsetispeed (&newtio, B38400) < 0)
409 if (tcsetattr (fd, TCSANOW, &newtio) < 0)
412 if (custom_baud_rate == TRUE){
413 #if defined(HAVE_LINUX_SERIAL_H)
414 struct serial_struct ser;
416 if (ioctl (fd, TIOCGSERIAL, &ser) < 0)
421 ser.custom_divisor = ser.baud_base / baud_rate;
422 ser.flags &= ~ASYNC_SPD_MASK;
423 ser.flags |= ASYNC_SPD_CUST;
425 if (ioctl (fd, TIOCSSERIAL, &ser) < 0)
429 #elif defined(HAVE_IOKIT)
430 speed_t speed = baud_rate;
431 if (ioctl(fd, IOSSIOSPEED, &speed) == -1)
434 /* Don't know how to set custom baud rate on this platform. */
444 get_signal_code (MonoSerialSignal signal)
465 static MonoSerialSignal
466 get_mono_signal_codes (int signals)
468 MonoSerialSignal retval = NoneSignal;
470 if ((signals & TIOCM_CAR) != 0)
472 if ((signals & TIOCM_CTS) != 0)
474 if ((signals & TIOCM_DSR) != 0)
476 if ((signals & TIOCM_DTR) != 0)
478 if ((signals & TIOCM_RTS) != 0)
485 get_signals (int fd, gint32 *error)
491 if (ioctl (fd, TIOCMGET, &signals) == -1) {
496 return get_mono_signal_codes (signals);
500 set_signal (int fd, MonoSerialSignal signal, gboolean value)
502 int signals, expected, activated;
504 expected = get_signal_code (signal);
505 if (ioctl (fd, TIOCMGET, &signals) == -1)
508 activated = (signals & expected) != 0;
509 if (activated == value) /* Already set */
515 signals &= ~expected;
517 if (ioctl (fd, TIOCMSET, &signals) == -1)
526 return tcsendbreak (fd, 0);
530 poll_serial (int fd, gint32 *error, int timeout)
537 pinfo.events = POLLIN;
540 while (poll (&pinfo, 1, timeout) == -1 && errno == EINTR) {
541 /* EINTR is an OK condition, we should not throw in the upper layer an IOException */
548 return (pinfo.revents & POLLIN) != 0 ? 1 : 0;
552 * mono internals should not be used here.
553 * this serial stuff needs to be implemented with icalls.
554 * make this at least compile until the code is moved elsewhere
555 * defined(linux) is wrong, too
558 list_serial_devices (void)
565 list_serial_devices (void)
569 /* Linux serial files are of the form ttyS[0-9]+ */
570 GSList *l, *list = NULL;
571 GDir* dir = g_dir_open ("/dev", 0, NULL);
572 const char *filename;
575 while ((filename = g_dir_read_name (dir))) {
577 if (!strncmp (filename, "ttyS", 4))
578 list = g_slist_append (list, g_strconcat ("/dev/", filename, NULL));
584 array = mono_array_new (mono_domain_get (), mono_get_string_class (), g_slist_length (list));
585 for (l = list; l; l = l->next) {
586 mono_array_set (array, gpointer, i++, mono_string_new (mono_domain_get (), (char*)l->data));
593 #warning "list_serial_devices isn't ported to this OS"