X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Flib%2Fdevices%2FCECBusDevice.cpp;h=b192391906607f0da79ea1cb377aa3aa375256b5;hb=cc938f166e50b0c9e4fdccf55e0a8cffb2c56602;hp=d6e447f96303f44cf91735807601704451a01df0;hpb=2efa39b7105c98a2219a12e5ab3432b77e042112;p=deb_libcec.git diff --git a/src/lib/devices/CECBusDevice.cpp b/src/lib/devices/CECBusDevice.cpp index d6e447f..b192391 100644 --- a/src/lib/devices/CECBusDevice.cpp +++ b/src/lib/devices/CECBusDevice.cpp @@ -1,7 +1,7 @@ /* * This file is part of the libCEC(R) library. * - * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited. All rights reserved. + * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved. * libCEC(R) is an original work, containing original code. * * libCEC(R) is a trademark of Pulse-Eight Limited. @@ -36,28 +36,40 @@ #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; +#include "CECAudioSystem.h" +#include "CECPlaybackDevice.h" +#include "CECRecordingDevice.h" +#include "CECTuner.h" +#include "CECTV.h" -#define ToString(p) m_processor->ToString(p) - -CCECBusDevice::CCECBusDevice(CCECProcessor *processor, cec_logical_address iLogicalAddress, uint16_t iPhysicalAddress) : - m_type(CEC_DEVICE_TYPE_RESERVED), - m_iPhysicalAddress(iPhysicalAddress), - m_iStreamPath(0), - 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_handlerMutex(false) +using namespace std; +using namespace CEC; +using namespace PLATFORM; + +#define LIB_CEC m_processor->GetLib() +#define ToString(p) LIB_CEC->ToString(p) + +CCECBusDevice::CCECBusDevice(CCECProcessor *processor, cec_logical_address iLogicalAddress, uint16_t iPhysicalAddress /* = CEC_INVALID_PHYSICAL_ADDRESS */) : + m_type (CEC_DEVICE_TYPE_RESERVED), + m_iPhysicalAddress (iPhysicalAddress), + m_iStreamPath (CEC_INVALID_PHYSICAL_ADDRESS), + 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); @@ -74,9 +86,60 @@ CCECBusDevice::~CCECBusDevice(void) delete m_handler; } -void CCECBusDevice::AddLog(cec_log_level level, const CStdString &strMessage) +bool CCECBusDevice::ReplaceHandler(bool bActivateSource /* = true */) { - m_processor->AddLog(level, strMessage); + bool bInitHandler(false); + { + 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()) + { + if (CCECCommandHandler::HasSpecificHandler(m_vendor)) + { + LIB_CEC->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; + } + } + } + + if (bInitHandler) + { + m_handler->InitHandler(); + + if (bActivateSource && IsHandledByLibCEC() && IsActiveSource()) + m_handler->ActivateSource(); + } + + MarkReady(); + + return true; } bool CCECBusDevice::HandleCommand(const cec_command &command) @@ -85,235 +148,422 @@ bool CCECBusDevice::HandleCommand(const cec_command &command) /* update "last active" */ { - CLockObject lock(&m_mutex); + CLockObject lock(m_mutex); m_iLastActive = GetTimeMs(); - if (m_deviceStatus != CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC) + /* 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; + + MarkBusy(); } /* handle the command */ bHandled = m_handler->HandleCommand(command); /* change status to present */ - if (bHandled) + if (bHandled && GetLogicalAddress() != CECDEVICE_BROADCAST) { - CLockObject lock(&m_mutex); + CLockObject lock(m_mutex); 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); - } + LIB_CEC->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; } } + MarkReady(); return bHandled; } -bool CCECBusDevice::PowerOn(void) +const char* CCECBusDevice::GetLogicalAddressName(void) const +{ + return ToString(m_iLogicalAddress); +} + +bool CCECBusDevice::IsPresent(void) +{ + CLockObject lock(m_mutex); + return m_deviceStatus == CEC_DEVICE_STATUS_PRESENT; +} + +bool CCECBusDevice::IsHandledByLibCEC(void) +{ + CLockObject lock(m_mutex); + return m_deviceStatus == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC; +} + +void CCECBusDevice::SetUnsupportedFeature(cec_opcode opcode) { - CStdString strLog; - strLog.Format("<< powering on '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); - AddLog(CEC_LOG_DEBUG, strLog.c_str()); + // 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; - if (m_handler->TransmitImageViewOn(GetMyLogicalAddress(), m_iLogicalAddress)) { + CLockObject lock(m_mutex); + if (m_unsupportedFeatures.find(opcode) == m_unsupportedFeatures.end()) { - CLockObject lock(&m_mutex); -// m_powerStatus = CEC_POWER_STATUS_UNKNOWN; - m_powerStatus = CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON; + LIB_CEC->AddLog(CEC_LOG_DEBUG, "marking opcode '%s' as unsupported feature for device '%s'", ToString(opcode), GetLogicalAddressName()); + m_unsupportedFeatures.insert(opcode); } -// cec_power_status status = GetPowerStatus(); -// if (status == CEC_POWER_STATUS_STANDBY || status == CEC_POWER_STATUS_UNKNOWN) -// { -// /* sending the normal power on command appears to have failed */ -// CStdString strLog; -// strLog.Format("<< sending power on keypress to '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); -// AddLog(CEC_LOG_DEBUG, strLog.c_str()); -// -// TransmitKeypress(CEC_USER_CONTROL_CODE_POWER); -// return TransmitKeyRelease(); -// } - return true; } - return false; + // signal threads that are waiting for a reponse + MarkBusy(); + m_handler->SignalOpcode(cec_command::GetResponseOpcode(opcode)); + MarkReady(); } -bool CCECBusDevice::Standby(void) +bool CCECBusDevice::IsUnsupportedFeature(cec_opcode opcode) { - CStdString strLog; - strLog.Format("<< putting '%s' (%X) in standby mode", GetLogicalAddressName(), m_iLogicalAddress); - AddLog(CEC_LOG_DEBUG, strLog.c_str()); + CLockObject lock(m_mutex); + bool bUnsupported = (m_unsupportedFeatures.find(opcode) != m_unsupportedFeatures.end()); + if (bUnsupported) + LIB_CEC->AddLog(CEC_LOG_DEBUG, "'%s' is marked as unsupported feature for device '%s'", ToString(opcode), GetLogicalAddressName()); + return bUnsupported; +} + +bool CCECBusDevice::TransmitKeypress(const cec_logical_address initiator, cec_user_control_code key, bool bWait /* = true */) +{ + MarkBusy(); + bool bReturn = m_handler->TransmitKeypress(initiator, m_iLogicalAddress, key, bWait); + MarkReady(); + return bReturn; +} - return m_handler->TransmitStandby(GetMyLogicalAddress(), m_iLogicalAddress); +bool CCECBusDevice::TransmitKeyRelease(const cec_logical_address initiator, bool bWait /* = true */) +{ + MarkBusy(); + bool bReturn = m_handler->TransmitKeyRelease(initiator, m_iLogicalAddress, bWait); + MarkReady(); + return bReturn; } -/** @name Getters */ -//@{ -cec_version CCECBusDevice::GetCecVersion(bool bUpdate /* = false */) +cec_version CCECBusDevice::GetCecVersion(const cec_logical_address initiator, bool bUpdate /* = false */) { + bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT); bool bRequestUpdate(false); { - CLockObject lock(&m_mutex); - bRequestUpdate = (GetStatus() == CEC_DEVICE_STATUS_PRESENT && - (bUpdate || m_cecVersion == CEC_VERSION_UNKNOWN)); + CLockObject lock(m_mutex); + bRequestUpdate = bIsPresent && + (bUpdate || m_cecVersion == CEC_VERSION_UNKNOWN); } if (bRequestUpdate) - RequestCecVersion(); + { + CheckVendorIdRequested(initiator); + RequestCecVersion(initiator); + } - CLockObject lock(&m_mutex); + CLockObject lock(m_mutex); return m_cecVersion; } -bool CCECBusDevice::RequestCecVersion(void) +void CCECBusDevice::SetCecVersion(const cec_version newVersion) +{ + CLockObject lock(m_mutex); + if (m_cecVersion != newVersion) + LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): CEC version %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(newVersion)); + m_cecVersion = newVersion; +} + +bool CCECBusDevice::RequestCecVersion(const cec_logical_address initiator, bool bWaitForResponse /* = true */) { bool bReturn(false); - if (!MyLogicalAddressContains(m_iLogicalAddress)) + if (!IsHandledByLibCEC() && + !IsUnsupportedFeature(CEC_OPCODE_GET_CEC_VERSION)) { - m_handler->MarkBusy(); - CStdString strLog; - strLog.Format("<< requesting CEC version of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); - AddLog(CEC_LOG_NOTICE, strLog); - - bReturn = m_handler->TransmitRequestCecVersion(GetMyLogicalAddress(), m_iLogicalAddress); - m_handler->MarkReady(); + MarkBusy(); + LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< requesting CEC version of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); + bReturn = m_handler->TransmitRequestCecVersion(initiator, m_iLogicalAddress, bWaitForResponse); + MarkReady(); } return bReturn; } -const char* CCECBusDevice::GetLogicalAddressName(void) const +bool CCECBusDevice::TransmitCECVersion(const cec_logical_address destination) { - return ToString(m_iLogicalAddress); + cec_version version; + { + CLockObject lock(m_mutex); + LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): cec version %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(destination), destination, ToString(m_cecVersion)); + version = m_cecVersion; + } + + MarkBusy(); + bool bReturn = m_handler->TransmitCECVersion(m_iLogicalAddress, destination, version); + MarkReady(); + return bReturn; } -cec_menu_language &CCECBusDevice::GetMenuLanguage(bool bUpdate /* = false */) +cec_menu_language &CCECBusDevice::GetMenuLanguage(const cec_logical_address initiator, bool bUpdate /* = false */) { + bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT); bool bRequestUpdate(false); { - CLockObject lock(&m_mutex); - bRequestUpdate = (GetStatus() == CEC_DEVICE_STATUS_PRESENT && + CLockObject lock(m_mutex); + bRequestUpdate = (bIsPresent && (bUpdate || !strcmp(m_menuLanguage.language, "???"))); } if (bRequestUpdate) - RequestMenuLanguage(); + { + CheckVendorIdRequested(initiator); + RequestMenuLanguage(initiator); + } - CLockObject lock(&m_mutex); + CLockObject lock(m_mutex); return m_menuLanguage; } -bool CCECBusDevice::RequestMenuLanguage(void) +void CCECBusDevice::SetMenuLanguage(const char *strLanguage) +{ + if (!strLanguage) + return; + + CLockObject lock(m_mutex); + if (strcmp(strLanguage, m_menuLanguage.language)) + { + memcpy(m_menuLanguage.language, strLanguage, 3); + LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): menu language set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, m_menuLanguage.language); + } +} + +void CCECBusDevice::SetMenuLanguage(const cec_menu_language &language) +{ + if (language.device == m_iLogicalAddress) + SetMenuLanguage(language.language); +} + +bool CCECBusDevice::RequestMenuLanguage(const cec_logical_address initiator, bool bWaitForResponse /* = true */) { bool bReturn(false); - if (!MyLogicalAddressContains(m_iLogicalAddress) && + if (!IsHandledByLibCEC() && !IsUnsupportedFeature(CEC_OPCODE_GET_MENU_LANGUAGE)) { - m_handler->MarkBusy(); - CStdString strLog; - strLog.Format("<< requesting menu language of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); - AddLog(CEC_LOG_NOTICE, strLog); - bReturn = m_handler->TransmitRequestMenuLanguage(GetMyLogicalAddress(), m_iLogicalAddress); - m_handler->MarkReady(); + MarkBusy(); + LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< requesting menu language of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); + bReturn = m_handler->TransmitRequestMenuLanguage(initiator, m_iLogicalAddress, bWaitForResponse); + MarkReady(); } return bReturn; } -cec_menu_state CCECBusDevice::GetMenuState(void) +bool CCECBusDevice::TransmitSetMenuLanguage(const cec_logical_address destination) { - CLockObject lock(&m_mutex); - return m_menuState; + bool bReturn(false); + cec_menu_language language; + { + CLockObject lock(m_mutex); + language = m_menuLanguage; + } + + char lang[3]; + { + CLockObject lock(m_mutex); + lang[0] = language.language[0]; + lang[1] = language.language[1]; + lang[2] = language.language[2]; + } + + MarkBusy(); + if (lang[0] == '?' && lang[1] == '?' && lang[2] == '?') + { + LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): Menu language feature abort", GetLogicalAddressName(), m_iLogicalAddress, ToString(destination), destination); + m_processor->TransmitAbort(m_iLogicalAddress, destination, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID); + bReturn = true; + } + else + { + LIB_CEC->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; } -cec_logical_address CCECBusDevice::GetMyLogicalAddress(void) const +bool CCECBusDevice::TransmitOSDString(const cec_logical_address destination, cec_display_control duration, const char *strMessage) { - return m_processor->GetLogicalAddress(); + bool bReturn(false); + if (!m_processor->GetDevice(destination)->IsUnsupportedFeature(CEC_OPCODE_SET_OSD_STRING)) + { + LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): display OSD message '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(destination), destination, strMessage); + MarkBusy(); + bReturn = m_handler->TransmitOSDString(m_iLogicalAddress, destination, duration, strMessage); + MarkReady(); + } + return bReturn; } -uint16_t CCECBusDevice::GetMyPhysicalAddress(void) const +CStdString CCECBusDevice::GetCurrentOSDName(void) { - return m_processor->GetPhysicalAddress(); + CLockObject lock(m_mutex); + return m_strDeviceName; } -CStdString CCECBusDevice::GetOSDName(bool bUpdate /* = false */) +CStdString CCECBusDevice::GetOSDName(const cec_logical_address initiator, bool bUpdate /* = false */) { + bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT); bool bRequestUpdate(false); { - CLockObject lock(&m_mutex); - bRequestUpdate = (GetStatus() == CEC_DEVICE_STATUS_PRESENT && + CLockObject lock(m_mutex); + bRequestUpdate = bIsPresent && (bUpdate || m_strDeviceName.Equals(ToString(m_iLogicalAddress))) && - m_type != CEC_DEVICE_TYPE_TV); + m_type != CEC_DEVICE_TYPE_TV; } if (bRequestUpdate) - RequestOSDName(); + { + CheckVendorIdRequested(initiator); + RequestOSDName(initiator); + } - CLockObject lock(&m_mutex); + CLockObject lock(m_mutex); return m_strDeviceName; } -bool CCECBusDevice::RequestOSDName(void) +void CCECBusDevice::SetOSDName(CStdString strName) +{ + CLockObject lock(m_mutex); + if (m_strDeviceName != strName) + { + LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): osd name set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, strName.c_str()); + m_strDeviceName = strName; + } +} + +bool CCECBusDevice::RequestOSDName(const cec_logical_address initiator, bool bWaitForResponse /* = true */) { bool bReturn(false); - if (!MyLogicalAddressContains(m_iLogicalAddress) && + if (!IsHandledByLibCEC() && !IsUnsupportedFeature(CEC_OPCODE_GIVE_OSD_NAME)) { - m_handler->MarkBusy(); - CStdString strLog; - strLog.Format("<< requesting OSD name of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); - AddLog(CEC_LOG_NOTICE, strLog); - bReturn = m_handler->TransmitRequestOSDName(GetMyLogicalAddress(), m_iLogicalAddress); - m_handler->MarkReady(); + MarkBusy(); + LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< requesting OSD name of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); + bReturn = m_handler->TransmitRequestOSDName(initiator, m_iLogicalAddress, bWaitForResponse); + MarkReady(); } return bReturn; } -uint16_t CCECBusDevice::GetPhysicalAddress(bool bUpdate /* = false */) +bool CCECBusDevice::TransmitOSDName(const cec_logical_address destination) { - bool bRequestUpdate(false); + CStdString strDeviceName; { - CLockObject lock(&m_mutex); - bRequestUpdate = (GetStatus() == CEC_DEVICE_STATUS_PRESENT && - (m_iPhysicalAddress == 0xFFFF || bUpdate)); + CLockObject lock(m_mutex); + LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): OSD name '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(destination), destination, m_strDeviceName.c_str()); + strDeviceName = m_strDeviceName; } - if (bRequestUpdate && !RequestPhysicalAddress()) - AddLog(CEC_LOG_ERROR, "failed to request the physical address (1)"); + MarkBusy(); + bool bReturn = m_handler->TransmitOSDName(m_iLogicalAddress, destination, strDeviceName); + MarkReady(); + return bReturn; +} + +bool CCECBusDevice::HasValidPhysicalAddress(void) +{ + CLockObject lock(m_mutex); + return CLibCEC::IsValidPhysicalAddress(m_iPhysicalAddress); +} + +uint16_t CCECBusDevice::GetCurrentPhysicalAddress(void) +{ + CLockObject lock(m_mutex); + return m_iPhysicalAddress; +} + +uint16_t CCECBusDevice::GetPhysicalAddress(const cec_logical_address initiator, bool bSuppressUpdate /* = false */) +{ + if (!bSuppressUpdate) + { + bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT); + bool bRequestUpdate(false); + { + CLockObject lock(m_mutex); + bRequestUpdate = bIsPresent && m_iPhysicalAddress == CEC_INVALID_PHYSICAL_ADDRESS; + } + + if (bRequestUpdate) + { + CheckVendorIdRequested(initiator); + if (!RequestPhysicalAddress(initiator)) + LIB_CEC->AddLog(CEC_LOG_ERROR, "failed to request the physical address"); + } + } - CLockObject lock(&m_mutex); + CLockObject lock(m_mutex); return m_iPhysicalAddress; } -bool CCECBusDevice::RequestPhysicalAddress(void) +bool CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress) +{ + CLockObject lock(m_mutex); + if (iNewAddress > 0 && m_iPhysicalAddress != iNewAddress) + { + LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): physical address changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress, iNewAddress); + m_iPhysicalAddress = iNewAddress; + } + return true; +} + +bool CCECBusDevice::RequestPhysicalAddress(const cec_logical_address initiator, bool bWaitForResponse /* = true */) { bool bReturn(false); - if (!MyLogicalAddressContains(m_iLogicalAddress)) + if (!IsHandledByLibCEC()) { - m_handler->MarkBusy(); - CStdString strLog; - strLog.Format("<< requesting physical address of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); - AddLog(CEC_LOG_NOTICE, strLog); - bReturn = m_handler->TransmitRequestPhysicalAddress(GetMyLogicalAddress(), m_iLogicalAddress); - m_handler->MarkReady(); + MarkBusy(); + LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< requesting physical address of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); + bReturn = m_handler->TransmitRequestPhysicalAddress(initiator, m_iLogicalAddress, bWaitForResponse); + MarkReady(); } return bReturn; } -cec_power_status CCECBusDevice::GetPowerStatus(bool bUpdate /* = false */) +bool CCECBusDevice::TransmitPhysicalAddress(void) { + uint16_t iPhysicalAddress; + cec_device_type type; + { + CLockObject lock(m_mutex); + if (m_iPhysicalAddress == CEC_INVALID_PHYSICAL_ADDRESS) + return false; + + LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> broadcast (F): physical adddress %4x", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress); + iPhysicalAddress = m_iPhysicalAddress; + type = m_type; + } + + MarkBusy(); + bool bReturn = m_handler->TransmitPhysicalAddress(m_iLogicalAddress, iPhysicalAddress, type); + MarkReady(); + return bReturn; +} + +cec_power_status CCECBusDevice::GetCurrentPowerStatus(void) +{ + CLockObject lock(m_mutex); + return m_powerStatus; +} + +cec_power_status CCECBusDevice::GetPowerStatus(const cec_logical_address initiator, bool bUpdate /* = false */) +{ + bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT); bool bRequestUpdate(false); { - CLockObject lock(&m_mutex); - bRequestUpdate = (GetStatus() == CEC_DEVICE_STATUS_PRESENT && + 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 || @@ -321,605 +571,661 @@ cec_power_status CCECBusDevice::GetPowerStatus(bool bUpdate /* = false */) } if (bRequestUpdate) - RequestPowerStatus(); + { + CheckVendorIdRequested(initiator); + RequestPowerStatus(initiator); + } - CLockObject lock(&m_mutex); + CLockObject lock(m_mutex); return m_powerStatus; } -bool CCECBusDevice::RequestPowerStatus(void) +void CCECBusDevice::SetPowerStatus(const cec_power_status powerStatus) +{ + CLockObject lock(m_mutex); + if (m_powerStatus != powerStatus) + { + m_iLastPowerStateUpdate = GetTimeMs(); + LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): power status changed from '%s' to '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_powerStatus), ToString(powerStatus)); + m_powerStatus = powerStatus; + } +} + +bool CCECBusDevice::RequestPowerStatus(const cec_logical_address initiator, bool bWaitForResponse /* = true */) { bool bReturn(false); - if (!MyLogicalAddressContains(m_iLogicalAddress) && + if (!IsHandledByLibCEC() && !IsUnsupportedFeature(CEC_OPCODE_GIVE_DEVICE_POWER_STATUS)) { - m_handler->MarkBusy(); - CStdString strLog; - strLog.Format("<< requesting power status of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); - AddLog(CEC_LOG_NOTICE, strLog); - bReturn = m_handler->TransmitRequestPowerStatus(GetMyLogicalAddress(), m_iLogicalAddress); - m_handler->MarkReady(); + MarkBusy(); + LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< requesting power status of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); + bReturn = m_handler->TransmitRequestPowerStatus(initiator, m_iLogicalAddress, bWaitForResponse); + MarkReady(); } return bReturn; } -cec_vendor_id CCECBusDevice::GetVendorId(bool bUpdate /* = false */) +bool CCECBusDevice::TransmitPowerState(const cec_logical_address destination) { - bool bRequestUpdate(false); + cec_power_status state; { - CLockObject lock(&m_mutex); - bRequestUpdate = (GetStatus() == CEC_DEVICE_STATUS_PRESENT && - (bUpdate || m_vendor == CEC_VENDOR_UNKNOWN)); + CLockObject lock(m_mutex); + LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(destination), destination, ToString(m_powerStatus)); + state = m_powerStatus; } - if (bRequestUpdate) - RequestVendorId(); + MarkBusy(); + bool bReturn = m_handler->TransmitPowerState(m_iLogicalAddress, destination, state); + MarkReady(); + return bReturn; +} - CLockObject lock(&m_mutex); +cec_vendor_id CCECBusDevice::GetCurrentVendorId(void) +{ + CLockObject lock(m_mutex); return m_vendor; } -bool CCECBusDevice::RequestVendorId(void) +cec_vendor_id CCECBusDevice::GetVendorId(const cec_logical_address initiator, bool bUpdate /* = false */) { - bool bReturn(false); - - if (!MyLogicalAddressContains(m_iLogicalAddress)) + bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT); + bool bRequestUpdate(false); { - m_handler->MarkBusy(); - CStdString strLog; - strLog.Format("<< requesting vendor ID of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); - AddLog(CEC_LOG_NOTICE, strLog); - bReturn = m_handler->TransmitRequestVendorId(GetMyLogicalAddress(), m_iLogicalAddress); - m_handler->MarkReady(); - - ReplaceHandler(true); + CLockObject lock(m_mutex); + bRequestUpdate = (bIsPresent && + (bUpdate || m_vendor == CEC_VENDOR_UNKNOWN)); } - return bReturn; -} -const char *CCECBusDevice::GetVendorName(bool bUpdate /* = false */) -{ - return ToString(GetVendorId(bUpdate)); + if (bRequestUpdate) + RequestVendorId(initiator); + + CLockObject lock(m_mutex); + return m_vendor; } -bool CCECBusDevice::MyLogicalAddressContains(cec_logical_address address) const +const char *CCECBusDevice::GetVendorName(const cec_logical_address initiator, bool bUpdate /* = false */) { - return m_processor->HasLogicalAddress(address); + return ToString(GetVendorId(initiator, bUpdate)); } -bool CCECBusDevice::NeedsPoll(void) +bool CCECBusDevice::SetVendorId(uint64_t iVendorId) { - bool bSendPoll(false); - switch (m_iLogicalAddress) + bool bVendorChanged(false); + { - 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; + CLockObject lock(m_mutex); + bVendorChanged = (m_vendor != (cec_vendor_id)iVendorId); + m_vendor = (cec_vendor_id)iVendorId; } - return bSendPoll; + if (bVendorChanged) + LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): vendor = %s (%06x)", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_vendor), m_vendor); + + return bVendorChanged; } -cec_bus_device_status CCECBusDevice::GetStatus(bool bForcePoll /* = false */) +bool CCECBusDevice::RequestVendorId(const cec_logical_address initiator, bool bWaitForResponse /* = true */) { - CLockObject lock(&m_mutex); - if (m_deviceStatus != CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC && - (m_deviceStatus == CEC_DEVICE_STATUS_UNKNOWN || bForcePoll)) + bool bReturn(false); + + if (!IsHandledByLibCEC() && initiator != CECDEVICE_UNKNOWN) { - lock.Leave(); - bool bPollAcked(false); - if (bForcePoll || NeedsPoll()) - bPollAcked = m_processor->PollDevice(m_iLogicalAddress); + MarkBusy(); + LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< requesting vendor ID of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); + bReturn = m_handler->TransmitRequestVendorId(initiator, m_iLogicalAddress, bWaitForResponse); + MarkReady(); - lock.Lock(); - m_deviceStatus = bPollAcked ? CEC_DEVICE_STATUS_PRESENT : CEC_DEVICE_STATUS_NOT_PRESENT; + if (bWaitForResponse) + ReplaceHandler(true); } - - return m_deviceStatus; -} - -//@} - -/** @name Setters */ -//@{ -void CCECBusDevice::SetCecVersion(const cec_version newVersion) -{ - m_cecVersion = newVersion; - - CStdString strLog; - strLog.Format("%s (%X): CEC version %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(newVersion)); - AddLog(CEC_LOG_DEBUG, strLog); + return bReturn; } -void CCECBusDevice::SetMenuLanguage(const cec_menu_language &language) +bool CCECBusDevice::TransmitVendorID(const cec_logical_address destination, bool bSendAbort /* = true */) { - CLockObject lock(&m_mutex); - if (language.device == m_iLogicalAddress) + bool bReturn(false); + uint64_t iVendorId; { - CStdString strLog; - strLog.Format(">> %s (%X): menu language set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, language.language); - m_processor->AddLog(CEC_LOG_DEBUG, strLog); - m_menuLanguage = language; + CLockObject lock(m_mutex); + iVendorId = (uint64_t)m_vendor; } -} -void CCECBusDevice::SetOSDName(CStdString strName) -{ - CLockObject lock(&m_mutex); - if (m_strDeviceName != strName) + MarkBusy(); + if (iVendorId == CEC_VENDOR_UNKNOWN) { - CStdString strLog; - strLog.Format(">> %s (%X): osd name set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, strName); - m_processor->AddLog(CEC_LOG_DEBUG, strLog); - m_strDeviceName = strName; + if (bSendAbort) + { + LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): vendor id feature abort", GetLogicalAddressName(), m_iLogicalAddress, ToString(destination), destination); + m_processor->TransmitAbort(m_iLogicalAddress, destination, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID); + bReturn = true; + } + } + else + { + LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): vendor id %s (%x)", GetLogicalAddressName(), m_iLogicalAddress, ToString(destination), destination, ToString((cec_vendor_id)iVendorId), iVendorId); + bReturn = m_handler->TransmitVendorID(m_iLogicalAddress, iVendorId); } + MarkReady(); + return bReturn; } -void CCECBusDevice::SetMenuState(const cec_menu_state state) +cec_bus_device_status CCECBusDevice::GetStatus(bool bForcePoll /* = false */, bool bSuppressPoll /* = false */) { - CLockObject lock(&m_mutex); - if (m_menuState != state) + cec_bus_device_status status(CEC_DEVICE_STATUS_UNKNOWN); + bool bNeedsPoll(false); + { - CStdString strLog; - strLog.Format(">> %s (%X): menu state set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_menuState)); - m_processor->AddLog(CEC_LOG_DEBUG, strLog); - m_menuState = state; + CLockObject lock(m_mutex); + status = m_deviceStatus; + bNeedsPoll = !bSuppressPoll && + (bForcePoll || m_deviceStatus == CEC_DEVICE_STATUS_UNKNOWN); } -} -void CCECBusDevice::SetInactiveSource(void) -{ + if (bNeedsPoll) { - CLockObject lock(&m_mutex); - m_bActiveSource = false; + 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); } - if (MyLogicalAddressContains(m_iLogicalAddress)) - SetPowerStatus(CEC_POWER_STATUS_STANDBY); + return status; } -void CCECBusDevice::SetActiveSource(void) +void CCECBusDevice::SetDeviceStatus(const cec_bus_device_status newStatus) { - CLockObject lock(&m_mutex); - if (!m_bActiveSource) { - CStdString strLog; - strLog.Format("making %s (%x) the active source", GetLogicalAddressName(), m_iLogicalAddress); - AddLog(CEC_LOG_DEBUG, strLog); + CLockObject lock(m_mutex); + switch (newStatus) + { + case CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC: + if (m_deviceStatus != newStatus) + LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): device status changed into 'handled by libCEC'", GetLogicalAddressName(), m_iLogicalAddress); + SetPowerStatus (CEC_POWER_STATUS_ON); + SetVendorId (CEC_VENDOR_UNKNOWN); + SetMenuState (CEC_MENU_STATE_ACTIVATED); + SetCecVersion (CEC_VERSION_1_3A); + SetStreamPath (CEC_INVALID_PHYSICAL_ADDRESS); + MarkAsInactiveSource(); + m_iLastActive = 0; + m_deviceStatus = newStatus; + break; + case CEC_DEVICE_STATUS_PRESENT: + if (m_deviceStatus != newStatus) + LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): device status changed into 'present'", GetLogicalAddressName(), m_iLogicalAddress); + m_deviceStatus = newStatus; + break; + case CEC_DEVICE_STATUS_NOT_PRESENT: + if (m_deviceStatus != newStatus) + { + LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): device status changed into 'not present'", GetLogicalAddressName(), m_iLogicalAddress); + ResetDeviceStatus(); + m_deviceStatus = newStatus; + } + break; + default: + ResetDeviceStatus(); + break; + } } +} - for (int iPtr = 0; iPtr < 16; iPtr++) - if (iPtr != m_iLogicalAddress) - m_processor->m_busDevices[iPtr]->SetInactiveSource(); - - m_bActiveSource = true; - m_powerStatus = CEC_POWER_STATUS_ON; +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 (CEC_INVALID_PHYSICAL_ADDRESS); + SetOSDName (ToString(m_iLogicalAddress)); + MarkAsInactiveSource(); + + m_iLastActive = 0; + m_bVendorIdRequested = false; + m_unsupportedFeatures.clear(); + + if (m_deviceStatus != CEC_DEVICE_STATUS_UNKNOWN) + LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): device status changed into 'unknown'", GetLogicalAddressName(), m_iLogicalAddress); + m_deviceStatus = CEC_DEVICE_STATUS_UNKNOWN; } -bool CCECBusDevice::TryLogicalAddress(void) +bool CCECBusDevice::TransmitPoll(const cec_logical_address dest) { - CStdString strLog; - strLog.Format("trying logical address '%s'", GetLogicalAddressName()); - AddLog(CEC_LOG_DEBUG, strLog); + bool bReturn(false); + cec_logical_address destination(dest); + if (destination == CECDEVICE_UNKNOWN) + destination = m_iLogicalAddress; - m_processor->SetAckMask(0x1 << m_iLogicalAddress); - if (!TransmitPoll(m_iLogicalAddress)) - { - strLog.Format("using logical address '%s'", GetLogicalAddressName()); - AddLog(CEC_LOG_NOTICE, strLog); - SetDeviceStatus(CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC); + CCECBusDevice *destDevice = m_processor->GetDevice(destination); + if (destDevice->m_deviceStatus == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC) + return bReturn; - return true; + MarkBusy(); + LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): POLL", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest); + bReturn = m_handler->TransmitPoll(m_iLogicalAddress, destination); + LIB_CEC->AddLog(CEC_LOG_DEBUG, bReturn ? ">> POLL sent" : ">> POLL not sent"); + + 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; - strLog.Format("logical address '%s' already taken", GetLogicalAddressName()); - AddLog(CEC_LOG_DEBUG, strLog); - SetDeviceStatus(CEC_DEVICE_STATUS_PRESENT); - return false; + MarkReady(); + return bReturn; } -void CCECBusDevice::SetDeviceStatus(const cec_bus_device_status newStatus) +void CCECBusDevice::HandlePoll(const cec_logical_address destination) { - CLockObject lock(&m_mutex); - switch (newStatus) - { - case CEC_DEVICE_STATUS_UNKNOWN: - m_iStreamPath = 0; - m_powerStatus = CEC_POWER_STATUS_UNKNOWN; - m_vendor = CEC_VENDOR_UNKNOWN; - m_menuState = CEC_MENU_STATE_ACTIVATED; - m_bActiveSource = false; - m_iLastActive = 0; - m_cecVersion = CEC_VERSION_UNKNOWN; - m_deviceStatus = newStatus; - break; - case CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC: - m_iStreamPath = 0; - m_powerStatus = CEC_POWER_STATUS_ON; - m_vendor = CEC_VENDOR_UNKNOWN; - m_menuState = CEC_MENU_STATE_ACTIVATED; - m_bActiveSource = false; - m_iLastActive = 0; - m_cecVersion = CEC_VERSION_1_3A; - m_deviceStatus = newStatus; - break; - case CEC_DEVICE_STATUS_PRESENT: - case CEC_DEVICE_STATUS_NOT_PRESENT: - m_deviceStatus = newStatus; - break; + if (destination >= 0 && destination < CECDEVICE_BROADCAST) + { + CCECBusDevice *device = m_processor->GetDevice(destination); + if (device) + device->HandlePollFrom(m_iLogicalAddress); } } -void CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress) +void CCECBusDevice::HandlePollFrom(const cec_logical_address initiator) { - CLockObject lock(&m_mutex); - 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); - AddLog(CEC_LOG_DEBUG, strLog.c_str()); - - m_iPhysicalAddress = iNewAddress; - } + LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< POLL: %s (%x) -> %s (%x)", ToString(initiator), initiator, ToString(m_iLogicalAddress), m_iLogicalAddress); + m_bAwaitingReceiveFailed = true; } -void CCECBusDevice::SetStreamPath(uint16_t iNewAddress, uint16_t iOldAddress /* = 0 */) +bool CCECBusDevice::HandleReceiveFailed(void) { - CLockObject lock(&m_mutex); - if (iNewAddress > 0) - { - CStdString strLog; - 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()); + bool bReturn = m_bAwaitingReceiveFailed; + m_bAwaitingReceiveFailed = false; + return bReturn; +} - m_iStreamPath = iNewAddress; +cec_menu_state CCECBusDevice::GetMenuState(const cec_logical_address UNUSED(initiator)) +{ + CLockObject lock(m_mutex); + return m_menuState; +} - if (iNewAddress > 0) - { - lock.Leave(); - SetPowerStatus(CEC_POWER_STATUS_ON); - } +void CCECBusDevice::SetMenuState(const cec_menu_state state) +{ + CLockObject lock(m_mutex); + if (m_menuState != state) + { + LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): menu state set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_menuState)); + m_menuState = state; } } -void CCECBusDevice::SetPowerStatus(const cec_power_status powerStatus) +bool CCECBusDevice::TransmitMenuState(const cec_logical_address dest) { - CLockObject lock(&m_mutex); - if (m_powerStatus != powerStatus) + cec_menu_state menuState; { - m_iLastPowerStateUpdate = GetTimeMs(); - CStdString strLog; - strLog.Format(">> %s (%X): power status changed from '%s' to '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_powerStatus), ToString(powerStatus)); - m_processor->AddLog(CEC_LOG_DEBUG, strLog); - m_powerStatus = powerStatus; + CLockObject lock(m_mutex); + LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): menu state '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString(m_menuState)); + menuState = m_menuState; } + + MarkBusy(); + bool bReturn = m_handler->TransmitMenuState(m_iLogicalAddress, dest, menuState); + MarkReady(); + return bReturn; } -bool CCECBusDevice::ReplaceHandler(bool bActivateSource /* = true */) +bool CCECBusDevice::ActivateSource(void) { - CLockObject lock(&m_mutex); - CLockObject handlerLock(&m_handlerMutex); + MarkAsActiveSource(); + LIB_CEC->AddLog(CEC_LOG_DEBUG, "activating source '%s'", ToString(m_iLogicalAddress)); + MarkBusy(); + bool bReturn = m_handler->ActivateSource(); + MarkReady(); + return bReturn; +} - if (m_vendor != m_handler->GetVendorId()) - { - if (CCECCommandHandler::HasSpecificHandler(m_vendor)) - { - CStdString strLog; - if (m_handler->InUse()) - { - strLog.Format("handler for device '%s' (%x) is being used. not replacing the command handler", GetLogicalAddressName(), GetLogicalAddress()); - m_processor->AddLog(CEC_LOG_DEBUG, strLog); - return false; - } +bool CCECBusDevice::RequestActiveSource(bool bWaitForResponse /* = true */) +{ + bool bReturn(false); - strLog.Format("replacing the command handler for device '%s' (%x)", GetLogicalAddressName(), GetLogicalAddress()); - m_processor->AddLog(CEC_LOG_DEBUG, strLog); - delete m_handler; + if (IsHandledByLibCEC()) + { + MarkBusy(); + LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< requesting active source"); - 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; - } + bReturn = m_handler->TransmitRequestActiveSource(m_iLogicalAddress, bWaitForResponse); + MarkReady(); + } + return bReturn; +} - m_handler->SetVendorId(m_vendor); - m_handler->InitHandler(); +void CCECBusDevice::MarkAsActiveSource(void) +{ + CLockObject lock(m_mutex); + if (!m_bActiveSource) + LIB_CEC->AddLog(CEC_LOG_DEBUG, "making %s (%x) the active source", GetLogicalAddressName(), m_iLogicalAddress); + else + LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%x) was already marked as active source", GetLogicalAddressName(), m_iLogicalAddress); - if (bActivateSource && m_processor->GetLogicalAddresses().IsSet(m_iLogicalAddress) && m_processor->IsInitialised() && IsActiveSource()) - m_handler->ActivateSource(); - } - } + CECDEVICEVEC devices; + m_processor->GetDevices()->Get(devices); + for (CECDEVICEVEC::iterator it = devices.begin(); it != devices.end(); it++) + if ((*it)->GetLogicalAddress() != m_iLogicalAddress) + (*it)->MarkAsInactiveSource(); - return true; + m_bActiveSource = true; + SetPowerStatus(CEC_POWER_STATUS_ON); } -bool CCECBusDevice::SetVendorId(uint64_t iVendorId) +void CCECBusDevice::MarkAsInactiveSource(void) { - bool bVendorChanged(false); - { - CLockObject lock(&m_mutex); - bVendorChanged = (m_vendor != (cec_vendor_id)iVendorId); - m_vendor = (cec_vendor_id)iVendorId; + CLockObject lock(m_mutex); + if (m_bActiveSource) + LIB_CEC->AddLog(CEC_LOG_DEBUG, "marking %s (%X) as inactive source", GetLogicalAddressName(), m_iLogicalAddress); + m_bActiveSource = false; } - - 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()); - - return bVendorChanged; } -//@} -/** @name Transmit methods */ -//@{ bool CCECBusDevice::TransmitActiveSource(void) { bool bSendActiveSource(false); { - CLockObject lock(&m_mutex); - if (m_powerStatus != CEC_POWER_STATUS_ON) - { - CStdString strLog; - strLog.Format("<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress); - AddLog(CEC_LOG_DEBUG, strLog); - } + CLockObject lock(m_mutex); + if (m_powerStatus != CEC_POWER_STATUS_ON && m_powerStatus != CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON) + LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress); else if (m_bActiveSource) { - CStdString strLog; - strLog.Format("<< %s (%X) -> broadcast (F): active source (%4x)", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress); - AddLog(CEC_LOG_NOTICE, strLog); + LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> broadcast (F): active source (%4x)", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress); bSendActiveSource = true; } else - { - CStdString strLog; - strLog.Format("<< %s (%X) is not the active source", GetLogicalAddressName(), m_iLogicalAddress); - AddLog(CEC_LOG_DEBUG, strLog); - } + LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< %s (%X) is not the active source", GetLogicalAddressName(), m_iLogicalAddress); } if (bSendActiveSource) { - m_handler->TransmitImageViewOn(m_iLogicalAddress, CECDEVICE_TV); + MarkBusy(); m_handler->TransmitActiveSource(m_iLogicalAddress, m_iPhysicalAddress); + MarkReady(); return true; } return false; } -bool CCECBusDevice::TransmitCECVersion(cec_logical_address dest) +bool CCECBusDevice::TransmitImageViewOn(void) { - cec_version version; { - CLockObject lock(&m_mutex); - 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); - version = m_cecVersion; + CLockObject lock(m_mutex); + if (m_powerStatus != CEC_POWER_STATUS_ON && m_powerStatus != CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON) + { + LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress); + return false; + } } - return m_handler->TransmitCECVersion(m_iLogicalAddress, dest, version); + MarkBusy(); + m_handler->TransmitImageViewOn(m_iLogicalAddress, CECDEVICE_TV); + MarkReady(); + return true; } bool CCECBusDevice::TransmitInactiveSource(void) { uint16_t iPhysicalAddress; { - CLockObject lock(&m_mutex); - CStdString strLog; - strLog.Format("<< %s (%X) -> broadcast (F): inactive source", GetLogicalAddressName(), m_iLogicalAddress); - AddLog(CEC_LOG_NOTICE, strLog); + CLockObject lock(m_mutex); + LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> broadcast (F): inactive source", GetLogicalAddressName(), m_iLogicalAddress); iPhysicalAddress = m_iPhysicalAddress; } - return m_handler->TransmitInactiveSource(m_iLogicalAddress, iPhysicalAddress); + MarkBusy(); + bool bReturn = m_handler->TransmitInactiveSource(m_iLogicalAddress, iPhysicalAddress); + MarkReady(); + return bReturn; } -bool CCECBusDevice::TransmitMenuState(cec_logical_address dest) +bool CCECBusDevice::TransmitPendingActiveSourceCommands(void) { - cec_menu_state menuState; - { - CLockObject lock(&m_mutex); - CStdString strLog; - strLog.Format("<< %s (%X) -> %s (%X): menu state '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString(m_menuState)); - AddLog(CEC_LOG_NOTICE, strLog); - menuState = m_menuState; - } - - return m_handler->TransmitMenuState(m_iLogicalAddress, dest, menuState); + MarkBusy(); + bool bReturn = m_handler->TransmitPendingActiveSourceCommands(); + MarkReady(); + return bReturn; } -bool CCECBusDevice::TransmitOSDName(cec_logical_address dest) +void CCECBusDevice::SetStreamPath(uint16_t iNewAddress, uint16_t iOldAddress /* = CEC_INVALID_PHYSICAL_ADDRESS */) { - CStdString strDeviceName; + CLockObject lock(m_mutex); + if (iNewAddress != m_iStreamPath) { - CLockObject lock(&m_mutex); - 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()); - strDeviceName = m_strDeviceName; + LIB_CEC->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; } - return m_handler->TransmitOSDName(m_iLogicalAddress, dest, strDeviceName); -} + if (!LIB_CEC->IsValidPhysicalAddress(iNewAddress)) + return; -bool CCECBusDevice::TransmitOSDString(cec_logical_address dest, cec_display_control duration, const char *strMessage) -{ - if (!IsUnsupportedFeature(CEC_OPCODE_SET_OSD_STRING)) + CCECBusDevice *device = m_processor->GetDeviceByPhysicalAddress(iNewAddress); + if (device) { - 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()); - - return m_handler->TransmitOSDString(m_iLogicalAddress, dest, duration, strMessage); + // if a device is found with the new physical address, mark it as active, which will automatically mark all other devices as inactive + device->MarkAsActiveSource(); } - return false; -} - -bool CCECBusDevice::TransmitPhysicalAddress(void) -{ - uint16_t iPhysicalAddress; - cec_device_type type; + else { - CLockObject lock(&m_mutex); - 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()); - - iPhysicalAddress = m_iPhysicalAddress; - type = m_type; + // try to find the device with the old address, and mark it as inactive when found + device = m_processor->GetDeviceByPhysicalAddress(iOldAddress); + if (device) + device->MarkAsInactiveSource(); } - - return m_handler->TransmitPhysicalAddress(m_iLogicalAddress, iPhysicalAddress, type); } -bool CCECBusDevice::TransmitPoll(cec_logical_address dest) +bool CCECBusDevice::PowerOn(const cec_logical_address initiator) { bool bReturn(false); - if (dest == CECDEVICE_UNKNOWN) - dest = m_iLogicalAddress; + GetVendorId(initiator); // ensure that we got the vendor id, because the implementations vary per vendor - CCECBusDevice *destDevice = m_processor->m_busDevices[dest]; - if (destDevice->m_deviceStatus == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC) - return bReturn; - - CStdString strLog; - strLog.Format("<< %s (%X) -> %s (%X): POLL", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest); - AddLog(CEC_LOG_NOTICE, strLog.c_str()); - bReturn = m_handler->TransmitPoll(m_iLogicalAddress, dest); - AddLog(CEC_LOG_DEBUG, bReturn ? ">> POLL sent" : ">> POLL not sent"); - - CLockObject lock(&m_mutex); - if (bReturn) + MarkBusy(); + cec_power_status currentStatus = GetPowerStatus(initiator, false); + if (currentStatus != CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON && + currentStatus != CEC_POWER_STATUS_ON) { - m_iLastActive = GetTimeMs(); - destDevice->m_deviceStatus = CEC_DEVICE_STATUS_PRESENT; + LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< powering on '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); + if (m_handler->PowerOn(initiator, m_iLogicalAddress)) + { + SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); + bReturn = true; + } } else - destDevice->m_deviceStatus = CEC_DEVICE_STATUS_NOT_PRESENT; + { + LIB_CEC->AddLog(CEC_LOG_NOTICE, "'%s' (%X) is already '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(currentStatus)); + } + MarkReady(); return bReturn; } -bool CCECBusDevice::TransmitPowerState(cec_logical_address dest) +bool CCECBusDevice::Standby(const cec_logical_address initiator) { - cec_power_status state; - { - CLockObject lock(&m_mutex); - 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()); - state = m_powerStatus; - } + GetVendorId(initiator); // ensure that we got the vendor id, because the implementations vary per vendor - return m_handler->TransmitPowerState(m_iLogicalAddress, dest, state); + LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< putting '%s' (%X) in standby mode", GetLogicalAddressName(), m_iLogicalAddress); + MarkBusy(); + bool bReturn = m_handler->TransmitStandby(initiator, m_iLogicalAddress); + MarkReady(); + return bReturn; } -bool CCECBusDevice::TransmitVendorID(cec_logical_address dest, bool bSendAbort /* = true */) +bool CCECBusDevice::NeedsPoll(void) { - uint64_t iVendorId; + bool bSendPoll(false); + cec_logical_address pollAddress(CECDEVICE_UNKNOWN); + switch (m_iLogicalAddress) { - CLockObject lock(&m_mutex); - iVendorId = (uint64_t)m_vendor; + case CECDEVICE_PLAYBACKDEVICE3: + pollAddress = CECDEVICE_PLAYBACKDEVICE2; + break; + case CECDEVICE_PLAYBACKDEVICE2: + pollAddress = CECDEVICE_PLAYBACKDEVICE1; + break; + case CECDEVICE_RECORDINGDEVICE3: + pollAddress = CECDEVICE_RECORDINGDEVICE2; + break; + case CECDEVICE_RECORDINGDEVICE2: + pollAddress = CECDEVICE_RECORDINGDEVICE1; + break; + case CECDEVICE_TUNER4: + pollAddress = CECDEVICE_TUNER3; + break; + case CECDEVICE_TUNER3: + pollAddress = CECDEVICE_TUNER2; + break; + case CECDEVICE_TUNER2: + pollAddress = CECDEVICE_TUNER1; + break; + case CECDEVICE_AUDIOSYSTEM: + case CECDEVICE_PLAYBACKDEVICE1: + case CECDEVICE_RECORDINGDEVICE1: + case CECDEVICE_TUNER1: + case CECDEVICE_TV: + bSendPoll = true; + break; + default: + break; } - if (iVendorId == CEC_VENDOR_UNKNOWN) + if (!bSendPoll && pollAddress != CECDEVICE_UNKNOWN) { - if (bSendAbort) + CCECBusDevice *device = m_processor->GetDevice(pollAddress); + if (device) { - CStdString strLog; - strLog.Format("<< %s (%X) -> %s (%X): vendor id feature abort", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest); - AddLog(CEC_LOG_NOTICE, strLog); - - m_processor->TransmitAbort(dest, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID); + cec_bus_device_status status = device->GetStatus(); + bSendPoll = (status == CEC_DEVICE_STATUS_PRESENT || status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC); + } + else + { + bSendPoll = true; } - return false; } - else + + return bSendPoll; +} + +void CCECBusDevice::CheckVendorIdRequested(const cec_logical_address initiator) +{ + bool bRequestVendorId(false); { - CStdString strLog; - strLog.Format("<< %s (%X) -> %s (%X): vendor id %s (%x)", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString((cec_vendor_id)iVendorId), iVendorId); - AddLog(CEC_LOG_NOTICE, strLog); + CLockObject lock(m_mutex); + bRequestVendorId = !m_bVendorIdRequested; + m_bVendorIdRequested = true; + } - return m_handler->TransmitVendorID(m_iLogicalAddress, iVendorId); + if (bRequestVendorId) + { + ReplaceHandler(false); + GetVendorId(initiator); } } +//@} -bool CCECBusDevice::TransmitKeypress(cec_user_control_code key, bool bWait /* = true */) +CCECAudioSystem *CCECBusDevice::AsAudioSystem(void) { - return m_handler->TransmitKeypress(m_processor->GetLogicalAddress(), m_iLogicalAddress, key, bWait); + return AsAudioSystem(this); } -bool CCECBusDevice::TransmitKeyRelease(bool bWait /* = true */) +CCECPlaybackDevice *CCECBusDevice::AsPlaybackDevice(void) { - return m_handler->TransmitKeyRelease(m_processor->GetLogicalAddress(), m_iLogicalAddress, bWait); + return AsPlaybackDevice(this); } -bool CCECBusDevice::IsUnsupportedFeature(cec_opcode opcode) const +CCECRecordingDevice *CCECBusDevice::AsRecordingDevice(void) { - return m_unsupportedFeatures.find(opcode) != m_unsupportedFeatures.end(); + return AsRecordingDevice(this); } -void CCECBusDevice::SetUnsupportedFeature(cec_opcode opcode) +CCECTuner *CCECBusDevice::AsTuner(void) { - m_unsupportedFeatures.insert(opcode); + return AsTuner(this); } -bool CCECBusDevice::ActivateSource(void) +CCECTV *CCECBusDevice::AsTV(void) { - CLockObject lock(&m_mutex); - return m_handler->ActivateSource(); + return AsTV(this); } -//@} +CCECAudioSystem *CCECBusDevice::AsAudioSystem(CCECBusDevice *device) +{ + if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM) + return static_cast(device); + return NULL; +} + +CCECPlaybackDevice *CCECBusDevice::AsPlaybackDevice(CCECBusDevice *device) +{ + if (device && + (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || + device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE)) + return static_cast(device); + return NULL; +} + +CCECRecordingDevice *CCECBusDevice::AsRecordingDevice(CCECBusDevice *device) +{ + if (device && device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE) + return static_cast(device); + return NULL; +} + +CCECTuner *CCECBusDevice::AsTuner(CCECBusDevice *device) +{ + if (device && device->GetType() == CEC_DEVICE_TYPE_TUNER) + return static_cast(device); + return NULL; +} + +CCECTV *CCECBusDevice::AsTV(CCECBusDevice *device) +{ + if (device && device->GetType() == CEC_DEVICE_TYPE_TV) + return static_cast(device); + return NULL; +} + +void CCECBusDevice::MarkBusy(void) +{ + CLockObject handlerLock(m_handlerMutex); + ++m_iHandlerUseCount; +} + +void CCECBusDevice::MarkReady(void) +{ + CLockObject handlerLock(m_handlerMutex); + if (m_iHandlerUseCount > 0) + --m_iHandlerUseCount; +} + +bool CCECBusDevice::TryLogicalAddress(void) +{ + LIB_CEC->AddLog(CEC_LOG_DEBUG, "trying logical address '%s'", GetLogicalAddressName()); + + if (!TransmitPoll(m_iLogicalAddress)) + { + LIB_CEC->AddLog(CEC_LOG_NOTICE, "using logical address '%s'", GetLogicalAddressName()); + SetDeviceStatus(CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC); + + return true; + } + + LIB_CEC->AddLog(CEC_LOG_DEBUG, "logical address '%s' already taken", GetLogicalAddressName()); + SetDeviceStatus(CEC_DEVICE_STATUS_PRESENT); + return false; +} + +CCECClient *CCECBusDevice::GetClient(void) +{ + return m_processor->GetClient(m_iLogicalAddress); +}