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