cec: fixed - commands that were forwarded to clients used the source address of the...
[deb_libcec.git] / src / lib / CECClient.cpp
index 6df9dbab4288f6898fa1316b3fbf38697a22da2d..35dd415139169200414e7ffc19c0d79c56d4093e 100644 (file)
 #include "CECClient.h"
 #include "CECProcessor.h"
 #include "LibCEC.h"
+#include "CECTypeUtils.h"
 #include "devices/CECPlaybackDevice.h"
 #include "devices/CECAudioSystem.h"
 #include "devices/CECTV.h"
+#include "implementations/CECCommandHandler.h"
 
 using namespace CEC;
 using namespace PLATFORM;
 
 #define LIB_CEC     m_processor->GetLib()
-#define ToString(x) LIB_CEC->ToString(x)
+#define ToString(x) CCECTypeUtils::ToString(x)
 
 CCECClient::CCECClient(CCECProcessor *processor, const libcec_configuration &configuration) :
     m_processor(processor),
@@ -119,9 +121,6 @@ bool CCECClient::OnRegister(void)
   // set the physical address
   SetPhysicalAddress(m_configuration);
 
-  // ensure that we know the vendor id of the TV, so we are using the correct handler
-  m_processor->GetTV()->GetVendorId(GetPrimaryLogicalAdddress());
-
   // make the primary device the active source if the option is set
   if (m_configuration.bActivateSource == 1)
     GetPrimaryDevice()->ActivateSource();
@@ -148,7 +147,7 @@ bool CCECClient::SetHDMIPort(const cec_logical_address iBaseDevice, const uint8_
   }
 
   // don't continue if the connection isn't opened
-  if (!m_processor->IsRunning() && !bForce)
+  if (!m_processor->CECInitialised() && !bForce)
     return true;
 
   // get the PA of the base device
@@ -194,15 +193,19 @@ void CCECClient::ResetPhysicalAddress(void)
 
 void CCECClient::SetPhysicalAddress(const libcec_configuration &configuration)
 {
-  // try to autodetect the address
   bool bPASet(false);
-  if (m_processor->IsRunning() && configuration.bAutodetectAddress == 1)
-    bPASet = AutodetectPhysicalAddress();
 
-  // try to use physical address setting
+  // override the physical address from configuration.iPhysicalAddress if it's set
   if (!bPASet && CLibCEC::IsValidPhysicalAddress(configuration.iPhysicalAddress))
     bPASet = SetPhysicalAddress(configuration.iPhysicalAddress);
 
+  // try to autodetect the address
+  if (!bPASet && m_processor->CECInitialised())
+  {
+    bPASet = AutodetectPhysicalAddress();
+    m_configuration.bAutodetectAddress = bPASet ? 1 : 0;
+  }
+
   // use the base device + hdmi port settings
   if (!bPASet)
     bPASet = SetHDMIPort(configuration.baseDevice, configuration.iHDMIPort);
@@ -234,8 +237,7 @@ bool CCECClient::SetPhysicalAddress(const uint16_t iPhysicalAddress)
   }
 
   // persist the new configuration
-  if (m_processor->IsRunning())
-    m_processor->PersistConfiguration(m_configuration);
+  m_processor->PersistConfiguration(m_configuration);
 
   // set the physical address for each device
   SetDevicePhysicalAddress(iPhysicalAddress);
@@ -246,11 +248,41 @@ bool CCECClient::SetPhysicalAddress(const uint16_t iPhysicalAddress)
   return true;
 }
 
