Merge pull request #5382 from kumpera/pedump_fix
[mono-project.git] / support / serial.c
blob5e3d732c005170bbcd4052841fad7dc3b27b342c
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_APPLETVOS)
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 and *BSD */
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 case 230400:
216 baud_rate = B230400;
217 break;
218 case 115200:
219 baud_rate = B115200;
220 break;
221 case 57600:
222 baud_rate = B57600;
223 break;
224 case 38400:
225 baud_rate = B38400;
226 break;
227 case 19200:
228 baud_rate = B19200;
229 break;
230 case 9600:
231 baud_rate = B9600;
232 break;
233 case 4800:
234 baud_rate = B4800;
235 break;
236 case 2400:
237 baud_rate = B2400;
238 break;
239 case 1800:
240 baud_rate = B1800;
241 break;
242 case 1200:
243 baud_rate = B1200;
244 break;
245 case 600:
246 baud_rate = B600;
247 break;
248 case 300:
249 baud_rate = B300;
250 break;
251 case 200:
252 baud_rate = B200;
253 break;
254 case 150:
255 baud_rate = B150;
256 break;
257 case 134:
258 baud_rate = B134;
259 break;
260 case 110:
261 baud_rate = B110;
262 break;
263 case 75:
264 baud_rate = B75;
265 break;
266 case 50:
267 #ifdef B50
268 baud_rate = B50;
269 #else
270 baud_rate = -1;
271 break;
272 #endif
273 case 0:
274 #ifdef B0
275 baud_rate = B0;
276 #else
277 baud_rate = -1;
278 #endif
279 break;
280 default:
281 *custom_baud_rate = TRUE;
282 break;
284 return baud_rate;
287 gboolean
288 set_attributes (int fd, int baud_rate, MonoParity parity, int dataBits, MonoStopBits stopBits, MonoHandshake handshake)
290 struct termios newtio;
291 gboolean custom_baud_rate = FALSE;
293 if (tcgetattr (fd, &newtio) == -1)
294 return FALSE;
296 newtio.c_cflag |= (CLOCAL | CREAD);
297 newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ISIG | IEXTEN );
298 newtio.c_oflag &= ~(OPOST);
299 newtio.c_iflag = IGNBRK;
301 /* setup baudrate */
302 baud_rate = setup_baud_rate (baud_rate, &custom_baud_rate);
304 /* char lenght */
305 newtio.c_cflag &= ~CSIZE;
306 switch (dataBits)
308 case 5:
309 newtio.c_cflag |= CS5;
310 break;
311 case 6:
312 newtio.c_cflag |= CS6;
313 break;
314 case 7:
315 newtio.c_cflag |= CS7;
316 break;
317 case 8:
318 default:
319 newtio.c_cflag |= CS8;
320 break;
323 /* stopbits */
324 switch (stopBits)
326 case NoneStopBits:
327 /* Unhandled */
328 break;
329 case One: /* One */
330 /* do nothing, the default is one stop bit */
331 newtio.c_cflag &= ~CSTOPB;
332 break;
333 case Two: /* Two */
334 newtio.c_cflag |= CSTOPB;
335 break;
336 case OnePointFive: /* OnePointFive */
337 /* XXX unhandled */
338 break;
341 /* parity */
342 newtio.c_iflag &= ~(INPCK | ISTRIP );
344 switch (parity)
346 case NoneParity: /* None */
347 newtio.c_cflag &= ~(PARENB | PARODD);
348 break;
350 case Odd: /* Odd */
351 newtio.c_cflag |= PARENB | PARODD;
352 break;
354 case Even: /* Even */
355 newtio.c_cflag &= ~(PARODD);
356 newtio.c_cflag |= (PARENB);
357 break;
359 case Mark: /* Mark */
360 /* XXX unhandled */
361 break;
362 case Space: /* Space */
363 /* XXX unhandled */
364 break;
367 newtio.c_iflag &= ~(IXOFF | IXON);
368 #ifdef CRTSCTS
369 newtio.c_cflag &= ~CRTSCTS;
370 #endif /* def CRTSCTS */
372 switch (handshake)
374 case NoneHandshake: /* None */
375 /* do nothing */
376 break;
377 case RequestToSend: /* RequestToSend (RTS) */
378 #ifdef CRTSCTS
379 newtio.c_cflag |= CRTSCTS;
380 #endif /* def CRTSCTS */
381 break;
382 case RequestToSendXOnXOff: /* RequestToSendXOnXOff (RTS + XON/XOFF) */
383 #ifdef CRTSCTS
384 newtio.c_cflag |= CRTSCTS;
385 #endif /* def CRTSCTS */
386 /* fall through */
387 case XOnXOff: /* XOnXOff */
388 newtio.c_iflag |= IXOFF | IXON;
389 break;
392 if (custom_baud_rate == FALSE) {
393 if (cfsetospeed (&newtio, baud_rate) < 0 || cfsetispeed (&newtio, baud_rate) < 0)
394 return FALSE;
395 } else {
396 #if __linux__ || defined(HAVE_IOKIT)
398 /* On Linux to set a custom baud rate, we must set the
399 * "standard" baud_rate to 38400. On Apple we set it purely
400 * so that tcsetattr has something to use (and report back later), but
401 * the Apple specific API is still opaque to these APIs, see:
402 * https://developer.apple.com/library/mac/samplecode/SerialPortSample/Listings/SerialPortSample_SerialPortSample_c.html#//apple_ref/doc/uid/DTS10000454-SerialPortSample_SerialPortSample_c-DontLinkElementID_4
404 if (cfsetospeed (&newtio, B38400) < 0 || cfsetispeed (&newtio, B38400) < 0)
405 return FALSE;
406 #endif
409 if (tcsetattr (fd, TCSANOW, &newtio) < 0)
410 return FALSE;
412 if (custom_baud_rate == TRUE){
413 #if defined(HAVE_LINUX_SERIAL_H)
414 struct serial_struct ser;
416 if (ioctl (fd, TIOCGSERIAL, &ser) < 0)
418 return FALSE;
421 ser.custom_divisor = ser.baud_base / baud_rate;
422 ser.flags &= ~ASYNC_SPD_MASK;
423 ser.flags |= ASYNC_SPD_CUST;
425 if (ioctl (fd, TIOCSSERIAL, &ser) < 0)
427 return FALSE;
429 #elif defined(HAVE_IOKIT)
430 speed_t speed = baud_rate;
431 if (ioctl(fd, IOSSIOSPEED, &speed) == -1)
432 return FALSE;
433 #else
434 /* Don't know how to set custom baud rate on this platform. */
435 return FALSE;
436 #endif
439 return TRUE;
443 static gint32
444 get_signal_code (MonoSerialSignal signal)
446 switch (signal) {
447 case Cd:
448 return TIOCM_CAR;
449 case Cts:
450 return TIOCM_CTS;
451 case Dsr:
452 return TIOCM_DSR;
453 case Dtr:
454 return TIOCM_DTR;
455 case Rts:
456 return TIOCM_RTS;
457 default:
458 return 0;
461 /* Not reached */
462 return 0;
465 static MonoSerialSignal
466 get_mono_signal_codes (int signals)
468 MonoSerialSignal retval = NoneSignal;
470 if ((signals & TIOCM_CAR) != 0)
471 retval |= Cd;
472 if ((signals & TIOCM_CTS) != 0)
473 retval |= Cts;
474 if ((signals & TIOCM_DSR) != 0)
475 retval |= Dsr;
476 if ((signals & TIOCM_DTR) != 0)
477 retval |= Dtr;
478 if ((signals & TIOCM_RTS) != 0)
479 retval |= Rts;
481 return retval;
484 MonoSerialSignal
485 get_signals (int fd, gint32 *error)
487 int signals;
489 *error = 0;
491 if (ioctl (fd, TIOCMGET, &signals) == -1) {
492 *error = -1;
493 return NoneSignal;
496 return get_mono_signal_codes (signals);
499 gint32
500 set_signal (int fd, MonoSerialSignal signal, gboolean value)
502 int signals, expected, activated;
504 expected = get_signal_code (signal);
505 if (ioctl (fd, TIOCMGET, &signals) == -1)
506 return -1;
508 activated = (signals & expected) != 0;
509 if (activated == value) /* Already set */
510 return 1;
512 if (value)
513 signals |= expected;
514 else
515 signals &= ~expected;
517 if (ioctl (fd, TIOCMSET, &signals) == -1)
518 return -1;
520 return 1;
524 breakprop (int fd)
526 return tcsendbreak (fd, 0);
529 gboolean
530 poll_serial (int fd, gint32 *error, int timeout)
532 struct pollfd pinfo;
534 *error = 0;
536 pinfo.fd = fd;
537 pinfo.events = POLLIN;
538 pinfo.revents = 0;
540 while (poll (&pinfo, 1, timeout) == -1 && errno == EINTR) {
541 /* EINTR is an OK condition, we should not throw in the upper layer an IOException */
542 if (errno != EINTR){
543 *error = -1;
544 return FALSE;
548 return (pinfo.revents & POLLIN) != 0 ? 1 : 0;
552 * mono internals should not be used here.
553 * this serial stuff needs to be implemented with icalls.
554 * make this at least compile until the code is moved elsewhere
555 * defined(linux) is wrong, too
557 void*
558 list_serial_devices (void)
560 return NULL;
563 #if 0
564 MonoArray *
565 list_serial_devices (void)
567 MonoArray *array;
568 #if defined(linux)
569 /* Linux serial files are of the form ttyS[0-9]+ */
570 GSList *l, *list = NULL;
571 GDir* dir = g_dir_open ("/dev", 0, NULL);
572 const char *filename;
573 int i = 0;
575 while ((filename = g_dir_read_name (dir))) {
576 if (filename) {
577 if (!strncmp (filename, "ttyS", 4))
578 list = g_slist_append (list, g_strconcat ("/dev/", filename, NULL));
582 g_dir_close (dir);
584 array = mono_array_new (mono_domain_get (), mono_get_string_class (), g_slist_length (list));
585 for (l = list; l; l = l->next) {
586 mono_array_set (array, gpointer, i++, mono_string_new (mono_domain_get (), (char*)l->data));
587 g_free (l->data);
590 g_slist_free (list);
592 #else
593 #warning "list_serial_devices isn't ported to this OS"
594 #endif
596 return array;
598 #endif