cec: get the cached power status of a device in CVLCommandHandler::PowerUpEventReceiv...
[deb_libcec.git] / src / lib / implementations / VLCommandHandler.cpp
index 0104e9a240e86baa26799e96ee95f16ac04e3bd9..642b3839a456d6cef69d75bca0092d02a92a71e7 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the libCEC(R) library.
  *
- * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited.  All rights reserved.
+ * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited.  All rights reserved.
  * libCEC(R) is an original work, containing original code.
  *
  * libCEC(R) is a trademark of Pulse-Eight Limited.
 
 #include "VLCommandHandler.h"
 #include "../devices/CECBusDevice.h"
-#include "../util/StdString.h"
+#include "../CECProcessor.h"
+#include "../LibCEC.h"
+#include "../CECClient.h"
+
+#define VL_POWER_CHANGE 0x20
+#define VL_POWERED_UP   0x00
+#define VL_POWERED_DOWN 0x01
 
 using namespace CEC;
+using namespace PLATFORM;
+
+#define LIB_CEC     m_busDevice->GetProcessor()->GetLib()
+#define ToString(p) LIB_CEC->ToString(p)
 
 CVLCommandHandler::CVLCommandHandler(CCECBusDevice *busDevice) :
-    CCECCommandHandler(busDevice)
+    CCECCommandHandler(busDevice),
+    m_bActiveSourcePending(false),
+    m_bPowerUpEventReceived(false)
 {
+  m_vendorId = CEC_VENDOR_PANASONIC;
 }
 
-bool CVLCommandHandler::HandleSetStreamPath(const cec_command &command)
+bool CVLCommandHandler::InitHandler(void)
 {
-  if (command.parameters.size >= 2)
+  CCECBusDevice *primary = m_processor->GetPrimaryDevice();
+  if (primary && primary->GetLogicalAddress() != CECDEVICE_UNREGISTERED)
   {
-    int streamaddr = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
-    CStdString strLog;
-    strLog.Format(">> %i requests stream path from physical address %04x", command.initiator, streamaddr);
-    m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str());
-    if (streamaddr == m_busDevice->GetMyPhysicalAddress())
+    /* use the VL commandhandler for the primary device that is handled by libCEC */
+    if (m_busDevice->GetLogicalAddress() == CECDEVICE_TV)
     {
-      CCECBusDevice *device = GetDevice(command.destination);
-      if (device)
+      if (primary && m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress())
       {
-        return device->BroadcastActiveSource() &&
-               device->BroadcastActiveView() &&
-               device->ReportMenuState(command.initiator);
+        primary->SetVendorId(CEC_VENDOR_PANASONIC);
+        primary->ReplaceHandler(false);
       }
-      return false;
     }
+
+    if (primary->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE)
+      return m_processor->GetPrimaryClient()->ChangeDeviceType(CEC_DEVICE_TYPE_RECORDING_DEVICE, CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
+  }
+
+  return CCECCommandHandler::InitHandler();
+}
+
+int CVLCommandHandler::HandleDeviceVendorCommandWithId(const cec_command &command)
+{
+  if (command.parameters[0] != 0x00 ||
+      command.parameters[1] != 0x80 ||
+      command.parameters[2] != 0x45)
+    return CEC_ABORT_REASON_INVALID_OPERAND;
+
+  if (command.initiator == CECDEVICE_TV &&
+      command.destination == CECDEVICE_BROADCAST &&
+      command.parameters.At(3) == VL_POWER_CHANGE)
+  {
+    if (command.parameters.At(4) == VL_POWERED_UP)
+    {
+      LIB_CEC->AddLog(CEC_LOG_DEBUG, "TV powered up");
+      {
+        CLockObject lock(m_mutex);
+        m_bPowerUpEventReceived = true;
+      }
+      m_processor->TransmitPendingActiveSourceCommands();
+    }
+    else if (command.parameters.At(4) == VL_POWERED_DOWN)
+      LIB_CEC->AddLog(CEC_LOG_DEBUG, "TV powered down");
+    else if (command.parameters.At(4) == VL_POWERED_DOWN)
+      LIB_CEC->AddLog(CEC_LOG_DEBUG, "unknown vendor command");
+
+    return COMMAND_HANDLED;
+  }
+
+  return CCECCommandHandler::HandleDeviceVendorCommandWithId(command);
+}
+
+bool CVLCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress)
+{
+  bool bPowerUpEventReceived(false);
+
+  CCECBusDevice *tv = m_processor->GetDevice(CECDEVICE_TV);
+  if (tv && tv->GetCurrentVendorId() == CEC_VENDOR_PANASONIC)
+  {
+    CVLCommandHandler *handler = static_cast<CVLCommandHandler *>(tv->GetHandler());
+    bPowerUpEventReceived = handler ? handler->PowerUpEventReceived() : false;
+    tv->MarkHandlerReady();
+  }
+
+  if (!bPowerUpEventReceived)
+  {
+    CLockObject lock(m_mutex);
+    // wait until we received the event
+    m_bActiveSourcePending = true;
+    return true;
+  }
+  else
+  {
+    // transmit standard active source message
+    return CCECCommandHandler::TransmitActiveSource(iInitiator, iPhysicalAddress);
+  }
+}
+
+bool CVLCommandHandler::TransmitPendingActiveSourceCommands(void)
+{
+  bool bTransmitCommand(false);
+  {
+    CLockObject lock(m_mutex);
+    bTransmitCommand = m_bActiveSourcePending;
+    m_bActiveSourcePending = false;
+  }
+
+  if (bTransmitCommand)
+  {
+    LIB_CEC->AddLog(CEC_LOG_DEBUG, "transmitting delayed activate source command");
+    return CCECCommandHandler::TransmitActiveSource(m_busDevice->GetLogicalAddress(), m_busDevice->GetCurrentPhysicalAddress()) &&
+        TransmitMenuState(m_busDevice->GetLogicalAddress(), CECDEVICE_TV, CEC_MENU_STATE_ACTIVATED);
   }
   return true;
 }
+
+bool CVLCommandHandler::PowerUpEventReceived(void)
+{
+  {
+    CLockObject lock(m_mutex);
+    if (m_bPowerUpEventReceived)
+      return true;
+  }
+
+  CLockObject lock(m_mutex);
+  m_bPowerUpEventReceived = (m_busDevice->GetCurrentPowerStatus() == CEC_POWER_STATUS_ON);
+  return m_bPowerUpEventReceived;
+}
+
+int CVLCommandHandler::HandleVendorCommand(const cec_command &command)
+{
+  // some vendor command voodoo that will enable more buttons on the remote
+  if (command.parameters.size == 3 &&
+      command.parameters[0] == 0x10 &&
+      command.parameters[1] == 0x01 &&
+      command.parameters[2] == 0x05)
+  {
+    cec_command response;
+    cec_command::Format(response, command.destination, command.initiator, CEC_OPCODE_VENDOR_COMMAND);
+    uint8_t iResponseData[] = {0x10, 0x02, 0xFF, 0xFF, 0x00, 0x05, 0x05, 0x45, 0x55, 0x5c, 0x58, 0x32};
+    response.PushArray(12, iResponseData);
+
+    Transmit(response, true);
+
+    return COMMAND_HANDLED;
+  }
+
+  return CEC_ABORT_REASON_INVALID_OPERAND;
+}
+