+void CCECClient::SetSupportedDeviceTypes(void)
+{
+  cec_device_type_list types;
+  types.Clear();
+
+  // get the command handler for the tv
+  CCECCommandHandler *tvHandler = m_processor->GetTV()->GetHandler();
+  if (!tvHandler)
+    return;
+
+  // check all device types
+  for (uint8_t iPtr = 0; iPtr < 5; iPtr++)
+  {
+    if (m_configuration.deviceTypes.types[iPtr] == CEC_DEVICE_TYPE_RESERVED)
+      continue;
+
+    // get the supported device type. the handler will replace types it doesn't support by one it does support
+    cec_device_type type = tvHandler->GetReplacementDeviceType(m_configuration.deviceTypes.types[iPtr]);
+    if (!types.IsSet(type))
+      types.Add(type);
+  }
+  m_processor->GetTV()->MarkHandlerReady();
+
+  // set the new type list
+  m_configuration.deviceTypes = types;
+}
+
 bool CCECClient::AllocateLogicalAddresses(void)
 {
   // reset all previous LAs that were set
   m_configuration.logicalAddresses.Clear();
 
+  // get the supported device types from the command handler of the TV
+  SetSupportedDeviceTypes();
+
   // display an error if no device types are set
   if (m_configuration.deviceTypes.IsEmpty())
   {
@@ -467,7 +499,7 @@ bool CCECClient::SendSetActiveSource(const cec_device_type type /* = CEC_DEVICE_
     CCECBusDevice *device = *devices.begin();
 
     // and activate it
-    if (!m_processor->IsRunning())
+    if (!m_processor->CECInitialised())
       device->MarkAsActiveSource();
     else if (device->HasValidPhysicalAddress())
       return device->ActivateSource();
@@ -679,21 +711,19 @@ uint8_t CCECClient::SendMuteAudio(void)
 
 bool CCECClient::SendKeypress(const cec_logical_address iDestination, const cec_user_control_code key, bool bWait /* = true */)
 {
-  CCECBusDevice *device = GetPrimaryDevice();
   CCECBusDevice *dest = m_processor->GetDevice(iDestination);
 
-  return device && dest ?
-      device->TransmitKeypress(GetPrimaryLogicalAdddress(), key, bWait) :
+  return dest ?
+      dest->TransmitKeypress(GetPrimaryLogicalAdddress(), key, bWait) :
       false;
 }
 
 bool CCECClient::SendKeyRelease(const cec_logical_address iDestination, bool bWait /* = true */)
 {
-  CCECBusDevice *device = GetPrimaryDevice();
   CCECBusDevice *dest = m_processor->GetDevice(iDestination);
 
-  return device && dest ?
-      device->TransmitKeyRelease(GetPrimaryLogicalAdddress(), bWait) :
+  return dest ?
+      dest->TransmitKeyRelease(GetPrimaryLogicalAdddress(), bWait) :
       false;
 }
 
@@ -742,12 +772,19 @@ bool CCECClient::GetCurrentConfiguration(libcec_configuration &configuration)
     memcpy(configuration.strDeviceLanguage, m_configuration.strDeviceLanguage, 3);
     configuration.iFirmwareBuildDate      = m_configuration.iFirmwareBuildDate;
   }
+
+  // client version 1.6.3
+  if (configuration.clientVersion >= CEC_CLIENT_VERSION_1_6_3)
+  {
+    configuration.bMonitorOnly            = m_configuration.bMonitorOnly;
+  }
+
   return true;
 }
 
 bool CCECClient::SetConfiguration(const libcec_configuration &configuration)
 {
-  bool bIsRunning(m_processor && m_processor->IsRunning());
+  bool bIsRunning(m_processor && m_processor->CECInitialised());
   CCECBusDevice *primary = bIsRunning ? GetPrimaryDevice() : NULL;
   uint16_t iPA = primary ? primary->GetCurrentPhysicalAddress() : CEC_INVALID_PHYSICAL_ADDRESS;
 
@@ -793,6 +830,12 @@ bool CCECClient::SetConfiguration(const libcec_configuration &configuration)
       memcpy(m_configuration.strDeviceLanguage, configuration.strDeviceLanguage, 3);
     }
 
+    // client version 1.6.3
+    if (configuration.clientVersion >= CEC_CLIENT_VERSION_1_6_3)
+    {
+      m_configuration.bMonitorOnly = configuration.bMonitorOnly;
+    }
+
     // ensure that there is at least 1 device type set
     if (m_configuration.deviceTypes.IsEmpty())
       m_configuration.deviceTypes.Add(CEC_DEVICE_TYPE_RECORDING_DEVICE);
@@ -818,8 +861,7 @@ bool CCECClient::SetConfiguration(const libcec_configuration &configuration)
     SetPhysicalAddress(configuration);
   }
 
-  if (bIsRunning)
-    m_processor->PersistConfiguration(m_configuration);
+  m_processor->PersistConfiguration(m_configuration);
 
   if (!primary)
     primary = GetPrimaryDevice();
