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(__APPLE__)
18 #elif defined(HAVE_POLL_H)
20 #elif defined(HAVE_SYS_POLL_H)
23 #include <sys/ioctl.h>
27 /* This is for FIONREAD on solaris */
29 #include <sys/filio.h>
32 /* sys/time.h (for timeval) is required when using osx 10.3 (but not 10.4) */
37 /* This is a copy of System.IO.Ports.Handshake */
42 RequestToSendXOnXOff = 3
45 /* This is a copy of System.IO.Ports.Parity */
54 /* This is a copy of System.IO.Ports.StopBits */
62 /* This is a copy of System.IO.Ports.SerialSignal */
65 Cd = 1, /* Carrier detect */
66 Cts = 2, /* Clear to send */
67 Dsr = 4, /* Data set ready */
68 Dtr = 8, /* Data terminal ready */
69 Rts = 16 /* Request to send */
73 * Silence the compiler, we do not need these prototypes to be public, since these are only
77 int open_serial (char *devfile);
78 int close_serial (int unix_fd);
79 guint32 read_serial (int fd, guchar *buffer, int offset, int count);
80 int write_serial (int fd, guchar *buffer, int offset, int count, int timeout);
81 int discard_buffer (int fd, gboolean input);
82 gint32 get_bytes_in_buffer (int fd, gboolean input);
83 gboolean is_baud_rate_legal (int baud_rate);
84 int setup_baud_rate (int baud_rate);
85 gboolean set_attributes (int fd, int baud_rate, MonoParity parity, int dataBits, MonoStopBits stopBits, MonoHandshake handshake);
86 MonoSerialSignal get_signals (int fd, gint32 *error);
87 gint32 set_signal (int fd, MonoSerialSignal signal, gboolean value);
88 int breakprop (int fd);
89 gboolean poll_serial (int fd, gint32 *error, int timeout);
90 void *list_serial_devices (void);
93 open_serial (char *devfile)
96 fd = open (devfile, O_RDWR | O_NOCTTY | O_NONBLOCK);
102 close_serial (int unix_fd)
104 // Linus writes: do not retry close after EINTR
105 return close (unix_fd);
109 read_serial (int fd, guchar *buffer, int offset, int count)
113 n = read (fd, buffer + offset, count);
119 write_serial (int fd, guchar *buffer, int offset, int count, int timeout)
125 pinfo.events = POLLOUT;
126 pinfo.revents = POLLOUT;
137 while ((c = poll (&pinfo, 1, timeout)) == -1 && errno == EINTR)
144 t = write (fd, buffer + offset, n);
145 } while (t == -1 && errno == EINTR);
158 discard_buffer (int fd, gboolean input)
160 return tcflush(fd, input ? TCIFLUSH : TCOFLUSH);
164 get_bytes_in_buffer (int fd, gboolean input)
168 if (ioctl (fd, input ? FIONREAD : TIOCOUTQ, &retval) == -1) {
176 is_baud_rate_legal (int baud_rate)
178 return setup_baud_rate (baud_rate) != -1;
182 setup_baud_rate (int baud_rate)
186 /*Some values are not defined on OSX and *BSD */
258 set_attributes (int fd, int baud_rate, MonoParity parity, int dataBits, MonoStopBits stopBits, MonoHandshake handshake)
260 struct termios newtio;
262 if (tcgetattr (fd, &newtio) == -1)
265 newtio.c_cflag |= (CLOCAL | CREAD);
266 newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ISIG | IEXTEN );
267 newtio.c_oflag &= ~(OPOST);
268 newtio.c_iflag = IGNBRK;
271 baud_rate = setup_baud_rate (baud_rate);
274 newtio.c_cflag &= ~CSIZE;
278 newtio.c_cflag |= CS5;
281 newtio.c_cflag |= CS6;
284 newtio.c_cflag |= CS7;
288 newtio.c_cflag |= CS8;
299 /* do nothing, the default is one stop bit */
300 newtio.c_cflag &= ~CSTOPB;
303 newtio.c_cflag |= CSTOPB;
305 case OnePointFive: /* OnePointFive */
311 newtio.c_iflag &= ~(INPCK | ISTRIP );
315 case NoneParity: /* None */
316 newtio.c_cflag &= ~(PARENB | PARODD);
320 newtio.c_cflag |= PARENB | PARODD;
323 case Even: /* Even */
324 newtio.c_cflag &= ~(PARODD);
325 newtio.c_cflag |= (PARENB);
328 case Mark: /* Mark */
331 case Space: /* Space */
336 newtio.c_iflag &= ~(IXOFF | IXON);
338 newtio.c_cflag &= ~CRTSCTS;
339 #endif /* def CRTSCTS */
343 case NoneHandshake: /* None */
346 case RequestToSend: /* RequestToSend (RTS) */
348 newtio.c_cflag |= CRTSCTS;
349 #endif /* def CRTSCTS */
351 case RequestToSendXOnXOff: /* RequestToSendXOnXOff (RTS + XON/XOFF) */
353 newtio.c_cflag |= CRTSCTS;
354 #endif /* def CRTSCTS */
356 case XOnXOff: /* XOnXOff */
357 newtio.c_iflag |= IXOFF | IXON;
361 if (cfsetospeed (&newtio, baud_rate) < 0 || cfsetispeed (&newtio, baud_rate) < 0 ||
362 tcsetattr (fd, TCSANOW, &newtio) < 0)
374 get_signal_code (MonoSerialSignal signal)
395 static MonoSerialSignal
396 get_mono_signal_codes (int signals)
398 MonoSerialSignal retval = NoneSignal;
400 if ((signals & TIOCM_CAR) != 0)
402 if ((signals & TIOCM_CTS) != 0)
404 if ((signals & TIOCM_DSR) != 0)
406 if ((signals & TIOCM_DTR) != 0)
408 if ((signals & TIOCM_RTS) != 0)
415 get_signals (int fd, gint32 *error)
421 if (ioctl (fd, TIOCMGET, &signals) == -1) {
426 return get_mono_signal_codes (signals);
430 set_signal (int fd, MonoSerialSignal signal, gboolean value)
432 int signals, expected, activated;
434 expected = get_signal_code (signal);
435 if (ioctl (fd, TIOCMGET, &signals) == -1)
438 activated = (signals & expected) != 0;
439 if (activated == value) /* Already set */
445 signals &= ~expected;
447 if (ioctl (fd, TIOCMSET, &signals) == -1)
456 return tcsendbreak (fd, 0);
460 poll_serial (int fd, gint32 *error, int timeout)
467 pinfo.events = POLLIN;
470 while (poll (&pinfo, 1, timeout) == -1 && errno == EINTR) {
471 /* EINTR is an OK condition, we should not throw in the upper layer an IOException */
478 return (pinfo.revents & POLLIN) != 0 ? 1 : 0;
482 * mono internals should not be used here.
483 * this serial stuff needs to be implemented with icalls.
484 * make this at least compile until the code is moved elsewhere
485 * defined(linux) is wrong, too
488 list_serial_devices (void)
495 list_serial_devices (void)
499 /* Linux serial files are of the form ttyS[0-9]+ */
500 GSList *l, *list = NULL;
501 GDir* dir = g_dir_open ("/dev", 0, NULL);
502 const char *filename;
505 while ((filename = g_dir_read_name (dir))) {
507 if (!strncmp (filename, "ttyS", 4))
508 list = g_slist_append (list, g_strconcat ("/dev/", filename, NULL));
514 array = mono_array_new (mono_domain_get (), mono_get_string_class (), g_slist_length (list));
515 for (l = list; l; l = l->next) {
516 mono_array_set (array, gpointer, i++, mono_string_new (mono_domain_get (), (char*)l->data));
523 #warning "list_serial_devices isn't ported to this OS"