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);
73 close_serial (int unix_fd)
75 // Linus writes: do not retry close after EINTR
76 return close (unix_fd);
80 read_serial (int fd, guchar *buffer, int offset, int count)
84 n = read (fd, buffer + offset, count);
90 write_serial (int fd, guchar *buffer, int offset, int count, int timeout)
96 pinfo.events = POLLOUT;
97 pinfo.revents = POLLOUT;
108 while ((c = poll (&pinfo, 1, timeout)) == -1 && errno == EINTR)
115 t = write (fd, buffer + offset, n);
116 } while (t == -1 && errno == EINTR);
129 discard_buffer (int fd, gboolean input)
131 return tcflush(fd, input ? TCIFLUSH : TCOFLUSH);
135 get_bytes_in_buffer (int fd, gboolean input)
139 if (ioctl (fd, input ? FIONREAD : TIOCOUTQ, &retval) == -1) {
147 set_attributes (int fd, int baud_rate, MonoParity parity, int dataBits, MonoStopBits stopBits, MonoHandshake handshake)
149 struct termios newtio;
151 if (tcgetattr (fd, &newtio) == -1)
154 newtio.c_cflag |= (CLOCAL | CREAD);
155 newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ISIG | IEXTEN );
156 newtio.c_oflag &= ~(OPOST);
157 newtio.c_iflag = IGNBRK;
162 /*Some values are not defined on OSX and *BSD */
232 newtio.c_cflag &= ~CSIZE;
236 newtio.c_cflag |= CS5;
239 newtio.c_cflag |= CS6;
242 newtio.c_cflag |= CS7;
246 newtio.c_cflag |= CS8;
257 /* do nothing, the default is one stop bit */
258 newtio.c_cflag &= ~CSTOPB;
261 newtio.c_cflag |= CSTOPB;
263 case OnePointFive: /* OnePointFive */
269 newtio.c_iflag &= ~(INPCK | ISTRIP );
273 case NoneParity: /* None */
274 newtio.c_cflag &= ~(PARENB | PARODD);
278 newtio.c_cflag |= PARENB | PARODD;
281 case Even: /* Even */
282 newtio.c_cflag &= ~(PARODD);
283 newtio.c_cflag |= (PARENB);
286 case Mark: /* Mark */
289 case Space: /* Space */
294 newtio.c_iflag &= ~(IXOFF | IXON);
296 newtio.c_cflag &= ~CRTSCTS;
297 #endif /* def CRTSCTS */
301 case NoneHandshake: /* None */
304 case RequestToSend: /* RequestToSend (RTS) */
306 newtio.c_cflag |= CRTSCTS;
307 #endif /* def CRTSCTS */
309 case RequestToSendXOnXOff: /* RequestToSendXOnXOff (RTS + XON/XOFF) */
311 newtio.c_cflag |= CRTSCTS;
312 #endif /* def CRTSCTS */
314 case XOnXOff: /* XOnXOff */
315 newtio.c_iflag |= IXOFF | IXON;
319 if (cfsetospeed (&newtio, baud_rate) < 0 || cfsetispeed (&newtio, baud_rate) < 0 ||
320 tcsetattr (fd, TCSANOW, &newtio) < 0)
332 get_signal_code (MonoSerialSignal signal)
353 static MonoSerialSignal
354 get_mono_signal_codes (int signals)
356 MonoSerialSignal retval = NoneSignal;
358 if ((signals & TIOCM_CAR) != 0)
360 if ((signals & TIOCM_CTS) != 0)
362 if ((signals & TIOCM_DSR) != 0)
364 if ((signals & TIOCM_DTR) != 0)
366 if ((signals & TIOCM_RTS) != 0)
373 get_signals (int fd, gint32 *error)
379 if (ioctl (fd, TIOCMGET, &signals) == -1) {
384 return get_mono_signal_codes (signals);
388 set_signal (int fd, MonoSerialSignal signal, gboolean value)
390 int signals, expected, activated;
392 expected = get_signal_code (signal);
393 if (ioctl (fd, TIOCMGET, &signals) == -1)
396 activated = (signals & expected) != 0;
397 if (activated == value) /* Already set */
403 signals &= ~expected;
405 if (ioctl (fd, TIOCMSET, &signals) == -1)
414 return tcsendbreak (fd, 0);
418 poll_serial (int fd, gint32 *error, int timeout)
425 pinfo.events = POLLIN;
428 while (poll (&pinfo, 1, timeout) == -1 && errno == EINTR) {
429 /* EINTR is an OK condition, we should not throw in the upper layer an IOException */
436 return (pinfo.revents & POLLIN) != 0 ? 1 : 0;
440 * mono internals should not be used here.
441 * this serial stuff needs to be implemented with icalls.
442 * make this at least compile until the code is moved elsewhere
443 * defined(linux) is wrong, too
446 list_serial_devices (void)
453 list_serial_devices (void)
457 /* Linux serial files are of the form ttyS[0-9]+ */
458 GSList *l, *list = NULL;
459 GDir* dir = g_dir_open ("/dev", 0, NULL);
460 const char *filename;
463 while ((filename = g_dir_read_name (dir))) {
465 if (!strncmp (filename, "ttyS", 4))
466 list = g_slist_append (list, g_strconcat ("/dev/", filename, NULL));
472 array = mono_array_new (mono_domain_get (), mono_get_string_class (), g_slist_length (list));
473 for (l = list; l; l = l->next) {
474 mono_array_set (array, gpointer, i++, mono_string_new (mono_domain_get (), (char*)l->data));
481 #warning "list_serial_devices isn't ported to this OS"