Added a check for poll.h.
[wine] / server / serial.c
1 /*
2  * Server-side serial port communications management
3  *
4  * Copyright (C) 1998 Alexandre Julliard
5  * Copyright (C) 2000,2001 Mike McCormack
6  *
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <assert.h>
27 #include <fcntl.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <sys/time.h>
33 #include <sys/types.h>
34 #include <time.h>
35 #include <unistd.h>
36 #ifdef HAVE_UTIME_H
37 #include <utime.h>
38 #endif
39 #ifdef HAVE_TERMIOS_H
40 #include <termios.h>
41 #endif
42 #ifdef HAVE_SYS_IOCTL_H
43 #include <sys/ioctl.h>
44 #endif
45 #ifdef HAVE_POLL_H
46 #include <poll.h>
47 #endif
48
49 #include "winerror.h"
50 #include "windef.h"
51 #include "winbase.h"
52 #include "winreg.h"
53 #include "winternl.h"
54
55 #include "file.h"
56 #include "handle.h"
57 #include "thread.h"
58 #include "request.h"
59
60 static void serial_dump( struct object *obj, int verbose );
61 static struct fd *serial_get_fd( struct object *obj );
62 static void serial_destroy(struct object *obj);
63
64 static int serial_get_poll_events( struct fd *fd );
65 static void serial_poll_event( struct fd *fd, int event );
66 static int serial_get_info( struct fd *fd );
67 static int serial_flush( struct fd *fd, struct event **event );
68 static void serial_queue_async( struct fd *fd, void *apc, void *user, void *iosb, int type, int count );
69 static void serial_cancel_async( struct fd *fd );
70
71 struct serial
72 {
73     struct object       obj;
74     struct fd          *fd;
75     unsigned int        options;
76
77     /* timeout values */
78     unsigned int        readinterval;
79     unsigned int        readconst;
80     unsigned int        readmult;
81     unsigned int        writeconst;
82     unsigned int        writemult;
83
84     unsigned int        eventmask;
85     unsigned int        commerror;
86
87     struct termios      original;
88
89     struct list         read_q;
90     struct list         write_q;
91     struct list         wait_q;
92
93     /* FIXME: add dcb, comm status, handler module, sharing */
94 };
95
96 static const struct object_ops serial_ops =
97 {
98     sizeof(struct serial),        /* size */
99     serial_dump,                  /* dump */
100     default_fd_add_queue,         /* add_queue */
101     default_fd_remove_queue,      /* remove_queue */
102     default_fd_signaled,          /* signaled */
103     no_satisfied,                 /* satisfied */
104     serial_get_fd,                /* get_fd */
105     serial_destroy                /* destroy */
106 };
107
108 static const struct fd_ops serial_fd_ops =
109 {
110     serial_get_poll_events,       /* get_poll_events */
111     serial_poll_event,            /* poll_event */
112     serial_flush,                 /* flush */
113     serial_get_info,              /* get_file_info */
114     serial_queue_async,           /* queue_async */
115     serial_cancel_async           /* cancel_async */
116 };
117
118 /* check if the given fd is a serial port */
119 int is_serial_fd( struct fd *fd )
120 {
121     struct termios tios;
122
123     return !tcgetattr( get_unix_fd(fd), &tios );
124 }
125
126 /* create a serial object for a given fd */
127 struct object *create_serial( struct fd *fd, unsigned int options )
128 {
129     struct serial *serial;
130     int unix_fd;
131
132     if ((unix_fd = dup( get_unix_fd(fd) )) == -1) return NULL;
133
134     /* set the fd back to blocking if necessary */
135     if (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT))
136         fcntl( unix_fd, F_SETFL, 0 );
137
138     if (!(serial = alloc_object( &serial_ops )))
139     {
140         close( unix_fd );
141         return NULL;
142     }
143     serial->options      = options;
144     serial->readinterval = 0;
145     serial->readmult     = 0;
146     serial->readconst    = 0;
147     serial->writemult    = 0;
148     serial->writeconst   = 0;
149     serial->eventmask    = 0;
150     serial->commerror    = 0;
151     list_init( &serial->read_q );
152     list_init( &serial->write_q );
153     list_init( &serial->wait_q );
154     if (!(serial->fd = create_anonymous_fd( &serial_fd_ops, unix_fd, &serial->obj )))
155     {
156         release_object( serial );
157         return NULL;
158     }
159     return &serial->obj;
160 }
161
162 static struct fd *serial_get_fd( struct object *obj )
163 {
164     struct serial *serial = (struct serial *)obj;
165     return (struct fd *)grab_object( serial->fd );
166 }
167
168 static void serial_destroy( struct object *obj)
169 {
170     struct serial *serial = (struct serial *)obj;
171
172     async_terminate_queue( &serial->read_q, STATUS_CANCELLED );
173     async_terminate_queue( &serial->write_q, STATUS_CANCELLED );
174     async_terminate_queue( &serial->wait_q, STATUS_CANCELLED );
175     if (serial->fd) release_object( serial->fd );
176 }
177
178 static void serial_dump( struct object *obj, int verbose )
179 {
180     struct serial *serial = (struct serial *)obj;
181     assert( obj->ops == &serial_ops );
182     fprintf( stderr, "Port fd=%p mask=%x\n", serial->fd, serial->eventmask );
183 }
184
185 static struct serial *get_serial_obj( struct process *process, obj_handle_t handle, unsigned int access )
186 {
187     return (struct serial *)get_handle_obj( process, handle, access, &serial_ops );
188 }
189
190 static int serial_get_poll_events( struct fd *fd )
191 {
192     struct serial *serial = get_fd_user( fd );
193     int events = 0;
194     assert( serial->obj.ops == &serial_ops );
195
196     if (!list_empty( &serial->read_q ))  events |= POLLIN;
197     if (!list_empty( &serial->write_q )) events |= POLLOUT;
198     if (!list_empty( &serial->wait_q ))  events |= POLLIN;
199
200     /* fprintf(stderr,"poll events are %04x\n",events); */
201
202     return events;
203 }
204
205 static int serial_get_info( struct fd *fd )
206 {
207     int flags = 0;
208     struct serial *serial = get_fd_user( fd );
209     assert( serial->obj.ops == &serial_ops );
210
211     if (!(serial->options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)))
212         flags |= FD_FLAG_OVERLAPPED;
213     else if (!(serial->readinterval == MAXDWORD &&
214                serial->readmult == 0 && serial->readconst == 0))
215         flags |= FD_FLAG_TIMEOUT;
216     if (serial->readinterval == MAXDWORD &&
217         serial->readmult == 0 && serial->readconst == 0)
218         flags |= FD_FLAG_AVAILABLE;
219
220     return flags;
221 }
222
223 static void serial_poll_event(struct fd *fd, int event)
224 {
225     struct serial *serial = get_fd_user( fd );
226
227     /* fprintf(stderr,"Poll event %02x\n",event); */
228
229     if (!list_empty( &serial->read_q ) && (POLLIN & event) )
230         async_terminate_head( &serial->read_q, STATUS_ALERTED );
231
232     if (!list_empty( &serial->write_q ) && (POLLOUT & event) )
233         async_terminate_head( &serial->write_q, STATUS_ALERTED );
234
235     if (!list_empty( &serial->wait_q ) && (POLLIN & event) )
236         async_terminate_head( &serial->wait_q, STATUS_ALERTED );
237
238     set_fd_events( fd, serial_get_poll_events(fd) );
239 }
240
241 static void serial_queue_async( struct fd *fd, void *apc, void *user, void *iosb,
242                                 int type, int count )
243 {
244     struct serial *serial = get_fd_user( fd );
245     struct list *queue;
246     int timeout;
247     int events;
248
249     assert(serial->obj.ops == &serial_ops);
250
251     switch (type)
252     {
253     case ASYNC_TYPE_READ:
254         queue = &serial->read_q;
255         timeout = serial->readconst + serial->readmult*count;
256         break;
257     case ASYNC_TYPE_WAIT:
258         queue = &serial->wait_q;
259         timeout = 0;
260         break;
261     case ASYNC_TYPE_WRITE:
262         queue = &serial->write_q;
263         timeout = serial->writeconst + serial->writemult*count;
264         break;
265     default:
266         set_error(STATUS_INVALID_PARAMETER);
267         return;
268     }
269
270     if (!create_async( fd, current, timeout, queue, apc, user, iosb ))
271         return;
272
273     /* Check if the new pending request can be served immediately */
274     events = check_fd_events( fd, serial_get_poll_events( fd ) );
275     if (events)
276     {
277         /* serial_poll_event() calls set_select_events() */
278         serial_poll_event( fd, events );
279         return;
280     }
281
282     set_fd_events( fd, serial_get_poll_events( fd ) );
283 }
284
285 static void serial_cancel_async( struct fd *fd )
286 {
287     struct serial *serial = get_fd_user( fd );
288     assert(serial->obj.ops == &serial_ops);
289
290     async_terminate_queue( &serial->read_q, STATUS_CANCELLED );
291     async_terminate_queue( &serial->write_q, STATUS_CANCELLED );
292     async_terminate_queue( &serial->wait_q, STATUS_CANCELLED );
293 }
294
295 static int serial_flush( struct fd *fd, struct event **event )
296 {
297     /* MSDN says: If hFile is a handle to a communications device,
298      * the function only flushes the transmit buffer.
299      */
300     int ret = (tcflush( get_unix_fd(fd), TCOFLUSH ) != -1);
301     if (!ret) file_set_error();
302     return ret;
303 }
304
305 DECL_HANDLER(get_serial_info)
306 {
307     struct serial *serial;
308
309     if ((serial = get_serial_obj( current->process, req->handle, 0 )))
310     {
311         /* timeouts */
312         reply->readinterval = serial->readinterval;
313         reply->readconst    = serial->readconst;
314         reply->readmult     = serial->readmult;
315         reply->writeconst   = serial->writeconst;
316         reply->writemult    = serial->writemult;
317
318         /* event mask */
319         reply->eventmask    = serial->eventmask;
320
321         /* comm port error status */
322         reply->commerror    = serial->commerror;
323
324         release_object( serial );
325     }
326 }
327
328 DECL_HANDLER(set_serial_info)
329 {
330     struct serial *serial;
331
332     if ((serial = get_serial_obj( current->process, req->handle, 0 )))
333     {
334         /* timeouts */
335         if (req->flags & SERIALINFO_SET_TIMEOUTS)
336         {
337             serial->readinterval = req->readinterval;
338             serial->readconst    = req->readconst;
339             serial->readmult     = req->readmult;
340             serial->writeconst   = req->writeconst;
341             serial->writemult    = req->writemult;
342         }
343
344         /* event mask */
345         if (req->flags & SERIALINFO_SET_MASK)
346         {
347             serial->eventmask = req->eventmask;
348             if (!serial->eventmask)
349             {
350                 async_terminate_queue( &serial->wait_q, STATUS_SUCCESS );
351             }
352         }
353
354         /* comm port error status */
355         if (req->flags & SERIALINFO_SET_ERROR)
356         {
357             serial->commerror = req->commerror;
358         }
359
360         release_object( serial );
361     }
362 }