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>
13 #include <sys/ioctl.h>
17 /* This is for FIONREAD on solaris */
19 #include <sys/filio.h>
22 /* This is a copy of System.IO.Ports.Handshake */
27 RequestToSendXOnXOff
= 3
30 /* This is a copy of System.IO.Ports.Parity */
39 /* This is a copy of System.IO.Ports.StopBits */
47 /* This is a copy of System.IO.Ports.SerialSignal */
50 Cd
= 1, /* Carrier detect */
51 Cts
= 2, /* Clear to send */
52 Dsr
= 4, /* Data set ready */
53 Dtr
= 8, /* Data terminal ready */
54 Rts
= 16 /* Request to send */
58 open_serial (char* devfile
)
61 struct termios newtio
;
63 fd
= open (devfile
, O_RDWR
);
68 newtio
.c_cflag
= CLOCAL
| CREAD
;
73 tcflush(fd
, TCIOFLUSH
);
74 tcsetattr(fd
,TCSANOW
,&newtio
);
80 close_serial (int unix_fd
)
86 read_serial (int fd
, guchar
*buffer
, int offset
, int count
)
90 n
= read (fd
, buffer
+ offset
, count
);
96 write_serial (int fd
, guchar
*buffer
, int offset
, int count
, int timeout
)
103 ufd
.events
= POLLHUP
| POLLOUT
| POLLERR
;
105 poll (&ufd
, 1, timeout
);
107 if ((ufd
.revents
& POLLOUT
) != POLLOUT
) {
111 n
= write (fd
, buffer
+ offset
, count
);
115 discard_buffer (int fd
, gboolean input
)
117 tcflush(fd
, input
? TCIFLUSH
: TCOFLUSH
);
121 get_bytes_in_buffer (int fd
, gboolean input
)
125 if (ioctl (fd
, input
? FIONREAD
: TIOCOUTQ
, &retval
) == -1) {
133 set_attributes (int fd
, int baud_rate
, MonoParity parity
, int dataBits
, MonoStopBits stopBits
, MonoHandshake handshake
)
135 struct termios newtio
;
137 tcgetattr (fd
, &newtio
);
140 case 230400: baud_rate
= B230400
; break;
141 case 115200: baud_rate
= B115200
; break;
142 case 57600: baud_rate
= B57600
; break;
143 case 38400: baud_rate
= B38400
; break;
144 case 19200: baud_rate
= B19200
; break;
145 case 9600: baud_rate
= B9600
; break;
146 case 4800: baud_rate
= B4800
; break;
147 case 2400: baud_rate
= B2400
; break;
148 case 1800: baud_rate
= B1800
; break;
149 case 1200: baud_rate
= B1200
; break;
150 case 600: baud_rate
= B600
; break;
151 case 300: baud_rate
= B300
; break;
152 case 200: baud_rate
= B200
; break;
153 case 150: baud_rate
= B150
; break;
154 case 134: baud_rate
= B134
; break;
155 case 110: baud_rate
= B110
; break;
156 case 75: baud_rate
= B75
; break;
165 case NoneParity
: /* None */
166 newtio
.c_iflag
|= IGNPAR
;
167 newtio
.c_cflag
&= ~(PARENB
| PARODD
);
170 newtio
.c_iflag
&= ~IGNPAR
;
171 newtio
.c_cflag
|= PARENB
| PARODD
;
173 case Even
: /* Even */
174 newtio
.c_iflag
&= ~(IGNPAR
| PARODD
);
175 newtio
.c_cflag
|= PARENB
;
177 case Mark
: /* Mark */
180 case Space
: /* Space */
185 newtio
.c_cflag
&= ~CSIZE
;
187 case 5: newtio
.c_cflag
|= CS5
; break;
188 case 6: newtio
.c_cflag
|= CS6
; break;
189 case 7: newtio
.c_cflag
|= CS7
; break;
192 newtio
.c_cflag
|= CS8
;
196 newtio
.c_cflag
&= ~CSTOPB
;
202 /* do nothing, the default is one stop bit */
205 newtio
.c_cflag
|= CSTOPB
;
207 case OnePointFive
: /* OnePointFive */
212 newtio
.c_iflag
&= ~IXOFF
;
213 newtio
.c_oflag
&= ~IXON
;
215 newtio
.c_cflag
&= ~CRTSCTS
;
216 #endif /* def CRTSCTS */
218 case NoneHandshake
: /* None */
221 case RequestToSend
: /* RequestToSend (RTS) */
223 newtio
.c_cflag
|= CRTSCTS
;
224 #endif /* def CRTSCTS */
226 case RequestToSendXOnXOff
: /* RequestToSendXOnXOff (RTS + XON/XOFF) */
228 newtio
.c_cflag
|= CRTSCTS
;
229 #endif /* def CRTSCTS */
231 case XOnXOff
: /* XOnXOff */
232 newtio
.c_iflag
|= IXOFF
;
233 // newtio.c_oflag |= IXON;
237 if (cfsetospeed (&newtio
, baud_rate
) < 0 || cfsetispeed (&newtio
, baud_rate
) < 0 ||
238 tcsetattr (fd
, TCSADRAIN
, &newtio
) < 0)
245 get_signal_code (MonoSerialSignal signal
)
266 static MonoSerialSignal
267 get_mono_signal_codes (int signals
)
269 MonoSerialSignal retval
= NoneSignal
;
271 if ((signals
& TIOCM_CAR
) != 0)
273 if ((signals
& TIOCM_CTS
) != 0)
275 if ((signals
& TIOCM_DSR
) != 0)
277 if ((signals
& TIOCM_DTR
) != 0)
279 if ((signals
& TIOCM_RTS
) != 0)
286 get_signals (int fd
, gint32
*error
)
292 if (ioctl (fd
, TIOCMGET
, &signals
) == -1) {
297 return get_mono_signal_codes (signals
);
301 set_signal (int fd
, MonoSerialSignal signal
, gboolean value
)
303 int signals
, expected
, activated
;
305 expected
= get_signal_code (signal
);
306 if (ioctl (fd
, TIOCMGET
, &signals
) == -1)
309 activated
= (signals
& expected
) != 0;
310 if (activated
== value
) /* Already set */
316 signals
&= ~expected
;
318 if (ioctl (fd
, TIOCMSET
, &signals
) == -1)
325 poll_serial (int fd
, gint32
*error
)
332 pinfo
.events
= POLLIN
;
335 if (poll (&pinfo
, 1, 0) == -1) {
340 return (pinfo
.revents
& POLLIN
) != 0 ? 1 : 0;
344 * mono internals should not be used here.
345 * this serial stuff needs to be implemented with icalls.
346 * make this at least compile until the code is moved elsewhere
347 * defined(linux) is wrong, too
350 list_serial_devices (void)
357 list_serial_devices (void)
361 /* Linux serial files are of the form ttyS[0-9]+ */
362 GSList
*l
, *list
= NULL
;
363 GDir
* dir
= g_dir_open ("/dev", 0, NULL
);
364 const char *filename
;
367 while ((filename
= g_dir_read_name (dir
))) {
369 if (!strncmp (filename
, "ttyS", 4))
370 list
= g_slist_append (list
, g_strconcat ("/dev/", filename
, NULL
));
376 array
= mono_array_new (mono_domain_get (), mono_get_string_class (), g_slist_length (list
));
377 for (l
= list
; l
; l
= l
->next
) {
378 mono_array_set (array
, gpointer
, i
++, mono_string_new (mono_domain_get (), (char*)l
->data
));
385 #warning "list_serial_devices isn't ported to this OS"