/*
* 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/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;
+using namespace PLATFORM;
-CCECProcessor::CCECProcessor(CLibCEC *controller, CAdapterCommunication *serComm, const char *strDeviceName, cec_logical_address iLogicalAddress /* = CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS*/) :
- m_bStarted(false),
- m_strDeviceName(strDeviceName),
- m_communication(serComm),
- m_controller(controller),
- m_bMonitor(false)
-{
- m_logicalAddresses.clear();
- m_logicalAddresses.set(iLogicalAddress);
- m_types.clear();
- for (int iPtr = 0; iPtr < 16; iPtr++)
- m_busDevices[iPtr] = new CCECBusDevice(this, (cec_logical_address) iPtr, iPtr == iLogicalAddress ? iPhysicalAddress : 0);
-}
-
-CCECProcessor::CCECProcessor(CLibCEC *controller, CAdapterCommunication *serComm, const char *strDeviceName, const cec_device_type_list &types) :
- m_bStarted(false),
- m_strDeviceName(strDeviceName),
- m_types(types),
- m_communication(serComm),
- m_controller(controller),
- m_bMonitor(false)
-{
- m_logicalAddresses.clear();
- for (int iPtr = 0; iPtr < 16; iPtr++)
+#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)
+{
+ m_busDevices = new CCECDeviceMap(this);
+}
+
+CCECProcessor::~CCECProcessor(void)
+{
+ m_bStallCommunication = false;
+ DELETE_AND_NULL(m_addrAllocator);
+ Close();
+ DELETE_AND_NULL(m_busDevices);
+}
+
+bool CCECProcessor::Start(const char *strPort, uint16_t iBaudRate /* = CEC_SERIAL_DEFAULT_BAUDRATE */, uint32_t iTimeoutMs /* = CEC_DEFAULT_CONNECT_TIMEOUT */)
+{
+ CLockObject lock(m_mutex);
+ // open a connection
+ if (!OpenConnection(strPort, iBaudRate, iTimeoutMs))
+ return false;
+
+ // create the processor thread
+ if (!IsRunning())
{
- switch(iPtr)
+ if (!CreateThread())
{
- case CECDEVICE_AUDIOSYSTEM:
- m_busDevices[iPtr] = new CCECAudioSystem(this, (cec_logical_address) iPtr, 0xFFFF);
- break;
- case CECDEVICE_PLAYBACKDEVICE1:
- case CECDEVICE_PLAYBACKDEVICE2:
- case CECDEVICE_PLAYBACKDEVICE3:
- m_busDevices[iPtr] = new CCECPlaybackDevice(this, (cec_logical_address) iPtr, 0xFFFF);
- break;
- case CECDEVICE_RECORDINGDEVICE1:
- case CECDEVICE_RECORDINGDEVICE2:
- case CECDEVICE_RECORDINGDEVICE3:
- m_busDevices[iPtr] = new CCECRecordingDevice(this, (cec_logical_address) iPtr, 0xFFFF);
- break;
- case CECDEVICE_TUNER1:
- case CECDEVICE_TUNER2:
- case CECDEVICE_TUNER3:
- case CECDEVICE_TUNER4:
- m_busDevices[iPtr] = new CCECTuner(this, (cec_logical_address) iPtr, 0xFFFF);
- break;
- case CECDEVICE_TV:
- m_busDevices[iPtr] = new CCECTV(this, (cec_logical_address) iPtr, 0);
- break;
- default:
- m_busDevices[iPtr] = new CCECBusDevice(this, (cec_logical_address) iPtr, 0xFFFF);
- break;
+ m_libcec->AddLog(CEC_LOG_ERROR, "could not create a processor thread");
+ return false;
}
}
+
+ return true;
}
-CCECProcessor::~CCECProcessor(void)
+void CCECProcessor::Close(void)
{
- m_startCondition.Broadcast();
+ // mark as uninitialised
+ SetCECInitialised(false);
+
+ // stop the processor
+ StopThread(-1);
+ m_inBuffer.Broadcast();
StopThread();
- m_communication = NULL;
- m_controller = NULL;
- for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
- delete m_busDevices[iPtr];
+
+ // close the connection
+ DELETE_AND_NULL(m_communication);
+}
+
+void CCECProcessor::ResetMembers(void)
+{
+ // 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();
}
-bool CCECProcessor::Start(void)
+bool CCECProcessor::OpenConnection(const char *strPort, uint16_t iBaudRate, uint32_t iTimeoutMs, bool bStartListening /* = true */)
{
- CLockObject lock(&m_mutex);
- if (!m_communication || !m_communication->IsOpen())
+ 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();
+
+ // check whether the Close() method deleted any previous connection
+ if (m_communication)
{
- m_controller->AddLog(CEC_LOG_ERROR, "connection is closed");
- return false;
+ m_libcec->AddLog(CEC_LOG_ERROR, "previous connection could not be closed");
+ return bReturn;
}
- if (CreateThread())
+ // create a new connection
+ m_communication = CAdapterFactory(this->m_libcec).GetInstance(strPort, iBaudRate);
+
+ // open a new connection
+ unsigned iConnectTry(0);
+ while (timeout.TimeLeft() > 0 && (bReturn = m_communication->Open((timeout.TimeLeft() / CEC_CONNECT_TRIES), false, bStartListening)) == false)
{
- if (!m_startCondition.Wait(&m_mutex) || !m_bStarted)
- {
- m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread");
+ m_libcec->AddLog(CEC_LOG_ERROR, "could not open a connection (try %d)", ++iConnectTry);
+ m_communication->Close();
+ CEvent::Sleep(CEC_DEFAULT_CONNECT_RETRY_WAIT);
+ }
+
+ m_libcec->AddLog(CEC_LOG_NOTICE, "connection opened");
+
+ // mark as initialised
+ SetCECInitialised(true);
+
+ return bReturn;
+}
+
+bool CCECProcessor::CECInitialised(void)
+{
+ CLockObject lock(m_threadMutex);
+ return m_bInitialised;
+}
+
+void CCECProcessor::SetCECInitialised(bool bSetTo /* = true */)
+{
+ {
+ CLockObject lock(m_mutex);
+ m_bInitialised = bSetTo;
+ }
+ if (!bSetTo)
+ UnregisterClients();
+}
+
+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;
- }
- return true;
+
+ // poll the LA if not
+ return device->TryLogicalAddress(libCECSpecVersion);
}
- else
- m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread");
return false;
}
-bool CCECProcessor::TryLogicalAddress(cec_logical_address address, unsigned int iIndex)
+void CCECProcessor::ReplaceHandlers(void)
{
- const char *strLabel = CCECCommandHandler::ToString(address);
- CStdString strLog;
- strLog.Format("trying logical address '%s'", strLabel);
- AddLog(CEC_LOG_DEBUG, strLog);
+ if (!CECInitialised())
+ return;
+
+ // check each device
+ for (CECDEVICEMAP::iterator it = m_busDevices->Begin(); it != m_busDevices->End(); it++)
+ it->second->ReplaceHandler(true);
+}
+
+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);
- SetAckMask(0x1 << address);
- if (!m_busDevices[address]->TransmitPoll(address))
+ // as long as we're not being stopped and the connection is open
+ while (!IsStopped() && m_communication->IsOpen())
{
- strLog.Format("using logical address '%s'", strLabel);
- AddLog(CEC_LOG_NOTICE, strLog);
+ // wait for a new incoming command, and process it
+ if (m_inBuffer.Pop(command, CEC_PROCESSOR_SIGNAL_WAIT_TIME))
+ ProcessCommand(command);
- /* only set our OSD name and active source for the primary device */
- if (m_logicalAddresses.empty())
+ if (CECInitialised() && !IsStopped())
{
- m_busDevices[address]->m_strDeviceName = m_strDeviceName;
- m_busDevices[address]->m_bActiveSource = true;
- }
- m_busDevices[address]->m_powerStatus = (m_types[0] == m_busDevices[address]->m_type) ? CEC_POWER_STATUS_ON : CEC_POWER_STATUS_STANDBY;
- m_busDevices[address]->m_cecVersion = CEC_VERSION_1_3A;
- m_logicalAddresses.set(address);
+ // check clients for keypress timeouts
+ m_libcec->CheckKeypressTimeout();
- // TODO
- m_busDevices[address]->SetPhysicalAddress(CEC_DEFAULT_PHYSICAL_ADDRESS + (iIndex * 0x100));
+ // check if we need to replace handlers
+ ReplaceHandlers();
- return true;
+ // 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);
+ }
+ }
}
- strLog.Format("logical address '%s' already taken", strLabel);
- AddLog(CEC_LOG_DEBUG, strLog);
- return false;
+ return NULL;
}
-bool CCECProcessor::FindLogicalAddressRecordingDevice(unsigned int iIndex)
+bool CCECProcessor::ActivateSource(uint16_t iStreamPath)
{
- AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'recording device'");
- return TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1, iIndex) ||
- TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2, iIndex) ||
- TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3, iIndex);
+ bool bReturn(false);
+
+ // 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);
+
+ return bReturn;
}
-bool CCECProcessor::FindLogicalAddressTuner(unsigned int iIndex)
+void CCECProcessor::SetStandardLineTimeout(uint8_t iTimeout)
{
- AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'tuner'");
- return TryLogicalAddress(CECDEVICE_TUNER1, iIndex) ||
- TryLogicalAddress(CECDEVICE_TUNER2, iIndex) ||
- TryLogicalAddress(CECDEVICE_TUNER3, iIndex) ||
- TryLogicalAddress(CECDEVICE_TUNER4, iIndex);
+ CLockObject lock(m_mutex);
+ m_iStandardLineTimeout = iTimeout;
}
-bool CCECProcessor::FindLogicalAddressPlaybackDevice(unsigned int iIndex)
+uint8_t CCECProcessor::GetStandardLineTimeout(void)
{
- AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'playback device'");
- return TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1, iIndex) ||
- TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2, iIndex) ||
- TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3, iIndex);
+ CLockObject lock(m_mutex);
+ return m_iStandardLineTimeout;
}
-bool CCECProcessor::FindLogicalAddressAudioSystem(unsigned int iIndex)
+void CCECProcessor::SetRetryLineTimeout(uint8_t iTimeout)
{
- AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'audio'");
- return TryLogicalAddress(CECDEVICE_AUDIOSYSTEM, iIndex);
+ CLockObject lock(m_mutex);
+ m_iRetryLineTimeout = iTimeout;
}
-bool CCECProcessor::FindLogicalAddresses(void)
+uint8_t CCECProcessor::GetRetryLineTimeout(void)
{
- bool bReturn(true);
- m_logicalAddresses.clear();
- CStdString strLog;
+ CLockObject lock(m_mutex);
+ return m_iRetryLineTimeout;
+}
- for (unsigned int iPtr = 0; iPtr < 5; iPtr++)
- {
- if (m_types.types[iPtr] == CEC_DEVICE_TYPE_RESERVED)
- continue;
-
- strLog.Format("%s - device %d: type %d", __FUNCTION__, iPtr, m_types.types[iPtr]);
- AddLog(CEC_LOG_DEBUG, strLog);
-
- if (m_types.types[iPtr] == CEC_DEVICE_TYPE_RECORDING_DEVICE)
- bReturn &= FindLogicalAddressRecordingDevice(iPtr);
- if (m_types.types[iPtr] == CEC_DEVICE_TYPE_TUNER)
- bReturn &= FindLogicalAddressTuner(iPtr);
- if (m_types.types[iPtr] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE)
- bReturn &= FindLogicalAddressPlaybackDevice(iPtr);
- if (m_types.types[iPtr] == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
- bReturn &= FindLogicalAddressAudioSystem(iPtr);
- }
+bool CCECProcessor::PhysicalAddressInUse(uint16_t iPhysicalAddress)
+{
+ CCECBusDevice *device = GetDeviceByPhysicalAddress(iPhysicalAddress);
+ return device != NULL;
+}
- return bReturn;
+void CCECProcessor::LogOutput(const cec_command &data)
+{
+ 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++)
+ strTx.AppendFormat(":%02x", data.parameters[iPtr]);
+
+ // and log it
+ m_libcec->AddLog(CEC_LOG_TRAFFIC, strTx.c_str());
}
-void *CCECProcessor::Process(void)
+bool CCECProcessor::PollDevice(cec_logical_address iAddress)
+{
+ // try to find the primary device
+ CCECBusDevice *primary = GetPrimaryDevice();
+ // poll the destination, with the primary as source
+ if (primary)
+ return primary->TransmitPoll(iAddress, false);
+
+ CCECBusDevice *device = m_busDevices->At(CECDEVICE_UNREGISTERED);
+ if (device)
+ return device->TransmitPoll(iAddress, false);
+
+ return false;
+}
+
+CCECBusDevice *CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress, bool bSuppressUpdate /* = true */)
+{
+ return m_busDevices ?
+ m_busDevices->GetDeviceByPhysicalAddress(iPhysicalAddress, bSuppressUpdate) :
+ NULL;
+}
+
+CCECBusDevice *CCECProcessor::GetDevice(cec_logical_address address) const
{
- bool bParseFrame(false);
- cec_command command;
- CCECAdapterMessage msg;
+ return m_busDevices ?
+ m_busDevices->At(address) :
+ NULL;
+}
+cec_logical_address CCECProcessor::GetActiveSource(bool bRequestActiveSource /* = true */)
+{
+ // get the device that is marked as active source from the device map
+ CCECBusDevice *activeSource = m_busDevices->GetActiveSource();
+ if (activeSource)
+ return activeSource->GetLogicalAddress();
+
+ if (bRequestActiveSource)
{
- if (m_logicalAddresses.empty() && !FindLogicalAddresses())
+ // request the active source from the bus
+ CCECBusDevice *primary = GetPrimaryDevice();
+ if (primary)
{
- CLockObject lock(&m_mutex);
- m_controller->AddLog(CEC_LOG_ERROR, "could not detect our logical addressed");
- m_startCondition.Signal();
- return NULL;
+ primary->RequestActiveSource();
+ return GetActiveSource(false);
}
+ }
- SetAckMask(m_logicalAddresses.ackmask());
+ // unknown or none
+ return CECDEVICE_UNKNOWN;
+}
+bool CCECProcessor::IsActiveSource(cec_logical_address iAddress)
+{
+ CCECBusDevice *device = m_busDevices->At(iAddress);
+ return device && device->IsActiveSource();
+}
+
+bool CCECProcessor::Transmit(const cec_command &data, bool bIsReply)
+{
+ cec_command transmitData(data);
+ uint8_t iMaxTries(0);
+ bool bRetry(true);
+ uint8_t iTries(0);
+
+ // get the current timeout setting
+ uint8_t iLineTimeout(GetStandardLineTimeout());
+
+ // 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))
{
- CLockObject lock(&m_mutex);
- m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started");
- m_bStarted = true;
- m_startCondition.Signal();
+ 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;
}
}
- while (!IsStopped())
+ LogOutput(transmitData);
+
+ // find the initiator device
+ CCECBusDevice *initiator = m_busDevices->At(transmitData.initiator);
+ if (!initiator)
{
- command.clear();
- msg.clear();
+ 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())
{
- CLockObject lock(&m_mutex);
- if (m_commandBuffer.Pop(command))
- {
- bParseFrame = true;
- }
- else if (m_communication->IsOpen() && m_communication->Read(msg, 50))
- {
- m_controller->AddLog(msg.is_error() ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString());
- if ((bParseFrame = (ParseMessage(msg) && !IsStopped())))
- command = m_currentframe;
- }
+ // 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;
}
+ }
- if (bParseFrame)
- ParseCommand(command);
- bParseFrame = false;
-
- Sleep(5);
+ // wait until we finished allocating a new LA if it got lost
+ while (m_bStallCommunication) Sleep(5);
- m_controller->CheckKeypressTimeout();
+ {
+ CLockObject lock(m_mutex);
+ m_iLastTransmission = GetTimeMs();
+ // set the number of tries
+ iMaxTries = initiator->GetHandler()->GetTransmitRetries() + 1;
+ initiator->MarkHandlerReady();
+ }
- for (unsigned int iDevicePtr = 0; iDevicePtr < 16; iDevicePtr++)
- m_busDevices[iDevicePtr]->PollVendorId();
+ // and try to send the command
+ while (bRetry && ++iTries < iMaxTries)
+ {
+ if (initiator->IsUnsupportedFeature(transmitData.opcode))
+ return false;
- Sleep(5);
+ adapterState = !IsStopped() && m_communication && m_communication->IsOpen() ?
+ m_communication->Write(transmitData, bRetry, iLineTimeout, bIsReply) :
+ ADAPTER_MESSAGE_STATE_ERROR;
+ iLineTimeout = m_iRetryLineTimeout;
}
- return NULL;
+ return bIsReply ?
+ adapterState == ADAPTER_MESSAGE_STATE_SENT_ACKED || adapterState == ADAPTER_MESSAGE_STATE_SENT || adapterState == ADAPTER_MESSAGE_STATE_WAITING_TO_BE_SENT :
+ adapterState == ADAPTER_MESSAGE_STATE_SENT_ACKED;
}
-bool CCECProcessor::SetActiveSource(cec_device_type type /* = CEC_DEVICE_TYPE_RESERVED */)
+void CCECProcessor::TransmitAbort(cec_logical_address source, cec_logical_address destination, cec_opcode opcode, cec_abort_reason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
{
- bool bReturn(false);
+ m_libcec->AddLog(CEC_LOG_DEBUG, "<< transmitting abort message");
- if (!IsRunning())
- return bReturn;
+ cec_command command;
+ cec_command::Format(command, source, destination, CEC_OPCODE_FEATURE_ABORT);
+ command.parameters.PushBack((uint8_t)opcode);
+ command.parameters.PushBack((uint8_t)reason);
- cec_logical_address addr = m_logicalAddresses.primary;
+ Transmit(command, true);
+}
- if (type != CEC_DEVICE_TYPE_RESERVED)
- {
- for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
- {
- if (m_logicalAddresses[iPtr] && m_busDevices[iPtr]->m_type == type)
- {
- addr = (cec_logical_address) iPtr;
- break;
- }
- }
- }
+void CCECProcessor::ProcessCommand(const cec_command &command)
+{
+ // 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());
+
+ // find the initiator
+ CCECBusDevice *device = m_busDevices->At(command.initiator);
- return SetStreamPath(m_busDevices[addr]->GetPhysicalAddress());
+ if (device)
+ device->HandleCommand(command);
}
-bool CCECProcessor::SetActiveView(void)
+bool CCECProcessor::IsPresentDevice(cec_logical_address address)
{
- return SetActiveSource();
+ CCECBusDevice *device = m_busDevices->At(address);
+ return device && device->GetStatus() == CEC_DEVICE_STATUS_PRESENT;
}
-bool CCECProcessor::SetStreamPath(uint16_t iStreamPath)
+bool CCECProcessor::IsPresentDeviceType(cec_device_type type)
{
- bool bReturn(false);
+ CECDEVICEVEC devices;
+ m_busDevices->GetByType(type, devices);
+ CCECDeviceMap::FilterActive(devices);
+ return !devices.empty();
+}
- CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamPath);
- if (device)
- {
- for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
- m_busDevices[iPtr]->m_bActiveSource = false;
+uint16_t CCECProcessor::GetDetectedPhysicalAddress(void) const
+{
+ return m_communication ? m_communication->GetPhysicalAddress() : CEC_INVALID_PHYSICAL_ADDRESS;
+}
- device->m_bActiveSource = true;
+bool CCECProcessor::ClearLogicalAddresses(void)
+{
+ cec_logical_addresses addresses; addresses.Clear();
+ return SetLogicalAddresses(addresses);
+}
- if (m_logicalAddresses.isset(device->m_iLogicalAddress))
- bReturn = device->TransmitActiveSource();
- else
- bReturn = true;
- }
+bool CCECProcessor::SetLogicalAddresses(const cec_logical_addresses &addresses)
+{
+ return m_communication ? m_communication->SetLogicalAddresses(addresses) : false;
+}
+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;
}
-bool CCECProcessor::SetInactiveView(void)
+bool CCECProcessor::StandbyDevice(const cec_logical_address initiator, cec_logical_address address)
{
- if (!IsRunning())
- return false;
-
- if (!m_logicalAddresses.empty() && m_busDevices[m_logicalAddresses.primary])
- return m_busDevices[m_logicalAddresses.primary]->TransmitInactiveView();
- return false;
+ CCECBusDevice *device = m_busDevices->At(address);
+ return device ? device->Standby(initiator) : false;
}
-void CCECProcessor::LogOutput(const cec_command &data)
+bool CCECProcessor::PowerOnDevices(const cec_logical_address initiator, const CECDEVICEVEC &devices)
{
- CStdString strTx;
- strTx.Format("<< %02x", ((uint8_t)data.initiator << 4) + (uint8_t)data.destination);
- if (data.opcode_set)
- strTx.AppendFormat(":%02x", (uint8_t)data.opcode);
+ bool bReturn(true);
+ for (CECDEVICEVEC::const_iterator it = devices.begin(); it != devices.end(); it++)
+ bReturn &= (*it)->PowerOn(initiator);
+ return bReturn;
+}
- for (uint8_t iPtr = 0; iPtr < data.parameters.size; iPtr++)
- strTx.AppendFormat(":%02x", data.parameters[iPtr]);
- m_controller->AddLog(CEC_LOG_TRAFFIC, strTx.c_str());
+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::SetLogicalAddress(cec_logical_address iLogicalAddress)
+bool CCECProcessor::StartBootloader(const char *strPort /* = NULL */)
{
- if (m_logicalAddresses.primary != iLogicalAddress)
+ 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
{
- CStdString strLog;
- strLog.Format("<< setting primary logical address to %1x", iLogicalAddress);
- m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
- m_logicalAddresses.primary = iLogicalAddress;
- m_logicalAddresses.set(iLogicalAddress);
- return SetAckMask(m_logicalAddresses.ackmask());
+ m_communication->StartBootloader();
+ Close();
+ bReturn = true;
}
- return true;
+ return bReturn;
}
-bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress)
+bool CCECProcessor::PingAdapter(void)
{
- if (!m_logicalAddresses.empty() && m_busDevices[m_logicalAddresses.primary])
- {
- m_busDevices[m_logicalAddresses.primary]->SetPhysicalAddress(iPhysicalAddress);
- return SetActiveView();
- }
- return false;
+ return m_communication->PingAdapter();
}
-bool CCECProcessor::SwitchMonitoring(bool bEnable)
+void CCECProcessor::HandlePoll(cec_logical_address initiator, cec_logical_address destination)
{
- 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 SetAckMask(0);
- else
- return SetAckMask(m_logicalAddresses.ackmask());
+ CCECBusDevice *device = m_busDevices->At(destination);
+ if (device)
+ device->HandlePollFrom(initiator);
}
-bool CCECProcessor::PollDevice(cec_logical_address iAddress)
+bool CCECProcessor::HandleReceiveFailed(cec_logical_address initiator)
{
- if (iAddress != CECDEVICE_UNKNOWN && m_busDevices[iAddress])
- return m_busDevices[m_logicalAddresses.primary]->TransmitPoll(iAddress);
- return false;
+ CCECBusDevice *device = m_busDevices->At(initiator);
+ return !device || !device->HandleReceiveFailed();
}
-
-CCECBusDevice *CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress) const
+bool CCECProcessor::CanPersistConfiguration(void)
{
- CCECBusDevice *device = NULL;
+ return m_communication ? m_communication->GetFirmwareVersion() >= 2 : false;
+}
- for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
+bool CCECProcessor::PersistConfiguration(const libcec_configuration &configuration)
+{
+ libcec_configuration persistConfiguration = configuration;
+ if (!CLibCEC::IsValidPhysicalAddress(configuration.iPhysicalAddress))
{
- if (m_busDevices[iPtr]->GetPhysicalAddress() == iPhysicalAddress)
- {
- device = m_busDevices[iPtr];
- break;
- }
+ CCECBusDevice *device = GetPrimaryDevice();
+ if (device)
+ persistConfiguration.iPhysicalAddress = device->GetCurrentPhysicalAddress();
}
- return device;
+ return m_communication ? m_communication->PersistConfiguration(persistConfiguration) : false;
}
-cec_version CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress)
+void CCECProcessor::RescanActiveDevices(void)
{
- return m_busDevices[iAddress]->GetCecVersion();
+ for (CECDEVICEMAP::iterator it = m_busDevices->Begin(); it != m_busDevices->End(); it++)
+ it->second->GetStatus(true);
}
-bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress, cec_menu_language *language)
+bool CCECProcessor::GetDeviceInformation(const char *strPort, libcec_configuration *config, uint32_t iTimeoutMs /* = CEC_DEFAULT_CONNECT_TIMEOUT */)
{
- if (m_busDevices[iAddress])
- {
- *language = m_busDevices[iAddress]->GetMenuLanguage();
- return (strcmp(language->language, "???") != 0);
- }
- return false;
+ if (!OpenConnection(strPort, CEC_SERIAL_DEFAULT_BAUDRATE, iTimeoutMs, false))
+ return false;
+
+ config->iFirmwareVersion = m_communication->GetFirmwareVersion();
+ config->iPhysicalAddress = m_communication->GetPhysicalAddress();
+ config->iFirmwareBuildDate = m_communication->GetFirmwareBuildDate();
+ config->adapterType = m_communication->GetAdapterType();
+
+ return true;
}
-uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress)
+bool CCECProcessor::TransmitPendingActiveSourceCommands(void)
{
- if (m_busDevices[iAddress])
- return m_busDevices[iAddress]->GetVendorId();
- return false;
+ bool bReturn(true);
+ for (CECDEVICEMAP::iterator it = m_busDevices->Begin(); it != m_busDevices->End(); it++)
+ bReturn &= it->second->TransmitPendingActiveSourceCommands();
+ return bReturn;
}
-cec_power_status CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress)
+CCECTV *CCECProcessor::GetTV(void) const
{
- if (m_busDevices[iAddress])
- return m_busDevices[iAddress]->GetPowerStatus();
- return CEC_POWER_STATUS_UNKNOWN;
+ return CCECBusDevice::AsTV(m_busDevices->At(CECDEVICE_TV));
}
-bool CCECProcessor::Transmit(const cec_command &data)
+CCECAudioSystem *CCECProcessor::GetAudioSystem(void) const
{
- bool bReturn(false);
- LogOutput(data);
+ return CCECBusDevice::AsAudioSystem(m_busDevices->At(CECDEVICE_AUDIOSYSTEM));
+}
- CCECAdapterMessage *output = new CCECAdapterMessage(data);
- bReturn = Transmit(output);
- delete output;
+CCECPlaybackDevice *CCECProcessor::GetPlaybackDevice(cec_logical_address address) const
+{
+ return CCECBusDevice::AsPlaybackDevice(m_busDevices->At(address));
+}
- return bReturn;
+CCECRecordingDevice *CCECProcessor::GetRecordingDevice(cec_logical_address address) const
+{
+ return CCECBusDevice::AsRecordingDevice(m_busDevices->At(address));
}
-bool CCECProcessor::Transmit(CCECAdapterMessage *output)
+CCECTuner *CCECProcessor::GetTuner(cec_logical_address address) const
{
- bool bReturn(false);
- CLockObject lock(&m_mutex);
+ 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++)
{
- CLockObject msgLock(&output->mutex);
- if (!m_communication || !m_communication->Write(output))
- return bReturn;
- else
- {
- output->condition.Wait(&output->mutex);
- if (output->state != ADAPTER_MESSAGE_STATE_SENT)
- {
- m_controller->AddLog(CEC_LOG_ERROR, "command was not sent");
- return bReturn;
- }
- }
+ // remove client entry
+ CLockObject lock(m_mutex);
+ m_clients.erase((*it)->GetLogicalAddress());
+ }
- if (output->transmit_timeout > 0)
- {
- if ((bReturn = WaitForTransmitSucceeded(output->size(), output->transmit_timeout)) == false)
- m_controller->AddLog(CEC_LOG_DEBUG, "did not receive ack");
- }
- else
- bReturn = true;
+ // 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;
}
- return bReturn;
-}
+ // 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<cec_logical_address, CCECClient *>((*it)->GetLogicalAddress(), client));
+ }
-void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode, ECecAbortReason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
-{
- m_controller->AddLog(CEC_LOG_DEBUG, "<< transmitting abort message");
+ // set the new ackmask
+ SetLogicalAddresses(GetLogicalAddresses());
- cec_command command;
- // TODO
- cec_command::format(command, m_logicalAddresses.primary, address, CEC_OPCODE_FEATURE_ABORT);
- command.parameters.push_back((uint8_t)opcode);
- command.parameters.push_back((uint8_t)reason);
+ // resume outgoing communication
+ m_bStallCommunication = false;
- Transmit(command);
+ return true;
}
-bool CCECProcessor::WaitForTransmitSucceeded(uint8_t iLength, uint32_t iTimeout /* = 1000 */)
+bool CCECProcessor::RegisterClient(CCECClient *client)
{
- bool bError(false);
- bool bTransmitSucceeded(false);
- uint8_t iPacketsLeft(iLength / 4);
+ if (!client)
+ return false;
- int64_t iNow = GetTimeMs();
- int64_t iTargetTime = iNow + (uint64_t) iTimeout;
+ libcec_configuration &configuration = *client->GetConfiguration();
- while (!bTransmitSucceeded && !bError && (iTimeout == 0 || iNow < iTargetTime))
+ if (configuration.clientVersion >= CEC_CLIENT_VERSION_1_6_3 && configuration.bMonitorOnly == 1)
+ return true;
+
+ if (!CECInitialised())
{
- CCECAdapterMessage msg;
+ m_libcec->AddLog(CEC_LOG_ERROR, "failed to register a new CEC client: CEC processor is not initialised");
+ return false;
+ }
- if (!m_communication->Read(msg, iTimeout > 0 ? (int32_t)(iTargetTime - iNow) : 1000))
- {
- iNow = GetTimeMs();
- continue;
- }
+ // unregister the client first if it's already been marked as registered
+ if (client->IsRegistered())
+ UnregisterClient(client);
- if ((bError = msg.is_error()) == false)
- {
- m_controller->AddLog(bError ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString());
+ // ensure that controlled mode is enabled
+ m_communication->SetControlledMode(true);
- switch(msg.message())
- {
- case MSGCODE_COMMAND_ACCEPTED:
- if (iPacketsLeft > 0)
- iPacketsLeft--;
- break;
- case MSGCODE_TRANSMIT_SUCCEEDED:
- bTransmitSucceeded = (iPacketsLeft == 0);
- bError = !bTransmitSucceeded;
- break;
- default:
- if (ParseMessage(msg))
- m_commandBuffer.Push(m_currentframe);
- }
+ // 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);
- iNow = GetTimeMs();
- }
+ // 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);
}
- return bTransmitSucceeded && !bError;
+ // 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::ParseMessage(const CCECAdapterMessage &msg)
+bool CCECProcessor::UnregisterClient(CCECClient *client)
{
- bool bEom = false;
+ if (!client)
+ return false;
+
+ if (client->IsRegistered())
+ m_libcec->AddLog(CEC_LOG_NOTICE, "unregistering client: %s", client->GetConnectionInfo().c_str());
- if (msg.empty())
- return bEom;
+ // notify the client that it will be unregistered
+ client->OnUnregister();
- switch(msg.message())
{
- case MSGCODE_FRAME_START:
+ 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++)
{
- m_currentframe.clear();
- if (msg.size() >= 2)
- {
- m_currentframe.initiator = msg.initiator();
- m_currentframe.destination = msg.destination();
- m_currentframe.ack = msg.ack();
- m_currentframe.eom = msg.eom();
- }
- }
- break;
- case MSGCODE_FRAME_DATA:
- {
- if (msg.size() >= 2)
- {
- m_currentframe.push_back(msg[1]);
- m_currentframe.eom = msg.eom();
- }
- bEom = msg.eom();
+ // 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();
}
- break;
- default:
- break;
}
- return bEom;
+ // set the new ackmask
+ cec_logical_addresses addresses = GetLogicalAddresses();
+ if (SetLogicalAddresses(addresses))
+ {
+ // no more clients left, disable controlled mode
+ if (addresses.IsEmpty() && !m_bMonitor)
+ m_communication->SetControlledMode(false);
+
+ return true;
+ }
+
+ return false;
}
-void CCECProcessor::ParseCommand(cec_command &command)
+void CCECProcessor::UnregisterClients(void)
{
- CStdString dataStr;
- dataStr.Format(">> %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_TRAFFIC, dataStr.c_str());
+ m_libcec->AddLog(CEC_LOG_DEBUG, "unregistering all CEC clients");
+
+ vector<CCECClient *> clients = m_libcec->GetClients();
+ for (vector<CCECClient *>::iterator client = clients.begin(); client != clients.end(); client++)
+ UnregisterClient(*client);
- if (!m_bMonitor)
- m_busDevices[(uint8_t)command.initiator]->HandleCommand(command);
+ CLockObject lock(m_mutex);
+ m_clients.clear();
}
-uint16_t CCECProcessor::GetPhysicalAddress(void) const
+CCECClient *CCECProcessor::GetClient(const cec_logical_address address)
{
- if (!m_logicalAddresses.empty() && m_busDevices[m_logicalAddresses.primary])
- return m_busDevices[m_logicalAddresses.primary]->GetPhysicalAddress();
- return false;
+ 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;
}
-void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode)
+CCECClient *CCECProcessor::GetPrimaryClient(void)
{
- m_controller->SetCurrentButton(iButtonCode);
+ 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;
}
-void CCECProcessor::AddCommand(const cec_command &command)
+CCECBusDevice *CCECProcessor::GetPrimaryDevice(void)
{
- m_controller->AddCommand(command);
+ return m_busDevices->At(GetLogicalAddress());
}
-void CCECProcessor::AddKey(cec_keypress &key)
+cec_logical_address CCECProcessor::GetLogicalAddress(void)
{
- m_controller->AddKey(key);
+ cec_logical_addresses addresses = GetLogicalAddresses();
+ return addresses.primary;
}
-void CCECProcessor::AddKey(void)
+cec_logical_addresses CCECProcessor::GetLogicalAddresses(void)
{
- m_controller->AddKey();
+ 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;
}
-void CCECProcessor::AddLog(cec_log_level level, const CStdString &strMessage)
+bool CCECProcessor::IsHandledByLibCEC(const cec_logical_address address) const
{
- m_controller->AddLog(level, strMessage);
+ CCECBusDevice *device = GetDevice(address);
+ return device && device->IsHandledByLibCEC();
}
-bool CCECProcessor::SetAckMask(uint16_t iMask)
+bool CCECProcessor::IsRunningLatestFirmware(void)
{
- bool bReturn(false);
- CStdString strLog;
- strLog.Format("setting ackmask to %2x", iMask);
- m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
+ return m_communication && m_communication->IsOpen() ?
+ m_communication->IsRunningLatestFirmware() :
+ true;
+}
+
+void CCECProcessor::SwitchMonitoring(bool bSwitchTo)
+{
+ {
+ CLockObject lock(m_mutex);
+ m_bMonitor = bSwitchTo;
+ }
+ if (bSwitchTo)
+ UnregisterClients();
+}
+
+void CCECProcessor::HandleLogicalAddressLost(cec_logical_address oldAddress)
+{
+ // 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)
+ client = GetPrimaryClient();
+ if (client)
+ {
+ if (m_addrAllocator)
+ while (m_addrAllocator->IsRunning()) Sleep(5);
+ delete m_addrAllocator;
- CCECAdapterMessage *output = new CCECAdapterMessage;
+ m_addrAllocator = new CCECAllocateLogicalAddress(this, client);
+ m_addrAllocator->CreateThread();
+ }
+}
- output->push_back(MSGSTART);
- output->push_escaped(MSGCODE_SET_ACK_MASK);
- output->push_escaped(iMask >> 8);
- output->push_escaped((uint8_t)iMask);
- output->push_back(MSGEND);
+uint16_t CCECProcessor::GetAdapterVendorId(void) const
+{
+ return m_communication ? m_communication->GetAdapterVendorId() : 0;
+}
- if ((bReturn = Transmit(output)) == false)
- m_controller->AddLog(CEC_LOG_ERROR, "could not set the ackmask");
+uint16_t CCECProcessor::GetAdapterProductId(void) const
+{
+ return m_communication ? m_communication->GetAdapterProductId() : 0;
+}
- delete output;
+CCECAllocateLogicalAddress::CCECAllocateLogicalAddress(CCECProcessor* processor, CCECClient* client) :
+ m_processor(processor),
+ m_client(client) { }
- return bReturn;
+void* CCECAllocateLogicalAddress::Process(void)
+{
+ m_processor->AllocateLogicalAddresses(m_client);
+ return NULL;
}