cec: handle bus scan from LG TVs
[deb_libcec.git] / src / lib / AdapterCommunication.cpp
index 74ea9e3f33826363d4ab34e6caf50ff99a0ae2d9..32ceeafc02e5835be628af9a2d8e140056c838b0 100644 (file)
@@ -215,8 +215,7 @@ CStdString CCECAdapterMessage::ToString(void) const
 bool CCECAdapterMessage::is_error(void) const
 {
   cec_adapter_messagecode code = message();
-  return (code == MSGCODE_TIMEOUT_ERROR ||
-    code == MSGCODE_HIGH_ERROR ||
+  return (code == MSGCODE_HIGH_ERROR ||
     code == MSGCODE_LOW_ERROR ||
     code == MSGCODE_RECEIVE_FAILED ||
     code == MSGCODE_COMMAND_REJECTED ||
@@ -229,7 +228,7 @@ bool CCECAdapterMessage::is_error(void) const
 
 void CCECAdapterMessage::push_escaped(uint8_t byte)
 {
-  if (byte >= MSGESC && byte != MSGSTART)
+  if (byte >= MSGESC)
   {
     push_back(MSGESC);
     push_back(byte - ESCOFFSET);
@@ -357,23 +356,26 @@ void CAdapterCommunication::WriteNextCommand(void)
 {
   CCECAdapterMessage *msg;
   if (m_outBuffer.Pop(msg))
+    SendMessageToAdapter(msg);
+}
+
+void CAdapterCommunication::SendMessageToAdapter(CCECAdapterMessage *msg)
+{
+  CLockObject lock(&msg->mutex);
+  if (m_port->Write(msg) != (int32_t) msg->size())
   {
-    CLockObject lock(&msg->mutex);
-    if (m_port->Write(msg) != (int32_t) msg->size())
-    {
-      CStdString strError;
-      strError.Format("error writing to serial port: %s", m_port->GetError().c_str());
-      m_controller->AddLog(CEC_LOG_ERROR, strError);
-      msg->state = ADAPTER_MESSAGE_STATE_ERROR;
-    }
-    else
-    {
-      m_controller->AddLog(CEC_LOG_DEBUG, "command sent");
-      CCondition::Sleep((uint32_t) msg->size() * 24 /*data*/ + 5 /*start bit (4.5 ms)*/ + 10);
-      msg->state = ADAPTER_MESSAGE_STATE_SENT;
-    }
-    msg->condition.Signal();
+    CStdString strError;
+    strError.Format("error writing to serial port: %s", m_port->GetError().c_str());
+    m_controller->AddLog(CEC_LOG_ERROR, strError);
+    msg->state = ADAPTER_MESSAGE_STATE_ERROR;
   }
+  else
+  {
+    m_controller->AddLog(CEC_LOG_DEBUG, "command sent");
+    CCondition::Sleep((uint32_t) msg->size() * 24 /*data*/ + 5 /*start bit (4.5 ms)*/);
+    msg->state = ADAPTER_MESSAGE_STATE_SENT;
+  }
+  msg->condition.Signal();
 }
 
 bool CAdapterCommunication::Write(CCECAdapterMessage *data)
@@ -411,7 +413,8 @@ bool CAdapterCommunication::Read(CCECAdapterMessage &msg, uint32_t iTimeout)
     }
     else if (buf == MSGSTART) //we found a msgstart before msgend, this is not right, remove
     {
-      m_controller->AddLog(CEC_LOG_ERROR, "received MSGSTART before MSGEND");
+      if (msg.size() > 0)
+        m_controller->AddLog(CEC_LOG_WARNING, "received MSGSTART before MSGEND, removing previous buffer contents");
       msg.clear();
       bGotStart = true;
     }
@@ -455,9 +458,8 @@ bool CAdapterCommunication::StartBootloader(void)
   output->push_escaped(MSGCODE_START_BOOTLOADER);
   output->push_back(MSGEND);
 
-  if ((bReturn = Write(output)) == false)
-    m_controller->AddLog(CEC_LOG_ERROR, "could not start the bootloader");
-
+  SendMessageToAdapter(output);
+  bReturn = output->state == ADAPTER_MESSAGE_STATE_SENT;
   delete output;
 
   return bReturn;
@@ -476,10 +478,8 @@ bool CAdapterCommunication::PingAdapter(void)
   output->push_escaped(MSGCODE_PING);
   output->push_back(MSGEND);
 
-  if ((bReturn = Write(output)) == false)
-    m_controller->AddLog(CEC_LOG_ERROR, "could not send ping command");
-
-  // TODO check for pong
+  SendMessageToAdapter(output);
+  bReturn = output->state == ADAPTER_MESSAGE_STATE_SENT;
   delete output;
 
   return bReturn;