cec: fixed - don't request the PA of devices when handling an incoming CEC command...
[deb_libcec.git] / src / lib / devices / CECBusDevice.cpp
index 3d4a20abfb9405b87c34cf3698515774b04c4a09..51ddfeca68af118baca2ed31bda586c1d0921cee 100644 (file)
@@ -45,23 +45,23 @@ using namespace PLATFORM;
 #define ToString(p) m_processor->ToString(p)
 
 CCECBusDevice::CCECBusDevice(CCECProcessor *processor, cec_logical_address iLogicalAddress, uint16_t iPhysicalAddress) :
-  m_type(CEC_DEVICE_TYPE_RESERVED),
-  m_iPhysicalAddress(iPhysicalAddress),
-  m_iStreamPath(0),
-  m_iLogicalAddress(iLogicalAddress),
-  m_powerStatus(CEC_POWER_STATUS_UNKNOWN),
-  m_processor(processor),
-  m_vendor(CEC_VENDOR_UNKNOWN),
-  m_bReplaceHandler(false),
-  m_menuState(CEC_MENU_STATE_ACTIVATED),
-  m_bActiveSource(false),
-  m_iLastActive(0),
-  m_iLastPowerStateUpdate(0),
-  m_cecVersion(CEC_VERSION_UNKNOWN),
-  m_deviceStatus(CEC_DEVICE_STATUS_UNKNOWN),
-  m_iHandlerUseCount(0),
+  m_type                  (CEC_DEVICE_TYPE_RESERVED),
+  m_iPhysicalAddress      (iPhysicalAddress),
+  m_iStreamPath           (0),
+  m_iLogicalAddress       (iLogicalAddress),
+  m_powerStatus           (CEC_POWER_STATUS_UNKNOWN),
+  m_processor             (processor),
+  m_vendor                (CEC_VENDOR_UNKNOWN),
+  m_bReplaceHandler       (false),
+  m_menuState             (CEC_MENU_STATE_ACTIVATED),
+  m_bActiveSource         (false),
+  m_iLastActive           (0),
+  m_iLastPowerStateUpdate (0),
+  m_cecVersion            (CEC_VERSION_UNKNOWN),
+  m_deviceStatus          (CEC_DEVICE_STATUS_UNKNOWN),
+  m_iHandlerUseCount      (0),
   m_bAwaitingReceiveFailed(false),
-  m_bVendorIdRequested(false)
+  m_bVendorIdRequested    (false)
 {
   m_handler = new CCECCommandHandler(this);
 
@@ -278,21 +278,23 @@ bool CCECBusDevice::RequestOSDName(bool bWaitForResponse /* = true */)
   return bReturn;
 }
 
-uint16_t CCECBusDevice::GetPhysicalAddress(bool bUpdate /* = false */, bool bSuppressPoll /* = false */)
+uint16_t CCECBusDevice::GetPhysicalAddress(bool bSuppressUpdate /* = true */)
 {
-  bool bIsPresent(GetStatus(false, bSuppressPoll) == CEC_DEVICE_STATUS_PRESENT);
-  bool bRequestUpdate(false);
+  if (!bSuppressUpdate)
   {
-    CLockObject lock(m_mutex);
-    bRequestUpdate = bIsPresent &&
-      (m_iPhysicalAddress == 0xFFFF || bUpdate);
-  }
+    bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT);
+    bool bRequestUpdate(false);
+    {
+      CLockObject lock(m_mutex);
+      bRequestUpdate = bIsPresent && m_iPhysicalAddress == 0xFFFF;
+    }
 
-  if (bRequestUpdate)
-  {
-    CheckVendorIdRequested();
-    if (!RequestPhysicalAddress())
-      CLibCEC::AddLog(CEC_LOG_ERROR, "failed to request the physical address");
+    if (bRequestUpdate)
+    {
+      CheckVendorIdRequested();
+      if (!RequestPhysicalAddress())
+        CLibCEC::AddLog(CEC_LOG_ERROR, "failed to request the physical address");
+    }
   }
 
   CLockObject lock(m_mutex);
