2011-10-11 11:16:05 +04:00
|
|
|
/**
|
2012-10-09 07:02:04 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2011-10-11 11:16:05 +04:00
|
|
|
* Redirected Parallel Port Device Service
|
|
|
|
*
|
|
|
|
* Copyright 2010 O.S. Systems Software Ltda.
|
|
|
|
* Copyright 2010 Eduardo Fiss Beloni <beloni@ossystems.com.br>
|
2015-06-08 15:54:36 +03:00
|
|
|
* Copyright 2015 Thincast Technologies GmbH
|
|
|
|
* Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
|
2011-10-11 11:16:05 +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
|
|
|
|
|
2011-10-11 11:16:05 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2011-10-21 16:11:33 +04:00
|
|
|
|
2012-07-29 06:24:14 +04:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2011-10-11 11:16:05 +04:00
|
|
|
#include <unistd.h>
|
2012-07-29 06:24:14 +04:00
|
|
|
#endif
|
|
|
|
|
2011-10-11 11:16:05 +04:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
2012-07-29 06:24:14 +04:00
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
#include <termios.h>
|
2011-10-11 11:16:05 +04:00
|
|
|
#include <strings.h>
|
|
|
|
#include <sys/ioctl.h>
|
2012-07-29 06:24:14 +04:00
|
|
|
#endif
|
|
|
|
|
2011-10-24 22:08:39 +04:00
|
|
|
#ifdef __LINUX__
|
2011-10-21 16:11:33 +04:00
|
|
|
#include <linux/ppdev.h>
|
|
|
|
#include <linux/parport.h>
|
2011-10-24 22:08:39 +04:00
|
|
|
#endif
|
2011-10-21 16:11:33 +04:00
|
|
|
|
2012-10-03 05:52:27 +04:00
|
|
|
#include <winpr/crt.h>
|
|
|
|
#include <winpr/synch.h>
|
|
|
|
#include <winpr/thread.h>
|
2014-06-11 23:09:28 +04:00
|
|
|
#include <winpr/stream.h>
|
2013-03-21 04:57:51 +04:00
|
|
|
#include <winpr/collections.h>
|
2012-10-03 05:52:27 +04:00
|
|
|
#include <winpr/interlocked.h>
|
|
|
|
|
2011-10-11 11:16:05 +04:00
|
|
|
#include <freerdp/types.h>
|
|
|
|
#include <freerdp/constants.h>
|
2012-10-09 05:00:07 +04:00
|
|
|
#include <freerdp/channels/rdpdr.h>
|
2015-06-08 15:54:36 +03:00
|
|
|
#include <freerdp/channels/log.h>
|
|
|
|
|
|
|
|
#define TAG CHANNELS_TAG("drive.client")
|
2011-10-11 11:16:05 +04:00
|
|
|
|
|
|
|
struct _PARALLEL_DEVICE
|
|
|
|
{
|
|
|
|
DEVICE device;
|
|
|
|
|
2011-10-11 19:43:54 +04:00
|
|
|
int file;
|
2011-10-11 11:16:05 +04:00
|
|
|
char* path;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 id;
|
2011-10-11 11:16:05 +04:00
|
|
|
|
2013-03-21 04:57:51 +04:00
|
|
|
HANDLE thread;
|
|
|
|
wMessageQueue* queue;
|
2015-07-15 10:50:35 +03:00
|
|
|
rdpContext* rdpcontext;
|
2011-10-11 11:16:05 +04:00
|
|
|
};
|
|
|
|
typedef struct _PARALLEL_DEVICE PARALLEL_DEVICE;
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT parallel_process_irp_create(PARALLEL_DEVICE* parallel, IRP* irp)
|
2011-10-11 19:43:54 +04:00
|
|
|
{
|
2013-01-16 04:14:03 +04:00
|
|
|
char* path = NULL;
|
2012-12-17 08:34:07 +04:00
|
|
|
int status;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 PathLength;
|
2011-10-11 19:43:54 +04:00
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek(irp->input, 28);
|
2011-10-21 16:11:33 +04:00
|
|
|
/* DesiredAccess(4) AllocationSize(8), FileAttributes(4) */
|
|
|
|
/* SharedAccess(4) CreateDisposition(4), CreateOptions(4) */
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT32(irp->input, PathLength);
|
2011-10-11 19:43:54 +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)
|
2015-06-08 15:54:36 +03:00
|
|
|
if (!(path = (char*) calloc(1, 1)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "calloc failed!");
|
|
|
|
return CHANNEL_RC_NO_MEMORY;
|
|
|
|
}
|
2011-10-11 19:43:54 +04:00
|
|
|
|
2011-10-21 16:11:33 +04:00
|
|
|
parallel->id = irp->devman->id_sequence++;
|
2011-10-11 19:43:54 +04:00
|
|
|
parallel->file = open(parallel->path, O_RDWR);
|
2012-07-29 06:24:14 +04:00
|
|
|
|
2011-10-11 19:43:54 +04:00
|
|
|
if (parallel->file < 0)
|
|
|
|
{
|
|
|
|
irp->IoStatus = STATUS_ACCESS_DENIED;
|
2011-10-21 16:11:33 +04:00
|
|
|
parallel->id = 0;
|
2011-10-11 19:43:54 +04:00
|
|
|
}
|
2011-10-21 16:11:33 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* all read and write operations should be non-blocking */
|
|
|
|
if (fcntl(parallel->file, F_SETFL, O_NONBLOCK) == -1)
|
2014-06-11 23:09:28 +04:00
|
|
|
{
|
2011-10-21 16:11:33 +04:00
|
|
|
|
2014-06-11 23:09:28 +04:00
|
|
|
}
|
2011-10-21 16:11:33 +04:00
|
|
|
}
|
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT32(irp->output, parallel->id);
|
|
|
|
Stream_Write_UINT8(irp->output, 0);
|
2011-10-11 19:43:54 +04:00
|
|
|
|
2012-10-09 07:21:26 +04:00
|
|
|
free(path);
|
2011-10-11 19:43:54 +04:00
|
|
|
|
2015-06-08 15:54:36 +03:00
|
|
|
return irp->Complete(irp);
|
2011-10-11 11:16:05 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT parallel_process_irp_close(PARALLEL_DEVICE* parallel, IRP* irp)
|
2011-10-11 11:16:05 +04:00
|
|
|
{
|
2011-10-21 16:11:33 +04:00
|
|
|
if (close(parallel->file) < 0)
|
2014-06-11 23:09:28 +04:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2011-10-21 16:11:33 +04:00
|
|
|
else
|
2014-06-11 23:09:28 +04:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2011-10-21 16:11:33 +04:00
|
|
|
|
2013-05-09 00:27:21 +04:00
|
|
|
Stream_Zero(irp->output, 5); /* Padding(5) */
|
2011-10-21 16:11:33 +04:00
|
|
|
|
2015-06-08 15:54:36 +03:00
|
|
|
return irp->Complete(irp);
|
2011-10-11 11:16:05 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT parallel_process_irp_read(PARALLEL_DEVICE* parallel, IRP* irp)
|
2011-10-11 11:16:05 +04:00
|
|
|
{
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 Length;
|
|
|
|
UINT64 Offset;
|
2011-10-11 19:43:54 +04:00
|
|
|
ssize_t status;
|
2012-10-09 11:01:37 +04:00
|
|
|
BYTE* buffer = NULL;
|
2011-10-11 19:43:54 +04:00
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT32(irp->input, Length);
|
|
|
|
Stream_Read_UINT64(irp->input, Offset);
|
2011-10-11 19:43:54 +04:00
|
|
|
|
2012-10-09 11:01:37 +04:00
|
|
|
buffer = (BYTE*) malloc(Length);
|
2015-06-08 15:54:36 +03:00
|
|
|
if (!buffer)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "malloc failed!");
|
|
|
|
return CHANNEL_RC_NO_MEMORY;
|
|
|
|
}
|
2011-10-11 19:43:54 +04:00
|
|
|
|
2013-08-29 13:01:58 +04:00
|
|
|
status = read(parallel->file, buffer, Length);
|
2011-10-11 19:43:54 +04:00
|
|
|
|
|
|
|
if (status < 0)
|
|
|
|
{
|
2011-10-21 16:11:33 +04:00
|
|
|
irp->IoStatus = STATUS_UNSUCCESSFUL;
|
2012-10-09 07:21:26 +04:00
|
|
|
free(buffer);
|
2011-10-21 16:11:33 +04:00
|
|
|
buffer = NULL;
|
|
|
|
Length = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-06-11 23:09:28 +04:00
|
|
|
|
2011-10-11 19:43:54 +04:00
|
|
|
}
|
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT32(irp->output, Length);
|
2013-03-21 04:57:51 +04:00
|
|
|
|
2011-10-21 16:11:33 +04:00
|
|
|
if (Length > 0)
|
|
|
|
{
|
2015-06-08 15:54:36 +03:00
|
|
|
if (!Stream_EnsureRemainingCapacity(irp->output, Length))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
|
|
|
|
free(buffer);
|
|
|
|
return CHANNEL_RC_NO_MEMORY;
|
|
|
|
}
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write(irp->output, buffer, Length);
|
2011-10-21 16:11:33 +04:00
|
|
|
}
|
2013-03-21 04:57:51 +04:00
|
|
|
|
2012-10-09 07:21:26 +04:00
|
|
|
free(buffer);
|
2011-10-21 16:11:33 +04:00
|
|
|
|
2015-06-08 15:54:36 +03:00
|
|
|
return irp->Complete(irp);
|
2011-10-11 11:16:05 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT parallel_process_irp_write(PARALLEL_DEVICE* parallel, IRP* irp)
|
2011-10-11 11:16:05 +04:00
|
|
|
{
|
2013-03-21 04:57:51 +04:00
|
|
|
UINT32 len;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 Length;
|
|
|
|
UINT64 Offset;
|
2011-10-11 19:43:54 +04:00
|
|
|
ssize_t status;
|
|
|
|
|
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-11 19:43:54 +04:00
|
|
|
|
2011-10-21 16:11:33 +04:00
|
|
|
len = Length;
|
2012-07-29 06:24:14 +04:00
|
|
|
|
2011-10-21 16:11:33 +04:00
|
|
|
while (len > 0)
|
2011-10-11 19:43:54 +04:00
|
|
|
{
|
2013-04-30 06:35:15 +04:00
|
|
|
status = write(parallel->file, Stream_Pointer(irp->input), len);
|
2011-10-11 19:43:54 +04:00
|
|
|
|
|
|
|
if (status < 0)
|
|
|
|
{
|
2011-10-21 16:11:33 +04:00
|
|
|
irp->IoStatus = STATUS_UNSUCCESSFUL;
|
|
|
|
Length = 0;
|
|
|
|
break;
|
2011-10-11 19:43:54 +04:00
|
|
|
}
|
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek(irp->input, status);
|
2011-10-21 16:11:33 +04:00
|
|
|
len -= status;
|
2011-10-11 19:43:54 +04:00
|
|
|
}
|
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT32(irp->output, Length);
|
|
|
|
Stream_Write_UINT8(irp->output, 0); /* Padding */
|
2011-10-11 19:43:54 +04:00
|
|
|
|
2015-06-08 15:54:36 +03:00
|
|
|
return irp->Complete(irp);
|
2011-10-11 11:16:05 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT parallel_process_irp_device_control(PARALLEL_DEVICE* parallel, IRP* irp)
|
2011-10-11 11:16:05 +04:00
|
|
|
{
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT32(irp->output, 0); /* OutputBufferLength */
|
2015-06-08 15:54:36 +03:00
|
|
|
return irp->Complete(irp);
|
2011-10-11 11:16:05 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT parallel_process_irp(PARALLEL_DEVICE* parallel, IRP* irp)
|
2011-10-11 11:16:05 +04:00
|
|
|
{
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error;
|
2015-06-08 15:54:36 +03:00
|
|
|
|
2011-10-11 11:16:05 +04:00
|
|
|
switch (irp->MajorFunction)
|
|
|
|
{
|
|
|
|
case IRP_MJ_CREATE:
|
2015-06-08 15:54:36 +03:00
|
|
|
if ((error = parallel_process_irp_create(parallel, irp)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "parallel_process_irp_create failed with error %d!", error);
|
|
|
|
return error;
|
|
|
|
}
|
2011-10-11 11:16:05 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case IRP_MJ_CLOSE:
|
2015-06-08 15:54:36 +03:00
|
|
|
if ((error = parallel_process_irp_close(parallel, irp)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "parallel_process_irp_close failed with error %d!", error);
|
|
|
|
return error;
|
|
|
|
}
|
2011-10-11 11:16:05 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case IRP_MJ_READ:
|
2015-06-08 15:54:36 +03:00
|
|
|
if ((error = parallel_process_irp_read(parallel, irp)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "parallel_process_irp_read failed with error %d!", error);
|
|
|
|
return error;
|
|
|
|
}
|
2011-10-11 11:16:05 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case IRP_MJ_WRITE:
|
2015-06-08 15:54:36 +03:00
|
|
|
if ((error = parallel_process_irp_write(parallel, irp)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "parallel_process_irp_write failed with error %d!", error);
|
|
|
|
return error;
|
|
|
|
}
|
2011-10-11 11:16:05 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case IRP_MJ_DEVICE_CONTROL:
|
2015-06-08 15:54:36 +03:00
|
|
|
if ((error = parallel_process_irp_device_control(parallel, irp)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "parallel_process_irp_device_control failed with error %d!", error);
|
|
|
|
return error;
|
|
|
|
}
|
2011-10-11 11:16:05 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
irp->IoStatus = STATUS_NOT_SUPPORTED;
|
2015-06-08 15:54:36 +03:00
|
|
|
return irp->Complete(irp);
|
2011-10-11 11:16:05 +04:00
|
|
|
break;
|
|
|
|
}
|
2015-06-08 15:54:36 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2011-10-11 11:16:05 +04:00
|
|
|
}
|
|
|
|
|
2013-03-21 04:57:51 +04:00
|
|
|
static void* parallel_thread_func(void* arg)
|
2011-10-11 11:16:05 +04:00
|
|
|
{
|
|
|
|
IRP* irp;
|
2013-03-21 04:57:51 +04:00
|
|
|
wMessage message;
|
|
|
|
PARALLEL_DEVICE* parallel = (PARALLEL_DEVICE*) arg;
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error = CHANNEL_RC_OK;
|
2011-10-11 11:16:05 +04:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2013-03-21 04:57:51 +04:00
|
|
|
if (!MessageQueue_Wait(parallel->queue))
|
2015-06-08 15:54:36 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "MessageQueue_Wait failed!");
|
|
|
|
error = ERROR_INTERNAL_ERROR;
|
2011-10-11 11:16:05 +04:00
|
|
|
break;
|
2015-06-08 15:54:36 +03:00
|
|
|
}
|
2011-10-11 11:16:05 +04:00
|
|
|
|
2013-03-21 04:57:51 +04:00
|
|
|
if (!MessageQueue_Peek(parallel->queue, &message, TRUE))
|
2015-06-08 15:54:36 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "MessageQueue_Peek failed!");
|
|
|
|
error = ERROR_INTERNAL_ERROR;
|
2011-10-11 11:16:05 +04:00
|
|
|
break;
|
2015-06-08 15:54:36 +03:00
|
|
|
}
|
2011-10-11 11:16:05 +04:00
|
|
|
|
2013-03-21 04:57:51 +04:00
|
|
|
if (message.id == WMQ_QUIT)
|
2011-10-11 11:16:05 +04:00
|
|
|
break;
|
|
|
|
|
2013-03-21 04:57:51 +04:00
|
|
|
irp = (IRP*) message.wParam;
|
2012-10-03 05:52:27 +04:00
|
|
|
|
2015-06-08 15:54:36 +03:00
|
|
|
if ((error = parallel_process_irp(parallel, irp)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "parallel_process_irp failed with error %d!", error);
|
|
|
|
break;
|
|
|
|
}
|
2011-10-11 11:16:05 +04:00
|
|
|
}
|
2015-07-15 10:50:35 +03:00
|
|
|
if (error && parallel->rdpcontext)
|
|
|
|
setChannelError(parallel->rdpcontext, error, "parallel_thread_func reported an error");
|
|
|
|
|
2015-06-08 15:54:36 +03:00
|
|
|
ExitThread((DWORD)error);
|
2011-10-11 11:16:05 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT parallel_irp_request(DEVICE* device, IRP* irp)
|
2011-10-11 11:16:05 +04:00
|
|
|
{
|
|
|
|
PARALLEL_DEVICE* parallel = (PARALLEL_DEVICE*) device;
|
|
|
|
|
2015-07-15 10:50:35 +03:00
|
|
|
if (!MessageQueue_Post(parallel->queue, NULL, 0, (void*) irp, NULL))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "MessageQueue_Post failed!");
|
|
|
|
return ERROR_INTERNAL_ERROR;
|
|
|
|
}
|
2015-06-08 15:54:36 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2011-10-11 11:16:05 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT parallel_free(DEVICE* device)
|
2011-10-11 11:16:05 +04:00
|
|
|
{
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error;
|
2011-10-11 11:16:05 +04:00
|
|
|
PARALLEL_DEVICE* parallel = (PARALLEL_DEVICE*) device;
|
|
|
|
|
2015-07-30 16:49:21 +03:00
|
|
|
if (MessageQueue_PostQuit(parallel->queue, 0) && (WaitForSingleObject(parallel->thread, INFINITE) == WAIT_FAILED))
|
|
|
|
{
|
|
|
|
error = GetLastError();
|
|
|
|
WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", error);
|
|
|
|
return error;
|
|
|
|
}
|
2014-03-26 19:05:12 +04:00
|
|
|
CloseHandle(parallel->thread);
|
2011-10-11 11:16:05 +04:00
|
|
|
|
2013-08-13 18:04:19 +04:00
|
|
|
Stream_Free(parallel->device.data, TRUE);
|
2013-03-21 04:57:51 +04:00
|
|
|
MessageQueue_Free(parallel->queue);
|
2011-10-11 11:16:05 +04:00
|
|
|
|
2012-10-09 07:21:26 +04:00
|
|
|
free(parallel);
|
2015-07-30 16:49:21 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2011-10-11 11:16:05 +04:00
|
|
|
}
|
|
|
|
|
2012-10-14 10:38:58 +04:00
|
|
|
#ifdef STATIC_CHANNELS
|
|
|
|
#define DeviceServiceEntry parallel_DeviceServiceEntry
|
|
|
|
#endif
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
UINT DeviceServiceEntry(PDEVICE_SERVICE_ENTRY_POINTS pEntryPoints)
|
2011-10-11 11:16:05 +04:00
|
|
|
{
|
|
|
|
char* name;
|
|
|
|
char* path;
|
2015-06-08 15:54:36 +03:00
|
|
|
int i;
|
|
|
|
size_t length;
|
2012-11-09 04:01:52 +04:00
|
|
|
RDPDR_PARALLEL* device;
|
2011-10-11 11:16:05 +04:00
|
|
|
PARALLEL_DEVICE* parallel;
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error;
|
2011-10-11 11:16:05 +04:00
|
|
|
|
2012-11-09 04:01:52 +04:00
|
|
|
device = (RDPDR_PARALLEL*) pEntryPoints->device;
|
|
|
|
name = device->Name;
|
|
|
|
path = device->Path;
|
2011-10-11 11:16:05 +04:00
|
|
|
|
2014-03-26 19:05:12 +04:00
|
|
|
if (!name || (name[0] == '*'))
|
|
|
|
{
|
|
|
|
/* TODO: implement auto detection of parallel ports */
|
2015-06-08 15:54:36 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2014-03-26 19:05:12 +04:00
|
|
|
}
|
|
|
|
|
2011-10-11 11:16:05 +04:00
|
|
|
if (name[0] && path[0])
|
|
|
|
{
|
2014-03-26 19:05:12 +04:00
|
|
|
parallel = (PARALLEL_DEVICE*) calloc(1, sizeof(PARALLEL_DEVICE));
|
|
|
|
if (!parallel)
|
2015-06-08 15:54:36 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "calloc failed!");
|
|
|
|
return CHANNEL_RC_NO_MEMORY;
|
|
|
|
}
|
2011-10-11 11:16:05 +04:00
|
|
|
|
|
|
|
parallel->device.type = RDPDR_DTYP_PARALLEL;
|
|
|
|
parallel->device.name = name;
|
|
|
|
parallel->device.IRPRequest = parallel_irp_request;
|
|
|
|
parallel->device.Free = parallel_free;
|
2015-07-15 10:50:35 +03:00
|
|
|
parallel->rdpcontext = pEntryPoints->rdpcontext;
|
2011-10-11 11:16:05 +04:00
|
|
|
|
|
|
|
length = strlen(name);
|
2013-05-09 01:48:30 +04:00
|
|
|
parallel->device.data = Stream_New(NULL, length + 1);
|
2015-05-18 12:28:00 +03:00
|
|
|
if (!parallel->device.data)
|
2015-06-08 15:54:36 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Stream_New failed!");
|
|
|
|
error = CHANNEL_RC_NO_MEMORY;
|
|
|
|
goto error_out;
|
|
|
|
}
|
2011-10-11 11:16:05 +04:00
|
|
|
|
|
|
|
for (i = 0; i <= length; i++)
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT8(parallel->device.data, name[i] < 0 ? '_' : name[i]);
|
2011-10-11 11:16:05 +04:00
|
|
|
|
|
|
|
parallel->path = path;
|
|
|
|
|
2013-11-12 18:18:35 +04:00
|
|
|
parallel->queue = MessageQueue_New(NULL);
|
2015-05-18 12:28:00 +03:00
|
|
|
if (!parallel->queue)
|
2015-06-08 15:54:36 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "MessageQueue_New failed!");
|
|
|
|
error = CHANNEL_RC_NO_MEMORY;
|
|
|
|
goto error_out;
|
|
|
|
}
|
2011-10-11 11:16:05 +04:00
|
|
|
|
2015-06-08 15:54:36 +03:00
|
|
|
if ((error = pEntryPoints->RegisterDevice(pEntryPoints->devman, (DEVICE*) parallel)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "RegisterDevice failed with error %lu!", error);
|
|
|
|
goto error_out;
|
|
|
|
}
|
2011-10-11 11:16:05 +04:00
|
|
|
|
|
|
|
|
2015-06-08 15:54:36 +03:00
|
|
|
if (!(parallel->thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) parallel_thread_func, (void*) parallel, 0, NULL)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "CreateThread failed!");
|
|
|
|
error = ERROR_INTERNAL_ERROR;
|
|
|
|
goto error_out;
|
|
|
|
}
|
|
|
|
}
|
2015-05-18 12:28:00 +03:00
|
|
|
|
2015-06-08 15:54:36 +03:00
|
|
|
return CHANNEL_RC_OK;
|
|
|
|
error_out:
|
2015-05-18 12:28:00 +03:00
|
|
|
MessageQueue_Free(parallel->queue);
|
|
|
|
Stream_Free(parallel->device.data, TRUE);
|
|
|
|
free(parallel);
|
2015-06-08 15:54:36 +03:00
|
|
|
return error;
|
2011-10-11 11:16:05 +04:00
|
|
|
}
|