cec: and more LG hacks
[deb_libcec.git] / src / lib / CECProcessor.cpp
index af45f9a653b04843f8f8c284b8e782527a186f49..4df840d02f48e261cac71d371cdfcda8c9b123d4 100644 (file)
@@ -57,7 +57,8 @@ CCECProcessor::CCECProcessor(CLibCEC *controller, const char *strDeviceName, cec
     m_bMonitor(false),
     m_busScan(NULL),
     m_iStandardLineTimeout(3),
-    m_iRetryLineTimeout(3)
+    m_iRetryLineTimeout(3),
+    m_iLastTransmission(0)
 {
   m_communication = new CAdapterCommunication(this);
   m_logicalAddresses.Clear();
@@ -76,7 +77,8 @@ CCECProcessor::CCECProcessor(CLibCEC *controller, const char *strDeviceName, con
     m_controller(controller),
     m_bMonitor(false),
     m_iStandardLineTimeout(3),
-    m_iRetryLineTimeout(3)
+    m_iRetryLineTimeout(3),
+    m_iLastTransmission(0)
 {
   m_communication = new CAdapterCommunication(this);
   m_logicalAddresses.Clear();
@@ -115,6 +117,9 @@ CCECProcessor::CCECProcessor(CLibCEC *controller, const char *strDeviceName, con
 
 CCECProcessor::~CCECProcessor(void)
 {
+  m_bStarted = false;
+  StopThread(false);
+
   if (m_busScan)
   {
     m_busScan->StopThread();
@@ -158,7 +163,7 @@ bool CCECProcessor::Start(const char *strPort, uint16_t iBaudRate /* = 38400 */,
     if (SetAckMask(m_logicalAddresses.AckMask()) &&
         SetHDMIPort(m_iBaseDevice, m_iHDMIPort, true))
     {
-      m_controller->AddLog(CEC_LOG_ERROR, "processor thread started");
+      m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started");
       m_busScan = new CCECBusScan(this);
       m_busScan->CreateThread(true);
       return true;
@@ -263,7 +268,9 @@ void *CCECProcessor::Process(void)
   }
   else
   {
-    m_busDevices[m_logicalAddresses.primary]->TransmitPhysicalAddress();
+    m_busDevices[CECDEVICE_TV]->GetVendorId();
+    m_busDevices[m_logicalAddresses.primary]->TransmitVendorID(CECDEVICE_TV, false);
+
     CLockObject lock(&m_mutex);
     m_bStarted = true;
     m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started");
@@ -314,7 +321,7 @@ bool CCECProcessor::SetActiveSource(cec_device_type type /* = CEC_DEVICE_TYPE_RE
 
   if (type != CEC_DEVICE_TYPE_RESERVED)
   {
-    for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
+    for (uint8_t iPtr = 0; iPtr <= 11; iPtr++)
     {
       if (m_logicalAddresses[iPtr] && m_busDevices[iPtr]->m_type == type)
       {
@@ -324,8 +331,7 @@ bool CCECProcessor::SetActiveSource(cec_device_type type /* = CEC_DEVICE_TYPE_RE
     }
   }
 
-  bReturn = m_busDevices[CECDEVICE_TV]->PowerOn() &&
-      m_busDevices[addr]->TransmitActiveSource() &&
+  bReturn = m_busDevices[addr]->TransmitActiveSource() &&
       SetStreamPath(m_busDevices[addr]->GetPhysicalAddress(false));
 
   if (bReturn && (m_busDevices[addr]->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE ||
@@ -349,11 +355,6 @@ void CCECProcessor::SetRetryLineTimeout(uint8_t iTimeout)
   m_iRetryLineTimeout = iTimeout;
 }
 
-bool CCECProcessor::SetActiveSource(cec_logical_address iAddress)
-{
-  return SetStreamPath(m_busDevices[iAddress]->GetPhysicalAddress(false));
-}
-
 bool CCECProcessor::SetActiveView(void)
 {
   return SetActiveSource(m_types.IsEmpty() ? CEC_DEVICE_TYPE_RESERVED : m_types[0]);
@@ -520,7 +521,10 @@ bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress)
   {
     for (uint8_t iPtr = 0; iPtr < 15; iPtr++)
       if (m_logicalAddresses[iPtr])
+      {
         m_busDevices[iPtr]->SetPhysicalAddress(iPhysicalAddress);
+        m_busDevices[iPtr]->TransmitPhysicalAddress();
+      }
     return SetActiveView();
   }
   return false;
@@ -536,7 +540,7 @@ bool CCECProcessor::SwitchMonitoring(bool bEnable)
     CLockObject lock(&m_mutex);
     m_bMonitor = bEnable;
 
-    if (bEnable)
+    if (!bEnable)
     {
       if (!m_busScan)
       {
@@ -680,8 +684,13 @@ bool CCECProcessor::Transmit(const cec_command &data)
 
   CCECAdapterMessage *output = new CCECAdapterMessage(data);
   bReturn = Transmit(output);
-  delete output;
 
+  /* set to "not present" on failed ack */
+  if (output->is_error() && output->reply == MSGCODE_TRANSMIT_FAILED_ACK &&
+      output->destination() != CECDEVICE_BROADCAST)
+    m_busDevices[output->destination()]->SetDeviceStatus(CEC_DEVICE_STATUS_NOT_PRESENT);
+
+  delete output;
   return bReturn;
 }
 
@@ -690,7 +699,9 @@ bool CCECProcessor::Transmit(CCECAdapterMessage *output)
   bool bReturn(false);
   CLockObject lock(&m_mutex);
   {
+    m_iLastTransmission = GetTimeMs();
     m_communication->SetLineTimeout(m_iStandardLineTimeout);
+    output->tries = 1;
 
     do
     {
@@ -717,7 +728,7 @@ bool CCECProcessor::Transmit(CCECAdapterMessage *output)
       }
       else
         bReturn = true;
-    }while (output->transmit_timeout > 0 && output->needs_retry() && ++output->tries <= output->maxTries);
+    }while (output->transmit_timeout > 0 && output->needs_retry() && ++output->tries < output->maxTries);
   }
 
   m_communication->SetLineTimeout(m_iStandardLineTimeout);
@@ -826,7 +837,7 @@ bool CCECProcessor::ParseMessage(const CCECAdapterMessage &msg)
         m_currentframe.ack         = msg.ack();
         m_currentframe.eom         = msg.eom();
       }
-      if (m_currentframe.ack == true)
+      if (m_currentframe.ack == 0x1)
       {
         m_lastInitiator = m_currentframe.initiator;
         m_busDevices[m_lastInitiator]->GetHandler()->HandlePoll(m_currentframe.initiator, m_currentframe.destination);
@@ -1286,37 +1297,48 @@ const char *CCECProcessor::ToString(const cec_vendor_id vendor)
 void *CCECBusScan::Process(void)
 {
   CCECBusDevice *device(NULL);
-  int iCount(0);
+  uint8_t iCounter(0);
+
   while (!IsStopped())
   {
-    if (iCount == 0)
+    if (++iCounter < 30)
     {
-      for (unsigned int iPtr = 0; iPtr < 15 && !IsStopped(); iPtr++)
+      Sleep(1000);
+      continue;
+    }
+    for (unsigned int iPtr = 0; iPtr <= 11 && !IsStopped(); iPtr++)
+    {
+      device = m_processor->m_busDevices[iPtr];
+      WaitUntilIdle();
+      if (device && device->GetStatus(true) == CEC_DEVICE_STATUS_PRESENT)
       {
-        device = m_processor->m_busDevices[iPtr];
-        if (device && device->GetStatus(true) == CEC_DEVICE_STATUS_PRESENT)
-        {
-          if (!IsStopped())
-          {
-            device->GetVendorId();
-            Sleep(5);
-          }
-          if (!IsStopped())
-          {
-            device->GetPowerStatus(true);
-            Sleep(5);
-          }
-        }
+        WaitUntilIdle();
+        if (!IsStopped())
+          device->GetVendorId();
+
+        WaitUntilIdle();
+        if (!IsStopped())
+          device->GetPowerStatus(true);
       }
     }
-
-    if (++iCount > 60)
-      iCount = 0;
-    Sleep(1000);
   }
+
   return NULL;
 }
 
+void CCECBusScan::WaitUntilIdle(void)
+{
+  if (IsStopped())
+    return;
+
+  int64_t iWaitTime = 3000 - (GetTimeMs() - m_processor->GetLastTransmission());
+  while (iWaitTime > 0)
+  {
+    Sleep(iWaitTime);
+    iWaitTime = 3000 - (GetTimeMs() - m_processor->GetLastTransmission());
+  }
+}
+
 bool CCECProcessor::StartBootloader(void)
 {
   return m_communication->StartBootloader();