2b122602d79b4b13e7716924f0096b57b3e03953
[deb_libcec.git] / src / lib / adapter / USBCECAdapterMessage.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 "../platform/util/buffer.h"
36 #include "../platform/threads/mutex.h"
37 #include "../../../include/cectypes.h"
38
39 namespace CEC
40 {
41 typedef enum cec_adapter_message_state
42 {
43 ADAPTER_MESSAGE_STATE_UNKNOWN = 0, /**< the initial state */
44 ADAPTER_MESSAGE_STATE_WAITING_TO_BE_SENT, /**< waiting in the send queue of the adapter, or timed out */
45 ADAPTER_MESSAGE_STATE_SENT, /**< sent and waiting on an ACK */
46 ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED, /**< sent, but failed to ACK */
47 ADAPTER_MESSAGE_STATE_SENT_ACKED, /**< sent, and ACK received */
48 ADAPTER_MESSAGE_STATE_INCOMING, /**< received from another device */
49 ADAPTER_MESSAGE_STATE_ERROR /**< an error occured */
50 } cec_adapter_message_state;
51
52 class CCECAdapterMessage
53 {
54 public:
55 /*!
56 * @brief Create an empty message.
57 */
58 CCECAdapterMessage(void);
59
60 /*!
61 * @brief Create a message with a command that is to be transmitted over the CEC line.
62 * @param command The command to transmit.
63 * @param iMaxTries The maximum number of tries.
64 * @param iLineTimeout The line timeout to use when sending this message the first time.
65 * @param iRetryLineTimeout The line timeout to use when retrying to send this message.
66 */
67 CCECAdapterMessage(const cec_command &command, uint8_t iMaxTries = 1, uint8_t iLineTimeout = 3, uint8_t iRetryLineTimeout = 3);
68
69 /*!
70 * @return the message as human readable string.
71 */
72 CStdString ToString(void) const;
73
74 /*!
75 * @brief Translate the messagecode into a human readable string.
76 * @param msgCode The messagecode to translate.
77 * @return The messagecode as string.
78 */
79 static const char *ToString(cec_adapter_messagecode msgCode);
80
81 /*!
82 * @brief Get the byte at the given position.
83 * @param pos The position to get.
84 * @return The requested byte, or 0 when it's out of range.
85 */
86 uint8_t At(uint8_t pos) const;
87 uint8_t operator[](uint8_t pos) const;
88
89 /*!
90 * @return The size of the packet in bytes.
91 */
92 uint8_t Size(void) const;
93
94 /*!
95 * @return True when empty, false otherwise.
96 */
97 bool IsEmpty(void) const;
98
99 /*!
100 * @brief Clear this message and reset everything to the initial values.
101 */
102 void Clear(void);
103
104 /*!
105 * @brief Shift the message by the given number of bytes.
106 * @param iShiftBy The number of bytes to shift.
107 */
108 void Shift(uint8_t iShiftBy);
109
110 /*!
111 * @brief Append the given message to this message.
112 * @param data The message to append.
113 */
114 void Append(CCECAdapterMessage &data);
115
116 /*!
117 * @brief Append the given datapacket to this message.
118 * @param data The packet to add.
119 */
120 void Append(cec_datapacket &data);
121
122 /*!
123 * @brief Adds a byte to this message. Does not escape the byte.
124 * @param byte The byte to add.
125 */
126 void PushBack(uint8_t byte);
127
128 /*!
129 * @brief Adds a byte to this message and escapes the byte if needed.
130 * @param byte The byte to add.
131 */
132 void PushEscaped(uint8_t byte);
133
134 /*!
135 * @brief Adds a byte to this message.
136 * @param byte The byte to add.
137 * @return True when a full message was received, false otherwise.
138 */
139 bool PushReceivedByte(uint8_t byte);
140
141 /*!
142 * @return The messagecode inside this adapter message, or MSGCODE_NOTHING if there is none.
143 */
144 cec_adapter_messagecode Message(void) const;
145
146 /*!
147 * @return True when this message is a transmission, false otherwise.
148 */
149 bool IsTranmission(void) const;
150
151 /*!
152 * @return True when the EOM bit is set, false otherwise.
153 */
154 bool IsEOM(void) const;
155
156 /*!
157 * @return True when the ACK bit is set, false otherwise.
158 */
159 bool IsACK(void) const;
160
161 /*!
162 * @return True when this message has been replied with an error code, false otherwise.
163 */
164 bool IsError(void) const;
165
166 /*!
167 * @return True when this message has been replied with an error code and needs to be retried, false otherwise.
168 */
169 bool NeedsRetry(void) const;
170
171 /*!
172 * @return The logical address of the initiator, or CECDEVICE_UNKNOWN if unknown.
173 */
174 cec_logical_address Initiator(void) const;
175
176 /*!
177 * @return The logical address of the destination, or CECDEVICE_UNKNOWN if unknown.
178 */
179 cec_logical_address Destination(void) const;
180
181 /*!
182 * @return True when this message contains a start message, false otherwise.
183 */
184 bool HasStartMessage(void) const;
185
186 /*!
187 * @brief Push this adapter message to the end of the given cec_command.
188 * @param command The command to push this message to.
189 * @return True when a full CEC message was received, false otherwise.
190 */
191 bool PushToCecCommand(cec_command &command) const;
192
193 /*!
194 * @return The response messagecode.
195 */
196 cec_adapter_messagecode Reply(void) const;
197
198 uint8_t maxTries; /**< the maximum number of times to try to send this message */
199 uint8_t tries; /**< the amount of times this message has been sent */
200 cec_datapacket response; /**< the response to this message */
201 cec_datapacket packet; /**< the actual data */
202 cec_adapter_message_state state; /**< the current state of this message */
203 int32_t transmit_timeout; /**< the timeout to use when sending this message */
204 uint8_t lineTimeout; /**< the default CEC line timeout to use when sending this message */
205 uint8_t retryTimeout; /**< the CEC line timeout to use when retrying to send this message */
206
207 private:
208 bool bNextByteIsEscaped; /**< true when the next byte that is added will be escaped, false otherwise */
209 };
210 }