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"
38 class CCECAdapterMessage
41 CCECAdapterMessage(void) :
47 CCECAdapterMessage(const cec_command
&command
)
51 //set ack polarity to high when transmitting to the broadcast address
52 //set ack polarity low when transmitting to any other address
54 PushEscaped(MSGCODE_TRANSMIT_ACK_POLARITY
);
55 if (command
.destination
== CECDEVICE_BROADCAST
)
56 PushEscaped(CEC_TRUE
);
58 PushEscaped(CEC_FALSE
);
61 // add source and destination
63 PushEscaped(command
.opcode_set
== 0 ? (uint8_t)MSGCODE_TRANSMIT_EOM
: (uint8_t)MSGCODE_TRANSMIT
);
64 PushBack(((uint8_t)command
.initiator
<< 4) + (uint8_t)command
.destination
);
68 if (command
.opcode_set
== 1)
71 PushEscaped(command
.parameters
.IsEmpty() ? (uint8_t)MSGCODE_TRANSMIT_EOM
: (uint8_t)MSGCODE_TRANSMIT
);
72 PushBack((uint8_t) command
.opcode
);
76 for (int8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
80 if (iPtr
== command
.parameters
.size
- 1)
81 PushEscaped( MSGCODE_TRANSMIT_EOM
);
83 PushEscaped(MSGCODE_TRANSMIT
);
85 PushEscaped(command
.parameters
[iPtr
]);
92 transmit_timeout
= command
.transmit_timeout
;
96 CCECAdapterMessage
&operator=(const CCECAdapterMessage
&msg
)
103 CStdString
ToString(void) const
108 strMsg
= "empty message";
112 strMsg
= MessageCodeAsString();
116 case MSGCODE_TIMEOUT_ERROR
:
117 case MSGCODE_HIGH_ERROR
:
118 case MSGCODE_LOW_ERROR
:
120 uint32_t iLine
= (Size() >= 3) ? (At(1) << 8) | At(2) : 0;
121 uint32_t iTime
= (Size() >= 7) ? (At(3) << 24) | (At(4) << 16) | (At(5) << 8) | At(6) : 0;
122 strMsg
.AppendFormat(" line:%u", iLine
);
123 strMsg
.AppendFormat(" time:%u", iTime
);
126 case MSGCODE_FRAME_START
:
128 strMsg
.AppendFormat(" initiator:%1x destination:%1x ack:%s %s", Initiator(), Destination(), IsACK() ? "high" : "low", IsEOM() ? "eom" : "");
130 case MSGCODE_FRAME_DATA
:
132 strMsg
.AppendFormat(" %02x %s", At(1), IsEOM() ? "eom" : "");
142 CStdString
MessageCodeAsString(void) const
147 case MSGCODE_NOTHING
:
153 case MSGCODE_TIMEOUT_ERROR
:
156 case MSGCODE_HIGH_ERROR
:
157 strMsg
= "HIGH_ERROR";
159 case MSGCODE_LOW_ERROR
:
160 strMsg
= "LOW_ERROR";
162 case MSGCODE_FRAME_START
:
163 strMsg
= "FRAME_START";
165 case MSGCODE_FRAME_DATA
:
166 strMsg
= "FRAME_DATA";
168 case MSGCODE_RECEIVE_FAILED
:
169 strMsg
= "RECEIVE_FAILED";
171 case MSGCODE_COMMAND_ACCEPTED
:
172 strMsg
= "COMMAND_ACCEPTED";
174 case MSGCODE_COMMAND_REJECTED
:
175 strMsg
= "COMMAND_REJECTED";
177 case MSGCODE_SET_ACK_MASK
:
178 strMsg
= "SET_ACK_MASK";
180 case MSGCODE_TRANSMIT
:
183 case MSGCODE_TRANSMIT_EOM
:
184 strMsg
= "TRANSMIT_EOM";
186 case MSGCODE_TRANSMIT_IDLETIME
:
187 strMsg
= "TRANSMIT_IDLETIME";
189 case MSGCODE_TRANSMIT_ACK_POLARITY
:
190 strMsg
= "TRANSMIT_ACK_POLARITY";
192 case MSGCODE_TRANSMIT_LINE_TIMEOUT
:
193 strMsg
= "TRANSMIT_LINE_TIMEOUT";
195 case MSGCODE_TRANSMIT_SUCCEEDED
:
196 strMsg
= "TRANSMIT_SUCCEEDED";
198 case MSGCODE_TRANSMIT_FAILED_LINE
:
199 strMsg
= "TRANSMIT_FAILED_LINE";
201 case MSGCODE_TRANSMIT_FAILED_ACK
:
202 strMsg
= "TRANSMIT_FAILED_ACK";
204 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
:
205 strMsg
= "TRANSMIT_FAILED_TIMEOUT_DATA";
207 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
:
208 strMsg
= "TRANSMIT_FAILED_TIMEOUT_LINE";
210 case MSGCODE_FIRMWARE_VERSION
:
211 strMsg
= "FIRMWARE_VERSION";
213 case MSGCODE_START_BOOTLOADER
:
214 strMsg
= "START_BOOTLOADER";
216 case MSGCODE_FRAME_EOM
:
217 strMsg
= "FRAME_EOM";
219 case MSGCODE_FRAME_ACK
:
220 strMsg
= "FRAME_ACK";
222 case MSGCODE_SET_POWERSTATE
:
223 strMsg
= "SET_POWERSTATE";
225 case MSGCODE_SET_CONTROLLED
:
226 strMsg
= "SET_CONTROLLED";
233 uint8_t operator[](uint8_t pos
) const
238 uint8_t At(uint8_t pos
) const
243 uint8_t Size(void) const
248 bool IsEmpty(void) const
250 return packet
.IsEmpty();
255 state
= ADAPTER_MESSAGE_STATE_UNKNOWN
;
256 transmit_timeout
= CEC_DEFAULT_TRANSMIT_TIMEOUT
;
258 maxTries
= CEC_DEFAULT_TRANSMIT_RETRIES
+ 1;
260 reply
= MSGCODE_NOTHING
;
261 isTransmission
= true;
262 expectControllerAck
= true;
267 void Shift(uint8_t iShiftBy
)
269 packet
.Shift(iShiftBy
);
272 void PushBack(uint8_t add
)
274 packet
.PushBack(add
);
277 void PushEscaped(uint8_t byte
)
282 PushBack(byte
- ESCOFFSET
);
290 cec_adapter_messagecode
Message(void) const
292 return packet
.size
>= 1 ?
293 (cec_adapter_messagecode
) (packet
.At(0) & ~(MSGCODE_FRAME_EOM
| MSGCODE_FRAME_ACK
)) :
297 bool IsEOM(void) const
299 return packet
.size
>= 1 ?
300 (packet
.At(0) & MSGCODE_FRAME_EOM
) != 0 :
304 bool IsACK(void) const
306 return packet
.size
>= 1 ?
307 (packet
.At(0) & MSGCODE_FRAME_ACK
) != 0 :
311 bool IsError(void) const
313 cec_adapter_messagecode code
= Message();
314 return (code
== MSGCODE_HIGH_ERROR
||
315 code
== MSGCODE_LOW_ERROR
||
316 code
== MSGCODE_RECEIVE_FAILED
||
317 code
== MSGCODE_COMMAND_REJECTED
||
318 code
== MSGCODE_TRANSMIT_LINE_TIMEOUT
||
319 code
== MSGCODE_TRANSMIT_FAILED_LINE
||
320 code
== MSGCODE_TRANSMIT_FAILED_ACK
||
321 code
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
||
322 code
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
);
325 bool NeedsRetry(void) const
327 return reply
== MSGCODE_NOTHING
||
328 reply
== MSGCODE_RECEIVE_FAILED
||
329 reply
== MSGCODE_TIMEOUT_ERROR
||
330 reply
== MSGCODE_TRANSMIT_FAILED_LINE
||
331 reply
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
||
332 reply
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
||
333 reply
== MSGCODE_TRANSMIT_LINE_TIMEOUT
;
336 cec_logical_address
Initiator(void) const
338 return packet
.size
>= 2 ?
339 (cec_logical_address
) (packet
.At(1) >> 4) :
343 cec_logical_address
Destination(void) const
345 return packet
.size
>= 2 ?
346 (cec_logical_address
) (packet
.At(1) & 0xF) :
352 cec_adapter_messagecode reply
;
353 cec_datapacket packet
;
354 cec_adapter_message_state state
;
355 int32_t transmit_timeout
;
357 bool expectControllerAck
;
359 uint8_t retryTimeout
;
360 PLATFORM::CEvent event
;