server: Use attributes instead of inherit flag in console requests.
[wine/multimedia.git] / server / serial.c
blob88f44fbaed5a7720c0c2dfa172a07289670b976a
1 /*
2 * Server-side serial port communications management
4 * Copyright (C) 1998 Alexandre Julliard
5 * Copyright (C) 2000,2001 Mike McCormack
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.
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.
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
23 #include "config.h"
24 #include "wine/port.h"
26 #include <assert.h>
27 #include <fcntl.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <sys/time.h>
33 #include <sys/types.h>
34 #include <time.h>
35 #include <unistd.h>
36 #ifdef HAVE_UTIME_H
37 #include <utime.h>
38 #endif
39 #ifdef HAVE_TERMIOS_H
40 #include <termios.h>
41 #endif
42 #ifdef HAVE_SYS_IOCTL_H
43 #include <sys/ioctl.h>
44 #endif
45 #ifdef HAVE_POLL_H
46 #include <poll.h>
47 #endif
49 #include "ntstatus.h"
50 #define WIN32_NO_STATUS
51 #include "windef.h"
52 #include "winternl.h"
54 #include "file.h"
55 #include "handle.h"
56 #include "thread.h"
57 #include "request.h"
59 static void serial_dump( struct object *obj, int verbose );
60 static struct fd *serial_get_fd( struct object *obj );
61 static void serial_destroy(struct object *obj);
63 static int serial_get_poll_events( struct fd *fd );
64 static void serial_poll_event( struct fd *fd, int event );
65 static int serial_get_info( struct fd *fd );
66 static int serial_flush( struct fd *fd, struct event **event );
67 static void serial_queue_async( struct fd *fd, void *apc, void *user, void *iosb, int type, int count );
68 static void serial_cancel_async( struct fd *fd );
70 struct serial
72 struct object obj;
73 struct fd *fd;
74 unsigned int options;
76 /* timeout values */
77 unsigned int readinterval;
78 unsigned int readconst;
79 unsigned int readmult;
80 unsigned int writeconst;
81 unsigned int writemult;
83 unsigned int eventmask;
84 unsigned int commerror;
86 struct termios original;
88 struct list read_q;
89 struct list write_q;
90 struct list wait_q;
92 /* FIXME: add dcb, comm status, handler module, sharing */
95 static const struct object_ops serial_ops =
97 sizeof(struct serial), /* size */
98 serial_dump, /* dump */
99 default_fd_add_queue, /* add_queue */
100 default_fd_remove_queue, /* remove_queue */
101 default_fd_signaled, /* signaled */
102 no_satisfied, /* satisfied */
103 no_signal, /* signal */
104 serial_get_fd, /* get_fd */
105 no_lookup_name, /* lookup_name */
106 no_close_handle, /* close_handle */
107 serial_destroy /* destroy */
110 static const struct fd_ops serial_fd_ops =
112 serial_get_poll_events, /* get_poll_events */
113 serial_poll_event, /* poll_event */
114 serial_flush, /* flush */
115 serial_get_info, /* get_file_info */
116 serial_queue_async, /* queue_async */
117 serial_cancel_async /* cancel_async */
120 /* check if the given fd is a serial port */
121 int is_serial_fd( struct fd *fd )
123 struct termios tios;
125 return !tcgetattr( get_unix_fd(fd), &tios );
128 /* create a serial object for a given fd */
129 struct object *create_serial( struct fd *fd, unsigned int options )
131 struct serial *serial;
132 int unix_fd;
134 if ((unix_fd = dup( get_unix_fd(fd) )) == -1) return NULL;
136 /* set the fd back to blocking if necessary */
137 if (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT))
138 fcntl( unix_fd, F_SETFL, 0 );
140 if (!(serial = alloc_object( &serial_ops )))
142 close( unix_fd );
143 return NULL;
145 serial->options = options;
146 serial->readinterval = 0;
147 serial->readmult = 0;
148 serial->readconst = 0;
149 serial->writemult = 0;
150 serial->writeconst = 0;
151 serial->eventmask = 0;
152 serial->commerror = 0;
153 list_init( &serial->read_q );
154 list_init( &serial->write_q );
155 list_init( &serial->wait_q );
156 if (!(serial->fd = create_anonymous_fd( &serial_fd_ops, unix_fd, &serial->obj )))
158 release_object( serial );
159 return NULL;
161 return &serial->obj;
164 static struct fd *serial_get_fd( struct object *obj )
166 struct serial *serial = (struct serial *)obj;
167 return (struct fd *)grab_object( serial->fd );
170 static void serial_destroy( struct object *obj)
172 struct serial *serial = (struct serial *)obj;
174 async_terminate_queue( &serial->read_q, STATUS_CANCELLED );
175 async_terminate_queue( &serial->write_q, STATUS_CANCELLED );
176 async_terminate_queue( &serial->wait_q, STATUS_CANCELLED );
177 if (serial->fd) release_object( serial->fd );
180 static void serial_dump( struct object *obj, int verbose )
182 struct serial *serial = (struct serial *)obj;
183 assert( obj->ops == &serial_ops );
184 fprintf( stderr, "Port fd=%p mask=%x\n", serial->fd, serial->eventmask );
187 static struct serial *get_serial_obj( struct process *process, obj_handle_t handle, unsigned int access )
189 return (struct serial *)get_handle_obj( process, handle, access, &serial_ops );
192 static int serial_get_poll_events( struct fd *fd )
194 struct serial *serial = get_fd_user( fd );
195 int events = 0;
196 assert( serial->obj.ops == &serial_ops );
198 if (!list_empty( &serial->read_q )) events |= POLLIN;
199 if (!list_empty( &serial->write_q )) events |= POLLOUT;
200 if (!list_empty( &serial->wait_q )) events |= POLLIN;
202 /* fprintf(stderr,"poll events are %04x\n",events); */
204 return events;
207 static int serial_get_info( struct fd *fd )
209 int flags = 0;
210 struct serial *serial = get_fd_user( fd );
211 assert( serial->obj.ops == &serial_ops );
213 if (!(serial->options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)))
214 flags |= FD_FLAG_OVERLAPPED;
215 else if (!(serial->readinterval == MAXDWORD &&
216 serial->readmult == 0 && serial->readconst == 0))
217 flags |= FD_FLAG_TIMEOUT;
218 if (serial->readinterval == MAXDWORD &&
219 serial->readmult == 0 && serial->readconst == 0)
220 flags |= FD_FLAG_AVAILABLE;
222 return flags;
225 static void serial_poll_event(struct fd *fd, int event)
227 struct serial *serial = get_fd_user( fd );
229 /* fprintf(stderr,"Poll event %02x\n",event); */
231 if (!list_empty( &serial->read_q ) && (POLLIN & event) )
232 async_terminate_head( &serial->read_q, STATUS_ALERTED );
234 if (!list_empty( &serial->write_q ) && (POLLOUT & event) )
235 async_terminate_head( &serial->write_q, STATUS_ALERTED );
237 if (!list_empty( &serial->wait_q ) && (POLLIN & event) )
238 async_terminate_head( &serial->wait_q, STATUS_ALERTED );
240 set_fd_events( fd, serial_get_poll_events(fd) );
243 static void serial_queue_async( struct fd *fd, void *apc, void *user, void *iosb,
244 int type, int count )
246 struct serial *serial = get_fd_user( fd );
247 struct list *queue;
248 int timeout;
249 int events;
251 assert(serial->obj.ops == &serial_ops);
253 switch (type)
255 case ASYNC_TYPE_READ:
256 queue = &serial->read_q;
257 timeout = serial->readconst + serial->readmult*count;
258 break;
259 case ASYNC_TYPE_WAIT:
260 queue = &serial->wait_q;
261 timeout = 0;
262 break;
263 case ASYNC_TYPE_WRITE:
264 queue = &serial->write_q;
265 timeout = serial->writeconst + serial->writemult*count;
266 break;
267 default:
268 set_error(STATUS_INVALID_PARAMETER);
269 return;
272 if (!create_async( current, &timeout, queue, apc, user, iosb ))
273 return;
275 /* Check if the new pending request can be served immediately */
276 events = check_fd_events( fd, serial_get_poll_events( fd ) );
277 if (events)
279 /* serial_poll_event() calls set_select_events() */
280 serial_poll_event( fd, events );
281 return;
284 set_fd_events( fd, serial_get_poll_events( fd ) );
287 static void serial_cancel_async( struct fd *fd )
289 struct serial *serial = get_fd_user( fd );
290 assert(serial->obj.ops == &serial_ops);
292 async_terminate_queue( &serial->read_q, STATUS_CANCELLED );
293 async_terminate_queue( &serial->write_q, STATUS_CANCELLED );
294 async_terminate_queue( &serial->wait_q, STATUS_CANCELLED );
297 static int serial_flush( struct fd *fd, struct event **event )
299 /* MSDN says: If hFile is a handle to a communications device,
300 * the function only flushes the transmit buffer.
302 int ret = (tcflush( get_unix_fd(fd), TCOFLUSH ) != -1);
303 if (!ret) file_set_error();
304 return ret;
307 DECL_HANDLER(get_serial_info)
309 struct serial *serial;
311 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
313 /* timeouts */
314 reply->readinterval = serial->readinterval;
315 reply->readconst = serial->readconst;
316 reply->readmult = serial->readmult;
317 reply->writeconst = serial->writeconst;
318 reply->writemult = serial->writemult;
320 /* event mask */
321 reply->eventmask = serial->eventmask;
323 /* comm port error status */
324 reply->commerror = serial->commerror;
326 release_object( serial );
330 DECL_HANDLER(set_serial_info)
332 struct serial *serial;
334 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
336 /* timeouts */
337 if (req->flags & SERIALINFO_SET_TIMEOUTS)
339 serial->readinterval = req->readinterval;
340 serial->readconst = req->readconst;
341 serial->readmult = req->readmult;
342 serial->writeconst = req->writeconst;
343 serial->writemult = req->writemult;
346 /* event mask */
347 if (req->flags & SERIALINFO_SET_MASK)
349 serial->eventmask = req->eventmask;
350 if (!serial->eventmask)
352 async_terminate_queue( &serial->wait_q, STATUS_SUCCESS );
356 /* comm port error status */
357 if (req->flags & SERIALINFO_SET_ERROR)
359 serial->commerror = req->commerror;
362 release_object( serial );