2010-01-22 Leszek Ciesielski <skolima@gmail.com>
[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         case 460800:
163             baud_rate = B460800;
164             break;
165         case 230400: 
166             baud_rate = B230400;
167             break;
168         case 115200: 
169             baud_rate = B115200;
170             break;
171         case 57600:
172             baud_rate = B57600;
173             break;
174         case 38400: 
175             baud_rate = B38400;
176             break;
177         case 19200: 
178             baud_rate = B19200;
179             break;
180         case 9600: 
181                 baud_rate = B9600;
182                 break;
183         case 4800: 
184             baud_rate = B4800;
185                 break;
186         case 2400: 
187             baud_rate = B2400;
188                 break;
189         case 1800: 
190             baud_rate = B1800;
191                 break;
192         case 1200: 
193             baud_rate = B1200;
194                 break;
195         case 600: 
196             baud_rate = B600;
197             break;
198         case 300: 
199             baud_rate = B300;
200             break;
201         case 200: 
202             baud_rate = B200;
203             break;
204         case 150: 
205             baud_rate = B150;
206             break;
207         case 134: 
208             baud_rate = B134;
209             break;
210         case 110: 
211             baud_rate = B110;
212             break;
213         case 75: 
214             baud_rate = B75;
215             break;
216         case 50:
217         case 0:
218         default:
219             baud_rate = B9600;
220                 break;
221         }
222
223         /* char lenght */
224         newtio.c_cflag &= ~CSIZE;
225         switch (dataBits)
226         {
227         case 5: 
228             newtio.c_cflag |= CS5;
229             break;
230         case 6: 
231             newtio.c_cflag |= CS6;
232             break;
233         case 7: 
234             newtio.c_cflag |= CS7;
235             break;
236         case 8:
237         default:
238                 newtio.c_cflag |= CS8;
239                 break;
240         }
241
242         /* stopbits */
243         switch (stopBits)
244         {
245         case NoneStopBits:
246                 /* Unhandled */
247                 break;
248         case One: /* One */
249                 /* do nothing, the default is one stop bit */
250             newtio.c_cflag &= ~CSTOPB;
251                 break;
252         case Two: /* Two */
253                 newtio.c_cflag |= CSTOPB;
254                 break;
255         case OnePointFive: /* OnePointFive */
256                 /* XXX unhandled */
257                 break;
258         }
259
260         /* parity */
261         newtio.c_iflag &= ~(INPCK | ISTRIP );
262
263         switch (parity)
264         {
265         case NoneParity: /* None */
266             newtio.c_cflag &= ~(PARENB | PARODD);
267             break;
268             
269         case Odd: /* Odd */
270             newtio.c_cflag |= PARENB | PARODD;
271             break;
272             
273         case Even: /* Even */
274             newtio.c_cflag &= ~(PARODD);
275             newtio.c_cflag |= (PARENB);
276             break;
277             
278         case Mark: /* Mark */
279             /* XXX unhandled */
280             break;
281         case Space: /* Space */
282             /* XXX unhandled */
283             break;
284         }
285
286         newtio.c_iflag &= ~(IXOFF | IXON);
287 #ifdef CRTSCTS
288         newtio.c_cflag &= ~CRTSCTS;
289 #endif /* def CRTSCTS */
290
291         switch (handshake)
292         {
293         case NoneHandshake: /* None */
294                 /* do nothing */
295                 break;
296         case RequestToSend: /* RequestToSend (RTS) */
297 #ifdef CRTSCTS
298                 newtio.c_cflag |= CRTSCTS;
299 #endif /* def CRTSCTS */
300                 break;
301         case RequestToSendXOnXOff: /* RequestToSendXOnXOff (RTS + XON/XOFF) */
302 #ifdef CRTSCTS
303                 newtio.c_cflag |= CRTSCTS;
304 #endif /* def CRTSCTS */
305                 /* fall through */
306         case XOnXOff: /* XOnXOff */
307             newtio.c_iflag |= IXOFF | IXON;
308                 break;
309         }
310         
311         if (cfsetospeed (&newtio, baud_rate) < 0 || cfsetispeed (&newtio, baud_rate) < 0 ||
312             tcsetattr (fd, TCSANOW, &newtio) < 0)
313         {
314                 return FALSE;
315         }
316         else
317         {
318         return TRUE;
319         }
320 }
321
322
323 static gint32
324 get_signal_code (MonoSerialSignal signal)
325 {
326         switch (signal) {
327                 case Cd:
328                         return TIOCM_CAR;
329                 case Cts:
330                         return TIOCM_CTS;
331                 case Dsr:
332                         return TIOCM_DSR;
333                 case Dtr:
334                         return TIOCM_DTR;
335                 case Rts:
336                         return TIOCM_RTS;
337                 default:
338                         return 0;
339         }
340
341         /* Not reached */
342         return 0;
343 }
344
345 static MonoSerialSignal
346 get_mono_signal_codes (int signals)
347 {
348         MonoSerialSignal retval = NoneSignal;
349
350         if ((signals & TIOCM_CAR) != 0)
351                 retval |= Cd;
352         if ((signals & TIOCM_CTS) != 0)
353                 retval |= Cts;
354         if ((signals & TIOCM_DSR) != 0)
355                 retval |= Dsr;
356         if ((signals & TIOCM_DTR) != 0)
357                 retval |= Dtr;
358         if ((signals & TIOCM_RTS) != 0)
359                 retval |= Rts;
360
361         return retval;
362 }
363
364 MonoSerialSignal
365 get_signals (int fd, gint32 *error)
366 {
367         int signals;
368
369         *error = 0;
370         
371         if (ioctl (fd, TIOCMGET, &signals) == -1) {
372                 *error = -1;
373                 return NoneSignal;
374         }
375         
376         return get_mono_signal_codes (signals);
377 }
378
379 gint32
380 set_signal (int fd, MonoSerialSignal signal, gboolean value)
381 {
382         int signals, expected, activated;
383
384         expected = get_signal_code (signal);
385         if (ioctl (fd, TIOCMGET, &signals) == -1)
386                 return -1;
387         
388         activated = (signals & expected) != 0;
389         if (activated == value) /* Already set */
390                 return 1;
391         
392         if (value)
393                 signals |= expected;
394         else
395                 signals &= ~expected;
396         
397         if (ioctl (fd, TIOCMSET, &signals) == -1)
398                 return -1;
399         
400         return 1;
401 }
402
403 int
404 breakprop (int fd)
405 {
406         return tcsendbreak (fd, 0);
407 }
408
409 gboolean
410 poll_serial (int fd, gint32 *error, int timeout)
411 {
412         struct pollfd pinfo;
413         
414         *error = 0;
415         
416         pinfo.fd = fd;
417         pinfo.events = POLLIN;
418         pinfo.revents = 0;
419
420         while (poll (&pinfo, 1, timeout) == -1 && errno == EINTR) {
421                 /* EINTR is an OK condition, we should not throw in the upper layer an IOException */
422                 if (errno != EINTR){
423                         *error = -1;
424                         return FALSE;
425                 }
426         }
427
428         return (pinfo.revents & POLLIN) != 0 ? 1 : 0;
429 }
430
431 /*
432  * mono internals should not be used here.
433  * this serial stuff needs to be implemented with icalls.
434  * make this at least compile until the code is moved elsewhere
435  * defined(linux) is wrong, too
436  */
437 void*
438 list_serial_devices (void)
439 {
440         return NULL;
441 }
442
443 #if 0
444 MonoArray *
445 list_serial_devices (void)
446 {
447         MonoArray *array;
448 #if defined(linux)
449         /* Linux serial files are of the form ttyS[0-9]+ */
450         GSList *l, *list = NULL;
451         GDir* dir = g_dir_open ("/dev", 0, NULL);
452         const char *filename;
453         int i = 0;
454
455         while ((filename = g_dir_read_name (dir))) {
456                 if (filename) {
457                         if (!strncmp (filename, "ttyS", 4))
458                                 list = g_slist_append (list, g_strconcat ("/dev/", filename, NULL));
459                 }
460         }
461
462         g_dir_close (dir);
463   
464         array = mono_array_new (mono_domain_get (), mono_get_string_class (), g_slist_length (list));
465         for (l = list; l; l = l->next) {
466                 mono_array_set (array, gpointer, i++, mono_string_new (mono_domain_get (), (char*)l->data));
467                 g_free (l->data);
468         }
469
470         g_slist_free (list);
471
472 #else
473 #warning "list_serial_devices isn't ported to this OS"
474 #endif
475
476         return array;
477 }
478 #endif
479