server: Make async I/O queues into real objects.
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 "ntstatus.h"
50 #define WIN32_NO_STATUS
51 #include "windef.h"
52 #include "winternl.h"
53
54 #include "file.h"
55 #include "handle.h"
56 #include "thread.h"
57 #include "request.h"
58
59 static void serial_dump( struct object *obj, int verbose );
60 static struct fd *serial_get_fd( struct object *obj );
61 static unsigned int serial_map_access( struct object *obj, unsigned int access );
62 static void serial_destroy(struct object *obj);
63
64 static enum server_fd_type serial_get_info( struct fd *fd, int *flags );
65 static void serial_flush( struct fd *fd, struct event **event );
66 static void serial_queue_async( struct fd *fd, const async_data_t *data, int type, int count );
67
68 struct serial
69 {
70     struct object       obj;
71     struct fd          *fd;
72     unsigned int        options;
73
74     /* timeout values */
75     unsigned int        readinterval;
76     unsigned int        readconst;
77     unsigned int        readmult;
78     unsigned int        writeconst;
79     unsigned int        writemult;
80
81     unsigned int        eventmask;
82
83     struct termios      original;
84
85     /* FIXME: add dcb, comm status, handler module, sharing */
86 };
87
88 static const struct object_ops serial_ops =
89 {
90     sizeof(struct serial),        /* size */
91     serial_dump,                  /* dump */
92     default_fd_add_queue,         /* add_queue */
93     default_fd_remove_queue,      /* remove_queue */
94     default_fd_signaled,          /* signaled */
95     no_satisfied,                 /* satisfied */
96     no_signal,                    /* signal */
97     serial_get_fd,                /* get_fd */
98     serial_map_access,            /* map_access */
99     no_lookup_name,               /* lookup_name */
100     no_open_file,                 /* open_file */
101     fd_close_handle,              /* close_handle */
102     serial_destroy                /* destroy */
103 };
104
105 static const struct fd_ops serial_fd_ops =
106 {
107     default_fd_get_poll_events,   /* get_poll_events */
108     default_poll_event,           /* poll_event */
109     serial_flush,                 /* flush */
110     serial_get_info,              /* get_file_info */
111     serial_queue_async,           /* queue_async */
112     default_fd_cancel_async       /* cancel_async */
113 };
114
115 /* check if the given fd is a serial port */
116 int is_serial_fd( struct fd *fd )
117 {
118     struct termios tios;
119
120     return !tcgetattr( get_unix_fd(fd), &tios );
121 }
122
123 /* create a serial object for a given fd */
124 struct object *create_serial( struct fd *fd, unsigned int options )
125 {
126     struct serial *serial;
127     int unix_fd = get_unix_fd( fd );
128
129     /* set the fd back to blocking if necessary */
130     if (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT))
131         fcntl( unix_fd, F_SETFL, 0 );
132
133     if (!(serial = alloc_object( &serial_ops ))) return NULL;
134
135     serial->options      = options;
136     serial->readinterval = 0;
137     serial->readmult     = 0;
138     serial->readconst    = 0;
139     serial->writemult    = 0;
140     serial->writeconst   = 0;
141     serial->eventmask    = 0;
142     serial->fd = (struct fd *)grab_object( fd );
143     set_fd_user( fd, &serial_fd_ops, &serial->obj );
144     return &serial->obj;
145 }
146
147 static struct fd *serial_get_fd( struct object *obj )
148 {
149     struct serial *serial = (struct serial *)obj;
150     return (struct fd *)grab_object( serial->fd );
151 }
152
153 static unsigned int serial_map_access( struct object *obj, unsigned int access )
154 {
155     if (access & GENERIC_READ)    access |= FILE_GENERIC_READ;
156     if (access & GENERIC_WRITE)   access |= FILE_GENERIC_WRITE;
157     if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
158     if (access & GENERIC_ALL)     access |= FILE_ALL_ACCESS;
159     return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
160 }
161
162 static void serial_destroy( struct object *obj)
163 {
164     struct serial *serial = (struct serial *)obj;
165     release_object( serial->fd );
166 }
167
168 static void serial_dump( struct object *obj, int verbose )
169 {
170     struct serial *serial = (struct serial *)obj;
171     assert( obj->ops == &serial_ops );
172     fprintf( stderr, "Port fd=%p mask=%x\n", serial->fd, serial->eventmask );
173 }
174
175 static struct serial *get_serial_obj( struct process *process, obj_handle_t handle, unsigned int access )
176 {
177     return (struct serial *)get_handle_obj( process, handle, access, &serial_ops );
178 }
179
180 static enum server_fd_type serial_get_info( struct fd *fd, int *flags )
181 {
182     struct serial *serial = get_fd_user( fd );
183     assert( serial->obj.ops == &serial_ops );
184
185     *flags = 0;
186     if (!(serial->options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)))
187         *flags |= FD_FLAG_OVERLAPPED;
188     else if (!(serial->readinterval == MAXDWORD &&
189                serial->readmult == 0 && serial->readconst == 0))
190         *flags |= FD_FLAG_TIMEOUT;
191     if (serial->readinterval == MAXDWORD &&
192         serial->readmult == 0 && serial->readconst == 0)
193         *flags |= FD_FLAG_AVAILABLE;
194
195     return FD_TYPE_SERIAL;
196 }
197
198 static void serial_queue_async( struct fd *fd, const async_data_t *data, int type, int count )
199 {
200     struct serial *serial = get_fd_user( fd );
201     struct timeval when = current_time;
202     int timeout = 0;
203
204     assert(serial->obj.ops == &serial_ops);
205
206     switch (type)
207     {
208     case ASYNC_TYPE_READ:
209         timeout = serial->readconst + serial->readmult*count;
210         break;
211     case ASYNC_TYPE_WRITE:
212         timeout = serial->writeconst + serial->writemult*count;
213         break;
214     }
215
216     add_timeout( &when, timeout );
217     fd_queue_async_timeout( fd, data, type, count, timeout ? &when : NULL );
218 }
219
220 static void serial_flush( struct fd *fd, struct event **event )
221 {
222     /* MSDN says: If hFile is a handle to a communications device,
223      * the function only flushes the transmit buffer.
224      */
225     if (tcflush( get_unix_fd(fd), TCOFLUSH ) == -1) file_set_error();
226 }
227
228 DECL_HANDLER(get_serial_info)
229 {
230     struct serial *serial;
231
232     if ((serial = get_serial_obj( current->process, req->handle, 0 )))
233     {
234         /* timeouts */
235         reply->readinterval = serial->readinterval;
236         reply->readconst    = serial->readconst;
237         reply->readmult     = serial->readmult;
238         reply->writeconst   = serial->writeconst;
239         reply->writemult    = serial->writemult;
240
241         /* event mask */
242         reply->eventmask    = serial->eventmask;
243
244         release_object( serial );
245     }
246 }
247
248 DECL_HANDLER(set_serial_info)
249 {
250     struct serial *serial;
251
252     if ((serial = get_serial_obj( current->process, req->handle, 0 )))
253     {
254         /* timeouts */
255         if (req->flags & SERIALINFO_SET_TIMEOUTS)
256         {
257             serial->readinterval = req->readinterval;
258             serial->readconst    = req->readconst;
259             serial->readmult     = req->readmult;
260             serial->writeconst   = req->writeconst;
261             serial->writemult    = req->writemult;
262         }
263
264         /* event mask */
265         if (req->flags & SERIALINFO_SET_MASK)
266         {
267             serial->eventmask = req->eventmask;
268             if (!serial->eventmask)
269             {
270                 fd_async_wake_up( serial->fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
271             }
272         }
273
274         release_object( serial );
275     }
276 }