2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
9 * This program is dual-licensed; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
27 * For more information contact:
28 * Pulse-Eight Licensing <license@pulse-eight.com>
29 * http://www.pulse-eight.com/
30 * http://www.pulse-eight.net/
33 #include "CECBusDevice.h"
34 #include "../CECProcessor.h"
35 #include "../implementations/ANCommandHandler.h"
36 #include "../implementations/CECCommandHandler.h"
37 #include "../implementations/SLCommandHandler.h"
38 #include "../implementations/VLCommandHandler.h"
39 #include "../LibCEC.h"
40 #include "../platform/util/timeutils.h"
43 using namespace PLATFORM
;
45 #define ToString(p) m_processor->ToString(p)
47 CCECBusDevice::CCECBusDevice(CCECProcessor
*processor
, cec_logical_address iLogicalAddress
, uint16_t iPhysicalAddress
) :
48 m_type (CEC_DEVICE_TYPE_RESERVED
),
49 m_iPhysicalAddress (iPhysicalAddress
),
50 m_iStreamPath (CEC_INVALID_PHYSICAL_ADDRESS
),
51 m_iLogicalAddress (iLogicalAddress
),
52 m_powerStatus (CEC_POWER_STATUS_UNKNOWN
),
53 m_processor (processor
),
54 m_vendor (CEC_VENDOR_UNKNOWN
),
55 m_bReplaceHandler (false),
56 m_menuState (CEC_MENU_STATE_ACTIVATED
),
57 m_bActiveSource (false),
59 m_iLastPowerStateUpdate (0),
60 m_cecVersion (CEC_VERSION_UNKNOWN
),
61 m_deviceStatus (CEC_DEVICE_STATUS_UNKNOWN
),
62 m_iHandlerUseCount (0),
63 m_bAwaitingReceiveFailed(false),
64 m_bVendorIdRequested (false)
66 m_handler
= new CCECCommandHandler(this);
68 for (unsigned int iPtr
= 0; iPtr
< 4; iPtr
++)
69 m_menuLanguage
.language
[iPtr
] = '?';
70 m_menuLanguage
.language
[3] = 0;
71 m_menuLanguage
.device
= iLogicalAddress
;
73 m_strDeviceName
= ToString(m_iLogicalAddress
);
76 CCECBusDevice::~CCECBusDevice(void)
81 bool CCECBusDevice::HandleCommand(const cec_command
&command
)
85 /* update "last active" */
87 CLockObject
lock(m_mutex
);
88 m_iLastActive
= GetTimeMs();
90 /* don't call GetStatus() here, just read the value with the mutex locked */
91 if (m_deviceStatus
!= CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
&& command
.opcode_set
== 1)
92 m_deviceStatus
= CEC_DEVICE_STATUS_PRESENT
;
97 /* handle the command */
98 bHandled
= m_handler
->HandleCommand(command
);
100 /* change status to present */
103 CLockObject
lock(m_mutex
);
104 if (m_deviceStatus
!= CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
)
106 if (m_deviceStatus
!= CEC_DEVICE_STATUS_PRESENT
)
107 CLibCEC::AddLog(CEC_LOG_DEBUG
, "device %s (%x) status changed to present after command %s", GetLogicalAddressName(), (uint8_t)GetLogicalAddress(), ToString(command
.opcode
));
108 m_deviceStatus
= CEC_DEVICE_STATUS_PRESENT
;
116 bool CCECBusDevice::PowerOn(void)
119 GetVendorId(); // ensure that we got the vendor id, because the implementations vary per vendor
122 cec_power_status currentStatus
= GetPowerStatus(false);
123 if (currentStatus
!= CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
&&
124 currentStatus
!= CEC_POWER_STATUS_ON
)
126 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< powering on '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
127 if (m_handler
->PowerOn(GetMyLogicalAddress(), m_iLogicalAddress
))
129 SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
135 CLibCEC::AddLog(CEC_LOG_NOTICE
, "'%s' (%X) is already '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(currentStatus
));
142 bool CCECBusDevice::Standby(void)
144 CLibCEC::AddLog(CEC_LOG_DEBUG
, "<< putting '%s' (%X) in standby mode", GetLogicalAddressName(), m_iLogicalAddress
);
146 bool bReturn
= m_handler
->TransmitStandby(GetMyLogicalAddress(), m_iLogicalAddress
);
153 cec_version
CCECBusDevice::GetCecVersion(bool bUpdate
/* = false */)
155 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
156 bool bRequestUpdate(false);
158 CLockObject
lock(m_mutex
);
159 bRequestUpdate
= bIsPresent
&&
160 (bUpdate
|| m_cecVersion
== CEC_VERSION_UNKNOWN
);
165 CheckVendorIdRequested();
169 CLockObject
lock(m_mutex
);
173 bool CCECBusDevice::RequestActiveSource(bool bWaitForResponse
/* = true */)
177 if (MyLogicalAddressContains(m_iLogicalAddress
))
180 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< requesting active source");
182 bReturn
= m_handler
->TransmitRequestActiveSource(GetMyLogicalAddress(), bWaitForResponse
);
188 bool CCECBusDevice::RequestCecVersion(bool bWaitForResponse
/* = true */)
192 if (!MyLogicalAddressContains(m_iLogicalAddress
) &&
193 !IsUnsupportedFeature(CEC_OPCODE_GET_CEC_VERSION
))
196 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< requesting CEC version of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
198 bReturn
= m_handler
->TransmitRequestCecVersion(GetMyLogicalAddress(), m_iLogicalAddress
, bWaitForResponse
);
204 const char* CCECBusDevice::GetLogicalAddressName(void) const
206 return ToString(m_iLogicalAddress
);
209 cec_menu_language
&CCECBusDevice::GetMenuLanguage(bool bUpdate
/* = false */)
211 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
212 bool bRequestUpdate(false);
214 CLockObject
lock(m_mutex
);
215 bRequestUpdate
= (bIsPresent
&&
216 (bUpdate
|| !strcmp(m_menuLanguage
.language
, "???")));
221 CheckVendorIdRequested();
222 RequestMenuLanguage();
225 CLockObject
lock(m_mutex
);
226 return m_menuLanguage
;
229 bool CCECBusDevice::RequestMenuLanguage(bool bWaitForResponse
/* = true */)
233 if (!MyLogicalAddressContains(m_iLogicalAddress
) &&
234 !IsUnsupportedFeature(CEC_OPCODE_GET_MENU_LANGUAGE
))
237 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< requesting menu language of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
238 bReturn
= m_handler
->TransmitRequestMenuLanguage(GetMyLogicalAddress(), m_iLogicalAddress
, bWaitForResponse
);
244 cec_menu_state
CCECBusDevice::GetMenuState(void)
246 CLockObject
lock(m_mutex
);
250 cec_logical_address
CCECBusDevice::GetMyLogicalAddress(void) const
252 return m_processor
->GetLogicalAddress();
255 uint16_t CCECBusDevice::GetMyPhysicalAddress(void) const
257 return m_processor
->GetPhysicalAddress();
260 CStdString
CCECBusDevice::GetOSDName(bool bUpdate
/* = false */)
262 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
263 bool bRequestUpdate(false);
265 CLockObject
lock(m_mutex
);
266 bRequestUpdate
= bIsPresent
&&
267 (bUpdate
|| m_strDeviceName
.Equals(ToString(m_iLogicalAddress
))) &&
268 m_type
!= CEC_DEVICE_TYPE_TV
;
273 CheckVendorIdRequested();
277 CLockObject
lock(m_mutex
);
278 return m_strDeviceName
;
281 bool CCECBusDevice::RequestOSDName(bool bWaitForResponse
/* = true */)
285 if (!MyLogicalAddressContains(m_iLogicalAddress
) &&
286 !IsUnsupportedFeature(CEC_OPCODE_GIVE_OSD_NAME
))
289 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< requesting OSD name of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
290 bReturn
= m_handler
->TransmitRequestOSDName(GetMyLogicalAddress(), m_iLogicalAddress
, bWaitForResponse
);
296 uint16_t CCECBusDevice::GetPhysicalAddress(bool bSuppressUpdate
/* = true */)
298 if (!bSuppressUpdate
)
300 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
301 bool bRequestUpdate(false);
303 CLockObject
lock(m_mutex
);
304 bRequestUpdate
= bIsPresent
&& m_iPhysicalAddress
== CEC_INVALID_PHYSICAL_ADDRESS
;
309 CheckVendorIdRequested();
310 if (!RequestPhysicalAddress())
311 CLibCEC::AddLog(CEC_LOG_ERROR
, "failed to request the physical address");
315 CLockObject
lock(m_mutex
);
316 return m_iPhysicalAddress
;
319 bool CCECBusDevice::RequestPhysicalAddress(bool bWaitForResponse
/* = true */)
323 if (!MyLogicalAddressContains(m_iLogicalAddress
))
326 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< requesting physical address of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
327 bReturn
= m_handler
->TransmitRequestPhysicalAddress(GetMyLogicalAddress(), m_iLogicalAddress
, bWaitForResponse
);
333 cec_power_status
CCECBusDevice::GetPowerStatus(bool bUpdate
/* = false */)
335 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
336 bool bRequestUpdate(false);
338 CLockObject
lock(m_mutex
);
339 bRequestUpdate
= (bIsPresent
&&
340 (bUpdate
|| m_powerStatus
== CEC_POWER_STATUS_UNKNOWN
||
341 m_powerStatus
== CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
||
342 m_powerStatus
== CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY
||
343 GetTimeMs() - m_iLastPowerStateUpdate
>= CEC_POWER_STATE_REFRESH_TIME
));
348 CheckVendorIdRequested();
349 RequestPowerStatus();
352 CLockObject
lock(m_mutex
);
353 return m_powerStatus
;
356 bool CCECBusDevice::RequestPowerStatus(bool bWaitForResponse
/* = true */)
360 if (!MyLogicalAddressContains(m_iLogicalAddress
) &&
361 !IsUnsupportedFeature(CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
))
364 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< requesting power status of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
365 bReturn
= m_handler
->TransmitRequestPowerStatus(GetMyLogicalAddress(), m_iLogicalAddress
, bWaitForResponse
);
371 cec_vendor_id
CCECBusDevice::GetVendorId(bool bUpdate
/* = false */)
373 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
374 bool bRequestUpdate(false);
376 CLockObject
lock(m_mutex
);
377 bRequestUpdate
= (bIsPresent
&&
378 (bUpdate
|| m_vendor
== CEC_VENDOR_UNKNOWN
));
384 CLockObject
lock(m_mutex
);
388 bool CCECBusDevice::RequestVendorId(bool bWaitForResponse
/* = true */)
392 if (!MyLogicalAddressContains(m_iLogicalAddress
) && GetMyLogicalAddress() != CECDEVICE_UNKNOWN
)
395 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< requesting vendor ID of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
396 bReturn
= m_handler
->TransmitRequestVendorId(GetMyLogicalAddress(), m_iLogicalAddress
, bWaitForResponse
);
399 if (bWaitForResponse
)
400 ReplaceHandler(true);
405 const char *CCECBusDevice::GetVendorName(bool bUpdate
/* = false */)
407 return ToString(GetVendorId(bUpdate
));
410 bool CCECBusDevice::MyLogicalAddressContains(cec_logical_address address
) const
412 return m_processor
->HasLogicalAddress(address
);
415 bool CCECBusDevice::NeedsPoll(void)
417 bool bSendPoll(false);
418 switch (m_iLogicalAddress
)
420 case CECDEVICE_PLAYBACKDEVICE3
:
422 cec_bus_device_status status
= m_processor
->m_busDevices
[CECDEVICE_PLAYBACKDEVICE2
]->GetStatus();
423 bSendPoll
= (status
== CEC_DEVICE_STATUS_PRESENT
|| status
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
426 case CECDEVICE_PLAYBACKDEVICE2
:
428 cec_bus_device_status status
= m_processor
->m_busDevices
[CECDEVICE_PLAYBACKDEVICE1
]->GetStatus();
429 bSendPoll
= (status
== CEC_DEVICE_STATUS_PRESENT
|| status
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
432 case CECDEVICE_RECORDINGDEVICE3
:
434 cec_bus_device_status status
= m_processor
->m_busDevices
[CECDEVICE_RECORDINGDEVICE2
]->GetStatus();
435 bSendPoll
= (status
== CEC_DEVICE_STATUS_PRESENT
|| status
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
438 case CECDEVICE_RECORDINGDEVICE2
:
440 cec_bus_device_status status
= m_processor
->m_busDevices
[CECDEVICE_RECORDINGDEVICE1
]->GetStatus();
441 bSendPoll
= (status
== CEC_DEVICE_STATUS_PRESENT
|| status
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
444 case CECDEVICE_TUNER4
:
446 cec_bus_device_status status
= m_processor
->m_busDevices
[CECDEVICE_TUNER3
]->GetStatus();
447 bSendPoll
= (status
== CEC_DEVICE_STATUS_PRESENT
|| status
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
450 case CECDEVICE_TUNER3
:
452 cec_bus_device_status status
= m_processor
->m_busDevices
[CECDEVICE_TUNER2
]->GetStatus();
453 bSendPoll
= (status
== CEC_DEVICE_STATUS_PRESENT
|| status
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
456 case CECDEVICE_TUNER2
:
458 cec_bus_device_status status
= m_processor
->m_busDevices
[CECDEVICE_TUNER1
]->GetStatus();
459 bSendPoll
= (status
== CEC_DEVICE_STATUS_PRESENT
|| status
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
462 case CECDEVICE_AUDIOSYSTEM
:
463 case CECDEVICE_PLAYBACKDEVICE1
:
464 case CECDEVICE_RECORDINGDEVICE1
:
465 case CECDEVICE_TUNER1
:
476 cec_bus_device_status
CCECBusDevice::GetStatus(bool bForcePoll
/* = false */, bool bSuppressPoll
/* = false */)
478 cec_bus_device_status
status(CEC_DEVICE_STATUS_UNKNOWN
);
479 bool bNeedsPoll(false);
482 CLockObject
lock(m_mutex
);
483 status
= m_deviceStatus
;
484 bNeedsPoll
= !bSuppressPoll
&&
485 (bForcePoll
|| m_deviceStatus
== CEC_DEVICE_STATUS_UNKNOWN
);
490 bool bPollAcked(false);
491 if (bNeedsPoll
&& NeedsPoll())
492 bPollAcked
= m_processor
->PollDevice(m_iLogicalAddress
);
494 status
= bPollAcked
? CEC_DEVICE_STATUS_PRESENT
: CEC_DEVICE_STATUS_NOT_PRESENT
;
495 SetDeviceStatus(status
);
505 void CCECBusDevice::SetCecVersion(const cec_version newVersion
)
507 if (m_cecVersion
!= newVersion
)
508 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s (%X): CEC version %s", GetLogicalAddressName(), m_iLogicalAddress
, ToString(newVersion
));
509 m_cecVersion
= newVersion
;
512 void CCECBusDevice::SetMenuLanguage(const cec_menu_language
&language
)
514 CLockObject
lock(m_mutex
);
515 if (language
.device
== m_iLogicalAddress
&&
516 strcmp(language
.language
, m_menuLanguage
.language
))
518 CLibCEC::AddLog(CEC_LOG_DEBUG
, ">> %s (%X): menu language set to '%s'", GetLogicalAddressName(), m_iLogicalAddress
, language
.language
);
519 m_menuLanguage
= language
;
523 void CCECBusDevice::SetOSDName(CStdString strName
)
525 CLockObject
lock(m_mutex
);
526 if (m_strDeviceName
!= strName
)
528 CLibCEC::AddLog(CEC_LOG_DEBUG
, ">> %s (%X): osd name set to '%s'", GetLogicalAddressName(), m_iLogicalAddress
, strName
.c_str());
529 m_strDeviceName
= strName
;
533 void CCECBusDevice::SetMenuState(const cec_menu_state state
)
535 CLockObject
lock(m_mutex
);
536 if (m_menuState
!= state
)
538 CLibCEC::AddLog(CEC_LOG_DEBUG
, ">> %s (%X): menu state set to '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(m_menuState
));
543 void CCECBusDevice::SetInactiveSource(void)
546 CLockObject
lock(m_mutex
);
548 CLibCEC::AddLog(CEC_LOG_DEBUG
, "marking %s (%X) as inactive source", GetLogicalAddressName(), m_iLogicalAddress
);
549 m_bActiveSource
= false;
553 void CCECBusDevice::SetActiveSource(void)
555 CLockObject
lock(m_mutex
);
556 if (!m_bActiveSource
)
557 CLibCEC::AddLog(CEC_LOG_DEBUG
, "making %s (%x) the active source", GetLogicalAddressName(), m_iLogicalAddress
);
559 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s (%x) was already marked as active source", GetLogicalAddressName(), m_iLogicalAddress
);
561 for (int iPtr
= 0; iPtr
< 16; iPtr
++)
562 if (iPtr
!= m_iLogicalAddress
)
563 m_processor
->m_busDevices
[iPtr
]->SetInactiveSource();
565 m_bActiveSource
= true;
566 SetPowerStatus(CEC_POWER_STATUS_ON
);
569 bool CCECBusDevice::TryLogicalAddress(void)
571 CLibCEC::AddLog(CEC_LOG_DEBUG
, "trying logical address '%s'", GetLogicalAddressName());
573 m_processor
->SetAckMask(0);
574 if (!TransmitPoll(m_iLogicalAddress
))
576 CLibCEC::AddLog(CEC_LOG_NOTICE
, "using logical address '%s'", GetLogicalAddressName());
577 SetDeviceStatus(CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
582 CLibCEC::AddLog(CEC_LOG_DEBUG
, "logical address '%s' already taken", GetLogicalAddressName());
583 SetDeviceStatus(CEC_DEVICE_STATUS_PRESENT
);
587 void CCECBusDevice::ResetDeviceStatus(void)
589 CLockObject
lock(m_mutex
);
590 SetPowerStatus (CEC_POWER_STATUS_UNKNOWN
);
591 SetVendorId (CEC_VENDOR_UNKNOWN
);
592 SetMenuState (CEC_MENU_STATE_ACTIVATED
);
593 SetCecVersion (CEC_VERSION_UNKNOWN
);
594 SetStreamPath (CEC_INVALID_PHYSICAL_ADDRESS
);
595 SetOSDName (ToString(m_iLogicalAddress
));
598 m_unsupportedFeatures
.clear();
601 void CCECBusDevice::SetDeviceStatus(const cec_bus_device_status newStatus
)
604 CLockObject
lock(m_mutex
);
607 case CEC_DEVICE_STATUS_UNKNOWN
:
608 if (m_deviceStatus
!= newStatus
)
609 CLibCEC::AddLog(CEC_LOG_DEBUG
, "device status of %s changed into 'unknown'", ToString(m_iLogicalAddress
));
611 m_deviceStatus
= newStatus
;
613 case CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
:
614 if (m_deviceStatus
!= newStatus
)
615 CLibCEC::AddLog(CEC_LOG_DEBUG
, "device status of %s changed into 'handled by libCEC'", ToString(m_iLogicalAddress
));
616 SetPowerStatus (CEC_POWER_STATUS_ON
);
617 SetVendorId (CEC_VENDOR_UNKNOWN
);
618 SetMenuState (CEC_MENU_STATE_ACTIVATED
);
619 SetCecVersion (CEC_VERSION_1_3A
);
620 SetStreamPath (CEC_INVALID_PHYSICAL_ADDRESS
);
623 m_deviceStatus
= newStatus
;
625 case CEC_DEVICE_STATUS_PRESENT
:
626 if (m_deviceStatus
!= newStatus
)
627 CLibCEC::AddLog(CEC_LOG_DEBUG
, "device status of %s changed into 'present'", ToString(m_iLogicalAddress
));
628 m_deviceStatus
= newStatus
;
630 case CEC_DEVICE_STATUS_NOT_PRESENT
:
631 if (m_deviceStatus
!= newStatus
)
633 CLibCEC::AddLog(CEC_LOG_DEBUG
, "device status of %s changed into 'not present'", ToString(m_iLogicalAddress
));
635 m_deviceStatus
= newStatus
;
641 if (newStatus
== CEC_DEVICE_STATUS_PRESENT
)
642 RequestVendorId(false);
645 void CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress
)
647 CLockObject
lock(m_mutex
);
648 if (iNewAddress
> 0 && m_iPhysicalAddress
!= iNewAddress
)
650 CLibCEC::AddLog(CEC_LOG_DEBUG
, ">> %s (%X): physical address changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress
, m_iPhysicalAddress
, iNewAddress
);
651 m_iPhysicalAddress
= iNewAddress
;
655 void CCECBusDevice::SetStreamPath(uint16_t iNewAddress
, uint16_t iOldAddress
/* = CEC_INVALID_PHYSICAL_ADDRESS */)
657 CLockObject
lock(m_mutex
);
658 if (iNewAddress
!= m_iStreamPath
)
660 CLibCEC::AddLog(CEC_LOG_DEBUG
, ">> %s (%X): stream path changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress
, iOldAddress
== 0 ? m_iStreamPath
: iOldAddress
, iNewAddress
);
661 m_iStreamPath
= iNewAddress
;
664 CCECBusDevice
*device
= m_processor
->GetDeviceByPhysicalAddress(iNewAddress
);
667 // if a device is found with the new physical address, mark it as active, which will automatically mark all other devices as inactive
668 device
->SetActiveSource();
672 // try to find the device with the old address, and mark it as inactive when found
673 device
= m_processor
->GetDeviceByPhysicalAddress(iOldAddress
);
675 device
->SetInactiveSource();
679 void CCECBusDevice::SetPowerStatus(const cec_power_status powerStatus
)
681 CLockObject
lock(m_mutex
);
682 if (m_powerStatus
!= powerStatus
)
684 m_iLastPowerStateUpdate
= GetTimeMs();
685 CLibCEC::AddLog(CEC_LOG_DEBUG
, ">> %s (%X): power status changed from '%s' to '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(m_powerStatus
), ToString(powerStatus
));
686 m_powerStatus
= powerStatus
;
690 void CCECBusDevice::MarkBusy(void)
692 CLockObject
handlerLock(m_handlerMutex
);
693 ++m_iHandlerUseCount
;
696 void CCECBusDevice::MarkReady(void)
698 CLockObject
handlerLock(m_handlerMutex
);
699 if (m_iHandlerUseCount
> 0)
700 --m_iHandlerUseCount
;
703 bool CCECBusDevice::ReplaceHandler(bool bActivateSource
/* = true */)
705 bool bInitHandler(false);
707 CTryLockObject
lock(m_mutex
);
708 if (!lock
.IsLocked())
711 CLockObject
handlerLock(m_handlerMutex
);
712 if (m_iHandlerUseCount
> 0)
717 if (m_vendor
!= m_handler
->GetVendorId())
719 if (CCECCommandHandler::HasSpecificHandler(m_vendor
))
721 CLibCEC::AddLog(CEC_LOG_DEBUG
, "replacing the command handler for device '%s' (%x)", GetLogicalAddressName(), GetLogicalAddress());
726 case CEC_VENDOR_SAMSUNG
:
727 m_handler
= new CANCommandHandler(this);
730 m_handler
= new CSLCommandHandler(this);
732 case CEC_VENDOR_PANASONIC
:
733 m_handler
= new CVLCommandHandler(this);
736 m_handler
= new CCECCommandHandler(this);
740 m_handler
->SetVendorId(m_vendor
);
748 m_handler
->InitHandler();
750 if (bActivateSource
&& m_processor
->GetLogicalAddresses().IsSet(m_iLogicalAddress
) && m_processor
->IsInitialised() && IsActiveSource())
751 m_handler
->ActivateSource();
759 bool CCECBusDevice::SetVendorId(uint64_t iVendorId
)
761 bool bVendorChanged(false);
764 CLockObject
lock(m_mutex
);
765 bVendorChanged
= (m_vendor
!= (cec_vendor_id
)iVendorId
);
766 m_vendor
= (cec_vendor_id
)iVendorId
;
770 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s (%X): vendor = %s (%06x)", GetLogicalAddressName(), m_iLogicalAddress
, ToString(m_vendor
), m_vendor
);
772 return bVendorChanged
;
776 /** @name Transmit methods */
778 bool CCECBusDevice::TransmitActiveSource(void)
780 bool bSendActiveSource(false);
783 CLockObject
lock(m_mutex
);
784 if (m_powerStatus
!= CEC_POWER_STATUS_ON
&& m_powerStatus
!= CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
)
785 CLibCEC::AddLog(CEC_LOG_DEBUG
, "<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress
);
786 else if (m_bActiveSource
)
788 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> broadcast (F): active source (%4x)", GetLogicalAddressName(), m_iLogicalAddress
, m_iPhysicalAddress
);
789 bSendActiveSource
= true;
792 CLibCEC::AddLog(CEC_LOG_DEBUG
, "<< %s (%X) is not the active source", GetLogicalAddressName(), m_iLogicalAddress
);
795 if (bSendActiveSource
)
798 m_handler
->TransmitActiveSource(m_iLogicalAddress
, m_iPhysicalAddress
);
806 bool CCECBusDevice::TransmitCECVersion(cec_logical_address dest
)
810 CLockObject
lock(m_mutex
);
811 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): cec version %s", GetLogicalAddressName(), m_iLogicalAddress
, ToString(dest
), dest
, ToString(m_cecVersion
));
812 version
= m_cecVersion
;
816 bool bReturn
= m_handler
->TransmitCECVersion(m_iLogicalAddress
, dest
, version
);
821 bool CCECBusDevice::TransmitImageViewOn(void)
824 CLockObject
lock(m_mutex
);
825 if (m_powerStatus
!= CEC_POWER_STATUS_ON
&& m_powerStatus
!= CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
)
827 CLibCEC::AddLog(CEC_LOG_DEBUG
, "<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress
);
833 m_handler
->TransmitImageViewOn(m_iLogicalAddress
, CECDEVICE_TV
);
838 bool CCECBusDevice::TransmitInactiveSource(void)
840 uint16_t iPhysicalAddress
;
842 CLockObject
lock(m_mutex
);
843 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> broadcast (F): inactive source", GetLogicalAddressName(), m_iLogicalAddress
);
844 iPhysicalAddress
= m_iPhysicalAddress
;
848 bool bReturn
= m_handler
->TransmitInactiveSource(m_iLogicalAddress
, iPhysicalAddress
);
853 bool CCECBusDevice::TransmitMenuState(cec_logical_address dest
)
855 cec_menu_state menuState
;
857 CLockObject
lock(m_mutex
);
858 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): menu state '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(dest
), dest
, ToString(m_menuState
));
859 menuState
= m_menuState
;
863 bool bReturn
= m_handler
->TransmitMenuState(m_iLogicalAddress
, dest
, menuState
);
868 bool CCECBusDevice::TransmitOSDName(cec_logical_address dest
)
870 CStdString strDeviceName
;
872 CLockObject
lock(m_mutex
);
873 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): OSD name '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(dest
), dest
, m_strDeviceName
.c_str());
874 strDeviceName
= m_strDeviceName
;
878 bool bReturn
= m_handler
->TransmitOSDName(m_iLogicalAddress
, dest
, strDeviceName
);
883 bool CCECBusDevice::TransmitOSDString(cec_logical_address dest
, cec_display_control duration
, const char *strMessage
)
886 if (!m_processor
->m_busDevices
[dest
]->IsUnsupportedFeature(CEC_OPCODE_SET_OSD_STRING
))
888 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): display OSD message '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(dest
), dest
, strMessage
);
890 bReturn
= m_handler
->TransmitOSDString(m_iLogicalAddress
, dest
, duration
, strMessage
);
896 bool CCECBusDevice::TransmitPhysicalAddress(void)
898 uint16_t iPhysicalAddress
;
899 cec_device_type type
;
901 CLockObject
lock(m_mutex
);
902 if (m_iPhysicalAddress
== CEC_INVALID_PHYSICAL_ADDRESS
)
905 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> broadcast (F): physical adddress %4x", GetLogicalAddressName(), m_iLogicalAddress
, m_iPhysicalAddress
);
906 iPhysicalAddress
= m_iPhysicalAddress
;
911 bool bReturn
= m_handler
->TransmitPhysicalAddress(m_iLogicalAddress
, iPhysicalAddress
, type
);
916 bool CCECBusDevice::TransmitSetMenuLanguage(cec_logical_address dest
)
919 cec_menu_language language
= GetMenuLanguage();
923 CLockObject
lock(m_mutex
);
924 lang
[0] = language
.language
[0];
925 lang
[1] = language
.language
[1];
926 lang
[2] = language
.language
[2];
930 if (lang
[0] == '?' && lang
[1] == '?' && lang
[2] == '?')
932 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): Menu language feature abort", GetLogicalAddressName(), m_iLogicalAddress
, ToString(dest
), dest
);
933 m_processor
->TransmitAbort(dest
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
938 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> broadcast (F): Menu language '%s'", GetLogicalAddressName(), m_iLogicalAddress
, lang
);
939 bReturn
= m_handler
->TransmitSetMenuLanguage(m_iLogicalAddress
, lang
);
945 bool CCECBusDevice::TransmitPoll(cec_logical_address dest
)
948 if (dest
== CECDEVICE_UNKNOWN
)
949 dest
= m_iLogicalAddress
;
951 CCECBusDevice
*destDevice
= m_processor
->m_busDevices
[dest
];
952 if (destDevice
->m_deviceStatus
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
)
956 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): POLL", GetLogicalAddressName(), m_iLogicalAddress
, ToString(dest
), dest
);
957 bReturn
= m_handler
->TransmitPoll(m_iLogicalAddress
, dest
);
958 CLibCEC::AddLog(CEC_LOG_DEBUG
, bReturn
? ">> POLL sent" : ">> POLL not sent");
960 CLockObject
lock(m_mutex
);
963 m_iLastActive
= GetTimeMs();
964 destDevice
->m_deviceStatus
= CEC_DEVICE_STATUS_PRESENT
;
967 destDevice
->m_deviceStatus
= CEC_DEVICE_STATUS_NOT_PRESENT
;
973 bool CCECBusDevice::TransmitPowerState(cec_logical_address dest
)
975 cec_power_status state
;
977 CLockObject
lock(m_mutex
);
978 if (!IsActiveSource())
980 CLibCEC::AddLog(CEC_LOG_NOTICE
, "power state requested of %s (%X), but we are not the active source. setting power state to standby", GetLogicalAddressName(), m_iLogicalAddress
);
981 SetPowerStatus(CEC_POWER_STATUS_STANDBY
);
984 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): %s", GetLogicalAddressName(), m_iLogicalAddress
, ToString(dest
), dest
, ToString(m_powerStatus
));
985 state
= m_powerStatus
;
989 bool bReturn
= m_handler
->TransmitPowerState(m_iLogicalAddress
, dest
, state
);
994 bool CCECBusDevice::TransmitVendorID(cec_logical_address dest
, bool bSendAbort
/* = true */)
999 CLockObject
lock(m_mutex
);
1000 iVendorId
= (uint64_t)m_vendor
;
1004 if (iVendorId
== CEC_VENDOR_UNKNOWN
)
1008 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): vendor id feature abort", GetLogicalAddressName(), m_iLogicalAddress
, ToString(dest
), dest
);
1009 m_processor
->TransmitAbort(dest
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
1015 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): vendor id %s (%x)", GetLogicalAddressName(), m_iLogicalAddress
, ToString(dest
), dest
, ToString((cec_vendor_id
)iVendorId
), iVendorId
);
1016 bReturn
= m_handler
->TransmitVendorID(m_iLogicalAddress
, iVendorId
);
1022 bool CCECBusDevice::TransmitKeypress(cec_user_control_code key
, bool bWait
/* = true */)
1025 bool bReturn
= m_handler
->TransmitKeypress(m_processor
->GetLogicalAddress(), m_iLogicalAddress
, key
, bWait
);
1030 bool CCECBusDevice::TransmitKeyRelease(bool bWait
/* = true */)
1033 bool bReturn
= m_handler
->TransmitKeyRelease(m_processor
->GetLogicalAddress(), m_iLogicalAddress
, bWait
);
1038 bool CCECBusDevice::IsUnsupportedFeature(cec_opcode opcode
)
1040 CLockObject
lock(m_mutex
);
1041 bool bUnsupported
= (m_unsupportedFeatures
.find(opcode
) != m_unsupportedFeatures
.end());
1043 CLibCEC::AddLog(CEC_LOG_DEBUG
, "'%s' is marked as unsupported feature for device '%s'", ToString(opcode
), GetLogicalAddressName());
1044 return bUnsupported
;
1047 void CCECBusDevice::SetUnsupportedFeature(cec_opcode opcode
)
1049 // some commands should never be marked as unsupported
1050 if (opcode
== CEC_OPCODE_VENDOR_COMMAND
||
1051 opcode
== CEC_OPCODE_VENDOR_COMMAND_WITH_ID
||
1052 opcode
== CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN
||
1053 opcode
== CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP
||
1054 opcode
== CEC_OPCODE_ABORT
||
1055 opcode
== CEC_OPCODE_FEATURE_ABORT
||
1056 opcode
== CEC_OPCODE_NONE
)
1060 CLockObject
lock(m_mutex
);
1061 if (m_unsupportedFeatures
.find(opcode
) == m_unsupportedFeatures
.end())
1063 CLibCEC::AddLog(CEC_LOG_DEBUG
, "marking opcode '%s' as unsupported feature for device '%s'", ToString(opcode
), GetLogicalAddressName());
1064 m_unsupportedFeatures
.insert(opcode
);
1068 // signal threads that are waiting for a reponse
1070 m_handler
->SignalOpcode(cec_command::GetResponseOpcode(opcode
));
1074 bool CCECBusDevice::ActivateSource(void)
1076 CLibCEC::AddLog(CEC_LOG_DEBUG
, "activating source '%s'", ToString(m_iLogicalAddress
));
1078 bool bReturn
= m_handler
->ActivateSource();
1083 void CCECBusDevice::HandlePoll(cec_logical_address destination
)
1085 if (destination
>= 0 && destination
< CECDEVICE_BROADCAST
)
1087 CCECBusDevice
*device
= m_processor
->m_busDevices
[destination
];
1089 device
->HandlePollFrom(m_iLogicalAddress
);
1093 void CCECBusDevice::HandlePollFrom(cec_logical_address initiator
)
1095 CLibCEC::AddLog(CEC_LOG_DEBUG
, "<< POLL: %s (%x) -> %s (%x)", ToString(initiator
), initiator
, ToString(m_iLogicalAddress
), m_iLogicalAddress
);
1096 m_bAwaitingReceiveFailed
= true;
1099 bool CCECBusDevice::HandleReceiveFailed(void)
1101 bool bReturn
= m_bAwaitingReceiveFailed
;
1102 m_bAwaitingReceiveFailed
= false;
1106 void CCECBusDevice::CheckVendorIdRequested(void)
1108 bool bRequestVendorId(false);
1110 CLockObject
lock(m_mutex
);
1111 bRequestVendorId
= !m_bVendorIdRequested
;
1112 m_bVendorIdRequested
= true;
1115 if (bRequestVendorId
)
1117 ReplaceHandler(false);
1122 bool CCECBusDevice::TransmitPendingActiveSourceCommands(void)
1125 bool bReturn
= m_handler
->TransmitPendingActiveSourceCommands();