allocate a new address in a background thread when the communication interface report...
authorLars Op den Kamp <lars@opdenkamp.eu>
Wed, 5 Sep 2012 12:59:39 +0000 (14:59 +0200)
committerLars Op den Kamp <lars@opdenkamp.eu>
Wed, 5 Sep 2012 13:07:59 +0000 (15:07 +0200)
src/lib/CECProcessor.cpp
src/lib/CECProcessor.h
src/lib/adapter/AdapterCommunication.h
src/lib/adapter/RPi/RPiCECAdapterCommunication.cpp
src/lib/adapter/RPi/RPiCECAdapterCommunication.h

index 34ffed58db84902d286a2532b78223f3b58af9c8..8df38846002ecb643e31135a0b37f33d26cd66fc 100644 (file)
@@ -63,13 +63,15 @@ CCECProcessor::CCECProcessor(CLibCEC *libcec) :
     m_iStandardLineTimeout(3),
     m_iRetryLineTimeout(3),
     m_iLastTransmission(0),
-    m_bMonitor(true)
+    m_bMonitor(true),
+    m_addrAllocator(NULL)
 {
   m_busDevices = new CCECDeviceMap(this);
 }
 
 CCECProcessor::~CCECProcessor(void)
 {
+  DELETE_AND_NULL(m_addrAllocator);
   Close();
   DELETE_AND_NULL(m_busDevices);
 }
@@ -914,18 +916,27 @@ void CCECProcessor::SwitchMonitoring(bool bSwitchTo)
     UnregisterClients();
 }
 
-void CCECProcessor::HandleLogicalAddressLost(cec_logical_address oldAddress, cec_logical_address newAddress)
+void CCECProcessor::HandleLogicalAddressLost(cec_logical_address oldAddress)
 {
-  m_libcec->AddLog(CEC_LOG_NOTICE, "logical address %x was taken by another device, changed to %x", oldAddress, newAddress);
+  m_libcec->AddLog(CEC_LOG_NOTICE, "logical address %x was taken by another device, allocating a new address", oldAddress);
   CCECClient* client = GetClient(oldAddress);
   if (client)
   {
-    if (newAddress == CECDEVICE_UNKNOWN)
-      UnregisterClient(client);
-    else
-    {
-      client->m_configuration.logicalAddresses.Unset(oldAddress);
-      client->m_configuration.logicalAddresses.Set(newAddress);
-    }
+    if (m_addrAllocator)
+      while (m_addrAllocator->IsRunning()) Sleep(5);
+    delete m_addrAllocator;
+
+    m_addrAllocator = new CCECAllocateLogicalAddress(this, client);
+    m_addrAllocator->CreateThread();
   }
 }
+
+CCECAllocateLogicalAddress::CCECAllocateLogicalAddress(CCECProcessor* processor, CCECClient* client) :
+    m_processor(processor),
+    m_client(client) { }
+
+void* CCECAllocateLogicalAddress::Process(void)
+{
+  m_processor->AllocateLogicalAddresses(m_client);
+  return NULL;
+}
index 6faf52132e44bccfa62c2ac2273c8a7cf31d49b1..6ad60c515a6c7a55137423b62f2602cf59f7b492 100644 (file)
@@ -51,6 +51,18 @@ namespace CEC
   class CCECTuner;
   class CCECTV;
   class CCECClient;
+  class CCECProcessor;
+
+  class CCECAllocateLogicalAddress : public PLATFORM::CThread
+  {
+  public:
+    CCECAllocateLogicalAddress(CCECProcessor* processor, CCECClient* client);
+    void* Process(void);
+
+  private:
+    CCECProcessor* m_processor;
+    CCECClient*    m_client;
+  };
 
   class CCECProcessor : public PLATFORM::CThread, public IAdapterCommunicationCallback
   {
@@ -69,7 +81,7 @@ namespace CEC
       CCECClient *GetClient(const cec_logical_address address);
 
       bool                  OnCommandReceived(const cec_command &command);
-      void                  HandleLogicalAddressLost(cec_logical_address oldAddress, cec_logical_address newAddress);
+      void                  HandleLogicalAddressLost(cec_logical_address oldAddress);
 
       CCECBusDevice *       GetDevice(cec_logical_address address) const;
       CCECAudioSystem *     GetAudioSystem(void) const;
@@ -130,6 +142,7 @@ namespace CEC
       bool IsRunningLatestFirmware(void);
       void SwitchMonitoring(bool bSwitchTo);
 
+      bool AllocateLogicalAddresses(CCECClient* client);
     private:
       bool OpenConnection(const char *strPort, uint16_t iBaudRate, uint32_t iTimeoutMs, bool bStartListening = true);
       void SetCECInitialised(bool bSetTo = true);
@@ -145,8 +158,6 @@ namespace CEC
 
       void ResetMembers(void);
 
-      bool AllocateLogicalAddresses(CCECClient* client);
-
       bool                                        m_bInitialised;
       PLATFORM::CMutex                            m_mutex;
       IAdapterCommunication *                     m_communication;
@@ -158,5 +169,6 @@ namespace CEC
       CCECDeviceMap *                             m_busDevices;
       std::map<cec_logical_address, CCECClient *> m_clients;
       bool                                        m_bMonitor;
+      CCECAllocateLogicalAddress*                 m_addrAllocator;
   };
 };
