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 /* This is a copy of System.IO.Ports.Handshake */
28 RequestToSendXOnXOff = 3
31 /* This is a copy of System.IO.Ports.Parity */
40 /* This is a copy of System.IO.Ports.StopBits */
48 /* This is a copy of System.IO.Ports.SerialSignal */
51 Cd = 1, /* Carrier detect */
52 Cts = 2, /* Clear to send */
53 Dsr = 4, /* Data set ready */
54 Dtr = 8, /* Data terminal ready */
55 Rts = 16 /* Request to send */
59 open_serial (char* devfile)
62 struct termios newtio;
64 fd = open (devfile, O_RDWR);
69 newtio.c_cflag = CLOCAL | CREAD;
74 tcflush(fd, TCIOFLUSH);
75 tcsetattr(fd,TCSANOW,&newtio);
81 close_serial (int unix_fd)
87 read_serial (int fd, guchar *buffer, int offset, int count)
91 n = read (fd, buffer + offset, count);
97 write_serial (int fd, guchar *buffer, int offset, int count, int timeout)
104 ufd.events = POLLHUP | POLLOUT | POLLERR;
106 while (poll (&ufd, 1, timeout) == -1 && errno == EINTR){
110 if ((ufd.revents & POLLOUT) != POLLOUT) {
114 n = write (fd, buffer + offset, count);
118 discard_buffer (int fd, gboolean input)
120 tcflush(fd, input ? TCIFLUSH : TCOFLUSH);
124 get_bytes_in_buffer (int fd, gboolean input)
128 if (ioctl (fd, input ? FIONREAD : TIOCOUTQ, &retval) == -1) {
136 set_attributes (int fd, int baud_rate, MonoParity parity, int dataBits, MonoStopBits stopBits, MonoHandshake handshake)
138 struct termios newtio;
140 tcgetattr (fd, &newtio);
143 case 230400: baud_rate = B230400; break;
144 case 115200: baud_rate = B115200; break;
145 case 57600: baud_rate = B57600; break;
146 case 38400: baud_rate = B38400; break;
147 case 19200: baud_rate = B19200; break;
148 case 9600: baud_rate = B9600; break;
149 case 4800: baud_rate = B4800; break;
150 case 2400: baud_rate = B2400; break;
151 case 1800: baud_rate = B1800; break;
152 case 1200: baud_rate = B1200; break;
153 case 600: baud_rate = B600; break;
154 case 300: baud_rate = B300; break;
155 case 200: baud_rate = B200; break;
156 case 150: baud_rate = B150; break;
157 case 134: baud_rate = B134; break;
158 case 110: baud_rate = B110; break;
159 case 75: baud_rate = B75; break;
168 case NoneParity: /* None */
169 newtio.c_iflag |= IGNPAR;
170 newtio.c_cflag &= ~(PARENB | PARODD);
173 newtio.c_iflag &= ~IGNPAR;
174 newtio.c_cflag |= PARENB | PARODD;
176 case Even: /* Even */
177 newtio.c_iflag &= ~(IGNPAR | PARODD);
178 newtio.c_cflag |= PARENB;
180 case Mark: /* Mark */
183 case Space: /* Space */
188 newtio.c_cflag &= ~CSIZE;
190 case 5: newtio.c_cflag |= CS5; break;
191 case 6: newtio.c_cflag |= CS6; break;
192 case 7: newtio.c_cflag |= CS7; break;
195 newtio.c_cflag |= CS8;
199 newtio.c_cflag &= ~CSTOPB;
205 /* do nothing, the default is one stop bit */
208 newtio.c_cflag |= CSTOPB;
210 case OnePointFive: /* OnePointFive */
215 newtio.c_iflag &= ~IXOFF;
216 newtio.c_oflag &= ~IXON;
218 newtio.c_cflag &= ~CRTSCTS;
219 #endif /* def CRTSCTS */
221 case NoneHandshake: /* None */
224 case RequestToSend: /* RequestToSend (RTS) */
226 newtio.c_cflag |= CRTSCTS;
227 #endif /* def CRTSCTS */
229 case RequestToSendXOnXOff: /* RequestToSendXOnXOff (RTS + XON/XOFF) */
231 newtio.c_cflag |= CRTSCTS;
232 #endif /* def CRTSCTS */
234 case XOnXOff: /* XOnXOff */
235 newtio.c_iflag |= IXOFF;
236 // newtio.c_oflag |= IXON;
240 if (cfsetospeed (&newtio, baud_rate) < 0 || cfsetispeed (&newtio, baud_rate) < 0 ||
241 tcsetattr (fd, TCSADRAIN, &newtio) < 0)
248 get_signal_code (MonoSerialSignal signal)
269 static MonoSerialSignal
270 get_mono_signal_codes (int signals)
272 MonoSerialSignal retval = NoneSignal;
274 if ((signals & TIOCM_CAR) != 0)
276 if ((signals & TIOCM_CTS) != 0)
278 if ((signals & TIOCM_DSR) != 0)
280 if ((signals & TIOCM_DTR) != 0)
282 if ((signals & TIOCM_RTS) != 0)
289 get_signals (int fd, gint32 *error)
295 if (ioctl (fd, TIOCMGET, &signals) == -1) {
300 return get_mono_signal_codes (signals);
304 set_signal (int fd, MonoSerialSignal signal, gboolean value)
306 int signals, expected, activated;
308 expected = get_signal_code (signal);
309 if (ioctl (fd, TIOCMGET, &signals) == -1)
312 activated = (signals & expected) != 0;
313 if (activated == value) /* Already set */
319 signals &= ~expected;
321 if (ioctl (fd, TIOCMSET, &signals) == -1)
328 poll_serial (int fd, gint32 *error, int timeout)
335 pinfo.events = POLLIN;
338 while (poll (&pinfo, 1, timeout) == -1 && errno == EINTR) {
339 /* EINTR is an OK condition, we should not throw in the upper layer an IOException */
346 return (pinfo.revents & POLLIN) != 0 ? 1 : 0;
350 * mono internals should not be used here.
351 * this serial stuff needs to be implemented with icalls.
352 * make this at least compile until the code is moved elsewhere
353 * defined(linux) is wrong, too
356 list_serial_devices (void)
363 list_serial_devices (void)
367 /* Linux serial files are of the form ttyS[0-9]+ */
368 GSList *l, *list = NULL;
369 GDir* dir = g_dir_open ("/dev", 0, NULL);
370 const char *filename;
373 while ((filename = g_dir_read_name (dir))) {
375 if (!strncmp (filename, "ttyS", 4))
376 list = g_slist_append (list, g_strconcat ("/dev/", filename, NULL));
382 array = mono_array_new (mono_domain_get (), mono_get_string_class (), g_slist_length (list));
383 for (l = list; l; l = l->next) {
384 mono_array_set (array, gpointer, i++, mono_string_new (mono_domain_get (), (char*)l->data));
391 #warning "list_serial_devices isn't ported to this OS"