user32: Implement semi-stub for FlashWindowEx.
[wine.git] / server / serial.c
blob164a4b1fe717a4d9d22efafd06bdc16ee89411bc
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 enum server_fd_type serial_get_fd_type( struct fd *fd );
64 static void serial_queue_async( struct fd *fd, const async_data_t *data, int type, int count );
66 struct serial
68 struct object obj;
69 struct fd *fd;
71 /* timeout values */
72 unsigned int readinterval;
73 unsigned int readconst;
74 unsigned int readmult;
75 unsigned int writeconst;
76 unsigned int writemult;
78 unsigned int eventmask;
79 unsigned int generation; /* event mask change counter */
80 unsigned int pending_write : 1;
81 unsigned int pending_wait : 1;
83 struct termios original;
85 /* FIXME: add dcb, comm status, handler module, sharing */
88 static const struct object_ops serial_ops =
90 sizeof(struct serial), /* size */
91 serial_dump, /* dump */
92 no_get_type, /* get_type */
93 add_queue, /* add_queue */
94 remove_queue, /* remove_queue */
95 default_fd_signaled, /* signaled */
96 no_satisfied, /* satisfied */
97 no_signal, /* signal */
98 serial_get_fd, /* get_fd */
99 default_fd_map_access, /* map_access */
100 default_get_sd, /* get_sd */
101 default_set_sd, /* set_sd */
102 no_lookup_name, /* lookup_name */
103 no_open_file, /* open_file */
104 fd_close_handle, /* close_handle */
105 serial_destroy /* destroy */
108 static const struct fd_ops serial_fd_ops =
110 default_fd_get_poll_events, /* get_poll_events */
111 default_poll_event, /* poll_event */
112 serial_get_fd_type, /* get_fd_type */
113 no_fd_read, /* read */
114 no_fd_write, /* write */
115 no_fd_flush, /* flush */
116 default_fd_ioctl, /* ioctl */
117 serial_queue_async, /* queue_async */
118 default_fd_reselect_async, /* reselect_async */
119 default_fd_cancel_async /* cancel_async */
122 /* check if the given fd is a serial port */
123 int is_serial_fd( struct fd *fd )
125 struct termios tios;
127 return !tcgetattr( get_unix_fd(fd), &tios );
130 /* create a serial object for a given fd */
131 struct object *create_serial( struct fd *fd )
133 struct serial *serial;
135 if (!(serial = alloc_object( &serial_ops ))) return NULL;
137 serial->readinterval = 0;
138 serial->readmult = 0;
139 serial->readconst = 0;
140 serial->writemult = 0;
141 serial->writeconst = 0;
142 serial->eventmask = 0;
143 serial->generation = 0;
144 serial->pending_write = 0;
145 serial->pending_wait = 0;
146 serial->fd = (struct fd *)grab_object( fd );
147 set_fd_user( fd, &serial_fd_ops, &serial->obj );
148 return &serial->obj;
151 static struct fd *serial_get_fd( struct object *obj )
153 struct serial *serial = (struct serial *)obj;
154 return (struct fd *)grab_object( serial->fd );
157 static void serial_destroy( struct object *obj)
159 struct serial *serial = (struct serial *)obj;
160 release_object( serial->fd );
163 static void serial_dump( struct object *obj, int verbose )
165 struct serial *serial = (struct serial *)obj;
166 assert( obj->ops == &serial_ops );
167 fprintf( stderr, "Port fd=%p mask=%x\n", serial->fd, serial->eventmask );
170 static struct serial *get_serial_obj( struct process *process, obj_handle_t handle, unsigned int access )
172 return (struct serial *)get_handle_obj( process, handle, access, &serial_ops );
175 static enum server_fd_type serial_get_fd_type( struct fd *fd )
177 return FD_TYPE_SERIAL;
180 static void serial_queue_async( struct fd *fd, const async_data_t *data, int type, int count )
182 struct serial *serial = get_fd_user( fd );
183 timeout_t timeout = 0;
184 struct async *async;
186 assert(serial->obj.ops == &serial_ops);
188 switch (type)
190 case ASYNC_TYPE_READ:
191 timeout = serial->readconst + (timeout_t)serial->readmult*count;
192 break;
193 case ASYNC_TYPE_WRITE:
194 timeout = serial->writeconst + (timeout_t)serial->writemult*count;
195 break;
198 if ((async = fd_queue_async( fd, data, type )))
200 if (timeout) async_set_timeout( async, timeout * -10000, STATUS_TIMEOUT );
201 release_object( async );
202 set_error( STATUS_PENDING );
206 DECL_HANDLER(get_serial_info)
208 struct serial *serial;
210 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
212 if (req->flags & SERIALINFO_PENDING_WAIT)
214 if (serial->pending_wait)
216 release_object( serial );
217 set_error( STATUS_INVALID_PARAMETER );
218 return;
220 serial->pending_wait = 1;
223 /* timeouts */
224 reply->readinterval = serial->readinterval;
225 reply->readconst = serial->readconst;
226 reply->readmult = serial->readmult;
227 reply->writeconst = serial->writeconst;
228 reply->writemult = serial->writemult;
230 /* event mask */
231 reply->eventmask = serial->eventmask;
232 reply->cookie = serial->generation;
234 /* pending write */
235 reply->pending_write = serial->pending_write;
236 if (req->flags & SERIALINFO_PENDING_WRITE)
237 serial->pending_write = 0;
239 release_object( serial );
243 DECL_HANDLER(set_serial_info)
245 struct serial *serial;
247 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
249 if (req->flags & SERIALINFO_PENDING_WAIT)
251 if (!serial->pending_wait)
253 release_object( serial );
254 set_error( STATUS_INVALID_PARAMETER );
255 return;
257 serial->pending_wait = 0;
260 /* timeouts */
261 if (req->flags & SERIALINFO_SET_TIMEOUTS)
263 serial->readinterval = req->readinterval;
264 serial->readconst = req->readconst;
265 serial->readmult = req->readmult;
266 serial->writeconst = req->writeconst;
267 serial->writemult = req->writemult;
270 /* pending write */
271 if (req->flags & SERIALINFO_PENDING_WRITE)
272 serial->pending_write = 1;
274 /* event mask */
275 if (req->flags & SERIALINFO_SET_MASK)
277 serial->eventmask = req->eventmask;
278 serial->generation++;
279 fd_async_wake_up( serial->fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
282 release_object( serial );