serial.c 7.23 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_flush( struct fd *fd, struct event **event );
65
static void serial_queue_async( struct fd *fd, const async_data_t *data, int type, int count );
66 67 68 69

struct serial
{
    struct object       obj;
70
    struct fd          *fd;
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

    /* 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 */
90
    no_get_type,                  /* get_type */
91 92
    add_queue,                    /* add_queue */
    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 99
    default_get_sd,               /* get_sd */
    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 */
104 105 106 107
};

static const struct fd_ops serial_fd_ops =
{
108 109
    default_fd_get_poll_events,   /* get_poll_events */
    default_poll_event,           /* poll_event */
110
    serial_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 */
116 117
};

118 119
/* check if the given fd is a serial port */
int is_serial_fd( struct fd *fd )
120
{
121 122
    struct termios tios;

123 124
    return !tcgetattr( get_unix_fd(fd), &tios );
}
125

126
/* create a serial object for a given fd */
127
struct object *create_serial( struct fd *fd )
128 129
{
    struct serial *serial;
130

131 132
    if (!(serial = alloc_object( &serial_ops ))) return NULL;

133 134 135 136 137 138
    serial->readinterval = 0;
    serial->readmult     = 0;
    serial->readconst    = 0;
    serial->writemult    = 0;
    serial->writeconst   = 0;
    serial->eventmask    = 0;
139 140
    serial->fd = (struct fd *)grab_object( fd );
    set_fd_user( fd, &serial_fd_ops, &serial->obj );
141
    return &serial->obj;
142 143
}

144 145 146 147 148 149
static struct fd *serial_get_fd( struct object *obj )
{
    struct serial *serial = (struct serial *)obj;
    return (struct fd *)grab_object( serial->fd );
}

150
static void serial_destroy( struct object *obj)
151 152
{
    struct serial *serial = (struct serial *)obj;
153
    release_object( serial->fd );
154 155
}

156
static void serial_dump( struct object *obj, int verbose )
157
{
158
    struct serial *serial = (struct serial *)obj;
159
    assert( obj->ops == &serial_ops );
160
    fprintf( stderr, "Port fd=%p mask=%x\n", serial->fd, serial->eventmask );
161 162
}

163
static struct serial *get_serial_obj( struct process *process, obj_handle_t handle, unsigned int access )
164 165 166 167
{
    return (struct serial *)get_handle_obj( process, handle, access, &serial_ops );
}

168
static enum server_fd_type serial_get_fd_type( struct fd *fd )
169
{
170
    return FD_TYPE_SERIAL;
171 172
}

173
static void serial_queue_async( struct fd *fd, const async_data_t *data, int type, int count )
174
{
175
    struct serial *serial = get_fd_user( fd );
176
    timeout_t timeout = 0;
177
    struct async *async;
178

179
    assert(serial->obj.ops == &serial_ops);
180

181
    switch (type)
182 183
    {
    case ASYNC_TYPE_READ:
184
        timeout = serial->readconst + (timeout_t)serial->readmult*count;
185
        break;
186
    case ASYNC_TYPE_WRITE:
187
        timeout = serial->writeconst + (timeout_t)serial->writemult*count;
188 189 190
        break;
    }

191
    if ((async = fd_queue_async( fd, data, type )))
192
    {
193
        if (timeout) async_set_timeout( async, timeout * -10000, STATUS_TIMEOUT );
194 195 196
        release_object( async );
        set_error( STATUS_PENDING );
    }
197
}
198

199
static void serial_flush( struct fd *fd, struct event **event )
200 201 202 203
{
    /* MSDN says: If hFile is a handle to a communications device,
     * the function only flushes the transmit buffer.
     */
204
    if (tcflush( get_unix_fd(fd), TCOFLUSH ) == -1) file_set_error();
205 206
}

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

    if ((serial = get_serial_obj( current->process, req->handle, 0 )))
    {
        /* timeouts */
214 215 216 217 218
        reply->readinterval = serial->readinterval;
        reply->readconst    = serial->readconst;
        reply->readmult     = serial->readmult;
        reply->writeconst   = serial->writeconst;
        reply->writemult    = serial->writemult;
219 220

        /* event mask */
221
        reply->eventmask    = serial->eventmask;
222 223 224 225 226 227 228 229 230 231 232 233

        release_object( serial );
    }
}

DECL_HANDLER(set_serial_info)
{
    struct serial *serial;

    if ((serial = get_serial_obj( current->process, req->handle, 0 )))
    {
        /* timeouts */
234
        if (req->flags & SERIALINFO_SET_TIMEOUTS)
235 236 237 238 239 240 241 242 243
        {
            serial->readinterval = req->readinterval;
            serial->readconst    = req->readconst;
            serial->readmult     = req->readmult;
            serial->writeconst   = req->writeconst;
            serial->writemult    = req->writemult;
        }

        /* event mask */
244
        if (req->flags & SERIALINFO_SET_MASK)
245 246
        {
            serial->eventmask = req->eventmask;
247
            if (!serial->eventmask)
248
            {
249
                fd_async_wake_up( serial->fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
250
            }
251 252 253 254 255
        }

        release_object( serial );
    }
}