- case MSGCODE_NOTHING:
- m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_NOTHING");
- break;
- case MSGCODE_TIMEOUT_ERROR:
- case MSGCODE_HIGH_ERROR:
- case MSGCODE_LOW_ERROR:
- {
- if (msg->message() == MSGCODE_TIMEOUT_ERROR)
- logStr = "MSGCODE_TIMEOUT";
- else if (msg->message() == MSGCODE_HIGH_ERROR)
- logStr = "MSGCODE_HIGH_ERROR";
- else
- logStr = "MSGCODE_LOW_ERROR";
-
- int iLine = (msg->size() >= 3) ? (msg->at(1) << 8) | (msg->at(2)) : 0;
- uint32_t iTime = (msg->size() >= 7) ? (msg->at(3) << 24) | (msg->at(4) << 16) | (msg->at(5) << 8) | (msg->at(6)) : 0;
- logStr.AppendFormat(" line:%i", iLine);
- logStr.AppendFormat(" time:%u", iTime);
- m_controller->AddLog(CEC_LOG_WARNING, logStr.c_str());
- }
- break;
- case MSGCODE_FRAME_START:
+ m_libcec->AddLog(CEC_LOG_ERROR, "failed to register a new CEC client: CEC processor is not initialised");
+ return false;
+ }
+
+ // unregister the client first if it's already been marked as registered
+ if (client->IsRegistered())
+ UnregisterClient(client);
+
+ // ensure that controlled mode is enabled
+ m_communication->SetControlledMode(true);
+
+ // ensure that we know the vendor id of the TV
+ CCECBusDevice *tv = GetTV();
+ cec_vendor_id tvVendor = CEC_VENDOR_UNKNOWN;
+ if (m_communication->SupportsSourceLogicalAddress(CECDEVICE_UNREGISTERED))
+ tvVendor = tv->GetVendorId(CECDEVICE_UNREGISTERED);
+ else if (m_communication->SupportsSourceLogicalAddress(CECDEVICE_FREEUSE))
+ tvVendor = tv->GetVendorId(CECDEVICE_FREEUSE);
+
+ // wait until the handler is replaced, to avoid double registrations
+ if (tvVendor != CEC_VENDOR_UNKNOWN &&
+ CCECCommandHandler::HasSpecificHandler(tvVendor))
+ {
+ while (!tv->ReplaceHandler(false))
+ CEvent::Sleep(5);
+ }
+
+ // get the configuration from the client
+ m_libcec->AddLog(CEC_LOG_NOTICE, "registering new CEC client - v%s", ToString((cec_client_version)configuration.clientVersion));
+
+ // 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 (!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;
+ }
+
+ // get the settings from the rom
+ if (configuration.bGetSettingsFromROM == 1)
+ {
+ libcec_configuration config; config.Clear();
+ 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();
+ configuration.adapterType = m_communication->GetAdapterType();
+
+ // mark the client as registered
+ client->SetRegistered(true);
+
+ // initialise the client
+ bool bReturn = 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();
+ GetTV()->MarkHandlerReady();
+ }
+
+ 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++)