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)
46 CCECAdapterMessage(const cec_command
&command
)
50 //set ack polarity to high when transmitting to the broadcast address
51 //set ack polarity low when transmitting to any other address
53 PushEscaped(MSGCODE_TRANSMIT_ACK_POLARITY
);
54 if (command
.destination
== CECDEVICE_BROADCAST
)
55 PushEscaped(CEC_TRUE
);
57 PushEscaped(CEC_FALSE
);
60 // add source and destination
62 PushEscaped(command
.opcode_set
== 0 ? (uint8_t)MSGCODE_TRANSMIT_EOM
: (uint8_t)MSGCODE_TRANSMIT
);
63 PushBack(((uint8_t)command
.initiator
<< 4) + (uint8_t)command
.destination
);
67 if (command
.opcode_set
== 1)
70 PushEscaped(command
.parameters
.IsEmpty() ? (uint8_t)MSGCODE_TRANSMIT_EOM
: (uint8_t)MSGCODE_TRANSMIT
);
71 PushBack((uint8_t) command
.opcode
);
75 for (int8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
79 if (iPtr
== command
.parameters
.size
- 1)
80 PushEscaped( MSGCODE_TRANSMIT_EOM
);
82 PushEscaped(MSGCODE_TRANSMIT
);
84 PushEscaped(command
.parameters
[iPtr
]);
91 transmit_timeout
= command
.transmit_timeout
;
95 CCECAdapterMessage
&operator=(const CCECAdapterMessage
&msg
)
102 CStdString
ToString(void) const
107 strMsg
= "empty message";
111 strMsg
= MessageCodeAsString();
115 case MSGCODE_TIMEOUT_ERROR
:
116 case MSGCODE_HIGH_ERROR
:
117 case MSGCODE_LOW_ERROR
:
119 uint32_t iLine
= (Size() >= 3) ? (At(1) << 8) | At(2) : 0;
120 uint32_t iTime
= (Size() >= 7) ? (At(3) << 24) | (At(4) << 16) | (At(5) << 8) | At(6) : 0;
121 strMsg
.AppendFormat(" line:%u", iLine
);
122 strMsg
.AppendFormat(" time:%u", iTime
);
125 case MSGCODE_FRAME_START
:
127 strMsg
.AppendFormat(" initiator:%1x destination:%1x ack:%s %s", Initiator(), Destination(), IsACK() ? "high" : "low", IsEOM() ? "eom" : "");
129 case MSGCODE_FRAME_DATA
:
131 strMsg
.AppendFormat(" %02x %s", At(1), IsEOM() ? "eom" : "");
141 CStdString
MessageCodeAsString(void) const
146 case MSGCODE_NOTHING
:
152 case MSGCODE_TIMEOUT_ERROR
:
155 case MSGCODE_HIGH_ERROR
:
156 strMsg
= "HIGH_ERROR";
158 case MSGCODE_LOW_ERROR
:
159 strMsg
= "LOW_ERROR";
161 case MSGCODE_FRAME_START
:
162 strMsg
= "FRAME_START";
164 case MSGCODE_FRAME_DATA
:
165 strMsg
= "FRAME_DATA";
167 case MSGCODE_RECEIVE_FAILED
:
168 strMsg
= "RECEIVE_FAILED";
170 case MSGCODE_COMMAND_ACCEPTED
:
171 strMsg
= "COMMAND_ACCEPTED";
173 case MSGCODE_COMMAND_REJECTED
:
174 strMsg
= "COMMAND_REJECTED";
176 case MSGCODE_SET_ACK_MASK
:
177 strMsg
= "SET_ACK_MASK";
179 case MSGCODE_TRANSMIT
:
182 case MSGCODE_TRANSMIT_EOM
:
183 strMsg
= "TRANSMIT_EOM";
185 case MSGCODE_TRANSMIT_IDLETIME
:
186 strMsg
= "TRANSMIT_IDLETIME";
188 case MSGCODE_TRANSMIT_ACK_POLARITY
:
189 strMsg
= "TRANSMIT_ACK_POLARITY";
191 case MSGCODE_TRANSMIT_LINE_TIMEOUT
:
192 strMsg
= "TRANSMIT_LINE_TIMEOUT";
194 case MSGCODE_TRANSMIT_SUCCEEDED
:
195 strMsg
= "TRANSMIT_SUCCEEDED";
197 case MSGCODE_TRANSMIT_FAILED_LINE
:
198 strMsg
= "TRANSMIT_FAILED_LINE";
200 case MSGCODE_TRANSMIT_FAILED_ACK
:
201 strMsg
= "TRANSMIT_FAILED_ACK";
203 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
:
204 strMsg
= "TRANSMIT_FAILED_TIMEOUT_DATA";
206 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
:
207 strMsg
= "TRANSMIT_FAILED_TIMEOUT_LINE";
209 case MSGCODE_FIRMWARE_VERSION
:
210 strMsg
= "FIRMWARE_VERSION";
212 case MSGCODE_START_BOOTLOADER
:
213 strMsg
= "START_BOOTLOADER";
215 case MSGCODE_FRAME_EOM
:
216 strMsg
= "FRAME_EOM";
218 case MSGCODE_FRAME_ACK
:
219 strMsg
= "FRAME_ACK";
221 case MSGCODE_SET_POWERSTATE
:
222 strMsg
= "SET_POWERSTATE";
224 case MSGCODE_SET_CONTROLLED
:
225 strMsg
= "SET_CONTROLLED";
232 uint8_t operator[](uint8_t pos
) const
237 uint8_t At(uint8_t pos
) const
242 uint8_t Size(void) const
247 bool IsEmpty(void) const
249 return packet
.IsEmpty();
254 state
= ADAPTER_MESSAGE_STATE_UNKNOWN
;
255 transmit_timeout
= CEC_DEFAULT_TRANSMIT_TIMEOUT
;
257 maxTries
= CEC_DEFAULT_TRANSMIT_RETRIES
+ 1;
259 reply
= MSGCODE_NOTHING
;
260 isTransmission
= true;
261 expectControllerAck
= true;
266 void Shift(uint8_t iShiftBy
)
268 packet
.Shift(iShiftBy
);
271 void PushBack(uint8_t add
)
273 packet
.PushBack(add
);
276 void PushEscaped(uint8_t byte
)
281 PushBack(byte
- ESCOFFSET
);
289 cec_adapter_messagecode
Message(void) const
291 return packet
.size
>= 1 ?
292 (cec_adapter_messagecode
) (packet
.At(0) & ~(MSGCODE_FRAME_EOM
| MSGCODE_FRAME_ACK
)) :
296 bool IsEOM(void) const
298 return packet
.size
>= 1 ?
299 (packet
.At(0) & MSGCODE_FRAME_EOM
) != 0 :
303 bool IsACK(void) const
305 return packet
.size
>= 1 ?
306 (packet
.At(0) & MSGCODE_FRAME_ACK
) != 0 :
310 bool IsError(void) const
312 cec_adapter_messagecode code
= Message();
313 return (code
== MSGCODE_HIGH_ERROR
||
314 code
== MSGCODE_LOW_ERROR
||
315 code
== MSGCODE_RECEIVE_FAILED
||
316 code
== MSGCODE_COMMAND_REJECTED
||
317 code
== MSGCODE_TRANSMIT_LINE_TIMEOUT
||
318 code
== MSGCODE_TRANSMIT_FAILED_LINE
||
319 code
== MSGCODE_TRANSMIT_FAILED_ACK
||
320 code
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
||
321 code
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
);
324 bool NeedsRetry(void) const
326 return reply
== MSGCODE_NOTHING
||
327 reply
== MSGCODE_RECEIVE_FAILED
||
328 reply
== MSGCODE_TIMEOUT_ERROR
||
329 reply
== MSGCODE_TRANSMIT_FAILED_LINE
||
330 reply
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
||
331 reply
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
||
332 reply
== MSGCODE_TRANSMIT_LINE_TIMEOUT
;
335 cec_logical_address
Initiator(void) const
337 return packet
.size
>= 2 ?
338 (cec_logical_address
) (packet
.At(1) >> 4) :
342 cec_logical_address
Destination(void) const
344 return packet
.size
>= 2 ?
345 (cec_logical_address
) (packet
.At(1) & 0xF) :
351 cec_adapter_messagecode reply
;
352 cec_datapacket packet
;
353 cec_adapter_message_state state
;
354 int32_t transmit_timeout
;
356 bool expectControllerAck
;
358 uint8_t retryTimeout
;
359 PLATFORM::CEvent event
;