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