From 7f9191154db80ab386da77ef6706961d9fbc8b97 Mon Sep 17 00:00:00 2001 From: Lars Op den Kamp Date: Mon, 5 Dec 2011 23:45:44 +0100 Subject: [PATCH] cec: couple of cleanups. don't refresh the vendorid when not needed. refresh the power state of devices each minute --- src/LibCecSharp/LibCecSharp.cpp | 8 +-- src/lib/CECProcessor.cpp | 17 +++-- src/lib/CECProcessor.h | 4 +- src/lib/LibCEC.cpp | 4 +- src/lib/devices/CECAudioSystem.cpp | 12 ++-- src/lib/devices/CECBusDevice.cpp | 64 ++++++++++--------- src/lib/devices/CECBusDevice.h | 34 +++++----- src/lib/implementations/CECCommandHandler.cpp | 4 +- src/lib/implementations/CECCommandHandler.h | 4 +- 9 files changed, 80 insertions(+), 71 deletions(-) diff --git a/src/LibCecSharp/LibCecSharp.cpp b/src/LibCecSharp/LibCecSharp.cpp index 03c0009..707248a 100644 --- a/src/LibCecSharp/LibCecSharp.cpp +++ b/src/LibCecSharp/LibCecSharp.cpp @@ -666,14 +666,14 @@ public: return m_libCec->MuteAudio(wait); } - bool SendKeypress(CecLogicalAddress destination, CecUserControlCode key, bool wait) + bool TransmitKeypress(CecLogicalAddress destination, CecUserControlCode key, bool wait) { - return m_libCec->SendKeypress((cec_logical_address)destination, (cec_user_control_code)key, wait); + return m_libCec->TransmitKeypress((cec_logical_address)destination, (cec_user_control_code)key, wait); } - bool SendKeyRelease(CecLogicalAddress destination, bool wait) + bool TransmitKeyRelease(CecLogicalAddress destination, bool wait) { - return m_libCec->SendKeyRelease((cec_logical_address)destination, wait); + return m_libCec->TransmitKeyRelease((cec_logical_address)destination, wait); } String ^ GetOSDName(CecLogicalAddress logicalAddress) diff --git a/src/lib/CECProcessor.cpp b/src/lib/CECProcessor.cpp index 81f501b..af45f9a 100644 --- a/src/lib/CECProcessor.cpp +++ b/src/lib/CECProcessor.cpp @@ -952,14 +952,14 @@ bool CCECProcessor::SetAckMask(uint16_t iMask) return bReturn; } -bool CCECProcessor::SendKeypress(cec_logical_address iDestination, cec_user_control_code key) +bool CCECProcessor::TransmitKeypress(cec_logical_address iDestination, cec_user_control_code key) { - return m_busDevices[iDestination]->SendKeypress(key); + return m_busDevices[iDestination]->TransmitKeypress(key); } -bool CCECProcessor::SendKeyRelease(cec_logical_address iDestination) +bool CCECProcessor::TransmitKeyRelease(cec_logical_address iDestination) { - return m_busDevices[iDestination]->SendKeyRelease(); + return m_busDevices[iDestination]->TransmitKeyRelease(); } const char *CCECProcessor::ToString(const cec_menu_state state) @@ -1297,8 +1297,15 @@ void *CCECBusScan::Process(void) if (device && device->GetStatus(true) == CEC_DEVICE_STATUS_PRESENT) { if (!IsStopped()) + { device->GetVendorId(); - Sleep(5); + Sleep(5); + } + if (!IsStopped()) + { + device->GetPowerStatus(true); + Sleep(5); + } } } } diff --git a/src/lib/CECProcessor.h b/src/lib/CECProcessor.h index 2a8a589..b725119 100644 --- a/src/lib/CECProcessor.h +++ b/src/lib/CECProcessor.h @@ -89,8 +89,8 @@ namespace CEC virtual uint8_t VolumeUp(void); virtual uint8_t VolumeDown(void); virtual uint8_t MuteAudio(void); - virtual bool SendKeypress(cec_logical_address iDestination, cec_user_control_code key); - virtual bool SendKeyRelease(cec_logical_address iDestination); + virtual bool TransmitKeypress(cec_logical_address iDestination, cec_user_control_code key); + virtual bool TransmitKeyRelease(cec_logical_address iDestination); virtual bool EnablePhysicalAddressDetection(void) { return false; }; void SetStandardLineTimeout(uint8_t iTimeout); void SetRetryLineTimeout(uint8_t iTimeout); diff --git a/src/lib/LibCEC.cpp b/src/lib/LibCEC.cpp index 3ed7ace..ebaae2d 100644 --- a/src/lib/LibCEC.cpp +++ b/src/lib/LibCEC.cpp @@ -284,14 +284,14 @@ uint8_t CLibCEC::MuteAudio(bool bWait /* = true */) bool CLibCEC::SendKeypress(cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = false */) { if (m_cec) - return m_cec->SendKeypress(iDestination, key); + return m_cec->TransmitKeypress(iDestination, key); return false; } bool CLibCEC::SendKeyRelease(cec_logical_address iDestination, bool bWait /* = false */) { if (m_cec) - return m_cec->SendKeyRelease(iDestination); + return m_cec->TransmitKeyRelease(iDestination); return false; } diff --git a/src/lib/devices/CECAudioSystem.cpp b/src/lib/devices/CECAudioSystem.cpp index 38bdb47..81a92c3 100644 --- a/src/lib/devices/CECAudioSystem.cpp +++ b/src/lib/devices/CECAudioSystem.cpp @@ -122,8 +122,8 @@ bool CCECAudioSystem::TransmitSystemAudioModeStatus(cec_logical_address dest) uint8_t CCECAudioSystem::VolumeUp(void) { - if (SendKeypress(CEC_USER_CONTROL_CODE_VOLUME_UP)) - SendKeyRelease(); + if (TransmitKeypress(CEC_USER_CONTROL_CODE_VOLUME_UP)) + TransmitKeyRelease(); CLockObject lock(&m_mutex); return m_audioStatus; @@ -131,8 +131,8 @@ uint8_t CCECAudioSystem::VolumeUp(void) uint8_t CCECAudioSystem::VolumeDown(void) { - if (SendKeypress(CEC_USER_CONTROL_CODE_VOLUME_DOWN)) - SendKeyRelease(); + if (TransmitKeypress(CEC_USER_CONTROL_CODE_VOLUME_DOWN)) + TransmitKeyRelease(); CLockObject lock(&m_mutex); return m_audioStatus; @@ -140,8 +140,8 @@ uint8_t CCECAudioSystem::VolumeDown(void) uint8_t CCECAudioSystem::MuteAudio(void) { - if (SendKeypress(CEC_USER_CONTROL_CODE_MUTE)) - SendKeyRelease(); + if (TransmitKeypress(CEC_USER_CONTROL_CODE_MUTE)) + TransmitKeyRelease(); CLockObject lock(&m_mutex); return m_audioStatus; diff --git a/src/lib/devices/CECBusDevice.cpp b/src/lib/devices/CECBusDevice.cpp index a794dc0..5deeddf 100644 --- a/src/lib/devices/CECBusDevice.cpp +++ b/src/lib/devices/CECBusDevice.cpp @@ -52,7 +52,6 @@ CCECBusDevice::CCECBusDevice(CCECProcessor *processor, cec_logical_address iLogi m_vendor(CEC_VENDOR_UNKNOWN), m_menuState(CEC_MENU_STATE_ACTIVATED), m_bActiveSource(false), - m_iLastCommandSent(0), m_iLastActive(0), m_cecVersion(CEC_VERSION_UNKNOWN), m_deviceStatus(CEC_DEVICE_STATUS_UNKNOWN) @@ -79,13 +78,19 @@ void CCECBusDevice::AddLog(cec_log_level level, const CStdString &strMessage) bool CCECBusDevice::HandleCommand(const cec_command &command) { + bool bHandled(false); + + /* update "last active" */ { CLockObject lock(&m_writeMutex); m_iLastActive = GetTimeMs(); } - m_handler->HandleCommand(command); + /* handle the command */ + bHandled = m_handler->HandleCommand(command); + /* change status to present */ + if (bHandled) { CLockObject lock(&m_writeMutex); if (m_deviceStatus != CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC) @@ -99,7 +104,8 @@ bool CCECBusDevice::HandleCommand(const cec_command &command) m_deviceStatus = CEC_DEVICE_STATUS_PRESENT; } } - return true; + + return bHandled; } bool CCECBusDevice::PowerOn(void) @@ -117,8 +123,13 @@ bool CCECBusDevice::PowerOn(void) cec_power_status status = GetPowerStatus(); if (status == CEC_POWER_STATUS_STANDBY || status == CEC_POWER_STATUS_UNKNOWN) { - SendKeypress(CEC_USER_CONTROL_CODE_POWER); - return SendKeyRelease(); + /* 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; } @@ -137,11 +148,11 @@ bool CCECBusDevice::Standby(void) /** @name Getters */ //@{ -cec_version CCECBusDevice::GetCecVersion(void) +cec_version CCECBusDevice::GetCecVersion(bool bUpdate /* = false */) { CLockObject lock(&m_mutex); if (GetStatus() == CEC_DEVICE_STATUS_PRESENT && - m_cecVersion == CEC_VERSION_UNKNOWN) + (bUpdate || m_cecVersion == CEC_VERSION_UNKNOWN)) RequestCecVersion(); return m_cecVersion; @@ -161,21 +172,16 @@ bool CCECBusDevice::RequestCecVersion(void) return bReturn; } -uint64_t CCECBusDevice::GetLastCommandSent(void) const -{ - return GetTimeMs() - m_iLastCommandSent; -} - const char* CCECBusDevice::GetLogicalAddressName(void) const { return ToString(m_iLogicalAddress); } -cec_menu_language &CCECBusDevice::GetMenuLanguage(void) +cec_menu_language &CCECBusDevice::GetMenuLanguage(bool bUpdate /* = false */) { CLockObject lock(&m_mutex); if (GetStatus() == CEC_DEVICE_STATUS_PRESENT && - !strcmp(m_menuLanguage.language, "???")) + (bUpdate || !strcmp(m_menuLanguage.language, "???"))) RequestMenuLanguage(); return m_menuLanguage; @@ -204,11 +210,11 @@ uint16_t CCECBusDevice::GetMyPhysicalAddress(void) const return m_processor->GetPhysicalAddress(); } -CStdString CCECBusDevice::GetOSDName(void) +CStdString CCECBusDevice::GetOSDName(bool bUpdate /* = false */) { CLockObject lock(&m_mutex); if (GetStatus() == CEC_DEVICE_STATUS_PRESENT && - m_strDeviceName.Equals(ToString(m_iLogicalAddress)) && + (bUpdate || m_strDeviceName.Equals(ToString(m_iLogicalAddress))) && m_type != CEC_DEVICE_TYPE_TV) RequestOSDName(); @@ -228,11 +234,11 @@ bool CCECBusDevice::RequestOSDName(void) return bReturn; } -uint16_t CCECBusDevice::GetPhysicalAddress(bool bRefresh /* = true */) +uint16_t CCECBusDevice::GetPhysicalAddress(bool bUpdate /* = false */) { CLockObject lock(&m_mutex); if (GetStatus() == CEC_DEVICE_STATUS_PRESENT && - (m_iPhysicalAddress == 0xFFFF || bRefresh)) + (m_iPhysicalAddress == 0xFFFF || bUpdate)) { if (!RequestPhysicalAddress()) AddLog(CEC_LOG_ERROR, "failed to request the physical address"); @@ -254,11 +260,11 @@ bool CCECBusDevice::RequestPhysicalAddress(void) return bReturn; } -cec_power_status CCECBusDevice::GetPowerStatus(void) +cec_power_status CCECBusDevice::GetPowerStatus(bool bUpdate /* = false */) { CLockObject lock(&m_mutex); if (GetStatus() == CEC_DEVICE_STATUS_PRESENT && - m_powerStatus == CEC_POWER_STATUS_UNKNOWN) + (bUpdate || m_powerStatus == CEC_POWER_STATUS_UNKNOWN)) RequestPowerStatus(); return m_powerStatus; @@ -277,11 +283,11 @@ bool CCECBusDevice::RequestPowerStatus(void) return bReturn; } -cec_vendor_id CCECBusDevice::GetVendorId(void) +cec_vendor_id CCECBusDevice::GetVendorId(bool bUpdate /* = false */) { CLockObject lock(&m_mutex); if (GetStatus() == CEC_DEVICE_STATUS_PRESENT && - m_vendor == CEC_VENDOR_UNKNOWN) + (bUpdate || m_vendor == CEC_VENDOR_UNKNOWN)) RequestVendorId(); return m_vendor; @@ -300,9 +306,9 @@ bool CCECBusDevice::RequestVendorId(void) return bReturn; } -const char *CCECBusDevice::GetVendorName(void) +const char *CCECBusDevice::GetVendorName(bool bUpdate /* = false */) { - return ToString(GetVendorId()); + return ToString(GetVendorId(bUpdate)); } bool CCECBusDevice::MyLogicalAddressContains(cec_logical_address address) const @@ -474,7 +480,6 @@ void CCECBusDevice::SetDeviceStatus(const cec_bus_device_status newStatus) m_vendor = CEC_VENDOR_UNKNOWN; m_menuState = CEC_MENU_STATE_ACTIVATED; m_bActiveSource = false; - m_iLastCommandSent = 0; m_iLastActive = 0; m_cecVersion = CEC_VERSION_UNKNOWN; m_deviceStatus = newStatus; @@ -485,7 +490,6 @@ void CCECBusDevice::SetDeviceStatus(const cec_bus_device_status newStatus) m_vendor = CEC_VENDOR_UNKNOWN; m_menuState = CEC_MENU_STATE_ACTIVATED; m_bActiveSource = false; - m_iLastCommandSent = 0; m_iLastActive = 0; m_cecVersion = CEC_VERSION_1_3A; m_deviceStatus = newStatus; @@ -764,13 +768,13 @@ bool CCECBusDevice::TransmitVendorID(cec_logical_address dest) } } -bool CCECBusDevice::SendKeypress(cec_user_control_code key) +bool CCECBusDevice::TransmitKeypress(cec_user_control_code key) { - return m_handler->SendKeypress(m_processor->GetLogicalAddress(), m_iLogicalAddress, key); + return m_handler->TransmitKeypress(m_processor->GetLogicalAddress(), m_iLogicalAddress, key); } -bool CCECBusDevice::SendKeyRelease(void) +bool CCECBusDevice::TransmitKeyRelease(void) { - return m_handler->SendKeyRelease(m_processor->GetLogicalAddress(), m_iLogicalAddress); + return m_handler->TransmitKeyRelease(m_processor->GetLogicalAddress(), m_iLogicalAddress); } //@} diff --git a/src/lib/devices/CECBusDevice.h b/src/lib/devices/CECBusDevice.h index 98e9431..6806df0 100644 --- a/src/lib/devices/CECBusDevice.h +++ b/src/lib/devices/CECBusDevice.h @@ -53,32 +53,24 @@ namespace CEC virtual bool PowerOn(void); virtual bool Standby(void); - virtual cec_version GetCecVersion(void); + virtual cec_version GetCecVersion(bool bUpdate = false); virtual CCECCommandHandler * GetHandler(void) const { return m_handler; }; - virtual uint64_t GetLastCommandSent(void) const; virtual uint64_t GetLastActive(void) const { return m_iLastActive; } virtual cec_logical_address GetLogicalAddress(void) const { return m_iLogicalAddress; } virtual const char* GetLogicalAddressName(void) const; - virtual cec_menu_language & GetMenuLanguage(void); + virtual cec_menu_language & GetMenuLanguage(bool bUpdate = false); virtual cec_logical_address GetMyLogicalAddress(void) const; virtual uint16_t GetMyPhysicalAddress(void) const; - virtual CStdString GetOSDName(void); - virtual uint16_t GetPhysicalAddress(bool bRefresh = true); - virtual cec_power_status GetPowerStatus(void); + virtual CStdString GetOSDName(bool bUpdate = false); + virtual uint16_t GetPhysicalAddress(bool bUpdate = false); + virtual cec_power_status GetPowerStatus(bool bUpdate = false); virtual CCECProcessor * GetProcessor(void) const { return m_processor; } virtual cec_device_type GetType(void) const { return m_type; } - virtual cec_vendor_id GetVendorId(void); - virtual const char * GetVendorName(void); + virtual cec_vendor_id GetVendorId(bool bUpdate = false); + virtual const char * GetVendorName(bool bUpdate = false); virtual bool MyLogicalAddressContains(cec_logical_address address) const; virtual cec_bus_device_status GetStatus(bool bForcePoll = false); - bool RequestCecVersion(void); - bool RequestMenuLanguage(void); - bool RequestPowerStatus(void); - bool RequestVendorId(void); - bool RequestPhysicalAddress(void); - bool RequestOSDName(void); - virtual void SetInactiveDevice(void); virtual void SetActiveDevice(void); virtual bool TryLogicalAddress(void); @@ -103,10 +95,17 @@ namespace CEC virtual bool TransmitPowerState(cec_logical_address dest); virtual bool TransmitPoll(cec_logical_address dest); virtual bool TransmitVendorID(cec_logical_address dest); - virtual bool SendKeypress(cec_user_control_code key); - virtual bool SendKeyRelease(void); + virtual bool TransmitKeypress(cec_user_control_code key); + virtual bool TransmitKeyRelease(void); protected: + bool RequestCecVersion(void); + bool RequestMenuLanguage(void); + bool RequestPowerStatus(void); + bool RequestVendorId(void); + bool RequestPhysicalAddress(void); + bool RequestOSDName(void); + bool NeedsPoll(void); cec_device_type m_type; @@ -121,7 +120,6 @@ namespace CEC cec_vendor_id m_vendor; cec_menu_state m_menuState; bool m_bActiveSource; - uint64_t m_iLastCommandSent; uint64_t m_iLastActive; cec_version m_cecVersion; cec_bus_device_status m_deviceStatus; diff --git a/src/lib/implementations/CECCommandHandler.cpp b/src/lib/implementations/CECCommandHandler.cpp index 8e62eaa..47a0b0e 100644 --- a/src/lib/implementations/CECCommandHandler.cpp +++ b/src/lib/implementations/CECCommandHandler.cpp @@ -874,7 +874,7 @@ bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator return Transmit(command); } -bool CCECCommandHandler::SendKeypress(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_user_control_code key) +bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_user_control_code key) { cec_command command; cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_USER_CONTROL_PRESSED, m_iTransmitTimeout); @@ -883,7 +883,7 @@ bool CCECCommandHandler::SendKeypress(const cec_logical_address iInitiator, cons return Transmit(command); } -bool CCECCommandHandler::SendKeyRelease(const cec_logical_address iInitiator, const cec_logical_address iDestination) +bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator, const cec_logical_address iDestination) { cec_command command; cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_USER_CONTROL_RELEASE, m_iTransmitTimeout); diff --git a/src/lib/implementations/CECCommandHandler.h b/src/lib/implementations/CECCommandHandler.h index 58dc67e..15ab552 100644 --- a/src/lib/implementations/CECCommandHandler.h +++ b/src/lib/implementations/CECCommandHandler.h @@ -76,8 +76,8 @@ namespace CEC virtual bool TransmitSetSystemAudioMode(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_system_audio_status state); virtual bool TransmitSystemAudioModeStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_system_audio_status state); virtual bool TransmitDeckStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_deck_info state); - virtual bool SendKeypress(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_user_control_code key); - virtual bool SendKeyRelease(const cec_logical_address iInitiator, const cec_logical_address iDestination); + virtual bool TransmitKeypress(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_user_control_code key); + virtual bool TransmitKeyRelease(const cec_logical_address iInitiator, const cec_logical_address iDestination); protected: virtual bool HandleActiveSource(const cec_command &command); -- 2.34.1