index 22e00cbef5998f7a324639e3da320a14feb5f128..07026713042a19e934bc84af18d4060de42788cb 100644 (file)
@@ -78,9 +78,8 @@ namespace CEC
     /*!
      * @brief Callback method for IAdapterCommunication, called when a logical address that libCEC uses was taken by another device.
      * @param oldAddress The logical address that was taken by another device.
-     * @param newAddress The new logical address, or CECDEVICE_UNKNOWN if no new LA could be allocated.
      */
-    virtual void HandleLogicalAddressLost(cec_logical_address oldAddress, cec_logical_address newAddress) = 0;
+    virtual void HandleLogicalAddressLost(cec_logical_address oldAddress) = 0;
 
     virtual CLibCEC *GetLib(void) const = 0;
   };
index 9176647f9f02630e212fa5d7363f84a26798620d..79d73ba323562c9aae47f53deab0b287bb2c9f11 100644 (file)
@@ -58,42 +58,16 @@ void rpi_cec_callback(void *callback_data, uint32_t p0, uint32_t p1, uint32_t p2
     static_cast<CRPiCECAdapterCommunication *>(callback_data)->OnDataReceived(p0, p1, p2, p3, p4);
 }
 
-CRPiCECAdapterFindNewLogicalAddress::CRPiCECAdapterFindNewLogicalAddress(CRPiCECAdapterCommunication* communication, const cec_logical_address address) :
-    m_communication(communication),
-    m_address(address) { }
-
-void *CRPiCECAdapterFindNewLogicalAddress::Process(void)
-{
-  cec_logical_address newAddress(CECDEVICE_UNKNOWN);
-  for (unsigned int iLA = CECDEVICE_RECORDINGDEVICE1; newAddress == CECDEVICE_UNKNOWN && iLA < CECDEVICE_BROADCAST; iLA++)
-  {
-    if (CCECTypeUtils::GetType((cec_logical_address)iLA) == CCECTypeUtils::GetType(m_address) &&
-        m_communication->SupportsSourceLogicalAddress((cec_logical_address)iLA) &&
-        m_communication->RegisterLogicalAddress((cec_logical_address)iLA))
-      newAddress = (cec_logical_address)iLA;
-  }
-
-  m_communication->m_callback->HandleLogicalAddressLost(m_address, newAddress);
-  return NULL;
-}
-
 CRPiCECAdapterCommunication::CRPiCECAdapterCommunication(IAdapterCommunicationCallback *callback) :
     IAdapterCommunication(callback),
     m_logicalAddress(CECDEVICE_UNKNOWN),
-    m_bLogicalAddressChanged(false),
-    m_laLost(NULL)
+    m_bLogicalAddressChanged(false)
 {
   m_queue = new CRPiCECAdapterMessageQueue(this);
 }
 
 CRPiCECAdapterCommunication::~CRPiCECAdapterCommunication(void)
 {
-  if (m_laLost)
-  {
-    m_laLost->StopThread();
-    delete m_laLost;
-    m_laLost = NULL;
-  }
   delete(m_queue);
   Close();
 }
@@ -220,11 +194,7 @@ void CRPiCECAdapterCommunication::OnDataReceived(uint32_t header, uint32_t p0, u
       cec_logical_address previousAddress = m_logicalAddress;
       m_logicalAddress = CECDEVICE_UNKNOWN;
 
-      if (m_laLost && !m_laLost->IsRunning())
-        delete m_laLost;
-      m_laLost = new CRPiCECAdapterFindNewLogicalAddress(this, previousAddress);
-      if (m_laLost)
-        m_laLost->CreateThread();
+      m_callback->HandleLogicalAddressLost(previousAddress);
     }
     break;
   case VC_CEC_TOPOLOGY:
index c874451227ccd0ab503f3a735341995b91027765..7f815450c27b60a87e28cc4256d7b4ed80880acf 100644 (file)
@@ -44,22 +44,9 @@ extern "C" {
 namespace CEC
 {
   class CRPiCECAdapterMessageQueue;
-  class CRPiCECAdapterCommunication;
-
-  class CRPiCECAdapterFindNewLogicalAddress : public PLATFORM::CThread
-  {
-  public:
-    CRPiCECAdapterFindNewLogicalAddress(CRPiCECAdapterCommunication* communication, const cec_logical_address address);
-    void *Process(void);
-  private:
-    CRPiCECAdapterCommunication* m_communication;
-    cec_logical_address          m_address;
-  };
 
   class CRPiCECAdapterCommunication : public IAdapterCommunication
   {
-    friend class CRPiCECAdapterFindNewLogicalAddress;
-
   public:
     /*!
      * @brief Create a new USB-CEC communication handler.
@@ -115,7 +102,6 @@ namespace CEC
     PLATFORM::CMutex            m_mutex;
     VCHI_INSTANCE_T             m_vchi_instance;
     VCHI_CONNECTION_T *         m_vchi_connection;
-    CRPiCECAdapterFindNewLogicalAddress* m_laLost;
   };
 };