2008-04-18 Miguel de Icaza <miguel@novell.com>
[mono/afaerber.git] / support / serial.c
blobed6cd47947c4f2f2ec7226d90dbcef2cb5a323ce
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 /* sys/time.h (for timeval) is required when using osx 10.3 (but not 10.4) */
24 #ifdef __APPLE__
25 #include <sys/time.h>
26 #endif
28 /* This is a copy of System.IO.Ports.Handshake */
29 typedef enum {
30 NoneHandshake = 0,
31 XOnXOff = 1,
32 RequestToSend = 2,
33 RequestToSendXOnXOff = 3
34 } MonoHandshake;
36 /* This is a copy of System.IO.Ports.Parity */
37 typedef enum {
38 NoneParity = 0,
39 Odd = 1,
40 Even = 2,
41 Mark = 3,
42 Space = 4
43 } MonoParity;
45 /* This is a copy of System.IO.Ports.StopBits */
46 typedef enum {
47 NoneStopBits = 0,
48 One = 1,
49 Two = 2,
50 OnePointFive = 3
51 } MonoStopBits;
53 /* This is a copy of System.IO.Ports.SerialSignal */
54 typedef enum {
55 NoneSignal,
56 Cd = 1, /* Carrier detect */
57 Cts = 2, /* Clear to send */
58 Dsr = 4, /* Data set ready */
59 Dtr = 8, /* Data terminal ready */
60 Rts = 16 /* Request to send */
61 } MonoSerialSignal;
63 int
64 open_serial (char* devfile)
66 int fd;
67 fd = open (devfile, O_RDWR | O_NOCTTY | O_NONBLOCK);
69 if (fd == -1)
70 return -1;
72 return fd;
75 void
76 close_serial (int unix_fd)
78 close (unix_fd);
81 guint32
82 read_serial (int fd, guchar *buffer, int offset, int count)
84 guint32 n;
86 n = read (fd, buffer + offset, count);
88 return (guint32) n;
91 int
92 write_serial (int fd, guchar *buffer, int offset, int count, int timeout)
94 struct pollfd pinfo;
95 guint32 n;
97 pinfo.fd = fd;
98 pinfo.events = POLLOUT;
99 pinfo.revents = POLLOUT;
101 n = count;
103 while (n > 0)
105 size_t t;
107 if (timeout > 0) {
108 int c;
110 while ((c = poll (&pinfo, 1, timeout)) == -1 && errno == EINTR)
112 if (c == -1)
113 return -1;
116 do {
117 t = write (fd, buffer + offset, n);
118 } while (t == -1 && errno == EINTR);
120 if (t < 0)
121 return -1;
123 offset += t;
124 n -= t;
127 return 0;
130 void
131 discard_buffer (int fd, gboolean input)
133 tcflush(fd, input ? TCIFLUSH : TCOFLUSH);
136 gint32
137 get_bytes_in_buffer (int fd, gboolean input)
139 gint32 retval;
141 if (ioctl (fd, input ? FIONREAD : TIOCOUTQ, &retval) == -1) {
142 return -1;
145 return retval;
148 gboolean
149 set_attributes (int fd, int baud_rate, MonoParity parity, int dataBits, MonoStopBits stopBits, MonoHandshake handshake)
151 struct termios newtio;
153 tcgetattr (fd, &newtio);
154 newtio.c_cflag |= (CLOCAL | CREAD);
155 newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ISIG | IEXTEN );
156 newtio.c_oflag &= ~(OPOST);
157 newtio.c_iflag = IGNBRK;
159 /* setup baudrate */
160 switch (baud_rate)
162 case 230400:
163 baud_rate = B230400;
164 break;
165 case 115200:
166 baud_rate = B115200;
167 break;
168 case 57600:
169 baud_rate = B57600;
170 break;
171 case 38400:
172 baud_rate = B38400;
173 break;
174 case 19200:
175 baud_rate = B19200;
176 break;
177 case 9600:
178 baud_rate = B9600;
179 break;
180 case 4800:
181 baud_rate = B4800;
182 break;
183 case 2400:
184 baud_rate = B2400;
185 break;
186 case 1800:
187 baud_rate = B1800;
188 break;
189 case 1200:
190 baud_rate = B1200;
191 break;
192 case 600:
193 baud_rate = B600;
194 break;
195 case 300:
196 baud_rate = B300;
197 break;
198 case 200:
199 baud_rate = B200;
200 break;
201 case 150:
202 baud_rate = B150;
203 break;
204 case 134:
205 baud_rate = B134;
206 break;
207 case 110:
208 baud_rate = B110;
209 break;
210 case 75:
211 baud_rate = B75;
212 break;
213 case 50:
214 case 0:
215 default:
216 baud_rate = B9600;
217 break;
220 /* char lenght */
221 newtio.c_cflag &= ~CSIZE;
222 switch (dataBits)
224 case 5:
225 newtio.c_cflag |= CS5;
226 break;
227 case 6:
228 newtio.c_cflag |= CS6;
229 break;
230 case 7:
231 newtio.c_cflag |= CS7;
232 break;
233 case 8:
234 default:
235 newtio.c_cflag |= CS8;
236 break;
239 /* stopbits */
240 switch (stopBits)
242 case NoneStopBits:
243 /* Unhandled */
244 break;
245 case One: /* One */
246 /* do nothing, the default is one stop bit */
247 newtio.c_cflag &= ~CSTOPB;
248 break;
249 case Two: /* Two */
250 newtio.c_cflag |= CSTOPB;
251 break;
252 case OnePointFive: /* OnePointFive */
253 /* XXX unhandled */
254 break;
257 /* parity */
258 newtio.c_iflag &= ~(INPCK | ISTRIP );
260 switch (parity)
262 case NoneParity: /* None */
263 newtio.c_cflag &= ~(PARENB | PARODD);
264 break;
266 case Odd: /* Odd */
267 newtio.c_cflag |= PARENB | PARODD;
268 break;
270 case Even: /* Even */
271 newtio.c_cflag &= ~(PARODD);
272 break;
274 case Mark: /* Mark */
275 /* XXX unhandled */
276 break;
277 case Space: /* Space */
278 /* XXX unhandled */
279 break;
282 newtio.c_iflag &= ~(IXOFF | IXON);
283 #ifdef CRTSCTS
284 newtio.c_cflag &= ~CRTSCTS;
285 #endif /* def CRTSCTS */
287 switch (handshake)
289 case NoneHandshake: /* None */
290 /* do nothing */
291 break;
292 case RequestToSend: /* RequestToSend (RTS) */
293 #ifdef CRTSCTS
294 newtio.c_cflag |= CRTSCTS;
295 #endif /* def CRTSCTS */
296 break;
297 case RequestToSendXOnXOff: /* RequestToSendXOnXOff (RTS + XON/XOFF) */
298 #ifdef CRTSCTS
299 newtio.c_cflag |= CRTSCTS;
300 #endif /* def CRTSCTS */
301 /* fall through */
302 case XOnXOff: /* XOnXOff */
303 newtio.c_iflag |= IXOFF | IXON;
304 break;
307 if (cfsetospeed (&newtio, baud_rate) < 0 || cfsetispeed (&newtio, baud_rate) < 0 ||
308 tcsetattr (fd, TCSANOW, &newtio) < 0)
310 return FALSE;
312 else
314 return TRUE;
319 static gint32
320 get_signal_code (MonoSerialSignal signal)
322 switch (signal) {
323 case Cd:
324 return TIOCM_CAR;
325 case Cts:
326 return TIOCM_CTS;
327 case Dsr:
328 return TIOCM_DSR;
329 case Dtr:
330 return TIOCM_DTR;
331 case Rts:
332 return TIOCM_RTS;
333 default:
334 return 0;
337 /* Not reached */
338 return 0;
341 static MonoSerialSignal
342 get_mono_signal_codes (int signals)
344 MonoSerialSignal retval = NoneSignal;
346 if ((signals & TIOCM_CAR) != 0)
347 retval |= Cd;
348 if ((signals & TIOCM_CTS) != 0)
349 retval |= Cts;
350 if ((signals & TIOCM_DSR) != 0)
351 retval |= Dsr;
352 if ((signals & TIOCM_DTR) != 0)
353 retval |= Dtr;
354 if ((signals & TIOCM_RTS) != 0)
355 retval |= Rts;
357 return retval;
360 MonoSerialSignal
361 get_signals (int fd, gint32 *error)
363 int signals;
365 *error = 0;
367 if (ioctl (fd, TIOCMGET, &signals) == -1) {
368 *error = -1;
369 return NoneSignal;
372 return get_mono_signal_codes (signals);
375 gint32
376 set_signal (int fd, MonoSerialSignal signal, gboolean value)
378 int signals, expected, activated;
380 expected = get_signal_code (signal);
381 if (ioctl (fd, TIOCMGET, &signals) == -1)
382 return -1;
384 activated = (signals & expected) != 0;
385 if (activated == value) /* Already set */
386 return 1;
388 if (value)
389 signals |= expected;
390 else
391 signals &= ~expected;
393 if (ioctl (fd, TIOCMSET, &signals) == -1)
394 return -1;
396 return 1;
399 void
400 breakprop (int fd)
402 tcsendbreak (fd, 0);
405 gboolean
406 poll_serial (int fd, gint32 *error, int timeout)
408 struct pollfd pinfo;
410 *error = 0;
412 pinfo.fd = fd;
413 pinfo.events = POLLIN;
414 pinfo.revents = 0;
416 while (poll (&pinfo, 1, timeout) == -1 && errno == EINTR) {
417 /* EINTR is an OK condition, we should not throw in the upper layer an IOException */
418 if (errno != EINTR){
419 *error = -1;
420 return FALSE;
424 return (pinfo.revents & POLLIN) != 0 ? 1 : 0;
428 * mono internals should not be used here.
429 * this serial stuff needs to be implemented with icalls.
430 * make this at least compile until the code is moved elsewhere
431 * defined(linux) is wrong, too
433 void*
434 list_serial_devices (void)
436 return NULL;
439 #if 0
440 MonoArray *
441 list_serial_devices (void)
443 MonoArray *array;
444 #if defined(linux)
445 /* Linux serial files are of the form ttyS[0-9]+ */
446 GSList *l, *list = NULL;
447 GDir* dir = g_dir_open ("/dev", 0, NULL);
448 const char *filename;
449 int i = 0;
451 while ((filename = g_dir_read_name (dir))) {
452 if (filename) {
453 if (!strncmp (filename, "ttyS", 4))
454 list = g_slist_append (list, g_strconcat ("/dev/", filename, NULL));
458 g_dir_close (dir);
460 array = mono_array_new (mono_domain_get (), mono_get_string_class (), g_slist_length (list));
461 for (l = list; l; l = l->next) {
462 mono_array_set (array, gpointer, i++, mono_string_new (mono_domain_get (), (char*)l->data));
463 g_free (l->data);
466 g_slist_free (list);
468 #else
469 #warning "list_serial_devices isn't ported to this OS"
470 #endif
472 return array;
474 #endif