2009-11-25 Zoltan Varga <vargaz@gmail.com>
[mono-project.git] / support / serial.c
blob0c393a7569427850d1d76b36811568f1f45a6587
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 /* sys/time.h (for timeval) is required when using osx 10.3 (but not 10.4) */
24 #ifdef __APPLE__
25 #include <sys/time.h>
26 #endif
28 /* This is a copy of System.IO.Ports.Handshake */
29 typedef enum {
30 NoneHandshake = 0,
31 XOnXOff = 1,
32 RequestToSend = 2,
33 RequestToSendXOnXOff = 3
34 } MonoHandshake;
36 /* This is a copy of System.IO.Ports.Parity */
37 typedef enum {
38 NoneParity = 0,
39 Odd = 1,
40 Even = 2,
41 Mark = 3,
42 Space = 4
43 } MonoParity;
45 /* This is a copy of System.IO.Ports.StopBits */
46 typedef enum {
47 NoneStopBits = 0,
48 One = 1,
49 Two = 2,
50 OnePointFive = 3
51 } MonoStopBits;
53 /* This is a copy of System.IO.Ports.SerialSignal */
54 typedef enum {
55 NoneSignal,
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 */
61 } MonoSerialSignal;
63 int
64 open_serial (char* devfile)
66 int fd;
67 fd = open (devfile, O_RDWR | O_NOCTTY | O_NONBLOCK);
69 if (fd == -1)
70 return -1;
72 return fd;
75 int
76 close_serial (int unix_fd)
78 // Linus writes: do not retry close after EINTR
79 return close (unix_fd);
82 guint32
83 read_serial (int fd, guchar *buffer, int offset, int count)
85 guint32 n;
87 n = read (fd, buffer + offset, count);
89 return (guint32) n;
92 int
93 write_serial (int fd, guchar *buffer, int offset, int count, int timeout)
95 struct pollfd pinfo;
96 guint32 n;
98 pinfo.fd = fd;
99 pinfo.events = POLLOUT;
100 pinfo.revents = POLLOUT;
102 n = count;
104 while (n > 0)
106 size_t t;
108 if (timeout != 0) {
109 int c;
111 while ((c = poll (&pinfo, 1, timeout)) == -1 && errno == EINTR)
113 if (c == -1)
114 return -1;
117 do {
118 t = write (fd, buffer + offset, n);
119 } while (t == -1 && errno == EINTR);
121 if (t < 0)
122 return -1;
124 offset += t;
125 n -= t;
128 return 0;
131 void
132 discard_buffer (int fd, gboolean input)
134 tcflush(fd, input ? TCIFLUSH : TCOFLUSH);
137 gint32
138 get_bytes_in_buffer (int fd, gboolean input)
140 gint32 retval;
142 if (ioctl (fd, input ? FIONREAD : TIOCOUTQ, &retval) == -1) {
143 return -1;
146 return retval;
149 gboolean
150 set_attributes (int fd, int baud_rate, MonoParity parity, int dataBits, MonoStopBits stopBits, MonoHandshake handshake)
152 struct termios newtio;
154 tcgetattr (fd, &newtio);
155 newtio.c_cflag |= (CLOCAL | CREAD);
156 newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ISIG | IEXTEN );
157 newtio.c_oflag &= ~(OPOST);
158 newtio.c_iflag = IGNBRK;
160 /* setup baudrate */
161 switch (baud_rate)
163 case 230400:
164 baud_rate = B230400;
165 break;
166 case 115200:
167 baud_rate = B115200;
168 break;
169 case 57600:
170 baud_rate = B57600;
171 break;
172 case 38400:
173 baud_rate = B38400;
174 break;
175 case 19200:
176 baud_rate = B19200;
177 break;
178 case 9600:
179 baud_rate = B9600;
180 break;
181 case 4800:
182 baud_rate = B4800;
183 break;
184 case 2400:
185 baud_rate = B2400;
186 break;
187 case 1800:
188 baud_rate = B1800;
189 break;
190 case 1200:
191 baud_rate = B1200;
192 break;
193 case 600:
194 baud_rate = B600;
195 break;
196 case 300:
197 baud_rate = B300;
198 break;
199 case 200:
200 baud_rate = B200;
201 break;
202 case 150:
203 baud_rate = B150;
204 break;
205 case 134:
206 baud_rate = B134;
207 break;
208 case 110:
209 baud_rate = B110;
210 break;
211 case 75:
212 baud_rate = B75;
213 break;
214 case 50:
215 case 0:
216 default:
217 baud_rate = B9600;
218 break;
221 /* char lenght */
222 newtio.c_cflag &= ~CSIZE;
223 switch (dataBits)
225 case 5:
226 newtio.c_cflag |= CS5;
227 break;
228 case 6:
229 newtio.c_cflag |= CS6;
230 break;
231 case 7:
232 newtio.c_cflag |= CS7;
233 break;
234 case 8:
235 default:
236 newtio.c_cflag |= CS8;
237 break;
240 /* stopbits */
241 switch (stopBits)
243 case NoneStopBits:
244 /* Unhandled */
245 break;
246 case One: /* One */
247 /* do nothing, the default is one stop bit */
248 newtio.c_cflag &= ~CSTOPB;
249 break;
250 case Two: /* Two */
251 newtio.c_cflag |= CSTOPB;
252 break;
253 case OnePointFive: /* OnePointFive */
254 /* XXX unhandled */
255 break;
258 /* parity */
259 newtio.c_iflag &= ~(INPCK | ISTRIP );
261 switch (parity)
263 case NoneParity: /* None */
264 newtio.c_cflag &= ~(PARENB | PARODD);
265 break;
267 case Odd: /* Odd */
268 newtio.c_cflag |= PARENB | PARODD;
269 break;
271 case Even: /* Even */
272 newtio.c_cflag &= ~(PARODD);
273 newtio.c_cflag |= (PARENB);
274 break;
276 case Mark: /* Mark */
277 /* XXX unhandled */
278 break;
279 case Space: /* Space */
280 /* XXX unhandled */
281 break;
284 newtio.c_iflag &= ~(IXOFF | IXON);
285 #ifdef CRTSCTS
286 newtio.c_cflag &= ~CRTSCTS;
287 #endif /* def CRTSCTS */
289 switch (handshake)
291 case NoneHandshake: /* None */
292 /* do nothing */
293 break;
294 case RequestToSend: /* RequestToSend (RTS) */
295 #ifdef CRTSCTS
296 newtio.c_cflag |= CRTSCTS;
297 #endif /* def CRTSCTS */
298 break;
299 case RequestToSendXOnXOff: /* RequestToSendXOnXOff (RTS + XON/XOFF) */
300 #ifdef CRTSCTS
301 newtio.c_cflag |= CRTSCTS;
302 #endif /* def CRTSCTS */
303 /* fall through */
304 case XOnXOff: /* XOnXOff */
305 newtio.c_iflag |= IXOFF | IXON;
306 break;
309 if (cfsetospeed (&newtio, baud_rate) < 0 || cfsetispeed (&newtio, baud_rate) < 0 ||
310 tcsetattr (fd, TCSANOW, &newtio) < 0)
312 return FALSE;
314 else
316 return TRUE;
321 static gint32
322 get_signal_code (MonoSerialSignal signal)
324 switch (signal) {
325 case Cd:
326 return TIOCM_CAR;
327 case Cts:
328 return TIOCM_CTS;
329 case Dsr:
330 return TIOCM_DSR;
331 case Dtr:
332 return TIOCM_DTR;
333 case Rts:
334 return TIOCM_RTS;
335 default:
336 return 0;
339 /* Not reached */
340 return 0;
343 static MonoSerialSignal
344 get_mono_signal_codes (int signals)
346 MonoSerialSignal retval = NoneSignal;
348 if ((signals & TIOCM_CAR) != 0)
349 retval |= Cd;
350 if ((signals & TIOCM_CTS) != 0)
351 retval |= Cts;
352 if ((signals & TIOCM_DSR) != 0)
353 retval |= Dsr;
354 if ((signals & TIOCM_DTR) != 0)
355 retval |= Dtr;
356 if ((signals & TIOCM_RTS) != 0)
357 retval |= Rts;
359 return retval;
362 MonoSerialSignal
363 get_signals (int fd, gint32 *error)
365 int signals;
367 *error = 0;
369 if (ioctl (fd, TIOCMGET, &signals) == -1) {
370 *error = -1;
371 return NoneSignal;
374 return get_mono_signal_codes (signals);
377 gint32
378 set_signal (int fd, MonoSerialSignal signal, gboolean value)
380 int signals, expected, activated;
382 expected = get_signal_code (signal);
383 if (ioctl (fd, TIOCMGET, &signals) == -1)
384 return -1;
386 activated = (signals & expected) != 0;
387 if (activated == value) /* Already set */
388 return 1;
390 if (value)
391 signals |= expected;
392 else
393 signals &= ~expected;
395 if (ioctl (fd, TIOCMSET, &signals) == -1)
396 return -1;
398 return 1;
401 void
402 breakprop (int fd)
404 tcsendbreak (fd, 0);
407 gboolean
408 poll_serial (int fd, gint32 *error, int timeout)
410 struct pollfd pinfo;
412 *error = 0;
414 pinfo.fd = fd;
415 pinfo.events = POLLIN;
416 pinfo.revents = 0;
418 while (poll (&pinfo, 1, timeout) == -1 && errno == EINTR) {
419 /* EINTR is an OK condition, we should not throw in the upper layer an IOException */
420 if (errno != EINTR){
421 *error = -1;
422 return FALSE;
426 return (pinfo.revents & POLLIN) != 0 ? 1 : 0;
430 * mono internals should not be used here.
431 * this serial stuff needs to be implemented with icalls.
432 * make this at least compile until the code is moved elsewhere
433 * defined(linux) is wrong, too
435 void*
436 list_serial_devices (void)
438 return NULL;
441 #if 0
442 MonoArray *
443 list_serial_devices (void)
445 MonoArray *array;
446 #if defined(linux)
447 /* Linux serial files are of the form ttyS[0-9]+ */
448 GSList *l, *list = NULL;
449 GDir* dir = g_dir_open ("/dev", 0, NULL);
450 const char *filename;
451 int i = 0;
453 while ((filename = g_dir_read_name (dir))) {
454 if (filename) {
455 if (!strncmp (filename, "ttyS", 4))
456 list = g_slist_append (list, g_strconcat ("/dev/", filename, NULL));
460 g_dir_close (dir);
462 array = mono_array_new (mono_domain_get (), mono_get_string_class (), g_slist_length (list));
463 for (l = list; l; l = l->next) {
464 mono_array_set (array, gpointer, i++, mono_string_new (mono_domain_get (), (char*)l->data));
465 g_free (l->data);
468 g_slist_free (list);
470 #else
471 #warning "list_serial_devices isn't ported to this OS"
472 #endif
474 return array;
476 #endif