Finish any pending WaitCommEvents if the event mask is set to 0.
[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
25 #include <assert.h>
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <errno.h>
31 #ifdef HAVE_SYS_ERRNO_H
32 #include <sys/errno.h>
33 #endif
34 #include <sys/time.h>
35 #include <sys/types.h>
36 #include <time.h>
37 #include <unistd.h>
38 #include <utime.h>
39 #include <termios.h>
40 #include <sys/ioctl.h>
41
42 #include "winerror.h"
43 #include "winbase.h"
44
45 #include "handle.h"
46 #include "thread.h"
47 #include "request.h"
48 #include "async.h"
49
50 static void serial_dump( struct object *obj, int verbose );
51 static int serial_get_fd( struct object *obj );
52 static int serial_get_info( struct object *obj, struct get_file_info_reply *reply, int *flags );
53 static int serial_get_poll_events( struct object *obj );
54 static struct async_queue * serial_queue_async(struct object *obj, struct async* async, int type, int count);
55 static void destroy_serial(struct object *obj);
56 static void serial_poll_event( struct object *obj, int event );
57
58 struct serial
59 {
60     struct object       obj;
61     unsigned int        access;
62     unsigned int        attrib;
63
64     /* timeout values */
65     unsigned int        readinterval;
66     unsigned int        readconst;
67     unsigned int        readmult;
68     unsigned int        writeconst;
69     unsigned int        writemult;
70
71     unsigned int        eventmask;
72     unsigned int        commerror;
73
74     struct termios      original;
75
76     struct async_queue  read_q;
77     struct async_queue  write_q;
78     struct async_queue  wait_q;
79
80     /* FIXME: add dcb, comm status, handler module, sharing */
81 };
82
83 static const struct object_ops serial_ops =
84 {
85     sizeof(struct serial),        /* size */
86     serial_dump,                  /* dump */
87     default_poll_add_queue,       /* add_queue */
88     default_poll_remove_queue,    /* remove_queue */
89     default_poll_signaled,        /* signaled */
90     no_satisfied,                 /* satisfied */
91     serial_get_poll_events,       /* get_poll_events */
92     serial_poll_event,            /* poll_event */
93     serial_get_fd,                /* get_fd */
94     no_flush,                     /* flush */
95     serial_get_info,              /* get_file_info */
96     serial_queue_async,           /* queue_async */
97     destroy_serial                /* destroy */
98 };
99
100 static struct serial *create_serial( const char *nameptr, size_t len, unsigned int access, int attributes )
101 {
102     struct serial *serial;
103     struct termios tios;
104     int fd, flags = 0;
105     char *name;
106
107     if (!(name = mem_alloc( len + 1 ))) return NULL;
108     memcpy( name, nameptr, len );
109     name[len] = 0;
110
111     switch(access & (GENERIC_READ | GENERIC_WRITE))
112     {
113     case GENERIC_READ:  flags |= O_RDONLY; break;
114     case GENERIC_WRITE: flags |= O_WRONLY; break;
115     case GENERIC_READ|GENERIC_WRITE: flags |= O_RDWR; break;
116     default: break;
117     }
118
119     flags |= O_NONBLOCK;
120
121     fd = open( name, flags );
122     free( name );
123     if (fd < 0)
124     {
125         file_set_error();
126         return NULL;
127     }
128
129     /* check its really a serial port */
130     if (tcgetattr(fd,&tios))
131     {
132         file_set_error();
133         close( fd );
134         return NULL;
135     }
136
137     /* set the fd back to blocking if necessary */
138     if( ! (attributes & FILE_FLAG_OVERLAPPED) )
139        if(0>fcntl(fd, F_SETFL, 0))
140            perror("fcntl");
141
142     if ((serial = alloc_object( &serial_ops, fd )))
143     {
144         serial->attrib       = attributes;
145         serial->access       = access;
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         init_async_queue(&serial->read_q);
154         init_async_queue(&serial->write_q);
155         init_async_queue(&serial->wait_q);
156     }
157     return serial;
158 }
159
160 static void destroy_serial( struct object *obj)
161 {
162     struct serial *serial = (struct serial *)obj;
163
164     destroy_async_queue(&serial->read_q);
165     destroy_async_queue(&serial->write_q);
166     destroy_async_queue(&serial->wait_q);
167 }
168
169 static void serial_dump( struct object *obj, int verbose )
170 {
171     struct serial *serial = (struct serial *)obj;
172     assert( obj->ops == &serial_ops );
173     fprintf( stderr, "Port fd=%d mask=%x\n", serial->obj.fd, serial->eventmask );
174 }
175
176 struct serial *get_serial_obj( struct process *process, handle_t handle, unsigned int access )
177 {
178     return (struct serial *)get_handle_obj( process, handle, access, &serial_ops );
179 }
180
181 static int serial_get_poll_events( struct object *obj )
182 {
183     struct serial *serial = (struct serial *)obj;
184     int events = 0;
185     assert( obj->ops == &serial_ops );
186
187     if(IS_READY(serial->read_q))
188         events |= POLLIN;
189     if(IS_READY(serial->write_q))
190         events |= POLLOUT;
191     if(IS_READY(serial->wait_q))
192         events |= POLLIN;
193
194     /* fprintf(stderr,"poll events are %04x\n",events); */
195
196     return events;
197 }
198
199 static int serial_get_fd( struct object *obj )
200 {
201     struct serial *serial = (struct serial *)obj;
202     assert( obj->ops == &serial_ops );
203     return serial->obj.fd;
204 }
205
206 static int serial_get_info( struct object *obj, struct get_file_info_reply *reply, int *flags )
207 {
208     struct serial *serial = (struct serial *) obj;
209     assert( obj->ops == &serial_ops );
210
211     if (reply)
212     {
213         reply->type        = FILE_TYPE_CHAR;
214         reply->attr        = 0;
215         reply->access_time = 0;
216         reply->write_time  = 0;
217         reply->size_high   = 0;
218         reply->size_low    = 0;
219         reply->links       = 0;
220         reply->index_high  = 0;
221         reply->index_low   = 0;
222         reply->serial      = 0;
223     }
224
225     *flags = 0;
226     if(serial->attrib & FILE_FLAG_OVERLAPPED)
227         *flags |= FD_FLAG_OVERLAPPED;
228     else if(!((serial->readinterval == MAXDWORD) &&
229               (serial->readmult == 0) && (serial->readconst == 0)) )
230         *flags |= FD_FLAG_TIMEOUT;
231
232     return FD_TYPE_DEFAULT;
233 }
234
235 static void serial_poll_event(struct object *obj, int event)
236 {
237     struct serial *serial = (struct serial *)obj;
238
239     /* fprintf(stderr,"Poll event %02x\n",event); */
240
241     if(IS_READY(serial->read_q) && (POLLIN & event) )
242         async_notify(serial->read_q.head,STATUS_ALERTED);
243
244     if(IS_READY(serial->write_q) && (POLLOUT & event) )
245         async_notify(serial->write_q.head,STATUS_ALERTED);
246
247     if(IS_READY(serial->wait_q) && (POLLIN & event) )
248         async_notify(serial->wait_q.head,STATUS_ALERTED);
249
250     set_select_events(obj,obj->ops->get_poll_events(obj));
251 }
252
253 /* 
254  * This function is an abuse of overloading that deserves some explanation.
255  *
256  * It has three purposes:
257  *
258  * 1. get the queue for a type of async operation
259  * 2. requeue an async operation
260  * 3. queue a new async operation
261  *
262  * It is overloaded so that these three functions only take one function pointer
263  *  in the object operations list.
264  *
265  * In all cases, it returns the async queue.
266  */
267 static struct async_queue *serial_queue_async(struct object *obj, struct async *async, int type, int count)
268 {
269     struct serial *serial = (struct serial *)obj;
270     struct async_queue *q;
271     int timeout;
272
273     assert(obj->ops == &serial_ops);
274
275     switch(type)
276     {
277     case ASYNC_TYPE_READ:
278         q = &serial->read_q;
279         timeout = serial->readconst + serial->readmult*count;
280         break;
281     case ASYNC_TYPE_WAIT:
282         q = &serial->wait_q;
283         timeout = 0;
284         break;
285     case ASYNC_TYPE_WRITE:
286         q = &serial->write_q;
287         timeout = serial->writeconst + serial->writemult*count;
288         break;
289     default:
290         set_error(STATUS_INVALID_PARAMETER);
291         return NULL;
292     }
293
294     if(async)
295     {
296         if(!async->q)
297         {
298             async_add_timeout(async,timeout);
299             async_insert(q, async);
300     }
301 }
302
303     return q;
304 }
305
306 /* create a serial */
307 DECL_HANDLER(create_serial)
308 {
309     struct serial *serial;
310
311     reply->handle = 0;
312     if ((serial = create_serial( get_req_data(), get_req_data_size(), req->access, req->attributes )))
313     {
314         reply->handle = alloc_handle( current->process, serial, req->access, req->inherit );
315         release_object( serial );
316     }
317 }
318
319 DECL_HANDLER(get_serial_info)
320 {
321     struct serial *serial;
322
323     if ((serial = get_serial_obj( current->process, req->handle, 0 )))
324     {
325         /* timeouts */
326         reply->readinterval = serial->readinterval;
327         reply->readconst    = serial->readconst;
328         reply->readmult     = serial->readmult;
329         reply->writeconst   = serial->writeconst;
330         reply->writemult    = serial->writemult;
331
332         /* event mask */
333         reply->eventmask    = serial->eventmask;
334
335         /* comm port error status */
336         reply->commerror    = serial->commerror;
337
338         release_object( serial );
339     }
340 }
341
342 DECL_HANDLER(set_serial_info)
343 {
344     struct serial *serial;
345
346     if ((serial = get_serial_obj( current->process, req->handle, 0 )))
347     {
348         /* timeouts */
349         if(req->flags & SERIALINFO_SET_TIMEOUTS)
350         {
351             serial->readinterval = req->readinterval;
352             serial->readconst    = req->readconst;
353             serial->readmult     = req->readmult;
354             serial->writeconst   = req->writeconst;
355             serial->writemult    = req->writemult;
356         }
357
358         /* event mask */
359         if(req->flags & SERIALINFO_SET_MASK)
360         {
361             serial->eventmask = req->eventmask;
362             if(!serial->eventmask)
363             {
364                 while(serial->wait_q.head)
365                 {
366                     async_notify(serial->wait_q.head, STATUS_SUCCESS);
367                     destroy_async(serial->wait_q.head);
368                 }
369             }
370         }
371
372         /* comm port error status */
373         if(req->flags & SERIALINFO_SET_ERROR)
374         {
375             serial->commerror = req->commerror;
376         }
377
378         release_object( serial );
379     }
380 }
381