2006-06-23 John Luke <john.luke@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
15 #include <glib.h>
16
17 /* This is for FIONREAD on solaris */
18 #if defined(sun)
19 #include <sys/filio.h>
20 #endif
21
22 /* This is a copy of System.IO.Ports.Handshake */
23 typedef enum {
24         NoneHandshake = 0,
25         XOnXOff = 1,
26         RequestToSend = 2,
27         RequestToSendXOnXOff = 3
28 } MonoHandshake;
29
30 /* This is a copy of System.IO.Ports.Parity */
31 typedef enum {
32         NoneParity = 0,
33         Odd = 1,
34         Even = 2,
35         Mark = 3,
36         Space = 4
37 } MonoParity;
38
39 /* This is a copy of System.IO.Ports.StopBits */
40 typedef enum {
41         NoneStopBits = 0,
42         One = 1,
43         Two = 2,
44         OnePointFive = 3
45 } MonoStopBits;
46
47 /* This is a copy of System.IO.Ports.SerialSignal */
48 typedef enum {
49         NoneSignal,
50         Cd = 1, /* Carrier detect */
51         Cts = 2, /* Clear to send */
52         Dsr = 4, /* Data set ready */
53         Dtr = 8, /* Data terminal ready */
54         Rts = 16  /* Request to send */
55 } MonoSerialSignal;
56
57 int
58 open_serial (char* devfile)
59 {
60         int fd;
61         struct termios newtio;
62
63         fd = open (devfile, O_RDWR);
64
65         if (fd == -1)
66                 return -1;
67
68         newtio.c_cflag = CLOCAL | CREAD;
69         newtio.c_iflag = 0;
70         newtio.c_oflag = 0;
71         newtio.c_lflag = 0;
72
73         tcflush(fd, TCIOFLUSH);
74         tcsetattr(fd,TCSANOW,&newtio);
75
76         return fd;
77 }
78
79 void
80 close_serial (int unix_fd)
81 {
82         close (unix_fd);
83 }
84
85 guint32
86 read_serial (int fd, guchar *buffer, int offset, int count)
87 {
88         guint32 n;
89  
90         n = read (fd, buffer + offset, count);
91
92         return (guint32) n;
93 }
94
95 void
96 write_serial (int fd, guchar *buffer, int offset, int count, int timeout)
97 {
98         guint32 n;
99
100         struct pollfd ufd;
101
102         ufd.fd = fd;
103         ufd.events = POLLHUP | POLLOUT | POLLERR;
104
105         poll (&ufd, 1, timeout);
106
107         if ((ufd.revents & POLLOUT) != POLLOUT) {
108                 return;
109         }
110  
111         n = write (fd, buffer + offset, count);
112 }
113
114 void
115 discard_buffer (int fd, gboolean input)
116 {
117         tcflush(fd, input ? TCIFLUSH : TCOFLUSH);
118 }
119
120 gint32
121 get_bytes_in_buffer (int fd, gboolean input, gint32 *error)
122 {
123         gint32 retval;
124
125         *error = 0;
126         if (ioctl (fd, input ? FIONREAD : TIOCOUTQ, &retval) == -1) {
127                 *error = -1;
128                 return -1;
129         }
130
131         return retval;
132 }
133
134 gboolean
135 set_attributes (int fd, int baud_rate, MonoParity parity, int dataBits, MonoStopBits stopBits, MonoHandshake handshake)
136 {
137         struct termios newtio;
138
139         tcgetattr (fd, &newtio);
140
141         switch (baud_rate) {
142         case 230400: baud_rate = B230400; break;
143         case 115200: baud_rate = B115200; break;
144         case 57600: baud_rate = B57600; break;
145         case 38400: baud_rate = B38400; break;
146         case 19200: baud_rate = B19200; break;
147         case 9600: baud_rate = B9600; break;
148         case 4800: baud_rate = B4800; break;
149         case 2400: baud_rate = B2400; break;
150         case 1800: baud_rate = B1800; break;
151         case 1200: baud_rate = B1200; break;
152         case 600: baud_rate = B600; break;
153         case 300: baud_rate = B300; break;
154         case 200: baud_rate = B200; break;
155         case 150: baud_rate = B150; break;
156         case 134: baud_rate = B134; break;
157         case 110: baud_rate = B110; break;
158         case 75: baud_rate = B75; break;
159         case 50:
160         case 0:
161         default:
162                 baud_rate = B9600;
163                 break;
164         }
165
166         switch (parity) {
167         case NoneParity: /* None */
168                 newtio.c_iflag |= IGNPAR;
169                 newtio.c_cflag &= ~(PARENB | PARODD);
170                 break;
171         case Odd: /* Odd */
172                 newtio.c_iflag &= ~IGNPAR;
173                 newtio.c_cflag |= PARENB | PARODD;
174                 break;
175         case Even: /* Even */
176                 newtio.c_iflag &= ~(IGNPAR | PARODD);
177                 newtio.c_cflag |= PARENB;
178                 break;
179         case Mark: /* Mark */
180                 /* XXX unhandled */
181                 break;
182         case Space: /* Space */
183                 /* XXX unhandled */
184                 break;
185         }
186
187         newtio.c_cflag &= ~CSIZE;
188         switch (dataBits) {
189         case 5: newtio.c_cflag |= CS5; break;
190         case 6: newtio.c_cflag |= CS6; break;
191         case 7: newtio.c_cflag |= CS7; break;
192         case 8:
193         default:
194                 newtio.c_cflag |= CS8;
195                 break;
196         }
197
198         newtio.c_cflag &= ~CSTOPB;
199         switch (stopBits) {
200         case NoneStopBits:
201                 /* Unhandled */
202                 break;
203         case One: /* One */
204                 /* do nothing, the default is one stop bit */
205                 break;
206         case Two: /* Two */
207                 newtio.c_cflag |= CSTOPB;
208                 break;
209         case OnePointFive: /* OnePointFive */
210                 /* XXX unhandled */
211                 break;
212         }
213
214         newtio.c_iflag &= ~IXOFF;
215         newtio.c_oflag &= ~IXON;
216 #ifdef CRTSCTS
217         newtio.c_cflag &= ~CRTSCTS;
218 #endif /* def CRTSCTS */
219         switch (handshake) {
220         case NoneHandshake: /* None */
221                 /* do nothing */
222                 break;
223         case RequestToSend: /* RequestToSend (RTS) */
224 #ifdef CRTSCTS
225                 newtio.c_cflag |= CRTSCTS;
226 #endif /* def CRTSCTS */
227                 break;
228         case RequestToSendXOnXOff: /* RequestToSendXOnXOff (RTS + XON/XOFF) */
229 #ifdef CRTSCTS
230                 newtio.c_cflag |= CRTSCTS;
231 #endif /* def CRTSCTS */
232                 /* fall through */
233         case XOnXOff: /* XOnXOff */
234                 newtio.c_iflag |= IXOFF;
235                 //              newtio.c_oflag |= IXON;
236                 break;
237         }
238         
239         if (cfsetospeed (&newtio, baud_rate) < 0 || cfsetispeed (&newtio, baud_rate) < 0 ||
240                         tcsetattr (fd, TCSADRAIN, &newtio) < 0)
241                 return FALSE;
242
243         return TRUE;
244 }
245
246 static gint32
247 get_signal_code (MonoSerialSignal signal)
248 {
249         switch (signal) {
250                 case Cd:
251                         return TIOCM_CAR;
252                 case Cts:
253                         return TIOCM_CTS;
254                 case Dsr:
255                         return TIOCM_DSR;
256                 case Dtr:
257                         return TIOCM_DTR;
258                 case Rts:
259                         return TIOCM_RTS;
260                 default:
261                         return 0;
262         }
263
264         /* Not reached */
265         return 0;
266 }
267
268 static MonoSerialSignal
269 get_mono_signal_codes (int signals)
270 {
271         MonoSerialSignal retval = NoneSignal;
272
273         if ((signals & TIOCM_CAR) != 0)
274                 retval |= Cd;
275         if ((signals & TIOCM_CTS) != 0)
276                 retval |= Cts;
277         if ((signals & TIOCM_DSR) != 0)
278                 retval |= Dsr;
279         if ((signals & TIOCM_DTR) != 0)
280                 retval |= Dtr;
281         if ((signals & TIOCM_RTS) != 0)
282                 retval |= Rts;
283
284         return retval;
285 }
286
287 MonoSerialSignal
288 get_signals (int fd, gint32 *error)
289 {
290         int signals;
291
292         *error = 0;
293         
294         if (ioctl (fd, TIOCMGET, &signals) == -1) {
295                 *error = -1;
296                 return NoneSignal;
297         }
298         
299         return get_mono_signal_codes (signals);
300 }
301
302 gint32
303 set_signal (int fd, MonoSerialSignal signal, gboolean value)
304 {
305         int signals, expected, activated;
306
307         expected = get_signal_code (signal);
308         if (ioctl (fd, TIOCMGET, &signals) == -1)
309                 return -1;
310         
311         activated = (signals & expected) != 0;
312         if (activated == value) /* Already set */
313                 return 1;
314         
315         if (value)
316                 signals |= expected;
317         else
318                 signals &= ~expected;
319         
320         if (ioctl (fd, TIOCMSET, &signals) == -1)
321                 return -1;
322         
323         return 1;
324 }
325
326 gboolean
327 poll_serial (int fd, gint32 *error)
328 {
329         struct pollfd pinfo;
330         
331         *error = 0;
332         
333         pinfo.fd = fd;
334         pinfo.events = POLLIN;
335         pinfo.revents = 0;
336
337         if (poll (&pinfo, 1, 0) == -1) {
338                 *error = -1;
339                 return FALSE;
340         }
341
342         return (pinfo.revents & POLLIN) != 0 ? 1 : 0;
343 }
344
345 /*
346  * mono internals should not be used here.
347  * this serial stuff needs to be implemented with icalls.
348  * make this at least compile until the code is moved elsewhere
349  * defined(linux) is wrong, too
350  */
351 void*
352 list_serial_devices (void)
353 {
354         return NULL;
355 }
356
357 #if 0
358 MonoArray *
359 list_serial_devices (void)
360 {
361         MonoArray *array;
362 #if defined(linux)
363         /* Linux serial files are of the form ttyS[0-9]+ */
364         GSList *l, *list = NULL;
365         GDir* dir = g_dir_open ("/dev", 0, NULL);
366         const char *filename;
367         int i = 0;
368
369         while ((filename = g_dir_read_name (dir))) {
370                 if (filename) {
371                         if (!strncmp (filename, "ttyS", 4))
372                                 list = g_slist_append (list, g_strconcat ("/dev/", filename, NULL));
373                 }
374         }
375
376         g_dir_close (dir);
377   
378         array = mono_array_new (mono_domain_get (), mono_get_string_class (), g_slist_length (list));
379         for (l = list; l; l = l->next) {
380                 mono_array_set (array, gpointer, i++, mono_string_new (mono_domain_get (), (char*)l->data));
381                 g_free (l->data);
382         }
383
384         g_slist_free (list);
385
386 #else
387 #warning "list_serial_devices isn't ported to this OS"
388 #endif
389
390         return array;
391 }
392 #endif
393