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 #include <sys/ioctl.h>
18 /* This is for FIONREAD on solaris */
20 #include <sys/filio.h>
23 /* sys/time.h (for timeval) is required when using osx 10.3 (but not 10.4) */
28 /* This is a copy of System.IO.Ports.Handshake */
33 RequestToSendXOnXOff = 3
36 /* This is a copy of System.IO.Ports.Parity */
45 /* This is a copy of System.IO.Ports.StopBits */
53 /* This is a copy of System.IO.Ports.SerialSignal */
56 Cd = 1, /* Carrier detect */
57 Cts = 2, /* Clear to send */
58 Dsr = 4, /* Data set ready */
59 Dtr = 8, /* Data terminal ready */
60 Rts = 16 /* Request to send */
64 open_serial (char* devfile)
67 fd = open (devfile, O_RDWR | O_NOCTTY | O_NONBLOCK);
76 close_serial (int unix_fd)
78 // Linus writes: do not retry close after EINTR
79 return close (unix_fd);
83 read_serial (int fd, guchar *buffer, int offset, int count)
87 n = read (fd, buffer + offset, count);
93 write_serial (int fd, guchar *buffer, int offset, int count, int timeout)
99 pinfo.events = POLLOUT;
100 pinfo.revents = POLLOUT;
111 while ((c = poll (&pinfo, 1, timeout)) == -1 && errno == EINTR)
118 t = write (fd, buffer + offset, n);
119 } while (t == -1 && errno == EINTR);
132 discard_buffer (int fd, gboolean input)
134 tcflush(fd, input ? TCIFLUSH : TCOFLUSH);
138 get_bytes_in_buffer (int fd, gboolean input)
142 if (ioctl (fd, input ? FIONREAD : TIOCOUTQ, &retval) == -1) {
150 set_attributes (int fd, int baud_rate, MonoParity parity, int dataBits, MonoStopBits stopBits, MonoHandshake handshake)
152 struct termios newtio;
154 tcgetattr (fd, &newtio);
155 newtio.c_cflag |= (CLOCAL | CREAD);
156 newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ISIG | IEXTEN );
157 newtio.c_oflag &= ~(OPOST);
158 newtio.c_iflag = IGNBRK;
222 newtio.c_cflag &= ~CSIZE;
226 newtio.c_cflag |= CS5;
229 newtio.c_cflag |= CS6;
232 newtio.c_cflag |= CS7;
236 newtio.c_cflag |= CS8;
247 /* do nothing, the default is one stop bit */
248 newtio.c_cflag &= ~CSTOPB;
251 newtio.c_cflag |= CSTOPB;
253 case OnePointFive: /* OnePointFive */
259 newtio.c_iflag &= ~(INPCK | ISTRIP );
263 case NoneParity: /* None */
264 newtio.c_cflag &= ~(PARENB | PARODD);
268 newtio.c_cflag |= PARENB | PARODD;
271 case Even: /* Even */
272 newtio.c_cflag &= ~(PARODD);
273 newtio.c_cflag |= (PARENB);
276 case Mark: /* Mark */
279 case Space: /* Space */
284 newtio.c_iflag &= ~(IXOFF | IXON);
286 newtio.c_cflag &= ~CRTSCTS;
287 #endif /* def CRTSCTS */
291 case NoneHandshake: /* None */
294 case RequestToSend: /* RequestToSend (RTS) */
296 newtio.c_cflag |= CRTSCTS;
297 #endif /* def CRTSCTS */
299 case RequestToSendXOnXOff: /* RequestToSendXOnXOff (RTS + XON/XOFF) */
301 newtio.c_cflag |= CRTSCTS;
302 #endif /* def CRTSCTS */
304 case XOnXOff: /* XOnXOff */
305 newtio.c_iflag |= IXOFF | IXON;
309 if (cfsetospeed (&newtio, baud_rate) < 0 || cfsetispeed (&newtio, baud_rate) < 0 ||
310 tcsetattr (fd, TCSANOW, &newtio) < 0)
322 get_signal_code (MonoSerialSignal signal)
343 static MonoSerialSignal
344 get_mono_signal_codes (int signals)
346 MonoSerialSignal retval = NoneSignal;
348 if ((signals & TIOCM_CAR) != 0)
350 if ((signals & TIOCM_CTS) != 0)
352 if ((signals & TIOCM_DSR) != 0)
354 if ((signals & TIOCM_DTR) != 0)
356 if ((signals & TIOCM_RTS) != 0)
363 get_signals (int fd, gint32 *error)
369 if (ioctl (fd, TIOCMGET, &signals) == -1) {
374 return get_mono_signal_codes (signals);
378 set_signal (int fd, MonoSerialSignal signal, gboolean value)
380 int signals, expected, activated;
382 expected = get_signal_code (signal);
383 if (ioctl (fd, TIOCMGET, &signals) == -1)
386 activated = (signals & expected) != 0;
387 if (activated == value) /* Already set */
393 signals &= ~expected;
395 if (ioctl (fd, TIOCMSET, &signals) == -1)
408 poll_serial (int fd, gint32 *error, int timeout)
415 pinfo.events = POLLIN;
418 while (poll (&pinfo, 1, timeout) == -1 && errno == EINTR) {
419 /* EINTR is an OK condition, we should not throw in the upper layer an IOException */
426 return (pinfo.revents & POLLIN) != 0 ? 1 : 0;
430 * mono internals should not be used here.
431 * this serial stuff needs to be implemented with icalls.
432 * make this at least compile until the code is moved elsewhere
433 * defined(linux) is wrong, too
436 list_serial_devices (void)
443 list_serial_devices (void)
447 /* Linux serial files are of the form ttyS[0-9]+ */
448 GSList *l, *list = NULL;
449 GDir* dir = g_dir_open ("/dev", 0, NULL);
450 const char *filename;
453 while ((filename = g_dir_read_name (dir))) {
455 if (!strncmp (filename, "ttyS", 4))
456 list = g_slist_append (list, g_strconcat ("/dev/", filename, NULL));
462 array = mono_array_new (mono_domain_get (), mono_get_string_class (), g_slist_length (list));
463 for (l = list; l; l = l->next) {
464 mono_array_set (array, gpointer, i++, mono_string_new (mono_domain_get (), (char*)l->data));
471 #warning "list_serial_devices isn't ported to this OS"