serial.c 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 20
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21 22 23
 */

#include "config.h"
24
#include "wine/port.h"
25 26 27 28 29 30 31 32 33 34

#include <assert.h>
#include <fcntl.h>
#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
35
#ifdef HAVE_UTIME_H
36
#include <utime.h>
Steven Edwards's avatar
Steven Edwards committed
37 38
#endif
#ifdef HAVE_TERMIOS_H
39
#include <termios.h>
Steven Edwards's avatar
Steven Edwards committed
40 41
#endif
#ifdef HAVE_SYS_IOCTL_H
42
#include <sys/ioctl.h>
Steven Edwards's avatar
Steven Edwards committed
43
#endif
44 45 46 47

#include "winerror.h"
#include "winbase.h"

48
#include "file.h"
49 50 51
#include "handle.h"
#include "thread.h"
#include "request.h"
52
#include "async.h"
53 54

static void serial_dump( struct object *obj, int verbose );
55 56 57 58 59 60 61
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 );
static int serial_get_info( struct fd *fd, struct get_file_info_reply *reply, int *flags );
static int serial_flush( struct fd *fd );
static void serial_queue_async(struct fd *fd, void *ptr, unsigned int status, int type, int count);
62 63 64 65

struct serial
{
    struct object       obj;
66
    unsigned int        access;
67
    unsigned int        attrib;
68 69 70 71 72 73 74 75 76 77 78 79 80

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

    unsigned int        eventmask;
    unsigned int        commerror;

    struct termios      original;

81 82 83 84
    struct async_queue  read_q;
    struct async_queue  write_q;
    struct async_queue  wait_q;

85 86 87 88 89 90 91
    /* FIXME: add dcb, comm status, handler module, sharing */
};

static const struct object_ops serial_ops =
{
    sizeof(struct serial),        /* size */
    serial_dump,                  /* dump */
92 93 94
    default_fd_add_queue,         /* add_queue */
    default_fd_remove_queue,      /* remove_queue */
    default_fd_signaled,          /* signaled */
95
    no_satisfied,                 /* satisfied */
96
    default_get_fd,               /* get_fd */
97
    serial_destroy                /* destroy */
98 99 100 101
};

static const struct fd_ops serial_fd_ops =
{
102
    serial_get_poll_events,       /* get_poll_events */
103
    serial_poll_event,            /* poll_event */
104
    serial_flush,                 /* flush */
105
    serial_get_info,              /* get_file_info */
106
    serial_queue_async            /* queue_async */
107 108
};

109
static struct serial *create_serial( const char *nameptr, size_t len, unsigned int access, int attributes )
110
{
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
    struct serial *serial;
    struct termios tios;
    int fd, flags = 0;
    char *name;

    if (!(name = mem_alloc( len + 1 ))) return NULL;
    memcpy( name, nameptr, len );
    name[len] = 0;

    switch(access & (GENERIC_READ | GENERIC_WRITE))
    {
    case GENERIC_READ:  flags |= O_RDONLY; break;
    case GENERIC_WRITE: flags |= O_WRONLY; break;
    case GENERIC_READ|GENERIC_WRITE: flags |= O_RDWR; break;
    default: break;
    }

128 129
    flags |= O_NONBLOCK;

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    fd = open( name, flags );
    free( name );
    if (fd < 0)
    {
        file_set_error();
        return NULL;
    }

    /* check its really a serial port */
    if (tcgetattr(fd,&tios))
    {
        file_set_error();
        close( fd );
        return NULL;
    }
145

146 147 148 149 150
    /* set the fd back to blocking if necessary */
    if( ! (attributes & FILE_FLAG_OVERLAPPED) )
       if(0>fcntl(fd, F_SETFL, 0))
           perror("fcntl");

151
    if ((serial = alloc_fd_object( &serial_ops, &serial_fd_ops, fd )))
152
    {
153
        serial->attrib       = attributes;
154 155 156 157 158 159 160 161
        serial->access       = access;
        serial->readinterval = 0;
        serial->readmult     = 0;
        serial->readconst    = 0;
        serial->writemult    = 0;
        serial->writeconst   = 0;
        serial->eventmask    = 0;
        serial->commerror    = 0;
162 163 164
        init_async_queue(&serial->read_q);
        init_async_queue(&serial->write_q);
        init_async_queue(&serial->wait_q);
165 166
    }
    return serial;
167 168
}

169
static void serial_destroy( struct object *obj)
170 171 172 173 174 175 176 177
{
    struct serial *serial = (struct serial *)obj;

    destroy_async_queue(&serial->read_q);
    destroy_async_queue(&serial->write_q);
    destroy_async_queue(&serial->wait_q);
}

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

185
struct serial *get_serial_obj( struct process *process, obj_handle_t handle, unsigned int access )
186 187 188 189
{
    return (struct serial *)get_handle_obj( process, handle, access, &serial_ops );
}

190
static int serial_get_poll_events( struct fd *fd )
191
{
192
    struct serial *serial = get_fd_user( fd );
193
    int events = 0;
194
    assert( serial->obj.ops == &serial_ops );
195 196 197 198 199 200 201 202 203 204

    if(IS_READY(serial->read_q))
        events |= POLLIN;
    if(IS_READY(serial->write_q))
        events |= POLLOUT;
    if(IS_READY(serial->wait_q))
        events |= POLLIN;

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

205 206 207
    return events;
}

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

