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();
124 bool CCECProcessor::PowerOnDevices(cec_logical_address address
/* = CECDEVICE_TV */)
130 strLog
.Format("<< powering on device with logical address %d", (int8_t)address
);
131 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
134 cec_command::format(command
, m_iLogicalAddress
, address
, CEC_OPCODE_IMAGE_VIEW_ON
);
136 return Transmit(command
);
139 bool CCECProcessor::StandbyDevices(cec_logical_address address
/* = CECDEVICE_BROADCAST */)
145 strLog
.Format("<< putting device with logical address %d in standby mode", (int8_t)address
);
146 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
149 cec_command::format(command
, m_iLogicalAddress
, address
, CEC_OPCODE_STANDBY
);
151 return Transmit(command
);
154 bool CCECProcessor::SetActiveView(void)
159 m_controller
->AddLog(CEC_LOG_DEBUG
, "<< setting active view");
162 cec_command::format(command
, m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
163 command
.parameters
.push_back((m_iPhysicalAddress
>> 8) & 0xFF);
164 command
.parameters
.push_back(m_iPhysicalAddress
& 0xFF);
166 return Transmit(command
);
169 bool CCECProcessor::SetInactiveView(void)
174 m_controller
->AddLog(CEC_LOG_DEBUG
, "<< setting inactive view");
177 cec_command::format(command
, m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_INACTIVE_SOURCE
);
178 command
.parameters
.push_back((m_iPhysicalAddress
>> 8) & 0xFF);
179 command
.parameters
.push_back(m_iPhysicalAddress
& 0xFF);
181 return Transmit(command
);
184 void CCECProcessor::LogOutput(const cec_command
&data
)
186 CStdString txStr
= "transmit ";
187 txStr
.AppendFormat(" %02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
);
188 txStr
.AppendFormat(":%02x", (uint8_t)data
.opcode
);
190 for (uint8_t iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
191 txStr
.AppendFormat(":%02x", data
.parameters
[iPtr
]);
192 m_controller
->AddLog(CEC_LOG_DEBUG
, txStr
.c_str());
195 bool CCECProcessor::Transmit(const cec_command
&data
, bool bWaitForAck
/* = true */)
199 cec_adapter_message output
;
201 CAdapterCommunication::FormatAdapterMessage(data
, output
);
203 return TransmitFormatted(output
, bWaitForAck
);
206 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress
)
209 strLog
.Format("<< setting logical address to %1x", iLogicalAddress
);
210 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
212 m_iLogicalAddress
= iLogicalAddress
;
213 return m_communication
&& m_communication
->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress
);
216 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress
)
219 strLog
.Format("<< setting physical address to %2x", iPhysicalAddress
);
220 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
222 m_iPhysicalAddress
= iPhysicalAddress
;
223 return SetActiveView();
226 bool CCECProcessor::SetOSDString(cec_logical_address iLogicalAddress
, cec_display_control duration
, const char *strMessage
)
229 strLog
.Format("<< display message '%s'", strMessage
);
230 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
233 cec_command::format(command
, m_iLogicalAddress
, iLogicalAddress
, CEC_OPCODE_SET_OSD_STRING
);
234 command
.parameters
.push_back((uint8_t)duration
);
236 for (unsigned int iPtr
= 0; iPtr
< strlen(strMessage
); iPtr
++)
237 command
.parameters
.push_back(strMessage
[iPtr
]);
239 return Transmit(command
);
242 bool CCECProcessor::SwitchMonitoring(bool bEnable
)
245 strLog
.Format("== %s monitoring mode ==", bEnable
? "enabling" : "disabling");
246 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
248 m_bMonitor
= bEnable
;
250 return m_communication
&& m_communication
->SetAckMask(0);
252 return m_communication
&& m_communication
->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress
);
255 bool CCECProcessor::TransmitFormatted(const cec_adapter_message
&data
, bool bWaitForAck
/* = true */)
257 CLockObject
lock(&m_mutex
);
258 if (!m_communication
|| !m_communication
->Write(data
))
263 uint64_t now
= GetTimeMs();
264 uint64_t target
= now
+ 1000;
268 while (!bGotAck
&& now
< target
)
270 bGotAck
= WaitForAck(&bError
, (uint32_t) (target
- now
));
273 if (bError
&& now
< target
)
275 m_controller
->AddLog(CEC_LOG_ERROR
, "retransmitting previous frame");
276 if (!m_communication
->Write(data
))
285 void CCECProcessor::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, ECecAbortReason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
287 m_controller
->AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
290 cec_command::format(command
, m_iLogicalAddress
, address
, CEC_OPCODE_FEATURE_ABORT
);
291 command
.parameters
.push_back((uint8_t)opcode
);
292 command
.parameters
.push_back((uint8_t)reason
);
297 void CCECProcessor::ReportCECVersion(cec_logical_address address
/* = CECDEVICE_TV */)
299 m_controller
->AddLog(CEC_LOG_NOTICE
, "<< reporting CEC version as 1.3a");
302 cec_command::format(command
, m_iLogicalAddress
, address
, CEC_OPCODE_CEC_VERSION
);
303 command
.parameters
.push_back(CEC_VERSION_1_3A
);
308 void CCECProcessor::ReportPowerState(cec_logical_address address
/*= CECDEVICE_TV */, bool bOn
/* = true */)
311 m_controller
->AddLog(CEC_LOG_NOTICE
, "<< reporting \"On\" power status");
313 m_controller
->AddLog(CEC_LOG_NOTICE
, "<< reporting \"Off\" power status");
316 cec_command::format(command
, m_iLogicalAddress
, address
, CEC_OPCODE_REPORT_POWER_STATUS
);
317 command
.parameters
.push_back(bOn
? (uint8_t) CEC_POWER_STATUS_ON
: (uint8_t) CEC_POWER_STATUS_STANDBY
);
322 void CCECProcessor::ReportMenuState(cec_logical_address address
/* = CECDEVICE_TV */, bool bActive
/* = true */)
325 m_controller
->AddLog(CEC_LOG_NOTICE
, "<< reporting menu state as active");
327 m_controller
->AddLog(CEC_LOG_NOTICE
, "<< reporting menu state as inactive");
330 cec_command::format(command
, m_iLogicalAddress
, address
, CEC_OPCODE_MENU_STATUS
);
331 command
.parameters
.push_back(bActive
? (uint8_t) CEC_MENU_STATE_ACTIVATED
: (uint8_t) CEC_MENU_STATE_DEACTIVATED
);
336 void CCECProcessor::ReportVendorID(cec_logical_address address
/* = CECDEVICE_TV */)
338 m_controller
->AddLog(CEC_LOG_NOTICE
, "<< vendor ID requested, feature abort");
339 TransmitAbort(address
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
342 void CCECProcessor::ReportOSDName(cec_logical_address address
/* = CECDEVICE_TV */)
344 const char *osdname
= m_strDeviceName
.c_str();
346 strLog
.Format("<< reporting OSD name as %s", osdname
);
347 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
350 cec_command::format(command
, m_iLogicalAddress
, address
, CEC_OPCODE_SET_OSD_NAME
);
351 for (unsigned int iPtr
= 0; iPtr
< strlen(osdname
); iPtr
++)
352 command
.parameters
.push_back(osdname
[iPtr
]);
357 void CCECProcessor::ReportPhysicalAddress(void)
360 strLog
.Format("<< reporting physical address as %04x", m_iPhysicalAddress
);
361 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
364 cec_command::format(command
, m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
365 command
.parameters
.push_back((uint8_t) ((m_iPhysicalAddress
>> 8) & 0xFF));
366 command
.parameters
.push_back((uint8_t) (m_iPhysicalAddress
& 0xFF));
367 command
.parameters
.push_back((uint8_t) (CEC_DEVICE_TYPE_PLAYBACK_DEVICE
));
372 void CCECProcessor::BroadcastActiveSource(void)
374 m_controller
->AddLog(CEC_LOG_NOTICE
, "<< broadcasting active source");
377 cec_command::format(command
, m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
378 command
.parameters
.push_back((uint8_t) ((m_iPhysicalAddress
>> 8) & 0xFF));
379 command
.parameters
.push_back((uint8_t) (m_iPhysicalAddress
& 0xFF));
384 bool CCECProcessor::WaitForAck(bool *bError
, uint32_t iTimeout
/* = 1000 */)
386 bool bTransmitSucceeded
= false, bEom
= false;
389 int64_t iNow
= GetTimeMs();
390 int64_t iTargetTime
= iNow
+ (uint64_t) iTimeout
;
392 while (!bTransmitSucceeded
&& !*bError
&& (iTimeout
== 0 || iNow
< iTargetTime
))
394 cec_adapter_message msg
;
397 if (!m_communication
->Read(msg
, iTimeout
> 0 ? (int32_t)(iTargetTime
- iNow
) : 1000))
403 ParseMessage(msg
, bError
, &bTransmitSucceeded
, &bEom
, false);
407 return bTransmitSucceeded
&& !*bError
;
410 void CCECProcessor::ParseMessage(cec_adapter_message
&msg
, bool *bError
, bool *bTransmitSucceeded
, bool *bEom
, bool bProcessMessages
/* = true */)
413 *bTransmitSucceeded
= false;
421 switch(msg
.message())
423 case MSGCODE_NOTHING
:
424 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_NOTHING");
426 case MSGCODE_TIMEOUT_ERROR
:
427 case MSGCODE_HIGH_ERROR
:
428 case MSGCODE_LOW_ERROR
:
430 if (msg
.message() == MSGCODE_TIMEOUT_ERROR
)
431 logStr
= "MSGCODE_TIMEOUT";
432 else if (msg
.message() == MSGCODE_HIGH_ERROR
)
433 logStr
= "MSGCODE_HIGH_ERROR";
435 logStr
= "MSGCODE_LOW_ERROR";
437 int iLine
= (msg
.size() >= 3) ? (msg
[1] << 8) | (msg
[2]) : 0;
438 uint32_t iTime
= (msg
.size() >= 7) ? (msg
[3] << 24) | (msg
[4] << 16) | (msg
[5] << 8) | (msg
[6]) : 0;
439 logStr
.AppendFormat(" line:%i", iLine
);
440 logStr
.AppendFormat(" time:%u", iTime
);
441 m_controller
->AddLog(CEC_LOG_WARNING
, logStr
.c_str());
445 case MSGCODE_FRAME_START
:
447 if (bProcessMessages
)
449 logStr
= "MSGCODE_FRAME_START";
450 m_currentframe
.clear();
453 logStr
.AppendFormat(" initiator:%u destination:%u ack:%s %s", msg
.initiator(), msg
.destination(), msg
.ack() ? "high" : "low", msg
.eom() ? "eom" : "");
454 m_currentframe
.initiator
= msg
.initiator();
455 m_currentframe
.destination
= msg
.destination();
456 m_currentframe
.ack
= msg
.ack();
457 m_currentframe
.eom
= msg
.eom();
459 m_controller
->AddLog(CEC_LOG_DEBUG
, logStr
.c_str());
463 m_frameBuffer
.Push(msg
);
467 case MSGCODE_FRAME_DATA
:
469 if (bProcessMessages
)
471 logStr
= "MSGCODE_FRAME_DATA";
474 uint8_t iData
= msg
[1];
475 logStr
.AppendFormat(" %02x", iData
);
476 m_currentframe
.push_back(iData
);
477 m_currentframe
.eom
= msg
.eom();
479 m_controller
->AddLog(CEC_LOG_DEBUG
, logStr
.c_str());
483 m_frameBuffer
.Push(msg
);
489 case MSGCODE_COMMAND_ACCEPTED
:
490 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_COMMAND_ACCEPTED");
492 case MSGCODE_TRANSMIT_SUCCEEDED
:
493 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_TRANSMIT_SUCCEEDED");
494 *bTransmitSucceeded
= true;
496 case MSGCODE_RECEIVE_FAILED
:
497 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_RECEIVE_FAILED");
500 case MSGCODE_COMMAND_REJECTED
:
501 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_COMMAND_REJECTED");
504 case MSGCODE_TRANSMIT_FAILED_LINE
:
505 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_LINE");
508 case MSGCODE_TRANSMIT_FAILED_ACK
:
509 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_ACK");
512 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
:
513 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA");
516 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
:
517 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE");
525 void CCECProcessor::ParseVendorId(cec_logical_address device
, const cec_datapacket
&data
)
529 m_controller
->AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
533 uint64_t iVendorId
= ((uint64_t)data
[0] << 3) +
534 ((uint64_t)data
[1] << 2) +
537 m_busDevices
[(uint8_t)device
]->SetVendorId(iVendorId
, data
.size
>= 4 ? data
[3] : 0);
540 void CCECProcessor::ParseCommand(cec_command
&command
)
543 dataStr
.Format(">> received frame: %1x%1x:%02x", command
.initiator
, command
.destination
, command
.opcode
);
544 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
545 dataStr
.AppendFormat(":%02x", (unsigned int)command
.parameters
[iPtr
]);
546 m_controller
->AddLog(CEC_LOG_DEBUG
, dataStr
.c_str());
549 m_busDevices
[(uint8_t)command
.initiator
]->HandleCommand(command
);
552 void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode
)
554 m_controller
->SetCurrentButton(iButtonCode
);
557 void CCECProcessor::AddCommand(const cec_command
&command
)
559 m_controller
->AddCommand(command
);
562 void CCECProcessor::AddKey(void)
564 m_controller
->AddKey();
567 void CCECProcessor::AddLog(cec_log_level level
, const CStdString
&strMessage
)
569 m_controller
->AddLog(level
, strMessage
);