cec: retry to get the firmware version when it failed. wait for active tranmission...
[deb_libcec.git] / src / lib / CECProcessor.cpp
index 7ece12ae1412293297f3ba12859a5e38afab94a0..12cea4f8ecc80186bf141dfd40e49dbf510a9a80 100644 (file)
@@ -150,16 +150,40 @@ bool CCECProcessor::OpenConnection(const char *strPort, uint16_t iBaudRate, uint
     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");
+  int iPingTry(0);
+  bool bPingOk(false);
+  while (!bPingOk && iPingTry++ < CEC_PING_ADAPTER_TRIES)
+  {
+    if ((bPingOk = m_communication->PingAdapter()) == false)
+    {
+      CLibCEC::AddLog(CEC_LOG_ERROR, "the adapter did not respond correctly to a ping (try %d of %d)", iPingTry, CEC_PING_ADAPTER_TRIES);
+      Sleep(500);
+    }
+  }
 
-  if (bReturn)
+  if (bPingOk)
   {
-    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");
+    uint16_t iFirmwareVersion(CEC_FW_VERSION_UNKNOWN);
+    int iFwVersionTry(0);
+    bool bFwVersionOk(false);
+    while (!bFwVersionOk && iFwVersionTry++ < CEC_FW_VERSION_TRIES)
+    {
+      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 of %d)", iFwVersionTry, CEC_FW_VERSION_TRIES);
+        Sleep(500);
+      }
+    }
 
-    CLibCEC::AddLog(CEC_LOG_NOTICE, "CEC Adapter firmware version: %d", iFirmwareVersion);
+    if (iFirmwareVersion == CEC_FW_VERSION_UNKNOWN)
+    {
+      bReturn = false;
+    }
+    else
+    {
+      bReturn = true;
+      CLibCEC::AddLog(CEC_LOG_NOTICE, "CEC Adapter firmware version: %d", iFirmwareVersion);
+    }
   }
 
   return bReturn;