serial.c 8.11 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 62
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 );
65 66 67 68

struct serial
{
    struct object       obj;
69
    struct fd          *fd;
70 71 72 73 74 75 76 77 78

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

    unsigned int        eventmask;
79
    unsigned int        generation; /* event mask change counter */
80 81
    unsigned int        pending_write : 1;
    unsigned int        pending_wait  : 1;
82 83 84 85 86 87 88 89 90 91

    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 */
92
    no_get_type,                  /* get_type */
93 94
    add_queue,                    /* add_queue */
    remove_queue,                 /* remove_queue */
95
    default_fd_signaled,          /* signaled */
96
    no_satisfied,                 /* satisfied */
97
    no_signal,                    /* signal */
98
    serial_get_fd,                /* get_fd */
99
    default_fd_map_access,        /* map_access */
100 101
    default_get_sd,               /* get_sd */
    default_set_sd,               /* set_sd */
102
    no_lookup_name,               /* lookup_name */
103
    no_open_file,                 /* open_file */
104
    fd_close_handle,              /* close_handle */
105
    serial_destroy                /* destroy */
106 107 108 109
};

static const struct fd_ops serial_fd_ops =
{
110 111
    default_fd_get_poll_events,   /* get_poll_events */
    default_poll_event,           /* poll_event */
112
    serial_get_fd_type,           /* get_fd_type */
113 114 115
    no_fd_read,                   /* read */
    no_fd_write,                  /* write */
    no_fd_flush,                  /* flush */
116
    default_fd_ioctl,             /* ioctl */
117
    serial_queue_async,           /* queue_async */
118
    default_fd_reselect_async,    /* reselect_async */
119
    default_fd_cancel_async       /* cancel_async */
120 121
};

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

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

130
/* create a serial object for a given fd */
131
struct object *create_serial( struct fd *fd )
132 133
{
    struct serial *serial;
134

135 136
    if (!(serial = alloc_object( &serial_ops ))) return NULL;

137 138 139 140 141 142
    serial->readinterval = 0;
    serial->readmult     = 0;
    serial->readconst    = 0;
    serial->writemult    = 0;
    serial->writeconst   = 0;
    serial->eventmask    = 0;
143
    serial->generation   = 0;
144
    serial->pending_write = 0;
145
    serial->pending_wait = 0;
146 147
    serial->fd = (struct fd *)grab_object( fd );
    set_fd_user( fd, &serial_fd_ops, &serial->obj );
148
    return &serial->obj;
149 150
}

151 152 153 154 155 156
static struct fd *serial_get_fd( struct object *obj )
{
    struct serial *serial = (struct serial *)obj;
    return (struct fd *)grab_object( serial->fd );
}

157
static void serial_destroy( struct object *obj)
158 159
{
    struct serial *serial = (struct serial *)obj;
160
    release_object( serial->fd );
161 162
}

163
static void serial_dump( struct object *obj, int verbose )
164
{
165
    struct serial *serial = (struct serial *)obj;
166
    assert( obj->ops == &serial_ops );
167
    fprintf( stderr, "Port fd=%p mask=%x\n", serial->fd, serial->eventmask );
168 169
}

170
static struct serial *get_serial_obj( struct process *process, obj_handle_t handle, unsigned int access )
171 172 173 174
{
    return (struct serial *)get_handle_obj( process, handle, access, &serial_ops );
}

175
static enum server_fd_type serial_get_fd_type( struct fd *fd )
176
{
177
    return FD_TYPE_SERIAL;
178 179
}

180
static void serial_queue_async( struct fd *fd, const async_data_t *data, int type, int count )
181
{
182
    struct serial *serial = get_fd_user( fd );
183
    timeout_t timeout = 0;
184
    struct async *async;
185

186
    assert(serial->obj.ops == &serial_ops);
187

188
    switch (type)
189 190
    {
    case ASYNC_TYPE_READ:
191
        timeout = serial->readconst + (timeout_t)serial->readmult*count;
192
        break;
193
    case ASYNC_TYPE_WRITE:
194
        timeout = serial->writeconst + (timeout_t)serial->writemult*count;
195 196 197
        break;
    }

198
    if ((async = fd_queue_async( fd, data, type )))
199
    {
200
        if (timeout) async_set_timeout( async, timeout * -10000, STATUS_TIMEOUT );
201 202 203
        release_object( async );
        set_error( STATUS_PENDING );
    }
204
}
205

206 207 208 209 210 211
DECL_HANDLER(get_serial_info)
{
    struct serial *serial;

    if ((serial = get_serial_obj( current->process, req->handle, 0 )))
    {
212 213 214 215 216 217 218 219 220 221 222
        if (req->flags & SERIALINFO_PENDING_WAIT)
        {
            if (serial->pending_wait)
            {
                release_object( serial );
                set_error( STATUS_INVALID_PARAMETER );
                return;
            }
            serial->pending_wait = 1;
        }

223
        /* timeouts */
224 225 226 227 228
        reply->readinterval = serial->readinterval;
        reply->readconst    = serial->readconst;
        reply->readmult     = serial->readmult;
        reply->writeconst   = serial->writeconst;
        reply->writemult    = serial->writemult;
229 230

        /* event mask */
231
        reply->eventmask    = serial->eventmask;
232
        reply->cookie       = serial->generation;
233

234 235 236 237 238
        /* pending write */
        reply->pending_write = serial->pending_write;
        if (req->flags & SERIALINFO_PENDING_WRITE)
            serial->pending_write = 0;

239 240 241 242 243 244 245 246 247 248
        release_object( serial );
    }
}

DECL_HANDLER(set_serial_info)
{
    struct serial *serial;

    if ((serial = get_serial_obj( current->process, req->handle, 0 )))
    {
249 250 251 252 253 254 255 256 257 258 259
        if (req->flags & SERIALINFO_PENDING_WAIT)
        {
            if (!serial->pending_wait)
            {
                release_object( serial );
                set_error( STATUS_INVALID_PARAMETER );
                return;
            }
            serial->pending_wait = 0;
        }

260
        /* timeouts */
261
        if (req->flags & SERIALINFO_SET_TIMEOUTS)
262 263 264 265 266 267 268 269
        {
            serial->readinterval = req->readinterval;
            serial->readconst    = req->readconst;
            serial->readmult     = req->readmult;
            serial->writeconst   = req->writeconst;
            serial->writemult    = req->writemult;
        }

270 271 272 273
        /* pending write */
        if (req->flags & SERIALINFO_PENDING_WRITE)
            serial->pending_write = 1;

274
        /* event mask */
275
        if (req->flags & SERIALINFO_SET_MASK)
276 277
        {
            serial->eventmask = req->eventmask;
278 279
            serial->generation++;
            fd_async_wake_up( serial->fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
280 281 282 283 284
        }

        release_object( serial );
    }
}