[xbuild] Implement FileLogger . Fix #676650 .
[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 <errno.h>
13 #if defined(__APPLE__)
14 #include "fakepoll.h"
15 #else
16 #include <sys/poll.h>
17 #endif
18 #include <sys/ioctl.h>
19
20 #include <glib.h>
21
22 /* This is for FIONREAD on solaris */
23 #if defined(sun)
24 #include <sys/filio.h>
25 #endif
26
27 /* sys/time.h (for timeval) is required when using osx 10.3 (but not 10.4) */
28 #ifdef __APPLE__
29 #include <sys/time.h>
30 #endif
31
32 /* This is a copy of System.IO.Ports.Handshake */
33 typedef enum {
34         NoneHandshake = 0,
35         XOnXOff = 1,
36         RequestToSend = 2,
37         RequestToSendXOnXOff = 3
38 } MonoHandshake;
39
40 /* This is a copy of System.IO.Ports.Parity */
41 typedef enum {
42         NoneParity = 0,
43         Odd = 1,
44         Even = 2,
45         Mark = 3,
46         Space = 4
47 } MonoParity;
48
49 /* This is a copy of System.IO.Ports.StopBits */
50 typedef enum {
51         NoneStopBits = 0,
52         One = 1,
53         Two = 2,
54         OnePointFive = 3
55 } MonoStopBits;
56
57 /* This is a copy of System.IO.Ports.SerialSignal */
58 typedef enum {
59         NoneSignal,
60         Cd = 1, /* Carrier detect */
61         Cts = 2, /* Clear to send */
62         Dsr = 4, /* Data set ready */
63         Dtr = 8, /* Data terminal ready */
64         Rts = 16  /* Request to send */
65 } MonoSerialSignal;
66
67 int
68 open_serial (char* devfile)
69 {
70         int fd;
71         fd = open (devfile, O_RDWR | O_NOCTTY | O_NONBLOCK);
72
73         return fd;
74 }
75
76 int
77 close_serial (int unix_fd)
78 {
79         // Linus writes: do not retry close after EINTR
80         return close (unix_fd);
81 }
82
83 guint32
84 read_serial (int fd, guchar *buffer, int offset, int count)
85 {
86         guint32 n;
87  
88         n = read (fd, buffer + offset, count);
89
90         return (guint32) n;
91 }
92
93 int
94 write_serial (int fd, guchar *buffer, int offset, int count, int timeout)
95 {
96         struct pollfd pinfo;
97         guint32 n;
98
99         pinfo.fd = fd;
100         pinfo.events = POLLOUT;
101         pinfo.revents = POLLOUT;
102
103         n = count;
104
105         while (n > 0)
106         {
107                 size_t t;
108                         
109                 if (timeout != 0) {
110                         int c;
111                         
112                         while ((c = poll (&pinfo, 1, timeout)) == -1 && errno == EINTR)
113                                 ;
114                         if (c == -1)
115                                 return -1;
116                 }               
117
118                 do {
119                         t = write (fd, buffer + offset, n);
120                 } while (t == -1 && errno == EINTR);
121
122                 if (t < 0)
123                         return -1;
124                 
125                 offset += t;
126                 n -= t; 
127         }
128  
129         return 0;
130 }
131
132 int
133 discard_buffer (int fd, gboolean input)
134 {
135         return tcflush(fd, input ? TCIFLUSH : TCOFLUSH);
136 }
137
138 gint32
139 get_bytes_in_buffer (int fd, gboolean input)
140 {
141         gint32 retval;
142
143         if (ioctl (fd, input ? FIONREAD : TIOCOUTQ, &retval) == -1) {
144                 return -1;
145         }
146
147         return retval;
148 }
149
150 gboolean
151 set_attributes (int fd, int baud_rate, MonoParity parity, int dataBits, MonoStopBits stopBits, MonoHandshake handshake)
152 {
153         struct termios newtio;
154
155         if (tcgetattr (fd, &newtio) == -1)
156                 return FALSE;
157
158         newtio.c_cflag |=  (CLOCAL | CREAD);
159         newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ISIG | IEXTEN );
160         newtio.c_oflag &= ~(OPOST);
161         newtio.c_iflag = IGNBRK;
162
163         /* setup baudrate */
164         switch (baud_rate)
165         {
166 /*Some values are not defined on OSX and *BSD */
167 #if defined(B921600)
168         case 921600:
169             baud_rate = B921600;
170             break;
171 #endif
172 #if defined(B460800)
173         case 460800:
174             baud_rate = B460800;
175             break;
176 #endif
177         case 230400: 
178             baud_rate = B230400;
179             break;
180         case 115200: 
181             baud_rate = B115200;
182             break;
183         case 57600:
184             baud_rate = B57600;
185             break;
186         case 38400: 
187             baud_rate = B38400;
188             break;
189         case 19200: 
190             baud_rate = B19200;
191             break;
192         case 9600: 
193                 baud_rate = B9600;
194                 break;
195         case 4800: 
196             baud_rate = B4800;
197                 break;
198         case 2400: 
199             baud_rate = B2400;
200                 break;
201         case 1800: 
202             baud_rate = B1800;
203                 break;
204         case 1200: 
205             baud_rate = B1200;
206                 break;
207         case 600: 
208             baud_rate = B600;
209             break;
210         case 300: 
211             baud_rate = B300;
212             break;
213         case 200: 
214             baud_rate = B200;
215             break;
216         case 150: 
217             baud_rate = B150;
218             break;
219         case 134: 
220             baud_rate = B134;
221             break;
222         case 110: 
223             baud_rate = B110;
224             break;
225         case 75: 
226             baud_rate = B75;
227             break;
228         case 50:
229         case 0:
230         default:
231             baud_rate = B9600;
232                 break;
233         }
234
235         /* char lenght */
236         newtio.c_cflag &= ~CSIZE;
237         switch (dataBits)
238         {
239         case 5: 
240             newtio.c_cflag |= CS5;
241             break;
242         case 6: 
243             newtio.c_cflag |= CS6;
244             break;
245         case 7: 
246             newtio.c_cflag |= CS7;
247             break;
248         case 8:
249         default:
250                 newtio.c_cflag |= CS8;
251                 break;
252         }
253
254         /* stopbits */
255         switch (stopBits)
256         {
257         case NoneStopBits:
258                 /* Unhandled */
259                 break;
260         case One: /* One */
261                 /* do nothing, the default is one stop bit */
262             newtio.c_cflag &= ~CSTOPB;
263                 break;
264         case Two: /* Two */
265                 newtio.c_cflag |= CSTOPB;
266                 break;
267         case OnePointFive: /* OnePointFive */
268                 /* XXX unhandled */
269                 break;
270         }
271
272         /* parity */
273         newtio.c_iflag &= ~(INPCK | ISTRIP );
274
275         switch (parity)
276         {
277         case NoneParity: /* None */
278             newtio.c_cflag &= ~(PARENB | PARODD);
279             break;
280             
281         case Odd: /* Odd */
282             newtio.c_cflag |= PARENB | PARODD;
283             break;
284             
285         case Even: /* Even */
286             newtio.c_cflag &= ~(PARODD);
287             newtio.c_cflag |= (PARENB);
288             break;
289             
290         case Mark: /* Mark */
291             /* XXX unhandled */
292             break;
293         case Space: /* Space */
294             /* XXX unhandled */
295             break;
296         }
297
298         newtio.c_iflag &= ~(IXOFF | IXON);
299 #ifdef CRTSCTS
300         newtio.c_cflag &= ~CRTSCTS;
301 #endif /* def CRTSCTS */
302
303         switch (handshake)
304         {
305         case NoneHandshake: /* None */
306                 /* do nothing */
307                 break;
308         case RequestToSend: /* RequestToSend (RTS) */
309 #ifdef CRTSCTS
310                 newtio.c_cflag |= CRTSCTS;
311 #endif /* def CRTSCTS */
312                 break;
313         case RequestToSendXOnXOff: /* RequestToSendXOnXOff (RTS + XON/XOFF) */
314 #ifdef CRTSCTS
315                 newtio.c_cflag |= CRTSCTS;
316 #endif /* def CRTSCTS */
317                 /* fall through */
318         case XOnXOff: /* XOnXOff */
319             newtio.c_iflag |= IXOFF | IXON;
320                 break;
321         }
322         
323         if (cfsetospeed (&newtio, baud_rate) < 0 || cfsetispeed (&newtio, baud_rate) < 0 ||
324             tcsetattr (fd, TCSANOW, &newtio) < 0)
325         {
326                 return FALSE;
327         }
328         else
329         {
330         return TRUE;
331         }
332 }
333
334
335 static gint32
336 get_signal_code (MonoSerialSignal signal)
337 {
338         switch (signal) {
339                 case Cd:
340                         return TIOCM_CAR;
341                 case Cts:
342                         return TIOCM_CTS;
343                 case Dsr:
344                         return TIOCM_DSR;
345                 case Dtr:
346                         return TIOCM_DTR;
347                 case Rts:
348                         return TIOCM_RTS;
349                 default:
350                         return 0;
351         }
352
353         /* Not reached */
354         return 0;
355 }
356
357 static MonoSerialSignal
358 get_mono_signal_codes (int signals)
359 {
360         MonoSerialSignal retval = NoneSignal;
361
362         if ((signals & TIOCM_CAR) != 0)
363                 retval |= Cd;
364         if ((signals & TIOCM_CTS) != 0)
365                 retval |= Cts;
366         if ((signals & TIOCM_DSR) != 0)
367                 retval |= Dsr;
368         if ((signals & TIOCM_DTR) != 0)
369                 retval |= Dtr;
370         if ((signals & TIOCM_RTS) != 0)
371                 retval |= Rts;
372
373         return retval;
374 }
375
376 MonoSerialSignal
377 get_signals (int fd, gint32 *error)
378 {
379         int signals;
380
381         *error = 0;
382         
383         if (ioctl (fd, TIOCMGET, &signals) == -1) {
384                 *error = -1;
385                 return NoneSignal;
386         }
387         
388         return get_mono_signal_codes (signals);
389 }
390
391 gint32
392 set_signal (int fd, MonoSerialSignal signal, gboolean value)
393 {
394         int signals, expected, activated;
395
396         expected = get_signal_code (signal);
397         if (ioctl (fd, TIOCMGET, &signals) == -1)
398                 return -1;
399         
400         activated = (signals & expected) != 0;
401         if (activated == value) /* Already set */
402                 return 1;
403         
404         if (value)
405                 signals |= expected;
406         else
407                 signals &= ~expected;
408         
409         if (ioctl (fd, TIOCMSET, &signals) == -1)
410                 return -1;
411         
412         return 1;
413 }
414
415 int
416 breakprop (int fd)
417 {
418         return tcsendbreak (fd, 0);
419 }
420
421 gboolean
422 poll_serial (int fd, gint32 *error, int timeout)
423 {
424         struct pollfd pinfo;
425         
426         *error = 0;
427         
428         pinfo.fd = fd;
429         pinfo.events = POLLIN;
430         pinfo.revents = 0;
431
432         while (poll (&pinfo, 1, timeout) == -1 && errno == EINTR) {
433                 /* EINTR is an OK condition, we should not throw in the upper layer an IOException */
434                 if (errno != EINTR){
435                         *error = -1;
436                         return FALSE;
437                 }
438         }
439
440         return (pinfo.revents & POLLIN) != 0 ? 1 : 0;
441 }
442
443 /*
444  * mono internals should not be used here.
445  * this serial stuff needs to be implemented with icalls.
446  * make this at least compile until the code is moved elsewhere
447  * defined(linux) is wrong, too
448  */
449 void*
450 list_serial_devices (void)
451 {
452         return NULL;
453 }
454
455 #if 0
456 MonoArray *
457 list_serial_devices (void)
458 {
459         MonoArray *array;
460 #if defined(linux)
461         /* Linux serial files are of the form ttyS[0-9]+ */
462         GSList *l, *list = NULL;
463         GDir* dir = g_dir_open ("/dev", 0, NULL);
464         const char *filename;
465         int i = 0;
466
467         while ((filename = g_dir_read_name (dir))) {
468                 if (filename) {
469                         if (!strncmp (filename, "ttyS", 4))
470                                 list = g_slist_append (list, g_strconcat ("/dev/", filename, NULL));
471                 }
472         }
473
474         g_dir_close (dir);
475   
476         array = mono_array_new (mono_domain_get (), mono_get_string_class (), g_slist_length (list));
477         for (l = list; l; l = l->next) {
478                 mono_array_set (array, gpointer, i++, mono_string_new (mono_domain_get (), (char*)l->data));
479                 g_free (l->data);
480         }
481
482         g_slist_free (list);
483
484 #else
485 #warning "list_serial_devices isn't ported to this OS"
486 #endif
487
488         return array;
489 }
490 #endif
491