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