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_physicaladdress(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);
99 CLockObject
lock(&m_mutex
);
100 if (m_communication
->IsOpen() && m_communication
->Read(msg
, 50))
101 bParseFrame
= ParseMessage(msg
) && !IsStopped();
104 command
= m_currentframe
;
108 ParseCommand(command
);
110 m_controller
->CheckKeypressTimeout();
119 bool CCECProcessor::PowerOnDevices(cec_logical_address address
/* = CECDEVICE_TV */)
125 strLog
.Format("<< powering on device with logical address %d", (int8_t)address
);
126 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
128 return Transmit(cec_command::format(m_iLogicalAddress
, address
, CEC_OPCODE_IMAGE_VIEW_ON
));
131 bool CCECProcessor::StandbyDevices(cec_logical_address address
/* = CECDEVICE_BROADCAST */)
137 strLog
.Format("<< putting device with logical address %d in standby mode", (int8_t)address
);
138 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
140 return Transmit(cec_command::format(m_iLogicalAddress
, address
, CEC_OPCODE_STANDBY
));
143 bool CCECProcessor::SetActiveView(void)
148 m_controller
->AddLog(CEC_LOG_DEBUG
, "<< setting active view");
150 cec_command command
= cec_command::format(m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
151 command
.parameters
.push_back((m_physicaladdress
>> 8) & 0xFF);
152 command
.parameters
.push_back(m_physicaladdress
& 0xFF);
154 return Transmit(command
);
157 bool CCECProcessor::SetInactiveView(void)
162 m_controller
->AddLog(CEC_LOG_DEBUG
, "<< setting inactive view");
164 cec_command command
= cec_command::format(m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_INACTIVE_SOURCE
);
165 command
.parameters
.push_back((m_physicaladdress
>> 8) & 0xFF);
166 command
.parameters
.push_back(m_physicaladdress
& 0xFF);
168 return Transmit(command
);
171 void CCECProcessor::LogOutput(const cec_command
&data
)
173 CStdString txStr
= "transmit ";
174 txStr
.AppendFormat(" %02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
);
175 txStr
.AppendFormat(" %02x", (uint8_t)data
.opcode
);
177 for (unsigned int iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
178 txStr
.AppendFormat(" %02x", data
.parameters
[iPtr
]);
179 m_controller
->AddLog(CEC_LOG_DEBUG
, txStr
.c_str());
182 bool CCECProcessor::Transmit(const cec_command
&data
, bool bWaitForAck
/* = true */)
186 cec_adapter_message output
;
188 CAdapterCommunication::FormatAdapterMessage(data
, output
);
190 return TransmitFormatted(output
, bWaitForAck
);
193 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress
)
196 strLog
.Format("<< setting logical address to %d", iLogicalAddress
);
197 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
199 m_iLogicalAddress
= iLogicalAddress
;
200 return m_communication
&& m_communication
->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress
);
203 bool CCECProcessor::TransmitFormatted(const cec_adapter_message
&data
, bool bWaitForAck
/* = true */)
205 CLockObject
lock(&m_mutex
);
206 if (!m_communication
|| !m_communication
->Write(data
))
211 uint64_t now
= GetTimeMs();
212 uint64_t target
= now
+ 1000;
216 while (!bGotAck
&& now
< target
)
218 bGotAck
= WaitForAck(&bError
, (uint64_t) (target
- now
));
221 if (bError
&& now
< target
)
223 m_controller
->AddLog(CEC_LOG_ERROR
, "retransmitting previous frame");
224 if (!m_communication
->Write(data
))
233 void CCECProcessor::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, ECecAbortReason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
235 m_controller
->AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
237 cec_command command
= cec_command::format(m_iLogicalAddress
, address
, CEC_OPCODE_FEATURE_ABORT
);
238 command
.parameters
.push_back((uint8_t)opcode
);
239 command
.parameters
.push_back((uint8_t)reason
);
244 void CCECProcessor::ReportCECVersion(cec_logical_address address
/* = CECDEVICE_TV */)
246 m_controller
->AddLog(CEC_LOG_NOTICE
, "<< reporting CEC version as 1.3a");
248 cec_command command
= cec_command::format(m_iLogicalAddress
, address
, CEC_OPCODE_CEC_VERSION
);
249 command
.parameters
.push_back(CEC_VERSION_1_3A
);
254 void CCECProcessor::ReportPowerState(cec_logical_address address
/*= CECDEVICE_TV */, bool bOn
/* = true */)
257 m_controller
->AddLog(CEC_LOG_NOTICE
, "<< reporting \"On\" power status");
259 m_controller
->AddLog(CEC_LOG_NOTICE
, "<< reporting \"Off\" power status");
261 cec_command command
= cec_command::format(m_iLogicalAddress
, address
, CEC_OPCODE_REPORT_POWER_STATUS
);
262 command
.parameters
.push_back(bOn
? (uint8_t) CEC_POWER_STATUS_ON
: (uint8_t) CEC_POWER_STATUS_STANDBY
);
267 void CCECProcessor::ReportMenuState(cec_logical_address address
/* = CECDEVICE_TV */, bool bActive
/* = true */)
270 m_controller
->AddLog(CEC_LOG_NOTICE
, "<< reporting menu state as active");
272 m_controller
->AddLog(CEC_LOG_NOTICE
, "<< reporting menu state as inactive");
274 cec_command command
= cec_command::format(m_iLogicalAddress
, address
, CEC_OPCODE_MENU_STATUS
);
275 command
.parameters
.push_back(bActive
? (uint8_t) CEC_MENU_STATE_ACTIVATED
: (uint8_t) CEC_MENU_STATE_DEACTIVATED
);
280 void CCECProcessor::ReportVendorID(cec_logical_address address
/* = CECDEVICE_TV */)
282 m_controller
->AddLog(CEC_LOG_NOTICE
, "<< vendor ID requested, feature abort");
283 TransmitAbort(address
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
286 void CCECProcessor::ReportOSDName(cec_logical_address address
/* = CECDEVICE_TV */)
288 const char *osdname
= m_strDeviceName
.c_str();
290 strLog
.Format("<< reporting OSD name as %s", osdname
);
291 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
293 cec_command command
= cec_command::format(m_iLogicalAddress
, address
, CEC_OPCODE_SET_OSD_NAME
);
294 for (unsigned int iPtr
= 0; iPtr
< strlen(osdname
); iPtr
++)
295 command
.parameters
.push_back(osdname
[iPtr
]);
300 void CCECProcessor::ReportPhysicalAddress(void)
303 strLog
.Format("<< reporting physical address as %04x", m_physicaladdress
);
304 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
306 cec_command command
= cec_command::format(m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
307 command
.parameters
.push_back((uint8_t) ((m_physicaladdress
>> 8) & 0xFF));
308 command
.parameters
.push_back((uint8_t) (m_physicaladdress
& 0xFF));
313 void CCECProcessor::BroadcastActiveSource(void)
315 m_controller
->AddLog(CEC_LOG_NOTICE
, "<< broadcasting active source");
317 cec_command command
= cec_command::format(m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
318 command
.parameters
.push_back((uint8_t) ((m_physicaladdress
>> 8) & 0xFF));
319 command
.parameters
.push_back((uint8_t) (m_physicaladdress
& 0xFF));
324 bool CCECProcessor::WaitForAck(bool *bError
, uint32_t iTimeout
/* = 1000 */)
329 int64_t iNow
= GetTimeMs();
330 int64_t iTargetTime
= iNow
+ (uint64_t) iTimeout
;
332 while (!bGotAck
&& !*bError
&& (iTimeout
== 0 || iNow
< iTargetTime
))
334 cec_adapter_message msg
;
337 if (!m_communication
->Read(msg
, iTimeout
> 0 ? iTargetTime
- iNow
: 1000))
343 switch (msg
.message())
345 case MSGCODE_COMMAND_ACCEPTED
:
346 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_COMMAND_ACCEPTED");
348 case MSGCODE_TRANSMIT_SUCCEEDED
:
349 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_TRANSMIT_SUCCEEDED");
352 case MSGCODE_RECEIVE_FAILED
:
353 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_RECEIVE_FAILED");
356 case MSGCODE_COMMAND_REJECTED
:
357 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_COMMAND_REJECTED");
360 case MSGCODE_TRANSMIT_FAILED_LINE
:
361 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_LINE");
364 case MSGCODE_TRANSMIT_FAILED_ACK
:
365 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_ACK");
368 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
:
369 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA");
372 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
:
373 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE");
377 m_frameBuffer
.Push(msg
);
384 return bGotAck
&& !*bError
;
387 bool CCECProcessor::ParseMessage(cec_adapter_message
&msg
)
396 switch(msg
.message())
398 case MSGCODE_NOTHING
:
399 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_NOTHING");
401 case MSGCODE_TIMEOUT_ERROR
:
402 case MSGCODE_HIGH_ERROR
:
403 case MSGCODE_LOW_ERROR
:
405 if (msg
.message() == MSGCODE_TIMEOUT_ERROR
)
406 logStr
= "MSGCODE_TIMEOUT";
407 else if (msg
.message() == MSGCODE_HIGH_ERROR
)
408 logStr
= "MSGCODE_HIGH_ERROR";
410 logStr
= "MSGCODE_LOW_ERROR";
412 int iLine
= (msg
.size() >= 3) ? (msg
[1] << 8) | (msg
[2]) : 0;
413 uint32_t iTime
= (msg
.size() >= 7) ? (msg
[3] << 24) | (msg
[4] << 16) | (msg
[5] << 8) | (msg
[6]) : 0;
414 logStr
.AppendFormat(" line:%i", iLine
);
415 logStr
.AppendFormat(" time:%u", iTime
);
416 m_controller
->AddLog(CEC_LOG_WARNING
, logStr
.c_str());
419 case MSGCODE_FRAME_START
:
421 logStr
= "MSGCODE_FRAME_START";
422 m_currentframe
.clear();
425 logStr
.AppendFormat(" initiator:%u destination:%u ack:%s %s", msg
.initiator(), msg
.destination(), msg
.ack() ? "high" : "low", msg
.eom() ? "eom" : "");
426 m_currentframe
.initiator
= msg
.initiator();
427 m_currentframe
.destination
= msg
.destination();
428 m_currentframe
.ack
= msg
.ack();
429 m_currentframe
.eom
= msg
.eom();
431 m_controller
->AddLog(CEC_LOG_DEBUG
, logStr
.c_str());
434 case MSGCODE_FRAME_DATA
:
436 logStr
= "MSGCODE_FRAME_DATA";
439 uint8_t iData
= msg
[1];
440 logStr
.AppendFormat(" %02x", iData
);
441 m_currentframe
.push_back(iData
);
443 m_controller
->AddLog(CEC_LOG_DEBUG
, logStr
.c_str());
455 void CCECProcessor::ParseVendorId(cec_logical_address device
, const cec_datapacket
&data
)
459 m_controller
->AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
463 uint64_t iVendorId
= ((uint64_t)data
[0] << 3) +
464 ((uint64_t)data
[1] << 2) +
467 m_vendorIds
[(uint8_t)device
] = iVendorId
;
468 m_vendorClasses
[(uint8_t)device
] = data
.size
>= 4 ? data
[3] : 0;
471 strLog
.Format("device %d: vendor = %s (%lld) class = %2x", (uint8_t)device
, CECVendorIdToString(m_vendorIds
[(uint8_t)device
]), iVendorId
, m_vendorClasses
[(uint8_t)device
]);
472 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
475 void CCECProcessor::ParseCommand(cec_command
&command
)
478 dataStr
.Format(">> received frame: initiator: %u destination: %u", command
.initiator
, command
.destination
);
479 if (command
.parameters
.size
> 1)
482 for (unsigned int iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
483 dataStr
.AppendFormat(" %02x", command
.parameters
[iPtr
]);
485 m_controller
->AddLog(CEC_LOG_DEBUG
, dataStr
.c_str());
487 if (command
.destination
== m_iLogicalAddress
)
489 switch(command
.opcode
)
491 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
492 ReportPhysicalAddress();
494 case CEC_OPCODE_GIVE_OSD_NAME
:
495 ReportOSDName(command
.initiator
);
497 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
498 ReportVendorID(command
.initiator
);
500 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
501 ParseVendorId(command
.initiator
, command
.parameters
);
502 TransmitAbort(command
.initiator
, CEC_OPCODE_VENDOR_COMMAND_WITH_ID
);
504 case CEC_OPCODE_GIVE_DECK_STATUS
:
505 // need to support opcodes play and deck control before doing anything with this
506 TransmitAbort(command
.initiator
, CEC_OPCODE_GIVE_DECK_STATUS
);
508 case CEC_OPCODE_MENU_REQUEST
:
509 if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_QUERY
)
510 ReportMenuState(command
.initiator
);
512 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
513 ReportPowerState(command
.initiator
);
516 case CEC_OPCODE_GET_CEC_VERSION
:
517 ReportCECVersion(command
.initiator
);
519 case CEC_OPCODE_USER_CONTROL_PRESSED
:
520 if (command
.parameters
.size
> 0)
522 m_controller
->AddKey();
524 if (command
.parameters
[0] <= CEC_USER_CONTROL_CODE_MAX
)
525 m_controller
->SetCurrentButton((cec_user_control_code
) command
.parameters
[0]);
528 case CEC_OPCODE_USER_CONTROL_RELEASE
:
529 m_controller
->AddKey();
532 m_controller
->AddCommand(command
);
536 else if (command
.destination
== CECDEVICE_BROADCAST
)
539 if (command
.opcode
== CEC_OPCODE_REQUEST_ACTIVE_SOURCE
)
541 strLog
.Format(">> %i requests active source", (uint8_t) command
.initiator
);
542 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
543 BroadcastActiveSource();
545 else if (command
.opcode
== CEC_OPCODE_SET_STREAM_PATH
)
547 if (command
.parameters
.size
>= 2)
549 int streamaddr
= ((int)command
.parameters
[0] << 8) | ((int)command
.parameters
[1]);
550 strLog
.Format(">> %i requests stream path from physical address %04x", command
.initiator
, streamaddr
);
551 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
552 if (streamaddr
== m_physicaladdress
)
553 BroadcastActiveSource();
558 m_controller
->AddCommand(command
);
564 strLog
.Format("ignoring frame: destination: %u != %u", command
.destination
, (uint8_t)m_iLogicalAddress
);
565 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());