e73e6f65591f3738f28e75ea035aee6b6212c1bb
[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
14 #include <glib.h>
15
16 #include "serial.h"
17
18 int
19 open_serial (char* devfile)
20 {
21         int fd;
22         struct termios newtio;
23
24         fd = open (devfile, O_RDWR);
25
26         if (fd == -1)
27                 return -1;
28
29         newtio.c_cflag = CLOCAL | CREAD;
30         newtio.c_iflag = 0;
31         newtio.c_oflag = 0;
32         newtio.c_lflag = 0;
33
34         tcflush(fd, TCIOFLUSH);
35         tcsetattr(fd,TCSANOW,&newtio);
36
37         fcntl (fd, F_SETFL, O_NONBLOCK);
38
39         return fd;
40 }
41
42 void
43 close_serial (int unix_fd)
44 {
45         close (unix_fd);
46 }
47
48 guint32
49 read_serial (int fd, guchar *buffer, int offset, int count, int timeout)
50 {
51         guint32 n;
52         struct pollfd ufd;
53
54         ufd.fd = fd;
55         ufd.events = POLLHUP | POLLIN | POLLERR;
56
57         poll (&ufd, 1, timeout);
58
59         if ((ufd.revents & POLLIN) != POLLIN) {
60                 return -1;
61         }
62  
63         n = read (fd, buffer + offset, count);
64
65         return (guint32) n;
66 }
67
68 void
69 write_serial (int fd, guchar *buffer, int offset, int count, int timeout)
70 {
71         guint32 n;
72
73         struct pollfd ufd;
74
75         ufd.fd = fd;
76         ufd.events = POLLHUP | POLLOUT | POLLERR;
77
78         poll (&ufd, 1, timeout);
79
80         if ((ufd.revents & POLLOUT) != POLLOUT) {
81                 return;
82         }
83  
84         n = write (fd, buffer + offset, count);
85 }
86
87 void
88 discard_buffer (int fd, gboolean input)
89 {
90         tcflush(fd, input ? TCIFLUSH : TCOFLUSH);
91 }
92
93 gboolean
94 set_attributes (int fd, int baud_rate, MonoParity parity, int dataBits, MonoStopBits stopBits, MonoHandshake handshake)
95 {
96         struct termios newtio;
97
98         tcgetattr (fd, &newtio);
99
100         switch (baud_rate) {
101         case 230400: baud_rate = B230400; break;
102         case 115200: baud_rate = B115200; break;
103         case 57600: baud_rate = B57600; break;
104         case 38400: baud_rate = B38400; break;
105         case 19200: baud_rate = B19200; break;
106         case 9600: baud_rate = B9600; break;
107         case 4800: baud_rate = B4800; break;
108         case 2400: baud_rate = B2400; break;
109         case 1800: baud_rate = B1800; break;
110         case 1200: baud_rate = B1200; break;
111         case 600: baud_rate = B600; break;
112         case 300: baud_rate = B300; break;
113         case 200: baud_rate = B200; break;
114         case 150: baud_rate = B150; break;
115         case 134: baud_rate = B134; break;
116         case 110: baud_rate = B110; break;
117         case 75: baud_rate = B75; break;
118         case 50:
119         case 0:
120         default:
121                 baud_rate = B9600;
122                 break;
123         }
124
125         switch (parity) {
126         case NoneParity: /* None */
127                 newtio.c_iflag |= IGNPAR;
128                 newtio.c_cflag &= ~(PARENB | PARODD);
129                 break;
130         case Odd: /* Odd */
131                 newtio.c_iflag &= ~IGNPAR;
132                 newtio.c_cflag |= PARENB | PARODD;
133                 break;
134         case Even: /* Even */
135                 newtio.c_iflag &= ~IGNPAR;
136                 newtio.c_cflag |= PARENB;
137                 break;
138         case Mark: /* Mark */
139                 /* XXX unhandled */
140                 break;
141         case Space: /* Space */
142                 /* XXX unhandled */
143                 break;
144         }
145
146         newtio.c_cflag &= ~CSIZE;
147         switch (dataBits) {
148         case 5: newtio.c_cflag |= CS5; break;
149         case 6: newtio.c_cflag |= CS6; break;
150         case 7: newtio.c_cflag |= CS7; break;
151         case 8:
152         default:
153                 newtio.c_cflag |= CS8;
154                 break;
155         }
156
157         newtio.c_cflag &= ~CSTOPB;
158         switch (stopBits) {
159         case NoneStopBits:
160                 /* Unhandled */
161                 break;
162         case One: /* One */
163                 /* do nothing, the default is one stop bit */
164                 break;
165         case Two: /* Two */
166                 newtio.c_cflag |= CSTOPB;
167                 break;
168         case OnePointFive: /* OnePointFive */
169                 /* XXX unhandled */
170                 break;
171         }
172
173         newtio.c_iflag &= ~IXOFF;
174         newtio.c_oflag &= ~IXON;
175 #ifdef CRTSCTS
176         newtio.c_cflag &= ~CRTSCTS;
177 #endif /* def CRTSCTS */
178         switch (handshake) {
179         case NoneHandshake: /* None */
180                 /* do nothing */
181                 break;
182         case XOnXOff: /* XOnXOff */
183                 newtio.c_iflag |= IXOFF;
184                 //              newtio.c_oflag |= IXON;
185                 break;
186         case RequestToSend: /* RequestToSend (RTS) */
187 #ifdef CRTSCTS
188                 newtio.c_cflag |= CRTSCTS;
189 #endif /* def CRTSCTS */
190                 break;
191         case RequestToSendXOnXOff: /* RequestToSendXOnXOff (RTS + XON/XOFF) */
192 #ifdef CRTSCTS
193                 newtio.c_cflag |= CRTSCTS;
194 #endif /* def CRTSCTS */
195                 /* fall through */
196         }
197         
198         if (cfsetospeed (&newtio, baud_rate) < 0 || cfsetispeed (&newtio, baud_rate) < 0 ||
199                         tcsetattr (fd, TCSADRAIN, &newtio) < 0)
200                 return FALSE;
201
202         return TRUE;
203 }
204
205 /*
206  * mono internals should not be used here.
207  * this serial stuff needs to be implemented with icalls.
208  * make this at least compile until the code is moved elsewhere
209  * defined(linux) is wrong, too
210  */
211 void*
212 list_serial_devices (void)
213 {
214         return NULL;
215 }
216
217 #if 0
218 MonoArray *
219 list_serial_devices (void)
220 {
221         MonoArray *array;
222 #if defined(linux)
223         /* Linux serial files are of the form ttyS[0-9]+ */
224         GSList *l, *list = NULL;
225         GDir* dir = g_dir_open ("/dev", 0, NULL);
226         const char *filename;
227         int i = 0;
228
229         while ((filename = g_dir_read_name (dir))) {
230                 if (filename) {
231                         if (!strncmp (filename, "ttyS", 4))
232                                 list = g_slist_append (list, g_strconcat ("/dev/", filename, NULL));
233                 }
234         }
235
236         g_dir_close (dir);
237   
238         array = mono_array_new (mono_domain_get (), mono_get_string_class (), g_slist_length (list));
239         for (l = list; l; l = l->next) {
240                 mono_array_set (array, gpointer, i++, mono_string_new (mono_domain_get (), (char*)l->data));
241                 g_free (l->data);
242         }
243
244         g_slist_free (list);
245
246 #else
247 #warning "list_serial_devices isn't ported to this OS"
248 #endif
249
250         return array;
251 }
252 #endif
253