+ // check whether the destination is not marked as not present or handled by libCEC
+ if (command.destination != CECDEVICE_BROADCAST && command.opcode_set)
+ {
+ CCECBusDevice* destinationDevice = m_processor->GetDevice(command.destination);
+ cec_bus_device_status status = destinationDevice ? destinationDevice->GetStatus() : CEC_DEVICE_STATUS_NOT_PRESENT;
+ if (status == CEC_DEVICE_STATUS_NOT_PRESENT)
+ {
+ LIB_CEC->AddLog(CEC_LOG_DEBUG, "not sending command '%s': destination device '%s' marked as not present", ToString(command.opcode),ToString(command.destination));
+ return bReturn;
+ }
+ else if (status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC)
+ {
+ LIB_CEC->AddLog(CEC_LOG_DEBUG, "not sending command '%s': destination device '%s' marked as handled by libCEC", ToString(command.opcode),ToString(command.destination));
+ return bReturn;
+ }
+ else if (destinationDevice->IsUnsupportedFeature(command.opcode))
+ {
+ return true;
+ }
+ }
+
+ {
+ uint8_t iTries(0), iMaxTries(m_iTransmitRetries + 1);
+ while (!bReturn && ++iTries <= iMaxTries)
+ {
+ if ((bReturn = m_processor->Transmit(command, bIsReply)) == true)
+ {
+#ifdef CEC_DEBUGGING
+ LIB_CEC->AddLog(CEC_LOG_DEBUG, "command transmitted");
+#endif
+ if (bExpectResponse)
+ {
+ bReturn = m_busDevice->WaitForOpcode(expectedResponse);
+ LIB_CEC->AddLog(CEC_LOG_DEBUG, bReturn ? "expected response received (%X: %s)" : "expected response not received (%X: %s)", (int)expectedResponse, ToString(expectedResponse));
+ }
+ }
+ }
+ }
+
+ return bReturn;
+}
+
+bool CCECCommandHandler::ActivateSource(bool bTransmitDelayedCommandsOnly /* = false */)
+{
+ if (m_busDevice->IsActiveSource() &&
+ m_busDevice->IsHandledByLibCEC())
+ {
+ {
+ CLockObject lock(m_mutex);
+ // check if we need to send a delayed source switch
+ if (bTransmitDelayedCommandsOnly)
+ {
+ if (m_iActiveSourcePending == 0 || GetTimeMs() < m_iActiveSourcePending)
+ return false;
+
+#ifdef CEC_DEBUGGING
+ LIB_CEC->AddLog(CEC_LOG_DEBUG, "transmitting delayed activate source command");
+#endif
+ }
+ }
+
+ // update the power state and menu state
+ if (!bTransmitDelayedCommandsOnly)
+ {
+ m_busDevice->SetPowerStatus(CEC_POWER_STATUS_ON);
+ m_busDevice->SetMenuState(CEC_MENU_STATE_ACTIVATED);
+ }
+
+ // vendor specific hook
+ VendorPreActivateSourceHook();
+
+ // power on the TV
+ CCECBusDevice* tv = m_processor->GetDevice(CECDEVICE_TV);
+ bool bTvPresent = (tv && tv->GetStatus() == CEC_DEVICE_STATUS_PRESENT);
+ bool bActiveSourceFailed(false);
+ if (bTvPresent)
+ bActiveSourceFailed = !tv->PowerOn(m_busDevice->GetLogicalAddress());
+ else
+ LIB_CEC->AddLog(CEC_LOG_DEBUG, "TV not present, not sending 'image view on'");
+
+ // check if we're allowed to switch sources
+ bool bSourceSwitchAllowed = SourceSwitchAllowed();
+ if (!bSourceSwitchAllowed)
+ LIB_CEC->AddLog(CEC_LOG_DEBUG, "source switch is currently not allowed by command handler");
+
+ // switch sources (if allowed)
+ if (!bActiveSourceFailed && bSourceSwitchAllowed)
+ {
+ bActiveSourceFailed = !m_busDevice->TransmitActiveSource(false);
+ if (bTvPresent && !bActiveSourceFailed)
+ bActiveSourceFailed = !m_busDevice->TransmitMenuState(CECDEVICE_TV, false);
+
+ // update the deck status for playback devices
+ if (bTvPresent && !bActiveSourceFailed)
+ {
+ CCECPlaybackDevice *playbackDevice = m_busDevice->AsPlaybackDevice();
+ if (playbackDevice && SendDeckStatusUpdateOnActiveSource())
+ bActiveSourceFailed = !playbackDevice->TransmitDeckStatus(CECDEVICE_TV, false);
+ }
+
+ // update system audio mode for audiosystem devices
+ if (bTvPresent && !bActiveSourceFailed)
+ {
+ CCECAudioSystem* audioDevice = m_busDevice->AsAudioSystem();
+ if (audioDevice)
+ bActiveSourceFailed = !audioDevice->TransmitSetSystemAudioMode(CECDEVICE_TV, false);
+ }
+ }
+
+ // retry later
+ if (bActiveSourceFailed || !bSourceSwitchAllowed)
+ {
+ LIB_CEC->AddLog(CEC_LOG_DEBUG, "failed to make '%s' the active source. will retry later", m_busDevice->GetLogicalAddressName());
+ int64_t now(GetTimeMs());
+ CLockObject lock(m_mutex);
+ if (m_iActiveSourcePending == 0 || m_iActiveSourcePending < now)
+ m_iActiveSourcePending = now + (int64_t)CEC_ACTIVE_SOURCE_SWITCH_RETRY_TIME_MS;
+ return false;
+ }
+ else
+ {
+ CLockObject lock(m_mutex);
+ // clear previous pending active source command
+ m_iActiveSourcePending = 0;
+ }
+
+ // mark the handler as initialised
+ CLockObject lock(m_mutex);
+ m_bHandlerInited = true;
+ }
+ return true;