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 iMaxTries
/* = 1 */, uint8_t iLineTimeout
/* = 3 */, uint8_t iRetryLineTimeout
/* = 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 /* set the number of retries */
92 if (command
.opcode
== CEC_OPCODE_NONE
) //TODO
94 else if (command
.initiator
!= CECDEVICE_BROADCAST
)
97 lineTimeout
= iLineTimeout
;
98 retryTimeout
= iRetryLineTimeout
;
102 CStdString
CCECAdapterMessage::ToString(void) const
107 strMsg
= "empty message";
111 strMsg
= ToString(Message());
115 case MSGCODE_TIMEOUT_ERROR
:
116 case MSGCODE_HIGH_ERROR
:
117 case MSGCODE_LOW_ERROR
:
119 uint32_t iLine
= (Size() >= 4) ? (At(2) << 8) | At(3) : 0;
120 uint32_t iTime
= (Size() >= 8) ? (At(4) << 24) | (At(5) << 16) | (At(6) << 8) | At(7) : 0;
121 strMsg
.AppendFormat(" line:%u", iLine
);
122 strMsg
.AppendFormat(" time:%u", iTime
);
125 case MSGCODE_FRAME_START
:
127 strMsg
.AppendFormat(" initiator:%1x destination:%1x ack:%s %s", Initiator(), Destination(), IsACK() ? "high" : "low", IsEOM() ? "eom" : "");
129 case MSGCODE_FRAME_DATA
:
131 strMsg
.AppendFormat(" %02x %s", At(2), IsEOM() ? "eom" : "");
141 const char *CCECAdapterMessage::ToString(cec_adapter_messagecode msgCode
)
145 case MSGCODE_NOTHING
:
149 case MSGCODE_TIMEOUT_ERROR
:
151 case MSGCODE_HIGH_ERROR
:
153 case MSGCODE_LOW_ERROR
:
155 case MSGCODE_FRAME_START
:
156 return "FRAME_START";
157 case MSGCODE_FRAME_DATA
:
159 case MSGCODE_RECEIVE_FAILED
:
160 return "RECEIVE_FAILED";
161 case MSGCODE_COMMAND_ACCEPTED
:
162 return "COMMAND_ACCEPTED";
163 case MSGCODE_COMMAND_REJECTED
:
164 return "COMMAND_REJECTED";
165 case MSGCODE_SET_ACK_MASK
:
166 return "SET_ACK_MASK";
167 case MSGCODE_TRANSMIT
:
169 case MSGCODE_TRANSMIT_EOM
:
170 return "TRANSMIT_EOM";
171 case MSGCODE_TRANSMIT_IDLETIME
:
172 return "TRANSMIT_IDLETIME";
173 case MSGCODE_TRANSMIT_ACK_POLARITY
:
174 return "TRANSMIT_ACK_POLARITY";
175 case MSGCODE_TRANSMIT_LINE_TIMEOUT
:
176 return "TRANSMIT_LINE_TIMEOUT";
177 case MSGCODE_TRANSMIT_SUCCEEDED
:
178 return "TRANSMIT_SUCCEEDED";
179 case MSGCODE_TRANSMIT_FAILED_LINE
:
180 return "TRANSMIT_FAILED_LINE";
181 case MSGCODE_TRANSMIT_FAILED_ACK
:
182 return "TRANSMIT_FAILED_ACK";
183 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
:
184 return "TRANSMIT_FAILED_TIMEOUT_DATA";
185 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
:
186 return "TRANSMIT_FAILED_TIMEOUT_LINE";
187 case MSGCODE_FIRMWARE_VERSION
:
188 return "FIRMWARE_VERSION";
189 case MSGCODE_START_BOOTLOADER
:
190 return "START_BOOTLOADER";
191 case MSGCODE_FRAME_EOM
:
193 case MSGCODE_FRAME_ACK
:
195 case MSGCODE_SET_POWERSTATE
:
196 return "SET_POWERSTATE";
197 case MSGCODE_SET_CONTROLLED
:
198 return "SET_CONTROLLED";
199 case MSGCODE_GET_AUTO_ENABLED
:
200 return "GET_AUTO_ENABLED";
201 case MSGCODE_SET_AUTO_ENABLED
:
202 return "SET_AUTO_ENABLED";
203 case MSGCODE_GET_DEFAULT_LOGICAL_ADDRESS
:
204 return "GET_DEFAULT_LOGICAL_ADDRESS";
205 case MSGCODE_SET_DEFAULT_LOGICAL_ADDRESS
:
206 return "SET_DEFAULT_LOGICAL_ADDRESS";
207 case MSGCODE_GET_LOGICAL_ADDRESS_MASK
:
208 return "GET_LOGICAL_ADDRESS_MASK";
209 case MSGCODE_SET_LOGICAL_ADDRESS_MASK
:
210 return "SET_LOGICAL_ADDRESS_MASK";
211 case MSGCODE_GET_PHYSICAL_ADDRESS
:
212 return "GET_PHYSICAL_ADDRESS";
213 case MSGCODE_SET_PHYSICAL_ADDRESS
:
214 return "SET_PHYSICAL_ADDRESS";
215 case MSGCODE_GET_DEVICE_TYPE
:
216 return "GET_DEVICE_TYPE";
217 case MSGCODE_SET_DEVICE_TYPE
:
218 return "SET_DEVICE_TYPE";
219 case MSGCODE_GET_HDMI_VERSION
:
220 return "GET_HDMI_VERSION";
221 case MSGCODE_SET_HDMI_VERSION
:
222 return "SET_HDMI_VERSION";
223 case MSGCODE_GET_OSD_NAME
:
224 return "GET_OSD_NAME";
225 case MSGCODE_SET_OSD_NAME
:
226 return "SET_OSD_NAME";
227 case MSGCODE_WRITE_EEPROM
:
228 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
;
260 maxTries
= CEC_DEFAULT_TRANSMIT_RETRIES
+ 1;
264 bNextByteIsEscaped
= false;
267 void CCECAdapterMessage::Shift(uint8_t iShiftBy
)
269 packet
.Shift(iShiftBy
);
272 void CCECAdapterMessage::Append(CCECAdapterMessage
&data
)
277 void CCECAdapterMessage::Append(cec_datapacket
&data
)
279 for (uint8_t iPtr
= 0; iPtr
< data
.size
; iPtr
++)
280 PushBack(data
[iPtr
]);
283 void CCECAdapterMessage::PushBack(uint8_t byte
)
285 packet
.PushBack(byte
);
288 void CCECAdapterMessage::PushEscaped(uint8_t byte
)
293 PushBack(byte
- ESCOFFSET
);
301 bool CCECAdapterMessage::PushReceivedByte(uint8_t byte
)
303 if (byte
== MSGSTART
)
305 if (HasStartMessage())
307 CLibCEC::AddLog(CEC_LOG_WARNING
, "received MSGSTART before MSGEND, removing previous buffer contents");
314 if (bNextByteIsEscaped
)
316 PushBack(byte
+ (uint8_t)ESCOFFSET
);
317 bNextByteIsEscaped
= false;
319 else if (byte
== MSGESC
)
320 bNextByteIsEscaped
= true;
325 return byte
== MSGEND
;
328 cec_adapter_messagecode
CCECAdapterMessage::Message(void) const
330 return packet
.size
>= 2 ?
331 (cec_adapter_messagecode
) (packet
.At(1) & ~(MSGCODE_FRAME_EOM
| MSGCODE_FRAME_ACK
)) :
335 bool CCECAdapterMessage::IsTranmission(void) const
337 cec_adapter_messagecode msgCode
= Message();
338 return msgCode
== MSGCODE_FRAME_ACK
||
339 msgCode
== MSGCODE_FRAME_DATA
||
340 msgCode
== MSGCODE_FRAME_EOM
||
341 msgCode
== MSGCODE_FRAME_START
||
342 msgCode
== MSGCODE_HIGH_ERROR
||
343 msgCode
== MSGCODE_LOW_ERROR
||
344 msgCode
== MSGCODE_RECEIVE_FAILED
||
345 msgCode
== MSGCODE_TRANSMIT_ACK_POLARITY
||
346 msgCode
== MSGCODE_TRANSMIT_EOM
||
347 msgCode
== MSGCODE_TRANSMIT_FAILED_ACK
||
348 msgCode
== MSGCODE_TRANSMIT_FAILED_LINE
||
349 msgCode
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
||
350 msgCode
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
||
351 msgCode
== MSGCODE_TRANSMIT_LINE_TIMEOUT
||
352 msgCode
== MSGCODE_TRANSMIT_SUCCEEDED
;
355 bool CCECAdapterMessage::IsEOM(void) const
357 return packet
.size
>= 2 ?
358 (packet
.At(1) & MSGCODE_FRAME_EOM
) != 0 :
362 bool CCECAdapterMessage::IsACK(void) const
364 return packet
.size
>= 2 ?
365 (packet
.At(1) & MSGCODE_FRAME_ACK
) != 0 :
369 bool CCECAdapterMessage::IsError(void) const
371 cec_adapter_messagecode code
= Message();
372 return (code
== MSGCODE_HIGH_ERROR
||
373 code
== MSGCODE_LOW_ERROR
||
374 code
== MSGCODE_RECEIVE_FAILED
||
375 code
== MSGCODE_COMMAND_REJECTED
||
376 code
== MSGCODE_TRANSMIT_LINE_TIMEOUT
||
377 code
== MSGCODE_TRANSMIT_FAILED_LINE
||
378 code
== MSGCODE_TRANSMIT_FAILED_ACK
||
379 code
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
||
380 code
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
);
383 bool CCECAdapterMessage::NeedsRetry(void) const
385 return Reply() == MSGCODE_NOTHING
||
386 Reply() == MSGCODE_RECEIVE_FAILED
||
387 Reply() == MSGCODE_TIMEOUT_ERROR
||
388 Reply() == MSGCODE_TRANSMIT_FAILED_LINE
||
389 Reply() == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
||
390 Reply() == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
||
391 Reply() == MSGCODE_TRANSMIT_LINE_TIMEOUT
;
394 cec_logical_address
CCECAdapterMessage::Initiator(void) const
396 return packet
.size
>= 3 ?
397 (cec_logical_address
) (packet
.At(2) >> 4) :
401 cec_logical_address
CCECAdapterMessage::Destination(void) const
403 return packet
.size
>= 3 ?
404 (cec_logical_address
) (packet
.At(2) & 0xF) :
408 bool CCECAdapterMessage::HasStartMessage(void) const
410 return packet
.size
>= 1 && packet
.At(0) == MSGSTART
;
413 bool CCECAdapterMessage::PushToCecCommand(cec_command
&command
) const
419 cec_adapter_messagecode msgCode
= Message();
420 if (msgCode
== MSGCODE_FRAME_START
)
425 command
.initiator
= Initiator();
426 command
.destination
= Destination();
427 command
.ack
= IsACK();
428 command
.eom
= IsEOM();
430 return IsEOM() && !IsError();
432 else if (msgCode
== MSGCODE_FRAME_DATA
)
436 command
.PushBack(At(2));
437 command
.eom
= IsEOM();
439 return IsEOM() && !IsError();
445 cec_adapter_messagecode
CCECAdapterMessage::Reply(void) const
447 return response
.size
>= 2 ?
448 (cec_adapter_messagecode
) (response
.At(1) & ~(MSGCODE_FRAME_EOM
| MSGCODE_FRAME_ACK
)) :