2005-11-06 Zoltan Varga <vargaz@freemail.hu>
[mono-project.git] / support / serial.c
blob8ae46b8a6bfcfd110ee0e63d4bfd512854d1668e
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>
14 #include <glib.h>
16 int
17 open_serial (char* devfile)
19 int fd;
20 struct termios newtio;
22 fd = open (devfile, O_RDWR);
24 if (fd == -1)
25 return -1;
27 newtio.c_cflag = CLOCAL | CREAD;
28 newtio.c_iflag = 0;
29 newtio.c_oflag = 0;
30 newtio.c_lflag = 0;
32 tcflush(fd, TCIOFLUSH);
33 tcsetattr(fd,TCSANOW,&newtio);
35 fcntl (fd, F_SETFL, O_NONBLOCK);
37 return fd;
40 void
41 close_serial (int unix_fd)
43 close (unix_fd);
46 guint32
47 read_serial (int fd, guchar *buffer, int offset, int count, int timeout)
49 guint32 n;
50 struct pollfd ufd;
52 ufd.fd = fd;
53 ufd.events = POLLHUP | POLLIN | POLLERR;
55 poll (&ufd, 1, timeout);
57 if ((ufd.revents & POLLIN) != POLLIN) {
58 return -1;
61 n = read (fd, buffer + offset, count);
63 return (guint32) n;
66 void
67 write_serial (int fd, guchar *buffer, int offset, int count, int timeout)
69 guint32 n;
71 struct pollfd ufd;
73 ufd.fd = fd;
74 ufd.events = POLLHUP | POLLOUT | POLLERR;
76 poll (&ufd, 1, timeout);
78 if ((ufd.revents & POLLOUT) != POLLOUT) {
79 return;
82 n = write (fd, buffer + offset, count);
85 void
86 discard_buffer (int fd, gboolean input)
88 tcflush(fd, input ? TCIFLUSH : TCOFLUSH);
91 gboolean
92 set_attributes (int fd, int baud_rate, int parity, int dataBits, int stopBits, int handshake)
94 struct termios newtio;
96 tcgetattr (fd, &newtio);
98 switch (baud_rate) {
99 case 230400: baud_rate = B230400; break;
100 case 115200: baud_rate = B115200; break;
101 case 57600: baud_rate = B57600; break;
102 case 38400: baud_rate = B38400; break;
103 case 19200: baud_rate = B19200; break;
104 case 9600: baud_rate = B9600; break;
105 case 4800: baud_rate = B4800; break;
106 case 2400: baud_rate = B2400; break;
107 case 1800: baud_rate = B1800; break;
108 case 1200: baud_rate = B1200; break;
109 case 600: baud_rate = B600; break;
110 case 300: baud_rate = B300; break;
111 case 200: baud_rate = B200; break;
112 case 150: baud_rate = B150; break;
113 case 134: baud_rate = B134; break;
114 case 110: baud_rate = B110; break;
115 case 75: baud_rate = B75; break;
116 case 50:
117 case 0:
118 default:
119 baud_rate = B9600;
120 break;
123 switch (parity) {
124 case 0: /* Even */
125 newtio.c_iflag &= ~IGNPAR;
126 newtio.c_cflag |= PARENB;
127 break;
128 case 1: /* Mark */
129 /* XXX unhandled */
130 break;
131 case 2: /* None */
132 newtio.c_iflag |= IGNPAR;
133 newtio.c_cflag &= ~(PARENB | PARODD);
134 break;
135 case 3: /* Odd */
136 newtio.c_iflag &= ~IGNPAR;
137 newtio.c_cflag |= PARENB | PARODD;
138 break;
139 case 4: /* Space */
140 /* XXX unhandled */
141 break;
144 newtio.c_cflag &= ~CSIZE;
145 switch (dataBits) {
146 case 5: newtio.c_cflag |= CS5; break;
147 case 6: newtio.c_cflag |= CS6; break;
148 case 7: newtio.c_cflag |= CS7; break;
149 case 8:
150 default:
151 newtio.c_cflag |= CS8;
152 break;
155 newtio.c_cflag &= ~CSTOPB;
156 switch (stopBits) {
157 case 0: /* One */
158 /* do nothing, the default is one stop bit */
159 break;
160 case 1: /* OnePointFive */
161 /* XXX unhandled */
162 break;
163 case 2: /* Two */
164 newtio.c_cflag |= CSTOPB;
165 break;
168 newtio.c_iflag &= ~IXOFF;
169 newtio.c_oflag &= ~IXON;
170 #ifdef CRTSCTS
171 newtio.c_cflag &= ~CRTSCTS;
172 #endif /* def CRTSCTS */
173 switch (handshake) {
174 case 0: /* None */
175 /* do nothing */
176 break;
177 case 1: /* RequestToSend (RTS) */
178 #ifdef CRTSCTS
179 newtio.c_cflag |= CRTSCTS;
180 #endif /* def CRTSCTS */
181 break;
182 case 2: /* RequestToSendXOnXOff (RTS + XON/XOFF) */
183 #ifdef CRTSCTS
184 newtio.c_cflag |= CRTSCTS;
185 #endif /* def CRTSCTS */
186 /* fall through */
187 case 3: /* XOnXOff */
188 newtio.c_iflag |= IXOFF;
189 // newtio.c_oflag |= IXON;
190 break;
193 cfsetospeed (&newtio, baud_rate);
194 cfsetispeed (&newtio, baud_rate);
196 tcsetattr(fd,TCSADRAIN,&newtio);
198 return TRUE;
202 * mono internals should not be used here.
203 * this serial stuff needs to be implemented with icalls.
204 * make this at least compile until the code is moved elsewhere
205 * defined(linux) is wrong, too
207 void*
208 list_serial_devices (void)
210 return NULL;
213 #if 0
214 MonoArray *
215 list_serial_devices (void)
217 MonoArray *array;
218 #if defined(linux)
219 /* Linux serial files are of the form ttyS[0-9]+ */
220 GSList *l, *list = NULL;
221 GDir* dir = g_dir_open ("/dev", 0, NULL);
222 const char *filename;
223 int i = 0;
225 while ((filename = g_dir_read_name (dir))) {
226 if (filename) {
227 if (!strncmp (filename, "ttyS", 4))
228 list = g_slist_append (list, g_strconcat ("/dev/", filename, NULL));
232 g_dir_close (dir);
234 array = mono_array_new (mono_domain_get (), mono_get_string_class (), g_slist_length (list));
235 for (l = list; l; l = l->next) {
236 mono_array_set (array, gpointer, i++, mono_string_new (mono_domain_get (), (char*)l->data));
237 g_free (l->data);
240 g_slist_free (list);
242 #else
243 #warning "list_serial_devices isn't ported to this OS"
244 #endif
246 return array;
248 #endif