From: Lars Op den Kamp Date: Tue, 24 Jan 2012 10:40:42 +0000 (+0100) Subject: cec: moved WaitForTransmitSucceeded() to CAdapterCommunication X-Git-Tag: upstream/2.2.0~1^2~39^2~18 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=6729ac71558216a4679b507e76acca467a2c31d8;p=deb_libcec.git cec: moved WaitForTransmitSucceeded() to CAdapterCommunication --- diff --git a/src/lib/CECProcessor.cpp b/src/lib/CECProcessor.cpp index dd47c0d..3a5742b 100644 --- a/src/lib/CECProcessor.cpp +++ b/src/lib/CECProcessor.cpp @@ -885,7 +885,7 @@ bool CCECProcessor::Transmit(CCECAdapterMessage *output) if (output->transmit_timeout > 0) { - if ((bReturn = WaitForTransmitSucceeded(output)) == false) + if ((bReturn = m_communication->WaitForTransmitSucceeded(output)) == false) m_controller->AddLog(CEC_LOG_DEBUG, "did not receive ack"); } else @@ -911,74 +911,6 @@ void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode Transmit(command); } -bool CCECProcessor::WaitForTransmitSucceeded(CCECAdapterMessage *message) -{ - bool bError(false); - bool bTransmitSucceeded(false); - uint8_t iPacketsLeft(message->Size() / 4); - - int64_t iNow = GetTimeMs(); - int64_t iTargetTime = iNow + message->transmit_timeout; - - while (!bTransmitSucceeded && !bError && (message->transmit_timeout == 0 || iNow < iTargetTime)) - { - CCECAdapterMessage msg; - - if (!m_communication->Read(msg, message->transmit_timeout > 0 ? (int32_t)(iTargetTime - iNow) : 1000)) - { - iNow = GetTimeMs(); - continue; - } - - if (msg.Message() == MSGCODE_FRAME_START && msg.IsACK()) - { - m_busDevices[msg.Initiator()]->GetHandler()->HandlePoll(msg.Initiator(), msg.Destination()); - m_lastInitiator = msg.Initiator(); - iNow = GetTimeMs(); - continue; - } - - bError = msg.IsError(); - if (msg.Message() == MSGCODE_RECEIVE_FAILED && - m_lastInitiator != CECDEVICE_UNKNOWN && - !m_busDevices[m_lastInitiator]->GetHandler()->HandleReceiveFailed()) - { - iNow = GetTimeMs(); - continue; - } - - if (bError) - { - message->reply = msg.Message(); - m_controller->AddLog(CEC_LOG_DEBUG, msg.ToString()); - } - else - { - switch(msg.Message()) - { - case MSGCODE_COMMAND_ACCEPTED: - m_controller->AddLog(CEC_LOG_DEBUG, msg.ToString()); - if (iPacketsLeft > 0) - iPacketsLeft--; - break; - case MSGCODE_TRANSMIT_SUCCEEDED: - m_controller->AddLog(CEC_LOG_DEBUG, msg.ToString()); - bTransmitSucceeded = (iPacketsLeft == 0); - bError = !bTransmitSucceeded; - message->reply = MSGCODE_TRANSMIT_SUCCEEDED; - break; - default: - // ignore other data while waiting - break; - } - - iNow = GetTimeMs(); - } - } - - return bTransmitSucceeded && !bError; -} - bool CCECProcessor::ParseMessage(const CCECAdapterMessage &msg) { bool bEom(false); @@ -1533,3 +1465,15 @@ bool CCECProcessor::PingAdapter(void) { return m_communication->PingAdapter(); } + +void CCECProcessor::HandlePoll(cec_logical_address initiator, cec_logical_address destination) +{ + m_busDevices[initiator]->GetHandler()->HandlePoll(initiator, destination); + m_lastInitiator = initiator; +} + +bool CCECProcessor::HandleReceiveFailed(void) +{ + return m_lastInitiator != CECDEVICE_UNKNOWN && + !m_busDevices[m_lastInitiator]->GetHandler()->HandleReceiveFailed(); +} diff --git a/src/lib/CECProcessor.h b/src/lib/CECProcessor.h index 55286f8..82ea16f 100644 --- a/src/lib/CECProcessor.h +++ b/src/lib/CECProcessor.h @@ -129,6 +129,8 @@ namespace CEC virtual bool StartBootloader(void); virtual bool PingAdapter(void); + virtual void HandlePoll(cec_logical_address initiator, cec_logical_address destination); + virtual bool HandleReceiveFailed(void); CCECBusDevice * m_busDevices[16]; PLATFORM::CMutex m_transmitMutex; @@ -148,7 +150,6 @@ namespace CEC bool FindLogicalAddressAudioSystem(void); void LogOutput(const cec_command &data); - bool WaitForTransmitSucceeded(CCECAdapterMessage *message); bool ParseMessage(const CCECAdapterMessage &msg); void ParseCommand(cec_command &command); diff --git a/src/lib/adapter/AdapterCommunication.cpp b/src/lib/adapter/AdapterCommunication.cpp index 4a136ab..a792a1d 100644 --- a/src/lib/adapter/AdapterCommunication.cpp +++ b/src/lib/adapter/AdapterCommunication.cpp @@ -275,6 +275,72 @@ bool CAdapterCommunication::IsOpen(void) return !IsStopped() && m_port->IsOpen() && IsRunning(); } +bool CAdapterCommunication::WaitForTransmitSucceeded(CCECAdapterMessage *message) +{ + bool bError(false); + bool bTransmitSucceeded(false); + uint8_t iPacketsLeft(message->Size() / 4); + + int64_t iNow = GetTimeMs(); + int64_t iTargetTime = iNow + message->transmit_timeout; + + while (!bTransmitSucceeded && !bError && (message->transmit_timeout == 0 || iNow < iTargetTime)) + { + CCECAdapterMessage msg; + + if (!Read(msg, message->transmit_timeout > 0 ? (int32_t)(iTargetTime - iNow) : 1000)) + { + iNow = GetTimeMs(); + continue; + } + + if (msg.Message() == MSGCODE_FRAME_START && msg.IsACK()) + { + m_processor->HandlePoll(msg.Initiator(), msg.Destination()); + iNow = GetTimeMs(); + continue; + } + + if (msg.Message() == MSGCODE_RECEIVE_FAILED && + m_processor->HandleReceiveFailed()) + { + iNow = GetTimeMs(); + continue; + } + + bError = msg.IsError(); + if (bError) + { + message->reply = msg.Message(); + m_processor->AddLog(CEC_LOG_DEBUG, msg.ToString()); + } + else + { + switch(msg.Message()) + { + case MSGCODE_COMMAND_ACCEPTED: + m_processor->AddLog(CEC_LOG_DEBUG, msg.ToString()); + if (iPacketsLeft > 0) + iPacketsLeft--; + break; + case MSGCODE_TRANSMIT_SUCCEEDED: + m_processor->AddLog(CEC_LOG_DEBUG, msg.ToString()); + bTransmitSucceeded = (iPacketsLeft == 0); + bError = !bTransmitSucceeded; + message->reply = MSGCODE_TRANSMIT_SUCCEEDED; + break; + default: + // ignore other data while waiting + break; + } + + iNow = GetTimeMs(); + } + } + + return bTransmitSucceeded && !bError; +} + void CAdapterCommunication::AddData(uint8_t *data, uint8_t iLen) { CLockObject lock(m_mutex); diff --git a/src/lib/adapter/AdapterCommunication.h b/src/lib/adapter/AdapterCommunication.h index 4367e40..51734e1 100644 --- a/src/lib/adapter/AdapterCommunication.h +++ b/src/lib/adapter/AdapterCommunication.h @@ -63,6 +63,8 @@ namespace CEC bool SetLineTimeout(uint8_t iTimeout); bool StartBootloader(void); + bool WaitForTransmitSucceeded(CCECAdapterMessage *message); + private: void SendMessageToAdapter(CCECAdapterMessage *msg); void WriteNextCommand(void);