cec: clean up lib/platform
[deb_libcec.git] / src / lib / platform / windows / serialport.cpp
index dba963157f52e0a5a6f4cbc7398682c82405eb3b..680b1f0426174729827e1b85c1ca4e75e136922f 100644 (file)
@@ -37,7 +37,7 @@
 using namespace std;
 using namespace PLATFORM;
 
-void CSerialPort::FormatWindowsError(int iErrorCode, CStdString &strMessage)
+void FormatWindowsError(int iErrorCode, CStdString &strMessage)
 {
   if (iErrorCode != ERROR_SUCCESS)
   {
@@ -48,22 +48,67 @@ void CSerialPort::FormatWindowsError(int iErrorCode, CStdString &strMessage)
   }
 }
 
-CSerialPort::CSerialPort(void) :
-  m_handle(INVALID_HANDLE_VALUE),
-  m_bIsOpen(false),
-  m_iBaudrate(0),
-  m_iDatabits(0),
-  m_iStopbits(0),
-  m_iParity(0)
+bool SetTimeouts(serial_socket_t socket, int* iError, bool bBlocking)
 {
+  if (socket == INVALID_HANDLE_VALUE)
+         return false;
+
+  COMMTIMEOUTS cto;
+  if (!GetCommTimeouts(socket, &cto))
+  {
+    *iError = GetLastError();
+    return false;
+  }
+
+  if (bBlocking)
+  {
+    cto.ReadIntervalTimeout         = 0;
+    cto.ReadTotalTimeoutConstant    = 0;
+    cto.ReadTotalTimeoutMultiplier  = 0;
+  }
+  else
+  {
+    cto.ReadIntervalTimeout         = MAXDWORD;
+    cto.ReadTotalTimeoutConstant    = 0;
+    cto.ReadTotalTimeoutMultiplier  = 0;
+  }
+
+  if (!SetCommTimeouts(socket, &cto))
+  {
+    *iError = GetLastError();
+    return false;
+  }
+
+  return true;
 }
 
