2011-10-04 22:15:15 +04:00
|
|
|
/**
|
2012-10-09 07:02:04 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2011-10-04 22:15:15 +04:00
|
|
|
* Serial Port Device Service Virtual Channel
|
|
|
|
*
|
|
|
|
* Copyright 2011 O.S. Systems Software Ltda.
|
|
|
|
* Copyright 2011 Eduardo Fiss Beloni <beloni@ossystems.com.br>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:09:01 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2011-10-04 22:15:15 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_MODEM_H
|
|
|
|
#include <sys/modem.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_FILIO_H
|
|
|
|
#include <sys/filio.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_STRTIO_H
|
|
|
|
#include <sys/strtio.h>
|
|
|
|
#endif
|
2011-10-27 21:41:39 +04:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2011-10-04 22:15:15 +04:00
|
|
|
|
|
|
|
#include "serial_tty.h"
|
|
|
|
#include "serial_constants.h"
|
|
|
|
|
2012-12-17 08:34:07 +04:00
|
|
|
#include <winpr/crt.h>
|
2013-03-21 05:22:47 +04:00
|
|
|
#include <winpr/synch.h>
|
|
|
|
#include <winpr/thread.h>
|
|
|
|
#include <winpr/collections.h>
|
2012-12-17 08:34:07 +04:00
|
|
|
|
2012-10-09 05:00:07 +04:00
|
|
|
#include <freerdp/freerdp.h>
|
2013-03-21 05:22:47 +04:00
|
|
|
#include <freerdp/utils/list.h>
|
2013-03-22 00:45:25 +04:00
|
|
|
#include <winpr/stream.h>
|
2012-10-09 05:00:07 +04:00
|
|
|
#include <freerdp/channels/rdpdr.h>
|
2011-10-04 22:15:15 +04:00
|
|
|
|
|
|
|
typedef struct _SERIAL_DEVICE SERIAL_DEVICE;
|
2012-10-09 05:00:07 +04:00
|
|
|
|
2011-10-04 22:15:15 +04:00
|
|
|
struct _SERIAL_DEVICE
|
|
|
|
{
|
|
|
|
DEVICE device;
|
|
|
|
|
|
|
|
char* path;
|
|
|
|
SERIAL_TTY* tty;
|
|
|
|
|
2013-03-21 05:22:47 +04:00
|
|
|
HANDLE thread;
|
2013-08-16 17:51:44 +04:00
|
|
|
HANDLE mthread;
|
2013-03-21 05:22:47 +04:00
|
|
|
HANDLE stopEvent;
|
2013-08-16 17:51:44 +04:00
|
|
|
HANDLE newEvent;
|
2013-03-21 05:22:47 +04:00
|
|
|
|
|
|
|
wQueue* queue;
|
2011-10-04 22:15:15 +04:00
|
|
|
LIST* pending_irps;
|
|
|
|
|
|
|
|
fd_set read_fds;
|
|
|
|
fd_set write_fds;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 nfds;
|
2011-10-04 22:15:15 +04:00
|
|
|
struct timeval tv;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 select_timeout;
|
|
|
|
UINT32 timeout_id;
|
2011-10-04 22:15:15 +04:00
|
|
|
};
|
|
|
|
|
2012-10-09 11:26:39 +04:00
|
|
|
static void serial_abort_single_io(SERIAL_DEVICE* serial, UINT32 file_id, UINT32 abort_io, UINT32 io_status);
|
2011-10-04 22:15:15 +04:00
|
|
|
static void serial_check_for_events(SERIAL_DEVICE* serial);
|
|
|
|
static void serial_handle_async_irp(SERIAL_DEVICE* serial, IRP* irp);
|
2012-10-09 10:38:39 +04:00
|
|
|
static BOOL serial_check_fds(SERIAL_DEVICE* serial);
|
2013-08-16 17:51:44 +04:00
|
|
|
static void* serial_thread_mfunc(void* arg);
|
2011-10-04 22:15:15 +04:00
|
|
|
|
|
|
|
static void serial_process_irp_create(SERIAL_DEVICE* serial, IRP* irp)
|
|
|
|
{
|
2013-01-16 04:14:03 +04:00
|
|
|
char* path = NULL;
|
2012-12-17 08:34:07 +04:00
|
|
|
int status;
|
2011-10-04 22:15:15 +04:00
|
|
|
SERIAL_TTY* tty;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 PathLength;
|
|
|
|
UINT32 FileId;
|
2011-10-04 22:15:15 +04:00
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek(irp->input, 28); /* DesiredAccess(4) AllocationSize(8), FileAttributes(4) */
|
2012-10-03 07:01:16 +04:00
|
|
|
/* SharedAccess(4) CreateDisposition(4), CreateOptions(4) */
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT32(irp->input, PathLength);
|
2011-10-04 22:15:15 +04:00
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
status = ConvertFromUnicode(CP_UTF8, 0, (WCHAR*) Stream_Pointer(irp->input),
|
2012-12-17 08:34:07 +04:00
|
|
|
PathLength / 2, &path, 0, NULL, NULL);
|
|
|
|
|
|
|
|
if (status < 1)
|
|
|
|
path = (char*) calloc(1, 1);
|
2011-10-04 22:15:15 +04:00
|
|
|
|
|
|
|
FileId = irp->devman->id_sequence++;
|
|
|
|
|
|
|
|
tty = serial_tty_new(serial->path, FileId);
|
2012-09-24 02:41:07 +04:00
|
|
|
|
2011-10-04 22:15:15 +04:00
|
|
|
if (tty == NULL)
|
|
|
|
{
|
|
|
|
irp->IoStatus = STATUS_UNSUCCESSFUL;
|
|
|
|
FileId = 0;
|
|
|
|
|
|
|
|
DEBUG_WARN("failed to create %s", path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
serial->tty = tty;
|
2013-08-16 12:34:54 +04:00
|
|
|
|
|
|
|
serial_abort_single_io(serial, serial->timeout_id, SERIAL_ABORT_IO_NONE,
|
|
|
|
STATUS_CANCELLED);
|
|
|
|
serial_abort_single_io(serial, serial->timeout_id, SERIAL_ABORT_IO_READ,
|
|
|
|
STATUS_CANCELLED);
|
|
|
|
serial_abort_single_io(serial, serial->timeout_id, SERIAL_ABORT_IO_WRITE,
|
|
|
|
STATUS_CANCELLED);
|
|
|
|
|
2013-08-16 17:51:44 +04:00
|
|
|
serial->mthread = CreateThread(NULL, 0,
|
|
|
|
(LPTHREAD_START_ROUTINE) serial_thread_mfunc, (void*) serial,
|
|
|
|
0, NULL);
|
|
|
|
|
2011-10-04 22:15:15 +04:00
|
|
|
DEBUG_SVC("%s(%d) created.", serial->path, FileId);
|
|
|
|
}
|
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT32(irp->output, FileId);
|
|
|
|
Stream_Write_UINT8(irp->output, 0);
|
2011-10-04 22:15:15 +04:00
|
|
|
|
2012-10-09 07:21:26 +04:00
|
|
|
free(path);
|
2011-10-04 22:15:15 +04:00
|
|
|
|
|
|
|
irp->Complete(irp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void serial_process_irp_close(SERIAL_DEVICE* serial, IRP* irp)
|
|
|
|
{
|
|
|
|
SERIAL_TTY* tty;
|
|
|
|
|
|
|
|
tty = serial->tty;
|
2012-09-24 02:41:07 +04:00
|
|
|
|
2011-10-04 22:15:15 +04:00
|
|
|
if (tty == NULL)
|
|
|
|
{
|
|
|
|
irp->IoStatus = STATUS_UNSUCCESSFUL;
|
|
|
|
DEBUG_WARN("tty not valid.");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DEBUG_SVC("%s(%d) closed.", serial->path, tty->id);
|
|
|
|
|
2013-08-16 17:51:44 +04:00
|
|
|
TerminateThread(serial->mthread, 0);
|
|
|
|
WaitForSingleObject(serial->mthread, INFINITE);
|
|
|
|
CloseHandle(serial->mthread);
|
|
|
|
serial->mthread = NULL;
|
|
|
|
|
2011-10-04 22:15:15 +04:00
|
|
|
serial_tty_free(tty);
|
|
|
|
serial->tty = NULL;
|
|
|
|
}
|
|
|
|
|
2013-05-09 00:27:21 +04:00
|
|
|
Stream_Zero(irp->output, 5); /* Padding(5) */
|
2011-10-04 22:15:15 +04:00
|
|
|
|
|
|
|
irp->Complete(irp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void serial_process_irp_read(SERIAL_DEVICE* serial, IRP* irp)
|
|
|
|
{
|
|
|
|
SERIAL_TTY* tty;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 Length;
|
|
|
|
UINT64 Offset;
|
2012-10-09 11:01:37 +04:00
|
|
|
BYTE* buffer = NULL;
|
2011-10-04 22:15:15 +04:00
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT32(irp->input, Length);
|
|
|
|
Stream_Read_UINT64(irp->input, Offset);
|
2011-10-04 22:15:15 +04:00
|
|
|
|
|
|
|
DEBUG_SVC("length %u offset %llu", Length, Offset);
|
|
|
|
|
|
|
|
tty = serial->tty;
|
2012-09-24 02:41:07 +04:00
|
|
|
|
2011-10-04 22:15:15 +04:00
|
|
|
if (tty == NULL)
|
|
|
|
{
|
|
|
|
irp->IoStatus = STATUS_UNSUCCESSFUL;
|
|
|
|
Length = 0;
|
|
|
|
|
|
|
|
DEBUG_WARN("tty not valid.");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
buffer = (BYTE*) malloc(Length);
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2011-10-04 22:15:15 +04:00
|
|
|
if (!serial_tty_read(tty, buffer, &Length))
|
|
|
|
{
|
|
|
|
irp->IoStatus = STATUS_UNSUCCESSFUL;
|
2012-10-09 07:21:26 +04:00
|
|
|
free(buffer);
|
2011-10-04 22:15:15 +04:00
|
|
|
buffer = NULL;
|
|
|
|
Length = 0;
|
|
|
|
|
|
|
|
DEBUG_WARN("read %s(%d) failed.", serial->path, tty->id);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DEBUG_SVC("read %llu-%llu from %d", Offset, Offset + Length, tty->id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT32(irp->output, Length);
|
2012-09-24 02:41:07 +04:00
|
|
|
|
2011-10-04 22:15:15 +04:00
|
|
|
if (Length > 0)
|
|
|
|
{
|
2013-05-02 02:15:55 +04:00
|
|
|
Stream_EnsureRemainingCapacity(irp->output, Length);
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write(irp->output, buffer, Length);
|
2011-10-04 22:15:15 +04:00
|
|
|
}
|
2012-09-24 02:41:07 +04:00
|
|
|
|
2012-10-09 07:21:26 +04:00
|
|
|
free(buffer);
|
2011-10-04 22:15:15 +04:00
|
|
|
|
|
|
|
irp->Complete(irp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void serial_process_irp_write(SERIAL_DEVICE* serial, IRP* irp)
|
|
|
|
{
|
|
|
|
SERIAL_TTY* tty;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 Length;
|
|
|
|
UINT64 Offset;
|
2011-10-04 22:15:15 +04:00
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT32(irp->input, Length);
|
|
|
|
Stream_Read_UINT64(irp->input, Offset);
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek(irp->input, 20); /* Padding */
|
2011-10-04 22:15:15 +04:00
|
|
|
|
|
|
|
DEBUG_SVC("length %u offset %llu", Length, Offset);
|
|
|
|
|
|
|
|
tty = serial->tty;
|
2012-09-24 02:41:07 +04:00
|
|
|
|
2011-10-04 22:15:15 +04:00
|
|
|
if (tty == NULL)
|
|
|
|
{
|
|
|
|
irp->IoStatus = STATUS_UNSUCCESSFUL;
|
|
|
|
Length = 0;
|
|
|
|
|
|
|
|
DEBUG_WARN("tty not valid.");
|
|
|
|
}
|
2013-04-30 06:35:15 +04:00
|
|
|
else if (!serial_tty_write(tty, Stream_Pointer(irp->input), Length))
|
2011-10-04 22:15:15 +04:00
|
|
|
{
|
|
|
|
irp->IoStatus = STATUS_UNSUCCESSFUL;
|
|
|
|
Length = 0;
|
|
|
|
|
|
|
|
DEBUG_WARN("write %s(%d) failed.", serial->path, tty->id);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DEBUG_SVC("write %llu-%llu to %s(%d).", Offset, Offset + Length, serial->path, tty->id);
|
|
|
|
}
|
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT32(irp->output, Length);
|
|
|
|
Stream_Write_UINT8(irp->output, 0); /* Padding */
|
2011-10-04 22:15:15 +04:00
|
|
|
|
|
|
|
irp->Complete(irp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void serial_process_irp_device_control(SERIAL_DEVICE* serial, IRP* irp)
|
|
|
|
{
|
|
|
|
SERIAL_TTY* tty;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 IoControlCode;
|
|
|
|
UINT32 InputBufferLength;
|
|
|
|
UINT32 OutputBufferLength;
|
|
|
|
UINT32 abort_io = SERIAL_ABORT_IO_NONE;
|
2011-10-04 22:15:15 +04:00
|
|
|
|
|
|
|
DEBUG_SVC("[in] pending size %d", list_size(serial->pending_irps));
|
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT32(irp->input, InputBufferLength);
|
|
|
|
Stream_Read_UINT32(irp->input, OutputBufferLength);
|
|
|
|
Stream_Read_UINT32(irp->input, IoControlCode);
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek(irp->input, 20); /* Padding */
|
2011-10-04 22:15:15 +04:00
|
|
|
|
|
|
|
tty = serial->tty;
|
2012-09-24 02:41:07 +04:00
|
|
|
|
2013-03-21 05:22:47 +04:00
|
|
|
if (!tty)
|
2011-10-04 22:15:15 +04:00
|
|
|
{
|
|
|
|
irp->IoStatus = STATUS_UNSUCCESSFUL;
|
|
|
|
OutputBufferLength = 0;
|
|
|
|
|
|
|
|
DEBUG_WARN("tty not valid.");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
irp->IoStatus = serial_tty_control(tty, IoControlCode, irp->input, irp->output, &abort_io);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (abort_io & SERIAL_ABORT_IO_WRITE)
|
|
|
|
serial_abort_single_io(serial, tty->id, SERIAL_ABORT_IO_WRITE, STATUS_CANCELLED);
|
|
|
|
if (abort_io & SERIAL_ABORT_IO_READ)
|
|
|
|
serial_abort_single_io(serial, tty->id, SERIAL_ABORT_IO_READ, STATUS_CANCELLED);
|
|
|
|
|
|
|
|
if (irp->IoStatus == STATUS_PENDING)
|
|
|
|
list_enqueue(serial->pending_irps, irp);
|
|
|
|
else
|
|
|
|
irp->Complete(irp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void serial_process_irp(SERIAL_DEVICE* serial, IRP* irp)
|
|
|
|
{
|
|
|
|
DEBUG_SVC("MajorFunction %u", irp->MajorFunction);
|
|
|
|
|
|
|
|
switch (irp->MajorFunction)
|
|
|
|
{
|
|
|
|
case IRP_MJ_CREATE:
|
|
|
|
serial_process_irp_create(serial, irp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IRP_MJ_CLOSE:
|
|
|
|
serial_process_irp_close(serial, irp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IRP_MJ_READ:
|
|
|
|
serial_handle_async_irp(serial, irp);
|
|
|
|
//serial_process_irp_read(serial, irp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IRP_MJ_WRITE:
|
|
|
|
serial_handle_async_irp(serial, irp);
|
|
|
|
//serial_process_irp_write(serial, irp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IRP_MJ_DEVICE_CONTROL:
|
|
|
|
serial_process_irp_device_control(serial, irp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
DEBUG_WARN("MajorFunction 0x%X not supported", irp->MajorFunction);
|
|
|
|
irp->IoStatus = STATUS_NOT_SUPPORTED;
|
|
|
|
irp->Complete(irp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
serial_check_for_events(serial);
|
|
|
|
}
|
|
|
|
|
2013-08-16 17:51:44 +04:00
|
|
|
/* This thread is used as a workaround for the missing serial event
|
|
|
|
* support in WaitForMultipleObjects.
|
|
|
|
* It monitors the terminal for events and posts it in a supported
|
|
|
|
* form that WaitForMultipleObjects can use it. */
|
|
|
|
void* serial_thread_mfunc(void* arg)
|
|
|
|
{
|
|
|
|
SERIAL_DEVICE* serial = (SERIAL_DEVICE*)arg;
|
|
|
|
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
int sl;
|
|
|
|
fd_set rd;
|
|
|
|
|
|
|
|
if(!serial->tty || serial->tty->fd <= 0)
|
|
|
|
{
|
|
|
|
DEBUG_WARN("Monitor thread still running, but no terminal opened!");
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FD_ZERO(&rd);
|
|
|
|
FD_SET(serial->tty->fd, &rd);
|
|
|
|
sl = select(serial->tty->fd + 1, &rd, NULL, NULL, NULL);
|
|
|
|
if( sl > 0 )
|
|
|
|
SetEvent(serial->newEvent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-10-04 22:15:15 +04:00
|
|
|
static void* serial_thread_func(void* arg)
|
|
|
|
{
|
2013-03-21 05:22:47 +04:00
|
|
|
IRP* irp;
|
2013-02-08 19:59:37 +04:00
|
|
|
DWORD status;
|
2011-10-04 22:15:15 +04:00
|
|
|
SERIAL_DEVICE* serial = (SERIAL_DEVICE*)arg;
|
2013-08-16 17:51:44 +04:00
|
|
|
HANDLE ev[] = {serial->stopEvent, Queue_Event(serial->queue), serial->newEvent};
|
2011-10-04 22:15:15 +04:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2013-08-16 17:51:44 +04:00
|
|
|
status = WaitForMultipleObjects(3, ev, FALSE, INFINITE);
|
|
|
|
|
2013-08-13 16:04:17 +04:00
|
|
|
if (WAIT_OBJECT_0 == status)
|
2013-02-22 06:09:42 +04:00
|
|
|
break;
|
2013-08-16 17:51:44 +04:00
|
|
|
else if (status == WAIT_OBJECT_0 + 1)
|
2013-02-08 19:59:37 +04:00
|
|
|
{
|
2013-08-16 14:40:06 +04:00
|
|
|
FD_ZERO(&serial->read_fds);
|
|
|
|
FD_ZERO(&serial->write_fds);
|
|
|
|
|
|
|
|
serial->tv.tv_sec = 0;
|
|
|
|
serial->tv.tv_usec = 0;
|
|
|
|
serial->select_timeout = 0;
|
|
|
|
|
2013-07-03 16:09:04 +04:00
|
|
|
if ((irp = (IRP*) Queue_Dequeue(serial->queue)))
|
|
|
|
serial_process_irp(serial, irp);
|
2011-10-04 22:15:15 +04:00
|
|
|
}
|
2013-08-16 17:51:44 +04:00
|
|
|
else if (status == WAIT_OBJECT_0 + 2)
|
|
|
|
ResetEvent(serial->newEvent);
|
2013-07-03 16:09:04 +04:00
|
|
|
|
2013-08-16 14:40:06 +04:00
|
|
|
if(serial->tty)
|
|
|
|
serial_check_fds(serial);
|
2011-10-04 22:15:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void serial_irp_request(DEVICE* device, IRP* irp)
|
|
|
|
{
|
2013-02-08 19:59:37 +04:00
|
|
|
SERIAL_DEVICE* serial = (SERIAL_DEVICE*) device;
|
2011-10-04 22:15:15 +04:00
|
|
|
|
2013-03-21 05:22:47 +04:00
|
|
|
Queue_Enqueue(serial->queue, irp);
|
2011-10-04 22:15:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void serial_free(DEVICE* device)
|
|
|
|
{
|
2012-10-03 07:01:16 +04:00
|
|
|
SERIAL_DEVICE* serial = (SERIAL_DEVICE*) device;
|
2011-10-04 22:15:15 +04:00
|
|
|
|
|
|
|
DEBUG_SVC("freeing device");
|
|
|
|
|
2013-08-13 18:04:19 +04:00
|
|
|
/* Stop thread */
|
2013-03-21 05:22:47 +04:00
|
|
|
SetEvent(serial->stopEvent);
|
2013-08-16 17:51:44 +04:00
|
|
|
if(serial->mthread)
|
|
|
|
{
|
|
|
|
TerminateThread(serial->mthread, 0);
|
|
|
|
WaitForSingleObject(serial->mthread, INFINITE);
|
|
|
|
CloseHandle(serial->mthread);
|
|
|
|
}
|
2013-08-13 18:04:19 +04:00
|
|
|
WaitForSingleObject(serial->thread, INFINITE);
|
|
|
|
|
2013-08-14 17:14:40 +04:00
|
|
|
serial_tty_free(serial->tty);
|
|
|
|
|
2013-08-13 18:04:19 +04:00
|
|
|
/* Clean up resources */
|
|
|
|
Stream_Free(serial->device.data, TRUE);
|
|
|
|
Queue_Free(serial->queue);
|
|
|
|
list_free(serial->pending_irps);
|
|
|
|
CloseHandle(serial->stopEvent);
|
2013-08-16 17:51:44 +04:00
|
|
|
CloseHandle(serial->newEvent);
|
2013-08-13 18:04:19 +04:00
|
|
|
CloseHandle(serial->thread);
|
2012-10-09 07:21:26 +04:00
|
|
|
free(serial);
|
2011-10-04 22:15:15 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 11:26:39 +04:00
|
|
|
static void serial_abort_single_io(SERIAL_DEVICE* serial, UINT32 file_id, UINT32 abort_io, UINT32 io_status)
|
2011-10-04 22:15:15 +04:00
|
|
|
{
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 major;
|
2013-03-21 05:22:47 +04:00
|
|
|
IRP* irp = NULL;
|
2011-10-04 22:15:15 +04:00
|
|
|
SERIAL_TTY* tty;
|
|
|
|
|
|
|
|
DEBUG_SVC("[in] pending size %d", list_size(serial->pending_irps));
|
|
|
|
|
|
|
|
tty = serial->tty;
|
2013-08-14 17:14:40 +04:00
|
|
|
if(!tty)
|
|
|
|
{
|
|
|
|
DEBUG_WARN("tty = %p", tty);
|
|
|
|
return;
|
|
|
|
}
|
2011-10-04 22:15:15 +04:00
|
|
|
|
|
|
|
switch (abort_io)
|
|
|
|
{
|
|
|
|
case SERIAL_ABORT_IO_NONE:
|
|
|
|
major = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SERIAL_ABORT_IO_READ:
|
|
|
|
major = IRP_MJ_READ;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SERIAL_ABORT_IO_WRITE:
|
|
|
|
major = IRP_MJ_WRITE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
DEBUG_SVC("unexpected abort_io code %d", abort_io);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-03 07:01:16 +04:00
|
|
|
irp = (IRP*) list_peek(serial->pending_irps);
|
|
|
|
|
2011-10-04 22:15:15 +04:00
|
|
|
while (irp)
|
|
|
|
{
|
|
|
|
if (irp->FileId != file_id || irp->MajorFunction != major)
|
|
|
|
{
|
2012-10-03 07:01:16 +04:00
|
|
|
irp = (IRP*) list_next(serial->pending_irps, irp);
|
2011-10-04 22:15:15 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Process a SINGLE FileId and MajorFunction */
|
|
|
|
list_remove(serial->pending_irps, irp);
|
|
|
|
irp->IoStatus = io_status;
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT32(irp->output, 0);
|
2011-10-04 22:15:15 +04:00
|
|
|
irp->Complete(irp);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG_SVC("[out] pending size %d", list_size(serial->pending_irps));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void serial_check_for_events(SERIAL_DEVICE* serial)
|
|
|
|
{
|
|
|
|
IRP* irp = NULL;
|
|
|
|
IRP* prev;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 result = 0;
|
2011-10-04 22:15:15 +04:00
|
|
|
SERIAL_TTY* tty;
|
|
|
|
|
|
|
|
tty = serial->tty;
|
2013-08-14 17:14:40 +04:00
|
|
|
if(!tty)
|
|
|
|
{
|
|
|
|
DEBUG_WARN("tty = %p", tty);
|
|
|
|
return;
|
|
|
|
}
|
2011-10-04 22:15:15 +04:00
|
|
|
|
|
|
|
DEBUG_SVC("[in] pending size %d", list_size(serial->pending_irps));
|
|
|
|
|
2012-10-03 07:01:16 +04:00
|
|
|
irp = (IRP*) list_peek(serial->pending_irps);
|
|
|
|
|
2011-10-04 22:15:15 +04:00
|
|
|
while (irp)
|
|
|
|
{
|
|
|
|
prev = NULL;
|
|
|
|
|
|
|
|
if (irp->MajorFunction == IRP_MJ_DEVICE_CONTROL)
|
|
|
|
{
|
|
|
|
if (serial_tty_get_event(tty, &result))
|
|
|
|
{
|
|
|
|
DEBUG_SVC("got event result %u", result);
|
|
|
|
|
|
|
|
irp->IoStatus = STATUS_SUCCESS;
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT32(irp->output, result);
|
2011-10-04 22:15:15 +04:00
|
|
|
irp->Complete(irp);
|
|
|
|
|
|
|
|
prev = irp;
|
2012-10-03 07:01:16 +04:00
|
|
|
irp = (IRP*) list_next(serial->pending_irps, irp);
|
2011-10-04 22:15:15 +04:00
|
|
|
list_remove(serial->pending_irps, prev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!prev)
|
2012-10-03 07:01:16 +04:00
|
|
|
irp = (IRP*) list_next(serial->pending_irps, irp);
|
2011-10-04 22:15:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG_SVC("[out] pending size %d", list_size(serial->pending_irps));
|
|
|
|
}
|
|
|
|
|
2012-10-09 11:26:39 +04:00
|
|
|
void serial_get_timeouts(SERIAL_DEVICE* serial, IRP* irp, UINT32* timeout, UINT32* interval_timeout)
|
2011-10-04 22:15:15 +04:00
|
|
|
{
|
|
|
|
SERIAL_TTY* tty;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 Length;
|
|
|
|
UINT32 pos;
|
2011-10-04 22:15:15 +04:00
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
pos = Stream_GetPosition(irp->input);
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT32(irp->input, Length);
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_SetPosition(irp->input, pos);
|
2011-10-04 22:15:15 +04:00
|
|
|
|
|
|
|
DEBUG_SVC("length read %u", Length);
|
|
|
|
|
|
|
|
tty = serial->tty;
|
2013-08-14 17:14:40 +04:00
|
|
|
if(!tty)
|
|
|
|
{
|
|
|
|
DEBUG_WARN("tty = %p", tty);
|
|
|
|
return;
|
|
|
|
}
|
2012-10-03 07:01:16 +04:00
|
|
|
|
|
|
|
*timeout = (tty->read_total_timeout_multiplier * Length) + tty->read_total_timeout_constant;
|
2011-10-04 22:15:15 +04:00
|
|
|
*interval_timeout = tty->read_interval_timeout;
|
|
|
|
|
|
|
|
DEBUG_SVC("timeouts %u %u", *timeout, *interval_timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void serial_handle_async_irp(SERIAL_DEVICE* serial, IRP* irp)
|
|
|
|
{
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 timeout = 0;
|
|
|
|
UINT32 itv_timeout = 0;
|
2011-10-04 22:15:15 +04:00
|
|
|
SERIAL_TTY* tty;
|
|
|
|
|
|
|
|
tty = serial->tty;
|
2013-08-14 17:14:40 +04:00
|
|
|
if(!tty)
|
|
|
|
{
|
|
|
|
DEBUG_WARN("tty = %p", tty);
|
|
|
|
return;
|
|
|
|
}
|
2011-10-04 22:15:15 +04:00
|
|
|
|
|
|
|
switch (irp->MajorFunction)
|
|
|
|
{
|
2012-10-03 07:01:16 +04:00
|
|
|
case IRP_MJ_WRITE:
|
|
|
|
DEBUG_SVC("handling IRP_MJ_WRITE");
|
|
|
|
break;
|
2011-10-04 22:15:15 +04:00
|
|
|
|
2012-10-03 07:01:16 +04:00
|
|
|
case IRP_MJ_READ:
|
|
|
|
DEBUG_SVC("handling IRP_MJ_READ");
|
2011-10-04 22:15:15 +04:00
|
|
|
|
2012-10-03 07:01:16 +04:00
|
|
|
serial_get_timeouts(serial, irp, &timeout, &itv_timeout);
|
2011-10-04 22:15:15 +04:00
|
|
|
|
2012-10-03 07:01:16 +04:00
|
|
|
/* Check if io request timeout is smaller than current (but not 0). */
|
2013-02-08 19:59:37 +04:00
|
|
|
if (timeout && ((serial->select_timeout == 0) || (timeout < serial->select_timeout)))
|
2012-10-03 07:01:16 +04:00
|
|
|
{
|
|
|
|
serial->select_timeout = timeout;
|
|
|
|
serial->tv.tv_sec = serial->select_timeout / 1000;
|
|
|
|
serial->tv.tv_usec = (serial->select_timeout % 1000) * 1000;
|
|
|
|
serial->timeout_id = tty->id;
|
|
|
|
}
|
2013-02-08 19:59:37 +04:00
|
|
|
if (itv_timeout && ((serial->select_timeout == 0) || (itv_timeout < serial->select_timeout)))
|
2012-10-03 07:01:16 +04:00
|
|
|
{
|
|
|
|
serial->select_timeout = itv_timeout;
|
|
|
|
serial->tv.tv_sec = serial->select_timeout / 1000;
|
|
|
|
serial->tv.tv_usec = (serial->select_timeout % 1000) * 1000;
|
|
|
|
serial->timeout_id = tty->id;
|
|
|
|
}
|
|
|
|
DEBUG_SVC("select_timeout %u, tv_sec %lu tv_usec %lu, timeout_id %u",
|
|
|
|
serial->select_timeout, serial->tv.tv_sec, serial->tv.tv_usec, serial->timeout_id);
|
|
|
|
break;
|
2011-10-04 22:15:15 +04:00
|
|
|
|
2012-10-03 07:01:16 +04:00
|
|
|
default:
|
|
|
|
DEBUG_SVC("no need to handle %d", irp->MajorFunction);
|
|
|
|
return;
|
2011-10-04 22:15:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
irp->IoStatus = STATUS_PENDING;
|
|
|
|
list_enqueue(serial->pending_irps, irp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __serial_check_fds(SERIAL_DEVICE* serial)
|
|
|
|
{
|
|
|
|
IRP* irp;
|
|
|
|
IRP* prev;
|
|
|
|
SERIAL_TTY* tty;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 result = 0;
|
2013-01-23 17:25:33 +04:00
|
|
|
BOOL irp_completed = FALSE;
|
2011-10-04 22:15:15 +04:00
|
|
|
|
2013-02-08 19:59:37 +04:00
|
|
|
ZeroMemory(&serial->tv, sizeof(struct timeval));
|
2011-10-04 22:15:15 +04:00
|
|
|
tty = serial->tty;
|
2013-08-14 17:14:40 +04:00
|
|
|
if(!tty)
|
|
|
|
{
|
|
|
|
DEBUG_WARN("tty = %p", tty);
|
|
|
|
return;
|
|
|
|
}
|
2011-10-04 22:15:15 +04:00
|
|
|
|
|
|
|
/* scan every pending */
|
|
|
|
irp = list_peek(serial->pending_irps);
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2011-10-04 22:15:15 +04:00
|
|
|
while (irp)
|
|
|
|
{
|
|
|
|
DEBUG_SVC("MajorFunction %u", irp->MajorFunction);
|
|
|
|
|
|
|
|
switch (irp->MajorFunction)
|
|
|
|
{
|
|
|
|
case IRP_MJ_READ:
|
|
|
|
if (FD_ISSET(tty->fd, &serial->read_fds))
|
|
|
|
{
|
|
|
|
irp->IoStatus = STATUS_SUCCESS;
|
|
|
|
serial_process_irp_read(serial, irp);
|
2013-01-23 17:25:33 +04:00
|
|
|
irp_completed = TRUE;
|
2011-10-04 22:15:15 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IRP_MJ_WRITE:
|
|
|
|
if (FD_ISSET(tty->fd, &serial->write_fds))
|
|
|
|
{
|
|
|
|
irp->IoStatus = STATUS_SUCCESS;
|
|
|
|
serial_process_irp_write(serial, irp);
|
2013-01-23 17:25:33 +04:00
|
|
|
irp_completed = TRUE;
|
2011-10-04 22:15:15 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IRP_MJ_DEVICE_CONTROL:
|
|
|
|
if (serial_tty_get_event(tty, &result))
|
|
|
|
{
|
|
|
|
DEBUG_SVC("got event result %u", result);
|
|
|
|
|
|
|
|
irp->IoStatus = STATUS_SUCCESS;
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT32(irp->output, result);
|
2011-10-04 22:15:15 +04:00
|
|
|
irp->Complete(irp);
|
2013-01-23 17:25:33 +04:00
|
|
|
irp_completed = TRUE;
|
2011-10-04 22:15:15 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
DEBUG_SVC("no request found");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
prev = irp;
|
2012-10-03 07:01:16 +04:00
|
|
|
irp = (IRP*) list_next(serial->pending_irps, irp);
|
|
|
|
|
2013-03-21 05:22:47 +04:00
|
|
|
if (irp_completed || (prev->IoStatus == STATUS_SUCCESS))
|
2011-10-04 22:15:15 +04:00
|
|
|
list_remove(serial->pending_irps, prev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void serial_set_fds(SERIAL_DEVICE* serial)
|
|
|
|
{
|
|
|
|
IRP* irp;
|
2012-10-03 07:01:16 +04:00
|
|
|
fd_set* fds;
|
2011-10-04 22:15:15 +04:00
|
|
|
SERIAL_TTY* tty;
|
|
|
|
|
|
|
|
DEBUG_SVC("[in] pending size %d", list_size(serial->pending_irps));
|
|
|
|
|
|
|
|
tty = serial->tty;
|
2013-08-14 17:14:40 +04:00
|
|
|
if(!tty)
|
|
|
|
{
|
|
|
|
DEBUG_WARN("tty = %p", tty);
|
|
|
|
return;
|
|
|
|
}
|
2012-10-03 07:01:16 +04:00
|
|
|
irp = (IRP*) list_peek(serial->pending_irps);
|
|
|
|
|
2011-10-04 22:15:15 +04:00
|
|
|
while (irp)
|
|
|
|
{
|
|
|
|
fds = NULL;
|
|
|
|
|
|
|
|
switch (irp->MajorFunction)
|
|
|
|
{
|
|
|
|
case IRP_MJ_WRITE:
|
|
|
|
fds = &serial->write_fds;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IRP_MJ_READ:
|
|
|
|
fds = &serial->read_fds;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fds && (tty->fd >= 0))
|
|
|
|
{
|
|
|
|
FD_SET(tty->fd, fds);
|
|
|
|
serial->nfds = MAX(serial->nfds, tty->fd);
|
|
|
|
}
|
2012-10-03 07:01:16 +04:00
|
|
|
|
|
|
|
irp = (IRP*) list_next(serial->pending_irps, irp);
|
2011-10-04 22:15:15 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
static BOOL serial_check_fds(SERIAL_DEVICE* serial)
|
2011-10-04 22:15:15 +04:00
|
|
|
{
|
|
|
|
if (list_size(serial->pending_irps) == 0)
|
|
|
|
return 1;
|
|
|
|
|
2013-08-16 14:40:06 +04:00
|
|
|
FD_ZERO(&serial->read_fds);
|
|
|
|
FD_ZERO(&serial->write_fds);
|
|
|
|
|
|
|
|
serial->tv.tv_sec = 0;
|
|
|
|
serial->tv.tv_usec = 0;
|
|
|
|
serial->select_timeout = 0;
|
|
|
|
|
2011-10-04 22:15:15 +04:00
|
|
|
serial_set_fds(serial);
|
|
|
|
DEBUG_SVC("waiting %lu %lu", serial->tv.tv_sec, serial->tv.tv_usec);
|
|
|
|
|
|
|
|
switch (select(serial->nfds + 1, &serial->read_fds, &serial->write_fds, NULL, &serial->tv))
|
|
|
|
{
|
|
|
|
case -1:
|
|
|
|
DEBUG_SVC("select has returned -1 with error: %s", strerror(errno));
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
if (serial->select_timeout)
|
|
|
|
{
|
2013-01-29 19:07:16 +04:00
|
|
|
__serial_check_fds(serial);
|
2011-10-04 22:15:15 +04:00
|
|
|
serial_abort_single_io(serial, serial->timeout_id, SERIAL_ABORT_IO_NONE, STATUS_TIMEOUT);
|
|
|
|
serial_abort_single_io(serial, serial->timeout_id, SERIAL_ABORT_IO_READ, STATUS_TIMEOUT);
|
|
|
|
serial_abort_single_io(serial, serial->timeout_id, SERIAL_ABORT_IO_WRITE, STATUS_TIMEOUT);
|
|
|
|
}
|
|
|
|
DEBUG_SVC("select has timed out");
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
__serial_check_fds(serial);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2012-11-09 04:01:52 +04:00
|
|
|
|
|
|
|
#ifdef STATIC_CHANNELS
|
|
|
|
#define DeviceServiceEntry serial_DeviceServiceEntry
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int DeviceServiceEntry(PDEVICE_SERVICE_ENTRY_POINTS pEntryPoints)
|
|
|
|
{
|
|
|
|
int i, len;
|
|
|
|
char* name;
|
|
|
|
char* path;
|
|
|
|
RDPDR_SERIAL* device;
|
|
|
|
SERIAL_DEVICE* serial;
|
|
|
|
|
|
|
|
device = (RDPDR_SERIAL*) pEntryPoints->device;
|
|
|
|
name = device->Name;
|
|
|
|
path = device->Path;
|
|
|
|
|
2013-07-03 16:09:04 +04:00
|
|
|
if ((name && name[0]) && (path && path[0]))
|
2012-11-09 04:01:52 +04:00
|
|
|
{
|
2012-11-22 05:21:08 +04:00
|
|
|
serial = (SERIAL_DEVICE*) malloc(sizeof(SERIAL_DEVICE));
|
|
|
|
ZeroMemory(serial, sizeof(SERIAL_DEVICE));
|
2012-11-09 04:01:52 +04:00
|
|
|
|
|
|
|
serial->device.type = RDPDR_DTYP_SERIAL;
|
|
|
|
serial->device.name = name;
|
|
|
|
serial->device.IRPRequest = serial_irp_request;
|
|
|
|
serial->device.Free = serial_free;
|
|
|
|
|
|
|
|
len = strlen(name);
|
2013-05-09 01:48:30 +04:00
|
|
|
serial->device.data = Stream_New(NULL, len + 1);
|
2012-11-09 04:01:52 +04:00
|
|
|
|
|
|
|
for (i = 0; i <= len; i++)
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT8(serial->device.data, name[i] < 0 ? '_' : name[i]);
|
2012-11-09 04:01:52 +04:00
|
|
|
|
|
|
|
serial->path = path;
|
2013-03-21 05:22:47 +04:00
|
|
|
serial->queue = Queue_New(TRUE, -1, -1);
|
2012-11-09 04:01:52 +04:00
|
|
|
serial->pending_irps = list_new();
|
|
|
|
|
2013-03-21 05:22:47 +04:00
|
|
|
serial->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
2013-08-16 17:51:44 +04:00
|
|
|
serial->newEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
2013-03-21 05:22:47 +04:00
|
|
|
|
2012-11-27 05:15:48 +04:00
|
|
|
pEntryPoints->RegisterDevice(pEntryPoints->devman, (DEVICE*) serial);
|
2012-11-09 04:01:52 +04:00
|
|
|
|
2013-08-14 17:14:40 +04:00
|
|
|
serial->thread = CreateThread(NULL, 0,
|
|
|
|
(LPTHREAD_START_ROUTINE) serial_thread_func, (void*) serial, 0, NULL);
|
2013-08-16 17:51:44 +04:00
|
|
|
serial->mthread = NULL;
|
2012-11-09 04:01:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|