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" : "");
133 const char *CCECAdapterMessage::ToString(cec_adapter_messagecode msgCode
)
137 case MSGCODE_NOTHING
:
141 case MSGCODE_TIMEOUT_ERROR
:
143 case MSGCODE_HIGH_ERROR
:
145 case MSGCODE_LOW_ERROR
:
147 case MSGCODE_FRAME_START
:
148 return "FRAME_START";
149 case MSGCODE_FRAME_DATA
:
151 case MSGCODE_RECEIVE_FAILED
:
152 return "RECEIVE_FAILED";
153 case MSGCODE_COMMAND_ACCEPTED
:
154 return "COMMAND_ACCEPTED";
155 case MSGCODE_COMMAND_REJECTED
:
156 return "COMMAND_REJECTED";
157 case MSGCODE_SET_ACK_MASK
:
158 return "SET_ACK_MASK";
159 case MSGCODE_TRANSMIT
:
161 case MSGCODE_TRANSMIT_EOM
:
162 return "TRANSMIT_EOM";
163 case MSGCODE_TRANSMIT_IDLETIME
:
164 return "TRANSMIT_IDLETIME";
165 case MSGCODE_TRANSMIT_ACK_POLARITY
:
166 return "TRANSMIT_ACK_POLARITY";
167 case MSGCODE_TRANSMIT_LINE_TIMEOUT
:
168 return "TRANSMIT_LINE_TIMEOUT";
169 case MSGCODE_TRANSMIT_SUCCEEDED
:
170 return "TRANSMIT_SUCCEEDED";
171 case MSGCODE_TRANSMIT_FAILED_LINE
:
172 return "TRANSMIT_FAILED_LINE";
173 case MSGCODE_TRANSMIT_FAILED_ACK
:
174 return "TRANSMIT_FAILED_ACK";
175 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
:
176 return "TRANSMIT_FAILED_TIMEOUT_DATA";
177 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
:
178 return "TRANSMIT_FAILED_TIMEOUT_LINE";
179 case MSGCODE_FIRMWARE_VERSION
:
180 return "FIRMWARE_VERSION";
181 case MSGCODE_START_BOOTLOADER
:
182 return "START_BOOTLOADER";
183 case MSGCODE_FRAME_EOM
:
185 case MSGCODE_FRAME_ACK
:
187 case MSGCODE_SET_POWERSTATE
:
188 return "SET_POWERSTATE";
189 case MSGCODE_SET_CONTROLLED
:
190 return "SET_CONTROLLED";
191 case MSGCODE_GET_AUTO_ENABLED
:
192 return "GET_AUTO_ENABLED";
193 case MSGCODE_SET_AUTO_ENABLED
:
194 return "SET_AUTO_ENABLED";
195 case MSGCODE_GET_DEFAULT_LOGICAL_ADDRESS
:
196 return "GET_DEFAULT_LOGICAL_ADDRESS";
197 case MSGCODE_SET_DEFAULT_LOGICAL_ADDRESS
:
198 return "SET_DEFAULT_LOGICAL_ADDRESS";
199 case MSGCODE_GET_LOGICAL_ADDRESS_MASK
:
200 return "GET_LOGICAL_ADDRESS_MASK";
201 case MSGCODE_SET_LOGICAL_ADDRESS_MASK
:
202 return "SET_LOGICAL_ADDRESS_MASK";
203 case MSGCODE_GET_PHYSICAL_ADDRESS
:
204 return "GET_PHYSICAL_ADDRESS";
205 case MSGCODE_SET_PHYSICAL_ADDRESS
:
206 return "SET_PHYSICAL_ADDRESS";
207 case MSGCODE_GET_DEVICE_TYPE
:
208 return "GET_DEVICE_TYPE";
209 case MSGCODE_SET_DEVICE_TYPE
:
210 return "SET_DEVICE_TYPE";
211 case MSGCODE_GET_HDMI_VERSION
:
212 return "GET_HDMI_VERSION";
213 case MSGCODE_SET_HDMI_VERSION
:
214 return "SET_HDMI_VERSION";
215 case MSGCODE_GET_OSD_NAME
:
216 return "GET_OSD_NAME";
217 case MSGCODE_SET_OSD_NAME
:
218 return "SET_OSD_NAME";
219 case MSGCODE_WRITE_EEPROM
:
220 return "WRITE_EEPROM";
226 uint8_t CCECAdapterMessage::operator[](uint8_t pos
) const
228 return pos
< packet
.size
? packet
[pos
] : 0;
231 uint8_t CCECAdapterMessage::At(uint8_t pos
) const
233 return pos
< packet
.size
? packet
[pos
] : 0;
236 uint8_t CCECAdapterMessage::Size(void) const
241 bool CCECAdapterMessage::IsEmpty(void) const
243 return packet
.IsEmpty();
246 void CCECAdapterMessage::Clear(void)
248 state
= ADAPTER_MESSAGE_STATE_UNKNOWN
;
249 transmit_timeout
= CEC_DEFAULT_TRANSMIT_TIMEOUT
;
253 bNextByteIsEscaped
= false;
256 void CCECAdapterMessage::Shift(uint8_t iShiftBy
)
258 packet
.Shift(iShiftBy
);
261 void CCECAdapterMessage::Append(CCECAdapterMessage
&data
)
266 void CCECAdapterMessage::Append(cec_datapacket
&data
)
268 for (uint8_t iPtr
= 0; iPtr
< data
.size
; iPtr
++)
269 PushBack(data
[iPtr
]);
272 void CCECAdapterMessage::PushBack(uint8_t byte
)
274 packet
.PushBack(byte
);
277 void CCECAdapterMessage::PushEscaped(uint8_t byte
)
282 PushBack(byte
- ESCOFFSET
);
290 bool CCECAdapterMessage::PushReceivedByte(uint8_t byte
)
292 if (byte
== MSGSTART
)
294 if (HasStartMessage())
296 CLibCEC::AddLog(CEC_LOG_WARNING
, "received MSGSTART before MSGEND, removing previous buffer contents");
303 if (bNextByteIsEscaped
)
305 PushBack(byte
+ (uint8_t)ESCOFFSET
);
306 bNextByteIsEscaped
= false;
308 else if (byte
== MSGESC
)
309 bNextByteIsEscaped
= true;
314 return byte
== MSGEND
;
317 cec_adapter_messagecode
CCECAdapterMessage::Message(void) const
319 return packet
.size
>= 2 ?
320 (cec_adapter_messagecode
) (packet
.At(1) & ~(MSGCODE_FRAME_EOM
| MSGCODE_FRAME_ACK
)) :
324 bool CCECAdapterMessage::IsTranmission(void) const
326 cec_adapter_messagecode msgCode
= Message();
327 return msgCode
== MSGCODE_FRAME_ACK
||
328 msgCode
== MSGCODE_FRAME_DATA
||
329 msgCode
== MSGCODE_FRAME_EOM
||
330 msgCode
== MSGCODE_FRAME_START
||
331 msgCode
== MSGCODE_HIGH_ERROR
||
332 msgCode
== MSGCODE_LOW_ERROR
||
333 msgCode
== MSGCODE_RECEIVE_FAILED
||
334 msgCode
== MSGCODE_TRANSMIT_ACK_POLARITY
||
335 msgCode
== MSGCODE_TRANSMIT_EOM
||
336 msgCode
== MSGCODE_TRANSMIT_FAILED_ACK
||
337 msgCode
== MSGCODE_TRANSMIT_FAILED_LINE
||
338 msgCode
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
||
339 msgCode
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
||
340 msgCode
== MSGCODE_TRANSMIT_LINE_TIMEOUT
||
341 msgCode
== MSGCODE_TRANSMIT_SUCCEEDED
;
344 bool CCECAdapterMessage::IsEOM(void) const
346 return packet
.size
>= 2 ?
347 (packet
.At(1) & MSGCODE_FRAME_EOM
) != 0 :
351 bool CCECAdapterMessage::IsACK(void) const
353 return packet
.size
>= 2 ?
354 (packet
.At(1) & MSGCODE_FRAME_ACK
) != 0 :
358 bool CCECAdapterMessage::IsError(void) const
360 cec_adapter_messagecode code
= Message();
361 return (code
== MSGCODE_HIGH_ERROR
||
362 code
== MSGCODE_LOW_ERROR
||
363 code
== MSGCODE_RECEIVE_FAILED
||
364 code
== MSGCODE_COMMAND_REJECTED
||
365 code
== MSGCODE_TRANSMIT_LINE_TIMEOUT
||
366 code
== MSGCODE_TRANSMIT_FAILED_LINE
||
367 code
== MSGCODE_TRANSMIT_FAILED_ACK
||
368 code
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
||
369 code
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
);
372 bool CCECAdapterMessage::NeedsRetry(void) const
374 return Reply() == MSGCODE_NOTHING
||
375 Reply() == MSGCODE_RECEIVE_FAILED
||
376 Reply() == MSGCODE_TIMEOUT_ERROR
||
377 Reply() == MSGCODE_TRANSMIT_FAILED_LINE
||
378 Reply() == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
||
379 Reply() == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
||
380 Reply() == MSGCODE_TRANSMIT_LINE_TIMEOUT
;
383 cec_logical_address
CCECAdapterMessage::Initiator(void) const
385 return packet
.size
>= 3 ?
386 (cec_logical_address
) (packet
.At(2) >> 4) :
390 cec_logical_address
CCECAdapterMessage::Destination(void) const
392 return packet
.size
>= 3 ?
393 (cec_logical_address
) (packet
.At(2) & 0xF) :
397 bool CCECAdapterMessage::HasStartMessage(void) const
399 return packet
.size
>= 1 && packet
.At(0) == MSGSTART
;
402 bool CCECAdapterMessage::PushToCecCommand(cec_command
&command
) const
408 cec_adapter_messagecode msgCode
= Message();
409 if (msgCode
== MSGCODE_FRAME_START
)
414 command
.initiator
= Initiator();
415 command
.destination
= Destination();
416 command
.ack
= IsACK();
417 command
.eom
= IsEOM();
419 return IsEOM() && !IsError();
421 else if (msgCode
== MSGCODE_FRAME_DATA
)
425 command
.PushBack(At(2));
426 command
.eom
= IsEOM();
428 return IsEOM() && !IsError();
434 cec_adapter_messagecode
CCECAdapterMessage::Reply(void) const
436 return response
.size
>= 2 ?
437 (cec_adapter_messagecode
) (response
.At(1) & ~(MSGCODE_FRAME_EOM
| MSGCODE_FRAME_ACK
)) :