+ LIB_CEC->AddLog(CEC_LOG_ERROR, "not transmitting a command without a valid initiator");
+ return bReturn;
+ }
+
+ {
+ uint8_t iTries(0), iMaxTries(!command.opcode_set ? 1 : m_iTransmitRetries + 1);
+ while (!bReturn && ++iTries <= iMaxTries && !m_busDevice->IsUnsupportedFeature(command.opcode))
+ {
+ if ((bReturn = m_processor->Transmit(command, bIsReply)) == true)
+ {
+ LIB_CEC->AddLog(CEC_LOG_DEBUG, "command transmitted");
+ 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;
+
+ LIB_CEC->AddLog(CEC_LOG_DEBUG, "transmitting delayed activate source command");
+ }
+
+ // clear previous pending active source command
+ m_iActiveSourcePending = 0;
+ }
+
+ // update the power state and menu state
+ m_busDevice->SetPowerStatus(CEC_POWER_STATUS_ON);
+ m_busDevice->SetMenuState(CEC_MENU_STATE_ACTIVATED); // TODO: LG
+
+ // power on the TV
+ bool bActiveSourceFailed = !m_busDevice->TransmitImageViewOn();
+
+ // 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) ||
+ !m_busDevice->TransmitMenuState(CECDEVICE_TV, false);
+
+ // update the deck status for playback devices
+ if (!bActiveSourceFailed)
+ {
+ CCECPlaybackDevice *playbackDevice = m_busDevice->AsPlaybackDevice();
+ if (playbackDevice && SendDeckStatusUpdateOnActiveSource())
+ bActiveSourceFailed = !playbackDevice->TransmitDeckStatus(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());
+ CLockObject lock(m_mutex);
+ m_iActiveSourcePending = GetTimeMs() + (int64_t)CEC_ACTIVE_SOURCE_SWITCH_RETRY_TIME_MS;
+ return false;
+ }
+
+ // mark the handler as initialised
+ CLockObject lock(m_mutex);
+ m_bHandlerInited = true;