added protection against standby without a notification from XBMC and clock changes...
[deb_libcec.git] / src / lib / CECProcessor.h
index b166b5f55879f9fb680526cc686e0352d83d0bce..44f1fc170bf1d22d674e7d057c44fa4024b5afb6 100644 (file)
@@ -2,7 +2,7 @@
 /*
  * This file is part of the libCEC(R) library.
  *
- * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited.  All rights reserved.
+ * libCEC(R) is Copyright (C) 2011-2013 Pulse-Eight Limited.  All rights reserved.
  * libCEC(R) is an original work, containing original code.
  *
  * libCEC(R) is a trademark of Pulse-Eight Limited.
  */
 
 #include <string>
-#include <cectypes.h>
-#include "AdapterCommunication.h"
-#include "platform/threads.h"
-#include "util/buffer.h"
-#include "util/StdString.h"
 
-class CSerialPort;
+#include "platform/threads/threads.h"
+#include "platform/util/buffer.h"
+
+#include "adapter/AdapterCommunication.h"
+#include "devices/CECDeviceMap.h"
+#include "CECInputBuffer.h"
 
 namespace CEC
 {
   class CLibCEC;
-  class CAdapterCommunication;
+  class IAdapterCommunication;
   class CCECBusDevice;
+  class CCECAudioSystem;
+  class CCECPlaybackDevice;
+  class CCECRecordingDevice;
+  class CCECTuner;
+  class CCECTV;
+  class CCECClient;
+  class CCECProcessor;
+  class CCECStandbyProtection;
+
+  class CCECAllocateLogicalAddress : public PLATFORM::CThread
+  {
+  public:
+    CCECAllocateLogicalAddress(CCECProcessor* processor, CCECClient* client);
+    void* Process(void);
+
+  private:
+    CCECProcessor* m_processor;
+    CCECClient*    m_client;
+  };
 
-  class CCECProcessor : public CThread
+  class CCECProcessor : public PLATFORM::CThread, public IAdapterCommunicationCallback
   {
     public:
-      CCECProcessor(CLibCEC *controller, CAdapterCommunication *serComm, const char *strDeviceName, cec_logical_address iLogicalAddress = CECDEVICE_PLAYBACKDEVICE1, uint16_t iPhysicalAddress = CEC_DEFAULT_PHYSICAL_ADDRESS);
-      CCECProcessor(CLibCEC *controller, CAdapterCommunication *serComm, const char *strDeviceName, const cec_device_type_list &types);
+      CCECProcessor(CLibCEC *libcec);
       virtual ~CCECProcessor(void);
 
-      virtual bool Start(void);
-      virtual void *Process(void);
-
-      virtual bool                  IsMonitoring(void) const { return m_bMonitor; }
-      virtual CCECBusDevice *       GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress) const;
-      virtual cec_version           GetDeviceCecVersion(cec_logical_address iAddress);
-      virtual bool                  GetDeviceMenuLanguage(cec_logical_address iAddress, cec_menu_language *language);
-      virtual const std::string &   GetDeviceName(void) { return m_strDeviceName; }
-      virtual uint64_t              GetDeviceVendorId(cec_logical_address iAddress);
-      virtual cec_power_status      GetDevicePowerStatus(cec_logical_address iAddress);
-      virtual cec_logical_address   GetLogicalAddress(void) const { return m_logicalAddresses.primary; }
-      virtual cec_logical_addresses GetLogicalAddresses(void) const { return m_logicalAddresses; }
-      virtual bool                  HasLogicalAddress(cec_logical_address address) const { return m_logicalAddresses.isset(address); }
-      virtual uint16_t              GetPhysicalAddress(void) const;
-
-      virtual bool SetActiveView(void);
-      virtual bool SetActiveSource(cec_device_type type = CEC_DEVICE_TYPE_RESERVED);
-      virtual bool SetInactiveView(void);
-      virtual bool SetLogicalAddress(cec_logical_address iLogicalAddress);
-      virtual bool SetPhysicalAddress(uint16_t iPhysicalAddress);
-      virtual bool SetStreamPath(uint16_t iStreamPath);
-      virtual bool SwitchMonitoring(bool bEnable);
-      virtual bool PollDevice(cec_logical_address iAddress);
-
-      virtual bool Transmit(const cec_command &data);
-      virtual bool Transmit(CCECAdapterMessage *output);
-      virtual void TransmitAbort(cec_logical_address address, cec_opcode opcode, cec_abort_reason reason = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE);
-
-      virtual void SetCurrentButton(cec_user_control_code iButtonCode);
-      virtual void AddCommand(const cec_command &command);
-      virtual void AddKey(cec_keypress &key);
-      virtual void AddKey(void);
-      virtual void AddLog(cec_log_level level, const CStdString &strMessage);
-
-      virtual bool FindLogicalAddresses(void);
-
-      CCECBusDevice *m_busDevices[16];
+      bool Start(const char *strPort, uint16_t iBaudRate = CEC_SERIAL_DEFAULT_BAUDRATE, uint32_t iTimeoutMs = CEC_DEFAULT_CONNECT_TIMEOUT);
+      void *Process(void);
+      void Close(void);
 
-  private:
-      bool TryLogicalAddress(cec_logical_address address, unsigned int iIndex);
-      bool FindLogicalAddressRecordingDevice(unsigned int iIndex);
-      bool FindLogicalAddressTuner(unsigned int iIndex);
-      bool FindLogicalAddressPlaybackDevice(unsigned int iIndex);
-      bool FindLogicalAddressAudioSystem(unsigned int iIndex);
+      bool RegisterClient(CCECClient *client);
+      bool UnregisterClient(CCECClient *client);
+      void UnregisterClients(void);
+      uint16_t GetPhysicalAddressFromEeprom(void);
+      CCECClient *GetPrimaryClient(void);
+      CCECClient *GetClient(const cec_logical_address address);
+
+      bool                  OnCommandReceived(const cec_command &command);
+      void                  HandleLogicalAddressLost(cec_logical_address oldAddress);
+      void                  HandlePhysicalAddressChanged(uint16_t iNewAddress);
+
+      CCECBusDevice *       GetDevice(cec_logical_address address) const;
+      CCECAudioSystem *     GetAudioSystem(void) const;
+      CCECPlaybackDevice *  GetPlaybackDevice(cec_logical_address address) const;
+      CCECRecordingDevice * GetRecordingDevice(cec_logical_address address) const;
+      CCECTuner *           GetTuner(cec_logical_address address) const;
+      CCECTV *              GetTV(void) const;
+
+      CCECBusDevice *       GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress, bool bSuppressUpdate = true);
+      CCECBusDevice *       GetPrimaryDevice(void);
+      cec_logical_address   GetLogicalAddress(void);
+      cec_logical_addresses GetLogicalAddresses(void);
+      bool                  IsPresentDevice(cec_logical_address address);
+      bool                  IsPresentDeviceType(cec_device_type type);
+      uint16_t              GetDetectedPhysicalAddress(void) const;
+      uint64_t              GetLastTransmission(void) const { return m_iLastTransmission; }
+      cec_logical_address   GetActiveSource(bool bRequestActiveSource = true);
+      bool                  IsActiveSource(cec_logical_address iAddress);
+      bool                  CECInitialised(void);
+
+      bool                  StandbyDevices(const cec_logical_address initiator, const CECDEVICEVEC &devices);
+      bool                  StandbyDevice(const cec_logical_address initiator, cec_logical_address address);
+      bool                  PowerOnDevices(const cec_logical_address initiator, const CECDEVICEVEC &devices);
+      bool                  PowerOnDevice(const cec_logical_address initiator, cec_logical_address address);
+
+      bool SetDeckInfo(cec_deck_info info, bool bSendUpdate = true);
+      bool ActivateSource(uint16_t iStreamPath);
+      void SetActiveSource(bool bSetTo, bool bClientUnregistered);
+      bool PollDevice(cec_logical_address iAddress);
+      void SetStandardLineTimeout(uint8_t iTimeout);
+      uint8_t GetStandardLineTimeout(void);
+      void SetRetryLineTimeout(uint8_t iTimeout);
+      uint8_t GetRetryLineTimeout(void);
+      bool CanPersistConfiguration(void);
+      bool PersistConfiguration(const libcec_configuration &configuration);
+      void RescanActiveDevices(void);
+
+      bool SetLineTimeout(uint8_t iTimeout);
+
+      bool Transmit(const cec_command &data, bool bIsReply);
+      void TransmitAbort(cec_logical_address source, cec_logical_address destination, cec_opcode opcode, cec_abort_reason reason = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE);
+
+      bool StartBootloader(const char *strPort = NULL);
+      bool PingAdapter(void);
+      void HandlePoll(cec_logical_address initiator, cec_logical_address destination);
+      bool HandleReceiveFailed(cec_logical_address initiator);
+
+      bool GetDeviceInformation(const char *strPort, libcec_configuration *config, uint32_t iTimeoutMs = CEC_DEFAULT_CONNECT_TIMEOUT);
+
+      bool TransmitPendingActiveSourceCommands(void);
+
+      CCECDeviceMap *GetDevices(void) const { return m_busDevices; }
+      CLibCEC *GetLib(void) const { return m_libcec; }
+
+      bool IsHandledByLibCEC(const cec_logical_address address) const;
+
+      bool TryLogicalAddress(cec_logical_address address, cec_version libCECSpecVersion = CEC_VERSION_1_4);
+
+      bool IsRunningLatestFirmware(void);
+      void SwitchMonitoring(bool bSwitchTo);
+
+      bool AllocateLogicalAddresses(CCECClient* client);
+
+      uint16_t GetAdapterVendorId(void) const;
+      uint16_t GetAdapterProductId(void) const;
+    private:
+      bool OpenConnection(const char *strPort, uint16_t iBaudRate, uint32_t iTimeoutMs, bool bStartListening = true);
+      void SetCECInitialised(bool bSetTo = true);
+
+      void ReplaceHandlers(void);
+      bool PhysicalAddressInUse(uint16_t iPhysicalAddress);
+
+      bool ClearLogicalAddresses(void);
+      bool SetLogicalAddresses(const cec_logical_addresses &addresses);
 
