cec: added PollDevice()/cec_poll_device()
[deb_libcec.git] / src / lib / CECProcessor.cpp
index 4f696fdfa4d3b79e44db321ef216c29f6757da66..49b3bd2e35a11e5727e0f4c4f850b878999cc440 100644 (file)
@@ -48,12 +48,13 @@ CCECProcessor::CCECProcessor(CLibCEC *controller, CAdapterCommunication *serComm
     m_controller(controller),
     m_bMonitor(false)
 {
-  for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
+  for (int iPtr = 0; iPtr < 16; iPtr++)
     m_busDevices[iPtr] = new CCECBusDevice(this, (cec_logical_address) iPtr, iPtr == iLogicalAddress ? iPhysicalAddress : 0);
 }
 
 CCECProcessor::~CCECProcessor(void)
 {
+  m_startCondition.Broadcast();
   StopThread();
   m_communication = NULL;
   m_controller = NULL;
@@ -63,20 +64,22 @@ CCECProcessor::~CCECProcessor(void)
 
 bool CCECProcessor::Start(void)
 {
+  CLockObject lock(&m_mutex);
   if (!m_communication || !m_communication->IsOpen())
   {
     m_controller->AddLog(CEC_LOG_ERROR, "connection is closed");
     return false;
   }
 
-  if (!SetLogicalAddress(m_iLogicalAddress))
-  {
-    m_controller->AddLog(CEC_LOG_ERROR, "could not set the logical address");
-    return false;
-  }
-
   if (CreateThread())
+  {
+    if (!m_startCondition.Wait(&m_mutex))
+    {
+      m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread");
+      return false;
+    }
     return true;
+  }
   else
     m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread");
 
@@ -85,25 +88,31 @@ bool CCECProcessor::Start(void)
 
 void *CCECProcessor::Process(void)
 {
-  m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started");
+  cec_command           command;
+  CCECAdapterMessage    msg;
 
-  cec_command command;
-  cec_adapter_message msg;
+  SetAckMask(0x1 << (uint8_t)m_iLogicalAddress);
+
+  {
+    CLockObject lock(&m_mutex);
+    m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started");
+    m_startCondition.Signal();
+  }
 
   while (!IsStopped())
   {
     bool bParseFrame(false);
-    bool bError(false);
-    bool bTransmitSucceeded(false);
     command.clear();
     msg.clear();
 
     {
       CLockObject lock(&m_mutex);
       if (m_communication->IsOpen() && m_communication->Read(msg, 50))
-        ParseMessage(msg, &bError, &bTransmitSucceeded, &bParseFrame);
+      {
+        m_controller->AddLog(msg.is_error() ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString());
+        bParseFrame = ParseMessage(msg) && !IsStopped();
+      }
 
-      bParseFrame &= !IsStopped();
       if (bParseFrame)
         command = m_currentframe;
     }
@@ -111,13 +120,14 @@ void *CCECProcessor::Process(void)
     if (bParseFrame)
       ParseCommand(command);
 
+    Sleep(5);
+
     m_controller->CheckKeypressTimeout();
 
     for (unsigned int iDevicePtr = 0; iDevicePtr < 16; iDevicePtr++)
       m_busDevices[iDevicePtr]->PollVendorId();
 
-    if (!IsStopped())
-      Sleep(5);
+    Sleep(5);
   }
 
   return NULL;
@@ -128,7 +138,9 @@ bool CCECProcessor::SetActiveView(void)
   if (!IsRunning())
     return false;
 
-  return m_busDevices[m_iLogicalAddress]->BroadcastActiveView();
+  if (m_iLogicalAddress != CECDEVICE_UNKNOWN && m_busDevices[m_iLogicalAddress])
+    return m_busDevices[m_iLogicalAddress]->BroadcastActiveView();
+  return false;
 }
 
 bool CCECProcessor::SetInactiveView(void)
@@ -136,44 +148,45 @@ bool CCECProcessor::SetInactiveView(void)
   if (!IsRunning())
     return false;
 
-  return m_busDevices[m_iLogicalAddress]->BroadcastInactiveView();
+  if (m_iLogicalAddress != CECDEVICE_UNKNOWN && m_busDevices[m_iLogicalAddress])
+    return m_busDevices[m_iLogicalAddress]->BroadcastInactiveView();
+  return false;
 }
 
 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]);
   m_controller->AddLog(CEC_LOG_TRAFFIC, strTx.c_str());
 }
 
