[sdks] Don't package android-sources on Windows
[mono-project.git] / support / serial.c
blob74c54b0a21ce1b93c064e9a9362ce6a0293dfe81
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 "map.h"
9 #include "mph.h"
11 #include <termios.h>
12 #include <unistd.h>
13 #include <fcntl.h>
14 #include <string.h>
15 #include <errno.h>
16 #if defined(HAVE_POLL_H)
17 #include <poll.h>
18 #elif defined(HAVE_SYS_POLL_H)
19 #include <sys/poll.h>
20 #endif
21 #include <sys/ioctl.h>
23 /* This is for ASYNC_*, serial_struct on linux */
24 #if defined(HAVE_LINUX_SERIAL_H)
25 #include <linux/serial.h>
26 #endif
28 #include <glib.h>
30 /* This is for FIONREAD on solaris */
31 #if defined(sun)
32 #include <sys/filio.h>
33 #endif
35 /* sys/time.h (for timeval) is required when using osx 10.3 (but not 10.4) */
36 /* IOKit is a private framework in iOS, so exclude there */
37 #if defined(__APPLE__) && !defined(HOST_IOS) && !defined(HOST_WATCHOS) && !defined(HOST_TVOS)
38 #define HAVE_IOKIT 1
39 #endif
41 #if defined(HAVE_IOKIT)
42 #include <sys/time.h>
43 #include <IOKit/IOKitLib.h>
44 #include <IOKit/serial/IOSerialKeys.h>
45 #include <IOKit/serial/ioss.h>
46 #include <IOKit/IOBSD.h>
47 #endif
49 /* This is a copy of System.IO.Ports.Handshake */
50 typedef enum {
51 NoneHandshake = 0,
52 XOnXOff = 1,
53 RequestToSend = 2,
54 RequestToSendXOnXOff = 3
55 } MonoHandshake;
57 /* This is a copy of System.IO.Ports.Parity */
58 typedef enum {
59 NoneParity = 0,
60 Odd = 1,
61 Even = 2,
62 Mark = 3,
63 Space = 4
64 } MonoParity;
66 /* This is a copy of System.IO.Ports.StopBits */
67 typedef enum {
68 NoneStopBits = 0,
69 One = 1,
70 Two = 2,
71 OnePointFive = 3
72 } MonoStopBits;
74 /* This is a copy of System.IO.Ports.SerialSignal */
75 typedef enum {
76 NoneSignal,
77 Cd = 1, /* Carrier detect */
78 Cts = 2, /* Clear to send */
79 Dsr = 4, /* Data set ready */
80 Dtr = 8, /* Data terminal ready */
81 Rts = 16 /* Request to send */
82 } MonoSerialSignal;
85 * Silence the compiler, we do not need these prototypes to be public, since these are only
86 * used by P/Invoke
89 int open_serial (char *devfile);
90 int close_serial (int unix_fd);
91 guint32 read_serial (int fd, guchar *buffer, int offset, int count);
92 int write_serial (int fd, guchar *buffer, int offset, int count, int timeout);
93 int discard_buffer (int fd, gboolean input);
94 gint32 get_bytes_in_buffer (int fd, gboolean input);
95 gboolean is_baud_rate_legal (int baud_rate);
96 int setup_baud_rate (int baud_rate, gboolean *custom_baud_rate);
97 gboolean set_attributes (int fd, int baud_rate, MonoParity parity, int dataBits, MonoStopBits stopBits, MonoHandshake handshake);
98 MonoSerialSignal get_signals (int fd, gint32 *error);
99 gint32 set_signal (int fd, MonoSerialSignal signal, gboolean value);
100 int breakprop (int fd);
101 gboolean poll_serial (int fd, gint32 *error, int timeout);
102 void *list_serial_devices (void);
105 open_serial (char *devfile)
107 int fd;
108 fd = open (devfile, O_RDWR | O_NOCTTY | O_NONBLOCK);
110 return fd;
114 close_serial (int unix_fd)
116 // Linus writes: do not retry close after EINTR
117 return close (unix_fd);
120 guint32
121 read_serial (int fd, guchar *buffer, int offset, int count)
123 guint32 n;
125 n = read (fd, buffer + offset, count);
127 return (guint32) n;
131 write_serial (int fd, guchar *buffer, int offset, int count, int timeout)
133 struct pollfd pinfo;
134 guint32 n;
136 pinfo.fd = fd;
137 pinfo.events = POLLOUT;
138 pinfo.revents = POLLOUT;
140 n = count;
142 while (n > 0)
144 ssize_t t;
146 if (timeout != 0) {
147 int c;
149 while ((c = poll (&pinfo, 1, timeout)) == -1 && errno == EINTR)
151 if (c == -1)
152 return -1;
155 do {
156 t = write (fd, buffer + offset, n);
157 } while (t == -1 && errno == EINTR);
159 if (t < 0)
160 return -1;
162 offset += t;
163 n -= t;
166 return 0;
170 discard_buffer (int fd, gboolean input)
172 return tcflush(fd, input ? TCIFLUSH : TCOFLUSH);
175 gint32
176 get_bytes_in_buffer (int fd, gboolean input)
178 #if defined(__HAIKU__)
179 /* FIXME: Haiku doesn't support TIOCOUTQ nor FIONREAD on fds */
180 return -1;
181 #define TIOCOUTQ 0
182 #endif
183 gint32 retval;
185 if (ioctl (fd, input ? FIONREAD : TIOCOUTQ, &retval) == -1) {
186 return -1;
189 return retval;
192 gboolean
193 is_baud_rate_legal (int baud_rate)
195 gboolean ignore = FALSE;
196 return setup_baud_rate (baud_rate, &ignore) != -1;
200 setup_baud_rate (int baud_rate, gboolean *custom_baud_rate)
202 switch (baud_rate)
204 /*Some values are not defined on OSX, *BSD, or AIX */
205 #if defined(B921600)
206 case 921600:
207 baud_rate = B921600;
208 break;
209 #endif
210 #if defined(B460800)
211 case 460800:
212 baud_rate = B460800;
213 break;
214 #endif
215 #if defined(B230400)
216 case 230400:
217 baud_rate = B230400;
218 break;
219 #endif
220 #if defined(B115200)
221 case 115200:
222 baud_rate = B115200;
223 break;
224 #endif
225 #if defined(B57600)
226 case 57600:
227 baud_rate = B57600;
228 break;
229 #endif
230 case 38400:
231 baud_rate = B38400;
232 break;
233 case 19200:
234 baud_rate = B19200;
235 break;
236 case 9600:
237 baud_rate = B9600;
238 break;
239 case 4800:
240 baud_rate = B4800;
241 break;
242 case 2400:
243 baud_rate = B2400;
244 break;
245 case 1800:
246 baud_rate = B1800;
247 break;
248 case 1200:
249 baud_rate = B1200;
250 break;
251 case 600:
252 baud_rate = B600;
253 break;
254 case 300:
255 baud_rate = B300;
256 break;
257 case 200:
258 baud_rate = B200;
259 break;
260 case 150:
261 baud_rate = B150;
262 break;
263 case 134:
264 baud_rate = B134;
265 break;
266 case 110:
267 baud_rate = B110;
268 break;
269 case 75:
270 baud_rate = B75;
271 break;
272 case 50:
273 #ifdef B50
274 baud_rate = B50;
275 #else
276 baud_rate = -1;
277 break;
278 #endif
279 case 0:
280 #ifdef B0
281 baud_rate = B0;
282 #else
283 baud_rate = -1;
284 #endif
285 break;
286 default:
287 *custom_baud_rate = TRUE;
288 break;
290 return baud_rate;
293 gboolean
294 set_attributes (int fd, int baud_rate, MonoParity parity, int dataBits, MonoStopBits stopBits, MonoHandshake handshake)
296 struct termios newtio;
297 gboolean custom_baud_rate = FALSE;
299 if (tcgetattr (fd, &newtio) == -1)
300 return FALSE;
302 newtio.c_cflag |= (CLOCAL | CREAD);
303 newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ISIG | IEXTEN );
304 newtio.c_oflag &= ~(OPOST);
305 newtio.c_iflag = IGNBRK;
307 /* setup baudrate */
308 baud_rate = setup_baud_rate (baud_rate, &custom_baud_rate);
310 /* char lenght */
311 newtio.c_cflag &= ~CSIZE;
312 switch (dataBits)
314 case 5:
315 newtio.c_cflag |= CS5;
316 break;
317 case 6:
318 newtio.c_cflag |= CS6;
319 break;
320 case 7:
321 newtio.c_cflag |= CS7;
322 break;
323 case 8:
324 default:
325 newtio.c_cflag |= CS8;
326 break;
329 /* stopbits */
330 switch (stopBits)
332 case NoneStopBits:
333 /* Unhandled */
334 break;
335 case One: /* One */
336 /* do nothing, the default is one stop bit */
337 newtio.c_cflag &= ~CSTOPB;
338 break;
339 case Two: /* Two */
340 newtio.c_cflag |= CSTOPB;
341 break;
342 case OnePointFive: /* OnePointFive */
343 /* 8250 UART handles stop bit flag as 1.5 stop bits for 5 data bits */
344 newtio.c_cflag |= CSTOPB;
345 break;
348 /* parity */
349 newtio.c_iflag &= ~(INPCK | ISTRIP );
351 switch (parity)
353 case NoneParity: /* None */
354 newtio.c_cflag &= ~(PARENB | PARODD);
355 break;
357 case Odd: /* Odd */
358 newtio.c_cflag |= PARENB | PARODD;
359 break;
361 case Even: /* Even */
362 newtio.c_cflag &= ~(PARODD);
363 newtio.c_cflag |= (PARENB);
364 break;
366 case Mark: /* Mark */
367 /* XXX unhandled */
368 break;
369 case Space: /* Space */
370 /* XXX unhandled */
371 break;
374 newtio.c_iflag &= ~(IXOFF | IXON);
375 #ifdef CRTSCTS
376 newtio.c_cflag &= ~CRTSCTS;
377 #endif /* def CRTSCTS */
379 switch (handshake)
381 case NoneHandshake: /* None */
382 /* do nothing */
383 break;
384 case RequestToSend: /* RequestToSend (RTS) */
385 #ifdef CRTSCTS
386 newtio.c_cflag |= CRTSCTS;
387 #endif /* def CRTSCTS */
388 break;
389 case RequestToSendXOnXOff: /* RequestToSendXOnXOff (RTS + XON/XOFF) */
390 #ifdef CRTSCTS
391 newtio.c_cflag |= CRTSCTS;
392 #endif /* def CRTSCTS */
393 /* fall through */
394 case XOnXOff: /* XOnXOff */
395 newtio.c_iflag |= IXOFF | IXON;
396 break;
399 if (custom_baud_rate == FALSE) {
400 if (cfsetospeed (&newtio, baud_rate) < 0 || cfsetispeed (&newtio, baud_rate) < 0)
401 return FALSE;
402 } else {
403 #if __linux__ || defined(HAVE_IOKIT)
405 /* On Linux to set a custom baud rate, we must set the
406 * "standard" baud_rate to 38400. On Apple we set it purely
407 * so that tcsetattr has something to use (and report back later), but
408 * the Apple specific API is still opaque to these APIs, see:
409 * https://developer.apple.com/library/mac/samplecode/SerialPortSample/Listings/SerialPortSample_SerialPortSample_c.html#//apple_ref/doc/uid/DTS10000454-SerialPortSample_SerialPortSample_c-DontLinkElementID_4
411 if (cfsetospeed (&newtio, B38400) < 0 || cfsetispeed (&newtio, B38400) < 0)
412 return FALSE;
413 #endif
416 if (tcsetattr (fd, TCSANOW, &newtio) < 0)
417 return FALSE;
419 if (custom_baud_rate == TRUE){
420 #if defined(HAVE_LINUX_SERIAL_H)
421 struct serial_struct ser;
423 if (ioctl (fd, TIOCGSERIAL, &ser) < 0)
425 return FALSE;
428 ser.custom_divisor = ser.baud_base / baud_rate;
429 ser.flags &= ~ASYNC_SPD_MASK;
430 ser.flags |= ASYNC_SPD_CUST;
432 if (ioctl (fd, TIOCSSERIAL, &ser) < 0)
434 return FALSE;
436 #elif defined(HAVE_IOKIT)
437 speed_t speed = baud_rate;
438 if (ioctl(fd, IOSSIOSPEED, &speed) == -1)
439 return FALSE;
440 #else
441 /* Don't know how to set custom baud rate on this platform. */
442 return FALSE;
443 #endif
446 return TRUE;
450 static gint32
451 get_signal_code (MonoSerialSignal signal)
453 switch (signal) {
454 case Cd:
455 return TIOCM_CAR;
456 case Cts:
457 return TIOCM_CTS;
458 case Dsr:
459 return TIOCM_DSR;
460 case Dtr:
461 return TIOCM_DTR;
462 case Rts:
463 return TIOCM_RTS;
464 default:
465 return 0;
468 /* Not reached */
469 return 0;
472 static MonoSerialSignal
473 get_mono_signal_codes (int signals)
475 MonoSerialSignal retval = NoneSignal;
477 if ((signals & TIOCM_CAR) != 0)
478 retval |= Cd;
479 if ((signals & TIOCM_CTS) != 0)
480 retval |= Cts;
481 if ((signals & TIOCM_DSR) != 0)
482 retval |= Dsr;
483 if ((signals & TIOCM_DTR) != 0)
484 retval |= Dtr;
485 if ((signals & TIOCM_RTS) != 0)
486 retval |= Rts;
488 return retval;
491 MonoSerialSignal
492 get_signals (int fd, gint32 *error)
494 int signals;
496 *error = 0;
498 if (ioctl (fd, TIOCMGET, &signals) == -1) {
499 *error = -1;
500 return NoneSignal;
503 return get_mono_signal_codes (signals);
506 gint32
507 set_signal (int fd, MonoSerialSignal signal, gboolean value)
509 int signals, expected, activated;
511 expected = get_signal_code (signal);
512 if (ioctl (fd, TIOCMGET, &signals) == -1)
513 return -1;
515 activated = (signals & expected) != 0;
516 if (activated == value) /* Already set */
517 return 1;
519 if (value)
520 signals |= expected;
521 else
522 signals &= ~expected;
524 if (ioctl (fd, TIOCMSET, &signals) == -1)
525 return -1;
527 return 1;
531 breakprop (int fd)
533 return tcsendbreak (fd, 0);
536 gboolean
537 poll_serial (int fd, gint32 *error, int timeout)
539 struct pollfd pinfo;
541 *error = 0;
543 pinfo.fd = fd;
544 pinfo.events = POLLIN;
545 pinfo.revents = 0;
547 while (poll (&pinfo, 1, timeout) == -1 && errno == EINTR) {
548 /* EINTR is an OK condition, we should not throw in the upper layer an IOException */
549 if (errno != EINTR){
550 *error = -1;
551 return FALSE;
555 return (pinfo.revents & POLLIN) != 0 ? 1 : 0;
559 * mono internals should not be used here.
560 * this serial stuff needs to be implemented with icalls.
561 * make this at least compile until the code is moved elsewhere
562 * defined(linux) is wrong, too
564 void*
565 list_serial_devices (void)
567 return NULL;
570 #if 0
571 MonoArray *
572 list_serial_devices (void)
574 MonoArray *array;
575 #if defined(linux)
576 /* Linux serial files are of the form ttyS[0-9]+ */
577 GSList *l, *list = NULL;
578 GDir* dir = g_dir_open ("/dev", 0, NULL);
579 const char *filename;
580 int i = 0;
582 while ((filename = g_dir_read_name (dir))) {
583 if (filename) {
584 if (!strncmp (filename, "ttyS", 4))
585 list = g_slist_append (list, g_strconcat ("/dev/", filename, NULL));
589 g_dir_close (dir);
591 array = mono_array_new (mono_domain_get (), mono_get_string_class (), g_slist_length (list));
592 for (l = list; l; l = l->next) {
593 mono_array_set (array, gpointer, i++, mono_string_new (mono_domain_get (), (char*)l->data));
594 g_free (l->data);
597 g_slist_free (list);
599 #else
600 #warning "list_serial_devices isn't ported to this OS"
601 #endif
603 return array;
605 #endif