cec: retry 'activate source' every 10 seconds if it failed
[deb_libcec.git] / src / lib / implementations / CECCommandHandler.cpp
index 2902ac1244d5b79074c78931044caa5de661d48a..3627c4e38a1e5421f4fb8fc93730a30d34b13f39 100644 (file)
 #include "../CECClient.h"
 #include "../CECProcessor.h"
 #include "../LibCEC.h"
+#include "../CECTypeUtils.h"
+#include "../platform/util/util.h"
 
 using namespace CEC;
 using namespace std;
 using namespace PLATFORM;
 
 #define LIB_CEC     m_busDevice->GetProcessor()->GetLib()
-#define ToString(p) LIB_CEC->ToString(p)
+#define ToString(p) CCECTypeUtils::ToString(p)
 
 CCECCommandHandler::CCECCommandHandler(CCECBusDevice *busDevice) :
     m_busDevice(busDevice),
@@ -54,13 +56,14 @@ CCECCommandHandler::CCECCommandHandler(CCECBusDevice *busDevice) :
     m_bHandlerInited(false),
     m_bOPTSendDeckStatusUpdateOnActiveSource(false),
     m_vendorId(CEC_VENDOR_UNKNOWN),
-    m_waitForResponse(new CWaitForResponse)
+    m_waitForResponse(new CWaitForResponse),
+    m_bActiveSourcePending(false)
 {
 }
 
 CCECCommandHandler::~CCECCommandHandler(void)
 {
-  delete m_waitForResponse;
+  DELETE_AND_NULL(m_waitForResponse);
 }
 
 bool CCECCommandHandler::HandleCommand(const cec_command &command)
@@ -221,10 +224,10 @@ bool CCECCommandHandler::HandleActiveSource(const cec_command &command)
 
 bool CCECCommandHandler::HandleDeckControl(const cec_command &command)
 {
-  CCECBusDevice *device = GetDevice(command.destination);
-  if (device && (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE) && command.parameters.size > 0)
+  CCECPlaybackDevice *device = CCECBusDevice::AsPlaybackDevice(GetDevice(command.destination));
+  if (device && command.parameters.size > 0)
   {
-    ((CCECPlaybackDevice *) device)->SetDeckControlMode((cec_deck_control_mode) command.parameters[0]);
+    device->SetDeckControlMode((cec_deck_control_mode) command.parameters[0]);
     return true;
   }
 
@@ -281,9 +284,9 @@ bool CCECCommandHandler::HandleGiveAudioStatus(const cec_command &command)
 {
   if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
   {
-    CCECBusDevice *device = GetDevice(command.destination);
-    if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
-      return ((CCECAudioSystem *) device)->TransmitAudioStatus(command.initiator);
+    CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.destination));
+    if (device)
+      return device->TransmitAudioStatus(command.initiator);
   }
 
   return false;
@@ -293,9 +296,9 @@ bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command &command)
 {
   if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
   {
-    CCECBusDevice *device = GetDevice(command.destination);
-    if (device && (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE))
-      return ((CCECPlaybackDevice *) device)->TransmitDeckStatus(command.initiator);
+    CCECPlaybackDevice *device = CCECBusDevice::AsPlaybackDevice(GetDevice(command.destination));
+    if (device)
+      return device->TransmitDeckStatus(command.initiator);
   }
 
   return false;
@@ -365,9 +368,9 @@ bool CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command &comm
 {
   if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
   {
-    CCECBusDevice *device = GetDevice(command.destination);
-    if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
-      return ((CCECAudioSystem *) device)->TransmitSystemAudioModeStatus(command.initiator);
+    CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.destination));
+    if (device)
+      return device->TransmitSystemAudioModeStatus(command.initiator);
   }
 
   return false;
@@ -417,10 +420,10 @@ bool CCECCommandHandler::HandleReportAudioStatus(const cec_command &command)
 {
   if (command.parameters.size == 1)
   {
-    CCECBusDevice *device = GetDevice(command.initiator);
-    if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
+    CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.initiator));
+    if (device)
     {
-      ((CCECAudioSystem *)device)->SetAudioStatus(command.parameters[0]);
+      device->SetAudioStatus(command.parameters[0]);
       return true;
     }
   }