@@ -840,14 +882,17 @@ bool CCECClient::SetConfiguration(const libcec_configuration &configuration)
 
 void CCECClient::AddCommand(const cec_command &command)
 {
-  CLockObject lock(m_mutex);
+  if (command.destination == CECDEVICE_BROADCAST || GetLogicalAddresses().IsSet(command.destination))
+  {
+    CLockObject lock(m_mutex);
 
-  LIB_CEC->AddLog(CEC_LOG_NOTICE, ">> %s (%X) -> %s (%X): %s (%2X)", ToString(command.initiator), command.initiator, ToString(command.destination), command.destination, ToString(command.opcode), command.opcode);
+    LIB_CEC->AddLog(CEC_LOG_NOTICE, ">> %s (%X) -> %s (%X): %s (%2X)", ToString(command.initiator), command.initiator, ToString(command.destination), command.destination, ToString(command.opcode), command.opcode);
 
-  if (m_configuration.callbacks && m_configuration.callbacks->CBCecCommand)
-    m_configuration.callbacks->CBCecCommand(m_configuration.callbackParam, command);
-  else if (!m_commandBuffer.Push(command))
-    LIB_CEC->AddLog(CEC_LOG_WARNING, "command buffer is full");
+    if (m_configuration.callbacks && m_configuration.callbacks->CBCecCommand)
+      m_configuration.callbacks->CBCecCommand(m_configuration.callbackParam, command);
+    else if (!m_commandBuffer.Push(command))
+      LIB_CEC->AddLog(CEC_LOG_WARNING, "command buffer is full");
+  }
 }
 
 int CCECClient::MenuStateChanged(const cec_menu_state newState)
@@ -864,6 +909,30 @@ int CCECClient::MenuStateChanged(const cec_menu_state newState)
   return 0;
 }
 
+void CCECClient::SourceActivated(const cec_logical_address logicalAddress)
+{
+  CLockObject lock(m_mutex);
+
+  LIB_CEC->AddLog(CEC_LOG_NOTICE, ">> source activated: %s (%x)", ToString(logicalAddress), logicalAddress);
+
+  if (m_configuration.callbacks &&
+      m_configuration.clientVersion >= CEC_CLIENT_VERSION_1_7_1 &&
+      m_configuration.callbacks->CBCecSourceActivated)
+    m_configuration.callbacks->CBCecSourceActivated(m_configuration.callbackParam, logicalAddress, 1);
+}
+
+void CCECClient::SourceDeactivated(const cec_logical_address logicalAddress)
+{
+  CLockObject lock(m_mutex);
+
+  LIB_CEC->AddLog(CEC_LOG_NOTICE, ">> source deactivated: %s (%x)", ToString(logicalAddress), logicalAddress);
+
+  if (m_configuration.callbacks &&
+      m_configuration.clientVersion >= CEC_CLIENT_VERSION_1_7_1 &&
+      m_configuration.callbacks->CBCecSourceActivated)
+    m_configuration.callbacks->CBCecSourceActivated(m_configuration.callbackParam, logicalAddress, 0);
+}
+
 void CCECClient::Alert(const libcec_alert type, const libcec_parameter &param)
 {
   CLockObject lock(m_mutex);
@@ -1044,7 +1113,7 @@ void CCECClient::SetOSDName(const CStdString &strDeviceName)
   if (primary && !primary->GetCurrentOSDName().Equals(strDeviceName))
   {
     primary->SetOSDName(strDeviceName);
-    if (m_processor && m_processor->IsRunning())
+    if (m_processor && m_processor->CECInitialised())
       primary->TransmitOSDName(CECDEVICE_TV);
   }
 }
@@ -1109,7 +1178,7 @@ bool CCECClient::SetDeviceTypes(const cec_device_type_list &deviceTypes)
 
   {
     CLockObject lock(m_mutex);
-    bNeedReinit = m_processor && m_processor->IsRunning() &&
+    bNeedReinit = m_processor && m_processor->CECInitialised() &&
         (m_configuration.deviceTypes != deviceTypes);
     m_configuration.deviceTypes = deviceTypes;
   }
@@ -1131,7 +1200,10 @@ cec_device_type_list CCECClient::GetDeviceTypes(void)
 bool CCECClient::SetDevicePhysicalAddress(const uint16_t iPhysicalAddress)
 {
   if (!CLibCEC::IsValidPhysicalAddress(iPhysicalAddress))
+  {
+    LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - not setting invalid physical address %04x", __FUNCTION__, iPhysicalAddress);
     return false;
+  }
 
   // reconfigure all devices
   cec_logical_address reactivateSource(CECDEVICE_UNKNOWN);
@@ -1157,7 +1229,7 @@ bool CCECClient::SetDevicePhysicalAddress(const uint16_t iPhysicalAddress)
 
   // reactivate the previous active source
   if (reactivateSource != CECDEVICE_UNKNOWN &&
-      m_processor->IsRunning() &&
+      m_processor->CECInitialised() &&
       IsInitialised())
   {
     CCECBusDevice *device = m_processor->GetDevice(reactivateSource);
@@ -1177,7 +1249,10 @@ bool CCECClient::SwitchMonitoring(bool bEnable)
     if (bEnable)
       return m_processor->UnregisterClient(this);
     else
+    {
+      m_configuration.bMonitorOnly = false;
       return m_processor->RegisterClient(this);
+    }
   }
 
   return false;