cec: fix venodor id change. fix physical address change
[deb_libcec.git] / src / lib / devices / CECBusDevice.cpp
index 689ff870b8e1d8a73cdeda66e3d847d145f3d1f0..fc44ebddb2b8294b3b956760074db385c0b55ed5 100644 (file)
@@ -40,7 +40,7 @@
 
 using namespace CEC;
 
-#define ToString(p) CCECCommandHandler::ToString(p)
+#define ToString(p) m_processor->ToString(p)
 
 CCECBusDevice::CCECBusDevice(CCECProcessor *processor, cec_logical_address iLogicalAddress, uint16_t iPhysicalAddress) :
   m_type(CEC_DEVICE_TYPE_RESERVED),
@@ -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,43 +85,40 @@ 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;
 }
 
-void CCECBusDevice::PollVendorId(void)
-{
-  CLockObject lock(&m_transmitMutex);
-  if (m_iLastActive > 0 && m_iLogicalAddress != CECDEVICE_BROADCAST &&
-      m_vendor == CEC_VENDOR_UNKNOWN &&
-      GetTimeMs() - m_iLastCommandSent > 5000 &&
-      !m_processor->IsMonitoring())
-  {
-    CStdString strLog;
-    strLog.Format("<< requesting vendor ID of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
-    AddLog(CEC_LOG_NOTICE, strLog);
-    m_iLastCommandSent = GetTimeMs();
-
-    cec_command command;
-    cec_command::Format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
-    if (m_processor->Transmit(command))
-      m_condition.Wait(&m_transmitMutex, 1000);
-  }
-}
-
 bool CCECBusDevice::PowerOn(void)
 {
-   CStdString strLog;
-   strLog.Format("<< powering on '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
-   AddLog(CEC_LOG_DEBUG, strLog.c_str());
+  CStdString strLog;
+  strLog.Format("<< powering on '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
+  AddLog(CEC_LOG_DEBUG, strLog.c_str());
 
-   cec_command command;
-   cec_command::Format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_IMAGE_VIEW_ON);
-   if (m_processor->Transmit(command))
-   {
-     GetPowerStatus();
-     return true;
+  cec_command command;
+  cec_command::Format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_IMAGE_VIEW_ON, m_iTransmitTimeout);
+  if (m_processor->Transmit(command))
+  {
+    {
+      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;
   }
 
   return false;
@@ -133,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);
 }
@@ -143,12 +141,9 @@ bool CCECBusDevice::Standby(void)
 cec_version CCECBusDevice::GetCecVersion(void)
 {
   CLockObject lock(&m_mutex);
-  if (m_cecVersion == CEC_VERSION_UNKNOWN)
-  {
-    lock.Leave();
+  if (GetStatus() == CEC_DEVICE_STATUS_PRESENT &&
+      m_cecVersion == CEC_VERSION_UNKNOWN)
     RequestCecVersion();
-    lock.Lock();
-  }
 
   return m_cecVersion;
 }
@@ -162,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);
@@ -170,6 +165,11 @@ bool CCECBusDevice::RequestCecVersion(void)
   return bReturn;
 }
 
+uint64_t CCECBusDevice::GetLastCommandSent(void) const
+{
+  return GetTimeMs() - m_iLastCommandSent;
+}
+
 const char* CCECBusDevice::GetLogicalAddressName(void) const
 {
   return ToString(m_iLogicalAddress);
@@ -178,12 +178,10 @@ const char* CCECBusDevice::GetLogicalAddressName(void) const
 cec_menu_language &CCECBusDevice::GetMenuLanguage(void)
 {
   CLockObject lock(&m_mutex);
-  if (!strcmp(m_menuLanguage.language, "???"))
-  {
-    lock.Leave();
+  if (GetStatus() == CEC_DEVICE_STATUS_PRESENT &&
+      !strcmp(m_menuLanguage.language, "???"))
     RequestMenuLanguage();
-    lock.Lock();
-  }
+
   return m_menuLanguage;
 }
 
@@ -196,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);
@@ -214,15 +212,71 @@ uint16_t CCECBusDevice::GetMyPhysicalAddress(void) const
   return m_processor->GetPhysicalAddress();
 }
 
