/*
* 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 "../implementations/CECCommandHandler.h"
#include "../implementations/SLCommandHandler.h"
#include "../implementations/VLCommandHandler.h"
-#include "../platform/timeutils.h"
+#include "../LibCEC.h"
+#include "../platform/util/timeutils.h"
using namespace CEC;
+using namespace PLATFORM;
+
+#define ToString(p) m_processor->ToString(p)
CCECBusDevice::CCECBusDevice(CCECProcessor *processor, cec_logical_address iLogicalAddress, uint16_t iPhysicalAddress) :
- m_iPhysicalAddress(iPhysicalAddress),
- m_iStreamPath(0),
- m_iLogicalAddress(iLogicalAddress),
- m_powerStatus(CEC_POWER_STATUS_UNKNOWN),
- m_processor(processor),
- m_bMenuActive(true),
- m_bActiveSource(false),
- m_iVendorClass(CEC_VENDOR_UNKNOWN),
- m_iLastCommandSent(0),
- m_iLastActive(0),
- m_cecVersion(CEC_VERSION_UNKNOWN)
+ m_type (CEC_DEVICE_TYPE_RESERVED),
+ m_iPhysicalAddress (iPhysicalAddress),
+ m_iStreamPath (0xFFFF),
+ m_iLogicalAddress (iLogicalAddress),
+ m_powerStatus (CEC_POWER_STATUS_UNKNOWN),
+ m_processor (processor),
+ m_vendor (CEC_VENDOR_UNKNOWN),
+ m_bReplaceHandler (false),
+ m_menuState (CEC_MENU_STATE_ACTIVATED),
+ m_bActiveSource (false),
+ m_iLastActive (0),
+ m_iLastPowerStateUpdate (0),
+ m_cecVersion (CEC_VERSION_UNKNOWN),
+ m_deviceStatus (CEC_DEVICE_STATUS_UNKNOWN),
+ m_iHandlerUseCount (0),
+ m_bAwaitingReceiveFailed(false),
+ m_bVendorIdRequested (false)
{
m_handler = new CCECCommandHandler(this);
m_menuLanguage.language[3] = 0;
m_menuLanguage.device = iLogicalAddress;
- m_vendor.vendor = CEC_VENDOR_UNKNOWN;
- m_type = CEC_DEVICE_TYPE_RESERVED;
- m_strDeviceName = CCECCommandHandler::ToString(m_iLogicalAddress);
+ m_strDeviceName = ToString(m_iLogicalAddress);
}
CCECBusDevice::~CCECBusDevice(void)
{
- m_condition.Broadcast();
delete m_handler;
}
-void CCECBusDevice::AddLog(cec_log_level level, const CStdString &strMessage)
-{
- m_processor->AddLog(level, strMessage);
-}
-
bool CCECBusDevice::HandleCommand(const cec_command &command)
{
- CLockObject lock(&m_mutex);
- m_iLastActive = GetTimeMs();
- m_handler->HandleCommand(command);
- m_condition.Signal();
- return true;
-}
+ bool bHandled(false);
-void CCECBusDevice::PollVendorId(void)
-{
- CLockObject lock(&m_mutex);
- if (m_iLastActive > 0 && m_iLogicalAddress != CECDEVICE_BROADCAST &&
- m_vendor.vendor == CEC_VENDOR_UNKNOWN &&
- GetTimeMs() - m_iLastCommandSent > 5000 &&
- !m_processor->IsMonitoring())
+ /* update "last active" */
{
- CStdString strLog;
- strLog.Format("<< requesting vendor ID of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
- AddLog(CEC_LOG_NOTICE, strLog);
- m_iLastCommandSent = GetTimeMs();
+ CLockObject lock(m_mutex);
+ m_iLastActive = GetTimeMs();
+
+ /* don't call GetStatus() here, just read the value with the mutex locked */
+ if (m_deviceStatus != CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC && command.opcode_set == 1)
+ m_deviceStatus = CEC_DEVICE_STATUS_PRESENT;
- 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_mutex, 1000);
+ MarkBusy();
}
-}
-bool CCECBusDevice::PowerOn(void)
-{
- cec_power_status current = GetPowerStatus();
- if (current != CEC_POWER_STATUS_ON &&
- current != CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON)
+ /* handle the command */
+ bHandled = m_handler->HandleCommand(command);
+
+ /* change status to present */
+ if (bHandled)
{
- CStdString strLog;
- strLog.Format("<< powering on '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
- AddLog(CEC_LOG_DEBUG, strLog.c_str());
+ CLockObject lock(m_mutex);
+ if (m_deviceStatus != CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC)
+ {
+ if (m_deviceStatus != CEC_DEVICE_STATUS_PRESENT)
+ CLibCEC::AddLog(CEC_LOG_DEBUG, "device %s (%x) status changed to present after command %s", GetLogicalAddressName(), (uint8_t)GetLogicalAddress(), ToString(command.opcode));
+ m_deviceStatus = CEC_DEVICE_STATUS_PRESENT;
+ }
+ }
- SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
+ MarkReady();
+ return bHandled;
+}
- cec_command command;
- cec_command::format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_IMAGE_VIEW_ON);
+bool CCECBusDevice::PowerOn(void)
+{
+ bool bReturn(false);
+ GetVendorId(); // ensure that we got the vendor id, because the implementations vary per vendor
- return m_processor->Transmit(command);
+ MarkBusy();
+ cec_power_status currentStatus = GetPowerStatus(false);
+ if (currentStatus != CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON &&
+ currentStatus != CEC_POWER_STATUS_ON)
+ {
+ CLibCEC::AddLog(CEC_LOG_NOTICE, "<< powering on '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
+ if (m_handler->PowerOn(GetMyLogicalAddress(), m_iLogicalAddress))
+ {
+ SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
+ bReturn = true;
+ }
+ }
+ else
+ {
+ CLibCEC::AddLog(CEC_LOG_NOTICE, "'%s' (%X) is already '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(currentStatus));
}
- return true;
+ MarkReady();
+ return bReturn;
}
bool CCECBusDevice::Standby(void)
{
- CStdString strLog;
- strLog.Format("<< putting '%s' (%X) in standby mode", GetLogicalAddressName(), m_iLogicalAddress);
- AddLog(CEC_LOG_DEBUG, strLog.c_str());
-
- cec_command command;
- cec_command::format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_STANDBY);
-
- return m_processor->Transmit(command);
+ CLibCEC::AddLog(CEC_LOG_DEBUG, "<< putting '%s' (%X) in standby mode", GetLogicalAddressName(), m_iLogicalAddress);
+ MarkBusy();
+ bool bReturn = m_handler->TransmitStandby(GetMyLogicalAddress(), m_iLogicalAddress);
+ MarkReady();
+ return bReturn;
}
/** @name Getters */
//@{
-cec_version CCECBusDevice::GetCecVersion(void)
+cec_version CCECBusDevice::GetCecVersion(bool bUpdate /* = false */)
{
- if (m_cecVersion == CEC_VERSION_UNKNOWN)
+ bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT);
+ bool bRequestUpdate(false);
{
- if (!MyLogicalAddressContains(m_iLogicalAddress))
- {
- CStdString strLog;
- 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);
- CLockObject lock(&m_mutex);
- if (m_processor->Transmit(command))
- m_condition.Wait(&m_mutex, 1000);
- }
+ CLockObject lock(m_mutex);
+ bRequestUpdate = bIsPresent &&
+ (bUpdate || m_cecVersion == CEC_VERSION_UNKNOWN);
+ }
+
+ if (bRequestUpdate)
+ {
+ CheckVendorIdRequested();
+ RequestCecVersion();
}
+ CLockObject lock(m_mutex);
return m_cecVersion;
}
+bool CCECBusDevice::RequestActiveSource(bool bWaitForResponse /* = true */)
+{
+ bool bReturn(false);
+
+ if (MyLogicalAddressContains(m_iLogicalAddress))
+ {
+ MarkBusy();
+ CLibCEC::AddLog(CEC_LOG_NOTICE, "<< requesting active source");
+
+ bReturn = m_handler->TransmitRequestActiveSource(GetMyLogicalAddress(), bWaitForResponse);
+ MarkReady();
+ }
+ return bReturn;
+}
+
+bool CCECBusDevice::RequestCecVersion(bool bWaitForResponse /* = true */)
+{
+ bool bReturn(false);
+
+ if (!MyLogicalAddressContains(m_iLogicalAddress) &&
+ !IsUnsupportedFeature(CEC_OPCODE_GET_CEC_VERSION))
+ {
+ MarkBusy();
+ CLibCEC::AddLog(CEC_LOG_NOTICE, "<< requesting CEC version of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
+
+ bReturn = m_handler->TransmitRequestCecVersion(GetMyLogicalAddress(), m_iLogicalAddress, bWaitForResponse);
+ MarkReady();
+ }
+ return bReturn;
+}
+
const char* CCECBusDevice::GetLogicalAddressName(void) const
{
- return CCECCommandHandler::ToString(m_iLogicalAddress);
+ return ToString(m_iLogicalAddress);
}
-cec_menu_language &CCECBusDevice::GetMenuLanguage(void)
+cec_menu_language &CCECBusDevice::GetMenuLanguage(bool bUpdate /* = false */)
{
- if (!strcmp(m_menuLanguage.language, "???"))
+ bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT);
+ bool bRequestUpdate(false);
{
- if (!MyLogicalAddressContains(m_iLogicalAddress))
- {
- CStdString strLog;
- 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);
- CLockObject lock(&m_mutex);
- if (m_processor->Transmit(command))
- m_condition.Wait(&m_mutex, 1000);
- }
+ CLockObject lock(m_mutex);
+ bRequestUpdate = (bIsPresent &&
+ (bUpdate || !strcmp(m_menuLanguage.language, "???")));
+ }
+
+ if (bRequestUpdate)
+ {
+ CheckVendorIdRequested();
+ RequestMenuLanguage();
}
+ CLockObject lock(m_mutex);
return m_menuLanguage;
}
+bool CCECBusDevice::RequestMenuLanguage(bool bWaitForResponse /* = true */)
+{
+ bool bReturn(false);
+
+ if (!MyLogicalAddressContains(m_iLogicalAddress) &&
+ !IsUnsupportedFeature(CEC_OPCODE_GET_MENU_LANGUAGE))
+ {
+ MarkBusy();
+ CLibCEC::AddLog(CEC_LOG_NOTICE, "<< requesting menu language of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
+ bReturn = m_handler->TransmitRequestMenuLanguage(GetMyLogicalAddress(), m_iLogicalAddress, bWaitForResponse);
+ MarkReady();
+ }
+ return bReturn;
+}
+
+cec_menu_state CCECBusDevice::GetMenuState(void)
+{
+ CLockObject lock(m_mutex);
+ return m_menuState;
+}
+
cec_logical_address CCECBusDevice::GetMyLogicalAddress(void) const
{
return m_processor->GetLogicalAddress();
return m_processor->GetPhysicalAddress();
}
-cec_power_status CCECBusDevice::GetPowerStatus(void)
+CStdString CCECBusDevice::GetOSDName(bool bUpdate /* = false */)
+{
+ bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT);
+ bool bRequestUpdate(false);
+ {
+ CLockObject lock(m_mutex);
+ bRequestUpdate = bIsPresent &&
+ (bUpdate || m_strDeviceName.Equals(ToString(m_iLogicalAddress))) &&
+ m_type != CEC_DEVICE_TYPE_TV;
+ }
+
+ if (bRequestUpdate)
+ {
+ CheckVendorIdRequested();
+ RequestOSDName();
+ }
+
+ CLockObject lock(m_mutex);
+ return m_strDeviceName;
+}
+
+bool CCECBusDevice::RequestOSDName(bool bWaitForResponse /* = true */)
+{
+ bool bReturn(false);
+
+ if (!MyLogicalAddressContains(m_iLogicalAddress) &&
+ !IsUnsupportedFeature(CEC_OPCODE_GIVE_OSD_NAME))
+ {
+ MarkBusy();
+ CLibCEC::AddLog(CEC_LOG_NOTICE, "<< requesting OSD name of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
+ bReturn = m_handler->TransmitRequestOSDName(GetMyLogicalAddress(), m_iLogicalAddress, bWaitForResponse);
+ MarkReady();
+ }
+ return bReturn;
+}
+
+uint16_t CCECBusDevice::GetPhysicalAddress(bool bSuppressUpdate /* = true */)
{
- if (m_powerStatus == CEC_POWER_STATUS_UNKNOWN)
+ if (!bSuppressUpdate)
{
- if (!MyLogicalAddressContains(m_iLogicalAddress))
+ bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT);
+ bool bRequestUpdate(false);
+ {
+ CLockObject lock(m_mutex);
+ bRequestUpdate = bIsPresent && m_iPhysicalAddress == 0xFFFF;
+ }
+
+ if (bRequestUpdate)
{
- CStdString strLog;
- 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);
- CLockObject lock(&m_mutex);
- if (m_processor->Transmit(command))
- m_condition.Wait(&m_mutex, 1000);
+ CheckVendorIdRequested();
+ if (!RequestPhysicalAddress())
+ CLibCEC::AddLog(CEC_LOG_ERROR, "failed to request the physical address");
}
}
+ CLockObject lock(m_mutex);
+ return m_iPhysicalAddress;
+}
+
+bool CCECBusDevice::RequestPhysicalAddress(bool bWaitForResponse /* = true */)
+{
+ bool bReturn(false);
+
+ if (!MyLogicalAddressContains(m_iLogicalAddress))
+ {
+ MarkBusy();
+ CLibCEC::AddLog(CEC_LOG_NOTICE, "<< requesting physical address of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
+ bReturn = m_handler->TransmitRequestPhysicalAddress(GetMyLogicalAddress(), m_iLogicalAddress, bWaitForResponse);
+ MarkReady();
+ }
+ return bReturn;
+}
+
+cec_power_status CCECBusDevice::GetPowerStatus(bool bUpdate /* = false */)
+{
+ bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT);
+ bool bRequestUpdate(false);
+ {
+ CLockObject lock(m_mutex);
+ bRequestUpdate = (bIsPresent &&
+ (bUpdate || m_powerStatus == CEC_POWER_STATUS_UNKNOWN ||
+ m_powerStatus == CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON ||
+ m_powerStatus == CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY ||
+ GetTimeMs() - m_iLastPowerStateUpdate >= CEC_POWER_STATE_REFRESH_TIME));
+ }
+
+ if (bRequestUpdate)
+ {
+ CheckVendorIdRequested();
+ RequestPowerStatus();
+ }
+
+ CLockObject lock(m_mutex);
return m_powerStatus;
}
-const cec_vendor &CCECBusDevice::GetVendor(void)
+bool CCECBusDevice::RequestPowerStatus(bool bWaitForResponse /* = true */)
+{
+ bool bReturn(false);
+
+ if (!MyLogicalAddressContains(m_iLogicalAddress) &&
+ !IsUnsupportedFeature(CEC_OPCODE_GIVE_DEVICE_POWER_STATUS))
+ {
+ MarkBusy();
+ CLibCEC::AddLog(CEC_LOG_NOTICE, "<< requesting power status of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
+ bReturn = m_handler->TransmitRequestPowerStatus(GetMyLogicalAddress(), m_iLogicalAddress, bWaitForResponse);
+ MarkReady();
+ }
+ return bReturn;
+}
+
+cec_vendor_id CCECBusDevice::GetVendorId(bool bUpdate /* = false */)
{
- if (m_vendor.vendor == CEC_VENDOR_UNKNOWN)
+ bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT);
+ bool bRequestUpdate(false);
{
- if (!MyLogicalAddressContains(m_iLogicalAddress))
- {
- CStdString strLog;
- 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);
- CLockObject lock(&m_mutex);
-
- if (m_processor->Transmit(command))
- m_condition.Wait(&m_mutex, 1000);
- }
+ CLockObject lock(m_mutex);
+ bRequestUpdate = (bIsPresent &&
+ (bUpdate || m_vendor == CEC_VENDOR_UNKNOWN));
}
+ if (bRequestUpdate)
+ RequestVendorId();
+
+ CLockObject lock(m_mutex);
return m_vendor;
}
-bool CCECBusDevice::MyLogicalAddressContains(cec_logical_address address) const
+bool CCECBusDevice::RequestVendorId(bool bWaitForResponse /* = true */)
{
- return m_processor->HasLogicalAddress(address);
+ bool bReturn(false);
+
+ if (!MyLogicalAddressContains(m_iLogicalAddress) && GetMyLogicalAddress() != CECDEVICE_UNKNOWN)
+ {
+ MarkBusy();
+ CLibCEC::AddLog(CEC_LOG_NOTICE, "<< requesting vendor ID of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
+ bReturn = m_handler->TransmitRequestVendorId(GetMyLogicalAddress(), m_iLogicalAddress, bWaitForResponse);
+ MarkReady();
+
+ if (bWaitForResponse)
+ ReplaceHandler(true);
+ }
+ return bReturn;
}
-//@}
+const char *CCECBusDevice::GetVendorName(bool bUpdate /* = false */)
+{
+ return ToString(GetVendorId(bUpdate));
+}
-/** @name Setters */
-//@{
-void CCECBusDevice::SetCecVersion(const cec_version newVersion)
+bool CCECBusDevice::MyLogicalAddressContains(cec_logical_address address) const
{
- CStdString strLog;
- m_cecVersion = newVersion;
+ return m_processor->HasLogicalAddress(address);
+}
- switch (newVersion)
+bool CCECBusDevice::NeedsPoll(void)
+{
+ bool bSendPoll(false);
+ switch (m_iLogicalAddress)
{
- case CEC_VERSION_1_2:
- strLog.Format("%s (%X): CEC version 1.2", GetLogicalAddressName(), m_iLogicalAddress);
+ case CECDEVICE_PLAYBACKDEVICE3:
+ {
+ cec_bus_device_status status = m_processor->m_busDevices[CECDEVICE_PLAYBACKDEVICE2]->GetStatus();
+ bSendPoll = (status == CEC_DEVICE_STATUS_PRESENT || status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC);
+ }
+ break;
+ case CECDEVICE_PLAYBACKDEVICE2:
+ {
+ cec_bus_device_status status = m_processor->m_busDevices[CECDEVICE_PLAYBACKDEVICE1]->GetStatus();
+ bSendPoll = (status == CEC_DEVICE_STATUS_PRESENT || status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC);
+ }
+ break;
+ case CECDEVICE_RECORDINGDEVICE3:
+ {
+ cec_bus_device_status status = m_processor->m_busDevices[CECDEVICE_RECORDINGDEVICE2]->GetStatus();
+ bSendPoll = (status == CEC_DEVICE_STATUS_PRESENT || status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC);
+ }
break;
- case CEC_VERSION_1_2A:
- strLog.Format("%s (%X): CEC version 1.2a", GetLogicalAddressName(), m_iLogicalAddress);
+ case CECDEVICE_RECORDINGDEVICE2:
+ {
+ cec_bus_device_status status = m_processor->m_busDevices[CECDEVICE_RECORDINGDEVICE1]->GetStatus();
+ bSendPoll = (status == CEC_DEVICE_STATUS_PRESENT || status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC);
+ }
+ break;
+ case CECDEVICE_TUNER4:
+ {
+ cec_bus_device_status status = m_processor->m_busDevices[CECDEVICE_TUNER3]->GetStatus();
+ bSendPoll = (status == CEC_DEVICE_STATUS_PRESENT || status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC);
+ }
+ break;
+ case CECDEVICE_TUNER3:
+ {
+ cec_bus_device_status status = m_processor->m_busDevices[CECDEVICE_TUNER2]->GetStatus();
+ bSendPoll = (status == CEC_DEVICE_STATUS_PRESENT || status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC);
+ }
break;
- case CEC_VERSION_1_3:
- strLog.Format("%s (%X): CEC version 1.3", GetLogicalAddressName(), m_iLogicalAddress);
+ case CECDEVICE_TUNER2:
+ {
+ cec_bus_device_status status = m_processor->m_busDevices[CECDEVICE_TUNER1]->GetStatus();
+ bSendPoll = (status == CEC_DEVICE_STATUS_PRESENT || status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC);
+ }
break;
- case CEC_VERSION_1_3A:
- strLog.Format("%s (%X): CEC version 1.3a", GetLogicalAddressName(), m_iLogicalAddress);
+ case CECDEVICE_AUDIOSYSTEM:
+ case CECDEVICE_PLAYBACKDEVICE1:
+ case CECDEVICE_RECORDINGDEVICE1:
+ case CECDEVICE_TUNER1:
+ case CECDEVICE_TV:
+ bSendPoll = true;
break;
default:
- strLog.Format("%s (%X): unknown CEC version", GetLogicalAddressName(), m_iLogicalAddress);
- m_cecVersion = CEC_VERSION_UNKNOWN;
break;
}
- AddLog(CEC_LOG_DEBUG, strLog);
+
+ return bSendPoll;
+}
+
+cec_bus_device_status CCECBusDevice::GetStatus(bool bForcePoll /* = false */, bool bSuppressPoll /* = false */)
+{
+ cec_bus_device_status status(CEC_DEVICE_STATUS_UNKNOWN);
+ bool bNeedsPoll(false);
+
+ {
+ CLockObject lock(m_mutex);
+ status = m_deviceStatus;
+ bNeedsPoll = !bSuppressPoll &&
+ (bForcePoll || m_deviceStatus == CEC_DEVICE_STATUS_UNKNOWN);
+ }
+
+ if (bNeedsPoll)
+ {
+ bool bPollAcked(false);
+ if (bNeedsPoll && NeedsPoll())
+ bPollAcked = m_processor->PollDevice(m_iLogicalAddress);
+
+ status = bPollAcked ? CEC_DEVICE_STATUS_PRESENT : CEC_DEVICE_STATUS_NOT_PRESENT;
+ SetDeviceStatus(status);
+ }
+
+ return status;
+}
+
+//@}
+
+/** @name Setters */
+//@{
+void CCECBusDevice::SetCecVersion(const cec_version newVersion)
+{
+ if (m_cecVersion != newVersion)
+ CLibCEC::AddLog(CEC_LOG_DEBUG, "%s (%X): CEC version %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(newVersion));
+ m_cecVersion = newVersion;
}
void CCECBusDevice::SetMenuLanguage(const cec_menu_language &language)
{
- if (language.device == m_iLogicalAddress)
+ CLockObject lock(m_mutex);
+ if (language.device == m_iLogicalAddress &&
+ strcmp(language.language, m_menuLanguage.language))
{
- CStdString strLog;
- strLog.Format(">> %s (%X): menu language set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, language.language);
- m_processor->AddLog(CEC_LOG_DEBUG, strLog);
+ CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %s (%X): menu language set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, language.language);
m_menuLanguage = language;
}
}
-void CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress)
+void CCECBusDevice::SetOSDName(CStdString strName)
{
- if (iNewAddress > 0)
+ CLockObject lock(m_mutex);
+ if (m_strDeviceName != strName)
{
- CStdString strLog;
- strLog.Format(">> %s (%X): physical address changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress, iNewAddress);
- AddLog(CEC_LOG_DEBUG, strLog.c_str());
+ CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %s (%X): osd name set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, strName.c_str());
+ m_strDeviceName = strName;
+ }
+}
- m_iPhysicalAddress = iNewAddress;
+void CCECBusDevice::SetMenuState(const cec_menu_state state)
+{
+ CLockObject lock(m_mutex);
+ if (m_menuState != state)
+ {
+ CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %s (%X): menu state set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_menuState));
+ m_menuState = state;
}
}
-void CCECBusDevice::SetStreamPath(uint16_t iNewAddress, uint16_t iOldAddress /* = 0 */)
+void CCECBusDevice::SetInactiveSource(void)
{
- if (iNewAddress > 0)
{
- CStdString strLog;
- strLog.Format(">> %s (%X): stream path changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress, iOldAddress, iNewAddress);
- AddLog(CEC_LOG_DEBUG, strLog.c_str());
+ CLockObject lock(m_mutex);
+ if (m_bActiveSource)
+ CLibCEC::AddLog(CEC_LOG_DEBUG, "marking %s (%X) as inactive source", GetLogicalAddressName(), m_iLogicalAddress);
+ m_bActiveSource = false;
+ }
+}
+
+void CCECBusDevice::SetActiveSource(void)
+{
+ CLockObject lock(m_mutex);
+ if (!m_bActiveSource)
+ CLibCEC::AddLog(CEC_LOG_DEBUG, "making %s (%x) the active source", GetLogicalAddressName(), m_iLogicalAddress);
+ else
+ CLibCEC::AddLog(CEC_LOG_DEBUG, "%s (%x) was already marked as active source", GetLogicalAddressName(), m_iLogicalAddress);
+ for (int iPtr = 0; iPtr < 16; iPtr++)
+ if (iPtr != m_iLogicalAddress)
+ m_processor->m_busDevices[iPtr]->SetInactiveSource();
+
+ m_bActiveSource = true;
+ SetPowerStatus(CEC_POWER_STATUS_ON);
+}
+
+bool CCECBusDevice::TryLogicalAddress(void)
+{
+ CLibCEC::AddLog(CEC_LOG_DEBUG, "trying logical address '%s'", GetLogicalAddressName());
+
+ m_processor->SetAckMask(0);
+ if (!TransmitPoll(m_iLogicalAddress))
+ {
+ CLibCEC::AddLog(CEC_LOG_NOTICE, "using logical address '%s'", GetLogicalAddressName());
+ SetDeviceStatus(CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC);
+
+ return true;
+ }
+
+ CLibCEC::AddLog(CEC_LOG_DEBUG, "logical address '%s' already taken", GetLogicalAddressName());
+ SetDeviceStatus(CEC_DEVICE_STATUS_PRESENT);
+ return false;
+}
+
+void CCECBusDevice::ResetDeviceStatus(void)
+{
+ CLockObject lock(m_mutex);
+ SetPowerStatus (CEC_POWER_STATUS_UNKNOWN);
+ SetVendorId (CEC_VENDOR_UNKNOWN);
+ SetMenuState (CEC_MENU_STATE_ACTIVATED);
+ SetCecVersion (CEC_VERSION_UNKNOWN);
+ SetStreamPath (0xFFFF);
+ SetOSDName (ToString(m_iLogicalAddress));
+ SetInactiveSource();
+ m_iLastActive = 0;
+ m_unsupportedFeatures.clear();
+}
+
+void CCECBusDevice::SetDeviceStatus(const cec_bus_device_status newStatus)
+{
+ {
+ CLockObject lock(m_mutex);
+ switch (newStatus)
+ {
+ case CEC_DEVICE_STATUS_UNKNOWN:
+ if (m_deviceStatus != newStatus)
+ CLibCEC::AddLog(CEC_LOG_DEBUG, "device status of %s changed into 'unknown'", ToString(m_iLogicalAddress));
+ ResetDeviceStatus();
+ m_deviceStatus = newStatus;
+ break;
+ case CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC:
+ if (m_deviceStatus != newStatus)
+ CLibCEC::AddLog(CEC_LOG_DEBUG, "device status of %s changed into 'handled by libCEC'", ToString(m_iLogicalAddress));
+ SetPowerStatus (CEC_POWER_STATUS_ON);
+ SetVendorId (CEC_VENDOR_UNKNOWN);
+ SetMenuState (CEC_MENU_STATE_ACTIVATED);
+ SetCecVersion (CEC_VERSION_1_3A);
+ SetStreamPath (0xFFFF);
+ SetInactiveSource();
+ m_iLastActive = 0;
+ m_deviceStatus = newStatus;
+ break;
+ case CEC_DEVICE_STATUS_PRESENT:
+ if (m_deviceStatus != newStatus)
+ CLibCEC::AddLog(CEC_LOG_DEBUG, "device status of %s changed into 'present'", ToString(m_iLogicalAddress));
+ m_deviceStatus = newStatus;
+ break;
+ case CEC_DEVICE_STATUS_NOT_PRESENT:
+ if (m_deviceStatus != newStatus)
+ {
+ CLibCEC::AddLog(CEC_LOG_DEBUG, "device status of %s changed into 'not present'", ToString(m_iLogicalAddress));
+ ResetDeviceStatus();
+ m_deviceStatus = newStatus;
+ }
+ break;
+ }
+ }
+
+ if (newStatus == CEC_DEVICE_STATUS_PRESENT)
+ RequestVendorId(false);
+}
+
+void CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress)
+{
+ CLockObject lock(m_mutex);
+ if (iNewAddress > 0 && m_iPhysicalAddress != iNewAddress)
+ {
+ CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %s (%X): physical address changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress, iNewAddress);
+ m_iPhysicalAddress = iNewAddress;
+ }
+}
+
+void CCECBusDevice::SetStreamPath(uint16_t iNewAddress, uint16_t iOldAddress /* = 0xFFFF */)
+{
+ CLockObject lock(m_mutex);
+ if (iNewAddress != m_iStreamPath)
+ {
+ CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %s (%X): stream path changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress, iOldAddress == 0 ? m_iStreamPath : iOldAddress, iNewAddress);
m_iStreamPath = iNewAddress;
+ }
- if (iNewAddress > 0)
- SetPowerStatus(CEC_POWER_STATUS_ON);
+ CCECBusDevice *device = m_processor->GetDeviceByPhysicalAddress(iNewAddress);
+ if (device)
+ {
+ // if a device is found with the new physical address, mark it as active, which will automatically mark all other devices as inactive
+ device->SetActiveSource();
+ }
+ else
+ {
+ // try to find the device with the old address, and mark it as inactive when found
+ device = m_processor->GetDeviceByPhysicalAddress(iOldAddress);
+ if (device)
+ device->SetInactiveSource();
}
}
void CCECBusDevice::SetPowerStatus(const cec_power_status powerStatus)
{
+ CLockObject lock(m_mutex);
if (m_powerStatus != powerStatus)
{
- CStdString strLog;
- strLog.Format(">> %s (%X): power status changed from '%s' to '%s'", GetLogicalAddressName(), m_iLogicalAddress, CCECCommandHandler::ToString(m_powerStatus), CCECCommandHandler::ToString(powerStatus));
- m_processor->AddLog(CEC_LOG_DEBUG, strLog);
+ m_iLastPowerStateUpdate = GetTimeMs();
+ CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %s (%X): power status changed from '%s' to '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_powerStatus), ToString(powerStatus));
m_powerStatus = powerStatus;
}
}
-void CCECBusDevice::SetVendorId(uint64_t iVendorId, uint8_t iVendorClass /* = 0 */)
+void CCECBusDevice::MarkBusy(void)
+{
+ CLockObject handlerLock(m_handlerMutex);
+ ++m_iHandlerUseCount;
+}
+
+void CCECBusDevice::MarkReady(void)
{
- m_vendor.vendor = (cec_vendor_id)iVendorId;
- m_iVendorClass = iVendorClass;
+ CLockObject handlerLock(m_handlerMutex);
+ if (m_iHandlerUseCount > 0)
+ --m_iHandlerUseCount;
+}
- switch (iVendorId)
+bool CCECBusDevice::ReplaceHandler(bool bActivateSource /* = true */)
+{
+ bool bInitHandler(false);
{
- case CEC_VENDOR_SAMSUNG:
- if (m_handler->GetVendorId() != CEC_VENDOR_SAMSUNG)
- {
- delete m_handler;
- m_handler = new CANCommandHandler(this);
- }
- break;
- case CEC_VENDOR_LG:
- if (m_handler->GetVendorId() != CEC_VENDOR_LG)
- {
- delete m_handler;
- m_handler = new CSLCommandHandler(this);
- }
- break;
- case CEC_VENDOR_PANASONIC:
- if (m_handler->GetVendorId() != CEC_VENDOR_PANASONIC)
- {
- delete m_handler;
- m_handler = new CVLCommandHandler(this);
- }
- break;
- default:
- if (m_handler->GetVendorId() != CEC_VENDOR_UNKNOWN)
+ CTryLockObject lock(m_mutex);
+ if (!lock.IsLocked())
+ return false;
+
+ CLockObject handlerLock(m_handlerMutex);
+ if (m_iHandlerUseCount > 0)
+ return false;
+
+ MarkBusy();
+
+ if (m_vendor != m_handler->GetVendorId())
{
- delete m_handler;
- m_handler = new CCECCommandHandler(this);
+ if (CCECCommandHandler::HasSpecificHandler(m_vendor))
+ {
+ CLibCEC::AddLog(CEC_LOG_DEBUG, "replacing the command handler for device '%s' (%x)", GetLogicalAddressName(), GetLogicalAddress());
+ delete m_handler;
+
+ switch (m_vendor)
+ {
+ case CEC_VENDOR_SAMSUNG:
+ m_handler = new CANCommandHandler(this);
+ break;
+ case CEC_VENDOR_LG:
+ m_handler = new CSLCommandHandler(this);
+ break;
+ case CEC_VENDOR_PANASONIC:
+ m_handler = new CVLCommandHandler(this);
+ break;
+ default:
+ m_handler = new CCECCommandHandler(this);
+ break;
+ }
+
+ m_handler->SetVendorId(m_vendor);
+ bInitHandler = true;
+ }
}
- break;
}
- CStdString strLog;
- strLog.Format("%s (%X): vendor = %s (%06x) class = %2x", GetLogicalAddressName(), m_iLogicalAddress, GetVendorName(), GetVendorId(), GetVendorClass());
- m_processor->AddLog(CEC_LOG_DEBUG, strLog.c_str());
+ if (bInitHandler)
+ {
+ m_handler->InitHandler();
+
+ if (bActivateSource && m_processor->GetLogicalAddresses().IsSet(m_iLogicalAddress) && m_processor->IsInitialised() && IsActiveSource())
+ m_handler->ActivateSource();
+ }
+
+ MarkReady();
+
+ return true;
+}
+
+bool CCECBusDevice::SetVendorId(uint64_t iVendorId)
+{
+ bool bVendorChanged(false);
+
+ {
+ CLockObject lock(m_mutex);
+ bVendorChanged = (m_vendor != (cec_vendor_id)iVendorId);
+ m_vendor = (cec_vendor_id)iVendorId;
+ }
+
+ if (bVendorChanged)
+ CLibCEC::AddLog(CEC_LOG_DEBUG, "%s (%X): vendor = %s (%06x)", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_vendor), m_vendor);
+
+ return bVendorChanged;
}
//@}
//@{
bool CCECBusDevice::TransmitActiveSource(void)
{
- if (m_bActiveSource)
- {
- CStdString strLog;
- strLog.Format("<< %s (%X) -> broadcast (F): active source (%4x)", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress);
- AddLog(CEC_LOG_NOTICE, strLog);
-
- cec_command command;
- cec_command::format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_ACTIVE_SOURCE);
- command.parameters.push_back((uint8_t) ((m_iPhysicalAddress >> 8) & 0xFF));
- command.parameters.push_back((uint8_t) (m_iPhysicalAddress & 0xFF));
+ bool bSendActiveSource(false);
- return m_processor->Transmit(command);
+ {
+ CLockObject lock(m_mutex);
+ if (m_powerStatus != CEC_POWER_STATUS_ON && m_powerStatus != CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON)
+ CLibCEC::AddLog(CEC_LOG_DEBUG, "<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress);
+ else if (m_bActiveSource)
+ {
+ CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> broadcast (F): active source (%4x)", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress);
+ bSendActiveSource = true;
+ }
+ else
+ CLibCEC::AddLog(CEC_LOG_DEBUG, "<< %s (%X) is not the active source", GetLogicalAddressName(), m_iLogicalAddress);
}
- else
+
+ if (bSendActiveSource)
{
- CStdString strLog;
- strLog.Format("<< %s (%X) is not the active source", GetLogicalAddressName(), m_iLogicalAddress);
- AddLog(CEC_LOG_DEBUG, strLog);
+ MarkBusy();
+ m_handler->TransmitActiveSource(m_iLogicalAddress, m_iPhysicalAddress);
+ MarkReady();
+ return true;
}
return false;
bool CCECBusDevice::TransmitCECVersion(cec_logical_address dest)
{
- CStdString strLog;
- strLog.Format("<< %s (%X) -> %s (%X): cec version ", GetLogicalAddressName(), m_iLogicalAddress, CCECCommandHandler::ToString(dest), dest);
- switch (m_cecVersion)
+ cec_version version;
{
- case CEC_VERSION_1_2:
- strLog.append("1.2");
- break;
- case CEC_VERSION_1_2A:
- strLog.append("1.2a");
- break;
- case CEC_VERSION_1_3:
- strLog.append("1.3");
- break;
- case CEC_VERSION_1_3A:
- strLog.append("1.3a");
- break;
- default:
- strLog.append("unknown");
- break;
+ CLockObject lock(m_mutex);
+ CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): cec version %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString(m_cecVersion));
+ version = m_cecVersion;
}
- AddLog(CEC_LOG_NOTICE, strLog);
-
- cec_command command;
- cec_command::format(command, m_iLogicalAddress, dest, CEC_OPCODE_CEC_VERSION);
- command.parameters.push_back(m_cecVersion);
- return m_processor->Transmit(command);
+ MarkBusy();
+ bool bReturn = m_handler->TransmitCECVersion(m_iLogicalAddress, dest, version);
+ MarkReady();
+ return bReturn;
}
-bool CCECBusDevice::TransmitInactiveView(void)
+bool CCECBusDevice::TransmitImageViewOn(void)
{
- CStdString strLog;
- strLog.Format("<< %s (%X) -> broadcast (F): inactive view", GetLogicalAddressName(), m_iLogicalAddress);
- AddLog(CEC_LOG_NOTICE, strLog);
+ {
+ CLockObject lock(m_mutex);
+ if (m_powerStatus != CEC_POWER_STATUS_ON && m_powerStatus != CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON)
+ {
+ CLibCEC::AddLog(CEC_LOG_DEBUG, "<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress);
+ return false;
+ }
+ }
+
+ MarkBusy();
+ m_handler->TransmitImageViewOn(m_iLogicalAddress, CECDEVICE_TV);
+ MarkReady();
+ return true;
+}
- cec_command command;
- cec_command::format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_INACTIVE_SOURCE);
- command.parameters.push_back((m_iPhysicalAddress >> 8) & 0xFF);
- command.parameters.push_back(m_iPhysicalAddress & 0xFF);
+bool CCECBusDevice::TransmitInactiveSource(void)
+{
+ uint16_t iPhysicalAddress;
+ {
+ CLockObject lock(m_mutex);
+ CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> broadcast (F): inactive source", GetLogicalAddressName(), m_iLogicalAddress);
+ iPhysicalAddress = m_iPhysicalAddress;
+ }
- return m_processor->Transmit(command);
+ MarkBusy();
+ bool bReturn = m_handler->TransmitInactiveSource(m_iLogicalAddress, iPhysicalAddress);
+ MarkReady();
+ return bReturn;
}
bool CCECBusDevice::TransmitMenuState(cec_logical_address dest)
{
- CStdString strLog;
- strLog.Format("<< %s (%X) -> %s (%X): ", GetLogicalAddressName(), m_iLogicalAddress, CCECCommandHandler::ToString(dest), dest);
- if (m_bMenuActive)
- strLog.append("menu active");
- else
- strLog.append("menu inactive");
- AddLog(CEC_LOG_NOTICE, strLog);
-
- cec_command command;
- cec_command::format(command, m_iLogicalAddress, dest, CEC_OPCODE_MENU_STATUS);
- command.parameters.push_back(m_bMenuActive ? (uint8_t) CEC_MENU_STATE_ACTIVATED : (uint8_t) CEC_MENU_STATE_DEACTIVATED);
+ cec_menu_state menuState;
+ {
+ CLockObject lock(m_mutex);
+ CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): menu state '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString(m_menuState));
+ menuState = m_menuState;
+ }
- return m_processor->Transmit(command);
+ MarkBusy();
+ bool bReturn = m_handler->TransmitMenuState(m_iLogicalAddress, dest, menuState);
+ MarkReady();
+ return bReturn;
}
bool CCECBusDevice::TransmitOSDName(cec_logical_address dest)
{
- CStdString strLog;
- strLog.Format("<< %s (%X) -> %s (%X): OSD name '%s'", GetLogicalAddressName(), m_iLogicalAddress, CCECCommandHandler::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);
- for (unsigned int iPtr = 0; iPtr < m_strDeviceName.length(); iPtr++)
- command.parameters.push_back(m_strDeviceName.at(iPtr));
+ CStdString strDeviceName;
+ {
+ CLockObject lock(m_mutex);
+ CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): OSD name '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, m_strDeviceName.c_str());
+ strDeviceName = m_strDeviceName;
+ }
- return m_processor->Transmit(command);
+ MarkBusy();
+ bool bReturn = m_handler->TransmitOSDName(m_iLogicalAddress, dest, strDeviceName);
+ MarkReady();
+ return bReturn;
}
bool CCECBusDevice::TransmitOSDString(cec_logical_address dest, cec_display_control duration, const char *strMessage)
{
- CStdString strLog;
- strLog.Format("<< %s (%X) -> %s (%X): display OSD message '%s'", GetLogicalAddressName(), m_iLogicalAddress, CCECCommandHandler::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);
- command.parameters.push_back((uint8_t)duration);
+ bool bReturn(false);
+ if (!m_processor->m_busDevices[dest]->IsUnsupportedFeature(CEC_OPCODE_SET_OSD_STRING))
+ {
+ CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): display OSD message '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, strMessage);
+ MarkBusy();
+ bReturn = m_handler->TransmitOSDString(m_iLogicalAddress, dest, duration, strMessage);
+ MarkReady();
+ }
+ return bReturn;
+}
- unsigned int iLen = strlen(strMessage);
- if (iLen > 13) iLen = 13;
+bool CCECBusDevice::TransmitPhysicalAddress(void)
+{
+ uint16_t iPhysicalAddress;
+ cec_device_type type;
+ {
+ CLockObject lock(m_mutex);
+ if (m_iPhysicalAddress == 0xffff)
+ return false;
- for (unsigned int iPtr = 0; iPtr < iLen; iPtr++)
- command.parameters.push_back(strMessage[iPtr]);
+ CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> broadcast (F): physical adddress %4x", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress);
+ iPhysicalAddress = m_iPhysicalAddress;
+ type = m_type;
+ }
- return m_processor->Transmit(command);
+ MarkBusy();
+ bool bReturn = m_handler->TransmitPhysicalAddress(m_iLogicalAddress, iPhysicalAddress, type);
+ MarkReady();
+ return bReturn;
}
-bool CCECBusDevice::TransmitPhysicalAddress(void)
+bool CCECBusDevice::TransmitSetMenuLanguage(cec_logical_address dest)
{
- CStdString strLog;
- strLog.Format("<< %s (%X) -> broadcast (F): physical adddress %4x", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress);
- AddLog(CEC_LOG_NOTICE, strLog.c_str());
+ bool bReturn(false);
+ cec_menu_language language = GetMenuLanguage();
- cec_command command;
- cec_command::format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS);
- command.parameters.push_back((uint8_t) ((m_iPhysicalAddress >> 8) & 0xFF));
- command.parameters.push_back((uint8_t) (m_iPhysicalAddress & 0xFF));
- command.parameters.push_back((uint8_t) (m_type));
+ char lang[3];
+ {
+ CLockObject lock(m_mutex);
+ lang[0] = language.language[0];
+ lang[1] = language.language[1];
+ lang[2] = language.language[2];
+ }
- return m_processor->Transmit(command);
+ MarkBusy();
+ if (lang[0] == '?' && lang[1] == '?' && lang[2] == '?')
+ {
+ CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): Menu language feature abort", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest);
+ m_processor->TransmitAbort(dest, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
+ bReturn = true;
+ }
+ else
+ {
+ CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> broadcast (F): Menu language '%s'", GetLogicalAddressName(), m_iLogicalAddress, lang);
+ bReturn = m_handler->TransmitSetMenuLanguage(m_iLogicalAddress, lang);
+ }
+ MarkReady();
+ return bReturn;
}
bool CCECBusDevice::TransmitPoll(cec_logical_address dest)
{
bool bReturn(false);
-
if (dest == CECDEVICE_UNKNOWN)
dest = m_iLogicalAddress;
- CStdString strLog;
- strLog.Format("<< %s (%X) -> %s (%X): POLL", GetLogicalAddressName(), m_iLogicalAddress, CCECCommandHandler::ToString(dest), dest);
- AddLog(CEC_LOG_NOTICE, strLog.c_str());
+ CCECBusDevice *destDevice = m_processor->m_busDevices[dest];
+ if (destDevice->m_deviceStatus == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC)
+ return bReturn;
+
+ MarkBusy();
+ CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): POLL", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest);
+ bReturn = m_handler->TransmitPoll(m_iLogicalAddress, dest);
+ CLibCEC::AddLog(CEC_LOG_DEBUG, bReturn ? ">> POLL sent" : ">> POLL not sent");
- cec_command command;
- cec_command::format(command, m_iLogicalAddress, dest, CEC_OPCODE_NONE);
- CLockObject lock(&m_mutex);
+ CLockObject lock(m_mutex);
+ if (bReturn)
+ {
+ m_iLastActive = GetTimeMs();
+ destDevice->m_deviceStatus = CEC_DEVICE_STATUS_PRESENT;
+ }
+ else
+ destDevice->m_deviceStatus = CEC_DEVICE_STATUS_NOT_PRESENT;
- bReturn = m_processor->Transmit(command);
- AddLog(CEC_LOG_DEBUG, bReturn ? ">> POLL sent" : ">> POLL not sent");
+ MarkReady();
return bReturn;
}
bool CCECBusDevice::TransmitPowerState(cec_logical_address dest)
{
- CStdString strLog;
- strLog.Format("<< %s (%X) -> %s (%X): %s", GetLogicalAddressName(), m_iLogicalAddress, CCECCommandHandler::ToString(dest), dest, CCECCommandHandler::ToString(m_powerStatus));
- AddLog(CEC_LOG_NOTICE, strLog.c_str());
+ cec_power_status state;
+ {
+ CLockObject lock(m_mutex);
+ if (!IsActiveSource())
+ {
+ CLibCEC::AddLog(CEC_LOG_NOTICE, "power state requested of %s (%X), but we are not the active source. setting power state to standby", GetLogicalAddressName(), m_iLogicalAddress);
+ SetPowerStatus(CEC_POWER_STATUS_STANDBY);
+ }
- cec_command command;
- cec_command::format(command, m_iLogicalAddress, dest, CEC_OPCODE_REPORT_POWER_STATUS);
- command.parameters.push_back((uint8_t) m_powerStatus);
+ CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString(m_powerStatus));
+ state = m_powerStatus;
+ }
- return m_processor->Transmit(command);
+ MarkBusy();
+ bool bReturn = m_handler->TransmitPowerState(m_iLogicalAddress, dest, state);
+ MarkReady();
+ return bReturn;
}
-bool CCECBusDevice::TransmitVendorID(cec_logical_address dest)
+bool CCECBusDevice::TransmitVendorID(cec_logical_address dest, bool bSendAbort /* = true */)
{
- CStdString strLog;
- strLog.Format("<< %s (%X) -> %s (%X): vendor id feature abort", GetLogicalAddressName(), m_iLogicalAddress, CCECCommandHandler::ToString(dest), dest);
- AddLog(CEC_LOG_NOTICE, strLog);
+ bool bReturn(false);
+ uint64_t iVendorId;
+ {
+ CLockObject lock(m_mutex);
+ iVendorId = (uint64_t)m_vendor;
+ }
- m_processor->TransmitAbort(dest, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
- return false;
+ MarkBusy();
+ if (iVendorId == CEC_VENDOR_UNKNOWN)
+ {
+ if (bSendAbort)
+ {
+ CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): vendor id feature abort", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest);
+ m_processor->TransmitAbort(dest, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
+ bReturn = true;
+ }
+ }
+ else
+ {
+ CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): vendor id %s (%x)", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString((cec_vendor_id)iVendorId), iVendorId);
+ bReturn = m_handler->TransmitVendorID(m_iLogicalAddress, iVendorId);
+ }
+ MarkReady();
+ return bReturn;
}
+
+bool CCECBusDevice::TransmitKeypress(cec_user_control_code key, bool bWait /* = true */)
+{
+ MarkBusy();
+ bool bReturn = m_handler->TransmitKeypress(m_processor->GetLogicalAddress(), m_iLogicalAddress, key, bWait);
+ MarkReady();
+ return bReturn;
+}
+
+bool CCECBusDevice::TransmitKeyRelease(bool bWait /* = true */)
+{
+ MarkBusy();
+ bool bReturn = m_handler->TransmitKeyRelease(m_processor->GetLogicalAddress(), m_iLogicalAddress, bWait);
+ MarkReady();
+ return bReturn;
+}
+
+bool CCECBusDevice::IsUnsupportedFeature(cec_opcode opcode)
+{
+ CLockObject lock(m_mutex);
+ bool bUnsupported = (m_unsupportedFeatures.find(opcode) != m_unsupportedFeatures.end());
+ if (bUnsupported)
+ CLibCEC::AddLog(CEC_LOG_DEBUG, "'%s' is marked as unsupported feature for device '%s'", ToString(opcode), GetLogicalAddressName());
+ return bUnsupported;
+}
+
+void CCECBusDevice::SetUnsupportedFeature(cec_opcode opcode)
+{
+ // some commands should never be marked as unsupported
+ if (opcode == CEC_OPCODE_VENDOR_COMMAND ||
+ opcode == CEC_OPCODE_VENDOR_COMMAND_WITH_ID ||
+ opcode == CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN ||
+ opcode == CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP ||
+ opcode == CEC_OPCODE_ABORT ||
+ opcode == CEC_OPCODE_FEATURE_ABORT ||
+ opcode == CEC_OPCODE_NONE)
+ return;
+
+ {
+ CLockObject lock(m_mutex);
+ if (m_unsupportedFeatures.find(opcode) == m_unsupportedFeatures.end())
+ {
+ CLibCEC::AddLog(CEC_LOG_DEBUG, "marking opcode '%s' as unsupported feature for device '%s'", ToString(opcode), GetLogicalAddressName());
+ m_unsupportedFeatures.insert(opcode);
+ }
+ }
+
+ // signal threads that are waiting for a reponse
+ MarkBusy();
+ m_handler->SignalOpcode(cec_command::GetResponseOpcode(opcode));
+ MarkReady();
+}
+
+bool CCECBusDevice::ActivateSource(void)
+{
+ CLibCEC::AddLog(CEC_LOG_DEBUG, "activating source '%s'", ToString(m_iLogicalAddress));
+ MarkBusy();
+ bool bReturn = m_handler->ActivateSource();
+ MarkReady();
+ return bReturn;
+}
+
+void CCECBusDevice::HandlePoll(cec_logical_address destination)
+{
+ if (destination >= 0 && destination < CECDEVICE_BROADCAST)
+ {
+ CCECBusDevice *device = m_processor->m_busDevices[destination];
+ if (device)
+ device->HandlePollFrom(m_iLogicalAddress);
+ }
+}
+
+void CCECBusDevice::HandlePollFrom(cec_logical_address initiator)
+{
+ CLibCEC::AddLog(CEC_LOG_DEBUG, "<< POLL: %s (%x) -> %s (%x)", ToString(initiator), initiator, ToString(m_iLogicalAddress), m_iLogicalAddress);
+ m_bAwaitingReceiveFailed = true;
+}
+
+bool CCECBusDevice::HandleReceiveFailed(void)
+{
+ bool bReturn = m_bAwaitingReceiveFailed;
+ m_bAwaitingReceiveFailed = false;
+ return bReturn;
+}
+
+void CCECBusDevice::CheckVendorIdRequested(void)
+{
+ bool bRequestVendorId(false);
+ {
+ CLockObject lock(m_mutex);
+ bRequestVendorId = !m_bVendorIdRequested;
+ m_bVendorIdRequested = true;
+ }
+
+ if (bRequestVendorId)
+ {
+ ReplaceHandler(false);
+ GetVendorId();
+ }
+}
+
+bool CCECBusDevice::TransmitPendingActiveSourceCommands(void)
+{
+ MarkBusy();
+ bool bReturn = m_handler->TransmitPendingActiveSourceCommands();
+ MarkReady();
+ return bReturn;
+}
+
//@}