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