From: Lars Op den Kamp Date: Sat, 29 Oct 2011 14:54:27 +0000 (+0200) Subject: cec: make all reads and write in CAdapterCommunication go through buffers, so we... X-Git-Tag: upstream/2.2.0~1^2~190 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=3c53ac93d0c95ebf700c08f9ccfb2810dfeea8b1;p=deb_libcec.git 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 --- 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; }; };