RPI updates with fixes
authorMatus Kral <matuskral@me.com>
Mon, 26 May 2014 18:56:10 +0000 (20:56 +0200)
committerMatus Kral <matuskral@me.com>
Thu, 29 May 2014 01:16:09 +0000 (03:16 +0200)
- losing device from TVs menu on source changes
- XBMC Input Device settings change will not freeze XBMC
(or disable adapter)
- TV auto connection to adapter properly upon AS command
- imposibility to switch back to RPI even if listed by TV

it is possible it fixes some others as well

src/lib/adapter/RPi/RPiCECAdapterCommunication.cpp
src/lib/adapter/RPi/RPiCECAdapterCommunication.h
src/lib/adapter/RPi/RPiCECAdapterMessageQueue.cpp
src/lib/adapter/RPi/RPiCECAdapterMessageQueue.h

index 243223d3f67d0a149d5e37a85bc8daa4e065a0aa..9af5357602ae5b32597c383ff0b4845f7d3a66e6 100644 (file)
@@ -78,7 +78,9 @@ CRPiCECAdapterCommunication::CRPiCECAdapterCommunication(IAdapterCommunicationCa
 CRPiCECAdapterCommunication::~CRPiCECAdapterCommunication(void)
 {
   delete(m_queue);
+  UnregisterLogicalAddress();
   Close();
+  vc_cec_set_passive(false);
 }
 
 const char *ToString(const VC_CEC_ERROR_T error)
@@ -212,6 +214,7 @@ void CRPiCECAdapterCommunication::OnDataReceived(uint32_t header, uint32_t p0, u
       {
         m_bLogicalAddressChanged = true;
         m_logicalAddress = (cec_logical_address)(p0 & 0xF);
+        m_bLogicalAddressRegistered = true;
         LIB_CEC->AddLog(CEC_LOG_DEBUG, "logical address changed to %s (%x)", LIB_CEC->ToString(m_logicalAddress), m_logicalAddress);
       }
       else
@@ -224,8 +227,9 @@ void CRPiCECAdapterCommunication::OnDataReceived(uint32_t header, uint32_t p0, u
     break;
   case VC_CEC_LOGICAL_ADDR_LOST:
     {
+      LIB_CEC->AddLog(CEC_LOG_DEBUG, "logical %s (%x) address lost", LIB_CEC->ToString(m_logicalAddress), m_logicalAddress);
       // the logical address was taken by another device
-      cec_logical_address previousAddress = m_logicalAddress == CECDEVICE_BROADCAST ? m_previousLogicalAddress : m_logicalAddress;
+      cec_logical_address previousAddress = m_logicalAddress == CECDEVICE_FREEUSE ? m_previousLogicalAddress : m_logicalAddress;
       m_logicalAddress = CECDEVICE_UNKNOWN;
 
       // notify libCEC that we lost our LA when the connection was initialised
@@ -305,23 +309,18 @@ bool CRPiCECAdapterCommunication::Open(uint32_t iTimeoutMs /* = CEC_DEFAULT_CONN
     vc_cec_register_callback(rpi_cec_callback, (void*)this);
     vc_tv_register_callback(rpi_tv_callback, (void*)this);
 
-    // release previous LA
-    vc_cec_release_logical_address();
-    if (!m_logicalAddressCondition.Wait(m_mutex, m_bLogicalAddressChanged, iTimeoutMs))
-    {
-      LIB_CEC->AddLog(CEC_LOG_ERROR, "failed to release the previous LA");
-      return false;
-    }
-
     // register LA "freeuse"
-    if (RegisterLogicalAddress(CECDEVICE_FREEUSE))
+    if (RegisterLogicalAddress(CECDEVICE_FREEUSE, iTimeoutMs))
     {
       LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - vc_cec initialised", __FUNCTION__);
       CLockObject lock(m_mutex);
       m_bInitialised = true;
     }
     else
+    {
       LIB_CEC->AddLog(CEC_LOG_ERROR, "%s - vc_cec could not be initialised", __FUNCTION__);
+      return false;
+    }
   }
 
   return true;
@@ -346,20 +345,10 @@ uint16_t CRPiCECAdapterCommunication::GetPhysicalAddress(void)
 
 void CRPiCECAdapterCommunication::Close(void)
 {
-  {
-    CLockObject lock(m_mutex);
-    if (m_bInitialised)
-      m_bInitialised = false;
-    else
-      return;
-  }
-  if (m_bInitialised)
+  if (m_bInitialised) {
     vc_tv_unregister_callback(rpi_tv_callback);
-
-  UnregisterLogicalAddress();
-
-  // disable passive mode
-  vc_cec_set_passive(false);
+    m_bInitialised = false;
+  }
 
   if (!g_bHostInited)
   {
@@ -374,27 +363,16 @@ std::string CRPiCECAdapterCommunication::GetError(void) const
   return strError;
 }
 
-cec_adapter_message_state CRPiCECAdapterCommunication::Write(const cec_command &data, bool &UNUSED(bRetry), uint8_t UNUSED(iLineTimeout), bool bIsReply)
+cec_adapter_message_state CRPiCECAdapterCommunication::Write(const cec_command &data, bool &bRetry, uint8_t iLineTimeout, bool bIsReply)
 {
-  // ensure that the source LA is registered
-  if (!RegisterLogicalAddress(data.initiator))
-  {
-    LIB_CEC->AddLog(CEC_LOG_DEBUG, "failed to register logical address %s (%X)", CCECTypeUtils::ToString(data.initiator), data.initiator);
-    return (data.initiator == data.destination) ? ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED : ADAPTER_MESSAGE_STATE_ERROR;
-  }
-
-  if (!m_queue->Write(data, bIsReply))
-  {
-    if (!data.opcode_set)
-    {
-      return ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED;
-    }
-    
-    return ADAPTER_MESSAGE_STATE_SENT;
-  }
-
-  return ADAPTER_MESSAGE_STATE_SENT_ACKED;
+  VC_CEC_ERROR_T vcAnswer;
+  uint32_t iTimeout = (data.transmit_timeout ? data.transmit_timeout : iLineTimeout*1000);
 
+  cec_adapter_message_state rc = m_queue->Write(data, bRetry, iTimeout, bIsReply, vcAnswer);
+#ifdef CEC_DEBUGGING
+  LIB_CEC->AddLog(CEC_LOG_DEBUG, "sending data: result %s", ToString(vcAnswer));
+#endif
+  return rc;
 }
 
 uint16_t CRPiCECAdapterCommunication::GetFirmwareVersion(void)
@@ -404,22 +382,15 @@ uint16_t CRPiCECAdapterCommunication::GetFirmwareVersion(void)
 
 cec_logical_address CRPiCECAdapterCommunication::GetLogicalAddress(void)
 {
-  {
-    CLockObject lock(m_mutex);
-    if (m_logicalAddress != CECDEVICE_UNKNOWN)
-      return m_logicalAddress;
-  }
+  CLockObject lock(m_mutex);
 
-  CEC_AllDevices_T address;
-  return (vc_cec_get_logical_address(&address) == VCHIQ_SUCCESS) ?
-      (cec_logical_address)address : CECDEVICE_UNKNOWN;
+  return m_logicalAddress;
 }
 
 bool CRPiCECAdapterCommunication::UnregisterLogicalAddress(void)
 {
   CLockObject lock(m_mutex);
-  if (m_logicalAddress == CECDEVICE_UNKNOWN ||
-      m_logicalAddress == CECDEVICE_BROADCAST)
+  if (!m_bInitialised)
     return true;
 
   LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - releasing previous logical address", __FUNCTION__);
@@ -434,34 +405,25 @@ bool CRPiCECAdapterCommunication::UnregisterLogicalAddress(void)
   return m_logicalAddressCondition.Wait(m_mutex, m_bLogicalAddressChanged);
 }
 
-bool CRPiCECAdapterCommunication::RegisterLogicalAddress(const cec_logical_address address)
+bool CRPiCECAdapterCommunication::RegisterLogicalAddress(const cec_logical_address address, uint32_t iTimeoutMs)
 {
   {
     CLockObject lock(m_mutex);
-    if (m_logicalAddress == address)
+    if ((m_logicalAddress == address) && m_bLogicalAddressRegistered)
       return true;
   }
 
-  if (!UnregisterLogicalAddress())
-    return false;
-
-  LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - registering address %x", __FUNCTION__, address);
-
-  CLockObject lock(m_mutex);
   m_bLogicalAddressChanged = false;
-  vc_cec_poll_address((CEC_AllDevices_T)address);
 
   // register the new LA
   int iRetval = vc_cec_set_logical_address((CEC_AllDevices_T)address, (CEC_DEVICE_TYPE_T)CCECTypeUtils::GetType(address), CEC_VENDOR_ID_BROADCOM);
   if (iRetval != VCHIQ_SUCCESS)
   {
     LIB_CEC->AddLog(CEC_LOG_ERROR, "%s - vc_cec_set_logical_address(%X) returned %s (%d)", __FUNCTION__, address, ToString((VC_CEC_ERROR_T)iRetval), iRetval);
-    return false;
+    UnregisterLogicalAddress();
   }
-
-  if (m_logicalAddressCondition.Wait(m_mutex, m_bLogicalAddressChanged))
+  else if (m_logicalAddressCondition.Wait(m_mutex, m_bLogicalAddressChanged, iTimeoutMs))
   {
-    m_bLogicalAddressRegistered = true;
     return true;
   }
   return false;
@@ -470,9 +432,8 @@ bool CRPiCECAdapterCommunication::RegisterLogicalAddress(const cec_logical_addre
 cec_logical_addresses CRPiCECAdapterCommunication::GetLogicalAddresses(void)
 {
   cec_logical_addresses addresses; addresses.Clear();
-  cec_logical_address current = GetLogicalAddress();
-  if (current != CECDEVICE_UNKNOWN)
-    addresses.Set(current);
+  if (m_bLogicalAddressRegistered)
+    addresses.primary = GetLogicalAddress();
 
   return addresses;
 }
index 7fbc1052f7793f3fa576e10d9c5d1374b8329f50..e6a644645ec210fa6b3633a0ec4b2e4d7aef0a2a 100644 (file)
@@ -97,7 +97,7 @@ namespace CEC
   private:
     cec_logical_address GetLogicalAddress(void);
     bool UnregisterLogicalAddress(void);
-    bool RegisterLogicalAddress(const cec_logical_address address);
+    bool RegisterLogicalAddress(const cec_logical_address address, uint32_t iTimeoutMs = CEC_DEFAULT_CONNECT_TIMEOUT);
     int InitHostCEC(void);
 
     bool m_bInitialised;   /**< true when the connection is initialised, false otherwise */
index aa9701d3ec260c26139fc15bc15bd712de4fa051..cb9fed01f8f9cc179b2b949cea72389f1167ae40 100644 (file)
@@ -71,9 +71,12 @@ void CRPiCECAdapterMessageQueueEntry::Broadcast(void)
 
 bool CRPiCECAdapterMessageQueueEntry::MessageReceived(cec_opcode opcode, cec_logical_address initiator, cec_logical_address destination, uint32_t response)
 {
-  if ((!m_command.opcode_set || m_command.opcode == opcode) &&
-      m_command.initiator == initiator &&
-      m_command.destination == destination)
+  if ((m_command.opcode_set && m_command.opcode == opcode &&
+     m_command.initiator == initiator &&
+     m_command.destination == destination)
+     ||
+     (!m_command.opcode_set && 
+     m_command.destination == destination))
   {
     CLockObject lock(m_mutex);
     m_retval = response;
@@ -93,9 +96,6 @@ bool CRPiCECAdapterMessageQueueEntry::Wait(uint32_t iTimeout)
     CLockObject lock(m_mutex);
     bReturn = m_bSucceeded ? true : m_condition.Wait(m_mutex, m_bSucceeded, iTimeout);
     m_bWaiting = false;
-
-    if (bReturn)
-      bReturn = m_retval == VCHIQ_SUCCESS;
   }
   return bReturn;
 }
@@ -124,7 +124,12 @@ void CRPiCECAdapterMessageQueue::MessageReceived(cec_opcode opcode, cec_logical_
     LIB_CEC->AddLog(CEC_LOG_WARNING, "unhandled response received: opcode=%x initiator=%x destination=%x response=%x", (int)opcode, (int)initiator, (int)destination, response);
 }
 
-bool CRPiCECAdapterMessageQueue::Write(const cec_command &command, bool bIsReply)
+uint32_t CRPiCECAdapterMessageQueueEntry::Result() const
+{
+  return m_retval;
+}
+
+cec_adapter_message_state CRPiCECAdapterMessageQueue::Write(const cec_command &command, bool &bRetry, uint32_t iLineTimeout, bool bIsReply, VC_CEC_ERROR_T &vcReply)
 {
   CRPiCECAdapterMessageQueueEntry *entry = new CRPiCECAdapterMessageQueueEntry(this, command);
   uint64_t iEntryId(0);
@@ -182,24 +187,43 @@ bool CRPiCECAdapterMessageQueue::Write(const cec_command &command, bool bIsReply
   LIB_CEC->AddLog(CEC_LOG_DEBUG, "sending data: %s", strDump.c_str());
 #endif
 
-   int iReturn = vc_cec_send_message((uint32_t)command.destination, (uint8_t*)&payload, iLength, bIsReply);
+  int iReturn = vc_cec_send_message((uint32_t)command.destination, command.opcode_set ? (uint8_t*)&payload : NULL, iLength, bIsReply);
 #endif
 
+  bRetry = false;
   if (iReturn != VCHIQ_SUCCESS)
   {
     LIB_CEC->AddLog(CEC_LOG_DEBUG, "sending command '%s' failed (%d)", command.opcode_set ? CCECTypeUtils::ToString(command.opcode) : "POLL", iReturn);
     delete (entry);
-    return false;
+    return ADAPTER_MESSAGE_STATE_ERROR;
   }
 
-  bool bReturn(true);
+  cec_adapter_message_state bReturn(ADAPTER_MESSAGE_STATE_ERROR);
   if (entry)
   {
-    if (!entry->Wait(CEC_DEFAULT_TRANSMIT_WAIT))
+    if (entry->Wait(iLineTimeout))
     {
-      LIB_CEC->AddLog(CEC_LOG_DEBUG, "command '%s' was not acked by the controller", command.opcode_set ? CCECTypeUtils::ToString(command.opcode) : "POLL");
-      bReturn = false;
+      int status = entry->Result();
+
+      if (status == VC_CEC_ERROR_NO_ACK)
+        bReturn = ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED;
+      else if (status == VC_CEC_SUCCESS)
+        bReturn = ADAPTER_MESSAGE_STATE_SENT_ACKED;
+      else
+        bReturn = ADAPTER_MESSAGE_STATE_SENT;
     }
+    else
+    {
+      if (command.opcode_set)
+      {
+        bRetry = true;
+        LIB_CEC->AddLog(CEC_LOG_DEBUG, "command '%s' timeout", command.opcode_set ? CCECTypeUtils::ToString(command.opcode) : "POLL");
+        sleep(CEC_DEFAULT_TRANSMIT_RETRY_WAIT);
+      }
+      bReturn = ADAPTER_MESSAGE_STATE_WAITING_TO_BE_SENT;
+    }
+
+    vcReply = (VC_CEC_ERROR_T)entry->Result();
 
     CLockObject lock(m_mutex);
     m_messages.erase(iEntryId);
index af5756b68aa5588815f19344f74d3aa66434b9e8..a6f4d7eb59881a43cbad6ac38e0f293178df983e 100644 (file)
 
 #include "lib/platform/util/buffer.h"
 #include <map>
+#include "lib/adapter/AdapterCommunication.h"
+
+extern "C" {
+#include <interface/vmcs_host/vc_cecservice.h>
+#include <interface/vchiq_arm/vchiq_if.h>
+}
 
 namespace CEC
 {
@@ -64,6 +70,11 @@ namespace CEC
      */
     bool IsWaiting(void);
 
+    /*!
+     * @brief Query result from worker thread
+     */
+    uint32_t Result() const;
+
     /*!
      * @return The command that was sent in human readable form.
      */
@@ -106,7 +117,7 @@ namespace CEC
 
     void MessageReceived(cec_opcode opcode, cec_logical_address initiator, cec_logical_address destination, uint32_t response);
 
-    bool Write(const cec_command &command, bool bIsReply);
+    cec_adapter_message_state Write(const cec_command &command, bool &bRetry, uint32_t iLineTimeout, bool bIsReply, VC_CEC_ERROR_T &vcReply);
 
   private:
     CRPiCECAdapterCommunication *                             m_com;                    /**< the communication handler */