#include "implementations/CECCommandHandler.h"
#include "LibCEC.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;
#define CEC_PROCESSOR_SIGNAL_WAIT_TIME 1000
+#define ACTIVE_SOURCE_CHECK_INTERVAL 500
-#define ToString(x) m_libcec->ToString(x)
+#define ToString(x) CCECTypeUtils::ToString(x)
CCECProcessor::CCECProcessor(CLibCEC *libcec) :
m_bInitialised(false),
m_communication(NULL),
m_libcec(libcec),
- m_bMonitor(false),
- m_iPreviousAckMask(0),
m_iStandardLineTimeout(3),
m_iRetryLineTimeout(3),
m_iLastTransmission(0)
CCECProcessor::~CCECProcessor(void)
{
Close();
- delete m_busDevices;
+ 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 */)
}
}
- // mark as initialised
- SetCECInitialised(true);
-
return true;
}
StopThread();
// close the connection
- if (m_communication)
- {
- delete m_communication;
- m_communication = NULL;
- }
+ DELETE_AND_NULL(m_communication);
}
void CCECProcessor::ResetMembers(void)
{
// close the connection
- if (m_communication)
- {
- delete m_communication;
- m_communication = NULL;
- }
+ DELETE_AND_NULL(m_communication);
// reset the other members to the initial state
- m_bMonitor = false;
- m_iPreviousAckMask = 0;
m_iStandardLineTimeout = 3;
m_iRetryLineTimeout = 3;
m_iLastTransmission = 0;
m_libcec->AddLog(CEC_LOG_NOTICE, "connection opened");
+ // mark as initialised
+ SetCECInitialised(true);
+
return bReturn;
}
{
m_libcec->AddLog(CEC_LOG_DEBUG, "processor thread started");
- cec_command command;
+ 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())
// 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);
+ }
}
}
m_libcec->AddLog(CEC_LOG_TRAFFIC, strTx.c_str());
}
-bool CCECProcessor::SwitchMonitoring(bool bEnable)
-{
- m_libcec->AddLog(CEC_LOG_NOTICE, "== %s monitoring mode ==", bEnable ? "enabling" : "disabling");
-
- {
- CLockObject lock(m_mutex);
- // switch to monitoring mode, which will stop processing of incoming messages
- m_bMonitor = bEnable;
- // and store the current ackmask
- m_iPreviousAckMask = m_communication->GetAckMask();
- }
-
- // set the mask to 0 when enabling monitor mode
- if (bEnable)
- return SetAckMask(0);
- // and restore the previous mask otherwise
- else
- return SetAckMask(m_iPreviousAckMask);
-}
-
bool CCECProcessor::PollDevice(cec_logical_address iAddress)
{
// try to find the primary device
m_iLastTransmission = GetTimeMs();
// set the number of tries
iMaxTries = initiator->GetHandler()->GetTransmitRetries() + 1;
+ initiator->MarkHandlerReady();
}
// and try to send the command
dataStr.AppendFormat(":%02x", (unsigned int)command.parameters[iPtr]);
m_libcec->AddLog(CEC_LOG_TRAFFIC, dataStr.c_str());
- // if we're not in monitor mode
- if (!m_bMonitor)
- {
- // find the initiator
- CCECBusDevice *device = m_busDevices->At(command.initiator);
- // and "handle" the command
- if (device)
- device->HandleCommand(command);
- }
+ // find the initiator
+ CCECBusDevice *device = m_busDevices->At(command.initiator);
+
+ if (device)
+ device->HandleCommand(command);
}
bool CCECProcessor::IsPresentDevice(cec_logical_address address)
if (comm->IsOpen())
{
bReturn = comm->StartBootloader();
- delete comm;
+ DELETE_AND_NULL(comm);
}
return bReturn;
}
bool CCECProcessor::SetStreamPath(uint16_t iPhysicalAddress)
{
// stream path changes are sent by the TV
- return GetTV()->GetHandler()->TransmitSetStreamPath(iPhysicalAddress);
+ bool bReturn = GetTV()->GetHandler()->TransmitSetStreamPath(iPhysicalAddress);
+ GetTV()->MarkHandlerReady();
+ return bReturn;
}
bool CCECProcessor::CanPersistConfiguration(void)
bool CCECProcessor::RegisterClient(CCECClient *client)
{
- if (!client || !IsRunning())
+ if (!client)
+ return false;
+
+ libcec_configuration &configuration = *client->GetConfiguration();
+
+ if (configuration.clientVersion >= CEC_CLIENT_VERSION_1_6_3 && configuration.bMonitorOnly == 1)
+ return true;
+
+ if (!CECInitialised())
+ {
+ m_libcec->AddLog(CEC_LOG_ERROR, "failed to register a new CEC client: CEC processor is not initialised");
return false;
+ }
+
+ // ensure that we know the vendor id of the TV
+ CCECBusDevice *tv = GetTV();
+ tv->GetVendorId(CECDEVICE_UNREGISTERED);
+ tv->ReplaceHandler(false);
// unregister the client first if it's already been marked as registered
if (client->IsRegistered())
UnregisterClient(client);
// get the configuration from the client
- libcec_configuration &configuration = *client->GetConfiguration();
m_libcec->AddLog(CEC_LOG_NOTICE, "registering new CEC client - v%s", ToString((cec_client_version)configuration.clientVersion));
// mark as uninitialised and unregistered
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());
// get the settings from the rom
if (configuration.bGetSettingsFromROM == 1)
{
- libcec_configuration config;
+ libcec_configuration config; config.Clear();
m_communication->GetConfiguration(config);
CLockObject lock(m_mutex);
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;
}
-void CCECProcessor::UnregisterClient(CCECClient *client)
+bool CCECProcessor::UnregisterClient(CCECClient *client)
{
if (!client)
- return;
+ return false;
if (client->IsRegistered())
m_libcec->AddLog(CEC_LOG_NOTICE, "unregistering client: %s", client->GetConnectionInfo().c_str());
}
// set the new ackmask
- SetAckMask(GetLogicalAddresses().AckMask());
+ return SetAckMask(GetLogicalAddresses().AckMask());
}
void CCECProcessor::UnregisterClients(void)