cec: added an IAdapterCommunicationCallback interface an use a callback method to...
[deb_libcec.git] / src / lib / CECProcessor.cpp
index 57704bdba531e571f8db1babbede3680ef167834..f1686d64251e0a8074a34d22f1f98bd7b2e0136b 100644 (file)
@@ -48,7 +48,6 @@ using namespace std;
 using namespace PLATFORM;
 
 CCECProcessor::CCECProcessor(CLibCEC *controller, const char *strDeviceName, cec_logical_address iLogicalAddress /* = CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS*/) :
-    m_bStarted(false),
     m_bInitialised(false),
     m_iHDMIPort(CEC_DEFAULT_HDMI_PORT),
     m_iBaseDevice((cec_logical_address)CEC_DEFAULT_BASE_DEVICE),
@@ -68,7 +67,6 @@ CCECProcessor::CCECProcessor(CLibCEC *controller, const char *strDeviceName, cec
 }
 
 CCECProcessor::CCECProcessor(CLibCEC *controller, const char *strDeviceName, const cec_device_type_list &types) :
-    m_bStarted(false),
     m_bInitialised(false),
     m_iHDMIPort(CEC_DEFAULT_HDMI_PORT),
     m_iBaseDevice((cec_logical_address)CEC_DEFAULT_BASE_DEVICE),
@@ -117,8 +115,6 @@ CCECProcessor::CCECProcessor(CLibCEC *controller, const char *strDeviceName, con
 
 CCECProcessor::~CCECProcessor(void)
 {
-  m_bStarted = false;
-  m_startCondition.Broadcast();
   StopThread();
 
   delete m_communication;
@@ -134,8 +130,9 @@ bool CCECProcessor::OpenConnection(const char *strPort, uint16_t iBaudRate, uint
   CLockObject lock(m_mutex);
   if (m_communication)
   {
-    CLibCEC::AddLog(CEC_LOG_ERROR, "existing connection handler found");
-    return bReturn;
+    CLibCEC::AddLog(CEC_LOG_WARNING, "existing connection handler found, deleting it");
+    m_communication->Close();
+    delete m_communication;
   }
 
   m_communication = new CUSBCECAdapterCommunication(this, strPort, iBaudRate);
@@ -149,11 +146,11 @@ bool CCECProcessor::OpenConnection(const char *strPort, uint16_t iBaudRate, uint
 
   uint64_t iNow = GetTimeMs();
   uint64_t iTarget = iTimeoutMs > 0 ? iNow + iTimeoutMs : iNow + CEC_DEFAULT_TRANSMIT_WAIT;
-  unsigned iConnectTry(0), iPingTry(0), iFwVersionTry(0);
-  bool bConnected(false), bPinged(false);
+  unsigned iConnectTry(0), iPingTry(0), iFwVersionTry(0), iControlledTry(0);
+  bool bConnected(false), bPinged(false), bControlled(false);
 
   /* open a new connection */
-  while (iNow < iTarget && (bConnected = m_communication->Open(iTimeoutMs)) == false)
+  while (iNow < iTarget && (bConnected = m_communication->Open(this, iTimeoutMs)) == false)
   {
     CLibCEC::AddLog(CEC_LOG_ERROR, "could not open a connection (try %d)", ++iConnectTry);
     Sleep(500);
@@ -177,6 +174,17 @@ bool CCECProcessor::OpenConnection(const char *strPort, uint16_t iBaudRate, uint
     iNow = GetTimeMs();
   }
 
+  if (iFirmwareVersion >= 2)
+  {
+    /* try to set controlled mode */
+    while (bConnected && iNow < iTarget && (bControlled = m_communication->SetControlledMode(true)) == false)
+    {
+      CLibCEC::AddLog(CEC_LOG_ERROR, "the adapter did not respond correctly to setting controlled mode (try %d)", ++iControlledTry);
+      Sleep(500);
+      iNow = GetTimeMs();
+    }
+  }
+
   if ((bReturn = iFirmwareVersion != CEC_FW_VERSION_UNKNOWN) == true)
     CLibCEC::AddLog(CEC_LOG_NOTICE, "connected to the CEC adapter. firmware version = %d", iFirmwareVersion);
 
@@ -229,7 +237,7 @@ bool CCECProcessor::Start(const char *strPort, uint16_t iBaudRate /* = 38400 */,
       return bReturn;
 
     /* create the processor thread */
-    if (!CreateThread() || !m_startCondition.Wait(m_mutex) || !m_bStarted)
+    if (!CreateThread())
     {
       CLibCEC::AddLog(CEC_LOG_ERROR, "could not create a processor thread");
       return bReturn;
@@ -402,43 +410,33 @@ void CCECProcessor::ReplaceHandlers(void)
     m_busDevices[iPtr]->ReplaceHandler(m_bInitialised);
 }
 
-void *CCECProcessor::Process(void)
+bool CCECProcessor::OnCommandReceived(const cec_command &command)
 {
-  bool                  bParseFrame(false);
-  cec_command           command;
-  CCECAdapterMessage    msg;
+  m_commandBuffer.Push(command);
+  return true;
+}
 
-  {
-    CLockObject lock(m_mutex);
-    m_bStarted = true;
-    CLibCEC::AddLog(CEC_LOG_DEBUG, "processor thread started");
-    m_startCondition.Signal();
-  }
+void *CCECProcessor::Process(void)
+{
+  cec_command command;
+  CLibCEC::AddLog(CEC_LOG_DEBUG, "processor thread started");
 
-  while (!IsStopped())
+  while (!IsStopped() && m_communication->IsOpen())
   {
     ReplaceHandlers();
-    command.Clear();
-
-    {
-      CLockObject lock(m_mutex);
-      if (m_commandBuffer.Pop(command))
-        bParseFrame = true;
-      else if (m_communication->IsOpen() && m_communication->Read(command, 50))
-        bParseFrame = true;
-    }
-
-    if (bParseFrame)
+    if (m_commandBuffer.Pop(command))
       ParseCommand(command);
-    bParseFrame = false;
-
-    Sleep(5);
 
     m_controller->CheckKeypressTimeout();
+    Sleep(5);
   }
 
   if (m_communication)
+  {
     m_communication->Close();
+    delete m_communication;
+    m_communication = NULL;
+  }
 
   return NULL;
 }
@@ -556,7 +554,7 @@ bool CCECProcessor::SetHDMIPort(cec_logical_address iBaseDevice, uint8_t iPort,
 
   m_iBaseDevice = iBaseDevice;
   m_iHDMIPort = iPort;
-  if (!m_bStarted && !bForce)
+  if (!IsRunning() && !bForce)
     return true;
 
   CLibCEC::AddLog(CEC_LOG_DEBUG, "setting HDMI port to %d on device %s (%d)", iPort, ToString(iBaseDevice), (int)iBaseDevice);
@@ -883,7 +881,7 @@ void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode
   Transmit(command);
 }
 
-void CCECProcessor::ParseCommand(cec_command &command)
+void CCECProcessor::ParseCommand(const cec_command &command)
 {
   CStdString dataStr;
   dataStr.Format(">> %1x%1x:%02x", command.initiator, command.destination, command.opcode);