cec: transmit 'menu state activated' after the stream path has been set to a device...
[deb_libcec.git] / src / lib / CECProcessor.cpp
index 7a4e019d0b7fdfb2a48968f9ff185c54c82ce2f0..6a8afd9009aee7e5b677bc74aeb93f88592b73fd 100644 (file)
@@ -41,6 +41,7 @@
 #include "devices/CECTV.h"
 #include "implementations/CECCommandHandler.h"
 #include "LibCEC.h"
+#include "platform/util/timeutils.h"
 
 using namespace CEC;
 using namespace std;
@@ -145,19 +146,44 @@ bool CCECProcessor::OpenConnection(const char *strPort, uint16_t iBaudRate, uint
     return bReturn;
   }
 
+  uint64_t iNow = GetTimeMs();
+  uint64_t iTarget = iTimeoutMs > 0 ? iNow + iTimeoutMs : iNow + CEC_DEFAULT_TRANSMIT_WAIT;
+
   /* open a new connection */
   if ((bReturn = m_communication->Open(strPort, iBaudRate, iTimeoutMs)) == false)
     CLibCEC::AddLog(CEC_LOG_ERROR, "could not open a connection");
 
   /* try to ping the adapter */
-  if ((bReturn = m_communication->PingAdapter()) == false)
-    CLibCEC::AddLog(CEC_LOG_ERROR, "the adapter does not respond correctly");
+  bool bPingOk(false);
+  unsigned iPingTry(0), iFwVersionTry(0);
+  uint16_t iFirmwareVersion(CEC_FW_VERSION_UNKNOWN);
+  while ((!bPingOk || iFirmwareVersion == CEC_FW_VERSION_UNKNOWN) && iTarget > iNow)
+  {
+    if (!bPingOk)
+    {
+      if ((bPingOk = m_communication->PingAdapter()) == false)
+      {
+        CLibCEC::AddLog(CEC_LOG_ERROR, "the adapter did not respond correctly to a ping (try %d)", ++iPingTry);
+        Sleep(500);
+      }
+    }
 
-  uint16_t iFirmwareVersion = m_communication->GetFirmwareVersion();
-  if ((bReturn = (iFirmwareVersion != CEC_FW_VERSION_UNKNOWN)) == false)
-    m_controller->AddLog(CEC_LOG_ERROR, "the adapter is running an unknown firmware version");
+    if (bPingOk)
+    {
+      if ((iFirmwareVersion = m_communication->GetFirmwareVersion()) == CEC_FW_VERSION_UNKNOWN)
+      {
+        CLibCEC::AddLog(CEC_LOG_ERROR, "the adapter did not respond with a correct firmware version (try %d)", ++iFwVersionTry);
+        Sleep(500);
+      }
+      else
+      {
+        CLibCEC::AddLog(CEC_LOG_NOTICE, "CEC Adapter firmware version: %d", iFirmwareVersion);
+        bReturn = true;
+      }
+    }
 
-  CLibCEC::AddLog(CEC_LOG_NOTICE, "CEC Adapter firmware version: %d", iFirmwareVersion);
+    iNow = GetTimeMs();
+  }
 
   return bReturn;
 }
@@ -988,26 +1014,6 @@ uint16_t CCECProcessor::GetPhysicalAddress(void) const
   return false;
 }
 
-void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode)
-{
-  m_controller->SetCurrentButton(iButtonCode);
-}
-
-void CCECProcessor::AddCommand(const cec_command &command)
-{
-  m_controller->AddCommand(command);
-}
-
-void CCECProcessor::AddKey(cec_keypress &key)
-{
-  m_controller->AddKey(key);
-}
-
-void CCECProcessor::AddKey(void)
-{
-  m_controller->AddKey();
-}
-
 bool CCECProcessor::SetAckMask(uint16_t iMask)
 {
   return m_communication->SetAckMask(iMask);