cec: move WaitForTransmissionSucceeded() to CAdapterCommunication and wait for MSGCOD...
authorLars Op den Kamp <lars@opdenkamp.eu>
Tue, 24 Jan 2012 23:15:11 +0000 (00:15 +0100)
committerLars Op den Kamp <lars@opdenkamp.eu>
Tue, 24 Jan 2012 23:20:06 +0000 (00:20 +0100)
src/lib/CECProcessor.cpp
src/lib/adapter/AdapterCommunication.cpp
src/lib/adapter/AdapterCommunication.h
src/lib/adapter/AdapterMessage.h

index 3a5742bd8c8c789fd56b2f6e42a1aeae351ee056..cb4fe8101e31adb144515dab7009830ea71f3c0c 100644 (file)
@@ -861,36 +861,19 @@ bool CCECProcessor::Transmit(CCECAdapterMessage *output)
   bool bReturn(false);
   CLockObject lock(m_mutex);
   {
+    if (!m_communication)
+      return bReturn;
+
     m_iLastTransmission = GetTimeMs();
     m_communication->SetLineTimeout(m_iStandardLineTimeout);
-    output->tries = 1;
+    output->tries = 0;
 
     do
     {
       if (output->tries > 0)
         m_communication->SetLineTimeout(m_iRetryLineTimeout);
-
-      CLockObject msgLock(output->mutex);
-      if (!m_communication || !m_communication->Write(output))
-        return bReturn;
-      else
-      {
-        output->condition.Wait(output->mutex);
-        if (output->state != ADAPTER_MESSAGE_STATE_SENT)
-        {
-          m_controller->AddLog(CEC_LOG_ERROR, "command was not sent");
-          return bReturn;
-        }
-      }
-
-      if (output->transmit_timeout > 0)
-      {
-        if ((bReturn = m_communication->WaitForTransmitSucceeded(output)) == false)
-          m_controller->AddLog(CEC_LOG_DEBUG, "did not receive ack");
-      }
-      else
-        bReturn = true;
-    }while (output->transmit_timeout > 0 && output->NeedsRetry() && ++output->tries < output->maxTries);
+      bReturn = m_communication->Write(output);
+    }while (!bReturn && output->transmit_timeout > 0 && output->NeedsRetry() && ++output->tries < output->maxTries);
   }
 
   m_communication->SetLineTimeout(m_iStandardLineTimeout);
@@ -1036,25 +1019,7 @@ void CCECProcessor::AddLog(cec_log_level level, const CStdString &strMessage)
 
 bool CCECProcessor::SetAckMask(uint16_t iMask)
 {
-  bool bReturn(false);
-  CStdString strLog;
-  strLog.Format("setting ackmask to %2x", iMask);
-  m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
-
-  CCECAdapterMessage *output = new CCECAdapterMessage;
-
-  output->PushBack(MSGSTART);
-  output->PushEscaped(MSGCODE_SET_ACK_MASK);
-  output->PushEscaped(iMask >> 8);
-  output->PushEscaped((uint8_t)iMask);
-  output->PushBack(MSGEND);
-
-  if ((bReturn = Transmit(output)) == false)
-    m_controller->AddLog(CEC_LOG_ERROR, "could not set the ackmask");
-
-  delete output;
-
-  return bReturn;
+  return m_communication->SetAckMask(iMask);
 }
 
 bool CCECProcessor::TransmitKeypress(cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = true */)
index a792a1d77071de9c9cb80cd3b395a429a69a605e..821a4f2aa276e919e3156d521b2114bd53b5248c 100644 (file)
@@ -140,9 +140,30 @@ void *CAdapterCommunication::Process(void)
 
 bool CAdapterCommunication::Write(CCECAdapterMessage *data)
 {
-  data->state = ADAPTER_MESSAGE_STATE_WAITING;
+  bool bReturn(false);
+
+  CLockObject lock(data->mutex);
+  data->state = ADAPTER_MESSAGE_STATE_WAITING_TO_BE_SENT;
   m_outBuffer.Push(data);
-  return true;
+  data->condition.Wait(data->mutex);
+
+  if (data->state != ADAPTER_MESSAGE_STATE_SENT)
+  {
+    m_processor->AddLog(CEC_LOG_ERROR, "command was not sent");
+  }
+  if (WaitForTransmitSucceeded(data))
+  {
+    if (data->isTransmission)
+      data->state = ADAPTER_MESSAGE_STATE_SENT_ACKED;
+    bReturn = true;
+  }
+  else
+  {
+    data->state = ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED;
+    m_processor->AddLog(CEC_LOG_DEBUG, "did not receive ack");
+  }
+
+  return bReturn;
 }
 
 bool CAdapterCommunication::Read(CCECAdapterMessage &msg, uint32_t iTimeout)
@@ -195,7 +216,7 @@ bool CAdapterCommunication::Read(CCECAdapterMessage &msg, uint32_t iTimeout)
   }
 
   if (bGotFullMessage)
-    msg.state = ADAPTER_MESSAGE_STATE_RECEIVED;
+    msg.state = ADAPTER_MESSAGE_STATE_INCOMING;
 
   return bGotFullMessage;
 }
@@ -217,11 +238,10 @@ bool CAdapterCommunication::StartBootloader(void)
   output->PushBack(MSGSTART);
   output->PushEscaped(MSGCODE_START_BOOTLOADER);
   output->PushBack(MSGEND);
