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 open_serial (char* devfile)
71 fd = open (devfile, O_RDWR | O_NOCTTY | O_NONBLOCK);
77 close_serial (int unix_fd)
79 // Linus writes: do not retry close after EINTR
80 return close (unix_fd);
84 read_serial (int fd, guchar *buffer, int offset, int count)
88 n = read (fd, buffer + offset, count);
94 write_serial (int fd, guchar *buffer, int offset, int count, int timeout)
100 pinfo.events = POLLOUT;
101 pinfo.revents = POLLOUT;
112 while ((c = poll (&pinfo, 1, timeout)) == -1 && errno == EINTR)
119 t = write (fd, buffer + offset, n);
120 } while (t == -1 && errno == EINTR);
133 discard_buffer (int fd, gboolean input)
135 return tcflush(fd, input ? TCIFLUSH : TCOFLUSH);
139 get_bytes_in_buffer (int fd, gboolean input)
143 if (ioctl (fd, input ? FIONREAD : TIOCOUTQ, &retval) == -1) {
151 set_attributes (int fd, int baud_rate, MonoParity parity, int dataBits, MonoStopBits stopBits, MonoHandshake handshake)
153 struct termios newtio;
155 if (tcgetattr (fd, &newtio) == -1)
158 newtio.c_cflag |= (CLOCAL | CREAD);
159 newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ISIG | IEXTEN );
160 newtio.c_oflag &= ~(OPOST);
161 newtio.c_iflag = IGNBRK;
166 /*Some values are not defined on OSX and *BSD */
236 newtio.c_cflag &= ~CSIZE;
240 newtio.c_cflag |= CS5;
243 newtio.c_cflag |= CS6;
246 newtio.c_cflag |= CS7;
250 newtio.c_cflag |= CS8;
261 /* do nothing, the default is one stop bit */
262 newtio.c_cflag &= ~CSTOPB;
265 newtio.c_cflag |= CSTOPB;
267 case OnePointFive: /* OnePointFive */
273 newtio.c_iflag &= ~(INPCK | ISTRIP );
277 case NoneParity: /* None */
278 newtio.c_cflag &= ~(PARENB | PARODD);
282 newtio.c_cflag |= PARENB | PARODD;
285 case Even: /* Even */
286 newtio.c_cflag &= ~(PARODD);
287 newtio.c_cflag |= (PARENB);
290 case Mark: /* Mark */
293 case Space: /* Space */
298 newtio.c_iflag &= ~(IXOFF | IXON);
300 newtio.c_cflag &= ~CRTSCTS;
301 #endif /* def CRTSCTS */
305 case NoneHandshake: /* None */
308 case RequestToSend: /* RequestToSend (RTS) */
310 newtio.c_cflag |= CRTSCTS;
311 #endif /* def CRTSCTS */
313 case RequestToSendXOnXOff: /* RequestToSendXOnXOff (RTS + XON/XOFF) */
315 newtio.c_cflag |= CRTSCTS;
316 #endif /* def CRTSCTS */
318 case XOnXOff: /* XOnXOff */
319 newtio.c_iflag |= IXOFF | IXON;
323 if (cfsetospeed (&newtio, baud_rate) < 0 || cfsetispeed (&newtio, baud_rate) < 0 ||
324 tcsetattr (fd, TCSANOW, &newtio) < 0)
336 get_signal_code (MonoSerialSignal signal)
357 static MonoSerialSignal
358 get_mono_signal_codes (int signals)
360 MonoSerialSignal retval = NoneSignal;
362 if ((signals & TIOCM_CAR) != 0)
364 if ((signals & TIOCM_CTS) != 0)
366 if ((signals & TIOCM_DSR) != 0)
368 if ((signals & TIOCM_DTR) != 0)
370 if ((signals & TIOCM_RTS) != 0)
377 get_signals (int fd, gint32 *error)
383 if (ioctl (fd, TIOCMGET, &signals) == -1) {
388 return get_mono_signal_codes (signals);
392 set_signal (int fd, MonoSerialSignal signal, gboolean value)
394 int signals, expected, activated;
396 expected = get_signal_code (signal);
397 if (ioctl (fd, TIOCMGET, &signals) == -1)
400 activated = (signals & expected) != 0;
401 if (activated == value) /* Already set */
407 signals &= ~expected;
409 if (ioctl (fd, TIOCMSET, &signals) == -1)
418 return tcsendbreak (fd, 0);
422 poll_serial (int fd, gint32 *error, int timeout)
429 pinfo.events = POLLIN;
432 while (poll (&pinfo, 1, timeout) == -1 && errno == EINTR) {
433 /* EINTR is an OK condition, we should not throw in the upper layer an IOException */
440 return (pinfo.revents & POLLIN) != 0 ? 1 : 0;
444 * mono internals should not be used here.
445 * this serial stuff needs to be implemented with icalls.
446 * make this at least compile until the code is moved elsewhere
447 * defined(linux) is wrong, too
450 list_serial_devices (void)
457 list_serial_devices (void)
461 /* Linux serial files are of the form ttyS[0-9]+ */
462 GSList *l, *list = NULL;
463 GDir* dir = g_dir_open ("/dev", 0, NULL);
464 const char *filename;
467 while ((filename = g_dir_read_name (dir))) {
469 if (!strncmp (filename, "ttyS", 4))
470 list = g_slist_append (list, g_strconcat ("/dev/", filename, NULL));
476 array = mono_array_new (mono_domain_get (), mono_get_string_class (), g_slist_length (list));
477 for (l = list; l; l = l->next) {
478 mono_array_set (array, gpointer, i++, mono_string_new (mono_domain_get (), (char*)l->data));
485 #warning "list_serial_devices isn't ported to this OS"