From: Lars Op den Kamp Date: Thu, 22 Mar 2012 12:03:08 +0000 (+0100) Subject: cec: added guards in CSerialPort X-Git-Tag: upstream/2.2.0~1^2~31^2~57 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=48c1be3c3a2c2a7ad4f698f358af4213d3ae5975;p=deb_libcec.git cec: added guards in CSerialPort --- diff --git a/src/lib/platform/posix/serialport.cpp b/src/lib/platform/posix/serialport.cpp index 31ae09b..cefc212 100644 --- a/src/lib/platform/posix/serialport.cpp +++ b/src/lib/platform/posix/serialport.cpp @@ -53,29 +53,32 @@ using namespace PLATFORM; void CSerialSocket::Close(void) { - SocketClose(m_socket); + if (IsOpen()) + SocketClose(m_socket); } void CSerialSocket::Shutdown(void) { - SocketClose(m_socket); + if (IsOpen()) + SocketClose(m_socket); } ssize_t CSerialSocket::Write(void* data, size_t len) { - return SocketWrite(m_socket, &m_iError, data, len); + return IsOpen() ? SocketWrite(m_socket, &m_iError, data, len) : -1; } ssize_t CSerialSocket::Read(void* data, size_t len, uint64_t iTimeoutMs /* = 0 */) { - return SocketRead(m_socket, &m_iError, data, len, iTimeoutMs); + return IsOpen() ? SocketRead(m_socket, &m_iError, data, len, iTimeoutMs) : -1; } //setting all this stuff up is a pain in the ass bool CSerialSocket::Open(uint64_t iTimeoutMs /* = 0 */) { iTimeoutMs = 0; - CLockObject lock(m_mutex); + if (IsOpen()) + return false; if (m_iDatabits != SERIAL_DATA_BITS_FIVE && m_iDatabits != SERIAL_DATA_BITS_SIX && m_iDatabits != SERIAL_DATA_BITS_SEVEN && m_iDatabits != SERIAL_DATA_BITS_EIGHT) diff --git a/src/lib/platform/windows/serialport.cpp b/src/lib/platform/windows/serialport.cpp index 680b1f0..f2d28c5 100644 --- a/src/lib/platform/windows/serialport.cpp +++ b/src/lib/platform/windows/serialport.cpp @@ -84,27 +84,32 @@ bool SetTimeouts(serial_socket_t socket, int* iError, bool bBlocking) void CSerialSocket::Close(void) { - SerialSocketClose(m_socket); + if (IsOpen()) + SerialSocketClose(m_socket); } void CSerialSocket::Shutdown(void) { - SerialSocketClose(m_socket); + if (IsOpen()) + SerialSocketClose(m_socket); } 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); + return IsOpen() ? SerialSocketRead(m_socket, &m_iError, data, len, iTimeoutMs) : -1; } bool CSerialSocket::Open(uint64_t iTimeoutMs /* = 0 */) { iTimeoutMs = 0; + if (IsOpen()) + return false; + CStdString 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);