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 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
);
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 */
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 no_flush
, /* flush */
111 serial_get_fd_type
, /* get_fd_type */
112 default_fd_ioctl
, /* ioctl */
113 serial_queue_async
, /* queue_async */
114 default_fd_reselect_async
, /* reselect_async */
115 default_fd_cancel_async
/* cancel_async */
118 /* check if the given fd is a serial port */
119 int is_serial_fd( struct fd
*fd
)
123 return !tcgetattr( get_unix_fd(fd
), &tios
);
126 /* create a serial object for a given fd */
127 struct object
*create_serial( struct fd
*fd
)
129 struct serial
*serial
;
131 if (!(serial
= alloc_object( &serial_ops
))) return NULL
;
133 serial
->readinterval
= 0;
134 serial
->readmult
= 0;
135 serial
->readconst
= 0;
136 serial
->writemult
= 0;
137 serial
->writeconst
= 0;
138 serial
->eventmask
= 0;
139 serial
->generation
= 0;
140 serial
->fd
= (struct fd
*)grab_object( fd
);
141 set_fd_user( fd
, &serial_fd_ops
, &serial
->obj
);
145 static struct fd
*serial_get_fd( struct object
*obj
)
147 struct serial
*serial
= (struct serial
*)obj
;
148 return (struct fd
*)grab_object( serial
->fd
);
151 static void serial_destroy( struct object
*obj
)
153 struct serial
*serial
= (struct serial
*)obj
;
154 release_object( serial
->fd
);
157 static void serial_dump( struct object
*obj
, int verbose
)
159 struct serial
*serial
= (struct serial
*)obj
;
160 assert( obj
->ops
== &serial_ops
);
161 fprintf( stderr
, "Port fd=%p mask=%x\n", serial
->fd
, serial
->eventmask
);
164 static struct serial
*get_serial_obj( struct process
*process
, obj_handle_t handle
, unsigned int access
)
166 return (struct serial
*)get_handle_obj( process
, handle
, access
, &serial_ops
);
169 static enum server_fd_type
serial_get_fd_type( struct fd
*fd
)
171 return FD_TYPE_SERIAL
;
174 static void serial_queue_async( struct fd
*fd
, const async_data_t
*data
, int type
, int count
)
176 struct serial
*serial
= get_fd_user( fd
);
177 timeout_t timeout
= 0;
180 assert(serial
->obj
.ops
== &serial_ops
);
184 case ASYNC_TYPE_READ
:
185 timeout
= serial
->readconst
+ (timeout_t
)serial
->readmult
*count
;
187 case ASYNC_TYPE_WRITE
:
188 timeout
= serial
->writeconst
+ (timeout_t
)serial
->writemult
*count
;
192 if ((async
= fd_queue_async( fd
, data
, type
)))
194 if (timeout
) async_set_timeout( async
, timeout
* -10000, STATUS_TIMEOUT
);
195 release_object( async
);
196 set_error( STATUS_PENDING
);
200 DECL_HANDLER(get_serial_info
)
202 struct serial
*serial
;
204 if ((serial
= get_serial_obj( current
->process
, req
->handle
, 0 )))
207 reply
->readinterval
= serial
->readinterval
;
208 reply
->readconst
= serial
->readconst
;
209 reply
->readmult
= serial
->readmult
;
210 reply
->writeconst
= serial
->writeconst
;
211 reply
->writemult
= serial
->writemult
;
214 reply
->eventmask
= serial
->eventmask
;
215 reply
->cookie
= serial
->generation
;
217 release_object( serial
);
221 DECL_HANDLER(set_serial_info
)
223 struct serial
*serial
;
225 if ((serial
= get_serial_obj( current
->process
, req
->handle
, 0 )))
228 if (req
->flags
& SERIALINFO_SET_TIMEOUTS
)
230 serial
->readinterval
= req
->readinterval
;
231 serial
->readconst
= req
->readconst
;
232 serial
->readmult
= req
->readmult
;
233 serial
->writeconst
= req
->writeconst
;
234 serial
->writemult
= req
->writemult
;
238 if (req
->flags
& SERIALINFO_SET_MASK
)
240 serial
->eventmask
= req
->eventmask
;
241 serial
->generation
++;
242 fd_async_wake_up( serial
->fd
, ASYNC_TYPE_WAIT
, STATUS_SUCCESS
);
245 release_object( serial
);