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)
65 m_handler
= new CCECCommandHandler(this);
67 for (unsigned int iPtr
= 0; iPtr
< 4; iPtr
++)
68 m_menuLanguage
.language
[iPtr
] = '?';
69 m_menuLanguage
.language
[3] = 0;
70 m_menuLanguage
.device
= iLogicalAddress
;
72 m_strDeviceName
= ToString(m_iLogicalAddress
);
75 CCECBusDevice::~CCECBusDevice(void)
80 bool CCECBusDevice::HandleCommand(const cec_command
&command
)
84 /* update "last active" */
86 CLockObject
lock(m_mutex
);
87 m_iLastActive
= GetTimeMs();
89 if (m_deviceStatus
!= CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
)
90 m_deviceStatus
= CEC_DEVICE_STATUS_PRESENT
;
95 /* handle the command */
96 bHandled
= m_handler
->HandleCommand(command
);
98 /* change status to present */
101 CLockObject
lock(m_mutex
);
102 if (m_deviceStatus
!= CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
)
104 if (m_deviceStatus
!= CEC_DEVICE_STATUS_PRESENT
)
105 CLibCEC::AddLog(CEC_LOG_DEBUG
, "device %s (%x) status changed to present after command %s", GetLogicalAddressName(), (uint8_t)GetLogicalAddress(), ToString(command
.opcode
));
106 m_deviceStatus
= CEC_DEVICE_STATUS_PRESENT
;
114 bool CCECBusDevice::PowerOn(void)
117 GetVendorId(); // ensure that we got the vendor id, because the implementations vary per vendor
120 cec_power_status currentStatus
= GetPowerStatus(false);
121 if (currentStatus
!= CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
&&
122 currentStatus
!= CEC_POWER_STATUS_ON
)
124 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< powering on '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
125 if (m_handler
->PowerOn(GetMyLogicalAddress(), m_iLogicalAddress
))
127 SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
133 CLibCEC::AddLog(CEC_LOG_NOTICE
, "'%s' (%X) is already '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(currentStatus
));
140 bool CCECBusDevice::Standby(void)
142 CLibCEC::AddLog(CEC_LOG_DEBUG
, "<< putting '%s' (%X) in standby mode", GetLogicalAddressName(), m_iLogicalAddress
);
144 bool bReturn
= m_handler
->TransmitStandby(GetMyLogicalAddress(), m_iLogicalAddress
);
151 cec_version
CCECBusDevice::GetCecVersion(bool bUpdate
/* = false */)
153 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
154 bool bRequestUpdate(false);
156 CLockObject
lock(m_mutex
);
157 bRequestUpdate
= bIsPresent
&&
158 (bUpdate
|| m_cecVersion
== CEC_VERSION_UNKNOWN
);
164 CLockObject
lock(m_mutex
);
168 bool CCECBusDevice::RequestCecVersion(void)
172 if (!MyLogicalAddressContains(m_iLogicalAddress
) &&
173 !IsUnsupportedFeature(CEC_OPCODE_GET_CEC_VERSION
))
176 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< requesting CEC version of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
178 bReturn
= m_handler
->TransmitRequestCecVersion(GetMyLogicalAddress(), m_iLogicalAddress
);
184 const char* CCECBusDevice::GetLogicalAddressName(void) const
186 return ToString(m_iLogicalAddress
);
189 cec_menu_language
&CCECBusDevice::GetMenuLanguage(bool bUpdate
/* = false */)
191 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
192 bool bRequestUpdate(false);
194 CLockObject
lock(m_mutex
);
195 bRequestUpdate
= (bIsPresent
&&
196 (bUpdate
|| !strcmp(m_menuLanguage
.language
, "???")));
200 RequestMenuLanguage();
202 CLockObject
lock(m_mutex
);
203 return m_menuLanguage
;
206 bool CCECBusDevice::RequestMenuLanguage(void)
210 if (!MyLogicalAddressContains(m_iLogicalAddress
) &&
211 !IsUnsupportedFeature(CEC_OPCODE_GET_MENU_LANGUAGE
))
214 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< requesting menu language of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
215 bReturn
= m_handler
->TransmitRequestMenuLanguage(GetMyLogicalAddress(), m_iLogicalAddress
);
221 cec_menu_state
CCECBusDevice::GetMenuState(void)
223 CLockObject
lock(m_mutex
);
227 cec_logical_address
CCECBusDevice::GetMyLogicalAddress(void) const
229 return m_processor
->GetLogicalAddress();
232 uint16_t CCECBusDevice::GetMyPhysicalAddress(void) const
234 return m_processor
->GetPhysicalAddress();
237 CStdString
CCECBusDevice::GetOSDName(bool bUpdate
/* = false */)
239 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
240 bool bRequestUpdate(false);
242 CLockObject
lock(m_mutex
);
243 bRequestUpdate
= bIsPresent
&&
244 (bUpdate
|| m_strDeviceName
.Equals(ToString(m_iLogicalAddress
))) &&
245 m_type
!= CEC_DEVICE_TYPE_TV
;
251 CLockObject
lock(m_mutex
);
252 return m_strDeviceName
;
255 bool CCECBusDevice::RequestOSDName(void)
259 if (!MyLogicalAddressContains(m_iLogicalAddress
) &&
260 !IsUnsupportedFeature(CEC_OPCODE_GIVE_OSD_NAME
))
263 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< requesting OSD name of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
264 bReturn
= m_handler
->TransmitRequestOSDName(GetMyLogicalAddress(), m_iLogicalAddress
);
270 uint16_t CCECBusDevice::GetPhysicalAddress(bool bUpdate
/* = false */)
272 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
273 bool bRequestUpdate(false);
275 CLockObject
lock(m_mutex
);
276 bRequestUpdate
= bIsPresent
&&
277 (m_iPhysicalAddress
== 0xFFFF || bUpdate
);
280 if (bRequestUpdate
&& !RequestPhysicalAddress())
281 CLibCEC::AddLog(CEC_LOG_ERROR
, "failed to request the physical address");
283 CLockObject
lock(m_mutex
);
284 return m_iPhysicalAddress
;
287 bool CCECBusDevice::RequestPhysicalAddress(void)
291 if (!MyLogicalAddressContains(m_iLogicalAddress
))
294 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< requesting physical address of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
295 bReturn
= m_handler
->TransmitRequestPhysicalAddress(GetMyLogicalAddress(), m_iLogicalAddress
);
301 cec_power_status
CCECBusDevice::GetPowerStatus(bool bUpdate
/* = false */)
303 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
304 bool bRequestUpdate(false);
306 CLockObject
lock(m_mutex
);
307 bRequestUpdate
= (bIsPresent
&&
308 (bUpdate
|| m_powerStatus
== CEC_POWER_STATUS_UNKNOWN
||
309 m_powerStatus
== CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
||
310 m_powerStatus
== CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY
||
311 GetTimeMs() - m_iLastPowerStateUpdate
>= CEC_POWER_STATE_REFRESH_TIME
));
315 RequestPowerStatus();
317 CLockObject
lock(m_mutex
);
318 return m_powerStatus
;
321 bool CCECBusDevice::RequestPowerStatus(void)
325 if (!MyLogicalAddressContains(m_iLogicalAddress
) &&
326 !IsUnsupportedFeature(CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
))
329 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< requesting power status of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
330 bReturn
= m_handler
->TransmitRequestPowerStatus(GetMyLogicalAddress(), m_iLogicalAddress
);
336 cec_vendor_id
CCECBusDevice::GetVendorId(bool bUpdate
/* = false */)
338 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
339 bool bRequestUpdate(false);
341 CLockObject
lock(m_mutex
);
342 bRequestUpdate
= (bIsPresent
&&
343 (bUpdate
|| m_vendor
== CEC_VENDOR_UNKNOWN
));
349 CLockObject
lock(m_mutex
);
353 bool CCECBusDevice::RequestVendorId(void)
357 if (!MyLogicalAddressContains(m_iLogicalAddress
))
360 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< requesting vendor ID of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
361 bReturn
= m_handler
->TransmitRequestVendorId(GetMyLogicalAddress(), m_iLogicalAddress
);
364 ReplaceHandler(true);
369 const char *CCECBusDevice::GetVendorName(bool bUpdate
/* = false */)
371 return ToString(GetVendorId(bUpdate
));
374 bool CCECBusDevice::MyLogicalAddressContains(cec_logical_address address
) const
376 return m_processor
->HasLogicalAddress(address
);
379 bool CCECBusDevice::NeedsPoll(void)
381 bool bSendPoll(false);
382 switch (m_iLogicalAddress
)
384 case CECDEVICE_PLAYBACKDEVICE3
:
386 cec_bus_device_status status
= m_processor
->m_busDevices
[CECDEVICE_PLAYBACKDEVICE2
]->GetStatus();
387 bSendPoll
= (status
== CEC_DEVICE_STATUS_PRESENT
|| status
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
390 case CECDEVICE_PLAYBACKDEVICE2
:
392 cec_bus_device_status status
= m_processor
->m_busDevices
[CECDEVICE_PLAYBACKDEVICE1
]->GetStatus();
393 bSendPoll
= (status
== CEC_DEVICE_STATUS_PRESENT
|| status
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
396 case CECDEVICE_RECORDINGDEVICE3
:
398 cec_bus_device_status status
= m_processor
->m_busDevices
[CECDEVICE_RECORDINGDEVICE2
]->GetStatus();
399 bSendPoll
= (status
== CEC_DEVICE_STATUS_PRESENT
|| status
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
402 case CECDEVICE_RECORDINGDEVICE2
:
404 cec_bus_device_status status
= m_processor
->m_busDevices
[CECDEVICE_RECORDINGDEVICE1
]->GetStatus();
405 bSendPoll
= (status
== CEC_DEVICE_STATUS_PRESENT
|| status
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
408 case CECDEVICE_TUNER4
:
410 cec_bus_device_status status
= m_processor
->m_busDevices
[CECDEVICE_TUNER3
]->GetStatus();
411 bSendPoll
= (status
== CEC_DEVICE_STATUS_PRESENT
|| status
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
414 case CECDEVICE_TUNER3
:
416 cec_bus_device_status status
= m_processor
->m_busDevices
[CECDEVICE_TUNER2
]->GetStatus();
417 bSendPoll
= (status
== CEC_DEVICE_STATUS_PRESENT
|| status
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
420 case CECDEVICE_TUNER2
:
422 cec_bus_device_status status
= m_processor
->m_busDevices
[CECDEVICE_TUNER1
]->GetStatus();
423 bSendPoll
= (status
== CEC_DEVICE_STATUS_PRESENT
|| status
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
426 case CECDEVICE_AUDIOSYSTEM
:
427 case CECDEVICE_PLAYBACKDEVICE1
:
428 case CECDEVICE_RECORDINGDEVICE1
:
429 case CECDEVICE_TUNER1
:
440 cec_bus_device_status
CCECBusDevice::GetStatus(bool bForcePoll
/* = false */)
442 CLockObject
lock(m_mutex
);
443 if (m_deviceStatus
!= CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
&&
444 (m_deviceStatus
== CEC_DEVICE_STATUS_UNKNOWN
|| bForcePoll
))
447 bool bPollAcked(false);
448 if (bForcePoll
|| NeedsPoll())
449 bPollAcked
= m_processor
->PollDevice(m_iLogicalAddress
);
452 m_deviceStatus
= bPollAcked
? CEC_DEVICE_STATUS_PRESENT
: CEC_DEVICE_STATUS_NOT_PRESENT
;
455 return m_deviceStatus
;
462 void CCECBusDevice::SetCecVersion(const cec_version newVersion
)
464 m_cecVersion
= newVersion
;
465 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s (%X): CEC version %s", GetLogicalAddressName(), m_iLogicalAddress
, ToString(newVersion
));
468 void CCECBusDevice::SetMenuLanguage(const cec_menu_language
&language
)
470 CLockObject
lock(m_mutex
);
471 if (language
.device
== m_iLogicalAddress
)
473 CLibCEC::AddLog(CEC_LOG_DEBUG
, ">> %s (%X): menu language set to '%s'", GetLogicalAddressName(), m_iLogicalAddress
, language
.language
);
474 m_menuLanguage
= language
;
478 void CCECBusDevice::SetOSDName(CStdString strName
)
480 CLockObject
lock(m_mutex
);
481 if (m_strDeviceName
!= strName
)
483 CLibCEC::AddLog(CEC_LOG_DEBUG
, ">> %s (%X): osd name set to '%s'", GetLogicalAddressName(), m_iLogicalAddress
, strName
.c_str());
484 m_strDeviceName
= strName
;
488 void CCECBusDevice::SetMenuState(const cec_menu_state state
)
490 CLockObject
lock(m_mutex
);
491 if (m_menuState
!= state
)
493 CLibCEC::AddLog(CEC_LOG_DEBUG
, ">> %s (%X): menu state set to '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(m_menuState
));
498 void CCECBusDevice::SetInactiveSource(void)
501 CLockObject
lock(m_mutex
);
502 m_bActiveSource
= false;
505 if (MyLogicalAddressContains(m_iLogicalAddress
))
506 SetPowerStatus(CEC_POWER_STATUS_STANDBY
);
509 void CCECBusDevice::SetActiveSource(void)
511 CLockObject
lock(m_mutex
);
512 if (!m_bActiveSource
)
513 CLibCEC::AddLog(CEC_LOG_DEBUG
, "making %s (%x) the active source", GetLogicalAddressName(), m_iLogicalAddress
);
515 for (int iPtr
= 0; iPtr
< 16; iPtr
++)
516 if (iPtr
!= m_iLogicalAddress
)
517 m_processor
->m_busDevices
[iPtr
]->SetInactiveSource();
519 m_bActiveSource
= true;
520 SetPowerStatus(CEC_POWER_STATUS_ON
);
523 bool CCECBusDevice::TryLogicalAddress(void)
525 CLibCEC::AddLog(CEC_LOG_DEBUG
, "trying logical address '%s'", GetLogicalAddressName());
527 m_processor
->SetAckMask(0);
528 if (!TransmitPoll(m_iLogicalAddress
))
530 CLibCEC::AddLog(CEC_LOG_NOTICE
, "using logical address '%s'", GetLogicalAddressName());
531 SetDeviceStatus(CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
536 CLibCEC::AddLog(CEC_LOG_DEBUG
, "logical address '%s' already taken", GetLogicalAddressName());
537 SetDeviceStatus(CEC_DEVICE_STATUS_PRESENT
);
541 void CCECBusDevice::SetDeviceStatus(const cec_bus_device_status newStatus
)
543 CLockObject
lock(m_mutex
);
546 case CEC_DEVICE_STATUS_UNKNOWN
:
547 if (m_deviceStatus
!= newStatus
)
548 CLibCEC::AddLog(CEC_LOG_DEBUG
, "device status of %s changed into 'unknown'", ToString(m_iLogicalAddress
));
550 m_powerStatus
= CEC_POWER_STATUS_UNKNOWN
;
551 m_vendor
= CEC_VENDOR_UNKNOWN
;
552 m_menuState
= CEC_MENU_STATE_ACTIVATED
;
553 m_bActiveSource
= false;
555 m_cecVersion
= CEC_VERSION_UNKNOWN
;
556 m_deviceStatus
= newStatus
;
558 case CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
:
559 if (m_deviceStatus
!= newStatus
)
560 CLibCEC::AddLog(CEC_LOG_DEBUG
, "device status of %s changed into 'handled by libCEC'", ToString(m_iLogicalAddress
));
562 m_powerStatus
= CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
;
563 m_vendor
= CEC_VENDOR_UNKNOWN
;
564 m_menuState
= CEC_MENU_STATE_ACTIVATED
;
565 m_bActiveSource
= false;
567 m_cecVersion
= CEC_VERSION_1_3A
;
568 m_deviceStatus
= newStatus
;
570 case CEC_DEVICE_STATUS_PRESENT
:
571 if (m_deviceStatus
!= newStatus
)
572 CLibCEC::AddLog(CEC_LOG_DEBUG
, "device status of %s changed into 'present'", ToString(m_iLogicalAddress
));
573 m_deviceStatus
= newStatus
;
575 case CEC_DEVICE_STATUS_NOT_PRESENT
:
576 if (m_deviceStatus
!= newStatus
)
577 CLibCEC::AddLog(CEC_LOG_DEBUG
, "device status of %s changed into 'not present'", ToString(m_iLogicalAddress
));
578 m_deviceStatus
= newStatus
;
583 void CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress
)
585 CLockObject
lock(m_mutex
);
586 if (iNewAddress
> 0 && m_iPhysicalAddress
!= iNewAddress
)
588 CLibCEC::AddLog(CEC_LOG_DEBUG
, ">> %s (%X): physical address changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress
, m_iPhysicalAddress
, iNewAddress
);
589 m_iPhysicalAddress
= iNewAddress
;
593 void CCECBusDevice::SetStreamPath(uint16_t iNewAddress
, uint16_t iOldAddress
/* = 0 */)
595 CLockObject
lock(m_mutex
);
598 CLibCEC::AddLog(CEC_LOG_DEBUG
, ">> %s (%X): stream path changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress
, iOldAddress
== 0 ? m_iStreamPath
: iOldAddress
, iNewAddress
);
599 m_iStreamPath
= iNewAddress
;
604 SetPowerStatus(CEC_POWER_STATUS_ON
);
609 void CCECBusDevice::SetPowerStatus(const cec_power_status powerStatus
)
611 CLockObject
lock(m_mutex
);
612 if (m_powerStatus
!= powerStatus
)
614 m_iLastPowerStateUpdate
= GetTimeMs();
615 CLibCEC::AddLog(CEC_LOG_DEBUG
, ">> %s (%X): power status changed from '%s' to '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(m_powerStatus
), ToString(powerStatus
));
616 m_powerStatus
= powerStatus
;
620 void CCECBusDevice::MarkBusy(void)
622 CLockObject
handlerLock(m_handlerMutex
);
623 ++m_iHandlerUseCount
;
626 void CCECBusDevice::MarkReady(void)
628 CLockObject
handlerLock(m_handlerMutex
);
629 if (m_iHandlerUseCount
> 0)
630 --m_iHandlerUseCount
;
633 bool CCECBusDevice::ReplaceHandler(bool bActivateSource
/* = true */)
635 bool bInitHandler(false);
637 CTryLockObject
lock(m_mutex
);
638 if (!lock
.IsLocked())
641 CLockObject
handlerLock(m_handlerMutex
);
642 if (m_iHandlerUseCount
> 0)
647 if (m_vendor
!= m_handler
->GetVendorId())
649 if (CCECCommandHandler::HasSpecificHandler(m_vendor
))
651 CLibCEC::AddLog(CEC_LOG_DEBUG
, "replacing the command handler for device '%s' (%x)", GetLogicalAddressName(), GetLogicalAddress());
656 case CEC_VENDOR_SAMSUNG
:
657 m_handler
= new CANCommandHandler(this);
660 m_handler
= new CSLCommandHandler(this);
662 case CEC_VENDOR_PANASONIC
:
663 m_handler
= new CVLCommandHandler(this);
666 m_handler
= new CCECCommandHandler(this);
670 m_handler
->SetVendorId(m_vendor
);
678 m_handler
->InitHandler();
680 if (bActivateSource
&& m_processor
->GetLogicalAddresses().IsSet(m_iLogicalAddress
) && m_processor
->IsInitialised() && IsActiveSource())
681 m_handler
->ActivateSource();
689 bool CCECBusDevice::SetVendorId(uint64_t iVendorId
)
691 bool bVendorChanged(false);
694 CLockObject
lock(m_mutex
);
695 bVendorChanged
= (m_vendor
!= (cec_vendor_id
)iVendorId
);
696 m_vendor
= (cec_vendor_id
)iVendorId
;
699 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s (%X): vendor = %s (%06x)", GetLogicalAddressName(), m_iLogicalAddress
, ToString(m_vendor
), m_vendor
);
701 return bVendorChanged
;
705 /** @name Transmit methods */
707 bool CCECBusDevice::TransmitActiveSource(void)
709 bool bSendActiveSource(false);
712 CLockObject
lock(m_mutex
);
713 if (m_powerStatus
!= CEC_POWER_STATUS_ON
&& m_powerStatus
!= CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
)
714 CLibCEC::AddLog(CEC_LOG_DEBUG
, "<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress
);
715 else if (m_bActiveSource
)
717 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> broadcast (F): active source (%4x)", GetLogicalAddressName(), m_iLogicalAddress
, m_iPhysicalAddress
);
718 bSendActiveSource
= true;
721 CLibCEC::AddLog(CEC_LOG_DEBUG
, "<< %s (%X) is not the active source", GetLogicalAddressName(), m_iLogicalAddress
);
724 if (bSendActiveSource
)
727 m_handler
->TransmitActiveSource(m_iLogicalAddress
, m_iPhysicalAddress
);
735 bool CCECBusDevice::TransmitCECVersion(cec_logical_address dest
)
739 CLockObject
lock(m_mutex
);
740 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): cec version %s", GetLogicalAddressName(), m_iLogicalAddress
, ToString(dest
), dest
, ToString(m_cecVersion
));
741 version
= m_cecVersion
;
745 bool bReturn
= m_handler
->TransmitCECVersion(m_iLogicalAddress
, dest
, version
);
750 bool CCECBusDevice::TransmitImageViewOn(void)
753 CLockObject
lock(m_mutex
);
754 if (m_powerStatus
!= CEC_POWER_STATUS_ON
&& m_powerStatus
!= CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
)
756 CLibCEC::AddLog(CEC_LOG_DEBUG
, "<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress
);
762 m_handler
->TransmitImageViewOn(m_iLogicalAddress
, CECDEVICE_TV
);
767 bool CCECBusDevice::TransmitInactiveSource(void)
769 uint16_t iPhysicalAddress
;
771 CLockObject
lock(m_mutex
);
772 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> broadcast (F): inactive source", GetLogicalAddressName(), m_iLogicalAddress
);
773 iPhysicalAddress
= m_iPhysicalAddress
;
777 bool bReturn
= m_handler
->TransmitInactiveSource(m_iLogicalAddress
, iPhysicalAddress
);
782 bool CCECBusDevice::TransmitMenuState(cec_logical_address dest
)
784 cec_menu_state menuState
;
786 CLockObject
lock(m_mutex
);
787 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): menu state '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(dest
), dest
, ToString(m_menuState
));
788 menuState
= m_menuState
;
792 bool bReturn
= m_handler
->TransmitMenuState(m_iLogicalAddress
, dest
, menuState
);
797 bool CCECBusDevice::TransmitOSDName(cec_logical_address dest
)
799 CStdString strDeviceName
;
801 CLockObject
lock(m_mutex
);
802 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): OSD name '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(dest
), dest
, m_strDeviceName
.c_str());
803 strDeviceName
= m_strDeviceName
;
807 bool bReturn
= m_handler
->TransmitOSDName(m_iLogicalAddress
, dest
, strDeviceName
);
812 bool CCECBusDevice::TransmitOSDString(cec_logical_address dest
, cec_display_control duration
, const char *strMessage
)
815 if (!m_processor
->m_busDevices
[dest
]->IsUnsupportedFeature(CEC_OPCODE_SET_OSD_STRING
))
817 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): display OSD message '%s'", GetLogicalAddressName(), m_iLogicalAddress
, ToString(dest
), dest
, strMessage
);
819 bReturn
= m_handler
->TransmitOSDString(m_iLogicalAddress
, dest
, duration
, strMessage
);
825 bool CCECBusDevice::TransmitPhysicalAddress(void)
827 uint16_t iPhysicalAddress
;
828 cec_device_type type
;
830 CLockObject
lock(m_mutex
);
831 if (m_iPhysicalAddress
== 0xffff)
834 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> broadcast (F): physical adddress %4x", GetLogicalAddressName(), m_iLogicalAddress
, m_iPhysicalAddress
);
835 iPhysicalAddress
= m_iPhysicalAddress
;
840 bool bReturn
= m_handler
->TransmitPhysicalAddress(m_iLogicalAddress
, iPhysicalAddress
, type
);
845 bool CCECBusDevice::TransmitPoll(cec_logical_address dest
)
848 if (dest
== CECDEVICE_UNKNOWN
)
849 dest
= m_iLogicalAddress
;
851 CCECBusDevice
*destDevice
= m_processor
->m_busDevices
[dest
];
852 if (destDevice
->m_deviceStatus
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
)
856 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): POLL", GetLogicalAddressName(), m_iLogicalAddress
, ToString(dest
), dest
);
857 bReturn
= m_handler
->TransmitPoll(m_iLogicalAddress
, dest
);
858 CLibCEC::AddLog(CEC_LOG_DEBUG
, bReturn
? ">> POLL sent" : ">> POLL not sent");
860 CLockObject
lock(m_mutex
);
863 m_iLastActive
= GetTimeMs();
864 destDevice
->m_deviceStatus
= CEC_DEVICE_STATUS_PRESENT
;
867 destDevice
->m_deviceStatus
= CEC_DEVICE_STATUS_NOT_PRESENT
;
873 bool CCECBusDevice::TransmitPowerState(cec_logical_address dest
)
875 cec_power_status state
;
877 CLockObject
lock(m_mutex
);
878 if (!IsActiveSource())
879 SetPowerStatus(CEC_POWER_STATUS_STANDBY
);
881 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): %s", GetLogicalAddressName(), m_iLogicalAddress
, ToString(dest
), dest
, ToString(m_powerStatus
));
882 state
= m_powerStatus
;
886 bool bReturn
= m_handler
->TransmitPowerState(m_iLogicalAddress
, dest
, state
);
891 bool CCECBusDevice::TransmitVendorID(cec_logical_address dest
, bool bSendAbort
/* = true */)
896 CLockObject
lock(m_mutex
);
897 iVendorId
= (uint64_t)m_vendor
;
901 if (iVendorId
== CEC_VENDOR_UNKNOWN
)
905 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< %s (%X) -> %s (%X): vendor id feature abort", GetLogicalAddressName(), m_iLogicalAddress
, ToString(dest
), dest
);
906 m_processor
->TransmitAbort(dest
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
912 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
);
913 bReturn
= m_handler
->TransmitVendorID(m_iLogicalAddress
, iVendorId
);
919 bool CCECBusDevice::TransmitKeypress(cec_user_control_code key
, bool bWait
/* = true */)
922 bool bReturn
= m_handler
->TransmitKeypress(m_processor
->GetLogicalAddress(), m_iLogicalAddress
, key
, bWait
);
927 bool CCECBusDevice::TransmitKeyRelease(bool bWait
/* = true */)
930 bool bReturn
= m_handler
->TransmitKeyRelease(m_processor
->GetLogicalAddress(), m_iLogicalAddress
, bWait
);
935 bool CCECBusDevice::IsUnsupportedFeature(cec_opcode opcode
) const
937 bool bUnsupported
= (m_unsupportedFeatures
.find(opcode
) != m_unsupportedFeatures
.end());
939 CLibCEC::AddLog(CEC_LOG_NOTICE
, "'%s' is marked as unsupported feature for device '%s'", ToString(opcode
), GetLogicalAddressName());
943 void CCECBusDevice::SetUnsupportedFeature(cec_opcode opcode
)
945 CLibCEC::AddLog(CEC_LOG_DEBUG
, "marking opcode '%s' as unsupported feature for device '%s'", ToString(opcode
), GetLogicalAddressName());
946 m_unsupportedFeatures
.insert(opcode
);
949 bool CCECBusDevice::ActivateSource(void)
952 bool bReturn
= m_handler
->ActivateSource();
957 void CCECBusDevice::HandlePoll(cec_logical_address iDestination
)
959 CLibCEC::AddLog(CEC_LOG_DEBUG
, "<< POLL: %s (%x) -> %s (%x)", ToString(m_iLogicalAddress
), m_iLogicalAddress
, ToString(iDestination
), iDestination
);
960 m_bAwaitingReceiveFailed
= true;
963 bool CCECBusDevice::HandleReceiveFailed(void)
965 bool bReturn
= m_bAwaitingReceiveFailed
;
966 m_bAwaitingReceiveFailed
= false;