cec: fix background polling and libcec exit
[deb_libcec.git] / src / lib / CECProcessor.cpp
index 673e83e34dc9e799da86d66d2f4f7036fb3b1276..84b9bafcbc451ba5e4881cd80f2e66107d5234bd 100644 (file)
 using namespace CEC;
 using namespace std;
 
-CCECProcessor::CCECProcessor(CLibCEC *controller, CAdapterCommunication *serComm, const char *strDeviceName, cec_logical_address iLogicalAddress /* = CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS*/) :
+CCECProcessor::CCECProcessor(CLibCEC *controller, const char *strDeviceName, cec_logical_address iLogicalAddress /* = CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS*/) :
     m_bStarted(false),
     m_iHDMIPort(CEC_DEFAULT_HDMI_PORT),
     m_iBaseDevice((cec_logical_address)CEC_DEFAULT_BASE_DEVICE),
     m_lastInitiator(CECDEVICE_UNKNOWN),
     m_strDeviceName(strDeviceName),
-    m_communication(serComm),
     m_controller(controller),
     m_bMonitor(false),
-    m_busScan(NULL)
+    m_busScan(NULL),
+    m_iStandardLineTimeout(3),
+    m_iRetryLineTimeout(3),
+    m_iLastTransmission(0)
 {
+  m_communication = new CAdapterCommunication(this);
   m_logicalAddresses.Clear();
   m_logicalAddresses.Set(iLogicalAddress);
   m_types.clear();
@@ -65,16 +68,19 @@ CCECProcessor::CCECProcessor(CLibCEC *controller, CAdapterCommunication *serComm
     m_busDevices[iPtr] = new CCECBusDevice(this, (cec_logical_address) iPtr, iPtr == iLogicalAddress ? iPhysicalAddress : 0);
 }
 
-CCECProcessor::CCECProcessor(CLibCEC *controller, CAdapterCommunication *serComm, const char *strDeviceName, const cec_device_type_list &types) :
+CCECProcessor::CCECProcessor(CLibCEC *controller, const char *strDeviceName, const cec_device_type_list &types) :
     m_bStarted(false),
     m_iHDMIPort(CEC_DEFAULT_HDMI_PORT),
     m_iBaseDevice((cec_logical_address)CEC_DEFAULT_BASE_DEVICE),
     m_strDeviceName(strDeviceName),
     m_types(types),
-    m_communication(serComm),
     m_controller(controller),
-    m_bMonitor(false)
+    m_bMonitor(false),
+    m_iStandardLineTimeout(3),
+    m_iRetryLineTimeout(3),
+    m_iLastTransmission(0)
 {
+  m_communication = new CAdapterCommunication(this);
   m_logicalAddresses.Clear();
   for (int iPtr = 0; iPtr < 16; iPtr++)
   {
@@ -111,6 +117,9 @@ CCECProcessor::CCECProcessor(CLibCEC *controller, CAdapterCommunication *serComm
 
 CCECProcessor::~CCECProcessor(void)
 {
+  m_bStarted = false;
+  StopThread(false);
+
   if (m_busScan)
   {
     m_busScan->StopThread();
@@ -120,18 +129,25 @@ CCECProcessor::~CCECProcessor(void)
   m_startCondition.Broadcast();
   StopThread();
 
+  delete m_communication;
   m_communication = NULL;
   m_controller = NULL;
   for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
     delete m_busDevices[iPtr];
 }
 
-bool CCECProcessor::Start(void)
+bool CCECProcessor::Start(const char *strPort, uint16_t iBaudRate /* = 38400 */, uint32_t iTimeoutMs /* = 10000 */)
 {
   CLockObject lock(&m_mutex);
-  if (!m_communication || !m_communication->IsOpen())
+  if (!m_communication || m_communication->IsOpen())
+  {
+    m_controller->AddLog(CEC_LOG_ERROR, "connection already opened");
+    return false;
+  }
+
+  if (!m_communication->Open(strPort, iBaudRate, iTimeoutMs))
   {
-    m_controller->AddLog(CEC_LOG_ERROR, "connection is closed");
+    m_controller->AddLog(CEC_LOG_ERROR, "could not open a connection");
     return false;
   }
 
@@ -147,14 +163,18 @@ bool CCECProcessor::Start(void)
     if (SetAckMask(m_logicalAddresses.AckMask()) &&
         SetHDMIPort(m_iBaseDevice, m_iHDMIPort, true))
     {
+      m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started");
       m_busScan = new CCECBusScan(this);
       m_busScan->CreateThread(true);
       return true;
     }
+    else
+    {
+      m_controller->AddLog(CEC_LOG_ERROR, "failed to initialise the processor");
+    }
   }
-  else
-    m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread");
 
+  m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread");
   return false;
 }
 
@@ -233,22 +253,6 @@ bool CCECProcessor::FindLogicalAddresses(void)
   return bReturn;
 }
 
-bool CCECProcessor::SetLineTimeout(uint8_t iTimeout)
-{
-  bool bReturn(false);
-  CCECAdapterMessage *output = new CCECAdapterMessage;
-
-  output->push_back(MSGSTART);
-  output->push_escaped(MSGCODE_TRANSMIT_IDLETIME);
-  output->push_escaped(iTimeout);
-  output->push_back(MSGEND);
-
-  if ((bReturn = Transmit(output)) == false)
-    m_controller->AddLog(CEC_LOG_ERROR, "could not set the idletime");
-  delete output;
-  return bReturn;
-}
-
 void *CCECProcessor::Process(void)
 {
   bool                  bParseFrame(false);
@@ -264,6 +268,7 @@ void *CCECProcessor::Process(void)
   }
   else
   {
+    m_busDevices[m_logicalAddresses.primary]->TransmitPhysicalAddress();
     CLockObject lock(&m_mutex);
     m_bStarted = true;
     m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started");
@@ -297,6 +302,9 @@ void *CCECProcessor::Process(void)
     m_controller->CheckKeypressTimeout();
   }
 
+  if (m_communication)
+    m_communication->Close();
+
   return NULL;
 }
 
@@ -311,7 +319,7 @@ bool CCECProcessor::SetActiveSource(cec_device_type type /* = CEC_DEVICE_TYPE_RE
 
   if (type != CEC_DEVICE_TYPE_RESERVED)
   {
-    for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
+    for (uint8_t iPtr = 0; iPtr <= 11; iPtr++)
     {
       if (m_logicalAddresses[iPtr] && m_busDevices[iPtr]->m_type == type)
       {
@@ -321,8 +329,29 @@ bool CCECProcessor::SetActiveSource(cec_device_type type /* = CEC_DEVICE_TYPE_RE
     }
   }
 
-  return SetStreamPath(m_busDevices[addr]->GetPhysicalAddress(false)) &&
-      m_busDevices[addr]->TransmitActiveSource();
+  bReturn = m_busDevices[CECDEVICE_TV]->PowerOn() &&
+      m_busDevices[addr]->TransmitActiveSource() &&
+      SetStreamPath(m_busDevices[addr]->GetPhysicalAddress(false));
+
+  if (bReturn && (m_busDevices[addr]->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE ||
+      m_busDevices[addr]->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE))
+  {
+    bReturn = ((CCECPlaybackDevice *)m_busDevices[addr])->TransmitDeckStatus(CECDEVICE_TV);
+  }
+
+  return bReturn;
+}
+
+void CCECProcessor::SetStandardLineTimeout(uint8_t iTimeout)
+{
+  CLockObject lock(&m_mutex);
+  m_iStandardLineTimeout = iTimeout;
+}
+
+void CCECProcessor::SetRetryLineTimeout(uint8_t iTimeout)
+{
+  CLockObject lock(&m_mutex);
+  m_iRetryLineTimeout = iTimeout;
 }
 
 bool CCECProcessor::SetActiveSource(cec_logical_address iAddress)
@@ -397,7 +426,7 @@ bool CCECProcessor::SetHDMIPort(cec_logical_address iBaseDevice, uint8_t iPort,
     iPhysicalAddress += (uint16_t)(iPort * iPos);
     strLog.Format("checking physical address %4x", iPhysicalAddress);
     AddLog(CEC_LOG_DEBUG, strLog);
-    if (CheckPhysicalAddress(iPhysicalAddress))
+    if (PhysicalAddressInUse(iPhysicalAddress))
     {
       strLog.Format("physical address %4x is in use", iPhysicalAddress);
       AddLog(CEC_LOG_DEBUG, strLog);
@@ -405,6 +434,8 @@ bool CCECProcessor::SetHDMIPort(cec_logical_address iBaseDevice, uint8_t iPort,
     }
     else
     {
+      strLog.Format("physical address %4x is free", iPhysicalAddress);
+      AddLog(CEC_LOG_DEBUG, strLog);
       SetPhysicalAddress(iPhysicalAddress);
       bReturn = true;
     }
@@ -413,7 +444,7 @@ bool CCECProcessor::SetHDMIPort(cec_logical_address iBaseDevice, uint8_t iPort,
   return bReturn;
 }
 
-bool CCECProcessor::CheckPhysicalAddress(uint16_t iPhysicalAddress)
+bool CCECProcessor::PhysicalAddressInUse(uint16_t iPhysicalAddress)
 {
   for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
   {
@@ -546,29 +577,29 @@ bool CCECProcessor::PollDevice(cec_logical_address iAddress)
   return false;
 }
 
-uint8_t CCECProcessor::VolumeUp(bool bWait /* = true */)
+uint8_t CCECProcessor::VolumeUp(void)
 {
   uint8_t status = 0;
   if (IsActiveDevice(CECDEVICE_AUDIOSYSTEM))
-    status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->VolumeUp(bWait);
+    status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->VolumeUp();
 
   return status;
 }
 
-uint8_t CCECProcessor::VolumeDown(bool bWait /* = true */)
+uint8_t CCECProcessor::VolumeDown(void)
 {
   uint8_t status = 0;
   if (IsActiveDevice(CECDEVICE_AUDIOSYSTEM))
-    status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->VolumeDown(bWait);
+    status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->VolumeDown();
 
   return status;
 }
 
-uint8_t CCECProcessor::MuteAudio(bool bWait /* = true */)
+uint8_t CCECProcessor::MuteAudio(void)
 {
   uint8_t status = 0;
   if (IsActiveDevice(CECDEVICE_AUDIOSYSTEM))
-    status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->MuteAudio(bWait);
+    status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->MuteAudio();
 
   return status;
 }
@@ -654,8 +685,13 @@ bool CCECProcessor::Transmit(const cec_command &data)
 
   CCECAdapterMessage *output = new CCECAdapterMessage(data);
   bReturn = Transmit(output);
-  delete output;
 
+  /* set to "not present" on failed ack */
+  if (output->is_error() && output->reply == MSGCODE_TRANSMIT_FAILED_ACK &&
+      output->destination() != CECDEVICE_BROADCAST)
+    m_busDevices[output->destination()]->SetDeviceStatus(CEC_DEVICE_STATUS_NOT_PRESENT);
+
+  delete output;
   return bReturn;
 }
 
@@ -664,28 +700,40 @@ 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
+    m_iLastTransmission = GetTimeMs();
+    m_communication->SetLineTimeout(m_iStandardLineTimeout);
+    output->tries = 1;
+
+    do
     {
-      output->condition.Wait(&output->mutex);
-      if (output->state != ADAPTER_MESSAGE_STATE_SENT)
-      {
-        m_controller->AddLog(CEC_LOG_ERROR, "command was not sent");
+      if (output->tries > 0)
+        m_communication->SetLineTimeout(m_iRetryLineTimeout);
+
+      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, "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_DEBUG, "did not receive ack");
-    }
-    else
-      bReturn = true;
+      if (output->transmit_timeout > 0)
+      {
+        if ((bReturn = WaitForTransmitSucceeded(output)) == false)
+          m_controller->AddLog(CEC_LOG_DEBUG, "did not receive ack");
+      }
+      else
+        bReturn = true;
+    }while (output->transmit_timeout > 0 && output->needs_retry() && ++output->tries < output->maxTries);
   }
 
