cec: use the correct Sleep() method in CCECProcessor::OpenConnection(). the thread...
[deb_libcec.git] / src / lib / adapter / USBCECAdapterCommunication.cpp
index 80670e42f5c981f01bb2b1aff2c2d6c0fec345f6..5400325bb215ff2bf7a41c2e7245df5acaf0541f 100644 (file)
@@ -67,7 +67,8 @@ CUSBCECAdapterCommunication::CUSBCECAdapterCommunication(CCECProcessor *processo
     m_lastInitiator(CECDEVICE_UNKNOWN),
     m_bNextIsEscaped(false),
     m_bGotStart(false),
-    m_messageProcessor(NULL)
+    m_messageProcessor(NULL),
+    m_bInitialised(false)
 {
   m_port = new PLATFORM::CSerialPort(strPort, iBaudRate);
 }
@@ -119,10 +120,15 @@ bool CUSBCECAdapterCommunication::CheckAdapter(uint32_t iTimeoutMs /* = 10000 */
   else
     bReturn = true;
 
+  {
+    CLockObject lock(m_mutex);
+    m_bInitialised = bReturn;
+  }
+
   return bReturn;
 }
 
-bool CUSBCECAdapterCommunication::Open(IAdapterCommunicationCallback *cb, uint32_t iTimeoutMs /* = 10000 */)
+bool CUSBCECAdapterCommunication::Open(IAdapterCommunicationCallback *cb, uint32_t iTimeoutMs /* = 10000 */, bool bSkipChecks /* = false */)
 {
   uint64_t iNow = GetTimeMs();
   uint64_t iTimeout = iNow + iTimeoutMs;
@@ -163,21 +169,25 @@ bool CUSBCECAdapterCommunication::Open(IAdapterCommunicationCallback *cb, uint32
 
     CLibCEC::AddLog(CEC_LOG_DEBUG, "connection opened, clearing any previous input and waiting for active transmissions to end before starting");
 
-    //clear any input bytes
-    uint8_t buff[1024];
-    while (m_port->Read(buff, 1024, 100) > 0)
+    if (!bSkipChecks)
     {
-      CLibCEC::AddLog(CEC_LOG_DEBUG, "data received, clearing it");
-      Sleep(250);
+      //clear any input bytes
+      uint8_t buff[1024];
+      while (m_port->Read(buff, 1024, 100) > 0)
+      {
+        CLibCEC::AddLog(CEC_LOG_DEBUG, "data received, clearing it");
+        Sleep(250);
+      }
     }
   }
 
   if (CreateThread())
   {
-    if (!CheckAdapter())
+    if (!bSkipChecks && !CheckAdapter())
     {
       StopThread();
       CLibCEC::AddLog(CEC_LOG_ERROR, "the adapter failed to pass basic checks");
+      return false;
     }
     else
     {
@@ -208,7 +218,7 @@ void *CUSBCECAdapterCommunication::Process(void)
     {
       CLockObject lock(m_mutex);
       ReadFromDevice(50);
-      bCommandReceived = m_callback && Read(command, 0);
+      bCommandReceived = m_callback && Read(command, 0) && m_bInitialised;
     }
 
     /* push the next command to the callback method if there is one */
@@ -320,7 +330,7 @@ bool CUSBCECAdapterCommunication::Read(CCECAdapterMessage &msg, uint32_t iTimeou
     if (iTimeout == 0 || !m_rcvCondition.Wait(m_mutex, m_bHasData, iTimeout))
       return false;
     m_inBuffer.Pop(buf);
-    m_bHasData = m_inBuffer.Size() > 0;
+    m_bHasData = !m_inBuffer.IsEmpty();
   }
 
   if (buf)
@@ -425,7 +435,6 @@ bool CUSBCECAdapterCommunication::ParseMessage(const CCECAdapterMessage &msg)
         m_currentframe.PushBack(msg[1]);
         m_currentframe.eom = msg.IsEOM();
       }
-      bEom = msg.IsEOM();
     }
     break;
   default:
@@ -433,7 +442,7 @@ bool CUSBCECAdapterCommunication::ParseMessage(const CCECAdapterMessage &msg)
   }
 
   CLibCEC::AddLog(bIsError ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString());
-  return bEom;
+  return msg.IsEOM();
 }
 
 uint16_t CUSBCECAdapterCommunication::GetFirmwareVersion(void)