cec: added SetHDMIPort()/cec_set_hdmi_port(). devices are now detected on load and...
[deb_libcec.git] / src / lib / devices / CECBusDevice.cpp
index 42aa02c6644b480c2c3e9b9f889423b1f42930fc..d5a0be6aef11dcb76351ad0bea58eaf4e3eb408a 100644 (file)
@@ -83,6 +83,8 @@ bool CCECBusDevice::HandleCommand(const cec_command &command)
   CLockObject lock(&m_transmitMutex);
   m_iLastActive = GetTimeMs();
   m_handler->HandleCommand(command);
+  if (m_deviceStatus != CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC)
+    m_deviceStatus = CEC_DEVICE_STATUS_PRESENT;
   m_condition.Signal();
   return true;
 }
@@ -212,6 +214,40 @@ uint16_t CCECBusDevice::GetMyPhysicalAddress(void) const
   return m_processor->GetPhysicalAddress();
 }
 
+uint16_t CCECBusDevice::GetPhysicalAddress(bool bRefresh /* = true */)
+{
+  if (GetStatus() == CEC_DEVICE_STATUS_PRESENT)
+  {
+    CLockObject lock(&m_mutex);
+    if (m_iPhysicalAddress == 0xFFFF || bRefresh)
+    {
+      lock.Leave();
+      RequestPhysicalAddress();
+      lock.Lock();
+    }
+  }
+
+  CLockObject lock(&m_mutex);
+  return m_iPhysicalAddress;
+}
+
+bool CCECBusDevice::RequestPhysicalAddress(void)
+{
+  bool bReturn(false);
+  if (!MyLogicalAddressContains(m_iLogicalAddress))
+  {
+    CStdString strLog;
+    strLog.Format("<< requesting physical address of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
+    AddLog(CEC_LOG_NOTICE, strLog);
+    cec_command command;
+    cec_command::Format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS);
+    CLockObject lock(&m_transmitMutex);
+    if (m_processor->Transmit(command))
+      bReturn = m_condition.Wait(&m_transmitMutex, 1000);
+  }
+  return bReturn;
+}
+
 cec_power_status CCECBusDevice::GetPowerStatus(void)
 {
   CLockObject lock(&m_mutex);
@@ -421,7 +457,7 @@ void CCECBusDevice::SetDeviceStatus(const cec_bus_device_status newStatus)
 void CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress)
 {
   CLockObject lock(&m_mutex);
-  if (iNewAddress > 0)
+  if (iNewAddress > 0 && m_iPhysicalAddress != iNewAddress)
   {
     CStdString strLog;
     strLog.Format(">> %s (%X): physical address changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress, iNewAddress);
@@ -437,7 +473,7 @@ void CCECBusDevice::SetStreamPath(uint16_t iNewAddress, uint16_t iOldAddress /*
   if (iNewAddress > 0)
   {
     CStdString strLog;
-    strLog.Format(">> %s (%X): stream path changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress, iOldAddress, iNewAddress);
+    strLog.Format(">> %s (%X): stream path changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress, iOldAddress == 0 ? m_iStreamPath : iOldAddress, iNewAddress);
     AddLog(CEC_LOG_DEBUG, strLog.c_str());
 
     m_iStreamPath = iNewAddress;
@@ -650,10 +686,20 @@ bool CCECBusDevice::TransmitPoll(cec_logical_address dest)
 
   cec_command command;
   cec_command::Format(command, m_iLogicalAddress, dest, CEC_OPCODE_NONE);
-  CLockObject lock(&m_transmitMutex);
 
-  bReturn = m_processor->Transmit(command);
+  {
+    CLockObject lock(&m_transmitMutex);
+    bReturn = m_processor->Transmit(command);
+  }
+
   AddLog(CEC_LOG_DEBUG, bReturn ? ">> POLL sent" : ">> POLL not sent");
+
+  if (bReturn)
+  {
+    CLockObject lock(&m_mutex);
+    m_iLastActive = GetTimeMs();
+  }
+
   return bReturn;
 }