X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Flib%2Fimplementations%2FCECCommandHandler.cpp;h=2fe03080c7911fb3acb4bd062fb8b9e6f203a045;hb=5f3167157997a0d64fd8a44ad04babf43327eec8;hp=a33a577b0606367ebee5da10c32a142cb7523747;hpb=fcf10e27f023e8e128d407331d0156f9a6f1baa5;p=deb_libcec.git diff --git a/src/lib/implementations/CECCommandHandler.cpp b/src/lib/implementations/CECCommandHandler.cpp index a33a577..2fe0308 100644 --- a/src/lib/implementations/CECCommandHandler.cpp +++ b/src/lib/implementations/CECCommandHandler.cpp @@ -39,20 +39,30 @@ using namespace CEC; using namespace std; -CCECCommandHandler::CCECCommandHandler(CCECBusDevice *busDevice) +CCECCommandHandler::CCECCommandHandler(CCECBusDevice *busDevice) : + m_busDevice(busDevice), + m_processor(m_busDevice->GetProcessor()), + m_iTransmitTimeout(CEC_DEFAULT_TRANSMIT_TIMEOUT), + m_iTransmitWait(CEC_DEFAULT_TRANSMIT_WAIT), + m_iTransmitRetries(CEC_DEFAULT_TRANSMIT_RETRIES) { - m_busDevice = busDevice; - m_processor = m_busDevice->GetProcessor(); +} + +CCECCommandHandler::~CCECCommandHandler(void) +{ + m_condition.Broadcast(); } bool CCECCommandHandler::HandleCommand(const cec_command &command) { - bool bHandled(true); + bool bHandled(true), bHandlerChanged(false); CStdString strLog; strLog.Format(">> %s (%X) -> %s (%X): %s (%2X)", m_processor->ToString(command.initiator), command.initiator, m_processor->ToString(command.destination), command.destination, m_processor->ToString(command.opcode), command.opcode); m_busDevice->AddLog(CEC_LOG_NOTICE, strLog); + m_processor->AddCommand(command); + switch(command.opcode) { case CEC_OPCODE_REPORT_POWER_STATUS: @@ -74,7 +84,7 @@ bool CCECCommandHandler::HandleCommand(const cec_command &command) HandleGiveDeviceVendorId(command); break; case CEC_OPCODE_DEVICE_VENDOR_ID: - HandleDeviceVendorId(command); + bHandlerChanged = HandleDeviceVendorId(command); break; case CEC_OPCODE_VENDOR_COMMAND_WITH_ID: HandleDeviceVendorCommandWithId(command); @@ -110,13 +120,13 @@ bool CCECCommandHandler::HandleCommand(const cec_command &command) HandleSystemAudioModeRequest(command); break; case CEC_OPCODE_REPORT_AUDIO_STATUS: - HandleReportAudioStatus(command);//YYY + HandleReportAudioStatus(command); break; case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS: - HandleSystemAudioModeStatus(command);//YYY + HandleSystemAudioModeStatus(command); break; case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE: - HandleSetSystemAudioMode(command);//YYY + HandleSetSystemAudioMode(command); break; case CEC_OPCODE_REQUEST_ACTIVE_SOURCE: HandleRequestActiveSource(command); @@ -154,7 +164,12 @@ bool CCECCommandHandler::HandleCommand(const cec_command &command) break; } - m_processor->AddCommand(command); + if (bHandled && !bHandlerChanged) + { + CLockObject lock(&m_receiveMutex); + m_condition.Signal(); + } + return bHandled; } @@ -195,7 +210,7 @@ bool CCECCommandHandler::HandleDeviceCecVersion(const cec_command &command) bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command &command) { - if (m_busDevice->MyLogicalAddressContains(command.destination)) + if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination)) m_processor->TransmitAbort(command.initiator, command.opcode, CEC_ABORT_REASON_REFUSED); return true; @@ -203,13 +218,12 @@ bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command &comm bool CCECCommandHandler::HandleDeviceVendorId(const cec_command &command) { - SetVendorId(command); - return true; + return SetVendorId(command); } bool CCECCommandHandler::HandleGetCecVersion(const cec_command &command) { - if (m_busDevice->MyLogicalAddressContains(command.destination)) + if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination)) { CCECBusDevice *device = GetDevice(command.destination); if (device) @@ -221,7 +235,7 @@ bool CCECCommandHandler::HandleGetCecVersion(const cec_command &command) bool CCECCommandHandler::HandleGiveAudioStatus(const cec_command &command) { - if (m_busDevice->MyLogicalAddressContains(command.destination)) + if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination)) { CCECBusDevice *device = GetDevice(command.destination); if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM) @@ -233,7 +247,7 @@ bool CCECCommandHandler::HandleGiveAudioStatus(const cec_command &command) bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command &command) { - if (m_busDevice->MyLogicalAddressContains(command.destination)) + if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination)) { CCECBusDevice *device = GetDevice(command.destination); if (device && (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE)) @@ -245,7 +259,7 @@ bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command &command) bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command) { - if (m_busDevice->MyLogicalAddressContains(command.destination)) + if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination)) { CCECBusDevice *device = GetDevice(command.destination); if (device) @@ -257,7 +271,7 @@ bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command) bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command &command) { - if (m_busDevice->MyLogicalAddressContains(command.destination)) + if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination)) { CCECBusDevice *device = GetDevice(command.destination); if (device) @@ -269,7 +283,7 @@ bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command &command) bool CCECCommandHandler::HandleGiveOSDName(const cec_command &command) { - if (m_busDevice->MyLogicalAddressContains(command.destination)) + if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination)) { CCECBusDevice *device = GetDevice(command.destination); if (device) @@ -281,7 +295,7 @@ bool CCECCommandHandler::HandleGiveOSDName(const cec_command &command) bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command &command) { - if (m_busDevice->MyLogicalAddressContains(command.destination)) + if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination)) { CCECBusDevice *device = GetDevice(command.destination); if (device) @@ -293,7 +307,7 @@ bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command &command) bool CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command &command) { - if (m_busDevice->MyLogicalAddressContains(command.destination)) + if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination)) { CCECBusDevice *device = GetDevice(command.destination); if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM) @@ -311,7 +325,7 @@ bool CCECCommandHandler::HandleImageViewOn(const cec_command &command) bool CCECCommandHandler::HandleMenuRequest(const cec_command &command) { - if (m_busDevice->MyLogicalAddressContains(command.destination)) + if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination)) { if (command.parameters[0] == CEC_MENU_REQUEST_TYPE_QUERY) { @@ -361,15 +375,19 @@ bool CCECCommandHandler::HandleReportPowerStatus(const cec_command &command) bool CCECCommandHandler::HandleRequestActiveSource(const cec_command &command) { - CStdString strLog; - strLog.Format(">> %i requests active source", (uint8_t) command.initiator); - m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str()); + if (m_processor->IsStarted()) + { + CStdString strLog; + strLog.Format(">> %i requests active source", (uint8_t) command.initiator); + m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str()); - vector devices; - for (int iDevicePtr = (int)GetMyDevices(devices)-1; iDevicePtr >=0; iDevicePtr--) - devices[iDevicePtr]->TransmitActiveSource(); + vector devices; + for (int iDevicePtr = (int)GetMyDevices(devices)-1; iDevicePtr >=0; iDevicePtr--) + devices[iDevicePtr]->TransmitActiveSource(); - return true; + return true; + } + return false; } bool CCECCommandHandler::HandleRoutingChange(const cec_command &command) @@ -439,7 +457,7 @@ bool CCECCommandHandler::HandleSetOSDName(const cec_command &command) bool CCECCommandHandler::HandleSetStreamPath(const cec_command &command) { - if (command.parameters.size >= 2) + if (m_processor->IsStarted() && command.parameters.size >= 2) { uint16_t iStreamAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]); CStdString strLog; @@ -461,7 +479,7 @@ bool CCECCommandHandler::HandleSetStreamPath(const cec_command &command) bool CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command &command) { - if (m_busDevice->MyLogicalAddressContains(command.destination)) + if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination)) { CCECBusDevice *device = GetDevice(command.destination); if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM) @@ -533,7 +551,7 @@ bool CCECCommandHandler::HandleTextViewOn(const cec_command &command) bool CCECCommandHandler::HandleUserControlPressed(const cec_command &command) { - if (m_busDevice->MyLogicalAddressContains(command.destination) && command.parameters.size > 0) + if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination) && command.parameters.size > 0) { m_processor->AddKey(); @@ -560,7 +578,7 @@ bool CCECCommandHandler::HandleUserControlPressed(const cec_command &command) bool CCECCommandHandler::HandleUserControlRelease(const cec_command &command) { - if (m_busDevice->MyLogicalAddressContains(command.destination)) + if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination)) m_processor->AddKey(); return true; @@ -610,12 +628,13 @@ CCECBusDevice *CCECCommandHandler::GetDeviceByType(cec_device_type type) const return m_processor->GetDeviceByType(type); } -void CCECCommandHandler::SetVendorId(const cec_command &command) +bool CCECCommandHandler::SetVendorId(const cec_command &command) { + bool bChanged(false); if (command.parameters.size < 3) { m_busDevice->AddLog(CEC_LOG_WARNING, "invalid vendor ID received"); - return; + return bChanged; } uint64_t iVendorId = ((uint64_t)command.parameters[0] << 16) + @@ -624,7 +643,8 @@ void CCECCommandHandler::SetVendorId(const cec_command &command) CCECBusDevice *device = GetDevice((cec_logical_address) command.initiator); if (device) - device->SetVendorId(iVendorId); + bChanged = device->SetVendorId(iVendorId); + return bChanged; } void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress, uint16_t iNewAddress) @@ -642,3 +662,254 @@ void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress, uint16 } } } + +void CCECCommandHandler::HandlePoll(const cec_logical_address iInitiator, const cec_logical_address iDestination) +{ + CStdString strLog; + strLog.Format("<< POLL: %s (%x) -> %s (%x)", m_processor->ToString(iInitiator), iInitiator, m_processor->ToString(iDestination), iDestination); + m_processor->AddLog(CEC_LOG_DEBUG, strLog); +} + +bool CCECCommandHandler::HandleReceiveFailed(void) +{ + /* default = error */ + return true; +} + +bool CCECCommandHandler::TransmitPowerOn(const cec_logical_address iInitiator, const cec_logical_address iDestination) +{ + cec_command command; + cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_IMAGE_VIEW_ON); + + return Transmit(command); +} + +bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator, const cec_logical_address iDestination) +{ + cec_command command; + cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_STANDBY); + + return Transmit(command); +} + +bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator, const cec_logical_address iDestination) +{ + cec_command command; + cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GET_CEC_VERSION); + + return Transmit(command); +} + +bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator, const cec_logical_address iDestination) +{ + cec_command command; + cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GET_MENU_LANGUAGE); + + return Transmit(command); +} + +bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator, const cec_logical_address iDestination) +{ + cec_command command; + cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_OSD_NAME); + + return Transmit(command); +} + +bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator, const cec_logical_address iDestination) +{ + cec_command command; + cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS); + + return Transmit(command); +} + +bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination) +{ + cec_command command; + cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS); + + return Transmit(command); +} + +bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator, const cec_logical_address iDestination) +{ + cec_command command; + cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID); + + return Transmit(command); +} + +bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress) +{ + cec_command command; + cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_ACTIVE_SOURCE); + command.parameters.PushBack((uint8_t) ((iPhysicalAddress >> 8) & 0xFF)); + command.parameters.PushBack((uint8_t) (iPhysicalAddress & 0xFF)); + + return Transmit(command); +} + +bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_version cecVersion) +{ + cec_command command; + cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_CEC_VERSION); + command.parameters.PushBack((uint8_t)cecVersion); + + return Transmit(command); +} + +bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress) +{ + cec_command command; + cec_command::Format(command, iInitiator, CECDEVICE_TV, CEC_OPCODE_INACTIVE_SOURCE); + command.parameters.PushBack((iPhysicalAddress >> 8) & 0xFF); + command.parameters.PushBack(iPhysicalAddress & 0xFF); + + return Transmit(command); +} + +bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_menu_state menuState) +{ + cec_command command; + cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_MENU_STATUS); + command.parameters.PushBack((uint8_t)menuState); + + return Transmit(command); +} + +bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator, const cec_logical_address iDestination, CStdString strDeviceName) +{ + cec_command command; + cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_NAME); + for (unsigned int iPtr = 0; iPtr < strDeviceName.length(); iPtr++) + command.parameters.PushBack(strDeviceName.at(iPtr)); + + return Transmit(command); +} + +bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_display_control duration, const char *strMessage) +{ + cec_command command; + cec_command::Format(command, iInitiator, iDestination, 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]); + + return Transmit(command); +} + +bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator, uint16_t iPhysicalAddress, cec_device_type type) +{ + cec_command command; + cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS); + command.parameters.PushBack((uint8_t) ((iPhysicalAddress >> 8) & 0xFF)); + command.parameters.PushBack((uint8_t) (iPhysicalAddress & 0xFF)); + command.parameters.PushBack((uint8_t) (type)); + + return Transmit(command); +} + +bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator, const cec_logical_address iDestination) +{ + cec_command command; + cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_NONE); + + return Transmit(command, false); +} + +bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_power_status state) +{ + cec_command command; + cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_REPORT_POWER_STATUS); + command.parameters.PushBack((uint8_t) state); + + return Transmit(command); +} + +bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator, uint64_t iVendorId) +{ + cec_command command; + cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_DEVICE_VENDOR_ID); + + command.parameters.PushBack((uint8_t) (((uint64_t)iVendorId >> 16) & 0xFF)); + command.parameters.PushBack((uint8_t) (((uint64_t)iVendorId >> 8) & 0xFF)); + command.parameters.PushBack((uint8_t) ((uint64_t)iVendorId & 0xFF)); + + return Transmit(command); +} + +bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, uint8_t state) +{ + cec_command command; + cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_REPORT_AUDIO_STATUS); + command.parameters.PushBack(state); + + return Transmit(command); +} + +bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_system_audio_status state) +{ + cec_command command; + cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE); + command.parameters.PushBack((uint8_t)state); + + return Transmit(command); +} + +bool CCECCommandHandler::TransmitSystemAudioModeStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_system_audio_status state) +{ + cec_command command; + cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS); + command.parameters.PushBack((uint8_t)state); + + return Transmit(command); +} + +bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_deck_info state) +{ + cec_command command; + cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_DECK_STATUS); + command.PushBack((uint8_t)state); + + return Transmit(command); +} + +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); + command.parameters.PushBack((uint8_t)key); + + return Transmit(command); +} + +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); + + return Transmit(command); +} + +bool CCECCommandHandler::Transmit(cec_command &command, bool bExpectResponse /* = true */) +{ + command.transmit_timeout = m_iTransmitTimeout; + if (command.retries == CEC_DEFAULT_TRANSMIT_RETRIES) + command.retries = m_iTransmitRetries; + + CLockObject writeLock(&m_processor->m_transmitMutex); + CLockObject receiveLock(&m_receiveMutex); + if (m_processor->Transmit(command)) + { + if (bExpectResponse) + return m_condition.Wait(&m_receiveMutex, m_iTransmitWait); + return true; + } + + return false; +}