From: Lars Op den Kamp Date: Sat, 29 Oct 2011 23:18:35 +0000 (+0200) Subject: cec: use boost::shared_ptr for messages X-Git-Tag: upstream/2.2.0~1^2~183 X-Git-Url: https://git.piment-noir.org/?p=deb_libcec.git;a=commitdiff_plain;h=5a1e87c8d9a1d8a6b8dbf8b7d1738395fdb6b856 cec: use boost::shared_ptr for messages --- diff --git a/configure.ac b/configure.ac index b350c82..124c013 100644 --- a/configure.ac +++ b/configure.ac @@ -23,6 +23,11 @@ libs_pre_dl=$LIBS AC_SUBST([LIBS_DL]) LIBS=$libs_pre_dl +AC_LANG_PUSH([C++]) +AC_CHECK_HEADERS([boost/shared_ptr.hpp], [], + [AC_MSG_ERROR(You need the Boost libraries.)]) +AC_LANG_POP([C++]) + CXXFLAGS="-fPIC -Wall -Wextra $CXXFLAGS" AC_SUBST(REQUIRES) diff --git a/src/lib/AdapterCommunication.cpp b/src/lib/AdapterCommunication.cpp index ba02723..743f00d 100644 --- a/src/lib/AdapterCommunication.cpp +++ b/src/lib/AdapterCommunication.cpp @@ -210,10 +210,10 @@ void CAdapterCommunication::AddData(uint8_t *data, uint8_t iLen) void CAdapterCommunication::WriteNextCommand(void) { - CCECAdapterMessage msg; + CCECAdapterMessagePtr msg; if (m_outBuffer.Pop(msg)) { - if (m_port->Write(msg) != (int32_t) msg.size()) + if (m_port->Write(msg) != (int32_t) msg.get()->size()) { CStdString strError; strError.Format("error writing to serial port: %s", m_port->GetError().c_str()); @@ -222,12 +222,12 @@ void CAdapterCommunication::WriteNextCommand(void) 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)*/); + CCondition::Sleep((uint32_t) msg.get()->size() * (uint32_t)24 /*data*/ + (uint32_t)5 /*start bit (4.5 ms)*/); } } } -bool CAdapterCommunication::Write(const CCECAdapterMessage &data) +bool CAdapterCommunication::Write(CCECAdapterMessagePtr data) { m_outBuffer.Push(data); return true; @@ -295,11 +295,11 @@ bool CAdapterCommunication::StartBootloader(void) return false; m_controller->AddLog(CEC_LOG_DEBUG, "starting the bootloader"); - CCECAdapterMessage output; + CCECAdapterMessagePtr output(new CCECAdapterMessage); - output.push_back(MSGSTART); - output.push_escaped(MSGCODE_START_BOOTLOADER); - output.push_back(MSGEND); + output->push_back(MSGSTART); + output->push_escaped(MSGCODE_START_BOOTLOADER); + output->push_back(MSGEND); if (!Write(output)) { @@ -319,13 +319,13 @@ bool CAdapterCommunication::SetAckMask(uint16_t iMask) strLog.Format("setting ackmask to %2x", iMask); m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str()); - CCECAdapterMessage output; + CCECAdapterMessagePtr output(new CCECAdapterMessage); - output.push_back(MSGSTART); - output.push_escaped(MSGCODE_SET_ACK_MASK); - output.push_escaped(iMask >> 8); - output.push_escaped((uint8_t)iMask); - output.push_back(MSGEND); + output->push_back(MSGSTART); + output->push_escaped(MSGCODE_SET_ACK_MASK); + output->push_escaped(iMask >> 8); + output->push_escaped((uint8_t)iMask); + output->push_back(MSGEND); if (!Write(output)) { @@ -342,11 +342,11 @@ bool CAdapterCommunication::PingAdapter(void) return false; m_controller->AddLog(CEC_LOG_DEBUG, "sending ping"); - CCECAdapterMessage output; + CCECAdapterMessagePtr output(new CCECAdapterMessage); - output.push_back(MSGSTART); - output.push_escaped(MSGCODE_PING); - output.push_back(MSGEND); + output->push_back(MSGSTART); + output->push_escaped(MSGCODE_PING); + output->push_back(MSGEND); if (!Write(output)) { diff --git a/src/lib/AdapterCommunication.h b/src/lib/AdapterCommunication.h index 9aab611..7168830 100644 --- a/src/lib/AdapterCommunication.h +++ b/src/lib/AdapterCommunication.h @@ -35,10 +35,12 @@ #include "platform/threads.h" #include "util/buffer.h" #include +#include +#include namespace CEC { - class CCECAdapterMessage + class CCECAdapterMessage : public boost::enable_shared_from_this { public: CCECAdapterMessage(void) {} @@ -61,6 +63,7 @@ namespace CEC cec_datapacket packet; }; + typedef boost::shared_ptr CCECAdapterMessagePtr; class CSerialPort; class CLibCEC; @@ -73,7 +76,7 @@ namespace CEC bool Open(const char *strPort, uint16_t iBaudRate = 38400, uint32_t iTimeoutMs = 10000); bool Read(CCECAdapterMessage &msg, uint32_t iTimeout = 1000); - bool Write(const CCECAdapterMessage &frame); + bool Write(CCECAdapterMessagePtr data); bool PingAdapter(void); void Close(void); bool IsOpen(void) const; @@ -89,11 +92,11 @@ namespace CEC void AddData(uint8_t *data, uint8_t iLen); bool ReadFromDevice(uint32_t iTimeout); - CSerialPort * m_port; - CLibCEC * m_controller; - CecBuffer m_inBuffer; - CecBuffer m_outBuffer; - CMutex m_bufferMutex; - CCondition m_rcvCondition; + CSerialPort * m_port; + CLibCEC * m_controller; + CecBuffer m_inBuffer; + CecBuffer m_outBuffer; + CMutex m_bufferMutex; + CCondition m_rcvCondition; }; }; diff --git a/src/lib/CECProcessor.cpp b/src/lib/CECProcessor.cpp index 7e8af29..73cae64 100644 --- a/src/lib/CECProcessor.cpp +++ b/src/lib/CECProcessor.cpp @@ -186,7 +186,7 @@ bool CCECProcessor::Transmit(const cec_command &data, bool bWaitForAck /* = true bool bReturn(false); LogOutput(data); - CCECAdapterMessage output(data); + CCECAdapterMessagePtr output(new CCECAdapterMessage(data)); CLockObject lock(&m_mutex); if (!m_communication || !m_communication->Write(output)) @@ -195,7 +195,7 @@ bool CCECProcessor::Transmit(const cec_command &data, bool bWaitForAck /* = true if (bWaitForAck) { bool bError(false); - if ((bReturn = WaitForAck(&bError, output.size(), 1000)) == false) + if ((bReturn = WaitForAck(&bError, output->size(), 1000)) == false) m_controller->AddLog(CEC_LOG_ERROR, "did not receive ack"); } else diff --git a/src/lib/platform/linux/serialport.cpp b/src/lib/platform/linux/serialport.cpp index 32f7675..e362396 100644 --- a/src/lib/platform/linux/serialport.cpp +++ b/src/lib/platform/linux/serialport.cpp @@ -46,7 +46,7 @@ CSerialPort::~CSerialPort() Close(); } -int8_t CSerialPort::Write(const CCECAdapterMessage &data) +int8_t CSerialPort::Write(CCECAdapterMessagePtr data) { fd_set port; @@ -59,7 +59,7 @@ int8_t CSerialPort::Write(const CCECAdapterMessage &data) int32_t byteswritten = 0; - while (byteswritten < (int32_t) data.size()) + while (byteswritten < (int32_t) data->size()) { FD_ZERO(&port); FD_SET(m_fd, &port); @@ -70,7 +70,7 @@ int8_t CSerialPort::Write(const CCECAdapterMessage &data) return -1; } - returnv = write(m_fd, data.packet.data + byteswritten, data.size() - byteswritten); + returnv = write(m_fd, data->packet.data + byteswritten, data->size() - byteswritten); if (returnv == -1) { m_error = strerror(errno); diff --git a/src/lib/platform/serialport.h b/src/lib/platform/serialport.h index ae46543..4fb2954 100644 --- a/src/lib/platform/serialport.h +++ b/src/lib/platform/serialport.h @@ -47,7 +47,7 @@ namespace CEC bool IsOpen(); void Close(); - int8_t Write(const CCECAdapterMessage &data); + int8_t Write(CCECAdapterMessagePtr data); int32_t Read(uint8_t* data, uint32_t len, uint64_t iTimeoutMs = 0); std::string GetError() { return m_error; }