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 "../platform/util/timeutils.h"
42 #include "CECAudioSystem.h"
43 #include "CECPlaybackDevice.h"
44 #include "CECRecordingDevice.h"
50 using namespace PLATFORM
;
52 #define LIB_CEC m_processor->GetLib()
53 #define ToString(p) LIB_CEC->ToString(p)
55 CCECBusDevice::CCECBusDevice(CCECProcessor
*processor
, cec_logical_address iLogicalAddress
, uint16_t iPhysicalAddress
/* = CEC_INVALID_PHYSICAL_ADDRESS */) :
56 m_type (CEC_DEVICE_TYPE_RESERVED
),
57 m_iPhysicalAddress (iPhysicalAddress
),
58 m_iStreamPath (CEC_INVALID_PHYSICAL_ADDRESS
),
59 m_iLogicalAddress (iLogicalAddress
),
60 m_powerStatus (CEC_POWER_STATUS_UNKNOWN
),
61 m_processor (processor
),
62 m_vendor (CEC_VENDOR_UNKNOWN
),
63 m_bReplaceHandler (false),
64 m_menuState (CEC_MENU_STATE_ACTIVATED
),
65 m_bActiveSource (false),
67 m_iLastPowerStateUpdate (0),
68 m_cecVersion (CEC_VERSION_UNKNOWN
),
69 m_deviceStatus (CEC_DEVICE_STATUS_UNKNOWN
),
70 m_iHandlerUseCount (0),
71 m_bAwaitingReceiveFailed(false),
72 m_bVendorIdRequested (false)
74 m_handler
= new CCECCommandHandler(this);
76 for (unsigned int iPtr
= 0; iPtr
< 4; iPtr
++)
77 m_menuLanguage
.language
[iPtr
] = '?';
78 m_menuLanguage
.language
[3] = 0;
79 m_menuLanguage
.device
= iLogicalAddress
;
81 m_strDeviceName
= ToString(m_iLogicalAddress
);
84 CCECBusDevice::~CCECBusDevice(void)
89 bool CCECBusDevice::ReplaceHandler(bool bActivateSource
/* = true */)
91 bool bInitHandler(false);
93 CTryLockObject
lock(m_mutex
);
97 CLockObject
handlerLock(m_handlerMutex
);
98 if (m_iHandlerUseCount
> 0)
103 if (m_vendor
!= m_handler
->GetVendorId())
105 if (CCECCommandHandler::HasSpecificHandler(m_vendor
))
107 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "replacing the command handler for device '%s' (%x)", GetLogicalAddressName(), GetLogicalAddress());
112 case CEC_VENDOR_SAMSUNG
:
113 m_handler
= new CANCommandHandler(this);
116 m_handler
= new CSLCommandHandler(this);
118 case CEC_VENDOR_PANASONIC
:
119 m_handler
= new CVLCommandHandler(this);
122 m_handler
= new CCECCommandHandler(this);
126 m_handler
->SetVendorId(m_vendor
);
134 m_handler
->InitHandler();
136 if (bActivateSource
&& IsHandledByLibCEC() && IsActiveSource())
137 m_handler
->ActivateSource();
145 bool CCECBusDevice::HandleCommand(const cec_command
&command
)
147 bool bHandled(false);
149 /* update "last active" */
151 CLockObject
lock(m_mutex
);
152 m_iLastActive
= GetTimeMs();
154 /* don't call GetStatus() here, just read the value with the mutex locked */
155 if (m_deviceStatus
!= CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
&& command
.opcode_set
== 1)
156 m_deviceStatus
= CEC_DEVICE_STATUS_PRESENT
;
161 /* handle the command */
162 bHandled
= m_handler
->HandleCommand(command
);
164 /* change status to present */
165 if (bHandled
&& GetLogicalAddress() != CECDEVICE_BROADCAST
)
167 CLockObject
lock(m_mutex
);
168 if (m_deviceStatus
!= CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
)
170 if (m_deviceStatus
!= CEC_DEVICE_STATUS_PRESENT
)
171 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "device %s (%x) status changed to present after command %s", GetLogicalAddressName(), (uint8_t)GetLogicalAddress(), ToString(command
.opcode
));
172 m_deviceStatus
= CEC_DEVICE_STATUS_PRESENT
;
180 const char* CCECBusDevice::GetLogicalAddressName(void) const
182 return ToString(m_iLogicalAddress
);
185 bool CCECBusDevice::IsPresent(void)
187 CLockObject
lock(m_mutex
);
188 return m_deviceStatus
== CEC_DEVICE_STATUS_PRESENT
;
191 bool CCECBusDevice::IsHandledByLibCEC(void)
193 CLockObject
lock(m_mutex
);
194 return m_deviceStatus
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
;
197 void CCECBusDevice::SetUnsupportedFeature(cec_opcode opcode
)
199 // some commands should never be marked as unsupported
200 if (opcode
== CEC_OPCODE_VENDOR_COMMAND
||
201 opcode
== CEC_OPCODE_VENDOR_COMMAND_WITH_ID
||
202 opcode
== CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN
||
203 opcode
== CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP
||
204 opcode
== CEC_OPCODE_ABORT
||
205 opcode
== CEC_OPCODE_FEATURE_ABORT
||
206 opcode
== CEC_OPCODE_NONE
)
210 CLockObject
lock(m_mutex
);
211 if (m_unsupportedFeatures
.find(opcode
) == m_unsupportedFeatures
.end())
213 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "marking opcode '%s' as unsupported feature for device '%s'", ToString(opcode
), GetLogicalAddressName());
214 m_unsupportedFeatures
.insert(opcode
);
218 // signal threads that are waiting for a reponse
220 m_handler
->SignalOpcode(cec_command::GetResponseOpcode(opcode
));
224 bool CCECBusDevice::IsUnsupportedFeature(cec_opcode opcode
)
226 CLockObject
lock(m_mutex
);
227 bool bUnsupported
= (m_unsupportedFeatures
.find(opcode
) != m_unsupportedFeatures
.end());
229 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "'%s' is marked as unsupported feature for device '%s'", ToString(opcode
), GetLogicalAddressName());
233 bool CCECBusDevice::TransmitKeypress(const cec_logical_address initiator
, cec_user_control_code key
, bool bWait
/* = true */)
236 bool bReturn
= m_handler
->TransmitKeypress(initiator
, m_iLogicalAddress
, key
, bWait
);
241 bool CCECBusDevice::TransmitKeyRelease(const cec_logical_address initiator
, bool bWait
/* = true */)
244 bool bReturn
= m_handler
->TransmitKeyRelease(initiator
, m_iLogicalAddress
, bWait
);
249 cec_version
CCECBusDevice::GetCecVersion(const cec_logical_address initiator
, bool bUpdate
/* = false */)
251 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
252 bool bRequestUpdate(false);
254 CLockObject
lock(m_mutex
);
255 bRequestUpdate
= bIsPresent
&&
256 (bUpdate
|| m_cecVersion
== CEC_VERSION_UNKNOWN
);
261 CheckVendorIdRequested(initiator
);
262 RequestCecVersion(initiator
);
265 CLockObject
lock(m_mutex
);
269 void CCECBusDevice::SetCecVersion(const cec_version newVersion
)
271 CLockObject
lock(m_mutex
);
272 if (m_cecVersion
!= newVersion
)
273 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): CEC version %s", GetLogicalAddressName(), m_iLogicalAddress
, ToString(newVersion
));
274 m_cecVersion
= newVersion
;
277 bool CCECBusDevice::RequestCecVersion(const cec_logical_address initiator
, bool bWaitForResponse
/* = true */)
281 if (!IsHandledByLibCEC() &&
282 !IsUnsupportedFeature(CEC_OPCODE_GET_CEC_VERSION
))
285 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< requesting CEC version of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
286 bReturn
= m_handler
->TransmitRequestCecVersion(initiator
, m_iLogicalAddress
, bWaitForResponse
);
292 bool CCECBusDevice::TransmitCECVersion(const cec_logical_address destination
)
296 CLockObject
lock(m_mutex
);
297 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): cec version %s", GetLogicalAddressName(), m_iLogicalAddress
, ToString(destination
), destination
, ToString(m_cecVersion
));
298 version
= m_cecVersion
;
302 bool bReturn
= m_handler
->TransmitCECVersion(m_iLogicalAddress
, destination
, version
);
307 cec_menu_language
&CCECBusDevice::GetMenuLanguage(const cec_logical_address initiator
, bool bUpdate
/* = false */)
309 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
310 bool bRequestUpdate(false);
312 CLockObject
lock(m_mutex
);
313 bRequestUpdate
= (bIsPresent
&&
314 (bUpdate
|| !strcmp(m_menuLanguage
.language
, "???")));
319 CheckVendorIdRequested(initiator
);
320 RequestMenuLanguage(initiator
);
323 CLockObject
lock(m_mutex
);
324 return m_menuLanguage
;
327 void CCECBusDevice::SetMenuLanguage(const char *strLanguage
)
332 CLockObject
lock(m_mutex
);
333 if (strcmp(strLanguage
, m_menuLanguage
.language
))
335 memcpy(m_menuLanguage
.language
, strLanguage
, 3);
336 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, ">> %s (%X): menu language set to '%s'", GetLogicalAddressName(), m_iLogicalAddress
, m_menuLanguage
.language
);
340 void CCECBusDevice::SetMenuLanguage(const cec_menu_language
&language
)
342 if (language
.device
== m_iLogicalAddress
)
343 SetMenuLanguage(language
.language
);
346 bool CCECBusDevice::RequestMenuLanguage(const cec_logical_address initiator
, bool bWaitForResponse
/* = true */)
350 if (!IsHandledByLibCEC() &&
351 !IsUnsupportedFeature(CEC_OPCODE_GET_MENU_LANGUAGE
))
354 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< requesting menu language of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
355 bReturn
= m_handler
->TransmitRequestMenuLanguage(initiator
, m_iLogicalAddress
, bWaitForResponse
);
361 bool CCECBusDevice::TransmitSetMenuLanguage(const cec_logical_address destination
)
364 cec_menu_language language
;
366 CLockObject
lock(m_mutex
);
367 language
= m_menuLanguage
;
372 CLockObject
lock(m_mutex
);
373 lang
[0] = language
.language
[0];
374 lang
[1] = language
.language
[1];
375 lang
[2] = language
.language
[2];
379 if (lang
[0] == '?' && lang
[1] == '?' && lang
[2] == '?')
381 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): Menu language feature abort", GetLogicalAddressName(), m_iLogicalAddress
, ToString(destination
), destination
);
382 m_processor
->TransmitAbort(m_iLogicalAddress
, destination
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
387 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> broadcast (F): Menu language '%s'", GetLogicalAddressName(), m_iLogicalAddress
, lang
);
388 bReturn
= m_handler
->TransmitSetMenuLanguage(m_iLogicalAddress
, lang
);
394 bool CCECBusDevice::TransmitOSDString(const cec_logical_address destination
, cec_display_control duration
, const char *strMessage
)
397 if (!m_processor
->GetDevice(destination
)->IsUnsupportedFeature(CEC_OPCODE_SET_OSD_STRING
))
399 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): display OSD message '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(destination
), destination
, strMessage
);
401 bReturn
= m_handler
->TransmitOSDString(m_iLogicalAddress
, destination
, duration
, strMessage
);
407 CStdString
CCECBusDevice::GetCurrentOSDName(void)
409 CLockObject
lock(m_mutex
);
410 return m_strDeviceName
;
413 CStdString
CCECBusDevice::GetOSDName(const cec_logical_address initiator
, bool bUpdate
/* = false */)
415 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
416 bool bRequestUpdate(false);
418 CLockObject
lock(m_mutex
);
419 bRequestUpdate
= bIsPresent
&&
420 (bUpdate
|| m_strDeviceName
.Equals(ToString(m_iLogicalAddress
))) &&
421 m_type
!= CEC_DEVICE_TYPE_TV
;
426 CheckVendorIdRequested(initiator
);
427 RequestOSDName(initiator
);
430 CLockObject
lock(m_mutex
);
431 return m_strDeviceName
;
434 void CCECBusDevice::SetOSDName(CStdString strName
)
436 CLockObject
lock(m_mutex
);
437 if (m_strDeviceName
!= strName
)
439 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, ">> %s (%X): osd name set to '%s'", GetLogicalAddressName(), m_iLogicalAddress
, strName
.c_str());
440 m_strDeviceName
= strName
;
444 bool CCECBusDevice::RequestOSDName(const cec_logical_address initiator
, bool bWaitForResponse
/* = true */)
448 if (!IsHandledByLibCEC() &&
449 !IsUnsupportedFeature(CEC_OPCODE_GIVE_OSD_NAME
))
452 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< requesting OSD name of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
453 bReturn
= m_handler
->TransmitRequestOSDName(initiator
, m_iLogicalAddress
, bWaitForResponse
);
459 bool CCECBusDevice::TransmitOSDName(const cec_logical_address destination
)
461 CStdString strDeviceName
;
463 CLockObject
lock(m_mutex
);
464 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): OSD name '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(destination
), destination
, m_strDeviceName
.c_str());
465 strDeviceName
= m_strDeviceName
;
469 bool bReturn
= m_handler
->TransmitOSDName(m_iLogicalAddress
, destination
, strDeviceName
);
474 bool CCECBusDevice::HasValidPhysicalAddress(void)
476 CLockObject
lock(m_mutex
);
477 return CLibCEC::IsValidPhysicalAddress(m_iPhysicalAddress
);
480 uint16_t CCECBusDevice::GetCurrentPhysicalAddress(void)
482 CLockObject
lock(m_mutex
);
483 return m_iPhysicalAddress
;
486 uint16_t CCECBusDevice::GetPhysicalAddress(const cec_logical_address initiator
, bool bSuppressUpdate
/* = false */)
488 if (!bSuppressUpdate
)
490 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
491 bool bRequestUpdate(false);
493 CLockObject
lock(m_mutex
);
494 bRequestUpdate
= bIsPresent
&& m_iPhysicalAddress
== CEC_INVALID_PHYSICAL_ADDRESS
;
499 CheckVendorIdRequested(initiator
);
500 if (!RequestPhysicalAddress(initiator
))
501 LIB_CEC
->AddLog(CEC_LOG_ERROR
, "failed to request the physical address");
505 CLockObject
lock(m_mutex
);
506 return m_iPhysicalAddress
;
509 bool CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress
)
511 CLockObject
lock(m_mutex
);
512 if (iNewAddress
> 0 && m_iPhysicalAddress
!= iNewAddress
)
514 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, ">> %s (%X): physical address changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress
, m_iPhysicalAddress
, iNewAddress
);
515 m_iPhysicalAddress
= iNewAddress
;
520 bool CCECBusDevice::RequestPhysicalAddress(const cec_logical_address initiator
, bool bWaitForResponse
/* = true */)
524 if (!IsHandledByLibCEC())
527 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< requesting physical address of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
528 bReturn
= m_handler
->TransmitRequestPhysicalAddress(initiator
, m_iLogicalAddress
, bWaitForResponse
);
534 bool CCECBusDevice::TransmitPhysicalAddress(void)
536 uint16_t iPhysicalAddress
;
537 cec_device_type type
;
539 CLockObject
lock(m_mutex
);
540 if (m_iPhysicalAddress
== CEC_INVALID_PHYSICAL_ADDRESS
)
543 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> broadcast (F): physical adddress %4x", GetLogicalAddressName(), m_iLogicalAddress
, m_iPhysicalAddress
);
544 iPhysicalAddress
= m_iPhysicalAddress
;
549 bool bReturn
= m_handler
->TransmitPhysicalAddress(m_iLogicalAddress
, iPhysicalAddress
, type
);
554 cec_power_status
CCECBusDevice::GetCurrentPowerStatus(void)
556 CLockObject
lock(m_mutex
);
557 return m_powerStatus
;
560 cec_power_status
CCECBusDevice::GetPowerStatus(const cec_logical_address initiator
, bool bUpdate
/* = false */)
562 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
563 bool bRequestUpdate(false);
565 CLockObject
lock(m_mutex
);
566 bRequestUpdate
= (bIsPresent
&&
567 (bUpdate
|| m_powerStatus
== CEC_POWER_STATUS_UNKNOWN
||
568 m_powerStatus
== CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
||
569 m_powerStatus
== CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY
||
570 GetTimeMs() - m_iLastPowerStateUpdate
>= CEC_POWER_STATE_REFRESH_TIME
));
575 CheckVendorIdRequested(initiator
);
576 RequestPowerStatus(initiator
);
579 CLockObject
lock(m_mutex
);
580 return m_powerStatus
;
583 void CCECBusDevice::SetPowerStatus(const cec_power_status powerStatus
)
585 CLockObject
lock(m_mutex
);
586 if (m_powerStatus
!= powerStatus
)
588 m_iLastPowerStateUpdate
= GetTimeMs();
589 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, ">> %s (%X): power status changed from '%s' to '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(m_powerStatus
), ToString(powerStatus
));
590 m_powerStatus
= powerStatus
;
594 bool CCECBusDevice::RequestPowerStatus(const cec_logical_address initiator
, bool bWaitForResponse
/* = true */)
598 if (!IsHandledByLibCEC() &&
599 !IsUnsupportedFeature(CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
))
602 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< requesting power status of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
603 bReturn
= m_handler
->TransmitRequestPowerStatus(initiator
, m_iLogicalAddress
, bWaitForResponse
);
609 bool CCECBusDevice::TransmitPowerState(const cec_logical_address destination
)
611 cec_power_status state
;
613 CLockObject
lock(m_mutex
);
614 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): %s", GetLogicalAddressName(), m_iLogicalAddress
, ToString(destination
), destination
, ToString(m_powerStatus
));
615 state
= m_powerStatus
;
619 bool bReturn
= m_handler
->TransmitPowerState(m_iLogicalAddress
, destination
, state
);
624 cec_vendor_id
CCECBusDevice::GetCurrentVendorId(void)
626 CLockObject
lock(m_mutex
);
630 cec_vendor_id
CCECBusDevice::GetVendorId(const cec_logical_address initiator
, bool bUpdate
/* = false */)
632 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
633 bool bRequestUpdate(false);
635 CLockObject
lock(m_mutex
);
636 bRequestUpdate
= (bIsPresent
&&
637 (bUpdate
|| m_vendor
== CEC_VENDOR_UNKNOWN
));
641 RequestVendorId(initiator
);
643 CLockObject
lock(m_mutex
);
647 const char *CCECBusDevice::GetVendorName(const cec_logical_address initiator
, bool bUpdate
/* = false */)
649 return ToString(GetVendorId(initiator
, bUpdate
));
652 bool CCECBusDevice::SetVendorId(uint64_t iVendorId
)
654 bool bVendorChanged(false);
657 CLockObject
lock(m_mutex
);
658 bVendorChanged
= (m_vendor
!= (cec_vendor_id
)iVendorId
);
659 m_vendor
= (cec_vendor_id
)iVendorId
;
663 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): vendor = %s (%06x)", GetLogicalAddressName(), m_iLogicalAddress
, ToString(m_vendor
), m_vendor
);
665 return bVendorChanged
;
668 bool CCECBusDevice::RequestVendorId(const cec_logical_address initiator
, bool bWaitForResponse
/* = true */)
672 if (!IsHandledByLibCEC() && initiator
!= CECDEVICE_UNKNOWN
)
675 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< requesting vendor ID of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
676 bReturn
= m_handler
->TransmitRequestVendorId(initiator
, m_iLogicalAddress
, bWaitForResponse
);
679 if (bWaitForResponse
)
680 ReplaceHandler(true);
685 bool CCECBusDevice::TransmitVendorID(const cec_logical_address destination
, bool bSendAbort
/* = true */)
690 CLockObject
lock(m_mutex
);
691 iVendorId
= (uint64_t)m_vendor
;
695 if (iVendorId
== CEC_VENDOR_UNKNOWN
)
699 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): vendor id feature abort", GetLogicalAddressName(), m_iLogicalAddress
, ToString(destination
), destination
);
700 m_processor
->TransmitAbort(m_iLogicalAddress
, destination
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
706 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
);
707 bReturn
= m_handler
->TransmitVendorID(m_iLogicalAddress
, iVendorId
);
713 cec_bus_device_status
CCECBusDevice::GetStatus(bool bForcePoll
/* = false */, bool bSuppressPoll
/* = false */)
715 cec_bus_device_status
status(CEC_DEVICE_STATUS_UNKNOWN
);
716 bool bNeedsPoll(false);
719 CLockObject
lock(m_mutex
);
720 status
= m_deviceStatus
;
721 bNeedsPoll
= !bSuppressPoll
&&
722 (bForcePoll
|| m_deviceStatus
== CEC_DEVICE_STATUS_UNKNOWN
);
727 bool bPollAcked(false);
728 if (bNeedsPoll
&& NeedsPoll())
729 bPollAcked
= m_processor
->PollDevice(m_iLogicalAddress
);
731 status
= bPollAcked
? CEC_DEVICE_STATUS_PRESENT
: CEC_DEVICE_STATUS_NOT_PRESENT
;
732 SetDeviceStatus(status
);
738 void CCECBusDevice::SetDeviceStatus(const cec_bus_device_status newStatus
)
741 CLockObject
lock(m_mutex
);
744 case CEC_DEVICE_STATUS_UNKNOWN
:
745 if (m_deviceStatus
!= newStatus
)
746 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "device status of %s changed into 'unknown'", ToString(m_iLogicalAddress
));
748 m_deviceStatus
= newStatus
;
750 case CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
:
751 if (m_deviceStatus
!= newStatus
)
752 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "device status of %s changed into 'handled by libCEC'", ToString(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
, "device status of %s changed into 'present'", ToString(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
, "device status of %s changed into 'not present'", ToString(m_iLogicalAddress
));
772 m_deviceStatus
= newStatus
;
779 void CCECBusDevice::ResetDeviceStatus(void)
781 CLockObject
lock(m_mutex
);
782 SetPowerStatus (CEC_POWER_STATUS_UNKNOWN
);
783 SetVendorId (CEC_VENDOR_UNKNOWN
);
784 SetMenuState (CEC_MENU_STATE_ACTIVATED
);
785 SetCecVersion (CEC_VERSION_UNKNOWN
);
786 SetStreamPath (CEC_INVALID_PHYSICAL_ADDRESS
);
787 SetOSDName (ToString(m_iLogicalAddress
));
788 MarkAsInactiveSource();
790 m_bVendorIdRequested
= false;
791 m_unsupportedFeatures
.clear();
794 bool CCECBusDevice::TransmitPoll(const cec_logical_address dest
)
797 cec_logical_address
destination(dest
);
798 if (destination
== CECDEVICE_UNKNOWN
)
799 destination
= m_iLogicalAddress
;
801 CCECBusDevice
*destDevice
= m_processor
->GetDevice(destination
);
802 if (destDevice
->m_deviceStatus
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
)
806 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): POLL", GetLogicalAddressName(), m_iLogicalAddress
, ToString(dest
), dest
);
807 bReturn
= m_handler
->TransmitPoll(m_iLogicalAddress
, destination
);
808 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, bReturn
? ">> POLL sent" : ">> POLL not sent");
810 CLockObject
lock(m_mutex
);
813 m_iLastActive
= GetTimeMs();
814 destDevice
->m_deviceStatus
= CEC_DEVICE_STATUS_PRESENT
;
817 destDevice
->m_deviceStatus
= CEC_DEVICE_STATUS_NOT_PRESENT
;
823 void CCECBusDevice::HandlePoll(const cec_logical_address destination
)
825 if (destination
>= 0 && destination
< CECDEVICE_BROADCAST
)
827 CCECBusDevice
*device
= m_processor
->GetDevice(destination
);
829 device
->HandlePollFrom(m_iLogicalAddress
);
833 void CCECBusDevice::HandlePollFrom(const cec_logical_address initiator
)
835 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "<< POLL: %s (%x) -> %s (%x)", ToString(initiator
), initiator
, ToString(m_iLogicalAddress
), m_iLogicalAddress
);
836 m_bAwaitingReceiveFailed
= true;
839 bool CCECBusDevice::HandleReceiveFailed(void)
841 bool bReturn
= m_bAwaitingReceiveFailed
;
842 m_bAwaitingReceiveFailed
= false;
846 cec_menu_state
CCECBusDevice::GetMenuState(const cec_logical_address
UNUSED(initiator
))
848 CLockObject
lock(m_mutex
);
852 void CCECBusDevice::SetMenuState(const cec_menu_state state
)
854 CLockObject
lock(m_mutex
);
855 if (m_menuState
!= state
)
857 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, ">> %s (%X): menu state set to '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(m_menuState
));
862 bool CCECBusDevice::TransmitMenuState(const cec_logical_address dest
)
864 cec_menu_state menuState
;
866 CLockObject
lock(m_mutex
);
867 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): menu state '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(dest
), dest
, ToString(m_menuState
));
868 menuState
= m_menuState
;
872 bool bReturn
= m_handler
->TransmitMenuState(m_iLogicalAddress
, dest
, menuState
);
877 bool CCECBusDevice::ActivateSource(void)
879 MarkAsActiveSource();
880 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "activating source '%s'", ToString(m_iLogicalAddress
));
882 bool bReturn
= m_handler
->ActivateSource();
887 bool CCECBusDevice::RequestActiveSource(bool bWaitForResponse
/* = true */)
891 if (IsHandledByLibCEC())
894 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< requesting active source");
896 bReturn
= m_handler
->TransmitRequestActiveSource(m_iLogicalAddress
, bWaitForResponse
);
902 void CCECBusDevice::MarkAsActiveSource(void)
904 CLockObject
lock(m_mutex
);
905 if (!m_bActiveSource
)
906 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "making %s (%x) the active source", GetLogicalAddressName(), m_iLogicalAddress
);
908 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%x) was already marked as active source", GetLogicalAddressName(), m_iLogicalAddress
);
910 CECDEVICEVEC devices
;
911 m_processor
->GetDevices()->Get(devices
);
912 for (CECDEVICEVEC::iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
913 if ((*it
)->GetLogicalAddress() != m_iLogicalAddress
)
914 (*it
)->MarkAsInactiveSource();
916 m_bActiveSource
= true;
917 SetPowerStatus(CEC_POWER_STATUS_ON
);
920 void CCECBusDevice::MarkAsInactiveSource(void)
923 CLockObject
lock(m_mutex
);
925 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "marking %s (%X) as inactive source", GetLogicalAddressName(), m_iLogicalAddress
);
926 m_bActiveSource
= false;
930 bool CCECBusDevice::TransmitActiveSource(void)
932 bool bSendActiveSource(false);
935 CLockObject
lock(m_mutex
);
936 if (m_powerStatus
!= CEC_POWER_STATUS_ON
&& m_powerStatus
!= CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
)
937 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress
);
938 else if (m_bActiveSource
)
940 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> broadcast (F): active source (%4x)", GetLogicalAddressName(), m_iLogicalAddress
, m_iPhysicalAddress
);
941 bSendActiveSource
= true;
944 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "<< %s (%X) is not the active source", GetLogicalAddressName(), m_iLogicalAddress
);
947 if (bSendActiveSource
)
950 m_handler
->TransmitActiveSource(m_iLogicalAddress
, m_iPhysicalAddress
);
958 bool CCECBusDevice::TransmitImageViewOn(void)
961 CLockObject
lock(m_mutex
);
962 if (m_powerStatus
!= CEC_POWER_STATUS_ON
&& m_powerStatus
!= CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
)
964 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress
);
970 m_handler
->TransmitImageViewOn(m_iLogicalAddress
, CECDEVICE_TV
);
975 bool CCECBusDevice::TransmitInactiveSource(void)
977 uint16_t iPhysicalAddress
;
979 CLockObject
lock(m_mutex
);
980 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> broadcast (F): inactive source", GetLogicalAddressName(), m_iLogicalAddress
);
981 iPhysicalAddress
= m_iPhysicalAddress
;
985 bool bReturn
= m_handler
->TransmitInactiveSource(m_iLogicalAddress
, iPhysicalAddress
);
990 bool CCECBusDevice::TransmitPendingActiveSourceCommands(void)
993 bool bReturn
= m_handler
->TransmitPendingActiveSourceCommands();
998 void CCECBusDevice::SetStreamPath(uint16_t iNewAddress
, uint16_t iOldAddress
/* = CEC_INVALID_PHYSICAL_ADDRESS */)
1000 CLockObject
lock(m_mutex
);
1001 if (iNewAddress
!= m_iStreamPath
)
1003 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, ">> %s (%X): stream path changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress
, iOldAddress
== 0 ? m_iStreamPath
: iOldAddress
, iNewAddress
);
1004 m_iStreamPath
= iNewAddress
;
1007 if (!LIB_CEC
->IsValidPhysicalAddress(iNewAddress
))
1010 CCECBusDevice
*device
= m_processor
->GetDeviceByPhysicalAddress(iNewAddress
);
1013 // if a device is found with the new physical address, mark it as active, which will automatically mark all other devices as inactive
1014 device
->MarkAsActiveSource();
1018 // try to find the device with the old address, and mark it as inactive when found
1019 device
= m_processor
->GetDeviceByPhysicalAddress(iOldAddress
);
1021 device
->MarkAsInactiveSource();
1025 bool CCECBusDevice::PowerOn(const cec_logical_address initiator
)
1027 bool bReturn(false);
1028 GetVendorId(initiator
); // ensure that we got the vendor id, because the implementations vary per vendor
1031 cec_power_status currentStatus
= GetPowerStatus(initiator
, false);
1032 if (currentStatus
!= CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
&&
1033 currentStatus
!= CEC_POWER_STATUS_ON
)
1035 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< powering on '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
1036 if (m_handler
->PowerOn(initiator
, m_iLogicalAddress
))
1038 SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
1044 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "'%s' (%X) is already '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(currentStatus
));
1051 bool CCECBusDevice::Standby(const cec_logical_address initiator
)
1053 GetVendorId(initiator
); // ensure that we got the vendor id, because the implementations vary per vendor
1055 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "<< putting '%s' (%X) in standby mode", GetLogicalAddressName(), m_iLogicalAddress
);
1057 bool bReturn
= m_handler
->TransmitStandby(initiator
, m_iLogicalAddress
);
1062 bool CCECBusDevice::NeedsPoll(void)
1064 bool bSendPoll(false);
1065 cec_logical_address
pollAddress(CECDEVICE_UNKNOWN
);
1066 switch (m_iLogicalAddress
)
1068 case CECDEVICE_PLAYBACKDEVICE3
:
1069 pollAddress
= CECDEVICE_PLAYBACKDEVICE2
;
1071 case CECDEVICE_PLAYBACKDEVICE2
:
1072 pollAddress
= CECDEVICE_PLAYBACKDEVICE1
;
1074 case CECDEVICE_RECORDINGDEVICE3
:
1075 pollAddress
= CECDEVICE_RECORDINGDEVICE2
;
1077 case CECDEVICE_RECORDINGDEVICE2
:
1078 pollAddress
= CECDEVICE_RECORDINGDEVICE1
;
1080 case CECDEVICE_TUNER4
:
1081 pollAddress
= CECDEVICE_TUNER3
;
1083 case CECDEVICE_TUNER3
:
1084 pollAddress
= CECDEVICE_TUNER2
;
1086 case CECDEVICE_TUNER2
:
1087 pollAddress
= CECDEVICE_TUNER1
;
1089 case CECDEVICE_AUDIOSYSTEM
:
1090 case CECDEVICE_PLAYBACKDEVICE1
:
1091 case CECDEVICE_RECORDINGDEVICE1
:
1092 case CECDEVICE_TUNER1
:
1100 if (!bSendPoll
&& pollAddress
!= CECDEVICE_UNKNOWN
)
1102 CCECBusDevice
*device
= m_processor
->GetDevice(pollAddress
);
1105 cec_bus_device_status status
= device
->GetStatus();
1106 bSendPoll
= (status
== CEC_DEVICE_STATUS_PRESENT
|| status
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
1117 void CCECBusDevice::CheckVendorIdRequested(const cec_logical_address initiator
)
1119 bool bRequestVendorId(false);
1121 CLockObject
lock(m_mutex
);
1122 bRequestVendorId
= !m_bVendorIdRequested
;
1123 m_bVendorIdRequested
= true;
1126 if (bRequestVendorId
)
1128 ReplaceHandler(false);
1129 GetVendorId(initiator
);
1134 CCECAudioSystem
*CCECBusDevice::AsAudioSystem(void)
1136 return AsAudioSystem(this);
1139 CCECPlaybackDevice
*CCECBusDevice::AsPlaybackDevice(void)
1141 return AsPlaybackDevice(this);
1144 CCECRecordingDevice
*CCECBusDevice::AsRecordingDevice(void)
1146 return AsRecordingDevice(this);
1149 CCECTuner
*CCECBusDevice::AsTuner(void)
1151 return AsTuner(this);
1154 CCECTV
*CCECBusDevice::AsTV(void)
1159 CCECAudioSystem
*CCECBusDevice::AsAudioSystem(CCECBusDevice
*device
)
1161 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
1162 return static_cast<CCECAudioSystem
*>(device
);
1166 CCECPlaybackDevice
*CCECBusDevice::AsPlaybackDevice(CCECBusDevice
*device
)
1169 (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
||
1170 device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
))
1171 return static_cast<CCECPlaybackDevice
*>(device
);
1175 CCECRecordingDevice
*CCECBusDevice::AsRecordingDevice(CCECBusDevice
*device
)
1177 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
1178 return static_cast<CCECRecordingDevice
*>(device
);
1182 CCECTuner
*CCECBusDevice::AsTuner(CCECBusDevice
*device
)
1184 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_TUNER
)
1185 return static_cast<CCECTuner
*>(device
);
1189 CCECTV
*CCECBusDevice::AsTV(CCECBusDevice
*device
)
1191 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_TV
)
1192 return static_cast<CCECTV
*>(device
);
1196 void CCECBusDevice::MarkBusy(void)
1198 CLockObject
handlerLock(m_handlerMutex
);
1199 ++m_iHandlerUseCount
;
1202 void CCECBusDevice::MarkReady(void)
1204 CLockObject
handlerLock(m_handlerMutex
);
1205 if (m_iHandlerUseCount
> 0)
1206 --m_iHandlerUseCount
;
1209 bool CCECBusDevice::TryLogicalAddress(void)
1211 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "trying logical address '%s'", GetLogicalAddressName());
1213 if (!TransmitPoll(m_iLogicalAddress
))
1215 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "using logical address '%s'", GetLogicalAddressName());
1216 SetDeviceStatus(CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
1221 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "logical address '%s' already taken", GetLogicalAddressName());
1222 SetDeviceStatus(CEC_DEVICE_STATUS_PRESENT
);
1226 CCECClient
*CCECBusDevice::GetClient(void)
1228 return m_processor
->GetClient(m_iLogicalAddress
);