cec: only set the logical address once when it hasn't changed
[deb_libcec.git] / src / lib / CECProcessor.cpp
index 08f69cd7b1674cc994f37c0468c16bbdd49b6b23..7e8af29c0a88abce63037bb29e2e614f5e5133f5 100644 (file)
@@ -88,7 +88,7 @@ void *CCECProcessor::Process(void)
   m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started");
 
   cec_command command;
-  cec_adapter_message msg;
+  CCECAdapterMessage msg;
 
   while (!IsStopped())
   {
@@ -147,25 +147,19 @@ void CCECProcessor::LogOutput(const cec_command &data)
   m_controller->AddLog(CEC_LOG_TRAFFIC, strTx.c_str());
 }
 
-bool CCECProcessor::Transmit(const cec_command &data, bool bWaitForAck /* = true */)
-{
-  LogOutput(data);
-
-  cec_adapter_message output;
-  output.clear();
-  CAdapterCommunication::FormatAdapterMessage(data, output);
-
-  return TransmitFormatted(output, bWaitForAck);
-}
-
 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress)
 {
-  CStdString strLog;
-  strLog.Format("<< setting logical address to %1x", iLogicalAddress);
-  m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
+  if (m_iLogicalAddress != iLogicalAddress)
+  {
+    CStdString strLog;
+    strLog.Format("<< setting logical address to %1x", iLogicalAddress);
+    m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
+
+    m_iLogicalAddress = iLogicalAddress;
+    return m_communication && m_communication->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress);
+  }
 
-  m_iLogicalAddress = iLogicalAddress;
-  return m_communication && m_communication->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress);
+  return true;
 }
 
 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress)
@@ -187,26 +181,21 @@ bool CCECProcessor::SwitchMonitoring(bool bEnable)
     return m_communication && m_communication->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress);
 }
 
-bool CCECProcessor::TransmitFormatted(const cec_adapter_message &data, bool bWaitForAck /* = true */)
+bool CCECProcessor::Transmit(const cec_command &data, bool bWaitForAck /* = true */)
 {
   bool bReturn(false);
+  LogOutput(data);
+
+  CCECAdapterMessage output(data);
+
   CLockObject lock(&m_mutex);
-  if (!m_communication || !m_communication->Write(data))
+  if (!m_communication || !m_communication->Write(output))
     return bReturn;
 
   if (bWaitForAck)
   {
-    uint64_t now = GetTimeMs();
-    uint64_t target = now + 1000;
     bool bError(false);
-
-    while (!bReturn && now < target && !bError)
-    {
-      bReturn = WaitForAck(&bError, data.size(), (uint32_t) (target - now));
-      now = GetTimeMs();
-    }
-
-    if (!bReturn)
+    if ((bReturn = WaitForAck(&bError, output.size(), 1000)) == false)
       m_controller->AddLog(CEC_LOG_ERROR, "did not receive ack");
   }
   else
@@ -240,8 +229,7 @@ bool CCECProcessor::WaitForAck(bool *bError, uint8_t iLength, uint32_t iTimeout
 
   while (!bTransmitSucceeded && !*bError && (iTimeout == 0 || iNow < iTargetTime))
   {
-    cec_adapter_message msg;
-    msg.clear();
+    CCECAdapterMessage msg;
 
     if (!m_communication->Read(msg, iTimeout > 0 ? (int32_t)(iTargetTime - iNow) : 1000))
     {
@@ -315,7 +303,7 @@ bool CCECProcessor::WaitForAck(bool *bError, uint8_t iLength, uint32_t iTimeout
   return bTransmitSucceeded && !*bError;
 }
 
-bool CCECProcessor::ParseMessage(cec_adapter_message &msg)
+bool CCECProcessor::ParseMessage(CCECAdapterMessage &msg)
 {
   bool bEom = false;