/*
* This file is part of the libCEC(R) library.
*
- * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited. All rights reserved.
+ * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
* libCEC(R) is an original work, containing original code.
*
* libCEC(R) is a trademark of Pulse-Eight Limited.
* http://www.pulse-eight.net/
*/
+#include "env.h"
#include "CECProcessor.h"
-#include "AdapterCommunication.h"
+#include "adapter/AdapterFactory.h"
+#include "devices/CECBusDevice.h"
+#include "devices/CECAudioSystem.h"
+#include "devices/CECPlaybackDevice.h"
+#include "devices/CECRecordingDevice.h"
+#include "devices/CECTuner.h"
+#include "devices/CECTV.h"
+#include "implementations/CECCommandHandler.h"
#include "LibCEC.h"
-#include "util/StdString.h"
-#include "platform/timeutils.h"
+#include "CECClient.h"
+#include "CECTypeUtils.h"
+#include "platform/util/timeutils.h"
+#include "platform/util/util.h"
using namespace CEC;
using namespace std;
-
-CCECProcessor::CCECProcessor(CLibCEC *controller, CAdapterCommunication *serComm, const char *strDeviceName, cec_logical_address iLogicalAddress /* = CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS*/) :
- m_iPhysicalAddress(iPhysicalAddress),
- m_iLogicalAddress(iLogicalAddress),
- m_strDeviceName(strDeviceName),
- m_communication(serComm),
- m_controller(controller),
- m_bMonitor(false)
+using namespace PLATFORM;
+
+#define CEC_PROCESSOR_SIGNAL_WAIT_TIME 1000
+#define ACTIVE_SOURCE_CHECK_INTERVAL 500
+
+#define ToString(x) CCECTypeUtils::ToString(x)
+
+CCECProcessor::CCECProcessor(CLibCEC *libcec) :
+ m_bInitialised(false),
+ m_communication(NULL),
+ m_libcec(libcec),
+ m_iStandardLineTimeout(3),
+ m_iRetryLineTimeout(3),
+ m_iLastTransmission(0),
+ m_bMonitor(true),
+ m_addrAllocator(NULL),
+ m_bStallCommunication(false)
{
- for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
- m_vendorIds[iPtr] = CEC_VENDOR_UNKNOWN;
- for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
- m_vendorClasses[iPtr] = (uint8_t) 0;
+ m_busDevices = new CCECDeviceMap(this);
}
CCECProcessor::~CCECProcessor(void)
{
- StopThread();
- m_communication = NULL;
- m_controller = NULL;
+ m_bStallCommunication = false;
+ DELETE_AND_NULL(m_addrAllocator);
+ Close();
+ DELETE_AND_NULL(m_busDevices);
}
-bool CCECProcessor::Start(void)
+bool CCECProcessor::Start(const char *strPort, uint16_t iBaudRate /* = CEC_SERIAL_DEFAULT_BAUDRATE */, uint32_t iTimeoutMs /* = CEC_DEFAULT_CONNECT_TIMEOUT */)
{
- if (!m_communication || !m_communication->IsOpen())
- {
- m_controller->AddLog(CEC_LOG_ERROR, "connection is closed");
+ CLockObject lock(m_mutex);
+ // open a connection
+ if (!OpenConnection(strPort, iBaudRate, iTimeoutMs))
return false;
- }
- if (!SetLogicalAddress(m_iLogicalAddress))
+ // create the processor thread
+ if (!IsRunning())
{
- m_controller->AddLog(CEC_LOG_ERROR, "could not set the logical address");
- return false;
+ if (!CreateThread())
+ {
+ m_libcec->AddLog(CEC_LOG_ERROR, "could not create a processor thread");
+ return false;
+ }
}
- if (CreateThread())
- return true;
- else
- m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread");
+ return true;
+}
- return false;
+void CCECProcessor::Close(void)
+{
+ // mark as uninitialised
+ SetCECInitialised(false);
+
+ // stop the processor
+ StopThread();
+
+ // close the connection
+ DELETE_AND_NULL(m_communication);
}
-void *CCECProcessor::Process(void)
+void CCECProcessor::ResetMembers(void)
{
- m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started");
+ // close the connection
+ DELETE_AND_NULL(m_communication);
+
+ // reset the other members to the initial state
+ m_iStandardLineTimeout = 3;
+ m_iRetryLineTimeout = 3;
+ m_iLastTransmission = 0;
+ m_busDevices->ResetDeviceStatus();
+}
- cec_command command;
- cec_adapter_message msg;
+bool CCECProcessor::OpenConnection(const char *strPort, uint16_t iBaudRate, uint32_t iTimeoutMs, bool bStartListening /* = true */)
+{
+ bool bReturn(false);
+ CTimeout timeout(iTimeoutMs > 0 ? iTimeoutMs : CEC_DEFAULT_TRANSMIT_WAIT);
+
+ // ensure that a previous connection is closed
+ Close();
+
+ // reset all member to the initial state
+ ResetMembers();
- while (!IsStopped())
+ // check whether the Close() method deleted any previous connection
+ if (m_communication)
{
- bool bParseFrame(false);
- bool bError(false);
- bool bTransmitSucceeded(false);
- command.clear();
- msg.clear();
+ m_libcec->AddLog(CEC_LOG_ERROR, "previous connection could not be closed");
+ return bReturn;
+ }
- {
- CLockObject lock(&m_mutex);
- if (m_communication->IsOpen() && m_communication->Read(msg, 50))
- ParseMessage(msg, &bError, &bTransmitSucceeded, &bParseFrame);
+ // create a new connection
+ m_communication = CAdapterFactory(this->m_libcec).GetInstance(strPort, iBaudRate);
- bParseFrame &= !IsStopped();
- if (bParseFrame)
- command = m_currentframe;
- }
+ // open a new connection
+ unsigned iConnectTry(0);
+ while (timeout.TimeLeft() > 0 && (bReturn = m_communication->Open((timeout.TimeLeft() / CEC_CONNECT_TRIES), false, bStartListening)) == false)
+ {
+ m_libcec->AddLog(CEC_LOG_ERROR, "could not open a connection (try %d)", ++iConnectTry);
+ m_communication->Close();
+ CEvent::Sleep(CEC_DEFAULT_CONNECT_RETRY_WAIT);
+ }
- if (bParseFrame)
- ParseCommand(command);
+ m_libcec->AddLog(CEC_LOG_NOTICE, "connection opened");
- m_controller->CheckKeypressTimeout();
+ // mark as initialised
+ SetCECInitialised(true);
- if (!IsStopped())
- Sleep(5);
- }
+ return bReturn;
+}
- return NULL;
+bool CCECProcessor::CECInitialised(void)
+{
+ CLockObject lock(m_threadMutex);
+ return m_bInitialised;
}
-bool CCECProcessor::PowerOnDevices(cec_logical_address address /* = CECDEVICE_TV */)
+void CCECProcessor::SetCECInitialised(bool bSetTo /* = true */)
{
- if (!IsRunning())
- return false;
+ {
+ CLockObject lock(m_mutex);
+ m_bInitialised = bSetTo;
+ }
+ if (!bSetTo)
+ UnregisterClients();
+}
- CStdString strLog;
- strLog.Format("<< powering on device with logical address %d", (int8_t)address);
- m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
+bool CCECProcessor::TryLogicalAddress(cec_logical_address address, cec_version libCECSpecVersion /* = CEC_VERSION_1_4 */)
+{
+ // find the device
+ CCECBusDevice *device = m_busDevices->At(address);
+ if (device)
+ {
+ // check if it's already marked as present or used
+ if (device->IsPresent() || device->IsHandledByLibCEC())
+ return false;
- cec_command command;
- cec_command::format(command, m_iLogicalAddress, address, CEC_OPCODE_IMAGE_VIEW_ON);
+ // poll the LA if not
+ return device->TryLogicalAddress(libCECSpecVersion);
+ }
- return Transmit(command);
+ return false;
}
-bool CCECProcessor::StandbyDevices(cec_logical_address address /* = CECDEVICE_BROADCAST */)
+void CCECProcessor::ReplaceHandlers(void)
{
- if (!IsRunning())
- return false;
+ if (!CECInitialised())
+ return;
- CStdString strLog;
- strLog.Format("<< putting device with logical address %d in standby mode", (int8_t)address);
- m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
+ // check each device
+ for (CECDEVICEMAP::iterator it = m_busDevices->Begin(); it != m_busDevices->End(); it++)
+ it->second->ReplaceHandler(true);
+}
- cec_command command;
- cec_command::format(command, m_iLogicalAddress, address, CEC_OPCODE_STANDBY);
+bool CCECProcessor::OnCommandReceived(const cec_command &command)
+{
+ return m_inBuffer.Push(command);
+}
+
+void *CCECProcessor::Process(void)
+{
+ m_libcec->AddLog(CEC_LOG_DEBUG, "processor thread started");
+
+ cec_command command; command.Clear();
+ CTimeout activeSourceCheck(ACTIVE_SOURCE_CHECK_INTERVAL);
+
+ // as long as we're not being stopped and the connection is open
+ while (!IsStopped() && m_communication->IsOpen())
+ {
+ // wait for a new incoming command, and process it
+ if (m_inBuffer.Pop(command, CEC_PROCESSOR_SIGNAL_WAIT_TIME))
+ ProcessCommand(command);
+
+ if (CECInitialised())
+ {
+ // check clients for keypress timeouts
+ m_libcec->CheckKeypressTimeout();
+
+ // check if we need to replace handlers
+ ReplaceHandlers();
+
+ // check whether we need to activate a source, if it failed before
+ if (activeSourceCheck.TimeLeft() == 0)
+ {
+ if (CECInitialised())
+ TransmitPendingActiveSourceCommands();
+ activeSourceCheck.Init(ACTIVE_SOURCE_CHECK_INTERVAL);
+ }
+ }
+ }
- return Transmit(command);
+ return NULL;
}
-bool CCECProcessor::SetActiveView(void)
+bool CCECProcessor::ActivateSource(uint16_t iStreamPath)
{
- if (!IsRunning())
- return false;
+ bool bReturn(false);
- m_controller->AddLog(CEC_LOG_DEBUG, "<< setting active view");
+ // find the device with the given PA
+ CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamPath);
+ // and make it the active source when found
+ if (device)
+ bReturn = device->ActivateSource();
+ else
+ m_libcec->AddLog(CEC_LOG_DEBUG, "device with PA '%04x' not found", iStreamPath);
- cec_command command;
- cec_command::format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_ACTIVE_SOURCE);
- command.parameters.push_back((m_iPhysicalAddress >> 8) & 0xFF);
- command.parameters.push_back(m_iPhysicalAddress & 0xFF);
+ return bReturn;
+}
- return Transmit(command);
+void CCECProcessor::SetStandardLineTimeout(uint8_t iTimeout)
+{
+ CLockObject lock(m_mutex);
+ m_iStandardLineTimeout = iTimeout;
}
-bool CCECProcessor::SetInactiveView(void)
+uint8_t CCECProcessor::GetStandardLineTimeout(void)
{
- if (!IsRunning())
- return false;
+ CLockObject lock(m_mutex);
+ return m_iStandardLineTimeout;
+}
- m_controller->AddLog(CEC_LOG_DEBUG, "<< setting inactive view");
+void CCECProcessor::SetRetryLineTimeout(uint8_t iTimeout)
+{
+ CLockObject lock(m_mutex);
+ m_iRetryLineTimeout = iTimeout;
+}
- cec_command command;
- cec_command::format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_INACTIVE_SOURCE);
- command.parameters.push_back((m_iPhysicalAddress >> 8) & 0xFF);
- command.parameters.push_back(m_iPhysicalAddress & 0xFF);
+uint8_t CCECProcessor::GetRetryLineTimeout(void)
+{
+ CLockObject lock(m_mutex);
+ return m_iRetryLineTimeout;
+}
- return Transmit(command);
+bool CCECProcessor::PhysicalAddressInUse(uint16_t iPhysicalAddress)
+{
+ CCECBusDevice *device = GetDeviceByPhysicalAddress(iPhysicalAddress);
+ return device != NULL;
}
void CCECProcessor::LogOutput(const cec_command &data)
{
- CStdString txStr = "transmit ";
- txStr.AppendFormat(" %02x", ((uint8_t)data.initiator << 4) + (uint8_t)data.destination);
- txStr.AppendFormat(":%02x", (uint8_t)data.opcode);
+ CStdString strTx;
+
+ // initiator and destination
+ strTx.Format("<< %02x", ((uint8_t)data.initiator << 4) + (uint8_t)data.destination);
+ // append the opcode
+ if (data.opcode_set)
+ strTx.AppendFormat(":%02x", (uint8_t)data.opcode);
+
+ // append the parameters
for (uint8_t iPtr = 0; iPtr < data.parameters.size; iPtr++)
- txStr.AppendFormat(":%02x", data.parameters[iPtr]);
- m_controller->AddLog(CEC_LOG_DEBUG, txStr.c_str());
+ strTx.AppendFormat(":%02x", data.parameters[iPtr]);
+
+ // and log it
+ m_libcec->AddLog(CEC_LOG_TRAFFIC, strTx.c_str());
}
-bool CCECProcessor::Transmit(const cec_command &data, bool bWaitForAck /* = true */)
+bool CCECProcessor::PollDevice(cec_logical_address iAddress)
{
- LogOutput(data);
+ // try to find the primary device
+ CCECBusDevice *primary = GetPrimaryDevice();
+ // poll the destination, with the primary as source
+ if (primary)
+ return primary->TransmitPoll(iAddress, false);
- cec_adapter_message output;
- output.clear();
- CAdapterCommunication::FormatAdapterMessage(data, output);
+ CCECBusDevice *device = m_busDevices->At(CECDEVICE_UNREGISTERED);
+ if (device)
+ return device->TransmitPoll(iAddress, false);
- return TransmitFormatted(output, bWaitForAck);
+ return false;
}
-bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress)
+CCECBusDevice *CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress, bool bSuppressUpdate /* = true */)
{
- CStdString strLog;
- strLog.Format("<< setting logical address to %1x", iLogicalAddress);
- m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
-
- m_iLogicalAddress = iLogicalAddress;
- return m_communication && m_communication->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress);
+ return m_busDevices ?
+ m_busDevices->GetDeviceByPhysicalAddress(iPhysicalAddress, bSuppressUpdate) :
+ NULL;
}
-bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress)
+CCECBusDevice *CCECProcessor::GetDevice(cec_logical_address address) const
{
- CStdString strLog;
- strLog.Format("<< setting physical address to %2x", iPhysicalAddress);
- m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
-
- m_iPhysicalAddress = iPhysicalAddress;
- return SetActiveView();
+ return m_busDevices ?
+ m_busDevices->At(address) :
+ NULL;
}
-bool CCECProcessor::SetOSDString(cec_logical_address iLogicalAddress, cec_display_control duration, const char *strMessage)
+cec_logical_address CCECProcessor::GetActiveSource(bool bRequestActiveSource /* = true */)
{
- CStdString strLog;
- strLog.Format("<< display message '%s'", strMessage);
- m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
-
- cec_command command;
- cec_command::format(command, m_iLogicalAddress, iLogicalAddress, CEC_OPCODE_SET_OSD_STRING);
- command.parameters.push_back((uint8_t)duration);
+ // get the device that is marked as active source from the device map
+ CCECBusDevice *activeSource = m_busDevices->GetActiveSource();
+ if (activeSource)
+ return activeSource->GetLogicalAddress();
- for (unsigned int iPtr = 0; iPtr < strlen(strMessage); iPtr++)
- command.parameters.push_back(strMessage[iPtr]);
+ if (bRequestActiveSource)
+ {
+ // request the active source from the bus
+ CCECBusDevice *primary = GetPrimaryDevice();
+ if (primary)
+ {
+ primary->RequestActiveSource();
+ return GetActiveSource(false);
+ }
+ }
- return Transmit(command);
+ // unknown or none
+ return CECDEVICE_UNKNOWN;
}
-bool CCECProcessor::SwitchMonitoring(bool bEnable)
+bool CCECProcessor::IsActiveSource(cec_logical_address iAddress)
{
- CStdString strLog;
- strLog.Format("== %s monitoring mode ==", bEnable ? "enabling" : "disabling");
- m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
-
- m_bMonitor = bEnable;
- if (bEnable)
- return m_communication && m_communication->SetAckMask(0);
- else
- return m_communication && m_communication->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress);
+ CCECBusDevice *device = m_busDevices->At(iAddress);
+ return device && device->IsActiveSource();
}
-bool CCECProcessor::TransmitFormatted(const cec_adapter_message &data, bool bWaitForAck /* = true */)
+bool CCECProcessor::Transmit(const cec_command &data, bool bIsReply)
{
- CLockObject lock(&m_mutex);
- if (!m_communication || !m_communication->Write(data))
- return false;
+ cec_command transmitData(data);
+ uint8_t iMaxTries(0);
+ bool bRetry(true);
+ uint8_t iTries(0);
- if (bWaitForAck)
- {
- uint64_t now = GetTimeMs();
- uint64_t target = now + 1000;
- bool bError(false);
- bool bGotAck(false);
+ // get the current timeout setting
+ uint8_t iLineTimeout(GetStandardLineTimeout());
- while (!bGotAck && now < target)
+ // reset the state of this message to 'unknown'
+ cec_adapter_message_state adapterState = ADAPTER_MESSAGE_STATE_UNKNOWN;
+
+ if (!m_communication->SupportsSourceLogicalAddress(transmitData.initiator))
+ {
+ if (transmitData.initiator == CECDEVICE_UNREGISTERED && m_communication->SupportsSourceLogicalAddress(CECDEVICE_FREEUSE))
{
- bGotAck = WaitForAck(&bError, (uint32_t) (target - now));
- now = GetTimeMs();
+ m_libcec->AddLog(CEC_LOG_DEBUG, "initiator '%s' is not supported by the CEC adapter. using '%s' instead", ToString(transmitData.initiator), ToString(CECDEVICE_FREEUSE));
+ transmitData.initiator = CECDEVICE_FREEUSE;
+ }
+ else
+ {
+ m_libcec->AddLog(CEC_LOG_DEBUG, "initiator '%s' is not supported by the CEC adapter", ToString(transmitData.initiator));
+ return false;
+ }
+ }
- if (bError && now < target)
- {
- m_controller->AddLog(CEC_LOG_ERROR, "retransmitting previous frame");
- if (!m_communication->Write(data))
- return false;
- }
+ LogOutput(transmitData);
+
+ // find the initiator device
+ CCECBusDevice *initiator = m_busDevices->At(transmitData.initiator);
+ if (!initiator)
+ {
+ m_libcec->AddLog(CEC_LOG_WARNING, "invalid initiator");
+ return false;
+ }
+
+ // find the destination device, if it's not the broadcast address
+ if (transmitData.destination != CECDEVICE_BROADCAST)
+ {
+ // check if the device is marked as handled by libCEC
+ CCECBusDevice *destination = m_busDevices->At(transmitData.destination);
+ if (destination && destination->IsHandledByLibCEC())
+ {
+ // and reject the command if it's trying to send data to a device that is handled by libCEC
+ m_libcec->AddLog(CEC_LOG_WARNING, "not sending data to myself!");
+ return false;
}
}
- return true;
+ // wait until we finished allocating a new LA if it got lost
+ while (m_bStallCommunication) Sleep(5);
+
+ {
+ CLockObject lock(m_mutex);
+ m_iLastTransmission = GetTimeMs();
+ // set the number of tries
+ iMaxTries = initiator->GetHandler()->GetTransmitRetries() + 1;
+ initiator->MarkHandlerReady();
+ }
+
+ // and try to send the command
+ while (bRetry && ++iTries < iMaxTries)
+ {
+ if (initiator->IsUnsupportedFeature(transmitData.opcode))
+ return false;
+
+ adapterState = !IsStopped() && m_communication && m_communication->IsOpen() ?
+ m_communication->Write(transmitData, bRetry, iLineTimeout, bIsReply) :
+ ADAPTER_MESSAGE_STATE_ERROR;
+ iLineTimeout = m_iRetryLineTimeout;
+ }
+
+ return adapterState == ADAPTER_MESSAGE_STATE_SENT_ACKED;
}
-void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode, ECecAbortReason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
+void CCECProcessor::TransmitAbort(cec_logical_address source, cec_logical_address destination, cec_opcode opcode, cec_abort_reason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
{
- m_controller->AddLog(CEC_LOG_DEBUG, "<< transmitting abort message");
+ m_libcec->AddLog(CEC_LOG_DEBUG, "<< transmitting abort message");
cec_command command;
- cec_command::format(command, m_iLogicalAddress, address, CEC_OPCODE_FEATURE_ABORT);
- command.parameters.push_back((uint8_t)opcode);
- command.parameters.push_back((uint8_t)reason);
+ cec_command::Format(command, source, destination, CEC_OPCODE_FEATURE_ABORT);
+ command.parameters.PushBack((uint8_t)opcode);
+ command.parameters.PushBack((uint8_t)reason);
- Transmit(command);
+ Transmit(command, true);
}
-void CCECProcessor::ReportCECVersion(cec_logical_address address /* = CECDEVICE_TV */)
+void CCECProcessor::ProcessCommand(const cec_command &command)
{
- m_controller->AddLog(CEC_LOG_NOTICE, "<< reporting CEC version as 1.3a");
+ // log the command
+ CStdString dataStr;
+ dataStr.Format(">> %1x%1x", command.initiator, command.destination);
+ if (command.opcode_set == 1)
+ dataStr.AppendFormat(":%02x", command.opcode);
+ for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
+ dataStr.AppendFormat(":%02x", (unsigned int)command.parameters[iPtr]);
+ m_libcec->AddLog(CEC_LOG_TRAFFIC, dataStr.c_str());
- cec_command command;
- cec_command::format(command, m_iLogicalAddress, address, CEC_OPCODE_CEC_VERSION);
- command.parameters.push_back(CEC_VERSION_1_3A);
+ // find the initiator
+ CCECBusDevice *device = m_busDevices->At(command.initiator);
- Transmit(command);
+ if (device)
+ device->HandleCommand(command);
}
-void CCECProcessor::ReportPowerState(cec_logical_address address /*= CECDEVICE_TV */, bool bOn /* = true */)
+bool CCECProcessor::IsPresentDevice(cec_logical_address address)
{
- if (bOn)
- m_controller->AddLog(CEC_LOG_NOTICE, "<< reporting \"On\" power status");
- else
- m_controller->AddLog(CEC_LOG_NOTICE, "<< reporting \"Off\" power status");
+ CCECBusDevice *device = m_busDevices->At(address);
+ return device && device->GetStatus() == CEC_DEVICE_STATUS_PRESENT;
+}
- cec_command command;
- cec_command::format(command, m_iLogicalAddress, address, CEC_OPCODE_REPORT_POWER_STATUS);
- command.parameters.push_back(bOn ? (uint8_t) CEC_POWER_STATUS_ON : (uint8_t) CEC_POWER_STATUS_STANDBY);
+bool CCECProcessor::IsPresentDeviceType(cec_device_type type)
+{
+ CECDEVICEVEC devices;
+ m_busDevices->GetByType(type, devices);
+ CCECDeviceMap::FilterActive(devices);
+ return !devices.empty();
+}
- Transmit(command);
+uint16_t CCECProcessor::GetDetectedPhysicalAddress(void) const
+{
+ return m_communication ? m_communication->GetPhysicalAddress() : CEC_INVALID_PHYSICAL_ADDRESS;
}
-void CCECProcessor::ReportMenuState(cec_logical_address address /* = CECDEVICE_TV */, bool bActive /* = true */)
+bool CCECProcessor::ClearLogicalAddresses(void)
{
- if (bActive)
- m_controller->AddLog(CEC_LOG_NOTICE, "<< reporting menu state as active");
- else
- m_controller->AddLog(CEC_LOG_NOTICE, "<< reporting menu state as inactive");
+ cec_logical_addresses addresses; addresses.Clear();
+ return SetLogicalAddresses(addresses);
+}
- cec_command command;
- cec_command::format(command, m_iLogicalAddress, address, CEC_OPCODE_MENU_STATUS);
- command.parameters.push_back(bActive ? (uint8_t) CEC_MENU_STATE_ACTIVATED : (uint8_t) CEC_MENU_STATE_DEACTIVATED);
+bool CCECProcessor::SetLogicalAddresses(const cec_logical_addresses &addresses)
+{
+ return m_communication ? m_communication->SetLogicalAddresses(addresses) : false;
+}
- Transmit(command);
+bool CCECProcessor::StandbyDevices(const cec_logical_address initiator, const CECDEVICEVEC &devices)
+{
+ bool bReturn(true);
+ for (CECDEVICEVEC::const_iterator it = devices.begin(); it != devices.end(); it++)
+ bReturn &= (*it)->Standby(initiator);
+ return bReturn;
}
-void CCECProcessor::ReportVendorID(cec_logical_address address /* = CECDEVICE_TV */)
+bool CCECProcessor::StandbyDevice(const cec_logical_address initiator, cec_logical_address address)
{
- m_controller->AddLog(CEC_LOG_NOTICE, "<< vendor ID requested, feature abort");
- TransmitAbort(address, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
+ CCECBusDevice *device = m_busDevices->At(address);
+ return device ? device->Standby(initiator) : false;
}
-void CCECProcessor::ReportOSDName(cec_logical_address address /* = CECDEVICE_TV */)
+bool CCECProcessor::PowerOnDevices(const cec_logical_address initiator, const CECDEVICEVEC &devices)
{
- const char *osdname = m_strDeviceName.c_str();
- CStdString strLog;
- strLog.Format("<< reporting OSD name as %s", osdname);
- m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
+ bool bReturn(true);
+ for (CECDEVICEVEC::const_iterator it = devices.begin(); it != devices.end(); it++)
+ bReturn &= (*it)->PowerOn(initiator);
+ return bReturn;
+}
- cec_command command;
- cec_command::format(command, m_iLogicalAddress, address, CEC_OPCODE_SET_OSD_NAME);
- for (unsigned int iPtr = 0; iPtr < strlen(osdname); iPtr++)
- command.parameters.push_back(osdname[iPtr]);
+bool CCECProcessor::PowerOnDevice(const cec_logical_address initiator, cec_logical_address address)
+{
+ CCECBusDevice *device = m_busDevices->At(address);
+ return device ? device->PowerOn(initiator) : false;
+}
+
+bool CCECProcessor::StartBootloader(const char *strPort /* = NULL */)
+{
+ bool bReturn(false);
+ // open a connection if no connection has been opened
+ if (!m_communication && strPort)
+ {
+ CAdapterFactory factory(this->m_libcec);
+ IAdapterCommunication *comm = factory.GetInstance(strPort);
+ CTimeout timeout(CEC_DEFAULT_CONNECT_TIMEOUT);
+ int iConnectTry(0);
+ while (timeout.TimeLeft() > 0 && (bReturn = comm->Open(timeout.TimeLeft() / CEC_CONNECT_TRIES, true)) == false)
+ {
+ m_libcec->AddLog(CEC_LOG_ERROR, "could not open a connection (try %d)", ++iConnectTry);
+ comm->Close();
+ Sleep(CEC_DEFAULT_TRANSMIT_RETRY_WAIT);
+ }
+ if (comm->IsOpen())
+ {
+ bReturn = comm->StartBootloader();
+ DELETE_AND_NULL(comm);
+ }
+ return bReturn;
+ }
+ else
+ {
+ m_communication->StartBootloader();
+ Close();
+ bReturn = true;
+ }
- Transmit(command);
+ return bReturn;
}
-void CCECProcessor::ReportPhysicalAddress(void)
+bool CCECProcessor::PingAdapter(void)
{
- CStdString strLog;
- strLog.Format("<< reporting physical address as %04x", m_iPhysicalAddress);
- m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
+ return m_communication->PingAdapter();
+}
- cec_command command;
- cec_command::format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS);
- command.parameters.push_back((uint8_t) ((m_iPhysicalAddress >> 8) & 0xFF));
- command.parameters.push_back((uint8_t) (m_iPhysicalAddress & 0xFF));
- command.parameters.push_back((uint8_t) (CEC_DEVICE_TYPE_PLAYBACK_DEVICE));
+void CCECProcessor::HandlePoll(cec_logical_address initiator, cec_logical_address destination)
+{
+ CCECBusDevice *device = m_busDevices->At(destination);
+ if (device)
+ device->HandlePollFrom(initiator);
+}
- Transmit(command);
+bool CCECProcessor::HandleReceiveFailed(cec_logical_address initiator)
+{
+ CCECBusDevice *device = m_busDevices->At(initiator);
+ return !device || !device->HandleReceiveFailed();
}
-void CCECProcessor::BroadcastActiveSource(void)
+bool CCECProcessor::CanPersistConfiguration(void)
{
- m_controller->AddLog(CEC_LOG_NOTICE, "<< broadcasting active source");
+ return m_communication ? m_communication->GetFirmwareVersion() >= 2 : false;
+}
- cec_command command;
- cec_command::format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_ACTIVE_SOURCE);
- command.parameters.push_back((uint8_t) ((m_iPhysicalAddress >> 8) & 0xFF));
- command.parameters.push_back((uint8_t) (m_iPhysicalAddress & 0xFF));
+bool CCECProcessor::PersistConfiguration(const libcec_configuration &configuration)
+{
+ libcec_configuration persistConfiguration = configuration;
+ if (!CLibCEC::IsValidPhysicalAddress(configuration.iPhysicalAddress))
+ {
+ CCECBusDevice *device = GetPrimaryDevice();
+ if (device)
+ persistConfiguration.iPhysicalAddress = device->GetCurrentPhysicalAddress();
+ }
- Transmit(command);
+ return m_communication ? m_communication->PersistConfiguration(persistConfiguration) : false;
}
-bool CCECProcessor::WaitForAck(bool *bError, uint32_t iTimeout /* = 1000 */)
+void CCECProcessor::RescanActiveDevices(void)
{
- bool bTransmitSucceeded = false, bEom = false;
- *bError = false;
+ for (CECDEVICEMAP::iterator it = m_busDevices->Begin(); it != m_busDevices->End(); it++)
+ it->second->GetStatus(true);
+}
- int64_t iNow = GetTimeMs();
- int64_t iTargetTime = iNow + (uint64_t) iTimeout;
+bool CCECProcessor::GetDeviceInformation(const char *strPort, libcec_configuration *config, uint32_t iTimeoutMs /* = CEC_DEFAULT_CONNECT_TIMEOUT */)
+{
+ if (!OpenConnection(strPort, CEC_SERIAL_DEFAULT_BAUDRATE, iTimeoutMs, false))
+ return false;
- while (!bTransmitSucceeded && !*bError && (iTimeout == 0 || iNow < iTargetTime))
- {
- cec_adapter_message msg;
- msg.clear();
+ config->iFirmwareVersion = m_communication->GetFirmwareVersion();
+ config->iPhysicalAddress = m_communication->GetPhysicalAddress();
+ config->iFirmwareBuildDate = m_communication->GetFirmwareBuildDate();
+ config->adapterType = m_communication->GetAdapterType();
- if (!m_communication->Read(msg, iTimeout > 0 ? (int32_t)(iTargetTime - iNow) : 1000))
- {
- iNow = GetTimeMs();
- continue;
- }
+ return true;
+}
- ParseMessage(msg, bError, &bTransmitSucceeded, &bEom, false);
- iNow = GetTimeMs();
- }
+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;
+}
- return bTransmitSucceeded && !*bError;
+CCECTV *CCECProcessor::GetTV(void) const
+{
+ return CCECBusDevice::AsTV(m_busDevices->At(CECDEVICE_TV));
}
-void CCECProcessor::ParseMessage(cec_adapter_message &msg, bool *bError, bool *bTransmitSucceeded, bool *bEom, bool bProcessMessages /* = true */)
+CCECAudioSystem *CCECProcessor::GetAudioSystem(void) const
{
- *bError = false;
- *bTransmitSucceeded = false;
- *bEom = false;
+ return CCECBusDevice::AsAudioSystem(m_busDevices->At(CECDEVICE_AUDIOSYSTEM));
+}
- if (msg.empty())
- return;
+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::AllocateLogicalAddresses(CCECClient* client)
+{
+ libcec_configuration &configuration = *client->GetConfiguration();
- CStdString logStr;
+ // mark as unregistered
+ client->SetRegistered(false);
- switch(msg.message())
+ // 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++)
{
- 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[1] << 8) | (msg[2]) : 0;
- uint32_t iTime = (msg.size() >= 7) ? (msg[3] << 24) | (msg[4] << 16) | (msg[5] << 8) | (msg[6]) : 0;
- logStr.AppendFormat(" line:%i", iLine);
- logStr.AppendFormat(" time:%u", iTime);
- m_controller->AddLog(CEC_LOG_WARNING, logStr.c_str());
- *bError = true;
- }
- break;
- case MSGCODE_FRAME_START:
- {
- if (bProcessMessages)
- {
- logStr = "MSGCODE_FRAME_START";
- m_currentframe.clear();
- if (msg.size() >= 2)
- {
- logStr.AppendFormat(" initiator:%u destination:%u ack:%s %s", msg.initiator(), msg.destination(), msg.ack() ? "high" : "low", msg.eom() ? "eom" : "");
- m_currentframe.initiator = msg.initiator();
- m_currentframe.destination = msg.destination();
- m_currentframe.ack = msg.ack();
- m_currentframe.eom = msg.eom();
- }
- m_controller->AddLog(CEC_LOG_DEBUG, logStr.c_str());
- }
- else
- {
- m_frameBuffer.Push(msg);
- }
- }
- break;
- case MSGCODE_FRAME_DATA:
- {
- if (bProcessMessages)
- {
- logStr = "MSGCODE_FRAME_DATA";
- if (msg.size() >= 2)
- {
- uint8_t iData = msg[1];
- logStr.AppendFormat(" %02x", iData);
- m_currentframe.push_back(iData);
- m_currentframe.eom = msg.eom();
- }
- m_controller->AddLog(CEC_LOG_DEBUG, logStr.c_str());
- }
- else
- {
- m_frameBuffer.Push(msg);
- }
+ // remove client entry
+ CLockObject lock(m_mutex);
+ m_clients.erase((*it)->GetLogicalAddress());
+ }
- *bEom = msg.eom();
- }
- break;
- case MSGCODE_COMMAND_ACCEPTED:
- m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_COMMAND_ACCEPTED");
- break;
- case MSGCODE_TRANSMIT_SUCCEEDED:
- m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_TRANSMIT_SUCCEEDED");
- *bTransmitSucceeded = true;
- break;
- case MSGCODE_RECEIVE_FAILED:
- m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_RECEIVE_FAILED");
- *bError = true;
- break;
- case MSGCODE_COMMAND_REJECTED:
- m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_COMMAND_REJECTED");
- *bError = true;
- break;
- case MSGCODE_TRANSMIT_FAILED_LINE:
- m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_LINE");
- *bError = true;
- break;
- case MSGCODE_TRANSMIT_FAILED_ACK:
- m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_ACK");
- *bError = true;
- break;
- case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA:
- m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA");
- *bError = true;
- break;
- case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE:
- m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE");
- *bError = true;
- break;
- default:
- break;
+ // 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;
}
-}
-void CCECProcessor::ParseVendorId(cec_logical_address device, const cec_datapacket &data)
-{
- if (data.size < 3)
+ // 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++)
{
- m_controller->AddLog(CEC_LOG_WARNING, "invalid vendor ID received");
- return;
+ // 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<cec_logical_address, CCECClient *>((*it)->GetLogicalAddress(), client));
}
- uint64_t iVendorId = ((uint64_t)data[0] << 3) +
- ((uint64_t)data[1] << 2) +
- (uint64_t)data[2];
+ // set the new ackmask
+ SetLogicalAddresses(GetLogicalAddresses());
- m_vendorIds[(uint8_t)device] = iVendorId;
- m_vendorClasses[(uint8_t)device] = data.size >= 4 ? data[3] : 0;
+ // resume outgoing communication
+ m_bStallCommunication = false;
- CStdString strLog;
- strLog.Format("device %d: vendor = %s (%04x) class = %2x", (uint8_t)device, CECVendorIdToString(m_vendorIds[(uint8_t)device]), iVendorId, m_vendorClasses[(uint8_t)device]);
- m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
+ return true;
}
-bool CCECProcessor::HandleANCommand(cec_command &command)
+bool CCECProcessor::RegisterClient(CCECClient *client)
{
- bool bHandled(true);
- if (command.destination == m_iLogicalAddress)
+ 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())
{
- switch(command.opcode)
- {
- case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN:
- if (command.parameters.size > 0)
- {
- m_controller->AddKey();
-
- uint8_t iButton = 0;
- switch (command.parameters[0])
- {
- case CEC_AN_USER_CONTROL_CODE_RETURN:
- iButton = CEC_USER_CONTROL_CODE_PREVIOUS_CHANNEL;
- break;
- default:
- break;
- }
-
- if (iButton > 0 && iButton <= CEC_USER_CONTROL_CODE_MAX)
- {
- CStdString strLog;
- strLog.Format("key pressed: %1x", iButton);
- m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
-
- m_controller->SetCurrentButton((cec_user_control_code) command.parameters[0]);
- }
- }
- break;
- case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP:
- m_controller->AddKey();
- break;
- default:
- bHandled = false;
- break;
- }
+ m_libcec->AddLog(CEC_LOG_ERROR, "failed to register a new CEC client: CEC processor is not initialised");
+ return false;
}
- else if (command.destination == CECDEVICE_BROADCAST)
+
+ // 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))
{
- switch(command.opcode)
- {
- // TODO
- default:
- bHandled = false;
- break;
- }
+ while (!tv->ReplaceHandler(false))
+ CEvent::Sleep(5);
}
- if (!bHandled)
- bHandled = HandleCecCommand(command);
+ // get the configuration from the client
+ m_libcec->AddLog(CEC_LOG_NOTICE, "registering new CEC client - v%s", ToString((cec_client_version)configuration.clientVersion));
- return bHandled;
-}
+ // get the current ackmask, so we can restore it if polling fails
+ cec_logical_addresses previousMask = GetLogicalAddresses();
-bool CCECProcessor::HandleSLCommand(cec_command &command)
-{
- bool bHandled(true);
- if (command.destination == m_iLogicalAddress)
+ // mark as uninitialised
+ client->SetInitialised(false);
+
+ // find logical addresses for this client
+ if (!AllocateLogicalAddresses(client))
{
- switch(command.opcode)
- {
- // TODO
- default:
- bHandled = false;
- break;
- }
+ m_libcec->AddLog(CEC_LOG_ERROR, "failed to register the new CEC client - cannot allocate the requested device types");
+ SetLogicalAddresses(previousMask);
+ return false;
}
- else if (command.destination == CECDEVICE_BROADCAST)
+
+ // get the settings from the rom
+ if (configuration.bGetSettingsFromROM == 1)
{
- switch(command.opcode)
- {
- // TODO
- default:
- bHandled = false;
- break;
- }
+ 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);
}
- if (!bHandled)
- bHandled = HandleCecCommand(command);
+ // 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 bHandled;
+ return bReturn;
}
-bool CCECProcessor::HandleCecCommand(cec_command &command)
+bool CCECProcessor::UnregisterClient(CCECClient *client)
{
- bool bHandled(true);
+ if (!client)
+ return false;
- if (command.destination == m_iLogicalAddress)
- {
- switch(command.opcode)
- {
- case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS:
- ReportPhysicalAddress();
- break;
- case CEC_OPCODE_GIVE_OSD_NAME:
- ReportOSDName(command.initiator);
- break;
- case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID:
- ReportVendorID(command.initiator);
- break;
- case CEC_OPCODE_DEVICE_VENDOR_ID:
- case CEC_OPCODE_VENDOR_COMMAND_WITH_ID:
- ParseVendorId(command.initiator, command.parameters);
- break;
- case CEC_OPCODE_GIVE_DECK_STATUS:
- // need to support opcodes play and deck control before doing anything with this
- TransmitAbort(command.initiator, CEC_OPCODE_GIVE_DECK_STATUS);
- break;
- case CEC_OPCODE_MENU_REQUEST:
- if (command.parameters[0] == CEC_MENU_REQUEST_TYPE_QUERY)
- ReportMenuState(command.initiator);
- break;
- case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS:
- ReportPowerState(command.initiator);
- break;
- case CEC_OPCODE_GET_CEC_VERSION:
- ReportCECVersion(command.initiator);
- break;
- case CEC_OPCODE_USER_CONTROL_PRESSED:
- if (command.parameters.size > 0)
- {
- m_controller->AddKey();
+ if (client->IsRegistered())
+ m_libcec->AddLog(CEC_LOG_NOTICE, "unregistering client: %s", client->GetConnectionInfo().c_str());
- if (command.parameters[0] <= CEC_USER_CONTROL_CODE_MAX)
- {
- CStdString strLog;
- strLog.Format("key pressed: %1x", command.parameters[0]);
- m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
+ // notify the client that it will be unregistered
+ client->OnUnregister();
- m_controller->SetCurrentButton((cec_user_control_code) command.parameters[0]);
- }
- }
- break;
- case CEC_OPCODE_USER_CONTROL_RELEASE:
- m_controller->AddKey();
- break;
- default:
- m_controller->AddCommand(command);
- bHandled = false;
- break;
- }
- }
- else if (command.destination == CECDEVICE_BROADCAST)
{
- CStdString strLog;
- switch (command.opcode)
+ 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++)
{
- case CEC_OPCODE_REQUEST_ACTIVE_SOURCE:
- strLog.Format(">> %i requests active source", (uint8_t) command.initiator);
- m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
- BroadcastActiveSource();
- break;
- case CEC_OPCODE_SET_STREAM_PATH:
- if (command.parameters.size >= 2)
- {
- int streamaddr = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
- strLog.Format(">> %i requests stream path from physical address %04x", command.initiator, streamaddr);
- m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
- if (streamaddr == m_iPhysicalAddress)
- BroadcastActiveSource();
- }
- break;
- case CEC_OPCODE_ROUTING_CHANGE:
- if (command.parameters.size == 4)
- {
- uint16_t iOldAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
- uint16_t iNewAddress = ((uint16_t)command.parameters[2] << 8) | ((uint16_t)command.parameters[3]);
- strLog.Format(">> %i changed physical address from %04x to %04x", command.initiator, iOldAddress, iNewAddress);
- m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
-
- m_controller->AddCommand(command);
- }
- break;
- case CEC_OPCODE_DEVICE_VENDOR_ID:
- case CEC_OPCODE_VENDOR_COMMAND_WITH_ID:
- ParseVendorId(command.initiator, command.parameters);
- break;
- default:
- m_controller->AddCommand(command);
- bHandled = false;
- break;
+ // 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();
}
}
- else
+
+ // set the new ackmask
+ cec_logical_addresses addresses = GetLogicalAddresses();
+ if (SetLogicalAddresses(addresses))
{
- CStdString strLog;
- strLog.Format("ignoring frame: destination: %u != %u", command.destination, (uint8_t)m_iLogicalAddress);
- m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
- bHandled = false;
+ // no more clients left, disable controlled mode
+ if (addresses.IsEmpty() && !m_bMonitor)
+ m_communication->SetControlledMode(false);
+
+ return true;
}
- return bHandled;
+ return false;
}
-void CCECProcessor::ParseCommand(cec_command &command)
+void CCECProcessor::UnregisterClients(void)
{
- CStdString dataStr;
- dataStr.Format(">> received frame: %1x%1x:%02x", command.initiator, command.destination, command.opcode);
- for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
- dataStr.AppendFormat(":%02x", (unsigned int)command.parameters[iPtr]);
- m_controller->AddLog(CEC_LOG_DEBUG, dataStr.c_str());
+ m_libcec->AddLog(CEC_LOG_NOTICE, "unregistering all CEC clients");
+
+ vector<CCECClient *> clients = m_libcec->GetClients();
+ for (vector<CCECClient *>::iterator client = clients.begin(); client != clients.end(); client++)
+ UnregisterClient(*client);
+
+ CLockObject lock(m_mutex);
+ m_clients.clear();
+}
+
+CCECClient *CCECProcessor::GetClient(const cec_logical_address address)
+{
+ CLockObject lock(m_mutex);
+ map<cec_logical_address, CCECClient *>::const_iterator client = m_clients.find(address);
+ if (client != m_clients.end())
+ return client->second;
+ return NULL;
+}
+
+CCECClient *CCECProcessor::GetPrimaryClient(void)
+{
+ CLockObject lock(m_mutex);
+ map<cec_logical_address, CCECClient *>::const_iterator client = m_clients.begin();
+ if (client != m_clients.end())
+ return client->second;
+ return NULL;
+}
+
+CCECBusDevice *CCECProcessor::GetPrimaryDevice(void)
+{
+ return m_busDevices->At(GetLogicalAddress());
+}
+
+cec_logical_address CCECProcessor::GetLogicalAddress(void)
+{
+ cec_logical_addresses addresses = GetLogicalAddresses();
+ return addresses.primary;
+}
+
+cec_logical_addresses CCECProcessor::GetLogicalAddresses(void)
+{
+ CLockObject lock(m_mutex);
+ cec_logical_addresses addresses;
+ addresses.Clear();
+ for (map<cec_logical_address, CCECClient *>::const_iterator client = m_clients.begin(); client != m_clients.end(); client++)
+ addresses.Set(client->first);
+
+ return addresses;
+}
+
+bool CCECProcessor::IsHandledByLibCEC(const cec_logical_address address) const
+{
+ CCECBusDevice *device = GetDevice(address);
+ return device && device->IsHandledByLibCEC();
+}
- if (!m_bMonitor)
+bool CCECProcessor::IsRunningLatestFirmware(void)
+{
+ return m_communication && m_communication->IsOpen() ?
+ m_communication->IsRunningLatestFirmware() :
+ true;
+}
+
+void CCECProcessor::SwitchMonitoring(bool bSwitchTo)
+{
{
- switch(m_vendorIds[command.initiator])
- {
- case CEC_VENDOR_LG:
- HandleSLCommand(command);
- break;
- case CEC_VENDOR_SAMSUNG:
- HandleANCommand(command);
- break;
- default:
- HandleCecCommand(command);
- break;
- }
+ CLockObject lock(m_mutex);
+ m_bMonitor = bSwitchTo;
}
+ if (bSwitchTo)
+ UnregisterClients();
}
-const char *CCECProcessor::CECVendorIdToString(const uint64_t iVendorId)
+void CCECProcessor::HandleLogicalAddressLost(cec_logical_address oldAddress)
{
- switch (iVendorId)
+ // stall outgoing messages until we know our new LA
+ m_bStallCommunication = true;
+
+ m_libcec->AddLog(CEC_LOG_NOTICE, "logical address %x was taken by another device, allocating a new address", oldAddress);
+ CCECClient* client = GetClient(oldAddress);
+ if (client)
{
- case CEC_VENDOR_SAMSUNG:
- return "Samsung";
- case CEC_VENDOR_LG:
- return "LG";
- default:
- return "Unknown";
+ if (m_addrAllocator)
+ while (m_addrAllocator->IsRunning()) Sleep(5);
+ delete m_addrAllocator;
+
+ m_addrAllocator = new CCECAllocateLogicalAddress(this, client);
+ m_addrAllocator->CreateThread();
}
}
+
+CCECAllocateLogicalAddress::CCECAllocateLogicalAddress(CCECProcessor* processor, CCECClient* client) :
+ m_processor(processor),
+ m_client(client) { }
+
+void* CCECAllocateLogicalAddress::Process(void)
+{
+ m_processor->AllocateLogicalAddresses(m_client);
+ return NULL;
+}