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
)
190 strTx
.Format("<< %02x:%02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
, (uint8_t)data
.opcode
);
192 for (uint8_t iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
193 strTx
.AppendFormat(":%02x", data
.parameters
[iPtr
]);
194 m_controller
->AddLog(CEC_LOG_TRAFFIC
, strTx
.c_str());
197 bool CCECProcessor::Transmit(const cec_command
&data
, bool bWaitForAck
/* = true */)
201 cec_adapter_message output
;
203 CAdapterCommunication::FormatAdapterMessage(data
, output
);
205 return TransmitFormatted(output
, bWaitForAck
);
208 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress
)
211 strLog
.Format("<< setting logical address to %1x", iLogicalAddress
);
212 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
214 m_iLogicalAddress
= iLogicalAddress
;
215 return m_communication
&& m_communication
->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress
);
218 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress
)
221 strLog
.Format("<< setting physical address to %2x", iPhysicalAddress
);
222 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
224 m_iPhysicalAddress
= iPhysicalAddress
;
225 return SetActiveView();
228 bool CCECProcessor::SetOSDString(cec_logical_address iLogicalAddress
, cec_display_control duration
, const char *strMessage
)
231 strLog
.Format("<< display message '%s'", strMessage
);
232 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
235 cec_command::format(command
, m_iLogicalAddress
, iLogicalAddress
, CEC_OPCODE_SET_OSD_STRING
);
236 command
.parameters
.push_back((uint8_t)duration
);
238 for (unsigned int iPtr
= 0; iPtr
< strlen(strMessage
); iPtr
++)
239 command
.parameters
.push_back(strMessage
[iPtr
]);
241 return Transmit(command
);
244 bool CCECProcessor::SwitchMonitoring(bool bEnable
)
247 strLog
.Format("== %s monitoring mode ==", bEnable
? "enabling" : "disabling");
248 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
250 m_bMonitor
= bEnable
;
252 return m_communication
&& m_communication
->SetAckMask(0);
254 return m_communication
&& m_communication
->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress
);
257 bool CCECProcessor::TransmitFormatted(const cec_adapter_message
&data
, bool bWaitForAck
/* = true */)
259 CLockObject
lock(&m_mutex
);
260 if (!m_communication
|| !m_communication
->Write(data
))
265 uint64_t now
= GetTimeMs();
266 uint64_t target
= now
+ 1000;
270 while (!bGotAck
&& now
< target
)
272 bGotAck
= WaitForAck(&bError
, (uint32_t) (target
- now
));
275 if (bError
&& now
< target
)
277 m_controller
->AddLog(CEC_LOG_ERROR
, "retransmitting previous frame");
278 if (!m_communication
->Write(data
))
287 void CCECProcessor::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, ECecAbortReason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
289 m_controller
->AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
292 cec_command::format(command
, m_iLogicalAddress
, address
, CEC_OPCODE_FEATURE_ABORT
);
293 command
.parameters
.push_back((uint8_t)opcode
);
294 command
.parameters
.push_back((uint8_t)reason
);
299 void CCECProcessor::ReportCECVersion(cec_logical_address address
/* = CECDEVICE_TV */)
301 m_controller
->AddLog(CEC_LOG_NOTICE
, "<< reporting CEC version as 1.3a");
304 cec_command::format(command
, m_iLogicalAddress
, address
, CEC_OPCODE_CEC_VERSION
);
305 command
.parameters
.push_back(CEC_VERSION_1_3A
);
310 void CCECProcessor::ReportPowerState(cec_logical_address address
/*= CECDEVICE_TV */, bool bOn
/* = true */)
313 m_controller
->AddLog(CEC_LOG_NOTICE
, "<< reporting \"On\" power status");
315 m_controller
->AddLog(CEC_LOG_NOTICE
, "<< reporting \"Off\" power status");
318 cec_command::format(command
, m_iLogicalAddress
, address
, CEC_OPCODE_REPORT_POWER_STATUS
);
319 command
.parameters
.push_back(bOn
? (uint8_t) CEC_POWER_STATUS_ON
: (uint8_t) CEC_POWER_STATUS_STANDBY
);
324 void CCECProcessor::ReportMenuState(cec_logical_address address
/* = CECDEVICE_TV */, bool bActive
/* = true */)
327 m_controller
->AddLog(CEC_LOG_NOTICE
, "<< reporting menu state as active");
329 m_controller
->AddLog(CEC_LOG_NOTICE
, "<< reporting menu state as inactive");
332 cec_command::format(command
, m_iLogicalAddress
, address
, CEC_OPCODE_MENU_STATUS
);
333 command
.parameters
.push_back(bActive
? (uint8_t) CEC_MENU_STATE_ACTIVATED
: (uint8_t) CEC_MENU_STATE_DEACTIVATED
);
338 void CCECProcessor::ReportVendorID(cec_logical_address address
/* = CECDEVICE_TV */)
340 m_controller
->AddLog(CEC_LOG_NOTICE
, "<< vendor ID requested, feature abort");
341 TransmitAbort(address
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
344 void CCECProcessor::ReportOSDName(cec_logical_address address
/* = CECDEVICE_TV */)
346 const char *osdname
= m_strDeviceName
.c_str();
348 strLog
.Format("<< reporting OSD name as %s", osdname
);
349 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
352 cec_command::format(command
, m_iLogicalAddress
, address
, CEC_OPCODE_SET_OSD_NAME
);
353 for (unsigned int iPtr
= 0; iPtr
< strlen(osdname
); iPtr
++)
354 command
.parameters
.push_back(osdname
[iPtr
]);
359 void CCECProcessor::ReportPhysicalAddress(void)
362 strLog
.Format("<< reporting physical address as %04x", m_iPhysicalAddress
);
363 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
366 cec_command::format(command
, m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
367 command
.parameters
.push_back((uint8_t) ((m_iPhysicalAddress
>> 8) & 0xFF));
368 command
.parameters
.push_back((uint8_t) (m_iPhysicalAddress
& 0xFF));
369 command
.parameters
.push_back((uint8_t) (CEC_DEVICE_TYPE_PLAYBACK_DEVICE
));
374 void CCECProcessor::BroadcastActiveSource(void)
376 m_controller
->AddLog(CEC_LOG_NOTICE
, "<< broadcasting active source");
379 cec_command::format(command
, m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
380 command
.parameters
.push_back((uint8_t) ((m_iPhysicalAddress
>> 8) & 0xFF));
381 command
.parameters
.push_back((uint8_t) (m_iPhysicalAddress
& 0xFF));
386 bool CCECProcessor::WaitForAck(bool *bError
, uint32_t iTimeout
/* = 1000 */)
388 bool bTransmitSucceeded
= false, bEom
= false;
391 int64_t iNow
= GetTimeMs();
392 int64_t iTargetTime
= iNow
+ (uint64_t) iTimeout
;
394 while (!bTransmitSucceeded
&& !*bError
&& (iTimeout
== 0 || iNow
< iTargetTime
))
396 cec_adapter_message msg
;
399 if (!m_communication
->Read(msg
, iTimeout
> 0 ? (int32_t)(iTargetTime
- iNow
) : 1000))
405 ParseMessage(msg
, bError
, &bTransmitSucceeded
, &bEom
, false);
409 return bTransmitSucceeded
&& !*bError
;
412 void CCECProcessor::ParseMessage(cec_adapter_message
&msg
, bool *bError
, bool *bTransmitSucceeded
, bool *bEom
, bool bProcessMessages
/* = true */)
415 *bTransmitSucceeded
= false;
423 switch(msg
.message())
425 case MSGCODE_NOTHING
:
426 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_NOTHING");
428 case MSGCODE_TIMEOUT_ERROR
:
429 case MSGCODE_HIGH_ERROR
:
430 case MSGCODE_LOW_ERROR
:
432 if (msg
.message() == MSGCODE_TIMEOUT_ERROR
)
433 logStr
= "MSGCODE_TIMEOUT";
434 else if (msg
.message() == MSGCODE_HIGH_ERROR
)
435 logStr
= "MSGCODE_HIGH_ERROR";
437 logStr
= "MSGCODE_LOW_ERROR";
439 int iLine
= (msg
.size() >= 3) ? (msg
[1] << 8) | (msg
[2]) : 0;
440 uint32_t iTime
= (msg
.size() >= 7) ? (msg
[3] << 24) | (msg
[4] << 16) | (msg
[5] << 8) | (msg
[6]) : 0;
441 logStr
.AppendFormat(" line:%i", iLine
);
442 logStr
.AppendFormat(" time:%u", iTime
);
443 m_controller
->AddLog(CEC_LOG_WARNING
, logStr
.c_str());
447 case MSGCODE_FRAME_START
:
449 if (bProcessMessages
)
451 logStr
= "MSGCODE_FRAME_START";
452 m_currentframe
.clear();
455 logStr
.AppendFormat(" initiator:%u destination:%u ack:%s %s", msg
.initiator(), msg
.destination(), msg
.ack() ? "high" : "low", msg
.eom() ? "eom" : "");
456 m_currentframe
.initiator
= msg
.initiator();
457 m_currentframe
.destination
= msg
.destination();
458 m_currentframe
.ack
= msg
.ack();
459 m_currentframe
.eom
= msg
.eom();
461 m_controller
->AddLog(CEC_LOG_DEBUG
, logStr
.c_str());
465 m_frameBuffer
.Push(msg
);
469 case MSGCODE_FRAME_DATA
:
471 if (bProcessMessages
)
473 logStr
= "MSGCODE_FRAME_DATA";
476 uint8_t iData
= msg
[1];
477 logStr
.AppendFormat(" %02x", iData
);
478 m_currentframe
.push_back(iData
);
479 m_currentframe
.eom
= msg
.eom();
481 m_controller
->AddLog(CEC_LOG_DEBUG
, logStr
.c_str());
485 m_frameBuffer
.Push(msg
);
491 case MSGCODE_COMMAND_ACCEPTED
:
492 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_COMMAND_ACCEPTED");
494 case MSGCODE_TRANSMIT_SUCCEEDED
:
495 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_TRANSMIT_SUCCEEDED");
496 *bTransmitSucceeded
= true;
498 case MSGCODE_RECEIVE_FAILED
:
499 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_RECEIVE_FAILED");
502 case MSGCODE_COMMAND_REJECTED
:
503 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_COMMAND_REJECTED");
506 case MSGCODE_TRANSMIT_FAILED_LINE
:
507 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_LINE");
510 case MSGCODE_TRANSMIT_FAILED_ACK
:
511 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_ACK");
514 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
:
515 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA");
518 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
:
519 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE");
527 void CCECProcessor::ParseVendorId(cec_logical_address device
, const cec_datapacket
&data
)
531 m_controller
->AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
535 uint64_t iVendorId
= ((uint64_t)data
[0] << 3) +
536 ((uint64_t)data
[1] << 2) +
539 m_busDevices
[(uint8_t)device
]->SetVendorId(iVendorId
, data
.size
>= 4 ? data
[3] : 0);
542 void CCECProcessor::ParseCommand(cec_command
&command
)
545 dataStr
.Format(">> %1x%1x:%02x", command
.initiator
, command
.destination
, command
.opcode
);
546 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
547 dataStr
.AppendFormat(":%02x", (unsigned int)command
.parameters
[iPtr
]);
548 m_controller
->AddLog(CEC_LOG_TRAFFIC
, dataStr
.c_str());
551 m_busDevices
[(uint8_t)command
.initiator
]->HandleCommand(command
);
554 void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode
)
556 m_controller
->SetCurrentButton(iButtonCode
);
559 void CCECProcessor::AddCommand(const cec_command
&command
)
561 m_controller
->AddCommand(command
);
564 void CCECProcessor::AddKey(void)
566 m_controller
->AddKey();
569 void CCECProcessor::AddLog(cec_log_level level
, const CStdString
&strMessage
)
571 m_controller
->AddLog(level
, strMessage
);