posix: fixed lib/platform/sockets/tcp.h compilation and some bugs. added CTcpSocket...
[deb_libcec.git] / src / lib / CECProcessor.cpp
index 7a4e019d0b7fdfb2a48968f9ff185c54c82ce2f0..0ad84d8168d86b8151654b696a1476798424459c 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the libCEC(R) library.
  *
- * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited.  All rights reserved.
+ * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited.  All rights reserved.
  * libCEC(R) is an original work, containing original code.
  *
  * libCEC(R) is a trademark of Pulse-Eight Limited.
@@ -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,38 @@ 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;
+  unsigned iConnectTry(0), iPingTry(0), iFwVersionTry(0);
+  bool bConnected(false), bPinged(false);
+
   /* open a new connection */
-  if ((bReturn = m_communication->Open(strPort, iBaudRate, iTimeoutMs)) == false)
-    CLibCEC::AddLog(CEC_LOG_ERROR, "could not open a connection");
+  while (iNow < iTarget && (bConnected = m_communication->Open(strPort, iBaudRate, iTimeoutMs)) == false)
+  {
+    CLibCEC::AddLog(CEC_LOG_ERROR, "could not open a connection (try %d)", ++iConnectTry);
+    Sleep(500);
+    iNow = GetTimeMs();
+  }
 
   /* try to ping the adapter */
-  if ((bReturn = m_communication->PingAdapter()) == false)
-    CLibCEC::AddLog(CEC_LOG_ERROR, "the adapter does not respond correctly");
+  while (bConnected && iNow < iTarget && (bPinged = m_communication->PingAdapter()) == false)
+  {
+    CLibCEC::AddLog(CEC_LOG_ERROR, "the adapter did not respond correctly to a ping (try %d)", ++iPingTry);
+    Sleep(500);
+    iNow = GetTimeMs();
+  }
 
-  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");
+  /* try to read the firmware version */
+  uint16_t iFirmwareVersion(CEC_FW_VERSION_UNKNOWN);
+  while (bPinged && iNow < iTarget && (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);
+    iNow = GetTimeMs();
+  }
 
-  CLibCEC::AddLog(CEC_LOG_NOTICE, "CEC Adapter firmware version: %d", iFirmwareVersion);
+  if ((bReturn = iFirmwareVersion != CEC_FW_VERSION_UNKNOWN) == true)
+    CLibCEC::AddLog(CEC_LOG_NOTICE, "connected to the CEC adapter. firmware version = %d", iFirmwareVersion);
 
   return bReturn;
 }
@@ -988,26 +1008,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);