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_iPhysicalAddress(iPhysicalAddress
),
45 m_iLogicalAddress(iLogicalAddress
),
46 m_strDeviceName(strDeviceName
),
47 m_communication(serComm
),
48 m_controller(controller
)
50 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
51 m_vendorIds
[iPtr
] = CEC_VENDOR_UNKNOWN
;
52 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
53 m_vendorClasses
[iPtr
] = (uint8_t) 0;
56 CCECProcessor::~CCECProcessor(void)
59 m_communication
= NULL
;
63 bool CCECProcessor::Start(void)
65 if (!m_communication
|| !m_communication
->IsOpen())
67 m_controller
->AddLog(CEC_LOG_ERROR
, "connection is closed");
71 if (!SetLogicalAddress(m_iLogicalAddress
))
73 m_controller
->AddLog(CEC_LOG_ERROR
, "could not set the logical address");
80 m_controller
->AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
85 void *CCECProcessor::Process(void)
87 m_controller
->AddLog(CEC_LOG_DEBUG
, "processor thread started");
90 cec_adapter_message msg
;
94 bool bParseFrame(false);
96 bool bTransmitSucceeded(false);
101 CLockObject
lock(&m_mutex
);
102 if (m_communication
->IsOpen() && m_communication
->Read(msg
, 50))
103 ParseMessage(msg
, &bError
, &bTransmitSucceeded
, &bParseFrame
);
105 bParseFrame
&= !IsStopped();
107 command
= m_currentframe
;
111 ParseCommand(command
);
113 m_controller
->CheckKeypressTimeout();
122 bool CCECProcessor::PowerOnDevices(cec_logical_address address
/* = CECDEVICE_TV */)
128 strLog
.Format("<< powering on device with logical address %d", (int8_t)address
);
129 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
132 cec_command::format(command
, m_iLogicalAddress
, address
, CEC_OPCODE_IMAGE_VIEW_ON
);
134 return Transmit(command
);
137 bool CCECProcessor::StandbyDevices(cec_logical_address address
/* = CECDEVICE_BROADCAST */)
143 strLog
.Format("<< putting device with logical address %d in standby mode", (int8_t)address
);
144 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
147 cec_command::format(command
, m_iLogicalAddress
, address
, CEC_OPCODE_STANDBY
);
149 return Transmit(command
);
152 bool CCECProcessor::SetActiveView(void)
157 m_controller
->AddLog(CEC_LOG_DEBUG
, "<< setting active view");
160 cec_command::format(command
, m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
161 command
.parameters
.push_back((m_iPhysicalAddress
>> 8) & 0xFF);
162 command
.parameters
.push_back(m_iPhysicalAddress
& 0xFF);
164 return Transmit(command
);
167 bool CCECProcessor::SetInactiveView(void)
172 m_controller
->AddLog(CEC_LOG_DEBUG
, "<< setting inactive view");
175 cec_command::format(command
, m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_INACTIVE_SOURCE
);
176 command
.parameters
.push_back((m_iPhysicalAddress
>> 8) & 0xFF);
177 command
.parameters
.push_back(m_iPhysicalAddress
& 0xFF);
179 return Transmit(command
);
182 void CCECProcessor::LogOutput(const cec_command
&data
)
184 CStdString txStr
= "transmit ";
185 txStr
.AppendFormat(" %02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
);
186 txStr
.AppendFormat(" %02x", (uint8_t)data
.opcode
);
188 for (uint8_t iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
189 txStr
.AppendFormat(" %02x", data
.parameters
[iPtr
]);
190 m_controller
->AddLog(CEC_LOG_DEBUG
, txStr
.c_str());
193 bool CCECProcessor::Transmit(const cec_command
&data
, bool bWaitForAck
/* = true */)
197 cec_adapter_message output
;
199 CAdapterCommunication::FormatAdapterMessage(data
, output
);
201 return TransmitFormatted(output
, bWaitForAck
);
204 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress
)
207 strLog
.Format("<< setting logical address to %1x", iLogicalAddress
);
208 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
210 m_iLogicalAddress
= iLogicalAddress
;
211 return m_communication
&& m_communication
->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress
);
214 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress
)
217 strLog
.Format("<< setting physical address to %2x", iPhysicalAddress
);
218 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
220 m_iPhysicalAddress
= iPhysicalAddress
;
221 return SetActiveView();
224 bool CCECProcessor::SetOSDString(cec_logical_address iLogicalAddress
, cec_display_control duration
, const char *strMessage
)
227 strLog
.Format("<< display message '%s'", strMessage
);
228 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
231 cec_command::format(command
, m_iLogicalAddress
, iLogicalAddress
, CEC_OPCODE_SET_OSD_STRING
);
232 command
.parameters
.push_back((uint8_t)duration
);
234 for (unsigned int iPtr
= 0; iPtr
< strlen(strMessage
); iPtr
++)
235 command
.parameters
.push_back(strMessage
[iPtr
]);
237 return Transmit(command
);
240 bool CCECProcessor::TransmitFormatted(const cec_adapter_message
&data
, bool bWaitForAck
/* = true */)
242 CLockObject
lock(&m_mutex
);
243 if (!m_communication
|| !m_communication
->Write(data
))
248 uint64_t now
= GetTimeMs();
249 uint64_t target
= now
+ 1000;
253 while (!bGotAck
&& now
< target
)
255 bGotAck
= WaitForAck(&bError
, (uint32_t) (target
- now
));
258 if (bError
&& now
< target
)
260 m_controller
->AddLog(CEC_LOG_ERROR
, "retransmitting previous frame");
261 if (!m_communication
->Write(data
))
270 void CCECProcessor::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, ECecAbortReason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
272 m_controller
->AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
275 cec_command::format(command
, m_iLogicalAddress
, address
, CEC_OPCODE_FEATURE_ABORT
);
276 command
.parameters
.push_back((uint8_t)opcode
);
277 command
.parameters
.push_back((uint8_t)reason
);
282 void CCECProcessor::ReportCECVersion(cec_logical_address address
/* = CECDEVICE_TV */)
284 m_controller
->AddLog(CEC_LOG_NOTICE
, "<< reporting CEC version as 1.3a");
287 cec_command::format(command
, m_iLogicalAddress
, address
, CEC_OPCODE_CEC_VERSION
);
288 command
.parameters
.push_back(CEC_VERSION_1_3A
);
293 void CCECProcessor::ReportPowerState(cec_logical_address address
/*= CECDEVICE_TV */, bool bOn
/* = true */)
296 m_controller
->AddLog(CEC_LOG_NOTICE
, "<< reporting \"On\" power status");
298 m_controller
->AddLog(CEC_LOG_NOTICE
, "<< reporting \"Off\" power status");
301 cec_command::format(command
, m_iLogicalAddress
, address
, CEC_OPCODE_REPORT_POWER_STATUS
);
302 command
.parameters
.push_back(bOn
? (uint8_t) CEC_POWER_STATUS_ON
: (uint8_t) CEC_POWER_STATUS_STANDBY
);
307 void CCECProcessor::ReportMenuState(cec_logical_address address
/* = CECDEVICE_TV */, bool bActive
/* = true */)
310 m_controller
->AddLog(CEC_LOG_NOTICE
, "<< reporting menu state as active");
312 m_controller
->AddLog(CEC_LOG_NOTICE
, "<< reporting menu state as inactive");
315 cec_command::format(command
, m_iLogicalAddress
, address
, CEC_OPCODE_MENU_STATUS
);
316 command
.parameters
.push_back(bActive
? (uint8_t) CEC_MENU_STATE_ACTIVATED
: (uint8_t) CEC_MENU_STATE_DEACTIVATED
);
321 void CCECProcessor::ReportVendorID(cec_logical_address address
/* = CECDEVICE_TV */)
323 m_controller
->AddLog(CEC_LOG_NOTICE
, "<< vendor ID requested, feature abort");
324 TransmitAbort(address
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
327 void CCECProcessor::ReportOSDName(cec_logical_address address
/* = CECDEVICE_TV */)
329 const char *osdname
= m_strDeviceName
.c_str();
331 strLog
.Format("<< reporting OSD name as %s", osdname
);
332 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
335 cec_command::format(command
, m_iLogicalAddress
, address
, CEC_OPCODE_SET_OSD_NAME
);
336 for (unsigned int iPtr
= 0; iPtr
< strlen(osdname
); iPtr
++)
337 command
.parameters
.push_back(osdname
[iPtr
]);
342 void CCECProcessor::ReportPhysicalAddress(void)
345 strLog
.Format("<< reporting physical address as %04x", m_iPhysicalAddress
);
346 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
349 cec_command::format(command
, m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
350 command
.parameters
.push_back((uint8_t) ((m_iPhysicalAddress
>> 8) & 0xFF));
351 command
.parameters
.push_back((uint8_t) (m_iPhysicalAddress
& 0xFF));
352 command
.parameters
.push_back((uint8_t) (CEC_DEVICE_TYPE_PLAYBACK_DEVICE
));
357 void CCECProcessor::BroadcastActiveSource(void)
359 m_controller
->AddLog(CEC_LOG_NOTICE
, "<< broadcasting active source");
362 cec_command::format(command
, m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
363 command
.parameters
.push_back((uint8_t) ((m_iPhysicalAddress
>> 8) & 0xFF));
364 command
.parameters
.push_back((uint8_t) (m_iPhysicalAddress
& 0xFF));
369 bool CCECProcessor::WaitForAck(bool *bError
, uint32_t iTimeout
/* = 1000 */)
371 bool bTransmitSucceeded
= false, bEom
= false;
374 int64_t iNow
= GetTimeMs();
375 int64_t iTargetTime
= iNow
+ (uint64_t) iTimeout
;
377 while (!bTransmitSucceeded
&& !*bError
&& (iTimeout
== 0 || iNow
< iTargetTime
))
379 cec_adapter_message msg
;
382 if (!m_communication
->Read(msg
, iTimeout
> 0 ? (int32_t)(iTargetTime
- iNow
) : 1000))
388 ParseMessage(msg
, bError
, &bTransmitSucceeded
, &bEom
, false);
392 return bTransmitSucceeded
&& !*bError
;
395 void CCECProcessor::ParseMessage(cec_adapter_message
&msg
, bool *bError
, bool *bTransmitSucceeded
, bool *bEom
, bool bProcessMessages
/* = true */)
398 *bTransmitSucceeded
= false;
406 switch(msg
.message())
408 case MSGCODE_NOTHING
:
409 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_NOTHING");
411 case MSGCODE_TIMEOUT_ERROR
:
412 case MSGCODE_HIGH_ERROR
:
413 case MSGCODE_LOW_ERROR
:
415 if (msg
.message() == MSGCODE_TIMEOUT_ERROR
)
416 logStr
= "MSGCODE_TIMEOUT";
417 else if (msg
.message() == MSGCODE_HIGH_ERROR
)
418 logStr
= "MSGCODE_HIGH_ERROR";
420 logStr
= "MSGCODE_LOW_ERROR";
422 int iLine
= (msg
.size() >= 3) ? (msg
[1] << 8) | (msg
[2]) : 0;
423 uint32_t iTime
= (msg
.size() >= 7) ? (msg
[3] << 24) | (msg
[4] << 16) | (msg
[5] << 8) | (msg
[6]) : 0;
424 logStr
.AppendFormat(" line:%i", iLine
);
425 logStr
.AppendFormat(" time:%u", iTime
);
426 m_controller
->AddLog(CEC_LOG_WARNING
, logStr
.c_str());
430 case MSGCODE_FRAME_START
:
432 if (bProcessMessages
)
434 logStr
= "MSGCODE_FRAME_START";
435 m_currentframe
.clear();
438 logStr
.AppendFormat(" initiator:%u destination:%u ack:%s %s", msg
.initiator(), msg
.destination(), msg
.ack() ? "high" : "low", msg
.eom() ? "eom" : "");
439 m_currentframe
.initiator
= msg
.initiator();
440 m_currentframe
.destination
= msg
.destination();
441 m_currentframe
.ack
= msg
.ack();
442 m_currentframe
.eom
= msg
.eom();
444 m_controller
->AddLog(CEC_LOG_DEBUG
, logStr
.c_str());
448 m_frameBuffer
.Push(msg
);
452 case MSGCODE_FRAME_DATA
:
454 if (bProcessMessages
)
456 logStr
= "MSGCODE_FRAME_DATA";
459 uint8_t iData
= msg
[1];
460 logStr
.AppendFormat(" %02x", iData
);
461 m_currentframe
.push_back(iData
);
462 m_currentframe
.eom
= msg
.eom();
464 m_controller
->AddLog(CEC_LOG_DEBUG
, logStr
.c_str());
468 m_frameBuffer
.Push(msg
);
474 case MSGCODE_COMMAND_ACCEPTED
:
475 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_COMMAND_ACCEPTED");
477 case MSGCODE_TRANSMIT_SUCCEEDED
:
478 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_TRANSMIT_SUCCEEDED");
479 *bTransmitSucceeded
= true;
481 case MSGCODE_RECEIVE_FAILED
:
482 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_RECEIVE_FAILED");
485 case MSGCODE_COMMAND_REJECTED
:
486 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_COMMAND_REJECTED");
489 case MSGCODE_TRANSMIT_FAILED_LINE
:
490 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_LINE");
493 case MSGCODE_TRANSMIT_FAILED_ACK
:
494 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_ACK");
497 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
:
498 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA");
501 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
:
502 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE");
510 void CCECProcessor::ParseVendorId(cec_logical_address device
, const cec_datapacket
&data
)
514 m_controller
->AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
518 uint64_t iVendorId
= ((uint64_t)data
[0] << 3) +
519 ((uint64_t)data
[1] << 2) +
522 m_vendorIds
[(uint8_t)device
] = iVendorId
;
523 m_vendorClasses
[(uint8_t)device
] = data
.size
>= 4 ? data
[3] : 0;
526 strLog
.Format("device %d: vendor = %s (%lld) class = %2x", (uint8_t)device
, CECVendorIdToString(m_vendorIds
[(uint8_t)device
]), iVendorId
, m_vendorClasses
[(uint8_t)device
]);
527 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
530 void CCECProcessor::ParseCommand(cec_command
&command
)
533 dataStr
.Format(">> received frame: initiator: %u destination: %u", command
.initiator
, command
.destination
);
534 if (command
.parameters
.size
> 1)
537 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
538 dataStr
.AppendFormat(" %02x", (unsigned int)command
.parameters
[iPtr
]);
540 m_controller
->AddLog(CEC_LOG_DEBUG
, dataStr
.c_str());
542 if (command
.destination
== m_iLogicalAddress
)
544 switch(command
.opcode
)
546 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
547 ReportPhysicalAddress();
549 case CEC_OPCODE_GIVE_OSD_NAME
:
550 ReportOSDName(command
.initiator
);
552 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
553 ReportVendorID(command
.initiator
);
555 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
556 ParseVendorId(command
.initiator
, command
.parameters
);
557 TransmitAbort(command
.initiator
, CEC_OPCODE_VENDOR_COMMAND_WITH_ID
);
559 case CEC_OPCODE_GIVE_DECK_STATUS
:
560 // need to support opcodes play and deck control before doing anything with this
561 TransmitAbort(command
.initiator
, CEC_OPCODE_GIVE_DECK_STATUS
);
563 case CEC_OPCODE_MENU_REQUEST
:
564 if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_QUERY
)
565 ReportMenuState(command
.initiator
);
567 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
568 ReportPowerState(command
.initiator
);
570 case CEC_OPCODE_GET_CEC_VERSION
:
571 ReportCECVersion(command
.initiator
);
573 case CEC_OPCODE_USER_CONTROL_PRESSED
:
574 if (command
.parameters
.size
> 0)
576 m_controller
->AddKey();
578 if (command
.parameters
[0] <= CEC_USER_CONTROL_CODE_MAX
)
581 strLog
.Format("key pressed: %1x", command
.parameters
[0]);
582 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
584 m_controller
->SetCurrentButton((cec_user_control_code
) command
.parameters
[0]);
588 case CEC_OPCODE_USER_CONTROL_RELEASE
:
589 m_controller
->AddKey();
592 m_controller
->AddCommand(command
);
596 else if (command
.destination
== CECDEVICE_BROADCAST
)
599 switch (command
.opcode
)
601 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
602 strLog
.Format(">> %i requests active source", (uint8_t) command
.initiator
);
603 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
604 BroadcastActiveSource();
606 case CEC_OPCODE_SET_STREAM_PATH
:
607 if (command
.parameters
.size
>= 2)
609 int streamaddr
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
610 strLog
.Format(">> %i requests stream path from physical address %04x", command
.initiator
, streamaddr
);
611 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
612 if (streamaddr
== m_iPhysicalAddress
)
613 BroadcastActiveSource();
616 case CEC_OPCODE_ROUTING_CHANGE
:
617 if (command
.parameters
.size
== 4)
619 uint16_t iOldAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
620 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[2] << 8) | ((uint16_t)command
.parameters
[3]);
621 strLog
.Format(">> %i changed physical address from %04x to %04x", command
.initiator
, iOldAddress
, iNewAddress
);
622 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
624 m_controller
->AddCommand(command
);
628 m_controller
->AddCommand(command
);
635 strLog
.Format("ignoring frame: destination: %u != %u", command
.destination
, (uint8_t)m_iLogicalAddress
);
636 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
640 const char *CCECProcessor::CECVendorIdToString(const uint64_t iVendorId
)
644 case CEC_VENDOR_SAMSUNG
: