2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011 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 "CECParser.h"
40 #include "util/StdString.h"
41 #include "libPlatform/serialport.h"
42 #include "util/threads.h"
43 #include "util/timeutils.h"
44 #include "CECDetect.h"
45 #include "Communication.h"
50 #define CEC_MAX_RETRY 5
51 #define CEC_BUTTON_TIMEOUT 500
54 * ICECDevice implementation
57 CCECParser::CCECParser(const char *strDeviceName
, cec_logical_address iLogicalAddress
/* = CECDEVICE_PLAYBACKDEVICE1 */, int iPhysicalAddress
/* = CEC_DEFAULT_PHYSICAL_ADDRESS*/) :
58 m_iCurrentButton(CEC_USER_CONTROL_CODE_UNKNOWN
),
59 m_physicaladdress(iPhysicalAddress
),
60 m_iLogicalAddress(iLogicalAddress
),
61 m_strDeviceName(strDeviceName
)
63 m_communication
= new CCommunication(this);
66 CCECParser::~CCECParser(void)
69 m_communication
->Close();
70 delete m_communication
;
73 bool CCECParser::Open(const char *strPort
, int iTimeoutMs
/* = 10000 */)
78 if (m_communication
->IsOpen())
80 AddLog(CEC_LOG_ERROR
, "connection already open");
84 if (!m_communication
->Open(strPort
, 38400, iTimeoutMs
))
86 AddLog(CEC_LOG_ERROR
, "could not open a connection");
90 if (!SetLogicalAddress(m_iLogicalAddress
))
92 AddLog(CEC_LOG_ERROR
, "could not set the logical address");
99 AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
104 void CCECParser::Close(void)
109 void *CCECParser::Process(void)
111 AddLog(CEC_LOG_DEBUG
, "processor thread started");
113 int64_t now
= GetTimeMs();
116 bool bParseFrame(false);
118 CLockObject
lock(&m_mutex
);
120 if (!m_bStop
&& m_communication
->IsOpen() && m_communication
->Read(msg
, CEC_BUTTON_TIMEOUT
))
121 bParseFrame
= ParseMessage(msg
);
128 CheckKeypressTimeout(now
);
129 CCondition::Sleep(50);
132 AddLog(CEC_LOG_DEBUG
, "processor thread terminated");
136 bool CCECParser::Ping(void)
141 AddLog(CEC_LOG_DEBUG
, "sending ping");
143 output
.push_back(MSGSTART
);
144 PushEscaped(output
, MSGCODE_PING
);
145 output
.push_back(MSGEND
);
147 if (!TransmitFormatted(output
, false))
149 AddLog(CEC_LOG_ERROR
, "could not send ping command");
153 AddLog(CEC_LOG_DEBUG
, "ping tranmitted");
155 // TODO check for pong
159 bool CCECParser::StartBootloader(void)
164 AddLog(CEC_LOG_DEBUG
, "starting the bootloader");
166 output
.push_back(MSGSTART
);
167 PushEscaped(output
, MSGCODE_START_BOOTLOADER
);
168 output
.push_back(MSGEND
);
170 if (!TransmitFormatted(output
, false))
172 AddLog(CEC_LOG_ERROR
, "could not start the bootloader");
176 AddLog(CEC_LOG_DEBUG
, "bootloader start command transmitted");
180 uint8_t CCECParser::GetSourceDestination(cec_logical_address destination
/* = CECDEVICE_BROADCAST */)
182 return ((uint8_t)m_iLogicalAddress
<< 4) + (uint8_t)destination
;
185 bool CCECParser::PowerOffDevices(cec_logical_address address
/* = CECDEVICE_BROADCAST */)
187 return StandbyDevices(address
);
190 bool CCECParser::PowerOnDevices(cec_logical_address address
/* = CECDEVICE_TV */)
196 strLog
.Format("powering on devices with logical address %d", (int8_t)address
);
197 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
199 frame
.push_back(GetSourceDestination(address
));
200 frame
.push_back(CEC_OPCODE_TEXT_VIEW_ON
);
201 return Transmit(frame
);
204 bool CCECParser::StandbyDevices(cec_logical_address address
/* = CECDEVICE_BROADCAST */)
210 strLog
.Format("putting all devices with logical address %d in standby mode", (int8_t)address
);
211 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
213 frame
.push_back(GetSourceDestination(address
));
214 frame
.push_back(CEC_OPCODE_STANDBY
);
215 return Transmit(frame
);
218 bool CCECParser::SetActiveView(void)
223 AddLog(CEC_LOG_DEBUG
, "setting active view");
225 frame
.push_back(GetSourceDestination(CECDEVICE_BROADCAST
));
226 frame
.push_back(CEC_OPCODE_ACTIVE_SOURCE
);
227 frame
.push_back((m_physicaladdress
>> 8) & 0xFF);
228 frame
.push_back(m_physicaladdress
& 0xFF);
229 return Transmit(frame
);
232 bool CCECParser::SetInactiveView(void)
237 AddLog(CEC_LOG_DEBUG
, "setting inactive view");
239 frame
.push_back(GetSourceDestination(CECDEVICE_BROADCAST
));
240 frame
.push_back(CEC_OPCODE_INACTIVE_SOURCE
);
241 frame
.push_back((m_physicaladdress
>> 8) & 0xFF);
242 frame
.push_back(m_physicaladdress
& 0xFF);
243 return Transmit(frame
);
246 bool CCECParser::GetNextLogMessage(cec_log_message
*message
)
248 return m_logBuffer
.Pop(*message
);
251 bool CCECParser::GetNextKeypress(cec_keypress
*key
)
253 return IsRunning() ? m_keyBuffer
.Pop(*key
) : false;
256 bool CCECParser::GetNextCommand(cec_command
*command
)
258 return IsRunning() ? m_commandBuffer
.Pop(*command
) : false;
262 void CCECParser::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, ECecAbortReason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
264 AddLog(CEC_LOG_DEBUG
, "transmitting abort message");
266 frame
.push_back(GetSourceDestination(address
));
267 frame
.push_back(CEC_OPCODE_FEATURE_ABORT
);
268 frame
.push_back(opcode
);
269 frame
.push_back(reason
);
273 void CCECParser::ReportCECVersion(cec_logical_address address
/* = CECDEVICE_TV */)
276 AddLog(CEC_LOG_NOTICE
, "reporting CEC version as 1.3a");
277 frame
.push_back(GetSourceDestination(address
));
278 frame
.push_back(CEC_OPCODE_CEC_VERSION
);
279 frame
.push_back(CEC_VERSION_1_3A
);
283 void CCECParser::ReportPowerState(cec_logical_address address
/*= CECDEVICE_TV */, bool bOn
/* = true */)
287 AddLog(CEC_LOG_NOTICE
, "reporting \"On\" power status");
289 AddLog(CEC_LOG_NOTICE
, "reporting \"Off\" power status");
291 frame
.push_back(GetSourceDestination(address
));
292 frame
.push_back(CEC_OPCODE_REPORT_POWER_STATUS
);
293 frame
.push_back(bOn
? CEC_POWER_STATUS_ON
: CEC_POWER_STATUS_STANDBY
);
297 void CCECParser::ReportMenuState(cec_logical_address address
/* = CECDEVICE_TV */, bool bActive
/* = true */)
301 AddLog(CEC_LOG_NOTICE
, "reporting menu state as active");
303 AddLog(CEC_LOG_NOTICE
, "reporting menu state as inactive");
305 frame
.push_back(GetSourceDestination(address
));
306 frame
.push_back(CEC_OPCODE_MENU_STATUS
);
307 frame
.push_back(bActive
? CEC_MENU_STATE_ACTIVATED
: CEC_MENU_STATE_DEACTIVATED
);
311 void CCECParser::ReportVendorID(cec_logical_address address
/* = CECDEVICE_TV */)
313 AddLog(CEC_LOG_NOTICE
, "vendor ID requested, feature abort");
314 TransmitAbort(address
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
317 void CCECParser::ReportOSDName(cec_logical_address address
/* = CECDEVICE_TV */)
320 const char *osdname
= m_strDeviceName
.c_str();
322 strLog
.Format("reporting OSD name as %s", osdname
);
323 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
324 frame
.push_back(GetSourceDestination(address
));
325 frame
.push_back(CEC_OPCODE_SET_OSD_NAME
);
327 for (unsigned int i
= 0; i
< strlen(osdname
); i
++)
328 frame
.push_back(osdname
[i
]);
333 void CCECParser::ReportPhysicalAddress(void)
337 strLog
.Format("reporting physical address as %04x", m_physicaladdress
);
338 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
339 frame
.push_back(GetSourceDestination(CECDEVICE_BROADCAST
));
340 frame
.push_back(CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
341 frame
.push_back((m_physicaladdress
>> 8) & 0xFF);
342 frame
.push_back(m_physicaladdress
& 0xFF);
343 frame
.push_back(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
347 void CCECParser::BroadcastActiveSource(void)
350 AddLog(CEC_LOG_NOTICE
, "broadcasting active source");
351 frame
.push_back(GetSourceDestination(CECDEVICE_BROADCAST
));
352 frame
.push_back(CEC_OPCODE_ACTIVE_SOURCE
);
353 frame
.push_back((m_physicaladdress
>> 8) & 0xFF);
354 frame
.push_back(m_physicaladdress
& 0xFF);
358 bool CCECParser::TransmitFormatted(const cec_frame
&data
, bool bWaitForAck
/* = true */)
360 CLockObject
lock(&m_mutex
);
361 if (!m_communication
|| !m_communication
->Write(data
))
366 CCondition::Sleep((int) data
.size() * 24 /*data*/ + 5 /*start bit (4.5 ms)*/ + 50 /* to be on the safe side */);
367 if (bWaitForAck
&& !WaitForAck())
369 AddLog(CEC_LOG_DEBUG
, "did not receive ACK");
376 bool CCECParser::Transmit(const cec_frame
&data
, bool bWaitForAck
/* = true */)
378 CStdString txStr
= "transmit ";
379 for (unsigned int i
= 0; i
< data
.size(); i
++)
380 txStr
.AppendFormat(" %02x", data
[i
]);
381 AddLog(CEC_LOG_DEBUG
, txStr
.c_str());
385 AddLog(CEC_LOG_WARNING
, "transmit buffer is empty");
391 //set ack polarity to high when transmitting to the broadcast address
392 //set ack polarity low when transmitting to any other address
393 output
.push_back(MSGSTART
);
394 PushEscaped(output
, MSGCODE_TRANSMIT_ACK_POLARITY
);
396 if ((data
[0] & 0xF) == 0xF)
397 PushEscaped(output
, CEC_TRUE
);
399 PushEscaped(output
, CEC_FALSE
);
401 output
.push_back(MSGEND
);
403 for (unsigned int i
= 0; i
< data
.size(); i
++)
405 output
.push_back(MSGSTART
);
407 if (i
== data
.size() - 1)
408 PushEscaped(output
, MSGCODE_TRANSMIT_EOM
);
410 PushEscaped(output
, MSGCODE_TRANSMIT
);
412 PushEscaped(output
, data
[i
]);
414 output
.push_back(MSGEND
);
417 return TransmitFormatted(output
, bWaitForAck
);
420 bool CCECParser::WaitForAck(int iTimeout
/* = 1000 */)
425 int64_t iNow
= GetTimeMs();
426 int64_t iTargetTime
= iNow
+ (int64_t) iTimeout
;
428 while (!bGotAck
&& !bError
&& (iTimeout
<= 0 || iNow
< iTargetTime
))
431 while (!bGotAck
&& !bError
&& m_communication
->Read(msg
, iTimeout
))
433 uint8_t iCode
= msg
[0] & ~(MSGCODE_FRAME_EOM
| MSGCODE_FRAME_ACK
);
437 case MSGCODE_COMMAND_ACCEPTED
:
438 AddLog(CEC_LOG_DEBUG
, "MSGCODE_COMMAND_ACCEPTED");
440 case MSGCODE_TRANSMIT_SUCCEEDED
:
441 AddLog(CEC_LOG_DEBUG
, "MSGCODE_TRANSMIT_SUCCEEDED");
445 case MSGCODE_RECEIVE_FAILED
:
446 AddLog(CEC_LOG_WARNING
, "MSGCODE_RECEIVE_FAILED");
449 case MSGCODE_COMMAND_REJECTED
:
450 AddLog(CEC_LOG_WARNING
, "MSGCODE_COMMAND_REJECTED");
453 case MSGCODE_TRANSMIT_FAILED_LINE
:
454 AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_LINE");
457 case MSGCODE_TRANSMIT_FAILED_ACK
:
458 AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_ACK");
461 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
:
462 AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA");
465 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
:
466 AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE");
470 m_frameBuffer
.Push(msg
);
471 bGotAck
= (msg
[0] & MSGCODE_FRAME_ACK
) != 0;
478 return bGotAck
&& !bError
;
481 bool CCECParser::ParseMessage(cec_frame
&msg
)
489 uint8_t iCode
= msg
[0] & ~(MSGCODE_FRAME_EOM
| MSGCODE_FRAME_ACK
);
490 bool bEom
= (msg
[0] & MSGCODE_FRAME_EOM
) != 0;
491 bool bAck
= (msg
[0] & MSGCODE_FRAME_ACK
) != 0;
495 case MSGCODE_NOTHING
:
496 AddLog(CEC_LOG_DEBUG
, "MSGCODE_NOTHING");
498 case MSGCODE_TIMEOUT_ERROR
:
499 case MSGCODE_HIGH_ERROR
:
500 case MSGCODE_LOW_ERROR
:
502 if (iCode
== MSGCODE_TIMEOUT_ERROR
)
503 logStr
= "MSGCODE_TIMEOUT";
504 else if (iCode
== MSGCODE_HIGH_ERROR
)
505 logStr
= "MSGCODE_HIGH_ERROR";
507 logStr
= "MSGCODE_LOW_ERROR";
509 int iLine
= (msg
.size() >= 3) ? (msg
[1] << 8) | (msg
[2]) : 0;
510 uint32_t iTime
= (msg
.size() >= 7) ? (msg
[3] << 24) | (msg
[4] << 16) | (msg
[5] << 8) | (msg
[6]) : 0;
511 logStr
.AppendFormat(" line:%i", iLine
);
512 logStr
.AppendFormat(" time:%u", iTime
);
513 AddLog(CEC_LOG_WARNING
, logStr
.c_str());
516 case MSGCODE_FRAME_START
:
518 logStr
= "MSGCODE_FRAME_START";
519 m_currentframe
.clear();
522 int iInitiator
= msg
[1] >> 4;
523 int iDestination
= msg
[1] & 0xF;
524 logStr
.AppendFormat(" initiator:%u destination:%u ack:%s %s", iInitiator
, iDestination
, bAck
? "high" : "low", bEom
? "eom" : "");
526 m_currentframe
.push_back(msg
[1]);
528 AddLog(CEC_LOG_DEBUG
, logStr
.c_str());
531 case MSGCODE_FRAME_DATA
:
533 logStr
= "MSGCODE_FRAME_DATA";
536 uint8_t iData
= msg
[1];
537 logStr
.AppendFormat(" %02x", iData
);
538 m_currentframe
.push_back(iData
);
540 AddLog(CEC_LOG_DEBUG
, logStr
.c_str());
552 void CCECParser::ParseCurrentFrame(void)
554 uint8_t initiator
= m_currentframe
[0] >> 4;
555 uint8_t destination
= m_currentframe
[0] & 0xF;
558 dataStr
.Format("received frame: initiator: %u destination: %u", initiator
, destination
);
560 if (m_currentframe
.size() > 1)
563 for (unsigned int i
= 1; i
< m_currentframe
.size(); i
++)
564 dataStr
.AppendFormat(" %02x", m_currentframe
[i
]);
566 AddLog(CEC_LOG_DEBUG
, dataStr
.c_str());
568 if (m_currentframe
.size() <= 1)
572 cec_opcode opCode
= (cec_opcode
) m_currentframe
[1];
573 if (destination
== (uint16_t) m_iLogicalAddress
)
577 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
578 ReportPhysicalAddress();
581 case CEC_OPCODE_GIVE_OSD_NAME
:
582 ReportOSDName((cec_logical_address
)initiator
);
584 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
585 ReportVendorID((cec_logical_address
)initiator
);
587 case CEC_OPCODE_MENU_REQUEST
:
588 ReportMenuState((cec_logical_address
)initiator
);
590 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
591 ReportPowerState((cec_logical_address
)initiator
);
593 case CEC_OPCODE_GET_CEC_VERSION
:
594 ReportCECVersion((cec_logical_address
)initiator
);
596 case CEC_OPCODE_USER_CONTROL_PRESSED
:
597 if (m_currentframe
.size() > 2)
601 if (m_currentframe
[2] <= CEC_USER_CONTROL_CODE_MAX
)
603 m_iCurrentButton
= (cec_user_control_code
) m_currentframe
[2];
604 m_buttontime
= GetTimeMs();
608 case CEC_OPCODE_USER_CONTROL_RELEASE
:
612 cec_frame params
= m_currentframe
;
613 params
.erase(params
.begin(), params
.begin() + 2);
614 AddCommand((cec_logical_address
) initiator
, (cec_logical_address
) destination
, opCode
, ¶ms
);
618 else if (destination
== (uint8_t) CECDEVICE_BROADCAST
)
620 if (opCode
== CEC_OPCODE_REQUEST_ACTIVE_SOURCE
)
622 BroadcastActiveSource();
624 else if (opCode
== CEC_OPCODE_SET_STREAM_PATH
)
626 if (m_currentframe
.size() >= 4)
628 int streamaddr
= ((int)m_currentframe
[2] << 8) | ((int)m_currentframe
[3]);
630 strLog
.Format("%i requests stream path from physical address %04x", initiator
, streamaddr
);
631 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
632 if (streamaddr
== m_physicaladdress
)
633 BroadcastActiveSource();
638 cec_frame params
= m_currentframe
;
639 params
.erase(params
.begin(), params
.begin() + 2);
640 AddCommand((cec_logical_address
) initiator
, (cec_logical_address
) destination
, opCode
, ¶ms
);
646 strLog
.Format("ignoring frame: destination: %u != %u", destination
, (uint16_t)m_iLogicalAddress
);
647 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
651 void CCECParser::PushEscaped(cec_frame
&vec
, uint8_t byte
)
653 if (byte
>= MSGESC
&& byte
!= MSGSTART
)
655 vec
.push_back(MSGESC
);
656 vec
.push_back(byte
- ESCOFFSET
);
664 void CCECParser::CheckKeypressTimeout(int64_t now
)
666 if (m_iCurrentButton
!= CEC_USER_CONTROL_CODE_UNKNOWN
&& now
- m_buttontime
> CEC_BUTTON_TIMEOUT
)
669 m_iCurrentButton
= CEC_USER_CONTROL_CODE_UNKNOWN
;
673 bool CCECParser::SetLogicalAddress(cec_logical_address iLogicalAddress
)
676 strLog
.Format("setting logical address to %d", iLogicalAddress
);
677 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
679 m_iLogicalAddress
= iLogicalAddress
;
680 return SetAckMask(0x1 << (uint8_t)m_iLogicalAddress
);
683 bool CCECParser::SetAckMask(uint16_t iMask
)
686 strLog
.Format("setting ackmask to %2x", iMask
);
687 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
691 output
.push_back(MSGSTART
);
692 PushEscaped(output
, MSGCODE_SET_ACK_MASK
);
693 PushEscaped(output
, iMask
>> 8);
694 PushEscaped(output
, (uint8_t)iMask
);
695 output
.push_back(MSGEND
);
697 if (!m_communication
->Write(output
))
699 AddLog(CEC_LOG_ERROR
, "could not set the ackmask");
706 void CCECParser::AddLog(cec_log_level level
, const string
&strMessage
)
708 cec_log_message message
;
709 message
.level
= level
;
710 message
.message
.assign(strMessage
.c_str());
711 m_logBuffer
.Push(message
);
714 void CCECParser::AddKey(void)
716 if (m_iCurrentButton
!= CEC_USER_CONTROL_CODE_UNKNOWN
)
719 key
.duration
= (unsigned int) (GetTimeMs() - m_buttontime
);
720 key
.keycode
= m_iCurrentButton
;
721 m_keyBuffer
.Push(key
);
722 m_iCurrentButton
= CEC_USER_CONTROL_CODE_UNKNOWN
;
727 void CCECParser::AddCommand(cec_logical_address source
, cec_logical_address destination
, cec_opcode opcode
, cec_frame
*parameters
)
730 command
.source
= source
;
731 command
.destination
= destination
;
732 command
.opcode
= opcode
;
734 command
.parameters
= *parameters
;
735 if (m_commandBuffer
.Push(command
))
738 strDebug
.Format("stored command '%d' in the command buffer. buffer size = %d", opcode
, m_commandBuffer
.Size());
739 AddLog(CEC_LOG_DEBUG
, strDebug
);
743 AddLog(CEC_LOG_WARNING
, "command buffer is full");
747 int CCECParser::GetMinVersion(void)
749 return CEC_MIN_VERSION
;
752 int CCECParser::GetLibVersion(void)
754 return CEC_LIB_VERSION
;
757 int CCECParser::FindDevices(std::vector
<cec_device
> &deviceList
, const char *strDevicePath
/* = NULL */)
761 strDebug
.Format("trying to autodetect the com port for device path '%s'", strDevicePath
);
763 strDebug
.Format("trying to autodetect all CEC adapters");
764 AddLog(CEC_LOG_DEBUG
, strDebug
);
766 return CCECDetect::FindDevices(deviceList
, strDevicePath
);
769 DECLSPEC
void * CECCreate(const char *strDeviceName
, CEC::cec_logical_address iLogicalAddress
/*= CEC::CECDEVICE_PLAYBACKDEVICE1 */, int iPhysicalAddress
/* = CEC_DEFAULT_PHYSICAL_ADDRESS */)
771 return static_cast< void* > (new CCECParser(strDeviceName
, iLogicalAddress
, iPhysicalAddress
));