-cec_power_status CCECBusDevice::GetPowerStatus(void)
+CStdString CCECBusDevice::GetOSDName(void)
+{
+  CLockObject lock(&m_mutex);
+  if (GetStatus() == CEC_DEVICE_STATUS_PRESENT &&
+      m_strDeviceName.Equals(ToString(m_iLogicalAddress)) &&
+      m_type != CEC_DEVICE_TYPE_TV)
+    RequestOSDName();
+
+  return m_strDeviceName;
+}
+
+bool CCECBusDevice::RequestOSDName(void)
+{
+  bool bReturn(false);
+  if (!MyLogicalAddressContains(m_iLogicalAddress))
+  {
+    CStdString strLog;
+    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, m_iTransmitTimeout);
+    CLockObject lock(&m_transmitMutex);
+    if (m_processor->Transmit(command))
+      bReturn = m_condition.Wait(&m_transmitMutex, 1000);
+  }
+  return bReturn;
+}
+
+uint16_t CCECBusDevice::GetPhysicalAddress(bool bRefresh /* = true */)
 {
   CLockObject lock(&m_mutex);
-  if (m_powerStatus == CEC_POWER_STATUS_UNKNOWN)
+  if (GetStatus() == CEC_DEVICE_STATUS_PRESENT &&
+      (m_iPhysicalAddress == 0xFFFF || bRefresh))
   {
-    lock.Leave();
-    RequestPowerStatus();
-    lock.Lock();
+    if (!RequestPhysicalAddress())
+      AddLog(CEC_LOG_ERROR, "failed to request the physical address");
   }
+
+  return m_iPhysicalAddress;
+}
+
+bool CCECBusDevice::RequestPhysicalAddress(void)
+{
+  bool bReturn(false);
+  if (!MyLogicalAddressContains(m_iLogicalAddress))
+  {
+    CStdString strLog;
+    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, m_iTransmitTimeout);
+    CLockObject lock(&m_transmitMutex);
+    if (m_processor->Transmit(command))
+      bReturn = m_condition.Wait(&m_transmitMutex, 1000);
+  }
+  return bReturn;
+}
+
+cec_power_status CCECBusDevice::GetPowerStatus(void)
+{
+  CLockObject lock(&m_mutex);
+  if (GetStatus() == CEC_DEVICE_STATUS_PRESENT &&
+      m_powerStatus == CEC_POWER_STATUS_UNKNOWN)
+    RequestPowerStatus();
+
   return m_powerStatus;
 }
 
@@ -235,7 +289,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);
@@ -246,12 +300,10 @@ bool CCECBusDevice::RequestPowerStatus(void)
 cec_vendor_id CCECBusDevice::GetVendorId(void)
 {
   CLockObject lock(&m_mutex);
-  if (m_vendor == CEC_VENDOR_UNKNOWN)
-  {
-    lock.Leave();
+  if (GetStatus() == CEC_DEVICE_STATUS_PRESENT &&
+      m_vendor == CEC_VENDOR_UNKNOWN)
     RequestVendorId();
-    lock.Lock();
-  }
+
   return m_vendor;
 }
 
@@ -264,7 +316,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))
@@ -283,15 +335,65 @@ bool CCECBusDevice::MyLogicalAddressContains(cec_logical_address address) const
   return m_processor->HasLogicalAddress(address);
 }
 
