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 "util/timeutils.h"
43 CCECProcessor::CCECProcessor(CLibCEC
*controller
, CAdapterCommunication
*serComm
, const char *strDeviceName
, cec_logical_address iLogicalAddress
/* = CECDEVICE_PLAYBACKDEVICE1 */, int 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
);
95 m_controller
->CheckKeypressTimeout();
96 CCondition::Sleep(50);
99 m_controller
->AddLog(CEC_LOG_DEBUG
, "processor thread terminated");
103 bool CCECProcessor::PowerOnDevices(cec_logical_address address
/* = CECDEVICE_TV */)
109 strLog
.Format("powering on devices with logical address %d", (int8_t)address
);
110 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
112 frame
.push_back(GetSourceDestination(address
));
113 frame
.push_back(CEC_OPCODE_TEXT_VIEW_ON
);
114 return Transmit(frame
);
117 bool CCECProcessor::StandbyDevices(cec_logical_address address
/* = CECDEVICE_BROADCAST */)
123 strLog
.Format("putting all devices with logical address %d in standby mode", (int8_t)address
);
124 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
126 frame
.push_back(GetSourceDestination(address
));
127 frame
.push_back(CEC_OPCODE_STANDBY
);
128 return Transmit(frame
);
131 bool CCECProcessor::SetActiveView(void)
136 m_controller
->AddLog(CEC_LOG_DEBUG
, "setting active view");
138 frame
.push_back(GetSourceDestination(CECDEVICE_BROADCAST
));
139 frame
.push_back(CEC_OPCODE_ACTIVE_SOURCE
);
140 frame
.push_back((m_physicaladdress
>> 8) & 0xFF);
141 frame
.push_back(m_physicaladdress
& 0xFF);
142 return Transmit(frame
);
145 bool CCECProcessor::SetInactiveView(void)
150 m_controller
->AddLog(CEC_LOG_DEBUG
, "setting inactive view");
152 frame
.push_back(GetSourceDestination(CECDEVICE_BROADCAST
));
153 frame
.push_back(CEC_OPCODE_INACTIVE_SOURCE
);
154 frame
.push_back((m_physicaladdress
>> 8) & 0xFF);
155 frame
.push_back(m_physicaladdress
& 0xFF);
156 return Transmit(frame
);
159 bool CCECProcessor::Transmit(const cec_frame
&data
, bool bWaitForAck
/* = true */)
161 CStdString txStr
= "transmit ";
162 for (unsigned int i
= 0; i
< data
.size(); i
++)
163 txStr
.AppendFormat(" %02x", data
[i
]);
164 m_controller
->AddLog(CEC_LOG_DEBUG
, txStr
.c_str());
168 m_controller
->AddLog(CEC_LOG_WARNING
, "transmit buffer is empty");
174 //set ack polarity to high when transmitting to the broadcast address
175 //set ack polarity low when transmitting to any other address
176 output
.push_back(MSGSTART
);
177 CAdapterCommunication::PushEscaped(output
, MSGCODE_TRANSMIT_ACK_POLARITY
);
179 if ((data
[0] & 0xF) == 0xF)
180 CAdapterCommunication::PushEscaped(output
, CEC_TRUE
);
182 CAdapterCommunication::PushEscaped(output
, CEC_FALSE
);
184 output
.push_back(MSGEND
);
186 for (unsigned int i
= 0; i
< data
.size(); i
++)
188 output
.push_back(MSGSTART
);
190 if (i
== data
.size() - 1)
191 CAdapterCommunication::PushEscaped(output
, MSGCODE_TRANSMIT_EOM
);
193 CAdapterCommunication::PushEscaped(output
, MSGCODE_TRANSMIT
);
195 CAdapterCommunication::PushEscaped(output
, data
[i
]);
197 output
.push_back(MSGEND
);
200 return TransmitFormatted(output
, bWaitForAck
);
203 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress
)
206 strLog
.Format("setting logical address to %d", iLogicalAddress
);
207 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
209 m_iLogicalAddress
= iLogicalAddress
;
210 return m_communication
&& m_communication
->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress
);
213 bool CCECProcessor::TransmitFormatted(const cec_frame
&data
, bool bWaitForAck
/* = true */)
215 CLockObject
lock(&m_mutex
);
216 if (!m_communication
|| !m_communication
->Write(data
))
219 if (bWaitForAck
&& !WaitForAck())
221 m_controller
->AddLog(CEC_LOG_DEBUG
, "did not receive ACK");
228 void CCECProcessor::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, ECecAbortReason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
230 m_controller
->AddLog(CEC_LOG_DEBUG
, "transmitting abort message");
232 frame
.push_back(GetSourceDestination(address
));
233 frame
.push_back(CEC_OPCODE_FEATURE_ABORT
);
234 frame
.push_back(opcode
);
235 frame
.push_back(reason
);
239 void CCECProcessor::ReportCECVersion(cec_logical_address address
/* = CECDEVICE_TV */)
242 m_controller
->AddLog(CEC_LOG_NOTICE
, "reporting CEC version as 1.3a");
243 frame
.push_back(GetSourceDestination(address
));
244 frame
.push_back(CEC_OPCODE_CEC_VERSION
);
245 frame
.push_back(CEC_VERSION_1_3A
);
249 void CCECProcessor::ReportPowerState(cec_logical_address address
/*= CECDEVICE_TV */, bool bOn
/* = true */)
253 m_controller
->AddLog(CEC_LOG_NOTICE
, "reporting \"On\" power status");
255 m_controller
->AddLog(CEC_LOG_NOTICE
, "reporting \"Off\" power status");
257 frame
.push_back(GetSourceDestination(address
));
258 frame
.push_back(CEC_OPCODE_REPORT_POWER_STATUS
);
259 frame
.push_back(bOn
? CEC_POWER_STATUS_ON
: CEC_POWER_STATUS_STANDBY
);
263 void CCECProcessor::ReportMenuState(cec_logical_address address
/* = CECDEVICE_TV */, bool bActive
/* = true */)
267 m_controller
->AddLog(CEC_LOG_NOTICE
, "reporting menu state as active");
269 m_controller
->AddLog(CEC_LOG_NOTICE
, "reporting menu state as inactive");
271 frame
.push_back(GetSourceDestination(address
));
272 frame
.push_back(CEC_OPCODE_MENU_STATUS
);
273 frame
.push_back(bActive
? CEC_MENU_STATE_ACTIVATED
: CEC_MENU_STATE_DEACTIVATED
);
277 void CCECProcessor::ReportVendorID(cec_logical_address address
/* = CECDEVICE_TV */)
279 m_controller
->AddLog(CEC_LOG_NOTICE
, "vendor ID requested, feature abort");
280 TransmitAbort(address
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
283 void CCECProcessor::ReportOSDName(cec_logical_address address
/* = CECDEVICE_TV */)
286 const char *osdname
= m_strDeviceName
.c_str();
288 strLog
.Format("reporting OSD name as %s", osdname
);
289 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
290 frame
.push_back(GetSourceDestination(address
));
291 frame
.push_back(CEC_OPCODE_SET_OSD_NAME
);
293 for (unsigned int i
= 0; i
< strlen(osdname
); i
++)
294 frame
.push_back(osdname
[i
]);
299 void CCECProcessor::ReportPhysicalAddress(void)
303 strLog
.Format("reporting physical address as %04x", m_physicaladdress
);
304 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
305 frame
.push_back(GetSourceDestination(CECDEVICE_BROADCAST
));
306 frame
.push_back(CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
307 frame
.push_back((m_physicaladdress
>> 8) & 0xFF);
308 frame
.push_back(m_physicaladdress
& 0xFF);
309 frame
.push_back(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
313 void CCECProcessor::BroadcastActiveSource(void)
316 m_controller
->AddLog(CEC_LOG_NOTICE
, "broadcasting active source");
317 frame
.push_back(GetSourceDestination(CECDEVICE_BROADCAST
));
318 frame
.push_back(CEC_OPCODE_ACTIVE_SOURCE
);
319 frame
.push_back((m_physicaladdress
>> 8) & 0xFF);
320 frame
.push_back(m_physicaladdress
& 0xFF);
324 uint8_t CCECProcessor::GetSourceDestination(cec_logical_address destination
/* = CECDEVICE_BROADCAST */) const
326 return ((uint8_t)m_iLogicalAddress
<< 4) + (uint8_t)destination
;
329 bool CCECProcessor::WaitForAck(int iTimeout
/* = 1000 */)
334 int64_t iNow
= GetTimeMs();
335 int64_t iTargetTime
= iNow
+ (int64_t) iTimeout
;
337 while (!bGotAck
&& !bError
&& (iTimeout
<= 0 || iNow
< iTargetTime
))
340 while (!bGotAck
&& !bError
&& m_communication
->Read(msg
, iTimeout
))
342 uint8_t iCode
= msg
[0] & ~(MSGCODE_FRAME_EOM
| MSGCODE_FRAME_ACK
);
346 case MSGCODE_COMMAND_ACCEPTED
:
347 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_COMMAND_ACCEPTED");
349 case MSGCODE_TRANSMIT_SUCCEEDED
:
350 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_TRANSMIT_SUCCEEDED");
354 case MSGCODE_RECEIVE_FAILED
:
355 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_RECEIVE_FAILED");
358 case MSGCODE_COMMAND_REJECTED
:
359 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_COMMAND_REJECTED");
362 case MSGCODE_TRANSMIT_FAILED_LINE
:
363 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_LINE");
366 case MSGCODE_TRANSMIT_FAILED_ACK
:
367 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_ACK");
370 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
:
371 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA");
374 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
:
375 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE");
379 m_frameBuffer
.Push(msg
);
380 bGotAck
= (msg
[0] & MSGCODE_FRAME_ACK
) != 0;
387 return bGotAck
&& !bError
;
390 bool CCECProcessor::ParseMessage(cec_frame
&msg
)
398 uint8_t iCode
= msg
[0] & ~(MSGCODE_FRAME_EOM
| MSGCODE_FRAME_ACK
);
399 bool bEom
= (msg
[0] & MSGCODE_FRAME_EOM
) != 0;
400 bool bAck
= (msg
[0] & MSGCODE_FRAME_ACK
) != 0;
404 case MSGCODE_NOTHING
:
405 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_NOTHING");
407 case MSGCODE_TIMEOUT_ERROR
:
408 case MSGCODE_HIGH_ERROR
:
409 case MSGCODE_LOW_ERROR
:
411 if (iCode
== MSGCODE_TIMEOUT_ERROR
)
412 logStr
= "MSGCODE_TIMEOUT";
413 else if (iCode
== MSGCODE_HIGH_ERROR
)
414 logStr
= "MSGCODE_HIGH_ERROR";
416 logStr
= "MSGCODE_LOW_ERROR";
418 int iLine
= (msg
.size() >= 3) ? (msg
[1] << 8) | (msg
[2]) : 0;
419 uint32_t iTime
= (msg
.size() >= 7) ? (msg
[3] << 24) | (msg
[4] << 16) | (msg
[5] << 8) | (msg
[6]) : 0;
420 logStr
.AppendFormat(" line:%i", iLine
);
421 logStr
.AppendFormat(" time:%u", iTime
);
422 m_controller
->AddLog(CEC_LOG_WARNING
, logStr
.c_str());
425 case MSGCODE_FRAME_START
:
427 logStr
= "MSGCODE_FRAME_START";
428 m_currentframe
.clear();
431 int iInitiator
= msg
[1] >> 4;
432 int iDestination
= msg
[1] & 0xF;
433 logStr
.AppendFormat(" initiator:%u destination:%u ack:%s %s", iInitiator
, iDestination
, bAck
? "high" : "low", bEom
? "eom" : "");
435 m_currentframe
.push_back(msg
[1]);
437 m_controller
->AddLog(CEC_LOG_DEBUG
, logStr
.c_str());
440 case MSGCODE_FRAME_DATA
:
442 logStr
= "MSGCODE_FRAME_DATA";
445 uint8_t iData
= msg
[1];
446 logStr
.AppendFormat(" %02x", iData
);
447 m_currentframe
.push_back(iData
);
449 m_controller
->AddLog(CEC_LOG_DEBUG
, logStr
.c_str());
461 void CCECProcessor::ParseCurrentFrame(void)
463 uint8_t initiator
= m_currentframe
[0] >> 4;
464 uint8_t destination
= m_currentframe
[0] & 0xF;
467 dataStr
.Format("received frame: initiator: %u destination: %u", initiator
, destination
);
469 if (m_currentframe
.size() > 1)
472 for (unsigned int i
= 1; i
< m_currentframe
.size(); i
++)
473 dataStr
.AppendFormat(" %02x", m_currentframe
[i
]);
475 m_controller
->AddLog(CEC_LOG_DEBUG
, dataStr
.c_str());
477 if (m_currentframe
.size() <= 1)
481 cec_opcode opCode
= (cec_opcode
) m_currentframe
[1];
482 if (destination
== (uint16_t) m_iLogicalAddress
)
486 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
487 ReportPhysicalAddress();
490 case CEC_OPCODE_GIVE_OSD_NAME
:
491 ReportOSDName((cec_logical_address
)initiator
);
493 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
494 ReportVendorID((cec_logical_address
)initiator
);
496 case CEC_OPCODE_MENU_REQUEST
:
497 ReportMenuState((cec_logical_address
)initiator
);
499 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
500 ReportPowerState((cec_logical_address
)initiator
);
502 case CEC_OPCODE_GET_CEC_VERSION
:
503 ReportCECVersion((cec_logical_address
)initiator
);
505 case CEC_OPCODE_USER_CONTROL_PRESSED
:
506 if (m_currentframe
.size() > 2)
508 m_controller
->AddKey();
510 if (m_currentframe
[2] <= CEC_USER_CONTROL_CODE_MAX
)
511 m_controller
->SetCurrentButton((cec_user_control_code
) m_currentframe
[2]);
514 case CEC_OPCODE_USER_CONTROL_RELEASE
:
515 m_controller
->AddKey();
518 cec_frame params
= m_currentframe
;
519 params
.erase(params
.begin(), params
.begin() + 2);
520 m_controller
->AddCommand((cec_logical_address
) initiator
, (cec_logical_address
) destination
, opCode
, ¶ms
);
524 else if (destination
== (uint8_t) CECDEVICE_BROADCAST
)
526 if (opCode
== CEC_OPCODE_REQUEST_ACTIVE_SOURCE
)
528 BroadcastActiveSource();
530 else if (opCode
== CEC_OPCODE_SET_STREAM_PATH
)
532 if (m_currentframe
.size() >= 4)
534 int streamaddr
= ((int)m_currentframe
[2] << 8) | ((int)m_currentframe
[3]);
536 strLog
.Format("%i requests stream path from physical address %04x", initiator
, streamaddr
);
537 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
538 if (streamaddr
== m_physicaladdress
)
539 BroadcastActiveSource();
544 cec_frame params
= m_currentframe
;
545 params
.erase(params
.begin(), params
.begin() + 2);
546 m_controller
->AddCommand((cec_logical_address
) initiator
, (cec_logical_address
) destination
, opCode
, ¶ms
);
552 strLog
.Format("ignoring frame: destination: %u != %u", destination
, (uint16_t)m_iLogicalAddress
);
553 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());