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"
36 #include "CECBusDevice.h"
38 #include "util/StdString.h"
39 #include "platform/timeutils.h"
44 CCECProcessor::CCECProcessor(CLibCEC
*controller
, CAdapterCommunication
*serComm
, const char *strDeviceName
, cec_logical_address iLogicalAddress
/* = CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress
/* = CEC_DEFAULT_PHYSICAL_ADDRESS*/) :
45 m_iPhysicalAddress(iPhysicalAddress
),
46 m_iLogicalAddress(iLogicalAddress
),
47 m_strDeviceName(strDeviceName
),
48 m_communication(serComm
),
49 m_controller(controller
),
52 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
53 m_busDevices
[iPtr
] = new CCECBusDevice(this, (cec_logical_address
) iPtr
, 0);
56 CCECProcessor::~CCECProcessor(void)
59 m_communication
= NULL
;
61 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
62 delete m_busDevices
[iPtr
];
65 bool CCECProcessor::Start(void)
67 if (!m_communication
|| !m_communication
->IsOpen())
69 m_controller
->AddLog(CEC_LOG_ERROR
, "connection is closed");
73 if (!SetLogicalAddress(m_iLogicalAddress
))
75 m_controller
->AddLog(CEC_LOG_ERROR
, "could not set the logical address");
82 m_controller
->AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
87 void *CCECProcessor::Process(void)
89 m_controller
->AddLog(CEC_LOG_DEBUG
, "processor thread started");
92 cec_adapter_message msg
;
96 bool bParseFrame(false);
98 bool bTransmitSucceeded(false);
103 CLockObject
lock(&m_mutex
);
104 if (m_communication
->IsOpen() && m_communication
->Read(msg
, 50))
105 ParseMessage(msg
, &bError
, &bTransmitSucceeded
, &bParseFrame
);
107 bParseFrame
&= !IsStopped();
109 command
= m_currentframe
;
113 ParseCommand(command
);
115 m_controller
->CheckKeypressTimeout();
117 for (unsigned int iDevicePtr
= 0; iDevicePtr
< 16; iDevicePtr
++)
118 m_busDevices
[iDevicePtr
]->PollVendorId();
127 bool CCECProcessor::PowerOnDevices(cec_logical_address address
/* = CECDEVICE_TV */)
133 strLog
.Format("<< powering on device with logical address %d", (int8_t)address
);
134 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
137 cec_command::format(command
, m_iLogicalAddress
, address
, CEC_OPCODE_IMAGE_VIEW_ON
);
139 return Transmit(command
);
142 bool CCECProcessor::StandbyDevices(cec_logical_address address
/* = CECDEVICE_BROADCAST */)
148 strLog
.Format("<< putting device with logical address %d in standby mode", (int8_t)address
);
149 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
152 cec_command::format(command
, m_iLogicalAddress
, address
, CEC_OPCODE_STANDBY
);
154 return Transmit(command
);
157 bool CCECProcessor::SetActiveView(void)
162 m_controller
->AddLog(CEC_LOG_DEBUG
, "<< setting active view");
165 cec_command::format(command
, m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
166 command
.parameters
.push_back((m_iPhysicalAddress
>> 8) & 0xFF);
167 command
.parameters
.push_back(m_iPhysicalAddress
& 0xFF);
169 return Transmit(command
);
172 bool CCECProcessor::SetInactiveView(void)
177 m_controller
->AddLog(CEC_LOG_DEBUG
, "<< setting inactive view");
180 cec_command::format(command
, m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_INACTIVE_SOURCE
);
181 command
.parameters
.push_back((m_iPhysicalAddress
>> 8) & 0xFF);
182 command
.parameters
.push_back(m_iPhysicalAddress
& 0xFF);
184 return Transmit(command
);
187 void CCECProcessor::LogOutput(const cec_command
&data
)
189 CStdString txStr
= "transmit ";
190 txStr
.AppendFormat(" %02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
);
191 txStr
.AppendFormat(":%02x", (uint8_t)data
.opcode
);
193 for (uint8_t iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
194 txStr
.AppendFormat(":%02x", data
.parameters
[iPtr
]);
195 m_controller
->AddLog(CEC_LOG_DEBUG
, txStr
.c_str());
198 bool CCECProcessor::Transmit(const cec_command
&data
, bool bWaitForAck
/* = true */)
202 cec_adapter_message output
;
204 CAdapterCommunication::FormatAdapterMessage(data
, output
);
206 return TransmitFormatted(output
, bWaitForAck
);
209 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress
)
212 strLog
.Format("<< setting logical address to %1x", iLogicalAddress
);
213 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
215 m_iLogicalAddress
= iLogicalAddress
;
216 return m_communication
&& m_communication
->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress
);
219 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress
)
222 strLog
.Format("<< setting physical address to %2x", iPhysicalAddress
);
223 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
225 m_iPhysicalAddress
= iPhysicalAddress
;
226 return SetActiveView();
229 bool CCECProcessor::SetOSDString(cec_logical_address iLogicalAddress
, cec_display_control duration
, const char *strMessage
)
232 strLog
.Format("<< display message '%s'", strMessage
);
233 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
236 cec_command::format(command
, m_iLogicalAddress
, iLogicalAddress
, CEC_OPCODE_SET_OSD_STRING
);
237 command
.parameters
.push_back((uint8_t)duration
);
239 for (unsigned int iPtr
= 0; iPtr
< strlen(strMessage
); iPtr
++)
240 command
.parameters
.push_back(strMessage
[iPtr
]);
242 return Transmit(command
);
245 bool CCECProcessor::SwitchMonitoring(bool bEnable
)
248 strLog
.Format("== %s monitoring mode ==", bEnable
? "enabling" : "disabling");
249 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
251 m_bMonitor
= bEnable
;
253 return m_communication
&& m_communication
->SetAckMask(0);
255 return m_communication
&& m_communication
->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress
);
258 bool CCECProcessor::TransmitFormatted(const cec_adapter_message
&data
, bool bWaitForAck
/* = true */)
260 CLockObject
lock(&m_mutex
);
261 if (!m_communication
|| !m_communication
->Write(data
))
266 uint64_t now
= GetTimeMs();
267 uint64_t target
= now
+ 1000;
271 while (!bGotAck
&& now
< target
)
273 bGotAck
= WaitForAck(&bError
, (uint32_t) (target
- now
));
276 if (bError
&& now
< target
)
278 m_controller
->AddLog(CEC_LOG_ERROR
, "retransmitting previous frame");
279 if (!m_communication
->Write(data
))
288 void CCECProcessor::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, ECecAbortReason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
290 m_controller
->AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
293 cec_command::format(command
, m_iLogicalAddress
, address
, CEC_OPCODE_FEATURE_ABORT
);
294 command
.parameters
.push_back((uint8_t)opcode
);
295 command
.parameters
.push_back((uint8_t)reason
);
300 void CCECProcessor::ReportCECVersion(cec_logical_address address
/* = CECDEVICE_TV */)
302 m_controller
->AddLog(CEC_LOG_NOTICE
, "<< reporting CEC version as 1.3a");
305 cec_command::format(command
, m_iLogicalAddress
, address
, CEC_OPCODE_CEC_VERSION
);
306 command
.parameters
.push_back(CEC_VERSION_1_3A
);
311 void CCECProcessor::ReportPowerState(cec_logical_address address
/*= CECDEVICE_TV */, bool bOn
/* = true */)
314 m_controller
->AddLog(CEC_LOG_NOTICE
, "<< reporting \"On\" power status");
316 m_controller
->AddLog(CEC_LOG_NOTICE
, "<< reporting \"Off\" power status");
319 cec_command::format(command
, m_iLogicalAddress
, address
, CEC_OPCODE_REPORT_POWER_STATUS
);
320 command
.parameters
.push_back(bOn
? (uint8_t) CEC_POWER_STATUS_ON
: (uint8_t) CEC_POWER_STATUS_STANDBY
);
325 void CCECProcessor::ReportMenuState(cec_logical_address address
/* = CECDEVICE_TV */, bool bActive
/* = true */)
328 m_controller
->AddLog(CEC_LOG_NOTICE
, "<< reporting menu state as active");
330 m_controller
->AddLog(CEC_LOG_NOTICE
, "<< reporting menu state as inactive");
333 cec_command::format(command
, m_iLogicalAddress
, address
, CEC_OPCODE_MENU_STATUS
);
334 command
.parameters
.push_back(bActive
? (uint8_t) CEC_MENU_STATE_ACTIVATED
: (uint8_t) CEC_MENU_STATE_DEACTIVATED
);
339 void CCECProcessor::ReportVendorID(cec_logical_address address
/* = CECDEVICE_TV */)
341 m_controller
->AddLog(CEC_LOG_NOTICE
, "<< vendor ID requested, feature abort");
342 TransmitAbort(address
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
345 void CCECProcessor::ReportOSDName(cec_logical_address address
/* = CECDEVICE_TV */)
347 const char *osdname
= m_strDeviceName
.c_str();
349 strLog
.Format("<< reporting OSD name as %s", osdname
);
350 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
353 cec_command::format(command
, m_iLogicalAddress
, address
, CEC_OPCODE_SET_OSD_NAME
);
354 for (unsigned int iPtr
= 0; iPtr
< strlen(osdname
); iPtr
++)
355 command
.parameters
.push_back(osdname
[iPtr
]);
360 void CCECProcessor::ReportPhysicalAddress(void)
363 strLog
.Format("<< reporting physical address as %04x", m_iPhysicalAddress
);
364 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
367 cec_command::format(command
, m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
368 command
.parameters
.push_back((uint8_t) ((m_iPhysicalAddress
>> 8) & 0xFF));
369 command
.parameters
.push_back((uint8_t) (m_iPhysicalAddress
& 0xFF));
370 command
.parameters
.push_back((uint8_t) (CEC_DEVICE_TYPE_PLAYBACK_DEVICE
));
375 void CCECProcessor::BroadcastActiveSource(void)
377 m_controller
->AddLog(CEC_LOG_NOTICE
, "<< broadcasting active source");
380 cec_command::format(command
, m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
381 command
.parameters
.push_back((uint8_t) ((m_iPhysicalAddress
>> 8) & 0xFF));
382 command
.parameters
.push_back((uint8_t) (m_iPhysicalAddress
& 0xFF));
387 bool CCECProcessor::WaitForAck(bool *bError
, uint32_t iTimeout
/* = 1000 */)
389 bool bTransmitSucceeded
= false, bEom
= false;
392 int64_t iNow
= GetTimeMs();
393 int64_t iTargetTime
= iNow
+ (uint64_t) iTimeout
;
395 while (!bTransmitSucceeded
&& !*bError
&& (iTimeout
== 0 || iNow
< iTargetTime
))
397 cec_adapter_message msg
;
400 if (!m_communication
->Read(msg
, iTimeout
> 0 ? (int32_t)(iTargetTime
- iNow
) : 1000))
406 ParseMessage(msg
, bError
, &bTransmitSucceeded
, &bEom
, false);
410 return bTransmitSucceeded
&& !*bError
;
413 void CCECProcessor::ParseMessage(cec_adapter_message
&msg
, bool *bError
, bool *bTransmitSucceeded
, bool *bEom
, bool bProcessMessages
/* = true */)
416 *bTransmitSucceeded
= false;
424 switch(msg
.message())
426 case MSGCODE_NOTHING
:
427 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_NOTHING");
429 case MSGCODE_TIMEOUT_ERROR
:
430 case MSGCODE_HIGH_ERROR
:
431 case MSGCODE_LOW_ERROR
:
433 if (msg
.message() == MSGCODE_TIMEOUT_ERROR
)
434 logStr
= "MSGCODE_TIMEOUT";
435 else if (msg
.message() == MSGCODE_HIGH_ERROR
)
436 logStr
= "MSGCODE_HIGH_ERROR";
438 logStr
= "MSGCODE_LOW_ERROR";
440 int iLine
= (msg
.size() >= 3) ? (msg
[1] << 8) | (msg
[2]) : 0;
441 uint32_t iTime
= (msg
.size() >= 7) ? (msg
[3] << 24) | (msg
[4] << 16) | (msg
[5] << 8) | (msg
[6]) : 0;
442 logStr
.AppendFormat(" line:%i", iLine
);
443 logStr
.AppendFormat(" time:%u", iTime
);
444 m_controller
->AddLog(CEC_LOG_WARNING
, logStr
.c_str());
448 case MSGCODE_FRAME_START
:
450 if (bProcessMessages
)
452 logStr
= "MSGCODE_FRAME_START";
453 m_currentframe
.clear();
456 logStr
.AppendFormat(" initiator:%u destination:%u ack:%s %s", msg
.initiator(), msg
.destination(), msg
.ack() ? "high" : "low", msg
.eom() ? "eom" : "");
457 m_currentframe
.initiator
= msg
.initiator();
458 m_currentframe
.destination
= msg
.destination();
459 m_currentframe
.ack
= msg
.ack();
460 m_currentframe
.eom
= msg
.eom();
462 m_controller
->AddLog(CEC_LOG_DEBUG
, logStr
.c_str());
466 m_frameBuffer
.Push(msg
);
470 case MSGCODE_FRAME_DATA
:
472 if (bProcessMessages
)
474 logStr
= "MSGCODE_FRAME_DATA";
477 uint8_t iData
= msg
[1];
478 logStr
.AppendFormat(" %02x", iData
);
479 m_currentframe
.push_back(iData
);
480 m_currentframe
.eom
= msg
.eom();
482 m_controller
->AddLog(CEC_LOG_DEBUG
, logStr
.c_str());
486 m_frameBuffer
.Push(msg
);
492 case MSGCODE_COMMAND_ACCEPTED
:
493 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_COMMAND_ACCEPTED");
495 case MSGCODE_TRANSMIT_SUCCEEDED
:
496 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_TRANSMIT_SUCCEEDED");
497 *bTransmitSucceeded
= true;
499 case MSGCODE_RECEIVE_FAILED
:
500 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_RECEIVE_FAILED");
503 case MSGCODE_COMMAND_REJECTED
:
504 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_COMMAND_REJECTED");
507 case MSGCODE_TRANSMIT_FAILED_LINE
:
508 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_LINE");
511 case MSGCODE_TRANSMIT_FAILED_ACK
:
512 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_ACK");
515 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
:
516 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA");
519 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
:
520 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE");
528 void CCECProcessor::ParseVendorId(cec_logical_address device
, const cec_datapacket
&data
)
532 m_controller
->AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
536 uint64_t iVendorId
= ((uint64_t)data
[0] << 3) +
537 ((uint64_t)data
[1] << 2) +
540 m_busDevices
[(uint8_t)device
]->SetVendorId(iVendorId
, data
.size
>= 4 ? data
[3] : 0);
543 void CCECProcessor::ParseCommand(cec_command
&command
)
546 dataStr
.Format(">> received frame: %1x%1x:%02x", command
.initiator
, command
.destination
, command
.opcode
);
547 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
548 dataStr
.AppendFormat(":%02x", (unsigned int)command
.parameters
[iPtr
]);
549 m_controller
->AddLog(CEC_LOG_DEBUG
, dataStr
.c_str());
552 m_busDevices
[(uint8_t)command
.initiator
]->HandleCommand(command
);
555 void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode
)
557 m_controller
->SetCurrentButton(iButtonCode
);
560 void CCECProcessor::AddCommand(const cec_command
&command
)
562 m_controller
->AddCommand(command
);
565 void CCECProcessor::AddKey(void)
567 m_controller
->AddKey();
570 void CCECProcessor::AddLog(cec_log_level level
, const CStdString
&strMessage
)
572 m_controller
->AddLog(level
, strMessage
);