213
    if (reply)
214
    {
215 216 217 218 219 220 221 222 223 224
        reply->type        = FILE_TYPE_CHAR;
        reply->attr        = 0;
        reply->access_time = 0;
        reply->write_time  = 0;
        reply->size_high   = 0;
        reply->size_low    = 0;
        reply->links       = 0;
        reply->index_high  = 0;
        reply->index_low   = 0;
        reply->serial      = 0;
225
    }
226

227
    *flags = 0;
228
    if(serial->attrib & FILE_FLAG_OVERLAPPED)
229 230 231 232
        *flags |= FD_FLAG_OVERLAPPED;
    else if(!((serial->readinterval == MAXDWORD) &&
              (serial->readmult == 0) && (serial->readconst == 0)) )
        *flags |= FD_FLAG_TIMEOUT;
233

234
    return FD_TYPE_DEFAULT;
235 236
}

237
static void serial_poll_event(struct fd *fd, int event)
238
{
239
    struct serial *serial = get_fd_user( fd );
240 241 242 243 244 245 246 247 248 249 250 251

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

    if(IS_READY(serial->read_q) && (POLLIN & event) )
        async_notify(serial->read_q.head,STATUS_ALERTED);

    if(IS_READY(serial->write_q) && (POLLOUT & event) )
        async_notify(serial->write_q.head,STATUS_ALERTED);

    if(IS_READY(serial->wait_q) && (POLLIN & event) )
        async_notify(serial->wait_q.head,STATUS_ALERTED);

252
    set_select_events( &serial->obj, serial_get_poll_events(fd) );
253 254
}

255
static void serial_queue_async(struct fd *fd, void *ptr, unsigned int status, int type, int count)
256
{
257
    struct serial *serial = get_fd_user( fd );
258
    struct async_queue *q;
259
    struct async *async;
260
    int timeout;
261

262
    assert(serial->obj.ops == &serial_ops);
263

264
    switch(type)
265 266
    {
    case ASYNC_TYPE_READ:
267 268 269 270 271 272 273
        q = &serial->read_q;
        timeout = serial->readconst + serial->readmult*count;
        break;
    case ASYNC_TYPE_WAIT:
        q = &serial->wait_q;
        timeout = 0;
        break;
274
    case ASYNC_TYPE_WRITE:
275 276 277 278 279
        q = &serial->write_q;
        timeout = serial->writeconst + serial->writemult*count;
        break;
    default:
        set_error(STATUS_INVALID_PARAMETER);
280
        return;
281 282
    }

283 284 285
    async = find_async ( q, current, ptr );

    if ( status == STATUS_PENDING )
286
    {
287
        int events;
288

289
        if ( !async )
290
            async = create_async ( &serial->obj, current, ptr );
291 292 293 294
        if ( !async )
            return;

        async->status = STATUS_PENDING;
295 296 297 298
        if(!async->q)
        {
            async_add_timeout(async,timeout);
            async_insert(q, async);
299
        }
300 301

        /* Check if the new pending request can be served immediately */
302 303 304
        events = check_fd_events( fd, serial_get_poll_events( fd ) );
        if (events)
        {
305
            /* serial_poll_event() calls set_select_events() */
306 307 308
            serial_poll_event( fd, events );
            return;
        }
309
    }
310 311
    else if ( async ) destroy_async ( async );
    else set_error ( STATUS_INVALID_PARAMETER );
312

313
    set_select_events ( &serial->obj, serial_get_poll_events( fd ));
314
}
315

316
static int serial_flush( struct fd *fd )
317 318 319 320
{
    /* MSDN says: If hFile is a handle to a communications device,
     * the function only flushes the transmit buffer.
     */
321
    int ret = (tcflush( get_unix_fd(fd), TCOFLUSH ) != -1);
322 323 324 325
    if (!ret) file_set_error();
    return ret;
}

326 327 328 329 330
/* create a serial */
DECL_HANDLER(create_serial)
{
    struct serial *serial;

331 332
    reply->handle = 0;
    if ((serial = create_serial( get_req_data(), get_req_data_size(), req->access, req->attributes )))
333
    {
334
        reply->handle = alloc_handle( current->process, serial, req->access, req->inherit );
335 336 337 338
        release_object( serial );
    }
}

339 340 341 342 343 344 345
DECL_HANDLER(get_serial_info)
{
    struct serial *serial;

    if ((serial = get_serial_obj( current->process, req->handle, 0 )))
    {
        /* timeouts */
346 347 348 349 350
        reply->readinterval = serial->readinterval;
        reply->readconst    = serial->readconst;
        reply->readmult     = serial->readmult;
        reply->writeconst   = serial->writeconst;
        reply->writemult    = serial->writemult;
351 352

        /* event mask */
353
        reply->eventmask    = serial->eventmask;
354 355

        /* comm port error status */
356
        reply->commerror    = serial->commerror;
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381

        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;
382 383 384 385 386 387 388 389
            if(!serial->eventmask)
            {
                while(serial->wait_q.head)
                {
                    async_notify(serial->wait_q.head, STATUS_SUCCESS);
                    destroy_async(serial->wait_q.head);
                }
            }
390 391 392 393 394 395 396 397 398 399 400
        }

        /* comm port error status */
        if(req->flags & SERIALINFO_SET_ERROR)
        {
            serial->commerror = req->commerror;
        }

        release_object( serial );
    }
}