Use wrappers for accessing socket errors. Cygwin does not have some errors in errno...
[mono-project.git] / support / serial.c
bloba6ad955179205d6fcdd750c3a670402f297ae3f1
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 <errno.h>
13 #if defined(__APPLE__)
14 #include "fakepoll.h"
15 #else
16 #include <sys/poll.h>
17 #endif
18 #include <sys/ioctl.h>
20 #include <glib.h>
22 /* This is for FIONREAD on solaris */
23 #if defined(sun)
24 #include <sys/filio.h>
25 #endif
27 /* sys/time.h (for timeval) is required when using osx 10.3 (but not 10.4) */
28 #ifdef __APPLE__
29 #include <sys/time.h>
30 #endif
32 /* This is a copy of System.IO.Ports.Handshake */
33 typedef enum {
34 NoneHandshake = 0,
35 XOnXOff = 1,
36 RequestToSend = 2,
37 RequestToSendXOnXOff = 3
38 } MonoHandshake;
40 /* This is a copy of System.IO.Ports.Parity */
41 typedef enum {
42 NoneParity = 0,
43 Odd = 1,
44 Even = 2,
45 Mark = 3,
46 Space = 4
47 } MonoParity;
49 /* This is a copy of System.IO.Ports.StopBits */
50 typedef enum {
51 NoneStopBits = 0,
52 One = 1,
53 Two = 2,
54 OnePointFive = 3
55 } MonoStopBits;
57 /* This is a copy of System.IO.Ports.SerialSignal */
58 typedef enum {
59 NoneSignal,
60 Cd = 1, /* Carrier detect */
61 Cts = 2, /* Clear to send */
62 Dsr = 4, /* Data set ready */
63 Dtr = 8, /* Data terminal ready */
64 Rts = 16 /* Request to send */
65 } MonoSerialSignal;
67 int
68 open_serial (char* devfile)
70 int fd;
71 fd = open (devfile, O_RDWR | O_NOCTTY | O_NONBLOCK);
73 return fd;
76 int
77 close_serial (int unix_fd)
79 // Linus writes: do not retry close after EINTR
80 return close (unix_fd);
83 guint32
84 read_serial (int fd, guchar *buffer, int offset, int count)
86 guint32 n;
88 n = read (fd, buffer + offset, count);
90 return (guint32) n;
93 int
94 write_serial (int fd, guchar *buffer, int offset, int count, int timeout)
96 struct pollfd pinfo;
97 guint32 n;
99 pinfo.fd = fd;
100 pinfo.events = POLLOUT;
101 pinfo.revents = POLLOUT;
103 n = count;
105 while (n > 0)
107 size_t t;
109 if (timeout != 0) {
110 int c;
112 while ((c = poll (&pinfo, 1, timeout)) == -1 && errno == EINTR)
114 if (c == -1)
115 return -1;
118 do {
119 t = write (fd, buffer + offset, n);
120 } while (t == -1 && errno == EINTR);
122 if (t < 0)
123 return -1;
125 offset += t;
126 n -= t;
129 return 0;
133 discard_buffer (int fd, gboolean input)
135 return tcflush(fd, input ? TCIFLUSH : TCOFLUSH);
138 gint32
139 get_bytes_in_buffer (int fd, gboolean input)
141 gint32 retval;
143 if (ioctl (fd, input ? FIONREAD : TIOCOUTQ, &retval) == -1) {
144 return -1;
147 return retval;
150 gboolean
151 set_attributes (int fd, int baud_rate, MonoParity parity, int dataBits, MonoStopBits stopBits, MonoHandshake handshake)
153 struct termios newtio;
155 if (tcgetattr (fd, &newtio) == -1)
156 return FALSE;
158 newtio.c_cflag |= (CLOCAL | CREAD);
159 newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ISIG | IEXTEN );
160 newtio.c_oflag &= ~(OPOST);
161 newtio.c_iflag = IGNBRK;
163 /* setup baudrate */
164 switch (baud_rate)
166 /*Some values are not defined on OSX and *BSD */
167 #if defined(B921600)
168 case 921600:
169 baud_rate = B921600;
170 break;
171 #endif
172 #if defined(B460800)
173 case 460800:
174 baud_rate = B460800;
175 break;
176 #endif
177 case 230400:
178 baud_rate = B230400;
179 break;
180 case 115200:
181 baud_rate = B115200;
182 break;
183 case 57600:
184 baud_rate = B57600;
185 break;
186 case 38400:
187 baud_rate = B38400;
188 break;
189 case 19200:
190 baud_rate = B19200;
191 break;
192 case 9600:
193 baud_rate = B9600;
194 break;
195 case 4800:
196 baud_rate = B4800;
197 break;
198 case 2400:
199 baud_rate = B2400;
200 break;
201 case 1800:
202 baud_rate = B1800;
203 break;
204 case 1200:
205 baud_rate = B1200;
206 break;
207 case 600:
208 baud_rate = B600;
209 break;
210 case 300:
211 baud_rate = B300;
212 break;
213 case 200:
214 baud_rate = B200;
215 break;
216 case 150:
217 baud_rate = B150;
218 break;
219 case 134:
220 baud_rate = B134;
221 break;
222 case 110:
223 baud_rate = B110;
224 break;
225 case 75:
226 baud_rate = B75;
227 break;
228 case 50:
229 case 0:
230 default:
231 baud_rate = B9600;
232 break;
235 /* char lenght */
236 newtio.c_cflag &= ~CSIZE;
237 switch (dataBits)
239 case 5:
240 newtio.c_cflag |= CS5;
241 break;
242 case 6:
243 newtio.c_cflag |= CS6;
244 break;
245 case 7:
246 newtio.c_cflag |= CS7;
247 break;
248 case 8:
249 default:
250 newtio.c_cflag |= CS8;
251 break;
254 /* stopbits */
255 switch (stopBits)
257 case NoneStopBits:
258 /* Unhandled */
259 break;
260 case One: /* One */
261 /* do nothing, the default is one stop bit */
262 newtio.c_cflag &= ~CSTOPB;
263 break;
264 case Two: /* Two */
265 newtio.c_cflag |= CSTOPB;
266 break;
267 case OnePointFive: /* OnePointFive */
268 /* XXX unhandled */
269 break;
272 /* parity */
273 newtio.c_iflag &= ~(INPCK | ISTRIP );
275 switch (parity)
277 case NoneParity: /* None */
278 newtio.c_cflag &= ~(PARENB | PARODD);
279 break;
281 case Odd: /* Odd */
282 newtio.c_cflag |= PARENB | PARODD;
283 break;
285 case Even: /* Even */
286 newtio.c_cflag &= ~(PARODD);
287 newtio.c_cflag |= (PARENB);
288 break;
290 case Mark: /* Mark */
291 /* XXX unhandled */
292 break;
293 case Space: /* Space */
294 /* XXX unhandled */
295 break;
298 newtio.c_iflag &= ~(IXOFF | IXON);
299 #ifdef CRTSCTS
300 newtio.c_cflag &= ~CRTSCTS;
301 #endif /* def CRTSCTS */
303 switch (handshake)
305 case NoneHandshake: /* None */
306 /* do nothing */
307 break;
308 case RequestToSend: /* RequestToSend (RTS) */
309 #ifdef CRTSCTS
310 newtio.c_cflag |= CRTSCTS;
311 #endif /* def CRTSCTS */
312 break;
313 case RequestToSendXOnXOff: /* RequestToSendXOnXOff (RTS + XON/XOFF) */
314 #ifdef CRTSCTS
315 newtio.c_cflag |= CRTSCTS;
316 #endif /* def CRTSCTS */
317 /* fall through */
318 case XOnXOff: /* XOnXOff */
319 newtio.c_iflag |= IXOFF | IXON;
320 break;
323 if (cfsetospeed (&newtio, baud_rate) < 0 || cfsetispeed (&newtio, baud_rate) < 0 ||
324 tcsetattr (fd, TCSANOW, &newtio) < 0)
326 return FALSE;
328 else
330 return TRUE;
335 static gint32
336 get_signal_code (MonoSerialSignal signal)
338 switch (signal) {
339 case Cd:
340 return TIOCM_CAR;
341 case Cts:
342 return TIOCM_CTS;
343 case Dsr:
344 return TIOCM_DSR;
345 case Dtr:
346 return TIOCM_DTR;
347 case Rts:
348 return TIOCM_RTS;
349 default:
350 return 0;
353 /* Not reached */
354 return 0;
357 static MonoSerialSignal
358 get_mono_signal_codes (int signals)
360 MonoSerialSignal retval = NoneSignal;
362 if ((signals & TIOCM_CAR) != 0)
363 retval |= Cd;
364 if ((signals & TIOCM_CTS) != 0)
365 retval |= Cts;
366 if ((signals & TIOCM_DSR) != 0)
367 retval |= Dsr;
368 if ((signals & TIOCM_DTR) != 0)
369 retval |= Dtr;
370 if ((signals & TIOCM_RTS) != 0)
371 retval |= Rts;
373 return retval;
376 MonoSerialSignal
377 get_signals (int fd, gint32 *error)
379 int signals;
381 *error = 0;
383 if (ioctl (fd, TIOCMGET, &signals) == -1) {
384 *error = -1;
385 return NoneSignal;
388 return get_mono_signal_codes (signals);
391 gint32
392 set_signal (int fd, MonoSerialSignal signal, gboolean value)
394 int signals, expected, activated;
396 expected = get_signal_code (signal);
397 if (ioctl (fd, TIOCMGET, &signals) == -1)
398 return -1;
400 activated = (signals & expected) != 0;
401 if (activated == value) /* Already set */
402 return 1;
404 if (value)
405 signals |= expected;
406 else
407 signals &= ~expected;
409 if (ioctl (fd, TIOCMSET, &signals) == -1)
410 return -1;
412 return 1;
416 breakprop (int fd)
418 return tcsendbreak (fd, 0);
421 gboolean
422 poll_serial (int fd, gint32 *error, int timeout)
424 struct pollfd pinfo;
426 *error = 0;
428 pinfo.fd = fd;
429 pinfo.events = POLLIN;
430 pinfo.revents = 0;
432 while (poll (&pinfo, 1, timeout) == -1 && errno == EINTR) {
433 /* EINTR is an OK condition, we should not throw in the upper layer an IOException */
434 if (errno != EINTR){
435 *error = -1;
436 return FALSE;
440 return (pinfo.revents & POLLIN) != 0 ? 1 : 0;
444 * mono internals should not be used here.
445 * this serial stuff needs to be implemented with icalls.
446 * make this at least compile until the code is moved elsewhere
447 * defined(linux) is wrong, too
449 void*
450 list_serial_devices (void)
452 return NULL;
455 #if 0
456 MonoArray *
457 list_serial_devices (void)
459 MonoArray *array;
460 #if defined(linux)
461 /* Linux serial files are of the form ttyS[0-9]+ */
462 GSList *l, *list = NULL;
463 GDir* dir = g_dir_open ("/dev", 0, NULL);
464 const char *filename;
465 int i = 0;
467 while ((filename = g_dir_read_name (dir))) {
468 if (filename) {
469 if (!strncmp (filename, "ttyS", 4))
470 list = g_slist_append (list, g_strconcat ("/dev/", filename, NULL));
474 g_dir_close (dir);
476 array = mono_array_new (mono_domain_get (), mono_get_string_class (), g_slist_length (list));
477 for (l = list; l; l = l->next) {
478 mono_array_set (array, gpointer, i++, mono_string_new (mono_domain_get (), (char*)l->data));
479 g_free (l->data);
482 g_slist_free (list);
484 #else
485 #warning "list_serial_devices isn't ported to this OS"
486 #endif
488 return array;
490 #endif