add some (mostly non-working) data binding tests
[mono-project.git] / support / serial.c
blobf7746c41a866419171a091c593fd838600a71ca7
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 /* serial port functions
5 * Author: Chris Toshok <toshok@ximian.com>
6 */
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>
16 #include <glib.h>
18 /* This is for FIONREAD on solaris */
19 #if defined(sun)
20 #include <sys/filio.h>
21 #endif
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;
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;
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;
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;
58 int
59 open_serial (char* devfile)
61 int fd;
62 struct termios newtio;
64 fd = open (devfile, O_RDWR);
66 if (fd == -1)
67 return -1;
69 newtio.c_cflag = CLOCAL | CREAD;
70 newtio.c_iflag = 0;
71 newtio.c_oflag = 0;
72 newtio.c_lflag = 0;
74 tcflush(fd, TCIOFLUSH);
75 tcsetattr(fd,TCSANOW,&newtio);
77 return fd;
80 void
81 close_serial (int unix_fd)
83 close (unix_fd);
86 guint32
87 read_serial (int fd, guchar *buffer, int offset, int count)
89 guint32 n;
91 n = read (fd, buffer + offset, count);
93 return (guint32) n;
96 void
97 write_serial (int fd, guchar *buffer, int offset, int count, int timeout)
99 guint32 n;
101 struct pollfd ufd;
103 ufd.fd = fd;
104 ufd.events = POLLHUP | POLLOUT | POLLERR;
106 while (poll (&ufd, 1, timeout) == -1 && errno == EINTR){
110 if ((ufd.revents & POLLOUT) != POLLOUT) {
111 return;
114 n = write (fd, buffer + offset, count);
117 void
118 discard_buffer (int fd, gboolean input)
120 tcflush(fd, input ? TCIFLUSH : TCOFLUSH);
123 gint32
124 get_bytes_in_buffer (int fd, gboolean input)
126 gint32 retval;
128 if (ioctl (fd, input ? FIONREAD : TIOCOUTQ, &retval) == -1) {
129 return -1;
132 return retval;
135 gboolean
136 set_attributes (int fd, int baud_rate, MonoParity parity, int dataBits, MonoStopBits stopBits, MonoHandshake handshake)
138 struct termios newtio;
140 tcgetattr (fd, &newtio);
142 switch (baud_rate) {
143 case 230400: baud_rate = B230400; break;
144 case 115200: baud_rate = B115200; break;
145 case 57600: baud_rate = B57600; break;
146 case 38400: baud_rate = B38400; break;
147 case 19200: baud_rate = B19200; break;
148 case 9600: baud_rate = B9600; break;
149 case 4800: baud_rate = B4800; break;
150 case 2400: baud_rate = B2400; break;
151 case 1800: baud_rate = B1800; break;
152 case 1200: baud_rate = B1200; break;
153 case 600: baud_rate = B600; break;
154 case 300: baud_rate = B300; break;
155 case 200: baud_rate = B200; break;
156 case 150: baud_rate = B150; break;
157 case 134: baud_rate = B134; break;
158 case 110: baud_rate = B110; break;
159 case 75: baud_rate = B75; break;
160 case 50:
161 case 0:
162 default:
163 baud_rate = B9600;
164 break;
167 switch (parity) {
168 case NoneParity: /* None */
169 newtio.c_iflag |= IGNPAR;
170 newtio.c_cflag &= ~(PARENB | PARODD);
171 break;
172 case Odd: /* Odd */
173 newtio.c_iflag &= ~IGNPAR;
174 newtio.c_cflag |= PARENB | PARODD;
175 break;
176 case Even: /* Even */
177 newtio.c_iflag &= ~(IGNPAR | PARODD);
178 newtio.c_cflag |= PARENB;
179 break;
180 case Mark: /* Mark */
181 /* XXX unhandled */
182 break;
183 case Space: /* Space */
184 /* XXX unhandled */
185 break;
188 newtio.c_cflag &= ~CSIZE;
189 switch (dataBits) {
190 case 5: newtio.c_cflag |= CS5; break;
191 case 6: newtio.c_cflag |= CS6; break;
192 case 7: newtio.c_cflag |= CS7; break;
193 case 8:
194 default:
195 newtio.c_cflag |= CS8;
196 break;
199 newtio.c_cflag &= ~CSTOPB;
200 switch (stopBits) {
201 case NoneStopBits:
202 /* Unhandled */
203 break;
204 case One: /* One */
205 /* do nothing, the default is one stop bit */
206 break;
207 case Two: /* Two */
208 newtio.c_cflag |= CSTOPB;
209 break;
210 case OnePointFive: /* OnePointFive */
211 /* XXX unhandled */
212 break;
215 newtio.c_iflag &= ~IXOFF;
216 newtio.c_oflag &= ~IXON;
217 #ifdef CRTSCTS
218 newtio.c_cflag &= ~CRTSCTS;
219 #endif /* def CRTSCTS */
220 switch (handshake) {
221 case NoneHandshake: /* None */
222 /* do nothing */
223 break;
224 case RequestToSend: /* RequestToSend (RTS) */
225 #ifdef CRTSCTS
226 newtio.c_cflag |= CRTSCTS;
227 #endif /* def CRTSCTS */
228 break;
229 case RequestToSendXOnXOff: /* RequestToSendXOnXOff (RTS + XON/XOFF) */
230 #ifdef CRTSCTS
231 newtio.c_cflag |= CRTSCTS;
232 #endif /* def CRTSCTS */
233 /* fall through */
234 case XOnXOff: /* XOnXOff */
235 newtio.c_iflag |= IXOFF;
236 // newtio.c_oflag |= IXON;
237 break;
240 if (cfsetospeed (&newtio, baud_rate) < 0 || cfsetispeed (&newtio, baud_rate) < 0 ||
241 tcsetattr (fd, TCSADRAIN, &newtio) < 0)
242 return FALSE;
244 return TRUE;
247 static gint32
248 get_signal_code (MonoSerialSignal signal)
250 switch (signal) {
251 case Cd:
252 return TIOCM_CAR;
253 case Cts:
254 return TIOCM_CTS;
255 case Dsr:
256 return TIOCM_DSR;
257 case Dtr:
258 return TIOCM_DTR;
259 case Rts:
260 return TIOCM_RTS;
261 default:
262 return 0;
265 /* Not reached */
266 return 0;
269 static MonoSerialSignal
270 get_mono_signal_codes (int signals)
272 MonoSerialSignal retval = NoneSignal;
274 if ((signals & TIOCM_CAR) != 0)
275 retval |= Cd;
276 if ((signals & TIOCM_CTS) != 0)
277 retval |= Cts;
278 if ((signals & TIOCM_DSR) != 0)
279 retval |= Dsr;
280 if ((signals & TIOCM_DTR) != 0)
281 retval |= Dtr;
282 if ((signals & TIOCM_RTS) != 0)
283 retval |= Rts;
285 return retval;
288 MonoSerialSignal
289 get_signals (int fd, gint32 *error)
291 int signals;
293 *error = 0;
295 if (ioctl (fd, TIOCMGET, &signals) == -1) {
296 *error = -1;
297 return NoneSignal;
300 return get_mono_signal_codes (signals);
303 gint32
304 set_signal (int fd, MonoSerialSignal signal, gboolean value)
306 int signals, expected, activated;
308 expected = get_signal_code (signal);
309 if (ioctl (fd, TIOCMGET, &signals) == -1)
310 return -1;
312 activated = (signals & expected) != 0;
313 if (activated == value) /* Already set */
314 return 1;
316 if (value)
317 signals |= expected;
318 else
319 signals &= ~expected;
321 if (ioctl (fd, TIOCMSET, &signals) == -1)
322 return -1;
324 return 1;
327 gboolean
328 poll_serial (int fd, gint32 *error, int timeout)
330 struct pollfd pinfo;
332 *error = 0;
334 pinfo.fd = fd;
335 pinfo.events = POLLIN;
336 pinfo.revents = 0;
338 while (poll (&pinfo, 1, timeout) == -1 && errno == EINTR) {
339 /* EINTR is an OK condition, we should not throw in the upper layer an IOException */
340 if (errno != EINTR){
341 *error = -1;
342 return FALSE;
346 return (pinfo.revents & POLLIN) != 0 ? 1 : 0;
350 * mono internals should not be used here.
351 * this serial stuff needs to be implemented with icalls.
352 * make this at least compile until the code is moved elsewhere
353 * defined(linux) is wrong, too
355 void*
356 list_serial_devices (void)
358 return NULL;
361 #if 0
362 MonoArray *
363 list_serial_devices (void)
365 MonoArray *array;
366 #if defined(linux)
367 /* Linux serial files are of the form ttyS[0-9]+ */
368 GSList *l, *list = NULL;
369 GDir* dir = g_dir_open ("/dev", 0, NULL);
370 const char *filename;
371 int i = 0;
373 while ((filename = g_dir_read_name (dir))) {
374 if (filename) {
375 if (!strncmp (filename, "ttyS", 4))
376 list = g_slist_append (list, g_strconcat ("/dev/", filename, NULL));
380 g_dir_close (dir);
382 array = mono_array_new (mono_domain_get (), mono_get_string_class (), g_slist_length (list));
383 for (l = list; l; l = l->next) {
384 mono_array_set (array, gpointer, i++, mono_string_new (mono_domain_get (), (char*)l->data));
385 g_free (l->data);
388 g_slist_free (list);
390 #else
391 #warning "list_serial_devices isn't ported to this OS"
392 #endif
394 return array;
396 #endif