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/
33 #include "USBCECAdapterMessage.h"
34 #include "../LibCEC.h"
37 using namespace PLATFORM
;
39 CCECAdapterMessage::CCECAdapterMessage(void)
44 CCECAdapterMessage::CCECAdapterMessage(const cec_command
&command
, uint8_t iLineTimeout
/* = 3 */)
48 //set ack polarity to high when transmitting to the broadcast address
49 //set ack polarity low when transmitting to any other address
51 PushEscaped(MSGCODE_TRANSMIT_ACK_POLARITY
);
52 if (command
.destination
== CECDEVICE_BROADCAST
)
53 PushEscaped(CEC_TRUE
);
55 PushEscaped(CEC_FALSE
);
58 // add source and destination
60 PushEscaped(command
.opcode_set
== 0 ? (uint8_t)MSGCODE_TRANSMIT_EOM
: (uint8_t)MSGCODE_TRANSMIT
);
61 PushBack(((uint8_t)command
.initiator
<< 4) + (uint8_t)command
.destination
);
65 if (command
.opcode_set
== 1)
68 PushEscaped(command
.parameters
.IsEmpty() ? (uint8_t)MSGCODE_TRANSMIT_EOM
: (uint8_t)MSGCODE_TRANSMIT
);
69 PushBack((uint8_t) command
.opcode
);
73 for (int8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
77 if (iPtr
== command
.parameters
.size
- 1)
78 PushEscaped( MSGCODE_TRANSMIT_EOM
);
80 PushEscaped(MSGCODE_TRANSMIT
);
82 PushEscaped(command
.parameters
[iPtr
]);
89 transmit_timeout
= command
.transmit_timeout
;
91 lineTimeout
= iLineTimeout
;
94 CStdString
CCECAdapterMessage::ToString(void) const
99 strMsg
= "empty message";
103 strMsg
= ToString(Message());
107 case MSGCODE_TIMEOUT_ERROR
:
108 case MSGCODE_HIGH_ERROR
:
109 case MSGCODE_LOW_ERROR
:
111 uint32_t iLine
= (Size() >= 4) ? (At(2) << 8) | At(3) : 0;
112 uint32_t iTime
= (Size() >= 8) ? (At(4) << 24) | (At(5) << 16) | (At(6) << 8) | At(7) : 0;
113 strMsg
.AppendFormat(" line:%u", iLine
);
114 strMsg
.AppendFormat(" time:%u", iTime
);
117 case MSGCODE_FRAME_START
:
119 strMsg
.AppendFormat(" initiator:%1x destination:%1x ack:%s %s", Initiator(), Destination(), IsACK() ? "high" : "low", IsEOM() ? "eom" : "");
121 case MSGCODE_FRAME_DATA
:
123 strMsg
.AppendFormat(" %02x %s", At(2), IsEOM() ? "eom" : "");
126 for (uint8_t iPtr
= 2; iPtr
< Size(); iPtr
++)
127 if (At(iPtr
) != MSGEND
)
128 strMsg
.AppendFormat(" %02x", At(iPtr
));
136 const char *CCECAdapterMessage::ToString(cec_adapter_messagecode msgCode
)
140 case MSGCODE_NOTHING
:
144 case MSGCODE_TIMEOUT_ERROR
:
146 case MSGCODE_HIGH_ERROR
:
148 case MSGCODE_LOW_ERROR
:
150 case MSGCODE_FRAME_START
:
151 return "FRAME_START";
152 case MSGCODE_FRAME_DATA
:
154 case MSGCODE_RECEIVE_FAILED
:
155 return "RECEIVE_FAILED";
156 case MSGCODE_COMMAND_ACCEPTED
:
157 return "COMMAND_ACCEPTED";
158 case MSGCODE_COMMAND_REJECTED
:
159 return "COMMAND_REJECTED";
160 case MSGCODE_SET_ACK_MASK
:
161 return "SET_ACK_MASK";
162 case MSGCODE_TRANSMIT
:
164 case MSGCODE_TRANSMIT_EOM
:
165 return "TRANSMIT_EOM";
166 case MSGCODE_TRANSMIT_IDLETIME
:
167 return "TRANSMIT_IDLETIME";
168 case MSGCODE_TRANSMIT_ACK_POLARITY
:
169 return "TRANSMIT_ACK_POLARITY";
170 case MSGCODE_TRANSMIT_LINE_TIMEOUT
:
171 return "TRANSMIT_LINE_TIMEOUT";
172 case MSGCODE_TRANSMIT_SUCCEEDED
:
173 return "TRANSMIT_SUCCEEDED";
174 case MSGCODE_TRANSMIT_FAILED_LINE
:
175 return "TRANSMIT_FAILED_LINE";
176 case MSGCODE_TRANSMIT_FAILED_ACK
:
177 return "TRANSMIT_FAILED_ACK";
178 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
:
179 return "TRANSMIT_FAILED_TIMEOUT_DATA";
180 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
:
181 return "TRANSMIT_FAILED_TIMEOUT_LINE";
182 case MSGCODE_FIRMWARE_VERSION
:
183 return "FIRMWARE_VERSION";
184 case MSGCODE_START_BOOTLOADER
:
185 return "START_BOOTLOADER";
186 case MSGCODE_FRAME_EOM
:
188 case MSGCODE_FRAME_ACK
:
190 case MSGCODE_GET_BUILDDATE
:
191 return "GET_BUILDDATE";
192 case MSGCODE_SET_CONTROLLED
:
193 return "SET_CONTROLLED";
194 case MSGCODE_GET_AUTO_ENABLED
:
195 return "GET_AUTO_ENABLED";
196 case MSGCODE_SET_AUTO_ENABLED
:
197 return "SET_AUTO_ENABLED";
198 case MSGCODE_GET_DEFAULT_LOGICAL_ADDRESS
:
199 return "GET_DEFAULT_LOGICAL_ADDRESS";
200 case MSGCODE_SET_DEFAULT_LOGICAL_ADDRESS
:
201 return "SET_DEFAULT_LOGICAL_ADDRESS";
202 case MSGCODE_GET_LOGICAL_ADDRESS_MASK
:
203 return "GET_LOGICAL_ADDRESS_MASK";
204 case MSGCODE_SET_LOGICAL_ADDRESS_MASK
:
205 return "SET_LOGICAL_ADDRESS_MASK";
206 case MSGCODE_GET_PHYSICAL_ADDRESS
:
207 return "GET_PHYSICAL_ADDRESS";
208 case MSGCODE_SET_PHYSICAL_ADDRESS
:
209 return "SET_PHYSICAL_ADDRESS";
210 case MSGCODE_GET_DEVICE_TYPE
:
211 return "GET_DEVICE_TYPE";
212 case MSGCODE_SET_DEVICE_TYPE
:
213 return "SET_DEVICE_TYPE";
214 case MSGCODE_GET_HDMI_VERSION
:
215 return "GET_HDMI_VERSION";
216 case MSGCODE_SET_HDMI_VERSION
:
217 return "SET_HDMI_VERSION";
218 case MSGCODE_GET_OSD_NAME
:
219 return "GET_OSD_NAME";
220 case MSGCODE_SET_OSD_NAME
:
221 return "SET_OSD_NAME";
222 case MSGCODE_WRITE_EEPROM
:
223 return "WRITE_EEPROM";
231 uint8_t CCECAdapterMessage::operator[](uint8_t pos
) const
233 return pos
< packet
.size
? packet
[pos
] : 0;
236 uint8_t CCECAdapterMessage::At(uint8_t pos
) const
238 return pos
< packet
.size
? packet
[pos
] : 0;
241 uint8_t CCECAdapterMessage::Size(void) const
246 bool CCECAdapterMessage::IsEmpty(void) const
248 return packet
.IsEmpty();
251 void CCECAdapterMessage::Clear(void)
253 state
= ADAPTER_MESSAGE_STATE_UNKNOWN
;
254 transmit_timeout
= CEC_DEFAULT_TRANSMIT_TIMEOUT
;
258 bNextByteIsEscaped
= false;
261 void CCECAdapterMessage::Shift(uint8_t iShiftBy
)
263 packet
.Shift(iShiftBy
);
266 void CCECAdapterMessage::Append(CCECAdapterMessage
&data
)
271 void CCECAdapterMessage::Append(cec_datapacket
&data
)
273 for (uint8_t iPtr
= 0; iPtr
< data
.size
; iPtr
++)
274 PushBack(data
[iPtr
]);
277 void CCECAdapterMessage::PushBack(uint8_t byte
)
279 packet
.PushBack(byte
);
282 void CCECAdapterMessage::PushEscaped(uint8_t byte
)
287 PushBack(byte
- ESCOFFSET
);
295 bool CCECAdapterMessage::PushReceivedByte(uint8_t byte
)
297 if (byte
== MSGSTART
)
299 if (HasStartMessage())
301 CLibCEC::AddLog(CEC_LOG_WARNING
, "received MSGSTART before MSGEND, removing previous buffer contents");
308 if (bNextByteIsEscaped
)
310 PushBack(byte
+ (uint8_t)ESCOFFSET
);
311 bNextByteIsEscaped
= false;
313 else if (byte
== MSGESC
)
314 bNextByteIsEscaped
= true;
319 return byte
== MSGEND
;
322 cec_adapter_messagecode
CCECAdapterMessage::Message(void) const
324 return packet
.size
>= 2 ?
325 (cec_adapter_messagecode
) (packet
.At(1) & ~(MSGCODE_FRAME_EOM
| MSGCODE_FRAME_ACK
)) :
329 bool CCECAdapterMessage::IsTranmission(void) const
331 cec_adapter_messagecode msgCode
= Message();
332 return msgCode
== MSGCODE_FRAME_ACK
||
333 msgCode
== MSGCODE_FRAME_DATA
||
334 msgCode
== MSGCODE_FRAME_EOM
||
335 msgCode
== MSGCODE_FRAME_START
||
336 msgCode
== MSGCODE_HIGH_ERROR
||
337 msgCode
== MSGCODE_LOW_ERROR
||
338 msgCode
== MSGCODE_RECEIVE_FAILED
||
339 msgCode
== MSGCODE_TRANSMIT_ACK_POLARITY
||
340 msgCode
== MSGCODE_TRANSMIT_EOM
||
341 msgCode
== MSGCODE_TRANSMIT_FAILED_ACK
||
342 msgCode
== MSGCODE_TRANSMIT_FAILED_LINE
||
343 msgCode
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
||
344 msgCode
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
||
345 msgCode
== MSGCODE_TRANSMIT_LINE_TIMEOUT
||
346 msgCode
== MSGCODE_TRANSMIT_SUCCEEDED
;
349 bool CCECAdapterMessage::IsEOM(void) const
351 return packet
.size
>= 2 ?
352 (packet
.At(1) & MSGCODE_FRAME_EOM
) != 0 :
356 bool CCECAdapterMessage::IsACK(void) const
358 return packet
.size
>= 2 ?
359 (packet
.At(1) & MSGCODE_FRAME_ACK
) != 0 :
363 bool CCECAdapterMessage::IsError(void) const
365 cec_adapter_messagecode code
= Message();
366 return (code
== MSGCODE_HIGH_ERROR
||
367 code
== MSGCODE_LOW_ERROR
||
368 code
== MSGCODE_RECEIVE_FAILED
||
369 code
== MSGCODE_COMMAND_REJECTED
||
370 code
== MSGCODE_TRANSMIT_LINE_TIMEOUT
||
371 code
== MSGCODE_TRANSMIT_FAILED_LINE
||
372 code
== MSGCODE_TRANSMIT_FAILED_ACK
||
373 code
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
||
374 code
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
);
377 bool CCECAdapterMessage::NeedsRetry(void) const
379 return Reply() == MSGCODE_NOTHING
||
380 Reply() == MSGCODE_RECEIVE_FAILED
||
381 Reply() == MSGCODE_TIMEOUT_ERROR
||
382 Reply() == MSGCODE_TRANSMIT_FAILED_LINE
||
383 Reply() == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
||
384 Reply() == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
||
385 Reply() == MSGCODE_TRANSMIT_LINE_TIMEOUT
;
388 cec_logical_address
CCECAdapterMessage::Initiator(void) const
390 return packet
.size
>= 3 ?
391 (cec_logical_address
) (packet
.At(2) >> 4) :
395 cec_logical_address
CCECAdapterMessage::Destination(void) const
397 return packet
.size
>= 3 ?
398 (cec_logical_address
) (packet
.At(2) & 0xF) :
402 bool CCECAdapterMessage::HasStartMessage(void) const
404 return packet
.size
>= 1 && packet
.At(0) == MSGSTART
;
407 bool CCECAdapterMessage::PushToCecCommand(cec_command
&command
) const
413 cec_adapter_messagecode msgCode
= Message();
414 if (msgCode
== MSGCODE_FRAME_START
)
419 command
.initiator
= Initiator();
420 command
.destination
= Destination();
421 command
.ack
= IsACK();
422 command
.eom
= IsEOM();
424 return IsEOM() && !IsError();
426 else if (msgCode
== MSGCODE_FRAME_DATA
)
430 command
.PushBack(At(2));
431 command
.eom
= IsEOM();
433 return IsEOM() && !IsError();
439 cec_adapter_messagecode
CCECAdapterMessage::Reply(void) const
441 return response
.size
>= 2 ?
442 (cec_adapter_messagecode
) (response
.At(1) & ~(MSGCODE_FRAME_EOM
| MSGCODE_FRAME_ACK
)) :