-bool CCECProcessor::Transmit(const cec_command &data, bool bWaitForAck /* = true */)
-{
-  LogOutput(data);
-
-  cec_adapter_message output;
-  output.clear();
-  CAdapterCommunication::FormatAdapterMessage(data, output);
-
-  return TransmitFormatted(output, bWaitForAck);
-}
-
 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress)
 {
-  CStdString strLog;
-  strLog.Format("<< setting logical address to %1x", iLogicalAddress);
-  m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
+  if (m_iLogicalAddress != iLogicalAddress)
+  {
+    CStdString strLog;
+    strLog.Format("<< setting logical address to %1x", iLogicalAddress);
+    m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
+    m_iLogicalAddress = iLogicalAddress;
+    return SetAckMask(0x1 << (uint8_t)m_iLogicalAddress);
+  }
 
-  m_iLogicalAddress = iLogicalAddress;
-  return m_communication && m_communication->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress);
+  return true;
 }
 
 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress)
 {
-  m_busDevices[m_iLogicalAddress]->SetPhysicalAddress(iPhysicalAddress);
-  return m_busDevices[m_iLogicalAddress]->BroadcastActiveView();
+  if (m_iLogicalAddress != CECDEVICE_UNKNOWN && m_busDevices[m_iLogicalAddress])
+  {
+    m_busDevices[m_iLogicalAddress]->SetPhysicalAddress(iPhysicalAddress);
+    return m_busDevices[m_iLogicalAddress]->BroadcastActiveView();
+  }
+  return false;
 }
 
 bool CCECProcessor::SwitchMonitoring(bool bEnable)
@@ -184,39 +197,87 @@ bool CCECProcessor::SwitchMonitoring(bool bEnable)
 
   m_bMonitor = bEnable;
   if (bEnable)
-    return m_communication && m_communication->SetAckMask(0);
+    return SetAckMask(0);
   else
-    return m_communication && m_communication->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress);
+    return SetAckMask(0x1 << (uint8_t)m_iLogicalAddress);
 }
 
-bool CCECProcessor::TransmitFormatted(const cec_adapter_message &data, bool bWaitForAck /* = true */)
+bool CCECProcessor::PollDevice(cec_logical_address iAddress)
 {
-  CLockObject lock(&m_mutex);
-  if (!m_communication || !m_communication->Write(data))
-    return false;
+  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();
+}
 
-  if (bWaitForAck)
+bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress, cec_menu_language *language)
+{
+  if (m_busDevices[iAddress])
   {
-    uint64_t now = GetTimeMs();
-    uint64_t target = now + 1000;
-    bool bError(false);
-    bool bGotAck(false);
+    *language = m_busDevices[iAddress]->GetMenuLanguage();
+    return (strcmp(language->language, "???") != 0);
+  }
+  return false;
+}
 
-    while (!bGotAck && now < target)
-    {
-      bGotAck = WaitForAck(&bError, (uint32_t) (target - now));
-      now = GetTimeMs();
+uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress)
+{
+  if (m_busDevices[iAddress])
+    return m_busDevices[iAddress]->GetVendorId();
+  return false;
+}
+
+cec_power_status CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress)
+{
+  if (m_busDevices[iAddress])
+    return m_busDevices[iAddress]->GetPowerStatus();
+  return CEC_POWER_STATUS_UNKNOWN;
+}
 
-      if (bError && now < target)
+bool CCECProcessor::Transmit(const cec_command &data)
+{
+  bool bReturn(false);
+  LogOutput(data);
+
+  CCECAdapterMessage *output = new CCECAdapterMessage(data);
+  bReturn = Transmit(output);
+  delete output;
+
+  return bReturn;
+}
+
+bool CCECProcessor::Transmit(CCECAdapterMessage *output)
+{
+  bool bReturn(false);
+  CLockObject lock(&m_mutex);
+  {
+    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, "retransmitting previous frame");
-        if (!m_communication->Write(data))
-          return false;
+        m_controller->AddLog(CEC_LOG_ERROR, "command was not sent");
+        return bReturn;
       }
     }
