2000-09-07 20:39:51 +02:00
|
|
|
/*
|
|
|
|
* Server-side serial port communications management
|
|
|
|
*
|
|
|
|
* Copyright (C) 1998 Alexandre Julliard
|
2001-12-20 01:07:05 +01:00
|
|
|
* Copyright (C) 2000,2001 Mike McCormack
|
2000-09-07 20:39:51 +02:00
|
|
|
*
|
2002-03-10 00:29:33 +01:00
|
|
|
*
|
|
|
|
* 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
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2000-09-07 20:39:51 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
2002-04-26 20:31:19 +02:00
|
|
|
#include "wine/port.h"
|
2000-09-07 20:39:51 +02:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <fcntl.h>
|
2003-09-06 01:08:26 +02:00
|
|
|
#include <stdarg.h>
|
2000-09-07 20:39:51 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
2003-02-11 23:27:13 +01:00
|
|
|
#ifdef HAVE_UTIME_H
|
2000-09-07 20:39:51 +02:00
|
|
|
#include <utime.h>
|
2003-02-11 23:27:13 +01:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_TERMIOS_H
|
2000-09-07 20:39:51 +02:00
|
|
|
#include <termios.h>
|
2003-02-11 23:27:13 +01:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_IOCTL_H
|
2000-09-07 20:39:51 +02:00
|
|
|
#include <sys/ioctl.h>
|
2003-02-11 23:27:13 +01:00
|
|
|
#endif
|
2005-03-04 13:38:36 +01:00
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
#include <poll.h>
|
|
|
|
#endif
|
2000-09-07 20:39:51 +02:00
|
|
|
|
2005-11-28 17:32:54 +01:00
|
|
|
#include "ntstatus.h"
|
|
|
|
#define WIN32_NO_STATUS
|
2003-09-06 01:08:26 +02:00
|
|
|
#include "windef.h"
|
2004-03-18 05:08:48 +01:00
|
|
|
#include "winternl.h"
|
2000-09-07 20:39:51 +02:00
|
|
|
|
2003-01-30 01:26:44 +01:00
|
|
|
#include "file.h"
|
2000-09-07 20:39:51 +02:00
|
|
|
#include "handle.h"
|
|
|
|
#include "thread.h"
|
|
|
|
#include "request.h"
|
|
|
|
|
|
|
|
static void serial_dump( struct object *obj, int verbose );
|
2003-02-19 01:33:32 +01:00
|
|
|
static struct fd *serial_get_fd( struct object *obj );
|
2005-12-12 16:46:17 +01:00
|
|
|
static unsigned int serial_map_access( struct object *obj, unsigned int access );
|
2003-02-14 21:27:09 +01:00
|
|
|
static void serial_destroy(struct object *obj);
|
|
|
|
|
2007-04-10 22:26:23 +02:00
|
|
|
static enum server_fd_type serial_get_fd_type( struct fd *fd );
|
2007-03-27 16:51:44 +02:00
|
|
|
static void serial_flush( struct fd *fd, struct event **event );
|
2007-03-20 20:21:12 +01:00
|
|
|
static void serial_queue_async( struct fd *fd, const async_data_t *data, int type, int count );
|
2000-09-07 20:39:51 +02:00
|
|
|
|
|
|
|
struct serial
|
|
|
|
{
|
|
|
|
struct object obj;
|
2003-02-19 01:33:32 +01:00
|
|
|
struct fd *fd;
|
2000-09-07 20:39:51 +02:00
|
|
|
|
|
|
|
/* 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 */
|
2007-04-04 19:39:29 +02:00
|
|
|
add_queue, /* add_queue */
|
|
|
|
remove_queue, /* remove_queue */
|
2003-01-30 01:26:44 +01:00
|
|
|
default_fd_signaled, /* signaled */
|
2000-09-07 20:39:51 +02:00
|
|
|
no_satisfied, /* satisfied */
|
2005-04-24 19:35:52 +02:00
|
|
|
no_signal, /* signal */
|
2003-02-19 01:33:32 +01:00
|
|
|
serial_get_fd, /* get_fd */
|
2005-12-12 16:46:17 +01:00
|
|
|
serial_map_access, /* map_access */
|
2005-11-22 15:55:42 +01:00
|
|
|
no_lookup_name, /* lookup_name */
|
2007-03-22 11:44:29 +01:00
|
|
|
no_open_file, /* open_file */
|
2006-11-02 20:52:22 +01:00
|
|
|
fd_close_handle, /* close_handle */
|
2003-02-14 21:27:09 +01:00
|
|
|
serial_destroy /* destroy */
|
2003-01-30 01:26:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct fd_ops serial_fd_ops =
|
|
|
|
{
|
2007-04-02 12:49:09 +02:00
|
|
|
default_fd_get_poll_events, /* get_poll_events */
|
|
|
|
default_poll_event, /* poll_event */
|
2002-07-16 03:20:38 +02:00
|
|
|
serial_flush, /* flush */
|
2007-04-10 22:26:23 +02:00
|
|
|
serial_get_fd_type, /* get_file_info */
|
2005-01-14 20:54:38 +01:00
|
|
|
serial_queue_async, /* queue_async */
|
2007-04-10 17:07:27 +02:00
|
|
|
default_fd_reselect_async, /* reselect_async */
|
2007-04-02 12:49:09 +02:00
|
|
|
default_fd_cancel_async /* cancel_async */
|
2000-09-07 20:39:51 +02:00
|
|
|
};
|
|
|
|
|
2004-03-18 05:08:48 +01:00
|
|
|
/* check if the given fd is a serial port */
|
|
|
|
int is_serial_fd( struct fd *fd )
|
2000-09-07 20:39:51 +02:00
|
|
|
{
|
2000-10-15 02:40:25 +02:00
|
|
|
struct termios tios;
|
|
|
|
|
2004-03-18 05:08:48 +01:00
|
|
|
return !tcgetattr( get_unix_fd(fd), &tios );
|
|
|
|
}
|
2001-08-21 19:01:48 +02:00
|
|
|
|
2004-03-18 05:08:48 +01:00
|
|
|
/* create a serial object for a given fd */
|
2007-04-10 22:25:07 +02:00
|
|
|
struct object *create_serial( struct fd *fd )
|
2004-03-18 05:08:48 +01:00
|
|
|
{
|
|
|
|
struct serial *serial;
|
2001-10-08 22:40:57 +02:00
|
|
|
|
2006-01-24 13:31:48 +01:00
|
|
|
if (!(serial = alloc_object( &serial_ops ))) return NULL;
|
|
|
|
|
2003-03-12 23:38:14 +01:00
|
|
|
serial->readinterval = 0;
|
|
|
|
serial->readmult = 0;
|
|
|
|
serial->readconst = 0;
|
|
|
|
serial->writemult = 0;
|
|
|
|
serial->writeconst = 0;
|
|
|
|
serial->eventmask = 0;
|
2006-01-24 13:31:48 +01:00
|
|
|
serial->fd = (struct fd *)grab_object( fd );
|
|
|
|
set_fd_user( fd, &serial_fd_ops, &serial->obj );
|
2004-03-18 05:08:48 +01:00
|
|
|
return &serial->obj;
|
2000-09-07 20:39:51 +02:00
|
|
|
}
|
|
|
|
|
2003-02-19 01:33:32 +01:00
|
|
|
static struct fd *serial_get_fd( struct object *obj )
|
|
|
|
{
|
|
|
|
struct serial *serial = (struct serial *)obj;
|
|
|
|
return (struct fd *)grab_object( serial->fd );
|
|
|
|
}
|
|
|
|
|
2005-12-12 16:46:17 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2003-02-14 21:27:09 +01:00
|
|
|
static void serial_destroy( struct object *obj)
|
2001-12-20 01:07:05 +01:00
|
|
|
{
|
|
|
|
struct serial *serial = (struct serial *)obj;
|
2007-04-02 12:49:09 +02:00
|
|
|
release_object( serial->fd );
|
2001-12-20 01:07:05 +01:00
|
|
|
}
|
|
|
|
|
2000-10-15 02:40:25 +02:00
|
|
|
static void serial_dump( struct object *obj, int verbose )
|
2000-09-07 20:39:51 +02:00
|
|
|
{
|
2000-10-15 02:40:25 +02:00
|
|
|
struct serial *serial = (struct serial *)obj;
|
2000-09-07 20:39:51 +02:00
|
|
|
assert( obj->ops == &serial_ops );
|
2003-02-19 01:33:32 +01:00
|
|
|
fprintf( stderr, "Port fd=%p mask=%x\n", serial->fd, serial->eventmask );
|
2000-09-07 20:39:51 +02:00
|
|
|
}
|
|
|
|
|
2003-02-19 01:33:32 +01:00
|
|
|
static struct serial *get_serial_obj( struct process *process, obj_handle_t handle, unsigned int access )
|
2000-09-07 20:39:51 +02:00
|
|
|
{
|
|
|
|
return (struct serial *)get_handle_obj( process, handle, access, &serial_ops );
|
|
|
|
}
|
|
|
|
|
2007-04-10 22:26:23 +02:00
|
|
|
static enum server_fd_type serial_get_fd_type( struct fd *fd )
|
2000-09-07 20:39:51 +02:00
|
|
|
{
|
2006-11-20 14:14:04 +01:00
|
|
|
return FD_TYPE_SERIAL;
|
2000-09-07 20:39:51 +02:00
|
|
|
}
|
|
|
|
|
2007-03-20 20:21:12 +01:00
|
|
|
static void serial_queue_async( struct fd *fd, const async_data_t *data, int type, int count )
|
2000-11-13 20:27:21 +01:00
|
|
|
{
|
2003-02-14 21:27:09 +01:00
|
|
|
struct serial *serial = get_fd_user( fd );
|
2007-04-02 12:49:09 +02:00
|
|
|
int timeout = 0;
|
2007-04-02 20:41:59 +02:00
|
|
|
struct async *async;
|
2000-11-13 20:27:21 +01:00
|
|
|
|
2003-02-14 21:27:09 +01:00
|
|
|
assert(serial->obj.ops == &serial_ops);
|
2000-11-13 20:27:21 +01:00
|
|
|
|
2005-01-14 20:54:38 +01:00
|
|
|
switch (type)
|
2000-11-13 20:27:21 +01:00
|
|
|
{
|
|
|
|
case ASYNC_TYPE_READ:
|
2001-12-20 01:07:05 +01:00
|
|
|
timeout = serial->readconst + serial->readmult*count;
|
|
|
|
break;
|
2000-11-13 20:27:21 +01:00
|
|
|
case ASYNC_TYPE_WRITE:
|
2001-12-20 01:07:05 +01:00
|
|
|
timeout = serial->writeconst + serial->writemult*count;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-04-02 20:41:59 +02:00
|
|
|
if ((async = fd_queue_async( fd, data, type, count )))
|
|
|
|
{
|
|
|
|
if (timeout)
|
|
|
|
{
|
|
|
|
struct timeval when = current_time;
|
|
|
|
add_timeout( &when, timeout );
|
2007-04-03 19:12:31 +02:00
|
|
|
async_set_timeout( async, &when, STATUS_TIMEOUT );
|
2007-04-02 20:41:59 +02:00
|
|
|
}
|
|
|
|
release_object( async );
|
|
|
|
set_error( STATUS_PENDING );
|
|
|
|
}
|
2001-12-20 01:07:05 +01:00
|
|
|
}
|
2000-11-13 20:27:21 +01:00
|
|
|
|
2007-03-27 16:51:44 +02:00
|
|
|
static void serial_flush( struct fd *fd, struct event **event )
|
2002-07-16 03:20:38 +02:00
|
|
|
{
|
|
|
|
/* MSDN says: If hFile is a handle to a communications device,
|
|
|
|
* the function only flushes the transmit buffer.
|
|
|
|
*/
|
2007-03-27 16:51:44 +02:00
|
|
|
if (tcflush( get_unix_fd(fd), TCOFLUSH ) == -1) file_set_error();
|
2002-07-16 03:20:38 +02:00
|
|
|
}
|
|
|
|
|
2000-09-16 22:55:12 +02:00
|
|
|
DECL_HANDLER(get_serial_info)
|
|
|
|
{
|
|
|
|
struct serial *serial;
|
|
|
|
|
|
|
|
if ((serial = get_serial_obj( current->process, req->handle, 0 )))
|
|
|
|
{
|
|
|
|
/* timeouts */
|
2001-11-30 19:46:42 +01:00
|
|
|
reply->readinterval = serial->readinterval;
|
|
|
|
reply->readconst = serial->readconst;
|
|
|
|
reply->readmult = serial->readmult;
|
|
|
|
reply->writeconst = serial->writeconst;
|
|
|
|
reply->writemult = serial->writemult;
|
2000-09-16 22:55:12 +02:00
|
|
|
|
|
|
|
/* event mask */
|
2001-11-30 19:46:42 +01:00
|
|
|
reply->eventmask = serial->eventmask;
|
2000-09-16 22:55:12 +02:00
|
|
|
|
|
|
|
release_object( serial );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DECL_HANDLER(set_serial_info)
|
|
|
|
{
|
|
|
|
struct serial *serial;
|
|
|
|
|
|
|
|
if ((serial = get_serial_obj( current->process, req->handle, 0 )))
|
|
|
|
{
|
|
|
|
/* timeouts */
|
2005-01-14 20:54:38 +01:00
|
|
|
if (req->flags & SERIALINFO_SET_TIMEOUTS)
|
2000-09-16 22:55:12 +02:00
|
|
|
{
|
|
|
|
serial->readinterval = req->readinterval;
|
|
|
|
serial->readconst = req->readconst;
|
|
|
|
serial->readmult = req->readmult;
|
|
|
|
serial->writeconst = req->writeconst;
|
|
|
|
serial->writemult = req->writemult;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* event mask */
|
2005-01-14 20:54:38 +01:00
|
|
|
if (req->flags & SERIALINFO_SET_MASK)
|
2000-09-16 22:55:12 +02:00
|
|
|
{
|
|
|
|
serial->eventmask = req->eventmask;
|
2005-01-14 20:54:38 +01:00
|
|
|
if (!serial->eventmask)
|
2002-03-12 20:19:57 +01:00
|
|
|
{
|
2007-04-02 20:09:29 +02:00
|
|
|
fd_async_wake_up( serial->fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
|
2002-03-12 20:19:57 +01:00
|
|
|
}
|
2000-09-16 22:55:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
release_object( serial );
|
|
|
|
}
|
|
|
|
}
|