2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011-2012 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 "CECBusDevice.h"
34 #include "../CECProcessor.h"
35 #include "../CECClient.h"
36 #include "../implementations/ANCommandHandler.h"
37 #include "../implementations/CECCommandHandler.h"
38 #include "../implementations/SLCommandHandler.h"
39 #include "../implementations/VLCommandHandler.h"
40 #include "../LibCEC.h"
41 #include "../CECTypeUtils.h"
42 #include "../platform/util/timeutils.h"
43 #include "../platform/util/util.h"
45 #include "CECAudioSystem.h"
46 #include "CECPlaybackDevice.h"
47 #include "CECRecordingDevice.h"
53 using namespace PLATFORM
;
55 #define LIB_CEC m_processor->GetLib()
56 #define ToString(p) CCECTypeUtils::ToString(p)
58 CCECBusDevice::CCECBusDevice(CCECProcessor
*processor
, cec_logical_address iLogicalAddress
, uint16_t iPhysicalAddress
/* = CEC_INVALID_PHYSICAL_ADDRESS */) :
59 m_type (CEC_DEVICE_TYPE_RESERVED
),
60 m_iPhysicalAddress (iPhysicalAddress
),
61 m_iStreamPath (CEC_INVALID_PHYSICAL_ADDRESS
),
62 m_iLogicalAddress (iLogicalAddress
),
63 m_powerStatus (CEC_POWER_STATUS_UNKNOWN
),
64 m_processor (processor
),
65 m_vendor (CEC_VENDOR_UNKNOWN
),
66 m_bReplaceHandler (false),
67 m_menuState (CEC_MENU_STATE_ACTIVATED
),
68 m_bActiveSource (false),
70 m_iLastPowerStateUpdate (0),
71 m_cecVersion (CEC_VERSION_UNKNOWN
),
72 m_deviceStatus (CEC_DEVICE_STATUS_UNKNOWN
),
73 m_iHandlerUseCount (0),
74 m_bAwaitingReceiveFailed(false),
75 m_bVendorIdRequested (false)
77 m_handler
= new CCECCommandHandler(this);
79 for (unsigned int iPtr
= 0; iPtr
< 4; iPtr
++)
80 m_menuLanguage
.language
[iPtr
] = '?';
81 m_menuLanguage
.language
[3] = 0;
82 m_menuLanguage
.device
= iLogicalAddress
;
84 m_strDeviceName
= ToString(m_iLogicalAddress
);
87 CCECBusDevice::~CCECBusDevice(void)
89 DELETE_AND_NULL(m_handler
);
92 bool CCECBusDevice::ReplaceHandler(bool bActivateSource
/* = true */)
94 bool bInitHandler(false);
96 CTryLockObject
lock(m_mutex
);
100 CLockObject
handlerLock(m_handlerMutex
);
101 if (m_iHandlerUseCount
> 0)
106 if (m_vendor
!= m_handler
->GetVendorId())
108 if (CCECCommandHandler::HasSpecificHandler(m_vendor
))
110 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "replacing the command handler for device '%s' (%x)", GetLogicalAddressName(), GetLogicalAddress());
111 DELETE_AND_NULL(m_handler
);
115 case CEC_VENDOR_SAMSUNG
:
116 m_handler
= new CANCommandHandler(this);
119 m_handler
= new CSLCommandHandler(this);
121 case CEC_VENDOR_PANASONIC
:
122 m_handler
= new CVLCommandHandler(this);
125 m_handler
= new CCECCommandHandler(this);
129 m_handler
->SetVendorId(m_vendor
);
137 CCECBusDevice
*primary
= GetProcessor()->GetPrimaryDevice();
138 if (primary
->GetLogicalAddress() != CECDEVICE_UNREGISTERED
)
140 m_handler
->InitHandler();
142 if (bActivateSource
&& IsHandledByLibCEC() && IsActiveSource())
143 m_handler
->ActivateSource();
152 CCECCommandHandler
*CCECBusDevice::GetHandler(void)
154 ReplaceHandler(false);
159 bool CCECBusDevice::HandleCommand(const cec_command
&command
)
161 bool bHandled(false);
163 /* update "last active" */
165 CLockObject
lock(m_mutex
);
166 m_iLastActive
= GetTimeMs();
168 /* don't call GetStatus() here, just read the value with the mutex locked */
169 if (m_deviceStatus
!= CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
&& command
.opcode_set
== 1)
170 m_deviceStatus
= CEC_DEVICE_STATUS_PRESENT
;
175 /* handle the command */
176 bHandled
= m_handler
->HandleCommand(command
);
178 /* change status to present */
179 if (bHandled
&& GetLogicalAddress() != CECDEVICE_BROADCAST
)
181 CLockObject
lock(m_mutex
);
182 if (m_deviceStatus
!= CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
)
184 if (m_deviceStatus
!= CEC_DEVICE_STATUS_PRESENT
)
185 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "device %s (%x) status changed to present after command %s", GetLogicalAddressName(), (uint8_t)GetLogicalAddress(), ToString(command
.opcode
));
186 m_deviceStatus
= CEC_DEVICE_STATUS_PRESENT
;
194 const char* CCECBusDevice::GetLogicalAddressName(void) const
196 return ToString(m_iLogicalAddress
);
199 bool CCECBusDevice::IsPresent(void)
201 CLockObject
lock(m_mutex
);
202 return m_deviceStatus
== CEC_DEVICE_STATUS_PRESENT
;
205 bool CCECBusDevice::IsHandledByLibCEC(void)
207 CLockObject
lock(m_mutex
);
208 return m_deviceStatus
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
;
211 void CCECBusDevice::SetUnsupportedFeature(cec_opcode opcode
)
213 // some commands should never be marked as unsupported
214 if (opcode
== CEC_OPCODE_VENDOR_COMMAND
||
215 opcode
== CEC_OPCODE_VENDOR_COMMAND_WITH_ID
||
216 opcode
== CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN
||
217 opcode
== CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP
||
218 opcode
== CEC_OPCODE_ABORT
||
219 opcode
== CEC_OPCODE_FEATURE_ABORT
||
220 opcode
== CEC_OPCODE_NONE
)
224 CLockObject
lock(m_mutex
);
225 if (m_unsupportedFeatures
.find(opcode
) == m_unsupportedFeatures
.end())
227 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "marking opcode '%s' as unsupported feature for device '%s'", ToString(opcode
), GetLogicalAddressName());
228 m_unsupportedFeatures
.insert(opcode
);
232 // signal threads that are waiting for a reponse
234 m_handler
->SignalOpcode(cec_command::GetResponseOpcode(opcode
));
238 bool CCECBusDevice::IsUnsupportedFeature(cec_opcode opcode
)
240 CLockObject
lock(m_mutex
);
241 bool bUnsupported
= (m_unsupportedFeatures
.find(opcode
) != m_unsupportedFeatures
.end());
243 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "'%s' is marked as unsupported feature for device '%s'", ToString(opcode
), GetLogicalAddressName());
247 bool CCECBusDevice::TransmitKeypress(const cec_logical_address initiator
, cec_user_control_code key
, bool bWait
/* = true */)
250 bool bReturn
= m_handler
->TransmitKeypress(initiator
, m_iLogicalAddress
, key
, bWait
);
255 bool CCECBusDevice::TransmitKeyRelease(const cec_logical_address initiator
, bool bWait
/* = true */)
258 bool bReturn
= m_handler
->TransmitKeyRelease(initiator
, m_iLogicalAddress
, bWait
);
263 cec_version
CCECBusDevice::GetCecVersion(const cec_logical_address initiator
, bool bUpdate
/* = false */)
265 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
266 bool bRequestUpdate(false);
268 CLockObject
lock(m_mutex
);
269 bRequestUpdate
= bIsPresent
&&
270 (bUpdate
|| m_cecVersion
== CEC_VERSION_UNKNOWN
);
275 CheckVendorIdRequested(initiator
);
276 RequestCecVersion(initiator
);
279 CLockObject
lock(m_mutex
);
283 void CCECBusDevice::SetCecVersion(const cec_version newVersion
)
285 CLockObject
lock(m_mutex
);
286 if (m_cecVersion
!= newVersion
)
287 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): CEC version %s", GetLogicalAddressName(), m_iLogicalAddress
, ToString(newVersion
));
288 m_cecVersion
= newVersion
;
291 bool CCECBusDevice::RequestCecVersion(const cec_logical_address initiator
, bool bWaitForResponse
/* = true */)
295 if (!IsHandledByLibCEC() &&
296 !IsUnsupportedFeature(CEC_OPCODE_GET_CEC_VERSION
))
299 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< requesting CEC version of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
300 bReturn
= m_handler
->TransmitRequestCecVersion(initiator
, m_iLogicalAddress
, bWaitForResponse
);
306 bool CCECBusDevice::TransmitCECVersion(const cec_logical_address destination
)
310 CLockObject
lock(m_mutex
);
311 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): cec version %s", GetLogicalAddressName(), m_iLogicalAddress
, ToString(destination
), destination
, ToString(m_cecVersion
));
312 version
= m_cecVersion
;
316 bool bReturn
= m_handler
->TransmitCECVersion(m_iLogicalAddress
, destination
, version
);
321 cec_menu_language
&CCECBusDevice::GetMenuLanguage(const cec_logical_address initiator
, bool bUpdate
/* = false */)
323 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
324 bool bRequestUpdate(false);
326 CLockObject
lock(m_mutex
);
327 bRequestUpdate
= (bIsPresent
&&
328 (bUpdate
|| !strcmp(m_menuLanguage
.language
, "???")));
333 CheckVendorIdRequested(initiator
);
334 RequestMenuLanguage(initiator
);
337 CLockObject
lock(m_mutex
);
338 return m_menuLanguage
;
341 void CCECBusDevice::SetMenuLanguage(const char *strLanguage
)
346 CLockObject
lock(m_mutex
);
347 if (strcmp(strLanguage
, m_menuLanguage
.language
))
349 memcpy(m_menuLanguage
.language
, strLanguage
, 3);
350 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): menu language set to '%s'", GetLogicalAddressName(), m_iLogicalAddress
, m_menuLanguage
.language
);
354 void CCECBusDevice::SetMenuLanguage(const cec_menu_language
&language
)
356 if (language
.device
== m_iLogicalAddress
)
357 SetMenuLanguage(language
.language
);
360 bool CCECBusDevice::RequestMenuLanguage(const cec_logical_address initiator
, bool bWaitForResponse
/* = true */)
364 if (!IsHandledByLibCEC() &&
365 !IsUnsupportedFeature(CEC_OPCODE_GET_MENU_LANGUAGE
))
368 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< requesting menu language of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
369 bReturn
= m_handler
->TransmitRequestMenuLanguage(initiator
, m_iLogicalAddress
, bWaitForResponse
);
375 bool CCECBusDevice::TransmitSetMenuLanguage(const cec_logical_address destination
)
378 cec_menu_language language
;
380 CLockObject
lock(m_mutex
);
381 language
= m_menuLanguage
;
386 CLockObject
lock(m_mutex
);
387 lang
[0] = language
.language
[0];
388 lang
[1] = language
.language
[1];
389 lang
[2] = language
.language
[2];
393 if (lang
[0] == '?' && lang
[1] == '?' && lang
[2] == '?')
395 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): Menu language feature abort", GetLogicalAddressName(), m_iLogicalAddress
, ToString(destination
), destination
);
396 m_processor
->TransmitAbort(m_iLogicalAddress
, destination
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
401 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> broadcast (F): Menu language '%s'", GetLogicalAddressName(), m_iLogicalAddress
, lang
);
402 bReturn
= m_handler
->TransmitSetMenuLanguage(m_iLogicalAddress
, lang
);
408 bool CCECBusDevice::TransmitOSDString(const cec_logical_address destination
, cec_display_control duration
, const char *strMessage
)
411 if (!m_processor
->GetDevice(destination
)->IsUnsupportedFeature(CEC_OPCODE_SET_OSD_STRING
))
413 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): display OSD message '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(destination
), destination
, strMessage
);
415 bReturn
= m_handler
->TransmitOSDString(m_iLogicalAddress
, destination
, duration
, strMessage
);
421 CStdString
CCECBusDevice::GetCurrentOSDName(void)
423 CLockObject
lock(m_mutex
);
424 return m_strDeviceName
;
427 CStdString
CCECBusDevice::GetOSDName(const cec_logical_address initiator
, bool bUpdate
/* = false */)
429 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
430 bool bRequestUpdate(false);
432 CLockObject
lock(m_mutex
);
433 bRequestUpdate
= bIsPresent
&&
434 (bUpdate
|| m_strDeviceName
.Equals(ToString(m_iLogicalAddress
))) &&
435 m_type
!= CEC_DEVICE_TYPE_TV
;
440 CheckVendorIdRequested(initiator
);
441 RequestOSDName(initiator
);
444 CLockObject
lock(m_mutex
);
445 return m_strDeviceName
;
448 void CCECBusDevice::SetOSDName(CStdString strName
)
450 CLockObject
lock(m_mutex
);
451 if (m_strDeviceName
!= strName
)
453 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): osd name set to '%s'", GetLogicalAddressName(), m_iLogicalAddress
, strName
.c_str());
454 m_strDeviceName
= strName
;
458 bool CCECBusDevice::RequestOSDName(const cec_logical_address initiator
, bool bWaitForResponse
/* = true */)
462 if (!IsHandledByLibCEC() &&
463 !IsUnsupportedFeature(CEC_OPCODE_GIVE_OSD_NAME
))
466 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< requesting OSD name of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
467 bReturn
= m_handler
->TransmitRequestOSDName(initiator
, m_iLogicalAddress
, bWaitForResponse
);
473 bool CCECBusDevice::TransmitOSDName(const cec_logical_address destination
)
475 CStdString strDeviceName
;
477 CLockObject
lock(m_mutex
);
478 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): OSD name '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(destination
), destination
, m_strDeviceName
.c_str());
479 strDeviceName
= m_strDeviceName
;
483 bool bReturn
= m_handler
->TransmitOSDName(m_iLogicalAddress
, destination
, strDeviceName
);
488 bool CCECBusDevice::HasValidPhysicalAddress(void)
490 CLockObject
lock(m_mutex
);
491 return CLibCEC::IsValidPhysicalAddress(m_iPhysicalAddress
);
494 uint16_t CCECBusDevice::GetCurrentPhysicalAddress(void)
496 CLockObject
lock(m_mutex
);
497 return m_iPhysicalAddress
;
500 uint16_t CCECBusDevice::GetPhysicalAddress(const cec_logical_address initiator
, bool bSuppressUpdate
/* = false */)
502 if (!bSuppressUpdate
)
504 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
505 bool bRequestUpdate(false);
507 CLockObject
lock(m_mutex
);
508 bRequestUpdate
= bIsPresent
&& m_iPhysicalAddress
== CEC_INVALID_PHYSICAL_ADDRESS
;
513 CheckVendorIdRequested(initiator
);
514 if (!RequestPhysicalAddress(initiator
))
515 LIB_CEC
->AddLog(CEC_LOG_ERROR
, "failed to request the physical address");
519 CLockObject
lock(m_mutex
);
520 return m_iPhysicalAddress
;
523 bool CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress
)
525 CLockObject
lock(m_mutex
);
526 if (iNewAddress
> 0 && m_iPhysicalAddress
!= iNewAddress
)
528 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): physical address changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress
, m_iPhysicalAddress
, iNewAddress
);
529 m_iPhysicalAddress
= iNewAddress
;
534 bool CCECBusDevice::RequestPhysicalAddress(const cec_logical_address initiator
, bool bWaitForResponse
/* = true */)
538 if (!IsHandledByLibCEC())
541 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< requesting physical address of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
542 bReturn
= m_handler
->TransmitRequestPhysicalAddress(initiator
, m_iLogicalAddress
, bWaitForResponse
);
548 bool CCECBusDevice::TransmitPhysicalAddress(void)
550 uint16_t iPhysicalAddress
;
551 cec_device_type type
;
553 CLockObject
lock(m_mutex
);
554 if (m_iPhysicalAddress
== CEC_INVALID_PHYSICAL_ADDRESS
)
557 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> broadcast (F): physical adddress %4x", GetLogicalAddressName(), m_iLogicalAddress
, m_iPhysicalAddress
);
558 iPhysicalAddress
= m_iPhysicalAddress
;
563 bool bReturn
= m_handler
->TransmitPhysicalAddress(m_iLogicalAddress
, iPhysicalAddress
, type
);
568 cec_power_status
CCECBusDevice::GetCurrentPowerStatus(void)
570 CLockObject
lock(m_mutex
);
571 return m_powerStatus
;
574 cec_power_status
CCECBusDevice::GetPowerStatus(const cec_logical_address initiator
, bool bUpdate
/* = false */)
576 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
577 bool bRequestUpdate(false);
579 CLockObject
lock(m_mutex
);
580 bRequestUpdate
= (bIsPresent
&&
581 (bUpdate
|| m_powerStatus
== CEC_POWER_STATUS_UNKNOWN
||
582 m_powerStatus
== CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
||
583 m_powerStatus
== CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY
||
584 GetTimeMs() - m_iLastPowerStateUpdate
>= CEC_POWER_STATE_REFRESH_TIME
));
589 CheckVendorIdRequested(initiator
);
590 RequestPowerStatus(initiator
);
593 CLockObject
lock(m_mutex
);
594 return m_powerStatus
;
597 void CCECBusDevice::SetPowerStatus(const cec_power_status powerStatus
)
599 CLockObject
lock(m_mutex
);
600 if (m_powerStatus
!= powerStatus
)
602 m_iLastPowerStateUpdate
= GetTimeMs();
603 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): power status changed from '%s' to '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(m_powerStatus
), ToString(powerStatus
));
604 m_powerStatus
= powerStatus
;
608 bool CCECBusDevice::RequestPowerStatus(const cec_logical_address initiator
, bool bWaitForResponse
/* = true */)
612 if (!IsHandledByLibCEC() &&
613 !IsUnsupportedFeature(CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
))
616 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< requesting power status of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
617 bReturn
= m_handler
->TransmitRequestPowerStatus(initiator
, m_iLogicalAddress
, bWaitForResponse
);
623 bool CCECBusDevice::TransmitPowerState(const cec_logical_address destination
)
625 cec_power_status state
;
627 CLockObject
lock(m_mutex
);
628 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): %s", GetLogicalAddressName(), m_iLogicalAddress
, ToString(destination
), destination
, ToString(m_powerStatus
));
629 state
= m_powerStatus
;
633 bool bReturn
= m_handler
->TransmitPowerState(m_iLogicalAddress
, destination
, state
);
638 cec_vendor_id
CCECBusDevice::GetCurrentVendorId(void)
640 CLockObject
lock(m_mutex
);
644 cec_vendor_id
CCECBusDevice::GetVendorId(const cec_logical_address initiator
, bool bUpdate
/* = false */)
646 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
647 bool bRequestUpdate(false);
649 CLockObject
lock(m_mutex
);
650 bRequestUpdate
= (bIsPresent
&&
651 (bUpdate
|| m_vendor
== CEC_VENDOR_UNKNOWN
));
655 RequestVendorId(initiator
);
657 CLockObject
lock(m_mutex
);
661 const char *CCECBusDevice::GetVendorName(const cec_logical_address initiator
, bool bUpdate
/* = false */)
663 return ToString(GetVendorId(initiator
, bUpdate
));
666 bool CCECBusDevice::SetVendorId(uint64_t iVendorId
)
668 bool bVendorChanged(false);
671 CLockObject
lock(m_mutex
);
672 bVendorChanged
= (m_vendor
!= (cec_vendor_id
)iVendorId
);
673 m_vendor
= (cec_vendor_id
)iVendorId
;
677 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): vendor = %s (%06x)", GetLogicalAddressName(), m_iLogicalAddress
, ToString(m_vendor
), m_vendor
);
679 return bVendorChanged
;
682 bool CCECBusDevice::RequestVendorId(const cec_logical_address initiator
, bool bWaitForResponse
/* = true */)
686 if (!IsHandledByLibCEC() && initiator
!= CECDEVICE_UNKNOWN
)
689 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< requesting vendor ID of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
690 bReturn
= m_handler
->TransmitRequestVendorId(initiator
, m_iLogicalAddress
, bWaitForResponse
);
693 if (bWaitForResponse
)
694 ReplaceHandler(true);
699 bool CCECBusDevice::TransmitVendorID(const cec_logical_address destination
, bool bSendAbort
/* = true */)
704 CLockObject
lock(m_mutex
);
705 iVendorId
= (uint64_t)m_vendor
;
709 if (iVendorId
== CEC_VENDOR_UNKNOWN
)
713 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): vendor id feature abort", GetLogicalAddressName(), m_iLogicalAddress
, ToString(destination
), destination
);
714 m_processor
->TransmitAbort(m_iLogicalAddress
, destination
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
720 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): vendor id %s (%x)", GetLogicalAddressName(), m_iLogicalAddress
, ToString(destination
), destination
, ToString((cec_vendor_id
)iVendorId
), iVendorId
);
721 bReturn
= m_handler
->TransmitVendorID(m_iLogicalAddress
, iVendorId
);
727 cec_bus_device_status
CCECBusDevice::GetStatus(bool bForcePoll
/* = false */, bool bSuppressPoll
/* = false */)
729 cec_bus_device_status
status(CEC_DEVICE_STATUS_UNKNOWN
);
730 bool bNeedsPoll(false);
733 CLockObject
lock(m_mutex
);
734 status
= m_deviceStatus
;
735 bNeedsPoll
= !bSuppressPoll
&&
736 (bForcePoll
|| m_deviceStatus
== CEC_DEVICE_STATUS_UNKNOWN
);
741 bool bPollAcked(false);
742 if (bNeedsPoll
&& NeedsPoll())
743 bPollAcked
= m_processor
->PollDevice(m_iLogicalAddress
);
745 status
= bPollAcked
? CEC_DEVICE_STATUS_PRESENT
: CEC_DEVICE_STATUS_NOT_PRESENT
;
746 SetDeviceStatus(status
);
752 void CCECBusDevice::SetDeviceStatus(const cec_bus_device_status newStatus
)
755 CLockObject
lock(m_mutex
);
758 case CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
:
759 if (m_deviceStatus
!= newStatus
)
760 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): device status changed into 'handled by libCEC'", GetLogicalAddressName(), m_iLogicalAddress
);
761 SetPowerStatus (CEC_POWER_STATUS_ON
);
762 SetVendorId (CEC_VENDOR_UNKNOWN
);
763 SetMenuState (CEC_MENU_STATE_ACTIVATED
);
764 SetCecVersion (CEC_VERSION_1_3A
);
765 SetStreamPath (CEC_INVALID_PHYSICAL_ADDRESS
);
766 MarkAsInactiveSource();
768 m_deviceStatus
= newStatus
;
770 case CEC_DEVICE_STATUS_PRESENT
:
771 if (m_deviceStatus
!= newStatus
)
772 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): device status changed into 'present'", GetLogicalAddressName(), m_iLogicalAddress
);
773 m_deviceStatus
= newStatus
;
775 case CEC_DEVICE_STATUS_NOT_PRESENT
:
776 if (m_deviceStatus
!= newStatus
)
778 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): device status changed into 'not present'", GetLogicalAddressName(), m_iLogicalAddress
);
780 m_deviceStatus
= newStatus
;
790 void CCECBusDevice::ResetDeviceStatus(void)
792 CLockObject
lock(m_mutex
);
793 SetPowerStatus (CEC_POWER_STATUS_UNKNOWN
);
794 SetVendorId (CEC_VENDOR_UNKNOWN
);
795 SetMenuState (CEC_MENU_STATE_ACTIVATED
);
796 SetCecVersion (CEC_VERSION_UNKNOWN
);
797 SetStreamPath (CEC_INVALID_PHYSICAL_ADDRESS
);
798 SetOSDName (ToString(m_iLogicalAddress
));
799 MarkAsInactiveSource();
802 m_bVendorIdRequested
= false;
803 m_unsupportedFeatures
.clear();
805 if (m_deviceStatus
!= CEC_DEVICE_STATUS_UNKNOWN
)
806 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): device status changed into 'unknown'", GetLogicalAddressName(), m_iLogicalAddress
);
807 m_deviceStatus
= CEC_DEVICE_STATUS_UNKNOWN
;
810 bool CCECBusDevice::TransmitPoll(const cec_logical_address dest
)
813 cec_logical_address
destination(dest
);
814 if (destination
== CECDEVICE_UNKNOWN
)
815 destination
= m_iLogicalAddress
;
817 CCECBusDevice
*destDevice
= m_processor
->GetDevice(destination
);
818 if (destDevice
->m_deviceStatus
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
)
822 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): POLL", GetLogicalAddressName(), m_iLogicalAddress
, ToString(dest
), dest
);
823 bReturn
= m_handler
->TransmitPoll(m_iLogicalAddress
, destination
);
824 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, bReturn
? ">> POLL sent" : ">> POLL not sent");
826 CLockObject
lock(m_mutex
);
829 m_iLastActive
= GetTimeMs();
830 destDevice
->m_deviceStatus
= CEC_DEVICE_STATUS_PRESENT
;
833 destDevice
->m_deviceStatus
= CEC_DEVICE_STATUS_NOT_PRESENT
;
839 void CCECBusDevice::HandlePoll(const cec_logical_address destination
)
841 if (destination
>= 0 && destination
< CECDEVICE_BROADCAST
)
843 CCECBusDevice
*device
= m_processor
->GetDevice(destination
);
845 device
->HandlePollFrom(m_iLogicalAddress
);
849 void CCECBusDevice::HandlePollFrom(const cec_logical_address initiator
)
851 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "<< POLL: %s (%x) -> %s (%x)", ToString(initiator
), initiator
, ToString(m_iLogicalAddress
), m_iLogicalAddress
);
852 m_bAwaitingReceiveFailed
= true;
855 bool CCECBusDevice::HandleReceiveFailed(void)
857 bool bReturn
= m_bAwaitingReceiveFailed
;
858 m_bAwaitingReceiveFailed
= false;
862 cec_menu_state
CCECBusDevice::GetMenuState(const cec_logical_address
UNUSED(initiator
))
864 CLockObject
lock(m_mutex
);
868 void CCECBusDevice::SetMenuState(const cec_menu_state state
)
870 CLockObject
lock(m_mutex
);
871 if (m_menuState
!= state
)
873 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): menu state set to '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(m_menuState
));
878 bool CCECBusDevice::TransmitMenuState(const cec_logical_address dest
)
880 cec_menu_state menuState
;
882 CLockObject
lock(m_mutex
);
883 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): menu state '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(dest
), dest
, ToString(m_menuState
));
884 menuState
= m_menuState
;
888 bool bReturn
= m_handler
->TransmitMenuState(m_iLogicalAddress
, dest
, menuState
);
893 bool CCECBusDevice::ActivateSource(void)
895 MarkAsActiveSource();
896 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "activating source '%s'", ToString(m_iLogicalAddress
));
898 bool bReturn
= m_handler
->ActivateSource();
903 bool CCECBusDevice::RequestActiveSource(bool bWaitForResponse
/* = true */)
907 if (IsHandledByLibCEC())
910 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< requesting active source");
912 bReturn
= m_handler
->TransmitRequestActiveSource(m_iLogicalAddress
, bWaitForResponse
);
918 void CCECBusDevice::MarkAsActiveSource(void)
920 bool bWasActivated(false);
922 // set the power status to powered on
923 SetPowerStatus(CEC_POWER_STATUS_ON
);
925 // mark this device as active source
927 CLockObject
lock(m_mutex
);
928 if (!m_bActiveSource
)
930 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "making %s (%x) the active source", GetLogicalAddressName(), m_iLogicalAddress
);
931 bWasActivated
= true;
934 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%x) was already marked as active source", GetLogicalAddressName(), m_iLogicalAddress
);
936 m_bActiveSource
= true;
939 // mark other devices as inactive sources
940 CECDEVICEVEC devices
;
941 m_processor
->GetDevices()->Get(devices
);
942 for (CECDEVICEVEC::iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
943 if ((*it
)->GetLogicalAddress() != m_iLogicalAddress
)
944 (*it
)->MarkAsInactiveSource();
948 CCECClient
*client
= GetClient();
950 client
->SourceActivated(m_iLogicalAddress
);
954 void CCECBusDevice::MarkAsInactiveSource(void)
956 bool bWasDeactivated(false);
958 CLockObject
lock(m_mutex
);
961 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "marking %s (%X) as inactive source", GetLogicalAddressName(), m_iLogicalAddress
);
962 bWasDeactivated
= true;
964 m_bActiveSource
= false;
969 CCECClient
*client
= GetClient();
971 client
->SourceDeactivated(m_iLogicalAddress
);
975 bool CCECBusDevice::TransmitActiveSource(void)
977 bool bSendActiveSource(false);
978 uint16_t iPhysicalAddress(CEC_INVALID_PHYSICAL_ADDRESS
);
981 CLockObject
lock(m_mutex
);
982 if (!HasValidPhysicalAddress())
984 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X) has an invalid physical address (%04x), not sending active source commands", GetLogicalAddressName(), m_iLogicalAddress
, m_iPhysicalAddress
);
988 iPhysicalAddress
= m_iPhysicalAddress
;
990 if (m_powerStatus
!= CEC_POWER_STATUS_ON
&& m_powerStatus
!= CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
)
991 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress
);
992 else if (m_bActiveSource
)
994 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> broadcast (F): active source (%4x)", GetLogicalAddressName(), m_iLogicalAddress
, m_iPhysicalAddress
);
995 bSendActiveSource
= true;
998 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "<< %s (%X) is not the active source", GetLogicalAddressName(), m_iLogicalAddress
);
1001 bool bActiveSourceSent(false);
1002 if (bSendActiveSource
)
1005 bActiveSourceSent
= m_handler
->TransmitActiveSource(m_iLogicalAddress
, iPhysicalAddress
);
1009 return bActiveSourceSent
;
1012 bool CCECBusDevice::TransmitImageViewOn(void)
1015 CLockObject
lock(m_mutex
);
1016 if (m_powerStatus
!= CEC_POWER_STATUS_ON
&& m_powerStatus
!= CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
)
1018 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress
);
1023 bool bImageViewOnSent(false);
1025 bImageViewOnSent
= m_handler
->TransmitImageViewOn(m_iLogicalAddress
, CECDEVICE_TV
);
1027 return bImageViewOnSent
;
1030 bool CCECBusDevice::TransmitInactiveSource(void)
1032 uint16_t iPhysicalAddress
;
1034 CLockObject
lock(m_mutex
);
1035 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> broadcast (F): inactive source", GetLogicalAddressName(), m_iLogicalAddress
);
1036 iPhysicalAddress
= m_iPhysicalAddress
;
1040 bool bReturn
= m_handler
->TransmitInactiveSource(m_iLogicalAddress
, iPhysicalAddress
);
1045 bool CCECBusDevice::TransmitPendingActiveSourceCommands(void)
1048 bool bReturn
= m_handler
->ActivateSource(true);
1053 void CCECBusDevice::SetStreamPath(uint16_t iNewAddress
, uint16_t iOldAddress
/* = CEC_INVALID_PHYSICAL_ADDRESS */)
1055 CLockObject
lock(m_mutex
);
1056 if (iNewAddress
!= m_iStreamPath
)
1058 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): stream path changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress
, iOldAddress
== 0 ? m_iStreamPath
: iOldAddress
, iNewAddress
);
1059 m_iStreamPath
= iNewAddress
;
1062 if (!LIB_CEC
->IsValidPhysicalAddress(iNewAddress
))
1065 CCECBusDevice
*device
= m_processor
->GetDeviceByPhysicalAddress(iNewAddress
);
1068 // if a device is found with the new physical address, mark it as active, which will automatically mark all other devices as inactive
1069 device
->MarkAsActiveSource();
1073 // try to find the device with the old address, and mark it as inactive when found
1074 device
= m_processor
->GetDeviceByPhysicalAddress(iOldAddress
);
1076 device
->MarkAsInactiveSource();
1080 bool CCECBusDevice::PowerOn(const cec_logical_address initiator
)
1082 bool bReturn(false);
1083 GetVendorId(initiator
); // ensure that we got the vendor id, because the implementations vary per vendor
1086 cec_power_status currentStatus
= GetPowerStatus(initiator
, false);
1087 if (currentStatus
!= CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
&&
1088 currentStatus
!= CEC_POWER_STATUS_ON
)
1090 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< powering on '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
1091 if (m_handler
->PowerOn(initiator
, m_iLogicalAddress
))
1093 SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
1099 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "'%s' (%X) is already '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(currentStatus
));
1106 bool CCECBusDevice::Standby(const cec_logical_address initiator
)
1108 GetVendorId(initiator
); // ensure that we got the vendor id, because the implementations vary per vendor
1110 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "<< putting '%s' (%X) in standby mode", GetLogicalAddressName(), m_iLogicalAddress
);
1112 bool bReturn
= m_handler
->TransmitStandby(initiator
, m_iLogicalAddress
);
1117 bool CCECBusDevice::NeedsPoll(void)
1119 bool bSendPoll(false);
1120 cec_logical_address
pollAddress(CECDEVICE_UNKNOWN
);
1121 switch (m_iLogicalAddress
)
1123 case CECDEVICE_PLAYBACKDEVICE3
:
1124 pollAddress
= CECDEVICE_PLAYBACKDEVICE2
;
1126 case CECDEVICE_PLAYBACKDEVICE2
:
1127 pollAddress
= CECDEVICE_PLAYBACKDEVICE1
;
1129 case CECDEVICE_RECORDINGDEVICE3
:
1130 pollAddress
= CECDEVICE_RECORDINGDEVICE2
;
1132 case CECDEVICE_RECORDINGDEVICE2
:
1133 pollAddress
= CECDEVICE_RECORDINGDEVICE1
;
1135 case CECDEVICE_TUNER4
:
1136 pollAddress
= CECDEVICE_TUNER3
;
1138 case CECDEVICE_TUNER3
:
1139 pollAddress
= CECDEVICE_TUNER2
;
1141 case CECDEVICE_TUNER2
:
1142 pollAddress
= CECDEVICE_TUNER1
;
1144 case CECDEVICE_AUDIOSYSTEM
:
1145 case CECDEVICE_PLAYBACKDEVICE1
:
1146 case CECDEVICE_RECORDINGDEVICE1
:
1147 case CECDEVICE_TUNER1
:
1155 if (!bSendPoll
&& pollAddress
!= CECDEVICE_UNKNOWN
)
1157 CCECBusDevice
*device
= m_processor
->GetDevice(pollAddress
);
1160 cec_bus_device_status status
= device
->GetStatus();
1161 bSendPoll
= (status
== CEC_DEVICE_STATUS_PRESENT
|| status
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
1172 void CCECBusDevice::CheckVendorIdRequested(const cec_logical_address initiator
)
1174 bool bRequestVendorId(false);
1176 CLockObject
lock(m_mutex
);
1177 bRequestVendorId
= !m_bVendorIdRequested
;
1178 m_bVendorIdRequested
= true;
1181 if (bRequestVendorId
)
1183 ReplaceHandler(false);
1184 GetVendorId(initiator
);
1189 CCECAudioSystem
*CCECBusDevice::AsAudioSystem(void)
1191 return AsAudioSystem(this);
1194 CCECPlaybackDevice
*CCECBusDevice::AsPlaybackDevice(void)
1196 return AsPlaybackDevice(this);
1199 CCECRecordingDevice
*CCECBusDevice::AsRecordingDevice(void)
1201 return AsRecordingDevice(this);
1204 CCECTuner
*CCECBusDevice::AsTuner(void)
1206 return AsTuner(this);
1209 CCECTV
*CCECBusDevice::AsTV(void)
1214 CCECAudioSystem
*CCECBusDevice::AsAudioSystem(CCECBusDevice
*device
)
1216 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
1217 return static_cast<CCECAudioSystem
*>(device
);
1221 CCECPlaybackDevice
*CCECBusDevice::AsPlaybackDevice(CCECBusDevice
*device
)
1224 (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
||
1225 device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
))
1226 return static_cast<CCECPlaybackDevice
*>(device
);
1230 CCECRecordingDevice
*CCECBusDevice::AsRecordingDevice(CCECBusDevice
*device
)
1232 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
1233 return static_cast<CCECRecordingDevice
*>(device
);
1237 CCECTuner
*CCECBusDevice::AsTuner(CCECBusDevice
*device
)
1239 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_TUNER
)
1240 return static_cast<CCECTuner
*>(device
);
1244 CCECTV
*CCECBusDevice::AsTV(CCECBusDevice
*device
)
1246 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_TV
)
1247 return static_cast<CCECTV
*>(device
);
1251 void CCECBusDevice::MarkBusy(void)
1253 CLockObject
handlerLock(m_handlerMutex
);
1254 ++m_iHandlerUseCount
;
1257 void CCECBusDevice::MarkReady(void)
1259 CLockObject
handlerLock(m_handlerMutex
);
1260 if (m_iHandlerUseCount
> 0)
1261 --m_iHandlerUseCount
;
1264 bool CCECBusDevice::TryLogicalAddress(void)
1266 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "trying logical address '%s'", GetLogicalAddressName());
1268 if (!TransmitPoll(m_iLogicalAddress
))
1270 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "using logical address '%s'", GetLogicalAddressName());
1271 SetDeviceStatus(CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
1276 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "logical address '%s' already taken", GetLogicalAddressName());
1277 SetDeviceStatus(CEC_DEVICE_STATUS_PRESENT
);
1281 CCECClient
*CCECBusDevice::GetClient(void)
1283 return m_processor
->GetClient(m_iLogicalAddress
);