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>
|
2014-04-28 21:57:17 +04:00
|
|
|
* Copyright 2014 Hewlett-Packard Development Company, L.P.
|
2011-10-04 22:15:15 +04:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
|
2013-09-05 16:27:34 +04:00
|
|
|
#include <assert.h>
|
2014-07-03 20:13:25 +04:00
|
|
|
#include <errno.h>
|
2011-10-04 22:15:15 +04:00
|
|
|
#include <stdio.h>
|
2014-07-03 20:13:25 +04:00
|
|
|
#include <stdint.h>
|
2011-10-04 22:15:15 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2014-04-27 21:41:25 +04:00
|
|
|
#include <winpr/collections.h>
|
|
|
|
#include <winpr/comm.h>
|
2012-12-17 08:34:07 +04:00
|
|
|
#include <winpr/crt.h>
|
2014-04-27 21:41:25 +04:00
|
|
|
#include <winpr/stream.h>
|
2013-03-21 05:22:47 +04:00
|
|
|
#include <winpr/synch.h>
|
|
|
|
#include <winpr/thread.h>
|
2014-07-02 17:44:29 +04:00
|
|
|
#include <winpr/wlog.h>
|
2012-12-17 08:34:07 +04:00
|
|
|
|
2012-10-09 05:00:07 +04:00
|
|
|
#include <freerdp/freerdp.h>
|
|
|
|
#include <freerdp/channels/rdpdr.h>
|
2015-06-12 12:24:45 +03:00
|
|
|
#include <freerdp/channels/log.h>
|
|
|
|
|
|
|
|
#define TAG CHANNELS_TAG("serial.client")
|
2011-10-04 22:15:15 +04:00
|
|
|
|
2014-07-04 18:57:25 +04:00
|
|
|
/* TODO: all #ifdef __linux__ could be removed once only some generic
|
|
|
|
* functions will be used. Replace CommReadFile by ReadFile,
|
|
|
|
* CommWriteFile by WriteFile etc.. */
|
2014-07-04 19:40:06 +04:00
|
|
|
#if defined __linux__ && !defined ANDROID
|
2014-07-04 18:57:25 +04:00
|
|
|
|
2014-05-21 12:36:55 +04:00
|
|
|
#define MAX_IRP_THREADS 5
|
|
|
|
|
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;
|
2014-09-16 14:08:33 +04:00
|
|
|
BOOL permissive;
|
2014-06-18 20:20:21 +04:00
|
|
|
SERIAL_DRIVER_ID ServerSerialDriverId;
|
2014-04-27 21:41:25 +04:00
|
|
|
HANDLE* hComm;
|
2011-10-04 22:15:15 +04:00
|
|
|
|
2014-07-02 17:44:29 +04:00
|
|
|
wLog* log;
|
2014-05-13 16:55:30 +04:00
|
|
|
HANDLE MainThread;
|
|
|
|
wMessageQueue* MainIrpQueue;
|
|
|
|
|
2014-05-21 12:36:55 +04:00
|
|
|
/* one thread per pending IRP and indexed according their CompletionId */
|
|
|
|
wListDictionary *IrpThreads;
|
2014-05-27 13:33:10 +04:00
|
|
|
UINT32 IrpThreadToBeTerminatedCount;
|
2014-05-23 14:27:09 +04:00
|
|
|
CRITICAL_SECTION TerminatingIrpThreadsLock;
|
2014-05-21 12:36:55 +04:00
|
|
|
};
|
2011-10-04 22:15:15 +04:00
|
|
|
|
2014-05-21 12:36:55 +04:00
|
|
|
typedef struct _IRP_THREAD_DATA IRP_THREAD_DATA;
|
2011-10-04 22:15:15 +04:00
|
|
|
|
2014-05-21 12:36:55 +04:00
|
|
|
struct _IRP_THREAD_DATA
|
|
|
|
{
|
|
|
|
SERIAL_DEVICE *serial;
|
|
|
|
IRP *irp;
|
2011-10-04 22:15:15 +04:00
|
|
|
};
|
|
|
|
|
2014-07-02 17:44:29 +04:00
|
|
|
static UINT32 _GetLastErrorToIoStatus(SERIAL_DEVICE* serial)
|
2011-10-04 22:15:15 +04:00
|
|
|
{
|
2014-06-17 19:18:42 +04:00
|
|
|
/* http://msdn.microsoft.com/en-us/library/ff547466%28v=vs.85%29.aspx#generic_status_values_for_serial_device_control_requests */
|
2014-03-26 23:21:44 +04:00
|
|
|
|
2014-06-17 19:18:42 +04:00
|
|
|
switch(GetLastError())
|
|
|
|
{
|
|
|
|
case ERROR_BAD_DEVICE:
|
|
|
|
return STATUS_INVALID_DEVICE_REQUEST;
|
2011-10-04 22:15:15 +04:00
|
|
|
|
2014-06-17 19:18:42 +04:00
|
|
|
case ERROR_CALL_NOT_IMPLEMENTED:
|
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
2011-10-04 22:15:15 +04:00
|
|
|
|
2014-06-17 19:18:42 +04:00
|
|
|
case ERROR_CANCELLED:
|
|
|
|
return STATUS_CANCELLED;
|
2012-12-17 08:34:07 +04:00
|
|
|
|
2014-06-17 19:18:42 +04:00
|
|
|
case ERROR_INSUFFICIENT_BUFFER:
|
|
|
|
return STATUS_BUFFER_TOO_SMALL; /* NB: STATUS_BUFFER_SIZE_TOO_SMALL not defined */
|
2011-10-04 22:15:15 +04:00
|
|
|
|
2014-06-17 19:18:42 +04:00
|
|
|
case ERROR_INVALID_DEVICE_OBJECT_PARAMETER: /* eg: SerCx2.sys' _purge() */
|
|
|
|
return STATUS_INVALID_DEVICE_STATE;
|
2011-10-04 22:15:15 +04:00
|
|
|
|
2014-06-17 19:18:42 +04:00
|
|
|
case ERROR_INVALID_HANDLE:
|
|
|
|
return STATUS_INVALID_DEVICE_REQUEST;
|
2012-09-24 02:41:07 +04:00
|
|
|
|
2014-06-17 19:18:42 +04:00
|
|
|
case ERROR_INVALID_PARAMETER:
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
case ERROR_IO_DEVICE:
|
|
|
|
return STATUS_IO_DEVICE_ERROR;
|
|
|
|
|
|
|
|
case ERROR_IO_PENDING:
|
|
|
|
return STATUS_PENDING;
|
|
|
|
|
|
|
|
case ERROR_NOT_SUPPORTED:
|
|
|
|
return STATUS_NOT_SUPPORTED;
|
|
|
|
|
|
|
|
case ERROR_TIMEOUT:
|
|
|
|
return STATUS_TIMEOUT;
|
|
|
|
|
|
|
|
/* no default */
|
2011-10-04 22:15:15 +04:00
|
|
|
}
|
2014-06-17 19:18:42 +04:00
|
|
|
|
2014-07-02 17:44:29 +04:00
|
|
|
WLog_Print(serial->log, WLOG_DEBUG, "unexpected last-error: 0x%lx", GetLastError());
|
2014-06-17 19:18:42 +04:00
|
|
|
return STATUS_UNSUCCESSFUL;
|
|
|
|
}
|
2014-05-21 12:36:55 +04:00
|
|
|
|
2011-10-04 22:15:15 +04:00
|
|
|
static void serial_process_irp_create(SERIAL_DEVICE* serial, IRP* irp)
|
|
|
|
{
|
2014-04-27 21:41:25 +04:00
|
|
|
DWORD DesiredAccess;
|
|
|
|
DWORD SharedAccess;
|
|
|
|
DWORD CreateDisposition;
|
2014-03-26 23:21:44 +04:00
|
|
|
UINT32 PathLength;
|
2014-05-12 19:33:56 +04:00
|
|
|
|
2014-04-27 21:41:25 +04:00
|
|
|
Stream_Read_UINT32(irp->input, DesiredAccess); /* DesiredAccess (4 bytes) */
|
|
|
|
Stream_Seek_UINT64(irp->input); /* AllocationSize (8 bytes) */
|
|
|
|
Stream_Seek_UINT32(irp->input); /* FileAttributes (4 bytes) */
|
|
|
|
Stream_Read_UINT32(irp->input, SharedAccess); /* SharedAccess (4 bytes) */
|
|
|
|
Stream_Read_UINT32(irp->input, CreateDisposition); /* CreateDisposition (4 bytes) */
|
|
|
|
Stream_Seek_UINT32(irp->input); /* CreateOptions (4 bytes) */
|
|
|
|
Stream_Read_UINT32(irp->input, PathLength); /* PathLength (4 bytes) */
|
|
|
|
Stream_Seek(irp->input, PathLength); /* Path (variable) */
|
|
|
|
|
|
|
|
assert(PathLength == 0); /* MS-RDPESP 2.2.2.2 */
|
|
|
|
|
2014-05-12 19:33:56 +04:00
|
|
|
|
|
|
|
#ifndef _WIN32
|
2014-06-16 21:18:45 +04:00
|
|
|
/* Windows 2012 server sends on a first call :
|
2014-05-12 19:33:56 +04:00
|
|
|
* DesiredAccess = 0x00100080: SYNCHRONIZE | FILE_READ_ATTRIBUTES
|
|
|
|
* SharedAccess = 0x00000007: FILE_SHARE_DELETE | FILE_SHARE_WRITE | FILE_SHARE_READ
|
|
|
|
* CreateDisposition = 0x00000001: CREATE_NEW
|
|
|
|
*
|
2014-06-16 21:18:45 +04:00
|
|
|
* then Windows 2012 sends :
|
2014-05-12 19:33:56 +04:00
|
|
|
* DesiredAccess = 0x00120089: SYNCHRONIZE | READ_CONTROL | FILE_READ_ATTRIBUTES | FILE_READ_EA | FILE_READ_DATA
|
|
|
|
* SharedAccess = 0x00000007: FILE_SHARE_DELETE | FILE_SHARE_WRITE | FILE_SHARE_READ
|
|
|
|
* CreateDisposition = 0x00000001: CREATE_NEW
|
|
|
|
*
|
|
|
|
* assert(DesiredAccess == (GENERIC_READ | GENERIC_WRITE));
|
|
|
|
* assert(SharedAccess == 0);
|
|
|
|
* assert(CreateDisposition == OPEN_EXISTING);
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2014-07-02 17:44:29 +04:00
|
|
|
WLog_Print(serial->log, WLOG_DEBUG, "DesiredAccess: 0x%lX, SharedAccess: 0x%lX, CreateDisposition: 0x%lX", DesiredAccess, SharedAccess, CreateDisposition);
|
2014-05-12 19:33:56 +04:00
|
|
|
|
|
|
|
/* FIXME: As of today only the flags below are supported by CommCreateFileA: */
|
|
|
|
DesiredAccess = GENERIC_READ | GENERIC_WRITE;
|
|
|
|
SharedAccess = 0;
|
|
|
|
CreateDisposition = OPEN_EXISTING;
|
|
|
|
#endif
|
2014-04-27 21:41:25 +04:00
|
|
|
|
|
|
|
serial->hComm = CreateFile(serial->device.name,
|
2014-06-16 21:18:45 +04:00
|
|
|
DesiredAccess,
|
|
|
|
SharedAccess,
|
|
|
|
NULL, /* SecurityAttributes */
|
|
|
|
CreateDisposition,
|
|
|
|
0, /* FlagsAndAttributes */
|
|
|
|
NULL); /* TemplateFile */
|
2014-04-27 21:41:25 +04:00
|
|
|
|
|
|
|
if (!serial->hComm || (serial->hComm == INVALID_HANDLE_VALUE))
|
2011-10-04 22:15:15 +04:00
|
|
|
{
|
2014-11-13 21:54:32 +03:00
|
|
|
WLog_Print(serial->log, WLOG_WARN, "CreateFile failure: %s last-error: 0x%lX\n", serial->device.name, GetLastError());
|
2014-04-27 21:41:25 +04:00
|
|
|
|
2011-10-04 22:15:15 +04:00
|
|
|
irp->IoStatus = STATUS_UNSUCCESSFUL;
|
2014-04-27 21:41:25 +04:00
|
|
|
goto error_handle;
|
2011-10-04 22:15:15 +04:00
|
|
|
}
|
|
|
|
|
2014-06-18 20:20:21 +04:00
|
|
|
_comm_setServerSerialDriver(serial->hComm, serial->ServerSerialDriverId);
|
2011-10-04 22:15:15 +04:00
|
|
|
|
2014-09-16 14:08:33 +04:00
|
|
|
_comm_set_permissive(serial->hComm, serial->permissive);
|
2011-10-04 22:15:15 +04:00
|
|
|
|
2014-05-12 19:33:56 +04:00
|
|
|
/* NOTE: binary mode/raw mode required for the redirection. On
|
|
|
|
* Linux, CommCreateFileA forces this setting.
|
|
|
|
*/
|
|
|
|
/* ZeroMemory(&dcb, sizeof(DCB)); */
|
|
|
|
/* dcb.DCBlength = sizeof(DCB); */
|
|
|
|
/* GetCommState(serial->hComm, &dcb); */
|
|
|
|
/* dcb.fBinary = TRUE; */
|
|
|
|
/* SetCommState(serial->hComm, &dcb); */
|
|
|
|
|
|
|
|
assert(irp->FileId == 0);
|
|
|
|
irp->FileId = irp->devman->id_sequence++; /* FIXME: why not ((WINPR_COMM*)hComm)->fd? */
|
|
|
|
|
2014-04-27 21:41:25 +04:00
|
|
|
irp->IoStatus = STATUS_SUCCESS;
|
|
|
|
|
2014-07-02 17:44:29 +04:00
|
|
|
WLog_Print(serial->log, WLOG_DEBUG, "%s (DeviceId: %d, FileId: %d) created.", serial->device.name, irp->device->id, irp->FileId);
|
2011-10-04 22:15:15 +04:00
|
|
|
|
2014-04-27 21:41:25 +04:00
|
|
|
error_handle:
|
2014-05-12 19:33:56 +04:00
|
|
|
Stream_Write_UINT32(irp->output, irp->FileId); /* FileId (4 bytes) */
|
2014-04-27 21:41:25 +04:00
|
|
|
Stream_Write_UINT8(irp->output, 0); /* Information (1 byte) */
|
2011-10-04 22:15:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void serial_process_irp_close(SERIAL_DEVICE* serial, IRP* irp)
|
|
|
|
{
|
2014-03-27 01:11:15 +04:00
|
|
|
Stream_Seek(irp->input, 32); /* Padding (32 bytes) */
|
2012-09-24 02:41:07 +04:00
|
|
|
|
2014-04-27 21:41:25 +04:00
|
|
|
if (!CloseHandle(serial->hComm))
|
2011-10-04 22:15:15 +04:00
|
|
|
{
|
2014-07-02 17:44:29 +04:00
|
|
|
WLog_Print(serial->log, WLOG_WARN, "CloseHandle failure: %s (%d) closed.", serial->device.name, irp->device->id);
|
2011-10-04 22:15:15 +04:00
|
|
|
irp->IoStatus = STATUS_UNSUCCESSFUL;
|
2014-04-27 21:41:25 +04:00
|
|
|
goto error_handle;
|
2011-10-04 22:15:15 +04:00
|
|
|
}
|
|
|
|
|
2014-07-02 17:44:29 +04:00
|
|
|
WLog_Print(serial->log, WLOG_DEBUG, "%s (DeviceId: %d, FileId: %d) closed.", serial->device.name, irp->device->id, irp->FileId);
|
2011-10-04 22:15:15 +04:00
|
|
|
|
2014-04-27 21:41:25 +04:00
|
|
|
serial->hComm = NULL;
|
|
|
|
irp->IoStatus = STATUS_SUCCESS;
|
2011-10-04 22:15:15 +04:00
|
|
|
|
2014-04-27 21:41:25 +04:00
|
|
|
error_handle:
|
2014-03-26 23:21:44 +04:00
|
|
|
Stream_Zero(irp->output, 5); /* Padding (5 bytes) */
|
2011-10-04 22:15:15 +04:00
|
|
|
}
|
|
|
|
|
2015-06-12 12:24:45 +03:00
|
|
|
static WIN32ERROR serial_process_irp_read(SERIAL_DEVICE* serial, IRP* irp)
|
2011-10-04 22:15:15 +04:00
|
|
|
{
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 Length;
|
|
|
|
UINT64 Offset;
|
2012-10-09 11:01:37 +04:00
|
|
|
BYTE* buffer = NULL;
|
2014-05-12 19:33:56 +04:00
|
|
|
DWORD nbRead = 0;
|
2011-10-04 22:15:15 +04:00
|
|
|
|
2014-03-27 01:11:15 +04:00
|
|
|
Stream_Read_UINT32(irp->input, Length); /* Length (4 bytes) */
|
|
|
|
Stream_Read_UINT64(irp->input, Offset); /* Offset (8 bytes) */
|
|
|
|
Stream_Seek(irp->input, 20); /* Padding (20 bytes) */
|
2012-09-24 02:41:07 +04:00
|
|
|
|
2014-05-12 19:33:56 +04:00
|
|
|
|
|
|
|
buffer = (BYTE*)calloc(Length, sizeof(BYTE));
|
|
|
|
if (buffer == NULL)
|
2011-10-04 22:15:15 +04:00
|
|
|
{
|
2014-05-12 19:33:56 +04:00
|
|
|
irp->IoStatus = STATUS_NO_MEMORY;
|
|
|
|
goto error_handle;
|
|
|
|
}
|
|
|
|
|
2014-06-16 21:18:45 +04:00
|
|
|
|
2014-05-19 18:53:57 +04:00
|
|
|
/* MS-RDPESP 3.2.5.1.4: If the Offset field is not set to 0, the value MUST be ignored
|
2014-05-12 19:33:56 +04:00
|
|
|
* assert(Offset == 0);
|
|
|
|
*/
|
2014-06-16 21:18:45 +04:00
|
|
|
|
2014-05-12 19:33:56 +04:00
|
|
|
|
2014-07-02 17:44:29 +04:00
|
|
|
WLog_Print(serial->log, WLOG_DEBUG, "reading %d bytes from %s", Length, serial->device.name);
|
2011-10-04 22:15:15 +04:00
|
|
|
|
2014-05-12 19:33:56 +04:00
|
|
|
/* FIXME: CommReadFile to be replaced by ReadFile */
|
|
|
|
if (CommReadFile(serial->hComm, buffer, Length, &nbRead, NULL))
|
|
|
|
{
|
|
|
|
irp->IoStatus = STATUS_SUCCESS;
|
2011-10-04 22:15:15 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-02 17:44:29 +04:00
|
|
|
WLog_Print(serial->log, WLOG_DEBUG, "read failure to %s, nbRead=%ld, last-error: 0x%lX", serial->device.name, nbRead, GetLastError());
|
2014-06-11 23:09:28 +04:00
|
|
|
|
2014-07-02 17:44:29 +04:00
|
|
|
irp->IoStatus = _GetLastErrorToIoStatus(serial);
|
2011-10-04 22:15:15 +04:00
|
|
|
}
|
|
|
|
|
2014-07-02 17:44:29 +04:00
|
|
|
WLog_Print(serial->log, WLOG_DEBUG, "%lu bytes read from %s", nbRead, serial->device.name);
|
2014-06-16 21:18:45 +04:00
|
|
|
|
2014-05-12 19:33:56 +04:00
|
|
|
error_handle:
|
|
|
|
|
|
|
|
Stream_Write_UINT32(irp->output, nbRead); /* Length (4 bytes) */
|
2012-09-24 02:41:07 +04:00
|
|
|
|
2014-05-12 19:33:56 +04:00
|
|
|
if (nbRead > 0)
|
2011-10-04 22:15:15 +04:00
|
|
|
{
|
2015-06-12 12:24:45 +03:00
|
|
|
if (!Stream_EnsureRemainingCapacity(irp->output, nbRead))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
|
|
|
|
free(buffer);
|
|
|
|
return CHANNEL_RC_NO_MEMORY;
|
|
|
|
}
|
2014-05-12 19:33:56 +04:00
|
|
|
Stream_Write(irp->output, buffer, nbRead); /* ReadData */
|
2011-10-04 22:15:15 +04:00
|
|
|
}
|
2012-09-24 02:41:07 +04:00
|
|
|
|
2015-05-11 10:07:39 +03:00
|
|
|
free(buffer);
|
2015-06-12 12:24:45 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2011-10-04 22:15:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void serial_process_irp_write(SERIAL_DEVICE* serial, IRP* irp)
|
|
|
|
{
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 Length;
|
|
|
|
UINT64 Offset;
|
2014-05-12 19:33:56 +04:00
|
|
|
DWORD nbWritten = 0;
|
2011-10-04 22:15:15 +04:00
|
|
|
|
2014-03-27 01:11:15 +04:00
|
|
|
Stream_Read_UINT32(irp->input, Length); /* Length (4 bytes) */
|
|
|
|
Stream_Read_UINT64(irp->input, Offset); /* Offset (8 bytes) */
|
|
|
|
Stream_Seek(irp->input, 20); /* Padding (20 bytes) */
|
2012-09-24 02:41:07 +04:00
|
|
|
|
2014-05-19 18:53:57 +04:00
|
|
|
/* MS-RDPESP 3.2.5.1.5: The Offset field is ignored
|
|
|
|
* assert(Offset == 0);
|
|
|
|
*
|
|
|
|
* Using a serial printer, noticed though this field could be
|
|
|
|
* set.
|
|
|
|
*/
|
2014-03-27 01:54:36 +04:00
|
|
|
|
2014-07-02 17:44:29 +04:00
|
|
|
WLog_Print(serial->log, WLOG_DEBUG, "writing %d bytes to %s", Length, serial->device.name);
|
2014-03-27 01:54:36 +04:00
|
|
|
|
2014-05-12 19:33:56 +04:00
|
|
|
/* FIXME: CommWriteFile to be replaced by WriteFile */
|
|
|
|
if (CommWriteFile(serial->hComm, Stream_Pointer(irp->input), Length, &nbWritten, NULL))
|
2011-10-04 22:15:15 +04:00
|
|
|
{
|
2014-05-12 19:33:56 +04:00
|
|
|
irp->IoStatus = STATUS_SUCCESS;
|
2011-10-04 22:15:15 +04:00
|
|
|
}
|
2014-05-12 19:33:56 +04:00
|
|
|
else
|
|
|
|
{
|
2014-07-02 17:44:29 +04:00
|
|
|
WLog_Print(serial->log, WLOG_DEBUG, "write failure to %s, nbWritten=%ld, last-error: 0x%lX", serial->device.name, nbWritten, GetLastError());
|
2014-03-27 01:54:36 +04:00
|
|
|
|
2014-07-02 17:44:29 +04:00
|
|
|
irp->IoStatus = _GetLastErrorToIoStatus(serial);
|
2011-10-04 22:15:15 +04:00
|
|
|
}
|
|
|
|
|
2014-07-02 17:44:29 +04:00
|
|
|
WLog_Print(serial->log, WLOG_DEBUG, "%lu bytes written to %s", nbWritten, serial->device.name);
|
2014-05-12 19:33:56 +04:00
|
|
|
|
|
|
|
Stream_Write_UINT32(irp->output, nbWritten); /* Length (4 bytes) */
|
2014-03-27 01:11:15 +04:00
|
|
|
Stream_Write_UINT8(irp->output, 0); /* Padding (1 byte) */
|
2011-10-04 22:15:15 +04:00
|
|
|
}
|
|
|
|
|
2014-05-19 18:53:57 +04:00
|
|
|
|
2015-06-12 12:24:45 +03:00
|
|
|
static WIN32ERROR serial_process_irp_device_control(SERIAL_DEVICE* serial, IRP* irp)
|
2011-10-04 22:15:15 +04:00
|
|
|
{
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 IoControlCode;
|
|
|
|
UINT32 InputBufferLength;
|
2014-04-27 21:41:25 +04:00
|
|
|
BYTE* InputBuffer = NULL;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 OutputBufferLength;
|
2014-04-27 21:41:25 +04:00
|
|
|
BYTE* OutputBuffer = NULL;
|
|
|
|
DWORD BytesReturned = 0;
|
2011-10-04 22:15:15 +04:00
|
|
|
|
2014-03-27 01:11:15 +04:00
|
|
|
Stream_Read_UINT32(irp->input, OutputBufferLength); /* OutputBufferLength (4 bytes) */
|
|
|
|
Stream_Read_UINT32(irp->input, InputBufferLength); /* InputBufferLength (4 bytes) */
|
|
|
|
Stream_Read_UINT32(irp->input, IoControlCode); /* IoControlCode (4 bytes) */
|
|
|
|
Stream_Seek(irp->input, 20); /* Padding (20 bytes) */
|
2012-09-24 02:41:07 +04:00
|
|
|
|
2014-04-27 21:41:25 +04:00
|
|
|
OutputBuffer = (BYTE*)calloc(OutputBufferLength, sizeof(BYTE));
|
|
|
|
if (OutputBuffer == NULL)
|
2011-10-04 22:15:15 +04:00
|
|
|
{
|
2014-04-27 21:41:25 +04:00
|
|
|
irp->IoStatus = STATUS_NO_MEMORY;
|
|
|
|
goto error_handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
InputBuffer = (BYTE*)calloc(InputBufferLength, sizeof(BYTE));
|
|
|
|
if (InputBuffer == NULL)
|
|
|
|
{
|
|
|
|
irp->IoStatus = STATUS_NO_MEMORY;
|
|
|
|
goto error_handle;
|
|
|
|
}
|
2014-05-12 19:33:56 +04:00
|
|
|
|
|
|
|
Stream_Read(irp->input, InputBuffer, InputBufferLength);
|
|
|
|
|
2014-07-02 17:44:29 +04:00
|
|
|
WLog_Print(serial->log, WLOG_DEBUG, "CommDeviceIoControl: CompletionId=%d, IoControlCode=[0x%X] %s", irp->CompletionId, IoControlCode, _comm_serial_ioctl_name(IoControlCode));
|
2014-06-16 21:18:45 +04:00
|
|
|
|
2014-05-12 19:33:56 +04:00
|
|
|
/* FIXME: CommDeviceIoControl to be replaced by DeviceIoControl() */
|
2014-04-27 21:41:25 +04:00
|
|
|
if (CommDeviceIoControl(serial->hComm, IoControlCode, InputBuffer, InputBufferLength, OutputBuffer, OutputBufferLength, &BytesReturned, NULL))
|
|
|
|
{
|
2014-07-02 17:44:29 +04:00
|
|
|
/* WLog_Print(serial->log, WLOG_DEBUG, "CommDeviceIoControl: CompletionId=%d, IoControlCode=[0x%X] %s done", irp->CompletionId, IoControlCode, _comm_serial_ioctl_name(IoControlCode)); */
|
2014-05-23 14:27:09 +04:00
|
|
|
|
2014-04-27 21:41:25 +04:00
|
|
|
irp->IoStatus = STATUS_SUCCESS;
|
2011-10-04 22:15:15 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-02 17:44:29 +04:00
|
|
|
WLog_Print(serial->log, WLOG_DEBUG, "CommDeviceIoControl failure: IoControlCode=[0x%X] %s, last-error: 0x%lX",
|
2014-06-16 21:18:45 +04:00
|
|
|
IoControlCode, _comm_serial_ioctl_name(IoControlCode), GetLastError());
|
2014-05-14 18:29:10 +04:00
|
|
|
|
2014-07-02 17:44:29 +04:00
|
|
|
irp->IoStatus = _GetLastErrorToIoStatus(serial);
|
2011-10-04 22:15:15 +04:00
|
|
|
}
|
|
|
|
|
2014-04-27 21:41:25 +04:00
|
|
|
error_handle:
|
2014-05-12 19:33:56 +04:00
|
|
|
|
2014-06-19 21:07:45 +04:00
|
|
|
/* FIXME: find out whether it's required or not to get
|
|
|
|
* BytesReturned == OutputBufferLength when
|
|
|
|
* CommDeviceIoControl returns FALSE */
|
2014-05-19 18:53:57 +04:00
|
|
|
assert(OutputBufferLength == BytesReturned);
|
|
|
|
|
2014-05-12 19:33:56 +04:00
|
|
|
Stream_Write_UINT32(irp->output, BytesReturned); /* OutputBufferLength (4 bytes) */
|
|
|
|
|
|
|
|
if (BytesReturned > 0)
|
|
|
|
{
|
2015-06-12 12:24:45 +03:00
|
|
|
if (!Stream_EnsureRemainingCapacity(irp->output, BytesReturned))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
|
|
|
|
free(InputBuffer);
|
|
|
|
free(OutputBuffer);
|
|
|
|
return CHANNEL_RC_NO_MEMORY;
|
|
|
|
}
|
|
|
|
|
2014-05-12 19:33:56 +04:00
|
|
|
Stream_Write(irp->output, OutputBuffer, BytesReturned); /* OutputBuffer */
|
|
|
|
}
|
2014-05-23 14:27:09 +04:00
|
|
|
/* FIXME: Why at least Windows 2008R2 gets lost with this
|
2014-05-12 21:21:06 +04:00
|
|
|
* extra byte and likely on a IOCTL_SERIAL_SET_BAUD_RATE? The
|
|
|
|
* extra byte is well required according MS-RDPEFS
|
|
|
|
* 2.2.1.5.5 */
|
|
|
|
/* else */
|
|
|
|
/* { */
|
|
|
|
/* Stream_Write_UINT8(irp->output, 0); /\* Padding (1 byte) *\/ */
|
|
|
|
/* } */
|
2014-05-12 19:33:56 +04:00
|
|
|
|
2015-05-11 10:07:39 +03:00
|
|
|
free(InputBuffer);
|
|
|
|
free(OutputBuffer);
|
2015-06-12 12:24:45 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2011-10-04 22:15:15 +04:00
|
|
|
}
|
|
|
|
|
2015-06-12 12:24:45 +03:00
|
|
|
static WIN32ERROR serial_process_irp(SERIAL_DEVICE* serial, IRP* irp)
|
2011-10-04 22:15:15 +04:00
|
|
|
{
|
2015-06-12 12:24:45 +03:00
|
|
|
WIN32ERROR error = CHANNEL_RC_OK;
|
2014-07-02 17:44:29 +04:00
|
|
|
WLog_Print(serial->log, WLOG_DEBUG, "IRP MajorFunction: 0x%04X MinorFunction: 0x%04X\n",
|
|
|
|
irp->MajorFunction, irp->MinorFunction);
|
2011-10-04 22:15:15 +04:00
|
|
|
|
|
|
|
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:
|
2015-06-12 12:24:45 +03:00
|
|
|
if ((error = serial_process_irp_read(serial, irp)))
|
|
|
|
WLog_ERR(TAG, "serial_process_irp_read failed with error %lu!", error);
|
2011-10-04 22:15:15 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case IRP_MJ_WRITE:
|
2014-03-26 23:21:44 +04:00
|
|
|
serial_process_irp_write(serial, irp);
|
2011-10-04 22:15:15 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case IRP_MJ_DEVICE_CONTROL:
|
2015-06-12 12:24:45 +03:00
|
|
|
if ((error = serial_process_irp_device_control(serial, irp)))
|
|
|
|
WLog_ERR(TAG, "serial_process_irp_device_control failed with error %lu!", error);
|
2011-10-04 22:15:15 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
irp->IoStatus = STATUS_NOT_SUPPORTED;
|
|
|
|
break;
|
|
|
|
}
|
2015-06-12 12:24:45 +03:00
|
|
|
return error;
|
2013-08-16 17:51:44 +04:00
|
|
|
}
|
|
|
|
|
2014-05-23 14:27:09 +04:00
|
|
|
|
2014-05-21 12:36:55 +04:00
|
|
|
static void* irp_thread_func(void* arg)
|
2014-05-13 16:55:30 +04:00
|
|
|
{
|
2014-05-21 12:36:55 +04:00
|
|
|
IRP_THREAD_DATA *data = (IRP_THREAD_DATA*)arg;
|
2015-06-12 12:24:45 +03:00
|
|
|
WIN32ERROR error;
|
2014-05-13 16:55:30 +04:00
|
|
|
|
2014-05-21 12:36:55 +04:00
|
|
|
/* blocks until the end of the request */
|
2015-06-12 12:24:45 +03:00
|
|
|
if ((error = serial_process_irp(data->serial, data->irp)))
|
|
|
|
WLog_ERR(TAG, "serial_process_irp failed with error %lu", error);
|
2014-05-21 12:36:55 +04:00
|
|
|
|
2014-05-23 14:27:09 +04:00
|
|
|
EnterCriticalSection(&data->serial->TerminatingIrpThreadsLock);
|
2014-05-27 13:33:10 +04:00
|
|
|
data->serial->IrpThreadToBeTerminatedCount++;
|
2014-05-23 14:27:09 +04:00
|
|
|
|
|
|
|
data->irp->Complete(data->irp);
|
|
|
|
|
|
|
|
LeaveCriticalSection(&data->serial->TerminatingIrpThreadsLock);
|
|
|
|
|
|
|
|
/* NB: At this point, the server might already being reusing
|
|
|
|
* the CompletionId whereas the thread is not yet
|
|
|
|
* terminated */
|
|
|
|
|
2014-05-21 12:36:55 +04:00
|
|
|
free(data);
|
|
|
|
|
|
|
|
ExitThread(0);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void create_irp_thread(SERIAL_DEVICE *serial, IRP *irp)
|
|
|
|
{
|
|
|
|
IRP_THREAD_DATA *data = NULL;
|
|
|
|
HANDLE irpThread = INVALID_HANDLE_VALUE;
|
|
|
|
HANDLE previousIrpThread;
|
2014-07-03 20:13:25 +04:00
|
|
|
uintptr_t key;
|
2014-05-21 12:36:55 +04:00
|
|
|
|
2014-05-27 13:33:10 +04:00
|
|
|
/* for a test/debug purpose, uncomment the code below to get a
|
|
|
|
* single thread for all IRPs. NB: two IRPs could not be
|
|
|
|
* processed at the same time, typically two concurent
|
|
|
|
* Read/Write operations could block each other. */
|
2014-05-23 14:27:09 +04:00
|
|
|
/* serial_process_irp(serial, irp); */
|
|
|
|
/* irp->Complete(irp); */
|
|
|
|
/* return; */
|
2014-05-21 12:36:55 +04:00
|
|
|
|
|
|
|
|
2014-05-27 13:33:10 +04:00
|
|
|
/* NOTE: for good or bad, this implementation relies on the
|
|
|
|
* server to avoid a flooding of requests. see also _purge().
|
|
|
|
*/
|
|
|
|
|
2014-05-23 14:27:09 +04:00
|
|
|
EnterCriticalSection(&serial->TerminatingIrpThreadsLock);
|
2014-05-27 13:33:10 +04:00
|
|
|
while (serial->IrpThreadToBeTerminatedCount > 0)
|
2014-05-13 16:55:30 +04:00
|
|
|
{
|
2014-05-23 14:27:09 +04:00
|
|
|
/* Cleaning up termitating and pending irp
|
|
|
|
* threads. See also: irp_thread_func() */
|
2014-05-13 16:55:30 +04:00
|
|
|
|
2014-05-23 14:27:09 +04:00
|
|
|
HANDLE irpThread;
|
|
|
|
ULONG_PTR *ids;
|
|
|
|
int i, nbIds;
|
2014-05-13 16:55:30 +04:00
|
|
|
|
2014-05-23 14:27:09 +04:00
|
|
|
nbIds = ListDictionary_GetKeys(serial->IrpThreads, &ids);
|
|
|
|
for (i=0; i<nbIds; i++)
|
2014-05-21 12:36:55 +04:00
|
|
|
{
|
2014-05-23 14:27:09 +04:00
|
|
|
/* Checking if ids[i] is terminating or pending */
|
2014-05-13 16:55:30 +04:00
|
|
|
|
2014-05-23 14:27:09 +04:00
|
|
|
DWORD waitResult;
|
|
|
|
ULONG_PTR id = ids[i];
|
2014-05-13 16:55:30 +04:00
|
|
|
|
2014-05-23 14:27:09 +04:00
|
|
|
irpThread = ListDictionary_GetItemValue(serial->IrpThreads, (void*)id);
|
2014-05-21 12:36:55 +04:00
|
|
|
|
2014-05-23 14:27:09 +04:00
|
|
|
/* FIXME: not quite sure a zero timeout is a good thing to check whether a thread is stil alived or not */
|
|
|
|
waitResult = WaitForSingleObject(irpThread, 0);
|
|
|
|
if (waitResult == WAIT_OBJECT_0)
|
|
|
|
{
|
|
|
|
/* terminating thread */
|
|
|
|
|
2014-07-02 17:44:29 +04:00
|
|
|
/* WLog_Print(serial->log, WLOG_DEBUG, "IRP thread with CompletionId=%d naturally died", id); */
|
2014-05-23 14:27:09 +04:00
|
|
|
|
|
|
|
CloseHandle(irpThread);
|
|
|
|
ListDictionary_Remove(serial->IrpThreads, (void*)id);
|
|
|
|
|
2014-05-27 13:33:10 +04:00
|
|
|
serial->IrpThreadToBeTerminatedCount--;
|
2014-05-23 14:27:09 +04:00
|
|
|
}
|
|
|
|
else if (waitResult != WAIT_TIMEOUT)
|
|
|
|
{
|
|
|
|
/* unexpected thread state */
|
|
|
|
|
2014-07-02 17:44:29 +04:00
|
|
|
WLog_Print(serial->log, WLOG_WARN, "WaitForSingleObject, got an unexpected result=0x%lX\n", waitResult);
|
2014-05-23 14:27:09 +04:00
|
|
|
assert(FALSE);
|
|
|
|
}
|
|
|
|
/* pending thread (but not yet terminating thread) if waitResult == WAIT_TIMEOUT */
|
2014-05-21 12:36:55 +04:00
|
|
|
}
|
2014-06-16 21:18:45 +04:00
|
|
|
|
2014-05-21 12:36:55 +04:00
|
|
|
|
2014-05-27 13:33:10 +04:00
|
|
|
if (serial->IrpThreadToBeTerminatedCount > 0)
|
2014-05-23 14:27:09 +04:00
|
|
|
{
|
2014-07-02 17:44:29 +04:00
|
|
|
WLog_Print(serial->log, WLOG_DEBUG, "%d IRP thread(s) not yet terminated", serial->IrpThreadToBeTerminatedCount);
|
2014-05-23 14:27:09 +04:00
|
|
|
Sleep(1); /* 1 ms */
|
2014-05-13 16:55:30 +04:00
|
|
|
}
|
|
|
|
}
|
2014-05-23 14:27:09 +04:00
|
|
|
LeaveCriticalSection(&serial->TerminatingIrpThreadsLock);
|
|
|
|
|
|
|
|
/* NB: At this point and thanks to the synchronization we're
|
|
|
|
* sure that the incoming IRP uses well a recycled
|
|
|
|
* CompletionId or the server sent again an IRP already posted
|
|
|
|
* which didn't get yet a response (this later server behavior
|
2014-05-27 13:33:10 +04:00
|
|
|
* at least observed with IOCTL_SERIAL_WAIT_ON_MASK and
|
2014-06-17 19:18:42 +04:00
|
|
|
* mstsc.exe).
|
2014-05-27 13:33:10 +04:00
|
|
|
*
|
|
|
|
* FIXME: behavior documented somewhere? behavior not yet
|
|
|
|
* observed with FreeRDP).
|
2014-05-23 14:27:09 +04:00
|
|
|
*/
|
|
|
|
|
2014-07-03 20:13:25 +04:00
|
|
|
key = irp->CompletionId;
|
|
|
|
previousIrpThread = ListDictionary_GetItemValue(serial->IrpThreads, (void*)key);
|
2014-06-16 21:18:45 +04:00
|
|
|
if (previousIrpThread)
|
2014-05-23 14:27:09 +04:00
|
|
|
{
|
|
|
|
/* Thread still alived <=> Request still pending */
|
|
|
|
|
2014-07-02 17:44:29 +04:00
|
|
|
WLog_Print(serial->log, WLOG_DEBUG, "IRP recall: IRP with the CompletionId=%d not yet completed!", irp->CompletionId);
|
2014-05-23 14:27:09 +04:00
|
|
|
|
2014-05-27 13:33:10 +04:00
|
|
|
assert(FALSE); /* unimplemented */
|
|
|
|
|
|
|
|
/* TODO: asserts that previousIrpThread handles well
|
|
|
|
* the same request by checking more details. Need an
|
|
|
|
* access to the IRP object used by previousIrpThread
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* TODO: taking over the pending IRP or sending a kind
|
|
|
|
* of wake up signal to accelerate the pending
|
|
|
|
* request
|
|
|
|
*
|
|
|
|
* To be considered:
|
|
|
|
* if (IoControlCode == IOCTL_SERIAL_WAIT_ON_MASK) {
|
|
|
|
* pComm->PendingEvents |= SERIAL_EV_FREERDP_*;
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
|
2014-05-23 14:27:09 +04:00
|
|
|
irp->Discard(irp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-13 16:55:30 +04:00
|
|
|
|
2014-05-21 12:36:55 +04:00
|
|
|
if (ListDictionary_Count(serial->IrpThreads) >= MAX_IRP_THREADS)
|
|
|
|
{
|
2014-07-02 17:44:29 +04:00
|
|
|
WLog_Print(serial->log, WLOG_WARN, "Number of IRP threads threshold reached: %d, keep on anyway", ListDictionary_Count(serial->IrpThreads));
|
2014-06-16 21:18:45 +04:00
|
|
|
|
2014-05-27 13:33:10 +04:00
|
|
|
assert(FALSE); /* unimplemented */
|
|
|
|
|
|
|
|
/* TODO: MAX_IRP_THREADS has been thought to avoid a
|
|
|
|
* flooding of pending requests. Use
|
|
|
|
* WaitForMultipleObjects() when available in winpr
|
|
|
|
* for threads.
|
|
|
|
*/
|
2014-05-21 12:36:55 +04:00
|
|
|
}
|
|
|
|
|
2014-05-23 14:27:09 +04:00
|
|
|
|
2014-05-21 12:36:55 +04:00
|
|
|
/* error_handle to be used ... */
|
|
|
|
|
|
|
|
data = (IRP_THREAD_DATA*)calloc(1, sizeof(IRP_THREAD_DATA));
|
|
|
|
if (data == NULL)
|
|
|
|
{
|
2014-07-02 17:44:29 +04:00
|
|
|
WLog_Print(serial->log, WLOG_WARN, "Could not allocate a new IRP_THREAD_DATA.");
|
2014-05-21 12:36:55 +04:00
|
|
|
goto error_handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->serial = serial;
|
|
|
|
data->irp = irp;
|
|
|
|
|
2014-05-27 13:33:10 +04:00
|
|
|
/* data freed by irp_thread_func */
|
|
|
|
|
2014-06-16 21:18:45 +04:00
|
|
|
irpThread = CreateThread(NULL,
|
|
|
|
0,
|
|
|
|
(LPTHREAD_START_ROUTINE)irp_thread_func,
|
|
|
|
(void*)data,
|
|
|
|
0,
|
|
|
|
NULL);
|
2014-05-21 12:36:55 +04:00
|
|
|
|
|
|
|
if (irpThread == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
2014-07-02 17:44:29 +04:00
|
|
|
WLog_Print(serial->log, WLOG_WARN, "Could not allocate a new IRP thread.");
|
2014-05-21 12:36:55 +04:00
|
|
|
goto error_handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-03 20:13:25 +04:00
|
|
|
key = irp->CompletionId;
|
|
|
|
ListDictionary_Add(serial->IrpThreads, (void*)key, irpThread);
|
2014-05-21 12:36:55 +04:00
|
|
|
|
2014-05-27 13:33:10 +04:00
|
|
|
return;
|
2014-05-21 12:36:55 +04:00
|
|
|
|
|
|
|
error_handle:
|
|
|
|
|
|
|
|
irp->IoStatus = STATUS_NO_MEMORY;
|
|
|
|
irp->Complete(irp);
|
|
|
|
|
2015-05-11 10:07:39 +03:00
|
|
|
free(data);
|
2014-05-13 16:55:30 +04:00
|
|
|
}
|
|
|
|
|
2014-05-21 12:36:55 +04:00
|
|
|
|
2014-05-27 18:17:47 +04:00
|
|
|
static void terminate_pending_irp_threads(SERIAL_DEVICE *serial)
|
|
|
|
{
|
|
|
|
ULONG_PTR *ids;
|
|
|
|
int i, nbIds;
|
|
|
|
|
|
|
|
nbIds = ListDictionary_GetKeys(serial->IrpThreads, &ids);
|
|
|
|
|
2014-07-02 17:44:29 +04:00
|
|
|
WLog_Print(serial->log, WLOG_DEBUG, "Terminating %d IRP thread(s)", nbIds);
|
2014-05-27 18:17:47 +04:00
|
|
|
|
|
|
|
for (i=0; i<nbIds; i++)
|
|
|
|
{
|
|
|
|
HANDLE irpThread;
|
|
|
|
ULONG_PTR id = ids[i];
|
|
|
|
|
2014-06-16 21:18:45 +04:00
|
|
|
irpThread = ListDictionary_GetItemValue(serial->IrpThreads, (void*)id);
|
2014-05-27 18:17:47 +04:00
|
|
|
|
|
|
|
TerminateThread(irpThread, 0);
|
|
|
|
|
|
|
|
WaitForSingleObject(irpThread, INFINITE);
|
|
|
|
|
|
|
|
CloseHandle(irpThread);
|
|
|
|
|
2014-07-02 17:44:29 +04:00
|
|
|
WLog_Print(serial->log, WLOG_DEBUG, "IRP thread terminated, CompletionId %d", id);
|
2014-05-27 18:17:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ListDictionary_Clear(serial->IrpThreads);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
2014-03-26 23:21:44 +04:00
|
|
|
wMessage message;
|
2014-05-13 16:55:30 +04:00
|
|
|
SERIAL_DEVICE* serial = (SERIAL_DEVICE*) arg;
|
2011-10-04 22:15:15 +04:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2014-05-13 16:55:30 +04:00
|
|
|
if (!MessageQueue_Wait(serial->MainIrpQueue))
|
2013-02-22 06:09:42 +04:00
|
|
|
break;
|
2013-08-16 14:40:06 +04:00
|
|
|
|
2014-05-13 16:55:30 +04:00
|
|
|
if (!MessageQueue_Peek(serial->MainIrpQueue, &message, TRUE))
|
2014-03-26 23:21:44 +04:00
|
|
|
break;
|
2013-08-16 14:40:06 +04:00
|
|
|
|
2014-03-26 23:21:44 +04:00
|
|
|
if (message.id == WMQ_QUIT)
|
2014-05-27 18:17:47 +04:00
|
|
|
{
|
|
|
|
terminate_pending_irp_threads(serial);
|
2014-03-26 23:21:44 +04:00
|
|
|
break;
|
2014-05-27 18:17:47 +04:00
|
|
|
}
|
2014-03-26 23:21:44 +04:00
|
|
|
|
|
|
|
irp = (IRP*) message.wParam;
|
2013-07-03 16:09:04 +04:00
|
|
|
|
2014-03-26 23:21:44 +04:00
|
|
|
if (irp)
|
2014-05-21 12:36:55 +04:00
|
|
|
create_irp_thread(serial, irp);
|
2011-10-04 22:15:15 +04:00
|
|
|
}
|
|
|
|
|
2013-09-05 16:27:34 +04:00
|
|
|
ExitThread(0);
|
2011-10-04 22:15:15 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-05-19 18:53:57 +04:00
|
|
|
|
2015-06-12 12:24:45 +03:00
|
|
|
static WIN32ERROR serial_irp_request(DEVICE* device, IRP* irp)
|
2011-10-04 22:15:15 +04:00
|
|
|
{
|
2013-02-08 19:59:37 +04:00
|
|
|
SERIAL_DEVICE* serial = (SERIAL_DEVICE*) device;
|
2014-05-13 16:55:30 +04:00
|
|
|
|
|
|
|
assert(irp != NULL);
|
|
|
|
|
|
|
|
if (irp == NULL)
|
2015-06-12 12:24:45 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2014-05-13 16:55:30 +04:00
|
|
|
|
|
|
|
/* NB: ENABLE_ASYNCIO is set, (MS-RDPEFS 2.2.2.7.2) this
|
|
|
|
* allows the server to send multiple simultaneous read or
|
|
|
|
* write requests.
|
|
|
|
*/
|
|
|
|
|
2014-05-21 12:36:55 +04:00
|
|
|
MessageQueue_Post(serial->MainIrpQueue, NULL, 0, (void*) irp, NULL);
|
2015-06-12 12:24:45 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2011-10-04 22:15:15 +04:00
|
|
|
}
|
|
|
|
|
2014-06-17 18:34:20 +04:00
|
|
|
|
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
|
|
|
|
2014-07-02 17:44:29 +04:00
|
|
|
WLog_Print(serial->log, WLOG_DEBUG, "freeing");
|
2011-10-04 22:15:15 +04:00
|
|
|
|
2014-05-13 16:55:30 +04:00
|
|
|
MessageQueue_PostQuit(serial->MainIrpQueue, 0);
|
2014-05-27 18:17:47 +04:00
|
|
|
WaitForSingleObject(serial->MainThread, INFINITE);
|
2014-05-13 16:55:30 +04:00
|
|
|
CloseHandle(serial->MainThread);
|
2013-08-13 18:04:19 +04:00
|
|
|
|
2014-05-12 19:33:56 +04:00
|
|
|
if (serial->hComm)
|
|
|
|
CloseHandle(serial->hComm);
|
2013-08-14 17:14:40 +04:00
|
|
|
|
2013-08-13 18:04:19 +04:00
|
|
|
/* Clean up resources */
|
|
|
|
Stream_Free(serial->device.data, TRUE);
|
2014-05-13 16:55:30 +04:00
|
|
|
MessageQueue_Free(serial->MainIrpQueue);
|
2014-05-21 12:36:55 +04:00
|
|
|
ListDictionary_Free(serial->IrpThreads);
|
2014-05-23 14:27:09 +04:00
|
|
|
DeleteCriticalSection(&serial->TerminatingIrpThreadsLock);
|
2014-03-26 19:05:12 +04:00
|
|
|
|
2012-10-09 07:21:26 +04:00
|
|
|
free(serial);
|
2011-10-04 22:15:15 +04:00
|
|
|
}
|
|
|
|
|
2014-07-04 18:57:25 +04:00
|
|
|
#endif /* __linux__ */
|
|
|
|
|
2012-11-09 04:01:52 +04:00
|
|
|
#ifdef STATIC_CHANNELS
|
|
|
|
#define DeviceServiceEntry serial_DeviceServiceEntry
|
|
|
|
#endif
|
|
|
|
|
2015-06-12 12:24:45 +03:00
|
|
|
WIN32ERROR DeviceServiceEntry(PDEVICE_SERVICE_ENTRY_POINTS pEntryPoints)
|
2012-11-09 04:01:52 +04:00
|
|
|
{
|
|
|
|
char* name;
|
|
|
|
char* path;
|
2014-06-18 20:20:21 +04:00
|
|
|
char* driver;
|
2012-11-09 04:01:52 +04:00
|
|
|
RDPDR_SERIAL* device;
|
2014-07-04 19:40:06 +04:00
|
|
|
#if defined __linux__ && !defined ANDROID
|
2014-07-04 18:57:25 +04:00
|
|
|
int i, len;
|
2012-11-09 04:01:52 +04:00
|
|
|
SERIAL_DEVICE* serial;
|
2014-07-04 18:57:25 +04:00
|
|
|
#endif /* __linux__ */
|
2015-06-12 12:24:45 +03:00
|
|
|
WIN32ERROR error = CHANNEL_RC_OK;
|
2012-11-09 04:01:52 +04:00
|
|
|
|
|
|
|
device = (RDPDR_SERIAL*) pEntryPoints->device;
|
|
|
|
name = device->Name;
|
|
|
|
path = device->Path;
|
2014-06-18 20:20:21 +04:00
|
|
|
driver = device->Driver;
|
2012-11-09 04:01:52 +04:00
|
|
|
|
2014-03-26 19:05:12 +04:00
|
|
|
if (!name || (name[0] == '*'))
|
|
|
|
{
|
2014-05-27 13:33:10 +04:00
|
|
|
/* TODO: implement auto detection of serial ports */
|
2015-06-12 12:24:45 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2014-03-26 19:05:12 +04:00
|
|
|
}
|
|
|
|
|
2013-07-03 16:09:04 +04:00
|
|
|
if ((name && name[0]) && (path && path[0]))
|
2012-11-09 04:01:52 +04:00
|
|
|
{
|
2014-07-04 18:57:25 +04:00
|
|
|
wLog* log;
|
|
|
|
|
|
|
|
WLog_Init();
|
|
|
|
log = WLog_Get("com.freerdp.channel.serial.client");
|
|
|
|
WLog_Print(log, WLOG_DEBUG, "initializing");
|
|
|
|
|
|
|
|
#ifndef __linux__ /* to be removed */
|
|
|
|
|
|
|
|
WLog_Print(log, WLOG_WARN, "Serial ports redirection not supported on this platform.");
|
2015-06-12 12:24:45 +03:00
|
|
|
return CHANNEL_RC_INITIALIZATION_ERROR;
|
2014-07-04 18:57:25 +04:00
|
|
|
|
|
|
|
#else /* __linux __ */
|
|
|
|
|
|
|
|
WLog_Print(log, WLOG_DEBUG, "Defining %s as %s", name, path);
|
|
|
|
|
2014-04-27 21:41:25 +04:00
|
|
|
if (!DefineCommDevice(name /* eg: COM1 */, path /* eg: /dev/ttyS0 */))
|
|
|
|
{
|
2015-06-12 12:24:45 +03:00
|
|
|
WLog_ERR(TAG, "DefineCommDevice failed!");
|
|
|
|
return ERROR_INTERNAL_ERROR;
|
2014-04-27 21:41:25 +04:00
|
|
|
}
|
2014-03-26 19:05:12 +04:00
|
|
|
|
2014-04-27 21:41:25 +04:00
|
|
|
serial = (SERIAL_DEVICE*) calloc(1, sizeof(SERIAL_DEVICE));
|
2014-03-26 19:05:12 +04:00
|
|
|
if (!serial)
|
2015-06-12 12:24:45 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "calloc failed!");
|
|
|
|
return CHANNEL_RC_NO_MEMORY;
|
|
|
|
}
|
2012-11-09 04:01:52 +04:00
|
|
|
|
2014-07-04 18:57:25 +04:00
|
|
|
serial->log = log;
|
2014-07-02 17:44:29 +04:00
|
|
|
|
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);
|
2015-06-12 12:24:45 +03:00
|
|
|
if (!serial->device.data)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "calloc failed!");
|
|
|
|
error = CHANNEL_RC_NO_MEMORY;
|
|
|
|
goto error_out;
|
|
|
|
}
|
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
|
|
|
|
2014-06-18 20:20:21 +04:00
|
|
|
if (driver != NULL)
|
|
|
|
{
|
|
|
|
if (_stricmp(driver, "Serial") == 0)
|
|
|
|
serial->ServerSerialDriverId = SerialDriverSerialSys;
|
|
|
|
else if (_stricmp(driver, "SerCx") == 0)
|
|
|
|
serial->ServerSerialDriverId = SerialDriverSerCxSys;
|
|
|
|
else if (_stricmp(driver, "SerCx2") == 0)
|
|
|
|
serial->ServerSerialDriverId = SerialDriverSerCx2Sys;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(FALSE);
|
|
|
|
|
2014-07-02 17:44:29 +04:00
|
|
|
WLog_Print(serial->log, WLOG_DEBUG, "Unknown server's serial driver: %s. SerCx2 will be used", driver);
|
2015-02-06 21:56:36 +03:00
|
|
|
serial->ServerSerialDriverId = SerialDriverSerialSys;
|
2014-06-18 20:20:21 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* default driver */
|
2015-02-06 21:56:36 +03:00
|
|
|
serial->ServerSerialDriverId = SerialDriverSerialSys;
|
2014-06-18 20:20:21 +04:00
|
|
|
}
|
|
|
|
|
2014-09-16 14:08:33 +04:00
|
|
|
|
|
|
|
if (device->Permissive != NULL)
|
|
|
|
{
|
|
|
|
if (_stricmp(device->Permissive, "permissive") == 0)
|
|
|
|
{
|
|
|
|
serial->permissive = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
WLog_Print(serial->log, WLOG_DEBUG, "Unknown flag: %s", device->Permissive);
|
|
|
|
assert(FALSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-02 17:44:29 +04:00
|
|
|
WLog_Print(serial->log, WLOG_DEBUG, "Server's serial driver: %s (id: %d)", driver, serial->ServerSerialDriverId);
|
2014-06-18 20:20:21 +04:00
|
|
|
/* TODO: implement auto detection of the server's serial driver */
|
|
|
|
|
2014-05-13 16:55:30 +04:00
|
|
|
serial->MainIrpQueue = MessageQueue_New(NULL);
|
2015-06-12 12:24:45 +03:00
|
|
|
if (!serial->MainIrpQueue)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "MessageQueue_New failed!");
|
|
|
|
error = CHANNEL_RC_NO_MEMORY;
|
|
|
|
goto error_out;
|
|
|
|
}
|
2013-03-21 05:22:47 +04:00
|
|
|
|
2014-05-27 13:33:10 +04:00
|
|
|
/* IrpThreads content only modified by create_irp_thread() */
|
2014-06-16 21:18:45 +04:00
|
|
|
serial->IrpThreads = ListDictionary_New(FALSE);
|
2015-06-12 12:24:45 +03:00
|
|
|
if(!serial->IrpThreads)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "ListDictionary_New failed!");
|
|
|
|
error = CHANNEL_RC_NO_MEMORY;
|
|
|
|
goto error_out;
|
|
|
|
}
|
2014-05-27 13:33:10 +04:00
|
|
|
serial->IrpThreadToBeTerminatedCount = 0;
|
2014-05-23 14:27:09 +04:00
|
|
|
InitializeCriticalSection(&serial->TerminatingIrpThreadsLock);
|
2013-03-21 05:22:47 +04:00
|
|
|
|
2015-06-12 12:24:45 +03:00
|
|
|
if ((error = pEntryPoints->RegisterDevice(pEntryPoints->devman, (DEVICE*) serial)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "EntryPoints->RegisterDevice failed with error %lu!", error);
|
|
|
|
goto error_out;
|
|
|
|
}
|
2012-11-09 04:01:52 +04:00
|
|
|
|
2015-06-12 12:24:45 +03:00
|
|
|
if (!(serial->MainThread = CreateThread(NULL,
|
2014-06-16 21:18:45 +04:00
|
|
|
0,
|
|
|
|
(LPTHREAD_START_ROUTINE) serial_thread_func,
|
|
|
|
(void*) serial,
|
|
|
|
0,
|
2015-06-12 12:24:45 +03:00
|
|
|
NULL)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "CreateThread failed!");
|
|
|
|
error = ERROR_INTERNAL_ERROR;
|
|
|
|
goto error_out;
|
|
|
|
}
|
2014-07-04 18:57:25 +04:00
|
|
|
#endif /* __linux __ */
|
2012-11-09 04:01:52 +04:00
|
|
|
}
|
|
|
|
|
2015-06-12 12:24:45 +03:00
|
|
|
return error;
|
|
|
|
error_out:
|
|
|
|
ListDictionary_Free(serial->IrpThreads);
|
|
|
|
MessageQueue_Free(serial->MainIrpQueue);
|
|
|
|
Stream_Free(serial->device.data, TRUE);
|
|
|
|
free(serial);
|
|
|
|
return error;
|
2012-11-09 04:01:52 +04:00
|
|
|
}
|