cec: added GetActiveDevices()/cec_get_active_devices(), IsActiveDevice()/cec_is_activ...
[deb_libcec.git] / src / lib / CECProcessor.cpp
index a7ff64ca09ea8c42357242b5125c6615b389d14d..dfd0cc1cb1b64a4bdd9e330aba2453a2ee597fa1 100644 (file)
@@ -137,70 +137,56 @@ bool CCECProcessor::Start(void)
   return false;
 }
 
-bool CCECProcessor::TryLogicalAddress(cec_logical_address address, unsigned int iIndex)
+bool CCECProcessor::TryLogicalAddress(cec_logical_address address)
 {
-  const char *strLabel = CCECCommandHandler::ToString(address);
-  CStdString strLog;
-  strLog.Format("trying logical address '%s'", strLabel);
-  AddLog(CEC_LOG_DEBUG, strLog);
-
-  SetAckMask(0x1 << address);
-  if (!m_busDevices[address]->TransmitPoll(address))
+  if (m_busDevices[address]->TryLogicalAddress())
   {
-    strLog.Format("using logical address '%s'", strLabel);
-    AddLog(CEC_LOG_NOTICE, strLog);
-
     /* only set our OSD name and active source for the primary device */
     if (m_logicalAddresses.IsEmpty())
     {
       m_busDevices[address]->m_strDeviceName = m_strDeviceName;
       m_busDevices[address]->m_bActiveSource = true;
     }
-    m_busDevices[address]->m_powerStatus = CEC_POWER_STATUS_STANDBY;
-    m_busDevices[address]->m_cecVersion =  CEC_VERSION_1_3A;
-
     m_logicalAddresses.Set(address);
 
     // TODO
-    m_busDevices[address]->SetPhysicalAddress((uint16_t)CEC_DEFAULT_PHYSICAL_ADDRESS + ((uint16_t)iIndex * 0x100));
+    m_busDevices[address]->SetPhysicalAddress((uint16_t)CEC_DEFAULT_PHYSICAL_ADDRESS);
 
     return true;
   }
 
-  strLog.Format("logical address '%s' already taken", strLabel);
-  AddLog(CEC_LOG_DEBUG, strLog);
   return false;
 }
 
-bool CCECProcessor::FindLogicalAddressRecordingDevice(unsigned int iIndex)
+bool CCECProcessor::FindLogicalAddressRecordingDevice(void)
 {
   AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'recording device'");
-  return TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1, iIndex) ||
-      TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2, iIndex) ||
-      TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3, iIndex);
+  return TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1) ||
+      TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2) ||
+      TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3);
 }
 
-bool CCECProcessor::FindLogicalAddressTuner(unsigned int iIndex)
+bool CCECProcessor::FindLogicalAddressTuner(void)
 {
   AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'tuner'");
-  return TryLogicalAddress(CECDEVICE_TUNER1, iIndex) ||
-      TryLogicalAddress(CECDEVICE_TUNER2, iIndex) ||
-      TryLogicalAddress(CECDEVICE_TUNER3, iIndex) ||
-      TryLogicalAddress(CECDEVICE_TUNER4, iIndex);
+  return TryLogicalAddress(CECDEVICE_TUNER1) ||
+      TryLogicalAddress(CECDEVICE_TUNER2) ||
+      TryLogicalAddress(CECDEVICE_TUNER3) ||
+      TryLogicalAddress(CECDEVICE_TUNER4);
 }
 
-bool CCECProcessor::FindLogicalAddressPlaybackDevice(unsigned int iIndex)
+bool CCECProcessor::FindLogicalAddressPlaybackDevice(void)
 {
   AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'playback device'");
-  return TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1, iIndex) ||
-      TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2, iIndex) ||
-      TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3, iIndex);
+  return TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1) ||
+      TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2) ||
+      TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3);
 }
 
-bool CCECProcessor::FindLogicalAddressAudioSystem(unsigned int iIndex)
+bool CCECProcessor::FindLogicalAddressAudioSystem(void)
 {
   AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'audio'");
-  return TryLogicalAddress(CECDEVICE_AUDIOSYSTEM, iIndex);
+  return TryLogicalAddress(CECDEVICE_AUDIOSYSTEM);
 }
 
 bool CCECProcessor::FindLogicalAddresses(void)
@@ -218,13 +204,13 @@ bool CCECProcessor::FindLogicalAddresses(void)
     AddLog(CEC_LOG_DEBUG, strLog);
 
     if (m_types.types[iPtr] == CEC_DEVICE_TYPE_RECORDING_DEVICE)
-      bReturn &= FindLogicalAddressRecordingDevice(iPtr);
+      bReturn &= FindLogicalAddressRecordingDevice();
     if (m_types.types[iPtr] == CEC_DEVICE_TYPE_TUNER)
-      bReturn &= FindLogicalAddressTuner(iPtr);
+      bReturn &= FindLogicalAddressTuner();
     if (m_types.types[iPtr] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE)
-      bReturn &= FindLogicalAddressPlaybackDevice(iPtr);
+      bReturn &= FindLogicalAddressPlaybackDevice();
     if (m_types.types[iPtr] == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
-      bReturn &= FindLogicalAddressAudioSystem(iPtr);
+      bReturn &= FindLogicalAddressAudioSystem();
   }
 
   return bReturn;
@@ -450,8 +436,10 @@ bool CCECProcessor::PollDevice(cec_logical_address iAddress)
 
 CCECBusDevice *CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress) const
 {
-  CCECBusDevice *device = NULL;
+  if (m_busDevices[m_logicalAddresses.primary]->GetPhysicalAddress() == iPhysicalAddress)
+    return m_busDevices[m_logicalAddresses.primary];
 
+  CCECBusDevice *device = NULL;
   for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
   {
     if (m_busDevices[iPtr]->GetPhysicalAddress() == iPhysicalAddress)
@@ -659,6 +647,31 @@ void CCECProcessor::ParseCommand(cec_command &command)
     m_busDevices[(uint8_t)command.initiator]->HandleCommand(command);
 }
 
+cec_logical_addresses CCECProcessor::GetActiveDevices(void)
+{
+  cec_logical_addresses addresses;
+  for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
+  {
+    if (m_busDevices[iPtr]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
+      addresses.Set((cec_logical_address) iPtr);
+  }
+  return addresses;
+}
+
+bool CCECProcessor::IsActiveDevice(cec_logical_address address)
+{
+  return m_busDevices[address]->GetStatus() == CEC_DEVICE_STATUS_PRESENT;
+}
+
+bool CCECProcessor::IsActiveDeviceType(cec_device_type type)
+{
+  cec_logical_addresses activeDevices = GetActiveDevices();
+  for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
+    if (activeDevices.IsSet((cec_logical_address) iPtr) && m_busDevices[iPtr]->GetType() == type)
+      return true;
+  return false;
+}
+
 uint16_t CCECProcessor::GetPhysicalAddress(void) const
 {
   if (!m_logicalAddresses.IsEmpty() && m_busDevices[m_logicalAddresses.primary])