2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
9 * This program is dual-licensed; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
27 * For more information contact:
28 * Pulse-Eight Licensing <license@pulse-eight.com>
29 * http://www.pulse-eight.com/
30 * http://www.pulse-eight.net/
34 #include "USBCECAdapterMessage.h"
36 #include "lib/LibCEC.h"
37 #include "lib/platform/util/StdString.h"
40 using namespace PLATFORM
;
42 CCECAdapterMessage::CCECAdapterMessage(void)
47 CCECAdapterMessage::CCECAdapterMessage(const cec_command
&command
, uint8_t iLineTimeout
/* = 3 */)
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
;
94 lineTimeout
= iLineTimeout
;
97 std::string
CCECAdapterMessage::ToString(void) const
102 strMsg
= "empty message";
106 strMsg
= ToString(Message());
110 case MSGCODE_TIMEOUT_ERROR
:
111 case MSGCODE_HIGH_ERROR
:
112 case MSGCODE_LOW_ERROR
:
114 uint32_t iLine
= (Size() >= 4) ? (At(2) << 8) | At(3) : 0;
115 uint32_t iTime
= (Size() >= 8) ? (At(4) << 24) | (At(5) << 16) | (At(6) << 8) | At(7) : 0;
116 strMsg
.AppendFormat(" line:%u", iLine
);
117 strMsg
.AppendFormat(" time:%u", iTime
);
120 case MSGCODE_FRAME_START
:
122 strMsg
.AppendFormat(" initiator:%1x destination:%1x ack:%s %s", Initiator(), Destination(), IsACK() ? "high" : "low", IsEOM() ? "eom" : "");
124 case MSGCODE_FRAME_DATA
:
126 strMsg
.AppendFormat(" %02x %s", At(2), IsEOM() ? "eom" : "");
129 if (Size() >= 2 && (Message() == MSGCODE_COMMAND_ACCEPTED
|| Message() == MSGCODE_COMMAND_REJECTED
))
130 strMsg
.AppendFormat(": %s", ToString((cec_adapter_messagecode
)At(2)));
133 for (uint8_t iPtr
= 2; iPtr
< Size(); iPtr
++)
134 if (At(iPtr
) != MSGEND
)
135 strMsg
.AppendFormat(" %02x", At(iPtr
));
141 return std::string(strMsg
.c_str());
144 const char *CCECAdapterMessage::ToString(cec_adapter_messagecode msgCode
)
148 case MSGCODE_NOTHING
:
152 case MSGCODE_TIMEOUT_ERROR
:
154 case MSGCODE_HIGH_ERROR
:
156 case MSGCODE_LOW_ERROR
:
158 case MSGCODE_FRAME_START
:
159 return "FRAME_START";
160 case MSGCODE_FRAME_DATA
:
162 case MSGCODE_RECEIVE_FAILED
:
163 return "RECEIVE_FAILED";
164 case MSGCODE_COMMAND_ACCEPTED
:
165 return "COMMAND_ACCEPTED";
166 case MSGCODE_COMMAND_REJECTED
:
167 return "COMMAND_REJECTED";
168 case MSGCODE_SET_ACK_MASK
:
169 return "SET_ACK_MASK";
170 case MSGCODE_TRANSMIT
:
172 case MSGCODE_TRANSMIT_EOM
:
173 return "TRANSMIT_EOM";
174 case MSGCODE_TRANSMIT_IDLETIME
:
175 return "TRANSMIT_IDLETIME";
176 case MSGCODE_TRANSMIT_ACK_POLARITY
:
177 return "CEC transmission";
178 case MSGCODE_TRANSMIT_LINE_TIMEOUT
:
179 return "TRANSMIT_LINE_TIMEOUT";
180 case MSGCODE_TRANSMIT_SUCCEEDED
:
181 return "TRANSMIT_SUCCEEDED";
182 case MSGCODE_TRANSMIT_FAILED_LINE
:
183 return "TRANSMIT_FAILED_LINE";
184 case MSGCODE_TRANSMIT_FAILED_ACK
:
185 return "TRANSMIT_FAILED_ACK";
186 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
:
187 return "TRANSMIT_FAILED_TIMEOUT_DATA";
188 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
:
189 return "TRANSMIT_FAILED_TIMEOUT_LINE";
190 case MSGCODE_FIRMWARE_VERSION
:
191 return "FIRMWARE_VERSION";
192 case MSGCODE_START_BOOTLOADER
:
193 return "START_BOOTLOADER";
194 case MSGCODE_FRAME_EOM
:
196 case MSGCODE_FRAME_ACK
:
198 case MSGCODE_GET_BUILDDATE
:
199 return "GET_BUILDDATE";
200 case MSGCODE_SET_CONTROLLED
:
201 return "SET_CONTROLLED";
202 case MSGCODE_GET_AUTO_ENABLED
:
203 return "GET_AUTO_ENABLED";
204 case MSGCODE_SET_AUTO_ENABLED
:
205 return "SET_AUTO_ENABLED";
206 case MSGCODE_GET_DEFAULT_LOGICAL_ADDRESS
:
207 return "GET_DEFAULT_LOGICAL_ADDRESS";
208 case MSGCODE_SET_DEFAULT_LOGICAL_ADDRESS
:
209 return "SET_DEFAULT_LOGICAL_ADDRESS";
210 case MSGCODE_GET_LOGICAL_ADDRESS_MASK
:
211 return "GET_LOGICAL_ADDRESS_MASK";
212 case MSGCODE_SET_LOGICAL_ADDRESS_MASK
:
213 return "SET_LOGICAL_ADDRESS_MASK";
214 case MSGCODE_GET_PHYSICAL_ADDRESS
:
215 return "GET_PHYSICAL_ADDRESS";
216 case MSGCODE_SET_PHYSICAL_ADDRESS
:
217 return "SET_PHYSICAL_ADDRESS";
218 case MSGCODE_GET_DEVICE_TYPE
:
219 return "GET_DEVICE_TYPE";
220 case MSGCODE_SET_DEVICE_TYPE
:
221 return "SET_DEVICE_TYPE";
222 case MSGCODE_GET_HDMI_VERSION
:
223 return "GET_HDMI_VERSION";
224 case MSGCODE_SET_HDMI_VERSION
:
225 return "SET_HDMI_VERSION";
226 case MSGCODE_GET_OSD_NAME
:
227 return "GET_OSD_NAME";
228 case MSGCODE_SET_OSD_NAME
:
229 return "SET_OSD_NAME";
230 case MSGCODE_WRITE_EEPROM
:
231 return "WRITE_EEPROM";
232 case MSGCODE_GET_ADAPTER_TYPE
:
233 return "GET_ADAPTER_TYPE";
241 uint8_t CCECAdapterMessage::operator[](uint8_t pos
) const
243 return pos
< packet
.size
? packet
[pos
] : 0;
246 uint8_t CCECAdapterMessage::At(uint8_t pos
) const
248 return pos
< packet
.size
? packet
[pos
] : 0;
251 uint8_t CCECAdapterMessage::Size(void) const
256 bool CCECAdapterMessage::IsEmpty(void) const
258 return packet
.IsEmpty();
261 void CCECAdapterMessage::Clear(void)
263 state
= ADAPTER_MESSAGE_STATE_UNKNOWN
;
264 transmit_timeout
= CEC_DEFAULT_TRANSMIT_TIMEOUT
;
268 bNextByteIsEscaped
= false;
269 bFireAndForget
= false;
272 void CCECAdapterMessage::Shift(uint8_t iShiftBy
)
274 packet
.Shift(iShiftBy
);
277 void CCECAdapterMessage::Append(CCECAdapterMessage
&data
)
282 void CCECAdapterMessage::Append(cec_datapacket
&data
)
284 for (uint8_t iPtr
= 0; iPtr
< data
.size
; iPtr
++)
285 PushBack(data
[iPtr
]);
288 void CCECAdapterMessage::PushBack(uint8_t byte
)
290 packet
.PushBack(byte
);
293 void CCECAdapterMessage::PushEscaped(uint8_t byte
)
298 PushBack(byte
- ESCOFFSET
);
306 bool CCECAdapterMessage::PushReceivedByte(uint8_t byte
)
308 if (byte
== MSGSTART
)
310 if (HasStartMessage())
312 //TODO CLibCEC::AddLog(CEC_LOG_WARNING, "received MSGSTART before MSGEND, removing previous buffer contents");
319 if (bNextByteIsEscaped
)
321 PushBack(byte
+ (uint8_t)ESCOFFSET
);
322 bNextByteIsEscaped
= false;
324 else if (byte
== MSGESC
)
325 bNextByteIsEscaped
= true;
330 return byte
== MSGEND
;
333 cec_adapter_messagecode
CCECAdapterMessage::Message(void) const
335 return packet
.size
>= 2 ?
336 (cec_adapter_messagecode
) (packet
.At(1) & ~(MSGCODE_FRAME_EOM
| MSGCODE_FRAME_ACK
)) :
340 cec_adapter_messagecode
CCECAdapterMessage::ResponseTo(void) const
342 return packet
.size
>= 3 ?
343 (cec_adapter_messagecode
) (packet
.At(2) & ~(MSGCODE_FRAME_EOM
| MSGCODE_FRAME_ACK
)) :
347 bool CCECAdapterMessage::IsTranmission(void) const
349 cec_adapter_messagecode msgCode
= Message();
350 return msgCode
== MSGCODE_FRAME_ACK
||
351 msgCode
== MSGCODE_FRAME_DATA
||
352 msgCode
== MSGCODE_FRAME_EOM
||
353 msgCode
== MSGCODE_FRAME_START
||
354 msgCode
== MSGCODE_HIGH_ERROR
||
355 msgCode
== MSGCODE_LOW_ERROR
||
356 msgCode
== MSGCODE_RECEIVE_FAILED
||
357 msgCode
== MSGCODE_TRANSMIT_ACK_POLARITY
||
358 msgCode
== MSGCODE_TRANSMIT_EOM
||
359 msgCode
== MSGCODE_TRANSMIT_FAILED_ACK
||
360 msgCode
== MSGCODE_TRANSMIT_FAILED_LINE
||
361 msgCode
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
||
362 msgCode
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
||
363 msgCode
== MSGCODE_TRANSMIT_LINE_TIMEOUT
||
364 msgCode
== MSGCODE_TRANSMIT_SUCCEEDED
;
367 bool CCECAdapterMessage::IsEOM(void) const
369 return packet
.size
>= 2 ?
370 (packet
.At(1) & MSGCODE_FRAME_EOM
) != 0 :
374 bool CCECAdapterMessage::IsACK(void) const
376 return packet
.size
>= 2 ?
377 (packet
.At(1) & MSGCODE_FRAME_ACK
) != 0 :
381 bool CCECAdapterMessage::MessageCodeIsError(const cec_adapter_messagecode code
)
383 return (code
== MSGCODE_HIGH_ERROR
||
384 code
== MSGCODE_LOW_ERROR
||
385 code
== MSGCODE_RECEIVE_FAILED
||
386 code
== MSGCODE_COMMAND_REJECTED
||
387 code
== MSGCODE_TRANSMIT_LINE_TIMEOUT
||
388 code
== MSGCODE_TRANSMIT_FAILED_LINE
||
389 code
== MSGCODE_TRANSMIT_FAILED_ACK
||
390 code
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
||
391 code
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
);
394 bool CCECAdapterMessage::IsError(void) const
396 return MessageCodeIsError(Message());
399 bool CCECAdapterMessage::ReplyIsError(void) const
401 return MessageCodeIsError(Reply());
404 bool CCECAdapterMessage::NeedsRetry(void) const
406 return Reply() == MSGCODE_NOTHING
||
407 Reply() == MSGCODE_RECEIVE_FAILED
||
408 Reply() == MSGCODE_TIMEOUT_ERROR
||
409 Reply() == MSGCODE_TRANSMIT_FAILED_LINE
||
410 Reply() == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
||
411 Reply() == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
||
412 Reply() == MSGCODE_TRANSMIT_LINE_TIMEOUT
;
415 cec_logical_address
CCECAdapterMessage::Initiator(void) const
417 return packet
.size
>= 3 ?
418 (cec_logical_address
) (packet
.At(2) >> 4) :
422 cec_logical_address
CCECAdapterMessage::Destination(void) const
424 return packet
.size
>= 3 ?
425 (cec_logical_address
) (packet
.At(2) & 0xF) :
429 bool CCECAdapterMessage::HasStartMessage(void) const
431 return packet
.size
>= 1 && packet
.At(0) == MSGSTART
;
434 bool CCECAdapterMessage::PushToCecCommand(cec_command
&command
) const
440 cec_adapter_messagecode msgCode
= Message();
441 if (msgCode
== MSGCODE_FRAME_START
)
446 command
.initiator
= Initiator();
447 command
.destination
= Destination();
448 command
.ack
= IsACK();
449 command
.eom
= IsEOM();
451 return IsEOM() && !IsError();
453 else if (msgCode
== MSGCODE_FRAME_DATA
)
457 command
.PushBack(At(2));
458 command
.eom
= IsEOM();
460 return IsEOM() && !IsError();
466 cec_adapter_messagecode
CCECAdapterMessage::Reply(void) const
468 return response
.size
>= 2 ?
469 (cec_adapter_messagecode
) (response
.At(1) & ~(MSGCODE_FRAME_EOM
| MSGCODE_FRAME_ACK
)) :