<commit_before>//..............................................................................
//
//  This file is part of the AXL library.
//
//  AXL is distributed under the MIT license.
//  For details see accompanying license.txt file,
//  the public copy of which is also available at:
//  http://tibbo.com/downloads/archive/axl/license.txt
//
//..............................................................................

#include "pch.h"
#include "axl_io_Serial.h"
#include "axl_sys_Event.h"

namespace axl {
namespace io {

//..............................................................................

#if (_AXL_OS_WIN)

bool
Serial::open (
	const sl::StringRef& name,
	uint_t flags
	)
{
	uint_t accessMode =
		(flags & FileFlag_ReadOnly) ? GENERIC_READ :
		(flags & FileFlag_WriteOnly) ? GENERIC_WRITE : GENERIC_READ | GENERIC_WRITE;

	uint_t flagsAttributes = (flags & FileFlag_Asynchronous) ? FILE_FLAG_OVERLAPPED : 0;

	return m_serial.open (name, accessMode, flagsAttributes);
}

bool
Serial::setSettings (
	const SerialSettings* settings,
	uint_t mask
	)
{
	DCB dcb;
	dcb.DCBlength = sizeof (dcb);

	bool result = m_serial.getSettings (&dcb);
	if (!result)
		return false;

	dcb.fBinary = TRUE;
	dcb.fDsrSensitivity = FALSE;
	dcb.fDtrControl = DTR_CONTROL_DISABLE;

	if (mask & SerialSettingId_BaudRate)
		dcb.BaudRate = settings->m_baudRate;

	if (mask & SerialSettingId_DataBits)
		dcb.ByteSize = settings->m_dataBits;

	if (mask & SerialSettingId_StopBits)
		dcb.StopBits = settings->m_stopBits;

	if (mask & SerialSettingId_Parity)
	{
		dcb.fParity = settings->m_parity != SerialParity_None;
		dcb.Parity = settings->m_parity;
	}

	if (mask & SerialSettingId_FlowControl)
		switch (settings->m_flowControl)
		{
		case SerialFlowControl_None:
			dcb.fOutxCtsFlow = FALSE;
			dcb.fRtsControl = RTS_CONTROL_DISABLE;
			break;

		case SerialFlowControl_RtsCts:
			dcb.fOutxCtsFlow = TRUE;
			dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
			break;

		case SerialFlowControl_XonXoff:
			dcb.fOutX = TRUE;
			dcb.fInX = TRUE;
			break;
		}

	if (!(mask & SerialSettingId_ReadInterval))
		return m_serial.setSettings (&dcb);

	COMMTIMEOUTS timeouts = { 0 };
	timeouts.ReadIntervalTimeout =
		settings->m_readInterval == 0 ? -1 :
		settings->m_readInterval == -1 ? 0 :
		settings->m_readInterval;

	return
		m_serial.setSettings (&dcb) &&
		m_serial.setTimeouts (&timeouts);
}

bool
Serial::getSettings (SerialSettings* settings)
{
	DCB dcb;
	dcb.DCBlength = sizeof (dcb);

	bool result = m_serial.getSettings (&dcb);
	if (!result)
		return false;

	settings->setDcb (&dcb);
	return true;<commit_msg>[axl_io] clear flow control fields in the DCB prior to setting the new value<commit_after>//..............................................................................
//
//  This file is part of the AXL library.
//
//  AXL is distributed under the MIT license.
//  For details see accompanying license.txt file,
//  the public copy of which is also available at:
//  http://tibbo.com/downloads/archive/axl/license.txt
//
//..............................................................................

#include "pch.h"
#include "axl_io_Serial.h"
#include "axl_sys_Event.h"

namespace axl {
namespace io {

//..............................................................................

#if (_AXL_OS_WIN)

bool
Serial::open (
	const sl::StringRef& name,
	uint_t flags
	)
{
	uint_t accessMode =
		(flags & FileFlag_ReadOnly) ? GENERIC_READ :
		(flags & FileFlag_WriteOnly) ? GENERIC_WRITE : GENERIC_READ | GENERIC_WRITE;

	uint_t flagsAttributes = (flags & FileFlag_Asynchronous) ? FILE_FLAG_OVERLAPPED : 0;

	return m_serial.open (name, accessMode, flagsAttributes);
}

bool
Serial::setSettings (
	const SerialSettings* settings,
	uint_t mask
	)
{
	DCB dcb;
	dcb.DCBlength = sizeof (dcb);

	bool result = m_serial.getSettings (&dcb);
	if (!result)
		return false;

	dcb.fBinary = TRUE;
	dcb.fDsrSensitivity = FALSE;
	dcb.fDtrControl = DTR_CONTROL_DISABLE;

	if (mask & SerialSettingId_BaudRate)
		dcb.BaudRate = settings->m_baudRate;

	if (mask & SerialSettingId_DataBits)
		dcb.ByteSize = settings->m_dataBits;

	if (mask & SerialSettingId_StopBits)
		dcb.StopBits = settings->m_stopBits;

	if (mask & SerialSettingId_Parity)
	{
		dcb.fParity = settings->m_parity != SerialParity_None;
		dcb.Parity = settings->m_parity;
	}

	if (mask & SerialSettingId_FlowControl)
	{
		dcb.fOutxCtsFlow = FALSE;
		dcb.fOutX = FALSE;
		dcb.fInX = FALSE;

		if (dcb.fRtsControl == RTS_CONTROL_HANDSHAKE)
			dcb.fRtsControl = RTS_CONTROL_DISABLE;

		switch (settings->m_flowControl)
		{
		case SerialFlowControl_RtsCts:
			dcb.fOutxCtsFlow = TRUE;
			dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
			break;

		case SerialFlowControl_XonXoff:
			dcb.fOutX = TRUE;
			dcb.fInX = TRUE;
			break;
		}
	}

	if (!(mask & SerialSettingId_ReadInterval))
		return m_serial.setSettings (&dcb);

	COMMTIMEOUTS timeouts = { 0 };
	timeouts.ReadIntervalTimeout =
		settings->m_readInterval == 0 ? -1 :
		settings->m_readInterval == -1 ? 0 :
		settings->m_readInterval;

	return
		m_serial.setSettings (&dcb) &&
		m_serial.setTimeouts (&timeouts);
}

bool
Serial::getSettings (SerialSettings* settings)
{
	DCB dcb;
	dcb.DCBlength = sizeof (dcb);

	bool result = m_serial.getSettings (&dcb);
	if (!result)
		return false;

	settings->setDcb (&dcb);
	return true;
{'fraction_non_alphanumeric': 0.1922141119221411, 'fraction_numerical': 0.0022459292532285235, 'mean_word_length': 2.7554462403373154, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6691793', 'n_tokens_mistral': 1976, 'n_tokens_neox': 1842, 'n_words': 396}