X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Flib%2Fdevices%2FCECBusDevice.cpp;h=48b75d7dda6ad936424c1801817a73c94e453f6f;hb=102120cb9d6b3c723ed44b22ae1c97cb160e0bcc;hp=5deeddf0649c2b2a2622541959630b3dfbcd7673;hpb=7f9191154db80ab386da77ef6706961d9fbc8b97;p=deb_libcec.git diff --git a/src/lib/devices/CECBusDevice.cpp b/src/lib/devices/CECBusDevice.cpp index 5deeddf..48b75d7 100644 --- a/src/lib/devices/CECBusDevice.cpp +++ b/src/lib/devices/CECBusDevice.cpp @@ -36,9 +36,10 @@ #include "../implementations/CECCommandHandler.h" #include "../implementations/SLCommandHandler.h" #include "../implementations/VLCommandHandler.h" -#include "../platform/timeutils.h" +#include "../LibCEC.h" using namespace CEC; +using namespace PLATFORM; #define ToString(p) m_processor->ToString(p) @@ -50,9 +51,11 @@ CCECBusDevice::CCECBusDevice(CCECProcessor *processor, cec_logical_address iLogi 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) { @@ -71,19 +74,17 @@ CCECBusDevice::~CCECBusDevice(void) 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) { bool bHandled(false); /* update "last active" */ { - CLockObject lock(&m_writeMutex); + CLockObject lock(m_mutex); m_iLastActive = GetTimeMs(); + + if (m_deviceStatus != CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC) + m_deviceStatus = CEC_DEVICE_STATUS_PRESENT; } /* handle the command */ @@ -92,15 +93,11 @@ bool CCECBusDevice::HandleCommand(const cec_command &command) /* change status to present */ if (bHandled) { - CLockObject lock(&m_writeMutex); + 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); - } + 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; } } @@ -110,27 +107,26 @@ bool CCECBusDevice::HandleCommand(const cec_command &command) bool CCECBusDevice::PowerOn(void) { - CStdString strLog; - strLog.Format("<< powering on '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); - AddLog(CEC_LOG_DEBUG, strLog.c_str()); + CLibCEC::AddLog(CEC_LOG_DEBUG, "<< powering on '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); - if (m_handler->TransmitPowerOn(GetMyLogicalAddress(), m_iLogicalAddress)) + if (m_handler->TransmitImageViewOn(GetMyLogicalAddress(), m_iLogicalAddress)) { { - CLockObject lock(&m_mutex); - m_powerStatus = CEC_POWER_STATUS_UNKNOWN; - } - cec_power_status status = GetPowerStatus(); - if (status == CEC_POWER_STATUS_STANDBY || status == CEC_POWER_STATUS_UNKNOWN) - { - /* 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(); + CLockObject lock(m_mutex); +// m_powerStatus = CEC_POWER_STATUS_UNKNOWN; + m_powerStatus = CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON; } +// 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); +// CLibCEC::AddLog(CEC_LOG_DEBUG, strLog.c_str()); +// +// TransmitKeypress(CEC_USER_CONTROL_CODE_POWER); +// return TransmitKeyRelease(); +// } return true; } @@ -139,10 +135,7 @@ bool CCECBusDevice::PowerOn(void) bool CCECBusDevice::Standby(void) { - CStdString strLog; - strLog.Format("<< putting '%s' (%X) in standby mode", GetLogicalAddressName(), m_iLogicalAddress); - AddLog(CEC_LOG_DEBUG, strLog.c_str()); - + CLibCEC::AddLog(CEC_LOG_DEBUG, "<< putting '%s' (%X) in standby mode", GetLogicalAddressName(), m_iLogicalAddress); return m_handler->TransmitStandby(GetMyLogicalAddress(), m_iLogicalAddress); } @@ -150,24 +143,31 @@ bool CCECBusDevice::Standby(void) //@{ cec_version CCECBusDevice::GetCecVersion(bool bUpdate /* = false */) { - CLockObject lock(&m_mutex); - if (GetStatus() == CEC_DEVICE_STATUS_PRESENT && - (bUpdate || m_cecVersion == CEC_VERSION_UNKNOWN)) + bool bRequestUpdate(false); + { + CLockObject lock(m_mutex); + bRequestUpdate = (GetStatus() == CEC_DEVICE_STATUS_PRESENT && + (bUpdate || m_cecVersion == CEC_VERSION_UNKNOWN)); + } + + if (bRequestUpdate) RequestCecVersion(); + CLockObject lock(m_mutex); return m_cecVersion; } bool CCECBusDevice::RequestCecVersion(void) { bool bReturn(false); + if (!MyLogicalAddressContains(m_iLogicalAddress)) { - CStdString strLog; - strLog.Format("<< requesting CEC version of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); - AddLog(CEC_LOG_NOTICE, strLog); + m_handler->MarkBusy(); + CLibCEC::AddLog(CEC_LOG_NOTICE, "<< requesting CEC version of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); bReturn = m_handler->TransmitRequestCecVersion(GetMyLogicalAddress(), m_iLogicalAddress); + m_handler->MarkReady(); } return bReturn; } @@ -179,27 +179,41 @@ const char* CCECBusDevice::GetLogicalAddressName(void) const cec_menu_language &CCECBusDevice::GetMenuLanguage(bool bUpdate /* = false */) { - CLockObject lock(&m_mutex); - if (GetStatus() == CEC_DEVICE_STATUS_PRESENT && - (bUpdate || !strcmp(m_menuLanguage.language, "???"))) + bool bRequestUpdate(false); + { + CLockObject lock(m_mutex); + bRequestUpdate = (GetStatus() == CEC_DEVICE_STATUS_PRESENT && + (bUpdate || !strcmp(m_menuLanguage.language, "???"))); + } + + if (bRequestUpdate) RequestMenuLanguage(); + CLockObject lock(m_mutex); return m_menuLanguage; } bool CCECBusDevice::RequestMenuLanguage(void) { bool bReturn(false); - if (!MyLogicalAddressContains(m_iLogicalAddress)) + + if (!MyLogicalAddressContains(m_iLogicalAddress) && + !IsUnsupportedFeature(CEC_OPCODE_GET_MENU_LANGUAGE)) { - CStdString strLog; - strLog.Format("<< requesting menu language of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); - AddLog(CEC_LOG_NOTICE, strLog); + m_handler->MarkBusy(); + CLibCEC::AddLog(CEC_LOG_NOTICE, "<< requesting menu language of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); bReturn = m_handler->TransmitRequestMenuLanguage(GetMyLogicalAddress(), m_iLogicalAddress); + m_handler->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(); @@ -212,96 +226,128 @@ uint16_t CCECBusDevice::GetMyPhysicalAddress(void) const CStdString CCECBusDevice::GetOSDName(bool bUpdate /* = false */) { - CLockObject lock(&m_mutex); - if (GetStatus() == CEC_DEVICE_STATUS_PRESENT && - (bUpdate || m_strDeviceName.Equals(ToString(m_iLogicalAddress))) && - m_type != CEC_DEVICE_TYPE_TV) + bool bRequestUpdate(false); + { + CLockObject lock(m_mutex); + bRequestUpdate = (GetStatus() == CEC_DEVICE_STATUS_PRESENT && + (bUpdate || m_strDeviceName.Equals(ToString(m_iLogicalAddress))) && + m_type != CEC_DEVICE_TYPE_TV); + } + + if (bRequestUpdate) RequestOSDName(); + CLockObject lock(m_mutex); return m_strDeviceName; } bool CCECBusDevice::RequestOSDName(void) { bool bReturn(false); - if (!MyLogicalAddressContains(m_iLogicalAddress)) + + if (!MyLogicalAddressContains(m_iLogicalAddress) && + !IsUnsupportedFeature(CEC_OPCODE_GIVE_OSD_NAME)) { - CStdString strLog; - strLog.Format("<< requesting OSD name of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); - AddLog(CEC_LOG_NOTICE, strLog); + m_handler->MarkBusy(); + CLibCEC::AddLog(CEC_LOG_NOTICE, "<< requesting OSD name of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); bReturn = m_handler->TransmitRequestOSDName(GetMyLogicalAddress(), m_iLogicalAddress); + m_handler->MarkReady(); } return bReturn; } uint16_t CCECBusDevice::GetPhysicalAddress(bool bUpdate /* = false */) { - CLockObject lock(&m_mutex); - if (GetStatus() == CEC_DEVICE_STATUS_PRESENT && - (m_iPhysicalAddress == 0xFFFF || bUpdate)) + bool bRequestUpdate(false); { - if (!RequestPhysicalAddress()) - AddLog(CEC_LOG_ERROR, "failed to request the physical address"); + CLockObject lock(m_mutex); + bRequestUpdate = (GetStatus() == CEC_DEVICE_STATUS_PRESENT && + (m_iPhysicalAddress == 0xFFFF || bUpdate)); } + if (bRequestUpdate && !RequestPhysicalAddress()) + CLibCEC::AddLog(CEC_LOG_ERROR, "failed to request the physical address"); + + CLockObject lock(m_mutex); return m_iPhysicalAddress; } bool CCECBusDevice::RequestPhysicalAddress(void) { bool bReturn(false); + if (!MyLogicalAddressContains(m_iLogicalAddress)) { - CStdString strLog; - strLog.Format("<< requesting physical address of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); - AddLog(CEC_LOG_NOTICE, strLog); + m_handler->MarkBusy(); + CLibCEC::AddLog(CEC_LOG_NOTICE, "<< requesting physical address of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); bReturn = m_handler->TransmitRequestPhysicalAddress(GetMyLogicalAddress(), m_iLogicalAddress); + m_handler->MarkReady(); } return bReturn; } cec_power_status CCECBusDevice::GetPowerStatus(bool bUpdate /* = false */) { - CLockObject lock(&m_mutex); - if (GetStatus() == CEC_DEVICE_STATUS_PRESENT && - (bUpdate || m_powerStatus == CEC_POWER_STATUS_UNKNOWN)) + bool bRequestUpdate(false); + { + CLockObject lock(m_mutex); + bRequestUpdate = (GetStatus() == CEC_DEVICE_STATUS_PRESENT && + (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) RequestPowerStatus(); + CLockObject lock(m_mutex); return m_powerStatus; } bool CCECBusDevice::RequestPowerStatus(void) { bool bReturn(false); - if (!MyLogicalAddressContains(m_iLogicalAddress)) + + if (!MyLogicalAddressContains(m_iLogicalAddress) && + !IsUnsupportedFeature(CEC_OPCODE_GIVE_DEVICE_POWER_STATUS)) { - CStdString strLog; - strLog.Format("<< requesting power status of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); - AddLog(CEC_LOG_NOTICE, strLog); + m_handler->MarkBusy(); + CLibCEC::AddLog(CEC_LOG_NOTICE, "<< requesting power status of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); bReturn = m_handler->TransmitRequestPowerStatus(GetMyLogicalAddress(), m_iLogicalAddress); + m_handler->MarkReady(); } return bReturn; } cec_vendor_id CCECBusDevice::GetVendorId(bool bUpdate /* = false */) { - CLockObject lock(&m_mutex); - if (GetStatus() == CEC_DEVICE_STATUS_PRESENT && - (bUpdate || m_vendor == CEC_VENDOR_UNKNOWN)) + bool bRequestUpdate(false); + { + CLockObject lock(m_mutex); + bRequestUpdate = (GetStatus() == CEC_DEVICE_STATUS_PRESENT && + (bUpdate || m_vendor == CEC_VENDOR_UNKNOWN)); + } + + if (bRequestUpdate) RequestVendorId(); + CLockObject lock(m_mutex); return m_vendor; } bool CCECBusDevice::RequestVendorId(void) { bool bReturn(false); + if (!MyLogicalAddressContains(m_iLogicalAddress)) { - CStdString strLog; - strLog.Format("<< requesting vendor ID of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); - AddLog(CEC_LOG_NOTICE, strLog); + m_handler->MarkBusy(); + CLibCEC::AddLog(CEC_LOG_NOTICE, "<< requesting vendor ID of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); bReturn = m_handler->TransmitRequestVendorId(GetMyLogicalAddress(), m_iLogicalAddress); + m_handler->MarkReady(); + + ReplaceHandler(true); } return bReturn; } @@ -365,10 +411,11 @@ bool CCECBusDevice::NeedsPoll(void) cec_bus_device_status CCECBusDevice::GetStatus(bool bForcePoll /* = false */) { - CLockObject lock(&m_writeMutex); - if (m_deviceStatus == CEC_DEVICE_STATUS_UNKNOWN || bForcePoll) + CLockObject lock(m_mutex); + if (m_deviceStatus != CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC && + (m_deviceStatus == CEC_DEVICE_STATUS_UNKNOWN || bForcePoll)) { - lock.Leave(); + lock.Unlock(); bool bPollAcked(false); if (bForcePoll || NeedsPoll()) bPollAcked = m_processor->PollDevice(m_iLogicalAddress); @@ -387,61 +434,59 @@ cec_bus_device_status CCECBusDevice::GetStatus(bool bForcePoll /* = false */) 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); + CLibCEC::AddLog(CEC_LOG_DEBUG, "%s (%X): CEC version %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(newVersion)); } void CCECBusDevice::SetMenuLanguage(const cec_menu_language &language) { - CLockObject lock(&m_writeMutex); + CLockObject lock(m_mutex); if (language.device == m_iLogicalAddress) { - 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::SetOSDName(CStdString strName) { - CLockObject lock(&m_writeMutex); + CLockObject lock(m_mutex); if (m_strDeviceName != strName) { - CStdString strLog; - strLog.Format(">> %s (%X): osd name set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, strName); - m_processor->AddLog(CEC_LOG_DEBUG, strLog); + CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %s (%X): osd name set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, strName.c_str()); m_strDeviceName = strName; } } void CCECBusDevice::SetMenuState(const cec_menu_state state) { - CLockObject lock(&m_writeMutex); + CLockObject lock(m_mutex); if (m_menuState != state) { - CStdString strLog; - strLog.Format(">> %s (%X): menu state set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_menuState)); - m_processor->AddLog(CEC_LOG_DEBUG, strLog); + CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %s (%X): menu state set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_menuState)); m_menuState = state; } } -void CCECBusDevice::SetInactiveDevice(void) +void CCECBusDevice::SetInactiveSource(void) { - CLockObject lock(&m_writeMutex); - m_bActiveSource = false; + { + CLockObject lock(m_mutex); + m_bActiveSource = false; + } + + if (MyLogicalAddressContains(m_iLogicalAddress)) + SetPowerStatus(CEC_POWER_STATUS_STANDBY); } -void CCECBusDevice::SetActiveDevice(void) +void CCECBusDevice::SetActiveSource(void) { - CLockObject lock(&m_writeMutex); + CLockObject lock(m_mutex); + if (!m_bActiveSource) + CLibCEC::AddLog(CEC_LOG_DEBUG, "making %s (%x) the active source", GetLogicalAddressName(), m_iLogicalAddress); for (int iPtr = 0; iPtr < 16; iPtr++) if (iPtr != m_iLogicalAddress) - m_processor->m_busDevices[iPtr]->SetInactiveDevice(); + m_processor->m_busDevices[iPtr]->SetInactiveSource(); m_bActiveSource = true; m_powerStatus = CEC_POWER_STATUS_ON; @@ -449,29 +494,25 @@ void CCECBusDevice::SetActiveDevice(void) bool CCECBusDevice::TryLogicalAddress(void) { - CStdString strLog; - strLog.Format("trying logical address '%s'", GetLogicalAddressName()); - AddLog(CEC_LOG_DEBUG, strLog); + CLibCEC::AddLog(CEC_LOG_DEBUG, "trying logical address '%s'", GetLogicalAddressName()); - m_processor->SetAckMask(0x1 << m_iLogicalAddress); + m_processor->SetAckMask(0); if (!TransmitPoll(m_iLogicalAddress)) { - strLog.Format("using logical address '%s'", GetLogicalAddressName()); - AddLog(CEC_LOG_NOTICE, strLog); + CLibCEC::AddLog(CEC_LOG_NOTICE, "using logical address '%s'", GetLogicalAddressName()); SetDeviceStatus(CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC); return true; } - strLog.Format("logical address '%s' already taken", GetLogicalAddressName()); - AddLog(CEC_LOG_DEBUG, strLog); + CLibCEC::AddLog(CEC_LOG_DEBUG, "logical address '%s' already taken", GetLogicalAddressName()); SetDeviceStatus(CEC_DEVICE_STATUS_PRESENT); return false; } void CCECBusDevice::SetDeviceStatus(const cec_bus_device_status newStatus) { - CLockObject lock(&m_writeMutex); + CLockObject lock(m_mutex); switch (newStatus) { case CEC_DEVICE_STATUS_UNKNOWN: @@ -503,31 +544,25 @@ void CCECBusDevice::SetDeviceStatus(const cec_bus_device_status newStatus) void CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress) { - CLockObject lock(&m_writeMutex); + 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()); - + 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 /* = 0 */) { - CLockObject lock(&m_writeMutex); + 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()); - + 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) { - lock.Leave(); + lock.Unlock(); SetPowerStatus(CEC_POWER_STATUS_ON); } } @@ -535,55 +570,72 @@ void CCECBusDevice::SetStreamPath(uint16_t iNewAddress, uint16_t iOldAddress /* void CCECBusDevice::SetPowerStatus(const cec_power_status powerStatus) { - CLockObject lock(&m_writeMutex); + CLockObject lock(m_mutex); if (m_powerStatus != powerStatus) { - 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_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; } } -bool CCECBusDevice::SetVendorId(uint64_t iVendorId, bool bInitHandler /* = true */) +bool CCECBusDevice::ReplaceHandler(bool bActivateSource /* = true */) { - bool bVendorChanged(false); + CLockObject lock(m_mutex); + CLockObject handlerLock(m_handlerMutex); + if (m_vendor != m_handler->GetVendorId()) { - CLockObject lock(&m_writeMutex); - bVendorChanged = (m_vendor != (cec_vendor_id)iVendorId); - m_vendor = (cec_vendor_id)iVendorId; + if (CCECCommandHandler::HasSpecificHandler(m_vendor)) + { + CStdString strLog; + if (m_handler->InUse()) + { + CLibCEC::AddLog(CEC_LOG_DEBUG, "handler for device '%s' (%x) is being used. not replacing the command handler", GetLogicalAddressName(), GetLogicalAddress()); + return false; + } - if (bVendorChanged) + CLibCEC::AddLog(CEC_LOG_DEBUG, "replacing the command handler for device '%s' (%x)", GetLogicalAddressName(), GetLogicalAddress()); delete m_handler; - switch (iVendorId) - { - case CEC_VENDOR_SAMSUNG: - if (bVendorChanged) + switch (m_vendor) + { + case CEC_VENDOR_SAMSUNG: m_handler = new CANCommandHandler(this); - break; - case CEC_VENDOR_LG: - if (bVendorChanged) + break; + case CEC_VENDOR_LG: m_handler = new CSLCommandHandler(this); - break; - case CEC_VENDOR_PANASONIC: - if (bVendorChanged) + break; + case CEC_VENDOR_PANASONIC: m_handler = new CVLCommandHandler(this); - break; - default: - if (bVendorChanged) + break; + default: m_handler = new CCECCommandHandler(this); - break; + break; + } + + m_handler->SetVendorId(m_vendor); + m_handler->InitHandler(); + + if (bActivateSource && m_processor->GetLogicalAddresses().IsSet(m_iLogicalAddress) && m_processor->IsInitialised() && IsActiveSource()) + m_handler->ActivateSource(); } } - if (bVendorChanged && bInitHandler) - m_handler->InitHandler(); + 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; + } - 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()); + CLibCEC::AddLog(CEC_LOG_DEBUG, "%s (%X): vendor = %s (%06x)", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_vendor), m_vendor); return bVendorChanged; } @@ -596,39 +648,34 @@ bool CCECBusDevice::TransmitActiveSource(void) bool bSendActiveSource(false); { - CLockObject lock(&m_writeMutex); + 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); - } + CLibCEC::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); + CLibCEC::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); - } + CLibCEC::AddLog(CEC_LOG_DEBUG, "<< %s (%X) is not the active source", GetLogicalAddressName(), m_iLogicalAddress); } - return bSendActiveSource ? m_handler->TransmitActiveSource(m_iLogicalAddress, m_iPhysicalAddress) : false; + if (bSendActiveSource) + { + m_handler->TransmitImageViewOn(m_iLogicalAddress, CECDEVICE_TV); + m_handler->TransmitActiveSource(m_iLogicalAddress, m_iPhysicalAddress); + return true; + } + + return false; } bool CCECBusDevice::TransmitCECVersion(cec_logical_address dest) { cec_version version; { - CLockObject lock(&m_writeMutex); - CStdString strLog; - strLog.Format("<< %s (%X) -> %s (%X): cec version %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString(m_cecVersion)); - AddLog(CEC_LOG_NOTICE, strLog); + 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; } @@ -639,10 +686,8 @@ bool CCECBusDevice::TransmitInactiveSource(void) { uint16_t iPhysicalAddress; { - CLockObject lock(&m_writeMutex); - CStdString strLog; - strLog.Format("<< %s (%X) -> broadcast (F): inactive source", GetLogicalAddressName(), m_iLogicalAddress); - AddLog(CEC_LOG_NOTICE, strLog); + CLockObject lock(m_mutex); + CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> broadcast (F): inactive source", GetLogicalAddressName(), m_iLogicalAddress); iPhysicalAddress = m_iPhysicalAddress; } @@ -653,10 +698,8 @@ bool CCECBusDevice::TransmitMenuState(cec_logical_address dest) { cec_menu_state menuState; { - CLockObject lock(&m_writeMutex); - 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); + 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; } @@ -667,10 +710,8 @@ bool CCECBusDevice::TransmitOSDName(cec_logical_address dest) { CStdString strDeviceName; { - CLockObject lock(&m_writeMutex); - CStdString strLog; - strLog.Format("<< %s (%X) -> %s (%X): OSD name '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, m_strDeviceName.c_str()); - AddLog(CEC_LOG_NOTICE, strLog.c_str()); + 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; } @@ -679,11 +720,12 @@ bool CCECBusDevice::TransmitOSDName(cec_logical_address dest) 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, ToString(dest), dest, strMessage); - AddLog(CEC_LOG_NOTICE, strLog.c_str()); - - return m_handler->TransmitOSDString(m_iLogicalAddress, dest, duration, strMessage); + if (!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); + return m_handler->TransmitOSDString(m_iLogicalAddress, dest, duration, strMessage); + } + return false; } bool CCECBusDevice::TransmitPhysicalAddress(void) @@ -691,14 +733,11 @@ bool CCECBusDevice::TransmitPhysicalAddress(void) uint16_t iPhysicalAddress; cec_device_type type; { - CLockObject lock(&m_writeMutex); + 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()); - + CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> broadcast (F): physical adddress %4x", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress); iPhysicalAddress = m_iPhysicalAddress; type = m_type; } @@ -712,17 +751,22 @@ bool CCECBusDevice::TransmitPoll(cec_logical_address dest) if (dest == CECDEVICE_UNKNOWN) dest = m_iLogicalAddress; - CStdString strLog; - strLog.Format("<< %s (%X) -> %s (%X): POLL", GetLogicalAddressName(), m_iLogicalAddress, 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; + + CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): POLL", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest); bReturn = m_handler->TransmitPoll(m_iLogicalAddress, dest); - AddLog(CEC_LOG_DEBUG, bReturn ? ">> POLL sent" : ">> POLL not sent"); + CLibCEC::AddLog(CEC_LOG_DEBUG, bReturn ? ">> POLL sent" : ">> POLL not sent"); + CLockObject lock(m_mutex); if (bReturn) { - CLockObject lock(&m_writeMutex); m_iLastActive = GetTimeMs(); + destDevice->m_deviceStatus = CEC_DEVICE_STATUS_PRESENT; } + else + destDevice->m_deviceStatus = CEC_DEVICE_STATUS_NOT_PRESENT; return bReturn; } @@ -731,50 +775,62 @@ bool CCECBusDevice::TransmitPowerState(cec_logical_address dest) { cec_power_status state; { - CLockObject lock(&m_writeMutex); - CStdString strLog; - strLog.Format("<< %s (%X) -> %s (%X): %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString(m_powerStatus)); - AddLog(CEC_LOG_NOTICE, strLog.c_str()); + CLockObject lock(m_mutex); + CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString(m_powerStatus)); state = m_powerStatus; } return m_handler->TransmitPowerState(m_iLogicalAddress, dest, state); } -bool CCECBusDevice::TransmitVendorID(cec_logical_address dest) +bool CCECBusDevice::TransmitVendorID(cec_logical_address dest, bool bSendAbort /* = true */) { uint64_t iVendorId; { - CLockObject lock(&m_writeMutex); + CLockObject lock(m_mutex); iVendorId = (uint64_t)m_vendor; } if (iVendorId == CEC_VENDOR_UNKNOWN) { - 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); + 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); + } return false; } else { - 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); - + 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); return m_handler->TransmitVendorID(m_iLogicalAddress, iVendorId); } } -bool CCECBusDevice::TransmitKeypress(cec_user_control_code key) +bool CCECBusDevice::TransmitKeypress(cec_user_control_code key, bool bWait /* = true */) +{ + return m_handler->TransmitKeypress(m_processor->GetLogicalAddress(), m_iLogicalAddress, key, bWait); +} + +bool CCECBusDevice::TransmitKeyRelease(bool bWait /* = true */) +{ + return m_handler->TransmitKeyRelease(m_processor->GetLogicalAddress(), m_iLogicalAddress, bWait); +} + +bool CCECBusDevice::IsUnsupportedFeature(cec_opcode opcode) const +{ + return m_unsupportedFeatures.find(opcode) != m_unsupportedFeatures.end(); +} + +void CCECBusDevice::SetUnsupportedFeature(cec_opcode opcode) { - return m_handler->TransmitKeypress(m_processor->GetLogicalAddress(), m_iLogicalAddress, key); + m_unsupportedFeatures.insert(opcode); } -bool CCECBusDevice::TransmitKeyRelease(void) +bool CCECBusDevice::ActivateSource(void) { - return m_handler->TransmitKeyRelease(m_processor->GetLogicalAddress(), m_iLogicalAddress); + CLockObject lock(m_mutex); + return m_handler->ActivateSource(); } + //@}