cec: increase the transmit timeout for LG devices
[deb_libcec.git] / src / lib / devices / CECBusDevice.cpp
index 439fc05fbf01d27b677eab7f7828f8fabb98bec4..01be1d2254f1398486e1cc1ed00333c1a50be176 100644 (file)
@@ -55,7 +55,8 @@ CCECBusDevice::CCECBusDevice(CCECProcessor *processor, cec_logical_address iLogi
   m_iLastCommandSent(0),
   m_iLastActive(0),
   m_cecVersion(CEC_VERSION_UNKNOWN),
-  m_deviceStatus(CEC_DEVICE_STATUS_UNKNOWN)
+  m_deviceStatus(CEC_DEVICE_STATUS_UNKNOWN),
+  m_iTransmitTimeout(1000)
 {
   m_handler = new CCECCommandHandler(this);
 
@@ -84,7 +85,15 @@ bool CCECBusDevice::HandleCommand(const cec_command &command)
   m_iLastActive = GetTimeMs();
   m_handler->HandleCommand(command);
   if (m_deviceStatus != CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC)
+  {
+    if (m_deviceStatus != CEC_DEVICE_STATUS_PRESENT)
+    {
+      CStdString strLog;
+      strLog.Format("device %s (%x) status changed to present after command %s", GetLogicalAddressName(), (uint8_t)GetLogicalAddress(), ToString(command.opcode));
+      AddLog(CEC_LOG_DEBUG, strLog);
+    }
     m_deviceStatus = CEC_DEVICE_STATUS_PRESENT;
+  }
   m_condition.Signal();
   return true;
 }
@@ -96,10 +105,19 @@ bool CCECBusDevice::PowerOn(void)
   AddLog(CEC_LOG_DEBUG, strLog.c_str());
 
   cec_command command;
-  cec_command::Format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_IMAGE_VIEW_ON);
+  cec_command::Format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_IMAGE_VIEW_ON, m_iTransmitTimeout);
   if (m_processor->Transmit(command))
   {
-    RequestPowerStatus();
+    {
+      CLockObject lock(&m_mutex);
+      m_powerStatus = CEC_POWER_STATUS_UNKNOWN;
+    }
+    cec_power_status status = GetPowerStatus();
+    if (status == CEC_POWER_STATUS_STANDBY || status == CEC_POWER_STATUS_UNKNOWN)
+    {
+      SendKeypress(CEC_USER_CONTROL_CODE_POWER, true);
+      return SendKeyRelease(false);
+    }
     return true;
   }
 
@@ -113,7 +131,7 @@ bool CCECBusDevice::Standby(void)
   AddLog(CEC_LOG_DEBUG, strLog.c_str());
 
   cec_command command;
-  cec_command::Format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_STANDBY);
+  cec_command::Format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_STANDBY, m_iTransmitTimeout);
 
   return m_processor->Transmit(command);
 }
@@ -139,7 +157,7 @@ bool CCECBusDevice::RequestCecVersion(void)
     strLog.Format("<< requesting CEC version of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
     AddLog(CEC_LOG_NOTICE, strLog);
     cec_command command;
-    cec_command::Format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GET_CEC_VERSION);
+    cec_command::Format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GET_CEC_VERSION, m_iTransmitTimeout);
     CLockObject lock(&m_transmitMutex);
     if (m_processor->Transmit(command))
       bReturn = m_condition.Wait(&m_transmitMutex, 1000);
@@ -176,7 +194,7 @@ bool CCECBusDevice::RequestMenuLanguage(void)
     strLog.Format("<< requesting menu language of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
     AddLog(CEC_LOG_NOTICE, strLog);
     cec_command command;
-    cec_command::Format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GET_MENU_LANGUAGE);
+    cec_command::Format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GET_MENU_LANGUAGE, m_iTransmitTimeout);
     CLockObject lock(&m_transmitMutex);
     if (m_processor->Transmit(command))
       bReturn = m_condition.Wait(&m_transmitMutex, 1000);
@@ -213,7 +231,7 @@ bool CCECBusDevice::RequestOSDName(void)
     strLog.Format("<< requesting OSD name of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
     AddLog(CEC_LOG_NOTICE, strLog);
     cec_command command;
-    cec_command::Format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GIVE_OSD_NAME);
+    cec_command::Format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GIVE_OSD_NAME, m_iTransmitTimeout);
     CLockObject lock(&m_transmitMutex);
     if (m_processor->Transmit(command))
       bReturn = m_condition.Wait(&m_transmitMutex, 1000);
