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 m_handler
->InitHandler();
138 if (bActivateSource
&& IsHandledByLibCEC() && IsActiveSource())
139 m_handler
->ActivateSource();
147 bool CCECBusDevice::HandleCommand(const cec_command
&command
)
149 bool bHandled(false);
151 /* update "last active" */
153 CLockObject
lock(m_mutex
);
154 m_iLastActive
= GetTimeMs();
156 /* don't call GetStatus() here, just read the value with the mutex locked */
157 if (m_deviceStatus
!= CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
&& command
.opcode_set
== 1)
158 m_deviceStatus
= CEC_DEVICE_STATUS_PRESENT
;
163 /* handle the command */
164 bHandled
= m_handler
->HandleCommand(command
);
166 /* change status to present */
167 if (bHandled
&& GetLogicalAddress() != CECDEVICE_BROADCAST
)
169 CLockObject
lock(m_mutex
);
170 if (m_deviceStatus
!= CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
)
172 if (m_deviceStatus
!= CEC_DEVICE_STATUS_PRESENT
)
173 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "device %s (%x) status changed to present after command %s", GetLogicalAddressName(), (uint8_t)GetLogicalAddress(), ToString(command
.opcode
));
174 m_deviceStatus
= CEC_DEVICE_STATUS_PRESENT
;
182 const char* CCECBusDevice::GetLogicalAddressName(void) const
184 return ToString(m_iLogicalAddress
);
187 bool CCECBusDevice::IsPresent(void)
189 CLockObject
lock(m_mutex
);
190 return m_deviceStatus
== CEC_DEVICE_STATUS_PRESENT
;
193 bool CCECBusDevice::IsHandledByLibCEC(void)
195 CLockObject
lock(m_mutex
);
196 return m_deviceStatus
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
;
199 void CCECBusDevice::SetUnsupportedFeature(cec_opcode opcode
)
201 // some commands should never be marked as unsupported
202 if (opcode
== CEC_OPCODE_VENDOR_COMMAND
||
203 opcode
== CEC_OPCODE_VENDOR_COMMAND_WITH_ID
||
204 opcode
== CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN
||
205 opcode
== CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP
||
206 opcode
== CEC_OPCODE_ABORT
||
207 opcode
== CEC_OPCODE_FEATURE_ABORT
||
208 opcode
== CEC_OPCODE_NONE
)
212 CLockObject
lock(m_mutex
);
213 if (m_unsupportedFeatures
.find(opcode
) == m_unsupportedFeatures
.end())
215 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "marking opcode '%s' as unsupported feature for device '%s'", ToString(opcode
), GetLogicalAddressName());
216 m_unsupportedFeatures
.insert(opcode
);
220 // signal threads that are waiting for a reponse
222 m_handler
->SignalOpcode(cec_command::GetResponseOpcode(opcode
));
226 bool CCECBusDevice::IsUnsupportedFeature(cec_opcode opcode
)
228 CLockObject
lock(m_mutex
);
229 bool bUnsupported
= (m_unsupportedFeatures
.find(opcode
) != m_unsupportedFeatures
.end());
231 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "'%s' is marked as unsupported feature for device '%s'", ToString(opcode
), GetLogicalAddressName());
235 bool CCECBusDevice::TransmitKeypress(const cec_logical_address initiator
, cec_user_control_code key
, bool bWait
/* = true */)
238 bool bReturn
= m_handler
->TransmitKeypress(initiator
, m_iLogicalAddress
, key
, bWait
);
243 bool CCECBusDevice::TransmitKeyRelease(const cec_logical_address initiator
, bool bWait
/* = true */)
246 bool bReturn
= m_handler
->TransmitKeyRelease(initiator
, m_iLogicalAddress
, bWait
);
251 cec_version
CCECBusDevice::GetCecVersion(const cec_logical_address initiator
, bool bUpdate
/* = false */)
253 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
254 bool bRequestUpdate(false);
256 CLockObject
lock(m_mutex
);
257 bRequestUpdate
= bIsPresent
&&
258 (bUpdate
|| m_cecVersion
== CEC_VERSION_UNKNOWN
);
263 CheckVendorIdRequested(initiator
);
264 RequestCecVersion(initiator
);
267 CLockObject
lock(m_mutex
);
271 void CCECBusDevice::SetCecVersion(const cec_version newVersion
)
273 CLockObject
lock(m_mutex
);
274 if (m_cecVersion
!= newVersion
)
275 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): CEC version %s", GetLogicalAddressName(), m_iLogicalAddress
, ToString(newVersion
));
276 m_cecVersion
= newVersion
;
279 bool CCECBusDevice::RequestCecVersion(const cec_logical_address initiator
, bool bWaitForResponse
/* = true */)
283 if (!IsHandledByLibCEC() &&
284 !IsUnsupportedFeature(CEC_OPCODE_GET_CEC_VERSION
))
287 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< requesting CEC version of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
288 bReturn
= m_handler
->TransmitRequestCecVersion(initiator
, m_iLogicalAddress
, bWaitForResponse
);
294 bool CCECBusDevice::TransmitCECVersion(const cec_logical_address destination
)
298 CLockObject
lock(m_mutex
);
299 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): cec version %s", GetLogicalAddressName(), m_iLogicalAddress
, ToString(destination
), destination
, ToString(m_cecVersion
));
300 version
= m_cecVersion
;
304 bool bReturn
= m_handler
->TransmitCECVersion(m_iLogicalAddress
, destination
, version
);
309 cec_menu_language
&CCECBusDevice::GetMenuLanguage(const cec_logical_address initiator
, bool bUpdate
/* = false */)
311 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
312 bool bRequestUpdate(false);
314 CLockObject
lock(m_mutex
);
315 bRequestUpdate
= (bIsPresent
&&
316 (bUpdate
|| !strcmp(m_menuLanguage
.language
, "???")));
321 CheckVendorIdRequested(initiator
);
322 RequestMenuLanguage(initiator
);
325 CLockObject
lock(m_mutex
);
326 return m_menuLanguage
;
329 void CCECBusDevice::SetMenuLanguage(const char *strLanguage
)
334 CLockObject
lock(m_mutex
);
335 if (strcmp(strLanguage
, m_menuLanguage
.language
))
337 memcpy(m_menuLanguage
.language
, strLanguage
, 3);
338 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): menu language set to '%s'", GetLogicalAddressName(), m_iLogicalAddress
, m_menuLanguage
.language
);
342 void CCECBusDevice::SetMenuLanguage(const cec_menu_language
&language
)
344 if (language
.device
== m_iLogicalAddress
)
345 SetMenuLanguage(language
.language
);
348 bool CCECBusDevice::RequestMenuLanguage(const cec_logical_address initiator
, bool bWaitForResponse
/* = true */)
352 if (!IsHandledByLibCEC() &&
353 !IsUnsupportedFeature(CEC_OPCODE_GET_MENU_LANGUAGE
))
356 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< requesting menu language of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
357 bReturn
= m_handler
->TransmitRequestMenuLanguage(initiator
, m_iLogicalAddress
, bWaitForResponse
);
363 bool CCECBusDevice::TransmitSetMenuLanguage(const cec_logical_address destination
)
366 cec_menu_language language
;
368 CLockObject
lock(m_mutex
);
369 language
= m_menuLanguage
;
374 CLockObject
lock(m_mutex
);
375 lang
[0] = language
.language
[0];
376 lang
[1] = language
.language
[1];
377 lang
[2] = language
.language
[2];
381 if (lang
[0] == '?' && lang
[1] == '?' && lang
[2] == '?')
383 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): Menu language feature abort", GetLogicalAddressName(), m_iLogicalAddress
, ToString(destination
), destination
);
384 m_processor
->TransmitAbort(m_iLogicalAddress
, destination
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
389 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> broadcast (F): Menu language '%s'", GetLogicalAddressName(), m_iLogicalAddress
, lang
);
390 bReturn
= m_handler
->TransmitSetMenuLanguage(m_iLogicalAddress
, lang
);
396 bool CCECBusDevice::TransmitOSDString(const cec_logical_address destination
, cec_display_control duration
, const char *strMessage
)
399 if (!m_processor
->GetDevice(destination
)->IsUnsupportedFeature(CEC_OPCODE_SET_OSD_STRING
))
401 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): display OSD message '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(destination
), destination
, strMessage
);
403 bReturn
= m_handler
->TransmitOSDString(m_iLogicalAddress
, destination
, duration
, strMessage
);
409 CStdString
CCECBusDevice::GetCurrentOSDName(void)
411 CLockObject
lock(m_mutex
);
412 return m_strDeviceName
;
415 CStdString
CCECBusDevice::GetOSDName(const cec_logical_address initiator
, bool bUpdate
/* = false */)
417 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
418 bool bRequestUpdate(false);
420 CLockObject
lock(m_mutex
);
421 bRequestUpdate
= bIsPresent
&&
422 (bUpdate
|| m_strDeviceName
.Equals(ToString(m_iLogicalAddress
))) &&
423 m_type
!= CEC_DEVICE_TYPE_TV
;
428 CheckVendorIdRequested(initiator
);
429 RequestOSDName(initiator
);
432 CLockObject
lock(m_mutex
);
433 return m_strDeviceName
;
436 void CCECBusDevice::SetOSDName(CStdString strName
)
438 CLockObject
lock(m_mutex
);
439 if (m_strDeviceName
!= strName
)
441 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): osd name set to '%s'", GetLogicalAddressName(), m_iLogicalAddress
, strName
.c_str());
442 m_strDeviceName
= strName
;
446 bool CCECBusDevice::RequestOSDName(const cec_logical_address initiator
, bool bWaitForResponse
/* = true */)
450 if (!IsHandledByLibCEC() &&
451 !IsUnsupportedFeature(CEC_OPCODE_GIVE_OSD_NAME
))
454 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< requesting OSD name of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
455 bReturn
= m_handler
->TransmitRequestOSDName(initiator
, m_iLogicalAddress
, bWaitForResponse
);
461 bool CCECBusDevice::TransmitOSDName(const cec_logical_address destination
)
463 CStdString strDeviceName
;
465 CLockObject
lock(m_mutex
);
466 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): OSD name '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(destination
), destination
, m_strDeviceName
.c_str());
467 strDeviceName
= m_strDeviceName
;
471 bool bReturn
= m_handler
->TransmitOSDName(m_iLogicalAddress
, destination
, strDeviceName
);
476 bool CCECBusDevice::HasValidPhysicalAddress(void)
478 CLockObject
lock(m_mutex
);
479 return CLibCEC::IsValidPhysicalAddress(m_iPhysicalAddress
);
482 uint16_t CCECBusDevice::GetCurrentPhysicalAddress(void)
484 CLockObject
lock(m_mutex
);
485 return m_iPhysicalAddress
;
488 uint16_t CCECBusDevice::GetPhysicalAddress(const cec_logical_address initiator
, bool bSuppressUpdate
/* = false */)
490 if (!bSuppressUpdate
)
492 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
493 bool bRequestUpdate(false);
495 CLockObject
lock(m_mutex
);
496 bRequestUpdate
= bIsPresent
&& m_iPhysicalAddress
== CEC_INVALID_PHYSICAL_ADDRESS
;
501 CheckVendorIdRequested(initiator
);
502 if (!RequestPhysicalAddress(initiator
))
503 LIB_CEC
->AddLog(CEC_LOG_ERROR
, "failed to request the physical address");
507 CLockObject
lock(m_mutex
);
508 return m_iPhysicalAddress
;
511 bool CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress
)
513 CLockObject
lock(m_mutex
);
514 if (iNewAddress
> 0 && m_iPhysicalAddress
!= iNewAddress
)
516 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): physical address changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress
, m_iPhysicalAddress
, iNewAddress
);
517 m_iPhysicalAddress
= iNewAddress
;
522 bool CCECBusDevice::RequestPhysicalAddress(const cec_logical_address initiator
, bool bWaitForResponse
/* = true */)
526 if (!IsHandledByLibCEC())
529 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< requesting physical address of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
530 bReturn
= m_handler
->TransmitRequestPhysicalAddress(initiator
, m_iLogicalAddress
, bWaitForResponse
);
536 bool CCECBusDevice::TransmitPhysicalAddress(void)
538 uint16_t iPhysicalAddress
;
539 cec_device_type type
;
541 CLockObject
lock(m_mutex
);
542 if (m_iPhysicalAddress
== CEC_INVALID_PHYSICAL_ADDRESS
)
545 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> broadcast (F): physical adddress %4x", GetLogicalAddressName(), m_iLogicalAddress
, m_iPhysicalAddress
);
546 iPhysicalAddress
= m_iPhysicalAddress
;
551 bool bReturn
= m_handler
->TransmitPhysicalAddress(m_iLogicalAddress
, iPhysicalAddress
, type
);
556 cec_power_status
CCECBusDevice::GetCurrentPowerStatus(void)
558 CLockObject
lock(m_mutex
);
559 return m_powerStatus
;
562 cec_power_status
CCECBusDevice::GetPowerStatus(const cec_logical_address initiator
, bool bUpdate
/* = false */)
564 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
565 bool bRequestUpdate(false);
567 CLockObject
lock(m_mutex
);
568 bRequestUpdate
= (bIsPresent
&&
569 (bUpdate
|| m_powerStatus
== CEC_POWER_STATUS_UNKNOWN
||
570 m_powerStatus
== CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
||
571 m_powerStatus
== CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY
||
572 GetTimeMs() - m_iLastPowerStateUpdate
>= CEC_POWER_STATE_REFRESH_TIME
));
577 CheckVendorIdRequested(initiator
);
578 RequestPowerStatus(initiator
);
581 CLockObject
lock(m_mutex
);
582 return m_powerStatus
;
585 void CCECBusDevice::SetPowerStatus(const cec_power_status powerStatus
)
587 CLockObject
lock(m_mutex
);
588 if (m_powerStatus
!= powerStatus
)
590 m_iLastPowerStateUpdate
= GetTimeMs();
591 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): power status changed from '%s' to '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(m_powerStatus
), ToString(powerStatus
));
592 m_powerStatus
= powerStatus
;
596 bool CCECBusDevice::RequestPowerStatus(const cec_logical_address initiator
, bool bWaitForResponse
/* = true */)
600 if (!IsHandledByLibCEC() &&
601 !IsUnsupportedFeature(CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
))
604 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< requesting power status of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
605 bReturn
= m_handler
->TransmitRequestPowerStatus(initiator
, m_iLogicalAddress
, bWaitForResponse
);
611 bool CCECBusDevice::TransmitPowerState(const cec_logical_address destination
)
613 cec_power_status state
;
615 CLockObject
lock(m_mutex
);
616 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): %s", GetLogicalAddressName(), m_iLogicalAddress
, ToString(destination
), destination
, ToString(m_powerStatus
));
617 state
= m_powerStatus
;
621 bool bReturn
= m_handler
->TransmitPowerState(m_iLogicalAddress
, destination
, state
);
626 cec_vendor_id
CCECBusDevice::GetCurrentVendorId(void)
628 CLockObject
lock(m_mutex
);
632 cec_vendor_id
CCECBusDevice::GetVendorId(const cec_logical_address initiator
, bool bUpdate
/* = false */)
634 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
635 bool bRequestUpdate(false);
637 CLockObject
lock(m_mutex
);
638 bRequestUpdate
= (bIsPresent
&&
639 (bUpdate
|| m_vendor
== CEC_VENDOR_UNKNOWN
));
643 RequestVendorId(initiator
);
645 CLockObject
lock(m_mutex
);
649 const char *CCECBusDevice::GetVendorName(const cec_logical_address initiator
, bool bUpdate
/* = false */)
651 return ToString(GetVendorId(initiator
, bUpdate
));
654 bool CCECBusDevice::SetVendorId(uint64_t iVendorId
)
656 bool bVendorChanged(false);
659 CLockObject
lock(m_mutex
);
660 bVendorChanged
= (m_vendor
!= (cec_vendor_id
)iVendorId
);
661 m_vendor
= (cec_vendor_id
)iVendorId
;
665 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): vendor = %s (%06x)", GetLogicalAddressName(), m_iLogicalAddress
, ToString(m_vendor
), m_vendor
);
667 return bVendorChanged
;
670 bool CCECBusDevice::RequestVendorId(const cec_logical_address initiator
, bool bWaitForResponse
/* = true */)
674 if (!IsHandledByLibCEC() && initiator
!= CECDEVICE_UNKNOWN
)
677 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< requesting vendor ID of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
678 bReturn
= m_handler
->TransmitRequestVendorId(initiator
, m_iLogicalAddress
, bWaitForResponse
);
681 if (bWaitForResponse
)
682 ReplaceHandler(true);
687 bool CCECBusDevice::TransmitVendorID(const cec_logical_address destination
, bool bSendAbort
/* = true */)
692 CLockObject
lock(m_mutex
);
693 iVendorId
= (uint64_t)m_vendor
;
697 if (iVendorId
== CEC_VENDOR_UNKNOWN
)
701 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): vendor id feature abort", GetLogicalAddressName(), m_iLogicalAddress
, ToString(destination
), destination
);
702 m_processor
->TransmitAbort(m_iLogicalAddress
, destination
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
708 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
);
709 bReturn
= m_handler
->TransmitVendorID(m_iLogicalAddress
, iVendorId
);
715 cec_bus_device_status
CCECBusDevice::GetStatus(bool bForcePoll
/* = false */, bool bSuppressPoll
/* = false */)
717 cec_bus_device_status
status(CEC_DEVICE_STATUS_UNKNOWN
);
718 bool bNeedsPoll(false);
721 CLockObject
lock(m_mutex
);
722 status
= m_deviceStatus
;
723 bNeedsPoll
= !bSuppressPoll
&&
724 (bForcePoll
|| m_deviceStatus
== CEC_DEVICE_STATUS_UNKNOWN
);
729 bool bPollAcked(false);
730 if (bNeedsPoll
&& NeedsPoll())
731 bPollAcked
= m_processor
->PollDevice(m_iLogicalAddress
);
733 status
= bPollAcked
? CEC_DEVICE_STATUS_PRESENT
: CEC_DEVICE_STATUS_NOT_PRESENT
;
734 SetDeviceStatus(status
);
740 void CCECBusDevice::SetDeviceStatus(const cec_bus_device_status newStatus
)
743 CLockObject
lock(m_mutex
);
746 case CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
:
747 if (m_deviceStatus
!= newStatus
)
748 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): device status changed into 'handled by libCEC'", GetLogicalAddressName(), m_iLogicalAddress
);
749 SetPowerStatus (CEC_POWER_STATUS_ON
);
750 SetVendorId (CEC_VENDOR_UNKNOWN
);
751 SetMenuState (CEC_MENU_STATE_ACTIVATED
);
752 SetCecVersion (CEC_VERSION_1_3A
);
753 SetStreamPath (CEC_INVALID_PHYSICAL_ADDRESS
);
754 MarkAsInactiveSource();
756 m_deviceStatus
= newStatus
;
758 case CEC_DEVICE_STATUS_PRESENT
:
759 if (m_deviceStatus
!= newStatus
)
760 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): device status changed into 'present'", GetLogicalAddressName(), m_iLogicalAddress
);
761 m_deviceStatus
= newStatus
;
763 case CEC_DEVICE_STATUS_NOT_PRESENT
:
764 if (m_deviceStatus
!= newStatus
)
766 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): device status changed into 'not present'", GetLogicalAddressName(), m_iLogicalAddress
);
768 m_deviceStatus
= newStatus
;
778 void CCECBusDevice::ResetDeviceStatus(void)
780 CLockObject
lock(m_mutex
);
781 SetPowerStatus (CEC_POWER_STATUS_UNKNOWN
);
782 SetVendorId (CEC_VENDOR_UNKNOWN
);
783 SetMenuState (CEC_MENU_STATE_ACTIVATED
);
784 SetCecVersion (CEC_VERSION_UNKNOWN
);
785 SetStreamPath (CEC_INVALID_PHYSICAL_ADDRESS
);
786 SetOSDName (ToString(m_iLogicalAddress
));
787 MarkAsInactiveSource();
790 m_bVendorIdRequested
= false;
791 m_unsupportedFeatures
.clear();
793 if (m_deviceStatus
!= CEC_DEVICE_STATUS_UNKNOWN
)
794 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): device status changed into 'unknown'", GetLogicalAddressName(), m_iLogicalAddress
);
795 m_deviceStatus
= CEC_DEVICE_STATUS_UNKNOWN
;
798 bool CCECBusDevice::TransmitPoll(const cec_logical_address dest
)
801 cec_logical_address
destination(dest
);
802 if (destination
== CECDEVICE_UNKNOWN
)
803 destination
= m_iLogicalAddress
;
805 CCECBusDevice
*destDevice
= m_processor
->GetDevice(destination
);
806 if (destDevice
->m_deviceStatus
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
)
810 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): POLL", GetLogicalAddressName(), m_iLogicalAddress
, ToString(dest
), dest
);
811 bReturn
= m_handler
->TransmitPoll(m_iLogicalAddress
, destination
);
812 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, bReturn
? ">> POLL sent" : ">> POLL not sent");
814 CLockObject
lock(m_mutex
);
817 m_iLastActive
= GetTimeMs();
818 destDevice
->m_deviceStatus
= CEC_DEVICE_STATUS_PRESENT
;
821 destDevice
->m_deviceStatus
= CEC_DEVICE_STATUS_NOT_PRESENT
;
827 void CCECBusDevice::HandlePoll(const cec_logical_address destination
)
829 if (destination
>= 0 && destination
< CECDEVICE_BROADCAST
)
831 CCECBusDevice
*device
= m_processor
->GetDevice(destination
);
833 device
->HandlePollFrom(m_iLogicalAddress
);
837 void CCECBusDevice::HandlePollFrom(const cec_logical_address initiator
)
839 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "<< POLL: %s (%x) -> %s (%x)", ToString(initiator
), initiator
, ToString(m_iLogicalAddress
), m_iLogicalAddress
);
840 m_bAwaitingReceiveFailed
= true;
843 bool CCECBusDevice::HandleReceiveFailed(void)
845 bool bReturn
= m_bAwaitingReceiveFailed
;
846 m_bAwaitingReceiveFailed
= false;
850 cec_menu_state
CCECBusDevice::GetMenuState(const cec_logical_address
UNUSED(initiator
))
852 CLockObject
lock(m_mutex
);
856 void CCECBusDevice::SetMenuState(const cec_menu_state state
)
858 CLockObject
lock(m_mutex
);
859 if (m_menuState
!= state
)
861 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): menu state set to '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(m_menuState
));
866 bool CCECBusDevice::TransmitMenuState(const cec_logical_address dest
)
868 cec_menu_state menuState
;
870 CLockObject
lock(m_mutex
);
871 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): menu state '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(dest
), dest
, ToString(m_menuState
));
872 menuState
= m_menuState
;
876 bool bReturn
= m_handler
->TransmitMenuState(m_iLogicalAddress
, dest
, menuState
);
881 bool CCECBusDevice::ActivateSource(void)
883 MarkAsActiveSource();
884 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "activating source '%s'", ToString(m_iLogicalAddress
));
886 bool bReturn
= m_handler
->ActivateSource();
891 bool CCECBusDevice::RequestActiveSource(bool bWaitForResponse
/* = true */)
895 if (IsHandledByLibCEC())
898 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< requesting active source");
900 bReturn
= m_handler
->TransmitRequestActiveSource(m_iLogicalAddress
, bWaitForResponse
);
906 void CCECBusDevice::MarkAsActiveSource(void)
908 CLockObject
lock(m_mutex
);
909 if (!m_bActiveSource
)
910 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "making %s (%x) the active source", GetLogicalAddressName(), m_iLogicalAddress
);
912 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%x) was already marked as active source", GetLogicalAddressName(), m_iLogicalAddress
);
914 CECDEVICEVEC devices
;
915 m_processor
->GetDevices()->Get(devices
);
916 for (CECDEVICEVEC::iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
917 if ((*it
)->GetLogicalAddress() != m_iLogicalAddress
)
918 (*it
)->MarkAsInactiveSource();
920 m_bActiveSource
= true;
921 SetPowerStatus(CEC_POWER_STATUS_ON
);
924 void CCECBusDevice::MarkAsInactiveSource(void)
927 CLockObject
lock(m_mutex
);
929 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "marking %s (%X) as inactive source", GetLogicalAddressName(), m_iLogicalAddress
);
930 m_bActiveSource
= false;
934 bool CCECBusDevice::TransmitActiveSource(void)
936 bool bSendActiveSource(false);
939 CLockObject
lock(m_mutex
);
940 if (m_powerStatus
!= CEC_POWER_STATUS_ON
&& m_powerStatus
!= CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
)
941 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress
);
942 else if (m_bActiveSource
)
944 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> broadcast (F): active source (%4x)", GetLogicalAddressName(), m_iLogicalAddress
, m_iPhysicalAddress
);
945 bSendActiveSource
= true;
948 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "<< %s (%X) is not the active source", GetLogicalAddressName(), m_iLogicalAddress
);
951 if (bSendActiveSource
)
954 m_handler
->TransmitActiveSource(m_iLogicalAddress
, m_iPhysicalAddress
);
962 bool CCECBusDevice::TransmitImageViewOn(void)
965 CLockObject
lock(m_mutex
);
966 if (m_powerStatus
!= CEC_POWER_STATUS_ON
&& m_powerStatus
!= CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
)
968 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress
);
974 m_handler
->TransmitImageViewOn(m_iLogicalAddress
, CECDEVICE_TV
);
979 bool CCECBusDevice::TransmitInactiveSource(void)
981 uint16_t iPhysicalAddress
;
983 CLockObject
lock(m_mutex
);
984 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> broadcast (F): inactive source", GetLogicalAddressName(), m_iLogicalAddress
);
985 iPhysicalAddress
= m_iPhysicalAddress
;
989 bool bReturn
= m_handler
->TransmitInactiveSource(m_iLogicalAddress
, iPhysicalAddress
);
994 bool CCECBusDevice::TransmitPendingActiveSourceCommands(void)
997 bool bReturn
= m_handler
->TransmitPendingActiveSourceCommands();
1002 void CCECBusDevice::SetStreamPath(uint16_t iNewAddress
, uint16_t iOldAddress
/* = CEC_INVALID_PHYSICAL_ADDRESS */)
1004 CLockObject
lock(m_mutex
);
1005 if (iNewAddress
!= m_iStreamPath
)
1007 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s (%X): stream path changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress
, iOldAddress
== 0 ? m_iStreamPath
: iOldAddress
, iNewAddress
);
1008 m_iStreamPath
= iNewAddress
;
1011 if (!LIB_CEC
->IsValidPhysicalAddress(iNewAddress
))
1014 CCECBusDevice
*device
= m_processor
->GetDeviceByPhysicalAddress(iNewAddress
);
1017 // if a device is found with the new physical address, mark it as active, which will automatically mark all other devices as inactive
1018 device
->MarkAsActiveSource();
1022 // try to find the device with the old address, and mark it as inactive when found
1023 device
= m_processor
->GetDeviceByPhysicalAddress(iOldAddress
);
1025 device
->MarkAsInactiveSource();
1029 bool CCECBusDevice::PowerOn(const cec_logical_address initiator
)
1031 bool bReturn(false);
1032 GetVendorId(initiator
); // ensure that we got the vendor id, because the implementations vary per vendor
1035 cec_power_status currentStatus
= GetPowerStatus(initiator
, false);
1036 if (currentStatus
!= CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
&&
1037 currentStatus
!= CEC_POWER_STATUS_ON
)
1039 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< powering on '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
1040 if (m_handler
->PowerOn(initiator
, m_iLogicalAddress
))
1042 SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
1048 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "'%s' (%X) is already '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(currentStatus
));
1055 bool CCECBusDevice::Standby(const cec_logical_address initiator
)
1057 GetVendorId(initiator
); // ensure that we got the vendor id, because the implementations vary per vendor
1059 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "<< putting '%s' (%X) in standby mode", GetLogicalAddressName(), m_iLogicalAddress
);
1061 bool bReturn
= m_handler
->TransmitStandby(initiator
, m_iLogicalAddress
);
1066 bool CCECBusDevice::NeedsPoll(void)
1068 bool bSendPoll(false);
1069 cec_logical_address
pollAddress(CECDEVICE_UNKNOWN
);
1070 switch (m_iLogicalAddress
)
1072 case CECDEVICE_PLAYBACKDEVICE3
:
1073 pollAddress
= CECDEVICE_PLAYBACKDEVICE2
;
1075 case CECDEVICE_PLAYBACKDEVICE2
:
1076 pollAddress
= CECDEVICE_PLAYBACKDEVICE1
;
1078 case CECDEVICE_RECORDINGDEVICE3
:
1079 pollAddress
= CECDEVICE_RECORDINGDEVICE2
;
1081 case CECDEVICE_RECORDINGDEVICE2
:
1082 pollAddress
= CECDEVICE_RECORDINGDEVICE1
;
1084 case CECDEVICE_TUNER4
:
1085 pollAddress
= CECDEVICE_TUNER3
;
1087 case CECDEVICE_TUNER3
:
1088 pollAddress
= CECDEVICE_TUNER2
;
1090 case CECDEVICE_TUNER2
:
1091 pollAddress
= CECDEVICE_TUNER1
;
1093 case CECDEVICE_AUDIOSYSTEM
:
1094 case CECDEVICE_PLAYBACKDEVICE1
:
1095 case CECDEVICE_RECORDINGDEVICE1
:
1096 case CECDEVICE_TUNER1
:
1104 if (!bSendPoll
&& pollAddress
!= CECDEVICE_UNKNOWN
)
1106 CCECBusDevice
*device
= m_processor
->GetDevice(pollAddress
);
1109 cec_bus_device_status status
= device
->GetStatus();
1110 bSendPoll
= (status
== CEC_DEVICE_STATUS_PRESENT
|| status
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
1121 void CCECBusDevice::CheckVendorIdRequested(const cec_logical_address initiator
)
1123 bool bRequestVendorId(false);
1125 CLockObject
lock(m_mutex
);
1126 bRequestVendorId
= !m_bVendorIdRequested
;
1127 m_bVendorIdRequested
= true;
1130 if (bRequestVendorId
)
1132 ReplaceHandler(false);
1133 GetVendorId(initiator
);
1138 CCECAudioSystem
*CCECBusDevice::AsAudioSystem(void)
1140 return AsAudioSystem(this);
1143 CCECPlaybackDevice
*CCECBusDevice::AsPlaybackDevice(void)
1145 return AsPlaybackDevice(this);
1148 CCECRecordingDevice
*CCECBusDevice::AsRecordingDevice(void)
1150 return AsRecordingDevice(this);
1153 CCECTuner
*CCECBusDevice::AsTuner(void)
1155 return AsTuner(this);
1158 CCECTV
*CCECBusDevice::AsTV(void)
1163 CCECAudioSystem
*CCECBusDevice::AsAudioSystem(CCECBusDevice
*device
)
1165 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
1166 return static_cast<CCECAudioSystem
*>(device
);
1170 CCECPlaybackDevice
*CCECBusDevice::AsPlaybackDevice(CCECBusDevice
*device
)
1173 (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
||
1174 device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
))
1175 return static_cast<CCECPlaybackDevice
*>(device
);
1179 CCECRecordingDevice
*CCECBusDevice::AsRecordingDevice(CCECBusDevice
*device
)
1181 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
1182 return static_cast<CCECRecordingDevice
*>(device
);
1186 CCECTuner
*CCECBusDevice::AsTuner(CCECBusDevice
*device
)
1188 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_TUNER
)
1189 return static_cast<CCECTuner
*>(device
);
1193 CCECTV
*CCECBusDevice::AsTV(CCECBusDevice
*device
)
1195 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_TV
)
1196 return static_cast<CCECTV
*>(device
);
1200 void CCECBusDevice::MarkBusy(void)
1202 CLockObject
handlerLock(m_handlerMutex
);
1203 ++m_iHandlerUseCount
;
1206 void CCECBusDevice::MarkReady(void)
1208 CLockObject
handlerLock(m_handlerMutex
);
1209 if (m_iHandlerUseCount
> 0)
1210 --m_iHandlerUseCount
;
1213 bool CCECBusDevice::TryLogicalAddress(void)
1215 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "trying logical address '%s'", GetLogicalAddressName());
1217 if (!TransmitPoll(m_iLogicalAddress
))
1219 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "using logical address '%s'", GetLogicalAddressName());
1220 SetDeviceStatus(CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
1225 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "logical address '%s' already taken", GetLogicalAddressName());
1226 SetDeviceStatus(CEC_DEVICE_STATUS_PRESENT
);
1230 CCECClient
*CCECBusDevice::GetClient(void)
1232 return m_processor
->GetClient(m_iLogicalAddress
);