X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Flib%2Fplatform%2Fwindows%2Fserialport.cpp;h=ad7f8711d059e7679b2f1bd00c94b9f9a95ad27c;hb=2b44051cbfa70deafc30d9767323214debbc1a75;hp=680b1f0426174729827e1b85c1ca4e75e136922f;hpb=996665192725398172263999b88c63663d11db04;p=deb_libcec.git diff --git a/src/lib/platform/windows/serialport.cpp b/src/lib/platform/windows/serialport.cpp index 680b1f0..ad7f871 100644 --- a/src/lib/platform/windows/serialport.cpp +++ b/src/lib/platform/windows/serialport.cpp @@ -30,14 +30,15 @@ * http://www.pulse-eight.net/ */ -#include "../sockets/serialport.h" -#include "../util/baudrate.h" -#include "../util/timeutils.h" +#include "env.h" +#include "lib/platform/sockets/serialport.h" +#include "lib/platform/util/baudrate.h" +#include "lib/platform/util/timeutils.h" using namespace std; using namespace PLATFORM; -void FormatWindowsError(int iErrorCode, CStdString &strMessage) +void FormatWindowsError(int iErrorCode, std::string &strMessage) { if (iErrorCode != ERROR_SUCCESS) { @@ -48,28 +49,25 @@ void FormatWindowsError(int iErrorCode, CStdString &strMessage) } } -bool SetTimeouts(serial_socket_t socket, int* iError, bool bBlocking) +bool CSerialSocket::SetTimeouts(serial_socket_t socket, int* iError, DWORD iTimeoutMs) { if (socket == INVALID_HANDLE_VALUE) return false; - COMMTIMEOUTS cto; - if (!GetCommTimeouts(socket, &cto)) - { - *iError = GetLastError(); - return false; - } + if (iTimeoutMs == m_iCurrentReadTimeout) + return true; - if (bBlocking) + COMMTIMEOUTS cto; + if (iTimeoutMs == 0) { - cto.ReadIntervalTimeout = 0; + cto.ReadIntervalTimeout = MAXDWORD; cto.ReadTotalTimeoutConstant = 0; cto.ReadTotalTimeoutMultiplier = 0; } else { - cto.ReadIntervalTimeout = MAXDWORD; - cto.ReadTotalTimeoutConstant = 0; + cto.ReadIntervalTimeout = 0; + cto.ReadTotalTimeoutConstant = iTimeoutMs; cto.ReadTotalTimeoutMultiplier = 0; } @@ -78,34 +76,51 @@ bool SetTimeouts(serial_socket_t socket, int* iError, bool bBlocking) *iError = GetLastError(); return false; } + else + { + m_iCurrentReadTimeout = iTimeoutMs; + } return true; } void CSerialSocket::Close(void) { - SerialSocketClose(m_socket); + if (IsOpen()) + SerialSocketClose(m_socket); + m_socket = INVALID_SERIAL_SOCKET_VALUE; } void CSerialSocket::Shutdown(void) { - SerialSocketClose(m_socket); + if (IsOpen()) + SerialSocketClose(m_socket); + m_socket = INVALID_SERIAL_SOCKET_VALUE; } ssize_t CSerialSocket::Write(void* data, size_t len) { - return SerialSocketWrite(m_socket, &m_iError, data, len); + return IsOpen() ? SerialSocketWrite(m_socket, &m_iError, data, len) : -1; } ssize_t CSerialSocket::Read(void* data, size_t len, uint64_t iTimeoutMs /* = 0 */) { - return SerialSocketRead(m_socket, &m_iError, data, len, iTimeoutMs); + DWORD dwTimeoutMs((DWORD)iTimeoutMs); + if (iTimeoutMs != (uint64_t)iTimeoutMs) + dwTimeoutMs = MAXDWORD; + + return IsOpen() && SetTimeouts(m_socket, &m_iError, dwTimeoutMs) ? + SerialSocketRead(m_socket, &m_iError, data, len, iTimeoutMs) : + -1; } bool CSerialSocket::Open(uint64_t iTimeoutMs /* = 0 */) { iTimeoutMs = 0; - CStdString strComPath = "\\\\.\\" + m_strName; + if (IsOpen()) + return false; + + std::string strComPath = "\\\\.\\" + m_strName; CLockObject lock(m_mutex); m_socket = CreateFile(strComPath.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); if (m_socket == INVALID_HANDLE_VALUE) @@ -146,7 +161,7 @@ bool CSerialSocket::Open(uint64_t iTimeoutMs /* = 0 */) return false; } - if (!SetTimeouts(m_socket, &m_iError, false)) + if (!SetTimeouts(m_socket, &m_iError, 0)) { m_strError = "unable to set timeouts"; FormatWindowsError(GetLastError(), m_strError);