server: Add a default access mapping function for files, and use it for devices too.
[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 void serial_destroy(struct object *obj);
62
63 static enum server_fd_type serial_get_fd_type( struct fd *fd );
64 static void serial_flush( struct fd *fd, struct event **event );
65 static void serial_queue_async( struct fd *fd, const async_data_t *data, int type, int count );
66
67 struct serial
68 {
69     struct object       obj;
70     struct fd          *fd;
71
72     /* timeout values */
73     unsigned int        readinterval;
74     unsigned int        readconst;
75     unsigned int        readmult;
76     unsigned int        writeconst;
77     unsigned int        writemult;
78
79     unsigned int        eventmask;
80
81     struct termios      original;
82
83     /* FIXME: add dcb, comm status, handler module, sharing */
84 };
85
86 static const struct object_ops serial_ops =
87 {
88     sizeof(struct serial),        /* size */
89     serial_dump,                  /* dump */
90     add_queue,                    /* add_queue */
91     remove_queue,                 /* remove_queue */
92     default_fd_signaled,          /* signaled */
93     no_satisfied,                 /* satisfied */
94     no_signal,                    /* signal */
95     serial_get_fd,                /* get_fd */
96     default_fd_map_access,        /* map_access */
97     no_lookup_name,               /* lookup_name */
98     no_open_file,                 /* open_file */
99     fd_close_handle,              /* close_handle */
100     serial_destroy                /* destroy */
101 };
102
103 static const struct fd_ops serial_fd_ops =
104 {
105     default_fd_get_poll_events,   /* get_poll_events */
106     default_poll_event,           /* poll_event */
107     serial_flush,                 /* flush */
108     serial_get_fd_type,           /* get_file_info */
109     default_fd_ioctl,             /* ioctl */
110     serial_queue_async,           /* queue_async */
111     default_fd_reselect_async,    /* reselect_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 )
125 {
126     struct serial *serial;
127
128     if (!(serial = alloc_object( &serial_ops ))) return NULL;
129
130     serial->readinterval = 0;
131     serial->readmult     = 0;
132     serial->readconst    = 0;
133     serial->writemult    = 0;
134     serial->writeconst   = 0;
135     serial->eventmask    = 0;
136     serial->fd = (struct fd *)grab_object( fd );
137     set_fd_user( fd, &serial_fd_ops, &serial->obj );
138     return &serial->obj;
139 }
140
141 static struct fd *serial_get_fd( struct object *obj )
142 {
143     struct serial *serial = (struct serial *)obj;
144     return (struct fd *)grab_object( serial->fd );
145 }
146
147 static void serial_destroy( struct object *obj)
148 {
149     struct serial *serial = (struct serial *)obj;
150     release_object( serial->fd );
151 }
152
153 static void serial_dump( struct object *obj, int verbose )
154 {
155     struct serial *serial = (struct serial *)obj;
156     assert( obj->ops == &serial_ops );
157     fprintf( stderr, "Port fd=%p mask=%x\n", serial->fd, serial->eventmask );
158 }
159
160 static struct serial *get_serial_obj( struct process *process, obj_handle_t handle, unsigned int access )
161 {
162     return (struct serial *)get_handle_obj( process, handle, access, &serial_ops );
163 }
164
165 static enum server_fd_type serial_get_fd_type( struct fd *fd )
166 {
167     return FD_TYPE_SERIAL;
168 }
169
170 static void serial_queue_async( struct fd *fd, const async_data_t *data, int type, int count )
171 {
172     struct serial *serial = get_fd_user( fd );
173     timeout_t timeout = 0;
174     struct async *async;
175
176     assert(serial->obj.ops == &serial_ops);
177
178     switch (type)
179     {
180     case ASYNC_TYPE_READ:
181         timeout = serial->readconst + (timeout_t)serial->readmult*count;
182         break;
183     case ASYNC_TYPE_WRITE:
184         timeout = serial->writeconst + (timeout_t)serial->writemult*count;
185         break;
186     }
187
188     if ((async = fd_queue_async( fd, data, type, count )))
189     {
190         if (timeout) async_set_timeout( async, timeout * -10000, STATUS_TIMEOUT );
191         release_object( async );
192         set_error( STATUS_PENDING );
193     }
194 }
195
196 static void serial_flush( struct fd *fd, struct event **event )
197 {
198     /* MSDN says: If hFile is a handle to a communications device,
199      * the function only flushes the transmit buffer.
200      */
201     if (tcflush( get_unix_fd(fd), TCOFLUSH ) == -1) file_set_error();
202 }
203
204 DECL_HANDLER(get_serial_info)
205 {
206     struct serial *serial;
207
208     if ((serial = get_serial_obj( current->process, req->handle, 0 )))
209     {
210         /* timeouts */
211         reply->readinterval = serial->readinterval;
212         reply->readconst    = serial->readconst;
213         reply->readmult     = serial->readmult;
214         reply->writeconst   = serial->writeconst;
215         reply->writemult    = serial->writemult;
216
217         /* event mask */
218         reply->eventmask    = serial->eventmask;
219
220         release_object( serial );
221     }
222 }
223
224 DECL_HANDLER(set_serial_info)
225 {
226     struct serial *serial;
227
228     if ((serial = get_serial_obj( current->process, req->handle, 0 )))
229     {
230         /* timeouts */
231         if (req->flags & SERIALINFO_SET_TIMEOUTS)
232         {
233             serial->readinterval = req->readinterval;
234             serial->readconst    = req->readconst;
235             serial->readmult     = req->readmult;
236             serial->writeconst   = req->writeconst;
237             serial->writemult    = req->writemult;
238         }
239
240         /* event mask */
241         if (req->flags & SERIALINFO_SET_MASK)
242         {
243             serial->eventmask = req->eventmask;
244             if (!serial->eventmask)
245             {
246                 fd_async_wake_up( serial->fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
247             }
248         }
249
250         release_object( serial );
251     }
252 }