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())
64 if (!SetLogicalAddress(m_iLogicalAddress
))
66 m_controller
->AddLog(CEC_LOG_ERROR
, "could not set the logical address");
73 m_controller
->AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
78 void *CCECProcessor::Process(void)
80 m_controller
->AddLog(CEC_LOG_DEBUG
, "processor thread started");
84 bool bParseFrame(false);
86 CLockObject
lock(&m_mutex
);
88 if (!m_bStop
&& m_communication
->IsOpen() && m_communication
->Read(msg
, CEC_BUTTON_TIMEOUT
))
89 bParseFrame
= ParseMessage(msg
);
92 if (!m_bStop
&& bParseFrame
)
97 m_controller
->CheckKeypressTimeout();
98 CCondition::Sleep(50);
105 bool CCECProcessor::PowerOnDevices(cec_logical_address address
/* = CECDEVICE_TV */)
111 strLog
.Format("powering on devices with logical address %d", (int8_t)address
);
112 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
114 frame
.push_back(GetSourceDestination(address
));
115 frame
.push_back((uint8_t) CEC_OPCODE_TEXT_VIEW_ON
);
116 return Transmit(frame
);
119 bool CCECProcessor::StandbyDevices(cec_logical_address address
/* = CECDEVICE_BROADCAST */)
125 strLog
.Format("putting all devices with logical address %d in standby mode", (int8_t)address
);
126 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
128 frame
.push_back(GetSourceDestination(address
));
129 frame
.push_back((uint8_t) CEC_OPCODE_STANDBY
);
130 return Transmit(frame
);
133 bool CCECProcessor::SetActiveView(void)
138 m_controller
->AddLog(CEC_LOG_DEBUG
, "setting active view");
140 frame
.push_back(GetSourceDestination(CECDEVICE_BROADCAST
));
141 frame
.push_back((uint8_t) CEC_OPCODE_ACTIVE_SOURCE
);
142 frame
.push_back((m_physicaladdress
>> 8) & 0xFF);
143 frame
.push_back(m_physicaladdress
& 0xFF);
144 return Transmit(frame
);
147 bool CCECProcessor::SetInactiveView(void)
152 m_controller
->AddLog(CEC_LOG_DEBUG
, "setting inactive view");
154 frame
.push_back(GetSourceDestination(CECDEVICE_BROADCAST
));
155 frame
.push_back((uint8_t) CEC_OPCODE_INACTIVE_SOURCE
);
156 frame
.push_back((m_physicaladdress
>> 8) & 0xFF);
157 frame
.push_back(m_physicaladdress
& 0xFF);
158 return Transmit(frame
);
161 bool CCECProcessor::Transmit(const cec_frame
&data
, bool bWaitForAck
/* = true */)
163 CStdString txStr
= "transmit ";
164 for (unsigned int i
= 0; i
< data
.size(); i
++)
165 txStr
.AppendFormat(" %02x", data
[i
]);
166 m_controller
->AddLog(CEC_LOG_DEBUG
, txStr
.c_str());
170 m_controller
->AddLog(CEC_LOG_WARNING
, "transmit buffer is empty");
176 //set ack polarity to high when transmitting to the broadcast address
177 //set ack polarity low when transmitting to any other address
178 output
.push_back(MSGSTART
);
179 CAdapterCommunication::PushEscaped(output
, MSGCODE_TRANSMIT_ACK_POLARITY
);
181 if ((data
[0] & 0xF) == 0xF)
182 CAdapterCommunication::PushEscaped(output
, CEC_TRUE
);
184 CAdapterCommunication::PushEscaped(output
, CEC_FALSE
);
186 output
.push_back(MSGEND
);
188 for (unsigned int i
= 0; i
< data
.size(); i
++)
190 output
.push_back(MSGSTART
);
192 if (i
== data
.size() - 1)
193 CAdapterCommunication::PushEscaped(output
, MSGCODE_TRANSMIT_EOM
);
195 CAdapterCommunication::PushEscaped(output
, MSGCODE_TRANSMIT
);
197 CAdapterCommunication::PushEscaped(output
, data
[i
]);
199 output
.push_back(MSGEND
);
202 return TransmitFormatted(output
, bWaitForAck
);
205 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress
)
208 strLog
.Format("setting logical address to %d", iLogicalAddress
);
209 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
211 m_iLogicalAddress
= iLogicalAddress
;
212 return m_communication
&& m_communication
->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress
);
215 bool CCECProcessor::TransmitFormatted(const cec_frame
&data
, bool bWaitForAck
/* = true */)
217 CLockObject
lock(&m_mutex
);
218 if (!m_communication
|| !m_communication
->Write(data
))
221 if (bWaitForAck
&& !WaitForAck())
223 m_controller
->AddLog(CEC_LOG_DEBUG
, "did not receive ACK");
230 void CCECProcessor::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, ECecAbortReason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
232 m_controller
->AddLog(CEC_LOG_DEBUG
, "transmitting abort message");
234 frame
.push_back(GetSourceDestination(address
));
235 frame
.push_back((uint8_t) CEC_OPCODE_FEATURE_ABORT
);
236 frame
.push_back((uint8_t) opcode
);
237 frame
.push_back((uint8_t) reason
);
241 void CCECProcessor::ReportCECVersion(cec_logical_address address
/* = CECDEVICE_TV */)
244 m_controller
->AddLog(CEC_LOG_NOTICE
, "reporting CEC version as 1.3a");
245 frame
.push_back(GetSourceDestination(address
));
246 frame
.push_back((uint8_t) CEC_OPCODE_CEC_VERSION
);
247 frame
.push_back(CEC_VERSION_1_3A
);
251 void CCECProcessor::ReportPowerState(cec_logical_address address
/*= CECDEVICE_TV */, bool bOn
/* = true */)
255 m_controller
->AddLog(CEC_LOG_NOTICE
, "reporting \"On\" power status");
257 m_controller
->AddLog(CEC_LOG_NOTICE
, "reporting \"Off\" power status");
259 frame
.push_back(GetSourceDestination(address
));
260 frame
.push_back((uint8_t) CEC_OPCODE_REPORT_POWER_STATUS
);
261 frame
.push_back(bOn
? (uint8_t) CEC_POWER_STATUS_ON
: (uint8_t) CEC_POWER_STATUS_STANDBY
);
265 void CCECProcessor::ReportMenuState(cec_logical_address address
/* = CECDEVICE_TV */, bool bActive
/* = true */)
269 m_controller
->AddLog(CEC_LOG_NOTICE
, "reporting menu state as active");
271 m_controller
->AddLog(CEC_LOG_NOTICE
, "reporting menu state as inactive");
273 frame
.push_back(GetSourceDestination(address
));
274 frame
.push_back((uint8_t) CEC_OPCODE_MENU_STATUS
);
275 frame
.push_back(bActive
? (uint8_t) CEC_MENU_STATE_ACTIVATED
: (uint8_t) CEC_MENU_STATE_DEACTIVATED
);
279 void CCECProcessor::ReportVendorID(cec_logical_address address
/* = CECDEVICE_TV */)
281 m_controller
->AddLog(CEC_LOG_NOTICE
, "vendor ID requested, feature abort");
282 TransmitAbort(address
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
285 void CCECProcessor::ReportOSDName(cec_logical_address address
/* = CECDEVICE_TV */)
288 const char *osdname
= m_strDeviceName
.c_str();
290 strLog
.Format("reporting OSD name as %s", osdname
);
291 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
292 frame
.push_back(GetSourceDestination(address
));
293 frame
.push_back((uint8_t) CEC_OPCODE_SET_OSD_NAME
);
295 for (unsigned int i
= 0; i
< strlen(osdname
); i
++)
296 frame
.push_back(osdname
[i
]);
301 void CCECProcessor::ReportPhysicalAddress(void)
305 strLog
.Format("reporting physical address as %04x", m_physicaladdress
);
306 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
307 frame
.push_back(GetSourceDestination(CECDEVICE_BROADCAST
));
308 frame
.push_back((uint8_t) CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
309 frame
.push_back((m_physicaladdress
>> 8) & 0xFF);
310 frame
.push_back(m_physicaladdress
& 0xFF);
311 frame
.push_back(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
315 void CCECProcessor::BroadcastActiveSource(void)
318 m_controller
->AddLog(CEC_LOG_NOTICE
, "broadcasting active source");
319 frame
.push_back(GetSourceDestination(CECDEVICE_BROADCAST
));
320 frame
.push_back((uint8_t) CEC_OPCODE_ACTIVE_SOURCE
);
321 frame
.push_back((m_physicaladdress
>> 8) & 0xFF);
322 frame
.push_back(m_physicaladdress
& 0xFF);
326 uint8_t CCECProcessor::GetSourceDestination(cec_logical_address destination
/* = CECDEVICE_BROADCAST */) const
328 return ((uint8_t)m_iLogicalAddress
<< 4) + (uint8_t)destination
;
331 bool CCECProcessor::WaitForAck(int iTimeout
/* = 1000 */)
336 int64_t iNow
= GetTimeMs();
337 int64_t iTargetTime
= iNow
+ (int64_t) iTimeout
;
339 while (!bGotAck
&& !bError
&& (iTimeout
<= 0 || iNow
< iTargetTime
))
342 while (!bGotAck
&& !bError
&& m_communication
->Read(msg
, iTimeout
))
344 uint8_t iCode
= msg
[0] & ~(MSGCODE_FRAME_EOM
| MSGCODE_FRAME_ACK
);
348 case MSGCODE_COMMAND_ACCEPTED
:
349 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_COMMAND_ACCEPTED");
351 case MSGCODE_TRANSMIT_SUCCEEDED
:
352 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_TRANSMIT_SUCCEEDED");
356 case MSGCODE_RECEIVE_FAILED
:
357 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_RECEIVE_FAILED");
360 case MSGCODE_COMMAND_REJECTED
:
361 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_COMMAND_REJECTED");
364 case MSGCODE_TRANSMIT_FAILED_LINE
:
365 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_LINE");
368 case MSGCODE_TRANSMIT_FAILED_ACK
:
369 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_ACK");
372 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
:
373 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA");
376 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
:
377 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE");
381 m_frameBuffer
.Push(msg
);
382 bGotAck
= (msg
[0] & MSGCODE_FRAME_ACK
) != 0;
389 return bGotAck
&& !bError
;
392 bool CCECProcessor::ParseMessage(cec_frame
&msg
)
400 uint8_t iCode
= msg
[0] & ~(MSGCODE_FRAME_EOM
| MSGCODE_FRAME_ACK
);
401 bool bEom
= (msg
[0] & MSGCODE_FRAME_EOM
) != 0;
402 bool bAck
= (msg
[0] & MSGCODE_FRAME_ACK
) != 0;
406 case MSGCODE_NOTHING
:
407 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_NOTHING");
409 case MSGCODE_TIMEOUT_ERROR
:
410 case MSGCODE_HIGH_ERROR
:
411 case MSGCODE_LOW_ERROR
:
413 if (iCode
== MSGCODE_TIMEOUT_ERROR
)
414 logStr
= "MSGCODE_TIMEOUT";
415 else if (iCode
== MSGCODE_HIGH_ERROR
)
416 logStr
= "MSGCODE_HIGH_ERROR";
418 logStr
= "MSGCODE_LOW_ERROR";
420 int iLine
= (msg
.size() >= 3) ? (msg
[1] << 8) | (msg
[2]) : 0;
421 uint32_t iTime
= (msg
.size() >= 7) ? (msg
[3] << 24) | (msg
[4] << 16) | (msg
[5] << 8) | (msg
[6]) : 0;
422 logStr
.AppendFormat(" line:%i", iLine
);
423 logStr
.AppendFormat(" time:%u", iTime
);
424 m_controller
->AddLog(CEC_LOG_WARNING
, logStr
.c_str());
427 case MSGCODE_FRAME_START
:
429 logStr
= "MSGCODE_FRAME_START";
430 m_currentframe
.clear();
433 int iInitiator
= msg
[1] >> 4;
434 int iDestination
= msg
[1] & 0xF;
435 logStr
.AppendFormat(" initiator:%u destination:%u ack:%s %s", iInitiator
, iDestination
, bAck
? "high" : "low", bEom
? "eom" : "");
437 m_currentframe
.push_back(msg
[1]);
439 m_controller
->AddLog(CEC_LOG_DEBUG
, logStr
.c_str());
442 case MSGCODE_FRAME_DATA
:
444 logStr
= "MSGCODE_FRAME_DATA";
447 uint8_t iData
= msg
[1];
448 logStr
.AppendFormat(" %02x", iData
);
449 m_currentframe
.push_back(iData
);
451 m_controller
->AddLog(CEC_LOG_DEBUG
, logStr
.c_str());
463 void CCECProcessor::ParseCurrentFrame(void)
465 uint8_t initiator
= m_currentframe
[0] >> 4;
466 uint8_t destination
= m_currentframe
[0] & 0xF;
469 dataStr
.Format("received frame: initiator: %u destination: %u", initiator
, destination
);
471 if (m_currentframe
.size() > 1)
474 for (unsigned int i
= 1; i
< m_currentframe
.size(); i
++)
475 dataStr
.AppendFormat(" %02x", m_currentframe
[i
]);
477 m_controller
->AddLog(CEC_LOG_DEBUG
, dataStr
.c_str());
479 if (m_currentframe
.size() <= 1)
483 cec_opcode opCode
= (cec_opcode
) m_currentframe
[1];
484 if (destination
== (uint16_t) m_iLogicalAddress
)
488 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
489 ReportPhysicalAddress();
492 case CEC_OPCODE_GIVE_OSD_NAME
:
493 ReportOSDName((cec_logical_address
)initiator
);
495 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
496 ReportVendorID((cec_logical_address
)initiator
);
498 case CEC_OPCODE_MENU_REQUEST
:
499 ReportMenuState((cec_logical_address
)initiator
);
501 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
502 ReportPowerState((cec_logical_address
)initiator
);
504 case CEC_OPCODE_GET_CEC_VERSION
:
505 ReportCECVersion((cec_logical_address
)initiator
);
507 case CEC_OPCODE_USER_CONTROL_PRESSED
:
508 if (m_currentframe
.size() > 2)
510 m_controller
->AddKey();
512 if (m_currentframe
[2] <= CEC_USER_CONTROL_CODE_MAX
)
513 m_controller
->SetCurrentButton((cec_user_control_code
) m_currentframe
[2]);
516 case CEC_OPCODE_USER_CONTROL_RELEASE
:
517 m_controller
->AddKey();
520 cec_frame params
= m_currentframe
;
521 params
.erase(params
.begin(), params
.begin() + 2);
522 m_controller
->AddCommand((cec_logical_address
) initiator
, (cec_logical_address
) destination
, opCode
, ¶ms
);
526 else if (destination
== (uint8_t) CECDEVICE_BROADCAST
)
528 if (opCode
== CEC_OPCODE_REQUEST_ACTIVE_SOURCE
)
530 BroadcastActiveSource();
532 else if (opCode
== CEC_OPCODE_SET_STREAM_PATH
)
534 if (m_currentframe
.size() >= 4)
536 int streamaddr
= ((int)m_currentframe
[2] << 8) | ((int)m_currentframe
[3]);
538 strLog
.Format("%i requests stream path from physical address %04x", initiator
, streamaddr
);
539 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
540 if (streamaddr
== m_physicaladdress
)
541 BroadcastActiveSource();
546 cec_frame params
= m_currentframe
;
547 params
.erase(params
.begin(), params
.begin() + 2);
548 m_controller
->AddCommand((cec_logical_address
) initiator
, (cec_logical_address
) destination
, opCode
, ¶ms
);
554 strLog
.Format("ignoring frame: destination: %u != %u", destination
, (uint16_t)m_iLogicalAddress
);
555 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());