Blame winpr/libwinpr/comm/comm_sercx_sys.c

Packit 1fb8d4
/**
Packit 1fb8d4
 * WinPR: Windows Portable Runtime
Packit 1fb8d4
 * Serial Communication API
Packit 1fb8d4
 *
Packit 1fb8d4
 * Copyright 2011 O.S. Systems Software Ltda.
Packit 1fb8d4
 * Copyright 2011 Eduardo Fiss Beloni <beloni@ossystems.com.br>
Packit 1fb8d4
 * Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
Packit 1fb8d4
 * Copyright 2014 Hewlett-Packard Development Company, L.P.
Packit 1fb8d4
 *
Packit 1fb8d4
 * Licensed under the Apache License, Version 2.0 (the "License");
Packit 1fb8d4
 * you may not use this file except in compliance with the License.
Packit 1fb8d4
 * You may obtain a copy of the License at
Packit 1fb8d4
 *
Packit 1fb8d4
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 1fb8d4
 *
Packit 1fb8d4
 * Unless required by applicable law or agreed to in writing, software
Packit 1fb8d4
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 1fb8d4
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 1fb8d4
 * See the License for the specific language governing permissions and
Packit 1fb8d4
 * limitations under the License.
Packit 1fb8d4
 */
Packit 1fb8d4
Packit 1fb8d4
#if defined __linux__ && !defined ANDROID
Packit 1fb8d4
Packit 1fb8d4
#include <assert.h>
Packit 1fb8d4
#include <termios.h>
Packit 1fb8d4
Packit 1fb8d4
#include <winpr/wlog.h>
Packit 1fb8d4
Packit 1fb8d4
#include "comm_serial_sys.h"
Packit 1fb8d4
Packit Service 5a9772
static BOOL _set_handflow(WINPR_COMM* pComm, const SERIAL_HANDFLOW* pHandflow)
Packit 1fb8d4
{
Packit 1fb8d4
	SERIAL_HANDFLOW SerCxHandflow;
Packit 1fb8d4
	BOOL result = TRUE;
Packit 1fb8d4
	SERIAL_DRIVER* pSerialSys = SerialSys_s();
Packit 1fb8d4
Packit 1fb8d4
	memcpy(&SerCxHandflow, pHandflow, sizeof(SERIAL_HANDFLOW));
Packit 1fb8d4
Packit 1fb8d4
	/* filter out unsupported bits by SerCx.sys
Packit 1fb8d4
	 *
Packit 1fb8d4
	 * http://msdn.microsoft.com/en-us/library/windows/hardware/jj680685%28v=vs.85%29.aspx
Packit 1fb8d4
	 */
Packit 1fb8d4
Packit Service 5a9772
	SerCxHandflow.ControlHandShake =
Packit Service 5a9772
	    pHandflow->ControlHandShake &
Packit Service 5a9772
	    (SERIAL_DTR_CONTROL | SERIAL_DTR_HANDSHAKE | SERIAL_CTS_HANDSHAKE | SERIAL_DSR_HANDSHAKE);
Packit Service 5a9772
	SerCxHandflow.FlowReplace =
Packit Service 5a9772
	    pHandflow->FlowReplace & (SERIAL_RTS_CONTROL | SERIAL_RTS_HANDSHAKE);
Packit 1fb8d4
Packit 1fb8d4
	if (SerCxHandflow.ControlHandShake != pHandflow->ControlHandShake)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (pHandflow->ControlHandShake & SERIAL_DCD_HANDSHAKE)
Packit 1fb8d4
		{
Packit Service 5a9772
			CommLog_Print(WLOG_WARN,
Packit Service 5a9772
			              "SERIAL_DCD_HANDSHAKE not supposed to be implemented by SerCx.sys");
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		if (pHandflow->ControlHandShake & SERIAL_DSR_SENSITIVITY)
Packit 1fb8d4
		{
Packit Service 5a9772
			CommLog_Print(WLOG_WARN,
Packit Service 5a9772
			              "SERIAL_DSR_SENSITIVITY not supposed to be implemented by SerCx.sys");
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		if (pHandflow->ControlHandShake & SERIAL_ERROR_ABORT)
Packit 1fb8d4
		{
Packit Service 5a9772
			CommLog_Print(WLOG_WARN,
Packit Service 5a9772
			              "SERIAL_ERROR_ABORT not supposed to be implemented by SerCx.sys");
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
Packit 1fb8d4
		result = FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (SerCxHandflow.FlowReplace != pHandflow->FlowReplace)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (pHandflow->ControlHandShake & SERIAL_AUTO_TRANSMIT)
Packit 1fb8d4
		{
Packit Service 5a9772
			CommLog_Print(WLOG_WARN,
Packit Service 5a9772
			              "SERIAL_AUTO_TRANSMIT not supposed to be implemented by SerCx.sys");
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		if (pHandflow->ControlHandShake & SERIAL_AUTO_RECEIVE)
Packit 1fb8d4
		{
Packit Service 5a9772
			CommLog_Print(WLOG_WARN,
Packit Service 5a9772
			              "SERIAL_AUTO_RECEIVE not supposed to be implemented by SerCx.sys");
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		if (pHandflow->ControlHandShake & SERIAL_ERROR_CHAR)
Packit 1fb8d4
		{
Packit Service 5a9772
			CommLog_Print(WLOG_WARN,
Packit Service 5a9772
			              "SERIAL_ERROR_CHAR not supposed to be implemented by SerCx.sys");
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		if (pHandflow->ControlHandShake & SERIAL_NULL_STRIPPING)
Packit 1fb8d4
		{
Packit Service 5a9772
			CommLog_Print(WLOG_WARN,
Packit Service 5a9772
			              "SERIAL_NULL_STRIPPING not supposed to be implemented by SerCx.sys");
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		if (pHandflow->ControlHandShake & SERIAL_BREAK_CHAR)
Packit 1fb8d4
		{
Packit Service 5a9772
			CommLog_Print(WLOG_WARN,
Packit Service 5a9772
			              "SERIAL_BREAK_CHAR not supposed to be implemented by SerCx.sys");
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		if (pHandflow->ControlHandShake & SERIAL_XOFF_CONTINUE)
Packit 1fb8d4
		{
Packit Service 5a9772
			CommLog_Print(WLOG_WARN,
Packit Service 5a9772
			              "SERIAL_XOFF_CONTINUE not supposed to be implemented by SerCx.sys");
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
Packit 1fb8d4
		result = FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (!pSerialSys->set_handflow(pComm, &SerCxHandflow))
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	return result;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static BOOL _get_handflow(WINPR_COMM* pComm, SERIAL_HANDFLOW* pHandflow)
Packit 1fb8d4
{
Packit 1fb8d4
	BOOL result;
Packit 1fb8d4
	SERIAL_DRIVER* pSerialSys = SerialSys_s();
Packit 1fb8d4
Packit 1fb8d4
	result = pSerialSys->get_handflow(pComm, pHandflow);
Packit 1fb8d4
Packit 1fb8d4
	/* filter out unsupported bits by SerCx.sys
Packit 1fb8d4
	 *
Packit 1fb8d4
	 * http://msdn.microsoft.com/en-us/library/windows/hardware/jj680685%28v=vs.85%29.aspx
Packit 1fb8d4
	 */
Packit 1fb8d4
Packit Service 5a9772
	pHandflow->ControlHandShake =
Packit Service 5a9772
	    pHandflow->ControlHandShake &
Packit Service 5a9772
	    (SERIAL_DTR_CONTROL | SERIAL_DTR_HANDSHAKE | SERIAL_CTS_HANDSHAKE | SERIAL_DSR_HANDSHAKE);
Packit 1fb8d4
	pHandflow->FlowReplace = pHandflow->FlowReplace & (SERIAL_RTS_CONTROL | SERIAL_RTS_HANDSHAKE);
Packit 1fb8d4
Packit 1fb8d4
	return result;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/* http://msdn.microsoft.com/en-us/library/windows/hardware/hh439605%28v=vs.85%29.aspx */
Packit Service 5a9772
static const ULONG _SERCX_SYS_SUPPORTED_EV_MASK = SERIAL_EV_RXCHAR |
Packit Service 5a9772
                                                  /* SERIAL_EV_RXFLAG   | */
Packit Service 5a9772
                                                  SERIAL_EV_TXEMPTY | SERIAL_EV_CTS |
Packit Service 5a9772
                                                  SERIAL_EV_DSR | SERIAL_EV_RLSD | SERIAL_EV_BREAK |
Packit Service 5a9772
                                                  SERIAL_EV_ERR | SERIAL_EV_RING /* |
Packit Service 5a9772
                                                               SERIAL_EV_PERR     |
Packit Service 5a9772
                                                               SERIAL_EV_RX80FULL |
Packit Service 5a9772
                                                               SERIAL_EV_EVENT1   |
Packit Service 5a9772
                                                               SERIAL_EV_EVENT2*/
Packit Service 5a9772
    ;
Packit Service 5a9772
Packit Service 5a9772
static BOOL _set_wait_mask(WINPR_COMM* pComm, const ULONG* pWaitMask)
Packit 1fb8d4
{
Packit 1fb8d4
	ULONG possibleMask;
Packit 1fb8d4
	SERIAL_DRIVER* pSerialSys = SerialSys_s();
Packit 1fb8d4
Packit 1fb8d4
	possibleMask = *pWaitMask & _SERCX_SYS_SUPPORTED_EV_MASK;
Packit 1fb8d4
Packit 1fb8d4
	if (possibleMask != *pWaitMask)
Packit 1fb8d4
	{
Packit Service 5a9772
		CommLog_Print(WLOG_WARN,
Packit Service 5a9772
		              "Not all wait events supported (SerCx.sys), requested events= 0x%08" PRIX32
Packit Service 5a9772
		              ", possible events= 0x%08" PRIX32 "",
Packit Service 5a9772
		              *pWaitMask, possibleMask);
Packit 1fb8d4
Packit 1fb8d4
		/* FIXME: shall we really set the possibleMask and return FALSE? */
Packit 1fb8d4
		pComm->WaitEventMask = possibleMask;
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* NB: All events that are supported by SerCx.sys are supported by Serial.sys*/
Packit 1fb8d4
	return pSerialSys->set_wait_mask(pComm, pWaitMask);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/* specific functions only */
Packit Service 5a9772
static SERIAL_DRIVER _SerCxSys = {
Packit Service 5a9772
	.id = SerialDriverSerCxSys,
Packit Service 5a9772
	.name = _T("SerCx.sys"),
Packit Service 5a9772
	.set_baud_rate = NULL,
Packit Service 5a9772
	.get_baud_rate = NULL,
Packit Service 5a9772
	.get_properties = NULL,
Packit 1fb8d4
	.set_serial_chars = NULL,
Packit 1fb8d4
	.get_serial_chars = NULL,
Packit 1fb8d4
	.set_line_control = NULL,
Packit 1fb8d4
	.get_line_control = NULL,
Packit Service 5a9772
	.set_handflow = _set_handflow,
Packit Service 5a9772
	.get_handflow = _get_handflow,
Packit Service 5a9772
	.set_timeouts = NULL,
Packit Service 5a9772
	.get_timeouts = NULL,
Packit Service 5a9772
	.set_dtr = NULL,
Packit Service 5a9772
	.clear_dtr = NULL,
Packit Service 5a9772
	.set_rts = NULL,
Packit Service 5a9772
	.clear_rts = NULL,
Packit Service 5a9772
	.get_modemstatus = NULL,
Packit Service 5a9772
	.set_wait_mask = _set_wait_mask,
Packit Service 5a9772
	.get_wait_mask = NULL,
Packit Service 5a9772
	.wait_on_mask = NULL,
Packit Service 5a9772
	.set_queue_size = NULL,
Packit Service 5a9772
	.purge = NULL,
Packit Service 5a9772
	.get_commstatus = NULL,
Packit Service 5a9772
	.set_break_on = NULL,
Packit Service 5a9772
	.set_break_off = NULL,
Packit Service 5a9772
	.set_xoff = NULL,
Packit Service 5a9772
	.set_xon = NULL,
Packit Service 5a9772
	.get_dtrrts = NULL,
Packit Service 5a9772
	.config_size = NULL, /* not supported by SerCx.sys */
Packit Service 5a9772
	.immediate_char = NULL,
Packit Service 5a9772
	.reset_device = NULL, /* not supported by SerCx.sys */
Packit 1fb8d4
};
Packit 1fb8d4
Packit 1fb8d4
SERIAL_DRIVER* SerCxSys_s()
Packit 1fb8d4
{
Packit 1fb8d4
	/* _SerCxSys completed with inherited functions from SerialSys */
Packit 1fb8d4
	SERIAL_DRIVER* pSerialSys = SerialSys_s();
Packit 1fb8d4
Packit Service 5a9772
	_SerCxSys.set_baud_rate = pSerialSys->set_baud_rate;
Packit Service 5a9772
	_SerCxSys.get_baud_rate = pSerialSys->get_baud_rate;
Packit 1fb8d4
Packit 1fb8d4
	_SerCxSys.get_properties = pSerialSys->get_properties;
Packit 1fb8d4
Packit 1fb8d4
	_SerCxSys.set_serial_chars = pSerialSys->set_serial_chars;
Packit 1fb8d4
	_SerCxSys.get_serial_chars = pSerialSys->get_serial_chars;
Packit 1fb8d4
	_SerCxSys.set_line_control = pSerialSys->set_line_control;
Packit 1fb8d4
	_SerCxSys.get_line_control = pSerialSys->get_line_control;
Packit 1fb8d4
Packit 1fb8d4
	_SerCxSys.set_timeouts = pSerialSys->set_timeouts;
Packit 1fb8d4
	_SerCxSys.get_timeouts = pSerialSys->get_timeouts;
Packit 1fb8d4
Packit 1fb8d4
	_SerCxSys.set_dtr = pSerialSys->set_dtr;
Packit 1fb8d4
	_SerCxSys.clear_dtr = pSerialSys->clear_dtr;
Packit 1fb8d4
Packit 1fb8d4
	_SerCxSys.set_rts = pSerialSys->set_rts;
Packit 1fb8d4
	_SerCxSys.clear_rts = pSerialSys->clear_rts;
Packit 1fb8d4
Packit 1fb8d4
	_SerCxSys.get_modemstatus = pSerialSys->get_modemstatus;
Packit 1fb8d4
Packit 1fb8d4
	_SerCxSys.set_wait_mask = pSerialSys->set_wait_mask;
Packit 1fb8d4
	_SerCxSys.get_wait_mask = pSerialSys->get_wait_mask;
Packit Service 5a9772
	_SerCxSys.wait_on_mask = pSerialSys->wait_on_mask;
Packit 1fb8d4
Packit 1fb8d4
	_SerCxSys.set_queue_size = pSerialSys->set_queue_size;
Packit 1fb8d4
Packit 1fb8d4
	_SerCxSys.purge = pSerialSys->purge;
Packit 1fb8d4
Packit 1fb8d4
	_SerCxSys.get_commstatus = pSerialSys->get_commstatus;
Packit 1fb8d4
Packit Service 5a9772
	_SerCxSys.set_break_on = pSerialSys->set_break_on;
Packit 1fb8d4
	_SerCxSys.set_break_off = pSerialSys->set_break_off;
Packit 1fb8d4
Packit 1fb8d4
	_SerCxSys.set_xoff = pSerialSys->set_xoff;
Packit Service 5a9772
	_SerCxSys.set_xon = pSerialSys->set_xon;
Packit 1fb8d4
Packit Service 5a9772
	_SerCxSys.get_dtrrts = pSerialSys->get_dtrrts;
Packit 1fb8d4
Packit 1fb8d4
	_SerCxSys.immediate_char = pSerialSys->immediate_char;
Packit 1fb8d4
Packit 1fb8d4
	return &_SerCxSys;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
#endif /* __linux__ */