@@ -487,14 +489,16 @@ cec_bus_device_status CCECBusDevice::GetStatus(bool bForcePoll /* = false */, bo
 //@{
 void CCECBusDevice::SetCecVersion(const cec_version newVersion)
 {
+  if (m_cecVersion != newVersion)
+    CLibCEC::AddLog(CEC_LOG_DEBUG, "%s (%X): CEC version %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(newVersion));
   m_cecVersion = newVersion;
-  CLibCEC::AddLog(CEC_LOG_DEBUG, "%s (%X): CEC version %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(newVersion));
 }
 
 void CCECBusDevice::SetMenuLanguage(const cec_menu_language &language)
 {
   CLockObject lock(m_mutex);
-  if (language.device == m_iLogicalAddress)
+  if (language.device == m_iLogicalAddress &&
+      strcmp(language.language, m_menuLanguage.language))
   {
     CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %s (%X): menu language set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, language.language);
     m_menuLanguage = language;
@@ -566,6 +570,19 @@ bool CCECBusDevice::TryLogicalAddress(void)
   return false;
 }
 
+void CCECBusDevice::ResetDeviceStatus(void)
+{
+  CLockObject lock(m_mutex);
+  SetPowerStatus   (CEC_POWER_STATUS_UNKNOWN);
+  SetVendorId      (CEC_VENDOR_UNKNOWN);
+  SetMenuState     (CEC_MENU_STATE_ACTIVATED);
+  SetCecVersion    (CEC_VERSION_UNKNOWN);
+  SetStreamPath    (0);
+  SetOSDName       (ToString(m_iLogicalAddress));
+  SetInactiveSource();
+  m_iLastActive = 0;
+}
+
 void CCECBusDevice::SetDeviceStatus(const cec_bus_device_status newStatus)
 {
   {
@@ -575,26 +592,20 @@ void CCECBusDevice::SetDeviceStatus(const cec_bus_device_status newStatus)
     case CEC_DEVICE_STATUS_UNKNOWN:
       if (m_deviceStatus != newStatus)
         CLibCEC::AddLog(CEC_LOG_DEBUG, "device status of %s changed into 'unknown'", ToString(m_iLogicalAddress));
-      m_iStreamPath      = 0;
-      m_powerStatus      = CEC_POWER_STATUS_UNKNOWN;
-      m_vendor           = CEC_VENDOR_UNKNOWN;
-      m_menuState        = CEC_MENU_STATE_ACTIVATED;
-      m_bActiveSource    = false;
-      m_iLastActive      = 0;
-      m_cecVersion       = CEC_VERSION_UNKNOWN;
-      m_deviceStatus     = newStatus;
+      ResetDeviceStatus();
+      m_deviceStatus = newStatus;
       break;
     case CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC:
       if (m_deviceStatus != newStatus)
         CLibCEC::AddLog(CEC_LOG_DEBUG, "device status of %s changed into 'handled by libCEC'", ToString(m_iLogicalAddress));
-      m_iStreamPath      = 0;
-      m_powerStatus      = CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON;
-      m_vendor           = CEC_VENDOR_UNKNOWN;
-      m_menuState        = CEC_MENU_STATE_ACTIVATED;
-      m_bActiveSource    = false;
-      m_iLastActive      = 0;
-      m_cecVersion       = CEC_VERSION_1_3A;
-      m_deviceStatus     = newStatus;
+      SetPowerStatus   (CEC_POWER_STATUS_ON);
+      SetVendorId      (CEC_VENDOR_UNKNOWN);
+      SetMenuState     (CEC_MENU_STATE_ACTIVATED);
+      SetCecVersion    (CEC_VERSION_1_3A);
+      SetStreamPath    (0);
+      SetInactiveSource();
+      m_iLastActive   = 0;
+      m_deviceStatus  = newStatus;
       break;
     case CEC_DEVICE_STATUS_PRESENT:
       if (m_deviceStatus != newStatus)
@@ -603,8 +614,11 @@ void CCECBusDevice::SetDeviceStatus(const cec_bus_device_status newStatus)
       break;
     case CEC_DEVICE_STATUS_NOT_PRESENT:
       if (m_deviceStatus != newStatus)
+      {
         CLibCEC::AddLog(CEC_LOG_DEBUG, "device status of %s changed into 'not present'", ToString(m_iLogicalAddress));
-      m_deviceStatus = newStatus;
+        ResetDeviceStatus();
+        m_deviceStatus = newStatus;
+      }
       break;
     }
   }
@@ -632,7 +646,7 @@ void CCECBusDevice::SetStreamPath(uint16_t iNewAddress, uint16_t iOldAddress /*
     m_iStreamPath = iNewAddress;
 
     // suppress polls when searching for a device
-    CCECBusDevice *device = m_processor->GetDeviceByPhysicalAddress(iNewAddress, false, true);
+    CCECBusDevice *device = m_processor->GetDeviceByPhysicalAddress(iNewAddress);
     if (device)
     {
       // if a device is found with the new physical address, mark it as active, which will automatically mark all other devices as inactive
@@ -641,7 +655,7 @@ void CCECBusDevice::SetStreamPath(uint16_t iNewAddress, uint16_t iOldAddress /*
     else
     {
       // try to find the device with the old address, and mark it as inactive when found
-      device = m_processor->GetDeviceByPhysicalAddress(iOldAddress, false, true);
+      device = m_processor->GetDeviceByPhysicalAddress(iOldAddress);
       if (device)
         device->SetInactiveSource();
     }
@@ -744,7 +758,8 @@ bool CCECBusDevice::SetVendorId(uint64_t iVendorId)
     m_vendor = (cec_vendor_id)iVendorId;
   }
 
-  CLibCEC::AddLog(CEC_LOG_DEBUG, "%s (%X): vendor = %s (%06x)", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_vendor), m_vendor);
+  if (bVendorChanged)
+    CLibCEC::AddLog(CEC_LOG_DEBUG, "%s (%X): vendor = %s (%06x)", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_vendor), m_vendor);
 
   return bVendorChanged;
 }