Changed fd operations to take a struct fd instead of a struct object.
[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 <sys/time.h>
32 #include <sys/types.h>
33 #include <time.h>
34 #include <unistd.h>
35 #ifdef HAVE_UTIME_H
36 #include <utime.h>
37 #endif
38 #ifdef HAVE_TERMIOS_H
39 #include <termios.h>
40 #endif
41 #ifdef HAVE_SYS_IOCTL_H
42 #include <sys/ioctl.h>
43 #endif
44
45 #include "winerror.h"
46 #include "winbase.h"
47
48 #include "file.h"
49 #include "handle.h"
50 #include "thread.h"
51 #include "request.h"
52 #include "async.h"
53
54 static void serial_dump( struct object *obj, int verbose );
55 static void serial_destroy(struct object *obj);
56
57 static int serial_get_poll_events( struct fd *fd );
58 static void serial_poll_event( struct fd *fd, int event );
59 static int serial_get_info( struct fd *fd, struct get_file_info_reply *reply, int *flags );
60 static int serial_flush( struct fd *fd );
61 static void serial_queue_async(struct fd *fd, void *ptr, unsigned int status, int type, int count);
62
63 struct serial
64 {
65     struct object       obj;
66     unsigned int        access;
67     unsigned int        attrib;
68
69     /* timeout values */
70     unsigned int        readinterval;
71     unsigned int        readconst;
72     unsigned int        readmult;
73     unsigned int        writeconst;
74     unsigned int        writemult;
75
76     unsigned int        eventmask;
77     unsigned int        commerror;
78
79     struct termios      original;
80
81     struct async_queue  read_q;
82     struct async_queue  write_q;
83     struct async_queue  wait_q;
84
85     /* FIXME: add dcb, comm status, handler module, sharing */
86 };
87
88 static const struct object_ops serial_ops =
89 {
90     sizeof(struct serial),        /* size */
91     serial_dump,                  /* dump */
92     default_fd_add_queue,         /* add_queue */
93     default_fd_remove_queue,      /* remove_queue */
94     default_fd_signaled,          /* signaled */
95     no_satisfied,                 /* satisfied */
96     default_get_fd,               /* get_fd */
97     serial_destroy                /* destroy */
98 };
99
100 static const struct fd_ops serial_fd_ops =
101 {
102     serial_get_poll_events,       /* get_poll_events */
103     serial_poll_event,            /* poll_event */
104     serial_flush,                 /* flush */
105     serial_get_info,              /* get_file_info */
106     serial_queue_async            /* queue_async */
107 };
108
109 static struct serial *create_serial( const char *nameptr, size_t len, unsigned int access, int attributes )
110 {
111     struct serial *serial;
112     struct termios tios;
113     int fd, flags = 0;
114     char *name;
115
116     if (!(name = mem_alloc( len + 1 ))) return NULL;
117     memcpy( name, nameptr, len );
118     name[len] = 0;
119
120     switch(access & (GENERIC_READ | GENERIC_WRITE))
121     {
122     case GENERIC_READ:  flags |= O_RDONLY; break;
123     case GENERIC_WRITE: flags |= O_WRONLY; break;
124     case GENERIC_READ|GENERIC_WRITE: flags |= O_RDWR; break;
125     default: break;
126     }
127
128     flags |= O_NONBLOCK;
129
130     fd = open( name, flags );
131     free( name );
132     if (fd < 0)
133     {
134         file_set_error();
135         return NULL;
136     }
137
138     /* check its really a serial port */
139     if (tcgetattr(fd,&tios))
140     {
141         file_set_error();
142         close( fd );
143         return NULL;
144     }
145
146     /* set the fd back to blocking if necessary */
147     if( ! (attributes & FILE_FLAG_OVERLAPPED) )
148        if(0>fcntl(fd, F_SETFL, 0))
149            perror("fcntl");
150
151     if ((serial = alloc_fd_object( &serial_ops, &serial_fd_ops, fd )))
152     {
153         serial->attrib       = attributes;
154         serial->access       = access;
155         serial->readinterval = 0;
156         serial->readmult     = 0;
157         serial->readconst    = 0;
158         serial->writemult    = 0;
159         serial->writeconst   = 0;
160         serial->eventmask    = 0;
161         serial->commerror    = 0;
162         init_async_queue(&serial->read_q);
163         init_async_queue(&serial->write_q);
164         init_async_queue(&serial->wait_q);
165     }
166     return serial;
167 }
168
169 static void serial_destroy( struct object *obj)
170 {
171     struct serial *serial = (struct serial *)obj;
172
173     destroy_async_queue(&serial->read_q);
174     destroy_async_queue(&serial->write_q);
175     destroy_async_queue(&serial->wait_q);
176 }
177
178 static void serial_dump( struct object *obj, int verbose )
179 {
180     struct serial *serial = (struct serial *)obj;
181     assert( obj->ops == &serial_ops );
182     fprintf( stderr, "Port fd=%p mask=%x\n", serial->obj.fd_obj, serial->eventmask );
183 }
184
185 struct serial *get_serial_obj( struct process *process, obj_handle_t handle, unsigned int access )
186 {
187     return (struct serial *)get_handle_obj( process, handle, access, &serial_ops );
188 }
189
190 static int serial_get_poll_events( struct fd *fd )
191 {
192     struct serial *serial = get_fd_user( fd );
193     int events = 0;
194     assert( serial->obj.ops == &serial_ops );
195
196     if(IS_READY(serial->read_q))
197         events |= POLLIN;
198     if(IS_READY(serial->write_q))
199         events |= POLLOUT;
200     if(IS_READY(serial->wait_q))
201         events |= POLLIN;
202
203     /* fprintf(stderr,"poll events are %04x\n",events); */
204
205     return events;
206 }
207
208 static int serial_get_info( struct fd *fd, struct get_file_info_reply *reply, int *flags )
209 {
210     struct serial *serial = get_fd_user( fd );
211     assert( serial->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 fd *fd, int event)
238 {
239     struct serial *serial = get_fd_user( fd );
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( &serial->obj, serial_get_poll_events(fd) );
253 }
254
255 static void serial_queue_async(struct fd *fd, void *ptr, unsigned int status, int type, int count)
256 {
257     struct serial *serial = get_fd_user( fd );
258     struct async_queue *q;
259     struct async *async;
260     int timeout;
261
262     assert(serial->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         int events;
288
289         if ( !async )
290             async = create_async ( &serial->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         events = check_fd_events( fd, serial_get_poll_events( fd ) );
303         if (events)
304         {
305             /* serial_poll_event() calls set_select_events() */
306             serial_poll_event( fd, events );
307             return;
308         }
309     }
310     else if ( async ) destroy_async ( async );
311     else set_error ( STATUS_INVALID_PARAMETER );
312
313     set_select_events ( &serial->obj, serial_get_poll_events( fd ));
314 }
315
316 static int serial_flush( struct fd *fd )
317 {
318     /* MSDN says: If hFile is a handle to a communications device,
319      * the function only flushes the transmit buffer.
320      */
321     int ret = (tcflush( get_unix_fd(fd), TCOFLUSH ) != -1);
322     if (!ret) file_set_error();
323     return ret;
324 }
325
326 /* create a serial */
327 DECL_HANDLER(create_serial)
328 {
329     struct serial *serial;
330
331     reply->handle = 0;
332     if ((serial = create_serial( get_req_data(), get_req_data_size(), req->access, req->attributes )))
333     {
334         reply->handle = alloc_handle( current->process, serial, req->access, req->inherit );
335         release_object( serial );
336     }
337 }
338
339 DECL_HANDLER(get_serial_info)
340 {
341     struct serial *serial;
342
343     if ((serial = get_serial_obj( current->process, req->handle, 0 )))
344     {
345         /* timeouts */
346         reply->readinterval = serial->readinterval;
347         reply->readconst    = serial->readconst;
348         reply->readmult     = serial->readmult;
349         reply->writeconst   = serial->writeconst;
350         reply->writemult    = serial->writemult;
351
352         /* event mask */
353         reply->eventmask    = serial->eventmask;
354
355         /* comm port error status */
356         reply->commerror    = serial->commerror;
357
358         release_object( serial );
359     }
360 }
361
362 DECL_HANDLER(set_serial_info)
363 {
364     struct serial *serial;
365
366     if ((serial = get_serial_obj( current->process, req->handle, 0 )))
367     {
368         /* timeouts */
369         if(req->flags & SERIALINFO_SET_TIMEOUTS)
370         {
371             serial->readinterval = req->readinterval;
372             serial->readconst    = req->readconst;
373             serial->readmult     = req->readmult;
374             serial->writeconst   = req->writeconst;
375             serial->writemult    = req->writemult;
376         }
377
378         /* event mask */
379         if(req->flags & SERIALINFO_SET_MASK)
380         {
381             serial->eventmask = req->eventmask;
382             if(!serial->eventmask)
383             {
384                 while(serial->wait_q.head)
385                 {
386                     async_notify(serial->wait_q.head, STATUS_SUCCESS);
387                     destroy_async(serial->wait_q.head);
388                 }
389             }
390         }
391
392         /* comm port error status */
393         if(req->flags & SERIALINFO_SET_ERROR)
394         {
395             serial->commerror = req->commerror;
396         }
397
398         release_object( serial );
399     }
400 }