cec: add a bRecursive parameter to the constructor of CMutex
[deb_libcec.git] / src / lib / implementations / CECCommandHandler.cpp
index 974273916cda249d242d30209a39554c94a5bd28..23fd550b068ea9a2a4ee15550faadb6dbb2faa6e 100644 (file)
@@ -44,7 +44,8 @@ CCECCommandHandler::CCECCommandHandler(CCECBusDevice *busDevice) :
     m_processor(m_busDevice->GetProcessor()),
     m_iTransmitTimeout(CEC_DEFAULT_TRANSMIT_TIMEOUT),
     m_iTransmitWait(CEC_DEFAULT_TRANSMIT_WAIT),
-    m_iTransmitRetries(CEC_DEFAULT_TRANSMIT_RETRIES)
+    m_iTransmitRetries(CEC_DEFAULT_TRANSMIT_RETRIES),
+    m_bHandlerInited(false)
 {
 }
 
@@ -158,6 +159,9 @@ bool CCECCommandHandler::HandleCommand(const cec_command &command)
   case CEC_OPCODE_TEXT_VIEW_ON:
     HandleTextViewOn(command);
     break;
+  case CEC_OPCODE_FEATURE_ABORT:
+    HandleFeatureAbort(command);
+    break;
   default:
     UnhandledCommand(command);
     bHandled = false;
@@ -166,7 +170,7 @@ bool CCECCommandHandler::HandleCommand(const cec_command &command)
 
   if (bHandled && !bHandlerChanged)
   {
-    CLockObject lock(&m_processor->m_transmitMutex);
+    CLockObject lock(&m_receiveMutex);
     m_condition.Signal();
   }
 
@@ -178,7 +182,7 @@ bool CCECCommandHandler::HandleActiveSource(const cec_command &command)
   if (command.parameters.size == 2)
   {
     uint16_t iAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
-    return m_processor->SetStreamPath(iAddress);
+    return m_processor->SetActiveSource(iAddress);
   }
 
   return true;
@@ -210,7 +214,7 @@ bool CCECCommandHandler::HandleDeviceCecVersion(const cec_command &command)
 
 bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command &command)
 {
-  if (m_busDevice->MyLogicalAddressContains(command.destination))
+  if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination))
     m_processor->TransmitAbort(command.initiator, command.opcode, CEC_ABORT_REASON_REFUSED);
 
   return true;
@@ -221,9 +225,18 @@ bool CCECCommandHandler::HandleDeviceVendorId(const cec_command &command)
   return SetVendorId(command);
 }
 
+bool CCECCommandHandler::HandleFeatureAbort(const cec_command &command)
+{
+  if (command.parameters.size == 2)
+  {
+    m_processor->m_busDevices[command.initiator]->SetUnsupportedFeature((cec_opcode)command.parameters[0]);
+  }
+  return true;
+}
+
 bool CCECCommandHandler::HandleGetCecVersion(const cec_command &command)
 {
-  if (m_busDevice->MyLogicalAddressContains(command.destination))
+  if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination))
   {
     CCECBusDevice *device = GetDevice(command.destination);
     if (device)
@@ -235,7 +248,7 @@ bool CCECCommandHandler::HandleGetCecVersion(const cec_command &command)
 
 bool CCECCommandHandler::HandleGiveAudioStatus(const cec_command &command)
 {
-  if (m_busDevice->MyLogicalAddressContains(command.destination))
+  if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination))
   {
     CCECBusDevice *device = GetDevice(command.destination);
     if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
@@ -247,7 +260,7 @@ bool CCECCommandHandler::HandleGiveAudioStatus(const cec_command &command)
 
 bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command &command)
 {
-  if (m_busDevice->MyLogicalAddressContains(command.destination))
+  if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination))
   {
     CCECBusDevice *device = GetDevice(command.destination);
     if (device && (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE))
@@ -259,7 +272,7 @@ bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command &command)
 
 bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command)
 {
-  if (m_busDevice->MyLogicalAddressContains(command.destination))
+  if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination))
   {
     CCECBusDevice *device = GetDevice(command.destination);
     if (device)
@@ -271,7 +284,7 @@ bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command)
 
 bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command &command)
 {
-  if (m_busDevice->MyLogicalAddressContains(command.destination))
+  if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination))
   {
     CCECBusDevice *device = GetDevice(command.destination);
     if (device)
@@ -283,7 +296,7 @@ bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command &command)
 
 bool CCECCommandHandler::HandleGiveOSDName(const cec_command &command)
 {
-  if (m_busDevice->MyLogicalAddressContains(command.destination))
+  if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination))
   {
     CCECBusDevice *device = GetDevice(command.destination);
     if (device)
@@ -295,11 +308,15 @@ bool CCECCommandHandler::HandleGiveOSDName(const cec_command &command)
 
 bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command &command)
 {
-  if (m_busDevice->MyLogicalAddressContains(command.destination))
+  if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination))
   {
     CCECBusDevice *device = GetDevice(command.destination);
     if (device)
-      return device->TransmitPhysicalAddress();
+    {
+      device->SetActiveSource();
+      return device->TransmitPhysicalAddress() &&
+          device->TransmitActiveSource();
+    }
   }
 
   return false;
