From 0e31a62c1ff3854f1c4dad3fb0917683c9528e5b Mon Sep 17 00:00:00 2001 From: Lars Op den Kamp Date: Sun, 30 Oct 2011 10:34:36 +0100 Subject: [PATCH] cec: set a message state in CCECAdapterMessage and check this state after sending a command, so we catch failed writes again --- src/lib/AdapterCommunication.cpp | 10 ++++++++-- src/lib/AdapterCommunication.h | 21 ++++++++++++++++----- src/lib/CECProcessor.cpp | 7 +++++++ 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/lib/AdapterCommunication.cpp b/src/lib/AdapterCommunication.cpp index 4845f57..3b0ca01 100644 --- a/src/lib/AdapterCommunication.cpp +++ b/src/lib/AdapterCommunication.cpp @@ -216,16 +216,18 @@ void CAdapterCommunication::WriteNextCommand(void) if (m_outBuffer.Pop(msg)) { CLockObject lock(&msg->mutex); - if (m_port->Write(msg) != (int32_t) msg.get()->size()) + if (m_port->Write(msg) != (int32_t) msg->size()) { CStdString strError; strError.Format("error writing to serial port: %s", m_port->GetError().c_str()); m_controller->AddLog(CEC_LOG_ERROR, strError); + msg->state = ADAPTER_MESSAGE_STATE_ERROR; } else { m_controller->AddLog(CEC_LOG_DEBUG, "command sent"); - CCondition::Sleep((uint32_t) msg.get()->size() * (uint32_t)24 /*data*/ + (uint32_t)5 /*start bit (4.5 ms)*/); + CCondition::Sleep((uint32_t) msg->size() * (uint32_t)24 /*data*/ + (uint32_t)5 /*start bit (4.5 ms)*/); + msg->state = ADAPTER_MESSAGE_STATE_SENT; } msg->condition.Signal(); } @@ -233,6 +235,7 @@ void CAdapterCommunication::WriteNextCommand(void) bool CAdapterCommunication::Write(CCECAdapterMessagePtr data) { + data->state = ADAPTER_MESSAGE_STATE_WAITING; m_outBuffer.Push(data); return true; } @@ -285,6 +288,9 @@ bool CAdapterCommunication::Read(CCECAdapterMessage &msg, uint32_t iTimeout) msg.push_back(buf); } + if (bGotFullMessage) + msg.state = ADAPTER_MESSAGE_STATE_RECEIVED; + return bGotFullMessage; } diff --git a/src/lib/AdapterCommunication.h b/src/lib/AdapterCommunication.h index a82f32c..9a3d126 100644 --- a/src/lib/AdapterCommunication.h +++ b/src/lib/AdapterCommunication.h @@ -40,17 +40,27 @@ namespace CEC { + typedef enum cec_adapter_message_state + { + ADAPTER_MESSAGE_STATE_UNKNOWN = 0, + ADAPTER_MESSAGE_STATE_WAITING, + ADAPTER_MESSAGE_STATE_SENT, + ADAPTER_MESSAGE_STATE_RECEIVED, + ADAPTER_MESSAGE_STATE_ERROR + } cec_adapter_message_state; + + class CCECAdapterMessage : public boost::enable_shared_from_this { public: - CCECAdapterMessage(void) {} + CCECAdapterMessage(void) { clear(); } CCECAdapterMessage(const cec_command &command); bool empty(void) const { return packet.empty(); } uint8_t operator[](uint8_t pos) const { return packet[pos]; } uint8_t at(uint8_t pos) const { return packet[pos]; } uint8_t size(void) const { return packet.size; } - void clear(void) { packet.clear(); } + void clear(void) { state = ADAPTER_MESSAGE_STATE_UNKNOWN; packet.clear(); } void shift(uint8_t iShiftBy) { packet.shift(iShiftBy); } void push_back(uint8_t add) { packet.push_back(add); } cec_adapter_messagecode message(void) const { return packet.size >= 1 ? (cec_adapter_messagecode) (packet.at(0) & ~(MSGCODE_FRAME_EOM | MSGCODE_FRAME_ACK)) : MSGCODE_NOTHING; } @@ -60,9 +70,10 @@ namespace CEC cec_logical_address destination(void) const { return packet.size >= 2 ? (cec_logical_address) (packet.at(1) & 0xF) : CECDEVICE_UNKNOWN; }; void push_escaped(int16_t byte); - cec_datapacket packet; - CMutex mutex; - CCondition condition; + cec_datapacket packet; + cec_adapter_message_state state; + CMutex mutex; + CCondition condition; private: CCECAdapterMessage &operator =(const CCECAdapterMessage &msg); diff --git a/src/lib/CECProcessor.cpp b/src/lib/CECProcessor.cpp index 350958f..1bc4c59 100644 --- a/src/lib/CECProcessor.cpp +++ b/src/lib/CECProcessor.cpp @@ -200,7 +200,14 @@ bool CCECProcessor::Transmit(const cec_command &data, bool bWaitForAck /* = true if (!m_communication || !m_communication->Write(output)) return bReturn; else + { output->condition.Wait(&output->mutex); + if (output->state != ADAPTER_MESSAGE_STATE_SENT) + { + m_controller->AddLog(CEC_LOG_ERROR, "command was not sent"); + return bReturn; + } + } } if (bWaitForAck) -- 2.34.1