@@ -243,7 +261,7 @@ bool CCECBusDevice::RequestPhysicalAddress(void)
     strLog.Format("<< requesting physical address of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
     AddLog(CEC_LOG_NOTICE, strLog);
     cec_command command;
-    cec_command::Format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS);
+    cec_command::Format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS, m_iTransmitTimeout);
     CLockObject lock(&m_transmitMutex);
     if (m_processor->Transmit(command))
       bReturn = m_condition.Wait(&m_transmitMutex, 1000);
@@ -270,7 +288,7 @@ bool CCECBusDevice::RequestPowerStatus(void)
     strLog.Format("<< requesting power status of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
     AddLog(CEC_LOG_NOTICE, strLog);
     cec_command command;
-    cec_command::Format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS);
+    cec_command::Format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS, m_iTransmitTimeout);
     CLockObject lock(&m_transmitMutex);
     if (m_processor->Transmit(command))
       bReturn = m_condition.Wait(&m_transmitMutex, 1000);
@@ -297,7 +315,7 @@ bool CCECBusDevice::RequestVendorId(void)
     strLog.Format("<< requesting vendor ID of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
     AddLog(CEC_LOG_NOTICE, strLog);
     cec_command command;
-    cec_command::Format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
+    cec_command::Format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID, m_iTransmitTimeout);
 
     CLockObject lock(&m_transmitMutex);
     if (m_processor->Transmit(command))
@@ -586,6 +604,8 @@ void CCECBusDevice::SetVendorId(uint64_t iVendorId)
     }
   }
 
+  m_handler->InitHandler();
+
   CStdString strLog;
   strLog.Format("%s (%X): vendor = %s (%06x)", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_vendor), m_vendor);
   m_processor->AddLog(CEC_LOG_DEBUG, strLog.c_str());
@@ -610,7 +630,7 @@ bool CCECBusDevice::TransmitActiveSource(void)
     AddLog(CEC_LOG_NOTICE, strLog);
 
     cec_command command;
-    cec_command::Format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_ACTIVE_SOURCE);
+    cec_command::Format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_ACTIVE_SOURCE, m_iTransmitTimeout);
     command.parameters.PushBack((uint8_t) ((m_iPhysicalAddress >> 8) & 0xFF));
     command.parameters.PushBack((uint8_t) (m_iPhysicalAddress & 0xFF));
 
@@ -635,7 +655,7 @@ bool CCECBusDevice::TransmitCECVersion(cec_logical_address dest)
   AddLog(CEC_LOG_NOTICE, strLog);
 
   cec_command command;
-  cec_command::Format(command, m_iLogicalAddress, dest, CEC_OPCODE_CEC_VERSION);
+  cec_command::Format(command, m_iLogicalAddress, dest, CEC_OPCODE_CEC_VERSION, m_iTransmitTimeout);
   command.parameters.PushBack((uint8_t)m_cecVersion);
 
   lock.Leave();
@@ -649,7 +669,7 @@ bool CCECBusDevice::TransmitInactiveSource(void)
   AddLog(CEC_LOG_NOTICE, strLog);
 
   cec_command command;
-  cec_command::Format(command, m_iLogicalAddress, CECDEVICE_TV, CEC_OPCODE_INACTIVE_SOURCE);
+  cec_command::Format(command, m_iLogicalAddress, CECDEVICE_TV, CEC_OPCODE_INACTIVE_SOURCE, m_iTransmitTimeout);
   command.parameters.PushBack((m_iPhysicalAddress >> 8) & 0xFF);
   command.parameters.PushBack(m_iPhysicalAddress & 0xFF);
 
@@ -663,7 +683,7 @@ bool CCECBusDevice::TransmitMenuState(cec_logical_address dest)
   AddLog(CEC_LOG_NOTICE, strLog);
 
   cec_command command;
-  cec_command::Format(command, m_iLogicalAddress, dest, CEC_OPCODE_MENU_STATUS);
+  cec_command::Format(command, m_iLogicalAddress, dest, CEC_OPCODE_MENU_STATUS, m_iTransmitTimeout);
   command.parameters.PushBack((uint8_t)m_menuState);
 
   return m_processor->Transmit(command);
@@ -677,7 +697,7 @@ bool CCECBusDevice::TransmitOSDName(cec_logical_address dest)
   AddLog(CEC_LOG_NOTICE, strLog.c_str());
 
   cec_command command;
