cec: use boost::shared_ptr for messages
authorLars Op den Kamp <lars@opdenkamp.eu>
Sat, 29 Oct 2011 23:18:35 +0000 (01:18 +0200)
committerLars Op den Kamp <lars@opdenkamp.eu>
Sat, 29 Oct 2011 23:54:39 +0000 (01:54 +0200)
configure.ac
src/lib/AdapterCommunication.cpp
src/lib/AdapterCommunication.h
src/lib/CECProcessor.cpp
src/lib/platform/linux/serialport.cpp
src/lib/platform/serialport.h

index b350c827cd30ad64b6b2d8483de40d5301788a87..124c013c0ee7cf69a752d9ad508fb0982da108ef 100644 (file)
@@ -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)
index ba027234d78253b9798ef73cf90cb3b6d66c207f..743f00d66e6caefe6bf0d079248a641e7d691bd3 100644 (file)
@@ -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))
   {
index 9aab611632a54bdda7a403aad54480f660d961af..71688301351c89d84428d63b31d319d90fc76706 100644 (file)
 #include "platform/threads.h"
 #include "util/buffer.h"
 #include <string>
+#include <boost/enable_shared_from_this.hpp>
+#include <boost/shared_ptr.hpp>
 
 namespace CEC
 {
-  class CCECAdapterMessage
+  class CCECAdapterMessage : public boost::enable_shared_from_this<CCECAdapterMessage>
   {
   public:
     CCECAdapterMessage(void) {}
@@ -61,6 +63,7 @@ namespace CEC
 
     cec_datapacket packet;
   };
+  typedef boost::shared_ptr<CCECAdapterMessage> 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<uint8_t>            m_inBuffer;
-    CecBuffer<CCECAdapterMessage> m_outBuffer;
-    CMutex                        m_bufferMutex;
-    CCondition                    m_rcvCondition;
+    CSerialPort *                    m_port;
+    CLibCEC *                        m_controller;
+    CecBuffer<uint8_t>               m_inBuffer;
+    CecBuffer<CCECAdapterMessagePtr> m_outBuffer;
+    CMutex                           m_bufferMutex;
+    CCondition                       m_rcvCondition;
   };
 };
index 7e8af29c0a88abce63037bb29e2e614f5e5133f5..73cae64b04ca0ba139b4b89a5ec40711a0f663a3 100644 (file)
@@ -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
index 32f76757584c5558a02f0bd5d4ec19a527542c4d..e362396e6cd471d41d4d562658fbc8adc5c54ec0 100644 (file)
@@ -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);
index ae465435851766b4c17f6df92543b1f37ff15e63..4fb29541008441c8255207615dc9b02e0f07e6f0 100644 (file)
@@ -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; }