changed the year in copyright notices to 2011-2012
[deb_libcec.git] / src / lib / platform / windows / serialport.cpp
index bfdc8d53d71917095b444a527f907c0ce8352fb9..dba963157f52e0a5a6f4cbc7398682c82405eb3b 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the libCEC(R) library.
  *
- * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited.  All rights reserved.
+ * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited.  All rights reserved.
  * libCEC(R) is an original work, containing original code.
  *
  * libCEC(R) is a trademark of Pulse-Eight Limited.
  *     http://www.pulse-eight.net/
  */
 
-#include "../serialport.h"
-#include "../baudrate.h"
-#include "../timeutils.h"
+#include "../sockets/serialport.h"
+#include "../util/baudrate.h"
+#include "../util/timeutils.h"
 
 using namespace std;
-using namespace CEC;
+using namespace PLATFORM;
 
-void FormatWindowsError(int iErrorCode, string &strMessage)
+void CSerialPort::FormatWindowsError(int iErrorCode, CStdString &strMessage)
 {
   if (iErrorCode != ERROR_SUCCESS)
   {
@@ -58,42 +58,39 @@ CSerialPort::CSerialPort(void) :
 {
 }
 
-CSerialPort::~CSerialPort(void)
+bool CSerialPort::Open(string name, uint32_t baudrate, uint8_t databits, uint8_t stopbits, uint8_t parity)
 {
-  Close();
-}
-
-bool CSerialPort::Open(string name, int baudrate, int databits, int stopbits, int parity)
-{
-  m_handle = CreateFile(name.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
+  CStdString strComPath = "\\\\.\\" + name;
+  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_error = "Unable to open COM port";
-    FormatWindowsError(GetLastError(), m_error);
+    m_strError = "Unable to open COM port";
+    FormatWindowsError(GetLastError(), m_strError);
     return false;
   }
 
   COMMCONFIG commConfig = {0};
   DWORD dwSize = sizeof(commConfig);
   commConfig.dwSize = dwSize;
-  if (GetDefaultCommConfig(name.c_str(), &commConfig,&dwSize))
+  if (GetDefaultCommConfig(strComPath.c_str(), &commConfig,&dwSize))
   {
     if (!SetCommConfig(m_handle, &commConfig,dwSize))
     {
-      m_error = "unable to set default config";
-      FormatWindowsError(GetLastError(), m_error);
+      m_strError = "unable to set default config";
+      FormatWindowsError(GetLastError(), m_strError);
     }
   }
   else
   {
-    m_error = "unable to get default config";
-    FormatWindowsError(GetLastError(), m_error);
+    m_strError = "unable to get default config";
+    FormatWindowsError(GetLastError(), m_strError);
   }
 
   if (!SetupComm(m_handle, 64, 64))
   {
-    m_error = "unable to set up the com port";
-    FormatWindowsError(GetLastError(), m_error);
+    m_strError = "unable to set up the com port";
+    FormatWindowsError(GetLastError(), m_strError);
   }
 
   m_iDatabits = databits;
@@ -101,16 +98,16 @@ bool CSerialPort::Open(string name, int baudrate, int databits, int stopbits, in
   m_iParity   = parity;
   if (!SetBaudRate(baudrate))
   {
-    m_error = "unable to set baud rate";
-    FormatWindowsError(GetLastError(), m_error);
+    m_strError = "unable to set baud rate";
+    FormatWindowsError(GetLastError(), m_strError);
     Close();
     return false;
   }
 
   if (!SetTimeouts(false))
   {
-    m_error = "unable to set timeouts";
-    FormatWindowsError(GetLastError(), m_error);
+    m_strError = "unable to set timeouts";
+    FormatWindowsError(GetLastError(), m_strError);
     Close();
     return false;
   }
@@ -127,8 +124,8 @@ bool CSerialPort::SetTimeouts(bool bBlocking)
   COMMTIMEOUTS cto;
   if (!GetCommTimeouts(m_handle, &cto))
   {
-    m_error = "GetCommTimeouts failed";
-    FormatWindowsError(GetLastError(), m_error);
+    m_strError = "GetCommTimeouts failed";
+    FormatWindowsError(GetLastError(), m_strError);
     return false;
   }
 
@@ -147,8 +144,8 @@ bool CSerialPort::SetTimeouts(bool bBlocking)
 
   if (!SetCommTimeouts(m_handle, &cto))
   {
-    m_error = "SetCommTimeouts failed";
-    FormatWindowsError(GetLastError(), m_error);
+    m_strError = "SetCommTimeouts failed";
+    FormatWindowsError(GetLastError(), m_strError);
     return false;
   }
 
@@ -157,6 +154,7 @@ bool CSerialPort::SetTimeouts(bool bBlocking)
 
 void CSerialPort::Close(void)
 {
+  CLockObject lock(m_mutex);
   if (m_bIsOpen)
   {
     CloseHandle(m_handle);
@@ -164,44 +162,55 @@ void CSerialPort::Close(void)
   }
 }
 
-int CSerialPort::Write(uint8_t* data, int len)
+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_error = "Error while writing to COM port";
-    FormatWindowsError(GetLastError(), m_error);
+    m_strError = "Error while writing to COM port";
+    FormatWindowsError(GetLastError(), m_strError);
     return -1;
   }
 
-  return (intiBytesWritten;
+  return (int64_t)iBytesWritten;
 }
 
-int CSerialPort::Read(uint8_t* data, int len, int iTimeoutMs /* = -1 */)
+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_error = "Error while reading from COM port: invalid handle";
-    return -1;
+    m_strError = "Error while reading from COM port: invalid handle";
+    return iReturn;
   }
 
   if(!ReadFile(m_handle, data, len, &iBytesRead, NULL) != 0)
   {
-    m_error = "unable to read from device";
-    FormatWindowsError(GetLastError(), m_error);
-    iBytesRead = -1;
+    m_strError = "unable to read from device";
+    FormatWindowsError(GetLastError(), m_strError);
+    iReturn = -1;
+  }
+  else
+  {
+    iReturn = (int32_t) iBytesRead;
   }
 
-  return (int) iBytesRead;
+  return iReturn;
 }
 
-bool CSerialPort::SetBaudRate(int baudrate)
+bool CSerialPort::SetBaudRate(uint32_t baudrate)
 {
-  m_iBaudrate = baudrate;
+  int32_t rate = IntToBaudrate(baudrate);
+  if (rate < 0)
+    m_iBaudrate = baudrate > 0 ? baudrate : 0;
+  else
+    m_iBaudrate = rate;
 
   DCB dcb;
   memset(&dcb,0,sizeof(dcb));
@@ -232,15 +241,16 @@ bool CSerialPort::SetBaudRate(int baudrate)
 
   if(!SetCommState(m_handle,&dcb))
   {
-    m_error = "SetCommState failed";
-    FormatWindowsError(GetLastError(), m_error);
+    m_strError = "SetCommState failed";
+    FormatWindowsError(GetLastError(), m_strError);
     return false;
   }
 
   return true;
 }
 
-bool CSerialPort::IsOpen() const
+bool CSerialPort::IsOpen(void)
 {
+  CLockObject lock(m_mutex);
   return m_bIsOpen;
 }