cec: moved cec_adapter_message to CCECAdapterMessage
authorLars Op den Kamp <lars@opdenkamp.eu>
Sat, 29 Oct 2011 15:17:35 +0000 (17:17 +0200)
committerLars Op den Kamp <lars@opdenkamp.eu>
Sat, 29 Oct 2011 15:17:35 +0000 (17:17 +0200)
include/cectypes.h
src/lib/AdapterCommunication.cpp
src/lib/AdapterCommunication.h
src/lib/CECProcessor.cpp
src/lib/CECProcessor.h
src/lib/platform/linux/serialport.cpp
src/lib/platform/serialport.h
src/lib/platform/windows/serialport.cpp

index 4396755d075587d313938d7ad5898ddb8f64b020..c08c64d4d4bdcb2d462a1083f4fc13b86e866856 100644 (file)
@@ -625,26 +625,6 @@ typedef struct cec_datapacket
 
 } cec_datapacket;
 
-typedef struct cec_adapter_message
-{
-  cec_datapacket packet;
-
-#ifdef __cplusplus
-  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                    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; }
-  bool                    eom(void) const               { return packet.size >= 1 ? (packet.at(0) & MSGCODE_FRAME_EOM) != 0 : false; }
-  bool                    ack(void) const               { return packet.size >= 1 ? (packet.at(0) & MSGCODE_FRAME_ACK) != 0 : false; }
-  cec_logical_address     initiator(void) const         { return packet.size >= 2 ? (cec_logical_address) (packet.at(1) >> 4)  : CECDEVICE_UNKNOWN; };
-  cec_logical_address     destination(void) const       { return packet.size >= 2 ? (cec_logical_address) (packet.at(1) & 0xF) : CECDEVICE_UNKNOWN; };
-#endif
-} cec_adapter_message;
-
 typedef struct cec_command
 {
   cec_logical_address initiator;
index 2e330d3fe1005596714019952db40e6f5572a019..0439100bf60e4938b026a9be22392bb153d518b5 100644 (file)
@@ -155,7 +155,7 @@ void CAdapterCommunication::AddData(uint8_t *data, uint8_t iLen)
 void CAdapterCommunication::WriteNextCommand(void)
 {
   CLockObject lock(&m_commMutex);
-  cec_adapter_message msg;
+  CCECAdapterMessage msg;
   if (m_outBuffer.Pop(msg))
   {
     if (m_port->Write(msg) != (int32_t) msg.size())
@@ -172,13 +172,13 @@ void CAdapterCommunication::WriteNextCommand(void)
   }
 }
 
-bool CAdapterCommunication::Write(const cec_adapter_message &data)
+bool CAdapterCommunication::Write(const CCECAdapterMessage &data)
 {
   m_outBuffer.Push(data);
   return true;
 }
 
-bool CAdapterCommunication::Read(cec_adapter_message &msg, uint32_t iTimeout)
+bool CAdapterCommunication::Read(CCECAdapterMessage &msg, uint32_t iTimeout)
 {
   CLockObject lock(&m_bufferMutex);
 
@@ -240,11 +240,10 @@ bool CAdapterCommunication::StartBootloader(void)
     return false;
 
   m_controller->AddLog(CEC_LOG_DEBUG, "starting the bootloader");
-  cec_adapter_message output;
-  output.clear();
+  CCECAdapterMessage output;
 
   output.push_back(MSGSTART);
-  PushEscaped(output, MSGCODE_START_BOOTLOADER);
+  output.push_escaped(MSGCODE_START_BOOTLOADER);
   output.push_back(MSGEND);
 
   if (!Write(output))
@@ -256,19 +255,6 @@ bool CAdapterCommunication::StartBootloader(void)
   return true;
 }
 
-void CAdapterCommunication::PushEscaped(cec_adapter_message &vec, uint8_t byte)
-{
-  if (byte >= MSGESC && byte != MSGSTART)
-  {
-    vec.push_back(MSGESC);
-    vec.push_back(byte - ESCOFFSET);
-  }
-  else
-  {
-    vec.push_back(byte);
-  }
-}
-
 bool CAdapterCommunication::SetAckMask(uint16_t iMask)
 {
   if (!IsRunning())
@@ -278,13 +264,12 @@ bool CAdapterCommunication::SetAckMask(uint16_t iMask)
   strLog.Format("setting ackmask to %2x", iMask);
   m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
 
-  cec_adapter_message output;
-  output.clear();
+  CCECAdapterMessage output;
 
   output.push_back(MSGSTART);
-  PushEscaped(output, MSGCODE_SET_ACK_MASK);
-  PushEscaped(output, iMask >> 8);
-  PushEscaped(output, (uint8_t)iMask);
+  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))
@@ -302,11 +287,10 @@ bool CAdapterCommunication::PingAdapter(void)
     return false;
 
   m_controller->AddLog(CEC_LOG_DEBUG, "sending ping");
