DIB Engine: Hook the engine between GDI32 and Display driver
[wine/hacks.git] / server / serial.c
blob11e6d9b904a12335deb26857a43d9b7ce28ee212
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_flush( struct fd *fd, struct event **event );
65 static void serial_queue_async( struct fd *fd, const async_data_t *data, int type, int count );
67 struct serial
69 struct object obj;
70 struct fd *fd;
72 /* timeout values */
73 unsigned int readinterval;
74 unsigned int readconst;
75 unsigned int readmult;
76 unsigned int writeconst;
77 unsigned int writemult;
79 unsigned int eventmask;
81 struct termios original;
83 /* FIXME: add dcb, comm status, handler module, sharing */
86 static const struct object_ops serial_ops =
88 sizeof(struct serial), /* size */
89 serial_dump, /* dump */
90 no_get_type, /* get_type */
91 add_queue, /* add_queue */
92 remove_queue, /* remove_queue */
93 default_fd_signaled, /* signaled */
94 no_satisfied, /* satisfied */
95 no_signal, /* signal */
96 serial_get_fd, /* get_fd */
97 default_fd_map_access, /* map_access */
98 default_get_sd, /* get_sd */
99 default_set_sd, /* set_sd */
100 no_lookup_name, /* lookup_name */
101 no_open_file, /* open_file */
102 fd_close_handle, /* close_handle */
103 serial_destroy /* destroy */
106 static const struct fd_ops serial_fd_ops =
108 default_fd_get_poll_events, /* get_poll_events */
109 default_poll_event, /* poll_event */
110 serial_flush, /* flush */
111 serial_get_fd_type, /* get_file_info */
112 default_fd_removable, /* removable */
113 default_fd_ioctl, /* ioctl */
114 serial_queue_async, /* queue_async */
115 default_fd_async_event, /* async_event */
116 default_fd_async_terminated, /* async_terminated */
117 default_fd_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 )
131 struct serial *serial;
133 if (!(serial = alloc_object( &serial_ops ))) return NULL;
135 serial->readinterval = 0;
136 serial->readmult = 0;
137 serial->readconst = 0;
138 serial->writemult = 0;
139 serial->writeconst = 0;
140 serial->eventmask = 0;
141 serial->fd = (struct fd *)grab_object( fd );
142 set_fd_user( fd, &serial_fd_ops, &serial->obj );
143 return &serial->obj;
146 static struct fd *serial_get_fd( struct object *obj )
148 struct serial *serial = (struct serial *)obj;
149 return (struct fd *)grab_object( serial->fd );
152 static void serial_destroy( struct object *obj)
154 struct serial *serial = (struct serial *)obj;
155 release_object( serial->fd );
158 static void serial_dump( struct object *obj, int verbose )
160 struct serial *serial = (struct serial *)obj;
161 assert( obj->ops == &serial_ops );
162 fprintf( stderr, "Port fd=%p mask=%x\n", serial->fd, serial->eventmask );
165 static struct serial *get_serial_obj( struct process *process, obj_handle_t handle, unsigned int access )
167 return (struct serial *)get_handle_obj( process, handle, access, &serial_ops );
170 static enum server_fd_type serial_get_fd_type( struct fd *fd )
172 return FD_TYPE_SERIAL;
175 static void serial_queue_async( struct fd *fd, const async_data_t *data, int type, int count )
177 struct serial *serial = get_fd_user( fd );
178 timeout_t timeout = 0;
179 struct async *async;
181 assert(serial->obj.ops == &serial_ops);
183 switch (type)
185 case ASYNC_TYPE_READ:
186 timeout = serial->readconst + (timeout_t)serial->readmult*count;
187 break;
188 case ASYNC_TYPE_WRITE:
189 timeout = serial->writeconst + (timeout_t)serial->writemult*count;
190 break;
193 if ((async = fd_queue_async( fd, data, type )))
195 if (timeout) async_set_timeout( async, timeout * -10000, STATUS_TIMEOUT );
196 release_object( async );
197 set_error( STATUS_PENDING );
201 static void serial_flush( struct fd *fd, struct event **event )
203 /* MSDN says: If hFile is a handle to a communications device,
204 * the function only flushes the transmit buffer.
206 if (tcflush( get_unix_fd(fd), TCOFLUSH ) == -1) file_set_error();
209 DECL_HANDLER(get_serial_info)
211 struct serial *serial;
213 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
215 /* timeouts */
216 reply->readinterval = serial->readinterval;
217 reply->readconst = serial->readconst;
218 reply->readmult = serial->readmult;
219 reply->writeconst = serial->writeconst;
220 reply->writemult = serial->writemult;
222 /* event mask */
223 reply->eventmask = serial->eventmask;
225 release_object( serial );
229 DECL_HANDLER(set_serial_info)
231 struct serial *serial;
233 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
235 /* timeouts */
236 if (req->flags & SERIALINFO_SET_TIMEOUTS)
238 serial->readinterval = req->readinterval;
239 serial->readconst = req->readconst;
240 serial->readmult = req->readmult;
241 serial->writeconst = req->writeconst;
242 serial->writemult = req->writemult;
245 /* event mask */
246 if (req->flags & SERIALINFO_SET_MASK)
248 serial->eventmask = req->eventmask;
249 if (!serial->eventmask)
251 fd_async_wake_up( serial->fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
255 release_object( serial );