cec: renamed CCommunication -> CAdapterCommunication
[deb_libcec.git] / src / lib / CECParser.cpp
index 5ac1d8ea76c0278a24fdccee6d380728aa5790fc..a86da1a58f3411c7e23193ddb852cd971209e48e 100644 (file)
 #include "util/threads.h"
 #include "util/timeutils.h"
 #include "CECDetect.h"
-#include "Communication.h"
+#include "AdapterCommunication.h"
 
 using namespace CEC;
 using namespace std;
 
 #define CEC_MAX_RETRY 5
+#define CEC_BUTTON_TIMEOUT 500
 
 /*!
  * ICECDevice implementation
@@ -57,15 +58,14 @@ CCECParser::CCECParser(const char *strDeviceName, cec_logical_address iLogicalAd
     m_iCurrentButton(CEC_USER_CONTROL_CODE_UNKNOWN),
     m_physicaladdress(iPhysicalAddress),
     m_iLogicalAddress(iLogicalAddress),
-    m_strDeviceName(strDeviceName),
-    m_bRunning(false)
+    m_strDeviceName(strDeviceName)
 {
-  m_communication = new CCommunication(this);
+  m_communication = new CAdapterCommunication(this);
 }
 
 CCECParser::~CCECParser(void)
 {
-  Close(0);
+  Close();
   m_communication->Close();
   delete m_communication;
 }
@@ -93,55 +93,36 @@ bool CCECParser::Open(const char *strPort, int iTimeoutMs /* = 10000 */)
     return false;
   }
 
-  if (pthread_create(&m_thread, NULL, (void *(*) (void *))&CCECParser::ThreadHandler, (void *)this) == 0)
-  {
-    m_bRunning = true;
-    AddLog(CEC_LOG_DEBUG, "processor thread created");
-    pthread_detach(m_thread);
+  if (CreateThread())
     return true;
-  }
   else
-  {
     AddLog(CEC_LOG_ERROR, "could not create a processor thread");
-    m_bRunning = false;
-  }
 
   return false;
 }
 
-bool CCECParser::Close(int iTimeoutMs /* = 2000 */)
+void CCECParser::Close(void)
 {
-  m_bRunning = false;
-  bool bExit(false);
-  if (iTimeoutMs > 0)
-  {
-    bExit = m_exitCondition.Wait(&m_mutex, iTimeoutMs);
-    m_mutex.Unlock();
-  }
-  else
-  {
-    pthread_join(m_thread, NULL);
-    bExit = true;
-  }
-
-  return bExit;
+  StopThread();
 }
 
-void *CCECParser::ThreadHandler(CCECParser *parser)
+void *CCECParser::Process(void)
 {
-  if (parser)
-    parser->Process();
-  return 0;
-}
+  AddLog(CEC_LOG_DEBUG, "processor thread started");
 
