X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Flib%2FCECProcessor.cpp;h=ebd17729f4e0355f3ad77ab8f2a30a3a85f70188;hb=ddbda099de53712c09315291d96b6d9db2ac1bbc;hp=faa8d8c27cdf182682903b46748615f5473279f8;hpb=8e57f83c5cc5b448024c9e2c9bf8f2d93b78c756;p=deb_libcec.git diff --git a/src/lib/CECProcessor.cpp b/src/lib/CECProcessor.cpp index faa8d8c..ebd1772 100644 --- a/src/lib/CECProcessor.cpp +++ b/src/lib/CECProcessor.cpp @@ -47,27 +47,28 @@ using namespace CEC; using namespace std; using namespace PLATFORM; -CCECProcessor::CCECProcessor(CLibCEC *controller, const char *strDeviceName, cec_logical_address iLogicalAddress /* = CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS*/) : +CCECProcessor::CCECProcessor(CLibCEC *controller, const libcec_configuration *configuration) : m_bInitialised(false), - m_iHDMIPort(CEC_DEFAULT_HDMI_PORT), - m_iBaseDevice((cec_logical_address)CEC_DEFAULT_BASE_DEVICE), - m_strDeviceName(strDeviceName), + m_iPhysicalAddress(configuration->iPhysicalAddress), + m_iHDMIPort(configuration->iHDMIPort), + m_iBaseDevice(configuration->baseDevice), + m_strDeviceName(configuration->strDeviceName), + m_types(configuration->deviceTypes), m_communication(NULL), m_controller(controller), m_bMonitor(false), m_iStandardLineTimeout(3), m_iRetryLineTimeout(3), - m_iLastTransmission(0) + m_iLastTransmission(0), + m_clientVersion(configuration->clientVersion) { m_logicalAddresses.Clear(); - m_logicalAddresses.Set(iLogicalAddress); - m_types.clear(); - for (int iPtr = 0; iPtr <= 16; iPtr++) - m_busDevices[iPtr] = new CCECBusDevice(this, (cec_logical_address) iPtr, iPtr == iLogicalAddress ? iPhysicalAddress : 0); + CreateBusDevices(); } -CCECProcessor::CCECProcessor(CLibCEC *controller, const char *strDeviceName, const cec_device_type_list &types) : +CCECProcessor::CCECProcessor(CLibCEC *controller, const char *strDeviceName, const cec_device_type_list &types, uint16_t iPhysicalAddress, cec_client_version clientVersion) : m_bInitialised(false), + m_iPhysicalAddress(iPhysicalAddress), m_iHDMIPort(CEC_DEFAULT_HDMI_PORT), m_iBaseDevice((cec_logical_address)CEC_DEFAULT_BASE_DEVICE), m_strDeviceName(strDeviceName), @@ -77,9 +78,15 @@ CCECProcessor::CCECProcessor(CLibCEC *controller, const char *strDeviceName, con m_bMonitor(false), m_iStandardLineTimeout(3), m_iRetryLineTimeout(3), - m_iLastTransmission(0) + m_iLastTransmission(0), + m_clientVersion(clientVersion) { m_logicalAddresses.Clear(); + CreateBusDevices(); +} + +void CCECProcessor::CreateBusDevices(void) +{ for (int iPtr = 0; iPtr < 16; iPtr++) { switch(iPtr) @@ -115,15 +122,25 @@ CCECProcessor::CCECProcessor(CLibCEC *controller, const char *strDeviceName, con CCECProcessor::~CCECProcessor(void) { - StopThread(); + Close(); - delete m_communication; - m_communication = NULL; - m_controller = NULL; for (unsigned int iPtr = 0; iPtr < 16; iPtr++) delete m_busDevices[iPtr]; } +void CCECProcessor::Close(void) +{ + StopThread(); + + CLockObject lock(m_mutex); + if (m_communication) + { + m_communication->Close(); + delete m_communication; + m_communication = NULL; + } +} + bool CCECProcessor::OpenConnection(const char *strPort, uint16_t iBaudRate, uint32_t iTimeoutMs) { bool bReturn(false); @@ -146,51 +163,28 @@ bool CCECProcessor::OpenConnection(const char *strPort, uint16_t iBaudRate, uint uint64_t iNow = GetTimeMs(); uint64_t iTarget = iTimeoutMs > 0 ? iNow + iTimeoutMs : iNow + CEC_DEFAULT_TRANSMIT_WAIT; - unsigned iConnectTry(0), iPingTry(0), iFwVersionTry(0), iControlledTry(0); - bool bConnected(false), bPinged(false), bControlled(false); /* open a new connection */ - while (iNow < iTarget && (bConnected = m_communication->Open(iTimeoutMs)) == false) + unsigned iConnectTry(0); + while (iNow < iTarget && (bReturn = m_communication->Open(this, iTimeoutMs)) == false) { CLibCEC::AddLog(CEC_LOG_ERROR, "could not open a connection (try %d)", ++iConnectTry); Sleep(500); iNow = GetTimeMs(); } - /* try to ping the adapter */ - while (bConnected && iNow < iTarget && (bPinged = m_communication->PingAdapter()) == false) - { - CLibCEC::AddLog(CEC_LOG_ERROR, "the adapter did not respond correctly to a ping (try %d)", ++iPingTry); - Sleep(500); - iNow = GetTimeMs(); - } - - /* try to read the firmware version */ - uint16_t iFirmwareVersion(CEC_FW_VERSION_UNKNOWN); - while (bPinged && iNow < iTarget && (iFirmwareVersion = m_communication->GetFirmwareVersion()) == CEC_FW_VERSION_UNKNOWN) - { - CLibCEC::AddLog(CEC_LOG_ERROR, "the adapter did not respond with a correct firmware version (try %d)", ++iFwVersionTry); - Sleep(500); - iNow = GetTimeMs(); - } - - if (iFirmwareVersion >= 2) - { - /* try to set controlled mode */ - while (bConnected && iNow < iTarget && (bControlled = m_communication->SetControlledMode(true)) == false) - { - CLibCEC::AddLog(CEC_LOG_ERROR, "the adapter did not respond correctly to setting controlled mode (try %d)", ++iControlledTry); - Sleep(500); - iNow = GetTimeMs(); - } - } - - if ((bReturn = iFirmwareVersion != CEC_FW_VERSION_UNKNOWN) == true) - CLibCEC::AddLog(CEC_LOG_NOTICE, "connected to the CEC adapter. firmware version = %d", iFirmwareVersion); + if (bReturn) + CLibCEC::AddLog(CEC_LOG_NOTICE, "connected to the CEC adapter. firmware version = %d, client version = %s", m_communication->GetFirmwareVersion(), ToString(m_clientVersion)); return bReturn; } +bool CCECProcessor::IsInitialised(void) +{ + CLockObject lock(m_mutex); + return m_bInitialised; +} + void CCECProcessor::SetInitialised(bool bSetTo /* = true */) { CLockObject lock(m_mutex); @@ -217,9 +211,15 @@ bool CCECProcessor::Initialise(void) /* get the vendor id from the TV, so we are using the correct handler */ m_busDevices[CECDEVICE_TV]->RequestVendorId(); - ReplaceHandlers(); - if ((bReturn = SetHDMIPort(m_iBaseDevice, m_iHDMIPort, true)) == false) + if (m_iPhysicalAddress != 0) + { + CLibCEC::AddLog(CEC_LOG_NOTICE, "setting the physical address to %4x", m_iPhysicalAddress); + m_busDevices[m_logicalAddresses.primary]->m_iPhysicalAddress = m_iPhysicalAddress; + if ((bReturn = m_busDevices[m_logicalAddresses.primary]->TransmitPhysicalAddress()) == false) + CLibCEC::AddLog(CEC_LOG_ERROR, "unable to set the physical address to %4x", m_iPhysicalAddress); + } + else if (m_iPhysicalAddress == 0 && (bReturn = SetHDMIPort(m_iBaseDevice, m_iHDMIPort, true)) == false) CLibCEC::AddLog(CEC_LOG_ERROR, "unable to set HDMI port %d on %s (%x)", m_iHDMIPort, ToString(m_iBaseDevice), (uint8_t)m_iBaseDevice); SetInitialised(bReturn); @@ -406,43 +406,31 @@ bool CCECProcessor::FindLogicalAddresses(void) void CCECProcessor::ReplaceHandlers(void) { + if (!IsInitialised()) + return; for (uint8_t iPtr = 0; iPtr <= CECDEVICE_PLAYBACKDEVICE3; iPtr++) m_busDevices[iPtr]->ReplaceHandler(m_bInitialised); } +bool CCECProcessor::OnCommandReceived(const cec_command &command) +{ + m_commandBuffer.Push(command); + return true; +} + void *CCECProcessor::Process(void) { - bool bParseCommand(false); cec_command command; CLibCEC::AddLog(CEC_LOG_DEBUG, "processor thread started"); - while (!IsStopped()) + while (!IsStopped() && m_communication->IsOpen()) { ReplaceHandlers(); - command.Clear(); - - { - CLockObject lock(m_mutex); - if (m_commandBuffer.Pop(command)) - bParseCommand = true; - else if (m_communication->IsOpen() && m_communication->Read(command, 50)) - bParseCommand = true; - } - - if (bParseCommand) + if (m_commandBuffer.Pop(command)) ParseCommand(command); - bParseCommand = false; - - Sleep(5); m_controller->CheckKeypressTimeout(); - } - - if (m_communication) - { - m_communication->Close(); - delete m_communication; - m_communication = NULL; + Sleep(5); } return NULL; @@ -471,22 +459,7 @@ bool CCECProcessor::SetActiveSource(cec_device_type type /* = CEC_DEVICE_TYPE_RE m_busDevices[addr]->SetActiveSource(); if (m_busDevices[addr]->GetPhysicalAddress(false) != 0xFFFF) - { - bReturn = m_busDevices[addr]->TransmitActiveSource(); - - if (bReturn) - { - m_busDevices[addr]->SetMenuState(CEC_MENU_STATE_ACTIVATED); - m_busDevices[addr]->TransmitMenuState(CECDEVICE_TV); - } - - 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); - } - } + bReturn = m_busDevices[addr]->ActivateSource(); return bReturn; } @@ -664,9 +637,12 @@ bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress, bool bSendUpda bool bSendActiveView(false); bool bReturn(false); cec_logical_addresses sendUpdatesTo; + sendUpdatesTo.Clear(); { CLockObject lock(m_mutex); + m_iPhysicalAddress = iPhysicalAddress; + if (!m_logicalAddresses.IsEmpty()) { bool bWasActiveSource(false); @@ -888,7 +864,7 @@ void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode Transmit(command); } -void CCECProcessor::ParseCommand(cec_command &command) +void CCECProcessor::ParseCommand(const cec_command &command) { CStdString dataStr; dataStr.Format(">> %1x%1x:%02x", command.initiator, command.destination, command.opcode); @@ -1294,6 +1270,19 @@ const char *CCECProcessor::ToString(const cec_vendor_id vendor) } } +const char *CCECProcessor::ToString(const cec_client_version version) +{ + switch (version) + { + case CEC_CLIENT_VERSION_PRE_1_5: + return "pre-1.5"; + case CEC_CLIENT_VERSION_1_5_0: + return "1.5.0"; + default: + return "Unknown"; + } +} + void *CCECBusScan::Process(void) { CCECBusDevice *device(NULL); @@ -1351,10 +1340,36 @@ bool CCECProcessor::PingAdapter(void) void CCECProcessor::HandlePoll(cec_logical_address initiator, cec_logical_address destination) { - m_busDevices[initiator]->GetHandler()->HandlePoll(initiator, destination); + m_busDevices[initiator]->HandlePoll(destination); } bool CCECProcessor::HandleReceiveFailed(cec_logical_address initiator) { - return !m_busDevices[initiator]->GetHandler()->HandleReceiveFailed(); + return !m_busDevices[initiator]->HandleReceiveFailed(); +} + +bool CCECProcessor::SetStreamPath(uint16_t iPhysicalAddress) +{ + // stream path changes are sent by the TV + return m_busDevices[CECDEVICE_TV]->GetHandler()->TransmitSetStreamPath(iPhysicalAddress); +} + +bool CCECProcessor::GetCurrentConfiguration(libcec_configuration *configuration) +{ + configuration->iPhysicalAddress = m_iPhysicalAddress; + configuration->iHDMIPort = m_iHDMIPort; + configuration->baseDevice = m_iBaseDevice; + snprintf(configuration->strDeviceName, 13, "%s", m_strDeviceName.c_str()); + configuration->deviceTypes = m_types; + return true; +} + +bool CCECProcessor::CanPersistConfiguration(void) +{ + return m_communication->GetFirmwareVersion() >= 2; +} + +bool CCECProcessor::PersistConfiguration(libcec_configuration *configuration) +{ + return m_communication->PersistConfiguration(configuration); }