+  output->isTransmission = false;
 
-  CLockObject lock(output->mutex);
-  if (Write(output))
-    output->condition.Wait(output->mutex);
-  bReturn = output->state == ADAPTER_MESSAGE_STATE_SENT;
+  if ((bReturn = Write(output)) == false)
+    m_processor->AddLog(CEC_LOG_ERROR, "could not start the bootloader");
   delete output;
 
   return bReturn;
@@ -239,11 +259,10 @@ bool CAdapterCommunication::PingAdapter(void)
   output->PushBack(MSGSTART);
   output->PushEscaped(MSGCODE_PING);
   output->PushBack(MSGEND);
+  output->isTransmission = false;
 
-  CLockObject lock(output->mutex);
-  if (Write(output))
-    output->condition.Wait(output->mutex);
-  bReturn = output->state == ADAPTER_MESSAGE_STATE_SENT;
+  if ((bReturn = Write(output)) == false)
+    m_processor->AddLog(CEC_LOG_ERROR, "could not ping the adapter");
   delete output;
 
   return bReturn;
@@ -261,6 +280,7 @@ bool CAdapterCommunication::SetLineTimeout(uint8_t iTimeout)
     output->PushEscaped(MSGCODE_TRANSMIT_IDLETIME);
     output->PushEscaped(iTimeout);
     output->PushBack(MSGEND);
+    output->isTransmission = false;
 
     if ((bReturn = Write(output)) == false)
       m_processor->AddLog(CEC_LOG_ERROR, "could not set the idletime");
@@ -270,6 +290,29 @@ bool CAdapterCommunication::SetLineTimeout(uint8_t iTimeout)
   return bReturn;
 }
 
+bool CAdapterCommunication::SetAckMask(uint16_t iMask)
+{
+  bool bReturn(false);
+  CStdString strLog;
+  strLog.Format("setting ackmask to %2x", iMask);
+  m_processor->AddLog(CEC_LOG_DEBUG, strLog.c_str());
+
+  CCECAdapterMessage *output = new CCECAdapterMessage;
+
+  output->PushBack(MSGSTART);
+  output->PushEscaped(MSGCODE_SET_ACK_MASK);
+  output->PushEscaped(iMask >> 8);
+  output->PushEscaped((uint8_t)iMask);
+  output->PushBack(MSGEND);
+  output->isTransmission = false;
+
+  if ((bReturn = Write(output)) == false)
+    m_processor->AddLog(CEC_LOG_ERROR, "could not set the ackmask");
+  delete output;
+
+  return bReturn;
+}
+
 bool CAdapterCommunication::IsOpen(void)
 {
   return !IsStopped() && m_port->IsOpen() && IsRunning();
@@ -322,6 +365,8 @@ bool CAdapterCommunication::WaitForTransmitSucceeded(CCECAdapterMessage *message
         m_processor->AddLog(CEC_LOG_DEBUG, msg.ToString());
         if (iPacketsLeft > 0)
           iPacketsLeft--;
+        if (!message->isTransmission && iPacketsLeft == 0)
+          bTransmitSucceeded = true;
         break;
       case MSGCODE_TRANSMIT_SUCCEEDED:
         m_processor->AddLog(CEC_LOG_DEBUG, msg.ToString());
index 51734e16e523e9ef2dee562ed4b88f132284cc58..bdc3de30f3f543e8bfd30d46528ef1a1d01e973f 100644 (file)
@@ -62,6 +62,7 @@ namespace CEC
 
     bool SetLineTimeout(uint8_t iTimeout);
     bool StartBootloader(void);
+    bool SetAckMask(uint16_t iMask);
 
     bool WaitForTransmitSucceeded(CCECAdapterMessage *message);
 
index 4fadbed97cb19198312bc894702ff35a96f0a725..26acd90d2f5d03e4079a98c52f5b01709f890704 100644 (file)
@@ -36,9 +36,11 @@ namespace CEC
   typedef enum cec_adapter_message_state
   {
     ADAPTER_MESSAGE_STATE_UNKNOWN = 0,
-    ADAPTER_MESSAGE_STATE_WAITING,
+    ADAPTER_MESSAGE_STATE_WAITING_TO_BE_SENT,
     ADAPTER_MESSAGE_STATE_SENT,
-    ADAPTER_MESSAGE_STATE_RECEIVED,
+    ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED,
+    ADAPTER_MESSAGE_STATE_SENT_ACKED,
+    ADAPTER_MESSAGE_STATE_INCOMING,
     ADAPTER_MESSAGE_STATE_ERROR
   } cec_adapter_message_state;
 
@@ -97,6 +99,7 @@ namespace CEC
 
       // set timeout
       transmit_timeout = command.transmit_timeout;
+      //TODO
     }
 
     CCECAdapterMessage &operator=(const CCECAdapterMessage &msg)
@@ -258,6 +261,7 @@ namespace CEC
       maxTries = CEC_DEFAULT_TRANSMIT_RETRIES + 1;
       tries = 0;
       reply = MSGCODE_NOTHING;
+      isTransmission = true;
     }
 
     void Shift(uint8_t iShiftBy)
@@ -349,6 +353,7 @@ namespace CEC
     cec_datapacket            packet;
     cec_adapter_message_state state;
     int32_t                   transmit_timeout;
+    bool                      isTransmission;
     PLATFORM::CMutex          mutex;
     PLATFORM::CCondition      condition;
   };