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_SET_POWERSTATE
:
191 return "SET_POWERSTATE";
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";
229 uint8_t CCECAdapterMessage::operator[](uint8_t pos
) const
231 return pos
< packet
.size
? packet
[pos
] : 0;
234 uint8_t CCECAdapterMessage::At(uint8_t pos
) const
236 return pos
< packet
.size
? packet
[pos
] : 0;
239 uint8_t CCECAdapterMessage::Size(void) const
244 bool CCECAdapterMessage::IsEmpty(void) const
246 return packet
.IsEmpty();
249 void CCECAdapterMessage::Clear(void)
251 state
= ADAPTER_MESSAGE_STATE_UNKNOWN
;
252 transmit_timeout
= CEC_DEFAULT_TRANSMIT_TIMEOUT
;
256 bNextByteIsEscaped
= false;
259 void CCECAdapterMessage::Shift(uint8_t iShiftBy
)
261 packet
.Shift(iShiftBy
);
264 void CCECAdapterMessage::Append(CCECAdapterMessage
&data
)
269 void CCECAdapterMessage::Append(cec_datapacket
&data
)
271 for (uint8_t iPtr
= 0; iPtr
< data
.size
; iPtr
++)
272 PushBack(data
[iPtr
]);
275 void CCECAdapterMessage::PushBack(uint8_t byte
)
277 packet
.PushBack(byte
);
280 void CCECAdapterMessage::PushEscaped(uint8_t byte
)
285 PushBack(byte
- ESCOFFSET
);
293 bool CCECAdapterMessage::PushReceivedByte(uint8_t byte
)
295 if (byte
== MSGSTART
)
297 if (HasStartMessage())
299 CLibCEC::AddLog(CEC_LOG_WARNING
, "received MSGSTART before MSGEND, removing previous buffer contents");
306 if (bNextByteIsEscaped
)
308 PushBack(byte
+ (uint8_t)ESCOFFSET
);
309 bNextByteIsEscaped
= false;
311 else if (byte
== MSGESC
)
312 bNextByteIsEscaped
= true;
317 return byte
== MSGEND
;
320 cec_adapter_messagecode
CCECAdapterMessage::Message(void) const
322 return packet
.size
>= 2 ?
323 (cec_adapter_messagecode
) (packet
.At(1) & ~(MSGCODE_FRAME_EOM
| MSGCODE_FRAME_ACK
)) :
327 bool CCECAdapterMessage::IsTranmission(void) const
329 cec_adapter_messagecode msgCode
= Message();
330 return msgCode
== MSGCODE_FRAME_ACK
||
331 msgCode
== MSGCODE_FRAME_DATA
||
332 msgCode
== MSGCODE_FRAME_EOM
||
333 msgCode
== MSGCODE_FRAME_START
||
334 msgCode
== MSGCODE_HIGH_ERROR
||
335 msgCode
== MSGCODE_LOW_ERROR
||
336 msgCode
== MSGCODE_RECEIVE_FAILED
||
337 msgCode
== MSGCODE_TRANSMIT_ACK_POLARITY
||
338 msgCode
== MSGCODE_TRANSMIT_EOM
||
339 msgCode
== MSGCODE_TRANSMIT_FAILED_ACK
||
340 msgCode
== MSGCODE_TRANSMIT_FAILED_LINE
||
341 msgCode
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
||
342 msgCode
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
||
343 msgCode
== MSGCODE_TRANSMIT_LINE_TIMEOUT
||
344 msgCode
== MSGCODE_TRANSMIT_SUCCEEDED
;
347 bool CCECAdapterMessage::IsEOM(void) const
349 return packet
.size
>= 2 ?
350 (packet
.At(1) & MSGCODE_FRAME_EOM
) != 0 :
354 bool CCECAdapterMessage::IsACK(void) const
356 return packet
.size
>= 2 ?
357 (packet
.At(1) & MSGCODE_FRAME_ACK
) != 0 :
361 bool CCECAdapterMessage::IsError(void) const
363 cec_adapter_messagecode code
= Message();
364 return (code
== MSGCODE_HIGH_ERROR
||
365 code
== MSGCODE_LOW_ERROR
||
366 code
== MSGCODE_RECEIVE_FAILED
||
367 code
== MSGCODE_COMMAND_REJECTED
||
368 code
== MSGCODE_TRANSMIT_LINE_TIMEOUT
||
369 code
== MSGCODE_TRANSMIT_FAILED_LINE
||
370 code
== MSGCODE_TRANSMIT_FAILED_ACK
||
371 code
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
||
372 code
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
);
375 bool CCECAdapterMessage::NeedsRetry(void) const
377 return Reply() == MSGCODE_NOTHING
||
378 Reply() == MSGCODE_RECEIVE_FAILED
||
379 Reply() == MSGCODE_TIMEOUT_ERROR
||
380 Reply() == MSGCODE_TRANSMIT_FAILED_LINE
||
381 Reply() == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
||
382 Reply() == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
||
383 Reply() == MSGCODE_TRANSMIT_LINE_TIMEOUT
;
386 cec_logical_address
CCECAdapterMessage::Initiator(void) const
388 return packet
.size
>= 3 ?
389 (cec_logical_address
) (packet
.At(2) >> 4) :
393 cec_logical_address
CCECAdapterMessage::Destination(void) const
395 return packet
.size
>= 3 ?
396 (cec_logical_address
) (packet
.At(2) & 0xF) :
400 bool CCECAdapterMessage::HasStartMessage(void) const
402 return packet
.size
>= 1 && packet
.At(0) == MSGSTART
;
405 bool CCECAdapterMessage::PushToCecCommand(cec_command
&command
) const
411 cec_adapter_messagecode msgCode
= Message();
412 if (msgCode
== MSGCODE_FRAME_START
)
417 command
.initiator
= Initiator();
418 command
.destination
= Destination();
419 command
.ack
= IsACK();
420 command
.eom
= IsEOM();
422 return IsEOM() && !IsError();
424 else if (msgCode
== MSGCODE_FRAME_DATA
)
428 command
.PushBack(At(2));
429 command
.eom
= IsEOM();
431 return IsEOM() && !IsError();
437 cec_adapter_messagecode
CCECAdapterMessage::Reply(void) const
439 return response
.size
>= 2 ?
440 (cec_adapter_messagecode
) (response
.At(1) & ~(MSGCODE_FRAME_EOM
| MSGCODE_FRAME_ACK
)) :