cec: set the ackmask to 0 when starting, so the earlier mask is cleared when starting...
[deb_libcec.git] / src / lib / CECProcessor.cpp
index 0714d2a659b7f31c025d3ff15e6730465fdb10fb..3bfa061548c0e4b74d4811e7296790a6849b2630 100644 (file)
@@ -56,8 +56,6 @@ 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)
@@ -81,18 +79,13 @@ bool CCECProcessor::Start(const char *strPort, uint16_t iBaudRate /* = CEC_SERIA
   // create the processor thread
   if (!IsRunning())
   {
-    if (CreateThread())
-      m_libcec->AddLog(CEC_LOG_DEBUG, "processor thread started");
-    else
+    if (!CreateThread())
     {
       m_libcec->AddLog(CEC_LOG_ERROR, "could not create a processor thread");
       return false;
     }
   }
 
-  // mark as initialised
-  SetCECInitialised(true);
-
   return true;
 }
 
@@ -122,8 +115,6 @@ void CCECProcessor::ResetMembers(void)
   }
 
   // reset the other members to the initial state
-  m_bMonitor = false;
-  m_iPreviousAckMask = 0;
   m_iStandardLineTimeout = 3;
   m_iRetryLineTimeout = 3;
   m_iLastTransmission = 0;
@@ -162,6 +153,12 @@ 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);
+
   return bReturn;
 }
 
@@ -304,26 +301,6 @@ void CCECProcessor::LogOutput(const cec_command &data)
   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
@@ -463,15 +440,11 @@ void CCECProcessor::ProcessCommand(const cec_command &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)
@@ -648,12 +621,22 @@ bool CCECProcessor::RegisterClient(CCECClient *client)
   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;
+  }
+
   // 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
@@ -727,12 +710,13 @@ bool CCECProcessor::RegisterClient(CCECClient *client)
   return bReturn;
 }
 
-void CCECProcessor::UnregisterClient(CCECClient *client)
+bool CCECProcessor::UnregisterClient(CCECClient *client)
 {
   if (!client)
-    return;
+    return false;
 
-  m_libcec->AddLog(CEC_LOG_NOTICE, "unregistering client: %s", client->GetConnectionInfo().c_str());
+  if (client->IsRegistered())
+    m_libcec->AddLog(CEC_LOG_NOTICE, "unregistering client: %s", client->GetConnectionInfo().c_str());
 
   // notify the client that it will be unregistered
   client->OnUnregister();
@@ -756,7 +740,7 @@ void CCECProcessor::UnregisterClient(CCECClient *client)
   }
 
   // set the new ackmask
-  SetAckMask(GetLogicalAddresses().AckMask());
+  return SetAckMask(GetLogicalAddresses().AckMask());
 }
 
 void CCECProcessor::UnregisterClients(void)