X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Flib%2Fdevices%2FCECBusDevice.cpp;h=814f58a63237b9cbb00456bcd54ace9f2b0cdf88;hb=e69d8f191784c77c156ffec90db50767f91421e3;hp=ce0c0869173207df92a639a2b1d06de57a503d3c;hpb=bbc71623d650a4f17057da7d218a824c0c95b3d3;p=deb_libcec.git diff --git a/src/lib/devices/CECBusDevice.cpp b/src/lib/devices/CECBusDevice.cpp index ce0c086..814f58a 100644 --- a/src/lib/devices/CECBusDevice.cpp +++ b/src/lib/devices/CECBusDevice.cpp @@ -60,7 +60,8 @@ CCECBusDevice::CCECBusDevice(CCECProcessor *processor, cec_logical_address iLogi m_cecVersion(CEC_VERSION_UNKNOWN), m_deviceStatus(CEC_DEVICE_STATUS_UNKNOWN), m_iHandlerUseCount(0), - m_bAwaitingReceiveFailed(false) + m_bAwaitingReceiveFailed(false), + m_bVendorIdRequested(false) { m_handler = new CCECCommandHandler(this); @@ -86,7 +87,8 @@ bool CCECBusDevice::HandleCommand(const cec_command &command) 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(); @@ -114,29 +116,23 @@ bool CCECBusDevice::HandleCommand(const cec_command &command) bool CCECBusDevice::PowerOn(void) { bool bReturn(false); - GetVendorId(); // ensure that we got the vendor id + GetVendorId(); // ensure that we got the vendor id, because the implementations vary per vendor MarkBusy(); - CLibCEC::AddLog(CEC_LOG_NOTICE, "<< powering on '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); - if (m_handler->PowerOn(GetMyLogicalAddress(), m_iLogicalAddress)) + 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)) { -// CLockObject lock(m_mutex); -// m_powerStatus = CEC_POWER_STATUS_UNKNOWN; SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); + bReturn = true; } -// 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(); -// } - bReturn = true; + } + else + { + CLibCEC::AddLog(CEC_LOG_NOTICE, "'%s' (%X) is already '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(currentStatus)); } MarkReady(); @@ -156,15 +152,19 @@ bool CCECBusDevice::Standby(void) //@{ cec_version CCECBusDevice::GetCecVersion(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)); + bRequestUpdate = bIsPresent && + (bUpdate || m_cecVersion == CEC_VERSION_UNKNOWN); } if (bRequestUpdate) + { + CheckVendorIdRequested(); RequestCecVersion(); + } CLockObject lock(m_mutex); return m_cecVersion; @@ -174,7 +174,8 @@ bool CCECBusDevice::RequestCecVersion(void) { bool bReturn(false); - if (!MyLogicalAddressContains(m_iLogicalAddress)) + 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); @@ -192,15 +193,19 @@ const char* CCECBusDevice::GetLogicalAddressName(void) const cec_menu_language &CCECBusDevice::GetMenuLanguage(bool bUpdate /* = false */) { + bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT); bool bRequestUpdate(false); { CLockObject lock(m_mutex); - bRequestUpdate = (GetStatus() == CEC_DEVICE_STATUS_PRESENT && + bRequestUpdate = (bIsPresent && (bUpdate || !strcmp(m_menuLanguage.language, "???"))); } if (bRequestUpdate) + { + CheckVendorIdRequested(); RequestMenuLanguage(); + } CLockObject lock(m_mutex); return m_menuLanguage; @@ -239,16 +244,20 @@ uint16_t CCECBusDevice::GetMyPhysicalAddress(void) const CStdString CCECBusDevice::GetOSDName(bool bUpdate /* = false */) { + bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT); bool bRequestUpdate(false); { CLockObject lock(m_mutex); - bRequestUpdate = (GetStatus() == CEC_DEVICE_STATUS_PRESENT && + bRequestUpdate = bIsPresent && (bUpdate || m_strDeviceName.Equals(ToString(m_iLogicalAddress))) && - m_type != CEC_DEVICE_TYPE_TV); + m_type != CEC_DEVICE_TYPE_TV; } if (bRequestUpdate) + { + CheckVendorIdRequested(); RequestOSDName(); + } CLockObject lock(m_mutex); return m_strDeviceName; @@ -271,15 +280,20 @@ bool CCECBusDevice::RequestOSDName(void) uint16_t CCECBusDevice::GetPhysicalAddress(bool bUpdate /* = false */) { + bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT); bool bRequestUpdate(false); { CLockObject lock(m_mutex); - bRequestUpdate = (GetStatus() == CEC_DEVICE_STATUS_PRESENT && - (m_iPhysicalAddress == 0xFFFF || bUpdate)); + bRequestUpdate = bIsPresent && + (m_iPhysicalAddress == 0xFFFF || bUpdate); } - if (bRequestUpdate && !RequestPhysicalAddress()) - CLibCEC::AddLog(CEC_LOG_ERROR, "failed to request the physical address"); + if (bRequestUpdate) + { + CheckVendorIdRequested(); + if (!RequestPhysicalAddress()) + CLibCEC::AddLog(CEC_LOG_ERROR, "failed to request the physical address"); + } CLockObject lock(m_mutex); return m_iPhysicalAddress; @@ -301,10 +315,11 @@ bool CCECBusDevice::RequestPhysicalAddress(void) cec_power_status CCECBusDevice::GetPowerStatus(bool bUpdate /* = false */) { + bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT); bool bRequestUpdate(false); { CLockObject lock(m_mutex); - bRequestUpdate = (GetStatus() == CEC_DEVICE_STATUS_PRESENT && + 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 || @@ -312,7 +327,10 @@ cec_power_status CCECBusDevice::GetPowerStatus(bool bUpdate /* = false */) } if (bRequestUpdate) + { + CheckVendorIdRequested(); RequestPowerStatus(); + } CLockObject lock(m_mutex); return m_powerStatus; @@ -335,10 +353,11 @@ bool CCECBusDevice::RequestPowerStatus(void) cec_vendor_id CCECBusDevice::GetVendorId(bool bUpdate /* = false */) { + bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT); bool bRequestUpdate(false); { CLockObject lock(m_mutex); - bRequestUpdate = (GetStatus() == CEC_DEVICE_STATUS_PRESENT && + bRequestUpdate = (bIsPresent && (bUpdate || m_vendor == CEC_VENDOR_UNKNOWN)); } @@ -438,20 +457,27 @@ bool CCECBusDevice::NeedsPoll(void) cec_bus_device_status CCECBusDevice::GetStatus(bool bForcePoll /* = false */) { - CLockObject lock(m_mutex); - if (m_deviceStatus != CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC && - (m_deviceStatus == CEC_DEVICE_STATUS_UNKNOWN || bForcePoll)) + cec_bus_device_status status(CEC_DEVICE_STATUS_UNKNOWN); + bool bNeedsPoll(false); + + { + CLockObject lock(m_mutex); + status = m_deviceStatus; + bNeedsPoll = (m_deviceStatus != CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC && + (m_deviceStatus == CEC_DEVICE_STATUS_UNKNOWN || bForcePoll)); + } + + if (bNeedsPoll) { - lock.Unlock(); bool bPollAcked(false); - if (bForcePoll || NeedsPoll()) + if (bNeedsPoll && NeedsPoll()) bPollAcked = m_processor->PollDevice(m_iLogicalAddress); - lock.Lock(); - m_deviceStatus = bPollAcked ? CEC_DEVICE_STATUS_PRESENT : CEC_DEVICE_STATUS_NOT_PRESENT; + status = bPollAcked ? CEC_DEVICE_STATUS_PRESENT : CEC_DEVICE_STATUS_NOT_PRESENT; + SetDeviceStatus(status); } - return m_deviceStatus; + return status; } //@} @@ -498,6 +524,8 @@ void CCECBusDevice::SetInactiveSource(void) { { CLockObject lock(m_mutex); + if (m_bActiveSource) + CLibCEC::AddLog(CEC_LOG_DEBUG, "marking %s (%X) as inactive source", GetLogicalAddressName(), m_iLogicalAddress); m_bActiveSource = false; } @@ -543,6 +571,8 @@ void CCECBusDevice::SetDeviceStatus(const cec_bus_device_status newStatus) 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)); m_iStreamPath = 0; m_powerStatus = CEC_POWER_STATUS_UNKNOWN; m_vendor = CEC_VENDOR_UNKNOWN; @@ -553,6 +583,8 @@ void CCECBusDevice::SetDeviceStatus(const cec_bus_device_status newStatus) 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)); m_iStreamPath = 0; m_powerStatus = CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON; m_vendor = CEC_VENDOR_UNKNOWN; @@ -563,7 +595,13 @@ void CCECBusDevice::SetDeviceStatus(const cec_bus_device_status newStatus) 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)); m_deviceStatus = newStatus; break; } @@ -587,6 +625,20 @@ void CCECBusDevice::SetStreamPath(uint16_t iNewAddress, uint16_t iOldAddress /* 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; + CCECBusDevice *device = m_processor->GetDeviceByPhysicalAddress(iNewAddress, false); + 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, false); + if (device) + device->SetInactiveSource(); + } + if (iNewAddress > 0) { lock.Unlock(); @@ -801,7 +853,7 @@ bool CCECBusDevice::TransmitOSDName(cec_logical_address dest) bool CCECBusDevice::TransmitOSDString(cec_logical_address dest, cec_display_control duration, const char *strMessage) { bool bReturn(false); - if (!IsUnsupportedFeature(CEC_OPCODE_SET_OSD_STRING)) + 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(); @@ -865,7 +917,10 @@ bool CCECBusDevice::TransmitPowerState(cec_logical_address dest) { 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); + } CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString(m_powerStatus)); state = m_powerStatus; @@ -923,11 +978,15 @@ bool CCECBusDevice::TransmitKeyRelease(bool bWait /* = true */) bool CCECBusDevice::IsUnsupportedFeature(cec_opcode opcode) const { - return m_unsupportedFeatures.find(opcode) != m_unsupportedFeatures.end(); + bool bUnsupported = (m_unsupportedFeatures.find(opcode) != m_unsupportedFeatures.end()); + if (bUnsupported) + CLibCEC::AddLog(CEC_LOG_NOTICE, "'%s' is marked as unsupported feature for device '%s'", ToString(opcode), GetLogicalAddressName()); + return bUnsupported; } void CCECBusDevice::SetUnsupportedFeature(cec_opcode opcode) { + CLibCEC::AddLog(CEC_LOG_DEBUG, "marking opcode '%s' as unsupported feature for device '%s'", ToString(opcode), GetLogicalAddressName()); m_unsupportedFeatures.insert(opcode); } @@ -941,17 +1000,31 @@ bool CCECBusDevice::ActivateSource(void) void CCECBusDevice::HandlePoll(cec_logical_address iDestination) { - CLockObject lock(m_mutex); CLibCEC::AddLog(CEC_LOG_DEBUG, "<< POLL: %s (%x) -> %s (%x)", ToString(m_iLogicalAddress), m_iLogicalAddress, ToString(iDestination), iDestination); m_bAwaitingReceiveFailed = true; } bool CCECBusDevice::HandleReceiveFailed(void) { - CLockObject lock(m_mutex); 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(); + } +} + //@}