2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
9 * This program is dual-licensed; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
27 * For more information contact:
28 * Pulse-Eight Licensing <license@pulse-eight.com>
29 * http://www.pulse-eight.com/
30 * http://www.pulse-eight.net/
33 #include "CECBusDevice.h"
34 #include "../CECProcessor.h"
35 #include "../CECClient.h"
36 #include "../implementations/ANCommandHandler.h"
37 #include "../implementations/CECCommandHandler.h"
38 #include "../implementations/SLCommandHandler.h"
39 #include "../implementations/VLCommandHandler.h"
40 #include "../LibCEC.h"
41 #include "../CECTypeUtils.h"
42 #include "../platform/util/timeutils.h"
43 #include "../platform/util/util.h"
45 #include "CECAudioSystem.h"
46 #include "CECPlaybackDevice.h"
47 #include "CECRecordingDevice.h"
53 using namespace PLATFORM
;
55 #define LIB_CEC m_processor->GetLib()
56 #define ToString(p) CCECTypeUtils::ToString(p)
58 CCECBusDevice::CCECBusDevice(CCECProcessor
*processor
, cec_logical_address iLogicalAddress
, uint16_t iPhysicalAddress
/* = CEC_INVALID_PHYSICAL_ADDRESS */) :
59 m_type (CEC_DEVICE_TYPE_RESERVED
),
60 m_iPhysicalAddress (iPhysicalAddress
),
61 m_iStreamPath (CEC_INVALID_PHYSICAL_ADDRESS
),
62 m_iLogicalAddress (iLogicalAddress
),
63 m_powerStatus (CEC_POWER_STATUS_UNKNOWN
),
64 m_processor (processor
),
65 m_vendor (CEC_VENDOR_UNKNOWN
),
66 m_bReplaceHandler (false),
67 m_menuState (CEC_MENU_STATE_ACTIVATED
),
68 m_bActiveSource (false),
70 m_iLastPowerStateUpdate (0),
71 m_cecVersion (CEC_VERSION_UNKNOWN
),
72 m_deviceStatus (CEC_DEVICE_STATUS_UNKNOWN
),
73 m_iHandlerUseCount (0),
74 m_bAwaitingReceiveFailed(false),
75 m_bVendorIdRequested (false),
76 m_waitForResponse (new CWaitForResponse
)
78 m_handler
= new CCECCommandHandler(this);
80 for (unsigned int iPtr
= 0; iPtr
< 4; iPtr
++)
81 m_menuLanguage
.language
[iPtr
] = '?';
82 m_menuLanguage
.language
[3] = 0;
83 m_menuLanguage
.device
= iLogicalAddress
;
85 m_strDeviceName
= ToString(m_iLogicalAddress
);
88 CCECBusDevice::~CCECBusDevice(void)
90 DELETE_AND_NULL(m_handler
);
91 DELETE_AND_NULL(m_waitForResponse
);
94 bool CCECBusDevice::ReplaceHandler(bool bActivateSource
/* = true */)
96 bool bInitHandler(false);
98 CTryLockObject
lock(m_mutex
);
102 CLockObject
handlerLock(m_handlerMutex
);
103 if (m_iHandlerUseCount
> 0)
108 if (m_vendor
!= m_handler
->GetVendorId())
110 if (CCECCommandHandler::HasSpecificHandler(m_vendor
))
112 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "replacing the command handler for device '%s' (%x)", GetLogicalAddressName(), GetLogicalAddress());
114 int32_t iTransmitTimeout
= m_handler
->m_iTransmitTimeout
;
115 int32_t iTransmitWait
= m_handler
->m_iTransmitWait
;
116 int8_t iTransmitRetries
= m_handler
->m_iTransmitRetries
;
117 int64_t iActiveSourcePending
= m_handler
->m_iActiveSourcePending
;
119 DELETE_AND_NULL(m_handler
);
123 case CEC_VENDOR_SAMSUNG
:
124 m_handler
= new CANCommandHandler(this, iTransmitTimeout
, iTransmitWait
, iTransmitRetries
, iActiveSourcePending
);
127 m_handler
= new CSLCommandHandler(this, iTransmitTimeout
, iTransmitWait
, iTransmitRetries
, iActiveSourcePending
);
129 case CEC_VENDOR_PANASONIC
:
130 m_handler
= new CVLCommandHandler(this, iTransmitTimeout
, iTransmitWait
, iTransmitRetries
, iActiveSourcePending
);
133 m_handler
= new CCECCommandHandler(this, iTransmitTimeout
, iTransmitWait
, iTransmitRetries
, iActiveSourcePending
);
137 m_handler
->SetVendorId(m_vendor
);
145 CCECBusDevice
*primary
= GetProcessor()->GetPrimaryDevice();
146 if (primary
->GetLogicalAddress() != CECDEVICE_UNREGISTERED
)
148 m_handler
->InitHandler();
150 if (bActivateSource
&& IsHandledByLibCEC() && IsActiveSource())
151 m_handler
->ActivateSource();
160 CCECCommandHandler
*CCECBusDevice::GetHandler(void)
162 ReplaceHandler(false);
167 bool CCECBusDevice::HandleCommand(const cec_command
&command
)
169 bool bHandled(false);
171 /* update "last active" */
173 CLockObject
lock(m_mutex
);
174 m_iLastActive
= GetTimeMs();
176 /* don't call GetStatus() here, just read the value with the mutex locked */
177 if (m_deviceStatus
!= CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
&& command
.opcode_set
== 1)
178 m_deviceStatus
= CEC_DEVICE_STATUS_PRESENT
;
183 /* handle the command */
184 bHandled
= m_handler
->HandleCommand(command
);
186 /* change status to present */
187 if (bHandled
&& GetLogicalAddress() != CECDEVICE_BROADCAST
)
189 CLockObject
lock(m_mutex
);
190 if (m_deviceStatus
!= CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
)
192 if (m_deviceStatus
!= CEC_DEVICE_STATUS_PRESENT
)
193 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "device %s (%x) status changed to present after command %s", GetLogicalAddressName(), (uint8_t)GetLogicalAddress(), ToString(command
.opcode
));
194 m_deviceStatus
= CEC_DEVICE_STATUS_PRESENT
;
202 const char* CCECBusDevice::GetLogicalAddressName(void) const
204 return ToString(m_iLogicalAddress
);
207 bool CCECBusDevice::IsPresent(void)
209 CLockObject
lock(m_mutex
);
210 return m_deviceStatus
== CEC_DEVICE_STATUS_PRESENT
;
213 bool CCECBusDevice::IsHandledByLibCEC(void)
215 CLockObject
lock(m_mutex
);
216 return m_deviceStatus
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
;
219 void CCECBusDevice::SetUnsupportedFeature(cec_opcode opcode
)
221 // some commands should never be marked as unsupported
222 if (opcode
== CEC_OPCODE_VENDOR_COMMAND
||
223 opcode
== CEC_OPCODE_VENDOR_COMMAND_WITH_ID
||
224 opcode
== CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN
||
225 opcode
== CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP
||
226 opcode
== CEC_OPCODE_ABORT
||
227 opcode
== CEC_OPCODE_FEATURE_ABORT
||
228 opcode
== CEC_OPCODE_NONE
)
232 CLockObject
lock(m_mutex
);
233 if (m_unsupportedFeatures
.find(opcode
) == m_unsupportedFeatures
.end())
235 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "marking opcode '%s' as unsupported feature for device '%s'", ToString(opcode
), GetLogicalAddressName());
236 m_unsupportedFeatures
.insert(opcode
);
240 // signal threads that are waiting for a reponse
242 SignalOpcode(cec_command::GetResponseOpcode(opcode
));
246 bool CCECBusDevice::IsUnsupportedFeature(cec_opcode opcode
)
248 CLockObject
lock(m_mutex
);
249 bool bUnsupported
= (m_unsupportedFeatures
.find(opcode
) != m_unsupportedFeatures
.end());
251 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "'%s' is marked as unsupported feature for device '%s'", ToString(opcode
), GetLogicalAddressName());
255 bool CCECBusDevice::TransmitKeypress(const cec_logical_address initiator
, cec_user_control_code key
, bool bWait
/* = true */)
258 bool bReturn
= m_handler
->TransmitKeypress(initiator
, m_iLogicalAddress
, key
, bWait
);
263 bool CCECBusDevice::TransmitKeyRelease(const cec_logical_address initiator
, bool bWait
/* = true */)
266 bool bReturn
= m_handler
->TransmitKeyRelease(initiator
, m_iLogicalAddress
, bWait
);
271 cec_version
CCECBusDevice::GetCecVersion(const cec_logical_address initiator
, bool bUpdate
/* = false */)
273 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
274 bool bRequestUpdate(false);
276 CLockObject
lock(m_mutex
);
277 bRequestUpdate
= bIsPresent
&&
278 (bUpdate
|| m_cecVersion
== CEC_VERSION_UNKNOWN
);
283 CheckVendorIdRequested(initiator
);
284 RequestCecVersion(initiator
);
287 CLockObject
lock(m_mutex
);
291 void CCECBusDevice::SetCecVersion(const cec_version newVersion
)
293 CLockObject
lock(m_mutex
);
294 if (m_cecVersion
!= newVersion
)
295 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): CEC version %s", GetLogicalAddressName(), m_iLogicalAddress
, ToString(newVersion
));
296 m_cecVersion
= newVersion
;
299 bool CCECBusDevice::RequestCecVersion(const cec_logical_address initiator
, bool bWaitForResponse
/* = true */)
303 if (!IsHandledByLibCEC() &&
304 !IsUnsupportedFeature(CEC_OPCODE_GET_CEC_VERSION
))
307 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< requesting CEC version of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
308 bReturn
= m_handler
->TransmitRequestCecVersion(initiator
, m_iLogicalAddress
, bWaitForResponse
);
314 bool CCECBusDevice::TransmitCECVersion(const cec_logical_address destination
)
318 CLockObject
lock(m_mutex
);
319 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): cec version %s", GetLogicalAddressName(), m_iLogicalAddress
, ToString(destination
), destination
, ToString(m_cecVersion
));
320 version
= m_cecVersion
;
324 bool bReturn
= m_handler
->TransmitCECVersion(m_iLogicalAddress
, destination
, version
);
329 cec_menu_language
&CCECBusDevice::GetMenuLanguage(const cec_logical_address initiator
, bool bUpdate
/* = false */)
331 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
332 bool bRequestUpdate(false);
334 CLockObject
lock(m_mutex
);
335 bRequestUpdate
= (bIsPresent
&&
336 (bUpdate
|| !strcmp(m_menuLanguage
.language
, "???")));
341 CheckVendorIdRequested(initiator
);
342 RequestMenuLanguage(initiator
);
345 CLockObject
lock(m_mutex
);
346 return m_menuLanguage
;
349 void CCECBusDevice::SetMenuLanguage(const char *strLanguage
)
354 CLockObject
lock(m_mutex
);
355 if (strcmp(strLanguage
, m_menuLanguage
.language
))
357 memcpy(m_menuLanguage
.language
, strLanguage
, 3);
358 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): menu language set to '%s'", GetLogicalAddressName(), m_iLogicalAddress
, m_menuLanguage
.language
);
362 void CCECBusDevice::SetMenuLanguage(const cec_menu_language
&language
)
364 if (language
.device
== m_iLogicalAddress
)
365 SetMenuLanguage(language
.language
);
368 bool CCECBusDevice::RequestMenuLanguage(const cec_logical_address initiator
, bool bWaitForResponse
/* = true */)
372 if (!IsHandledByLibCEC() &&
373 !IsUnsupportedFeature(CEC_OPCODE_GET_MENU_LANGUAGE
))
376 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< requesting menu language of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
377 bReturn
= m_handler
->TransmitRequestMenuLanguage(initiator
, m_iLogicalAddress
, bWaitForResponse
);
383 bool CCECBusDevice::TransmitSetMenuLanguage(const cec_logical_address destination
)
386 cec_menu_language language
;
388 CLockObject
lock(m_mutex
);
389 language
= m_menuLanguage
;
394 CLockObject
lock(m_mutex
);
395 lang
[0] = language
.language
[0];
396 lang
[1] = language
.language
[1];
397 lang
[2] = language
.language
[2];
401 if (lang
[0] == '?' && lang
[1] == '?' && lang
[2] == '?')
403 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): Menu language feature abort", GetLogicalAddressName(), m_iLogicalAddress
, ToString(destination
), destination
);
404 m_processor
->TransmitAbort(m_iLogicalAddress
, destination
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
409 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> broadcast (F): Menu language '%s'", GetLogicalAddressName(), m_iLogicalAddress
, lang
);
410 bReturn
= m_handler
->TransmitSetMenuLanguage(m_iLogicalAddress
, lang
);
416 bool CCECBusDevice::TransmitOSDString(const cec_logical_address destination
, cec_display_control duration
, const char *strMessage
)
419 if (!m_processor
->GetDevice(destination
)->IsUnsupportedFeature(CEC_OPCODE_SET_OSD_STRING
))
421 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): display OSD message '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(destination
), destination
, strMessage
);
423 bReturn
= m_handler
->TransmitOSDString(m_iLogicalAddress
, destination
, duration
, strMessage
);
429 CStdString
CCECBusDevice::GetCurrentOSDName(void)
431 CLockObject
lock(m_mutex
);
432 return m_strDeviceName
;
435 CStdString
CCECBusDevice::GetOSDName(const cec_logical_address initiator
, bool bUpdate
/* = false */)
437 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
438 bool bRequestUpdate(false);
440 CLockObject
lock(m_mutex
);
441 bRequestUpdate
= bIsPresent
&&
442 (bUpdate
|| m_strDeviceName
.Equals(ToString(m_iLogicalAddress
))) &&
443 m_type
!= CEC_DEVICE_TYPE_TV
;
448 CheckVendorIdRequested(initiator
);
449 RequestOSDName(initiator
);
452 CLockObject
lock(m_mutex
);
453 return m_strDeviceName
;
456 void CCECBusDevice::SetOSDName(CStdString strName
)
458 CLockObject
lock(m_mutex
);
459 if (m_strDeviceName
!= strName
)
461 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): osd name set to '%s'", GetLogicalAddressName(), m_iLogicalAddress
, strName
.c_str());
462 m_strDeviceName
= strName
;
466 bool CCECBusDevice::RequestOSDName(const cec_logical_address initiator
, bool bWaitForResponse
/* = true */)
470 if (!IsHandledByLibCEC() &&
471 !IsUnsupportedFeature(CEC_OPCODE_GIVE_OSD_NAME
))
474 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< requesting OSD name of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
475 bReturn
= m_handler
->TransmitRequestOSDName(initiator
, m_iLogicalAddress
, bWaitForResponse
);
481 bool CCECBusDevice::TransmitOSDName(const cec_logical_address destination
)
483 CStdString strDeviceName
;
485 CLockObject
lock(m_mutex
);
486 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): OSD name '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(destination
), destination
, m_strDeviceName
.c_str());
487 strDeviceName
= m_strDeviceName
;
491 bool bReturn
= m_handler
->TransmitOSDName(m_iLogicalAddress
, destination
, strDeviceName
);
496 bool CCECBusDevice::HasValidPhysicalAddress(void)
498 CLockObject
lock(m_mutex
);
499 return CLibCEC::IsValidPhysicalAddress(m_iPhysicalAddress
);
502 uint16_t CCECBusDevice::GetCurrentPhysicalAddress(void)
504 CLockObject
lock(m_mutex
);
505 return m_iPhysicalAddress
;
508 uint16_t CCECBusDevice::GetPhysicalAddress(const cec_logical_address initiator
, bool bSuppressUpdate
/* = false */)
510 if (!bSuppressUpdate
)
512 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
513 bool bRequestUpdate(false);
515 CLockObject
lock(m_mutex
);
516 bRequestUpdate
= bIsPresent
&& m_iPhysicalAddress
== CEC_INVALID_PHYSICAL_ADDRESS
;
521 CheckVendorIdRequested(initiator
);
522 if (!RequestPhysicalAddress(initiator
))
523 LIB_CEC
->AddLog(CEC_LOG_ERROR
, "failed to request the physical address");
527 CLockObject
lock(m_mutex
);
528 return m_iPhysicalAddress
;
531 bool CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress
)
533 CLockObject
lock(m_mutex
);
534 if (iNewAddress
> 0 && m_iPhysicalAddress
!= iNewAddress
)
536 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): physical address changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress
, m_iPhysicalAddress
, iNewAddress
);
537 m_iPhysicalAddress
= iNewAddress
;
542 bool CCECBusDevice::RequestPhysicalAddress(const cec_logical_address initiator
, bool bWaitForResponse
/* = true */)
546 if (!IsHandledByLibCEC())
549 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< requesting physical address of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
550 bReturn
= m_handler
->TransmitRequestPhysicalAddress(initiator
, m_iLogicalAddress
, bWaitForResponse
);
556 bool CCECBusDevice::TransmitPhysicalAddress(void)
558 uint16_t iPhysicalAddress
;
559 cec_device_type type
;
561 CLockObject
lock(m_mutex
);
562 if (m_iPhysicalAddress
== CEC_INVALID_PHYSICAL_ADDRESS
)
565 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> broadcast (F): physical adddress %4x", GetLogicalAddressName(), m_iLogicalAddress
, m_iPhysicalAddress
);
566 iPhysicalAddress
= m_iPhysicalAddress
;
571 bool bReturn
= m_handler
->TransmitPhysicalAddress(m_iLogicalAddress
, iPhysicalAddress
, type
);
576 cec_power_status
CCECBusDevice::GetCurrentPowerStatus(void)
578 CLockObject
lock(m_mutex
);
579 return m_powerStatus
;
582 cec_power_status
CCECBusDevice::GetPowerStatus(const cec_logical_address initiator
, bool bUpdate
/* = false */)
584 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
585 bool bRequestUpdate(false);
587 CLockObject
lock(m_mutex
);
588 bRequestUpdate
= (bIsPresent
&&
589 (bUpdate
|| m_powerStatus
== CEC_POWER_STATUS_UNKNOWN
||
590 m_powerStatus
== CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
||
591 m_powerStatus
== CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY
||
592 GetTimeMs() - m_iLastPowerStateUpdate
>= CEC_POWER_STATE_REFRESH_TIME
));
597 CheckVendorIdRequested(initiator
);
598 RequestPowerStatus(initiator
);
601 CLockObject
lock(m_mutex
);
602 return m_powerStatus
;
605 void CCECBusDevice::SetPowerStatus(const cec_power_status powerStatus
)
607 CLockObject
lock(m_mutex
);
608 if (m_powerStatus
!= powerStatus
)
610 m_iLastPowerStateUpdate
= GetTimeMs();
611 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): power status changed from '%s' to '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(m_powerStatus
), ToString(powerStatus
));
612 m_powerStatus
= powerStatus
;
616 bool CCECBusDevice::RequestPowerStatus(const cec_logical_address initiator
, bool bWaitForResponse
/* = true */)
620 if (!IsHandledByLibCEC() &&
621 !IsUnsupportedFeature(CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
))
624 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< requesting power status of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
625 bReturn
= m_handler
->TransmitRequestPowerStatus(initiator
, m_iLogicalAddress
, bWaitForResponse
);
631 bool CCECBusDevice::TransmitPowerState(const cec_logical_address destination
)
633 cec_power_status state
;
635 CLockObject
lock(m_mutex
);
636 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): %s", GetLogicalAddressName(), m_iLogicalAddress
, ToString(destination
), destination
, ToString(m_powerStatus
));
637 state
= m_powerStatus
;
641 bool bReturn
= m_handler
->TransmitPowerState(m_iLogicalAddress
, destination
, state
);
646 cec_vendor_id
CCECBusDevice::GetCurrentVendorId(void)
648 CLockObject
lock(m_mutex
);
652 cec_vendor_id
CCECBusDevice::GetVendorId(const cec_logical_address initiator
, bool bUpdate
/* = false */)
654 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
655 bool bRequestUpdate(false);
657 CLockObject
lock(m_mutex
);
658 bRequestUpdate
= (bIsPresent
&&
659 (bUpdate
|| m_vendor
== CEC_VENDOR_UNKNOWN
));
663 RequestVendorId(initiator
);
665 CLockObject
lock(m_mutex
);
669 const char *CCECBusDevice::GetVendorName(const cec_logical_address initiator
, bool bUpdate
/* = false */)
671 return ToString(GetVendorId(initiator
, bUpdate
));
674 bool CCECBusDevice::SetVendorId(uint64_t iVendorId
)
676 bool bVendorChanged(false);
679 CLockObject
lock(m_mutex
);
680 bVendorChanged
= (m_vendor
!= (cec_vendor_id
)iVendorId
);
681 m_vendor
= (cec_vendor_id
)iVendorId
;
685 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): vendor = %s (%06x)", GetLogicalAddressName(), m_iLogicalAddress
, ToString(m_vendor
), m_vendor
);
687 return bVendorChanged
;
690 bool CCECBusDevice::RequestVendorId(const cec_logical_address initiator
, bool bWaitForResponse
/* = true */)
694 if (!IsHandledByLibCEC() && initiator
!= CECDEVICE_UNKNOWN
)
697 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< requesting vendor ID of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
698 bReturn
= m_handler
->TransmitRequestVendorId(initiator
, m_iLogicalAddress
, bWaitForResponse
);
701 if (bWaitForResponse
)
702 ReplaceHandler(true);
707 bool CCECBusDevice::TransmitVendorID(const cec_logical_address destination
, bool bSendAbort
/* = true */)
712 CLockObject
lock(m_mutex
);
713 iVendorId
= (uint64_t)m_vendor
;
717 if (iVendorId
== CEC_VENDOR_UNKNOWN
)
721 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): vendor id feature abort", GetLogicalAddressName(), m_iLogicalAddress
, ToString(destination
), destination
);
722 m_processor
->TransmitAbort(m_iLogicalAddress
, destination
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
728 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
);
729 bReturn
= m_handler
->TransmitVendorID(m_iLogicalAddress
, iVendorId
);
735 cec_bus_device_status
CCECBusDevice::GetStatus(bool bForcePoll
/* = false */, bool bSuppressPoll
/* = false */)
737 cec_bus_device_status
status(CEC_DEVICE_STATUS_UNKNOWN
);
738 bool bNeedsPoll(false);
741 CLockObject
lock(m_mutex
);
742 status
= m_deviceStatus
;
743 bNeedsPoll
= !bSuppressPoll
&&
744 (bForcePoll
|| m_deviceStatus
== CEC_DEVICE_STATUS_UNKNOWN
);
749 bool bPollAcked(false);
750 if (bNeedsPoll
&& NeedsPoll())
751 bPollAcked
= m_processor
->PollDevice(m_iLogicalAddress
);
753 status
= bPollAcked
? CEC_DEVICE_STATUS_PRESENT
: CEC_DEVICE_STATUS_NOT_PRESENT
;
754 SetDeviceStatus(status
);
760 void CCECBusDevice::SetDeviceStatus(const cec_bus_device_status newStatus
)
763 CLockObject
lock(m_mutex
);
766 case CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
:
767 if (m_deviceStatus
!= newStatus
)
768 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): device status changed into 'handled by libCEC'", GetLogicalAddressName(), m_iLogicalAddress
);
769 SetPowerStatus (CEC_POWER_STATUS_ON
);
770 SetVendorId (CEC_VENDOR_UNKNOWN
);
771 SetMenuState (CEC_MENU_STATE_ACTIVATED
);
772 SetCecVersion (CEC_VERSION_1_3A
);
773 SetStreamPath (CEC_INVALID_PHYSICAL_ADDRESS
);
774 MarkAsInactiveSource();
776 m_deviceStatus
= newStatus
;
778 case CEC_DEVICE_STATUS_PRESENT
:
779 if (m_deviceStatus
!= newStatus
)
780 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): device status changed into 'present'", GetLogicalAddressName(), m_iLogicalAddress
);
781 m_deviceStatus
= newStatus
;
783 case CEC_DEVICE_STATUS_NOT_PRESENT
:
784 if (m_deviceStatus
!= newStatus
)
786 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): device status changed into 'not present'", GetLogicalAddressName(), m_iLogicalAddress
);
788 m_deviceStatus
= newStatus
;
798 void CCECBusDevice::ResetDeviceStatus(void)
800 CLockObject
lock(m_mutex
);
801 SetPowerStatus (CEC_POWER_STATUS_UNKNOWN
);
802 SetVendorId (CEC_VENDOR_UNKNOWN
);
803 SetMenuState (CEC_MENU_STATE_ACTIVATED
);
804 SetCecVersion (CEC_VERSION_UNKNOWN
);
805 SetStreamPath (CEC_INVALID_PHYSICAL_ADDRESS
);
806 SetOSDName (ToString(m_iLogicalAddress
));
807 MarkAsInactiveSource();
810 m_bVendorIdRequested
= false;
811 m_unsupportedFeatures
.clear();
812 m_waitForResponse
->Clear();
814 if (m_deviceStatus
!= CEC_DEVICE_STATUS_UNKNOWN
)
815 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): device status changed into 'unknown'", GetLogicalAddressName(), m_iLogicalAddress
);
816 m_deviceStatus
= CEC_DEVICE_STATUS_UNKNOWN
;
819 bool CCECBusDevice::TransmitPoll(const cec_logical_address dest
)
822 cec_logical_address
destination(dest
);
823 if (destination
== CECDEVICE_UNKNOWN
)
824 destination
= m_iLogicalAddress
;
826 CCECBusDevice
*destDevice
= m_processor
->GetDevice(destination
);
827 if (destDevice
->m_deviceStatus
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
)
831 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): POLL", GetLogicalAddressName(), m_iLogicalAddress
, ToString(dest
), dest
);
832 bReturn
= m_handler
->TransmitPoll(m_iLogicalAddress
, destination
);
833 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, bReturn
? ">> POLL sent" : ">> POLL not sent");
835 CLockObject
lock(m_mutex
);
838 m_iLastActive
= GetTimeMs();
839 destDevice
->m_deviceStatus
= CEC_DEVICE_STATUS_PRESENT
;
842 destDevice
->m_deviceStatus
= CEC_DEVICE_STATUS_NOT_PRESENT
;
848 void CCECBusDevice::HandlePoll(const cec_logical_address destination
)
850 if (destination
>= 0 && destination
< CECDEVICE_BROADCAST
)
852 CCECBusDevice
*device
= m_processor
->GetDevice(destination
);
854 device
->HandlePollFrom(m_iLogicalAddress
);
858 void CCECBusDevice::HandlePollFrom(const cec_logical_address initiator
)
860 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "<< POLL: %s (%x) -> %s (%x)", ToString(initiator
), initiator
, ToString(m_iLogicalAddress
), m_iLogicalAddress
);
861 m_bAwaitingReceiveFailed
= true;
864 bool CCECBusDevice::HandleReceiveFailed(void)
866 bool bReturn
= m_bAwaitingReceiveFailed
;
867 m_bAwaitingReceiveFailed
= false;
871 cec_menu_state
CCECBusDevice::GetMenuState(const cec_logical_address
UNUSED(initiator
))
873 CLockObject
lock(m_mutex
);
877 void CCECBusDevice::SetMenuState(const cec_menu_state state
)
879 CLockObject
lock(m_mutex
);
880 if (m_menuState
!= state
)
882 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): menu state set to '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(m_menuState
));
887 bool CCECBusDevice::TransmitMenuState(const cec_logical_address dest
)
889 cec_menu_state menuState
;
891 CLockObject
lock(m_mutex
);
892 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): menu state '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(dest
), dest
, ToString(m_menuState
));
893 menuState
= m_menuState
;
897 bool bReturn
= m_handler
->TransmitMenuState(m_iLogicalAddress
, dest
, menuState
);
902 bool CCECBusDevice::ActivateSource(uint64_t iDelay
/* = 0 */)
904 MarkAsActiveSource();
909 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "sending active source message for '%s'", ToString(m_iLogicalAddress
));
910 bReturn
= m_handler
->ActivateSource();
914 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "scheduling active source message for '%s'", ToString(m_iLogicalAddress
));
915 m_handler
->ScheduleActivateSource(iDelay
);
921 bool CCECBusDevice::RequestActiveSource(bool bWaitForResponse
/* = true */)
925 if (IsHandledByLibCEC())
928 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< requesting active source");
930 bReturn
= m_handler
->TransmitRequestActiveSource(m_iLogicalAddress
, bWaitForResponse
);
936 void CCECBusDevice::MarkAsActiveSource(void)
938 bool bWasActivated(false);
940 // set the power status to powered on
941 SetPowerStatus(CEC_POWER_STATUS_ON
);
943 // mark this device as active source
945 CLockObject
lock(m_mutex
);
946 if (!m_bActiveSource
)
948 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "making %s (%x) the active source", GetLogicalAddressName(), m_iLogicalAddress
);
949 bWasActivated
= true;
952 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%x) was already marked as active source", GetLogicalAddressName(), m_iLogicalAddress
);
954 m_bActiveSource
= true;
957 // mark other devices as inactive sources
958 CECDEVICEVEC devices
;
959 m_processor
->GetDevices()->Get(devices
);
960 for (CECDEVICEVEC::iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
961 if ((*it
)->GetLogicalAddress() != m_iLogicalAddress
)
962 (*it
)->MarkAsInactiveSource();
966 CCECClient
*client
= GetClient();
968 client
->SourceActivated(m_iLogicalAddress
);
972 void CCECBusDevice::MarkAsInactiveSource(void)
974 bool bWasDeactivated(false);
976 CLockObject
lock(m_mutex
);
979 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "marking %s (%X) as inactive source", GetLogicalAddressName(), m_iLogicalAddress
);
980 bWasDeactivated
= true;
982 m_bActiveSource
= false;
987 CCECClient
*client
= GetClient();
989 client
->SourceDeactivated(m_iLogicalAddress
);
993 bool CCECBusDevice::TransmitActiveSource(void)
995 bool bSendActiveSource(false);
996 uint16_t iPhysicalAddress(CEC_INVALID_PHYSICAL_ADDRESS
);
999 CLockObject
lock(m_mutex
);
1000 if (!HasValidPhysicalAddress())
1002 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X) has an invalid physical address (%04x), not sending active source commands", GetLogicalAddressName(), m_iLogicalAddress
, m_iPhysicalAddress
);
1006 iPhysicalAddress
= m_iPhysicalAddress
;
1008 if (m_powerStatus
!= CEC_POWER_STATUS_ON
&& m_powerStatus
!= CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
)
1009 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress
);
1010 else if (m_bActiveSource
)
1012 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> broadcast (F): active source (%4x)", GetLogicalAddressName(), m_iLogicalAddress
, m_iPhysicalAddress
);
1013 bSendActiveSource
= true;
1016 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "<< %s (%X) is not the active source", GetLogicalAddressName(), m_iLogicalAddress
);
1019 bool bActiveSourceSent(false);
1020 if (bSendActiveSource
)
1023 bActiveSourceSent
= m_handler
->TransmitActiveSource(m_iLogicalAddress
, iPhysicalAddress
);
1027 return bActiveSourceSent
;
1030 bool CCECBusDevice::TransmitImageViewOn(void)
1033 CLockObject
lock(m_mutex
);
1034 if (m_powerStatus
!= CEC_POWER_STATUS_ON
&& m_powerStatus
!= CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
)
1036 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress
);
1041 bool bImageViewOnSent(false);
1043 bImageViewOnSent
= m_handler
->TransmitImageViewOn(m_iLogicalAddress
, CECDEVICE_TV
);
1045 return bImageViewOnSent
;
1048 bool CCECBusDevice::TransmitInactiveSource(void)
1050 uint16_t iPhysicalAddress
;
1052 CLockObject
lock(m_mutex
);
1053 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> broadcast (F): inactive source", GetLogicalAddressName(), m_iLogicalAddress
);
1054 iPhysicalAddress
= m_iPhysicalAddress
;
1058 bool bReturn
= m_handler
->TransmitInactiveSource(m_iLogicalAddress
, iPhysicalAddress
);
1063 bool CCECBusDevice::TransmitPendingActiveSourceCommands(void)
1066 bool bReturn
= m_handler
->ActivateSource(true);
1071 void CCECBusDevice::SetStreamPath(uint16_t iNewAddress
, uint16_t iOldAddress
/* = CEC_INVALID_PHYSICAL_ADDRESS */)
1073 CLockObject
lock(m_mutex
);
1074 if (iNewAddress
!= m_iStreamPath
)
1076 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): stream path changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress
, iOldAddress
== 0 ? m_iStreamPath
: iOldAddress
, iNewAddress
);
1077 m_iStreamPath
= iNewAddress
;
1080 if (!LIB_CEC
->IsValidPhysicalAddress(iNewAddress
))
1083 CCECBusDevice
*device
= m_processor
->GetDeviceByPhysicalAddress(iNewAddress
);
1086 // if a device is found with the new physical address, mark it as active, which will automatically mark all other devices as inactive
1087 device
->MarkAsActiveSource();
1091 // try to find the device with the old address, and mark it as inactive when found
1092 device
= m_processor
->GetDeviceByPhysicalAddress(iOldAddress
);
1094 device
->MarkAsInactiveSource();
1098 bool CCECBusDevice::PowerOn(const cec_logical_address initiator
)
1100 bool bReturn(false);
1101 GetVendorId(initiator
); // ensure that we got the vendor id, because the implementations vary per vendor
1104 cec_power_status currentStatus
= GetPowerStatus(initiator
, false);
1105 if (currentStatus
!= CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
&&
1106 currentStatus
!= CEC_POWER_STATUS_ON
)
1108 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< powering on '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
1109 if (m_handler
->PowerOn(initiator
, m_iLogicalAddress
))
1111 SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
1117 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "'%s' (%X) is already '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(currentStatus
));
1124 bool CCECBusDevice::Standby(const cec_logical_address initiator
)
1126 GetVendorId(initiator
); // ensure that we got the vendor id, because the implementations vary per vendor
1128 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "<< putting '%s' (%X) in standby mode", GetLogicalAddressName(), m_iLogicalAddress
);
1130 bool bReturn
= m_handler
->TransmitStandby(initiator
, m_iLogicalAddress
);
1135 bool CCECBusDevice::NeedsPoll(void)
1137 bool bSendPoll(false);
1138 cec_logical_address
pollAddress(CECDEVICE_UNKNOWN
);
1139 switch (m_iLogicalAddress
)
1141 case CECDEVICE_PLAYBACKDEVICE3
:
1142 pollAddress
= CECDEVICE_PLAYBACKDEVICE2
;
1144 case CECDEVICE_PLAYBACKDEVICE2
:
1145 pollAddress
= CECDEVICE_PLAYBACKDEVICE1
;
1147 case CECDEVICE_RECORDINGDEVICE3
:
1148 pollAddress
= CECDEVICE_RECORDINGDEVICE2
;
1150 case CECDEVICE_RECORDINGDEVICE2
:
1151 pollAddress
= CECDEVICE_RECORDINGDEVICE1
;
1153 case CECDEVICE_TUNER4
:
1154 pollAddress
= CECDEVICE_TUNER3
;
1156 case CECDEVICE_TUNER3
:
1157 pollAddress
= CECDEVICE_TUNER2
;
1159 case CECDEVICE_TUNER2
:
1160 pollAddress
= CECDEVICE_TUNER1
;
1162 case CECDEVICE_AUDIOSYSTEM
:
1163 case CECDEVICE_PLAYBACKDEVICE1
:
1164 case CECDEVICE_RECORDINGDEVICE1
:
1165 case CECDEVICE_TUNER1
:
1173 if (!bSendPoll
&& pollAddress
!= CECDEVICE_UNKNOWN
)
1175 CCECBusDevice
*device
= m_processor
->GetDevice(pollAddress
);
1178 cec_bus_device_status status
= device
->GetStatus();
1179 bSendPoll
= (status
== CEC_DEVICE_STATUS_PRESENT
|| status
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
1190 void CCECBusDevice::CheckVendorIdRequested(const cec_logical_address initiator
)
1192 bool bRequestVendorId(false);
1194 CLockObject
lock(m_mutex
);
1195 bRequestVendorId
= !m_bVendorIdRequested
;
1196 m_bVendorIdRequested
= true;
1199 if (bRequestVendorId
)
1201 ReplaceHandler(false);
1202 GetVendorId(initiator
);
1207 CCECAudioSystem
*CCECBusDevice::AsAudioSystem(void)
1209 return AsAudioSystem(this);
1212 CCECPlaybackDevice
*CCECBusDevice::AsPlaybackDevice(void)
1214 return AsPlaybackDevice(this);
1217 CCECRecordingDevice
*CCECBusDevice::AsRecordingDevice(void)
1219 return AsRecordingDevice(this);
1222 CCECTuner
*CCECBusDevice::AsTuner(void)
1224 return AsTuner(this);
1227 CCECTV
*CCECBusDevice::AsTV(void)
1232 CCECAudioSystem
*CCECBusDevice::AsAudioSystem(CCECBusDevice
*device
)
1234 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
1235 return static_cast<CCECAudioSystem
*>(device
);
1239 CCECPlaybackDevice
*CCECBusDevice::AsPlaybackDevice(CCECBusDevice
*device
)
1242 (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
||
1243 device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
))
1244 return static_cast<CCECPlaybackDevice
*>(device
);
1248 CCECRecordingDevice
*CCECBusDevice::AsRecordingDevice(CCECBusDevice
*device
)
1250 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
1251 return static_cast<CCECRecordingDevice
*>(device
);
1255 CCECTuner
*CCECBusDevice::AsTuner(CCECBusDevice
*device
)
1257 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_TUNER
)
1258 return static_cast<CCECTuner
*>(device
);
1262 CCECTV
*CCECBusDevice::AsTV(CCECBusDevice
*device
)
1264 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_TV
)
1265 return static_cast<CCECTV
*>(device
);
1269 void CCECBusDevice::MarkBusy(void)
1271 CLockObject
handlerLock(m_handlerMutex
);
1272 ++m_iHandlerUseCount
;
1275 void CCECBusDevice::MarkReady(void)
1277 CLockObject
handlerLock(m_handlerMutex
);
1278 if (m_iHandlerUseCount
> 0)
1279 --m_iHandlerUseCount
;
1282 bool CCECBusDevice::TryLogicalAddress(void)
1284 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "trying logical address '%s'", GetLogicalAddressName());
1286 if (!TransmitPoll(m_iLogicalAddress
))
1288 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "using logical address '%s'", GetLogicalAddressName());
1289 SetDeviceStatus(CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
1294 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "logical address '%s' already taken", GetLogicalAddressName());
1295 SetDeviceStatus(CEC_DEVICE_STATUS_PRESENT
);
1299 CCECClient
*CCECBusDevice::GetClient(void)
1301 return m_processor
->GetClient(m_iLogicalAddress
);
1304 void CCECBusDevice::SignalOpcode(cec_opcode opcode
)
1306 m_waitForResponse
->Received(opcode
);
1309 bool CCECBusDevice::WaitForOpcode(cec_opcode opcode
)
1311 return m_waitForResponse
->Wait(opcode
);