From: Lars Op den Kamp Date: Wed, 25 Jan 2012 19:47:46 +0000 (+0100) Subject: cec: retry to get the firmware version when it failed. wait for active tranmission... X-Git-Tag: upstream/2.2.0~1^2~39^2~1 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=2c7804018856ae3cbfc937b7c9b4cf9b00bc0e8d;p=deb_libcec.git cec: retry to get the firmware version when it failed. wait for active tranmission to finish when opening a connection to the adapter. fixes initialisation errors --- diff --git a/include/cectypes.h b/include/cectypes.h index e602ac9..5213b37 100644 --- a/include/cectypes.h +++ b/include/cectypes.h @@ -77,6 +77,7 @@ namespace CEC { #define CEC_DEFAULT_TRANSMIT_WAIT 2000 #define CEC_DEFAULT_TRANSMIT_RETRIES 1 #define CEC_PING_ADAPTER_TRIES 5 +#define CEC_FW_VERSION_TRIES 5 #define CEC_MIN_LIB_VERSION 1 #define CEC_LIB_VERSION_MAJOR 1 diff --git a/src/lib/CECProcessor.cpp b/src/lib/CECProcessor.cpp index 8e1c101..12cea4f 100644 --- a/src/lib/CECProcessor.cpp +++ b/src/lib/CECProcessor.cpp @@ -163,11 +163,27 @@ bool CCECProcessor::OpenConnection(const char *strPort, uint16_t iBaudRate, uint 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; diff --git a/src/lib/adapter/AdapterCommunication.cpp b/src/lib/adapter/AdapterCommunication.cpp index 05ab9b8..2bbcb01 100644 --- a/src/lib/adapter/AdapterCommunication.cpp +++ b/src/lib/adapter/AdapterCommunication.cpp @@ -98,11 +98,15 @@ bool CAdapterCommunication::Open(const char *strPort, uint16_t iBaudRate /* = 38 return false; } - CLibCEC::AddLog(CEC_LOG_DEBUG, "connection opened"); + CLibCEC::AddLog(CEC_LOG_DEBUG, "connection opened, clearing any previous input and waiting for active transmissions to end before starting"); //clear any input bytes - uint8_t buff[1]; - while (m_port->Read(buff, 1, 5) == 1) {} + uint8_t buff[1024]; + while (m_port->Read(buff, 1024, 100) > 0) + { + CLibCEC::AddLog(CEC_LOG_DEBUG, "data received, clearing it"); + Sleep(250); + } if (CreateThread()) { @@ -300,7 +304,7 @@ uint16_t CAdapterCommunication::GetFirmwareVersion(void) CCECAdapterMessage input; if (!Read(input, CEC_DEFAULT_TRANSMIT_WAIT) || input.Message() != MSGCODE_FIRMWARE_VERSION || input.Size() != 3) - CLibCEC::AddLog(CEC_LOG_ERROR, "no or invalid firmware version"); + CLibCEC::AddLog(CEC_LOG_ERROR, "no or invalid firmware version (size = %d, message = %d)", input.Size(), input.Message()); else { m_iFirmwareVersion = (input[1] << 8 | input[2]);