From 38b8eaae3ef151943595c4033b628a7c98d90c64 Mon Sep 17 00:00:00 2001
From: Matus Kral <matuskral@me.com>
Date: Tue, 15 Apr 2014 11:07:00 +0200
Subject: [PATCH 1/5] fix RPI POLL during LA address negotiation

---
 src/lib/adapter/RPi/RPiCECAdapterCommunication.cpp | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/src/lib/adapter/RPi/RPiCECAdapterCommunication.cpp b/src/lib/adapter/RPi/RPiCECAdapterCommunication.cpp
index 76c61b3..2905c56 100644
--- a/src/lib/adapter/RPi/RPiCECAdapterCommunication.cpp
+++ b/src/lib/adapter/RPi/RPiCECAdapterCommunication.cpp
@@ -382,13 +382,18 @@ cec_adapter_message_state CRPiCECAdapterCommunication::Write(const cec_command &
     return (data.initiator == data.destination) ? ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED : ADAPTER_MESSAGE_STATE_ERROR;
   }
 
-  if (!data.opcode_set && data.initiator == data.destination)
+  if (!m_queue->Write(data, bIsReply))
   {
-    // registration of the logical address would have failed
-    return ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED;
+    if (!data.opcode_set)
+    {
+      return ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED;
+    }
+    
+    return ADAPTER_MESSAGE_STATE_SENT;
   }
 
-  return m_queue->Write(data, bIsReply) ? ADAPTER_MESSAGE_STATE_SENT_ACKED : ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED;
+  return ADAPTER_MESSAGE_STATE_SENT_ACKED;
+
 }
 
 uint16_t CRPiCECAdapterCommunication::GetFirmwareVersion(void)

From 485660f88df81ef5e535a074b1385c49bc687489 Mon Sep 17 00:00:00 2001
From: Matus Kral <matuskral@me.com>
Date: Sun, 20 Apr 2014 22:24:03 +0200
Subject: [PATCH 2/5] fix assertion on XBMC exit

---
 src/lib/adapter/RPi/RPiCECAdapterCommunication.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/src/lib/adapter/RPi/RPiCECAdapterCommunication.cpp b/src/lib/adapter/RPi/RPiCECAdapterCommunication.cpp
index 2905c56..243223d 100644
--- a/src/lib/adapter/RPi/RPiCECAdapterCommunication.cpp
+++ b/src/lib/adapter/RPi/RPiCECAdapterCommunication.cpp
@@ -353,7 +353,8 @@ void CRPiCECAdapterCommunication::Close(void)
     else
       return;
   }
-  vc_tv_unregister_callback(rpi_tv_callback);
+  if (m_bInitialised)
+    vc_tv_unregister_callback(rpi_tv_callback);
 
   UnregisterLogicalAddress();
 

From f9c4a2de412487b78c333dc21d9b7e41a5fe69e3 Mon Sep 17 00:00:00 2001
From: Matus Kral <matuskral@me.com>
Date: Mon, 26 May 2014 20:56:10 +0200
Subject: [PATCH 4/5] RPI updates with fixes - 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 | 97 +++++++---------------
 src/lib/adapter/RPi/RPiCECAdapterCommunication.h   |  2 +-
 src/lib/adapter/RPi/RPiCECAdapterMessageQueue.cpp  | 50 ++++++++---
 src/lib/adapter/RPi/RPiCECAdapterMessageQueue.h    | 13 ++-
 4 files changed, 79 insertions(+), 83 deletions(-)

diff --git a/src/lib/adapter/RPi/RPiCECAdapterCommunication.cpp b/src/lib/adapter/RPi/RPiCECAdapterCommunication.cpp
index 243223d..9af5357 100644
--- a/src/lib/adapter/RPi/RPiCECAdapterCommunication.cpp
+++ b/src/lib/adapter/RPi/RPiCECAdapterCommunication.cpp
@@ -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;
 }
diff --git a/src/lib/adapter/RPi/RPiCECAdapterCommunication.h b/src/lib/adapter/RPi/RPiCECAdapterCommunication.h
index 7fbc105..e6a6446 100644
--- a/src/lib/adapter/RPi/RPiCECAdapterCommunication.h
+++ b/src/lib/adapter/RPi/RPiCECAdapterCommunication.h
@@ -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 */
diff --git a/src/lib/adapter/RPi/RPiCECAdapterMessageQueue.cpp b/src/lib/adapter/RPi/RPiCECAdapterMessageQueue.cpp
index aa9701d..cb9fed0 100644
--- a/src/lib/adapter/RPi/RPiCECAdapterMessageQueue.cpp
+++ b/src/lib/adapter/RPi/RPiCECAdapterMessageQueue.cpp
@@ -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);
diff --git a/src/lib/adapter/RPi/RPiCECAdapterMessageQueue.h b/src/lib/adapter/RPi/RPiCECAdapterMessageQueue.h
index af5756b..a6f4d7e 100644
--- a/src/lib/adapter/RPi/RPiCECAdapterMessageQueue.h
+++ b/src/lib/adapter/RPi/RPiCECAdapterMessageQueue.h
@@ -33,6 +33,12 @@
 
 #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
 {
@@ -65,6 +71,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.
      */
     const char *ToString(void) const { return "CEC transmission"; }
@@ -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 */

From 0ba92c2d4fb9088e52c9a0d80169fd86d48156b8 Mon Sep 17 00:00:00 2001
From: Matus Kral <matuskral@me.com>
Date: Mon, 26 May 2014 20:59:17 +0200
Subject: [PATCH 5/5] probably ugly hack to help with some newer Samsung TVs
 and activating source

---
 src/lib/implementations/ANCommandHandler.cpp | 10 ++++++++++
 src/lib/implementations/ANCommandHandler.h   |  1 +
 2 files changed, 11 insertions(+)

diff --git a/src/lib/implementations/ANCommandHandler.cpp b/src/lib/implementations/ANCommandHandler.cpp
index 7e0e9b9..93e8c92 100644
--- a/src/lib/implementations/ANCommandHandler.cpp
+++ b/src/lib/implementations/ANCommandHandler.cpp
@@ -116,3 +116,13 @@ int CANCommandHandler::HandleDeviceVendorCommandWithId(const cec_command &comman
   }
   return CEC_ABORT_REASON_INVALID_OPERAND;
 }
+
+int CANCommandHandler::HandleSetMenuLanguage(const cec_command &command)
+{
+  if (m_processor->CECInitialised() && command.initiator == CECDEVICE_TV && command.destination == CECDEVICE_BROADCAST)
+  {
+    m_processor->GetDevice(command.initiator)->SetPowerStatus(CEC_POWER_STATUS_ON);
+  }
+
+  return CCECCommandHandler::HandleSetMenuLanguage(command);
+}
diff --git a/src/lib/implementations/ANCommandHandler.h b/src/lib/implementations/ANCommandHandler.h
index 595170a..73d3cae 100644
--- a/src/lib/implementations/ANCommandHandler.h
+++ b/src/lib/implementations/ANCommandHandler.h
@@ -47,6 +47,7 @@ namespace CEC
 
     int HandleVendorRemoteButtonDown(const cec_command &command);
     int HandleDeviceVendorCommandWithId(const cec_command &command);
+    int HandleSetMenuLanguage(const cec_command &command);
 
   protected:
     bool PowerOn(const cec_logical_address iInitiator, const cec_logical_address iDestination);
