* Makefile: Don't build make-map.exe.
[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)
122 {
123         gint32 retval;
124
125         if (ioctl (fd, input ? FIONREAD : TIOCOUTQ, &retval) == -1) {
126                 return -1;
127         }
128
129         return retval;
130 }
131
132 gboolean
133 set_attributes (int fd, int baud_rate, MonoParity parity, int dataBits, MonoStopBits stopBits, MonoHandshake handshake)
134 {
135         struct termios newtio;
136
137         tcgetattr (fd, &newtio);
138
139         switch (baud_rate) {
140         case 230400: baud_rate = B230400; break;
141         case 115200: baud_rate = B115200; break;
142         case 57600: baud_rate = B57600; break;
143         case 38400: baud_rate = B38400; break;
144         case 19200: baud_rate = B19200; break;
145         case 9600: baud_rate = B9600; break;
146         case 4800: baud_rate = B4800; break;
147         case 2400: baud_rate = B2400; break;
148         case 1800: baud_rate = B1800; break;
149         case 1200: baud_rate = B1200; break;
150         case 600: baud_rate = B600; break;
151         case 300: baud_rate = B300; break;
152         case 200: baud_rate = B200; break;
153         case 150: baud_rate = B150; break;
154         case 134: baud_rate = B134; break;
155         case 110: baud_rate = B110; break;
156         case 75: baud_rate = B75; break;
157         case 50:
158         case 0:
159         default:
160                 baud_rate = B9600;
161                 break;
162         }
163
164         switch (parity) {
165         case NoneParity: /* None */
166                 newtio.c_iflag |= IGNPAR;
167                 newtio.c_cflag &= ~(PARENB | PARODD);
168                 break;
169         case Odd: /* Odd */
170                 newtio.c_iflag &= ~IGNPAR;
171                 newtio.c_cflag |= PARENB | PARODD;
172                 break;
173         case Even: /* Even */
174                 newtio.c_iflag &= ~(IGNPAR | PARODD);
175                 newtio.c_cflag |= PARENB;
176                 break;
177         case Mark: /* Mark */
178                 /* XXX unhandled */
179                 break;
180         case Space: /* Space */
181                 /* XXX unhandled */
182                 break;
183         }
184
185         newtio.c_cflag &= ~CSIZE;
186         switch (dataBits) {
187         case 5: newtio.c_cflag |= CS5; break;
188         case 6: newtio.c_cflag |= CS6; break;
189         case 7: newtio.c_cflag |= CS7; break;
190         case 8:
191         default:
192                 newtio.c_cflag |= CS8;
193                 break;
194         }
195
196         newtio.c_cflag &= ~CSTOPB;
197         switch (stopBits) {
198         case NoneStopBits:
199                 /* Unhandled */
200                 break;
201         case One: /* One */
202                 /* do nothing, the default is one stop bit */
203                 break;
204         case Two: /* Two */
205                 newtio.c_cflag |= CSTOPB;
206                 break;
207         case OnePointFive: /* OnePointFive */
208                 /* XXX unhandled */
209                 break;
210         }
211
212         newtio.c_iflag &= ~IXOFF;
213         newtio.c_oflag &= ~IXON;
214 #ifdef CRTSCTS
215         newtio.c_cflag &= ~CRTSCTS;
216 #endif /* def CRTSCTS */
217         switch (handshake) {
218         case NoneHandshake: /* None */
219                 /* do nothing */
220                 break;
221         case RequestToSend: /* RequestToSend (RTS) */
222 #ifdef CRTSCTS
223                 newtio.c_cflag |= CRTSCTS;
224 #endif /* def CRTSCTS */
225                 break;
226         case RequestToSendXOnXOff: /* RequestToSendXOnXOff (RTS + XON/XOFF) */
227 #ifdef CRTSCTS
228                 newtio.c_cflag |= CRTSCTS;
229 #endif /* def CRTSCTS */
230                 /* fall through */
231         case XOnXOff: /* XOnXOff */
232                 newtio.c_iflag |= IXOFF;
233                 //              newtio.c_oflag |= IXON;
234                 break;
235         }
236         
237         if (cfsetospeed (&newtio, baud_rate) < 0 || cfsetispeed (&newtio, baud_rate) < 0 ||
238                         tcsetattr (fd, TCSADRAIN, &newtio) < 0)
239                 return FALSE;
240
241         return TRUE;
242 }
243
244 static gint32
245 get_signal_code (MonoSerialSignal signal)
246 {
247         switch (signal) {
248                 case Cd:
249                         return TIOCM_CAR;
250                 case Cts:
251                         return TIOCM_CTS;
252                 case Dsr:
253                         return TIOCM_DSR;
254                 case Dtr:
255                         return TIOCM_DTR;
256                 case Rts:
257                         return TIOCM_RTS;
258                 default:
259                         return 0;
260         }
261
262         /* Not reached */
263         return 0;
264 }
265
266 static MonoSerialSignal
267 get_mono_signal_codes (int signals)
268 {
269         MonoSerialSignal retval = NoneSignal;
270
271         if ((signals & TIOCM_CAR) != 0)
272                 retval |= Cd;
273         if ((signals & TIOCM_CTS) != 0)
274                 retval |= Cts;
275         if ((signals & TIOCM_DSR) != 0)
276                 retval |= Dsr;
277         if ((signals & TIOCM_DTR) != 0)
278                 retval |= Dtr;
279         if ((signals & TIOCM_RTS) != 0)
280                 retval |= Rts;
281
282         return retval;
283 }
284
285 MonoSerialSignal
286 get_signals (int fd, gint32 *error)
287 {
288         int signals;
289
290         *error = 0;
291         
292         if (ioctl (fd, TIOCMGET, &signals) == -1) {
293                 *error = -1;
294                 return NoneSignal;
295         }
296         
297         return get_mono_signal_codes (signals);
298 }
299
300 gint32
301 set_signal (int fd, MonoSerialSignal signal, gboolean value)
302 {
303         int signals, expected, activated;
304
305         expected = get_signal_code (signal);
306         if (ioctl (fd, TIOCMGET, &signals) == -1)
307                 return -1;
308         
309         activated = (signals & expected) != 0;
310         if (activated == value) /* Already set */
311                 return 1;
312         
313         if (value)
314                 signals |= expected;
315         else
316                 signals &= ~expected;
317         
318         if (ioctl (fd, TIOCMSET, &signals) == -1)
319                 return -1;
320         
321         return 1;
322 }
323
324 gboolean
325 poll_serial (int fd, gint32 *error)
326 {
327         struct pollfd pinfo;
328         
329         *error = 0;
330         
331         pinfo.fd = fd;
332         pinfo.events = POLLIN;
333         pinfo.revents = 0;
334
335         if (poll (&pinfo, 1, 0) == -1) {
336                 *error = -1;
337                 return FALSE;
338         }
339
340         return (pinfo.revents & POLLIN) != 0 ? 1 : 0;
341 }
342
343 /*
344  * mono internals should not be used here.
345  * this serial stuff needs to be implemented with icalls.
346  * make this at least compile until the code is moved elsewhere
347  * defined(linux) is wrong, too
348  */
349 void*
350 list_serial_devices (void)
351 {
352         return NULL;
353 }
354
355 #if 0
356 MonoArray *
357 list_serial_devices (void)
358 {
359         MonoArray *array;
360 #if defined(linux)
361         /* Linux serial files are of the form ttyS[0-9]+ */
362         GSList *l, *list = NULL;
363         GDir* dir = g_dir_open ("/dev", 0, NULL);
364         const char *filename;
365         int i = 0;
366
367         while ((filename = g_dir_read_name (dir))) {
368                 if (filename) {
369                         if (!strncmp (filename, "ttyS", 4))
370                                 list = g_slist_append (list, g_strconcat ("/dev/", filename, NULL));
371                 }
372         }
373
374         g_dir_close (dir);
375   
376         array = mono_array_new (mono_domain_get (), mono_get_string_class (), g_slist_length (list));
377         for (l = list; l; l = l->next) {
378                 mono_array_set (array, gpointer, i++, mono_string_new (mono_domain_get (), (char*)l->data));
379                 g_free (l->data);
380         }
381
382         g_slist_free (list);
383
384 #else
385 #warning "list_serial_devices isn't ported to this OS"
386 #endif
387
388         return array;
389 }
390 #endif
391