3 * This file is part of the libCEC(R) library.
5 * libCEC(R) is Copyright (C) 2011 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 typedef enum cec_adapter_message_state
40 ADAPTER_MESSAGE_STATE_UNKNOWN
= 0,
41 ADAPTER_MESSAGE_STATE_WAITING_TO_BE_SENT
,
42 ADAPTER_MESSAGE_STATE_SENT
,
43 ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED
,
44 ADAPTER_MESSAGE_STATE_SENT_ACKED
,
45 ADAPTER_MESSAGE_STATE_INCOMING
,
46 ADAPTER_MESSAGE_STATE_ERROR
47 } cec_adapter_message_state
;
50 class CCECAdapterMessage
53 CCECAdapterMessage(void)
58 CCECAdapterMessage(const cec_command
&command
)
62 //set ack polarity to high when transmitting to the broadcast address
63 //set ack polarity low when transmitting to any other address
65 PushEscaped(MSGCODE_TRANSMIT_ACK_POLARITY
);
66 if (command
.destination
== CECDEVICE_BROADCAST
)
67 PushEscaped(CEC_TRUE
);
69 PushEscaped(CEC_FALSE
);
72 // add source and destination
74 PushEscaped(command
.opcode_set
== 0 ? (uint8_t)MSGCODE_TRANSMIT_EOM
: (uint8_t)MSGCODE_TRANSMIT
);
75 PushBack(((uint8_t)command
.initiator
<< 4) + (uint8_t)command
.destination
);
79 if (command
.opcode_set
== 1)
82 PushEscaped(command
.parameters
.IsEmpty() ? (uint8_t)MSGCODE_TRANSMIT_EOM
: (uint8_t)MSGCODE_TRANSMIT
);
83 PushBack((uint8_t) command
.opcode
);
87 for (int8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
91 if (iPtr
== command
.parameters
.size
- 1)
92 PushEscaped( MSGCODE_TRANSMIT_EOM
);
94 PushEscaped(MSGCODE_TRANSMIT
);
96 PushEscaped(command
.parameters
[iPtr
]);
103 transmit_timeout
= command
.transmit_timeout
;
107 CCECAdapterMessage
&operator=(const CCECAdapterMessage
&msg
)
114 CStdString
ToString(void) const
119 strMsg
= "empty message";
123 strMsg
= MessageCodeAsString();
127 case MSGCODE_TIMEOUT_ERROR
:
128 case MSGCODE_HIGH_ERROR
:
129 case MSGCODE_LOW_ERROR
:
131 uint32_t iLine
= (Size() >= 3) ? (At(1) << 8) | At(2) : 0;
132 uint32_t iTime
= (Size() >= 7) ? (At(3) << 24) | (At(4) << 16) | (At(5) << 8) | At(6) : 0;
133 strMsg
.AppendFormat(" line:%u", iLine
);
134 strMsg
.AppendFormat(" time:%u", iTime
);
137 case MSGCODE_FRAME_START
:
139 strMsg
.AppendFormat(" initiator:%1x destination:%1x ack:%s %s", Initiator(), Destination(), IsACK() ? "high" : "low", IsEOM() ? "eom" : "");
141 case MSGCODE_FRAME_DATA
:
143 strMsg
.AppendFormat(" %02x %s", At(1), IsEOM() ? "eom" : "");
153 CStdString
MessageCodeAsString(void) const
158 case MSGCODE_NOTHING
:
164 case MSGCODE_TIMEOUT_ERROR
:
167 case MSGCODE_HIGH_ERROR
:
168 strMsg
= "HIGH_ERROR";
170 case MSGCODE_LOW_ERROR
:
171 strMsg
= "LOW_ERROR";
173 case MSGCODE_FRAME_START
:
174 strMsg
= "FRAME_START";
176 case MSGCODE_FRAME_DATA
:
177 strMsg
= "FRAME_DATA";
179 case MSGCODE_RECEIVE_FAILED
:
180 strMsg
= "RECEIVE_FAILED";
182 case MSGCODE_COMMAND_ACCEPTED
:
183 strMsg
= "COMMAND_ACCEPTED";
185 case MSGCODE_COMMAND_REJECTED
:
186 strMsg
= "COMMAND_REJECTED";
188 case MSGCODE_SET_ACK_MASK
:
189 strMsg
= "SET_ACK_MASK";
191 case MSGCODE_TRANSMIT
:
194 case MSGCODE_TRANSMIT_EOM
:
195 strMsg
= "TRANSMIT_EOM";
197 case MSGCODE_TRANSMIT_IDLETIME
:
198 strMsg
= "TRANSMIT_IDLETIME";
200 case MSGCODE_TRANSMIT_ACK_POLARITY
:
201 strMsg
= "TRANSMIT_ACK_POLARITY";
203 case MSGCODE_TRANSMIT_LINE_TIMEOUT
:
204 strMsg
= "TRANSMIT_LINE_TIMEOUT";
206 case MSGCODE_TRANSMIT_SUCCEEDED
:
207 strMsg
= "TRANSMIT_SUCCEEDED";
209 case MSGCODE_TRANSMIT_FAILED_LINE
:
210 strMsg
= "TRANSMIT_FAILED_LINE";
212 case MSGCODE_TRANSMIT_FAILED_ACK
:
213 strMsg
= "TRANSMIT_FAILED_ACK";
215 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
:
216 strMsg
= "TRANSMIT_FAILED_TIMEOUT_DATA";
218 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
:
219 strMsg
= "TRANSMIT_FAILED_TIMEOUT_LINE";
221 case MSGCODE_FIRMWARE_VERSION
:
222 strMsg
= "FIRMWARE_VERSION";
224 case MSGCODE_START_BOOTLOADER
:
225 strMsg
= "START_BOOTLOADER";
227 case MSGCODE_FRAME_EOM
:
228 strMsg
= "FRAME_EOM";
230 case MSGCODE_FRAME_ACK
:
231 strMsg
= "FRAME_ACK";
238 uint8_t operator[](uint8_t pos
) const
243 uint8_t At(uint8_t pos
) const
248 uint8_t Size(void) const
253 bool IsEmpty(void) const
255 return packet
.IsEmpty();
260 state
= ADAPTER_MESSAGE_STATE_UNKNOWN
;
261 transmit_timeout
= CEC_DEFAULT_TRANSMIT_TIMEOUT
;
263 maxTries
= CEC_DEFAULT_TRANSMIT_RETRIES
+ 1;
265 reply
= MSGCODE_NOTHING
;
266 isTransmission
= true;
267 expectControllerAck
= true;
270 void Shift(uint8_t iShiftBy
)
272 packet
.Shift(iShiftBy
);
275 void PushBack(uint8_t add
)
277 packet
.PushBack(add
);
280 void PushEscaped(uint8_t byte
)
285 PushBack(byte
- ESCOFFSET
);
293 cec_adapter_messagecode
Message(void) const
295 return packet
.size
>= 1 ?
296 (cec_adapter_messagecode
) (packet
.At(0) & ~(MSGCODE_FRAME_EOM
| MSGCODE_FRAME_ACK
)) :
300 bool IsEOM(void) const
302 return packet
.size
>= 1 ?
303 (packet
.At(0) & MSGCODE_FRAME_EOM
) != 0 :
307 bool IsACK(void) const
309 return packet
.size
>= 1 ?
310 (packet
.At(0) & MSGCODE_FRAME_ACK
) != 0 :
314 bool IsError(void) const
316 cec_adapter_messagecode code
= Message();
317 return (code
== MSGCODE_HIGH_ERROR
||
318 code
== MSGCODE_LOW_ERROR
||
319 code
== MSGCODE_RECEIVE_FAILED
||
320 code
== MSGCODE_COMMAND_REJECTED
||
321 code
== MSGCODE_TRANSMIT_LINE_TIMEOUT
||
322 code
== MSGCODE_TRANSMIT_FAILED_LINE
||
323 code
== MSGCODE_TRANSMIT_FAILED_ACK
||
324 code
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
||
325 code
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
);
328 bool NeedsRetry(void) const
330 return reply
== MSGCODE_NOTHING
||
331 reply
== MSGCODE_RECEIVE_FAILED
||
332 reply
== MSGCODE_TIMEOUT_ERROR
||
333 reply
== MSGCODE_TRANSMIT_FAILED_LINE
||
334 reply
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
||
335 reply
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
||
336 reply
== MSGCODE_TRANSMIT_LINE_TIMEOUT
;
339 cec_logical_address
Initiator(void) const
341 return packet
.size
>= 2 ?
342 (cec_logical_address
) (packet
.At(1) >> 4) :
346 cec_logical_address
Destination(void) const
348 return packet
.size
>= 2 ?
349 (cec_logical_address
) (packet
.At(1) & 0xF) :
355 cec_adapter_messagecode reply
;
356 cec_datapacket packet
;
357 cec_adapter_message_state state
;
358 int32_t transmit_timeout
;
360 bool expectControllerAck
;
361 PLATFORM::CMutex mutex
;
362 PLATFORM::CCondition condition
;