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
),
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::RequestCecVersion(void)
177 if (!MyLogicalAddressContains(m_iLogicalAddress
) &&
178 !IsUnsupportedFeature(CEC_OPCODE_GET_CEC_VERSION
))
181 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< requesting CEC version of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
183 bReturn
= m_handler
->TransmitRequestCecVersion(GetMyLogicalAddress(), m_iLogicalAddress
);
189 const char* CCECBusDevice::GetLogicalAddressName(void) const
191 return ToString(m_iLogicalAddress
);
194 cec_menu_language
&CCECBusDevice::GetMenuLanguage(bool bUpdate
/* = false */)
196 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
197 bool bRequestUpdate(false);
199 CLockObject
lock(m_mutex
);
200 bRequestUpdate
= (bIsPresent
&&
201 (bUpdate
|| !strcmp(m_menuLanguage
.language
, "???")));
206 CheckVendorIdRequested();
207 RequestMenuLanguage();
210 CLockObject
lock(m_mutex
);
211 return m_menuLanguage
;
214 bool CCECBusDevice::RequestMenuLanguage(void)
218 if (!MyLogicalAddressContains(m_iLogicalAddress
) &&
219 !IsUnsupportedFeature(CEC_OPCODE_GET_MENU_LANGUAGE
))
222 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< requesting menu language of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
223 bReturn
= m_handler
->TransmitRequestMenuLanguage(GetMyLogicalAddress(), m_iLogicalAddress
);
229 cec_menu_state
CCECBusDevice::GetMenuState(void)
231 CLockObject
lock(m_mutex
);
235 cec_logical_address
CCECBusDevice::GetMyLogicalAddress(void) const
237 return m_processor
->GetLogicalAddress();
240 uint16_t CCECBusDevice::GetMyPhysicalAddress(void) const
242 return m_processor
->GetPhysicalAddress();
245 CStdString
CCECBusDevice::GetOSDName(bool bUpdate
/* = false */)
247 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
248 bool bRequestUpdate(false);
250 CLockObject
lock(m_mutex
);
251 bRequestUpdate
= bIsPresent
&&
252 (bUpdate
|| m_strDeviceName
.Equals(ToString(m_iLogicalAddress
))) &&
253 m_type
!= CEC_DEVICE_TYPE_TV
;
258 CheckVendorIdRequested();
262 CLockObject
lock(m_mutex
);
263 return m_strDeviceName
;
266 bool CCECBusDevice::RequestOSDName(void)
270 if (!MyLogicalAddressContains(m_iLogicalAddress
) &&
271 !IsUnsupportedFeature(CEC_OPCODE_GIVE_OSD_NAME
))
274 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< requesting OSD name of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
275 bReturn
= m_handler
->TransmitRequestOSDName(GetMyLogicalAddress(), m_iLogicalAddress
);
281 uint16_t CCECBusDevice::GetPhysicalAddress(bool bUpdate
/* = false */)
283 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
284 bool bRequestUpdate(false);
286 CLockObject
lock(m_mutex
);
287 bRequestUpdate
= bIsPresent
&&
288 (m_iPhysicalAddress
== 0xFFFF || bUpdate
);
293 CheckVendorIdRequested();
294 if (!RequestPhysicalAddress())
295 CLibCEC::AddLog(CEC_LOG_ERROR
, "failed to request the physical address");
298 CLockObject
lock(m_mutex
);
299 return m_iPhysicalAddress
;
302 bool CCECBusDevice::RequestPhysicalAddress(void)
306 if (!MyLogicalAddressContains(m_iLogicalAddress
))
309 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< requesting physical address of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
310 bReturn
= m_handler
->TransmitRequestPhysicalAddress(GetMyLogicalAddress(), m_iLogicalAddress
);
316 cec_power_status
CCECBusDevice::GetPowerStatus(bool bUpdate
/* = false */)
318 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
319 bool bRequestUpdate(false);
321 CLockObject
lock(m_mutex
);
322 bRequestUpdate
= (bIsPresent
&&
323 (bUpdate
|| m_powerStatus
== CEC_POWER_STATUS_UNKNOWN
||
324 m_powerStatus
== CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
||
325 m_powerStatus
== CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY
||
326 GetTimeMs() - m_iLastPowerStateUpdate
>= CEC_POWER_STATE_REFRESH_TIME
));
331 CheckVendorIdRequested();
332 RequestPowerStatus();
335 CLockObject
lock(m_mutex
);
336 return m_powerStatus
;
339 bool CCECBusDevice::RequestPowerStatus(void)
343 if (!MyLogicalAddressContains(m_iLogicalAddress
) &&
344 !IsUnsupportedFeature(CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
))
347 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< requesting power status of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
348 bReturn
= m_handler
->TransmitRequestPowerStatus(GetMyLogicalAddress(), m_iLogicalAddress
);
354 cec_vendor_id
CCECBusDevice::GetVendorId(bool bUpdate
/* = false */)
356 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
357 bool bRequestUpdate(false);
359 CLockObject
lock(m_mutex
);
360 bRequestUpdate
= (bIsPresent
&&
361 (bUpdate
|| m_vendor
== CEC_VENDOR_UNKNOWN
));
367 CLockObject
lock(m_mutex
);
371 bool CCECBusDevice::RequestVendorId(void)
375 if (!MyLogicalAddressContains(m_iLogicalAddress
))
378 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< requesting vendor ID of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
379 bReturn
= m_handler
->TransmitRequestVendorId(GetMyLogicalAddress(), m_iLogicalAddress
);
382 ReplaceHandler(true);
387 const char *CCECBusDevice::GetVendorName(bool bUpdate
/* = false */)
389 return ToString(GetVendorId(bUpdate
));
392 bool CCECBusDevice::MyLogicalAddressContains(cec_logical_address address
) const
394 return m_processor
->HasLogicalAddress(address
);
397 bool CCECBusDevice::NeedsPoll(void)
399 bool bSendPoll(false);
400 switch (m_iLogicalAddress
)
402 case CECDEVICE_PLAYBACKDEVICE3
:
404 cec_bus_device_status status
= m_processor
->m_busDevices
[CECDEVICE_PLAYBACKDEVICE2
]->GetStatus();
405 bSendPoll
= (status
== CEC_DEVICE_STATUS_PRESENT
|| status
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
408 case CECDEVICE_PLAYBACKDEVICE2
:
410 cec_bus_device_status status
= m_processor
->m_busDevices
[CECDEVICE_PLAYBACKDEVICE1
]->GetStatus();
411 bSendPoll
= (status
== CEC_DEVICE_STATUS_PRESENT
|| status
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
414 case CECDEVICE_RECORDINGDEVICE3
:
416 cec_bus_device_status status
= m_processor
->m_busDevices
[CECDEVICE_RECORDINGDEVICE2
]->GetStatus();
417 bSendPoll
= (status
== CEC_DEVICE_STATUS_PRESENT
|| status
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
420 case CECDEVICE_RECORDINGDEVICE2
:
422 cec_bus_device_status status
= m_processor
->m_busDevices
[CECDEVICE_RECORDINGDEVICE1
]->GetStatus();
423 bSendPoll
= (status
== CEC_DEVICE_STATUS_PRESENT
|| status
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
426 case CECDEVICE_TUNER4
:
428 cec_bus_device_status status
= m_processor
->m_busDevices
[CECDEVICE_TUNER3
]->GetStatus();
429 bSendPoll
= (status
== CEC_DEVICE_STATUS_PRESENT
|| status
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
432 case CECDEVICE_TUNER3
:
434 cec_bus_device_status status
= m_processor
->m_busDevices
[CECDEVICE_TUNER2
]->GetStatus();
435 bSendPoll
= (status
== CEC_DEVICE_STATUS_PRESENT
|| status
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
438 case CECDEVICE_TUNER2
:
440 cec_bus_device_status status
= m_processor
->m_busDevices
[CECDEVICE_TUNER1
]->GetStatus();
441 bSendPoll
= (status
== CEC_DEVICE_STATUS_PRESENT
|| status
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
444 case CECDEVICE_AUDIOSYSTEM
:
445 case CECDEVICE_PLAYBACKDEVICE1
:
446 case CECDEVICE_RECORDINGDEVICE1
:
447 case CECDEVICE_TUNER1
:
458 cec_bus_device_status
CCECBusDevice::GetStatus(bool bForcePoll
/* = false */)
460 cec_bus_device_status
status(CEC_DEVICE_STATUS_UNKNOWN
);
461 bool bNeedsPoll(false);
464 CLockObject
lock(m_mutex
);
465 status
= m_deviceStatus
;
466 bNeedsPoll
= (m_deviceStatus
!= CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
&&
467 (m_deviceStatus
== CEC_DEVICE_STATUS_UNKNOWN
|| bForcePoll
));
472 bool bPollAcked(false);
473 if (bNeedsPoll
&& NeedsPoll())
474 bPollAcked
= m_processor
->PollDevice(m_iLogicalAddress
);
476 status
= bPollAcked
? CEC_DEVICE_STATUS_PRESENT
: CEC_DEVICE_STATUS_NOT_PRESENT
;
477 SetDeviceStatus(status
);
487 void CCECBusDevice::SetCecVersion(const cec_version newVersion
)
489 m_cecVersion
= newVersion
;
490 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s (%X): CEC version %s", GetLogicalAddressName(), m_iLogicalAddress
, ToString(newVersion
));
493 void CCECBusDevice::SetMenuLanguage(const cec_menu_language
&language
)
495 CLockObject
lock(m_mutex
);
496 if (language
.device
== m_iLogicalAddress
)
498 CLibCEC::AddLog(CEC_LOG_DEBUG
, ">> %s (%X): menu language set to '%s'", GetLogicalAddressName(), m_iLogicalAddress
, language
.language
);
499 m_menuLanguage
= language
;
503 void CCECBusDevice::SetOSDName(CStdString strName
)
505 CLockObject
lock(m_mutex
);
506 if (m_strDeviceName
!= strName
)
508 CLibCEC::AddLog(CEC_LOG_DEBUG
, ">> %s (%X): osd name set to '%s'", GetLogicalAddressName(), m_iLogicalAddress
, strName
.c_str());
509 m_strDeviceName
= strName
;
513 void CCECBusDevice::SetMenuState(const cec_menu_state state
)
515 CLockObject
lock(m_mutex
);
516 if (m_menuState
!= state
)
518 CLibCEC::AddLog(CEC_LOG_DEBUG
, ">> %s (%X): menu state set to '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(m_menuState
));
523 void CCECBusDevice::SetInactiveSource(void)
526 CLockObject
lock(m_mutex
);
528 CLibCEC::AddLog(CEC_LOG_DEBUG
, "marking %s (%X) as inactive source", GetLogicalAddressName(), m_iLogicalAddress
);
529 m_bActiveSource
= false;
532 if (MyLogicalAddressContains(m_iLogicalAddress
))
533 SetPowerStatus(CEC_POWER_STATUS_STANDBY
);
536 void CCECBusDevice::SetActiveSource(void)
538 CLockObject
lock(m_mutex
);
539 if (!m_bActiveSource
)
540 CLibCEC::AddLog(CEC_LOG_DEBUG
, "making %s (%x) the active source", GetLogicalAddressName(), m_iLogicalAddress
);
542 for (int iPtr
= 0; iPtr
< 16; iPtr
++)
543 if (iPtr
!= m_iLogicalAddress
)
544 m_processor
->m_busDevices
[iPtr
]->SetInactiveSource();
546 m_bActiveSource
= true;
547 SetPowerStatus(CEC_POWER_STATUS_ON
);
550 bool CCECBusDevice::TryLogicalAddress(void)
552 CLibCEC::AddLog(CEC_LOG_DEBUG
, "trying logical address '%s'", GetLogicalAddressName());
554 m_processor
->SetAckMask(0);
555 if (!TransmitPoll(m_iLogicalAddress
))
557 CLibCEC::AddLog(CEC_LOG_NOTICE
, "using logical address '%s'", GetLogicalAddressName());
558 SetDeviceStatus(CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
563 CLibCEC::AddLog(CEC_LOG_DEBUG
, "logical address '%s' already taken", GetLogicalAddressName());
564 SetDeviceStatus(CEC_DEVICE_STATUS_PRESENT
);
568 void CCECBusDevice::SetDeviceStatus(const cec_bus_device_status newStatus
)
571 CLockObject
lock(m_mutex
);
574 case CEC_DEVICE_STATUS_UNKNOWN
:
575 if (m_deviceStatus
!= newStatus
)
576 CLibCEC::AddLog(CEC_LOG_DEBUG
, "device status of %s changed into 'unknown'", ToString(m_iLogicalAddress
));
578 m_powerStatus
= CEC_POWER_STATUS_UNKNOWN
;
579 m_vendor
= CEC_VENDOR_UNKNOWN
;
580 m_menuState
= CEC_MENU_STATE_ACTIVATED
;
581 m_bActiveSource
= false;
583 m_cecVersion
= CEC_VERSION_UNKNOWN
;
584 m_deviceStatus
= newStatus
;
586 case CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
:
587 if (m_deviceStatus
!= newStatus
)
588 CLibCEC::AddLog(CEC_LOG_DEBUG
, "device status of %s changed into 'handled by libCEC'", ToString(m_iLogicalAddress
));
590 m_powerStatus
= CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
;
591 m_vendor
= CEC_VENDOR_UNKNOWN
;
592 m_menuState
= CEC_MENU_STATE_ACTIVATED
;
593 m_bActiveSource
= false;
595 m_cecVersion
= CEC_VERSION_1_3A
;
596 m_deviceStatus
= newStatus
;
598 case CEC_DEVICE_STATUS_PRESENT
:
599 if (m_deviceStatus
!= newStatus
)
600 CLibCEC::AddLog(CEC_LOG_DEBUG
, "device status of %s changed into 'present'", ToString(m_iLogicalAddress
));
601 m_deviceStatus
= newStatus
;
603 case CEC_DEVICE_STATUS_NOT_PRESENT
:
604 if (m_deviceStatus
!= newStatus
)
605 CLibCEC::AddLog(CEC_LOG_DEBUG
, "device status of %s changed into 'not present'", ToString(m_iLogicalAddress
));
606 m_deviceStatus
= newStatus
;
611 if (newStatus
== CEC_DEVICE_STATUS_PRESENT
)
615 void CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress
)
617 CLockObject
lock(m_mutex
);
618 if (iNewAddress
> 0 && m_iPhysicalAddress
!= iNewAddress
)
620 CLibCEC::AddLog(CEC_LOG_DEBUG
, ">> %s (%X): physical address changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress
, m_iPhysicalAddress
, iNewAddress
);
621 m_iPhysicalAddress
= iNewAddress
;
625 void CCECBusDevice::SetStreamPath(uint16_t iNewAddress
, uint16_t iOldAddress
/* = 0 */)
627 CLockObject
lock(m_mutex
);
630 CLibCEC::AddLog(CEC_LOG_DEBUG
, ">> %s (%X): stream path changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress
, iOldAddress
== 0 ? m_iStreamPath
: iOldAddress
, iNewAddress
);
631 m_iStreamPath
= iNewAddress
;
633 CCECBusDevice
*device
= m_processor
->GetDeviceByPhysicalAddress(iNewAddress
, false);
636 // if a device is found with the new physical address, mark it as active, which will automatically mark all other devices as inactive
637 device
->SetActiveSource();
641 // try to find the device with the old address, and mark it as inactive when found
642 device
= m_processor
->GetDeviceByPhysicalAddress(iOldAddress
, false);
644 device
->SetInactiveSource();
650 SetPowerStatus(CEC_POWER_STATUS_ON
);
655 void CCECBusDevice::SetPowerStatus(const cec_power_status powerStatus
)
657 CLockObject
lock(m_mutex
);
658 if (m_powerStatus
!= powerStatus
)
660 m_iLastPowerStateUpdate
= GetTimeMs();
661 CLibCEC::AddLog(CEC_LOG_DEBUG
, ">> %s (%X): power status changed from '%s' to '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(m_powerStatus
), ToString(powerStatus
));
662 m_powerStatus
= powerStatus
;
666 void CCECBusDevice::MarkBusy(void)
668 CLockObject
handlerLock(m_handlerMutex
);
669 ++m_iHandlerUseCount
;
672 void CCECBusDevice::MarkReady(void)
674 CLockObject
handlerLock(m_handlerMutex
);
675 if (m_iHandlerUseCount
> 0)
676 --m_iHandlerUseCount
;
679 bool CCECBusDevice::ReplaceHandler(bool bActivateSource
/* = true */)
681 bool bInitHandler(false);
683 CTryLockObject
lock(m_mutex
);
684 if (!lock
.IsLocked())
687 CLockObject
handlerLock(m_handlerMutex
);
688 if (m_iHandlerUseCount
> 0)
693 if (m_vendor
!= m_handler
->GetVendorId())
695 if (CCECCommandHandler::HasSpecificHandler(m_vendor
))
697 CLibCEC::AddLog(CEC_LOG_DEBUG
, "replacing the command handler for device '%s' (%x)", GetLogicalAddressName(), GetLogicalAddress());
702 case CEC_VENDOR_SAMSUNG
:
703 m_handler
= new CANCommandHandler(this);
706 m_handler
= new CSLCommandHandler(this);
708 case CEC_VENDOR_PANASONIC
:
709 m_handler
= new CVLCommandHandler(this);
712 m_handler
= new CCECCommandHandler(this);
716 m_handler
->SetVendorId(m_vendor
);
724 m_handler
->InitHandler();
726 if (bActivateSource
&& m_processor
->GetLogicalAddresses().IsSet(m_iLogicalAddress
) && m_processor
->IsInitialised() && IsActiveSource())
727 m_handler
->ActivateSource();
735 bool CCECBusDevice::SetVendorId(uint64_t iVendorId
)
737 bool bVendorChanged(false);
740 CLockObject
lock(m_mutex
);
741 bVendorChanged
= (m_vendor
!= (cec_vendor_id
)iVendorId
);
742 m_vendor
= (cec_vendor_id
)iVendorId
;
745 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s (%X): vendor = %s (%06x)", GetLogicalAddressName(), m_iLogicalAddress
, ToString(m_vendor
), m_vendor
);
747 return bVendorChanged
;
751 /** @name Transmit methods */
753 bool CCECBusDevice::TransmitActiveSource(void)
755 bool bSendActiveSource(false);
758 CLockObject
lock(m_mutex
);
759 if (m_powerStatus
!= CEC_POWER_STATUS_ON
&& m_powerStatus
!= CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
)
760 CLibCEC::AddLog(CEC_LOG_DEBUG
, "<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress
);
761 else if (m_bActiveSource
)
763 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> broadcast (F): active source (%4x)", GetLogicalAddressName(), m_iLogicalAddress
, m_iPhysicalAddress
);
764 bSendActiveSource
= true;
767 CLibCEC::AddLog(CEC_LOG_DEBUG
, "<< %s (%X) is not the active source", GetLogicalAddressName(), m_iLogicalAddress
);
770 if (bSendActiveSource
)
773 m_handler
->TransmitActiveSource(m_iLogicalAddress
, m_iPhysicalAddress
);
781 bool CCECBusDevice::TransmitCECVersion(cec_logical_address dest
)
785 CLockObject
lock(m_mutex
);
786 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): cec version %s", GetLogicalAddressName(), m_iLogicalAddress
, ToString(dest
), dest
, ToString(m_cecVersion
));
787 version
= m_cecVersion
;
791 bool bReturn
= m_handler
->TransmitCECVersion(m_iLogicalAddress
, dest
, version
);
796 bool CCECBusDevice::TransmitImageViewOn(void)
799 CLockObject
lock(m_mutex
);
800 if (m_powerStatus
!= CEC_POWER_STATUS_ON
&& m_powerStatus
!= CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
)
802 CLibCEC::AddLog(CEC_LOG_DEBUG
, "<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress
);
808 m_handler
->TransmitImageViewOn(m_iLogicalAddress
, CECDEVICE_TV
);
813 bool CCECBusDevice::TransmitInactiveSource(void)
815 uint16_t iPhysicalAddress
;
817 CLockObject
lock(m_mutex
);
818 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> broadcast (F): inactive source", GetLogicalAddressName(), m_iLogicalAddress
);
819 iPhysicalAddress
= m_iPhysicalAddress
;
823 bool bReturn
= m_handler
->TransmitInactiveSource(m_iLogicalAddress
, iPhysicalAddress
);
828 bool CCECBusDevice::TransmitMenuState(cec_logical_address dest
)
830 cec_menu_state menuState
;
832 CLockObject
lock(m_mutex
);
833 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): menu state '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(dest
), dest
, ToString(m_menuState
));
834 menuState
= m_menuState
;
838 bool bReturn
= m_handler
->TransmitMenuState(m_iLogicalAddress
, dest
, menuState
);
843 bool CCECBusDevice::TransmitOSDName(cec_logical_address dest
)
845 CStdString strDeviceName
;
847 CLockObject
lock(m_mutex
);
848 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): OSD name '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(dest
), dest
, m_strDeviceName
.c_str());
849 strDeviceName
= m_strDeviceName
;
853 bool bReturn
= m_handler
->TransmitOSDName(m_iLogicalAddress
, dest
, strDeviceName
);
858 bool CCECBusDevice::TransmitOSDString(cec_logical_address dest
, cec_display_control duration
, const char *strMessage
)
861 if (!m_processor
->m_busDevices
[dest
]->IsUnsupportedFeature(CEC_OPCODE_SET_OSD_STRING
))
863 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): display OSD message '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(dest
), dest
, strMessage
);
865 bReturn
= m_handler
->TransmitOSDString(m_iLogicalAddress
, dest
, duration
, strMessage
);
871 bool CCECBusDevice::TransmitPhysicalAddress(void)
873 uint16_t iPhysicalAddress
;
874 cec_device_type type
;
876 CLockObject
lock(m_mutex
);
877 if (m_iPhysicalAddress
== 0xffff)
880 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> broadcast (F): physical adddress %4x", GetLogicalAddressName(), m_iLogicalAddress
, m_iPhysicalAddress
);
881 iPhysicalAddress
= m_iPhysicalAddress
;
886 bool bReturn
= m_handler
->TransmitPhysicalAddress(m_iLogicalAddress
, iPhysicalAddress
, type
);
891 bool CCECBusDevice::TransmitPoll(cec_logical_address dest
)
894 if (dest
== CECDEVICE_UNKNOWN
)
895 dest
= m_iLogicalAddress
;
897 CCECBusDevice
*destDevice
= m_processor
->m_busDevices
[dest
];
898 if (destDevice
->m_deviceStatus
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
)
902 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): POLL", GetLogicalAddressName(), m_iLogicalAddress
, ToString(dest
), dest
);
903 bReturn
= m_handler
->TransmitPoll(m_iLogicalAddress
, dest
);
904 CLibCEC::AddLog(CEC_LOG_DEBUG
, bReturn
? ">> POLL sent" : ">> POLL not sent");
906 CLockObject
lock(m_mutex
);
909 m_iLastActive
= GetTimeMs();
910 destDevice
->m_deviceStatus
= CEC_DEVICE_STATUS_PRESENT
;
913 destDevice
->m_deviceStatus
= CEC_DEVICE_STATUS_NOT_PRESENT
;
919 bool CCECBusDevice::TransmitPowerState(cec_logical_address dest
)
921 cec_power_status state
;
923 CLockObject
lock(m_mutex
);
924 if (!IsActiveSource())
926 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
);
927 SetPowerStatus(CEC_POWER_STATUS_STANDBY
);
930 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): %s", GetLogicalAddressName(), m_iLogicalAddress
, ToString(dest
), dest
, ToString(m_powerStatus
));
931 state
= m_powerStatus
;
935 bool bReturn
= m_handler
->TransmitPowerState(m_iLogicalAddress
, dest
, state
);
940 bool CCECBusDevice::TransmitVendorID(cec_logical_address dest
, bool bSendAbort
/* = true */)
945 CLockObject
lock(m_mutex
);
946 iVendorId
= (uint64_t)m_vendor
;
950 if (iVendorId
== CEC_VENDOR_UNKNOWN
)
954 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): vendor id feature abort", GetLogicalAddressName(), m_iLogicalAddress
, ToString(dest
), dest
);
955 m_processor
->TransmitAbort(dest
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
961 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
);
962 bReturn
= m_handler
->TransmitVendorID(m_iLogicalAddress
, iVendorId
);
968 bool CCECBusDevice::TransmitKeypress(cec_user_control_code key
, bool bWait
/* = true */)
971 bool bReturn
= m_handler
->TransmitKeypress(m_processor
->GetLogicalAddress(), m_iLogicalAddress
, key
, bWait
);
976 bool CCECBusDevice::TransmitKeyRelease(bool bWait
/* = true */)
979 bool bReturn
= m_handler
->TransmitKeyRelease(m_processor
->GetLogicalAddress(), m_iLogicalAddress
, bWait
);
984 bool CCECBusDevice::IsUnsupportedFeature(cec_opcode opcode
) const
986 bool bUnsupported
= (m_unsupportedFeatures
.find(opcode
) != m_unsupportedFeatures
.end());
988 CLibCEC::AddLog(CEC_LOG_NOTICE
, "'%s' is marked as unsupported feature for device '%s'", ToString(opcode
), GetLogicalAddressName());
992 void CCECBusDevice::SetUnsupportedFeature(cec_opcode opcode
)
994 CLibCEC::AddLog(CEC_LOG_DEBUG
, "marking opcode '%s' as unsupported feature for device '%s'", ToString(opcode
), GetLogicalAddressName());
995 m_unsupportedFeatures
.insert(opcode
);
998 bool CCECBusDevice::ActivateSource(void)
1001 bool bReturn
= m_handler
->ActivateSource();
1006 void CCECBusDevice::HandlePoll(cec_logical_address iDestination
)
1008 CLibCEC::AddLog(CEC_LOG_DEBUG
, "<< POLL: %s (%x) -> %s (%x)", ToString(m_iLogicalAddress
), m_iLogicalAddress
, ToString(iDestination
), iDestination
);
1009 m_bAwaitingReceiveFailed
= true;
1012 bool CCECBusDevice::HandleReceiveFailed(void)
1014 bool bReturn
= m_bAwaitingReceiveFailed
;
1015 m_bAwaitingReceiveFailed
= false;
1019 void CCECBusDevice::CheckVendorIdRequested(void)
1021 bool bRequestVendorId(false);
1023 CLockObject
lock(m_mutex
);
1024 bRequestVendorId
= !m_bVendorIdRequested
;
1025 m_bVendorIdRequested
= true;
1028 if (bRequestVendorId
)
1030 ReplaceHandler(false);