1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
* Server-side serial port communications management
*
* Copyright (C) 1998 Alexandre Julliard
* Copyright (C) 2000,2001 Mike McCormack
*
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "config.h"
#include "wine/port.h"
#include <assert.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#ifdef HAVE_UTIME_H
#include <utime.h>
#endif
#ifdef HAVE_TERMIOS_H
#include <termios.h>
#endif
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#ifdef HAVE_POLL_H
#include <poll.h>
#endif
#include "ntstatus.h"
#define WIN32_NO_STATUS
#include "windef.h"
#include "winternl.h"
#include "file.h"
#include "handle.h"
#include "thread.h"
#include "request.h"
static void serial_dump( struct object *obj, int verbose );
static struct fd *serial_get_fd( struct object *obj );
static void serial_destroy(struct object *obj);
static enum server_fd_type serial_get_fd_type( struct fd *fd );
static void serial_flush( struct fd *fd, struct event **event );
static void serial_queue_async( struct fd *fd, const async_data_t *data, int type, int count );
struct serial
{
struct object obj;
struct fd *fd;
/* timeout values */
unsigned int readinterval;
unsigned int readconst;
unsigned int readmult;
unsigned int writeconst;
unsigned int writemult;
unsigned int eventmask;
struct termios original;
/* FIXME: add dcb, comm status, handler module, sharing */
};
static const struct object_ops serial_ops =
{
sizeof(struct serial), /* size */
serial_dump, /* dump */
no_get_type, /* get_type */
add_queue, /* add_queue */
remove_queue, /* remove_queue */
default_fd_signaled, /* signaled */
no_satisfied, /* satisfied */
no_signal, /* signal */
serial_get_fd, /* get_fd */
default_fd_map_access, /* map_access */
default_get_sd, /* get_sd */
default_set_sd, /* set_sd */
no_lookup_name, /* lookup_name */
no_open_file, /* open_file */
fd_close_handle, /* close_handle */
serial_destroy /* destroy */
};
static const struct fd_ops serial_fd_ops =
{
default_fd_get_poll_events, /* get_poll_events */
default_poll_event, /* poll_event */
serial_flush, /* flush */
serial_get_fd_type, /* get_fd_type */
default_fd_ioctl, /* ioctl */
serial_queue_async, /* queue_async */
default_fd_reselect_async, /* reselect_async */
default_fd_cancel_async /* cancel_async */
};
/* check if the given fd is a serial port */
int is_serial_fd( struct fd *fd )
{
struct termios tios;
return !tcgetattr( get_unix_fd(fd), &tios );
}
/* create a serial object for a given fd */
struct object *create_serial( struct fd *fd )
{
struct serial *serial;
if (!(serial = alloc_object( &serial_ops ))) return NULL;
serial->readinterval = 0;
serial->readmult = 0;
serial->readconst = 0;
serial->writemult = 0;
serial->writeconst = 0;
serial->eventmask = 0;
serial->fd = (struct fd *)grab_object( fd );
set_fd_user( fd, &serial_fd_ops, &serial->obj );
return &serial->obj;
}
static struct fd *serial_get_fd( struct object *obj )
{
struct serial *serial = (struct serial *)obj;
return (struct fd *)grab_object( serial->fd );
}
static void serial_destroy( struct object *obj)
{
struct serial *serial = (struct serial *)obj;
release_object( serial->fd );
}
static void serial_dump( struct object *obj, int verbose )
{
struct serial *serial = (struct serial *)obj;
assert( obj->ops == &serial_ops );
fprintf( stderr, "Port fd=%p mask=%x\n", serial->fd, serial->eventmask );
}
static struct serial *get_serial_obj( struct process *process, obj_handle_t handle, unsigned int access )
{
return (struct serial *)get_handle_obj( process, handle, access, &serial_ops );
}
static enum server_fd_type serial_get_fd_type( struct fd *fd )
{
return FD_TYPE_SERIAL;
}
static void serial_queue_async( struct fd *fd, const async_data_t *data, int type, int count )
{
struct serial *serial = get_fd_user( fd );
timeout_t timeout = 0;
struct async *async;
assert(serial->obj.ops == &serial_ops);
switch (type)
{
case ASYNC_TYPE_READ:
timeout = serial->readconst + (timeout_t)serial->readmult*count;
break;
case ASYNC_TYPE_WRITE:
timeout = serial->writeconst + (timeout_t)serial->writemult*count;
break;
}
if ((async = fd_queue_async( fd, data, type )))
{
if (timeout) async_set_timeout( async, timeout * -10000, STATUS_TIMEOUT );
release_object( async );
set_error( STATUS_PENDING );
}
}
static void serial_flush( struct fd *fd, struct event **event )
{
/* MSDN says: If hFile is a handle to a communications device,
* the function only flushes the transmit buffer.
*/
if (tcflush( get_unix_fd(fd), TCOFLUSH ) == -1) file_set_error();
}
DECL_HANDLER(get_serial_info)
{
struct serial *serial;
if ((serial = get_serial_obj( current->process, req->handle, 0 )))
{
/* timeouts */
reply->readinterval = serial->readinterval;
reply->readconst = serial->readconst;
reply->readmult = serial->readmult;
reply->writeconst = serial->writeconst;
reply->writemult = serial->writemult;
/* event mask */
reply->eventmask = serial->eventmask;
release_object( serial );
}
}
DECL_HANDLER(set_serial_info)
{
struct serial *serial;
if ((serial = get_serial_obj( current->process, req->handle, 0 )))
{
/* timeouts */
if (req->flags & SERIALINFO_SET_TIMEOUTS)
{
serial->readinterval = req->readinterval;
serial->readconst = req->readconst;
serial->readmult = req->readmult;
serial->writeconst = req->writeconst;
serial->writemult = req->writemult;
}
/* event mask */
if (req->flags & SERIALINFO_SET_MASK)
{
serial->eventmask = req->eventmask;
if (!serial->eventmask)
{
fd_async_wake_up( serial->fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
}
}
release_object( serial );
}
}