cec: fixed - frequency wasn't checked in GetTimeMs(), leading to incorrect wait times...
[deb_libcec.git] / src / lib / adapter / USBCECAdapterCommunication.cpp
index 64212cb18a99cb01823c0326d5fd42e88ee8250d..49bd9ea5fa40521affe14746eedccff79d7d9fed 100644 (file)
@@ -66,12 +66,14 @@ CUSBCECAdapterCommunication::CUSBCECAdapterCommunication(CCECProcessor *processo
     m_bHasData(false),
     m_iLineTimeout(0),
     m_iFirmwareVersion(CEC_FW_VERSION_UNKNOWN),
-    m_lastInitiator(CECDEVICE_UNKNOWN),
+    m_lastDestination(CECDEVICE_UNKNOWN),
     m_bNextIsEscaped(false),
     m_bGotStart(false),
     m_messageProcessor(NULL),
     m_bInitialised(false)
 {
+  for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
+    m_bWaitingForAck[iPtr] = false;
   m_port = new CSerialPort(strPort, iBaudRate);
 }
 
@@ -181,9 +183,20 @@ bool CUSBCECAdapterCommunication::Open(IAdapterCommunicationCallback *cb, uint32
     {
       //clear any input bytes
       uint8_t buff[1024];
-      while (m_port->Read(buff, 1024, 100) > 0)
+      ssize_t iBytesRead(0);
+      bool bGotMsgStart(false), bGotMsgEnd(false);
+      while ((iBytesRead = m_port->Read(buff, 1024, 100)) > 0 || (bGotMsgStart && !bGotMsgEnd))
       {
-        CLibCEC::AddLog(CEC_LOG_DEBUG, "data received, clearing it");
+        if (!bGotMsgStart)
+          CLibCEC::AddLog(CEC_LOG_DEBUG, "data received, clearing it");
+        // if something was received, wait for MSGEND
+        for (ssize_t iPtr = 0; iPtr < iBytesRead; iPtr++)
+        {
+          if (buff[iPtr] == MSGSTART)
+            bGotMsgStart = true;
+          else if (buff[iPtr] == MSGEND)
+            bGotMsgEnd = true;
+        }
         Sleep(250);
       }
     }
@@ -292,6 +305,11 @@ cec_adapter_message_state CUSBCECAdapterCommunication::Write(const cec_command &
   output->retryTimeout = iRetryLineTimeout;
   output->tries = 0;
 
+  {
+    CLockObject lock(m_mutex);
+    m_bWaitingForAck[data.destination] = true;
+  }
+
   bool bRetry(true);
   while (bRetry && ++output->tries < output->maxTries)
   {
@@ -415,17 +433,19 @@ bool CUSBCECAdapterCommunication::ParseMessage(const CCECAdapterMessage &msg)
       }
       if (m_currentframe.ack == 0x1)
       {
-        m_lastInitiator    = m_currentframe.initiator;
-        m_currentframe.eom = 1;
-        bEom = true;
+        m_lastDestination    = m_currentframe.destination;
+        if (!m_bWaitingForAck[m_currentframe.destination])
+          m_processor->HandlePoll(m_currentframe.initiator, m_currentframe.destination);
+        else
+          m_bWaitingForAck[m_currentframe.destination] = false;
       }
     }
     break;
   case MSGCODE_RECEIVE_FAILED:
     {
       m_currentframe.Clear();
-      if (m_lastInitiator != CECDEVICE_UNKNOWN)
-        bIsError = m_processor->HandleReceiveFailed(m_lastInitiator);
+      if (m_lastDestination != CECDEVICE_UNKNOWN)
+        bIsError = m_processor->HandleReceiveFailed(m_lastDestination);
     }
     break;
   case MSGCODE_FRAME_DATA:
@@ -442,7 +462,7 @@ bool CUSBCECAdapterCommunication::ParseMessage(const CCECAdapterMessage &msg)
   }
 
   CLibCEC::AddLog(bIsError ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString());
-  return msg.IsEOM() || bEom;
+  return msg.IsEOM();
 }
 
 uint16_t CUSBCECAdapterCommunication::GetFirmwareVersion(void)
@@ -795,15 +815,20 @@ bool CUSBCECAdapterCommunication::WaitForAck(CCECAdapterMessage &message)
 
     if (msg.Message() == MSGCODE_FRAME_START && msg.IsACK())
     {
-      m_processor->HandlePoll(msg.Initiator(), msg.Destination());
-      m_lastInitiator = msg.Initiator();
+      if (m_bWaitingForAck[msg.Initiator()])
+        m_bWaitingForAck[msg.Initiator()] = false;
+      else
+      {
+        m_processor->HandlePoll(msg.Initiator(), msg.Destination());
+        m_lastDestination = msg.Initiator();
+      }
       iNow = GetTimeMs();
       continue;
     }
 
     if (msg.Message() == MSGCODE_RECEIVE_FAILED &&
-        m_lastInitiator != CECDEVICE_UNKNOWN &&
-        m_processor->HandleReceiveFailed(m_lastInitiator))
+        m_lastDestination != CECDEVICE_UNKNOWN &&
+        m_processor->HandleReceiveFailed(m_lastDestination))
     {
       iNow = GetTimeMs();
       continue;