cec: fix reconnect
[deb_libcec.git] / src / lib / AdapterCommunication.cpp
index a7466b70a1013c11fb9e231b1e0eb130434f1134..ae1a4e451c288fd4820cc3527efaf4084d67f20b 100644 (file)
@@ -40,6 +40,7 @@ using namespace std;
 using namespace CEC;
 
 CAdapterCommunication::CAdapterCommunication(CLibCEC *controller) :
+    m_port(NULL),
     m_controller(controller),
     m_inbuf(NULL),
     m_iInbufSize(0),
@@ -52,14 +53,19 @@ CAdapterCommunication::CAdapterCommunication(CLibCEC *controller) :
 
 CAdapterCommunication::~CAdapterCommunication(void)
 {
-  m_port->Close();
-  m_port = NULL;
+  Close();
+
+  if (m_port)
+  {
+    delete m_port;
+    m_port = NULL;
+  }
 }
 
-bool CAdapterCommunication::Open(const char *strPort, int iBaudRate /* = 38400 */, int iTimeoutMs /* = 10000 */)
+bool CAdapterCommunication::Open(const char *strPort, uint16_t iBaudRate /* = 38400 */, uint64_t iTimeoutMs /* = 10000 */)
 {
   CLockObject lock(&m_commMutex);
-  if (m_bStarted)
+  if (m_bStarted || !m_port)
     return false;
 
   if (!m_port->Open(strPort, iBaudRate))
@@ -76,14 +82,12 @@ bool CAdapterCommunication::Open(const char *strPort, int iBaudRate /* = 38400 *
   uint8_t buff[1024];
   m_port->Read(buff, sizeof(buff), 50);
 
-  CCondition::Sleep(CEC_SETTLE_DOWN_TIME);
-
-  m_bStop = false;
-  m_bStarted = true;
+  Sleep(CEC_SETTLE_DOWN_TIME);
 
   if (CreateThread())
   {
     m_controller->AddLog(CEC_LOG_DEBUG, "reader thread created");
+    m_bStarted = true;
     return true;
   }
   else
@@ -96,37 +100,45 @@ bool CAdapterCommunication::Open(const char *strPort, int iBaudRate /* = 38400 *
 
 void CAdapterCommunication::Close(void)
 {
+  CLockObject lock(&m_commMutex);
+  if (m_port)
+    m_port->Close();
+
   StopThread();
-  m_port->Close();
 }
 
 void *CAdapterCommunication::Process(void)
 {
+  m_controller->AddLog(CEC_LOG_DEBUG, "communication thread started");
+
   while (!m_bStop)
   {
+    CLockObject lock(&m_commMutex);
     if (!ReadFromDevice(250))
     {
       m_bStarted = false;
       break;
     }
 
-    CCondition::Sleep(50);
+    if (!m_bStop)
+    {
+      lock.Leave();
+      Sleep(50);
+    }
   }
 
-  m_controller->AddLog(CEC_LOG_DEBUG, "reader thread terminated");
-
-  CLockObject lock(&m_commMutex);
   m_bStarted = false;
   return NULL;
 }
 
-bool CAdapterCommunication::ReadFromDevice(int iTimeout)
+bool CAdapterCommunication::ReadFromDevice(uint64_t iTimeout)
 {
   uint8_t buff[1024];
-  CLockObject lock(&m_commMutex);
-  int iBytesRead = m_port->Read(buff, sizeof(buff), iTimeout);
-  lock.Leave();
-  if (iBytesRead < 0)
+  if (!m_port)
+    return false;
+
+  int32_t iBytesRead = m_port->Read(buff, sizeof(buff), iTimeout);
+  if (iBytesRead < 0 || iBytesRead > 256)
   {
     CStdString strError;
     strError.Format("error reading from serial port: %s", m_port->GetError().c_str());
@@ -134,12 +146,12 @@ bool CAdapterCommunication::ReadFromDevice(int iTimeout)
     return false;
   }
   else if (iBytesRead > 0)
-    AddData(buff, iBytesRead);
+    AddData(buff, (uint8_t) iBytesRead);
 
   return true;
 }
 
-void CAdapterCommunication::AddData(uint8_t *data, int iLen)
+void CAdapterCommunication::AddData(uint8_t *data, uint8_t iLen)
 {
   CLockObject lock(&m_bufferMutex);
   if (iLen + m_iInbufUsed > m_iInbufSize)
@@ -158,7 +170,7 @@ bool CAdapterCommunication::Write(const cec_frame &data)
 {
   CLockObject lock(&m_commMutex);
 
-  if (m_port->Write(data) != data.size())
+  if (m_port->Write(data) != (int) data.size())
   {
     CStdString strError;
     strError.Format("error writing to serial port: %s", m_port->GetError().c_str());
@@ -168,12 +180,12 @@ bool CAdapterCommunication::Write(const cec_frame &data)
 
   m_controller->AddLog(CEC_LOG_DEBUG, "command sent");
 
-  CCondition::Sleep((int) data.size() * 24 /*data*/ + 5 /*start bit (4.5 ms)*/ + 50 /* to be on the safe side */);
+  Sleep((int) data.size() * 24 /*data*/ + 5 /*start bit (4.5 ms)*/ + 50 /* to be on the safe side */);
 
   return true;
 }
 
-bool CAdapterCommunication::Read(cec_frame &msg, int iTimeout)
+bool CAdapterCommunication::Read(cec_frame &msg, uint64_t iTimeout)
 {
   CLockObject lock(&m_bufferMutex);