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"
49 #define CEC_MAX_RETRY 5
52 * ICECDevice implementation
55 CCECParser::CCECParser(const char *strDeviceName
, cec_logical_address iLogicalAddress
/* = CECDEVICE_PLAYBACKDEVICE1 */, int iPhysicalAddress
/* = CEC_DEFAULT_PHYSICAL_ADDRESS*/) :
59 m_iCurrentButton(CEC_USER_CONTROL_CODE_UNKNOWN
),
60 m_physicaladdress(iPhysicalAddress
),
61 m_iLogicalAddress(iLogicalAddress
),
62 m_strDeviceName(strDeviceName
),
65 m_serialport
= new CSerialPort
;
68 CCECParser::~CCECParser(void)
71 m_serialport
->Close();
75 bool CCECParser::Open(const char *strPort
, int iTimeoutMs
/* = 10000 */)
79 if (!(bReturn
= m_serialport
->Open(strPort
, 38400)))
82 strError
.Format("error opening serial port '%s': %s", strPort
, m_serialport
->GetError().c_str());
83 AddLog(CEC_LOG_ERROR
, strError
);
87 //clear any input bytes
89 m_serialport
->Read(buff
, sizeof(buff
), CEC_SETTLE_DOWN_TIME
);
92 bReturn
= SetLogicalAddress(m_iLogicalAddress
);
97 strError
.Format("error opening serial port '%s': %s", strPort
, m_serialport
->GetError().c_str());
98 AddLog(CEC_LOG_ERROR
, strError
);
105 if (pthread_create(&m_thread
, NULL
, (void *(*) (void *))&CCECParser::ThreadHandler
, (void *)this) == 0)
106 pthread_detach(m_thread
);
114 bool CCECParser::Close(int iTimeoutMs
/* = 2000 */)
120 bExit
= m_exitCondition
.Wait(&m_mutex
, iTimeoutMs
);
125 pthread_join(m_thread
, NULL
);
132 void *CCECParser::ThreadHandler(CCECParser
*parser
)
139 bool CCECParser::Process(void)
141 int64_t now
= GetTimeMs();
145 CLockObject
lock(&m_mutex
, 1000);
148 if (!ReadFromDevice(100))
156 //AddLog(CEC_LOG_DEBUG, "processing messages");
159 CheckKeypressTimeout(now
);
160 CCondition::Sleep(50);
163 AddLog(CEC_LOG_DEBUG
, "reader thread terminated");
165 m_exitCondition
.Signal();
169 bool CCECParser::Ping(void)
174 AddLog(CEC_LOG_DEBUG
, "sending ping");
176 output
.push_back(MSGSTART
);
177 PushEscaped(output
, MSGCODE_PING
);
178 output
.push_back(MSGEND
);
180 if (!TransmitFormatted(output
, false, (int64_t) 5000))
182 AddLog(CEC_LOG_ERROR
, "could not send ping command");
186 AddLog(CEC_LOG_DEBUG
, "ping tranmitted");
188 // TODO check for pong
192 bool CCECParser::StartBootloader(void)
197 AddLog(CEC_LOG_DEBUG
, "starting the bootloader");
199 output
.push_back(MSGSTART
);
200 PushEscaped(output
, MSGCODE_START_BOOTLOADER
);
201 output
.push_back(MSGEND
);
203 if (!TransmitFormatted(output
, false, (int64_t) 5000))
205 AddLog(CEC_LOG_ERROR
, "could not start the bootloader");
209 AddLog(CEC_LOG_DEBUG
, "bootloader start command transmitted");
213 uint8_t CCECParser::GetSourceDestination(cec_logical_address destination
/* = CECDEVICE_BROADCAST */)
215 return ((uint8_t)m_iLogicalAddress
<< 4) + (uint8_t)destination
;
218 bool CCECParser::PowerOffDevices(cec_logical_address address
/* = CECDEVICE_BROADCAST */)
224 strLog
.Format("powering off devices with logical address %d", (int8_t)address
);
225 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
227 frame
.push_back(GetSourceDestination(address
));
228 frame
.push_back(CEC_OPCODE_STANDBY
);
229 return Transmit(frame
);
232 bool CCECParser::PowerOnDevices(cec_logical_address address
/* = CECDEVICE_BROADCAST */)
238 strLog
.Format("powering on devices with logical address %d", (int8_t)address
);
239 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
241 frame
.push_back(GetSourceDestination(address
));
242 frame
.push_back(CEC_OPCODE_TEXT_VIEW_ON
);
243 return Transmit(frame
);
246 bool CCECParser::StandbyDevices(cec_logical_address address
/* = CECDEVICE_BROADCAST */)
252 strLog
.Format("putting all devices with logical address %d in standby mode", (int8_t)address
);
253 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
255 frame
.push_back(GetSourceDestination(address
));
256 frame
.push_back(CEC_OPCODE_STANDBY
);
257 return Transmit(frame
);
260 bool CCECParser::SetActiveView(void)
265 AddLog(CEC_LOG_DEBUG
, "setting active view");
267 frame
.push_back(GetSourceDestination(CECDEVICE_BROADCAST
));
268 frame
.push_back(CEC_OPCODE_ACTIVE_SOURCE
);
269 frame
.push_back((m_physicaladdress
>> 8) & 0xFF);
270 frame
.push_back(m_physicaladdress
& 0xFF);
271 return Transmit(frame
);
274 bool CCECParser::SetInactiveView(void)
279 AddLog(CEC_LOG_DEBUG
, "setting inactive view");
281 frame
.push_back(GetSourceDestination(CECDEVICE_BROADCAST
));
282 frame
.push_back(CEC_OPCODE_INACTIVE_SOURCE
);
283 frame
.push_back((m_physicaladdress
>> 8) & 0xFF);
284 frame
.push_back(m_physicaladdress
& 0xFF);
285 return Transmit(frame
);
288 bool CCECParser::GetNextLogMessage(cec_log_message
*message
)
290 return m_bRunning
? m_logBuffer
.Pop(*message
) : false;
293 bool CCECParser::GetNextKeypress(cec_keypress
*key
)
295 return m_bRunning
? m_keyBuffer
.Pop(*key
) : false;
298 bool CCECParser::GetNextCommand(cec_command
*command
)
300 return m_bRunning
? m_commandBuffer
.Pop(*command
) : false;
304 void CCECParser::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, ECecAbortReason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
306 AddLog(CEC_LOG_DEBUG
, "transmitting abort message");
308 frame
.push_back(GetSourceDestination(address
));
309 frame
.push_back(CEC_OPCODE_FEATURE_ABORT
);
310 frame
.push_back(opcode
);
311 frame
.push_back(reason
);
315 void CCECParser::ReportCECVersion(cec_logical_address address
/* = CECDEVICE_TV */)
318 AddLog(CEC_LOG_NOTICE
, "reporting CEC version as 1.3a");
319 frame
.push_back(GetSourceDestination(address
));
320 frame
.push_back(CEC_OPCODE_CEC_VERSION
);
321 frame
.push_back(CEC_VERSION_1_3A
);
325 void CCECParser::ReportPowerState(cec_logical_address address
/*= CECDEVICE_TV */, bool bOn
/* = true */)
329 AddLog(CEC_LOG_NOTICE
, "reporting \"On\" power status");
331 AddLog(CEC_LOG_NOTICE
, "reporting \"Off\" power status");
333 frame
.push_back(GetSourceDestination(address
));
334 frame
.push_back(CEC_OPCODE_REPORT_POWER_STATUS
);
335 frame
.push_back(bOn
? CEC_POWER_STATUS_ON
: CEC_POWER_STATUS_STANDBY
);
339 void CCECParser::ReportMenuState(cec_logical_address address
/* = CECDEVICE_TV */, bool bActive
/* = true */)
343 AddLog(CEC_LOG_NOTICE
, "reporting menu state as active");
345 AddLog(CEC_LOG_NOTICE
, "reporting menu state as inactive");
347 frame
.push_back(GetSourceDestination(address
));
348 frame
.push_back(CEC_OPCODE_MENU_STATUS
);
349 frame
.push_back(bActive
? CEC_MENU_STATE_ACTIVATED
: CEC_MENU_STATE_DEACTIVATED
);
353 void CCECParser::ReportVendorID(cec_logical_address address
/* = CECDEVICE_TV */)
355 AddLog(CEC_LOG_NOTICE
, "vendor ID requested, feature abort");
356 TransmitAbort(address
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
359 void CCECParser::ReportOSDName(cec_logical_address address
/* = CECDEVICE_TV */)
362 const char *osdname
= m_strDeviceName
.c_str();
364 strLog
.Format("reporting OSD name as %s", osdname
);
365 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
366 frame
.push_back(GetSourceDestination(address
));
367 frame
.push_back(CEC_OPCODE_SET_OSD_NAME
);
369 for (unsigned int i
= 0; i
< strlen(osdname
); i
++)
370 frame
.push_back(osdname
[i
]);
375 void CCECParser::ReportPhysicalAddress(void)
379 strLog
.Format("reporting physical address as %04x", m_physicaladdress
);
380 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
381 frame
.push_back(GetSourceDestination(CECDEVICE_BROADCAST
));
382 frame
.push_back(CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
383 frame
.push_back((m_physicaladdress
>> 8) & 0xFF);
384 frame
.push_back(m_physicaladdress
& 0xFF);
385 frame
.push_back(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
389 void CCECParser::BroadcastActiveSource(void)
392 AddLog(CEC_LOG_NOTICE
, "broadcasting active source");
393 frame
.push_back(GetSourceDestination(CECDEVICE_BROADCAST
));
394 frame
.push_back(CEC_OPCODE_ACTIVE_SOURCE
);
395 frame
.push_back((m_physicaladdress
>> 8) & 0xFF);
396 frame
.push_back(m_physicaladdress
& 0xFF);
400 bool CCECParser::TransmitFormatted(const cec_frame
&data
, bool bWaitForAck
/* = true */, int64_t iTimeout
/* = 2000 */)
402 CLockObject
lock(&m_mutex
, iTimeout
);
403 if (!lock
.IsLocked())
405 AddLog(CEC_LOG_ERROR
, "could not get a write lock");
409 if (m_serialport
->Write(data
) != data
.size())
412 strError
.Format("error writing to serial port: %s", m_serialport
->GetError().c_str());
413 AddLog(CEC_LOG_ERROR
, strError
);
416 AddLog(CEC_LOG_DEBUG
, "command sent");
418 CCondition::Sleep((int) data
.size() * 24 /*data*/ + 5 /*start bit (4.5 ms)*/ + 50 /* to be on the safe side */);
419 if (bWaitForAck
&& !WaitForAck())
421 AddLog(CEC_LOG_DEBUG
, "did not receive ACK");
428 bool CCECParser::Transmit(const cec_frame
&data
, bool bWaitForAck
/* = true */, int64_t iTimeout
/* = 5000 */)
430 CStdString txStr
= "transmit ";
431 for (unsigned int i
= 0; i
< data
.size(); i
++)
432 txStr
.AppendFormat(" %02x", data
[i
]);
433 AddLog(CEC_LOG_DEBUG
, txStr
.c_str());
437 AddLog(CEC_LOG_WARNING
, "transmit buffer is empty");
443 //set ack polarity to high when transmitting to the broadcast address
444 //set ack polarity low when transmitting to any other address
445 output
.push_back(MSGSTART
);
446 PushEscaped(output
, MSGCODE_TRANSMIT_ACK_POLARITY
);
448 if ((data
[0] & 0xF) == 0xF)
449 PushEscaped(output
, CEC_TRUE
);
451 PushEscaped(output
, CEC_FALSE
);
453 output
.push_back(MSGEND
);
455 for (unsigned int i
= 0; i
< data
.size(); i
++)
457 output
.push_back(MSGSTART
);
459 if (i
== data
.size() - 1)
460 PushEscaped(output
, MSGCODE_TRANSMIT_EOM
);
462 PushEscaped(output
, MSGCODE_TRANSMIT
);
464 PushEscaped(output
, data
[i
]);
466 output
.push_back(MSGEND
);
469 return TransmitFormatted(output
, bWaitForAck
, iTimeout
);
472 bool CCECParser::WaitForAck(int64_t iTimeout
/* = 1000 */)
477 int64_t iNow
= GetTimeMs();
478 int64_t iTargetTime
= iNow
+ iTimeout
;
480 while (!bGotAck
&& !bError
&& (iTimeout
<= 0 || iNow
< iTargetTime
))
482 if (!ReadFromDevice((int) iTimeout
))
484 AddLog(CEC_LOG_ERROR
, "failed to read from device");
489 while (!bGotAck
&& !bError
&& GetMessage(msg
, false))
491 uint8_t iCode
= msg
[0] & ~(MSGCODE_FRAME_EOM
| MSGCODE_FRAME_ACK
);
495 case MSGCODE_COMMAND_ACCEPTED
:
496 AddLog(CEC_LOG_DEBUG
, "MSGCODE_COMMAND_ACCEPTED");
498 case MSGCODE_TRANSMIT_SUCCEEDED
:
499 AddLog(CEC_LOG_DEBUG
, "MSGCODE_TRANSMIT_SUCCEEDED");
503 case MSGCODE_RECEIVE_FAILED
:
504 AddLog(CEC_LOG_WARNING
, "MSGCODE_RECEIVE_FAILED");
507 case MSGCODE_COMMAND_REJECTED
:
508 AddLog(CEC_LOG_WARNING
, "MSGCODE_COMMAND_REJECTED");
511 case MSGCODE_TRANSMIT_FAILED_LINE
:
512 AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_LINE");
515 case MSGCODE_TRANSMIT_FAILED_ACK
:
516 AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_ACK");
519 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
:
520 AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA");
523 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
:
524 AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE");
528 m_frameBuffer
.Push(msg
);
529 bGotAck
= (msg
[0] & MSGCODE_FRAME_ACK
) != 0;
536 return bGotAck
&& !bError
;
539 bool CCECParser::ReadFromDevice(int iTimeout
)
542 int iBytesRead
= m_serialport
->Read(buff
, sizeof(buff
), iTimeout
);
546 strError
.Format("error reading from serial port: %s", m_serialport
->GetError().c_str());
547 AddLog(CEC_LOG_ERROR
, strError
);
550 else if (iBytesRead
> 0)
551 AddData(buff
, iBytesRead
);
556 void CCECParser::ProcessMessages(void)
559 while (m_bRunning
&& GetMessage(msg
))
563 bool CCECParser::GetMessage(cec_frame
&msg
, bool bFromBuffer
/* = true */)
565 if (bFromBuffer
&& m_frameBuffer
.Pop(msg
))
568 if (m_iInbufUsed
< 1)
571 //search for first start of message
573 for (int i
= 0; i
< m_iInbufUsed
; i
++)
575 if (m_inbuf
[i
] == MSGSTART
)
585 //move anything from the first start of message to the beginning of the buffer
588 memmove(m_inbuf
, m_inbuf
+ startpos
, m_iInbufUsed
- startpos
);
589 m_iInbufUsed
-= startpos
;
592 if (m_iInbufUsed
< 2)
595 //look for end of message
598 for (int i
= 1; i
< m_iInbufUsed
; i
++)
600 if (m_inbuf
[i
] == MSGEND
)
605 else if (m_inbuf
[i
] == MSGSTART
)
612 if (startpos
> 0) //we found a msgstart before msgend, this is not right, remove
614 AddLog(CEC_LOG_ERROR
, "received MSGSTART before MSGEND");
615 memmove(m_inbuf
, m_inbuf
+ startpos
, m_iInbufUsed
- startpos
);
616 m_iInbufUsed
-= startpos
;
620 if (endpos
> 0) //found a MSGEND
624 for (int i
= 1; i
< endpos
; i
++)
628 msg
.push_back(m_inbuf
[i
] + (uint8_t)ESCOFFSET
);
631 else if (m_inbuf
[i
] == MSGESC
)
637 msg
.push_back(m_inbuf
[i
]);
641 if (endpos
+ 1 < m_iInbufUsed
)
642 memmove(m_inbuf
, m_inbuf
+ endpos
+ 1, m_iInbufUsed
- endpos
- 1);
644 m_iInbufUsed
-= endpos
+ 1;
652 void CCECParser::ParseMessage(cec_frame
&msg
)
658 uint8_t iCode
= msg
[0] & ~(MSGCODE_FRAME_EOM
| MSGCODE_FRAME_ACK
);
659 bool bEom
= (msg
[0] & MSGCODE_FRAME_EOM
) != 0;
660 bool bAck
= (msg
[0] & MSGCODE_FRAME_ACK
) != 0;
664 case MSGCODE_NOTHING
:
665 AddLog(CEC_LOG_DEBUG
, "MSGCODE_NOTHING");
667 case MSGCODE_TIMEOUT_ERROR
:
668 case MSGCODE_HIGH_ERROR
:
669 case MSGCODE_LOW_ERROR
:
671 if (iCode
== MSGCODE_TIMEOUT_ERROR
)
672 logStr
= "MSGCODE_TIMEOUT";
673 else if (iCode
== MSGCODE_HIGH_ERROR
)
674 logStr
= "MSGCODE_HIGH_ERROR";
676 logStr
= "MSGCODE_LOW_ERROR";
678 int iLine
= (msg
.size() >= 3) ? (msg
[1] << 8) | (msg
[2]) : 0;
679 uint32_t iTime
= (msg
.size() >= 7) ? (msg
[3] << 24) | (msg
[4] << 16) | (msg
[5] << 8) | (msg
[6]) : 0;
680 logStr
.AppendFormat(" line:%i", iLine
);
681 logStr
.AppendFormat(" time:%u", iTime
);
682 AddLog(CEC_LOG_WARNING
, logStr
.c_str());
685 case MSGCODE_FRAME_START
:
687 logStr
= "MSGCODE_FRAME_START";
688 m_currentframe
.clear();
691 int iInitiator
= msg
[1] >> 4;
692 int iDestination
= msg
[1] & 0xF;
693 logStr
.AppendFormat(" initiator:%u destination:%u ack:%s %s", iInitiator
, iDestination
, bAck
? "high" : "low", bEom
? "eom" : "");
695 m_currentframe
.push_back(msg
[1]);
697 AddLog(CEC_LOG_DEBUG
, logStr
.c_str());
700 case MSGCODE_FRAME_DATA
:
702 logStr
= "MSGCODE_FRAME_DATA";
705 uint8_t iData
= msg
[1];
706 logStr
.AppendFormat(" %02x", iData
);
707 m_currentframe
.push_back(iData
);
709 AddLog(CEC_LOG_DEBUG
, logStr
.c_str());
719 void CCECParser::ParseCurrentFrame(void)
721 uint8_t initiator
= m_currentframe
[0] >> 4;
722 uint8_t destination
= m_currentframe
[0] & 0xF;
725 dataStr
.Format("received frame: initiator: %u destination: %u", initiator
, destination
);
727 if (m_currentframe
.size() > 1)
730 for (unsigned int i
= 1; i
< m_currentframe
.size(); i
++)
731 dataStr
.AppendFormat(" %02x", m_currentframe
[i
]);
733 AddLog(CEC_LOG_DEBUG
, dataStr
.c_str());
735 if (m_currentframe
.size() <= 1)
739 cec_opcode opCode
= (cec_opcode
) m_currentframe
[1];
740 if (destination
== (uint16_t) m_iLogicalAddress
)
744 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
745 ReportPhysicalAddress();
748 case CEC_OPCODE_GIVE_OSD_NAME
:
749 ReportOSDName((cec_logical_address
)initiator
);
751 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
752 ReportVendorID((cec_logical_address
)initiator
);
754 case CEC_OPCODE_MENU_REQUEST
:
755 ReportMenuState((cec_logical_address
)initiator
);
757 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
758 ReportPowerState((cec_logical_address
)initiator
);
760 case CEC_OPCODE_GET_CEC_VERSION
:
761 ReportCECVersion((cec_logical_address
)initiator
);
763 case CEC_OPCODE_USER_CONTROL_PRESSED
:
764 if (m_currentframe
.size() > 2)
768 if (m_currentframe
[2] <= CEC_USER_CONTROL_CODE_MAX
)
770 m_iCurrentButton
= (cec_user_control_code
) m_currentframe
[2];
771 m_buttontime
= GetTimeMs();
775 case CEC_OPCODE_USER_CONTROL_RELEASE
:
779 cec_frame params
= m_currentframe
;
780 params
.erase(params
.begin(), params
.begin() + 2);
781 AddCommand((cec_logical_address
) initiator
, (cec_logical_address
) destination
, opCode
, ¶ms
);
785 else if (destination
== (uint8_t) CECDEVICE_BROADCAST
)
787 if (opCode
== CEC_OPCODE_REQUEST_ACTIVE_SOURCE
)
789 BroadcastActiveSource();
791 else if (opCode
== CEC_OPCODE_SET_STREAM_PATH
)
793 if (m_currentframe
.size() >= 4)
795 int streamaddr
= ((int)m_currentframe
[2] << 8) | ((int)m_currentframe
[3]);
797 strLog
.Format("%i requests stream path from physical address %04x", initiator
, streamaddr
);
798 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
799 if (streamaddr
== m_physicaladdress
)
800 BroadcastActiveSource();
805 cec_frame params
= m_currentframe
;
806 params
.erase(params
.begin(), params
.begin() + 2);
807 AddCommand((cec_logical_address
) initiator
, (cec_logical_address
) destination
, opCode
, ¶ms
);
813 strLog
.Format("ignoring frame: destination: %u != %u", destination
, (uint16_t)m_iLogicalAddress
);
814 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
818 void CCECParser::AddData(uint8_t *data
, int iLen
)
820 if (iLen
+ m_iInbufUsed
> m_iInbufSize
)
822 m_iInbufSize
= iLen
+ m_iInbufUsed
;
823 m_inbuf
= (uint8_t*)realloc(m_inbuf
, m_iInbufSize
);
826 memcpy(m_inbuf
+ m_iInbufUsed
, data
, iLen
);
827 m_iInbufUsed
+= iLen
;
830 void CCECParser::PushEscaped(cec_frame
&vec
, uint8_t byte
)
832 if (byte
>= MSGESC
&& byte
!= MSGSTART
)
834 vec
.push_back(MSGESC
);
835 vec
.push_back(byte
- ESCOFFSET
);
843 void CCECParser::CheckKeypressTimeout(int64_t now
)
845 if (m_iCurrentButton
!= CEC_USER_CONTROL_CODE_UNKNOWN
&& now
- m_buttontime
> 500)
848 m_iCurrentButton
= CEC_USER_CONTROL_CODE_UNKNOWN
;
852 bool CCECParser::SetLogicalAddress(cec_logical_address iLogicalAddress
)
855 strLog
.Format("setting logical address to %d", iLogicalAddress
);
856 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
858 m_iLogicalAddress
= iLogicalAddress
;
859 return SetAckMask(0x1 << (uint8_t)m_iLogicalAddress
);
862 bool CCECParser::SetAckMask(uint16_t iMask
)
865 strLog
.Format("setting ackmask to %2x", iMask
);
866 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
870 output
.push_back(MSGSTART
);
871 PushEscaped(output
, MSGCODE_SET_ACK_MASK
);
872 PushEscaped(output
, iMask
>> 8);
873 PushEscaped(output
, (uint8_t)iMask
);
874 output
.push_back(MSGEND
);
876 if (m_serialport
->Write(output
) == -1)
878 strLog
.Format("error writing to serial port: %s", m_serialport
->GetError().c_str());
879 AddLog(CEC_LOG_ERROR
, strLog
);
886 void CCECParser::AddLog(cec_log_level level
, const string
&strMessage
)
888 cec_log_message message
;
889 message
.level
= level
;
890 message
.message
.assign(strMessage
.c_str());
891 m_logBuffer
.Push(message
);
894 void CCECParser::AddKey(void)
896 if (m_iCurrentButton
!= CEC_USER_CONTROL_CODE_UNKNOWN
)
899 key
.duration
= (unsigned int) (GetTimeMs() - m_buttontime
);
900 key
.keycode
= m_iCurrentButton
;
901 m_keyBuffer
.Push(key
);
902 m_iCurrentButton
= CEC_USER_CONTROL_CODE_UNKNOWN
;
907 void CCECParser::AddCommand(cec_logical_address source
, cec_logical_address destination
, cec_opcode opcode
, cec_frame
*parameters
)
910 command
.source
= source
;
911 command
.destination
= destination
;
912 command
.opcode
= opcode
;
914 command
.parameters
= *parameters
;
915 if (m_commandBuffer
.Push(command
))
918 strDebug
.Format("stored command '%d' in the command buffer. buffer size = %d", opcode
, m_commandBuffer
.Size());
919 AddLog(CEC_LOG_DEBUG
, strDebug
);
923 AddLog(CEC_LOG_WARNING
, "command buffer is full");
927 int CCECParser::GetMinVersion(void)
929 return CEC_MIN_VERSION
;
932 int CCECParser::GetLibVersion(void)
934 return CEC_LIB_VERSION
;
937 int CCECParser::FindDevices(std::vector
<cec_device
> &deviceList
, const char *strDevicePath
/* = NULL */)
941 strDebug
.Format("trying to autodetect the com port for device path '%s'", strDevicePath
);
943 strDebug
.Format("trying to autodetect all CEC adapters");
944 AddLog(CEC_LOG_DEBUG
, strDebug
);
946 return CCECDetect::FindDevices(deviceList
, strDevicePath
);
949 DECLSPEC
void * CECCreate(const char *strDeviceName
, CEC::cec_logical_address iLogicalAddress
/*= CEC::CECDEVICE_PLAYBACKDEVICE1 */, int iPhysicalAddress
/* = CEC_DEFAULT_PHYSICAL_ADDRESS */)
951 return static_cast< void* > (new CCECParser(strDeviceName
, iLogicalAddress
, iPhysicalAddress
));