serial.c 10.6 KB
Newer Older
1 2 3 4
/*
 * Server-side serial port communications management
 *
 * Copyright (C) 1998 Alexandre Julliard
5
 * Copyright (C) 2000,2001 Mike McCormack
6
 *
7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * 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
20
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 22 23
 */

#include "config.h"
24
#include "wine/port.h"
25 26 27

#include <assert.h>
#include <fcntl.h>
28
#include <stdarg.h>
29 30 31 32 33 34 35
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
Steven Edwards's avatar
Steven Edwards committed
36
#ifdef HAVE_UTIME_H
37
#include <utime.h>
Steven Edwards's avatar
Steven Edwards committed
38 39
#endif
#ifdef HAVE_TERMIOS_H
40
#include <termios.h>
Steven Edwards's avatar
Steven Edwards committed
41 42
#endif
#ifdef HAVE_SYS_IOCTL_H
43
#include <sys/ioctl.h>
Steven Edwards's avatar
Steven Edwards committed
44
#endif
45 46 47
#ifdef HAVE_POLL_H
#include <poll.h>
#endif
48

49 50
#include "ntstatus.h"
#define WIN32_NO_STATUS
51
#include "windef.h"
52
#include "winternl.h"
53

54
#include "file.h"
55 56 57 58 59
#include "handle.h"
#include "thread.h"
#include "request.h"

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 63 64 65
static void serial_destroy(struct object *obj);

static int serial_get_poll_events( struct fd *fd );
static void serial_poll_event( struct fd *fd, int event );
66
static int serial_get_info( struct fd *fd );
67
static int serial_flush( struct fd *fd, struct event **event );
68 69
static void serial_queue_async( struct fd *fd, void *apc, void *user, void *iosb, int type, int count );
static void serial_cancel_async( struct fd *fd );
70 71 72 73

struct serial
{
    struct object       obj;
74
    struct fd          *fd;
75
    unsigned int        options;
76 77 78 79 80 81 82 83 84 85 86 87

    /* timeout values */
    unsigned int        readinterval;
    unsigned int        readconst;
    unsigned int        readmult;
    unsigned int        writeconst;
    unsigned int        writemult;

    unsigned int        eventmask;

    struct termios      original;

88 89 90
    struct list         read_q;
    struct list         write_q;
    struct list         wait_q;
91

92 93 94 95 96 97 98
    /* FIXME: add dcb, comm status, handler module, sharing */
};

static const struct object_ops serial_ops =
{
    sizeof(struct serial),        /* size */
    serial_dump,                  /* dump */
99 100 101
    default_fd_add_queue,         /* add_queue */
    default_fd_remove_queue,      /* remove_queue */
    default_fd_signaled,          /* signaled */
102
    no_satisfied,                 /* satisfied */
103
    no_signal,                    /* signal */
104
    serial_get_fd,                /* get_fd */
105
    serial_map_access,            /* map_access */
106
    no_lookup_name,               /* lookup_name */
107
    no_close_handle,              /* close_handle */
108
    serial_destroy                /* destroy */
109 110 111 112
};

static const struct fd_ops serial_fd_ops =
{
113
    serial_get_poll_events,       /* get_poll_events */
114
    serial_poll_event,            /* poll_event */
115
    serial_flush,                 /* flush */
116
    serial_get_info,              /* get_file_info */
117 118
    serial_queue_async,           /* queue_async */
    serial_cancel_async           /* cancel_async */
119 120
};

121 122
/* check if the given fd is a serial port */
int is_serial_fd( struct fd *fd )
123
{
124 125
    struct termios tios;

126 127
    return !tcgetattr( get_unix_fd(fd), &tios );
}
128

129 130 131 132
/* create a serial object for a given fd */
struct object *create_serial( struct fd *fd, unsigned int options )
{
    struct serial *serial;
133
    int unix_fd = get_unix_fd( fd );
134

135
    /* set the fd back to blocking if necessary */
136 137
    if (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT))
        fcntl( unix_fd, F_SETFL, 0 );
138

