Handle ReadIntervalTimeout=MAXDWORD special case a bit better.
[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     if( (serial->readinterval == MAXDWORD) &&
215         (serial->readmult == 0) && (serial->readconst == 0) )
216         return FD_TYPE_DEFAULT;
217
218     return FD_TYPE_TIMEOUT;
219 }
220
221 static void serial_poll_event(struct object *obj, int event)
222 {
223     struct serial *serial = (struct serial *)obj;
224
225     /* fprintf(stderr,"Poll event %02x\n",event); */
226
227     if(IS_READY(serial->read_q) && (POLLIN & event) )
228         async_notify(serial->read_q.head,STATUS_ALERTED);
229
230     if(IS_READY(serial->write_q) && (POLLOUT & event) )
231         async_notify(serial->write_q.head,STATUS_ALERTED);
232
233     if(IS_READY(serial->wait_q) && (POLLIN & event) )
234         async_notify(serial->wait_q.head,STATUS_ALERTED);
235
236     set_select_events(obj,obj->ops->get_poll_events(obj));
237 }
238
239 /* 
240  * This function is an abuse of overloading that deserves some explanation.
241  *
242  * It has three purposes:
243  *
244  * 1. get the queue for a type of async operation
245  * 2. requeue an async operation
246  * 3. queue a new async operation
247  *
248  * It is overloaded so that these three functions only take one function pointer
249  *  in the object operations list.
250  *
251  * In all cases, it returns the async queue.
252  */
253 static struct async_queue *serial_queue_async(struct object *obj, struct async *async, int type, int count)
254 {
255     struct serial *serial = (struct serial *)obj;
256     struct async_queue *q;
257     int timeout;
258
259     assert(obj->ops == &serial_ops);
260
261     switch(type)
262     {
263     case ASYNC_TYPE_READ:
264         q = &serial->read_q;
265         timeout = serial->readconst + serial->readmult*count;
266         break;
267     case ASYNC_TYPE_WAIT:
268         q = &serial->wait_q;
269         timeout = 0;
270         break;
271     case ASYNC_TYPE_WRITE:
272         q = &serial->write_q;
273         timeout = serial->writeconst + serial->writemult*count;
274         break;
275     default:
276         set_error(STATUS_INVALID_PARAMETER);
277         return NULL;
278     }
279
280     if(async)
281     {
282         if(!async->q)
283         {
284             async_add_timeout(async,timeout);
285             async_insert(q, async);
286     }
287 }
288
289     return q;
290 }
291
292 /* create a serial */
293 DECL_HANDLER(create_serial)
294 {
295     struct serial *serial;
296
297     reply->handle = 0;
298     if ((serial = create_serial( get_req_data(), get_req_data_size(), req->access, req->attributes )))
299     {
300         reply->handle = alloc_handle( current->process, serial, req->access, req->inherit );
301         release_object( serial );
302     }
303 }
304
305 DECL_HANDLER(get_serial_info)
306 {
307     struct serial *serial;
308
309     if ((serial = get_serial_obj( current->process, req->handle, 0 )))
310     {
311         /* timeouts */
312         reply->readinterval = serial->readinterval;
313         reply->readconst    = serial->readconst;
314         reply->readmult     = serial->readmult;
315         reply->writeconst   = serial->writeconst;
316         reply->writemult    = serial->writemult;
317
318         /* event mask */
319         reply->eventmask    = serial->eventmask;
320
321         /* comm port error status */
322         reply->commerror    = serial->commerror;
323
324         release_object( serial );
325     }
326 }
327
328 DECL_HANDLER(set_serial_info)
329 {
330     struct serial *serial;
331
332     if ((serial = get_serial_obj( current->process, req->handle, 0 )))
333     {
334         /* timeouts */
335         if(req->flags & SERIALINFO_SET_TIMEOUTS)
336         {
337             serial->readinterval = req->readinterval;
338             serial->readconst    = req->readconst;
339             serial->readmult     = req->readmult;
340             serial->writeconst   = req->writeconst;
341             serial->writemult    = req->writemult;
342         }
343
344         /* event mask */
345         if(req->flags & SERIALINFO_SET_MASK)
346         {
347             serial->eventmask = req->eventmask;
348         }
349
350         /* comm port error status */
351         if(req->flags & SERIALINFO_SET_ERROR)
352         {
353             serial->commerror = req->commerror;
354         }
355
356         release_object( serial );
357     }
358 }
359