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