+ // 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);