-cec_bus_device_status CCECBusDevice::GetStatus(void)
+bool CCECBusDevice::NeedsPoll(void)
 {
-  CLockObject lock(&m_mutex);
-  if (m_deviceStatus == CEC_DEVICE_STATUS_UNKNOWN)
+  bool bSendPoll(false);
+  switch (m_iLogicalAddress)
   {
-    if (m_processor->PollDevice(m_iLogicalAddress))
-      m_deviceStatus = CEC_DEVICE_STATUS_PRESENT;
-    else
-      m_deviceStatus = CEC_DEVICE_STATUS_NOT_PRESENT;
+  case CECDEVICE_PLAYBACKDEVICE3:
+    if (m_processor->m_busDevices[CECDEVICE_PLAYBACKDEVICE2]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
+      bSendPoll = true;
+    break;
+  case CECDEVICE_PLAYBACKDEVICE2:
+    if (m_processor->m_busDevices[CECDEVICE_PLAYBACKDEVICE1]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
+      bSendPoll = true;
+    break;
+  case CECDEVICE_RECORDINGDEVICE3:
+    if (m_processor->m_busDevices[CECDEVICE_RECORDINGDEVICE2]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
+      bSendPoll = true;
+    break;
+  case CECDEVICE_RECORDINGDEVICE2:
+    if (m_processor->m_busDevices[CECDEVICE_RECORDINGDEVICE1]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
+      bSendPoll = true;
+    break;
+  case CECDEVICE_TUNER4:
+    if (m_processor->m_busDevices[CECDEVICE_TUNER3]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
+      bSendPoll = true;
+    break;
+  case CECDEVICE_TUNER3:
+    if (m_processor->m_busDevices[CECDEVICE_TUNER2]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
+      bSendPoll = true;
+    break;
+  case CECDEVICE_TUNER2:
+    if (m_processor->m_busDevices[CECDEVICE_TUNER1]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
+      bSendPoll = true;
+    break;
+  case CECDEVICE_AUDIOSYSTEM:
+  case CECDEVICE_PLAYBACKDEVICE1:
+  case CECDEVICE_RECORDINGDEVICE1:
+  case CECDEVICE_TUNER1:
+  case CECDEVICE_TV:
+    bSendPoll = true;
+    break;
+  default:
+    break;
+  }
+
+  return bSendPoll;
+}
+
+cec_bus_device_status CCECBusDevice::GetStatus(bool bForcePoll /* = false */)
+{
+  CLockObject lock(&m_writeMutex);
+  if (m_deviceStatus == CEC_DEVICE_STATUS_UNKNOWN || bForcePoll)
+  {
+    lock.Leave();
+    bool bPollAcked(false);
+    if (bForcePoll || NeedsPoll())
+      bPollAcked = m_processor->PollDevice(m_iLogicalAddress);
+
+    lock.Lock();
+    m_deviceStatus = bPollAcked ? CEC_DEVICE_STATUS_PRESENT : CEC_DEVICE_STATUS_NOT_PRESENT;
   }
 
   return m_deviceStatus;
@@ -312,7 +414,7 @@ void CCECBusDevice::SetCecVersion(const cec_version newVersion)
 
 void CCECBusDevice::SetMenuLanguage(const cec_menu_language &language)
 {
-  CLockObject lock(&m_mutex);
+  CLockObject lock(&m_writeMutex);
   if (language.device == m_iLogicalAddress)
   {
     CStdString strLog;
@@ -324,7 +426,7 @@ void CCECBusDevice::SetMenuLanguage(const cec_menu_language &language)
 
 void CCECBusDevice::SetOSDName(CStdString strName)
 {
-  CLockObject lock(&m_mutex);
+  CLockObject lock(&m_writeMutex);
   if (m_strDeviceName != strName)
   {
     CStdString strLog;
@@ -336,7 +438,7 @@ void CCECBusDevice::SetOSDName(CStdString strName)
 
 void CCECBusDevice::SetMenuState(const cec_menu_state state)
 {
-  CLockObject lock(&m_mutex);
+  CLockObject lock(&m_writeMutex);
   if (m_menuState != state)
   {
     CStdString strLog;
@@ -348,13 +450,13 @@ void CCECBusDevice::SetMenuState(const cec_menu_state state)
 
 void CCECBusDevice::SetInactiveDevice(void)
 {
-  CLockObject lock(&m_mutex);
+  CLockObject lock(&m_writeMutex);
   m_bActiveSource = false;
 }
 
 void CCECBusDevice::SetActiveDevice(void)
 {
-  CLockObject lock(&m_mutex);
+  CLockObject lock(&m_writeMutex);
 
   for (int iPtr = 0; iPtr < 16; iPtr++)
     if (iPtr != m_iLogicalAddress)
@@ -388,7 +490,7 @@ bool CCECBusDevice::TryLogicalAddress(void)
 
 void CCECBusDevice::SetDeviceStatus(const cec_bus_device_status newStatus)
 {
-  CLockObject lock(&m_mutex);
+  CLockObject lock(&m_writeMutex);
   switch (newStatus)
   {
   case CEC_DEVICE_STATUS_UNKNOWN:
@@ -422,8 +524,8 @@ void CCECBusDevice::SetDeviceStatus(const cec_bus_device_status newStatus)
 
 void CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress)
 {
-  CLockObject lock(&m_mutex);
-  if (iNewAddress > 0)
+  CLockObject lock(&m_writeMutex);
+  if (iNewAddress > 0 && m_iPhysicalAddress != iNewAddress)
   {
     CStdString strLog;
     strLog.Format(">> %s (%X): physical address changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress, iNewAddress);
@@ -435,11 +537,11 @@ void CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress)
 
 void CCECBusDevice::SetStreamPath(uint16_t iNewAddress, uint16_t iOldAddress /* = 0 */)
 {
-  CLockObject lock(&m_mutex);
+  CLockObject lock(&m_writeMutex);
   if (iNewAddress > 0)
   {
     CStdString strLog;
-    strLog.Format(">> %s (%X): stream path changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress, iOldAddress, iNewAddress);
+    strLog.Format(">> %s (%X): stream path changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress, iOldAddress == 0 ? m_iStreamPath : iOldAddress, iNewAddress);
     AddLog(CEC_LOG_DEBUG, strLog.c_str());
 
     m_iStreamPath = iNewAddress;
@@ -454,7 +556,7 @@ void CCECBusDevice::SetStreamPath(uint16_t iNewAddress, uint16_t iOldAddress /*
 
 void CCECBusDevice::SetPowerStatus(const cec_power_status powerStatus)
 {
-  CLockObject lock(&m_mutex);
+  CLockObject lock(&m_writeMutex);
   if (m_powerStatus != powerStatus)
   {
     CStdString strLog;
@@ -464,47 +566,44 @@ void CCECBusDevice::SetPowerStatus(const cec_power_status powerStatus)
   }
 }
 
-void CCECBusDevice::SetVendorId(uint64_t iVendorId)
+void CCECBusDevice::SetVendorId(uint64_t iVendorId, bool bInitHandler /* = true */)
 {
+  bool bVendorChanged(false);
+
   {
-    CLockObject lock(&m_mutex);
+    CLockObject lock(&m_writeMutex);
+    bVendorChanged = (m_vendor != (cec_vendor_id)iVendorId);
     m_vendor = (cec_vendor_id)iVendorId;
 
+    if (bVendorChanged)
+      delete m_handler;
+
     switch (iVendorId)
     {
     case CEC_VENDOR_SAMSUNG:
-      if (m_handler->GetVendorId() != CEC_VENDOR_SAMSUNG)
-      {
-        delete m_handler;
+      if (bVendorChanged)
         m_handler = new CANCommandHandler(this);
-      }
       break;
     case CEC_VENDOR_LG:
-      if (m_handler->GetVendorId() != CEC_VENDOR_LG)
-      {
-        delete m_handler;
+      if (bVendorChanged)
         m_handler = new CSLCommandHandler(this);
-      }
       break;
     case CEC_VENDOR_PANASONIC:
-      if (m_handler->GetVendorId() != CEC_VENDOR_PANASONIC)
-      {
-        delete m_handler;
+      if (bVendorChanged)
         m_handler = new CVLCommandHandler(this);
-      }
       break;
     default:
-      if (m_handler->GetVendorId() != CEC_VENDOR_UNKNOWN)
-      {
-        delete m_handler;
+      if (bVendorChanged)
         m_handler = new CCECCommandHandler(this);
-      }
       break;
     }
   }
 
+  if (bVendorChanged && bInitHandler)
+    m_handler->InitHandler();
+
   CStdString strLog;
-  strLog.Format("%s (%X): vendor = %s (%06x)", GetLogicalAddressName(), m_iLogicalAddress, GetVendorName(), m_vendor);
+  strLog.Format("%s (%X): vendor = %s (%06x)", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_vendor), m_vendor);
   m_processor->AddLog(CEC_LOG_DEBUG, strLog.c_str());
 }
 //@}
@@ -513,7 +612,7 @@ void CCECBusDevice::SetVendorId(uint64_t iVendorId)
 //@{
 bool CCECBusDevice::TransmitActiveSource(void)
 {
-  CLockObject lock(&m_mutex);
+  CLockObject lock(&m_writeMutex);
   if (m_powerStatus != CEC_POWER_STATUS_ON)
   {
     CStdString strLog;
@@ -527,7 +626,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));
 
@@ -546,27 +645,27 @@ bool CCECBusDevice::TransmitActiveSource(void)
 
 bool CCECBusDevice::TransmitCECVersion(cec_logical_address dest)
 {
-  CLockObject lock(&m_mutex);
+  CLockObject lock(&m_writeMutex);
   CStdString strLog;
   strLog.Format("<< %s (%X) -> %s (%X): cec version %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString(m_cecVersion));
   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();
   return m_processor->Transmit(command);
 }
 
-bool CCECBusDevice::TransmitInactiveView(void)
+bool CCECBusDevice::TransmitInactiveSource(void)
 {
   CStdString strLog;
-  strLog.Format("<< %s (%X) -> broadcast (F): inactive view", GetLogicalAddressName(), m_iLogicalAddress);
+  strLog.Format("<< %s (%X) -> broadcast (F): inactive source", GetLogicalAddressName(), m_iLogicalAddress);
   AddLog(CEC_LOG_NOTICE, strLog);
 
   cec_command command;
-  cec_command::Format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, 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);
 
@@ -580,7 +679,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);
@@ -588,13 +687,13 @@ bool CCECBusDevice::TransmitMenuState(cec_logical_address dest)
 
 bool CCECBusDevice::TransmitOSDName(cec_logical_address dest)
 {
-  CLockObject lock(&m_mutex);
+  CLockObject lock(&m_writeMutex);
   CStdString strLog;
   strLog.Format("<< %s (%X) -> %s (%X): OSD name '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, m_strDeviceName.c_str());
   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));
 
@@ -604,13 +703,13 @@ bool CCECBusDevice::TransmitOSDName(cec_logical_address dest)
 
 bool CCECBusDevice::TransmitOSDString(cec_logical_address dest, cec_display_control duration, const char *strMessage)
 {
-  CLockObject lock(&m_mutex);
+  CLockObject lock(&m_writeMutex);
   CStdString strLog;
   strLog.Format("<< %s (%X) -> %s (%X): display OSD message '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, strMessage);
   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);
@@ -625,13 +724,17 @@ bool CCECBusDevice::TransmitOSDString(cec_logical_address dest, cec_display_cont
 
 bool CCECBusDevice::TransmitPhysicalAddress(void)
 {
-  CLockObject lock(&m_mutex);
+  CLockObject lock(&m_writeMutex);
+
+  if (m_iPhysicalAddress == 0xffff)
+    return false;
+
   CStdString strLog;
   strLog.Format("<< %s (%X) -> broadcast (F): physical adddress %4x", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress);
   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));
@@ -646,28 +749,37 @@ bool CCECBusDevice::TransmitPoll(cec_logical_address dest)
   if (dest == CECDEVICE_UNKNOWN)
     dest = m_iLogicalAddress;
 
+  CLockObject lock(&m_writeMutex);
+
   CStdString strLog;
   strLog.Format("<< %s (%X) -> %s (%X): POLL", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest);
   AddLog(CEC_LOG_NOTICE, strLog.c_str());
 
   cec_command command;
-  cec_command::Format(command, m_iLogicalAddress, dest, CEC_OPCODE_NONE);
-  CLockObject lock(&m_transmitMutex);
+  cec_command::Format(command, m_iLogicalAddress, dest, CEC_OPCODE_NONE, m_iTransmitTimeout);
+  command.retries = 0;
+
+  {
+    CLockObject lock(&m_transmitMutex);
+    bReturn = m_processor->Transmit(command);
+  }
 
-  bReturn = m_processor->Transmit(command);
   AddLog(CEC_LOG_DEBUG, bReturn ? ">> POLL sent" : ">> POLL not sent");
+  if (bReturn)
+    m_iLastActive = GetTimeMs();
+
   return bReturn;
 }
 
 bool CCECBusDevice::TransmitPowerState(cec_logical_address dest)
 {
-  CLockObject lock(&m_mutex);
+  CLockObject lock(&m_writeMutex);
   CStdString strLog;
   strLog.Format("<< %s (%X) -> %s (%X): %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString(m_powerStatus));
   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();
@@ -676,7 +788,7 @@ bool CCECBusDevice::TransmitPowerState(cec_logical_address dest)
 
 bool CCECBusDevice::TransmitVendorID(cec_logical_address dest)
 {
-  CLockObject lock(&m_mutex);
+  CLockObject lock(&m_writeMutex);
   if (m_vendor == CEC_VENDOR_UNKNOWN)
   {
     CStdString strLog;
@@ -694,7 +806,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));
@@ -704,4 +816,39 @@ bool CCECBusDevice::TransmitVendorID(cec_logical_address dest)
     return m_processor->Transmit(command);
   }
 }
+
+bool CCECBusDevice::SendKeypress(cec_user_control_code key, bool bWait /* = false */)
+{
+  CLockObject lock(&m_transmitMutex);
+  cec_command command;
+  cec_command::Format(command, m_processor->GetLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_USER_CONTROL_PRESSED, m_iTransmitTimeout);
+  command.parameters.PushBack((uint8_t)key);
+
+  if (bWait)
+  {
+    if (m_processor->Transmit(command))
+      return m_condition.Wait(&m_transmitMutex, 1000);
+    return false;
+  }
+
+  return m_processor->Transmit(command);
+}
+
+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, m_iTransmitTimeout);
+
+  if (bWait)
+  {
+    if (m_processor->Transmit(command))
+      return m_condition.Wait(&m_transmitMutex, 1000);
+    return false;
+  }
+  else
+  {
+    return m_processor->Transmit(command);
+  }
+}
 //@}