-bool CCECParser::Process(void)
-{
   int64_t now = GetTimeMs();
-  while (m_bRunning)
+  while (!m_bStop)
   {
-    cec_frame msg;
-    while (m_bRunning && m_communication->IsOpen() && m_communication->Read(msg, 500))
-      ParseMessage(msg);
+    bool bParseFrame(false);
+    {
+      CLockObject lock(&m_mutex);
+      cec_frame msg;
+      if (!m_bStop && m_communication->IsOpen() && m_communication->Read(msg, CEC_BUTTON_TIMEOUT))
+        bParseFrame = ParseMessage(msg);
+    }
+
+    if (bParseFrame)
+      ParseCurrentFrame();
 
     now = GetTimeMs();
     CheckKeypressTimeout(now);
@@ -149,14 +130,12 @@ bool CCECParser::Process(void)
   }
 
   AddLog(CEC_LOG_DEBUG, "processor thread terminated");
-  m_bRunning = false;
-  m_exitCondition.Signal();
-  return true;
+  return NULL;
 }
 
 bool CCECParser::Ping(void)
 {
-  if (!m_bRunning)
+  if (!IsRunning())
     return false;
 
   AddLog(CEC_LOG_DEBUG, "sending ping");
@@ -165,7 +144,7 @@ bool CCECParser::Ping(void)
   PushEscaped(output, MSGCODE_PING);
   output.push_back(MSGEND);
 
-  if (!TransmitFormatted(output, false, (int64_t) 5000))
+  if (!TransmitFormatted(output, false))
   {
     AddLog(CEC_LOG_ERROR, "could not send ping command");
     return false;
@@ -179,7 +158,7 @@ bool CCECParser::Ping(void)
 
 bool CCECParser::StartBootloader(void)
 {
-  if (!m_bRunning)
+  if (!IsRunning())
     return false;
 
   AddLog(CEC_LOG_DEBUG, "starting the bootloader");
@@ -188,7 +167,7 @@ bool CCECParser::StartBootloader(void)
   PushEscaped(output, MSGCODE_START_BOOTLOADER);
   output.push_back(MSGEND);
 
-  if (!TransmitFormatted(output, false, (int64_t) 5000))
+  if (!TransmitFormatted(output, false))
   {
     AddLog(CEC_LOG_ERROR, "could not start the bootloader");
     return false;
@@ -210,7 +189,7 @@ bool CCECParser::PowerOffDevices(cec_logical_address address /* = CECDEVICE_BROA
 
 bool CCECParser::PowerOnDevices(cec_logical_address address /* = CECDEVICE_TV */)
 {
-  if (!m_bRunning)
+  if (!IsRunning())
     return false;
 
   CStdString strLog;
@@ -224,7 +203,7 @@ bool CCECParser::PowerOnDevices(cec_logical_address address /* = CECDEVICE_TV */
 
 bool CCECParser::StandbyDevices(cec_logical_address address /* = CECDEVICE_BROADCAST */)
 {
-  if (!m_bRunning)
+  if (!IsRunning())
     return false;
 
   CStdString strLog;
@@ -238,7 +217,7 @@ bool CCECParser::StandbyDevices(cec_logical_address address /* = CECDEVICE_BROAD
 
 bool CCECParser::SetActiveView(void)
 {
-  if (!m_bRunning)
+  if (!IsRunning())
     return false;
 
   AddLog(CEC_LOG_DEBUG, "setting active view");
@@ -252,7 +231,7 @@ bool CCECParser::SetActiveView(void)
 
 bool CCECParser::SetInactiveView(void)
 {
-  if (!m_bRunning)
+  if (!IsRunning())
     return false;
 
   AddLog(CEC_LOG_DEBUG, "setting inactive view");
@@ -271,12 +250,12 @@ bool CCECParser::GetNextLogMessage(cec_log_message *message)
 
 bool CCECParser::GetNextKeypress(cec_keypress *key)
 {
-  return m_bRunning ? m_keyBuffer.Pop(*key) : false;
+  return IsRunning() ? m_keyBuffer.Pop(*key) : false;
 }
 
 bool CCECParser::GetNextCommand(cec_command *command)
 {
-  return m_bRunning ? m_commandBuffer.Pop(*command) : false;
+  return IsRunning() ? m_commandBuffer.Pop(*command) : false;
 }
 //@}
 
@@ -376,10 +355,13 @@ void CCECParser::BroadcastActiveSource(void)
   Transmit(frame);
 }
 
-bool CCECParser::TransmitFormatted(const cec_frame &data, bool bWaitForAck /* = true */, int64_t iTimeout /* = 2000 */)
+bool CCECParser::TransmitFormatted(const cec_frame &data, bool bWaitForAck /* = true */)
 {
-  if (!m_communication || m_communication->Write(data) != data.size())
+  CLockObject lock(&m_mutex);
+  if (!m_communication || !m_communication->Write(data))
+  {
     return false;
+  }
 
   CCondition::Sleep((int) data.size() * 24 /*data*/ + 5 /*start bit (4.5 ms)*/ + 50 /* to be on the safe side */);
   if (bWaitForAck && !WaitForAck())
@@ -391,7 +373,7 @@ bool CCECParser::TransmitFormatted(const cec_frame &data, bool bWaitForAck /* =
   return true;
 }
 
-bool CCECParser::Transmit(const cec_frame &data, bool bWaitForAck /* = true */, int64_t iTimeout /* = 5000 */)
+bool CCECParser::Transmit(const cec_frame &data, bool bWaitForAck /* = true */)
 {
   CStdString txStr = "transmit ";
   for (unsigned int i = 0; i < data.size(); i++)
@@ -432,16 +414,16 @@ bool CCECParser::Transmit(const cec_frame &data, bool bWaitForAck /* = true */,
     output.push_back(MSGEND);
   }
 
-  return TransmitFormatted(output, bWaitForAck, iTimeout);
+  return TransmitFormatted(output, bWaitForAck);
 }
 
-bool CCECParser::WaitForAck(int64_t iTimeout /* = 1000 */)
+bool CCECParser::WaitForAck(int iTimeout /* = 1000 */)
 {
   bool bGotAck(false);
   bool bError(false);
 
   int64_t iNow = GetTimeMs();
-  int64_t iTargetTime = iNow + iTimeout;
+  int64_t iTargetTime = iNow + (int64_t) iTimeout;
 
   while (!bGotAck && !bError && (iTimeout <= 0 || iNow < iTargetTime))
   {
@@ -496,10 +478,12 @@ bool CCECParser::WaitForAck(int64_t iTimeout /* = 1000 */)
   return bGotAck && !bError;
 }
 
-void CCECParser::ParseMessage(cec_frame &msg)
+bool CCECParser::ParseMessage(cec_frame &msg)
 {
+  bool bReturn(false);
+
   if (msg.empty())
-    return;
+    return bReturn;
 
   CStdString logStr;
   uint8_t iCode = msg[0] & ~(MSGCODE_FRAME_EOM | MSGCODE_FRAME_ACK);
@@ -556,11 +540,13 @@ void CCECParser::ParseMessage(cec_frame &msg)
       AddLog(CEC_LOG_DEBUG, logStr.c_str());
     }
     if (bEom)
-      ParseCurrentFrame();
+      bReturn = true;
     break;
   default:
     break;
   }
+
+  return bReturn;
 }
 
 void CCECParser::ParseCurrentFrame(void)
@@ -677,7 +663,7 @@ void CCECParser::PushEscaped(cec_frame &vec, uint8_t byte)
 
 void CCECParser::CheckKeypressTimeout(int64_t now)
 {
-  if (m_iCurrentButton != CEC_USER_CONTROL_CODE_UNKNOWN && now - m_buttontime > 500)
+  if (m_iCurrentButton != CEC_USER_CONTROL_CODE_UNKNOWN && now - m_buttontime > CEC_BUTTON_TIMEOUT)
   {
     AddKey();
     m_iCurrentButton = CEC_USER_CONTROL_CODE_UNKNOWN;
@@ -708,7 +694,7 @@ bool CCECParser::SetAckMask(uint16_t iMask)
   PushEscaped(output, (uint8_t)iMask);
   output.push_back(MSGEND);
 
-  if (m_communication->Write(output) == -1)
+  if (!m_communication->Write(output))
   {
     AddLog(CEC_LOG_ERROR, "could not set the ackmask");
     return false;