3 * This file is part of the libCEC(R) library.
5 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
6 * libCEC(R) is an original work, containing original code.
8 * libCEC(R) is a trademark of Pulse-Eight Limited.
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.
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.
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.
25 * Alternatively, you can license this library under a commercial license,
26 * please contact Pulse-Eight Licensing for more information.
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/
34 #include "../platform/util/StdString.h"
35 #include "../platform/util/buffer.h"
36 #include "../platform/threads/mutex.h"
37 #include "../../../include/cectypes.h"
41 typedef enum cec_adapter_message_state
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
;
52 class CCECAdapterMessage
56 * @brief Create an empty message.
58 CCECAdapterMessage(void);
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.
67 CCECAdapterMessage(const cec_command
&command
, uint8_t iMaxTries
= 1, uint8_t iLineTimeout
= 3, uint8_t iRetryLineTimeout
= 3);
70 * @return the message as human readable string.
72 CStdString
ToString(void) const;
75 * @brief Translate the messagecode into a human readable string.
76 * @param msgCode The messagecode to translate.
77 * @return The messagecode as string.
79 static const char *ToString(cec_adapter_messagecode msgCode
);
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.
86 uint8_t At(uint8_t pos
) const;
87 uint8_t operator[](uint8_t pos
) const;
90 * @return The size of the packet in bytes.
92 uint8_t Size(void) const;
95 * @return True when empty, false otherwise.
97 bool IsEmpty(void) const;
100 * @brief Clear this message and reset everything to the initial values.
105 * @brief Shift the message by the given number of bytes.
106 * @param iShiftBy The number of bytes to shift.
108 void Shift(uint8_t iShiftBy
);
111 * @brief Append the given message to this message.
112 * @param data The message to append.
114 void Append(CCECAdapterMessage
&data
);
117 * @brief Append the given datapacket to this message.
118 * @param data The packet to add.
120 void Append(cec_datapacket
&data
);
123 * @brief Adds a byte to this message. Does not escape the byte.
124 * @param byte The byte to add.
126 void PushBack(uint8_t byte
);
129 * @brief Adds a byte to this message and escapes the byte if needed.
130 * @param byte The byte to add.
132 void PushEscaped(uint8_t byte
);
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.
139 bool PushReceivedByte(uint8_t byte
);
142 * @return The messagecode inside this adapter message, or MSGCODE_NOTHING if there is none.
144 cec_adapter_messagecode
Message(void) const;
147 * @return True when this message is a transmission, false otherwise.
149 bool IsTranmission(void) const;
152 * @return True when the EOM bit is set, false otherwise.
154 bool IsEOM(void) const;
157 * @return True when the ACK bit is set, false otherwise.
159 bool IsACK(void) const;
162 * @return True when this message has been replied with an error code, false otherwise.
164 bool IsError(void) const;
167 * @return True when this message has been replied with an error code and needs to be retried, false otherwise.
169 bool NeedsRetry(void) const;
172 * @return The logical address of the initiator, or CECDEVICE_UNKNOWN if unknown.
174 cec_logical_address
Initiator(void) const;
177 * @return The logical address of the destination, or CECDEVICE_UNKNOWN if unknown.
179 cec_logical_address
Destination(void) const;
182 * @return True when this message contains a start message, false otherwise.
184 bool HasStartMessage(void) const;
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.
191 bool PushToCecCommand(cec_command
&command
) const;
194 * @return The response messagecode.
196 cec_adapter_messagecode
Reply(void) const;
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 */
208 bool bNextByteIsEscaped
; /**< true when the next byte that is added will be escaped, false otherwise */