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