@@ -307,7 +324,7 @@ bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command &command)
 
 bool CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command &command)
 {
-  if (m_busDevice->MyLogicalAddressContains(command.destination))
+  if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination))
   {
     CCECBusDevice *device = GetDevice(command.destination);
     if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
@@ -319,13 +336,13 @@ bool CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command &comm
 
 bool CCECCommandHandler::HandleImageViewOn(const cec_command &command)
 {
-  m_processor->SetActiveSource(command.initiator);
+  m_processor->m_busDevices[command.initiator]->SetActiveSource();
   return true;
 }
 
 bool CCECCommandHandler::HandleMenuRequest(const cec_command &command)
 {
-  if (m_busDevice->MyLogicalAddressContains(command.destination))
+  if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination))
   {
     if (command.parameters[0] == CEC_MENU_REQUEST_TYPE_QUERY)
     {
@@ -375,15 +392,19 @@ bool CCECCommandHandler::HandleReportPowerStatus(const cec_command &command)
 
 bool CCECCommandHandler::HandleRequestActiveSource(const cec_command &command)
 {
-  CStdString strLog;
-  strLog.Format(">> %i requests active source", (uint8_t) command.initiator);
-  m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str());
+  if (m_processor->IsStarted())
+  {
+    CStdString strLog;
+    strLog.Format(">> %i requests active source", (uint8_t) command.initiator);
+    m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str());
 
-  vector<CCECBusDevice *> devices;
-  for (int iDevicePtr = (int)GetMyDevices(devices)-1; iDevicePtr >=0; iDevicePtr--)
-    devices[iDevicePtr]->TransmitActiveSource();
+    vector<CCECBusDevice *> devices;
+    for (int iDevicePtr = (int)GetMyDevices(devices)-1; iDevicePtr >=0; iDevicePtr--)
+      devices[iDevicePtr]->TransmitActiveSource();
 
-  return true;
+    return true;
+  }
+  return false;
 }
 
 bool CCECCommandHandler::HandleRoutingChange(const cec_command &command)
@@ -405,7 +426,7 @@ bool CCECCommandHandler::HandleRoutingInformation(const cec_command &command)
   if (command.parameters.size == 2)
   {
     uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
-    m_processor->SetStreamPath(iNewAddress);
+    m_processor->SetActiveSource(iNewAddress);
   }
 
   return false;
@@ -453,21 +474,19 @@ bool CCECCommandHandler::HandleSetOSDName(const cec_command &command)
 
 bool CCECCommandHandler::HandleSetStreamPath(const cec_command &command)
 {
-  if (command.parameters.size >= 2)
+  if (m_processor->IsStarted() && command.parameters.size >= 2)
   {
     uint16_t iStreamAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
     CStdString strLog;
     strLog.Format(">> %i sets stream path to physical address %04x", command.initiator, iStreamAddress);
     m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str());
 
-    if (m_processor->SetStreamPath(iStreamAddress))
+    /* one of the device handled by libCEC has been made active */
+    CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamAddress);
+    if (device && m_busDevice->MyLogicalAddressContains(device->GetLogicalAddress()))
     {
-      CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamAddress);
-      if (device)
-      {
-        return device->TransmitActiveSource() &&
-            device->TransmitMenuState(command.initiator);
-      }
+      device->SetActiveSource();
+      device->TransmitActiveSource();
     }
   }
   return false;