-  cec_adapter_message output;
-  output.clear();
+  CCECAdapterMessage output;
 
   output.push_back(MSGSTART);
-  PushEscaped(output, MSGCODE_PING);
+  output.push_escaped(MSGCODE_PING);
   output.push_back(MSGEND);
 
   if (!Write(output))
@@ -325,46 +309,3 @@ bool CAdapterCommunication::IsOpen(void) const
 {
   return !IsStopped() && m_port->IsOpen();
 }
-
-void CAdapterCommunication::FormatAdapterMessage(const cec_command &command, cec_adapter_message &packet)
-{
-  packet.clear();
-
-  //set ack polarity to high when transmitting to the broadcast address
-  //set ack polarity low when transmitting to any other address
-  packet.push_back(MSGSTART);
-  PushEscaped(packet, MSGCODE_TRANSMIT_ACK_POLARITY);
-  if (command.destination == CECDEVICE_BROADCAST)
-    PushEscaped(packet, CEC_TRUE);
-  else
-    PushEscaped(packet, CEC_FALSE);
-  packet.push_back(MSGEND);
-
-  // add source and destination
-  packet.push_back(MSGSTART);
-  PushEscaped(packet, MSGCODE_TRANSMIT);
-  packet.push_back(((uint8_t)command.initiator << 4) + (uint8_t)command.destination);
-  packet.push_back(MSGEND);
-
-  // add opcode
-  packet.push_back(MSGSTART);
-  PushEscaped(packet, command.parameters.empty() ? (uint8_t)MSGCODE_TRANSMIT_EOM : (uint8_t)MSGCODE_TRANSMIT);
-  packet.push_back((uint8_t) command.opcode);
-  packet.push_back(MSGEND);
-
-  // add parameters
-  for (int8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
-  {
-    packet.push_back(MSGSTART);
-
-    if (iPtr == command.parameters.size - 1)
-      PushEscaped(packet, MSGCODE_TRANSMIT_EOM);
-    else
-      PushEscaped(packet, MSGCODE_TRANSMIT);
-
-    PushEscaped(packet, command.parameters[iPtr]);
-
-    packet.push_back(MSGEND);
-  }
-}
-
index 32d4a157ebb7bf77a7eca084c91653cf46d0175b..c9a8e8c1155de1132d85c324c702182b5123e538 100644 (file)
 
 namespace CEC
 {
+  class CCECAdapterMessage
+  {
+  public:
+    CCECAdapterMessage(void) {}
+    CCECAdapterMessage(const cec_command &command)
+    {
+      clear();
+
+      //set ack polarity to high when transmitting to the broadcast address
+      //set ack polarity low when transmitting to any other address
+      push_back(MSGSTART);
+      push_escaped(MSGCODE_TRANSMIT_ACK_POLARITY);
+      if (command.destination == CECDEVICE_BROADCAST)
+        push_escaped(CEC_TRUE);
+      else
+        push_escaped(CEC_FALSE);
+      push_back(MSGEND);
+
+      // add source and destination
+      push_back(MSGSTART);
+      push_escaped(MSGCODE_TRANSMIT);
+      push_back(((uint8_t)command.initiator << 4) + (uint8_t)command.destination);
+      push_back(MSGEND);
+
+      // add opcode
+      push_back(MSGSTART);
+      push_escaped(command.parameters.empty() ? (uint8_t)MSGCODE_TRANSMIT_EOM : (uint8_t)MSGCODE_TRANSMIT);
+      push_back((uint8_t) command.opcode);
+      push_back(MSGEND);
+
+      // add parameters
+      for (int8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
+      {
+        push_back(MSGSTART);
+
+        if (iPtr == command.parameters.size - 1)
+          push_escaped( MSGCODE_TRANSMIT_EOM);
+        else
+          push_escaped(MSGCODE_TRANSMIT);
+
+        push_escaped(command.parameters[iPtr]);
+
+        push_back(MSGEND);
+      }
+    }
+
+    CCECAdapterMessage &operator =(const CCECAdapterMessage &msg)
+    {
+      packet = msg.packet;
+      return *this;
+    }
+
+    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                    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; }
+    bool                    eom(void) const               { return packet.size >= 1 ? (packet.at(0) & MSGCODE_FRAME_EOM) != 0 : false; }
+    bool                    ack(void) const               { return packet.size >= 1 ? (packet.at(0) & MSGCODE_FRAME_ACK) != 0 : false; }
+    cec_logical_address     initiator(void) const         { return packet.size >= 2 ? (cec_logical_address) (packet.at(1) >> 4)  : CECDEVICE_UNKNOWN; };
+    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)
+    {
+      if (byte >= MSGESC && byte != MSGSTART)
+      {
+        push_back(MSGESC);
+        push_back(byte - ESCOFFSET);
+      }
+      else
+         push_back(byte);
+    }
+
+    cec_datapacket packet;
+  };
+
   class CSerialPort;
   class CLibCEC;
 
@@ -48,8 +126,8 @@ namespace CEC
     virtual ~CAdapterCommunication();
 
     bool Open(const char *strPort, uint16_t iBaudRate = 38400, uint32_t iTimeoutMs = 10000);
-    bool Read(cec_adapter_message &msg, uint32_t iTimeout = 1000);
-    bool Write(const cec_adapter_message &frame);
+    bool Read(CCECAdapterMessage &msg, uint32_t iTimeout = 1000);
+    bool Write(const CCECAdapterMessage &frame);
     bool PingAdapter(void);
     void Close(void);
     bool IsOpen(void) const;
@@ -59,20 +137,18 @@ namespace CEC
 
     bool StartBootloader(void);
     bool SetAckMask(uint16_t iMask);
-    static void PushEscaped(cec_adapter_message &vec, uint8_t byte);
-    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<uint8_t>             m_inBuffer;
-    CecBuffer<cec_adapter_message> m_outBuffer;
-    CMutex                         m_bufferMutex;
-    CMutex                         m_commMutex;
-    CCondition                     m_rcvCondition;
+    CSerialPort *                 m_port;
+    CLibCEC *                     m_controller;
+    CecBuffer<uint8_t>            m_inBuffer;
+    CecBuffer<CCECAdapterMessage> m_outBuffer;
+    CMutex                        m_bufferMutex;
+    CMutex                        m_commMutex;
+    CCondition                    m_rcvCondition;
   };
 };
