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)
180 if (ioctl (fd, input ? FIONREAD : TIOCOUTQ, &retval) == -1) {
188 is_baud_rate_legal (int baud_rate)
190 gboolean ignore = FALSE;
191 return setup_baud_rate (baud_rate, &ignore) != -1;
195 setup_baud_rate (int baud_rate, gboolean *custom_baud_rate)
199 /*Some values are not defined on OSX and *BSD */
276 *custom_baud_rate = TRUE;
283 set_attributes (int fd, int baud_rate, MonoParity parity, int dataBits, MonoStopBits stopBits, MonoHandshake handshake)
285 struct termios newtio;
286 gboolean custom_baud_rate = FALSE;
288 if (tcgetattr (fd, &newtio) == -1)
291 newtio.c_cflag |= (CLOCAL | CREAD);
292 newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ISIG | IEXTEN );
293 newtio.c_oflag &= ~(OPOST);
294 newtio.c_iflag = IGNBRK;
297 baud_rate = setup_baud_rate (baud_rate, &custom_baud_rate);
300 newtio.c_cflag &= ~CSIZE;
304 newtio.c_cflag |= CS5;
307 newtio.c_cflag |= CS6;
310 newtio.c_cflag |= CS7;
314 newtio.c_cflag |= CS8;
325 /* do nothing, the default is one stop bit */
326 newtio.c_cflag &= ~CSTOPB;
329 newtio.c_cflag |= CSTOPB;
331 case OnePointFive: /* OnePointFive */
337 newtio.c_iflag &= ~(INPCK | ISTRIP );
341 case NoneParity: /* None */
342 newtio.c_cflag &= ~(PARENB | PARODD);
346 newtio.c_cflag |= PARENB | PARODD;
349 case Even: /* Even */
350 newtio.c_cflag &= ~(PARODD);
351 newtio.c_cflag |= (PARENB);
354 case Mark: /* Mark */
357 case Space: /* Space */
362 newtio.c_iflag &= ~(IXOFF | IXON);
364 newtio.c_cflag &= ~CRTSCTS;
365 #endif /* def CRTSCTS */
369 case NoneHandshake: /* None */
372 case RequestToSend: /* RequestToSend (RTS) */
374 newtio.c_cflag |= CRTSCTS;
375 #endif /* def CRTSCTS */
377 case RequestToSendXOnXOff: /* RequestToSendXOnXOff (RTS + XON/XOFF) */
379 newtio.c_cflag |= CRTSCTS;
380 #endif /* def CRTSCTS */
382 case XOnXOff: /* XOnXOff */
383 newtio.c_iflag |= IXOFF | IXON;
387 if (custom_baud_rate == FALSE) {
388 if (cfsetospeed (&newtio, baud_rate) < 0 || cfsetispeed (&newtio, baud_rate) < 0)
391 #if __linux__ || defined(HAVE_IOKIT)
393 /* On Linux to set a custom baud rate, we must set the
394 * "standard" baud_rate to 38400. On Apple we set it purely
395 * so that tcsetattr has something to use (and report back later), but
396 * the Apple specific API is still opaque to these APIs, see:
397 * https://developer.apple.com/library/mac/samplecode/SerialPortSample/Listings/SerialPortSample_SerialPortSample_c.html#//apple_ref/doc/uid/DTS10000454-SerialPortSample_SerialPortSample_c-DontLinkElementID_4
399 if (cfsetospeed (&newtio, B38400) < 0 || cfsetispeed (&newtio, B38400) < 0)
404 if (tcsetattr (fd, TCSANOW, &newtio) < 0)
407 if (custom_baud_rate == TRUE){
408 #if defined(HAVE_LINUX_SERIAL_H)
409 struct serial_struct ser;
411 if (ioctl (fd, TIOCGSERIAL, &ser) < 0)
416 ser.custom_divisor = ser.baud_base / baud_rate;
417 ser.flags &= ~ASYNC_SPD_MASK;
418 ser.flags |= ASYNC_SPD_CUST;
420 if (ioctl (fd, TIOCSSERIAL, &ser) < 0)
424 #elif defined(HAVE_IOKIT)
425 speed_t speed = baud_rate;
426 if (ioctl(fd, IOSSIOSPEED, &speed) == -1)
429 /* Don't know how to set custom baud rate on this platform. */
439 get_signal_code (MonoSerialSignal signal)
460 static MonoSerialSignal
461 get_mono_signal_codes (int signals)
463 MonoSerialSignal retval = NoneSignal;
465 if ((signals & TIOCM_CAR) != 0)
467 if ((signals & TIOCM_CTS) != 0)
469 if ((signals & TIOCM_DSR) != 0)
471 if ((signals & TIOCM_DTR) != 0)
473 if ((signals & TIOCM_RTS) != 0)
480 get_signals (int fd, gint32 *error)
486 if (ioctl (fd, TIOCMGET, &signals) == -1) {
491 return get_mono_signal_codes (signals);
495 set_signal (int fd, MonoSerialSignal signal, gboolean value)
497 int signals, expected, activated;
499 expected = get_signal_code (signal);
500 if (ioctl (fd, TIOCMGET, &signals) == -1)
503 activated = (signals & expected) != 0;
504 if (activated == value) /* Already set */
510 signals &= ~expected;
512 if (ioctl (fd, TIOCMSET, &signals) == -1)
521 return tcsendbreak (fd, 0);
525 poll_serial (int fd, gint32 *error, int timeout)
532 pinfo.events = POLLIN;
535 while (poll (&pinfo, 1, timeout) == -1 && errno == EINTR) {
536 /* EINTR is an OK condition, we should not throw in the upper layer an IOException */
543 return (pinfo.revents & POLLIN) != 0 ? 1 : 0;
547 * mono internals should not be used here.
548 * this serial stuff needs to be implemented with icalls.
549 * make this at least compile until the code is moved elsewhere
550 * defined(linux) is wrong, too
553 list_serial_devices (void)
560 list_serial_devices (void)
564 /* Linux serial files are of the form ttyS[0-9]+ */
565 GSList *l, *list = NULL;
566 GDir* dir = g_dir_open ("/dev", 0, NULL);
567 const char *filename;
570 while ((filename = g_dir_read_name (dir))) {
572 if (!strncmp (filename, "ttyS", 4))
573 list = g_slist_append (list, g_strconcat ("/dev/", filename, NULL));
579 array = mono_array_new (mono_domain_get (), mono_get_string_class (), g_slist_length (list));
580 for (l = list; l; l = l->next) {
581 mono_array_set (array, gpointer, i++, mono_string_new (mono_domain_get (), (char*)l->data));
588 #warning "list_serial_devices isn't ported to this OS"