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