+  m_communication->SetLineTimeout(m_iStandardLineTimeout);
+
   return bReturn;
 }
 
@@ -702,20 +750,20 @@ void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode
   Transmit(command);
 }
 
-bool CCECProcessor::WaitForTransmitSucceeded(uint8_t iLength, uint32_t iTimeout /* = 1000 */)
+bool CCECProcessor::WaitForTransmitSucceeded(CCECAdapterMessage *message)
 {
   bool bError(false);
   bool bTransmitSucceeded(false);
-  uint8_t iPacketsLeft(iLength / 4);
+  uint8_t iPacketsLeft(message->size() / 4);
 
   int64_t iNow = GetTimeMs();
-  int64_t iTargetTime = iNow + (uint64_t) iTimeout;
+  int64_t iTargetTime = iNow + message->transmit_timeout;
 
-  while (!bTransmitSucceeded && !bError && (iTimeout == 0 || iNow < iTargetTime))
+  while (!bTransmitSucceeded && !bError && (message->transmit_timeout == 0 || iNow < iTargetTime))
   {
     CCECAdapterMessage msg;
 
-    if (!m_communication->Read(msg, iTimeout > 0 ? (int32_t)(iTargetTime - iNow) : 1000))
+    if (!m_communication->Read(msg, message->transmit_timeout > 0 ? (int32_t)(iTargetTime - iNow) : 1000))
     {
       iNow = GetTimeMs();
       continue;
@@ -738,23 +786,29 @@ bool CCECProcessor::WaitForTransmitSucceeded(uint8_t iLength, uint32_t iTimeout
       continue;
     }
 
-    if ((bError = msg.is_error()) == false)
+    if (bError)
+    {
+      message->reply = msg.message();
+      m_controller->AddLog(CEC_LOG_DEBUG, msg.ToString());
+    }
+    else
     {
-      m_controller->AddLog(bError ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString());
-
       switch(msg.message())
       {
       case MSGCODE_COMMAND_ACCEPTED:
+        m_controller->AddLog(CEC_LOG_DEBUG, msg.ToString());
         if (iPacketsLeft > 0)
           iPacketsLeft--;
         break;
       case MSGCODE_TRANSMIT_SUCCEEDED:
+        m_controller->AddLog(CEC_LOG_DEBUG, msg.ToString());
         bTransmitSucceeded = (iPacketsLeft == 0);
         bError = !bTransmitSucceeded;
+        message->reply = MSGCODE_TRANSMIT_SUCCEEDED;
         break;
       default:
-        if (ParseMessage(msg))
-          m_commandBuffer.Push(m_currentframe);
+        // ignore other data while waiting
+        break;
       }
 
       iNow = GetTimeMs();
@@ -784,7 +838,7 @@ bool CCECProcessor::ParseMessage(const CCECAdapterMessage &msg)
         m_currentframe.ack         = msg.ack();
         m_currentframe.eom         = msg.eom();
       }
-      if (m_currentframe.ack == true)
+      if (m_currentframe.ack == 0x1)
       {
         m_lastInitiator = m_currentframe.initiator;
         m_busDevices[m_lastInitiator]->GetHandler()->HandlePoll(m_currentframe.initiator, m_currentframe.destination);
@@ -910,14 +964,14 @@ bool CCECProcessor::SetAckMask(uint16_t iMask)
   return bReturn;
 }
 
-bool CCECProcessor::SendKeypress(cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = false */)
+bool CCECProcessor::TransmitKeypress(cec_logical_address iDestination, cec_user_control_code key)
 {
-  return m_busDevices[iDestination]->SendKeypress(key, bWait);
+  return m_busDevices[iDestination]->TransmitKeypress(key);
 }
 
-bool CCECProcessor::SendKeyRelease(cec_logical_address iDestination, bool bWait /* = false */)
+bool CCECProcessor::TransmitKeyRelease(cec_logical_address iDestination)
 {
-  return m_busDevices[iDestination]->SendKeyRelease(bWait);
+  return m_busDevices[iDestination]->TransmitKeyRelease();
 }
 
 const char *CCECProcessor::ToString(const cec_menu_state state)
@@ -1244,27 +1298,48 @@ const char *CCECProcessor::ToString(const cec_vendor_id vendor)
 void *CCECBusScan::Process(void)
 {
   CCECBusDevice *device(NULL);
+
   while (!IsStopped())
   {
-    for (unsigned int iPtr = 0; iPtr < 15 && !IsStopped(); iPtr++)
+    for (unsigned int iPtr = 0; iPtr <= 11 && !IsStopped(); iPtr++)
     {
       device = m_processor->m_busDevices[iPtr];
-      if (device && device->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
+      WaitUntilIdle();
+      if (device && device->GetStatus(true) == CEC_DEVICE_STATUS_PRESENT)
       {
+        WaitUntilIdle();
         if (!IsStopped())
-          device->GetPhysicalAddress(false);
-        Sleep(5);
-
-        if (!IsStopped())
-          device->GetCecVersion();
-        Sleep(5);
+          device->GetVendorId();
 
+        WaitUntilIdle();
         if (!IsStopped())
-          device->GetVendorId();
-        Sleep(5);
+          device->GetPowerStatus(true);
       }
     }
-    Sleep(1000);
   }
+
   return NULL;
 }
+
+void CCECBusScan::WaitUntilIdle(void)
+{
+  if (IsStopped())
+    return;
+
+  int64_t iWaitTime = 3000 - (GetTimeMs() - m_processor->GetLastTransmission());
+  while (iWaitTime > 0)
+  {
+    Sleep(iWaitTime);
+    iWaitTime = 3000 - (GetTimeMs() - m_processor->GetLastTransmission());
+  }
+}
+
+bool CCECProcessor::StartBootloader(void)
+{
+  return m_communication->StartBootloader();
+}
+
+bool CCECProcessor::PingAdapter(void)
+{
+  return m_communication->PingAdapter();
+}