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