cec: added guards in CSerialPort
[deb_libcec.git] / src / lib / platform / windows / serialport.cpp
index 680b1f0426174729827e1b85c1ca4e75e136922f..f2d28c5b7340256f652a59931451cf2f2c9c8c0c 100644 (file)
@@ -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);