From 3c53ac93d0c95ebf700c08f9ccfb2810dfeea8b1 Mon Sep 17 00:00:00 2001 From: Lars Op den Kamp Date: Sat, 29 Oct 2011 16:54:27 +0200 Subject: [PATCH] cec: make all reads and write in CAdapterCommunication go through buffers, so we don't have to block and keep locks. proper handling of failed writes will have to be re-added --- src/lib/AdapterCommunication.cpp | 38 ++++++++++++++++++-------------- src/lib/AdapterCommunication.h | 14 +++++++----- 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/src/lib/AdapterCommunication.cpp b/src/lib/AdapterCommunication.cpp index 50d1c88..2e330d3 100644 --- a/src/lib/AdapterCommunication.cpp +++ b/src/lib/AdapterCommunication.cpp @@ -113,13 +113,9 @@ void *CAdapterCommunication::Process(void) while (!IsStopped()) { - { - CLockObject lock(&m_commMutex); - ReadFromDevice(100); - } - - if (!IsStopped()) - Sleep(5); + ReadFromDevice(100); + WriteNextCommand(); + Sleep(5); } return NULL; @@ -127,6 +123,7 @@ void *CAdapterCommunication::Process(void) bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout) { + CLockObject lock(&m_commMutex); int32_t iBytesRead; uint8_t buff[1024]; if (!m_port) @@ -155,20 +152,29 @@ void CAdapterCommunication::AddData(uint8_t *data, uint8_t iLen) m_rcvCondition.Signal(); } -bool CAdapterCommunication::Write(const cec_adapter_message &data) +void CAdapterCommunication::WriteNextCommand(void) { CLockObject lock(&m_commMutex); - if (m_port->Write(data) != (int32_t) data.size()) + cec_adapter_message msg; + if (m_outBuffer.Pop(msg)) { - CStdString strError; - strError.Format("error writing to serial port: %s", m_port->GetError().c_str()); - m_controller->AddLog(CEC_LOG_ERROR, strError); - return false; + 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); + } + else + { + m_controller->AddLog(CEC_LOG_DEBUG, "command sent"); + CCondition::Sleep((uint32_t) msg.size() * (uint32_t)24 /*data*/ + (uint32_t)5 /*start bit (4.5 ms)*/); + } } +} - m_controller->AddLog(CEC_LOG_DEBUG, "command sent"); - CCondition::Sleep((uint32_t) data.size() * (uint32_t)24 /*data*/ + (uint32_t)5 /*start bit (4.5 ms)*/); - +bool CAdapterCommunication::Write(const cec_adapter_message &data) +{ + m_outBuffer.Push(data); return true; } diff --git a/src/lib/AdapterCommunication.h b/src/lib/AdapterCommunication.h index badeae9..32d4a15 100644 --- a/src/lib/AdapterCommunication.h +++ b/src/lib/AdapterCommunication.h @@ -63,14 +63,16 @@ namespace CEC static void FormatAdapterMessage(const cec_command &command, cec_adapter_message &packet); private: + void WriteNextCommand(void); void AddData(uint8_t *data, uint8_t iLen); bool ReadFromDevice(uint32_t iTimeout); - CSerialPort * m_port; - CLibCEC * m_controller; - CecBuffer m_inBuffer; - CMutex m_bufferMutex; - CMutex m_commMutex; - CCondition m_rcvCondition; + CSerialPort * m_port; + CLibCEC * m_controller; + CecBuffer m_inBuffer; + CecBuffer m_outBuffer; + CMutex m_bufferMutex; + CMutex m_commMutex; + CCondition m_rcvCondition; }; }; -- 2.34.1