cec: added PollDevice()/cec_poll_device()
[deb_libcec.git] / src / lib / CECProcessor.cpp
index 09e1ce4e0231de8c49b1c61c6d54e666dabd773a..49b3bd2e35a11e5727e0f4c4f850b878999cc440 100644 (file)
@@ -88,17 +88,17 @@ bool CCECProcessor::Start(void)
 
 void *CCECProcessor::Process(void)
 {
+  cec_command           command;
+  CCECAdapterMessage    msg;
+
+  SetAckMask(0x1 << (uint8_t)m_iLogicalAddress);
+
   {
     CLockObject lock(&m_mutex);
     m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started");
     m_startCondition.Signal();
   }
 
-  cec_command           command;
-  CCECAdapterMessage    msg;
-
-  SetAckMask(0x1 << (uint8_t)m_iLogicalAddress);
-
   while (!IsStopped())
   {
     bool bParseFrame(false);
@@ -156,7 +156,9 @@ bool CCECProcessor::SetInactiveView(void)
 void CCECProcessor::LogOutput(const cec_command &data)
 {
   CStdString strTx;
-  strTx.Format("<< %02x:%02x", ((uint8_t)data.initiator << 4) + (uint8_t)data.destination, (uint8_t)data.opcode);
+  strTx.Format("<< %02x", ((uint8_t)data.initiator << 4) + (uint8_t)data.destination);
+  if (data.opcode_set)
+      strTx.AppendFormat(":%02x", (uint8_t)data.opcode);
 
   for (uint8_t iPtr = 0; iPtr < data.parameters.size; iPtr++)
     strTx.AppendFormat(":%02x", data.parameters[iPtr]);
@@ -200,6 +202,13 @@ bool CCECProcessor::SwitchMonitoring(bool bEnable)
     return SetAckMask(0x1 << (uint8_t)m_iLogicalAddress);
 }
 
+bool CCECProcessor::PollDevice(cec_logical_address iAddress)
+{
+  if (iAddress != CECDEVICE_UNKNOWN && m_busDevices[iAddress])
+    return m_busDevices[iAddress]->PollDevice();
+  return false;
+}
+
 cec_version CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress)
 {
   return m_busDevices[iAddress]->GetCecVersion();
@@ -231,13 +240,17 @@ cec_power_status CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddres
 
 bool CCECProcessor::Transmit(const cec_command &data)
 {
+  bool bReturn(false);
   LogOutput(data);
 
-  CCECAdapterMessagePtr output(new CCECAdapterMessage(data));
-  return Transmit(output);
+  CCECAdapterMessage *output = new CCECAdapterMessage(data);
+  bReturn = Transmit(output);
+  delete output;
+
+  return bReturn;
 }
 
-bool CCECProcessor::Transmit(CCECAdapterMessagePtr output)
+bool CCECProcessor::Transmit(CCECAdapterMessage *output)
 {
   bool bReturn(false);
   CLockObject lock(&m_mutex);
@@ -247,7 +260,7 @@ bool CCECProcessor::Transmit(CCECAdapterMessagePtr output)
       return bReturn;
     else
     {
-      output->condition.Wait(&output->mutex, 1000);
+      output->condition.Wait(&output->mutex);
       if (output->state != ADAPTER_MESSAGE_STATE_SENT)
       {
         m_controller->AddLog(CEC_LOG_ERROR, "command was not sent");
@@ -298,29 +311,26 @@ bool CCECProcessor::WaitForTransmitSucceeded(uint8_t iLength, uint32_t iTimeout
       continue;
     }
 
-    bError = msg.is_error();
-    m_controller->AddLog(msg.is_error() ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString());
-
-    switch(msg.message())
+    if ((bError = msg.is_error()) == false)
     {
-    case MSGCODE_COMMAND_ACCEPTED:
-      if (iPacketsLeft > 0)
-        iPacketsLeft--;
-      break;
-    case MSGCODE_TRANSMIT_SUCCEEDED:
-      bTransmitSucceeded = (iPacketsLeft == 0);
-      bError = !bTransmitSucceeded;
-      break;
-    default:
-      CStdString strLog;
-      strLog.Format("received unexpected reply '%s' instead of ack", msg.MessageCodeAsString().c_str());
-      m_controller->AddLog(CEC_LOG_WARNING, strLog);
-      ParseMessage(msg);
-      bError = true;
-      break;
-    }
+      m_controller->AddLog(bError ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString());
+
+      switch(msg.message())
+      {
+      case MSGCODE_COMMAND_ACCEPTED:
+        if (iPacketsLeft > 0)
+          iPacketsLeft--;
+        break;
+      case MSGCODE_TRANSMIT_SUCCEEDED:
+        bTransmitSucceeded = (iPacketsLeft == 0);
+        bError = !bTransmitSucceeded;
+        break;
+      default:
+        ParseMessage(msg);
+      }
 
-    iNow = GetTimeMs();
+      iNow = GetTimeMs();
+    }
   }
 
   return bTransmitSucceeded && !bError;
@@ -390,7 +400,12 @@ void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode)
 
 void CCECProcessor::AddCommand(const cec_command &command)
 {
-//  m_controller->AddCommand(command);
+  m_controller->AddCommand(command);
+}
+
+void CCECProcessor::AddKey(cec_keypress &key)
+{
+  m_controller->AddKey(key);
 }
 
 void CCECProcessor::AddKey(void)
@@ -405,11 +420,12 @@ void CCECProcessor::AddLog(cec_log_level level, const CStdString &strMessage)
 
 bool CCECProcessor::SetAckMask(uint16_t iMask)
 {
+  bool bReturn(false);
   CStdString strLog;
   strLog.Format("setting ackmask to %2x", iMask);
   m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
 
-  CCECAdapterMessagePtr output(new CCECAdapterMessage);
+  CCECAdapterMessage *output = new CCECAdapterMessage;
 
   output->push_back(MSGSTART);
   output->push_escaped(MSGCODE_SET_ACK_MASK);
@@ -417,11 +433,10 @@ bool CCECProcessor::SetAckMask(uint16_t iMask)
   output->push_escaped((uint8_t)iMask);
   output->push_back(MSGEND);
 
-  if (!Transmit(output))
-  {
+  if ((bReturn = Transmit(output)) == false)
     m_controller->AddLog(CEC_LOG_ERROR, "could not set the ackmask");
-    return false;
-  }
 
-  return true;
+  delete output;
+
+  return bReturn;
 }