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 poll (&ufd, 1, timeout);
108 if ((ufd.revents & POLLOUT) != POLLOUT) {
112 n = write (fd, buffer + offset, count);
116 discard_buffer (int fd, gboolean input)
118 tcflush(fd, input ? TCIFLUSH : TCOFLUSH);
122 get_bytes_in_buffer (int fd, gboolean input)
126 if (ioctl (fd, input ? FIONREAD : TIOCOUTQ, &retval) == -1) {
134 set_attributes (int fd, int baud_rate, MonoParity parity, int dataBits, MonoStopBits stopBits, MonoHandshake handshake)
136 struct termios newtio;
138 tcgetattr (fd, &newtio);
141 case 230400: baud_rate = B230400; break;
142 case 115200: baud_rate = B115200; break;
143 case 57600: baud_rate = B57600; break;
144 case 38400: baud_rate = B38400; break;
145 case 19200: baud_rate = B19200; break;
146 case 9600: baud_rate = B9600; break;
147 case 4800: baud_rate = B4800; break;
148 case 2400: baud_rate = B2400; break;
149 case 1800: baud_rate = B1800; break;
150 case 1200: baud_rate = B1200; break;
151 case 600: baud_rate = B600; break;
152 case 300: baud_rate = B300; break;
153 case 200: baud_rate = B200; break;
154 case 150: baud_rate = B150; break;
155 case 134: baud_rate = B134; break;
156 case 110: baud_rate = B110; break;
157 case 75: baud_rate = B75; break;
166 case NoneParity: /* None */
167 newtio.c_iflag |= IGNPAR;
168 newtio.c_cflag &= ~(PARENB | PARODD);
171 newtio.c_iflag &= ~IGNPAR;
172 newtio.c_cflag |= PARENB | PARODD;
174 case Even: /* Even */
175 newtio.c_iflag &= ~(IGNPAR | PARODD);
176 newtio.c_cflag |= PARENB;
178 case Mark: /* Mark */
181 case Space: /* Space */
186 newtio.c_cflag &= ~CSIZE;
188 case 5: newtio.c_cflag |= CS5; break;
189 case 6: newtio.c_cflag |= CS6; break;
190 case 7: newtio.c_cflag |= CS7; break;
193 newtio.c_cflag |= CS8;
197 newtio.c_cflag &= ~CSTOPB;
203 /* do nothing, the default is one stop bit */
206 newtio.c_cflag |= CSTOPB;
208 case OnePointFive: /* OnePointFive */
213 newtio.c_iflag &= ~IXOFF;
214 newtio.c_oflag &= ~IXON;
216 newtio.c_cflag &= ~CRTSCTS;
217 #endif /* def CRTSCTS */
219 case NoneHandshake: /* None */
222 case RequestToSend: /* RequestToSend (RTS) */
224 newtio.c_cflag |= CRTSCTS;
225 #endif /* def CRTSCTS */
227 case RequestToSendXOnXOff: /* RequestToSendXOnXOff (RTS + XON/XOFF) */
229 newtio.c_cflag |= CRTSCTS;
230 #endif /* def CRTSCTS */
232 case XOnXOff: /* XOnXOff */
233 newtio.c_iflag |= IXOFF;
234 // newtio.c_oflag |= IXON;
238 if (cfsetospeed (&newtio, baud_rate) < 0 || cfsetispeed (&newtio, baud_rate) < 0 ||
239 tcsetattr (fd, TCSADRAIN, &newtio) < 0)
246 get_signal_code (MonoSerialSignal signal)
267 static MonoSerialSignal
268 get_mono_signal_codes (int signals)
270 MonoSerialSignal retval = NoneSignal;
272 if ((signals & TIOCM_CAR) != 0)
274 if ((signals & TIOCM_CTS) != 0)
276 if ((signals & TIOCM_DSR) != 0)
278 if ((signals & TIOCM_DTR) != 0)
280 if ((signals & TIOCM_RTS) != 0)
287 get_signals (int fd, gint32 *error)
293 if (ioctl (fd, TIOCMGET, &signals) == -1) {
298 return get_mono_signal_codes (signals);
302 set_signal (int fd, MonoSerialSignal signal, gboolean value)
304 int signals, expected, activated;
306 expected = get_signal_code (signal);
307 if (ioctl (fd, TIOCMGET, &signals) == -1)
310 activated = (signals & expected) != 0;
311 if (activated == value) /* Already set */
317 signals &= ~expected;
319 if (ioctl (fd, TIOCMSET, &signals) == -1)
326 poll_serial (int fd, gint32 *error, int timeout)
333 pinfo.events = POLLIN;
336 if (poll (&pinfo, 1, timeout) == -1) {
337 /* EINTR is an OK condition, we should not throw in the upper layer an IOException */
344 return (pinfo.revents & POLLIN) != 0 ? 1 : 0;
348 * mono internals should not be used here.
349 * this serial stuff needs to be implemented with icalls.
350 * make this at least compile until the code is moved elsewhere
351 * defined(linux) is wrong, too
354 list_serial_devices (void)
361 list_serial_devices (void)
365 /* Linux serial files are of the form ttyS[0-9]+ */
366 GSList *l, *list = NULL;
367 GDir* dir = g_dir_open ("/dev", 0, NULL);
368 const char *filename;
371 while ((filename = g_dir_read_name (dir))) {
373 if (!strncmp (filename, "ttyS", 4))
374 list = g_slist_append (list, g_strconcat ("/dev/", filename, NULL));
380 array = mono_array_new (mono_domain_get (), mono_get_string_class (), g_slist_length (list));
381 for (l = list; l; l = l->next) {
382 mono_array_set (array, gpointer, i++, mono_string_new (mono_domain_get (), (char*)l->data));
389 #warning "list_serial_devices isn't ported to this OS"