#include "devices/CECTV.h"
#include "implementations/CECCommandHandler.h"
#include "LibCEC.h"
+#include "platform/util/timeutils.h"
using namespace CEC;
using namespace std;
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 */
- int iPingTry(0);
- bool bPingOk(false);
- while (!bPingOk && iPingTry++ < CEC_PING_ADAPTER_TRIES)
+ while (bConnected && iNow < iTarget && (bPinged = m_communication->PingAdapter()) == false)
{
- 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);
- }
+ CLibCEC::AddLog(CEC_LOG_ERROR, "the adapter did not respond correctly to a ping (try %d)", ++iPingTry);
+ Sleep(500);
+ iNow = GetTimeMs();
}
- if (bPingOk)
+ /* 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)
{
- 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);
- }
- }
-
- if (iFirmwareVersion == CEC_FW_VERSION_UNKNOWN)
- {
- bReturn = false;
- }
- else
- {
- bReturn = true;
- CLibCEC::AddLog(CEC_LOG_NOTICE, "CEC Adapter firmware version: %d", iFirmwareVersion);
- }
+ CLibCEC::AddLog(CEC_LOG_ERROR, "the adapter did not respond with a correct firmware version (try %d)", ++iFwVersionTry);
+ Sleep(500);
+ iNow = GetTimeMs();
}
+ if ((bReturn = iFirmwareVersion != CEC_FW_VERSION_UNKNOWN) == true)
+ CLibCEC::AddLog(CEC_LOG_NOTICE, "connected to the CEC adapter. firmware version = %d", iFirmwareVersion);
+
return bReturn;
}