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>
13 #if defined(__APPLE__)
18 #include <sys/ioctl.h>
22 /* This is for FIONREAD on solaris */
24 #include <sys/filio.h>
27 /* sys/time.h (for timeval) is required when using osx 10.3 (but not 10.4) */
32 /* This is a copy of System.IO.Ports.Handshake */
37 RequestToSendXOnXOff = 3
40 /* This is a copy of System.IO.Ports.Parity */
49 /* This is a copy of System.IO.Ports.StopBits */
57 /* This is a copy of System.IO.Ports.SerialSignal */
60 Cd = 1, /* Carrier detect */
61 Cts = 2, /* Clear to send */
62 Dsr = 4, /* Data set ready */
63 Dtr = 8, /* Data terminal ready */
64 Rts = 16 /* Request to send */
68 * Silence the compiler, we do not need these prototypes to be public, since these are only
72 int open_serial (char *devfile);
73 int close_serial (int unix_fd);
74 guint32 read_serial (int fd, guchar *buffer, int offset, int count);
75 int write_serial (int fd, guchar *buffer, int offset, int count, int timeout);
76 int discard_buffer (int fd, gboolean input);
77 gint32 get_bytes_in_buffer (int fd, gboolean input);
78 gboolean is_baud_rate_legal (int baud_rate);
79 int setup_baud_rate (int baud_rate);
80 gboolean set_attributes (int fd, int baud_rate, MonoParity parity, int dataBits, MonoStopBits stopBits, MonoHandshake handshake);
81 MonoSerialSignal get_signals (int fd, gint32 *error);
82 gint32 set_signal (int fd, MonoSerialSignal signal, gboolean value);
83 int breakprop (int fd);
84 gboolean poll_serial (int fd, gint32 *error, int timeout);
85 void *list_serial_devices (void);
88 open_serial (char *devfile)
91 fd = open (devfile, O_RDWR | O_NOCTTY | O_NONBLOCK);
97 close_serial (int unix_fd)
99 // Linus writes: do not retry close after EINTR
100 return close (unix_fd);
104 read_serial (int fd, guchar *buffer, int offset, int count)
108 n = read (fd, buffer + offset, count);
114 write_serial (int fd, guchar *buffer, int offset, int count, int timeout)
120 pinfo.events = POLLOUT;
121 pinfo.revents = POLLOUT;
132 while ((c = poll (&pinfo, 1, timeout)) == -1 && errno == EINTR)
139 t = write (fd, buffer + offset, n);
140 } while (t == -1 && errno == EINTR);
153 discard_buffer (int fd, gboolean input)
155 return tcflush(fd, input ? TCIFLUSH : TCOFLUSH);
159 get_bytes_in_buffer (int fd, gboolean input)
163 if (ioctl (fd, input ? FIONREAD : TIOCOUTQ, &retval) == -1) {
171 is_baud_rate_legal (int baud_rate)
173 return setup_baud_rate (baud_rate) != -1;
177 setup_baud_rate (int baud_rate)
181 /*Some values are not defined on OSX and *BSD */
253 set_attributes (int fd, int baud_rate, MonoParity parity, int dataBits, MonoStopBits stopBits, MonoHandshake handshake)
255 struct termios newtio;
257 if (tcgetattr (fd, &newtio) == -1)
260 newtio.c_cflag |= (CLOCAL | CREAD);
261 newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ISIG | IEXTEN );
262 newtio.c_oflag &= ~(OPOST);
263 newtio.c_iflag = IGNBRK;
266 baud_rate = setup_baud_rate (baud_rate);
269 newtio.c_cflag &= ~CSIZE;
273 newtio.c_cflag |= CS5;
276 newtio.c_cflag |= CS6;
279 newtio.c_cflag |= CS7;
283 newtio.c_cflag |= CS8;
294 /* do nothing, the default is one stop bit */
295 newtio.c_cflag &= ~CSTOPB;
298 newtio.c_cflag |= CSTOPB;
300 case OnePointFive: /* OnePointFive */
306 newtio.c_iflag &= ~(INPCK | ISTRIP );
310 case NoneParity: /* None */
311 newtio.c_cflag &= ~(PARENB | PARODD);
315 newtio.c_cflag |= PARENB | PARODD;
318 case Even: /* Even */
319 newtio.c_cflag &= ~(PARODD);
320 newtio.c_cflag |= (PARENB);
323 case Mark: /* Mark */
326 case Space: /* Space */
331 newtio.c_iflag &= ~(IXOFF | IXON);
333 newtio.c_cflag &= ~CRTSCTS;
334 #endif /* def CRTSCTS */
338 case NoneHandshake: /* None */
341 case RequestToSend: /* RequestToSend (RTS) */
343 newtio.c_cflag |= CRTSCTS;
344 #endif /* def CRTSCTS */
346 case RequestToSendXOnXOff: /* RequestToSendXOnXOff (RTS + XON/XOFF) */
348 newtio.c_cflag |= CRTSCTS;
349 #endif /* def CRTSCTS */
351 case XOnXOff: /* XOnXOff */
352 newtio.c_iflag |= IXOFF | IXON;
356 if (cfsetospeed (&newtio, baud_rate) < 0 || cfsetispeed (&newtio, baud_rate) < 0 ||
357 tcsetattr (fd, TCSANOW, &newtio) < 0)
369 get_signal_code (MonoSerialSignal signal)
390 static MonoSerialSignal
391 get_mono_signal_codes (int signals)
393 MonoSerialSignal retval = NoneSignal;
395 if ((signals & TIOCM_CAR) != 0)
397 if ((signals & TIOCM_CTS) != 0)
399 if ((signals & TIOCM_DSR) != 0)
401 if ((signals & TIOCM_DTR) != 0)
403 if ((signals & TIOCM_RTS) != 0)
410 get_signals (int fd, gint32 *error)
416 if (ioctl (fd, TIOCMGET, &signals) == -1) {
421 return get_mono_signal_codes (signals);
425 set_signal (int fd, MonoSerialSignal signal, gboolean value)
427 int signals, expected, activated;
429 expected = get_signal_code (signal);
430 if (ioctl (fd, TIOCMGET, &signals) == -1)
433 activated = (signals & expected) != 0;
434 if (activated == value) /* Already set */
440 signals &= ~expected;
442 if (ioctl (fd, TIOCMSET, &signals) == -1)
451 return tcsendbreak (fd, 0);
455 poll_serial (int fd, gint32 *error, int timeout)
462 pinfo.events = POLLIN;
465 while (poll (&pinfo, 1, timeout) == -1 && errno == EINTR) {
466 /* EINTR is an OK condition, we should not throw in the upper layer an IOException */
473 return (pinfo.revents & POLLIN) != 0 ? 1 : 0;
477 * mono internals should not be used here.
478 * this serial stuff needs to be implemented with icalls.
479 * make this at least compile until the code is moved elsewhere
480 * defined(linux) is wrong, too
483 list_serial_devices (void)
490 list_serial_devices (void)
494 /* Linux serial files are of the form ttyS[0-9]+ */
495 GSList *l, *list = NULL;
496 GDir* dir = g_dir_open ("/dev", 0, NULL);
497 const char *filename;
500 while ((filename = g_dir_read_name (dir))) {
502 if (!strncmp (filename, "ttyS", 4))
503 list = g_slist_append (list, g_strconcat ("/dev/", filename, NULL));
509 array = mono_array_new (mono_domain_get (), mono_get_string_class (), g_slist_length (list));
510 for (l = list; l; l = l->next) {
511 mono_array_set (array, gpointer, i++, mono_string_new (mono_domain_get (), (char*)l->data));
518 #warning "list_serial_devices isn't ported to this OS"