@@ -550,23 +553,23 @@ bool CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command &command
 {
   if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
   {
-    CCECBusDevice *device = GetDevice(command.destination);
-    if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
+    CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.destination));
+    if (device)
     {
       if (command.parameters.size >= 2)
       {
         device->SetPowerStatus(CEC_POWER_STATUS_ON);
-        ((CCECAudioSystem *) device)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON);
+        device->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON);
         uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
         CCECBusDevice *newActiveDevice = GetDeviceByPhysicalAddress(iNewAddress);
         if (newActiveDevice)
           newActiveDevice->MarkAsActiveSource();
-        return ((CCECAudioSystem *) device)->TransmitSetSystemAudioMode(command.initiator);
+        return device->TransmitSetSystemAudioMode(command.initiator);
       }
       else
       {
-        ((CCECAudioSystem *) device)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF);
-        return ((CCECAudioSystem *) device)->TransmitSetSystemAudioMode(command.initiator);
+        device->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF);
+        return device->TransmitSetSystemAudioMode(command.initiator);
       }
     }
   }
@@ -586,10 +589,10 @@ bool CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command &command)
 {
   if (command.parameters.size == 1)
   {
-    CCECBusDevice *device = GetDevice(command.initiator);
-    if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
+    CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.initiator));
+    if (device)
     {
-      ((CCECAudioSystem *)device)->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]);
+      device->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]);
       return true;
     }
   }
@@ -601,10 +604,10 @@ bool CCECCommandHandler::HandleSetSystemAudioMode(const cec_command &command)
 {
   if (command.parameters.size == 1)
   {
-    CCECBusDevice *device = GetDevice(command.initiator);
-    if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
+    CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.initiator));
+    if (device)
     {
-      ((CCECAudioSystem *)device)->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]);
+      device->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]);
       return true;
     }
   }
@@ -1054,16 +1057,27 @@ bool CCECCommandHandler::ActivateSource(void)
   if (m_busDevice->IsActiveSource() &&
     m_busDevice->IsHandledByLibCEC())
   {
+    {
+      CLockObject lock(m_mutex);
+      m_bActiveSourcePending = false;
+    }
+
     m_busDevice->SetPowerStatus(CEC_POWER_STATUS_ON);
     m_busDevice->SetMenuState(CEC_MENU_STATE_ACTIVATED);
 
-    m_busDevice->TransmitImageViewOn();
-    m_busDevice->TransmitActiveSource();
-    m_busDevice->TransmitMenuState(CECDEVICE_TV);
-    if ((m_busDevice->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE ||
-      m_busDevice->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE) &&
-      SendDeckStatusUpdateOnActiveSource())
-      ((CCECPlaybackDevice *)m_busDevice)->TransmitDeckStatus(CECDEVICE_TV);
+    if (!m_busDevice->TransmitImageViewOn() ||
+        !m_busDevice->TransmitActiveSource() ||
+        !m_busDevice->TransmitMenuState(CECDEVICE_TV))
+    {
+      CLockObject lock(m_mutex);
+      m_bActiveSourcePending = true;
+      return false;
+    }
+
+    CCECPlaybackDevice *playbackDevice = m_busDevice->AsPlaybackDevice();
+    if (playbackDevice && SendDeckStatusUpdateOnActiveSource())
+      playbackDevice->TransmitDeckStatus(CECDEVICE_TV);
+
     m_bHandlerInited = true;
   }
   return true;
@@ -1073,3 +1087,9 @@ void CCECCommandHandler::SignalOpcode(cec_opcode opcode)
 {
   m_waitForResponse->Received(opcode);
 }
+
+bool CCECCommandHandler::ActiveSourcePending(void)
+{
+  CLockObject lock(m_mutex);
+  return m_bActiveSourcePending;
+}