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 "CECProcessor.h"
35 #include "AdapterCommunication.h"
37 #include "util/StdString.h"
38 #include "platform/timeutils.h"
43 CCECProcessor::CCECProcessor(CLibCEC
*controller
, CAdapterCommunication
*serComm
, const char *strDeviceName
, cec_logical_address iLogicalAddress
/* = CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress
/* = CEC_DEFAULT_PHYSICAL_ADDRESS*/) :
44 m_physicaladdress(iPhysicalAddress
),
45 m_iLogicalAddress(iLogicalAddress
),
46 m_strDeviceName(strDeviceName
),
47 m_communication(serComm
),
48 m_controller(controller
)
52 CCECProcessor::~CCECProcessor(void)
55 m_communication
= NULL
;
59 bool CCECProcessor::Start(void)
61 if (!m_communication
|| !m_communication
->IsOpen())
63 m_controller
->AddLog(CEC_LOG_ERROR
, "connection is closed");
67 if (!SetLogicalAddress(m_iLogicalAddress
))
69 m_controller
->AddLog(CEC_LOG_ERROR
, "could not set the logical address");
76 m_controller
->AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
81 void *CCECProcessor::Process(void)
83 m_controller
->AddLog(CEC_LOG_DEBUG
, "processor thread started");
87 bool bParseFrame(false);
89 CLockObject
lock(&m_mutex
);
93 if (m_communication
->IsOpen() && m_communication
->Read(msg
, CEC_BUTTON_TIMEOUT
))
94 bParseFrame
= ParseMessage(msg
) && !IsStopped();
100 m_controller
->CheckKeypressTimeout();
109 bool CCECProcessor::PowerOnDevices(cec_logical_address address
/* = CECDEVICE_TV */)
115 strLog
.Format("powering on devices with logical address %d", (int8_t)address
);
116 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
120 frame
.push_back(GetSourceDestination(address
));
121 frame
.push_back((uint8_t) CEC_OPCODE_IMAGE_VIEW_ON
);
122 return Transmit(frame
);
125 bool CCECProcessor::StandbyDevices(cec_logical_address address
/* = CECDEVICE_BROADCAST */)
131 strLog
.Format("putting all devices with logical address %d in standby mode", (int8_t)address
);
132 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
136 frame
.push_back(GetSourceDestination(address
));
137 frame
.push_back((uint8_t) CEC_OPCODE_STANDBY
);
138 return Transmit(frame
);
141 bool CCECProcessor::SetActiveView(void)
146 m_controller
->AddLog(CEC_LOG_DEBUG
, "setting active view");
150 frame
.push_back(GetSourceDestination(CECDEVICE_BROADCAST
));
151 frame
.push_back((uint8_t) CEC_OPCODE_ACTIVE_SOURCE
);
152 frame
.push_back((m_physicaladdress
>> 8) & 0xFF);
153 frame
.push_back(m_physicaladdress
& 0xFF);
154 return Transmit(frame
);
157 bool CCECProcessor::SetInactiveView(void)
162 m_controller
->AddLog(CEC_LOG_DEBUG
, "setting inactive view");
166 frame
.push_back(GetSourceDestination(CECDEVICE_BROADCAST
));
167 frame
.push_back((uint8_t) CEC_OPCODE_INACTIVE_SOURCE
);
168 frame
.push_back((m_physicaladdress
>> 8) & 0xFF);
169 frame
.push_back(m_physicaladdress
& 0xFF);
170 return Transmit(frame
);
173 bool CCECProcessor::Transmit(const cec_frame
&data
, bool bWaitForAck
/* = true */)
175 CStdString txStr
= "transmit ";
176 for (unsigned int i
= 0; i
< data
.size
; i
++)
177 txStr
.AppendFormat(" %02x", data
.data
[i
]);
178 m_controller
->AddLog(CEC_LOG_DEBUG
, txStr
.c_str());
182 m_controller
->AddLog(CEC_LOG_WARNING
, "transmit buffer is empty");
189 //set ack polarity to high when transmitting to the broadcast address
190 //set ack polarity low when transmitting to any other address
191 output
.push_back(MSGSTART
);
192 CAdapterCommunication::PushEscaped(output
, MSGCODE_TRANSMIT_ACK_POLARITY
);
194 if ((data
.data
[0] & 0xF) == 0xF)
195 CAdapterCommunication::PushEscaped(output
, CEC_TRUE
);
197 CAdapterCommunication::PushEscaped(output
, CEC_FALSE
);
199 output
.push_back(MSGEND
);
201 for (int8_t i
= 0; i
< data
.size
; i
++)
203 output
.push_back(MSGSTART
);
205 if (i
== (int8_t)data
.size
- 1)
206 CAdapterCommunication::PushEscaped(output
, MSGCODE_TRANSMIT_EOM
);
208 CAdapterCommunication::PushEscaped(output
, MSGCODE_TRANSMIT
);
210 CAdapterCommunication::PushEscaped(output
, data
.data
[i
]);
212 output
.push_back(MSGEND
);
215 return TransmitFormatted(output
, bWaitForAck
);
218 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress
)
221 strLog
.Format("setting logical address to %d", iLogicalAddress
);
222 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
224 m_iLogicalAddress
= iLogicalAddress
;
225 return m_communication
&& m_communication
->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress
);
228 bool CCECProcessor::TransmitFormatted(const cec_frame
&data
, bool bWaitForAck
/* = true */)
230 CLockObject
lock(&m_mutex
);
231 if (!m_communication
|| !m_communication
->Write(data
))
234 if (bWaitForAck
&& !WaitForAck())
236 m_controller
->AddLog(CEC_LOG_DEBUG
, "did not receive ACK");
243 void CCECProcessor::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, ECecAbortReason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
245 m_controller
->AddLog(CEC_LOG_DEBUG
, "transmitting abort message");
249 frame
.push_back(GetSourceDestination(address
));
250 frame
.push_back((uint8_t) CEC_OPCODE_FEATURE_ABORT
);
251 frame
.push_back((uint8_t) opcode
);
252 frame
.push_back((uint8_t) reason
);
256 void CCECProcessor::ReportCECVersion(cec_logical_address address
/* = CECDEVICE_TV */)
261 m_controller
->AddLog(CEC_LOG_NOTICE
, "reporting CEC version as 1.3a");
262 frame
.push_back(GetSourceDestination(address
));
263 frame
.push_back((uint8_t) CEC_OPCODE_CEC_VERSION
);
264 frame
.push_back((uint8_t) CEC_VERSION_1_3A
);
268 void CCECProcessor::ReportPowerState(cec_logical_address address
/*= CECDEVICE_TV */, bool bOn
/* = true */)
274 m_controller
->AddLog(CEC_LOG_NOTICE
, "reporting \"On\" power status");
276 m_controller
->AddLog(CEC_LOG_NOTICE
, "reporting \"Off\" power status");
278 frame
.push_back(GetSourceDestination(address
));
279 frame
.push_back((uint8_t) CEC_OPCODE_REPORT_POWER_STATUS
);
280 frame
.push_back(bOn
? (uint8_t) CEC_POWER_STATUS_ON
: (uint8_t) CEC_POWER_STATUS_STANDBY
);
284 void CCECProcessor::ReportMenuState(cec_logical_address address
/* = CECDEVICE_TV */, bool bActive
/* = true */)
290 m_controller
->AddLog(CEC_LOG_NOTICE
, "reporting menu state as active");
292 m_controller
->AddLog(CEC_LOG_NOTICE
, "reporting menu state as inactive");
294 frame
.push_back(GetSourceDestination(address
));
295 frame
.push_back((uint8_t) CEC_OPCODE_MENU_STATUS
);
296 frame
.push_back(bActive
? (uint8_t) CEC_MENU_STATE_ACTIVATED
: (uint8_t) CEC_MENU_STATE_DEACTIVATED
);
300 void CCECProcessor::ReportVendorID(cec_logical_address address
/* = CECDEVICE_TV */)
302 m_controller
->AddLog(CEC_LOG_NOTICE
, "vendor ID requested, feature abort");
303 TransmitAbort(address
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
306 void CCECProcessor::ReportOSDName(cec_logical_address address
/* = CECDEVICE_TV */)
311 const char *osdname
= m_strDeviceName
.c_str();
313 strLog
.Format("reporting OSD name as %s", osdname
);
314 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
315 frame
.push_back(GetSourceDestination(address
));
316 frame
.push_back((uint8_t) CEC_OPCODE_SET_OSD_NAME
);
318 for (unsigned int i
= 0; i
< strlen(osdname
); i
++)
319 frame
.push_back(osdname
[i
]);
324 void CCECProcessor::ReportPhysicalAddress(void)
330 strLog
.Format("reporting physical address as %04x", m_physicaladdress
);
331 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
332 frame
.push_back(GetSourceDestination(CECDEVICE_BROADCAST
));
333 frame
.push_back((uint8_t) CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
334 frame
.push_back((uint8_t) ((m_physicaladdress
>> 8) & 0xFF));
335 frame
.push_back((uint8_t) (m_physicaladdress
& 0xFF));
336 frame
.push_back((uint8_t) CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
340 void CCECProcessor::BroadcastActiveSource(void)
345 m_controller
->AddLog(CEC_LOG_NOTICE
, "broadcasting active source");
346 frame
.push_back(GetSourceDestination(CECDEVICE_BROADCAST
));
347 frame
.push_back((uint8_t) CEC_OPCODE_ACTIVE_SOURCE
);
348 frame
.push_back((uint8_t) ((m_physicaladdress
>> 8) & 0xFF));
349 frame
.push_back((uint8_t) (m_physicaladdress
& 0xFF));
353 uint8_t CCECProcessor::GetSourceDestination(cec_logical_address destination
/* = CECDEVICE_BROADCAST */) const
355 return ((uint8_t)m_iLogicalAddress
<< 4) + (uint8_t)destination
;
358 bool CCECProcessor::WaitForAck(uint32_t iTimeout
/* = 1000 */)
363 int64_t iNow
= GetTimeMs();
364 int64_t iTargetTime
= iNow
+ (uint64_t) iTimeout
;
366 while (!bGotAck
&& !bError
&& (iTimeout
== 0 || iNow
< iTargetTime
))
371 while (!bGotAck
&& !bError
&& m_communication
->Read(msg
, iTimeout
))
373 uint8_t iCode
= msg
.data
[0] & ~(MSGCODE_FRAME_EOM
| MSGCODE_FRAME_ACK
);
377 case MSGCODE_COMMAND_ACCEPTED
:
378 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_COMMAND_ACCEPTED");
380 case MSGCODE_TRANSMIT_SUCCEEDED
:
381 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_TRANSMIT_SUCCEEDED");
385 case MSGCODE_RECEIVE_FAILED
:
386 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_RECEIVE_FAILED");
389 case MSGCODE_COMMAND_REJECTED
:
390 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_COMMAND_REJECTED");
393 case MSGCODE_TRANSMIT_FAILED_LINE
:
394 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_LINE");
397 case MSGCODE_TRANSMIT_FAILED_ACK
:
398 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_ACK");
401 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
:
402 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA");
405 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
:
406 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE");
410 m_frameBuffer
.Push(msg
);
411 bGotAck
= (msg
.data
[0] & MSGCODE_FRAME_ACK
) != 0;
418 return bGotAck
&& !bError
;
421 bool CCECProcessor::ParseMessage(cec_frame
&msg
)
429 uint8_t iCode
= msg
.data
[0] & ~(MSGCODE_FRAME_EOM
| MSGCODE_FRAME_ACK
);
430 bool bEom
= (msg
.data
[0] & MSGCODE_FRAME_EOM
) != 0;
431 bool bAck
= (msg
.data
[0] & MSGCODE_FRAME_ACK
) != 0;
435 case MSGCODE_NOTHING
:
436 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_NOTHING");
438 case MSGCODE_TIMEOUT_ERROR
:
439 case MSGCODE_HIGH_ERROR
:
440 case MSGCODE_LOW_ERROR
:
442 if (iCode
== MSGCODE_TIMEOUT_ERROR
)
443 logStr
= "MSGCODE_TIMEOUT";
444 else if (iCode
== MSGCODE_HIGH_ERROR
)
445 logStr
= "MSGCODE_HIGH_ERROR";
447 logStr
= "MSGCODE_LOW_ERROR";
449 int iLine
= (msg
.size
>= 3) ? (msg
.data
[1] << 8) | (msg
.data
[2]) : 0;
450 uint32_t iTime
= (msg
.size
>= 7) ? (msg
.data
[3] << 24) | (msg
.data
[4] << 16) | (msg
.data
[5] << 8) | (msg
.data
[6]) : 0;
451 logStr
.AppendFormat(" line:%i", iLine
);
452 logStr
.AppendFormat(" time:%u", iTime
);
453 m_controller
->AddLog(CEC_LOG_WARNING
, logStr
.c_str());
456 case MSGCODE_FRAME_START
:
458 logStr
= "MSGCODE_FRAME_START";
459 m_currentframe
.clear();
462 int iInitiator
= msg
.data
[1] >> 4;
463 int iDestination
= msg
.data
[1] & 0xF;
464 logStr
.AppendFormat(" initiator:%u destination:%u ack:%s %s", iInitiator
, iDestination
, bAck
? "high" : "low", bEom
? "eom" : "");
466 m_currentframe
.push_back(msg
.data
[1]);
468 m_controller
->AddLog(CEC_LOG_DEBUG
, logStr
.c_str());
471 case MSGCODE_FRAME_DATA
:
473 logStr
= "MSGCODE_FRAME_DATA";
476 uint8_t iData
= msg
.data
[1];
477 logStr
.AppendFormat(" %02x", iData
);
478 m_currentframe
.push_back(iData
);
480 m_controller
->AddLog(CEC_LOG_DEBUG
, logStr
.c_str());
492 void CCECProcessor::ParseCurrentFrame(void)
494 cec_frame frame
= m_currentframe
;
495 m_currentframe
.clear();
497 uint8_t initiator
= frame
.data
[0] >> 4;
498 uint8_t destination
= frame
.data
[0] & 0xF;
501 dataStr
.Format("received frame: initiator: %u destination: %u", initiator
, destination
);
506 for (unsigned int i
= 1; i
< frame
.size
; i
++)
507 dataStr
.AppendFormat(" %02x", frame
.data
[i
]);
509 m_controller
->AddLog(CEC_LOG_DEBUG
, dataStr
.c_str());
514 cec_opcode opCode
= (cec_opcode
) frame
.data
[1];
515 if (destination
== (uint16_t) m_iLogicalAddress
)
519 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
520 ReportPhysicalAddress();
523 case CEC_OPCODE_GIVE_OSD_NAME
:
524 ReportOSDName((cec_logical_address
)initiator
);
526 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
527 ReportVendorID((cec_logical_address
)initiator
);
529 case CEC_OPCODE_MENU_REQUEST
:
530 ReportMenuState((cec_logical_address
)initiator
);
532 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
533 ReportPowerState((cec_logical_address
)initiator
);
535 case CEC_OPCODE_GET_CEC_VERSION
:
536 ReportCECVersion((cec_logical_address
)initiator
);
538 case CEC_OPCODE_USER_CONTROL_PRESSED
:
541 m_controller
->AddKey();
543 if (frame
.data
[2] <= CEC_USER_CONTROL_CODE_MAX
)
544 m_controller
->SetCurrentButton((cec_user_control_code
) frame
.data
[2]);
547 case CEC_OPCODE_USER_CONTROL_RELEASE
:
548 m_controller
->AddKey();
551 cec_frame params
= frame
;
553 m_controller
->AddCommand((cec_logical_address
) initiator
, (cec_logical_address
) destination
, opCode
, ¶ms
);
557 else if (destination
== (uint8_t) CECDEVICE_BROADCAST
)
560 if (opCode
== CEC_OPCODE_REQUEST_ACTIVE_SOURCE
)
562 strLog
.Format("%i requests active source", initiator
);
563 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
564 BroadcastActiveSource();
566 else if (opCode
== CEC_OPCODE_SET_STREAM_PATH
)
570 int streamaddr
= ((int)frame
.data
[2] << 8) | ((int)frame
.data
[3]);
571 strLog
.Format("%i requests stream path from physical address %04x", initiator
, streamaddr
);
572 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
573 if (streamaddr
== m_physicaladdress
)
574 BroadcastActiveSource();
579 cec_frame params
= frame
;
581 m_controller
->AddCommand((cec_logical_address
) initiator
, (cec_logical_address
) destination
, opCode
, ¶ms
);
587 strLog
.Format("ignoring frame: destination: %u != %u", destination
, (uint16_t)m_iLogicalAddress
);
588 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());