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