cec: put the message processing code from WaitForAck() in ParseMessage(). fixes missi...
authorLars Op den Kamp <lars@opdenkamp.eu>
Fri, 14 Oct 2011 08:51:29 +0000 (10:51 +0200)
committerLars Op den Kamp <lars@opdenkamp.eu>
Fri, 14 Oct 2011 08:51:29 +0000 (10:51 +0200)
src/lib/CECProcessor.cpp
src/lib/CECProcessor.h

index 1b86820447a2e36d3522e54daf688b6a0dd5ad34..ae7d5fd16b93c1b7ecab70f2b8c784b8234f46b5 100644 (file)
@@ -92,14 +92,17 @@ void *CCECProcessor::Process(void)
   while (!IsStopped())
   {
     bool bParseFrame(false);
+    bool bError(false);
+    bool bTransmitSucceeded(false);
     command.clear();
     msg.clear();
 
     {
       CLockObject lock(&m_mutex);
       if (m_communication->IsOpen() && m_communication->Read(msg, 50))
-        bParseFrame = ParseMessage(msg) && !IsStopped();
+        ParseMessage(msg, &bError, &bTransmitSucceeded, &bParseFrame);
 
+      bParseFrame &= !IsStopped();
       if (bParseFrame)
         command = m_currentframe;
     }
@@ -339,13 +342,13 @@ void CCECProcessor::BroadcastActiveSource(void)
 
 bool CCECProcessor::WaitForAck(bool *bError, uint32_t iTimeout /* = 1000 */)
 {
-  bool bGotAck(false);
+  bool bTransmitSucceeded = false, bEom = false;
   *bError = false;
 
   int64_t iNow = GetTimeMs();
   int64_t iTargetTime = iNow + (uint64_t) iTimeout;
 
-  while (!bGotAck && !*bError && (iTimeout == 0 || iNow < iTargetTime))
+  while (!bTransmitSucceeded && !*bError && (iTimeout == 0 || iNow < iTargetTime))
   {
     cec_adapter_message msg;
     msg.clear();
@@ -356,56 +359,21 @@ bool CCECProcessor::WaitForAck(bool *bError, uint32_t iTimeout /* = 1000 */)
       continue;
     }
 
-    switch (msg.message())
-    {
-    case MSGCODE_COMMAND_ACCEPTED:
-      m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_COMMAND_ACCEPTED");
-      break;
-    case MSGCODE_TRANSMIT_SUCCEEDED:
-      m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_TRANSMIT_SUCCEEDED");
-      bGotAck = true;
-      break;
-    case MSGCODE_RECEIVE_FAILED:
-      m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_RECEIVE_FAILED");
-      *bError = true;
-      break;
-    case MSGCODE_COMMAND_REJECTED:
-      m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_COMMAND_REJECTED");
-      *bError = true;
-      break;
-    case MSGCODE_TRANSMIT_FAILED_LINE:
-      m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_LINE");
-      *bError = true;
-      break;
-    case MSGCODE_TRANSMIT_FAILED_ACK:
-      m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_ACK");
-      *bError = true;
-      break;
-    case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA:
-      m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA");
-      *bError = true;
-      break;
-    case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE:
-      m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE");
-      *bError = true;
-      break;
-    default:
-      m_frameBuffer.Push(msg);
-      break;
-    }
-
+    ParseMessage(msg, bError, &bTransmitSucceeded, &bEom, false);
     iNow = GetTimeMs();
   }
 
-  return bGotAck && !*bError;
+  return bTransmitSucceeded && !*bError;
 }
 
