X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Flib%2Fdevices%2FCECBusDevice.cpp;h=3638453125809c92ef2383e4f77cad62202ff7af;hb=6bbfc3f7460c5806ffd80834b3c3c68e0738a2d5;hp=a907fc8fd22d2950c649d8e49123adc52a344443;hpb=f00ff009cfc5dfefdf09ca241b9560e74575b3f5;p=deb_libcec.git diff --git a/src/lib/devices/CECBusDevice.cpp b/src/lib/devices/CECBusDevice.cpp index a907fc8..3638453 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,6 +36,8 @@ #include "../implementations/CECCommandHandler.h" #include "../implementations/SLCommandHandler.h" #include "../implementations/VLCommandHandler.h" +#include "../LibCEC.h" +#include "../platform/util/timeutils.h" using namespace CEC; using namespace PLATFORM; @@ -56,7 +58,9 @@ CCECBusDevice::CCECBusDevice(CCECProcessor *processor, cec_logical_address iLogi m_iLastActive(0), m_iLastPowerStateUpdate(0), m_cecVersion(CEC_VERSION_UNKNOWN), - m_deviceStatus(CEC_DEVICE_STATUS_UNKNOWN) + m_deviceStatus(CEC_DEVICE_STATUS_UNKNOWN), + m_iHandlerUseCount(0), + m_bAwaitingReceiveFailed(false) { m_handler = new CCECCommandHandler(this); @@ -73,11 +77,6 @@ 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); @@ -89,6 +88,8 @@ bool CCECBusDevice::HandleCommand(const cec_command &command) if (m_deviceStatus != CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC) m_deviceStatus = CEC_DEVICE_STATUS_PRESENT; + + MarkBusy(); } /* handle the command */ @@ -101,66 +102,60 @@ bool CCECBusDevice::HandleCommand(const cec_command &command) 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; } } + MarkReady(); return bHandled; } bool CCECBusDevice::PowerOn(void) { - CStdString strLog; - strLog.Format("<< powering on '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); - AddLog(CEC_LOG_DEBUG, strLog.c_str()); + bool bReturn(false); + GetVendorId(); // ensure that we got the vendor id, because the implementations vary per vendor - if (m_handler->TransmitImageViewOn(GetMyLogicalAddress(), m_iLogicalAddress)) + MarkBusy(); + cec_power_status currentStatus = GetPowerStatus(false); + if (currentStatus != CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON && + currentStatus != CEC_POWER_STATUS_ON) { + CLibCEC::AddLog(CEC_LOG_NOTICE, "<< powering on '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); + if (m_handler->PowerOn(GetMyLogicalAddress(), m_iLogicalAddress)) { - 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); -// AddLog(CEC_LOG_DEBUG, strLog.c_str()); -// -// TransmitKeypress(CEC_USER_CONTROL_CODE_POWER); -// return TransmitKeyRelease(); -// } - return true; + } + else + { + CLibCEC::AddLog(CEC_LOG_NOTICE, "'%s' (%X) is already '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(currentStatus)); } - return false; + MarkReady(); + return bReturn; } bool CCECBusDevice::Standby(void) { - CStdString strLog; - strLog.Format("<< putting '%s' (%X) in standby mode", GetLogicalAddressName(), m_iLogicalAddress); - AddLog(CEC_LOG_DEBUG, strLog.c_str()); - - return m_handler->TransmitStandby(GetMyLogicalAddress(), m_iLogicalAddress); + CLibCEC::AddLog(CEC_LOG_DEBUG, "<< putting '%s' (%X) in standby mode", GetLogicalAddressName(), m_iLogicalAddress); + MarkBusy(); + bool bReturn = m_handler->TransmitStandby(GetMyLogicalAddress(), m_iLogicalAddress); + MarkReady(); + return bReturn; } /** @name Getters */ //@{ cec_version CCECBusDevice::GetCecVersion(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) @@ -176,13 +171,11 @@ bool CCECBusDevice::RequestCecVersion(void) if (!MyLogicalAddressContains(m_iLogicalAddress)) { - m_handler->MarkBusy(); - CStdString strLog; - strLog.Format("<< requesting CEC version of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); - AddLog(CEC_LOG_NOTICE, strLog); + 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(); + MarkReady(); } return bReturn; } @@ -194,10 +187,11 @@ 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, "???"))); } @@ -215,12 +209,10 @@ bool CCECBusDevice::RequestMenuLanguage(void) if (!MyLogicalAddressContains(m_iLogicalAddress) && !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); + 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(); + MarkReady(); } return bReturn; } @@ -243,12 +235,13 @@ 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) @@ -265,27 +258,26 @@ bool CCECBusDevice::RequestOSDName(void) if (!MyLogicalAddressContains(m_iLogicalAddress) && !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); + 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(); + MarkReady(); } return bReturn; } 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()) - AddLog(CEC_LOG_ERROR, "failed to request the physical address (1)"); + CLibCEC::AddLog(CEC_LOG_ERROR, "failed to request the physical address"); CLockObject lock(m_mutex); return m_iPhysicalAddress; @@ -297,22 +289,21 @@ bool CCECBusDevice::RequestPhysicalAddress(void) if (!MyLogicalAddressContains(m_iLogicalAddress)) { - m_handler->MarkBusy(); - CStdString strLog; - strLog.Format("<< requesting physical address of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); - AddLog(CEC_LOG_NOTICE, strLog); + 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(); + MarkReady(); } return bReturn; } cec_power_status CCECBusDevice::GetPowerStatus(bool bUpdate /* = false */) { + bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT); bool bRequestUpdate(false); { CLockObject lock(m_mutex); - bRequestUpdate = (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 || @@ -333,22 +324,21 @@ bool CCECBusDevice::RequestPowerStatus(void) if (!MyLogicalAddressContains(m_iLogicalAddress) && !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); + 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(); + MarkReady(); } return bReturn; } 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)); } @@ -365,12 +355,10 @@ bool CCECBusDevice::RequestVendorId(void) if (!MyLogicalAddressContains(m_iLogicalAddress)) { - m_handler->MarkBusy(); - CStdString strLog; - strLog.Format("<< requesting vendor ID of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); - AddLog(CEC_LOG_NOTICE, strLog); + 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(); + MarkReady(); ReplaceHandler(true); } @@ -393,32 +381,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: @@ -459,10 +461,7 @@ 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) @@ -470,9 +469,7 @@ void CCECBusDevice::SetMenuLanguage(const cec_menu_language &language) 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; } } @@ -482,9 +479,7 @@ void CCECBusDevice::SetOSDName(CStdString strName) 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; } } @@ -494,9 +489,7 @@ void CCECBusDevice::SetMenuState(const cec_menu_state state) 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; } } @@ -516,38 +509,30 @@ void CCECBusDevice::SetActiveSource(void) { 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); - } + 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]->SetInactiveSource(); m_bActiveSource = true; - m_powerStatus = CEC_POWER_STATUS_ON; + SetPowerStatus(CEC_POWER_STATUS_ON); } 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; } @@ -558,6 +543,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; @@ -568,8 +555,10 @@ 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_ON; + m_powerStatus = CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON; m_vendor = CEC_VENDOR_UNKNOWN; m_menuState = CEC_MENU_STATE_ACTIVATED; m_bActiveSource = false; @@ -578,7 +567,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; } @@ -589,10 +584,7 @@ void CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress) 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; } } @@ -602,10 +594,7 @@ void CCECBusDevice::SetStreamPath(uint16_t iNewAddress, uint16_t iOldAddress /* 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) @@ -622,58 +611,77 @@ void CCECBusDevice::SetPowerStatus(const cec_power_status powerStatus) if (m_powerStatus != powerStatus) { 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); + 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::ReplaceHandler(bool bActivateSource /* = true */) +void CCECBusDevice::MarkBusy(void) { - CLockObject lock(m_mutex); CLockObject handlerLock(m_handlerMutex); + ++m_iHandlerUseCount; +} + +void CCECBusDevice::MarkReady(void) +{ + CLockObject handlerLock(m_handlerMutex); + if (m_iHandlerUseCount > 0) + --m_iHandlerUseCount; +} - if (m_vendor != m_handler->GetVendorId()) +bool CCECBusDevice::ReplaceHandler(bool bActivateSource /* = true */) +{ + bool bInitHandler(false); { - 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; - } + CTryLockObject lock(m_mutex); + if (!lock.IsLocked()) + return false; - strLog.Format("replacing the command handler for device '%s' (%x)", GetLogicalAddressName(), GetLogicalAddress()); - m_processor->AddLog(CEC_LOG_DEBUG, strLog); - delete m_handler; + CLockObject handlerLock(m_handlerMutex); + if (m_iHandlerUseCount > 0) + return false; - switch (m_vendor) + MarkBusy(); + + if (m_vendor != m_handler->GetVendorId()) + { + 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); - m_handler->InitHandler(); + if (bInitHandler) + { + m_handler->InitHandler(); - if (bActivateSource && m_processor->GetLogicalAddresses().IsSet(m_iLogicalAddress) && m_processor->IsInitialised() && IsActiveSource()) - m_handler->ActivateSource(); - } + if (bActivateSource && m_processor->GetLogicalAddresses().IsSet(m_iLogicalAddress) && m_processor->IsInitialised() && IsActiveSource()) + m_handler->ActivateSource(); } + MarkReady(); + return true; } @@ -687,9 +695,7 @@ bool CCECBusDevice::SetVendorId(uint64_t 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; } @@ -703,31 +709,22 @@ bool CCECBusDevice::TransmitActiveSource(void) { 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); - } + 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) { - 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); } if (bSendActiveSource) { - m_handler->TransmitImageViewOn(m_iLogicalAddress, CECDEVICE_TV); + MarkBusy(); m_handler->TransmitActiveSource(m_iLogicalAddress, m_iPhysicalAddress); + MarkReady(); return true; } @@ -739,13 +736,31 @@ bool CCECBusDevice::TransmitCECVersion(cec_logical_address dest) 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); + CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): cec version %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString(m_cecVersion)); version = m_cecVersion; } - return m_handler->TransmitCECVersion(m_iLogicalAddress, dest, version); + MarkBusy(); + bool bReturn = m_handler->TransmitCECVersion(m_iLogicalAddress, dest, version); + MarkReady(); + 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) @@ -753,13 +768,14 @@ 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); + CLibCEC::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) @@ -767,13 +783,14 @@ bool CCECBusDevice::TransmitMenuState(cec_logical_address dest) 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); + CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): menu state '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString(m_menuState)); menuState = m_menuState; } - return m_handler->TransmitMenuState(m_iLogicalAddress, dest, menuState); + MarkBusy(); + bool bReturn = m_handler->TransmitMenuState(m_iLogicalAddress, dest, menuState); + MarkReady(); + return bReturn; } bool CCECBusDevice::TransmitOSDName(cec_logical_address dest) @@ -781,26 +798,27 @@ bool CCECBusDevice::TransmitOSDName(cec_logical_address dest) CStdString strDeviceName; { 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()); + CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): OSD name '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, m_strDeviceName.c_str()); strDeviceName = m_strDeviceName; } - return m_handler->TransmitOSDName(m_iLogicalAddress, dest, strDeviceName); + MarkBusy(); + bool bReturn = m_handler->TransmitOSDName(m_iLogicalAddress, dest, strDeviceName); + MarkReady(); + return bReturn; } bool CCECBusDevice::TransmitOSDString(cec_logical_address dest, cec_display_control duration, const char *strMessage) { + bool bReturn(false); if (!IsUnsupportedFeature(CEC_OPCODE_SET_OSD_STRING)) { - 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); + CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): display OSD message '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, strMessage); + MarkBusy(); + bReturn = m_handler->TransmitOSDString(m_iLogicalAddress, dest, duration, strMessage); + MarkReady(); } - return false; + return bReturn; } bool CCECBusDevice::TransmitPhysicalAddress(void) @@ -812,15 +830,15 @@ bool CCECBusDevice::TransmitPhysicalAddress(void) 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; } - return m_handler->TransmitPhysicalAddress(m_iLogicalAddress, iPhysicalAddress, type); + MarkBusy(); + bool bReturn = m_handler->TransmitPhysicalAddress(m_iLogicalAddress, iPhysicalAddress, type); + MarkReady(); + return bReturn; } bool CCECBusDevice::TransmitPoll(cec_logical_address dest) @@ -833,11 +851,10 @@ bool CCECBusDevice::TransmitPoll(cec_logical_address 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()); + MarkBusy(); + 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) @@ -848,6 +865,7 @@ bool CCECBusDevice::TransmitPoll(cec_logical_address dest) else destDevice->m_deviceStatus = CEC_DEVICE_STATUS_NOT_PRESENT; + MarkReady(); return bReturn; } @@ -856,53 +874,61 @@ bool CCECBusDevice::TransmitPowerState(cec_logical_address dest) 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()); + if (!IsActiveSource()) + 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; } - return m_handler->TransmitPowerState(m_iLogicalAddress, dest, state); + MarkBusy(); + bool bReturn = m_handler->TransmitPowerState(m_iLogicalAddress, dest, state); + MarkReady(); + return bReturn; } bool CCECBusDevice::TransmitVendorID(cec_logical_address dest, bool bSendAbort /* = true */) { + bool bReturn(false); uint64_t iVendorId; { CLockObject lock(m_mutex); iVendorId = (uint64_t)m_vendor; } + MarkBusy(); if (iVendorId == CEC_VENDOR_UNKNOWN) { if (bSendAbort) { - CStdString strLog; - strLog.Format("<< %s (%X) -> %s (%X): vendor id feature abort", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest); - AddLog(CEC_LOG_NOTICE, strLog); - + CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): vendor id feature abort", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest); m_processor->TransmitAbort(dest, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID); + bReturn = true; } - 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); - - return m_handler->TransmitVendorID(m_iLogicalAddress, iVendorId); + CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): vendor id %s (%x)", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString((cec_vendor_id)iVendorId), iVendorId); + bReturn = m_handler->TransmitVendorID(m_iLogicalAddress, iVendorId); } + MarkReady(); + return bReturn; } bool CCECBusDevice::TransmitKeypress(cec_user_control_code key, bool bWait /* = true */) { - return m_handler->TransmitKeypress(m_processor->GetLogicalAddress(), m_iLogicalAddress, key, bWait); + MarkBusy(); + bool bReturn = m_handler->TransmitKeypress(m_processor->GetLogicalAddress(), m_iLogicalAddress, key, bWait); + MarkReady(); + return bReturn; } bool CCECBusDevice::TransmitKeyRelease(bool bWait /* = true */) { - return m_handler->TransmitKeyRelease(m_processor->GetLogicalAddress(), m_iLogicalAddress, bWait); + MarkBusy(); + bool bReturn = m_handler->TransmitKeyRelease(m_processor->GetLogicalAddress(), m_iLogicalAddress, bWait); + MarkReady(); + return bReturn; } bool CCECBusDevice::IsUnsupportedFeature(cec_opcode opcode) const @@ -917,8 +943,23 @@ void CCECBusDevice::SetUnsupportedFeature(cec_opcode opcode) bool CCECBusDevice::ActivateSource(void) { - CLockObject lock(m_mutex); - return m_handler->ActivateSource(); + MarkBusy(); + bool bReturn = m_handler->ActivateSource(); + MarkReady(); + return bReturn; +} + +void CCECBusDevice::HandlePoll(cec_logical_address iDestination) +{ + 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) +{ + bool bReturn = m_bAwaitingReceiveFailed; + m_bAwaitingReceiveFailed = false; + return bReturn; } //@}