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 CCECCommandHandler
*CCECBusDevice::GetHandler(void)
153 ReplaceHandler(false);
158 bool CCECBusDevice::HandleCommand(const cec_command
&command
)
160 bool bHandled(false);
162 /* update "last active" */
164 CLockObject
lock(m_mutex
);
165 m_iLastActive
= GetTimeMs();
167 /* don't call GetStatus() here, just read the value with the mutex locked */
168 if (m_deviceStatus
!= CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
&& command
.opcode_set
== 1)
169 m_deviceStatus
= CEC_DEVICE_STATUS_PRESENT
;
174 /* handle the command */
175 bHandled
= m_handler
->HandleCommand(command
);
177 /* change status to present */
178 if (bHandled
&& GetLogicalAddress() != CECDEVICE_BROADCAST
)
180 CLockObject
lock(m_mutex
);
181 if (m_deviceStatus
!= CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
)
183 if (m_deviceStatus
!= CEC_DEVICE_STATUS_PRESENT
)
184 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "device %s (%x) status changed to present after command %s", GetLogicalAddressName(), (uint8_t)GetLogicalAddress(), ToString(command
.opcode
));
185 m_deviceStatus
= CEC_DEVICE_STATUS_PRESENT
;
193 const char* CCECBusDevice::GetLogicalAddressName(void) const
195 return ToString(m_iLogicalAddress
);
198 bool CCECBusDevice::IsPresent(void)
200 CLockObject
lock(m_mutex
);
201 return m_deviceStatus
== CEC_DEVICE_STATUS_PRESENT
;
204 bool CCECBusDevice::IsHandledByLibCEC(void)
206 CLockObject
lock(m_mutex
);
207 return m_deviceStatus
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
;
210 void CCECBusDevice::SetUnsupportedFeature(cec_opcode opcode
)
212 // some commands should never be marked as unsupported
213 if (opcode
== CEC_OPCODE_VENDOR_COMMAND
||
214 opcode
== CEC_OPCODE_VENDOR_COMMAND_WITH_ID
||
215 opcode
== CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN
||
216 opcode
== CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP
||
217 opcode
== CEC_OPCODE_ABORT
||
218 opcode
== CEC_OPCODE_FEATURE_ABORT
||
219 opcode
== CEC_OPCODE_NONE
)
223 CLockObject
lock(m_mutex
);
224 if (m_unsupportedFeatures
.find(opcode
) == m_unsupportedFeatures
.end())
226 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "marking opcode '%s' as unsupported feature for device '%s'", ToString(opcode
), GetLogicalAddressName());
227 m_unsupportedFeatures
.insert(opcode
);
231 // signal threads that are waiting for a reponse
233 m_handler
->SignalOpcode(cec_command::GetResponseOpcode(opcode
));
237 bool CCECBusDevice::IsUnsupportedFeature(cec_opcode opcode
)
239 CLockObject
lock(m_mutex
);
240 bool bUnsupported
= (m_unsupportedFeatures
.find(opcode
) != m_unsupportedFeatures
.end());
242 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "'%s' is marked as unsupported feature for device '%s'", ToString(opcode
), GetLogicalAddressName());
246 bool CCECBusDevice::TransmitKeypress(const cec_logical_address initiator
, cec_user_control_code key
, bool bWait
/* = true */)
249 bool bReturn
= m_handler
->TransmitKeypress(initiator
, m_iLogicalAddress
, key
, bWait
);
254 bool CCECBusDevice::TransmitKeyRelease(const cec_logical_address initiator
, bool bWait
/* = true */)
257 bool bReturn
= m_handler
->TransmitKeyRelease(initiator
, m_iLogicalAddress
, bWait
);
262 cec_version
CCECBusDevice::GetCecVersion(const cec_logical_address initiator
, bool bUpdate
/* = false */)
264 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
265 bool bRequestUpdate(false);
267 CLockObject
lock(m_mutex
);
268 bRequestUpdate
= bIsPresent
&&
269 (bUpdate
|| m_cecVersion
== CEC_VERSION_UNKNOWN
);
274 CheckVendorIdRequested(initiator
);
275 RequestCecVersion(initiator
);
278 CLockObject
lock(m_mutex
);
282 void CCECBusDevice::SetCecVersion(const cec_version newVersion
)
284 CLockObject
lock(m_mutex
);
285 if (m_cecVersion
!= newVersion
)
286 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): CEC version %s", GetLogicalAddressName(), m_iLogicalAddress
, ToString(newVersion
));
287 m_cecVersion
= newVersion
;
290 bool CCECBusDevice::RequestCecVersion(const cec_logical_address initiator
, bool bWaitForResponse
/* = true */)
294 if (!IsHandledByLibCEC() &&
295 !IsUnsupportedFeature(CEC_OPCODE_GET_CEC_VERSION
))
298 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< requesting CEC version of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
299 bReturn
= m_handler
->TransmitRequestCecVersion(initiator
, m_iLogicalAddress
, bWaitForResponse
);
305 bool CCECBusDevice::TransmitCECVersion(const cec_logical_address destination
)
309 CLockObject
lock(m_mutex
);
310 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): cec version %s", GetLogicalAddressName(), m_iLogicalAddress
, ToString(destination
), destination
, ToString(m_cecVersion
));
311 version
= m_cecVersion
;
315 bool bReturn
= m_handler
->TransmitCECVersion(m_iLogicalAddress
, destination
, version
);
320 cec_menu_language
&CCECBusDevice::GetMenuLanguage(const cec_logical_address initiator
, bool bUpdate
/* = false */)
322 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
323 bool bRequestUpdate(false);
325 CLockObject
lock(m_mutex
);
326 bRequestUpdate
= (bIsPresent
&&
327 (bUpdate
|| !strcmp(m_menuLanguage
.language
, "???")));
332 CheckVendorIdRequested(initiator
);
333 RequestMenuLanguage(initiator
);
336 CLockObject
lock(m_mutex
);
337 return m_menuLanguage
;
340 void CCECBusDevice::SetMenuLanguage(const char *strLanguage
)
345 CLockObject
lock(m_mutex
);
346 if (strcmp(strLanguage
, m_menuLanguage
.language
))
348 memcpy(m_menuLanguage
.language
, strLanguage
, 3);
349 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): menu language set to '%s'", GetLogicalAddressName(), m_iLogicalAddress
, m_menuLanguage
.language
);
353 void CCECBusDevice::SetMenuLanguage(const cec_menu_language
&language
)
355 if (language
.device
== m_iLogicalAddress
)
356 SetMenuLanguage(language
.language
);
359 bool CCECBusDevice::RequestMenuLanguage(const cec_logical_address initiator
, bool bWaitForResponse
/* = true */)
363 if (!IsHandledByLibCEC() &&
364 !IsUnsupportedFeature(CEC_OPCODE_GET_MENU_LANGUAGE
))
367 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< requesting menu language of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
368 bReturn
= m_handler
->TransmitRequestMenuLanguage(initiator
, m_iLogicalAddress
, bWaitForResponse
);
374 bool CCECBusDevice::TransmitSetMenuLanguage(const cec_logical_address destination
)
377 cec_menu_language language
;
379 CLockObject
lock(m_mutex
);
380 language
= m_menuLanguage
;
385 CLockObject
lock(m_mutex
);
386 lang
[0] = language
.language
[0];
387 lang
[1] = language
.language
[1];
388 lang
[2] = language
.language
[2];
392 if (lang
[0] == '?' && lang
[1] == '?' && lang
[2] == '?')
394 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): Menu language feature abort", GetLogicalAddressName(), m_iLogicalAddress
, ToString(destination
), destination
);
395 m_processor
->TransmitAbort(m_iLogicalAddress
, destination
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
400 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> broadcast (F): Menu language '%s'", GetLogicalAddressName(), m_iLogicalAddress
, lang
);
401 bReturn
= m_handler
->TransmitSetMenuLanguage(m_iLogicalAddress
, lang
);
407 bool CCECBusDevice::TransmitOSDString(const cec_logical_address destination
, cec_display_control duration
, const char *strMessage
)
410 if (!m_processor
->GetDevice(destination
)->IsUnsupportedFeature(CEC_OPCODE_SET_OSD_STRING
))
412 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): display OSD message '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(destination
), destination
, strMessage
);
414 bReturn
= m_handler
->TransmitOSDString(m_iLogicalAddress
, destination
, duration
, strMessage
);
420 CStdString
CCECBusDevice::GetCurrentOSDName(void)
422 CLockObject
lock(m_mutex
);
423 return m_strDeviceName
;
426 CStdString
CCECBusDevice::GetOSDName(const cec_logical_address initiator
, bool bUpdate
/* = false */)
428 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
429 bool bRequestUpdate(false);
431 CLockObject
lock(m_mutex
);
432 bRequestUpdate
= bIsPresent
&&
433 (bUpdate
|| m_strDeviceName
.Equals(ToString(m_iLogicalAddress
))) &&
434 m_type
!= CEC_DEVICE_TYPE_TV
;
439 CheckVendorIdRequested(initiator
);
440 RequestOSDName(initiator
);
443 CLockObject
lock(m_mutex
);
444 return m_strDeviceName
;
447 void CCECBusDevice::SetOSDName(CStdString strName
)
449 CLockObject
lock(m_mutex
);
450 if (m_strDeviceName
!= strName
)
452 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): osd name set to '%s'", GetLogicalAddressName(), m_iLogicalAddress
, strName
.c_str());
453 m_strDeviceName
= strName
;
457 bool CCECBusDevice::RequestOSDName(const cec_logical_address initiator
, bool bWaitForResponse
/* = true */)
461 if (!IsHandledByLibCEC() &&
462 !IsUnsupportedFeature(CEC_OPCODE_GIVE_OSD_NAME
))
465 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< requesting OSD name of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
466 bReturn
= m_handler
->TransmitRequestOSDName(initiator
, m_iLogicalAddress
, bWaitForResponse
);
472 bool CCECBusDevice::TransmitOSDName(const cec_logical_address destination
)
474 CStdString strDeviceName
;
476 CLockObject
lock(m_mutex
);
477 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): OSD name '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(destination
), destination
, m_strDeviceName
.c_str());
478 strDeviceName
= m_strDeviceName
;
482 bool bReturn
= m_handler
->TransmitOSDName(m_iLogicalAddress
, destination
, strDeviceName
);
487 bool CCECBusDevice::HasValidPhysicalAddress(void)
489 CLockObject
lock(m_mutex
);
490 return CLibCEC::IsValidPhysicalAddress(m_iPhysicalAddress
);
493 uint16_t CCECBusDevice::GetCurrentPhysicalAddress(void)
495 CLockObject
lock(m_mutex
);
496 return m_iPhysicalAddress
;
499 uint16_t CCECBusDevice::GetPhysicalAddress(const cec_logical_address initiator
, bool bSuppressUpdate
/* = false */)
501 if (!bSuppressUpdate
)
503 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
504 bool bRequestUpdate(false);
506 CLockObject
lock(m_mutex
);
507 bRequestUpdate
= bIsPresent
&& m_iPhysicalAddress
== CEC_INVALID_PHYSICAL_ADDRESS
;
512 CheckVendorIdRequested(initiator
);
513 if (!RequestPhysicalAddress(initiator
))
514 LIB_CEC
->AddLog(CEC_LOG_ERROR
, "failed to request the physical address");
518 CLockObject
lock(m_mutex
);
519 return m_iPhysicalAddress
;
522 bool CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress
)
524 CLockObject
lock(m_mutex
);
525 if (iNewAddress
> 0 && m_iPhysicalAddress
!= iNewAddress
)
527 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): physical address changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress
, m_iPhysicalAddress
, iNewAddress
);
528 m_iPhysicalAddress
= iNewAddress
;
533 bool CCECBusDevice::RequestPhysicalAddress(const cec_logical_address initiator
, bool bWaitForResponse
/* = true */)
537 if (!IsHandledByLibCEC())
540 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< requesting physical address of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
541 bReturn
= m_handler
->TransmitRequestPhysicalAddress(initiator
, m_iLogicalAddress
, bWaitForResponse
);
547 bool CCECBusDevice::TransmitPhysicalAddress(void)
549 uint16_t iPhysicalAddress
;
550 cec_device_type type
;
552 CLockObject
lock(m_mutex
);
553 if (m_iPhysicalAddress
== CEC_INVALID_PHYSICAL_ADDRESS
)
556 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> broadcast (F): physical adddress %4x", GetLogicalAddressName(), m_iLogicalAddress
, m_iPhysicalAddress
);
557 iPhysicalAddress
= m_iPhysicalAddress
;
562 bool bReturn
= m_handler
->TransmitPhysicalAddress(m_iLogicalAddress
, iPhysicalAddress
, type
);
567 cec_power_status
CCECBusDevice::GetCurrentPowerStatus(void)
569 CLockObject
lock(m_mutex
);
570 return m_powerStatus
;
573 cec_power_status
CCECBusDevice::GetPowerStatus(const cec_logical_address initiator
, bool bUpdate
/* = false */)
575 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
576 bool bRequestUpdate(false);
578 CLockObject
lock(m_mutex
);
579 bRequestUpdate
= (bIsPresent
&&
580 (bUpdate
|| m_powerStatus
== CEC_POWER_STATUS_UNKNOWN
||
581 m_powerStatus
== CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
||
582 m_powerStatus
== CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY
||
583 GetTimeMs() - m_iLastPowerStateUpdate
>= CEC_POWER_STATE_REFRESH_TIME
));
588 CheckVendorIdRequested(initiator
);
589 RequestPowerStatus(initiator
);
592 CLockObject
lock(m_mutex
);
593 return m_powerStatus
;
596 void CCECBusDevice::SetPowerStatus(const cec_power_status powerStatus
)
598 CLockObject
lock(m_mutex
);
599 if (m_powerStatus
!= powerStatus
)
601 m_iLastPowerStateUpdate
= GetTimeMs();
602 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): power status changed from '%s' to '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(m_powerStatus
), ToString(powerStatus
));
603 m_powerStatus
= powerStatus
;
607 bool CCECBusDevice::RequestPowerStatus(const cec_logical_address initiator
, bool bWaitForResponse
/* = true */)
611 if (!IsHandledByLibCEC() &&
612 !IsUnsupportedFeature(CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
))
615 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< requesting power status of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
616 bReturn
= m_handler
->TransmitRequestPowerStatus(initiator
, m_iLogicalAddress
, bWaitForResponse
);
622 bool CCECBusDevice::TransmitPowerState(const cec_logical_address destination
)
624 cec_power_status state
;
626 CLockObject
lock(m_mutex
);
627 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): %s", GetLogicalAddressName(), m_iLogicalAddress
, ToString(destination
), destination
, ToString(m_powerStatus
));
628 state
= m_powerStatus
;
632 bool bReturn
= m_handler
->TransmitPowerState(m_iLogicalAddress
, destination
, state
);
637 cec_vendor_id
CCECBusDevice::GetCurrentVendorId(void)
639 CLockObject
lock(m_mutex
);
643 cec_vendor_id
CCECBusDevice::GetVendorId(const cec_logical_address initiator
, bool bUpdate
/* = false */)
645 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
646 bool bRequestUpdate(false);
648 CLockObject
lock(m_mutex
);
649 bRequestUpdate
= (bIsPresent
&&
650 (bUpdate
|| m_vendor
== CEC_VENDOR_UNKNOWN
));
654 RequestVendorId(initiator
);
656 CLockObject
lock(m_mutex
);
660 const char *CCECBusDevice::GetVendorName(const cec_logical_address initiator
, bool bUpdate
/* = false */)
662 return ToString(GetVendorId(initiator
, bUpdate
));
665 bool CCECBusDevice::SetVendorId(uint64_t iVendorId
)
667 bool bVendorChanged(false);
670 CLockObject
lock(m_mutex
);
671 bVendorChanged
= (m_vendor
!= (cec_vendor_id
)iVendorId
);
672 m_vendor
= (cec_vendor_id
)iVendorId
;
676 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): vendor = %s (%06x)", GetLogicalAddressName(), m_iLogicalAddress
, ToString(m_vendor
), m_vendor
);
678 return bVendorChanged
;
681 bool CCECBusDevice::RequestVendorId(const cec_logical_address initiator
, bool bWaitForResponse
/* = true */)
685 if (!IsHandledByLibCEC() && initiator
!= CECDEVICE_UNKNOWN
)
688 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< requesting vendor ID of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
689 bReturn
= m_handler
->TransmitRequestVendorId(initiator
, m_iLogicalAddress
, bWaitForResponse
);
692 if (bWaitForResponse
)
693 ReplaceHandler(true);
698 bool CCECBusDevice::TransmitVendorID(const cec_logical_address destination
, bool bSendAbort
/* = true */)
703 CLockObject
lock(m_mutex
);
704 iVendorId
= (uint64_t)m_vendor
;
708 if (iVendorId
== CEC_VENDOR_UNKNOWN
)
712 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): vendor id feature abort", GetLogicalAddressName(), m_iLogicalAddress
, ToString(destination
), destination
);
713 m_processor
->TransmitAbort(m_iLogicalAddress
, destination
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
719 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
);
720 bReturn
= m_handler
->TransmitVendorID(m_iLogicalAddress
, iVendorId
);
726 cec_bus_device_status
CCECBusDevice::GetStatus(bool bForcePoll
/* = false */, bool bSuppressPoll
/* = false */)
728 cec_bus_device_status
status(CEC_DEVICE_STATUS_UNKNOWN
);
729 bool bNeedsPoll(false);
732 CLockObject
lock(m_mutex
);
733 status
= m_deviceStatus
;
734 bNeedsPoll
= !bSuppressPoll
&&
735 (bForcePoll
|| m_deviceStatus
== CEC_DEVICE_STATUS_UNKNOWN
);
740 bool bPollAcked(false);
741 if (bNeedsPoll
&& NeedsPoll())
742 bPollAcked
= m_processor
->PollDevice(m_iLogicalAddress
);
744 status
= bPollAcked
? CEC_DEVICE_STATUS_PRESENT
: CEC_DEVICE_STATUS_NOT_PRESENT
;
745 SetDeviceStatus(status
);
751 void CCECBusDevice::SetDeviceStatus(const cec_bus_device_status newStatus
)
754 CLockObject
lock(m_mutex
);
757 case CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
:
758 if (m_deviceStatus
!= newStatus
)
759 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): device status changed into 'handled by libCEC'", GetLogicalAddressName(), m_iLogicalAddress
);
760 SetPowerStatus (CEC_POWER_STATUS_ON
);
761 SetVendorId (CEC_VENDOR_UNKNOWN
);
762 SetMenuState (CEC_MENU_STATE_ACTIVATED
);
763 SetCecVersion (CEC_VERSION_1_3A
);
764 SetStreamPath (CEC_INVALID_PHYSICAL_ADDRESS
);
765 MarkAsInactiveSource();
767 m_deviceStatus
= newStatus
;
769 case CEC_DEVICE_STATUS_PRESENT
:
770 if (m_deviceStatus
!= newStatus
)
771 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): device status changed into 'present'", GetLogicalAddressName(), m_iLogicalAddress
);
772 m_deviceStatus
= newStatus
;
774 case CEC_DEVICE_STATUS_NOT_PRESENT
:
775 if (m_deviceStatus
!= newStatus
)
777 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): device status changed into 'not present'", GetLogicalAddressName(), m_iLogicalAddress
);
779 m_deviceStatus
= newStatus
;
789 void CCECBusDevice::ResetDeviceStatus(void)
791 CLockObject
lock(m_mutex
);
792 SetPowerStatus (CEC_POWER_STATUS_UNKNOWN
);
793 SetVendorId (CEC_VENDOR_UNKNOWN
);
794 SetMenuState (CEC_MENU_STATE_ACTIVATED
);
795 SetCecVersion (CEC_VERSION_UNKNOWN
);
796 SetStreamPath (CEC_INVALID_PHYSICAL_ADDRESS
);
797 SetOSDName (ToString(m_iLogicalAddress
));
798 MarkAsInactiveSource();
801 m_bVendorIdRequested
= false;
802 m_unsupportedFeatures
.clear();
804 if (m_deviceStatus
!= CEC_DEVICE_STATUS_UNKNOWN
)
805 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): device status changed into 'unknown'", GetLogicalAddressName(), m_iLogicalAddress
);
806 m_deviceStatus
= CEC_DEVICE_STATUS_UNKNOWN
;
809 bool CCECBusDevice::TransmitPoll(const cec_logical_address dest
)
812 cec_logical_address
destination(dest
);
813 if (destination
== CECDEVICE_UNKNOWN
)
814 destination
= m_iLogicalAddress
;
816 CCECBusDevice
*destDevice
= m_processor
->GetDevice(destination
);
817 if (destDevice
->m_deviceStatus
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
)
821 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): POLL", GetLogicalAddressName(), m_iLogicalAddress
, ToString(dest
), dest
);
822 bReturn
= m_handler
->TransmitPoll(m_iLogicalAddress
, destination
);
823 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, bReturn
? ">> POLL sent" : ">> POLL not sent");
825 CLockObject
lock(m_mutex
);
828 m_iLastActive
= GetTimeMs();
829 destDevice
->m_deviceStatus
= CEC_DEVICE_STATUS_PRESENT
;
832 destDevice
->m_deviceStatus
= CEC_DEVICE_STATUS_NOT_PRESENT
;
838 void CCECBusDevice::HandlePoll(const cec_logical_address destination
)
840 if (destination
>= 0 && destination
< CECDEVICE_BROADCAST
)
842 CCECBusDevice
*device
= m_processor
->GetDevice(destination
);
844 device
->HandlePollFrom(m_iLogicalAddress
);
848 void CCECBusDevice::HandlePollFrom(const cec_logical_address initiator
)
850 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "<< POLL: %s (%x) -> %s (%x)", ToString(initiator
), initiator
, ToString(m_iLogicalAddress
), m_iLogicalAddress
);
851 m_bAwaitingReceiveFailed
= true;
854 bool CCECBusDevice::HandleReceiveFailed(void)
856 bool bReturn
= m_bAwaitingReceiveFailed
;
857 m_bAwaitingReceiveFailed
= false;
861 cec_menu_state
CCECBusDevice::GetMenuState(const cec_logical_address
UNUSED(initiator
))
863 CLockObject
lock(m_mutex
);
867 void CCECBusDevice::SetMenuState(const cec_menu_state state
)
869 CLockObject
lock(m_mutex
);
870 if (m_menuState
!= state
)
872 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): menu state set to '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(m_menuState
));
877 bool CCECBusDevice::TransmitMenuState(const cec_logical_address dest
)
879 cec_menu_state menuState
;
881 CLockObject
lock(m_mutex
);
882 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): menu state '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(dest
), dest
, ToString(m_menuState
));
883 menuState
= m_menuState
;
887 bool bReturn
= m_handler
->TransmitMenuState(m_iLogicalAddress
, dest
, menuState
);
892 bool CCECBusDevice::ActivateSource(void)
894 MarkAsActiveSource();
895 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "activating source '%s'", ToString(m_iLogicalAddress
));
897 bool bReturn
= m_handler
->ActivateSource();
902 bool CCECBusDevice::RequestActiveSource(bool bWaitForResponse
/* = true */)
906 if (IsHandledByLibCEC())
909 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< requesting active source");
911 bReturn
= m_handler
->TransmitRequestActiveSource(m_iLogicalAddress
, bWaitForResponse
);
917 void CCECBusDevice::MarkAsActiveSource(void)
919 CLockObject
lock(m_mutex
);
920 if (!m_bActiveSource
)
921 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "making %s (%x) the active source", GetLogicalAddressName(), m_iLogicalAddress
);
923 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%x) was already marked as active source", GetLogicalAddressName(), m_iLogicalAddress
);
925 CECDEVICEVEC devices
;
926 m_processor
->GetDevices()->Get(devices
);
927 for (CECDEVICEVEC::iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
928 if ((*it
)->GetLogicalAddress() != m_iLogicalAddress
)
929 (*it
)->MarkAsInactiveSource();
931 m_bActiveSource
= true;
932 SetPowerStatus(CEC_POWER_STATUS_ON
);
935 void CCECBusDevice::MarkAsInactiveSource(void)
938 CLockObject
lock(m_mutex
);
940 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "marking %s (%X) as inactive source", GetLogicalAddressName(), m_iLogicalAddress
);
941 m_bActiveSource
= false;
945 bool CCECBusDevice::TransmitActiveSource(void)
947 bool bSendActiveSource(false);
950 CLockObject
lock(m_mutex
);
951 if (m_powerStatus
!= CEC_POWER_STATUS_ON
&& m_powerStatus
!= CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
)
952 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress
);
953 else if (m_bActiveSource
)
955 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> broadcast (F): active source (%4x)", GetLogicalAddressName(), m_iLogicalAddress
, m_iPhysicalAddress
);
956 bSendActiveSource
= true;
959 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "<< %s (%X) is not the active source", GetLogicalAddressName(), m_iLogicalAddress
);
962 bool bActiveSourceSent(false);
963 if (bSendActiveSource
)
966 bActiveSourceSent
= m_handler
->TransmitActiveSource(m_iLogicalAddress
, m_iPhysicalAddress
);
970 return bActiveSourceSent
;
973 bool CCECBusDevice::TransmitImageViewOn(void)
976 CLockObject
lock(m_mutex
);
977 if (m_powerStatus
!= CEC_POWER_STATUS_ON
&& m_powerStatus
!= CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
)
979 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress
);
984 bool bImageViewOnSent(false);
986 bImageViewOnSent
= m_handler
->TransmitImageViewOn(m_iLogicalAddress
, CECDEVICE_TV
);
988 return bImageViewOnSent
;
991 bool CCECBusDevice::TransmitInactiveSource(void)
993 uint16_t iPhysicalAddress
;
995 CLockObject
lock(m_mutex
);
996 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> broadcast (F): inactive source", GetLogicalAddressName(), m_iLogicalAddress
);
997 iPhysicalAddress
= m_iPhysicalAddress
;
1001 bool bReturn
= m_handler
->TransmitInactiveSource(m_iLogicalAddress
, iPhysicalAddress
);
1006 bool CCECBusDevice::TransmitPendingActiveSourceCommands(void)
1009 bool bReturn
= m_handler
->TransmitPendingActiveSourceCommands();
1014 void CCECBusDevice::SetStreamPath(uint16_t iNewAddress
, uint16_t iOldAddress
/* = CEC_INVALID_PHYSICAL_ADDRESS */)
1016 CLockObject
lock(m_mutex
);
1017 if (iNewAddress
!= m_iStreamPath
)
1019 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): stream path changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress
, iOldAddress
== 0 ? m_iStreamPath
: iOldAddress
, iNewAddress
);
1020 m_iStreamPath
= iNewAddress
;
1023 if (!LIB_CEC
->IsValidPhysicalAddress(iNewAddress
))
1026 CCECBusDevice
*device
= m_processor
->GetDeviceByPhysicalAddress(iNewAddress
);
1029 // if a device is found with the new physical address, mark it as active, which will automatically mark all other devices as inactive
1030 device
->MarkAsActiveSource();
1034 // try to find the device with the old address, and mark it as inactive when found
1035 device
= m_processor
->GetDeviceByPhysicalAddress(iOldAddress
);
1037 device
->MarkAsInactiveSource();
1041 bool CCECBusDevice::PowerOn(const cec_logical_address initiator
)
1043 bool bReturn(false);
1044 GetVendorId(initiator
); // ensure that we got the vendor id, because the implementations vary per vendor
1047 cec_power_status currentStatus
= GetPowerStatus(initiator
, false);
1048 if (currentStatus
!= CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
&&
1049 currentStatus
!= CEC_POWER_STATUS_ON
)
1051 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< powering on '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
1052 if (m_handler
->PowerOn(initiator
, m_iLogicalAddress
))
1054 SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
1060 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "'%s' (%X) is already '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(currentStatus
));
1067 bool CCECBusDevice::Standby(const cec_logical_address initiator
)
1069 GetVendorId(initiator
); // ensure that we got the vendor id, because the implementations vary per vendor
1071 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "<< putting '%s' (%X) in standby mode", GetLogicalAddressName(), m_iLogicalAddress
);
1073 bool bReturn
= m_handler
->TransmitStandby(initiator
, m_iLogicalAddress
);
1078 bool CCECBusDevice::NeedsPoll(void)
1080 bool bSendPoll(false);
1081 cec_logical_address
pollAddress(CECDEVICE_UNKNOWN
);
1082 switch (m_iLogicalAddress
)
1084 case CECDEVICE_PLAYBACKDEVICE3
:
1085 pollAddress
= CECDEVICE_PLAYBACKDEVICE2
;
1087 case CECDEVICE_PLAYBACKDEVICE2
:
1088 pollAddress
= CECDEVICE_PLAYBACKDEVICE1
;
1090 case CECDEVICE_RECORDINGDEVICE3
:
1091 pollAddress
= CECDEVICE_RECORDINGDEVICE2
;
1093 case CECDEVICE_RECORDINGDEVICE2
:
1094 pollAddress
= CECDEVICE_RECORDINGDEVICE1
;
1096 case CECDEVICE_TUNER4
:
1097 pollAddress
= CECDEVICE_TUNER3
;
1099 case CECDEVICE_TUNER3
:
1100 pollAddress
= CECDEVICE_TUNER2
;
1102 case CECDEVICE_TUNER2
:
1103 pollAddress
= CECDEVICE_TUNER1
;
1105 case CECDEVICE_AUDIOSYSTEM
:
1106 case CECDEVICE_PLAYBACKDEVICE1
:
1107 case CECDEVICE_RECORDINGDEVICE1
:
1108 case CECDEVICE_TUNER1
:
1116 if (!bSendPoll
&& pollAddress
!= CECDEVICE_UNKNOWN
)
1118 CCECBusDevice
*device
= m_processor
->GetDevice(pollAddress
);
1121 cec_bus_device_status status
= device
->GetStatus();
1122 bSendPoll
= (status
== CEC_DEVICE_STATUS_PRESENT
|| status
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
1133 void CCECBusDevice::CheckVendorIdRequested(const cec_logical_address initiator
)
1135 bool bRequestVendorId(false);
1137 CLockObject
lock(m_mutex
);
1138 bRequestVendorId
= !m_bVendorIdRequested
;
1139 m_bVendorIdRequested
= true;
1142 if (bRequestVendorId
)
1144 ReplaceHandler(false);
1145 GetVendorId(initiator
);
1150 CCECAudioSystem
*CCECBusDevice::AsAudioSystem(void)
1152 return AsAudioSystem(this);
1155 CCECPlaybackDevice
*CCECBusDevice::AsPlaybackDevice(void)
1157 return AsPlaybackDevice(this);
1160 CCECRecordingDevice
*CCECBusDevice::AsRecordingDevice(void)
1162 return AsRecordingDevice(this);
1165 CCECTuner
*CCECBusDevice::AsTuner(void)
1167 return AsTuner(this);
1170 CCECTV
*CCECBusDevice::AsTV(void)
1175 CCECAudioSystem
*CCECBusDevice::AsAudioSystem(CCECBusDevice
*device
)
1177 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
1178 return static_cast<CCECAudioSystem
*>(device
);
1182 CCECPlaybackDevice
*CCECBusDevice::AsPlaybackDevice(CCECBusDevice
*device
)
1185 (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
||
1186 device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
))
1187 return static_cast<CCECPlaybackDevice
*>(device
);
1191 CCECRecordingDevice
*CCECBusDevice::AsRecordingDevice(CCECBusDevice
*device
)
1193 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
1194 return static_cast<CCECRecordingDevice
*>(device
);
1198 CCECTuner
*CCECBusDevice::AsTuner(CCECBusDevice
*device
)
1200 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_TUNER
)
1201 return static_cast<CCECTuner
*>(device
);
1205 CCECTV
*CCECBusDevice::AsTV(CCECBusDevice
*device
)
1207 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_TV
)
1208 return static_cast<CCECTV
*>(device
);
1212 void CCECBusDevice::MarkBusy(void)
1214 CLockObject
handlerLock(m_handlerMutex
);
1215 ++m_iHandlerUseCount
;
1218 void CCECBusDevice::MarkReady(void)
1220 CLockObject
handlerLock(m_handlerMutex
);
1221 if (m_iHandlerUseCount
> 0)
1222 --m_iHandlerUseCount
;
1225 bool CCECBusDevice::TryLogicalAddress(void)
1227 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "trying logical address '%s'", GetLogicalAddressName());
1229 if (!TransmitPoll(m_iLogicalAddress
))
1231 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "using logical address '%s'", GetLogicalAddressName());
1232 SetDeviceStatus(CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
1237 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "logical address '%s' already taken", GetLogicalAddressName());
1238 SetDeviceStatus(CEC_DEVICE_STATUS_PRESENT
);
1242 CCECClient
*CCECBusDevice::GetClient(void)
1244 return m_processor
->GetClient(m_iLogicalAddress
);