Merge pull request #1809 from kumpera/double_loading_fix
[mono-project.git] / support / serial.c
blob94d7e0b19f5d72c19bb9aa3808b17e0d22f391a8
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)
38 #include <sys/time.h>
39 #include <IOKit/IOKitLib.h>
40 #include <IOKit/serial/IOSerialKeys.h>
41 #include <IOKit/serial/ioss.h>
42 #include <IOKit/IOBSD.h>
43 #endif
45 /* This is a copy of System.IO.Ports.Handshake */
46 typedef enum {
47 NoneHandshake = 0,
48 XOnXOff = 1,
49 RequestToSend = 2,
50 RequestToSendXOnXOff = 3
51 } MonoHandshake;
53 /* This is a copy of System.IO.Ports.Parity */
54 typedef enum {
55 NoneParity = 0,
56 Odd = 1,
57 Even = 2,
58 Mark = 3,
59 Space = 4
60 } MonoParity;
62 /* This is a copy of System.IO.Ports.StopBits */
63 typedef enum {
64 NoneStopBits = 0,
65 One = 1,
66 Two = 2,
67 OnePointFive = 3
68 } MonoStopBits;
70 /* This is a copy of System.IO.Ports.SerialSignal */
71 typedef enum {
72 NoneSignal,
73 Cd = 1, /* Carrier detect */
74 Cts = 2, /* Clear to send */
75 Dsr = 4, /* Data set ready */
76 Dtr = 8, /* Data terminal ready */
77 Rts = 16 /* Request to send */
78 } MonoSerialSignal;
81 * Silence the compiler, we do not need these prototypes to be public, since these are only
82 * used by P/Invoke
85 int open_serial (char *devfile);
86 int close_serial (int unix_fd);
87 guint32 read_serial (int fd, guchar *buffer, int offset, int count);
88 int write_serial (int fd, guchar *buffer, int offset, int count, int timeout);
89 int discard_buffer (int fd, gboolean input);
90 gint32 get_bytes_in_buffer (int fd, gboolean input);
91 gboolean is_baud_rate_legal (int baud_rate);
92 int setup_baud_rate (int baud_rate, gboolean *custom_baud_rate);
93 gboolean set_attributes (int fd, int baud_rate, MonoParity parity, int dataBits, MonoStopBits stopBits, MonoHandshake handshake);
94 MonoSerialSignal get_signals (int fd, gint32 *error);
95 gint32 set_signal (int fd, MonoSerialSignal signal, gboolean value);
96 int breakprop (int fd);
97 gboolean poll_serial (int fd, gint32 *error, int timeout);
98 void *list_serial_devices (void);
101 open_serial (char *devfile)
103 int fd;
104 fd = open (devfile, O_RDWR | O_NOCTTY | O_NONBLOCK);
106 return fd;
110 close_serial (int unix_fd)
112 // Linus writes: do not retry close after EINTR
113 return close (unix_fd);
116 guint32
117 read_serial (int fd, guchar *buffer, int offset, int count)
119 guint32 n;
121 n = read (fd, buffer + offset, count);
123 return (guint32) n;
127 write_serial (int fd, guchar *buffer, int offset, int count, int timeout)
129 struct pollfd pinfo;
130 guint32 n;
132 pinfo.fd = fd;
133 pinfo.events = POLLOUT;
134 pinfo.revents = POLLOUT;
136 n = count;
138 while (n > 0)
140 ssize_t t;
142 if (timeout != 0) {
143 int c;
145 while ((c = poll (&pinfo, 1, timeout)) == -1 && errno == EINTR)
147 if (c == -1)
148 return -1;
151 do {
152 t = write (fd, buffer + offset, n);
153 } while (t == -1 && errno == EINTR);
155 if (t < 0)
156 return -1;
158 offset += t;
159 n -= t;
162 return 0;
166 discard_buffer (int fd, gboolean input)
168 return tcflush(fd, input ? TCIFLUSH : TCOFLUSH);
171 gint32
172 get_bytes_in_buffer (int fd, gboolean input)
174 gint32 retval;
176 if (ioctl (fd, input ? FIONREAD : TIOCOUTQ, &retval) == -1) {
177 return -1;
180 return retval;
183 gboolean
184 is_baud_rate_legal (int baud_rate)
186 gboolean ignore = FALSE;
187 return setup_baud_rate (baud_rate, &ignore) != -1;
191 setup_baud_rate (int baud_rate, gboolean *custom_baud_rate)
193 switch (baud_rate)
195 /*Some values are not defined on OSX and *BSD */
196 #if defined(B921600)
197 case 921600:
198 baud_rate = B921600;
199 break;
200 #endif
201 #if defined(B460800)
202 case 460800:
203 baud_rate = B460800;
204 break;
205 #endif
206 case 230400:
207 baud_rate = B230400;
208 break;
209 case 115200:
210 baud_rate = B115200;
211 break;
212 case 57600:
213 baud_rate = B57600;
214 break;
215 case 38400:
216 baud_rate = B38400;
217 break;
218 case 19200:
219 baud_rate = B19200;
220 break;
221 case 9600:
222 baud_rate = B9600;
223 break;
224 case 4800:
225 baud_rate = B4800;
226 break;
227 case 2400:
228 baud_rate = B2400;
229 break;
230 case 1800:
231 baud_rate = B1800;
232 break;
233 case 1200:
234 baud_rate = B1200;
235 break;
236 case 600:
237 baud_rate = B600;
238 break;
239 case 300:
240 baud_rate = B300;
241 break;
242 case 200:
243 baud_rate = B200;
244 break;
245 case 150:
246 baud_rate = B150;
247 break;
248 case 134:
249 baud_rate = B134;
250 break;
251 case 110:
252 baud_rate = B110;
253 break;
254 case 75:
255 baud_rate = B75;
256 break;
257 case 50:
258 #ifdef B50
259 baud_rate = B50;
260 #else
261 baud_rate = -1;
262 break;
263 #endif
264 case 0:
265 #ifdef B0
266 baud_rate = B0;
267 #else
268 baud_rate = -1;
269 #endif
270 break;
271 default:
272 *custom_baud_rate = TRUE;
273 break;
275 return baud_rate;
278 gboolean
279 set_attributes (int fd, int baud_rate, MonoParity parity, int dataBits, MonoStopBits stopBits, MonoHandshake handshake)
281 struct termios newtio;
282 gboolean custom_baud_rate = FALSE;
284 if (tcgetattr (fd, &newtio) == -1)
285 return FALSE;
287 newtio.c_cflag |= (CLOCAL | CREAD);
288 newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ISIG | IEXTEN );
289 newtio.c_oflag &= ~(OPOST);
290 newtio.c_iflag = IGNBRK;
292 /* setup baudrate */
293 baud_rate = setup_baud_rate (baud_rate, &custom_baud_rate);
295 /* char lenght */
296 newtio.c_cflag &= ~CSIZE;
297 switch (dataBits)
299 case 5:
300 newtio.c_cflag |= CS5;
301 break;
302 case 6:
303 newtio.c_cflag |= CS6;
304 break;
305 case 7:
306 newtio.c_cflag |= CS7;
307 break;
308 case 8:
309 default:
310 newtio.c_cflag |= CS8;
311 break;
314 /* stopbits */
315 switch (stopBits)
317 case NoneStopBits:
318 /* Unhandled */
319 break;
320 case One: /* One */
321 /* do nothing, the default is one stop bit */
322 newtio.c_cflag &= ~CSTOPB;
323 break;
324 case Two: /* Two */
325 newtio.c_cflag |= CSTOPB;
326 break;
327 case OnePointFive: /* OnePointFive */
328 /* XXX unhandled */
329 break;
332 /* parity */
333 newtio.c_iflag &= ~(INPCK | ISTRIP );
335 switch (parity)
337 case NoneParity: /* None */
338 newtio.c_cflag &= ~(PARENB | PARODD);
339 break;
341 case Odd: /* Odd */
342 newtio.c_cflag |= PARENB | PARODD;
343 break;
345 case Even: /* Even */
346 newtio.c_cflag &= ~(PARODD);
347 newtio.c_cflag |= (PARENB);
348 break;
350 case Mark: /* Mark */
351 /* XXX unhandled */
352 break;
353 case Space: /* Space */
354 /* XXX unhandled */
355 break;
358 newtio.c_iflag &= ~(IXOFF | IXON);
359 #ifdef CRTSCTS
360 newtio.c_cflag &= ~CRTSCTS;
361 #endif /* def CRTSCTS */
363 switch (handshake)
365 case NoneHandshake: /* None */
366 /* do nothing */
367 break;
368 case RequestToSend: /* RequestToSend (RTS) */
369 #ifdef CRTSCTS
370 newtio.c_cflag |= CRTSCTS;
371 #endif /* def CRTSCTS */
372 break;
373 case RequestToSendXOnXOff: /* RequestToSendXOnXOff (RTS + XON/XOFF) */
374 #ifdef CRTSCTS
375 newtio.c_cflag |= CRTSCTS;
376 #endif /* def CRTSCTS */
377 /* fall through */
378 case XOnXOff: /* XOnXOff */
379 newtio.c_iflag |= IXOFF | IXON;
380 break;
383 if (custom_baud_rate == FALSE) {
384 if (cfsetospeed (&newtio, baud_rate) < 0 || cfsetispeed (&newtio, baud_rate) < 0)
385 return FALSE;
386 } else {
387 #if __linux__ || (defined(__APPLE__) && !defined(HOST_IOS))
389 /* On Linux to set a custom baud rate, we must set the
390 * "standard" baud_rate to 38400. On Apple we set it purely
391 * so that tcsetattr has something to use (and report back later), but
392 * the Apple specific API is still opaque to these APIs, see:
393 * https://developer.apple.com/library/mac/samplecode/SerialPortSample/Listings/SerialPortSample_SerialPortSample_c.html#//apple_ref/doc/uid/DTS10000454-SerialPortSample_SerialPortSample_c-DontLinkElementID_4
395 if (cfsetospeed (&newtio, B38400) < 0 || cfsetispeed (&newtio, B38400) < 0)
396 return FALSE;
397 #endif
400 if (tcsetattr (fd, TCSANOW, &newtio) < 0)
401 return FALSE;
403 if (custom_baud_rate == TRUE){
404 #if defined(HAVE_LINUX_SERIAL_H)
405 struct serial_struct ser;
407 if (ioctl (fd, TIOCGSERIAL, &ser) < 0)
409 return FALSE;
412 ser.custom_divisor = ser.baud_base / baud_rate;
413 ser.flags &= ~ASYNC_SPD_MASK;
414 ser.flags |= ASYNC_SPD_CUST;
416 if (ioctl (fd, TIOCSSERIAL, &ser) < 0)
418 return FALSE;
420 #elif defined(__APPLE__) && !defined(HOST_IOS)
421 speed_t speed = baud_rate;
422 if (ioctl(fd, IOSSIOSPEED, &speed) == -1)
423 return FALSE;
424 #else
425 /* Don't know how to set custom baud rate on this platform. */
426 return FALSE;
427 #endif
430 return TRUE;
434 static gint32
435 get_signal_code (MonoSerialSignal signal)
437 switch (signal) {
438 case Cd:
439 return TIOCM_CAR;
440 case Cts:
441 return TIOCM_CTS;
442 case Dsr:
443 return TIOCM_DSR;
444 case Dtr:
445 return TIOCM_DTR;
446 case Rts:
447 return TIOCM_RTS;
448 default:
449 return 0;
452 /* Not reached */
453 return 0;
456 static MonoSerialSignal
457 get_mono_signal_codes (int signals)
459 MonoSerialSignal retval = NoneSignal;
461 if ((signals & TIOCM_CAR) != 0)
462 retval |= Cd;
463 if ((signals & TIOCM_CTS) != 0)
464 retval |= Cts;
465 if ((signals & TIOCM_DSR) != 0)
466 retval |= Dsr;
467 if ((signals & TIOCM_DTR) != 0)
468 retval |= Dtr;
469 if ((signals & TIOCM_RTS) != 0)
470 retval |= Rts;
472 return retval;
475 MonoSerialSignal
476 get_signals (int fd, gint32 *error)
478 int signals;
480 *error = 0;
482 if (ioctl (fd, TIOCMGET, &signals) == -1) {
483 *error = -1;
484 return NoneSignal;
487 return get_mono_signal_codes (signals);
490 gint32
491 set_signal (int fd, MonoSerialSignal signal, gboolean value)
493 int signals, expected, activated;
495 expected = get_signal_code (signal);
496 if (ioctl (fd, TIOCMGET, &signals) == -1)
497 return -1;
499 activated = (signals & expected) != 0;
500 if (activated == value) /* Already set */
501 return 1;
503 if (value)
504 signals |= expected;
505 else
506 signals &= ~expected;
508 if (ioctl (fd, TIOCMSET, &signals) == -1)
509 return -1;
511 return 1;
515 breakprop (int fd)
517 return tcsendbreak (fd, 0);
520 gboolean
521 poll_serial (int fd, gint32 *error, int timeout)
523 struct pollfd pinfo;
525 *error = 0;
527 pinfo.fd = fd;
528 pinfo.events = POLLIN;
529 pinfo.revents = 0;
531 while (poll (&pinfo, 1, timeout) == -1 && errno == EINTR) {
532 /* EINTR is an OK condition, we should not throw in the upper layer an IOException */
533 if (errno != EINTR){
534 *error = -1;
535 return FALSE;
539 return (pinfo.revents & POLLIN) != 0 ? 1 : 0;
543 * mono internals should not be used here.
544 * this serial stuff needs to be implemented with icalls.
545 * make this at least compile until the code is moved elsewhere
546 * defined(linux) is wrong, too
548 void*
549 list_serial_devices (void)
551 return NULL;
554 #if 0
555 MonoArray *
556 list_serial_devices (void)
558 MonoArray *array;
559 #if defined(linux)
560 /* Linux serial files are of the form ttyS[0-9]+ */
561 GSList *l, *list = NULL;
562 GDir* dir = g_dir_open ("/dev", 0, NULL);
563 const char *filename;
564 int i = 0;
566 while ((filename = g_dir_read_name (dir))) {
567 if (filename) {
568 if (!strncmp (filename, "ttyS", 4))
569 list = g_slist_append (list, g_strconcat ("/dev/", filename, NULL));
573 g_dir_close (dir);
575 array = mono_array_new (mono_domain_get (), mono_get_string_class (), g_slist_length (list));
576 for (l = list; l; l = l->next) {
577 mono_array_set (array, gpointer, i++, mono_string_new (mono_domain_get (), (char*)l->data));
578 g_free (l->data);
581 g_slist_free (list);
583 #else
584 #warning "list_serial_devices isn't ported to this OS"
585 #endif
587 return array;
589 #endif