Added a close_handle method to the object operations, and use it to
[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     no_signal,                    /* signal */
105     serial_get_fd,                /* get_fd */
106     no_close_handle,              /* close_handle */
107     serial_destroy                /* destroy */
108 };
109
110 static const struct fd_ops serial_fd_ops =
111 {
112     serial_get_poll_events,       /* get_poll_events */
113     serial_poll_event,            /* poll_event */
114     serial_flush,                 /* flush */
115     serial_get_info,              /* get_file_info */
116     serial_queue_async,           /* queue_async */
117     serial_cancel_async           /* cancel_async */
118 };
119
120 /* check if the given fd is a serial port */
121 int is_serial_fd( struct fd *fd )
122 {
123     struct termios tios;
124
125     return !tcgetattr( get_unix_fd(fd), &tios );
126 }
127
128 /* create a serial object for a given fd */
129 struct object *create_serial( struct fd *fd, unsigned int options )
130 {
131     struct serial *serial;
132     int unix_fd;
133
134     if ((unix_fd = dup( get_unix_fd(fd) )) == -1) return NULL;
135
136     /* set the fd back to blocking if necessary */
137     if (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT))
138         fcntl( unix_fd, F_SETFL, 0 );
139
140     if (!(serial = alloc_object( &serial_ops )))
141     {
142         close( unix_fd );
143         return NULL;
144     }
145     serial->options      = options;
146     serial->readinterval = 0;
147     serial->readmult     = 0;
148     serial->readconst    = 0;
149     serial->writemult    = 0;
150     serial->writeconst   = 0;
151     serial->eventmask    = 0;
152     serial->commerror    = 0;
153     list_init( &serial->read_q );
154     list_init( &serial->write_q );
155     list_init( &serial->wait_q );
156     if (!(serial->fd = create_anonymous_fd( &serial_fd_ops, unix_fd, &serial->obj )))
157     {
158         release_object( serial );
159         return NULL;
160     }
161     return &serial->obj;
162 }
163
164 static struct fd *serial_get_fd( struct object *obj )
165 {
166     struct serial *serial = (struct serial *)obj;
167     return (struct fd *)grab_object( serial->fd );
168 }
169
170 static void serial_destroy( struct object *obj)
171 {
172     struct serial *serial = (struct serial *)obj;
173
174     async_terminate_queue( &serial->read_q, STATUS_CANCELLED );
175     async_terminate_queue( &serial->write_q, STATUS_CANCELLED );
176     async_terminate_queue( &serial->wait_q, STATUS_CANCELLED );
177     if (serial->fd) release_object( serial->fd );
178 }
179
180 static void serial_dump( struct object *obj, int verbose )
181 {
182     struct serial *serial = (struct serial *)obj;
183     assert( obj->ops == &serial_ops );
184     fprintf( stderr, "Port fd=%p mask=%x\n", serial->fd, serial->eventmask );
185 }
186
187 static struct serial *get_serial_obj( struct process *process, obj_handle_t handle, unsigned int access )
188 {
189     return (struct serial *)get_handle_obj( process, handle, access, &serial_ops );
190 }
191
192 static int serial_get_poll_events( struct fd *fd )
193 {
194     struct serial *serial = get_fd_user( fd );
195     int events = 0;
196     assert( serial->obj.ops == &serial_ops );
197
198     if (!list_empty( &serial->read_q ))  events |= POLLIN;
199     if (!list_empty( &serial->write_q )) events |= POLLOUT;
200     if (!list_empty( &serial->wait_q ))  events |= POLLIN;
201
202     /* fprintf(stderr,"poll events are %04x\n",events); */
203
204     return events;
205 }
206
207 static int serial_get_info( struct fd *fd )
208 {
209     int flags = 0;
210     struct serial *serial = get_fd_user( fd );
211     assert( serial->obj.ops == &serial_ops );
212
213     if (!(serial->options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)))
214         flags |= FD_FLAG_OVERLAPPED;
215     else if (!(serial->readinterval == MAXDWORD &&
216                serial->readmult == 0 && serial->readconst == 0))
217         flags |= FD_FLAG_TIMEOUT;
218     if (serial->readinterval == MAXDWORD &&
219         serial->readmult == 0 && serial->readconst == 0)
220         flags |= FD_FLAG_AVAILABLE;
221
222     return flags;
223 }
224
225 static void serial_poll_event(struct fd *fd, int event)
226 {
227     struct serial *serial = get_fd_user( fd );
228
229     /* fprintf(stderr,"Poll event %02x\n",event); */
230
231     if (!list_empty( &serial->read_q ) && (POLLIN & event) )
232         async_terminate_head( &serial->read_q, STATUS_ALERTED );
233
234     if (!list_empty( &serial->write_q ) && (POLLOUT & event) )
235         async_terminate_head( &serial->write_q, STATUS_ALERTED );
236
237     if (!list_empty( &serial->wait_q ) && (POLLIN & event) )
238         async_terminate_head( &serial->wait_q, STATUS_ALERTED );
239
240     set_fd_events( fd, serial_get_poll_events(fd) );
241 }
242
243 static void serial_queue_async( struct fd *fd, void *apc, void *user, void *iosb,
244                                 int type, int count )
245 {
246     struct serial *serial = get_fd_user( fd );
247     struct list *queue;
248     int timeout;
249     int events;
250
251     assert(serial->obj.ops == &serial_ops);
252
253     switch (type)
254     {
255     case ASYNC_TYPE_READ:
256         queue = &serial->read_q;
257         timeout = serial->readconst + serial->readmult*count;
258         break;
259     case ASYNC_TYPE_WAIT:
260         queue = &serial->wait_q;
261         timeout = 0;
262         break;
263     case ASYNC_TYPE_WRITE:
264         queue = &serial->write_q;
265         timeout = serial->writeconst + serial->writemult*count;
266         break;
267     default:
268         set_error(STATUS_INVALID_PARAMETER);
269         return;
270     }
271
272     if (!create_async( current, &timeout, queue, apc, user, iosb ))
273         return;
274
275     /* Check if the new pending request can be served immediately */
276     events = check_fd_events( fd, serial_get_poll_events( fd ) );
277     if (events)
278     {
279         /* serial_poll_event() calls set_select_events() */
280         serial_poll_event( fd, events );
281         return;
282     }
283
284     set_fd_events( fd, serial_get_poll_events( fd ) );
285 }
286
287 static void serial_cancel_async( struct fd *fd )
288 {
289     struct serial *serial = get_fd_user( fd );
290     assert(serial->obj.ops == &serial_ops);
291
292     async_terminate_queue( &serial->read_q, STATUS_CANCELLED );
293     async_terminate_queue( &serial->write_q, STATUS_CANCELLED );
294     async_terminate_queue( &serial->wait_q, STATUS_CANCELLED );
295 }
296
297 static int serial_flush( struct fd *fd, struct event **event )
298 {
299     /* MSDN says: If hFile is a handle to a communications device,
300      * the function only flushes the transmit buffer.
301      */
302     int ret = (tcflush( get_unix_fd(fd), TCOFLUSH ) != -1);
303     if (!ret) file_set_error();
304     return ret;
305 }
306
307 DECL_HANDLER(get_serial_info)
308 {
309     struct serial *serial;
310
311     if ((serial = get_serial_obj( current->process, req->handle, 0 )))
312     {
313         /* timeouts */
314         reply->readinterval = serial->readinterval;
315         reply->readconst    = serial->readconst;
316         reply->readmult     = serial->readmult;
317         reply->writeconst   = serial->writeconst;
318         reply->writemult    = serial->writemult;
319
320         /* event mask */
321         reply->eventmask    = serial->eventmask;
322
323         /* comm port error status */
324         reply->commerror    = serial->commerror;
325
326         release_object( serial );
327     }
328 }
329
330 DECL_HANDLER(set_serial_info)
331 {
332     struct serial *serial;
333
334     if ((serial = get_serial_obj( current->process, req->handle, 0 )))
335     {
336         /* timeouts */
337         if (req->flags & SERIALINFO_SET_TIMEOUTS)
338         {
339             serial->readinterval = req->readinterval;
340             serial->readconst    = req->readconst;
341             serial->readmult     = req->readmult;
342             serial->writeconst   = req->writeconst;
343             serial->writemult    = req->writemult;
344         }
345
346         /* event mask */
347         if (req->flags & SERIALINFO_SET_MASK)
348         {
349             serial->eventmask = req->eventmask;
350             if (!serial->eventmask)
351             {
352                 async_terminate_queue( &serial->wait_q, STATUS_SUCCESS );
353             }
354         }
355
356         /* comm port error status */
357         if (req->flags & SERIALINFO_SET_ERROR)
358         {
359             serial->commerror = req->commerror;
360         }
361
362         release_object( serial );
363     }
364 }