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(__linux__)
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) */
38 #include <IOKit/IOKitLib.h>
39 #include <IOKit/serial/IOSerialKeys.h>
40 #include <IOKit/serial/ioss.h>
41 #include <IOKit/IOBSD.h>
44 /* This is a copy of System.IO.Ports.Handshake */
49 RequestToSendXOnXOff = 3
52 /* This is a copy of System.IO.Ports.Parity */
61 /* This is a copy of System.IO.Ports.StopBits */
69 /* This is a copy of System.IO.Ports.SerialSignal */
72 Cd = 1, /* Carrier detect */
73 Cts = 2, /* Clear to send */
74 Dsr = 4, /* Data set ready */
75 Dtr = 8, /* Data terminal ready */
76 Rts = 16 /* Request to send */
80 * Silence the compiler, we do not need these prototypes to be public, since these are only
84 int open_serial (char *devfile);
85 int close_serial (int unix_fd);
86 guint32 read_serial (int fd, guchar *buffer, int offset, int count);
87 int write_serial (int fd, guchar *buffer, int offset, int count, int timeout);
88 int discard_buffer (int fd, gboolean input);
89 gint32 get_bytes_in_buffer (int fd, gboolean input);
90 gboolean is_baud_rate_legal (int baud_rate);
91 int setup_baud_rate (int baud_rate, gboolean *custom_baud_rate);
92 gboolean set_attributes (int fd, int baud_rate, MonoParity parity, int dataBits, MonoStopBits stopBits, MonoHandshake handshake);
93 MonoSerialSignal get_signals (int fd, gint32 *error);
94 gint32 set_signal (int fd, MonoSerialSignal signal, gboolean value);
95 int breakprop (int fd);
96 gboolean poll_serial (int fd, gint32 *error, int timeout);
97 void *list_serial_devices (void);
100 open_serial (char *devfile)
103 fd = open (devfile, O_RDWR | O_NOCTTY | O_NONBLOCK);
109 close_serial (int unix_fd)
111 // Linus writes: do not retry close after EINTR
112 return close (unix_fd);
116 read_serial (int fd, guchar *buffer, int offset, int count)
120 n = read (fd, buffer + offset, count);
126 write_serial (int fd, guchar *buffer, int offset, int count, int timeout)
132 pinfo.events = POLLOUT;
133 pinfo.revents = POLLOUT;
144 while ((c = poll (&pinfo, 1, timeout)) == -1 && errno == EINTR)
151 t = write (fd, buffer + offset, n);
152 } while (t == -1 && errno == EINTR);
165 discard_buffer (int fd, gboolean input)
167 return tcflush(fd, input ? TCIFLUSH : TCOFLUSH);
171 get_bytes_in_buffer (int fd, gboolean input)
175 if (ioctl (fd, input ? FIONREAD : TIOCOUTQ, &retval) == -1) {
183 is_baud_rate_legal (int baud_rate)
185 gboolean ignore = FALSE;
186 return setup_baud_rate (baud_rate, &ignore) != -1;
190 setup_baud_rate (int baud_rate, gboolean *custom_baud_rate)
194 /*Some values are not defined on OSX and *BSD */
271 *custom_baud_rate = TRUE;
278 set_attributes (int fd, int baud_rate, MonoParity parity, int dataBits, MonoStopBits stopBits, MonoHandshake handshake)
280 struct termios newtio;
281 gboolean custom_baud_rate = FALSE;
283 if (tcgetattr (fd, &newtio) == -1)
286 newtio.c_cflag |= (CLOCAL | CREAD);
287 newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ISIG | IEXTEN );
288 newtio.c_oflag &= ~(OPOST);
289 newtio.c_iflag = IGNBRK;
292 baud_rate = setup_baud_rate (baud_rate, &custom_baud_rate);
295 newtio.c_cflag &= ~CSIZE;
299 newtio.c_cflag |= CS5;
302 newtio.c_cflag |= CS6;
305 newtio.c_cflag |= CS7;
309 newtio.c_cflag |= CS8;
320 /* do nothing, the default is one stop bit */
321 newtio.c_cflag &= ~CSTOPB;
324 newtio.c_cflag |= CSTOPB;
326 case OnePointFive: /* OnePointFive */
332 newtio.c_iflag &= ~(INPCK | ISTRIP );
336 case NoneParity: /* None */
337 newtio.c_cflag &= ~(PARENB | PARODD);
341 newtio.c_cflag |= PARENB | PARODD;
344 case Even: /* Even */
345 newtio.c_cflag &= ~(PARODD);
346 newtio.c_cflag |= (PARENB);
349 case Mark: /* Mark */
352 case Space: /* Space */
357 newtio.c_iflag &= ~(IXOFF | IXON);
359 newtio.c_cflag &= ~CRTSCTS;
360 #endif /* def CRTSCTS */
364 case NoneHandshake: /* None */
367 case RequestToSend: /* RequestToSend (RTS) */
369 newtio.c_cflag |= CRTSCTS;
370 #endif /* def CRTSCTS */
372 case RequestToSendXOnXOff: /* RequestToSendXOnXOff (RTS + XON/XOFF) */
374 newtio.c_cflag |= CRTSCTS;
375 #endif /* def CRTSCTS */
377 case XOnXOff: /* XOnXOff */
378 newtio.c_iflag |= IXOFF | IXON;
382 if (custom_baud_rate == FALSE) {
383 if (cfsetospeed (&newtio, baud_rate) < 0 || cfsetispeed (&newtio, baud_rate) < 0)
386 #if __linux__ || __APPLE__
388 /* On Linux to set a custom baud rate, we must set the
389 * "standard" baud_rate to 38400. On Apple we set it purely
390 * so that tcsetattr has something to use (and report back later), but
391 * the Apple specific API is still opaque to these APIs, see:
392 * https://developer.apple.com/library/mac/samplecode/SerialPortSample/Listings/SerialPortSample_SerialPortSample_c.html#//apple_ref/doc/uid/DTS10000454-SerialPortSample_SerialPortSample_c-DontLinkElementID_4
394 if (cfsetospeed (&newtio, B38400) < 0 || cfsetispeed (&newtio, B38400) < 0)
399 if (tcsetattr (fd, TCSANOW, &newtio) < 0)
402 if (custom_baud_rate == TRUE){
403 #if defined(__linux__)
404 struct serial_struct ser;
406 if (ioctl (fd, TIOCGSERIAL, &ser) < 0)
411 ser.custom_divisor = ser.baud_base / baud_rate;
412 ser.flags &= ~ASYNC_SPD_MASK;
413 ser.flags |= ASYNC_SPD_CUST;
415 if (ioctl (fd, TIOCSSERIAL, &ser) < 0)
420 speed_t speed = baud_rate;
421 if (ioctl(fd, IOSSIOSPEED, &speed) == -1)
424 /* Don't know how to set custom baud rate on this platform. */
434 get_signal_code (MonoSerialSignal signal)
455 static MonoSerialSignal
456 get_mono_signal_codes (int signals)
458 MonoSerialSignal retval = NoneSignal;
460 if ((signals & TIOCM_CAR) != 0)
462 if ((signals & TIOCM_CTS) != 0)
464 if ((signals & TIOCM_DSR) != 0)
466 if ((signals & TIOCM_DTR) != 0)
468 if ((signals & TIOCM_RTS) != 0)
475 get_signals (int fd, gint32 *error)
481 if (ioctl (fd, TIOCMGET, &signals) == -1) {
486 return get_mono_signal_codes (signals);
490 set_signal (int fd, MonoSerialSignal signal, gboolean value)
492 int signals, expected, activated;
494 expected = get_signal_code (signal);
495 if (ioctl (fd, TIOCMGET, &signals) == -1)
498 activated = (signals & expected) != 0;
499 if (activated == value) /* Already set */
505 signals &= ~expected;
507 if (ioctl (fd, TIOCMSET, &signals) == -1)
516 return tcsendbreak (fd, 0);
520 poll_serial (int fd, gint32 *error, int timeout)
527 pinfo.events = POLLIN;
530 while (poll (&pinfo, 1, timeout) == -1 && errno == EINTR) {
531 /* EINTR is an OK condition, we should not throw in the upper layer an IOException */
538 return (pinfo.revents & POLLIN) != 0 ? 1 : 0;
542 * mono internals should not be used here.
543 * this serial stuff needs to be implemented with icalls.
544 * make this at least compile until the code is moved elsewhere
545 * defined(linux) is wrong, too
548 list_serial_devices (void)
555 list_serial_devices (void)
559 /* Linux serial files are of the form ttyS[0-9]+ */
560 GSList *l, *list = NULL;
561 GDir* dir = g_dir_open ("/dev", 0, NULL);
562 const char *filename;
565 while ((filename = g_dir_read_name (dir))) {
567 if (!strncmp (filename, "ttyS", 4))
568 list = g_slist_append (list, g_strconcat ("/dev/", filename, NULL));
574 array = mono_array_new (mono_domain_get (), mono_get_string_class (), g_slist_length (list));
575 for (l = list; l; l = l->next) {
576 mono_array_set (array, gpointer, i++, mono_string_new (mono_domain_get (), (char*)l->data));
583 #warning "list_serial_devices isn't ported to this OS"