X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Flib%2FCECParser.cpp;h=7a6d871700a7d701ec0eccf572a9c62d3e883afa;hb=5f39c4d854ec7441761bc6db870b6bbc73016309;hp=5ac1d8ea76c0278a24fdccee6d380728aa5790fc;hpb=a8f0bd18be9ae8f70822b0b22038f40d12b4fcad;p=deb_libcec.git diff --git a/src/lib/CECParser.cpp b/src/lib/CECParser.cpp index 5ac1d8e..7a6d871 100644 --- a/src/lib/CECParser.cpp +++ b/src/lib/CECParser.cpp @@ -48,6 +48,7 @@ using namespace CEC; using namespace std; #define CEC_MAX_RETRY 5 +#define CEC_BUTTON_TIMEOUT 500 /*! * ICECDevice implementation @@ -65,7 +66,7 @@ CCECParser::CCECParser(const char *strDeviceName, cec_logical_address iLogicalAd CCECParser::~CCECParser(void) { - Close(0); + Close(); m_communication->Close(); delete m_communication; } @@ -109,22 +110,10 @@ bool CCECParser::Open(const char *strPort, int iTimeoutMs /* = 10000 */) return false; } -bool CCECParser::Close(int iTimeoutMs /* = 2000 */) +void CCECParser::Close(void) { m_bRunning = false; - bool bExit(false); - if (iTimeoutMs > 0) - { - bExit = m_exitCondition.Wait(&m_mutex, iTimeoutMs); - m_mutex.Unlock(); - } - else - { - pthread_join(m_thread, NULL); - bExit = true; - } - - return bExit; + pthread_join(m_thread, NULL); } void *CCECParser::ThreadHandler(CCECParser *parser) @@ -140,7 +129,7 @@ bool CCECParser::Process(void) while (m_bRunning) { cec_frame msg; - while (m_bRunning && m_communication->IsOpen() && m_communication->Read(msg, 500)) + while (m_bRunning && m_communication->IsOpen() && m_communication->Read(msg, CEC_BUTTON_TIMEOUT)) ParseMessage(msg); now = GetTimeMs(); @@ -165,7 +154,7 @@ bool CCECParser::Ping(void) PushEscaped(output, MSGCODE_PING); output.push_back(MSGEND); - if (!TransmitFormatted(output, false, (int64_t) 5000)) + if (!TransmitFormatted(output, false)) { AddLog(CEC_LOG_ERROR, "could not send ping command"); return false; @@ -188,7 +177,7 @@ bool CCECParser::StartBootloader(void) PushEscaped(output, MSGCODE_START_BOOTLOADER); output.push_back(MSGEND); - if (!TransmitFormatted(output, false, (int64_t) 5000)) + if (!TransmitFormatted(output, false)) { AddLog(CEC_LOG_ERROR, "could not start the bootloader"); return false; @@ -376,9 +365,9 @@ void CCECParser::BroadcastActiveSource(void) Transmit(frame); } -bool CCECParser::TransmitFormatted(const cec_frame &data, bool bWaitForAck /* = true */, int64_t iTimeout /* = 2000 */) +bool CCECParser::TransmitFormatted(const cec_frame &data, bool bWaitForAck /* = true */) { - if (!m_communication || m_communication->Write(data) != data.size()) + if (!m_communication || !m_communication->Write(data)) return false; CCondition::Sleep((int) data.size() * 24 /*data*/ + 5 /*start bit (4.5 ms)*/ + 50 /* to be on the safe side */); @@ -391,7 +380,7 @@ bool CCECParser::TransmitFormatted(const cec_frame &data, bool bWaitForAck /* = return true; } -bool CCECParser::Transmit(const cec_frame &data, bool bWaitForAck /* = true */, int64_t iTimeout /* = 5000 */) +bool CCECParser::Transmit(const cec_frame &data, bool bWaitForAck /* = true */) { CStdString txStr = "transmit "; for (unsigned int i = 0; i < data.size(); i++) @@ -432,16 +421,16 @@ bool CCECParser::Transmit(const cec_frame &data, bool bWaitForAck /* = true */, output.push_back(MSGEND); } - return TransmitFormatted(output, bWaitForAck, iTimeout); + return TransmitFormatted(output, bWaitForAck); } -bool CCECParser::WaitForAck(int64_t iTimeout /* = 1000 */) +bool CCECParser::WaitForAck(int iTimeout /* = 1000 */) { bool bGotAck(false); bool bError(false); int64_t iNow = GetTimeMs(); - int64_t iTargetTime = iNow + iTimeout; + int64_t iTargetTime = iNow + (int64_t) iTimeout; while (!bGotAck && !bError && (iTimeout <= 0 || iNow < iTargetTime)) { @@ -677,7 +666,7 @@ void CCECParser::PushEscaped(cec_frame &vec, uint8_t byte) void CCECParser::CheckKeypressTimeout(int64_t now) { - if (m_iCurrentButton != CEC_USER_CONTROL_CODE_UNKNOWN && now - m_buttontime > 500) + if (m_iCurrentButton != CEC_USER_CONTROL_CODE_UNKNOWN && now - m_buttontime > CEC_BUTTON_TIMEOUT) { AddKey(); m_iCurrentButton = CEC_USER_CONTROL_CODE_UNKNOWN; @@ -708,7 +697,7 @@ bool CCECParser::SetAckMask(uint16_t iMask) PushEscaped(output, (uint8_t)iMask); output.push_back(MSGEND); - if (m_communication->Write(output) == -1) + if (!m_communication->Write(output)) { AddLog(CEC_LOG_ERROR, "could not set the ackmask"); return false;