+
+    if (output->transmit_timeout > 0)
+    {
+      if ((bReturn = WaitForTransmitSucceeded(output->size(), output->transmit_timeout)) == false)
+        m_controller->AddLog(CEC_LOG_ERROR, "did not receive ack");
+    }
+    else
+      bReturn = true;
   }
 
-  return true;
+  return bReturn;
 }
 
 void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode, ECecAbortReason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
@@ -231,18 +292,18 @@ void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode
   Transmit(command);
 }
 
-bool CCECProcessor::WaitForAck(bool *bError, uint32_t iTimeout /* = 1000 */)
+bool CCECProcessor::WaitForTransmitSucceeded(uint8_t iLength, uint32_t iTimeout /* = 1000 */)
 {
-  bool bTransmitSucceeded = false, bEom = false;
-  *bError = false;
+  bool bError(false);
+  bool bTransmitSucceeded(false);
+  uint8_t iPacketsLeft(iLength / 4);
 
   int64_t iNow = GetTimeMs();
   int64_t iTargetTime = iNow + (uint64_t) iTimeout;
 
-  while (!bTransmitSucceeded && !*bError && (iTimeout == 0 || iNow < iTargetTime))
+  while (!bTransmitSucceeded && !bError && (iTimeout == 0 || iNow < iTargetTime))
   {
-    cec_adapter_message msg;
-    msg.clear();
+    CCECAdapterMessage msg;
 
     if (!m_communication->Read(msg, iTimeout > 0 ? (int32_t)(iTargetTime - iNow) : 1000))
     {
@@ -250,126 +311,67 @@ bool CCECProcessor::WaitForAck(bool *bError, uint32_t iTimeout /* = 1000 */)
       continue;
     }
 
-    ParseMessage(msg, bError, &bTransmitSucceeded, &bEom, false);
-    iNow = GetTimeMs();
+    if ((bError = msg.is_error()) == false)
+    {
+      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();
+    }
   }
 
-  return bTransmitSucceeded && !*bError;
+  return bTransmitSucceeded && !bError;
 }
 
