Don't send commands if we know them to be unsupported. bugzid: 725
[deb_libcec.git] / src / lib / adapter / AdapterCommunication.h
1 #pragma once
2 /*
3 * This file is part of the libCEC(R) library.
4 *
5 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
6 * libCEC(R) is an original work, containing original code.
7 *
8 * libCEC(R) is a trademark of Pulse-Eight Limited.
9 *
10 * This program is dual-licensed; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 *
24 *
25 * Alternatively, you can license this library under a commercial license,
26 * please contact Pulse-Eight Licensing for more information.
27 *
28 * For more information contact:
29 * Pulse-Eight Licensing <license@pulse-eight.com>
30 * http://www.pulse-eight.com/
31 * http://www.pulse-eight.net/
32 */
33
34 #include "../platform/util/StdString.h"
35 #include "USBCECAdapterMessage.h"
36
37 namespace CEC
38 {
39 class IAdapterCommunicationCallback
40 {
41 public:
42 IAdapterCommunicationCallback(void) {}
43 virtual ~IAdapterCommunicationCallback(void) {}
44
45 /*!
46 * @brief Callback method for IAdapterCommunication, called when a new cec_command is received
47 * @param command The command that has been received
48 * @return True when it was handled by this listener, false otherwise.
49 */
50 virtual bool OnCommandReceived(const cec_command &command) = 0;
51
52 /*!
53 * @brief Callback method for IAdapterCommunication, called when a poll was received.
54 * @param initiator The initiator that sent the poll.
55 * @param destination The destination of the poll message.
56 */
57 virtual void HandlePoll(cec_logical_address initiator, cec_logical_address destination) = 0;
58
59 /*!
60 * @brief Callback method for IAdapterCommunication, called when a receive failed message was received.
61 * @param initiator The initiator that sent the receive failed message.
62 * @return True when this is an error, false otherwise.
63 */
64 virtual bool HandleReceiveFailed(cec_logical_address initiator) = 0;
65 };
66
67 class IAdapterCommunication
68 {
69 public:
70 /*!
71 * @param callback The callback struct. if set to NULL, the Read() method has to be used to read commands. if set, OnCommandReceived() will be called for each command that was received
72 */
73 IAdapterCommunication(IAdapterCommunicationCallback *callback) :
74 m_callback(callback) {}
75 virtual ~IAdapterCommunication(void) {}
76
77 /*!
78 * @brief Open a connection to the CEC adapter
79 * @param iTimeoutMs Connection timeout in ms
80 * @param bSkipChecks Skips all initial checks of the adapter, and starts the reader/writer threads directly after connecting.
81 * @param bStartListening Start a listener thread when true. False to just open a connection, read the device info, and close the connection.
82 * @return True when connected, false otherwise
83 */
84 virtual bool Open(uint32_t iTimeoutMs = 10000, bool bSkipChecks = false, bool bStartListening = true) = 0;
85
86 /*!
87 * @brief Close an open connection
88 */
89 virtual void Close(void) = 0;
90
91 /*!
92 * @return True when the connection is open, false otherwise
93 */
94 virtual bool IsOpen(void) = 0;
95
96 /*!
97 * @return The last error message, or an empty string if there was none
98 */
99 virtual CStdString GetError(void) const = 0;
100
101 /*!
102 * @brief Write a cec_command to the adapter
103 * @param data The command to write
104 * @param bRetry The command can be retried
105 * @param iLineTimeout The line timeout to be used
106 * @return The last state of the transmitted command
107 */
108 virtual cec_adapter_message_state Write(const cec_command &data, bool &bRetry, uint8_t iLineTimeout = 3) = 0;
109
110 /*!
111 * @brief Change the current line timeout on the CEC bus
112 * @param iTimeout The new timeout
113 * @return True when set, false otherwise
114 */
115 virtual bool SetLineTimeout(uint8_t iTimeout) = 0;
116
117 /*!
118 * @brief Put the device in bootloader mode (which will disrupt CEC communication when it succeeds)
119 * @return True when the bootloader command has been sent, false otherwise.
120 */
121 virtual bool StartBootloader(void) = 0;
122
123 /*!
124 * @brief Change the ACK-mask of the device, the mask for logical addresses to which the CEC device should ACK
125 * @param iMask The new mask
126 * @return True when set, false otherwise.
127 */
128 virtual bool SetAckMask(uint16_t iMask) = 0;
129
130 /*!
131 * @brief Check whether the CEC adapter responds
132 * @return True when the ping was sent and acked, false otherwise.
133 */
134 virtual bool PingAdapter(void) = 0;
135
136 /*!
137 * @return The firmware version of this CEC adapter, or 0 if it's unknown.
138 */
139 virtual uint16_t GetFirmwareVersion(void) = 0;
140
141 /*!
142 * @return True when the control mode has been set, false otherwise.
143 */
144 virtual bool SetControlledMode(bool controlled) = 0;
145
146 /*!
147 * @brief Persist the given configuration in adapter (if supported)
148 * @brief The configuration to store.
149 * @return True when the configuration was persisted, false otherwise.
150 */
151 virtual bool PersistConfiguration(libcec_configuration *configuration) = 0;
152
153 /*!
154 * @brief Get the persisted configuration from the adapter (if supported)
155 * @param configuration The updated configuration.
156 * @return True when the configuration was updated, false otherwise.
157 */
158 virtual bool GetConfiguration(libcec_configuration *configuration) = 0;
159
160 /*!
161 * @return The name of the port
162 */
163 virtual CStdString GetPortName(void) = 0;
164
165 /*!
166 * @return The physical address, if the adapter supports this. 0 otherwise.
167 */
168 virtual uint16_t GetPhysicalAddress(void) = 0;
169
170 protected:
171 IAdapterCommunicationCallback *m_callback;
172 };
173 };