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 "../implementations/ANCommandHandler.h"
36 #include "../implementations/CECCommandHandler.h"
37 #include "../implementations/SLCommandHandler.h"
38 #include "../implementations/VLCommandHandler.h"
39 #include "../LibCEC.h"
40 #include "../CECTypeUtils.h"
41 #include "../platform/util/timeutils.h"
42 #include "../platform/util/util.h"
44 #include "CECAudioSystem.h"
45 #include "CECPlaybackDevice.h"
46 #include "CECRecordingDevice.h"
52 using namespace PLATFORM
;
54 #define LIB_CEC m_processor->GetLib()
55 #define ToString(p) CCECTypeUtils::ToString(p)
57 CCECBusDevice::CCECBusDevice(CCECProcessor
*processor
, cec_logical_address iLogicalAddress
, uint16_t iPhysicalAddress
/* = CEC_INVALID_PHYSICAL_ADDRESS */) :
58 m_type (CEC_DEVICE_TYPE_RESERVED
),
59 m_iPhysicalAddress (iPhysicalAddress
),
60 m_iStreamPath (CEC_INVALID_PHYSICAL_ADDRESS
),
61 m_iLogicalAddress (iLogicalAddress
),
62 m_powerStatus (CEC_POWER_STATUS_UNKNOWN
),
63 m_processor (processor
),
64 m_vendor (CEC_VENDOR_UNKNOWN
),
65 m_bReplaceHandler (false),
66 m_menuState (CEC_MENU_STATE_ACTIVATED
),
67 m_bActiveSource (false),
69 m_iLastPowerStateUpdate (0),
70 m_cecVersion (CEC_VERSION_UNKNOWN
),
71 m_deviceStatus (CEC_DEVICE_STATUS_UNKNOWN
),
72 m_iHandlerUseCount (0),
73 m_bAwaitingReceiveFailed(false),
74 m_bVendorIdRequested (false)
76 m_handler
= new CCECCommandHandler(this);
78 for (unsigned int iPtr
= 0; iPtr
< 4; iPtr
++)
79 m_menuLanguage
.language
[iPtr
] = '?';
80 m_menuLanguage
.language
[3] = 0;
81 m_menuLanguage
.device
= iLogicalAddress
;
83 m_strDeviceName
= ToString(m_iLogicalAddress
);
86 CCECBusDevice::~CCECBusDevice(void)
88 DELETE_AND_NULL(m_handler
);
91 bool CCECBusDevice::ReplaceHandler(bool bActivateSource
/* = true */)
93 bool bInitHandler(false);
95 CTryLockObject
lock(m_mutex
);
99 CLockObject
handlerLock(m_handlerMutex
);
100 if (m_iHandlerUseCount
> 0)
105 if (m_vendor
!= m_handler
->GetVendorId())
107 if (CCECCommandHandler::HasSpecificHandler(m_vendor
))
109 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "replacing the command handler for device '%s' (%x)", GetLogicalAddressName(), GetLogicalAddress());
110 DELETE_AND_NULL(m_handler
);
114 case CEC_VENDOR_SAMSUNG
:
115 m_handler
= new CANCommandHandler(this);
118 m_handler
= new CSLCommandHandler(this);
120 case CEC_VENDOR_PANASONIC
:
121 m_handler
= new CVLCommandHandler(this);
124 m_handler
= new CCECCommandHandler(this);
128 m_handler
->SetVendorId(m_vendor
);
136 CCECBusDevice
*primary
= GetProcessor()->GetPrimaryDevice();
137 if (primary
->GetLogicalAddress() != CECDEVICE_UNREGISTERED
)
139 m_handler
->InitHandler();
141 if (bActivateSource
&& IsHandledByLibCEC() && IsActiveSource())
142 m_handler
->ActivateSource();
151 bool CCECBusDevice::HandleCommand(const cec_command
&command
)
153 bool bHandled(false);
155 /* update "last active" */
157 CLockObject
lock(m_mutex
);
158 m_iLastActive
= GetTimeMs();
160 /* don't call GetStatus() here, just read the value with the mutex locked */
161 if (m_deviceStatus
!= CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
&& command
.opcode_set
== 1)
162 m_deviceStatus
= CEC_DEVICE_STATUS_PRESENT
;
167 /* handle the command */
168 bHandled
= m_handler
->HandleCommand(command
);
170 /* change status to present */
171 if (bHandled
&& GetLogicalAddress() != CECDEVICE_BROADCAST
)
173 CLockObject
lock(m_mutex
);
174 if (m_deviceStatus
!= CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
)
176 if (m_deviceStatus
!= CEC_DEVICE_STATUS_PRESENT
)
177 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "device %s (%x) status changed to present after command %s", GetLogicalAddressName(), (uint8_t)GetLogicalAddress(), ToString(command
.opcode
));
178 m_deviceStatus
= CEC_DEVICE_STATUS_PRESENT
;
186 const char* CCECBusDevice::GetLogicalAddressName(void) const
188 return ToString(m_iLogicalAddress
);
191 bool CCECBusDevice::IsPresent(void)
193 CLockObject
lock(m_mutex
);
194 return m_deviceStatus
== CEC_DEVICE_STATUS_PRESENT
;
197 bool CCECBusDevice::IsHandledByLibCEC(void)
199 CLockObject
lock(m_mutex
);
200 return m_deviceStatus
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
;
203 void CCECBusDevice::SetUnsupportedFeature(cec_opcode opcode
)
205 // some commands should never be marked as unsupported
206 if (opcode
== CEC_OPCODE_VENDOR_COMMAND
||
207 opcode
== CEC_OPCODE_VENDOR_COMMAND_WITH_ID
||
208 opcode
== CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN
||
209 opcode
== CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP
||
210 opcode
== CEC_OPCODE_ABORT
||
211 opcode
== CEC_OPCODE_FEATURE_ABORT
||
212 opcode
== CEC_OPCODE_NONE
)
216 CLockObject
lock(m_mutex
);
217 if (m_unsupportedFeatures
.find(opcode
) == m_unsupportedFeatures
.end())
219 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "marking opcode '%s' as unsupported feature for device '%s'", ToString(opcode
), GetLogicalAddressName());
220 m_unsupportedFeatures
.insert(opcode
);
224 // signal threads that are waiting for a reponse
226 m_handler
->SignalOpcode(cec_command::GetResponseOpcode(opcode
));
230 bool CCECBusDevice::IsUnsupportedFeature(cec_opcode opcode
)
232 CLockObject
lock(m_mutex
);
233 bool bUnsupported
= (m_unsupportedFeatures
.find(opcode
) != m_unsupportedFeatures
.end());
235 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "'%s' is marked as unsupported feature for device '%s'", ToString(opcode
), GetLogicalAddressName());
239 bool CCECBusDevice::TransmitKeypress(const cec_logical_address initiator
, cec_user_control_code key
, bool bWait
/* = true */)
242 bool bReturn
= m_handler
->TransmitKeypress(initiator
, m_iLogicalAddress
, key
, bWait
);
247 bool CCECBusDevice::TransmitKeyRelease(const cec_logical_address initiator
, bool bWait
/* = true */)
250 bool bReturn
= m_handler
->TransmitKeyRelease(initiator
, m_iLogicalAddress
, bWait
);
255 cec_version
CCECBusDevice::GetCecVersion(const cec_logical_address initiator
, bool bUpdate
/* = false */)
257 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
258 bool bRequestUpdate(false);
260 CLockObject
lock(m_mutex
);
261 bRequestUpdate
= bIsPresent
&&
262 (bUpdate
|| m_cecVersion
== CEC_VERSION_UNKNOWN
);
267 CheckVendorIdRequested(initiator
);
268 RequestCecVersion(initiator
);
271 CLockObject
lock(m_mutex
);
275 void CCECBusDevice::SetCecVersion(const cec_version newVersion
)
277 CLockObject
lock(m_mutex
);
278 if (m_cecVersion
!= newVersion
)
279 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): CEC version %s", GetLogicalAddressName(), m_iLogicalAddress
, ToString(newVersion
));
280 m_cecVersion
= newVersion
;
283 bool CCECBusDevice::RequestCecVersion(const cec_logical_address initiator
, bool bWaitForResponse
/* = true */)
287 if (!IsHandledByLibCEC() &&
288 !IsUnsupportedFeature(CEC_OPCODE_GET_CEC_VERSION
))
291 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< requesting CEC version of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
292 bReturn
= m_handler
->TransmitRequestCecVersion(initiator
, m_iLogicalAddress
, bWaitForResponse
);
298 bool CCECBusDevice::TransmitCECVersion(const cec_logical_address destination
)
302 CLockObject
lock(m_mutex
);
303 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): cec version %s", GetLogicalAddressName(), m_iLogicalAddress
, ToString(destination
), destination
, ToString(m_cecVersion
));
304 version
= m_cecVersion
;
308 bool bReturn
= m_handler
->TransmitCECVersion(m_iLogicalAddress
, destination
, version
);
313 cec_menu_language
&CCECBusDevice::GetMenuLanguage(const cec_logical_address initiator
, bool bUpdate
/* = false */)
315 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
316 bool bRequestUpdate(false);
318 CLockObject
lock(m_mutex
);
319 bRequestUpdate
= (bIsPresent
&&
320 (bUpdate
|| !strcmp(m_menuLanguage
.language
, "???")));
325 CheckVendorIdRequested(initiator
);
326 RequestMenuLanguage(initiator
);
329 CLockObject
lock(m_mutex
);
330 return m_menuLanguage
;
333 void CCECBusDevice::SetMenuLanguage(const char *strLanguage
)
338 CLockObject
lock(m_mutex
);
339 if (strcmp(strLanguage
, m_menuLanguage
.language
))
341 memcpy(m_menuLanguage
.language
, strLanguage
, 3);
342 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): menu language set to '%s'", GetLogicalAddressName(), m_iLogicalAddress
, m_menuLanguage
.language
);
346 void CCECBusDevice::SetMenuLanguage(const cec_menu_language
&language
)
348 if (language
.device
== m_iLogicalAddress
)
349 SetMenuLanguage(language
.language
);
352 bool CCECBusDevice::RequestMenuLanguage(const cec_logical_address initiator
, bool bWaitForResponse
/* = true */)
356 if (!IsHandledByLibCEC() &&
357 !IsUnsupportedFeature(CEC_OPCODE_GET_MENU_LANGUAGE
))
360 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< requesting menu language of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
361 bReturn
= m_handler
->TransmitRequestMenuLanguage(initiator
, m_iLogicalAddress
, bWaitForResponse
);
367 bool CCECBusDevice::TransmitSetMenuLanguage(const cec_logical_address destination
)
370 cec_menu_language language
;
372 CLockObject
lock(m_mutex
);
373 language
= m_menuLanguage
;
378 CLockObject
lock(m_mutex
);
379 lang
[0] = language
.language
[0];
380 lang
[1] = language
.language
[1];
381 lang
[2] = language
.language
[2];
385 if (lang
[0] == '?' && lang
[1] == '?' && lang
[2] == '?')
387 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): Menu language feature abort", GetLogicalAddressName(), m_iLogicalAddress
, ToString(destination
), destination
);
388 m_processor
->TransmitAbort(m_iLogicalAddress
, destination
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
393 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> broadcast (F): Menu language '%s'", GetLogicalAddressName(), m_iLogicalAddress
, lang
);
394 bReturn
= m_handler
->TransmitSetMenuLanguage(m_iLogicalAddress
, lang
);
400 bool CCECBusDevice::TransmitOSDString(const cec_logical_address destination
, cec_display_control duration
, const char *strMessage
)
403 if (!m_processor
->GetDevice(destination
)->IsUnsupportedFeature(CEC_OPCODE_SET_OSD_STRING
))
405 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): display OSD message '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(destination
), destination
, strMessage
);
407 bReturn
= m_handler
->TransmitOSDString(m_iLogicalAddress
, destination
, duration
, strMessage
);
413 CStdString
CCECBusDevice::GetCurrentOSDName(void)
415 CLockObject
lock(m_mutex
);
416 return m_strDeviceName
;
419 CStdString
CCECBusDevice::GetOSDName(const cec_logical_address initiator
, bool bUpdate
/* = false */)
421 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
422 bool bRequestUpdate(false);
424 CLockObject
lock(m_mutex
);
425 bRequestUpdate
= bIsPresent
&&
426 (bUpdate
|| m_strDeviceName
.Equals(ToString(m_iLogicalAddress
))) &&
427 m_type
!= CEC_DEVICE_TYPE_TV
;
432 CheckVendorIdRequested(initiator
);
433 RequestOSDName(initiator
);
436 CLockObject
lock(m_mutex
);
437 return m_strDeviceName
;
440 void CCECBusDevice::SetOSDName(CStdString strName
)
442 CLockObject
lock(m_mutex
);
443 if (m_strDeviceName
!= strName
)
445 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): osd name set to '%s'", GetLogicalAddressName(), m_iLogicalAddress
, strName
.c_str());
446 m_strDeviceName
= strName
;
450 bool CCECBusDevice::RequestOSDName(const cec_logical_address initiator
, bool bWaitForResponse
/* = true */)
454 if (!IsHandledByLibCEC() &&
455 !IsUnsupportedFeature(CEC_OPCODE_GIVE_OSD_NAME
))
458 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< requesting OSD name of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
459 bReturn
= m_handler
->TransmitRequestOSDName(initiator
, m_iLogicalAddress
, bWaitForResponse
);
465 bool CCECBusDevice::TransmitOSDName(const cec_logical_address destination
)
467 CStdString strDeviceName
;
469 CLockObject
lock(m_mutex
);
470 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): OSD name '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(destination
), destination
, m_strDeviceName
.c_str());
471 strDeviceName
= m_strDeviceName
;
475 bool bReturn
= m_handler
->TransmitOSDName(m_iLogicalAddress
, destination
, strDeviceName
);
480 bool CCECBusDevice::HasValidPhysicalAddress(void)
482 CLockObject
lock(m_mutex
);
483 return CLibCEC::IsValidPhysicalAddress(m_iPhysicalAddress
);
486 uint16_t CCECBusDevice::GetCurrentPhysicalAddress(void)
488 CLockObject
lock(m_mutex
);
489 return m_iPhysicalAddress
;
492 uint16_t CCECBusDevice::GetPhysicalAddress(const cec_logical_address initiator
, bool bSuppressUpdate
/* = false */)
494 if (!bSuppressUpdate
)
496 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
497 bool bRequestUpdate(false);
499 CLockObject
lock(m_mutex
);
500 bRequestUpdate
= bIsPresent
&& m_iPhysicalAddress
== CEC_INVALID_PHYSICAL_ADDRESS
;
505 CheckVendorIdRequested(initiator
);
506 if (!RequestPhysicalAddress(initiator
))
507 LIB_CEC
->AddLog(CEC_LOG_ERROR
, "failed to request the physical address");
511 CLockObject
lock(m_mutex
);
512 return m_iPhysicalAddress
;
515 bool CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress
)
517 CLockObject
lock(m_mutex
);
518 if (iNewAddress
> 0 && m_iPhysicalAddress
!= iNewAddress
)
520 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): physical address changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress
, m_iPhysicalAddress
, iNewAddress
);
521 m_iPhysicalAddress
= iNewAddress
;
526 bool CCECBusDevice::RequestPhysicalAddress(const cec_logical_address initiator
, bool bWaitForResponse
/* = true */)
530 if (!IsHandledByLibCEC())
533 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< requesting physical address of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
534 bReturn
= m_handler
->TransmitRequestPhysicalAddress(initiator
, m_iLogicalAddress
, bWaitForResponse
);
540 bool CCECBusDevice::TransmitPhysicalAddress(void)
542 uint16_t iPhysicalAddress
;
543 cec_device_type type
;
545 CLockObject
lock(m_mutex
);
546 if (m_iPhysicalAddress
== CEC_INVALID_PHYSICAL_ADDRESS
)
549 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> broadcast (F): physical adddress %4x", GetLogicalAddressName(), m_iLogicalAddress
, m_iPhysicalAddress
);
550 iPhysicalAddress
= m_iPhysicalAddress
;
555 bool bReturn
= m_handler
->TransmitPhysicalAddress(m_iLogicalAddress
, iPhysicalAddress
, type
);
560 cec_power_status
CCECBusDevice::GetCurrentPowerStatus(void)
562 CLockObject
lock(m_mutex
);
563 return m_powerStatus
;
566 cec_power_status
CCECBusDevice::GetPowerStatus(const cec_logical_address initiator
, bool bUpdate
/* = false */)
568 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
569 bool bRequestUpdate(false);
571 CLockObject
lock(m_mutex
);
572 bRequestUpdate
= (bIsPresent
&&
573 (bUpdate
|| m_powerStatus
== CEC_POWER_STATUS_UNKNOWN
||
574 m_powerStatus
== CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
||
575 m_powerStatus
== CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY
||
576 GetTimeMs() - m_iLastPowerStateUpdate
>= CEC_POWER_STATE_REFRESH_TIME
));
581 CheckVendorIdRequested(initiator
);
582 RequestPowerStatus(initiator
);
585 CLockObject
lock(m_mutex
);
586 return m_powerStatus
;
589 void CCECBusDevice::SetPowerStatus(const cec_power_status powerStatus
)
591 CLockObject
lock(m_mutex
);
592 if (m_powerStatus
!= powerStatus
)
594 m_iLastPowerStateUpdate
= GetTimeMs();
595 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): power status changed from '%s' to '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(m_powerStatus
), ToString(powerStatus
));
596 m_powerStatus
= powerStatus
;
600 bool CCECBusDevice::RequestPowerStatus(const cec_logical_address initiator
, bool bWaitForResponse
/* = true */)
604 if (!IsHandledByLibCEC() &&
605 !IsUnsupportedFeature(CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
))
608 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< requesting power status of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
609 bReturn
= m_handler
->TransmitRequestPowerStatus(initiator
, m_iLogicalAddress
, bWaitForResponse
);
615 bool CCECBusDevice::TransmitPowerState(const cec_logical_address destination
)
617 cec_power_status state
;
619 CLockObject
lock(m_mutex
);
620 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): %s", GetLogicalAddressName(), m_iLogicalAddress
, ToString(destination
), destination
, ToString(m_powerStatus
));
621 state
= m_powerStatus
;
625 bool bReturn
= m_handler
->TransmitPowerState(m_iLogicalAddress
, destination
, state
);
630 cec_vendor_id
CCECBusDevice::GetCurrentVendorId(void)
632 CLockObject
lock(m_mutex
);
636 cec_vendor_id
CCECBusDevice::GetVendorId(const cec_logical_address initiator
, bool bUpdate
/* = false */)
638 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
639 bool bRequestUpdate(false);
641 CLockObject
lock(m_mutex
);
642 bRequestUpdate
= (bIsPresent
&&
643 (bUpdate
|| m_vendor
== CEC_VENDOR_UNKNOWN
));
647 RequestVendorId(initiator
);
649 CLockObject
lock(m_mutex
);
653 const char *CCECBusDevice::GetVendorName(const cec_logical_address initiator
, bool bUpdate
/* = false */)
655 return ToString(GetVendorId(initiator
, bUpdate
));
658 bool CCECBusDevice::SetVendorId(uint64_t iVendorId
)
660 bool bVendorChanged(false);
663 CLockObject
lock(m_mutex
);
664 bVendorChanged
= (m_vendor
!= (cec_vendor_id
)iVendorId
);
665 m_vendor
= (cec_vendor_id
)iVendorId
;
669 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): vendor = %s (%06x)", GetLogicalAddressName(), m_iLogicalAddress
, ToString(m_vendor
), m_vendor
);
671 return bVendorChanged
;
674 bool CCECBusDevice::RequestVendorId(const cec_logical_address initiator
, bool bWaitForResponse
/* = true */)
678 if (!IsHandledByLibCEC() && initiator
!= CECDEVICE_UNKNOWN
)
681 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< requesting vendor ID of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
682 bReturn
= m_handler
->TransmitRequestVendorId(initiator
, m_iLogicalAddress
, bWaitForResponse
);
685 if (bWaitForResponse
)
686 ReplaceHandler(true);
691 bool CCECBusDevice::TransmitVendorID(const cec_logical_address destination
, bool bSendAbort
/* = true */)
696 CLockObject
lock(m_mutex
);
697 iVendorId
= (uint64_t)m_vendor
;
701 if (iVendorId
== CEC_VENDOR_UNKNOWN
)
705 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): vendor id feature abort", GetLogicalAddressName(), m_iLogicalAddress
, ToString(destination
), destination
);
706 m_processor
->TransmitAbort(m_iLogicalAddress
, destination
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
712 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
);
713 bReturn
= m_handler
->TransmitVendorID(m_iLogicalAddress
, iVendorId
);
719 cec_bus_device_status
CCECBusDevice::GetStatus(bool bForcePoll
/* = false */, bool bSuppressPoll
/* = false */)
721 cec_bus_device_status
status(CEC_DEVICE_STATUS_UNKNOWN
);
722 bool bNeedsPoll(false);
725 CLockObject
lock(m_mutex
);
726 status
= m_deviceStatus
;
727 bNeedsPoll
= !bSuppressPoll
&&
728 (bForcePoll
|| m_deviceStatus
== CEC_DEVICE_STATUS_UNKNOWN
);
733 bool bPollAcked(false);
734 if (bNeedsPoll
&& NeedsPoll())
735 bPollAcked
= m_processor
->PollDevice(m_iLogicalAddress
);
737 status
= bPollAcked
? CEC_DEVICE_STATUS_PRESENT
: CEC_DEVICE_STATUS_NOT_PRESENT
;
738 SetDeviceStatus(status
);
744 void CCECBusDevice::SetDeviceStatus(const cec_bus_device_status newStatus
)
747 CLockObject
lock(m_mutex
);
750 case CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
:
751 if (m_deviceStatus
!= newStatus
)
752 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): device status changed into 'handled by libCEC'", GetLogicalAddressName(), m_iLogicalAddress
);
753 SetPowerStatus (CEC_POWER_STATUS_ON
);
754 SetVendorId (CEC_VENDOR_UNKNOWN
);
755 SetMenuState (CEC_MENU_STATE_ACTIVATED
);
756 SetCecVersion (CEC_VERSION_1_3A
);
757 SetStreamPath (CEC_INVALID_PHYSICAL_ADDRESS
);
758 MarkAsInactiveSource();
760 m_deviceStatus
= newStatus
;
762 case CEC_DEVICE_STATUS_PRESENT
:
763 if (m_deviceStatus
!= newStatus
)
764 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): device status changed into 'present'", GetLogicalAddressName(), m_iLogicalAddress
);
765 m_deviceStatus
= newStatus
;
767 case CEC_DEVICE_STATUS_NOT_PRESENT
:
768 if (m_deviceStatus
!= newStatus
)
770 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): device status changed into 'not present'", GetLogicalAddressName(), m_iLogicalAddress
);
772 m_deviceStatus
= newStatus
;
782 void CCECBusDevice::ResetDeviceStatus(void)
784 CLockObject
lock(m_mutex
);
785 SetPowerStatus (CEC_POWER_STATUS_UNKNOWN
);
786 SetVendorId (CEC_VENDOR_UNKNOWN
);
787 SetMenuState (CEC_MENU_STATE_ACTIVATED
);
788 SetCecVersion (CEC_VERSION_UNKNOWN
);
789 SetStreamPath (CEC_INVALID_PHYSICAL_ADDRESS
);
790 SetOSDName (ToString(m_iLogicalAddress
));
791 MarkAsInactiveSource();
794 m_bVendorIdRequested
= false;
795 m_unsupportedFeatures
.clear();
797 if (m_deviceStatus
!= CEC_DEVICE_STATUS_UNKNOWN
)
798 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): device status changed into 'unknown'", GetLogicalAddressName(), m_iLogicalAddress
);
799 m_deviceStatus
= CEC_DEVICE_STATUS_UNKNOWN
;
802 bool CCECBusDevice::TransmitPoll(const cec_logical_address dest
)
805 cec_logical_address
destination(dest
);
806 if (destination
== CECDEVICE_UNKNOWN
)
807 destination
= m_iLogicalAddress
;
809 CCECBusDevice
*destDevice
= m_processor
->GetDevice(destination
);
810 if (destDevice
->m_deviceStatus
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
)
814 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): POLL", GetLogicalAddressName(), m_iLogicalAddress
, ToString(dest
), dest
);
815 bReturn
= m_handler
->TransmitPoll(m_iLogicalAddress
, destination
);
816 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, bReturn
? ">> POLL sent" : ">> POLL not sent");
818 CLockObject
lock(m_mutex
);
821 m_iLastActive
= GetTimeMs();
822 destDevice
->m_deviceStatus
= CEC_DEVICE_STATUS_PRESENT
;
825 destDevice
->m_deviceStatus
= CEC_DEVICE_STATUS_NOT_PRESENT
;
831 void CCECBusDevice::HandlePoll(const cec_logical_address destination
)
833 if (destination
>= 0 && destination
< CECDEVICE_BROADCAST
)
835 CCECBusDevice
*device
= m_processor
->GetDevice(destination
);
837 device
->HandlePollFrom(m_iLogicalAddress
);
841 void CCECBusDevice::HandlePollFrom(const cec_logical_address initiator
)
843 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "<< POLL: %s (%x) -> %s (%x)", ToString(initiator
), initiator
, ToString(m_iLogicalAddress
), m_iLogicalAddress
);
844 m_bAwaitingReceiveFailed
= true;
847 bool CCECBusDevice::HandleReceiveFailed(void)
849 bool bReturn
= m_bAwaitingReceiveFailed
;
850 m_bAwaitingReceiveFailed
= false;
854 cec_menu_state
CCECBusDevice::GetMenuState(const cec_logical_address
UNUSED(initiator
))
856 CLockObject
lock(m_mutex
);
860 void CCECBusDevice::SetMenuState(const cec_menu_state state
)
862 CLockObject
lock(m_mutex
);
863 if (m_menuState
!= state
)
865 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): menu state set to '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(m_menuState
));
870 bool CCECBusDevice::TransmitMenuState(const cec_logical_address dest
)
872 cec_menu_state menuState
;
874 CLockObject
lock(m_mutex
);
875 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): menu state '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(dest
), dest
, ToString(m_menuState
));
876 menuState
= m_menuState
;
880 bool bReturn
= m_handler
->TransmitMenuState(m_iLogicalAddress
, dest
, menuState
);
885 bool CCECBusDevice::ActivateSource(void)
887 MarkAsActiveSource();
888 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "activating source '%s'", ToString(m_iLogicalAddress
));
890 bool bReturn
= m_handler
->ActivateSource();
895 bool CCECBusDevice::RequestActiveSource(bool bWaitForResponse
/* = true */)
899 if (IsHandledByLibCEC())
902 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< requesting active source");
904 bReturn
= m_handler
->TransmitRequestActiveSource(m_iLogicalAddress
, bWaitForResponse
);
910 void CCECBusDevice::MarkAsActiveSource(void)
912 CLockObject
lock(m_mutex
);
913 if (!m_bActiveSource
)
914 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "making %s (%x) the active source", GetLogicalAddressName(), m_iLogicalAddress
);
916 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%x) was already marked as active source", GetLogicalAddressName(), m_iLogicalAddress
);
918 CECDEVICEVEC devices
;
919 m_processor
->GetDevices()->Get(devices
);
920 for (CECDEVICEVEC::iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
921 if ((*it
)->GetLogicalAddress() != m_iLogicalAddress
)
922 (*it
)->MarkAsInactiveSource();
924 m_bActiveSource
= true;
925 SetPowerStatus(CEC_POWER_STATUS_ON
);
928 void CCECBusDevice::MarkAsInactiveSource(void)
931 CLockObject
lock(m_mutex
);
933 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "marking %s (%X) as inactive source", GetLogicalAddressName(), m_iLogicalAddress
);
934 m_bActiveSource
= false;
938 bool CCECBusDevice::TransmitActiveSource(void)
940 bool bSendActiveSource(false);
943 CLockObject
lock(m_mutex
);
944 if (m_powerStatus
!= CEC_POWER_STATUS_ON
&& m_powerStatus
!= CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
)
945 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress
);
946 else if (m_bActiveSource
)
948 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> broadcast (F): active source (%4x)", GetLogicalAddressName(), m_iLogicalAddress
, m_iPhysicalAddress
);
949 bSendActiveSource
= true;
952 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "<< %s (%X) is not the active source", GetLogicalAddressName(), m_iLogicalAddress
);
955 bool bActiveSourceSent(false);
956 if (bSendActiveSource
)
959 bActiveSourceSent
= m_handler
->TransmitActiveSource(m_iLogicalAddress
, m_iPhysicalAddress
);
963 return bActiveSourceSent
;
966 bool CCECBusDevice::TransmitImageViewOn(void)
969 CLockObject
lock(m_mutex
);
970 if (m_powerStatus
!= CEC_POWER_STATUS_ON
&& m_powerStatus
!= CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
)
972 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress
);
977 bool bImageViewOnSent(false);
979 bImageViewOnSent
= m_handler
->TransmitImageViewOn(m_iLogicalAddress
, CECDEVICE_TV
);
981 return bImageViewOnSent
;
984 bool CCECBusDevice::TransmitInactiveSource(void)
986 uint16_t iPhysicalAddress
;
988 CLockObject
lock(m_mutex
);
989 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> broadcast (F): inactive source", GetLogicalAddressName(), m_iLogicalAddress
);
990 iPhysicalAddress
= m_iPhysicalAddress
;
994 bool bReturn
= m_handler
->TransmitInactiveSource(m_iLogicalAddress
, iPhysicalAddress
);
999 bool CCECBusDevice::TransmitPendingActiveSourceCommands(void)
1002 bool bReturn
= m_handler
->TransmitPendingActiveSourceCommands();
1007 void CCECBusDevice::SetStreamPath(uint16_t iNewAddress
, uint16_t iOldAddress
/* = CEC_INVALID_PHYSICAL_ADDRESS */)
1009 CLockObject
lock(m_mutex
);
1010 if (iNewAddress
!= m_iStreamPath
)
1012 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): stream path changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress
, iOldAddress
== 0 ? m_iStreamPath
: iOldAddress
, iNewAddress
);
1013 m_iStreamPath
= iNewAddress
;
1016 if (!LIB_CEC
->IsValidPhysicalAddress(iNewAddress
))
1019 CCECBusDevice
*device
= m_processor
->GetDeviceByPhysicalAddress(iNewAddress
);
1022 // if a device is found with the new physical address, mark it as active, which will automatically mark all other devices as inactive
1023 device
->MarkAsActiveSource();
1027 // try to find the device with the old address, and mark it as inactive when found
1028 device
= m_processor
->GetDeviceByPhysicalAddress(iOldAddress
);
1030 device
->MarkAsInactiveSource();
1034 bool CCECBusDevice::PowerOn(const cec_logical_address initiator
)
1036 bool bReturn(false);
1037 GetVendorId(initiator
); // ensure that we got the vendor id, because the implementations vary per vendor
1040 cec_power_status currentStatus
= GetPowerStatus(initiator
, false);
1041 if (currentStatus
!= CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
&&
1042 currentStatus
!= CEC_POWER_STATUS_ON
)
1044 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< powering on '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
1045 if (m_handler
->PowerOn(initiator
, m_iLogicalAddress
))
1047 SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
1053 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "'%s' (%X) is already '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(currentStatus
));
1060 bool CCECBusDevice::Standby(const cec_logical_address initiator
)
1062 GetVendorId(initiator
); // ensure that we got the vendor id, because the implementations vary per vendor
1064 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "<< putting '%s' (%X) in standby mode", GetLogicalAddressName(), m_iLogicalAddress
);
1066 bool bReturn
= m_handler
->TransmitStandby(initiator
, m_iLogicalAddress
);
1071 bool CCECBusDevice::NeedsPoll(void)
1073 bool bSendPoll(false);
1074 cec_logical_address
pollAddress(CECDEVICE_UNKNOWN
);
1075 switch (m_iLogicalAddress
)
1077 case CECDEVICE_PLAYBACKDEVICE3
:
1078 pollAddress
= CECDEVICE_PLAYBACKDEVICE2
;
1080 case CECDEVICE_PLAYBACKDEVICE2
:
1081 pollAddress
= CECDEVICE_PLAYBACKDEVICE1
;
1083 case CECDEVICE_RECORDINGDEVICE3
:
1084 pollAddress
= CECDEVICE_RECORDINGDEVICE2
;
1086 case CECDEVICE_RECORDINGDEVICE2
:
1087 pollAddress
= CECDEVICE_RECORDINGDEVICE1
;
1089 case CECDEVICE_TUNER4
:
1090 pollAddress
= CECDEVICE_TUNER3
;
1092 case CECDEVICE_TUNER3
:
1093 pollAddress
= CECDEVICE_TUNER2
;
1095 case CECDEVICE_TUNER2
:
1096 pollAddress
= CECDEVICE_TUNER1
;
1098 case CECDEVICE_AUDIOSYSTEM
:
1099 case CECDEVICE_PLAYBACKDEVICE1
:
1100 case CECDEVICE_RECORDINGDEVICE1
:
1101 case CECDEVICE_TUNER1
:
1109 if (!bSendPoll
&& pollAddress
!= CECDEVICE_UNKNOWN
)
1111 CCECBusDevice
*device
= m_processor
->GetDevice(pollAddress
);
1114 cec_bus_device_status status
= device
->GetStatus();
1115 bSendPoll
= (status
== CEC_DEVICE_STATUS_PRESENT
|| status
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
1126 void CCECBusDevice::CheckVendorIdRequested(const cec_logical_address initiator
)
1128 bool bRequestVendorId(false);
1130 CLockObject
lock(m_mutex
);
1131 bRequestVendorId
= !m_bVendorIdRequested
;
1132 m_bVendorIdRequested
= true;
1135 if (bRequestVendorId
)
1137 ReplaceHandler(false);
1138 GetVendorId(initiator
);
1143 CCECAudioSystem
*CCECBusDevice::AsAudioSystem(void)
1145 return AsAudioSystem(this);
1148 CCECPlaybackDevice
*CCECBusDevice::AsPlaybackDevice(void)
1150 return AsPlaybackDevice(this);
1153 CCECRecordingDevice
*CCECBusDevice::AsRecordingDevice(void)
1155 return AsRecordingDevice(this);
1158 CCECTuner
*CCECBusDevice::AsTuner(void)
1160 return AsTuner(this);
1163 CCECTV
*CCECBusDevice::AsTV(void)
1168 CCECAudioSystem
*CCECBusDevice::AsAudioSystem(CCECBusDevice
*device
)
1170 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
1171 return static_cast<CCECAudioSystem
*>(device
);
1175 CCECPlaybackDevice
*CCECBusDevice::AsPlaybackDevice(CCECBusDevice
*device
)
1178 (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
||
1179 device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
))
1180 return static_cast<CCECPlaybackDevice
*>(device
);
1184 CCECRecordingDevice
*CCECBusDevice::AsRecordingDevice(CCECBusDevice
*device
)
1186 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
1187 return static_cast<CCECRecordingDevice
*>(device
);
1191 CCECTuner
*CCECBusDevice::AsTuner(CCECBusDevice
*device
)
1193 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_TUNER
)
1194 return static_cast<CCECTuner
*>(device
);
1198 CCECTV
*CCECBusDevice::AsTV(CCECBusDevice
*device
)
1200 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_TV
)
1201 return static_cast<CCECTV
*>(device
);
1205 void CCECBusDevice::MarkBusy(void)
1207 CLockObject
handlerLock(m_handlerMutex
);
1208 ++m_iHandlerUseCount
;
1211 void CCECBusDevice::MarkReady(void)
1213 CLockObject
handlerLock(m_handlerMutex
);
1214 if (m_iHandlerUseCount
> 0)
1215 --m_iHandlerUseCount
;
1218 bool CCECBusDevice::TryLogicalAddress(void)
1220 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "trying logical address '%s'", GetLogicalAddressName());
1222 if (!TransmitPoll(m_iLogicalAddress
))
1224 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "using logical address '%s'", GetLogicalAddressName());
1225 SetDeviceStatus(CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
1230 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "logical address '%s' already taken", GetLogicalAddressName());
1231 SetDeviceStatus(CEC_DEVICE_STATUS_PRESENT
);
1235 CCECClient
*CCECBusDevice::GetClient(void)
1237 return m_processor
->GetClient(m_iLogicalAddress
);