139 140
    if (!(serial = alloc_object( &serial_ops ))) return NULL;

141
    serial->options      = options;
142 143 144 145 146 147
    serial->readinterval = 0;
    serial->readmult     = 0;
    serial->readconst    = 0;
    serial->writemult    = 0;
    serial->writeconst   = 0;
    serial->eventmask    = 0;
148 149 150
    list_init( &serial->read_q );
    list_init( &serial->write_q );
    list_init( &serial->wait_q );
151 152
    serial->fd = (struct fd *)grab_object( fd );
    set_fd_user( fd, &serial_fd_ops, &serial->obj );
153
    return &serial->obj;
154 155
}

156 157 158 159 160 161
static struct fd *serial_get_fd( struct object *obj )
{
    struct serial *serial = (struct serial *)obj;
    return (struct fd *)grab_object( serial->fd );
}

162 163 164 165 166 167 168 169 170
static unsigned int serial_map_access( struct object *obj, unsigned int access )
{
    if (access & GENERIC_READ)    access |= FILE_GENERIC_READ;
    if (access & GENERIC_WRITE)   access |= FILE_GENERIC_WRITE;
    if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
    if (access & GENERIC_ALL)     access |= FILE_ALL_ACCESS;
    return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
}

171
static void serial_destroy( struct object *obj)
172 173 174
{
    struct serial *serial = (struct serial *)obj;

175 176 177
    async_terminate_queue( &serial->read_q, STATUS_CANCELLED );
    async_terminate_queue( &serial->write_q, STATUS_CANCELLED );
    async_terminate_queue( &serial->wait_q, STATUS_CANCELLED );
178
    if (serial->fd) release_object( serial->fd );
179 180
}

181
static void serial_dump( struct object *obj, int verbose )
182
{
183
    struct serial *serial = (struct serial *)obj;
184
    assert( obj->ops == &serial_ops );
185
    fprintf( stderr, "Port fd=%p mask=%x\n", serial->fd, serial->eventmask );
186 187
}

188
static struct serial *get_serial_obj( struct process *process, obj_handle_t handle, unsigned int access )
189 190 191 192
{
    return (struct serial *)get_handle_obj( process, handle, access, &serial_ops );
}

193
static int serial_get_poll_events( struct fd *fd )
194
{
195
    struct serial *serial = get_fd_user( fd );
196
    int events = 0;
197
    assert( serial->obj.ops == &serial_ops );
198

199 200 201
    if (!list_empty( &serial->read_q ))  events |= POLLIN;
    if (!list_empty( &serial->write_q )) events |= POLLOUT;
    if (!list_empty( &serial->wait_q ))  events |= POLLIN;
202 203 204

    /* fprintf(stderr,"poll events are %04x\n",events); */

205 206 207
    return events;
}

208
static int serial_get_info( struct fd *fd )
209
{
210
    int flags = 0;
211 212
    struct serial *serial = get_fd_user( fd );
    assert( serial->obj.ops == &serial_ops );
213

214
    if (!(serial->options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)))
215
        flags |= FD_FLAG_OVERLAPPED;
216 217
    else if (!(serial->readinterval == MAXDWORD &&
               serial->readmult == 0 && serial->readconst == 0))
218 219
        flags |= FD_FLAG_TIMEOUT;
    if (serial->readinterval == MAXDWORD &&
220
        serial->readmult == 0 && serial->readconst == 0)
221
        flags |= FD_FLAG_AVAILABLE;
222

223
    return flags;
224 225
}

226
static void serial_poll_event(struct fd *fd, int event)
227
{
228
    struct serial *serial = get_fd_user( fd );
229 230 231

    /* fprintf(stderr,"Poll event %02x\n",event); */

232 233
    if (!list_empty( &serial->read_q ) && (POLLIN & event) )
        async_terminate_head( &serial->read_q, STATUS_ALERTED );
234

235 236
    if (!list_empty( &serial->write_q ) && (POLLOUT & event) )
        async_terminate_head( &serial->write_q, STATUS_ALERTED );
237

238 239
    if (!list_empty( &serial->wait_q ) && (POLLIN & event) )
        async_terminate_head( &serial->wait_q, STATUS_ALERTED );
240

241
    set_fd_events( fd, serial_get_poll_events(fd) );
242 243
}

