+ StopThread(true);
+ }
+
+ return bReturn;
+}
+
+bool CCECProcessor::TryLogicalAddress(cec_logical_address address)
+{
+ if (m_busDevices[address]->TryLogicalAddress())
+ {
+ m_logicalAddresses.Set(address);
+ return true;
+ }
+
+ return false;
+}
+
+bool CCECProcessor::FindLogicalAddressRecordingDevice(void)
+{
+ AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'recording device'");
+ return TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1) ||
+ TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2) ||
+ TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3);
+}
+
+bool CCECProcessor::FindLogicalAddressTuner(void)
+{
+ AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'tuner'");
+ return TryLogicalAddress(CECDEVICE_TUNER1) ||
+ TryLogicalAddress(CECDEVICE_TUNER2) ||
+ TryLogicalAddress(CECDEVICE_TUNER3) ||
+ TryLogicalAddress(CECDEVICE_TUNER4);
+}
+
+bool CCECProcessor::FindLogicalAddressPlaybackDevice(void)
+{
+ AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'playback device'");
+ return TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1) ||
+ TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2) ||
+ TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3);
+}
+
+bool CCECProcessor::FindLogicalAddressAudioSystem(void)
+{
+ AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'audio'");
+ return TryLogicalAddress(CECDEVICE_AUDIOSYSTEM);
+}
+
+bool CCECProcessor::ChangeDeviceType(cec_device_type from, cec_device_type to)
+{
+ bool bChanged(false);
+
+ CStdString strLog;
+ strLog.Format("changing device type '%s' into '%s'", ToString(from), ToString(to));
+ AddLog(CEC_LOG_NOTICE, strLog);
+
+ CLockObject lock(&m_mutex);
+ CCECBusDevice *previousDevice = GetDeviceByType(from);
+ m_logicalAddresses.primary = CECDEVICE_UNKNOWN;
+
+ for (unsigned int iPtr = 0; iPtr < 5; iPtr++)
+ {
+ if (m_types.types[iPtr] == CEC_DEVICE_TYPE_RESERVED)
+ continue;
+
+ if (m_types.types[iPtr] == from)
+ {
+ bChanged = true;
+ m_types.types[iPtr] = to;
+ }
+ else if (m_types.types[iPtr] == to && bChanged)
+ {
+ m_types.types[iPtr] = CEC_DEVICE_TYPE_RESERVED;
+ }
+ }
+
+ if (bChanged)
+ {
+ FindLogicalAddresses();
+
+ CCECBusDevice *newDevice = GetDeviceByType(to);
+ if (previousDevice && newDevice)
+ {
+ newDevice->SetDeviceStatus(CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC);
+ previousDevice->SetDeviceStatus(CEC_DEVICE_STATUS_UNKNOWN);
+
+ newDevice->SetCecVersion(previousDevice->GetCecVersion(false));
+ previousDevice->SetCecVersion(CEC_VERSION_UNKNOWN);
+
+ newDevice->SetMenuLanguage(previousDevice->GetMenuLanguage(false));
+ cec_menu_language lang;
+ lang.device = previousDevice->GetLogicalAddress();
+ for (unsigned int iPtr = 0; iPtr < 4; iPtr++)
+ lang.language[iPtr] = '?';
+ lang.language[3] = 0;
+ previousDevice->SetMenuLanguage(lang);
+
+ newDevice->SetMenuState(previousDevice->GetMenuState());
+ previousDevice->SetMenuState(CEC_MENU_STATE_DEACTIVATED);
+
+ newDevice->SetOSDName(previousDevice->GetOSDName(false));
+ previousDevice->SetOSDName(ToString(previousDevice->GetLogicalAddress()));
+
+ newDevice->SetPhysicalAddress(previousDevice->GetPhysicalAddress(false));
+ previousDevice->SetPhysicalAddress(0xFFFF);
+
+ newDevice->SetPowerStatus(previousDevice->GetPowerStatus(false));
+ previousDevice->SetPowerStatus(CEC_POWER_STATUS_UNKNOWN);
+
+ newDevice->SetVendorId(previousDevice->GetVendorId(false));
+ previousDevice->SetVendorId(CEC_VENDOR_UNKNOWN);
+
+ if ((from == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || from == CEC_DEVICE_TYPE_RECORDING_DEVICE) &&
+ (to == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || to == CEC_DEVICE_TYPE_RECORDING_DEVICE))
+ {
+ ((CCECPlaybackDevice *) newDevice)->SetDeckControlMode(((CCECPlaybackDevice *) previousDevice)->GetDeckControlMode());
+ ((CCECPlaybackDevice *) previousDevice)->SetDeckControlMode(CEC_DECK_CONTROL_MODE_STOP);
+
+ ((CCECPlaybackDevice *) newDevice)->SetDeckStatus(((CCECPlaybackDevice *) previousDevice)->GetDeckStatus());
+ ((CCECPlaybackDevice *) previousDevice)->SetDeckStatus(CEC_DECK_INFO_STOP);
+ }
+ }
+ }
+
+ return true;
+}
+
+bool CCECProcessor::FindLogicalAddresses(void)
+{
+ bool bReturn(true);
+ m_logicalAddresses.Clear();
+ CStdString strLog;
+
+ for (unsigned int iPtr = 0; iPtr < 5; iPtr++)
+ {
+ if (m_types.types[iPtr] == CEC_DEVICE_TYPE_RESERVED)
+ continue;
+
+ strLog.Format("%s - device %d: type %d", __FUNCTION__, iPtr, m_types.types[iPtr]);
+ AddLog(CEC_LOG_DEBUG, strLog);
+
+ if (m_types.types[iPtr] == CEC_DEVICE_TYPE_RECORDING_DEVICE)
+ bReturn &= FindLogicalAddressRecordingDevice();
+ if (m_types.types[iPtr] == CEC_DEVICE_TYPE_TUNER)
+ bReturn &= FindLogicalAddressTuner();
+ if (m_types.types[iPtr] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE)
+ bReturn &= FindLogicalAddressPlaybackDevice();
+ if (m_types.types[iPtr] == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
+ bReturn &= FindLogicalAddressAudioSystem();
+ }
+
+ if (bReturn)
+ SetAckMask(m_logicalAddresses.AckMask());
+
+ return bReturn;
+}
+
+void CCECProcessor::ReplaceHandlers(void)
+{
+ for (uint8_t iPtr = 0; iPtr <= CECDEVICE_PLAYBACKDEVICE3; iPtr++)
+ m_busDevices[iPtr]->ReplaceHandler(m_bInitialised);
+}
+
+void *CCECProcessor::Process(void)
+{
+ bool bParseFrame(false);
+ cec_command command;
+ CCECAdapterMessage msg;
+
+ {
+ CLockObject lock(&m_mutex);
+ m_bStarted = true;
+ m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started");
+ m_startCondition.Signal();
+ }
+
+ while (!IsStopped())
+ {
+ ReplaceHandlers();
+ command.Clear();
+ msg.clear();
+
+ {
+ CLockObject lock(&m_mutex);
+ if (m_commandBuffer.Pop(command))
+ {
+ bParseFrame = true;
+ }
+ else if (m_communication->IsOpen() && m_communication->Read(msg, 50))
+ {
+ if ((bParseFrame = (ParseMessage(msg) && !IsStopped())) == true)
+ command = m_currentframe;
+ }
+ }
+
+ if (bParseFrame)
+ ParseCommand(command);
+ bParseFrame = false;
+
+ Sleep(5);
+
+ m_controller->CheckKeypressTimeout();
+ }
+
+ if (m_communication)
+ m_communication->Close();
+
+ return NULL;
+}
+
+bool CCECProcessor::SetActiveSource(cec_device_type type /* = CEC_DEVICE_TYPE_RESERVED */)
+{
+ bool bReturn(false);
+
+ if (!IsRunning())
+ return bReturn;
+
+ cec_logical_address addr = m_logicalAddresses.primary;
+
+ if (type != CEC_DEVICE_TYPE_RESERVED)
+ {
+ for (uint8_t iPtr = 0; iPtr <= 11; iPtr++)
+ {
+ if (m_logicalAddresses[iPtr] && m_busDevices[iPtr]->m_type == type)
+ {
+ addr = (cec_logical_address) iPtr;
+ break;
+ }
+ }
+ }
+
+ m_busDevices[addr]->SetActiveSource();
+ if (m_busDevices[addr]->GetPhysicalAddress(false) != 0xFFFF)
+ {
+ bReturn = m_busDevices[addr]->TransmitActiveSource();
+
+ if (bReturn && (m_busDevices[addr]->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE ||
+ m_busDevices[addr]->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE) &&
+ m_busDevices[addr]->GetHandler()->SendDeckStatusUpdateOnActiveSource())
+ {
+ bReturn = ((CCECPlaybackDevice *)m_busDevices[addr])->TransmitDeckStatus(CECDEVICE_TV);
+ }
+ }
+
+ return bReturn;
+}
+
+bool CCECProcessor::SetActiveSource(uint16_t iStreamPath)
+{
+ bool bReturn(false);
+
+ CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamPath);
+ if (device)
+ {
+ device->SetActiveSource();
+ bReturn = true;
+ }
+
+ return bReturn;
+}
+
+void CCECProcessor::SetStandardLineTimeout(uint8_t iTimeout)
+{
+ CLockObject lock(&m_mutex);
+ m_iStandardLineTimeout = iTimeout;
+}
+
+void CCECProcessor::SetRetryLineTimeout(uint8_t iTimeout)
+{
+ CLockObject lock(&m_mutex);
+ m_iRetryLineTimeout = iTimeout;
+}
+
+bool CCECProcessor::SetActiveView(void)
+{
+ return SetActiveSource(m_types.IsEmpty() ? CEC_DEVICE_TYPE_RESERVED : m_types[0]);
+}
+
+bool CCECProcessor::SetDeckControlMode(cec_deck_control_mode mode, bool bSendUpdate /* = true */)
+{
+ bool bReturn(false);
+
+ CCECBusDevice *device = GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
+ if (device)
+ {
+ ((CCECPlaybackDevice *) device)->SetDeckControlMode(mode);
+ if (bSendUpdate)
+ ((CCECPlaybackDevice *) device)->TransmitDeckStatus(CECDEVICE_TV);
+ bReturn = true;
+ }
+
+ return bReturn;
+}
+
+bool CCECProcessor::SetDeckInfo(cec_deck_info info, bool bSendUpdate /* = true */)
+{
+ bool bReturn(false);
+
+ CCECBusDevice *device = GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
+ if (device)
+ {
+ ((CCECPlaybackDevice *) device)->SetDeckStatus(info);
+ if (bSendUpdate)
+ ((CCECPlaybackDevice *) device)->TransmitDeckStatus(CECDEVICE_TV);
+ bReturn = true;
+ }
+
+ return bReturn;
+}
+
+bool CCECProcessor::SetHDMIPort(cec_logical_address iBaseDevice, uint8_t iPort, bool bForce /* = false */)
+{
+ bool bReturn(false);
+ CLockObject lock(&m_mutex);
+
+ m_iBaseDevice = iBaseDevice;
+ m_iHDMIPort = iPort;
+ if (!m_bStarted && !bForce)
+ return true;
+
+ CStdString strLog;
+ strLog.Format("setting HDMI port to %d on device %s (%d)", iPort, ToString(iBaseDevice), (int)iBaseDevice);
+ AddLog(CEC_LOG_DEBUG, strLog);
+
+ uint16_t iPhysicalAddress(0);
+ if (iBaseDevice > CECDEVICE_TV)
+ {
+ lock.Leave();
+ iPhysicalAddress = m_busDevices[iBaseDevice]->GetPhysicalAddress();
+ lock.Lock();
+ }
+
+ if (iPhysicalAddress < 0xffff)
+ {
+ if (iPhysicalAddress == 0)
+ iPhysicalAddress += 0x1000 * iPort;
+ else if (iPhysicalAddress % 0x1000 == 0)
+ iPhysicalAddress += 0x100 * iPort;
+ else if (iPhysicalAddress % 0x100 == 0)
+ iPhysicalAddress += 0x10 * iPort;
+ else if (iPhysicalAddress % 0x10 == 0)
+ iPhysicalAddress += iPort;
+
+ bReturn = true;
+ }
+
+ if (!bReturn)
+ m_controller->AddLog(CEC_LOG_ERROR, "failed to set the physical address");
+ else
+ {
+ lock.Leave();
+ SetPhysicalAddress(iPhysicalAddress);
+ }
+
+ return bReturn;
+}
+
+bool CCECProcessor::PhysicalAddressInUse(uint16_t iPhysicalAddress)
+{
+ for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
+ {
+ if (m_busDevices[iPtr]->GetPhysicalAddress(false) == iPhysicalAddress)
+ return true;
+ }
+ return false;
+}
+
+bool CCECProcessor::TransmitInactiveSource(void)
+{
+ if (!IsRunning())
+ return false;
+
+ if (!m_logicalAddresses.IsEmpty() && m_busDevices[m_logicalAddresses.primary])
+ return m_busDevices[m_logicalAddresses.primary]->TransmitInactiveSource();
+ return false;
+}
+
+void CCECProcessor::LogOutput(const cec_command &data)
+{
+ CStdString strTx;
+ strTx.Format("<< %02x", ((uint8_t)data.initiator << 4) + (uint8_t)data.destination);
+ if (data.opcode_set)
+ strTx.AppendFormat(":%02x", (uint8_t)data.opcode);
+
+ for (uint8_t iPtr = 0; iPtr < data.parameters.size; iPtr++)
+ strTx.AppendFormat(":%02x", data.parameters[iPtr]);
+ m_controller->AddLog(CEC_LOG_TRAFFIC, strTx.c_str());
+}
+
+bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress)
+{
+ CLockObject lock(&m_mutex);
+ if (m_logicalAddresses.primary != iLogicalAddress)
+ {
+ CStdString strLog;
+ strLog.Format("<< setting primary logical address to %1x", iLogicalAddress);
+ m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
+ m_logicalAddresses.primary = iLogicalAddress;
+ m_logicalAddresses.Set(iLogicalAddress);
+ return SetAckMask(m_logicalAddresses.AckMask());
+ }
+
+ return true;
+}
+
+bool CCECProcessor::SetMenuState(cec_menu_state state, bool bSendUpdate /* = true */)
+{
+ for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
+ {
+ if (m_logicalAddresses[iPtr])
+ m_busDevices[iPtr]->SetMenuState(state);
+ }
+
+ if (bSendUpdate)
+ m_busDevices[m_logicalAddresses.primary]->TransmitMenuState(CECDEVICE_TV);
+
+ return true;
+}
+
+bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress, bool bSendUpdate /* = true */)
+{
+ bool bSendActiveView(false);
+ bool bReturn(false);
+ cec_logical_addresses sendUpdatesTo;
+
+ {
+ CLockObject lock(&m_mutex);
+ if (!m_logicalAddresses.IsEmpty())
+ {
+ bool bWasActiveSource(false);
+ for (uint8_t iPtr = 0; iPtr < 15; iPtr++)
+ if (m_logicalAddresses[iPtr])
+ {
+ bWasActiveSource |= m_busDevices[iPtr]->IsActiveSource();
+ m_busDevices[iPtr]->SetInactiveSource();
+ m_busDevices[iPtr]->SetPhysicalAddress(iPhysicalAddress);
+ if (bSendUpdate)
+ sendUpdatesTo.Set((cec_logical_address)iPtr);
+ }
+
+ bSendActiveView = bWasActiveSource && bSendUpdate;
+ bReturn = true;
+ }
+ }
+
+ for (uint8_t iPtr = 0; iPtr < 15; iPtr++)
+ if (sendUpdatesTo[iPtr])
+ m_busDevices[iPtr]->TransmitPhysicalAddress();
+
+ if (bSendActiveView)
+ SetActiveView();
+
+ return bReturn;
+}
+
+bool CCECProcessor::SwitchMonitoring(bool bEnable)
+{
+ CStdString strLog;
+ strLog.Format("== %s monitoring mode ==", bEnable ? "enabling" : "disabling");
+ m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
+
+ {
+ CLockObject lock(&m_mutex);
+ m_bMonitor = bEnable;
+ }
+
+ if (bEnable)
+ return SetAckMask(0);
+ else
+ return SetAckMask(m_logicalAddresses.AckMask());
+}
+
+bool CCECProcessor::PollDevice(cec_logical_address iAddress)
+{
+ if (iAddress != CECDEVICE_UNKNOWN && m_busDevices[iAddress])
+ {
+ return m_logicalAddresses.primary == CECDEVICE_UNKNOWN ?
+ m_busDevices[iAddress]->TransmitPoll(iAddress) :
+ m_busDevices[m_logicalAddresses.primary]->TransmitPoll(iAddress);
+ }
+ return false;
+}
+
+uint8_t CCECProcessor::VolumeUp(bool bSendRelease /* = true */)
+{
+ uint8_t status = 0;
+ if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM))
+ status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->VolumeUp(bSendRelease);
+
+ return status;
+}
+
+uint8_t CCECProcessor::VolumeDown(bool bSendRelease /* = true */)
+{
+ uint8_t status = 0;
+ if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM))
+ status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->VolumeDown(bSendRelease);
+
+ return status;
+}
+
+uint8_t CCECProcessor::MuteAudio(bool bSendRelease /* = true */)
+{
+ uint8_t status = 0;
+ if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM))
+ status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->MuteAudio(bSendRelease);
+
+ return status;
+}
+
+CCECBusDevice *CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress, bool bRefresh /* = false */) const
+{
+ if (m_busDevices[m_logicalAddresses.primary]->GetPhysicalAddress(false) == iPhysicalAddress)
+ return m_busDevices[m_logicalAddresses.primary];
+
+ CCECBusDevice *device = NULL;
+ for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
+ {
+ if (m_busDevices[iPtr]->GetPhysicalAddress(bRefresh) == iPhysicalAddress)
+ {
+ device = m_busDevices[iPtr];
+ break;
+ }
+ }