-bool CCECProcessor::ParseMessage(cec_adapter_message &msg)
+void CCECProcessor::ParseMessage(cec_adapter_message &msg, bool *bError, bool *bTransmitSucceeded, bool *bEom, bool bProcessMessages /* = true */)
 {
-  bool bReturn(false);
+  *bError = false;
+  *bTransmitSucceeded = false;
+  *bEom = false;
 
   if (msg.empty())
-    return bReturn;
+    return;
 
   CStdString logStr;
 
@@ -430,43 +398,87 @@ bool CCECProcessor::ParseMessage(cec_adapter_message &msg)
       logStr.AppendFormat(" line:%i", iLine);
       logStr.AppendFormat(" time:%u", iTime);
       m_controller->AddLog(CEC_LOG_WARNING, logStr.c_str());
+      *bError = true;
     }
     break;
   case MSGCODE_FRAME_START:
     {
-      logStr = "MSGCODE_FRAME_START";
-      m_currentframe.clear();
-      if (msg.size() >= 2)
+      if (bProcessMessages)
+      {
+        logStr = "MSGCODE_FRAME_START";
+        m_currentframe.clear();
+        if (msg.size() >= 2)
+        {
+          logStr.AppendFormat(" initiator:%u destination:%u ack:%s %s", msg.initiator(), msg.destination(), msg.ack() ? "high" : "low", msg.eom() ? "eom" : "");
+          m_currentframe.initiator   = msg.initiator();
+          m_currentframe.destination = msg.destination();
+          m_currentframe.ack         = msg.ack();
+          m_currentframe.eom         = msg.eom();
+        }
+        m_controller->AddLog(CEC_LOG_DEBUG, logStr.c_str());
+      }
+      else
       {
-        logStr.AppendFormat(" initiator:%u destination:%u ack:%s %s", msg.initiator(), msg.destination(), msg.ack() ? "high" : "low", msg.eom() ? "eom" : "");
-        m_currentframe.initiator   = msg.initiator();
-        m_currentframe.destination = msg.destination();
-        m_currentframe.ack         = msg.ack();
-        m_currentframe.eom         = msg.eom();
+        m_frameBuffer.Push(msg);
       }
-      m_controller->AddLog(CEC_LOG_DEBUG, logStr.c_str());
     }
     break;
   case MSGCODE_FRAME_DATA:
     {
-      logStr = "MSGCODE_FRAME_DATA";
-      if (msg.size() >= 2)
+      if (bProcessMessages)
       {
-        uint8_t iData = msg[1];
-        logStr.AppendFormat(" %02x", iData);
-        m_currentframe.push_back(iData);
-        m_currentframe.eom = msg.eom();
+        logStr = "MSGCODE_FRAME_DATA";
+        if (msg.size() >= 2)
+        {
+          uint8_t iData = msg[1];
+          logStr.AppendFormat(" %02x", iData);
+          m_currentframe.push_back(iData);
+          m_currentframe.eom = msg.eom();
+        }
+        m_controller->AddLog(CEC_LOG_DEBUG, logStr.c_str());
       }
-      m_controller->AddLog(CEC_LOG_DEBUG, logStr.c_str());
+      else
+      {
+        m_frameBuffer.Push(msg);
+      }
+
+      *bEom = msg.eom();
     }
-    if (msg.eom())
-      bReturn = true;
+    break;
+  case MSGCODE_COMMAND_ACCEPTED:
+    m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_COMMAND_ACCEPTED");
+    break;
+  case MSGCODE_TRANSMIT_SUCCEEDED:
+    m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_TRANSMIT_SUCCEEDED");
+    *bTransmitSucceeded = true;
+    break;
+  case MSGCODE_RECEIVE_FAILED:
+    m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_RECEIVE_FAILED");
+    *bError = true;
+    break;
+  case MSGCODE_COMMAND_REJECTED:
+    m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_COMMAND_REJECTED");
+    *bError = true;
+    break;
+  case MSGCODE_TRANSMIT_FAILED_LINE:
+    m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_LINE");
+    *bError = true;
+    break;
+  case MSGCODE_TRANSMIT_FAILED_ACK:
+    m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_ACK");
+    *bError = true;
+    break;
+  case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA:
+    m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA");
+    *bError = true;
+    break;
+  case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE:
+    m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE");
+    *bError = true;
     break;
   default:
     break;
   }
-
-  return bReturn;
 }
 
 void CCECProcessor::ParseVendorId(cec_logical_address device, const cec_datapacket &data)
index 39f1a2efc362b753db1aeb0f26902e300ec56f0c..8617257f30437a3236c32cd613a2a6a89ed997b6 100644 (file)
@@ -75,7 +75,7 @@ namespace CEC
     private:
       void LogOutput(const cec_command &data);
       bool WaitForAck(bool *bError, uint32_t iTimeout = 1000);
-      bool ParseMessage(cec_adapter_message &msg);
+      void ParseMessage(cec_adapter_message &msg, bool *bError, bool *bTransmitSucceeded, bool *bEom, bool bProcessMessages = true);
       void ParseCommand(cec_command &command);
       void ParseVendorId(cec_logical_address device, const cec_datapacket &data);