index 08f69cd7b1674cc994f37c0468c16bbdd49b6b23..e15b2345d6cc3b560189ce94fbcacc4875d5713b 100644 (file)
@@ -88,7 +88,7 @@ void *CCECProcessor::Process(void)
   m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started");
 
   cec_command command;
-  cec_adapter_message msg;
+  CCECAdapterMessage msg;
 
   while (!IsStopped())
   {
@@ -151,10 +151,7 @@ bool CCECProcessor::Transmit(const cec_command &data, bool bWaitForAck /* = true
 {
   LogOutput(data);
 
-  cec_adapter_message output;
-  output.clear();
-  CAdapterCommunication::FormatAdapterMessage(data, output);
-
+  CCECAdapterMessage output(data);
   return TransmitFormatted(output, bWaitForAck);
 }
 
@@ -187,7 +184,7 @@ bool CCECProcessor::SwitchMonitoring(bool bEnable)
     return m_communication && m_communication->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress);
 }
 
-bool CCECProcessor::TransmitFormatted(const cec_adapter_message &data, bool bWaitForAck /* = true */)
+bool CCECProcessor::TransmitFormatted(const CCECAdapterMessage &data, bool bWaitForAck /* = true */)
 {
   bool bReturn(false);
   CLockObject lock(&m_mutex);
@@ -240,8 +237,7 @@ bool CCECProcessor::WaitForAck(bool *bError, uint8_t iLength, uint32_t iTimeout
 
   while (!bTransmitSucceeded && !*bError && (iTimeout == 0 || iNow < iTargetTime))
   {
-    cec_adapter_message msg;
-    msg.clear();
+    CCECAdapterMessage msg;
 
     if (!m_communication->Read(msg, iTimeout > 0 ? (int32_t)(iTargetTime - iNow) : 1000))
     {
@@ -315,7 +311,7 @@ bool CCECProcessor::WaitForAck(bool *bError, uint8_t iLength, uint32_t iTimeout
   return bTransmitSucceeded && !*bError;
 }
 
-bool CCECProcessor::ParseMessage(cec_adapter_message &msg)
+bool CCECProcessor::ParseMessage(CCECAdapterMessage &msg)
 {
   bool bEom = false;
 
index fc37c02a084c18e467a0843519d6a7f05b3a93b0..f635cd7d5e87df66ac8355af0713adeb19ad0c91 100644 (file)
@@ -33,6 +33,7 @@
 
 #include <string>
 #include <cectypes.h>
+#include "AdapterCommunication.h"
 #include "platform/threads.h"
 #include "util/buffer.h"
 #include "util/StdString.h"
@@ -70,7 +71,7 @@ namespace CEC
       virtual void AddKey(void);
       virtual void AddLog(cec_log_level level, const CStdString &strMessage);
 
-      virtual bool TransmitFormatted(const cec_adapter_message &data, bool bWaitForAck = true);
+      virtual bool TransmitFormatted(const CCECAdapterMessage &data, bool bWaitForAck = true);
       virtual void TransmitAbort(cec_logical_address address, cec_opcode opcode, ECecAbortReason reason = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE);
 
       CCECBusDevice *m_busDevices[16];
@@ -78,16 +79,16 @@ namespace CEC
     private:
       void LogOutput(const cec_command &data);
       bool WaitForAck(bool *bError, uint8_t iLength, uint32_t iTimeout = 1000);
-      bool ParseMessage(cec_adapter_message &msg);
+      bool ParseMessage(CCECAdapterMessage &msg);
       void ParseCommand(cec_command &command);
 
-      cec_command                    m_currentframe;
-      cec_logical_address            m_iLogicalAddress;
-      CecBuffer<cec_adapter_message> m_frameBuffer;
-      std::string                    m_strDeviceName;
-      CMutex                         m_mutex;
-      CAdapterCommunication         *m_communication;
-      CLibCEC                       *m_controller;
-      bool                           m_bMonitor;
+      cec_command                   m_currentframe;
+      cec_logical_address           m_iLogicalAddress;
+      CecBuffer<CCECAdapterMessage> m_frameBuffer;
+      std::string                   m_strDeviceName;
+      CMutex                        m_mutex;
+      CAdapterCommunication        *m_communication;
+      CLibCEC                      *m_controller;
+      bool                          m_bMonitor;
   };
 };
index d323708214c8b602a9edc1dd02f0ead087a3b635..32f76757584c5558a02f0bd5d4ec19a527542c4d 100644 (file)
@@ -46,7 +46,7 @@ CSerialPort::~CSerialPort()
   Close();
 }
 
-int8_t CSerialPort::Write(const cec_adapter_message &data)
+int8_t CSerialPort::Write(const CCECAdapterMessage &data)
 {
   fd_set port;
 
index 4d1be825c77d373a5eaacc2092c9f39b13b11dc9..ae465435851766b4c17f6df92543b1f37ff15e63 100644 (file)
@@ -22,6 +22,7 @@
 #include <cectypes.h>
 #include <string>
 #include <stdint.h>
+#include "../AdapterCommunication.h"
 #include "../platform/threads.h"
 
 #ifndef __WINDOWS__
@@ -46,7 +47,7 @@ namespace CEC
       bool IsOpen();
       void Close();
 
-      int8_t Write(const cec_adapter_message &data);
+      int8_t Write(const CCECAdapterMessage &data);
       int32_t Read(uint8_t* data, uint32_t len, uint64_t iTimeoutMs = 0);
 
       std::string GetError() { return m_error; }
index de2a9a394171f8c60367038cac90592953223afd..d8f0abe63979f1b55254cbacfdd32c7f4f4a6a4e 100644 (file)
@@ -166,7 +166,7 @@ void CSerialPort::Close(void)
   }
 }
 
-int8_t CSerialPort::Write(const cec_adapter_message &data)
+int8_t CSerialPort::Write(const CCECAdapterMessage &data)
 {
   CLockObject lock(&m_mutex);
   DWORD iBytesWritten = 0;