2006-11-28 Miguel de Icaza <miguel@novell.com>
[mono-project.git] / support / serial.c
blob34d3943142d064db85829fb41129aef88ee0092d
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>
6 */
8 #include <termios.h>
9 #include <unistd.h>
10 #include <fcntl.h>
11 #include <string.h>
12 #include <sys/poll.h>
13 #include <sys/ioctl.h>
14 #include <errno.h>
16 #include <glib.h>
18 /* This is for FIONREAD on solaris */
19 #if defined(sun)
20 #include <sys/filio.h>
21 #endif
23 /* This is a copy of System.IO.Ports.Handshake */
24 typedef enum {
25 NoneHandshake = 0,
26 XOnXOff = 1,
27 RequestToSend = 2,
28 RequestToSendXOnXOff = 3
29 } MonoHandshake;
31 /* This is a copy of System.IO.Ports.Parity */
32 typedef enum {
33 NoneParity = 0,
34 Odd = 1,
35 Even = 2,
36 Mark = 3,
37 Space = 4
38 } MonoParity;
40 /* This is a copy of System.IO.Ports.StopBits */
41 typedef enum {
42 NoneStopBits = 0,
43 One = 1,
44 Two = 2,
45 OnePointFive = 3
46 } MonoStopBits;
48 /* This is a copy of System.IO.Ports.SerialSignal */
49 typedef enum {
50 NoneSignal,
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 */
56 } MonoSerialSignal;
58 int
59 open_serial (char* devfile)
61 int fd;
62 struct termios newtio;
64 fd = open (devfile, O_RDWR);
66 if (fd == -1)
67 return -1;
69 newtio.c_cflag = CLOCAL | CREAD;
70 newtio.c_iflag = 0;
71 newtio.c_oflag = 0;
72 newtio.c_lflag = 0;
74 tcflush(fd, TCIOFLUSH);
75 tcsetattr(fd,TCSANOW,&newtio);
77 return fd;
80 void
81 close_serial (int unix_fd)
83 close (unix_fd);
86 guint32
87 read_serial (int fd, guchar *buffer, int offset, int count)
89 guint32 n;
91 n = read (fd, buffer + offset, count);
93 return (guint32) n;
96 void
97 write_serial (int fd, guchar *buffer, int offset, int count, int timeout)
99 guint32 n;
101 struct pollfd ufd;
103 ufd.fd = fd;
104 ufd.events = POLLHUP | POLLOUT | POLLERR;
106 poll (&ufd, 1, timeout);
108 if ((ufd.revents & POLLOUT) != POLLOUT) {
109 return;
112 n = write (fd, buffer + offset, count);
115 void
116 discard_buffer (int fd, gboolean input)
118 tcflush(fd, input ? TCIFLUSH : TCOFLUSH);
121 gint32
122 get_bytes_in_buffer (int fd, gboolean input)
124 gint32 retval;
126 if (ioctl (fd, input ? FIONREAD : TIOCOUTQ, &retval) == -1) {
127 return -1;
130 return retval;
133 gboolean
134 set_attributes (int fd, int baud_rate, MonoParity parity, int dataBits, MonoStopBits stopBits, MonoHandshake handshake)
136 struct termios newtio;
138 tcgetattr (fd, &newtio);
140 switch (baud_rate) {
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;
158 case 50:
159 case 0:
160 default:
161 baud_rate = B9600;
162 break;
165 switch (parity) {
166 case NoneParity: /* None */
167 newtio.c_iflag |= IGNPAR;
168 newtio.c_cflag &= ~(PARENB | PARODD);
169 break;
170 case Odd: /* Odd */
171 newtio.c_iflag &= ~IGNPAR;
172 newtio.c_cflag |= PARENB | PARODD;
173 break;
174 case Even: /* Even */
175 newtio.c_iflag &= ~(IGNPAR | PARODD);
176 newtio.c_cflag |= PARENB;
177 break;
178 case Mark: /* Mark */
179 /* XXX unhandled */
180 break;
181 case Space: /* Space */
182 /* XXX unhandled */
183 break;
186 newtio.c_cflag &= ~CSIZE;
187 switch (dataBits) {
188 case 5: newtio.c_cflag |= CS5; break;
189 case 6: newtio.c_cflag |= CS6; break;
190 case 7: newtio.c_cflag |= CS7; break;
191 case 8:
192 default:
193 newtio.c_cflag |= CS8;
194 break;
197 newtio.c_cflag &= ~CSTOPB;
198 switch (stopBits) {
199 case NoneStopBits:
200 /* Unhandled */
201 break;
202 case One: /* One */
203 /* do nothing, the default is one stop bit */
204 break;
205 case Two: /* Two */
206 newtio.c_cflag |= CSTOPB;
207 break;
208 case OnePointFive: /* OnePointFive */
209 /* XXX unhandled */
210 break;
213 newtio.c_iflag &= ~IXOFF;
214 newtio.c_oflag &= ~IXON;
215 #ifdef CRTSCTS
216 newtio.c_cflag &= ~CRTSCTS;
217 #endif /* def CRTSCTS */
218 switch (handshake) {
219 case NoneHandshake: /* None */
220 /* do nothing */
221 break;
222 case RequestToSend: /* RequestToSend (RTS) */
223 #ifdef CRTSCTS
224 newtio.c_cflag |= CRTSCTS;
225 #endif /* def CRTSCTS */
226 break;
227 case RequestToSendXOnXOff: /* RequestToSendXOnXOff (RTS + XON/XOFF) */
228 #ifdef CRTSCTS
229 newtio.c_cflag |= CRTSCTS;
230 #endif /* def CRTSCTS */
231 /* fall through */
232 case XOnXOff: /* XOnXOff */
233 newtio.c_iflag |= IXOFF;
234 // newtio.c_oflag |= IXON;
235 break;
238 if (cfsetospeed (&newtio, baud_rate) < 0 || cfsetispeed (&newtio, baud_rate) < 0 ||
239 tcsetattr (fd, TCSADRAIN, &newtio) < 0)
240 return FALSE;
242 return TRUE;
245 static gint32
246 get_signal_code (MonoSerialSignal signal)
248 switch (signal) {
249 case Cd:
250 return TIOCM_CAR;
251 case Cts:
252 return TIOCM_CTS;
253 case Dsr:
254 return TIOCM_DSR;
255 case Dtr:
256 return TIOCM_DTR;
257 case Rts:
258 return TIOCM_RTS;
259 default:
260 return 0;
263 /* Not reached */
264 return 0;
267 static MonoSerialSignal
268 get_mono_signal_codes (int signals)
270 MonoSerialSignal retval = NoneSignal;
272 if ((signals & TIOCM_CAR) != 0)
273 retval |= Cd;
274 if ((signals & TIOCM_CTS) != 0)
275 retval |= Cts;
276 if ((signals & TIOCM_DSR) != 0)
277 retval |= Dsr;
278 if ((signals & TIOCM_DTR) != 0)
279 retval |= Dtr;
280 if ((signals & TIOCM_RTS) != 0)
281 retval |= Rts;
283 return retval;
286 MonoSerialSignal
287 get_signals (int fd, gint32 *error)
289 int signals;
291 *error = 0;
293 if (ioctl (fd, TIOCMGET, &signals) == -1) {
294 *error = -1;
295 return NoneSignal;
298 return get_mono_signal_codes (signals);
301 gint32
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)
308 return -1;
310 activated = (signals & expected) != 0;
311 if (activated == value) /* Already set */
312 return 1;
314 if (value)
315 signals |= expected;
316 else
317 signals &= ~expected;
319 if (ioctl (fd, TIOCMSET, &signals) == -1)
320 return -1;
322 return 1;
325 gboolean
326 poll_serial (int fd, gint32 *error, int timeout)
328 struct pollfd pinfo;
330 *error = 0;
332 pinfo.fd = fd;
333 pinfo.events = POLLIN;
334 pinfo.revents = 0;
336 if (poll (&pinfo, 1, timeout) == -1) {
337 /* EINTR is an OK condition, we should not throw in the upper layer an IOException */
338 if (errno == EINTR)
339 return FALSE;
340 *error = -1;
341 return FALSE;
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
353 void*
354 list_serial_devices (void)
356 return NULL;
359 #if 0
360 MonoArray *
361 list_serial_devices (void)
363 MonoArray *array;
364 #if defined(linux)
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;
369 int i = 0;
371 while ((filename = g_dir_read_name (dir))) {
372 if (filename) {
373 if (!strncmp (filename, "ttyS", 4))
374 list = g_slist_append (list, g_strconcat ("/dev/", filename, NULL));
378 g_dir_close (dir);
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));
383 g_free (l->data);
386 g_slist_free (list);
388 #else
389 #warning "list_serial_devices isn't ported to this OS"
390 #endif
392 return array;
394 #endif