svn path=/branches/mono-1-1-9/mcs/; revision=51214
[mono.git] / support / serial.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
3 /* serial port functions
4  *
5  * Author: Chris Toshok <toshok@ximian.com>
6  */
7
8 #include <termios.h>
9 #include <unistd.h>
10 #include <fcntl.h>
11 #include <string.h>
12 #include <sys/poll.h>
13
14 #include <glib.h>
15
16 int
17 open_serial (char* devfile)
18 {
19         int fd;
20         struct termios newtio;
21
22         fd = open (devfile, O_RDWR);
23
24         if (fd == -1)
25                 return -1;
26
27         newtio.c_cflag = CLOCAL | CREAD;
28         newtio.c_iflag = 0;
29         newtio.c_oflag = 0;
30         newtio.c_lflag = 0;
31
32         tcflush(fd, TCIOFLUSH);
33         tcsetattr(fd,TCSANOW,&newtio);
34
35         fcntl (fd, F_SETFL, O_NONBLOCK);
36
37         return fd;
38 }
39
40 void
41 close_serial (int unix_fd)
42 {
43         close (unix_fd);
44 }
45
46 guint32
47 read_serial (int fd, guchar *buffer, int offset, int count, int timeout)
48 {
49         guint32 n;
50         struct pollfd ufd;
51
52         ufd.fd = fd;
53         ufd.events = POLLHUP | POLLIN | POLLERR;
54
55         poll (&ufd, 1, timeout);
56
57         if ((ufd.revents & POLLIN) != POLLIN) {
58                 return -1;
59         }
60  
61         n = read (fd, buffer + offset, count);
62
63         return (guint32) n;
64 }
65
66 void
67 write_serial (int fd, guchar *buffer, int offset, int count, int timeout)
68 {
69         guint32 n;
70
71         struct pollfd ufd;
72
73         ufd.fd = fd;
74         ufd.events = POLLHUP | POLLOUT | POLLERR;
75
76         poll (&ufd, 1, timeout);
77
78         if ((ufd.revents & POLLOUT) != POLLOUT) {
79                 return;
80         }
81  
82         n = write (fd, buffer + offset, count);
83 }
84
85 void
86 discard_buffer (int fd, gboolean input)
87 {
88         tcflush(fd, input ? TCIFLUSH : TCOFLUSH);
89 }
90
91 gboolean
92 set_attributes (int fd, int baud_rate, int parity, int dataBits, int stopBits, int handshake)
93 {
94         struct termios newtio;
95
96         tcgetattr (fd, &newtio);
97
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;
121         }
122
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;
142         }
143
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;
153         }
154
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;
166         }
167
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;
191         }
192         
193         cfsetospeed (&newtio, baud_rate);
194         cfsetispeed (&newtio, baud_rate);
195
196         tcsetattr(fd,TCSADRAIN,&newtio);
197
198         return TRUE;
199 }
200
201 /*
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
206  */
207 void*
208 list_serial_devices (void)
209 {
210         return NULL;
211 }
212
213 #if 0
214 MonoArray *
215 list_serial_devices (void)
216 {
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;
224
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));
229                 }
230         }
231
232         g_dir_close (dir);
233   
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);
238         }
239
240         g_slist_free (list);
241
242 #else
243 #warning "list_serial_devices isn't ported to this OS"
244 #endif
245
246         return array;
247 }
248 #endif
249