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/
36 typedef enum cec_adapter_message_state
38 ADAPTER_MESSAGE_STATE_UNKNOWN
= 0,
39 ADAPTER_MESSAGE_STATE_WAITING_TO_BE_SENT
,
40 ADAPTER_MESSAGE_STATE_SENT
,
41 ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED
,
42 ADAPTER_MESSAGE_STATE_SENT_ACKED
,
43 ADAPTER_MESSAGE_STATE_INCOMING
,
44 ADAPTER_MESSAGE_STATE_ERROR
45 } cec_adapter_message_state
;
48 class CCECAdapterMessage
51 CCECAdapterMessage(void)
56 CCECAdapterMessage(const cec_command
&command
)
60 //set ack polarity to high when transmitting to the broadcast address
61 //set ack polarity low when transmitting to any other address
63 PushEscaped(MSGCODE_TRANSMIT_ACK_POLARITY
);
64 if (command
.destination
== CECDEVICE_BROADCAST
)
65 PushEscaped(CEC_TRUE
);
67 PushEscaped(CEC_FALSE
);
70 // add source and destination
72 PushEscaped(command
.opcode_set
== 0 ? (uint8_t)MSGCODE_TRANSMIT_EOM
: (uint8_t)MSGCODE_TRANSMIT
);
73 PushBack(((uint8_t)command
.initiator
<< 4) + (uint8_t)command
.destination
);
77 if (command
.opcode_set
== 1)
80 PushEscaped(command
.parameters
.IsEmpty() ? (uint8_t)MSGCODE_TRANSMIT_EOM
: (uint8_t)MSGCODE_TRANSMIT
);
81 PushBack((uint8_t) command
.opcode
);
85 for (int8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
89 if (iPtr
== command
.parameters
.size
- 1)
90 PushEscaped( MSGCODE_TRANSMIT_EOM
);
92 PushEscaped(MSGCODE_TRANSMIT
);
94 PushEscaped(command
.parameters
[iPtr
]);
101 transmit_timeout
= command
.transmit_timeout
;
105 CCECAdapterMessage
&operator=(const CCECAdapterMessage
&msg
)
112 CStdString
ToString(void) const
117 strMsg
= "empty message";
121 strMsg
= MessageCodeAsString();
125 case MSGCODE_TIMEOUT_ERROR
:
126 case MSGCODE_HIGH_ERROR
:
127 case MSGCODE_LOW_ERROR
:
129 uint32_t iLine
= (Size() >= 3) ? (At(1) << 8) | At(2) : 0;
130 uint32_t iTime
= (Size() >= 7) ? (At(3) << 24) | (At(4) << 16) | (At(5) << 8) | At(6) : 0;
131 strMsg
.AppendFormat(" line:%u", iLine
);
132 strMsg
.AppendFormat(" time:%u", iTime
);
135 case MSGCODE_FRAME_START
:
137 strMsg
.AppendFormat(" initiator:%1x destination:%1x ack:%s %s", Initiator(), Destination(), IsACK() ? "high" : "low", IsEOM() ? "eom" : "");
139 case MSGCODE_FRAME_DATA
:
141 strMsg
.AppendFormat(" %02x %s", At(1), IsEOM() ? "eom" : "");
151 CStdString
MessageCodeAsString(void) const
156 case MSGCODE_NOTHING
:
162 case MSGCODE_TIMEOUT_ERROR
:
165 case MSGCODE_HIGH_ERROR
:
166 strMsg
= "HIGH_ERROR";
168 case MSGCODE_LOW_ERROR
:
169 strMsg
= "LOW_ERROR";
171 case MSGCODE_FRAME_START
:
172 strMsg
= "FRAME_START";
174 case MSGCODE_FRAME_DATA
:
175 strMsg
= "FRAME_DATA";
177 case MSGCODE_RECEIVE_FAILED
:
178 strMsg
= "RECEIVE_FAILED";
180 case MSGCODE_COMMAND_ACCEPTED
:
181 strMsg
= "COMMAND_ACCEPTED";
183 case MSGCODE_COMMAND_REJECTED
:
184 strMsg
= "COMMAND_REJECTED";
186 case MSGCODE_SET_ACK_MASK
:
187 strMsg
= "SET_ACK_MASK";
189 case MSGCODE_TRANSMIT
:
192 case MSGCODE_TRANSMIT_EOM
:
193 strMsg
= "TRANSMIT_EOM";
195 case MSGCODE_TRANSMIT_IDLETIME
:
196 strMsg
= "TRANSMIT_IDLETIME";
198 case MSGCODE_TRANSMIT_ACK_POLARITY
:
199 strMsg
= "TRANSMIT_ACK_POLARITY";
201 case MSGCODE_TRANSMIT_LINE_TIMEOUT
:
202 strMsg
= "TRANSMIT_LINE_TIMEOUT";
204 case MSGCODE_TRANSMIT_SUCCEEDED
:
205 strMsg
= "TRANSMIT_SUCCEEDED";
207 case MSGCODE_TRANSMIT_FAILED_LINE
:
208 strMsg
= "TRANSMIT_FAILED_LINE";
210 case MSGCODE_TRANSMIT_FAILED_ACK
:
211 strMsg
= "TRANSMIT_FAILED_ACK";
213 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
:
214 strMsg
= "TRANSMIT_FAILED_TIMEOUT_DATA";
216 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
:
217 strMsg
= "TRANSMIT_FAILED_TIMEOUT_LINE";
219 case MSGCODE_FIRMWARE_VERSION
:
220 strMsg
= "FIRMWARE_VERSION";
222 case MSGCODE_START_BOOTLOADER
:
223 strMsg
= "START_BOOTLOADER";
225 case MSGCODE_FRAME_EOM
:
226 strMsg
= "FRAME_EOM";
228 case MSGCODE_FRAME_ACK
:
229 strMsg
= "FRAME_ACK";
236 uint8_t operator[](uint8_t pos
) const
241 uint8_t At(uint8_t pos
) const
246 uint8_t Size(void) const
251 bool IsEmpty(void) const
253 return packet
.IsEmpty();
258 state
= ADAPTER_MESSAGE_STATE_UNKNOWN
;
259 transmit_timeout
= CEC_DEFAULT_TRANSMIT_TIMEOUT
;
261 maxTries
= CEC_DEFAULT_TRANSMIT_RETRIES
+ 1;
263 reply
= MSGCODE_NOTHING
;
264 isTransmission
= true;
265 expectControllerAck
= true;
268 void Shift(uint8_t iShiftBy
)
270 packet
.Shift(iShiftBy
);
273 void PushBack(uint8_t add
)
275 packet
.PushBack(add
);
278 void PushEscaped(uint8_t byte
)
283 PushBack(byte
- ESCOFFSET
);
291 cec_adapter_messagecode
Message(void) const
293 return packet
.size
>= 1 ?
294 (cec_adapter_messagecode
) (packet
.At(0) & ~(MSGCODE_FRAME_EOM
| MSGCODE_FRAME_ACK
)) :
298 bool IsEOM(void) const
300 return packet
.size
>= 1 ?
301 (packet
.At(0) & MSGCODE_FRAME_EOM
) != 0 :
305 bool IsACK(void) const
307 return packet
.size
>= 1 ?
308 (packet
.At(0) & MSGCODE_FRAME_ACK
) != 0 :
312 bool IsError(void) const
314 cec_adapter_messagecode code
= Message();
315 return (code
== MSGCODE_HIGH_ERROR
||
316 code
== MSGCODE_LOW_ERROR
||
317 code
== MSGCODE_RECEIVE_FAILED
||
318 code
== MSGCODE_COMMAND_REJECTED
||
319 code
== MSGCODE_TRANSMIT_LINE_TIMEOUT
||
320 code
== MSGCODE_TRANSMIT_FAILED_LINE
||
321 code
== MSGCODE_TRANSMIT_FAILED_ACK
||
322 code
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
||
323 code
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
);
326 bool NeedsRetry(void) const
328 return reply
== MSGCODE_NOTHING
||
329 reply
== MSGCODE_RECEIVE_FAILED
||
330 reply
== MSGCODE_TIMEOUT_ERROR
||
331 reply
== MSGCODE_TRANSMIT_FAILED_LINE
||
332 reply
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
||
333 reply
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
||
334 reply
== MSGCODE_TRANSMIT_LINE_TIMEOUT
;
337 cec_logical_address
Initiator(void) const
339 return packet
.size
>= 2 ?
340 (cec_logical_address
) (packet
.At(1) >> 4) :
344 cec_logical_address
Destination(void) const
346 return packet
.size
>= 2 ?
347 (cec_logical_address
) (packet
.At(1) & 0xF) :
353 cec_adapter_messagecode reply
;
354 cec_datapacket packet
;
355 cec_adapter_message_state state
;
356 int32_t transmit_timeout
;
358 bool expectControllerAck
;
359 PLATFORM::CMutex mutex
;
360 PLATFORM::CCondition condition
;