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 pthread_join(m_thread
, NULL
);
72 m_serialport
->Close();
76 bool CCECParser::Open(const char *strPort
, int iTimeoutMs
/* = 10000 */)
80 if (!(bReturn
= m_serialport
->Open(strPort
, 38400)))
83 strError
.Format("error opening serial port '%s': %s", strPort
, m_serialport
->GetError().c_str());
84 AddLog(CEC_LOG_ERROR
, strError
);
88 //clear any input bytes
90 m_serialport
->Read(buff
, sizeof(buff
), CEC_SETTLE_DOWN_TIME
);
93 bReturn
= SetLogicalAddress(m_iLogicalAddress
);
98 strError
.Format("error opening serial port '%s': %s", strPort
, m_serialport
->GetError().c_str());
99 AddLog(CEC_LOG_ERROR
, strError
);
106 if (pthread_create(&m_thread
, NULL
, (void *(*) (void *))&CCECParser::ThreadHandler
, (void *)this) == 0)
107 pthread_detach(m_thread
);
115 void *CCECParser::ThreadHandler(CCECParser
*parser
)
122 bool CCECParser::Process(void)
124 int64_t now
= GetTimeMs();
128 CLockObject
lock(&m_mutex
, 1000);
131 if (!ReadFromDevice(100))
139 //AddLog(CEC_LOG_DEBUG, "processing messages");
142 CheckKeypressTimeout(now
);
143 CCondition::Sleep(50);
146 AddLog(CEC_LOG_DEBUG
, "reader thread terminated");
151 bool CCECParser::Ping(void)
153 AddLog(CEC_LOG_DEBUG
, "sending ping");
155 output
.push_back(MSGSTART
);
156 PushEscaped(output
, MSGCODE_PING
);
157 output
.push_back(MSGEND
);
159 if (!TransmitFormatted(output
, false, (int64_t) 5000))
161 AddLog(CEC_LOG_ERROR
, "could not send ping command");
165 AddLog(CEC_LOG_DEBUG
, "ping tranmitted");
167 // TODO check for pong
171 bool CCECParser::StartBootloader(void)
173 AddLog(CEC_LOG_DEBUG
, "starting the bootloader");
175 output
.push_back(MSGSTART
);
176 PushEscaped(output
, MSGCODE_START_BOOTLOADER
);
177 output
.push_back(MSGEND
);
179 if (!TransmitFormatted(output
, false, (int64_t) 5000))
181 AddLog(CEC_LOG_ERROR
, "could not start the bootloader");
185 AddLog(CEC_LOG_DEBUG
, "bootloader start command transmitted");
189 uint8_t CCECParser::GetSourceDestination(cec_logical_address destination
/* = CECDEVICE_BROADCAST */)
191 return ((uint8_t)m_iLogicalAddress
<< 4) + (uint8_t)destination
;
194 bool CCECParser::PowerOffDevices(cec_logical_address address
/* = CECDEVICE_BROADCAST */)
197 strLog
.Format("powering off devices with logical address %d", (int8_t)address
);
198 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
200 frame
.push_back(GetSourceDestination(address
));
201 frame
.push_back(CEC_OPCODE_STANDBY
);
202 return Transmit(frame
);
205 bool CCECParser::PowerOnDevices(cec_logical_address address
/* = CECDEVICE_BROADCAST */)
208 strLog
.Format("powering on devices with logical address %d", (int8_t)address
);
209 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
211 frame
.push_back(GetSourceDestination(address
));
212 frame
.push_back(CEC_OPCODE_TEXT_VIEW_ON
);
213 return Transmit(frame
);
216 bool CCECParser::StandbyDevices(cec_logical_address address
/* = CECDEVICE_BROADCAST */)
219 strLog
.Format("putting all devices with logical address %d in standby mode", (int8_t)address
);
220 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
222 frame
.push_back(GetSourceDestination(address
));
223 frame
.push_back(CEC_OPCODE_STANDBY
);
224 return Transmit(frame
);
227 bool CCECParser::SetActiveView(void)
229 AddLog(CEC_LOG_DEBUG
, "setting active view");
231 frame
.push_back(GetSourceDestination(CECDEVICE_BROADCAST
));
232 frame
.push_back(CEC_OPCODE_ACTIVE_SOURCE
);
233 frame
.push_back((m_physicaladdress
>> 8) & 0xFF);
234 frame
.push_back(m_physicaladdress
& 0xFF);
235 return Transmit(frame
);
238 bool CCECParser::SetInactiveView(void)
240 AddLog(CEC_LOG_DEBUG
, "setting inactive view");
242 frame
.push_back(GetSourceDestination(CECDEVICE_BROADCAST
));
243 frame
.push_back(CEC_OPCODE_INACTIVE_SOURCE
);
244 frame
.push_back((m_physicaladdress
>> 8) & 0xFF);
245 frame
.push_back(m_physicaladdress
& 0xFF);
246 return Transmit(frame
);
249 bool CCECParser::GetNextLogMessage(cec_log_message
*message
)
251 return m_logBuffer
.Pop(*message
);
254 bool CCECParser::GetNextKeypress(cec_keypress
*key
)
256 return m_keyBuffer
.Pop(*key
);
259 bool CCECParser::GetNextCommand(cec_command
*command
)
261 return m_commandBuffer
.Pop(*command
);
265 void CCECParser::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, ECecAbortReason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
267 AddLog(CEC_LOG_DEBUG
, "transmitting abort message");
269 frame
.push_back(GetSourceDestination(address
));
270 frame
.push_back(CEC_OPCODE_FEATURE_ABORT
);
271 frame
.push_back(opcode
);
272 frame
.push_back(reason
);
276 void CCECParser::ReportCECVersion(cec_logical_address address
/* = CECDEVICE_TV */)
279 AddLog(CEC_LOG_NOTICE
, "reporting CEC version as 1.3a");
280 frame
.push_back(GetSourceDestination(address
));
281 frame
.push_back(CEC_OPCODE_CEC_VERSION
);
282 frame
.push_back(CEC_VERSION_1_3A
);
286 void CCECParser::ReportPowerState(cec_logical_address address
/*= CECDEVICE_TV */, bool bOn
/* = true */)
290 AddLog(CEC_LOG_NOTICE
, "reporting \"On\" power status");
292 AddLog(CEC_LOG_NOTICE
, "reporting \"Off\" power status");
294 frame
.push_back(GetSourceDestination(address
));
295 frame
.push_back(CEC_OPCODE_REPORT_POWER_STATUS
);
296 frame
.push_back(bOn
? CEC_POWER_STATUS_ON
: CEC_POWER_STATUS_STANDBY
);
300 void CCECParser::ReportMenuState(cec_logical_address address
/* = CECDEVICE_TV */, bool bActive
/* = true */)
304 AddLog(CEC_LOG_NOTICE
, "reporting menu state as active");
306 AddLog(CEC_LOG_NOTICE
, "reporting menu state as inactive");
308 frame
.push_back(GetSourceDestination(address
));
309 frame
.push_back(CEC_OPCODE_MENU_STATUS
);
310 frame
.push_back(bActive
? CEC_MENU_STATE_ACTIVATED
: CEC_MENU_STATE_DEACTIVATED
);
314 void CCECParser::ReportVendorID(cec_logical_address address
/* = CECDEVICE_TV */)
316 AddLog(CEC_LOG_NOTICE
, "vendor ID requested, feature abort");
317 TransmitAbort(address
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
320 void CCECParser::ReportOSDName(cec_logical_address address
/* = CECDEVICE_TV */)
323 const char *osdname
= m_strDeviceName
.c_str();
325 strLog
.Format("reporting OSD name as %s", osdname
);
326 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
327 frame
.push_back(GetSourceDestination(address
));
328 frame
.push_back(CEC_OPCODE_SET_OSD_NAME
);
330 for (unsigned int i
= 0; i
< strlen(osdname
); i
++)
331 frame
.push_back(osdname
[i
]);
336 void CCECParser::ReportPhysicalAddress(void)
340 strLog
.Format("reporting physical address as %04x", m_physicaladdress
);
341 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
342 frame
.push_back(GetSourceDestination(CECDEVICE_BROADCAST
));
343 frame
.push_back(CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
344 frame
.push_back((m_physicaladdress
>> 8) & 0xFF);
345 frame
.push_back(m_physicaladdress
& 0xFF);
346 frame
.push_back(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
350 void CCECParser::BroadcastActiveSource(void)
353 AddLog(CEC_LOG_NOTICE
, "broadcasting active source");
354 frame
.push_back(GetSourceDestination(CECDEVICE_BROADCAST
));
355 frame
.push_back(CEC_OPCODE_ACTIVE_SOURCE
);
356 frame
.push_back((m_physicaladdress
>> 8) & 0xFF);
357 frame
.push_back(m_physicaladdress
& 0xFF);
361 bool CCECParser::TransmitFormatted(const cec_frame
&data
, bool bWaitForAck
/* = true */, int64_t iTimeout
/* = 2000 */)
363 CLockObject
lock(&m_mutex
, iTimeout
);
364 if (!lock
.IsLocked())
366 AddLog(CEC_LOG_ERROR
, "could not get a write lock");
370 if (m_serialport
->Write(data
) != data
.size())
373 strError
.Format("error writing to serial port: %s", m_serialport
->GetError().c_str());
374 AddLog(CEC_LOG_ERROR
, strError
);
377 AddLog(CEC_LOG_DEBUG
, "command sent");
379 CCondition::Sleep((int) data
.size() * 24 /*data*/ + 5 /*start bit (4.5 ms)*/ + 50 /* to be on the safe side */);
380 if (bWaitForAck
&& !WaitForAck())
382 AddLog(CEC_LOG_DEBUG
, "did not receive ACK");
389 bool CCECParser::Transmit(const cec_frame
&data
, bool bWaitForAck
/* = true */, int64_t iTimeout
/* = 5000 */)
391 CStdString txStr
= "transmit ";
392 for (unsigned int i
= 0; i
< data
.size(); i
++)
393 txStr
.AppendFormat(" %02x", data
[i
]);
394 AddLog(CEC_LOG_DEBUG
, txStr
.c_str());
398 AddLog(CEC_LOG_WARNING
, "transmit buffer is empty");
404 //set ack polarity to high when transmitting to the broadcast address
405 //set ack polarity low when transmitting to any other address
406 output
.push_back(MSGSTART
);
407 PushEscaped(output
, MSGCODE_TRANSMIT_ACK_POLARITY
);
409 if ((data
[0] & 0xF) == 0xF)
410 PushEscaped(output
, CEC_TRUE
);
412 PushEscaped(output
, CEC_FALSE
);
414 output
.push_back(MSGEND
);
416 for (unsigned int i
= 0; i
< data
.size(); i
++)
418 output
.push_back(MSGSTART
);
420 if (i
== data
.size() - 1)
421 PushEscaped(output
, MSGCODE_TRANSMIT_EOM
);
423 PushEscaped(output
, MSGCODE_TRANSMIT
);
425 PushEscaped(output
, data
[i
]);
427 output
.push_back(MSGEND
);
430 return TransmitFormatted(output
, bWaitForAck
, iTimeout
);
433 bool CCECParser::WaitForAck(int64_t iTimeout
/* = 1000 */)
438 int64_t iNow
= GetTimeMs();
439 int64_t iTargetTime
= iNow
+ iTimeout
;
441 while (!bGotAck
&& !bError
&& (iTimeout
<= 0 || iNow
< iTargetTime
))
443 if (!ReadFromDevice((int) iTimeout
))
445 AddLog(CEC_LOG_ERROR
, "failed to read from device");
450 while (!bGotAck
&& !bError
&& GetMessage(msg
, false))
452 uint8_t iCode
= msg
[0] & ~(MSGCODE_FRAME_EOM
| MSGCODE_FRAME_ACK
);
456 case MSGCODE_COMMAND_ACCEPTED
:
457 AddLog(CEC_LOG_DEBUG
, "MSGCODE_COMMAND_ACCEPTED");
459 case MSGCODE_TRANSMIT_SUCCEEDED
:
460 AddLog(CEC_LOG_DEBUG
, "MSGCODE_TRANSMIT_SUCCEEDED");
464 case MSGCODE_RECEIVE_FAILED
:
465 AddLog(CEC_LOG_WARNING
, "MSGCODE_RECEIVE_FAILED");
468 case MSGCODE_COMMAND_REJECTED
:
469 AddLog(CEC_LOG_WARNING
, "MSGCODE_COMMAND_REJECTED");
472 case MSGCODE_TRANSMIT_FAILED_LINE
:
473 AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_LINE");
476 case MSGCODE_TRANSMIT_FAILED_ACK
:
477 AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_ACK");
480 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
:
481 AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA");
484 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
:
485 AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE");
489 m_frameBuffer
.Push(msg
);
490 bGotAck
= (msg
[0] & MSGCODE_FRAME_ACK
) != 0;
497 return bGotAck
&& !bError
;
500 bool CCECParser::ReadFromDevice(int iTimeout
)
503 int iBytesRead
= m_serialport
->Read(buff
, sizeof(buff
), iTimeout
);
507 strError
.Format("error reading from serial port: %s", m_serialport
->GetError().c_str());
508 AddLog(CEC_LOG_ERROR
, strError
);
511 else if (iBytesRead
> 0)
512 AddData(buff
, iBytesRead
);
517 void CCECParser::ProcessMessages(void)
520 while (GetMessage(msg
))
524 bool CCECParser::GetMessage(cec_frame
&msg
, bool bFromBuffer
/* = true */)
526 if (bFromBuffer
&& m_frameBuffer
.Pop(msg
))
529 if (m_iInbufUsed
< 1)
532 //search for first start of message
534 for (int i
= 0; i
< m_iInbufUsed
; i
++)
536 if (m_inbuf
[i
] == MSGSTART
)
546 //move anything from the first start of message to the beginning of the buffer
549 memmove(m_inbuf
, m_inbuf
+ startpos
, m_iInbufUsed
- startpos
);
550 m_iInbufUsed
-= startpos
;
553 if (m_iInbufUsed
< 2)
556 //look for end of message
559 for (int i
= 1; i
< m_iInbufUsed
; i
++)
561 if (m_inbuf
[i
] == MSGEND
)
566 else if (m_inbuf
[i
] == MSGSTART
)
573 if (startpos
> 0) //we found a msgstart before msgend, this is not right, remove
575 AddLog(CEC_LOG_ERROR
, "received MSGSTART before MSGEND");
576 memmove(m_inbuf
, m_inbuf
+ startpos
, m_iInbufUsed
- startpos
);
577 m_iInbufUsed
-= startpos
;
581 if (endpos
> 0) //found a MSGEND
585 for (int i
= 1; i
< endpos
; i
++)
589 msg
.push_back(m_inbuf
[i
] + (uint8_t)ESCOFFSET
);
592 else if (m_inbuf
[i
] == MSGESC
)
598 msg
.push_back(m_inbuf
[i
]);
602 if (endpos
+ 1 < m_iInbufUsed
)
603 memmove(m_inbuf
, m_inbuf
+ endpos
+ 1, m_iInbufUsed
- endpos
- 1);
605 m_iInbufUsed
-= endpos
+ 1;
613 void CCECParser::ParseMessage(cec_frame
&msg
)
619 uint8_t iCode
= msg
[0] & ~(MSGCODE_FRAME_EOM
| MSGCODE_FRAME_ACK
);
620 bool bEom
= (msg
[0] & MSGCODE_FRAME_EOM
) != 0;
621 bool bAck
= (msg
[0] & MSGCODE_FRAME_ACK
) != 0;
625 case MSGCODE_NOTHING
:
626 AddLog(CEC_LOG_DEBUG
, "MSGCODE_NOTHING");
628 case MSGCODE_TIMEOUT_ERROR
:
629 case MSGCODE_HIGH_ERROR
:
630 case MSGCODE_LOW_ERROR
:
632 if (iCode
== MSGCODE_TIMEOUT_ERROR
)
633 logStr
= "MSGCODE_TIMEOUT";
634 else if (iCode
== MSGCODE_HIGH_ERROR
)
635 logStr
= "MSGCODE_HIGH_ERROR";
637 logStr
= "MSGCODE_LOW_ERROR";
639 int iLine
= (msg
.size() >= 3) ? (msg
[1] << 8) | (msg
[2]) : 0;
640 uint32_t iTime
= (msg
.size() >= 7) ? (msg
[3] << 24) | (msg
[4] << 16) | (msg
[5] << 8) | (msg
[6]) : 0;
641 logStr
.AppendFormat(" line:%i", iLine
);
642 logStr
.AppendFormat(" time:%u", iTime
);
643 AddLog(CEC_LOG_WARNING
, logStr
.c_str());
646 case MSGCODE_FRAME_START
:
648 logStr
= "MSGCODE_FRAME_START";
649 m_currentframe
.clear();
652 int iInitiator
= msg
[1] >> 4;
653 int iDestination
= msg
[1] & 0xF;
654 logStr
.AppendFormat(" initiator:%u destination:%u ack:%s %s", iInitiator
, iDestination
, bAck
? "high" : "low", bEom
? "eom" : "");
656 m_currentframe
.push_back(msg
[1]);
658 AddLog(CEC_LOG_DEBUG
, logStr
.c_str());
661 case MSGCODE_FRAME_DATA
:
663 logStr
= "MSGCODE_FRAME_DATA";
666 uint8_t iData
= msg
[1];
667 logStr
.AppendFormat(" %02x", iData
);
668 m_currentframe
.push_back(iData
);
670 AddLog(CEC_LOG_DEBUG
, logStr
.c_str());
680 void CCECParser::ParseCurrentFrame(void)
682 uint8_t initiator
= m_currentframe
[0] >> 4;
683 uint8_t destination
= m_currentframe
[0] & 0xF;
686 dataStr
.Format("received frame: initiator: %u destination: %u", initiator
, destination
);
688 if (m_currentframe
.size() > 1)
691 for (unsigned int i
= 1; i
< m_currentframe
.size(); i
++)
692 dataStr
.AppendFormat(" %02x", m_currentframe
[i
]);
694 AddLog(CEC_LOG_DEBUG
, dataStr
.c_str());
696 if (m_currentframe
.size() <= 1)
700 cec_opcode opCode
= (cec_opcode
) m_currentframe
[1];
701 if (destination
== (uint16_t) m_iLogicalAddress
)
705 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
706 ReportPhysicalAddress();
709 case CEC_OPCODE_GIVE_OSD_NAME
:
710 ReportOSDName((cec_logical_address
)initiator
);
712 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
713 ReportVendorID((cec_logical_address
)initiator
);
715 case CEC_OPCODE_MENU_REQUEST
:
716 ReportMenuState((cec_logical_address
)initiator
);
718 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
719 ReportPowerState((cec_logical_address
)initiator
);
721 case CEC_OPCODE_GET_CEC_VERSION
:
722 ReportCECVersion((cec_logical_address
)initiator
);
724 case CEC_OPCODE_USER_CONTROL_PRESSED
:
725 if (m_currentframe
.size() > 2)
729 if (m_currentframe
[2] <= CEC_USER_CONTROL_CODE_MAX
)
731 m_iCurrentButton
= (cec_user_control_code
) m_currentframe
[2];
732 m_buttontime
= GetTimeMs();
736 case CEC_OPCODE_USER_CONTROL_RELEASE
:
740 cec_frame params
= m_currentframe
;
741 params
.erase(params
.begin(), params
.begin() + 2);
742 AddCommand((cec_logical_address
) initiator
, (cec_logical_address
) destination
, opCode
, ¶ms
);
746 else if (destination
== (uint8_t) CECDEVICE_BROADCAST
)
748 if (opCode
== CEC_OPCODE_REQUEST_ACTIVE_SOURCE
)
750 BroadcastActiveSource();
752 else if (opCode
== CEC_OPCODE_SET_STREAM_PATH
)
754 if (m_currentframe
.size() >= 4)
756 int streamaddr
= ((int)m_currentframe
[2] << 8) | ((int)m_currentframe
[3]);
758 strLog
.Format("%i requests stream path from physical address %04x", initiator
, streamaddr
);
759 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
760 if (streamaddr
== m_physicaladdress
)
761 BroadcastActiveSource();
768 strLog
.Format("ignoring frame: destination: %u != %u", destination
, (uint16_t)m_iLogicalAddress
);
769 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
773 void CCECParser::AddData(uint8_t *data
, int iLen
)
775 if (iLen
+ m_iInbufUsed
> m_iInbufSize
)
777 m_iInbufSize
= iLen
+ m_iInbufUsed
;
778 m_inbuf
= (uint8_t*)realloc(m_inbuf
, m_iInbufSize
);
781 memcpy(m_inbuf
+ m_iInbufUsed
, data
, iLen
);
782 m_iInbufUsed
+= iLen
;
785 void CCECParser::PushEscaped(cec_frame
&vec
, uint8_t byte
)
787 if (byte
>= MSGESC
&& byte
!= MSGSTART
)
789 vec
.push_back(MSGESC
);
790 vec
.push_back(byte
- ESCOFFSET
);
798 void CCECParser::CheckKeypressTimeout(int64_t now
)
800 if (m_iCurrentButton
!= CEC_USER_CONTROL_CODE_UNKNOWN
&& now
- m_buttontime
> 500)
803 m_iCurrentButton
= CEC_USER_CONTROL_CODE_UNKNOWN
;
807 bool CCECParser::SetLogicalAddress(cec_logical_address iLogicalAddress
)
810 strLog
.Format("setting logical address to %d", iLogicalAddress
);
811 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
813 m_iLogicalAddress
= iLogicalAddress
;
814 return SetAckMask(0x1 << (uint8_t)m_iLogicalAddress
);
817 bool CCECParser::SetAckMask(uint16_t iMask
)
820 strLog
.Format("setting ackmask to %2x", iMask
);
821 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
825 output
.push_back(MSGSTART
);
826 PushEscaped(output
, MSGCODE_SET_ACK_MASK
);
827 PushEscaped(output
, iMask
>> 8);
828 PushEscaped(output
, (uint8_t)iMask
);
829 output
.push_back(MSGEND
);
831 if (m_serialport
->Write(output
) == -1)
833 strLog
.Format("error writing to serial port: %s", m_serialport
->GetError().c_str());
834 AddLog(CEC_LOG_ERROR
, strLog
);
841 void CCECParser::AddLog(cec_log_level level
, const string
&strMessage
)
843 cec_log_message message
;
844 message
.level
= level
;
845 message
.message
.assign(strMessage
.c_str());
846 m_logBuffer
.Push(message
);
849 void CCECParser::AddKey(void)
851 if (m_iCurrentButton
!= CEC_USER_CONTROL_CODE_UNKNOWN
)
854 key
.duration
= (unsigned int) (GetTimeMs() - m_buttontime
);
855 key
.keycode
= m_iCurrentButton
;
856 m_keyBuffer
.Push(key
);
857 m_iCurrentButton
= CEC_USER_CONTROL_CODE_UNKNOWN
;
862 void CCECParser::AddCommand(cec_logical_address source
, cec_logical_address destination
, cec_opcode opcode
, cec_frame
*parameters
)
865 command
.source
= source
;
866 command
.destination
= destination
;
867 command
.opcode
= opcode
;
869 command
.parameters
= *parameters
;
870 m_commandBuffer
.Push(command
);
873 int CCECParser::GetMinVersion(void)
875 return CEC_MIN_VERSION
;
878 int CCECParser::GetLibVersion(void)
880 return CEC_LIB_VERSION
;
883 int CCECParser::FindDevices(std::vector
<cec_device
> &deviceList
, const char *strDevicePath
/* = NULL */)
887 strDebug
.Format("trying to autodetect the com port for device path '%s'", strDevicePath
);
889 strDebug
.Format("trying to autodetect all CEC adapters");
890 AddLog(CEC_LOG_DEBUG
, strDebug
);
892 return CCECDetect::FindDevices(deviceList
, strDevicePath
);
895 DECLSPEC
void * CECCreate(const char *strDeviceName
, CEC::cec_logical_address iLogicalAddress
/*= CEC::CECDEVICE_PLAYBACKDEVICE1 */, int iPhysicalAddress
/* = CEC_DEFAULT_PHYSICAL_ADDRESS */)
897 return static_cast< void* > (new CCECParser(strDeviceName
, iLogicalAddress
, iPhysicalAddress
));