cec: receive and send full cec_commands, not CCECAdapterMessages in CAdapterCommunica...
[deb_libcec.git] / src / lib / adapter / AdapterCommunication.h
index 54f30cef3265b8ff32d43b0f6f3f236e3632d38c..cec8501a0c3e22a051fee4e3709dd758281577bf 100644 (file)
  *     http://www.pulse-eight.net/
  */
 
-#include <cectypes.h>
-#include "../platform/threads/threads.h"
-#include "../platform/util/buffer.h"
 #include "../platform/util/StdString.h"
 
-namespace PLATFORM
-{
-  class ISocket;
-}
-
 namespace CEC
 {
-  class CCECProcessor;
-  class CCECAdapterMessage;
+  typedef enum cec_adapter_message_state
+  {
+    ADAPTER_MESSAGE_STATE_UNKNOWN = 0,          /**< the initial state */
+    ADAPTER_MESSAGE_STATE_WAITING_TO_BE_SENT,   /**< waiting in the send queue of the adapter, or timed out */
+    ADAPTER_MESSAGE_STATE_SENT,                 /**< sent and waiting on an ACK */
+    ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED,       /**< sent, but failed to ACK */
+    ADAPTER_MESSAGE_STATE_SENT_ACKED,           /**< sent, and ACK received */
+    ADAPTER_MESSAGE_STATE_INCOMING,             /**< received from another device */
+    ADAPTER_MESSAGE_STATE_ERROR                 /**< an error occured */
+  } cec_adapter_message_state;
 
-  class CAdapterCommunication : private PLATFORM::CThread
+  class IAdapterCommunication
   {
   public:
-    CAdapterCommunication(CCECProcessor *processor, const char *strPort, uint16_t iBaudRate = 38400);
-    virtual ~CAdapterCommunication();
+    /*!
+     * @brief Open a connection to the CEC adapter
+     * @param iTimeoutMs Connection timeout in ms
+     * @return True when connected, false otherwise
+     */
+    virtual bool Open(uint32_t iTimeoutMs = 10000) = 0;
+
+    /*!
+     * @brief Close an open connection
+     */
+    virtual void Close(void) = 0;
+
+    /*!
+     * @return True when the connection is open, false otherwise
+     */
+    virtual bool IsOpen(void) = 0;
+
+    /*!
+     * @return The last error message, or an empty string if there was none
+     */
+    virtual CStdString GetError(void) const = 0;
+
+    /*!
+     * @brief Reads one cec_command from the adapter
+     * @param command The command that will be read (output)
+     * @param iTimeout The read timeout
+     * @return True when a command has been read, false otherwise.
+     */
+    virtual bool Read(cec_command &command, uint32_t iTimeout) = 0;
 
-    bool Open(uint32_t iTimeoutMs = 10000);
-    bool Read(CCECAdapterMessage &msg, uint32_t iTimeout = 1000);
-    bool Write(CCECAdapterMessage *data);
-    void Close(void);
-    bool IsOpen(void);
-    CStdString GetError(void) const;
+    /*!
+     * @brief Write a cec_command to the adapter
+     * @param data The command to write
+     * @param iMaxTries The maximum number of tries
+     * @param iLineTimeout The line timeout for the first try
+     * @param iRetryLineTimeout The line timeout for each next try
+     * @return The last state of the transmitted command
+     */
+    virtual cec_adapter_message_state Write(const cec_command &data, uint8_t iMaxTries, uint8_t iLineTimeout = 3, uint8_t iRetryLineTimeout = 3) = 0;
 
-    void *Process(void);
+    /*!
+     * @brief Change the current line timeout on the CEC bus
+     * @param iTimeout The new timeout
+     * @return True when set, false otherwise
+     */
+    virtual bool SetLineTimeout(uint8_t iTimeout) = 0;
 
-    bool SetLineTimeout(uint8_t iTimeout);
-    bool StartBootloader(void);
-    bool SetAckMask(uint16_t iMask);
-    bool PingAdapter(void);
-    uint16_t GetFirmwareVersion(void);
+    /*!
+     * @brief Put the device in bootloader mode (which will disrupt CEC communication when it succeeds)
+     * @return True when the bootloader command has been sent, false otherwise.
+     */
+    virtual bool StartBootloader(void) = 0;
 
-    bool WaitForAck(CCECAdapterMessage &message);
+    /*!
+     * @brief Change the ACK-mask of the device, the mask for logical addresses to which the CEC device should ACK
+     * @param iMask The new mask
+     * @return True when set, false otherwise.
+     */
+    virtual bool SetAckMask(uint16_t iMask) = 0;
 
-  private:
-    void SendMessageToAdapter(CCECAdapterMessage *msg);
-    void WriteNextCommand(void);
-    void AddData(uint8_t *data, size_t iLen);
-    bool ReadFromDevice(uint32_t iTimeout);
+    /*!
+     * @brief Check whether the CEC adapter responds
+     * @return True when the ping was sent and acked, false otherwise.
+     */
+    virtual bool PingAdapter(void) = 0;
 
-    PLATFORM::ISocket *                          m_port;
-    CCECProcessor *                              m_processor;
-    PLATFORM::SyncedBuffer<uint8_t>              m_inBuffer;
-    PLATFORM::SyncedBuffer<CCECAdapterMessage *> m_outBuffer;
-    PLATFORM::CMutex                             m_mutex;
-    PLATFORM::CCondition                         m_rcvCondition;
-    uint8_t                                      m_iLineTimeout;
-    uint16_t                                     m_iFirmwareVersion;
+    /*!
+     * @return The firmware version of this CEC adapter.
+     */
+    virtual uint16_t GetFirmwareVersion(void) = 0;
   };
 };