X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Flib%2FCECProcessor.cpp;h=0ad84d8168d86b8151654b696a1476798424459c;hb=cb2397e953b3f892b4206d5bb87968ca4cccbac8;hp=8e1c1017ff722e434447ff8a8247602530cf0b98;hpb=c71891a335d74d5f31dc96c2f5853384167a081b;p=deb_libcec.git diff --git a/src/lib/CECProcessor.cpp b/src/lib/CECProcessor.cpp index 8e1c101..0ad84d8 100644 --- a/src/lib/CECProcessor.cpp +++ b/src/lib/CECProcessor.cpp @@ -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,31 +146,39 @@ 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 */ - 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 = 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"); - - 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; }