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)
82 read_serial (int fd, guchar *buffer, int offset, int count)
86 n = read (fd, buffer + offset, count);
92 write_serial (int fd, guchar *buffer, int offset, int count, int timeout)
97 pinfo.events = POLLOUT;
98 pinfo.revents = POLLOUT;
101 struct timeval tmval;
107 FD_SET(fd, &writefs);
108 tmval.tv_sec = timeout / 1000;
109 tmval.tv_usec = (timeout - tmval.tv_sec) * 1000;
118 while ((c = poll (&pinfo, 1, timeout)) == -1 && errno == EINTR)
125 t = write (fd, buffer + offset, n);
126 } while (t == -1 && errno == EINTR);
139 discard_buffer (int fd, gboolean input)
141 tcflush(fd, input ? TCIFLUSH : TCOFLUSH);
145 get_bytes_in_buffer (int fd, gboolean input)
149 if (ioctl (fd, input ? FIONREAD : TIOCOUTQ, &retval) == -1) {
157 set_attributes (int fd, int baud_rate, MonoParity parity, int dataBits, MonoStopBits stopBits, MonoHandshake handshake)
159 struct termios newtio;
161 tcgetattr (fd, &newtio);
162 newtio.c_cflag |= (CLOCAL | CREAD);
163 newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ISIG | IEXTEN );
164 newtio.c_oflag &= ~(OPOST);
165 newtio.c_iflag = IGNBRK;
229 newtio.c_cflag &= ~CSIZE;
233 newtio.c_cflag |= CS5;
236 newtio.c_cflag |= CS6;
239 newtio.c_cflag |= CS7;
243 newtio.c_cflag |= CS8;
254 /* do nothing, the default is one stop bit */
255 newtio.c_cflag &= ~CSTOPB;
258 newtio.c_cflag |= CSTOPB;
260 case OnePointFive: /* OnePointFive */
266 newtio.c_iflag &= ~(INPCK | ISTRIP );
270 case NoneParity: /* None */
271 newtio.c_cflag &= ~(PARENB | PARODD);
275 newtio.c_cflag |= PARENB | PARODD;
278 case Even: /* Even */
279 newtio.c_cflag &= ~(PARODD);
282 case Mark: /* Mark */
285 case Space: /* Space */
290 newtio.c_iflag &= ~(IXOFF | IXON);
292 newtio.c_cflag &= ~CRTSCTS;
293 #endif /* def CRTSCTS */
297 case NoneHandshake: /* None */
300 case RequestToSend: /* RequestToSend (RTS) */
302 newtio.c_cflag |= CRTSCTS;
303 #endif /* def CRTSCTS */
305 case RequestToSendXOnXOff: /* RequestToSendXOnXOff (RTS + XON/XOFF) */
307 newtio.c_cflag |= CRTSCTS;
308 #endif /* def CRTSCTS */
310 case XOnXOff: /* XOnXOff */
311 newtio.c_iflag |= IXOFF | IXON;
315 if (cfsetospeed (&newtio, baud_rate) < 0 || cfsetispeed (&newtio, baud_rate) < 0 ||
316 tcsetattr (fd, TCSANOW, &newtio) < 0)
328 get_signal_code (MonoSerialSignal signal)
349 static MonoSerialSignal
350 get_mono_signal_codes (int signals)
352 MonoSerialSignal retval = NoneSignal;
354 if ((signals & TIOCM_CAR) != 0)
356 if ((signals & TIOCM_CTS) != 0)
358 if ((signals & TIOCM_DSR) != 0)
360 if ((signals & TIOCM_DTR) != 0)
362 if ((signals & TIOCM_RTS) != 0)
369 get_signals (int fd, gint32 *error)
375 if (ioctl (fd, TIOCMGET, &signals) == -1) {
380 return get_mono_signal_codes (signals);
384 set_signal (int fd, MonoSerialSignal signal, gboolean value)
386 int signals, expected, activated;
388 expected = get_signal_code (signal);
389 if (ioctl (fd, TIOCMGET, &signals) == -1)
392 activated = (signals & expected) != 0;
393 if (activated == value) /* Already set */
399 signals &= ~expected;
401 if (ioctl (fd, TIOCMSET, &signals) == -1)
414 poll_serial (int fd, gint32 *error, int timeout)
421 pinfo.events = POLLIN;
424 while (poll (&pinfo, 1, timeout) == -1 && errno == EINTR) {
425 /* EINTR is an OK condition, we should not throw in the upper layer an IOException */
432 return (pinfo.revents & POLLIN) != 0 ? 1 : 0;
436 * mono internals should not be used here.
437 * this serial stuff needs to be implemented with icalls.
438 * make this at least compile until the code is moved elsewhere
439 * defined(linux) is wrong, too
442 list_serial_devices (void)
449 list_serial_devices (void)
453 /* Linux serial files are of the form ttyS[0-9]+ */
454 GSList *l, *list = NULL;
455 GDir* dir = g_dir_open ("/dev", 0, NULL);
456 const char *filename;
459 while ((filename = g_dir_read_name (dir))) {
461 if (!strncmp (filename, "ttyS", 4))
462 list = g_slist_append (list, g_strconcat ("/dev/", filename, NULL));
468 array = mono_array_new (mono_domain_get (), mono_get_string_class (), g_slist_length (list));
469 for (l = list; l; l = l->next) {
470 mono_array_set (array, gpointer, i++, mono_string_new (mono_domain_get (), (char*)l->data));
477 #warning "list_serial_devices isn't ported to this OS"