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 for (uint8_t iPtr
= 2; iPtr
< Size(); iPtr
++)
130 if (At(iPtr
) != MSGEND
)
131 strMsg
.AppendFormat(" %02x", At(iPtr
));
136 return std::string(strMsg
.c_str());
139 const char *CCECAdapterMessage::ToString(cec_adapter_messagecode msgCode
)
143 case MSGCODE_NOTHING
:
147 case MSGCODE_TIMEOUT_ERROR
:
149 case MSGCODE_HIGH_ERROR
:
151 case MSGCODE_LOW_ERROR
:
153 case MSGCODE_FRAME_START
:
154 return "FRAME_START";
155 case MSGCODE_FRAME_DATA
:
157 case MSGCODE_RECEIVE_FAILED
:
158 return "RECEIVE_FAILED";
159 case MSGCODE_COMMAND_ACCEPTED
:
160 return "COMMAND_ACCEPTED";
161 case MSGCODE_COMMAND_REJECTED
:
162 return "COMMAND_REJECTED";
163 case MSGCODE_SET_ACK_MASK
:
164 return "SET_ACK_MASK";
165 case MSGCODE_TRANSMIT
:
167 case MSGCODE_TRANSMIT_EOM
:
168 return "TRANSMIT_EOM";
169 case MSGCODE_TRANSMIT_IDLETIME
:
170 return "TRANSMIT_IDLETIME";
171 case MSGCODE_TRANSMIT_ACK_POLARITY
:
172 return "TRANSMIT_ACK_POLARITY";
173 case MSGCODE_TRANSMIT_LINE_TIMEOUT
:
174 return "TRANSMIT_LINE_TIMEOUT";
175 case MSGCODE_TRANSMIT_SUCCEEDED
:
176 return "TRANSMIT_SUCCEEDED";
177 case MSGCODE_TRANSMIT_FAILED_LINE
:
178 return "TRANSMIT_FAILED_LINE";
179 case MSGCODE_TRANSMIT_FAILED_ACK
:
180 return "TRANSMIT_FAILED_ACK";
181 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
:
182 return "TRANSMIT_FAILED_TIMEOUT_DATA";
183 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
:
184 return "TRANSMIT_FAILED_TIMEOUT_LINE";
185 case MSGCODE_FIRMWARE_VERSION
:
186 return "FIRMWARE_VERSION";
187 case MSGCODE_START_BOOTLOADER
:
188 return "START_BOOTLOADER";
189 case MSGCODE_FRAME_EOM
:
191 case MSGCODE_FRAME_ACK
:
193 case MSGCODE_GET_BUILDDATE
:
194 return "GET_BUILDDATE";
195 case MSGCODE_SET_CONTROLLED
:
196 return "SET_CONTROLLED";
197 case MSGCODE_GET_AUTO_ENABLED
:
198 return "GET_AUTO_ENABLED";
199 case MSGCODE_SET_AUTO_ENABLED
:
200 return "SET_AUTO_ENABLED";
201 case MSGCODE_GET_DEFAULT_LOGICAL_ADDRESS
:
202 return "GET_DEFAULT_LOGICAL_ADDRESS";
203 case MSGCODE_SET_DEFAULT_LOGICAL_ADDRESS
:
204 return "SET_DEFAULT_LOGICAL_ADDRESS";
205 case MSGCODE_GET_LOGICAL_ADDRESS_MASK
:
206 return "GET_LOGICAL_ADDRESS_MASK";
207 case MSGCODE_SET_LOGICAL_ADDRESS_MASK
:
208 return "SET_LOGICAL_ADDRESS_MASK";
209 case MSGCODE_GET_PHYSICAL_ADDRESS
:
210 return "GET_PHYSICAL_ADDRESS";
211 case MSGCODE_SET_PHYSICAL_ADDRESS
:
212 return "SET_PHYSICAL_ADDRESS";
213 case MSGCODE_GET_DEVICE_TYPE
:
214 return "GET_DEVICE_TYPE";
215 case MSGCODE_SET_DEVICE_TYPE
:
216 return "SET_DEVICE_TYPE";
217 case MSGCODE_GET_HDMI_VERSION
:
218 return "GET_HDMI_VERSION";
219 case MSGCODE_SET_HDMI_VERSION
:
220 return "SET_HDMI_VERSION";
221 case MSGCODE_GET_OSD_NAME
:
222 return "GET_OSD_NAME";
223 case MSGCODE_SET_OSD_NAME
:
224 return "SET_OSD_NAME";
225 case MSGCODE_WRITE_EEPROM
:
226 return "WRITE_EEPROM";
234 uint8_t CCECAdapterMessage::operator[](uint8_t pos
) const
236 return pos
< packet
.size
? packet
[pos
] : 0;
239 uint8_t CCECAdapterMessage::At(uint8_t pos
) const
241 return pos
< packet
.size
? packet
[pos
] : 0;
244 uint8_t CCECAdapterMessage::Size(void) const
249 bool CCECAdapterMessage::IsEmpty(void) const
251 return packet
.IsEmpty();
254 void CCECAdapterMessage::Clear(void)
256 state
= ADAPTER_MESSAGE_STATE_UNKNOWN
;
257 transmit_timeout
= CEC_DEFAULT_TRANSMIT_TIMEOUT
;
261 bNextByteIsEscaped
= false;
264 void CCECAdapterMessage::Shift(uint8_t iShiftBy
)
266 packet
.Shift(iShiftBy
);
269 void CCECAdapterMessage::Append(CCECAdapterMessage
&data
)
274 void CCECAdapterMessage::Append(cec_datapacket
&data
)
276 for (uint8_t iPtr
= 0; iPtr
< data
.size
; iPtr
++)
277 PushBack(data
[iPtr
]);
280 void CCECAdapterMessage::PushBack(uint8_t byte
)
282 packet
.PushBack(byte
);
285 void CCECAdapterMessage::PushEscaped(uint8_t byte
)
290 PushBack(byte
- ESCOFFSET
);
298 bool CCECAdapterMessage::PushReceivedByte(uint8_t byte
)
300 if (byte
== MSGSTART
)
302 if (HasStartMessage())
304 //TODO CLibCEC::AddLog(CEC_LOG_WARNING, "received MSGSTART before MSGEND, removing previous buffer contents");
311 if (bNextByteIsEscaped
)
313 PushBack(byte
+ (uint8_t)ESCOFFSET
);
314 bNextByteIsEscaped
= false;
316 else if (byte
== MSGESC
)
317 bNextByteIsEscaped
= true;
322 return byte
== MSGEND
;
325 cec_adapter_messagecode
CCECAdapterMessage::Message(void) const
327 return packet
.size
>= 2 ?
328 (cec_adapter_messagecode
) (packet
.At(1) & ~(MSGCODE_FRAME_EOM
| MSGCODE_FRAME_ACK
)) :
332 bool CCECAdapterMessage::IsTranmission(void) const
334 cec_adapter_messagecode msgCode
= Message();
335 return msgCode
== MSGCODE_FRAME_ACK
||
336 msgCode
== MSGCODE_FRAME_DATA
||
337 msgCode
== MSGCODE_FRAME_EOM
||
338 msgCode
== MSGCODE_FRAME_START
||
339 msgCode
== MSGCODE_HIGH_ERROR
||
340 msgCode
== MSGCODE_LOW_ERROR
||
341 msgCode
== MSGCODE_RECEIVE_FAILED
||
342 msgCode
== MSGCODE_TRANSMIT_ACK_POLARITY
||
343 msgCode
== MSGCODE_TRANSMIT_EOM
||
344 msgCode
== MSGCODE_TRANSMIT_FAILED_ACK
||
345 msgCode
== MSGCODE_TRANSMIT_FAILED_LINE
||
346 msgCode
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
||
347 msgCode
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
||
348 msgCode
== MSGCODE_TRANSMIT_LINE_TIMEOUT
||
349 msgCode
== MSGCODE_TRANSMIT_SUCCEEDED
;
352 bool CCECAdapterMessage::IsEOM(void) const
354 return packet
.size
>= 2 ?
355 (packet
.At(1) & MSGCODE_FRAME_EOM
) != 0 :
359 bool CCECAdapterMessage::IsACK(void) const
361 return packet
.size
>= 2 ?
362 (packet
.At(1) & MSGCODE_FRAME_ACK
) != 0 :
366 bool CCECAdapterMessage::IsError(void) const
368 cec_adapter_messagecode code
= Message();
369 return (code
== MSGCODE_HIGH_ERROR
||
370 code
== MSGCODE_LOW_ERROR
||
371 code
== MSGCODE_RECEIVE_FAILED
||
372 code
== MSGCODE_COMMAND_REJECTED
||
373 code
== MSGCODE_TRANSMIT_LINE_TIMEOUT
||
374 code
== MSGCODE_TRANSMIT_FAILED_LINE
||
375 code
== MSGCODE_TRANSMIT_FAILED_ACK
||
376 code
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
||
377 code
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
);
380 bool CCECAdapterMessage::NeedsRetry(void) const
382 return Reply() == MSGCODE_NOTHING
||
383 Reply() == MSGCODE_RECEIVE_FAILED
||
384 Reply() == MSGCODE_TIMEOUT_ERROR
||
385 Reply() == MSGCODE_TRANSMIT_FAILED_LINE
||
386 Reply() == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
||
387 Reply() == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
||
388 Reply() == MSGCODE_TRANSMIT_LINE_TIMEOUT
;
391 cec_logical_address
CCECAdapterMessage::Initiator(void) const
393 return packet
.size
>= 3 ?
394 (cec_logical_address
) (packet
.At(2) >> 4) :
398 cec_logical_address
CCECAdapterMessage::Destination(void) const
400 return packet
.size
>= 3 ?
401 (cec_logical_address
) (packet
.At(2) & 0xF) :
405 bool CCECAdapterMessage::HasStartMessage(void) const
407 return packet
.size
>= 1 && packet
.At(0) == MSGSTART
;
410 bool CCECAdapterMessage::PushToCecCommand(cec_command
&command
) const
416 cec_adapter_messagecode msgCode
= Message();
417 if (msgCode
== MSGCODE_FRAME_START
)
422 command
.initiator
= Initiator();
423 command
.destination
= Destination();
424 command
.ack
= IsACK();
425 command
.eom
= IsEOM();
427 return IsEOM() && !IsError();
429 else if (msgCode
== MSGCODE_FRAME_DATA
)
433 command
.PushBack(At(2));
434 command
.eom
= IsEOM();
436 return IsEOM() && !IsError();
442 cec_adapter_messagecode
CCECAdapterMessage::Reply(void) const
444 return response
.size
>= 2 ?
445 (cec_adapter_messagecode
) (response
.At(1) & ~(MSGCODE_FRAME_EOM
| MSGCODE_FRAME_ACK
)) :