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) */
40 /* This is a copy of System.IO.Ports.Handshake */
45 RequestToSendXOnXOff = 3
48 /* This is a copy of System.IO.Ports.Parity */
57 /* This is a copy of System.IO.Ports.StopBits */
65 /* This is a copy of System.IO.Ports.SerialSignal */
68 Cd = 1, /* Carrier detect */
69 Cts = 2, /* Clear to send */
70 Dsr = 4, /* Data set ready */
71 Dtr = 8, /* Data terminal ready */
72 Rts = 16 /* Request to send */
76 * Silence the compiler, we do not need these prototypes to be public, since these are only
80 int open_serial (char *devfile);
81 int close_serial (int unix_fd);
82 guint32 read_serial (int fd, guchar *buffer, int offset, int count);
83 int write_serial (int fd, guchar *buffer, int offset, int count, int timeout);
84 int discard_buffer (int fd, gboolean input);
85 gint32 get_bytes_in_buffer (int fd, gboolean input);
86 gboolean is_baud_rate_legal (int baud_rate);
87 int setup_baud_rate (int baud_rate, gboolean *custom_baud_rate);
88 gboolean set_attributes (int fd, int baud_rate, MonoParity parity, int dataBits, MonoStopBits stopBits, MonoHandshake handshake);
89 MonoSerialSignal get_signals (int fd, gint32 *error);
90 gint32 set_signal (int fd, MonoSerialSignal signal, gboolean value);
91 int breakprop (int fd);
92 gboolean poll_serial (int fd, gint32 *error, int timeout);
93 void *list_serial_devices (void);
96 open_serial (char *devfile)
99 fd = open (devfile, O_RDWR | O_NOCTTY | O_NONBLOCK);
105 close_serial (int unix_fd)
107 // Linus writes: do not retry close after EINTR
108 return close (unix_fd);
112 read_serial (int fd, guchar *buffer, int offset, int count)
116 n = read (fd, buffer + offset, count);
122 write_serial (int fd, guchar *buffer, int offset, int count, int timeout)
128 pinfo.events = POLLOUT;
129 pinfo.revents = POLLOUT;
140 while ((c = poll (&pinfo, 1, timeout)) == -1 && errno == EINTR)
147 t = write (fd, buffer + offset, n);
148 } while (t == -1 && errno == EINTR);
161 discard_buffer (int fd, gboolean input)
163 return tcflush(fd, input ? TCIFLUSH : TCOFLUSH);
167 get_bytes_in_buffer (int fd, gboolean input)
171 if (ioctl (fd, input ? FIONREAD : TIOCOUTQ, &retval) == -1) {
179 is_baud_rate_legal (int baud_rate)
181 gboolean ignore = FALSE;
182 return setup_baud_rate (baud_rate, &ignore) != -1;
186 setup_baud_rate (int baud_rate, gboolean *custom_baud_rate)
190 /*Some values are not defined on OSX and *BSD */
267 *custom_baud_rate = TRUE;
274 set_attributes (int fd, int baud_rate, MonoParity parity, int dataBits, MonoStopBits stopBits, MonoHandshake handshake)
276 struct termios newtio;
277 gboolean custom_baud_rate = FALSE;
279 if (tcgetattr (fd, &newtio) == -1)
282 newtio.c_cflag |= (CLOCAL | CREAD);
283 newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ISIG | IEXTEN );
284 newtio.c_oflag &= ~(OPOST);
285 newtio.c_iflag = IGNBRK;
288 baud_rate = setup_baud_rate (baud_rate, &custom_baud_rate);
291 newtio.c_cflag &= ~CSIZE;
295 newtio.c_cflag |= CS5;
298 newtio.c_cflag |= CS6;
301 newtio.c_cflag |= CS7;
305 newtio.c_cflag |= CS8;
316 /* do nothing, the default is one stop bit */
317 newtio.c_cflag &= ~CSTOPB;
320 newtio.c_cflag |= CSTOPB;
322 case OnePointFive: /* OnePointFive */
328 newtio.c_iflag &= ~(INPCK | ISTRIP );
332 case NoneParity: /* None */
333 newtio.c_cflag &= ~(PARENB | PARODD);
337 newtio.c_cflag |= PARENB | PARODD;
340 case Even: /* Even */
341 newtio.c_cflag &= ~(PARODD);
342 newtio.c_cflag |= (PARENB);
345 case Mark: /* Mark */
348 case Space: /* Space */
353 newtio.c_iflag &= ~(IXOFF | IXON);
355 newtio.c_cflag &= ~CRTSCTS;
356 #endif /* def CRTSCTS */
360 case NoneHandshake: /* None */
363 case RequestToSend: /* RequestToSend (RTS) */
365 newtio.c_cflag |= CRTSCTS;
366 #endif /* def CRTSCTS */
368 case RequestToSendXOnXOff: /* RequestToSendXOnXOff (RTS + XON/XOFF) */
370 newtio.c_cflag |= CRTSCTS;
371 #endif /* def CRTSCTS */
373 case XOnXOff: /* XOnXOff */
374 newtio.c_iflag |= IXOFF | IXON;
378 if (custom_baud_rate == FALSE) {
379 if (cfsetospeed (&newtio, baud_rate) < 0 || cfsetispeed (&newtio, baud_rate) < 0)
382 /* On Linux, to set a custom baud rate, we must set the "standard" baud_rate
385 if (cfsetospeed (&newtio, B38400) < 0 || cfsetispeed (&newtio, B38400) < 0)
389 if (tcsetattr (fd, TCSANOW, &newtio) < 0)
392 if (custom_baud_rate == TRUE){
393 #if defined(__linux__)
394 struct serial_struct ser;
396 if (ioctl (fd, TIOCGSERIAL, &ser) < 0)
401 ser.custom_divisor = ser.baud_base / baud_rate;
402 ser.flags &= ~ASYNC_SPD_MASK;
403 ser.flags |= ASYNC_SPD_CUST;
405 if (ioctl (fd, TIOCSSERIAL, &ser) < 0)
410 /* Don't know how to set custom baud rate on this platform. */
420 get_signal_code (MonoSerialSignal signal)
441 static MonoSerialSignal
442 get_mono_signal_codes (int signals)
444 MonoSerialSignal retval = NoneSignal;
446 if ((signals & TIOCM_CAR) != 0)
448 if ((signals & TIOCM_CTS) != 0)
450 if ((signals & TIOCM_DSR) != 0)
452 if ((signals & TIOCM_DTR) != 0)
454 if ((signals & TIOCM_RTS) != 0)
461 get_signals (int fd, gint32 *error)
467 if (ioctl (fd, TIOCMGET, &signals) == -1) {
472 return get_mono_signal_codes (signals);
476 set_signal (int fd, MonoSerialSignal signal, gboolean value)
478 int signals, expected, activated;
480 expected = get_signal_code (signal);
481 if (ioctl (fd, TIOCMGET, &signals) == -1)
484 activated = (signals & expected) != 0;
485 if (activated == value) /* Already set */
491 signals &= ~expected;
493 if (ioctl (fd, TIOCMSET, &signals) == -1)
502 return tcsendbreak (fd, 0);
506 poll_serial (int fd, gint32 *error, int timeout)
513 pinfo.events = POLLIN;
516 while (poll (&pinfo, 1, timeout) == -1 && errno == EINTR) {
517 /* EINTR is an OK condition, we should not throw in the upper layer an IOException */
524 return (pinfo.revents & POLLIN) != 0 ? 1 : 0;
528 * mono internals should not be used here.
529 * this serial stuff needs to be implemented with icalls.
530 * make this at least compile until the code is moved elsewhere
531 * defined(linux) is wrong, too
534 list_serial_devices (void)
541 list_serial_devices (void)
545 /* Linux serial files are of the form ttyS[0-9]+ */
546 GSList *l, *list = NULL;
547 GDir* dir = g_dir_open ("/dev", 0, NULL);
548 const char *filename;
551 while ((filename = g_dir_read_name (dir))) {
553 if (!strncmp (filename, "ttyS", 4))
554 list = g_slist_append (list, g_strconcat ("/dev/", filename, NULL));
560 array = mono_array_new (mono_domain_get (), mono_get_string_class (), g_slist_length (list));
561 for (l = list; l; l = l->next) {
562 mono_array_set (array, gpointer, i++, mono_string_new (mono_domain_get (), (char*)l->data));
569 #warning "list_serial_devices isn't ported to this OS"