Remove redundant timeout check, de-select, use-poll instead
[mono.git] / support / serial.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
3 /* serial port functions
4  *
5  * Author: Chris Toshok <toshok@ximian.com>
6  */
7
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>
15
16 #include <glib.h>
17
18 /* This is for FIONREAD on solaris */
19 #if defined(sun)
20 #include <sys/filio.h>
21 #endif
22
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
27
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;
35
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;
44
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;
52
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;
62
63 int
64 open_serial (char* devfile)
65 {
66         int fd;
67         fd = open (devfile, O_RDWR | O_NOCTTY | O_NONBLOCK);
68
69         if (fd == -1)
70                 return -1;
71
72         return fd;
73 }
74
75 void
76 close_serial (int unix_fd)
77 {
78         close (unix_fd);
79 }
80
81 guint32
82 read_serial (int fd, guchar *buffer, int offset, int count)
83 {
84         guint32 n;
85  
86         n = read (fd, buffer + offset, count);
87
88         return (guint32) n;
89 }
90
91 int
92 write_serial (int fd, guchar *buffer, int offset, int count, int timeout)
93 {
94         struct pollfd pinfo;
95
96         pinfo.fd = fd;
97         pinfo.events = POLLOUT;
98         pinfo.revents = POLLOUT;
99
100         
101         struct timeval tmval;
102         fd_set writefs;
103         guint32 n;
104
105         n = count;
106
107         FD_SET(fd, &writefs);
108         tmval.tv_sec = timeout / 1000;
109         tmval.tv_usec = (timeout - tmval.tv_sec) * 1000;        
110         
111         while (n > 0)
112         {
113                 size_t t;
114                         
115                 if (timeout > 0) {
116                         int c;
117                         
118                         while ((c = poll (&pinfo, 1, timeout)) == -1 && errno == EINTR)
119                                 ;
120                         if (c == -1)
121                                 return -1;
122                 }               
123
124                 do {
125                         t = write (fd, buffer + offset, n);
126                 } while (t == -1 && errno == EINTR);
127
128                 if (t < 0)
129                         return -1;
130                 
131                 offset += t;
132                 n -= t; 
133         }
134  
135         return 0;
136 }
137
138 void
139 discard_buffer (int fd, gboolean input)
140 {
141         tcflush(fd, input ? TCIFLUSH : TCOFLUSH);
142 }
143
144 gint32
145 get_bytes_in_buffer (int fd, gboolean input)
146 {
147         gint32 retval;
148
149         if (ioctl (fd, input ? FIONREAD : TIOCOUTQ, &retval) == -1) {
150                 return -1;
151         }
152
153         return retval;
154 }
155
156 gboolean
157 set_attributes (int fd, int baud_rate, MonoParity parity, int dataBits, MonoStopBits stopBits, MonoHandshake handshake)
158 {
159         struct termios newtio;
160
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;
166
167         /* setup baudrate */
168         switch (baud_rate)
169         {
170         case 230400: 
171             baud_rate = B230400;
172             break;
173         case 115200: 
174             baud_rate = B115200;
175             break;
176         case 57600:
177             baud_rate = B57600;
178             break;
179         case 38400: 
180             baud_rate = B38400;
181             break;
182         case 19200: 
183             baud_rate = B19200;
184             break;
185         case 9600: 
186                 baud_rate = B9600;
187                 break;
188         case 4800: 
189             baud_rate = B4800;
190                 break;
191         case 2400: 
192             baud_rate = B2400;
193                 break;
194         case 1800: 
195             baud_rate = B1800;
196                 break;
197         case 1200: 
198             baud_rate = B1200;
199                 break;
200         case 600: 
201             baud_rate = B600;
202             break;
203         case 300: 
204             baud_rate = B300;
205             break;
206         case 200: 
207             baud_rate = B200;
208             break;
209         case 150: 
210             baud_rate = B150;
211             break;
212         case 134: 
213             baud_rate = B134;
214             break;
215         case 110: 
216             baud_rate = B110;
217             break;
218         case 75: 
219             baud_rate = B75;
220             break;
221         case 50:
222         case 0:
223         default:
224             baud_rate = B9600;
225                 break;
226         }
227
228         /* char lenght */
229         newtio.c_cflag &= ~CSIZE;
230         switch (dataBits)
231         {
232         case 5: 
233             newtio.c_cflag |= CS5;
234             break;
235         case 6: 
236             newtio.c_cflag |= CS6;
237             break;
238         case 7: 
239             newtio.c_cflag |= CS7;
240             break;
241         case 8:
242         default:
243                 newtio.c_cflag |= CS8;
244                 break;
245         }
246
247         /* stopbits */
248         switch (stopBits)
249         {
250         case NoneStopBits:
251                 /* Unhandled */
252                 break;
253         case One: /* One */
254                 /* do nothing, the default is one stop bit */
255             newtio.c_cflag &= ~CSTOPB;
256                 break;
257         case Two: /* Two */
258                 newtio.c_cflag |= CSTOPB;
259                 break;
260         case OnePointFive: /* OnePointFive */
261                 /* XXX unhandled */
262                 break;
263         }
264
265         /* parity */
266         newtio.c_iflag &= ~(INPCK | ISTRIP );
267
268         switch (parity)
269         {
270         case NoneParity: /* None */
271             newtio.c_cflag &= ~(PARENB | PARODD);
272             break;
273             
274         case Odd: /* Odd */
275             newtio.c_cflag |= PARENB | PARODD;
276             break;
277             
278         case Even: /* Even */
279             newtio.c_cflag &= ~(PARODD);
280             break;
281             
282         case Mark: /* Mark */
283             /* XXX unhandled */
284             break;
285         case Space: /* Space */
286             /* XXX unhandled */
287             break;
288         }
289
290         newtio.c_iflag &= ~(IXOFF | IXON);
291 #ifdef CRTSCTS
292         newtio.c_cflag &= ~CRTSCTS;
293 #endif /* def CRTSCTS */
294
295         switch (handshake)
296         {
297         case NoneHandshake: /* None */
298                 /* do nothing */
299                 break;
300         case RequestToSend: /* RequestToSend (RTS) */
301 #ifdef CRTSCTS
302                 newtio.c_cflag |= CRTSCTS;
303 #endif /* def CRTSCTS */
304                 break;
305         case RequestToSendXOnXOff: /* RequestToSendXOnXOff (RTS + XON/XOFF) */
306 #ifdef CRTSCTS
307                 newtio.c_cflag |= CRTSCTS;
308 #endif /* def CRTSCTS */
309                 /* fall through */
310         case XOnXOff: /* XOnXOff */
311             newtio.c_iflag |= IXOFF | IXON;
312                 break;
313         }
314         
315         if (cfsetospeed (&newtio, baud_rate) < 0 || cfsetispeed (&newtio, baud_rate) < 0 ||
316             tcsetattr (fd, TCSANOW, &newtio) < 0)
317         {
318                 return FALSE;
319         }
320         else
321         {
322         return TRUE;
323         }
324 }
325
326
327 static gint32
328 get_signal_code (MonoSerialSignal signal)
329 {
330         switch (signal) {
331                 case Cd:
332                         return TIOCM_CAR;
333                 case Cts:
334                         return TIOCM_CTS;
335                 case Dsr:
336                         return TIOCM_DSR;
337                 case Dtr:
338                         return TIOCM_DTR;
339                 case Rts:
340                         return TIOCM_RTS;
341                 default:
342                         return 0;
343         }
344
345         /* Not reached */
346         return 0;
347 }
348
349 static MonoSerialSignal
350 get_mono_signal_codes (int signals)
351 {
352         MonoSerialSignal retval = NoneSignal;
353
354         if ((signals & TIOCM_CAR) != 0)
355                 retval |= Cd;
356         if ((signals & TIOCM_CTS) != 0)
357                 retval |= Cts;
358         if ((signals & TIOCM_DSR) != 0)
359                 retval |= Dsr;
360         if ((signals & TIOCM_DTR) != 0)
361                 retval |= Dtr;
362         if ((signals & TIOCM_RTS) != 0)
363                 retval |= Rts;
364
365         return retval;
366 }
367
368 MonoSerialSignal
369 get_signals (int fd, gint32 *error)
370 {
371         int signals;
372
373         *error = 0;
374         
375         if (ioctl (fd, TIOCMGET, &signals) == -1) {
376                 *error = -1;
377                 return NoneSignal;
378         }
379         
380         return get_mono_signal_codes (signals);
381 }
382
383 gint32
384 set_signal (int fd, MonoSerialSignal signal, gboolean value)
385 {
386         int signals, expected, activated;
387
388         expected = get_signal_code (signal);
389         if (ioctl (fd, TIOCMGET, &signals) == -1)
390                 return -1;
391         
392         activated = (signals & expected) != 0;
393         if (activated == value) /* Already set */
394                 return 1;
395         
396         if (value)
397                 signals |= expected;
398         else
399                 signals &= ~expected;
400         
401         if (ioctl (fd, TIOCMSET, &signals) == -1)
402                 return -1;
403         
404         return 1;
405 }
406
407 void
408 breakprop (int fd)
409 {
410         tcsendbreak (fd, 0);
411 }
412
413 gboolean
414 poll_serial (int fd, gint32 *error, int timeout)
415 {
416         struct pollfd pinfo;
417         
418         *error = 0;
419         
420         pinfo.fd = fd;
421         pinfo.events = POLLIN;
422         pinfo.revents = 0;
423
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 */
426                 if (errno != EINTR){
427                         *error = -1;
428                         return FALSE;
429                 }
430         }
431
432         return (pinfo.revents & POLLIN) != 0 ? 1 : 0;
433 }
434
435 /*
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
440  */
441 void*
442 list_serial_devices (void)
443 {
444         return NULL;
445 }
446
447 #if 0
448 MonoArray *
449 list_serial_devices (void)
450 {
451         MonoArray *array;
452 #if defined(linux)
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;
457         int i = 0;
458
459         while ((filename = g_dir_read_name (dir))) {
460                 if (filename) {
461                         if (!strncmp (filename, "ttyS", 4))
462                                 list = g_slist_append (list, g_strconcat ("/dev/", filename, NULL));
463                 }
464         }
465
466         g_dir_close (dir);
467   
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));
471                 g_free (l->data);
472         }
473
474         g_slist_free (list);
475
476 #else
477 #warning "list_serial_devices isn't ported to this OS"
478 #endif
479
480         return array;
481 }
482 #endif
483