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
24 #include "wine/port.h"
33 #include <sys/types.h>
42 #ifdef HAVE_SYS_IOCTL_H
43 #include <sys/ioctl.h>
50 #define WIN32_NO_STATUS
59 static void serial_dump( struct object *obj, int verbose );
60 static struct fd *serial_get_fd( struct object *obj );
61 static unsigned int serial_map_access( struct object *obj, unsigned int access );
62 static void serial_destroy(struct object *obj);
64 static enum server_fd_type serial_get_info( struct fd *fd, int *flags );
65 static void serial_flush( struct fd *fd, struct event **event );
66 static void serial_queue_async( struct fd *fd, const async_data_t *data, int type, int count );
74 unsigned int readinterval;
75 unsigned int readconst;
76 unsigned int readmult;
77 unsigned int writeconst;
78 unsigned int writemult;
80 unsigned int eventmask;
82 struct termios original;
84 /* FIXME: add dcb, comm status, handler module, sharing */
87 static const struct object_ops serial_ops =
89 sizeof(struct serial), /* size */
90 serial_dump, /* dump */
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 serial_map_access, /* map_access */
98 no_lookup_name, /* lookup_name */
99 no_open_file, /* open_file */
100 fd_close_handle, /* close_handle */
101 serial_destroy /* destroy */
104 static const struct fd_ops serial_fd_ops =
106 default_fd_get_poll_events, /* get_poll_events */
107 default_poll_event, /* poll_event */
108 serial_flush, /* flush */
109 serial_get_info, /* get_file_info */
110 serial_queue_async, /* queue_async */
111 default_fd_reselect_async, /* reselect_async */
112 default_fd_cancel_async /* cancel_async */
115 /* check if the given fd is a serial port */
116 int is_serial_fd( struct fd *fd )
120 return !tcgetattr( get_unix_fd(fd), &tios );
123 /* create a serial object for a given fd */
124 struct object *create_serial( struct fd *fd )
126 struct serial *serial;
128 if (!(serial = alloc_object( &serial_ops ))) return NULL;
130 serial->readinterval = 0;
131 serial->readmult = 0;
132 serial->readconst = 0;
133 serial->writemult = 0;
134 serial->writeconst = 0;
135 serial->eventmask = 0;
136 serial->fd = (struct fd *)grab_object( fd );
137 set_fd_user( fd, &serial_fd_ops, &serial->obj );
141 static struct fd *serial_get_fd( struct object *obj )
143 struct serial *serial = (struct serial *)obj;
144 return (struct fd *)grab_object( serial->fd );
147 static unsigned int serial_map_access( struct object *obj, unsigned int access )
149 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
150 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
151 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
152 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
153 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
156 static void serial_destroy( struct object *obj)
158 struct serial *serial = (struct serial *)obj;
159 release_object( serial->fd );
162 static void serial_dump( struct object *obj, int verbose )
164 struct serial *serial = (struct serial *)obj;
165 assert( obj->ops == &serial_ops );
166 fprintf( stderr, "Port fd=%p mask=%x\n", serial->fd, serial->eventmask );
169 static struct serial *get_serial_obj( struct process *process, obj_handle_t handle, unsigned int access )
171 return (struct serial *)get_handle_obj( process, handle, access, &serial_ops );
174 static enum server_fd_type serial_get_info( struct fd *fd, int *flags )
176 struct serial *serial = get_fd_user( fd );
177 assert( serial->obj.ops == &serial_ops );
180 return FD_TYPE_SERIAL;
183 static void serial_queue_async( struct fd *fd, const async_data_t *data, int type, int count )
185 struct serial *serial = get_fd_user( fd );
189 assert(serial->obj.ops == &serial_ops);
193 case ASYNC_TYPE_READ:
194 timeout = serial->readconst + serial->readmult*count;
196 case ASYNC_TYPE_WRITE:
197 timeout = serial->writeconst + serial->writemult*count;
201 if ((async = fd_queue_async( fd, data, type, count )))
205 struct timeval when = current_time;
206 add_timeout( &when, timeout );
207 async_set_timeout( async, &when, STATUS_TIMEOUT );
209 release_object( async );
210 set_error( STATUS_PENDING );
214 static void serial_flush( struct fd *fd, struct event **event )
216 /* MSDN says: If hFile is a handle to a communications device,
217 * the function only flushes the transmit buffer.
219 if (tcflush( get_unix_fd(fd), TCOFLUSH ) == -1) file_set_error();
222 DECL_HANDLER(get_serial_info)
224 struct serial *serial;
226 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
229 reply->readinterval = serial->readinterval;
230 reply->readconst = serial->readconst;
231 reply->readmult = serial->readmult;
232 reply->writeconst = serial->writeconst;
233 reply->writemult = serial->writemult;
236 reply->eventmask = serial->eventmask;
238 release_object( serial );
242 DECL_HANDLER(set_serial_info)
244 struct serial *serial;
246 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
249 if (req->flags & SERIALINFO_SET_TIMEOUTS)
251 serial->readinterval = req->readinterval;
252 serial->readconst = req->readconst;
253 serial->readmult = req->readmult;
254 serial->writeconst = req->writeconst;
255 serial->writemult = req->writemult;
259 if (req->flags & SERIALINFO_SET_MASK)
261 serial->eventmask = req->eventmask;
262 if (!serial->eventmask)
264 fd_async_wake_up( serial->fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
268 release_object( serial );