-bool CSerialPort::Open(string name, uint32_t baudrate, uint8_t databits, uint8_t stopbits, uint8_t parity)
+void CSerialSocket::Close(void)
 {
-  CStdString strComPath = "\\\\.\\" + name;
+  SerialSocketClose(m_socket);
+}
+
+void CSerialSocket::Shutdown(void)
+{
+  SerialSocketClose(m_socket);
+}
+
+ssize_t CSerialSocket::Write(void* data, size_t len)
+{
+  return SerialSocketWrite(m_socket, &m_iError, data, len);
+}
+
+ssize_t CSerialSocket::Read(void* data, size_t len, uint64_t iTimeoutMs /* = 0 */)
+{
+  return SerialSocketRead(m_socket, &m_iError, data, len, iTimeoutMs);
+}
+
+bool CSerialSocket::Open(uint64_t iTimeoutMs /* = 0 */)
+{
+  iTimeoutMs = 0;
+  CStdString strComPath = "\\\\.\\" + m_strName;
   CLockObject lock(m_mutex);
-  m_handle = CreateFile(strComPath.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
-  if (m_handle == INVALID_HANDLE_VALUE)
+  m_socket = CreateFile(strComPath.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
+  if (m_socket == INVALID_HANDLE_VALUE)
   {
     m_strError = "Unable to open COM port";
     FormatWindowsError(GetLastError(), m_strError);
@@ -75,7 +120,7 @@ bool CSerialPort::Open(string name, uint32_t baudrate, uint8_t databits, uint8_t
   commConfig.dwSize = dwSize;
   if (GetDefaultCommConfig(strComPath.c_str(), &commConfig,&dwSize))
   {
-    if (!SetCommConfig(m_handle, &commConfig,dwSize))
+    if (!SetCommConfig(m_socket, &commConfig,dwSize))
     {
       m_strError = "unable to set default config";
       FormatWindowsError(GetLastError(), m_strError);
@@ -87,16 +132,13 @@ bool CSerialPort::Open(string name, uint32_t baudrate, uint8_t databits, uint8_t
     FormatWindowsError(GetLastError(), m_strError);
   }
 
-  if (!SetupComm(m_handle, 64, 64))
+  if (!SetupComm(m_socket, 64, 64))
   {
     m_strError = "unable to set up the com port";
     FormatWindowsError(GetLastError(), m_strError);
   }
 
-  m_iDatabits = databits;
-  m_iStopbits = stopbits;
-  m_iParity   = parity;
-  if (!SetBaudRate(baudrate))
+  if (!SetBaudRate(m_iBaudrate))
   {
     m_strError = "unable to set baud rate";
     FormatWindowsError(GetLastError(), m_strError);
@@ -104,7 +146,7 @@ bool CSerialPort::Open(string name, uint32_t baudrate, uint8_t databits, uint8_t
     return false;
   }
 
-  if (!SetTimeouts(false))
+  if (!SetTimeouts(m_socket, &m_iError, false))
   {
     m_strError = "unable to set timeouts";
     FormatWindowsError(GetLastError(), m_strError);
@@ -116,95 +158,7 @@ bool CSerialPort::Open(string name, uint32_t baudrate, uint8_t databits, uint8_t
   return m_bIsOpen;
 }
 
-bool CSerialPort::SetTimeouts(bool bBlocking)
-{
-  if (m_handle == INVALID_HANDLE_VALUE)
-         return false;
-
-  COMMTIMEOUTS cto;
-  if (!GetCommTimeouts(m_handle, &cto))
-  {
-    m_strError = "GetCommTimeouts failed";
-    FormatWindowsError(GetLastError(), m_strError);
-    return false;
-  }
-
-  if (bBlocking)
-  {
-    cto.ReadIntervalTimeout         = 0;
-    cto.ReadTotalTimeoutConstant    = 0;
-    cto.ReadTotalTimeoutMultiplier  = 0;
-  }
-  else
-  {
-    cto.ReadIntervalTimeout         = MAXDWORD;
-    cto.ReadTotalTimeoutConstant    = 0;
-    cto.ReadTotalTimeoutMultiplier  = 0;
-  }
-
-  if (!SetCommTimeouts(m_handle, &cto))
-  {
-    m_strError = "SetCommTimeouts failed";
-    FormatWindowsError(GetLastError(), m_strError);
-    return false;
-  }
-
-  return true;
-}
-
-void CSerialPort::Close(void)
-{
-  CLockObject lock(m_mutex);
-  if (m_bIsOpen)
-  {
-    CloseHandle(m_handle);
-    m_bIsOpen = false;
-  }
-}
-
-int64_t CSerialPort::Write(uint8_t* data, uint32_t len)
-{
-  CLockObject lock(m_mutex);
-  DWORD iBytesWritten = 0;
-  if (!m_bIsOpen)
-    return -1;
-
-  if (!WriteFile(m_handle, data, len, &iBytesWritten, NULL))
-  {
-    m_strError = "Error while writing to COM port";
-    FormatWindowsError(GetLastError(), m_strError);
-    return -1;
-  }
-
-  return (int64_t)iBytesWritten;
-}
-
-int32_t CSerialPort::Read(uint8_t* data, uint32_t len, uint64_t iTimeoutMs /* = 0 */)
-{
-  CLockObject lock(m_mutex);
-  int32_t iReturn(-1);
-  DWORD iBytesRead = 0;
-  if (m_handle == 0)
-  {
-    m_strError = "Error while reading from COM port: invalid handle";
-    return iReturn;
-  }
-
-  if(!ReadFile(m_handle, data, len, &iBytesRead, NULL) != 0)
-  {
-    m_strError = "unable to read from device";
-    FormatWindowsError(GetLastError(), m_strError);
-    iReturn = -1;
-  }
-  else
-  {
-    iReturn = (int32_t) iBytesRead;
-  }
-
-  return iReturn;
-}
-
-bool CSerialPort::SetBaudRate(uint32_t baudrate)
+bool CSerialSocket::SetBaudRate(uint32_t baudrate)
 {
   int32_t rate = IntToBaudrate(baudrate);
   if (rate < 0)
@@ -225,21 +179,21 @@ bool CSerialPort::SetBaudRate(uint32_t baudrate)
        dcb.fInX          = false;
   dcb.fAbortOnError = true;
 
-  if (m_iParity == PAR_NONE)
+  if (m_iParity == SERIAL_PARITY_NONE)
     dcb.Parity = NOPARITY;
-  else if (m_iParity == PAR_EVEN)
+  else if (m_iParity == SERIAL_PARITY_EVEN)
     dcb.Parity = EVENPARITY;
   else
     dcb.Parity = ODDPARITY;
 
-  if (m_iStopbits == 2)
+  if (m_iStopbits == SERIAL_STOP_BITS_TWO)
     dcb.StopBits = TWOSTOPBITS;
   else
     dcb.StopBits = ONESTOPBIT;
 
-  dcb.ByteSize = m_iDatabits;
+  dcb.ByteSize = (BYTE)m_iDatabits;
 
-  if(!SetCommState(m_handle,&dcb))
+  if(!SetCommState(m_socket,&dcb))
   {
     m_strError = "SetCommState failed";
     FormatWindowsError(GetLastError(), m_strError);
@@ -248,9 +202,3 @@ bool CSerialPort::SetBaudRate(uint32_t baudrate)
 
   return true;
 }
-
-bool CSerialPort::IsOpen(void)
-{
-  CLockObject lock(m_mutex);
-  return m_bIsOpen;
-}