cec: retry 'activate source' every 10 seconds if it failed
authorLars Op den Kamp <lars@opdenkamp.eu>
Thu, 31 May 2012 21:06:42 +0000 (23:06 +0200)
committerLars Op den Kamp <lars@opdenkamp.eu>
Thu, 31 May 2012 21:09:29 +0000 (23:09 +0200)
src/lib/CECProcessor.cpp
src/lib/CECProcessor.h
src/lib/implementations/CECCommandHandler.cpp
src/lib/implementations/CECCommandHandler.h

index 11076ecb78d191dfabdb80341a0a124af90be085..fe40990553d2a4ea2dc278e70cda437262f2c666 100644 (file)
@@ -51,6 +51,7 @@ using namespace std;
 using namespace PLATFORM;
 
 #define CEC_PROCESSOR_SIGNAL_WAIT_TIME 1000
+#define ACTIVE_SOURCE_CHECK_TIMEOUT    10000
 
 #define ToString(x) CCECTypeUtils::ToString(x)
 
@@ -200,6 +201,19 @@ void CCECProcessor::ReplaceHandlers(void)
     it->second->ReplaceHandler(true);
 }
 
+void CCECProcessor::CheckPendingActiveSource(void)
+{
+  if (!CECInitialised())
+    return;
+
+  // check each device
+  for (CECDEVICEMAP::iterator it = m_busDevices->Begin(); it != m_busDevices->End(); it++)
+  {
+    if (it->second->GetHandler()->ActiveSourcePending())
+      it->second->ActivateSource();
+  }
+}
+
 bool CCECProcessor::OnCommandReceived(const cec_command &command)
 {
   return m_inBuffer.Push(command);
@@ -210,6 +224,7 @@ void *CCECProcessor::Process(void)
   m_libcec->AddLog(CEC_LOG_DEBUG, "processor thread started");
 
   cec_command command;
+  CTimeout activeSourceCheck(ACTIVE_SOURCE_CHECK_TIMEOUT);
 
   // as long as we're not being stopped and the connection is open
   while (!IsStopped() && m_communication->IsOpen())
@@ -225,6 +240,13 @@ void *CCECProcessor::Process(void)
 
       // check if we need to replace handlers
       ReplaceHandlers();
+
+      // check whether we need to activate a source, if it failed before
+      if (activeSourceCheck.TimeLeft() == 0)
+      {
+        CheckPendingActiveSource();
+        activeSourceCheck.Init(ACTIVE_SOURCE_CHECK_TIMEOUT);
+      }
     }
   }
 
index 5f496d2631558506045e645ecf450e4ba37c5995..04d84f55ee2f5edab2997a5aec3d51a5df25d316 100644 (file)
@@ -134,6 +134,7 @@ namespace CEC
       void SetCECInitialised(bool bSetTo = true);
 
       void ReplaceHandlers(void);
+      void CheckPendingActiveSource(void);
       bool PhysicalAddressInUse(uint16_t iPhysicalAddress);
       bool SetAckMask(uint16_t iMask);
 
index 6ffc4ac980db71cdfbdbd42cfd577084426e0e00..3627c4e38a1e5421f4fb8fc93730a30d34b13f39 100644 (file)
@@ -56,7 +56,8 @@ 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)
 {
 }
 
@@ -1056,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->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;
@@ -1075,3 +1087,9 @@ void CCECCommandHandler::SignalOpcode(cec_opcode opcode)
 {
   m_waitForResponse->Received(opcode);
 }
+
+bool CCECCommandHandler::ActiveSourcePending(void)
+{
+  CLockObject lock(m_mutex);
+  return m_bActiveSourcePending;
+}
index 1bced84ff8fc469b17a004aa70891233913a1f78..6e0121fdee49886dc4b3db09b47465d8175aa9e6 100644 (file)
@@ -162,6 +162,8 @@ namespace CEC
 
     virtual void SignalOpcode(cec_opcode opcode);
 
+    virtual bool ActiveSourcePending(void);
+
   protected:
     virtual bool HandleActiveSource(const cec_command &command);
     virtual bool HandleDeckControl(const cec_command &command);
@@ -218,5 +220,7 @@ namespace CEC
     bool                                  m_bOPTSendDeckStatusUpdateOnActiveSource;
     cec_vendor_id                         m_vendorId;
     CWaitForResponse                     *m_waitForResponse;
+    bool                                  m_bActiveSourcePending;
+    PLATFORM::CMutex                      m_mutex;
   };
 };