X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Flib%2Fdevices%2FCECBusDevice.cpp;h=cfdc520b3934f27610faa1c19b2b319ad77bb2dc;hb=ecc676cab7e89d8e576ca1af3b32856240adcbab;hp=ed11d0c325e33d5d8d2d60d9e9b93a0fc6db6c75;hpb=104125dc8316fb58253c34f417ded1e85b22c9f8;p=deb_libcec.git diff --git a/src/lib/devices/CECBusDevice.cpp b/src/lib/devices/CECBusDevice.cpp index ed11d0c..cfdc520 100644 --- a/src/lib/devices/CECBusDevice.cpp +++ b/src/lib/devices/CECBusDevice.cpp @@ -45,22 +45,23 @@ using namespace PLATFORM; #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_iHandlerUseCount(0), - m_bAwaitingReceiveFailed(false) + 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_iHandlerUseCount (0), + 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,27 +116,23 @@ bool CCECBusDevice::HandleCommand(const cec_command &command) bool CCECBusDevice::PowerOn(void) { bool bReturn(false); - CLibCEC::AddLog(CEC_LOG_DEBUG, "<< powering on '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); + GetVendorId(); // ensure that we got the vendor id, because the implementations vary per vendor + MarkBusy(); - if (m_handler->TransmitImageViewOn(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; - m_powerStatus = CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON; + 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(); @@ -154,30 +152,35 @@ 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; } -bool CCECBusDevice::RequestCecVersion(void) +bool CCECBusDevice::RequestCecVersion(bool bWaitForResponse /* = true */) { 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); - bReturn = m_handler->TransmitRequestCecVersion(GetMyLogicalAddress(), m_iLogicalAddress); + bReturn = m_handler->TransmitRequestCecVersion(GetMyLogicalAddress(), m_iLogicalAddress, bWaitForResponse); MarkReady(); } return bReturn; @@ -190,21 +193,25 @@ 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; } -bool CCECBusDevice::RequestMenuLanguage(void) +bool CCECBusDevice::RequestMenuLanguage(bool bWaitForResponse /* = true */) { bool bReturn(false); @@ -213,7 +220,7 @@ bool CCECBusDevice::RequestMenuLanguage(void) { MarkBusy(); CLibCEC::AddLog(CEC_LOG_NOTICE, "<< requesting menu language of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); - bReturn = m_handler->TransmitRequestMenuLanguage(GetMyLogicalAddress(), m_iLogicalAddress); + bReturn = m_handler->TransmitRequestMenuLanguage(GetMyLogicalAddress(), m_iLogicalAddress, bWaitForResponse); MarkReady(); } return bReturn; @@ -237,22 +244,26 @@ 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; } -bool CCECBusDevice::RequestOSDName(void) +bool CCECBusDevice::RequestOSDName(bool bWaitForResponse /* = true */) { bool bReturn(false); @@ -261,29 +272,34 @@ bool CCECBusDevice::RequestOSDName(void) { MarkBusy(); CLibCEC::AddLog(CEC_LOG_NOTICE, "<< requesting OSD name of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); - bReturn = m_handler->TransmitRequestOSDName(GetMyLogicalAddress(), m_iLogicalAddress); + bReturn = m_handler->TransmitRequestOSDName(GetMyLogicalAddress(), m_iLogicalAddress, bWaitForResponse); MarkReady(); } return bReturn; } -uint16_t CCECBusDevice::GetPhysicalAddress(bool bUpdate /* = false */) +uint16_t CCECBusDevice::GetPhysicalAddress(bool bUpdate /* = false */, bool bSuppressPoll /* = false */) { + bool bIsPresent(GetStatus(false, bSuppressPoll) == 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; } -bool CCECBusDevice::RequestPhysicalAddress(void) +bool CCECBusDevice::RequestPhysicalAddress(bool bWaitForResponse /* = true */) { bool bReturn(false); @@ -291,7 +307,7 @@ bool CCECBusDevice::RequestPhysicalAddress(void) { MarkBusy(); CLibCEC::AddLog(CEC_LOG_NOTICE, "<< requesting physical address of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); - bReturn = m_handler->TransmitRequestPhysicalAddress(GetMyLogicalAddress(), m_iLogicalAddress); + bReturn = m_handler->TransmitRequestPhysicalAddress(GetMyLogicalAddress(), m_iLogicalAddress, bWaitForResponse); MarkReady(); } return bReturn; @@ -299,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 || @@ -310,13 +327,16 @@ cec_power_status CCECBusDevice::GetPowerStatus(bool bUpdate /* = false */) } if (bRequestUpdate) + { + CheckVendorIdRequested(); RequestPowerStatus(); + } CLockObject lock(m_mutex); return m_powerStatus; } -bool CCECBusDevice::RequestPowerStatus(void) +bool CCECBusDevice::RequestPowerStatus(bool bWaitForResponse /* = true */) { bool bReturn(false); @@ -325,7 +345,7 @@ bool CCECBusDevice::RequestPowerStatus(void) { MarkBusy(); CLibCEC::AddLog(CEC_LOG_NOTICE, "<< requesting power status of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); - bReturn = m_handler->TransmitRequestPowerStatus(GetMyLogicalAddress(), m_iLogicalAddress); + bReturn = m_handler->TransmitRequestPowerStatus(GetMyLogicalAddress(), m_iLogicalAddress, bWaitForResponse); MarkReady(); } return bReturn; @@ -333,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)); } @@ -347,18 +368,19 @@ cec_vendor_id CCECBusDevice::GetVendorId(bool bUpdate /* = false */) return m_vendor; } -bool CCECBusDevice::RequestVendorId(void) +bool CCECBusDevice::RequestVendorId(bool bWaitForResponse /* = true */) { bool bReturn(false); - if (!MyLogicalAddressContains(m_iLogicalAddress)) + if (!MyLogicalAddressContains(m_iLogicalAddress) && GetMyLogicalAddress() != CECDEVICE_UNKNOWN) { MarkBusy(); CLibCEC::AddLog(CEC_LOG_NOTICE, "<< requesting vendor ID of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); - bReturn = m_handler->TransmitRequestVendorId(GetMyLogicalAddress(), m_iLogicalAddress); + bReturn = m_handler->TransmitRequestVendorId(GetMyLogicalAddress(), m_iLogicalAddress, bWaitForResponse); MarkReady(); - ReplaceHandler(true); + if (bWaitForResponse) + ReplaceHandler(true); } return bReturn; } @@ -379,32 +401,46 @@ bool CCECBusDevice::NeedsPoll(void) switch (m_iLogicalAddress) { case CECDEVICE_PLAYBACKDEVICE3: - if (m_processor->m_busDevices[CECDEVICE_PLAYBACKDEVICE2]->GetStatus() == CEC_DEVICE_STATUS_PRESENT) - bSendPoll = true; + { + cec_bus_device_status status = m_processor->m_busDevices[CECDEVICE_PLAYBACKDEVICE2]->GetStatus(); + bSendPoll = (status == CEC_DEVICE_STATUS_PRESENT || status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC); + } break; case CECDEVICE_PLAYBACKDEVICE2: - if (m_processor->m_busDevices[CECDEVICE_PLAYBACKDEVICE1]->GetStatus() == CEC_DEVICE_STATUS_PRESENT) - bSendPoll = true; + { + cec_bus_device_status status = m_processor->m_busDevices[CECDEVICE_PLAYBACKDEVICE1]->GetStatus(); + bSendPoll = (status == CEC_DEVICE_STATUS_PRESENT || status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC); + } break; case CECDEVICE_RECORDINGDEVICE3: - if (m_processor->m_busDevices[CECDEVICE_RECORDINGDEVICE2]->GetStatus() == CEC_DEVICE_STATUS_PRESENT) - bSendPoll = true; + { + cec_bus_device_status status = m_processor->m_busDevices[CECDEVICE_RECORDINGDEVICE2]->GetStatus(); + bSendPoll = (status == CEC_DEVICE_STATUS_PRESENT || status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC); + } break; case CECDEVICE_RECORDINGDEVICE2: - if (m_processor->m_busDevices[CECDEVICE_RECORDINGDEVICE1]->GetStatus() == CEC_DEVICE_STATUS_PRESENT) - bSendPoll = true; + { + cec_bus_device_status status = m_processor->m_busDevices[CECDEVICE_RECORDINGDEVICE1]->GetStatus(); + bSendPoll = (status == CEC_DEVICE_STATUS_PRESENT || status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC); + } break; case CECDEVICE_TUNER4: - if (m_processor->m_busDevices[CECDEVICE_TUNER3]->GetStatus() == CEC_DEVICE_STATUS_PRESENT) - bSendPoll = true; + { + cec_bus_device_status status = m_processor->m_busDevices[CECDEVICE_TUNER3]->GetStatus(); + bSendPoll = (status == CEC_DEVICE_STATUS_PRESENT || status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC); + } break; case CECDEVICE_TUNER3: - if (m_processor->m_busDevices[CECDEVICE_TUNER2]->GetStatus() == CEC_DEVICE_STATUS_PRESENT) - bSendPoll = true; + { + cec_bus_device_status status = m_processor->m_busDevices[CECDEVICE_TUNER2]->GetStatus(); + bSendPoll = (status == CEC_DEVICE_STATUS_PRESENT || status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC); + } break; case CECDEVICE_TUNER2: - if (m_processor->m_busDevices[CECDEVICE_TUNER1]->GetStatus() == CEC_DEVICE_STATUS_PRESENT) - bSendPoll = true; + { + cec_bus_device_status status = m_processor->m_busDevices[CECDEVICE_TUNER1]->GetStatus(); + bSendPoll = (status == CEC_DEVICE_STATUS_PRESENT || status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC); + } break; case CECDEVICE_AUDIOSYSTEM: case CECDEVICE_PLAYBACKDEVICE1: @@ -420,22 +456,29 @@ bool CCECBusDevice::NeedsPoll(void) return bSendPoll; } -cec_bus_device_status CCECBusDevice::GetStatus(bool bForcePoll /* = false */) +cec_bus_device_status CCECBusDevice::GetStatus(bool bForcePoll /* = false */, bool bSuppressPoll /* = 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 = !bSuppressPoll && + (bForcePoll || m_deviceStatus == CEC_DEVICE_STATUS_UNKNOWN); + } + + 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; } //@} @@ -444,14 +487,16 @@ cec_bus_device_status CCECBusDevice::GetStatus(bool bForcePoll /* = false */) //@{ void CCECBusDevice::SetCecVersion(const cec_version newVersion) { + if (m_cecVersion != newVersion) + CLibCEC::AddLog(CEC_LOG_DEBUG, "%s (%X): CEC version %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(newVersion)); m_cecVersion = newVersion; - 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_mutex); - if (language.device == m_iLogicalAddress) + if (language.device == m_iLogicalAddress && + strcmp(language.language, m_menuLanguage.language)) { CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %s (%X): menu language set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, language.language); m_menuLanguage = language; @@ -482,6 +527,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; } @@ -500,7 +547,7 @@ void CCECBusDevice::SetActiveSource(void) m_processor->m_busDevices[iPtr]->SetInactiveSource(); m_bActiveSource = true; - m_powerStatus = CEC_POWER_STATUS_ON; + SetPowerStatus(CEC_POWER_STATUS_ON); } bool CCECBusDevice::TryLogicalAddress(void) @@ -521,36 +568,61 @@ bool CCECBusDevice::TryLogicalAddress(void) return false; } -void CCECBusDevice::SetDeviceStatus(const cec_bus_device_status newStatus) +void CCECBusDevice::ResetDeviceStatus(void) { 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; + SetPowerStatus (CEC_POWER_STATUS_UNKNOWN); + SetVendorId (CEC_VENDOR_UNKNOWN); + SetMenuState (CEC_MENU_STATE_ACTIVATED); + SetCecVersion (CEC_VERSION_UNKNOWN); + SetStreamPath (0); + SetOSDName (ToString(m_iLogicalAddress)); + SetInactiveSource(); + m_iLastActive = 0; +} + +void CCECBusDevice::SetDeviceStatus(const cec_bus_device_status newStatus) +{ + { + CLockObject lock(m_mutex); + switch (newStatus) + { + case CEC_DEVICE_STATUS_UNKNOWN: + if (m_deviceStatus != newStatus) + CLibCEC::AddLog(CEC_LOG_DEBUG, "device status of %s changed into 'unknown'", ToString(m_iLogicalAddress)); + ResetDeviceStatus(); + m_deviceStatus = newStatus; + break; + case CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC: + if (m_deviceStatus != newStatus) + CLibCEC::AddLog(CEC_LOG_DEBUG, "device status of %s changed into 'handled by libCEC'", ToString(m_iLogicalAddress)); + SetPowerStatus (CEC_POWER_STATUS_ON); + SetVendorId (CEC_VENDOR_UNKNOWN); + SetMenuState (CEC_MENU_STATE_ACTIVATED); + SetCecVersion (CEC_VERSION_1_3A); + SetStreamPath (0); + SetInactiveSource(); + m_iLastActive = 0; + m_deviceStatus = newStatus; + break; + case CEC_DEVICE_STATUS_PRESENT: + if (m_deviceStatus != newStatus) + CLibCEC::AddLog(CEC_LOG_DEBUG, "device status of %s changed into 'present'", ToString(m_iLogicalAddress)); + m_deviceStatus = newStatus; + break; + case CEC_DEVICE_STATUS_NOT_PRESENT: + if (m_deviceStatus != newStatus) + { + CLibCEC::AddLog(CEC_LOG_DEBUG, "device status of %s changed into 'not present'", ToString(m_iLogicalAddress)); + ResetDeviceStatus(); + m_deviceStatus = newStatus; + } + break; + } } + + if (newStatus == CEC_DEVICE_STATUS_PRESENT) + RequestVendorId(false); } void CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress) @@ -571,6 +643,21 @@ 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; + // suppress polls when searching for a device + CCECBusDevice *device = m_processor->GetDeviceByPhysicalAddress(iNewAddress, false, true); + 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, true); + if (device) + device->SetInactiveSource(); + } + if (iNewAddress > 0) { lock.Unlock(); @@ -605,42 +692,44 @@ void CCECBusDevice::MarkReady(void) bool CCECBusDevice::ReplaceHandler(bool bActivateSource /* = true */) { - CTryLockObject lock(m_mutex); - if (!lock.IsLocked()) - return false; + bool bInitHandler(false); + { + CTryLockObject lock(m_mutex); + if (!lock.IsLocked()) + return false; - CLockObject handlerLock(m_handlerMutex); - if (m_iHandlerUseCount > 0) - return false; + CLockObject handlerLock(m_handlerMutex); + if (m_iHandlerUseCount > 0) + return false; - bool bInitHandler(false); - MarkBusy(); + MarkBusy(); - if (m_vendor != m_handler->GetVendorId()) - { - if (CCECCommandHandler::HasSpecificHandler(m_vendor)) + if (m_vendor != m_handler->GetVendorId()) { - CLibCEC::AddLog(CEC_LOG_DEBUG, "replacing the command handler for device '%s' (%x)", GetLogicalAddressName(), GetLogicalAddress()); - delete m_handler; - - switch (m_vendor) + if (CCECCommandHandler::HasSpecificHandler(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; + CLibCEC::AddLog(CEC_LOG_DEBUG, "replacing the command handler for device '%s' (%x)", GetLogicalAddressName(), GetLogicalAddress()); + delete m_handler; + + switch (m_vendor) + { + case CEC_VENDOR_SAMSUNG: + m_handler = new CANCommandHandler(this); + break; + case CEC_VENDOR_LG: + m_handler = new CSLCommandHandler(this); + break; + case CEC_VENDOR_PANASONIC: + m_handler = new CVLCommandHandler(this); + break; + default: + m_handler = new CCECCommandHandler(this); + break; + } + + m_handler->SetVendorId(m_vendor); + bInitHandler = true; } - - m_handler->SetVendorId(m_vendor); - bInitHandler = true; } } @@ -667,7 +756,8 @@ bool CCECBusDevice::SetVendorId(uint64_t iVendorId) m_vendor = (cec_vendor_id)iVendorId; } - CLibCEC::AddLog(CEC_LOG_DEBUG, "%s (%X): vendor = %s (%06x)", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_vendor), m_vendor); + if (bVendorChanged) + CLibCEC::AddLog(CEC_LOG_DEBUG, "%s (%X): vendor = %s (%06x)", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_vendor), m_vendor); return bVendorChanged; } @@ -681,7 +771,7 @@ bool CCECBusDevice::TransmitActiveSource(void) { CLockObject lock(m_mutex); - if (m_powerStatus != CEC_POWER_STATUS_ON) + if (m_powerStatus != CEC_POWER_STATUS_ON && m_powerStatus != CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON) CLibCEC::AddLog(CEC_LOG_DEBUG, "<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress); else if (m_bActiveSource) { @@ -695,7 +785,6 @@ bool CCECBusDevice::TransmitActiveSource(void) if (bSendActiveSource) { MarkBusy(); - m_handler->TransmitImageViewOn(m_iLogicalAddress, CECDEVICE_TV); m_handler->TransmitActiveSource(m_iLogicalAddress, m_iPhysicalAddress); MarkReady(); return true; @@ -719,6 +808,23 @@ bool CCECBusDevice::TransmitCECVersion(cec_logical_address dest) return bReturn; } +bool CCECBusDevice::TransmitImageViewOn(void) +{ + { + CLockObject lock(m_mutex); + if (m_powerStatus != CEC_POWER_STATUS_ON && m_powerStatus != CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON) + { + CLibCEC::AddLog(CEC_LOG_DEBUG, "<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress); + return false; + } + } + + MarkBusy(); + m_handler->TransmitImageViewOn(m_iLogicalAddress, CECDEVICE_TV); + MarkReady(); + return true; +} + bool CCECBusDevice::TransmitInactiveSource(void) { uint16_t iPhysicalAddress; @@ -767,7 +873,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(); @@ -797,6 +903,35 @@ bool CCECBusDevice::TransmitPhysicalAddress(void) return bReturn; } +bool CCECBusDevice::TransmitSetMenuLanguage(cec_logical_address dest) +{ + bool bReturn(false); + cec_menu_language language = GetMenuLanguage(); + + 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] == '?') + { + CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): Menu language feature abort", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest); + m_processor->TransmitAbort(dest, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID); + bReturn = true; + } + else + { + CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> broadcast (F): Menu language '%s'", GetLogicalAddressName(), m_iLogicalAddress, lang); + bReturn = m_handler->TransmitSetMenuLanguage(m_iLogicalAddress, lang); + } + MarkReady(); + return bReturn; +} + bool CCECBusDevice::TransmitPoll(cec_logical_address dest) { bool bReturn(false); @@ -830,6 +965,12 @@ bool CCECBusDevice::TransmitPowerState(cec_logical_address dest) cec_power_status state; { CLockObject lock(m_mutex); + if (!IsActiveSource()) + { + CLibCEC::AddLog(CEC_LOG_NOTICE, "power state requested of %s (%X), but we are not the active source. setting power state to standby", GetLogicalAddressName(), m_iLogicalAddress); + SetPowerStatus(CEC_POWER_STATUS_STANDBY); + } + CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString(m_powerStatus)); state = m_powerStatus; } @@ -884,37 +1025,76 @@ bool CCECBusDevice::TransmitKeyRelease(bool bWait /* = true */) return bReturn; } -bool CCECBusDevice::IsUnsupportedFeature(cec_opcode opcode) const +bool CCECBusDevice::IsUnsupportedFeature(cec_opcode opcode) { - return m_unsupportedFeatures.find(opcode) != m_unsupportedFeatures.end(); + CLockObject lock(m_mutex); + 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) { + CLockObject lock(m_mutex); + CLibCEC::AddLog(CEC_LOG_DEBUG, "marking opcode '%s' as unsupported feature for device '%s'", ToString(opcode), GetLogicalAddressName()); m_unsupportedFeatures.insert(opcode); } bool CCECBusDevice::ActivateSource(void) { + CLibCEC::AddLog(CEC_LOG_DEBUG, "activating source '%s'", ToString(m_iLogicalAddress)); MarkBusy(); bool bReturn = m_handler->ActivateSource(); MarkReady(); return bReturn; } -void CCECBusDevice::HandlePoll(cec_logical_address iDestination) +void CCECBusDevice::HandlePoll(cec_logical_address destination) { - CLockObject lock(m_mutex); - CLibCEC::AddLog(CEC_LOG_DEBUG, "<< POLL: %s (%x) -> %s (%x)", ToString(m_iLogicalAddress), m_iLogicalAddress, ToString(iDestination), iDestination); + if (destination >= 0 && destination < CECDEVICE_BROADCAST) + { + CCECBusDevice *device = m_processor->m_busDevices[destination]; + if (device) + device->HandlePollFrom(m_iLogicalAddress); + } +} + +void CCECBusDevice::HandlePollFrom(cec_logical_address initiator) +{ + CLibCEC::AddLog(CEC_LOG_DEBUG, "<< POLL: %s (%x) -> %s (%x)", ToString(initiator), initiator, ToString(m_iLogicalAddress), m_iLogicalAddress); m_bAwaitingReceiveFailed = true; } bool CCECBusDevice::HandleReceiveFailed(void) { - 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(); + } +} + +bool CCECBusDevice::TransmitPendingActiveSourceCommands(void) +{ + MarkBusy(); + bool bReturn = m_handler->TransmitPendingActiveSourceCommands(); + MarkReady(); + return bReturn; +} + //@}