@@ -475,7 +494,7 @@ bool CCECCommandHandler::HandleSetStreamPath(const cec_command &command)
 
 bool CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command &command)
 {
-  if (m_busDevice->MyLogicalAddressContains(command.destination))
+  if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination))
   {
     CCECBusDevice *device = GetDevice(command.destination);
     if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
@@ -487,7 +506,7 @@ bool CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command &command
         uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
         CCECBusDevice *newActiveDevice = GetDeviceByPhysicalAddress(iNewAddress);
         if (newActiveDevice)
-          m_processor->SetActiveSource(newActiveDevice->GetLogicalAddress());
+          newActiveDevice->SetActiveSource();
         return ((CCECAudioSystem *) device)->TransmitSetSystemAudioMode(command.initiator);
       }
       else
@@ -541,13 +560,13 @@ bool CCECCommandHandler::HandleSetSystemAudioMode(const cec_command &command)
 
 bool CCECCommandHandler::HandleTextViewOn(const cec_command &command)
 {
-  m_processor->SetActiveSource(command.initiator);
+  m_processor->m_busDevices[command.initiator]->SetActiveSource();
   return true;
 }
 
 bool CCECCommandHandler::HandleUserControlPressed(const cec_command &command)
 {
-  if (m_busDevice->MyLogicalAddressContains(command.destination) && command.parameters.size > 0)
+  if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination) && command.parameters.size > 0)
   {
     m_processor->AddKey();
 
@@ -562,10 +581,23 @@ bool CCECCommandHandler::HandleUserControlPressed(const cec_command &command)
       {
         CCECBusDevice *device = GetDevice(command.destination);
         if (device)
+        {
           device->SetPowerStatus(CEC_POWER_STATUS_ON);
+          if (device->MyLogicalAddressContains(device->GetLogicalAddress()))
+          {
+            device->SetActiveSource();
+            device->TransmitActiveSource();
+
+            if (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE ||
+                device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE)
+              ((CCECPlaybackDevice *)device)->TransmitDeckStatus(command.initiator);
+          }
+        }
+      }
+      else
+      {
+        m_processor->SetCurrentButton((cec_user_control_code) command.parameters[0]);
       }
-
-      m_processor->SetCurrentButton((cec_user_control_code) command.parameters[0]);
       return true;
     }
   }
@@ -574,7 +606,7 @@ bool CCECCommandHandler::HandleUserControlPressed(const cec_command &command)
 
 bool CCECCommandHandler::HandleUserControlRelease(const cec_command &command)
 {
-  if (m_busDevice->MyLogicalAddressContains(command.destination))
+  if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination))
     m_processor->AddKey();
 
   return true;
@@ -875,36 +907,53 @@ bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator
   return Transmit(command);
 }
 
-bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_user_control_code key)
+bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = true */)
 {
   cec_command command;
   cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_USER_CONTROL_PRESSED);
   command.parameters.PushBack((uint8_t)key);
 
-  return Transmit(command);
+  return Transmit(command, bWait);
 }
 
-bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator, const cec_logical_address iDestination)
+bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWait /* = true */)
 {
   cec_command command;
   cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_USER_CONTROL_RELEASE);
 
-  return Transmit(command);
+  return Transmit(command, bWait);
 }
 
 bool CCECCommandHandler::Transmit(cec_command &command, bool bExpectResponse /* = true */)
 {
   command.transmit_timeout = m_iTransmitTimeout;
-  if (command.retries == CEC_DEFAULT_TRANSMIT_RETRIES)
-    command.retries = m_iTransmitRetries;
 
   CLockObject writeLock(&m_processor->m_transmitMutex);
+  CLockObject receiveLock(&m_receiveMutex);
   if (m_processor->Transmit(command))
   {
     if (bExpectResponse)
-      return m_condition.Wait(&m_processor->m_transmitMutex, m_iTransmitWait);
+      return m_condition.Wait(&m_receiveMutex, m_iTransmitWait);
     return true;
   }
 
   return false;
 }
+
+bool CCECCommandHandler::InitHandler(void)
+{
+  if (m_busDevice->GetLogicalAddress() == CECDEVICE_TV)
+  {
+    CCECBusDevice *primary = m_processor->GetPrimaryDevice();
+    primary->SetPowerStatus(CEC_POWER_STATUS_ON);
+    primary->SetMenuState(CEC_MENU_STATE_ACTIVATED);
+
+    if (m_processor->GetPrimaryDevice()->GetPhysicalAddress(false) != 0xffff)
+    {
+      m_processor->SetActiveSource();
+      primary->TransmitMenuState(m_busDevice->GetLogicalAddress());
+      m_bHandlerInited = true;
+    }
+  }
+  return true;
+}