From: Lars Op den Kamp Date: Wed, 5 Sep 2012 12:13:16 +0000 (+0200) Subject: extract logical address assignment from CCECProcessor::RegisterClient() X-Git-Tag: upstream/2.2.0~1^2~18^2^2~10 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=74c9d740fc14b30cefee2b4ad4d089d1004c5965;p=deb_libcec.git extract logical address assignment from CCECProcessor::RegisterClient() --- diff --git a/src/lib/CECProcessor.cpp b/src/lib/CECProcessor.cpp index 1eb272a..34ffed5 100644 --- a/src/lib/CECProcessor.cpp +++ b/src/lib/CECProcessor.cpp @@ -642,6 +642,51 @@ CCECTuner *CCECProcessor::GetTuner(cec_logical_address address) const return CCECBusDevice::AsTuner(m_busDevices->At(address)); } +bool CCECProcessor::AllocateLogicalAddresses(CCECClient* client) +{ + libcec_configuration &configuration = *client->GetConfiguration(); + + // mark as unregistered + client->SetRegistered(false); + + // unregister this client from the old addresses + CECDEVICEVEC devices; + m_busDevices->GetByLogicalAddresses(devices, configuration.logicalAddresses); + for (CECDEVICEVEC::const_iterator it = devices.begin(); it != devices.end(); it++) + { + // remove client entry + CLockObject lock(m_mutex); + m_clients.erase((*it)->GetLogicalAddress()); + } + + // find logical addresses for this client + if (!client->AllocateLogicalAddresses()) + { + m_libcec->AddLog(CEC_LOG_ERROR, "failed to find a free logical address for the client"); + return false; + } + + // register this client on the new addresses + devices.clear(); + m_busDevices->GetByLogicalAddresses(devices, configuration.logicalAddresses); + for (CECDEVICEVEC::const_iterator it = devices.begin(); it != devices.end(); it++) + { + // set the physical address of the device at this LA + if (CLibCEC::IsValidPhysicalAddress(configuration.iPhysicalAddress)) + (*it)->SetPhysicalAddress(configuration.iPhysicalAddress); + + // replace a previous client + CLockObject lock(m_mutex); + m_clients.erase((*it)->GetLogicalAddress()); + m_clients.insert(make_pair((*it)->GetLogicalAddress(), client)); + } + + // set the new ackmask + SetLogicalAddresses(GetLogicalAddresses()); + + return true; +} + bool CCECProcessor::RegisterClient(CCECClient *client) { if (!client) @@ -684,36 +729,20 @@ bool CCECProcessor::RegisterClient(CCECClient *client) // get the configuration from the client m_libcec->AddLog(CEC_LOG_NOTICE, "registering new CEC client - v%s", ToString((cec_client_version)configuration.clientVersion)); - // mark as uninitialised and unregistered - client->SetRegistered(false); - client->SetInitialised(false); - // get the current ackmask, so we can restore it if polling fails cec_logical_addresses previousMask = GetLogicalAddresses(); + // mark as uninitialised + client->SetInitialised(false); + // find logical addresses for this client - if (!client->AllocateLogicalAddresses()) + if (!AllocateLogicalAddresses(client)) { m_libcec->AddLog(CEC_LOG_ERROR, "failed to register the new CEC client - cannot allocate the requested device types"); SetLogicalAddresses(previousMask); return false; } - // register this client on the new addresses - CECDEVICEVEC devices; - m_busDevices->GetByLogicalAddresses(devices, configuration.logicalAddresses); - for (CECDEVICEVEC::const_iterator it = devices.begin(); it != devices.end(); it++) - { - // set the physical address of the device at this LA - if (CLibCEC::IsValidPhysicalAddress(configuration.iPhysicalAddress)) - (*it)->SetPhysicalAddress(configuration.iPhysicalAddress); - - // replace a previous client - CLockObject lock(m_mutex); - m_clients.erase((*it)->GetLogicalAddress()); - m_clients.insert(make_pair((*it)->GetLogicalAddress(), client)); - } - // get the settings from the rom if (configuration.bGetSettingsFromROM == 1) { @@ -737,10 +766,8 @@ bool CCECProcessor::RegisterClient(CCECClient *client) // mark the client as registered client->SetRegistered(true); - // set the new ack mask - bool bReturn = SetLogicalAddresses(GetLogicalAddresses()) && - // and initialise the client - client->OnRegister(); + // initialise the client + bool bReturn = client->OnRegister(); // log the new registration CStdString strLog; diff --git a/src/lib/CECProcessor.h b/src/lib/CECProcessor.h index 3494d72..6faf521 100644 --- a/src/lib/CECProcessor.h +++ b/src/lib/CECProcessor.h @@ -145,6 +145,8 @@ namespace CEC void ResetMembers(void); + bool AllocateLogicalAddresses(CCECClient* client); + bool m_bInitialised; PLATFORM::CMutex m_mutex; IAdapterCommunication * m_communication;