-      bool SetAckMask(uint16_t iMask);
       void LogOutput(const cec_command &data);
-      bool WaitForTransmitSucceeded(uint8_t iLength, uint32_t iTimeout = 1000);
-      bool ParseMessage(const CCECAdapterMessage &msg);
-      void ParseCommand(cec_command &command);
-
-      bool                   m_bStarted;
-      cec_command            m_currentframe;
-      cec_logical_addresses  m_logicalAddresses;
-      std::string            m_strDeviceName;
-      cec_device_type_list   m_types;
-      CMutex                 m_mutex;
-      CCondition             m_startCondition;
-      CAdapterCommunication* m_communication;
-      CLibCEC*               m_controller;
-      bool                   m_bMonitor;
-      CecBuffer<cec_command> m_commandBuffer;
+      void ProcessCommand(const cec_command &command);
+
+      void ResetMembers(void);
+
+      bool                                        m_bInitialised;
+      PLATFORM::CMutex                            m_mutex;
+      IAdapterCommunication *                     m_communication;
+      CLibCEC*                                    m_libcec;
+      uint8_t                                     m_iStandardLineTimeout;
+      uint8_t                                     m_iRetryLineTimeout;
+      uint64_t                                    m_iLastTransmission;
+      CCECInputBuffer                             m_inBuffer;
+      CCECDeviceMap *                             m_busDevices;
+      std::map<cec_logical_address, CCECClient *> m_clients;
+      bool                                        m_bMonitor;
+      CCECAllocateLogicalAddress*                 m_addrAllocator;
+      bool                                        m_bStallCommunication;
+      CCECStandbyProtection*                      m_connCheck;
+  };
+
+  class CCECStandbyProtection : public PLATFORM::CThread
+  {
+  public:
+    CCECStandbyProtection(CCECProcessor* processor);
+    virtual ~CCECStandbyProtection(void);
+    void* Process(void);
+
+  private:
+    CCECProcessor* m_processor;
   };
 };