cec: don't refresh the physical address in the poll thread
[deb_libcec.git] / src / lib / CECProcessor.cpp
index 8c95da7294a4fe866115ca6003947f7961fe173a..292d69dd56e223dbf5249383831ade80b29de88e 100644 (file)
@@ -53,7 +53,8 @@ CCECProcessor::CCECProcessor(CLibCEC *controller, CAdapterCommunication *serComm
     m_strDeviceName(strDeviceName),
     m_communication(serComm),
     m_controller(controller),
-    m_bMonitor(false)
+    m_bMonitor(false),
+    m_busScan(NULL)
 {
   m_logicalAddresses.Clear();
   m_logicalAddresses.Set(iLogicalAddress);
@@ -107,8 +108,15 @@ CCECProcessor::CCECProcessor(CLibCEC *controller, CAdapterCommunication *serComm
 
 CCECProcessor::~CCECProcessor(void)
 {
+  if (m_busScan)
+  {
+    m_busScan->StopThread();
+    delete m_busScan;
+  }
+
   m_startCondition.Broadcast();
   StopThread();
+
   m_communication = NULL;
   m_controller = NULL;
   for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
@@ -131,7 +139,15 @@ bool CCECProcessor::Start(void)
       m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread");
       return false;
     }
-    return true;
+
+    lock.Leave();
+    if (SetAckMask(m_logicalAddresses.AckMask()) &&
+        SetHDMIPort(m_iHDMIPort, true))
+    {
+      m_busScan = new CCECBusScan(this);
+      m_busScan->CreateThread(true);
+      return true;
+    }
   }
   else
     m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread");
@@ -220,30 +236,19 @@ void *CCECProcessor::Process(void)
   cec_command           command;
   CCECAdapterMessage    msg;
 
+  if (m_logicalAddresses.IsEmpty() && !FindLogicalAddresses())
   {
-    if (m_logicalAddresses.IsEmpty() && !FindLogicalAddresses())
-    {
-      CLockObject lock(&m_mutex);
-      m_controller->AddLog(CEC_LOG_ERROR, "could not detect our logical addresses");
-      m_startCondition.Signal();
-      return NULL;
-    }
-
-    SetAckMask(m_logicalAddresses.AckMask());
-
-    ScanCECBus();
-
-    {
-      CLockObject lock(&m_mutex);
-      m_bStarted = true;
-      lock.Leave();
-
-      SetHDMIPort(m_iHDMIPort);
-
-      lock.Lock();
-      m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started");
-      m_startCondition.Signal();
-    }
+    CLockObject lock(&m_mutex);
+    m_controller->AddLog(CEC_LOG_ERROR, "could not detect our logical addresses");
+    m_startCondition.Signal();
+    return NULL;
+  }
+  else
+  {
+    CLockObject lock(&m_mutex);
+    m_bStarted = true;
+    m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started");
+    m_startCondition.Signal();
   }
 
   while (!IsStopped())
@@ -272,14 +277,6 @@ void *CCECProcessor::Process(void)
     Sleep(5);
 
     m_controller->CheckKeypressTimeout();
-
-    for (unsigned int iDevicePtr = 0; iDevicePtr < 16; iDevicePtr++)
-    {
-      if (!m_logicalAddresses[iDevicePtr])
-        m_busDevices[iDevicePtr]->PollVendorId();
-    }
-
-    Sleep(5);
   }
 
   return NULL;
@@ -352,7 +349,7 @@ bool CCECProcessor::SetDeckInfo(cec_deck_info info, bool bSendUpdate /* = true *
   return bReturn;
 }
 
-bool CCECProcessor::SetHDMIPort(uint8_t iPort)
+bool CCECProcessor::SetHDMIPort(uint8_t iPort, bool bForce /* = false */)
 {
   bool bReturn(false);
 
@@ -361,7 +358,7 @@ bool CCECProcessor::SetHDMIPort(uint8_t iPort)
   AddLog(CEC_LOG_DEBUG, strLog);
 
   m_iHDMIPort = iPort;
-  if (!m_bStarted)
+  if (!m_bStarted && !bForce)
     return true;
 
   uint16_t iPhysicalAddress(0);
@@ -387,24 +384,9 @@ bool CCECProcessor::SetHDMIPort(uint8_t iPort)
   return bReturn;
 }
 
-void CCECProcessor::ScanCECBus(void)
-{
-  CCECBusDevice *device(NULL);
-  for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
-  {
-    device = m_busDevices[iPtr];
-    if (device && device->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
-    {
-      device->GetPhysicalAddress();
-      device->GetCecVersion();
-      device->GetVendorId();
-    }
-  }
-}
-
 bool CCECProcessor::CheckPhysicalAddress(uint16_t iPhysicalAddress)
 {
-  for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
+  for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
   {
     if (m_busDevices[iPtr]->GetPhysicalAddress(false) == iPhysicalAddress)
       return true;
@@ -426,13 +408,13 @@ bool CCECProcessor::SetStreamPath(uint16_t iStreamPath)
   return bReturn;
 }
 
-bool CCECProcessor::SetInactiveView(void)
+bool CCECProcessor::TransmitInactiveSource(void)
 {
   if (!IsRunning())
     return false;
 
   if (!m_logicalAddresses.IsEmpty() && m_busDevices[m_logicalAddresses.primary])
-    return m_busDevices[m_logicalAddresses.primary]->TransmitInactiveView();
+    return m_busDevices[m_logicalAddresses.primary]->TransmitInactiveSource();
   return false;
 }
 
@@ -767,6 +749,7 @@ void CCECProcessor::ParseCommand(cec_command &command)
 cec_logical_addresses CCECProcessor::GetActiveDevices(void)
 {
   cec_logical_addresses addresses;
+  addresses.Clear();
   for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
   {
     if (m_busDevices[iPtr]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
@@ -855,3 +838,31 @@ bool CCECProcessor::SendKeyRelease(cec_logical_address iDestination, bool bWait
 {
   return m_busDevices[iDestination]->SendKeyRelease(bWait);
 }
+
+void *CCECBusScan::Process(void)
+{
+  CCECBusDevice *device(NULL);
+  while (!IsStopped())
+  {
+    for (unsigned int iPtr = 0; iPtr < 15 && !IsStopped(); iPtr++)
+    {
+      device = m_processor->m_busDevices[iPtr];
+      if (device && device->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
+      {
+        if (!IsStopped())
+          device->GetPhysicalAddress(false);
+        Sleep(5);
+
+        if (!IsStopped())
+          device->GetCecVersion();
+        Sleep(5);
+
+        if (!IsStopped())
+          device->GetVendorId();
+        Sleep(5);
+      }
+    }
+    Sleep(1000);
+  }
+  return NULL;
+}