X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Flib%2Fdevices%2FCECBusDevice.cpp;h=328e2b68d60253a782110bfde0f6d0b2fbbab88f;hb=97fc4ffb29ee23862d42f0875ae03ee8fbd7da44;hp=42aa02c6644b480c2c3e9b9f889423b1f42930fc;hpb=00762a71e9bdc59bfbe263d20b33ced5bcea2fb0;p=deb_libcec.git diff --git a/src/lib/devices/CECBusDevice.cpp b/src/lib/devices/CECBusDevice.cpp index 42aa02c..328e2b6 100644 --- a/src/lib/devices/CECBusDevice.cpp +++ b/src/lib/devices/CECBusDevice.cpp @@ -40,7 +40,7 @@ using namespace CEC; -#define ToString(p) CCECCommandHandler::ToString(p) +#define ToString(p) m_processor->ToString(p) CCECBusDevice::CCECBusDevice(CCECProcessor *processor, cec_logical_address iLogicalAddress, uint16_t iPhysicalAddress) : m_type(CEC_DEVICE_TYPE_RESERVED), @@ -50,12 +50,13 @@ CCECBusDevice::CCECBusDevice(CCECProcessor *processor, cec_logical_address iLogi m_powerStatus(CEC_POWER_STATUS_UNKNOWN), m_processor(processor), m_vendor(CEC_VENDOR_UNKNOWN), + m_bReplaceHandler(false), m_menuState(CEC_MENU_STATE_ACTIVATED), m_bActiveSource(false), - m_iLastCommandSent(0), m_iLastActive(0), m_cecVersion(CEC_VERSION_UNKNOWN), - m_deviceStatus(CEC_DEVICE_STATUS_UNKNOWN) + m_deviceStatus(CEC_DEVICE_STATUS_UNKNOWN), + m_handlerMutex(false) { m_handler = new CCECCommandHandler(this); @@ -69,7 +70,6 @@ CCECBusDevice::CCECBusDevice(CCECProcessor *processor, cec_logical_address iLogi CCECBusDevice::~CCECBusDevice(void) { - m_condition.Broadcast(); delete m_handler; } @@ -80,45 +80,64 @@ void CCECBusDevice::AddLog(cec_log_level level, const CStdString &strMessage) bool CCECBusDevice::HandleCommand(const cec_command &command) { - CLockObject lock(&m_transmitMutex); - m_iLastActive = GetTimeMs(); - m_handler->HandleCommand(command); - m_condition.Signal(); - return true; -} + bool bHandled(false); -void CCECBusDevice::PollVendorId(void) -{ - CLockObject lock(&m_transmitMutex); - if (m_iLastActive > 0 && m_iLogicalAddress != CECDEVICE_BROADCAST && - m_vendor == CEC_VENDOR_UNKNOWN && - GetTimeMs() - m_iLastCommandSent > 5000 && - !m_processor->IsMonitoring()) + /* update "last active" */ { - CStdString strLog; - strLog.Format("<< requesting vendor ID of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); - AddLog(CEC_LOG_NOTICE, strLog); - m_iLastCommandSent = GetTimeMs(); + CLockObject lock(&m_mutex); + m_iLastActive = GetTimeMs(); - cec_command command; - cec_command::Format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID); - if (m_processor->Transmit(command)) - m_condition.Wait(&m_transmitMutex, 1000); + if (m_deviceStatus != CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC) + m_deviceStatus = CEC_DEVICE_STATUS_PRESENT; + } + + /* handle the command */ + bHandled = m_handler->HandleCommand(command); + + /* change status to present */ + if (bHandled) + { + CLockObject lock(&m_mutex); + if (m_deviceStatus != CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC) + { + if (m_deviceStatus != CEC_DEVICE_STATUS_PRESENT) + { + CStdString strLog; + strLog.Format("device %s (%x) status changed to present after command %s", GetLogicalAddressName(), (uint8_t)GetLogicalAddress(), ToString(command.opcode)); + AddLog(CEC_LOG_DEBUG, strLog); + } + m_deviceStatus = CEC_DEVICE_STATUS_PRESENT; + } } + + return bHandled; } bool CCECBusDevice::PowerOn(void) { - CStdString strLog; - strLog.Format("<< powering on '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); - AddLog(CEC_LOG_DEBUG, strLog.c_str()); + CStdString strLog; + strLog.Format("<< powering on '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); + AddLog(CEC_LOG_DEBUG, strLog.c_str()); - cec_command command; - cec_command::Format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_IMAGE_VIEW_ON); - if (m_processor->Transmit(command)) - { - GetPowerStatus(); - return true; + if (m_handler->TransmitImageViewOn(GetMyLogicalAddress(), m_iLogicalAddress)) + { + { + CLockObject lock(&m_mutex); +// m_powerStatus = CEC_POWER_STATUS_UNKNOWN; + m_powerStatus = CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON; + } +// cec_power_status status = GetPowerStatus(); +// if (status == CEC_POWER_STATUS_STANDBY || status == CEC_POWER_STATUS_UNKNOWN) +// { +// /* sending the normal power on command appears to have failed */ +// CStdString strLog; +// strLog.Format("<< sending power on keypress to '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); +// AddLog(CEC_LOG_DEBUG, strLog.c_str()); +// +// TransmitKeypress(CEC_USER_CONTROL_CODE_POWER); +// return TransmitKeyRelease(); +// } + return true; } return false; @@ -130,40 +149,38 @@ bool CCECBusDevice::Standby(void) strLog.Format("<< putting '%s' (%X) in standby mode", GetLogicalAddressName(), m_iLogicalAddress); AddLog(CEC_LOG_DEBUG, strLog.c_str()); - cec_command command; - cec_command::Format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_STANDBY); - - return m_processor->Transmit(command); + return m_handler->TransmitStandby(GetMyLogicalAddress(), m_iLogicalAddress); } /** @name Getters */ //@{ -cec_version CCECBusDevice::GetCecVersion(void) +cec_version CCECBusDevice::GetCecVersion(bool bUpdate /* = false */) { - CLockObject lock(&m_mutex); - if (m_cecVersion == CEC_VERSION_UNKNOWN) + bool bRequestUpdate(false); { - lock.Leave(); - RequestCecVersion(); - lock.Lock(); + CLockObject lock(&m_mutex); + bRequestUpdate = (GetStatus() == CEC_DEVICE_STATUS_PRESENT && + (bUpdate || m_cecVersion == CEC_VERSION_UNKNOWN)); } + if (bRequestUpdate) + RequestCecVersion(); + + CLockObject lock(&m_mutex); return m_cecVersion; } bool CCECBusDevice::RequestCecVersion(void) { bool bReturn(false); + if (!MyLogicalAddressContains(m_iLogicalAddress)) { CStdString strLog; strLog.Format("<< requesting CEC version of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); AddLog(CEC_LOG_NOTICE, strLog); - cec_command command; - cec_command::Format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GET_CEC_VERSION); - CLockObject lock(&m_transmitMutex); - if (m_processor->Transmit(command)) - bReturn = m_condition.Wait(&m_transmitMutex, 1000); + + bReturn = m_handler->TransmitRequestCecVersion(GetMyLogicalAddress(), m_iLogicalAddress); } return bReturn; } @@ -173,31 +190,33 @@ 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 (!strcmp(m_menuLanguage.language, "???")) + bool bRequestUpdate(false); { - lock.Leave(); - RequestMenuLanguage(); - lock.Lock(); + CLockObject lock(&m_mutex); + bRequestUpdate = (GetStatus() == CEC_DEVICE_STATUS_PRESENT && + (bUpdate || !strcmp(m_menuLanguage.language, "???"))); } + + if (bRequestUpdate) + RequestMenuLanguage(); + + CLockObject lock(&m_mutex); return m_menuLanguage; } bool CCECBusDevice::RequestMenuLanguage(void) { bool bReturn(false); - if (!MyLogicalAddressContains(m_iLogicalAddress)) + + if (!MyLogicalAddressContains(m_iLogicalAddress) && + !IsUnsupportedFeature(CEC_OPCODE_GET_MENU_LANGUAGE)) { CStdString strLog; strLog.Format("<< requesting menu language of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); AddLog(CEC_LOG_NOTICE, strLog); - cec_command command; - cec_command::Format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GET_MENU_LANGUAGE); - CLockObject lock(&m_transmitMutex); - if (m_processor->Transmit(command)) - bReturn = m_condition.Wait(&m_transmitMutex, 1000); + bReturn = m_handler->TransmitRequestMenuLanguage(GetMyLogicalAddress(), m_iLogicalAddress); } return bReturn; } @@ -212,68 +231,132 @@ uint16_t CCECBusDevice::GetMyPhysicalAddress(void) const return m_processor->GetPhysicalAddress(); } -cec_power_status CCECBusDevice::GetPowerStatus(void) +CStdString CCECBusDevice::GetOSDName(bool bUpdate /* = false */) { + bool bRequestUpdate(false); + { + CLockObject lock(&m_mutex); + bRequestUpdate = (GetStatus() == CEC_DEVICE_STATUS_PRESENT && + (bUpdate || m_strDeviceName.Equals(ToString(m_iLogicalAddress))) && + m_type != CEC_DEVICE_TYPE_TV); + } + + if (bRequestUpdate) + RequestOSDName(); + CLockObject lock(&m_mutex); - if (m_powerStatus == CEC_POWER_STATUS_UNKNOWN) + return m_strDeviceName; +} + +bool CCECBusDevice::RequestOSDName(void) +{ + bool bReturn(false); + + if (!MyLogicalAddressContains(m_iLogicalAddress) && + !IsUnsupportedFeature(CEC_OPCODE_GIVE_OSD_NAME)) { - lock.Leave(); - RequestPowerStatus(); - lock.Lock(); + CStdString strLog; + strLog.Format("<< requesting OSD name of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); + AddLog(CEC_LOG_NOTICE, strLog); + bReturn = m_handler->TransmitRequestOSDName(GetMyLogicalAddress(), m_iLogicalAddress); + } + return bReturn; +} + +uint16_t CCECBusDevice::GetPhysicalAddress(bool bUpdate /* = false */) +{ + bool bRequestUpdate(false); + { + CLockObject lock(&m_mutex); + bRequestUpdate = (GetStatus() == CEC_DEVICE_STATUS_PRESENT && + (m_iPhysicalAddress == 0xFFFF || bUpdate)); + } + + if (bRequestUpdate && !RequestPhysicalAddress()) + AddLog(CEC_LOG_ERROR, "failed to request the physical address (1)"); + + CLockObject lock(&m_mutex); + return m_iPhysicalAddress; +} + +bool CCECBusDevice::RequestPhysicalAddress(void) +{ + bool bReturn(false); + + if (!MyLogicalAddressContains(m_iLogicalAddress)) + { + CStdString strLog; + strLog.Format("<< requesting physical address of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); + AddLog(CEC_LOG_NOTICE, strLog); + bReturn = m_handler->TransmitRequestPhysicalAddress(GetMyLogicalAddress(), m_iLogicalAddress); + } + return bReturn; +} + +cec_power_status CCECBusDevice::GetPowerStatus(bool bUpdate /* = false */) +{ + bool bRequestUpdate(false); + { + CLockObject lock(&m_mutex); + bRequestUpdate = (GetStatus() == CEC_DEVICE_STATUS_PRESENT && + (bUpdate || m_powerStatus == CEC_POWER_STATUS_UNKNOWN)); } + + if (bRequestUpdate) + RequestPowerStatus(); + + CLockObject lock(&m_mutex); return m_powerStatus; } bool CCECBusDevice::RequestPowerStatus(void) { bool bReturn(false); - if (!MyLogicalAddressContains(m_iLogicalAddress)) + + if (!MyLogicalAddressContains(m_iLogicalAddress) && + !IsUnsupportedFeature(CEC_OPCODE_GIVE_DEVICE_POWER_STATUS)) { CStdString strLog; strLog.Format("<< requesting power status of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); AddLog(CEC_LOG_NOTICE, strLog); - cec_command command; - cec_command::Format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS); - CLockObject lock(&m_transmitMutex); - if (m_processor->Transmit(command)) - bReturn = m_condition.Wait(&m_transmitMutex, 1000); + bReturn = m_handler->TransmitRequestPowerStatus(GetMyLogicalAddress(), m_iLogicalAddress); } return bReturn; } -cec_vendor_id CCECBusDevice::GetVendorId(void) +cec_vendor_id CCECBusDevice::GetVendorId(bool bUpdate /* = false */) { - CLockObject lock(&m_mutex); - if (m_vendor == CEC_VENDOR_UNKNOWN) + bool bRequestUpdate(false); { - lock.Leave(); - RequestVendorId(); - lock.Lock(); + CLockObject lock(&m_mutex); + bRequestUpdate = (GetStatus() == CEC_DEVICE_STATUS_PRESENT && + (bUpdate || m_vendor == CEC_VENDOR_UNKNOWN)); } + + if (bRequestUpdate) + RequestVendorId(); + + CLockObject lock(&m_mutex); return m_vendor; } bool CCECBusDevice::RequestVendorId(void) { bool bReturn(false); + if (!MyLogicalAddressContains(m_iLogicalAddress)) { CStdString strLog; strLog.Format("<< requesting vendor ID of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); AddLog(CEC_LOG_NOTICE, strLog); - cec_command command; - cec_command::Format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID); - - CLockObject lock(&m_transmitMutex); - if (m_processor->Transmit(command)) - bReturn = m_condition.Wait(&m_transmitMutex, 1000); + bReturn = m_handler->TransmitRequestVendorId(GetMyLogicalAddress(), m_iLogicalAddress); } 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 @@ -281,15 +364,66 @@ bool CCECBusDevice::MyLogicalAddressContains(cec_logical_address address) const return m_processor->HasLogicalAddress(address); } -cec_bus_device_status CCECBusDevice::GetStatus(void) +bool CCECBusDevice::NeedsPoll(void) +{ + bool bSendPoll(false); + switch (m_iLogicalAddress) + { + case CECDEVICE_PLAYBACKDEVICE3: + if (m_processor->m_busDevices[CECDEVICE_PLAYBACKDEVICE2]->GetStatus() == CEC_DEVICE_STATUS_PRESENT) + bSendPoll = true; + break; + case CECDEVICE_PLAYBACKDEVICE2: + if (m_processor->m_busDevices[CECDEVICE_PLAYBACKDEVICE1]->GetStatus() == CEC_DEVICE_STATUS_PRESENT) + bSendPoll = true; + break; + case CECDEVICE_RECORDINGDEVICE3: + if (m_processor->m_busDevices[CECDEVICE_RECORDINGDEVICE2]->GetStatus() == CEC_DEVICE_STATUS_PRESENT) + bSendPoll = true; + break; + case CECDEVICE_RECORDINGDEVICE2: + if (m_processor->m_busDevices[CECDEVICE_RECORDINGDEVICE1]->GetStatus() == CEC_DEVICE_STATUS_PRESENT) + bSendPoll = true; + break; + case CECDEVICE_TUNER4: + if (m_processor->m_busDevices[CECDEVICE_TUNER3]->GetStatus() == CEC_DEVICE_STATUS_PRESENT) + bSendPoll = true; + break; + case CECDEVICE_TUNER3: + if (m_processor->m_busDevices[CECDEVICE_TUNER2]->GetStatus() == CEC_DEVICE_STATUS_PRESENT) + bSendPoll = true; + break; + case CECDEVICE_TUNER2: + if (m_processor->m_busDevices[CECDEVICE_TUNER1]->GetStatus() == CEC_DEVICE_STATUS_PRESENT) + bSendPoll = true; + break; + case CECDEVICE_AUDIOSYSTEM: + case CECDEVICE_PLAYBACKDEVICE1: + case CECDEVICE_RECORDINGDEVICE1: + case CECDEVICE_TUNER1: + case CECDEVICE_TV: + bSendPoll = true; + break; + default: + break; + } + + return bSendPoll; +} + +cec_bus_device_status CCECBusDevice::GetStatus(bool bForcePoll /* = false */) { CLockObject lock(&m_mutex); - if (m_deviceStatus == CEC_DEVICE_STATUS_UNKNOWN) + if (m_deviceStatus != CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC && + (m_deviceStatus == CEC_DEVICE_STATUS_UNKNOWN || bForcePoll)) { - if (m_processor->PollDevice(m_iLogicalAddress)) - m_deviceStatus = CEC_DEVICE_STATUS_PRESENT; - else - m_deviceStatus = CEC_DEVICE_STATUS_NOT_PRESENT; + lock.Leave(); + bool bPollAcked(false); + if (bForcePoll || NeedsPoll()) + bPollAcked = m_processor->PollDevice(m_iLogicalAddress); + + lock.Lock(); + m_deviceStatus = bPollAcked ? CEC_DEVICE_STATUS_PRESENT : CEC_DEVICE_STATUS_NOT_PRESENT; } return m_deviceStatus; @@ -344,19 +478,24 @@ void CCECBusDevice::SetMenuState(const cec_menu_state state) } } -void CCECBusDevice::SetInactiveDevice(void) +void CCECBusDevice::SetInactiveSource(void) { - CLockObject lock(&m_mutex); - m_bActiveSource = false; + { + CLockObject lock(&m_mutex); + m_bActiveSource = false; + } + + if (MyLogicalAddressContains(m_iLogicalAddress)) + SetPowerStatus(CEC_POWER_STATUS_STANDBY); } -void CCECBusDevice::SetActiveDevice(void) +void CCECBusDevice::SetActiveSource(void) { CLockObject lock(&m_mutex); for (int iPtr = 0; iPtr < 16; iPtr++) if (iPtr != m_iLogicalAddress) - m_processor->m_busDevices[iPtr]->SetInactiveDevice(); + m_processor->m_busDevices[iPtr]->SetInactiveSource(); m_bActiveSource = true; m_powerStatus = CEC_POWER_STATUS_ON; @@ -395,7 +534,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; @@ -406,7 +544,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; @@ -421,7 +558,7 @@ void CCECBusDevice::SetDeviceStatus(const cec_bus_device_status newStatus) void CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress) { CLockObject lock(&m_mutex); - if (iNewAddress > 0) + 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); @@ -437,7 +574,7 @@ void CCECBusDevice::SetStreamPath(uint16_t iNewAddress, uint16_t iOldAddress /* if (iNewAddress > 0) { CStdString strLog; - strLog.Format(">> %s (%X): stream path changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress, iOldAddress, iNewAddress); + 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()); m_iStreamPath = iNewAddress; @@ -462,48 +599,57 @@ void CCECBusDevice::SetPowerStatus(const cec_power_status powerStatus) } } -void CCECBusDevice::SetVendorId(uint64_t iVendorId) +bool CCECBusDevice::ReplaceHandler(bool bInitHandler /* = true */) { + CLockObject lock(&m_mutex); + CLockObject handlerLock(&m_handlerMutex); + + if (m_vendor != m_handler->GetVendorId()) { - CLockObject lock(&m_mutex); - m_vendor = (cec_vendor_id)iVendorId; + if (m_handler->InUse()) + return false; + + delete m_handler; - switch (iVendorId) + switch (m_vendor) { case CEC_VENDOR_SAMSUNG: - if (m_handler->GetVendorId() != CEC_VENDOR_SAMSUNG) - { - delete m_handler; - m_handler = new CANCommandHandler(this); - } + m_handler = new CANCommandHandler(this); break; case CEC_VENDOR_LG: - if (m_handler->GetVendorId() != CEC_VENDOR_LG) - { - delete m_handler; - m_handler = new CSLCommandHandler(this); - } + m_handler = new CSLCommandHandler(this); break; case CEC_VENDOR_PANASONIC: - if (m_handler->GetVendorId() != CEC_VENDOR_PANASONIC) - { - delete m_handler; - m_handler = new CVLCommandHandler(this); - } + m_handler = new CVLCommandHandler(this); break; default: - if (m_handler->GetVendorId() != CEC_VENDOR_UNKNOWN) - { - delete m_handler; - m_handler = new CCECCommandHandler(this); - } + m_handler = new CCECCommandHandler(this); break; } + + if (bInitHandler && m_processor->GetLogicalAddresses().IsSet(m_iLogicalAddress) && m_processor->IsInitialised()) + m_handler->InitHandler(); + } + + return true; +} + +bool CCECBusDevice::SetVendorId(uint64_t iVendorId, bool bInitHandler /* = true */) +{ + bool bVendorChanged(false); + + { + CLockObject lock(&m_mutex); + bVendorChanged = (m_vendor != (cec_vendor_id)iVendorId); + m_vendor = (cec_vendor_id)iVendorId; + ReplaceHandler(bInitHandler); } CStdString strLog; - strLog.Format("%s (%X): vendor = %s (%06x)", GetLogicalAddressName(), m_iLogicalAddress, GetVendorName(), m_vendor); + strLog.Format("%s (%X): vendor = %s (%06x)", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_vendor), m_vendor); m_processor->AddLog(CEC_LOG_DEBUG, strLog.c_str()); + + return bVendorChanged; } //@} @@ -511,32 +657,36 @@ void CCECBusDevice::SetVendorId(uint64_t iVendorId) //@{ bool CCECBusDevice::TransmitActiveSource(void) { - CLockObject lock(&m_mutex); - if (m_powerStatus != CEC_POWER_STATUS_ON) + bool bSendActiveSource(false); + { - CStdString strLog; - strLog.Format("<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress); - AddLog(CEC_LOG_DEBUG, strLog); + 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); + } + 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); + bSendActiveSource = true; + } + else + { + CStdString strLog; + strLog.Format("<< %s (%X) is not the active source", GetLogicalAddressName(), m_iLogicalAddress); + AddLog(CEC_LOG_DEBUG, strLog); + } } - 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); - cec_command command; - cec_command::Format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_ACTIVE_SOURCE); - command.parameters.PushBack((uint8_t) ((m_iPhysicalAddress >> 8) & 0xFF)); - command.parameters.PushBack((uint8_t) (m_iPhysicalAddress & 0xFF)); - - lock.Leave(); - return m_processor->Transmit(command); - } - else + if (bSendActiveSource) { - CStdString strLog; - strLog.Format("<< %s (%X) is not the active source", GetLogicalAddressName(), m_iLogicalAddress); - AddLog(CEC_LOG_DEBUG, strLog); + m_handler->TransmitActiveSource(m_iLogicalAddress, m_iPhysicalAddress); + m_handler->TransmitImageViewOn(m_iLogicalAddress, CECDEVICE_TV); + return true; } return false; @@ -544,98 +694,91 @@ bool CCECBusDevice::TransmitActiveSource(void) bool CCECBusDevice::TransmitCECVersion(cec_logical_address dest) { - 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); - - cec_command command; - cec_command::Format(command, m_iLogicalAddress, dest, CEC_OPCODE_CEC_VERSION); - command.parameters.PushBack((uint8_t)m_cecVersion); + 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); + version = m_cecVersion; + } - lock.Leave(); - return m_processor->Transmit(command); + return m_handler->TransmitCECVersion(m_iLogicalAddress, dest, version); } -bool CCECBusDevice::TransmitInactiveView(void) +bool CCECBusDevice::TransmitInactiveSource(void) { - CStdString strLog; - strLog.Format("<< %s (%X) -> broadcast (F): inactive view", GetLogicalAddressName(), m_iLogicalAddress); - AddLog(CEC_LOG_NOTICE, strLog); - - cec_command command; - cec_command::Format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_INACTIVE_SOURCE); - command.parameters.PushBack((m_iPhysicalAddress >> 8) & 0xFF); - command.parameters.PushBack(m_iPhysicalAddress & 0xFF); + 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); + iPhysicalAddress = m_iPhysicalAddress; + } - return m_processor->Transmit(command); + return m_handler->TransmitInactiveSource(m_iLogicalAddress, iPhysicalAddress); } bool CCECBusDevice::TransmitMenuState(cec_logical_address dest) { - 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); - - cec_command command; - cec_command::Format(command, m_iLogicalAddress, dest, CEC_OPCODE_MENU_STATUS); - command.parameters.PushBack((uint8_t)m_menuState); + 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); + menuState = m_menuState; + } - return m_processor->Transmit(command); + return m_handler->TransmitMenuState(m_iLogicalAddress, dest, menuState); } bool CCECBusDevice::TransmitOSDName(cec_logical_address dest) { - 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()); - - cec_command command; - cec_command::Format(command, m_iLogicalAddress, dest, CEC_OPCODE_SET_OSD_NAME); - for (unsigned int iPtr = 0; iPtr < m_strDeviceName.length(); iPtr++) - command.parameters.PushBack(m_strDeviceName.at(iPtr)); + 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()); + strDeviceName = m_strDeviceName; + } - lock.Leave(); - return m_processor->Transmit(command); + return m_handler->TransmitOSDName(m_iLogicalAddress, dest, strDeviceName); } bool CCECBusDevice::TransmitOSDString(cec_logical_address dest, cec_display_control duration, const char *strMessage) { - CLockObject lock(&m_mutex); - 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()); - - cec_command command; - cec_command::Format(command, m_iLogicalAddress, dest, CEC_OPCODE_SET_OSD_STRING); - command.parameters.PushBack((uint8_t)duration); - - unsigned int iLen = strlen(strMessage); - if (iLen > 13) iLen = 13; - - for (unsigned int iPtr = 0; iPtr < iLen; iPtr++) - command.parameters.PushBack(strMessage[iPtr]); + 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()); - lock.Leave(); - return m_processor->Transmit(command); + return m_handler->TransmitOSDString(m_iLogicalAddress, dest, duration, strMessage); + } + return false; } bool CCECBusDevice::TransmitPhysicalAddress(void) { - CLockObject lock(&m_mutex); - CStdString strLog; - strLog.Format("<< %s (%X) -> broadcast (F): physical adddress %4x", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress); - AddLog(CEC_LOG_NOTICE, strLog.c_str()); + uint16_t iPhysicalAddress; + cec_device_type type; + { + CLockObject lock(&m_mutex); + if (m_iPhysicalAddress == 0xffff) + return false; - cec_command command; - cec_command::Format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS); - command.parameters.PushBack((uint8_t) ((m_iPhysicalAddress >> 8) & 0xFF)); - command.parameters.PushBack((uint8_t) (m_iPhysicalAddress & 0xFF)); - command.parameters.PushBack((uint8_t) (m_type)); + CStdString strLog; + strLog.Format("<< %s (%X) -> broadcast (F): physical adddress %4x", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress); + AddLog(CEC_LOG_NOTICE, strLog.c_str()); - lock.Leave(); - return m_processor->Transmit(command); + iPhysicalAddress = m_iPhysicalAddress; + type = m_type; + } + + return m_handler->TransmitPhysicalAddress(m_iLogicalAddress, iPhysicalAddress, type); } bool CCECBusDevice::TransmitPoll(cec_logical_address dest) @@ -644,62 +787,96 @@ bool CCECBusDevice::TransmitPoll(cec_logical_address dest) if (dest == CECDEVICE_UNKNOWN) dest = m_iLogicalAddress; + CCECBusDevice *destDevice = m_processor->m_busDevices[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()); + bReturn = m_handler->TransmitPoll(m_iLogicalAddress, dest); + AddLog(CEC_LOG_DEBUG, bReturn ? ">> POLL sent" : ">> POLL not sent"); - cec_command command; - cec_command::Format(command, m_iLogicalAddress, dest, CEC_OPCODE_NONE); - CLockObject lock(&m_transmitMutex); + CLockObject lock(&m_mutex); + if (bReturn) + { + m_iLastActive = GetTimeMs(); + destDevice->m_deviceStatus = CEC_DEVICE_STATUS_PRESENT; + } + else + destDevice->m_deviceStatus = CEC_DEVICE_STATUS_NOT_PRESENT; - bReturn = m_processor->Transmit(command); - AddLog(CEC_LOG_DEBUG, bReturn ? ">> POLL sent" : ">> POLL not sent"); return bReturn; } bool CCECBusDevice::TransmitPowerState(cec_logical_address dest) { - 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()); - - cec_command command; - cec_command::Format(command, m_iLogicalAddress, dest, CEC_OPCODE_REPORT_POWER_STATUS); - command.parameters.PushBack((uint8_t) m_powerStatus); + 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()); + state = m_powerStatus; + } - lock.Leave(); - return m_processor->Transmit(command); + return m_handler->TransmitPowerState(m_iLogicalAddress, dest, state); } -bool CCECBusDevice::TransmitVendorID(cec_logical_address dest) +bool CCECBusDevice::TransmitVendorID(cec_logical_address dest, bool bSendAbort /* = true */) { - CLockObject lock(&m_mutex); - if (m_vendor == CEC_VENDOR_UNKNOWN) + uint64_t iVendorId; { - CStdString strLog; - strLog.Format("<< %s (%X) -> %s (%X): vendor id feature abort", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest); - AddLog(CEC_LOG_NOTICE, strLog); + CLockObject lock(&m_mutex); + iVendorId = (uint64_t)m_vendor; + } - lock.Leave(); - m_processor->TransmitAbort(dest, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID); + 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); + + m_processor->TransmitAbort(dest, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID); + } return false; } else { CStdString strLog; - strLog.Format("<< %s (%X) -> %s (%X): vendor id %s (%x)", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString(m_vendor), (uint64_t)m_vendor); + 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); - cec_command command; - cec_command::Format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_DEVICE_VENDOR_ID); + return m_handler->TransmitVendorID(m_iLogicalAddress, iVendorId); + } +} - command.parameters.PushBack((uint8_t) (((uint64_t)m_vendor >> 16) & 0xFF)); - command.parameters.PushBack((uint8_t) (((uint64_t)m_vendor >> 8) & 0xFF)); - command.parameters.PushBack((uint8_t) ((uint64_t)m_vendor & 0xFF)); +bool CCECBusDevice::TransmitKeypress(cec_user_control_code key, bool bWait /* = true */) +{ + return m_handler->TransmitKeypress(m_processor->GetLogicalAddress(), m_iLogicalAddress, key, bWait); +} - lock.Leave(); - return m_processor->Transmit(command); - } +bool CCECBusDevice::TransmitKeyRelease(bool bWait /* = true */) +{ + return m_handler->TransmitKeyRelease(m_processor->GetLogicalAddress(), m_iLogicalAddress, bWait); +} + +bool CCECBusDevice::IsUnsupportedFeature(cec_opcode opcode) const +{ + return m_unsupportedFeatures.find(opcode) != m_unsupportedFeatures.end(); } + +void CCECBusDevice::SetUnsupportedFeature(cec_opcode opcode) +{ + m_unsupportedFeatures.insert(opcode); +} + +bool CCECBusDevice::InitHandler(void) +{ + CLockObject lock(&m_mutex); + return m_handler->InitHandler(); +} + //@}