-  cec_command::Format(command, m_iLogicalAddress, dest, CEC_OPCODE_SET_OSD_NAME);
+  cec_command::Format(command, m_iLogicalAddress, dest, CEC_OPCODE_SET_OSD_NAME, m_iTransmitTimeout);
   for (unsigned int iPtr = 0; iPtr < m_strDeviceName.length(); iPtr++)
     command.parameters.PushBack(m_strDeviceName.at(iPtr));
 
@@ -693,7 +713,7 @@ bool CCECBusDevice::TransmitOSDString(cec_logical_address dest, cec_display_cont
   AddLog(CEC_LOG_NOTICE, strLog.c_str());
 
   cec_command command;
-  cec_command::Format(command, m_iLogicalAddress, dest, CEC_OPCODE_SET_OSD_STRING);
+  cec_command::Format(command, m_iLogicalAddress, dest, CEC_OPCODE_SET_OSD_STRING, m_iTransmitTimeout);
   command.parameters.PushBack((uint8_t)duration);
 
   unsigned int iLen = strlen(strMessage);
@@ -714,7 +734,7 @@ bool CCECBusDevice::TransmitPhysicalAddress(void)
   AddLog(CEC_LOG_NOTICE, strLog.c_str());
 
   cec_command command;
-  cec_command::Format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS);
+  cec_command::Format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS, m_iTransmitTimeout);
   command.parameters.PushBack((uint8_t) ((m_iPhysicalAddress >> 8) & 0xFF));
   command.parameters.PushBack((uint8_t) (m_iPhysicalAddress & 0xFF));
   command.parameters.PushBack((uint8_t) (m_type));
@@ -734,7 +754,8 @@ bool CCECBusDevice::TransmitPoll(cec_logical_address dest)
   AddLog(CEC_LOG_NOTICE, strLog.c_str());
 
   cec_command command;
-  cec_command::Format(command, m_iLogicalAddress, dest, CEC_OPCODE_NONE);
+  cec_command::Format(command, m_iLogicalAddress, dest, CEC_OPCODE_NONE, m_iTransmitTimeout);
+  command.retries = 0;
 
   {
     CLockObject lock(&m_transmitMutex);
@@ -760,7 +781,7 @@ bool CCECBusDevice::TransmitPowerState(cec_logical_address dest)
   AddLog(CEC_LOG_NOTICE, strLog.c_str());
 
   cec_command command;
-  cec_command::Format(command, m_iLogicalAddress, dest, CEC_OPCODE_REPORT_POWER_STATUS);
+  cec_command::Format(command, m_iLogicalAddress, dest, CEC_OPCODE_REPORT_POWER_STATUS, m_iTransmitTimeout);
   command.parameters.PushBack((uint8_t) m_powerStatus);
 
   lock.Leave();
@@ -787,7 +808,7 @@ bool CCECBusDevice::TransmitVendorID(cec_logical_address dest)
     AddLog(CEC_LOG_NOTICE, strLog);
 
     cec_command command;
-    cec_command::Format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_DEVICE_VENDOR_ID);
+    cec_command::Format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_DEVICE_VENDOR_ID, m_iTransmitTimeout);
 
     command.parameters.PushBack((uint8_t) (((uint64_t)m_vendor >> 16) & 0xFF));
     command.parameters.PushBack((uint8_t) (((uint64_t)m_vendor >> 8) & 0xFF));
@@ -802,7 +823,7 @@ bool CCECBusDevice::SendKeypress(cec_user_control_code key, bool bWait /* = fals
 {
   CLockObject lock(&m_transmitMutex);
   cec_command command;
-  cec_command::Format(command, m_processor->GetLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_USER_CONTROL_PRESSED);
+  cec_command::Format(command, m_processor->GetLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_USER_CONTROL_PRESSED, m_iTransmitTimeout);
   command.parameters.PushBack((uint8_t)key);
 
   if (bWait)
@@ -819,7 +840,7 @@ bool CCECBusDevice::SendKeyRelease(bool bWait /* = false */)
 {
   CLockObject lock(&m_transmitMutex);
   cec_command command;
-  cec_command::Format(command, m_processor->GetLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_USER_CONTROL_RELEASE);
+  cec_command::Format(command, m_processor->GetLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_USER_CONTROL_RELEASE, m_iTransmitTimeout);
 
   if (bWait)
   {