+ return true;
+}
+
+bool CCECProcessor::TransmitPendingActiveSourceCommands(void)
+{
+ bool bReturn(true);
+ for (CECDEVICEMAP::iterator it = m_busDevices->Begin(); it != m_busDevices->End(); it++)
+ bReturn &= it->second->TransmitPendingActiveSourceCommands();
+ return bReturn;
+}
+
+CCECTV *CCECProcessor::GetTV(void) const
+{
+ return CCECBusDevice::AsTV(m_busDevices->At(CECDEVICE_TV));
+}
+
+CCECAudioSystem *CCECProcessor::GetAudioSystem(void) const
+{
+ return CCECBusDevice::AsAudioSystem(m_busDevices->At(CECDEVICE_AUDIOSYSTEM));
+}
+
+CCECPlaybackDevice *CCECProcessor::GetPlaybackDevice(cec_logical_address address) const
+{
+ return CCECBusDevice::AsPlaybackDevice(m_busDevices->At(address));
+}
+
+CCECRecordingDevice *CCECProcessor::GetRecordingDevice(cec_logical_address address) const
+{
+ return CCECBusDevice::AsRecordingDevice(m_busDevices->At(address));
+}
+
+CCECTuner *CCECProcessor::GetTuner(cec_logical_address address) const
+{
+ return CCECBusDevice::AsTuner(m_busDevices->At(address));
+}
+
+bool CCECProcessor::RegisterClient(CCECClient *client)
+{
+ if (!client)
+ return false;
+
+ libcec_configuration &configuration = *client->GetConfiguration();
+
+ if (configuration.clientVersion >= CEC_CLIENT_VERSION_1_6_3 && configuration.bMonitorOnly == 1)
+ return true;
+
+ if (!CECInitialised())
+ {
+ m_libcec->AddLog(CEC_LOG_ERROR, "failed to register a new CEC client: CEC processor is not initialised");
+ return false;
+ }
+
+ // ensure that we know the vendor id of the TV
+ GetTV()->GetVendorId(CECDEVICE_UNREGISTERED);
+
+ // unregister the client first if it's already been marked as registered
+ if (client->IsRegistered())
+ UnregisterClient(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
+ uint16_t iPreviousMask(m_communication->GetAckMask());
+
+ // find logical addresses for this client
+ if (!client->AllocateLogicalAddresses())
+ {
+ m_libcec->AddLog(CEC_LOG_ERROR, "failed to register the new CEC client - cannot allocate the requested device types");
+ SetAckMask(iPreviousMask);
+ 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++)
+ {
+ // replace a previous client
+ CLockObject lock(m_mutex);
+ m_clients.erase((*it)->GetLogicalAddress());
+ m_clients.insert(make_pair<cec_logical_address, CCECClient *>((*it)->GetLogicalAddress(), client));
+ }
+
+ // get the settings from the rom
+ if (configuration.bGetSettingsFromROM == 1)
+ {
+ libcec_configuration config;
+ m_communication->GetConfiguration(config);
+
+ CLockObject lock(m_mutex);
+ if (!config.deviceTypes.IsEmpty())
+ configuration.deviceTypes = config.deviceTypes;
+ if (CLibCEC::IsValidPhysicalAddress(config.iPhysicalAddress))
+ configuration.iPhysicalAddress = config.iPhysicalAddress;
+ snprintf(configuration.strDeviceName, 13, "%s", config.strDeviceName);
+ }
+
+ // set the firmware version and build date
+ configuration.serverVersion = LIBCEC_VERSION_CURRENT;
+ configuration.iFirmwareVersion = m_communication->GetFirmwareVersion();
+ configuration.iFirmwareBuildDate = m_communication->GetFirmwareBuildDate();
+
+ // mark the client as registered
+ client->SetRegistered(true);
+
+ // set the new ack mask
+ bool bReturn = SetAckMask(GetLogicalAddresses().AckMask()) &&
+ // and initialise the client
+ client->OnRegister();
+
+ // log the new registration
+ CStdString strLog;
+ strLog.Format("%s: %s", bReturn ? "CEC client registered" : "failed to register the CEC client", client->GetConnectionInfo().c_str());
+ m_libcec->AddLog(bReturn ? CEC_LOG_NOTICE : CEC_LOG_ERROR, strLog);
+
+ // display a warning if the firmware can be upgraded
+ if (bReturn && !IsRunningLatestFirmware())
+ {
+ const char *strUpgradeMessage = "The firmware of this adapter can be upgraded. Please visit http://blog.pulse-eight.com/ for more information.";
+ m_libcec->AddLog(CEC_LOG_WARNING, strUpgradeMessage);
+ libcec_parameter param;
+ param.paramData = (void*)strUpgradeMessage; param.paramType = CEC_PARAMETER_TYPE_STRING;
+ client->Alert(CEC_ALERT_SERVICE_DEVICE, param);
+ }
+
+ // ensure that the command handler for the TV is initialised
+ if (bReturn)
+ {
+ CCECCommandHandler *handler = GetTV()->GetHandler();
+ if (handler)
+ handler->InitHandler();
+ }
+
+ return bReturn;
+}
+
+bool CCECProcessor::UnregisterClient(CCECClient *client)
+{
+ if (!client)
+ return false;
+
+ if (client->IsRegistered())
+ m_libcec->AddLog(CEC_LOG_NOTICE, "unregistering client: %s", client->GetConnectionInfo().c_str());
+
+ // notify the client that it will be unregistered
+ client->OnUnregister();
+
+ {
+ CLockObject lock(m_mutex);
+ // find all devices that match the LA's of this client
+ CECDEVICEVEC devices;
+ m_busDevices->GetByLogicalAddresses(devices, client->GetConfiguration()->logicalAddresses);
+ for (CECDEVICEVEC::const_iterator it = devices.begin(); it != devices.end(); it++)
+ {
+ // find the client
+ map<cec_logical_address, CCECClient *>::iterator entry = m_clients.find((*it)->GetLogicalAddress());
+ // unregister the client
+ if (entry != m_clients.end())
+ m_clients.erase(entry);
+
+ // reset the device status
+ (*it)->ResetDeviceStatus();