cec: safe deletes for members
[deb_libcec.git] / src / lib / CECProcessor.cpp
index e90d5cb59038a62f8917bbb8cab61f52e24bc43e..016d88c8e517933129ad120fe19c8661a28c83f7 100644 (file)
@@ -43,6 +43,7 @@
 #include "LibCEC.h"
 #include "CECClient.h"
 #include "platform/util/timeutils.h"
+#include "platform/util/util.h"
 
 using namespace CEC;
 using namespace std;
@@ -66,7 +67,7 @@ CCECProcessor::CCECProcessor(CLibCEC *libcec) :
 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 */)
@@ -98,21 +99,13 @@ void CCECProcessor::Close(void)
   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_iStandardLineTimeout = 3;
@@ -153,6 +146,9 @@ bool CCECProcessor::OpenConnection(const char *strPort, uint16_t iBaudRate, uint
 
   m_libcec->AddLog(CEC_LOG_NOTICE, "connection opened");
 
+  // always start by setting the ackmask to 0, to clear previous values
+  SetAckMask(0);
+
   // mark as initialised
   SetCECInitialised(true);
 
@@ -514,7 +510,7 @@ bool CCECProcessor::StartBootloader(const char *strPort /* = NULL */)
     if (comm->IsOpen())
     {
       bReturn = comm->StartBootloader();
-      delete comm;
+      DELETE_AND_NULL(comm);
     }
     return bReturn;
   }
@@ -615,7 +611,15 @@ CCECTuner *CCECProcessor::GetTuner(cec_logical_address address) const
 
 bool CCECProcessor::RegisterClient(CCECClient *client)
 {
-  if (!client || !CECInitialised())
+  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;
@@ -626,7 +630,6 @@ bool CCECProcessor::RegisterClient(CCECClient *client)
     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