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
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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
|
2000-09-07 20:39:51 +02:00
|
|
|
|
|
|
|
#include "winerror.h"
|
2003-09-06 01:08:26 +02:00
|
|
|
#include "windef.h"
|
2000-09-07 20:39:51 +02:00
|
|
|
#include "winbase.h"
|
2004-03-18 05:08:48 +01:00
|
|
|
#include "winreg.h"
|
|
|
|
#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"
|
2001-12-20 01:07:05 +01:00
|
|
|
#include "async.h"
|
2000-09-07 20:39:51 +02:00
|
|
|
|
|
|
|
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 );
|
2003-02-14 21:27:09 +01:00
|
|
|
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 );
|
2004-08-18 02:04:58 +02:00
|
|
|
static int serial_get_info( struct fd *fd );
|
2003-05-15 06:22:45 +02:00
|
|
|
static int serial_flush( struct fd *fd, struct event **event );
|
2003-02-14 21:27:09 +01:00
|
|
|
static void serial_queue_async(struct fd *fd, void *ptr, unsigned int status, 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;
|
2004-03-18 05:08:48 +01:00
|
|
|
unsigned int options;
|
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;
|
|
|
|
unsigned int commerror;
|
|
|
|
|
|
|
|
struct termios original;
|
|
|
|
|
2001-12-20 01:07:05 +01:00
|
|
|
struct async_queue read_q;
|
|
|
|
struct async_queue write_q;
|
|
|
|
struct async_queue wait_q;
|
|
|
|
|
2000-09-07 20:39:51 +02:00
|
|
|
/* FIXME: add dcb, comm status, handler module, sharing */
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct object_ops serial_ops =
|
|
|
|
{
|
|
|
|
sizeof(struct serial), /* size */
|
|
|
|
serial_dump, /* dump */
|
2003-01-30 01:26:44 +01:00
|
|
|
default_fd_add_queue, /* add_queue */
|
|
|
|
default_fd_remove_queue, /* remove_queue */
|
|
|
|
default_fd_signaled, /* signaled */
|
2000-09-07 20:39:51 +02:00
|
|
|
no_satisfied, /* satisfied */
|
2003-02-19 01:33:32 +01:00
|
|
|
serial_get_fd, /* get_fd */
|
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 =
|
|
|
|
{
|
2000-09-07 20:39:51 +02:00
|
|
|
serial_get_poll_events, /* get_poll_events */
|
2001-12-20 01:07:05 +01:00
|
|
|
serial_poll_event, /* poll_event */
|
2002-07-16 03:20:38 +02:00
|
|
|
serial_flush, /* flush */
|
2000-09-07 20:39:51 +02:00
|
|
|
serial_get_info, /* get_file_info */
|
2003-01-30 01:26:44 +01:00
|
|
|
serial_queue_async /* queue_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 */
|
|
|
|
struct object *create_serial( struct fd *fd, unsigned int options )
|
|
|
|
{
|
|
|
|
struct serial *serial;
|
|
|
|
int unix_fd;
|
2000-10-15 02:40:25 +02:00
|
|
|
|
2004-03-18 05:08:48 +01:00
|
|
|
if ((unix_fd = dup( get_unix_fd(fd) )) == -1) return NULL;
|
2000-09-07 20:39:51 +02:00
|
|
|
|
2001-10-08 22:40:57 +02:00
|
|
|
/* set the fd back to blocking if necessary */
|
2004-03-18 05:08:48 +01:00
|
|
|
if (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT))
|
|
|
|
fcntl( unix_fd, F_SETFL, 0 );
|
2001-10-08 22:40:57 +02:00
|
|
|
|
2003-03-12 23:38:14 +01:00
|
|
|
if (!(serial = alloc_object( &serial_ops )))
|
2000-10-15 02:40:25 +02:00
|
|
|
{
|
2004-03-18 05:08:48 +01:00
|
|
|
close( unix_fd );
|
2003-03-12 23:38:14 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2004-03-18 05:08:48 +01:00
|
|
|
serial->options = options;
|
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;
|
|
|
|
serial->commerror = 0;
|
|
|
|
init_async_queue(&serial->read_q);
|
|
|
|
init_async_queue(&serial->write_q);
|
|
|
|
init_async_queue(&serial->wait_q);
|
2004-03-18 05:08:48 +01:00
|
|
|
if (!(serial->fd = create_anonymous_fd( &serial_fd_ops, unix_fd, &serial->obj )))
|
2003-03-12 23:38:14 +01:00
|
|
|
{
|
|
|
|
release_object( serial );
|
|
|
|
return NULL;
|
2000-10-15 02:40:25 +02:00
|
|
|
}
|
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 );
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
destroy_async_queue(&serial->read_q);
|
|
|
|
destroy_async_queue(&serial->write_q);
|
|
|
|
destroy_async_queue(&serial->wait_q);
|
2003-02-19 01:33:32 +01:00
|
|
|
if (serial->fd) 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 );
|
|
|
|
}
|
|
|
|
|
2003-02-14 21:27:09 +01:00
|
|
|
static int serial_get_poll_events( struct fd *fd )
|
2000-09-07 20:39:51 +02:00
|
|
|
{
|
2003-02-14 21:27:09 +01:00
|
|
|
struct serial *serial = get_fd_user( fd );
|
2000-09-07 20:39:51 +02:00
|
|
|
int events = 0;
|
2003-02-14 21:27:09 +01:00
|
|
|
assert( serial->obj.ops == &serial_ops );
|
2001-12-20 01:07:05 +01:00
|
|
|
|
|
|
|
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); */
|
|
|
|
|
2000-09-07 20:39:51 +02:00
|
|
|
return events;
|
|
|
|
}
|
|
|
|
|
2004-08-18 02:04:58 +02:00
|
|
|
static int serial_get_info( struct fd *fd )
|
2000-09-07 20:39:51 +02:00
|
|
|
{
|
2004-08-18 02:04:58 +02:00
|
|
|
int flags = 0;
|
2003-02-14 21:27:09 +01:00
|
|
|
struct serial *serial = get_fd_user( fd );
|
|
|
|
assert( serial->obj.ops == &serial_ops );
|
2001-10-08 22:40:57 +02:00
|
|
|
|
2004-03-18 05:08:48 +01:00
|
|
|
if (!(serial->options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)))
|
2004-08-18 02:04:58 +02:00
|
|
|
flags |= FD_FLAG_OVERLAPPED;
|
2004-08-18 01:37:55 +02:00
|
|
|
else if (!(serial->readinterval == MAXDWORD &&
|
|
|
|
serial->readmult == 0 && serial->readconst == 0))
|
2004-08-18 02:04:58 +02:00
|
|
|
flags |= FD_FLAG_TIMEOUT;
|
|
|
|
if (serial->readinterval == MAXDWORD &&
|
2004-08-18 01:37:55 +02:00
|
|
|
serial->readmult == 0 && serial->readconst == 0)
|
2004-08-18 02:04:58 +02:00
|
|
|
flags |= FD_FLAG_AVAILABLE;
|
2001-10-08 22:40:57 +02:00
|
|
|
|
2004-08-18 02:04:58 +02:00
|
|
|
return flags;
|
2000-09-07 20:39:51 +02:00
|
|
|
}
|
|
|
|
|
2003-02-14 21:27:09 +01:00
|
|
|
static void serial_poll_event(struct fd *fd, int event)
|
2001-12-20 01:07:05 +01:00
|
|
|
{
|
2003-02-14 21:27:09 +01:00
|
|
|
struct serial *serial = get_fd_user( fd );
|
2001-12-20 01:07:05 +01:00
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
2003-02-19 01:33:32 +01:00
|
|
|
set_fd_events( fd, serial_get_poll_events(fd) );
|
2001-12-20 01:07:05 +01:00
|
|
|
}
|
|
|
|
|
2003-02-14 21:27:09 +01:00
|
|
|
static void serial_queue_async(struct fd *fd, void *ptr, unsigned int status, 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 );
|
2001-12-20 01:07:05 +01:00
|
|
|
struct async_queue *q;
|
2002-04-24 23:29:54 +02:00
|
|
|
struct async *async;
|
2001-12-20 01:07:05 +01:00
|
|
|
int timeout;
|
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
|
|
|
|
2001-03-22 21:09:34 +01:00
|
|
|
switch(type)
|
2000-11-13 20:27:21 +01:00
|
|
|
{
|
|
|
|
case ASYNC_TYPE_READ:
|
2001-12-20 01:07:05 +01:00
|
|
|
q = &serial->read_q;
|
|
|
|
timeout = serial->readconst + serial->readmult*count;
|
|
|
|
break;
|
|
|
|
case ASYNC_TYPE_WAIT:
|
|
|
|
q = &serial->wait_q;
|
|
|
|
timeout = 0;
|
|
|
|
break;
|
2000-11-13 20:27:21 +01:00
|
|
|
case ASYNC_TYPE_WRITE:
|
2001-12-20 01:07:05 +01:00
|
|
|
q = &serial->write_q;
|
|
|
|
timeout = serial->writeconst + serial->writemult*count;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
set_error(STATUS_INVALID_PARAMETER);
|
2002-04-24 23:29:54 +02:00
|
|
|
return;
|
2001-12-20 01:07:05 +01:00
|
|
|
}
|
|
|
|
|
2002-04-24 23:29:54 +02:00
|
|
|
async = find_async ( q, current, ptr );
|
|
|
|
|
|
|
|
if ( status == STATUS_PENDING )
|
2001-12-20 01:07:05 +01:00
|
|
|
{
|
2003-02-14 21:27:09 +01:00
|
|
|
int events;
|
2002-04-26 20:31:19 +02:00
|
|
|
|
2002-04-24 23:29:54 +02:00
|
|
|
if ( !async )
|
2003-02-14 21:27:09 +01:00
|
|
|
async = create_async ( &serial->obj, current, ptr );
|
2002-04-24 23:29:54 +02:00
|
|
|
if ( !async )
|
|
|
|
return;
|
|
|
|
|
|
|
|
async->status = STATUS_PENDING;
|
2001-12-20 01:07:05 +01:00
|
|
|
if(!async->q)
|
|
|
|
{
|
|
|
|
async_add_timeout(async,timeout);
|
|
|
|
async_insert(q, async);
|
2002-04-24 23:29:54 +02:00
|
|
|
}
|
2002-04-26 20:31:19 +02:00
|
|
|
|
|
|
|
/* Check if the new pending request can be served immediately */
|
2003-02-14 21:27:09 +01:00
|
|
|
events = check_fd_events( fd, serial_get_poll_events( fd ) );
|
|
|
|
if (events)
|
|
|
|
{
|
2002-04-26 20:31:19 +02:00
|
|
|
/* serial_poll_event() calls set_select_events() */
|
2003-02-14 21:27:09 +01:00
|
|
|
serial_poll_event( fd, events );
|
|
|
|
return;
|
|
|
|
}
|
2000-11-13 20:27:21 +01:00
|
|
|
}
|
2002-04-24 23:29:54 +02:00
|
|
|
else if ( async ) destroy_async ( async );
|
|
|
|
else set_error ( STATUS_INVALID_PARAMETER );
|
2000-11-13 20:27:21 +01:00
|
|
|
|
2004-08-18 01:37:55 +02:00
|
|
|
set_fd_events ( fd, serial_get_poll_events( fd ) );
|
2001-12-20 01:07:05 +01:00
|
|
|
}
|
2000-11-13 20:27:21 +01:00
|
|
|
|
2003-05-15 06:22:45 +02:00
|
|
|
static int 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.
|
|
|
|
*/
|
2003-02-14 21:27:09 +01:00
|
|
|
int ret = (tcflush( get_unix_fd(fd), TCOFLUSH ) != -1);
|
2002-07-16 03:20:38 +02:00
|
|
|
if (!ret) file_set_error();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
/* comm port error status */
|
2001-11-30 19:46:42 +01:00
|
|
|
reply->commerror = serial->commerror;
|
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 */
|
|
|
|
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;
|
2002-03-12 20:19:57 +01:00
|
|
|
if(!serial->eventmask)
|
|
|
|
{
|
|
|
|
while(serial->wait_q.head)
|
|
|
|
{
|
|
|
|
async_notify(serial->wait_q.head, STATUS_SUCCESS);
|
|
|
|
destroy_async(serial->wait_q.head);
|
|
|
|
}
|
|
|
|
}
|
2000-09-16 22:55:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* comm port error status */
|
|
|
|
if(req->flags & SERIALINFO_SET_ERROR)
|
|
|
|
{
|
|
|
|
serial->commerror = req->commerror;
|
|
|
|
}
|
|
|
|
|
|
|
|
release_object( serial );
|
|
|
|
}
|
|
|
|
}
|