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 */)
220 return StandbyDevices(address
);
223 bool CCECParser::PowerOnDevices(cec_logical_address address
/* = CECDEVICE_TV */)
229 strLog
.Format("powering on devices with logical address %d", (int8_t)address
);
230 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
232 frame
.push_back(GetSourceDestination(address
));
233 frame
.push_back(CEC_OPCODE_TEXT_VIEW_ON
);
234 return Transmit(frame
);
237 bool CCECParser::StandbyDevices(cec_logical_address address
/* = CECDEVICE_BROADCAST */)
243 strLog
.Format("putting all devices with logical address %d in standby mode", (int8_t)address
);
244 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
246 frame
.push_back(GetSourceDestination(address
));
247 frame
.push_back(CEC_OPCODE_STANDBY
);
248 return Transmit(frame
);
251 bool CCECParser::SetActiveView(void)
256 AddLog(CEC_LOG_DEBUG
, "setting active view");
258 frame
.push_back(GetSourceDestination(CECDEVICE_BROADCAST
));
259 frame
.push_back(CEC_OPCODE_ACTIVE_SOURCE
);
260 frame
.push_back((m_physicaladdress
>> 8) & 0xFF);
261 frame
.push_back(m_physicaladdress
& 0xFF);
262 return Transmit(frame
);
265 bool CCECParser::SetInactiveView(void)
270 AddLog(CEC_LOG_DEBUG
, "setting inactive view");
272 frame
.push_back(GetSourceDestination(CECDEVICE_BROADCAST
));
273 frame
.push_back(CEC_OPCODE_INACTIVE_SOURCE
);
274 frame
.push_back((m_physicaladdress
>> 8) & 0xFF);
275 frame
.push_back(m_physicaladdress
& 0xFF);
276 return Transmit(frame
);
279 bool CCECParser::GetNextLogMessage(cec_log_message
*message
)
281 return m_bRunning
? m_logBuffer
.Pop(*message
) : false;
284 bool CCECParser::GetNextKeypress(cec_keypress
*key
)
286 return m_bRunning
? m_keyBuffer
.Pop(*key
) : false;
289 bool CCECParser::GetNextCommand(cec_command
*command
)
291 return m_bRunning
? m_commandBuffer
.Pop(*command
) : false;
295 void CCECParser::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, ECecAbortReason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
297 AddLog(CEC_LOG_DEBUG
, "transmitting abort message");
299 frame
.push_back(GetSourceDestination(address
));
300 frame
.push_back(CEC_OPCODE_FEATURE_ABORT
);
301 frame
.push_back(opcode
);
302 frame
.push_back(reason
);
306 void CCECParser::ReportCECVersion(cec_logical_address address
/* = CECDEVICE_TV */)
309 AddLog(CEC_LOG_NOTICE
, "reporting CEC version as 1.3a");
310 frame
.push_back(GetSourceDestination(address
));
311 frame
.push_back(CEC_OPCODE_CEC_VERSION
);
312 frame
.push_back(CEC_VERSION_1_3A
);
316 void CCECParser::ReportPowerState(cec_logical_address address
/*= CECDEVICE_TV */, bool bOn
/* = true */)
320 AddLog(CEC_LOG_NOTICE
, "reporting \"On\" power status");
322 AddLog(CEC_LOG_NOTICE
, "reporting \"Off\" power status");
324 frame
.push_back(GetSourceDestination(address
));
325 frame
.push_back(CEC_OPCODE_REPORT_POWER_STATUS
);
326 frame
.push_back(bOn
? CEC_POWER_STATUS_ON
: CEC_POWER_STATUS_STANDBY
);
330 void CCECParser::ReportMenuState(cec_logical_address address
/* = CECDEVICE_TV */, bool bActive
/* = true */)
334 AddLog(CEC_LOG_NOTICE
, "reporting menu state as active");
336 AddLog(CEC_LOG_NOTICE
, "reporting menu state as inactive");
338 frame
.push_back(GetSourceDestination(address
));
339 frame
.push_back(CEC_OPCODE_MENU_STATUS
);
340 frame
.push_back(bActive
? CEC_MENU_STATE_ACTIVATED
: CEC_MENU_STATE_DEACTIVATED
);
344 void CCECParser::ReportVendorID(cec_logical_address address
/* = CECDEVICE_TV */)
346 AddLog(CEC_LOG_NOTICE
, "vendor ID requested, feature abort");
347 TransmitAbort(address
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
350 void CCECParser::ReportOSDName(cec_logical_address address
/* = CECDEVICE_TV */)
353 const char *osdname
= m_strDeviceName
.c_str();
355 strLog
.Format("reporting OSD name as %s", osdname
);
356 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
357 frame
.push_back(GetSourceDestination(address
));
358 frame
.push_back(CEC_OPCODE_SET_OSD_NAME
);
360 for (unsigned int i
= 0; i
< strlen(osdname
); i
++)
361 frame
.push_back(osdname
[i
]);
366 void CCECParser::ReportPhysicalAddress(void)
370 strLog
.Format("reporting physical address as %04x", m_physicaladdress
);
371 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
372 frame
.push_back(GetSourceDestination(CECDEVICE_BROADCAST
));
373 frame
.push_back(CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
374 frame
.push_back((m_physicaladdress
>> 8) & 0xFF);
375 frame
.push_back(m_physicaladdress
& 0xFF);
376 frame
.push_back(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
380 void CCECParser::BroadcastActiveSource(void)
383 AddLog(CEC_LOG_NOTICE
, "broadcasting active source");
384 frame
.push_back(GetSourceDestination(CECDEVICE_BROADCAST
));
385 frame
.push_back(CEC_OPCODE_ACTIVE_SOURCE
);
386 frame
.push_back((m_physicaladdress
>> 8) & 0xFF);
387 frame
.push_back(m_physicaladdress
& 0xFF);
391 bool CCECParser::TransmitFormatted(const cec_frame
&data
, bool bWaitForAck
/* = true */, int64_t iTimeout
/* = 2000 */)
393 CLockObject
lock(&m_mutex
, iTimeout
);
394 if (!lock
.IsLocked())
396 AddLog(CEC_LOG_ERROR
, "could not get a write lock");
400 if (m_serialport
->Write(data
) != data
.size())
403 strError
.Format("error writing to serial port: %s", m_serialport
->GetError().c_str());
404 AddLog(CEC_LOG_ERROR
, strError
);
407 AddLog(CEC_LOG_DEBUG
, "command sent");
409 CCondition::Sleep((int) data
.size() * 24 /*data*/ + 5 /*start bit (4.5 ms)*/ + 50 /* to be on the safe side */);
410 if (bWaitForAck
&& !WaitForAck())
412 AddLog(CEC_LOG_DEBUG
, "did not receive ACK");
419 bool CCECParser::Transmit(const cec_frame
&data
, bool bWaitForAck
/* = true */, int64_t iTimeout
/* = 5000 */)
421 CStdString txStr
= "transmit ";
422 for (unsigned int i
= 0; i
< data
.size(); i
++)
423 txStr
.AppendFormat(" %02x", data
[i
]);
424 AddLog(CEC_LOG_DEBUG
, txStr
.c_str());
428 AddLog(CEC_LOG_WARNING
, "transmit buffer is empty");
434 //set ack polarity to high when transmitting to the broadcast address
435 //set ack polarity low when transmitting to any other address
436 output
.push_back(MSGSTART
);
437 PushEscaped(output
, MSGCODE_TRANSMIT_ACK_POLARITY
);
439 if ((data
[0] & 0xF) == 0xF)
440 PushEscaped(output
, CEC_TRUE
);
442 PushEscaped(output
, CEC_FALSE
);
444 output
.push_back(MSGEND
);
446 for (unsigned int i
= 0; i
< data
.size(); i
++)
448 output
.push_back(MSGSTART
);
450 if (i
== data
.size() - 1)
451 PushEscaped(output
, MSGCODE_TRANSMIT_EOM
);
453 PushEscaped(output
, MSGCODE_TRANSMIT
);
455 PushEscaped(output
, data
[i
]);
457 output
.push_back(MSGEND
);
460 return TransmitFormatted(output
, bWaitForAck
, iTimeout
);
463 bool CCECParser::WaitForAck(int64_t iTimeout
/* = 1000 */)
468 int64_t iNow
= GetTimeMs();
469 int64_t iTargetTime
= iNow
+ iTimeout
;
471 while (!bGotAck
&& !bError
&& (iTimeout
<= 0 || iNow
< iTargetTime
))
473 if (!ReadFromDevice((int) iTimeout
))
475 AddLog(CEC_LOG_ERROR
, "failed to read from device");
480 while (!bGotAck
&& !bError
&& GetMessage(msg
, false))
482 uint8_t iCode
= msg
[0] & ~(MSGCODE_FRAME_EOM
| MSGCODE_FRAME_ACK
);
486 case MSGCODE_COMMAND_ACCEPTED
:
487 AddLog(CEC_LOG_DEBUG
, "MSGCODE_COMMAND_ACCEPTED");
489 case MSGCODE_TRANSMIT_SUCCEEDED
:
490 AddLog(CEC_LOG_DEBUG
, "MSGCODE_TRANSMIT_SUCCEEDED");
494 case MSGCODE_RECEIVE_FAILED
:
495 AddLog(CEC_LOG_WARNING
, "MSGCODE_RECEIVE_FAILED");
498 case MSGCODE_COMMAND_REJECTED
:
499 AddLog(CEC_LOG_WARNING
, "MSGCODE_COMMAND_REJECTED");
502 case MSGCODE_TRANSMIT_FAILED_LINE
:
503 AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_LINE");
506 case MSGCODE_TRANSMIT_FAILED_ACK
:
507 AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_ACK");
510 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
:
511 AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA");
514 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
:
515 AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE");
519 m_frameBuffer
.Push(msg
);
520 bGotAck
= (msg
[0] & MSGCODE_FRAME_ACK
) != 0;
527 return bGotAck
&& !bError
;
530 bool CCECParser::ReadFromDevice(int iTimeout
)
533 int iBytesRead
= m_serialport
->Read(buff
, sizeof(buff
), iTimeout
);
537 strError
.Format("error reading from serial port: %s", m_serialport
->GetError().c_str());
538 AddLog(CEC_LOG_ERROR
, strError
);
541 else if (iBytesRead
> 0)
542 AddData(buff
, iBytesRead
);
547 void CCECParser::ProcessMessages(void)
550 while (m_bRunning
&& GetMessage(msg
))
554 bool CCECParser::GetMessage(cec_frame
&msg
, bool bFromBuffer
/* = true */)
556 if (bFromBuffer
&& m_frameBuffer
.Pop(msg
))
559 if (m_iInbufUsed
< 1)
562 //search for first start of message
564 for (int i
= 0; i
< m_iInbufUsed
; i
++)
566 if (m_inbuf
[i
] == MSGSTART
)
576 //move anything from the first start of message to the beginning of the buffer
579 memmove(m_inbuf
, m_inbuf
+ startpos
, m_iInbufUsed
- startpos
);
580 m_iInbufUsed
-= startpos
;
583 if (m_iInbufUsed
< 2)
586 //look for end of message
589 for (int i
= 1; i
< m_iInbufUsed
; i
++)
591 if (m_inbuf
[i
] == MSGEND
)
596 else if (m_inbuf
[i
] == MSGSTART
)
603 if (startpos
> 0) //we found a msgstart before msgend, this is not right, remove
605 AddLog(CEC_LOG_ERROR
, "received MSGSTART before MSGEND");
606 memmove(m_inbuf
, m_inbuf
+ startpos
, m_iInbufUsed
- startpos
);
607 m_iInbufUsed
-= startpos
;
611 if (endpos
> 0) //found a MSGEND
615 for (int i
= 1; i
< endpos
; i
++)
619 msg
.push_back(m_inbuf
[i
] + (uint8_t)ESCOFFSET
);
622 else if (m_inbuf
[i
] == MSGESC
)
628 msg
.push_back(m_inbuf
[i
]);
632 if (endpos
+ 1 < m_iInbufUsed
)
633 memmove(m_inbuf
, m_inbuf
+ endpos
+ 1, m_iInbufUsed
- endpos
- 1);
635 m_iInbufUsed
-= endpos
+ 1;
643 void CCECParser::ParseMessage(cec_frame
&msg
)
649 uint8_t iCode
= msg
[0] & ~(MSGCODE_FRAME_EOM
| MSGCODE_FRAME_ACK
);
650 bool bEom
= (msg
[0] & MSGCODE_FRAME_EOM
) != 0;
651 bool bAck
= (msg
[0] & MSGCODE_FRAME_ACK
) != 0;
655 case MSGCODE_NOTHING
:
656 AddLog(CEC_LOG_DEBUG
, "MSGCODE_NOTHING");
658 case MSGCODE_TIMEOUT_ERROR
:
659 case MSGCODE_HIGH_ERROR
:
660 case MSGCODE_LOW_ERROR
:
662 if (iCode
== MSGCODE_TIMEOUT_ERROR
)
663 logStr
= "MSGCODE_TIMEOUT";
664 else if (iCode
== MSGCODE_HIGH_ERROR
)
665 logStr
= "MSGCODE_HIGH_ERROR";
667 logStr
= "MSGCODE_LOW_ERROR";
669 int iLine
= (msg
.size() >= 3) ? (msg
[1] << 8) | (msg
[2]) : 0;
670 uint32_t iTime
= (msg
.size() >= 7) ? (msg
[3] << 24) | (msg
[4] << 16) | (msg
[5] << 8) | (msg
[6]) : 0;
671 logStr
.AppendFormat(" line:%i", iLine
);
672 logStr
.AppendFormat(" time:%u", iTime
);
673 AddLog(CEC_LOG_WARNING
, logStr
.c_str());
676 case MSGCODE_FRAME_START
:
678 logStr
= "MSGCODE_FRAME_START";
679 m_currentframe
.clear();
682 int iInitiator
= msg
[1] >> 4;
683 int iDestination
= msg
[1] & 0xF;
684 logStr
.AppendFormat(" initiator:%u destination:%u ack:%s %s", iInitiator
, iDestination
, bAck
? "high" : "low", bEom
? "eom" : "");
686 m_currentframe
.push_back(msg
[1]);
688 AddLog(CEC_LOG_DEBUG
, logStr
.c_str());
691 case MSGCODE_FRAME_DATA
:
693 logStr
= "MSGCODE_FRAME_DATA";
696 uint8_t iData
= msg
[1];
697 logStr
.AppendFormat(" %02x", iData
);
698 m_currentframe
.push_back(iData
);
700 AddLog(CEC_LOG_DEBUG
, logStr
.c_str());
710 void CCECParser::ParseCurrentFrame(void)
712 uint8_t initiator
= m_currentframe
[0] >> 4;
713 uint8_t destination
= m_currentframe
[0] & 0xF;
716 dataStr
.Format("received frame: initiator: %u destination: %u", initiator
, destination
);
718 if (m_currentframe
.size() > 1)
721 for (unsigned int i
= 1; i
< m_currentframe
.size(); i
++)
722 dataStr
.AppendFormat(" %02x", m_currentframe
[i
]);
724 AddLog(CEC_LOG_DEBUG
, dataStr
.c_str());
726 if (m_currentframe
.size() <= 1)
730 cec_opcode opCode
= (cec_opcode
) m_currentframe
[1];
731 if (destination
== (uint16_t) m_iLogicalAddress
)
735 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
736 ReportPhysicalAddress();
739 case CEC_OPCODE_GIVE_OSD_NAME
:
740 ReportOSDName((cec_logical_address
)initiator
);
742 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
743 ReportVendorID((cec_logical_address
)initiator
);
745 case CEC_OPCODE_MENU_REQUEST
:
746 ReportMenuState((cec_logical_address
)initiator
);
748 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
749 ReportPowerState((cec_logical_address
)initiator
);
751 case CEC_OPCODE_GET_CEC_VERSION
:
752 ReportCECVersion((cec_logical_address
)initiator
);
754 case CEC_OPCODE_USER_CONTROL_PRESSED
:
755 if (m_currentframe
.size() > 2)
759 if (m_currentframe
[2] <= CEC_USER_CONTROL_CODE_MAX
)
761 m_iCurrentButton
= (cec_user_control_code
) m_currentframe
[2];
762 m_buttontime
= GetTimeMs();
766 case CEC_OPCODE_USER_CONTROL_RELEASE
:
770 cec_frame params
= m_currentframe
;
771 params
.erase(params
.begin(), params
.begin() + 2);
772 AddCommand((cec_logical_address
) initiator
, (cec_logical_address
) destination
, opCode
, ¶ms
);
776 else if (destination
== (uint8_t) CECDEVICE_BROADCAST
)
778 if (opCode
== CEC_OPCODE_REQUEST_ACTIVE_SOURCE
)
780 BroadcastActiveSource();
782 else if (opCode
== CEC_OPCODE_SET_STREAM_PATH
)
784 if (m_currentframe
.size() >= 4)
786 int streamaddr
= ((int)m_currentframe
[2] << 8) | ((int)m_currentframe
[3]);
788 strLog
.Format("%i requests stream path from physical address %04x", initiator
, streamaddr
);
789 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
790 if (streamaddr
== m_physicaladdress
)
791 BroadcastActiveSource();
796 cec_frame params
= m_currentframe
;
797 params
.erase(params
.begin(), params
.begin() + 2);
798 AddCommand((cec_logical_address
) initiator
, (cec_logical_address
) destination
, opCode
, ¶ms
);
804 strLog
.Format("ignoring frame: destination: %u != %u", destination
, (uint16_t)m_iLogicalAddress
);
805 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
809 void CCECParser::AddData(uint8_t *data
, int iLen
)
811 if (iLen
+ m_iInbufUsed
> m_iInbufSize
)
813 m_iInbufSize
= iLen
+ m_iInbufUsed
;
814 m_inbuf
= (uint8_t*)realloc(m_inbuf
, m_iInbufSize
);
817 memcpy(m_inbuf
+ m_iInbufUsed
, data
, iLen
);
818 m_iInbufUsed
+= iLen
;
821 void CCECParser::PushEscaped(cec_frame
&vec
, uint8_t byte
)
823 if (byte
>= MSGESC
&& byte
!= MSGSTART
)
825 vec
.push_back(MSGESC
);
826 vec
.push_back(byte
- ESCOFFSET
);
834 void CCECParser::CheckKeypressTimeout(int64_t now
)
836 if (m_iCurrentButton
!= CEC_USER_CONTROL_CODE_UNKNOWN
&& now
- m_buttontime
> 500)
839 m_iCurrentButton
= CEC_USER_CONTROL_CODE_UNKNOWN
;
843 bool CCECParser::SetLogicalAddress(cec_logical_address iLogicalAddress
)
846 strLog
.Format("setting logical address to %d", iLogicalAddress
);
847 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
849 m_iLogicalAddress
= iLogicalAddress
;
850 return SetAckMask(0x1 << (uint8_t)m_iLogicalAddress
);
853 bool CCECParser::SetAckMask(uint16_t iMask
)
856 strLog
.Format("setting ackmask to %2x", iMask
);
857 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
861 output
.push_back(MSGSTART
);
862 PushEscaped(output
, MSGCODE_SET_ACK_MASK
);
863 PushEscaped(output
, iMask
>> 8);
864 PushEscaped(output
, (uint8_t)iMask
);
865 output
.push_back(MSGEND
);
867 if (m_serialport
->Write(output
) == -1)
869 strLog
.Format("error writing to serial port: %s", m_serialport
->GetError().c_str());
870 AddLog(CEC_LOG_ERROR
, strLog
);
877 void CCECParser::AddLog(cec_log_level level
, const string
&strMessage
)
879 cec_log_message message
;
880 message
.level
= level
;
881 message
.message
.assign(strMessage
.c_str());
882 m_logBuffer
.Push(message
);
885 void CCECParser::AddKey(void)
887 if (m_iCurrentButton
!= CEC_USER_CONTROL_CODE_UNKNOWN
)
890 key
.duration
= (unsigned int) (GetTimeMs() - m_buttontime
);
891 key
.keycode
= m_iCurrentButton
;
892 m_keyBuffer
.Push(key
);
893 m_iCurrentButton
= CEC_USER_CONTROL_CODE_UNKNOWN
;
898 void CCECParser::AddCommand(cec_logical_address source
, cec_logical_address destination
, cec_opcode opcode
, cec_frame
*parameters
)
901 command
.source
= source
;
902 command
.destination
= destination
;
903 command
.opcode
= opcode
;
905 command
.parameters
= *parameters
;
906 if (m_commandBuffer
.Push(command
))
909 strDebug
.Format("stored command '%d' in the command buffer. buffer size = %d", opcode
, m_commandBuffer
.Size());
910 AddLog(CEC_LOG_DEBUG
, strDebug
);
914 AddLog(CEC_LOG_WARNING
, "command buffer is full");
918 int CCECParser::GetMinVersion(void)
920 return CEC_MIN_VERSION
;
923 int CCECParser::GetLibVersion(void)
925 return CEC_LIB_VERSION
;
928 int CCECParser::FindDevices(std::vector
<cec_device
> &deviceList
, const char *strDevicePath
/* = NULL */)
932 strDebug
.Format("trying to autodetect the com port for device path '%s'", strDevicePath
);
934 strDebug
.Format("trying to autodetect all CEC adapters");
935 AddLog(CEC_LOG_DEBUG
, strDebug
);
937 return CCECDetect::FindDevices(deviceList
, strDevicePath
);
940 DECLSPEC
void * CECCreate(const char *strDeviceName
, CEC::cec_logical_address iLogicalAddress
/*= CEC::CECDEVICE_PLAYBACKDEVICE1 */, int iPhysicalAddress
/* = CEC_DEFAULT_PHYSICAL_ADDRESS */)
942 return static_cast< void* > (new CCECParser(strDeviceName
, iLogicalAddress
, iPhysicalAddress
));