-void CCECProcessor::ParseMessage(cec_adapter_message &msg, bool *bError, bool *bTransmitSucceeded, bool *bEom, bool bProcessMessages /* = true */)
+bool CCECProcessor::ParseMessage(const CCECAdapterMessage &msg)
 {
-  *bError = false;
-  *bTransmitSucceeded = false;
-  *bEom = false;
+  bool bEom = false;
 
   if (msg.empty())
-    return;
-
-  CStdString logStr;
+    return bEom;
 
   switch(msg.message())
   {
-  case MSGCODE_NOTHING:
-    m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_NOTHING");
-    break;
-  case MSGCODE_TIMEOUT_ERROR:
-  case MSGCODE_HIGH_ERROR:
-  case MSGCODE_LOW_ERROR:
-    {
-      if (msg.message() == MSGCODE_TIMEOUT_ERROR)
-        logStr = "MSGCODE_TIMEOUT";
-      else if (msg.message() == MSGCODE_HIGH_ERROR)
-        logStr = "MSGCODE_HIGH_ERROR";
-      else
-        logStr = "MSGCODE_LOW_ERROR";
-
-      int iLine      = (msg.size() >= 3) ? (msg[1] << 8) | (msg[2]) : 0;
-      uint32_t iTime = (msg.size() >= 7) ? (msg[3] << 24) | (msg[4] << 16) | (msg[5] << 8) | (msg[6]) : 0;
-      logStr.AppendFormat(" line:%i", iLine);
-      logStr.AppendFormat(" time:%u", iTime);
-      m_controller->AddLog(CEC_LOG_WARNING, logStr.c_str());
-      *bError = true;
-    }
-    break;
   case MSGCODE_FRAME_START:
     {
-      if (bProcessMessages)
-      {
-        logStr = "MSGCODE_FRAME_START";
-        m_currentframe.clear();
-        if (msg.size() >= 2)
-        {
-          logStr.AppendFormat(" initiator:%u destination:%u ack:%s %s", msg.initiator(), msg.destination(), msg.ack() ? "high" : "low", msg.eom() ? "eom" : "");
-          m_currentframe.initiator   = msg.initiator();
-          m_currentframe.destination = msg.destination();
-          m_currentframe.ack         = msg.ack();
-          m_currentframe.eom         = msg.eom();
-        }
-        m_controller->AddLog(CEC_LOG_DEBUG, logStr.c_str());
-      }
-      else
+      m_currentframe.clear();
+      if (msg.size() >= 2)
       {
-        m_frameBuffer.Push(msg);
+        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 (bProcessMessages)
-      {
-        logStr = "MSGCODE_FRAME_DATA";
-        if (msg.size() >= 2)
-        {
-          uint8_t iData = msg[1];
-          logStr.AppendFormat(" %02x", iData);
-          m_currentframe.push_back(iData);
-          m_currentframe.eom = msg.eom();
-        }
-        m_controller->AddLog(CEC_LOG_DEBUG, logStr.c_str());
-      }
-      else
+      if (msg.size() >= 2)
       {
-        m_frameBuffer.Push(msg);
+        m_currentframe.push_back(msg[1]);
+        m_currentframe.eom = msg.eom();
       }
-
-      *bEom = msg.eom();
+      bEom = msg.eom();
     }
     break;
-  case MSGCODE_COMMAND_ACCEPTED:
-    m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_COMMAND_ACCEPTED");
-    break;
-  case MSGCODE_TRANSMIT_SUCCEEDED:
-    m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_TRANSMIT_SUCCEEDED");
-    *bTransmitSucceeded = true;
-    break;
-  case MSGCODE_RECEIVE_FAILED:
-    m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_RECEIVE_FAILED");
-    *bError = true;
-    break;
-  case MSGCODE_COMMAND_REJECTED:
-    m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_COMMAND_REJECTED");
-    *bError = true;
-    break;
-  case MSGCODE_TRANSMIT_FAILED_LINE:
-    m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_LINE");
-    *bError = true;
-    break;
-  case MSGCODE_TRANSMIT_FAILED_ACK:
-    m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_ACK");
-    *bError = true;
-    break;
-  case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA:
-    m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA");
-    *bError = true;
-    break;
-  case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE:
-    m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE");
-    *bError = true;
-    break;
   default:
     break;
   }
+
+  return bEom;
 }
 
 void CCECProcessor::ParseCommand(cec_command &command)
@@ -386,7 +388,9 @@ void CCECProcessor::ParseCommand(cec_command &command)
 
 uint16_t CCECProcessor::GetPhysicalAddress(void) const
 {
-  return m_busDevices[m_iLogicalAddress]->GetPhysicalAddress();
+  if (m_iLogicalAddress != CECDEVICE_UNKNOWN && m_busDevices[m_iLogicalAddress])
+    return m_busDevices[m_iLogicalAddress]->GetPhysicalAddress();
+  return false;
 }
 
 void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode)
@@ -399,6 +403,11 @@ void CCECProcessor::AddCommand(const cec_command &command)
   m_controller->AddCommand(command);
 }
 
+void CCECProcessor::AddKey(cec_keypress &key)
+{
+  m_controller->AddKey(key);
+}
+
 void CCECProcessor::AddKey(void)
 {
   m_controller->AddKey();
@@ -408,3 +417,26 @@ void CCECProcessor::AddLog(cec_log_level level, const CStdString &strMessage)
 {
   m_controller->AddLog(level, 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());
+
+  CCECAdapterMessage *output = new CCECAdapterMessage;
+
+  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);
+
+  if ((bReturn = Transmit(output)) == false)
+    m_controller->AddLog(CEC_LOG_ERROR, "could not set the ackmask");
+
+  delete output;
+
+  return bReturn;
+}