244 245
static void serial_queue_async( struct fd *fd, void *apc, void *user, void *iosb,
                                int type, int count )
246
{
247
    struct serial *serial = get_fd_user( fd );
248
    struct list *queue;
249
    struct timeval when = current_time;
250
    int timeout;
251
    int events;
252

253
    assert(serial->obj.ops == &serial_ops);
254

255
    switch (type)
256 257
    {
    case ASYNC_TYPE_READ:
258
        queue = &serial->read_q;
259 260 261
        timeout = serial->readconst + serial->readmult*count;
        break;
    case ASYNC_TYPE_WAIT:
262
        queue = &serial->wait_q;
263 264
        timeout = 0;
        break;
265
    case ASYNC_TYPE_WRITE:
266
        queue = &serial->write_q;
267 268 269 270
        timeout = serial->writeconst + serial->writemult*count;
        break;
    default:
        set_error(STATUS_INVALID_PARAMETER);
271
        return;
272 273
    }

274 275
    add_timeout( &when, timeout );
    if (!create_async( current, &when, queue, apc, user, iosb )) return;
276

277 278 279
    /* Check if the new pending request can be served immediately */
    events = check_fd_events( fd, serial_get_poll_events( fd ) );
    if (events)
280
    {
281 282 283 284
        /* serial_poll_event() calls set_select_events() */
        serial_poll_event( fd, events );
        return;
    }
285

286 287
    set_fd_events( fd, serial_get_poll_events( fd ) );
}
288

289 290 291 292
static void serial_cancel_async( struct fd *fd )
{
    struct serial *serial = get_fd_user( fd );
    assert(serial->obj.ops == &serial_ops);
293

294 295 296
    async_terminate_queue( &serial->read_q, STATUS_CANCELLED );
    async_terminate_queue( &serial->write_q, STATUS_CANCELLED );
    async_terminate_queue( &serial->wait_q, STATUS_CANCELLED );
297
}
298

299
static int serial_flush( struct fd *fd, struct event **event )
300 301 302 303
{
    /* MSDN says: If hFile is a handle to a communications device,
     * the function only flushes the transmit buffer.
     */
304
    int ret = (tcflush( get_unix_fd(fd), TCOFLUSH ) != -1);
305 306 307 308
    if (!ret) file_set_error();
    return ret;
}

309 310 311 312 313 314 315
DECL_HANDLER(get_serial_info)
{
    struct serial *serial;

    if ((serial = get_serial_obj( current->process, req->handle, 0 )))
    {
        /* timeouts */
316 317 318 319 320
        reply->readinterval = serial->readinterval;
        reply->readconst    = serial->readconst;
        reply->readmult     = serial->readmult;
        reply->writeconst   = serial->writeconst;
        reply->writemult    = serial->writemult;
321 322

        /* event mask */
323
        reply->eventmask    = serial->eventmask;
324 325 326 327 328 329 330 331 332 333 334 335

        release_object( serial );
    }
}

DECL_HANDLER(set_serial_info)
{
    struct serial *serial;

    if ((serial = get_serial_obj( current->process, req->handle, 0 )))
    {
        /* timeouts */
336
        if (req->flags & SERIALINFO_SET_TIMEOUTS)
337 338 339 340 341 342 343 344 345
        {
            serial->readinterval = req->readinterval;
            serial->readconst    = req->readconst;
            serial->readmult     = req->readmult;
            serial->writeconst   = req->writeconst;
            serial->writemult    = req->writemult;
        }

        /* event mask */
346
        if (req->flags & SERIALINFO_SET_MASK)
347 348
        {
            serial->eventmask = req->eventmask;
349
            if (!serial->eventmask)
350
            {
351
                async_terminate_queue( &serial->wait_q, STATUS_SUCCESS );
352
            }
353 354 355 356 357
        }

        release_object( serial );
    }
}