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 "CECClient.h"
34 #include "CECProcessor.h"
36 #include "devices/CECPlaybackDevice.h"
37 #include "devices/CECAudioSystem.h"
38 #include "devices/CECTV.h"
41 using namespace PLATFORM
;
43 #define LIB_CEC m_processor->GetLib()
44 #define ToString(x) LIB_CEC->ToString(x)
46 CCECClient::CCECClient(CCECProcessor
*processor
, const libcec_configuration
&configuration
) :
47 m_processor(processor
),
48 m_bInitialised(false),
50 m_iCurrentButton(CEC_USER_CONTROL_CODE_UNKNOWN
),
53 // set the initial configuration
54 SetConfiguration(configuration
);
57 CCECClient::~CCECClient(void)
59 // unregister the client
60 if (m_processor
&& IsRegistered())
61 m_processor
->UnregisterClient(this);
64 bool CCECClient::IsInitialised(void)
66 CLockObject
lock(m_mutex
);
67 return m_bInitialised
&& m_processor
;
70 void CCECClient::SetInitialised(bool bSetTo
)
72 CLockObject
lock(m_mutex
);
73 m_bInitialised
= bSetTo
;
76 bool CCECClient::IsRegistered(void)
78 CLockObject
lock(m_mutex
);
79 return m_bRegistered
&& m_processor
;
82 void CCECClient::SetRegistered(bool bSetTo
)
84 CLockObject
lock(m_mutex
);
85 m_bRegistered
= bSetTo
;
88 bool CCECClient::OnRegister(void)
90 // return false if already initialised
94 // get all device we control
96 m_processor
->GetDevices()->GetByLogicalAddresses(devices
, m_configuration
.logicalAddresses
);
98 // return false when no devices were found
101 LIB_CEC
->AddLog(CEC_LOG_WARNING
, "cannot find the primary device (logical address %x)", GetPrimaryLogicalAdddress());
105 // mark as initialised
106 SetInitialised(true);
108 // configure all devices
109 for (CECDEVICEVEC::iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
111 // only set our OSD name for the primary device
112 if ((*it
)->GetLogicalAddress() == GetPrimaryLogicalAdddress())
113 (*it
)->SetOSDName(m_configuration
.strDeviceName
);
115 // set the default menu language for devices we control
116 (*it
)->SetMenuLanguage(m_configuration
.strDeviceLanguage
);
119 // set the physical address
120 SetPhysicalAddress(m_configuration
);
122 // ensure that we know the vendor id of the TV, so we are using the correct handler
123 m_processor
->GetTV()->GetVendorId(GetPrimaryLogicalAdddress());
125 // make the primary device the active source if the option is set
126 if (m_configuration
.bActivateSource
== 1)
127 GetPrimaryDevice()->ActivateSource();
132 bool CCECClient::SetHDMIPort(const cec_logical_address iBaseDevice
, const uint8_t iPort
, bool bForce
/* = false */)
136 // limit the HDMI port range to 1-15
137 if (iPort
< CEC_MIN_HDMI_PORTNUMBER
||
138 iPort
> CEC_MAX_HDMI_PORTNUMBER
)
141 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "setting HDMI port to %d on device %s (%d)", iPort
, ToString(iBaseDevice
), (int)iBaseDevice
);
143 // update the configuration
145 CLockObject
lock(m_mutex
);
146 m_configuration
.baseDevice
= iBaseDevice
;
147 m_configuration
.iHDMIPort
= iPort
;
150 // don't continue if the connection isn't opened
151 if (!m_processor
->IsRunning() && !bForce
)
154 // get the PA of the base device
155 uint16_t iPhysicalAddress(CEC_INVALID_PHYSICAL_ADDRESS
);
156 CCECBusDevice
*baseDevice
= m_processor
->GetDevice(iBaseDevice
);
158 iPhysicalAddress
= baseDevice
->GetPhysicalAddress(GetPrimaryLogicalAdddress());
160 // add our port number
161 if (iPhysicalAddress
<= CEC_MAX_PHYSICAL_ADDRESS
)
163 if (iPhysicalAddress
== 0)
164 iPhysicalAddress
+= 0x1000 * iPort
;
165 else if (iPhysicalAddress
% 0x1000 == 0)
166 iPhysicalAddress
+= 0x100 * iPort
;
167 else if (iPhysicalAddress
% 0x100 == 0)
168 iPhysicalAddress
+= 0x10 * iPort
;
169 else if (iPhysicalAddress
% 0x10 == 0)
170 iPhysicalAddress
+= iPort
;
175 // set the default address when something went wrong
178 LIB_CEC
->AddLog(CEC_LOG_WARNING
, "failed to set the physical address to %04X, setting it to the default value %04X", iPhysicalAddress
, CEC_DEFAULT_PHYSICAL_ADDRESS
);
179 iPhysicalAddress
= CEC_DEFAULT_PHYSICAL_ADDRESS
;
182 // and set the address
183 SetDevicePhysicalAddress(iPhysicalAddress
);
185 ConfigurationChanged(m_configuration
);
190 void CCECClient::ResetPhysicalAddress(void)
192 SetPhysicalAddress(m_configuration
);
195 void CCECClient::SetPhysicalAddress(const libcec_configuration
&configuration
)
197 // try to autodetect the address
199 if (m_processor
->IsRunning() && configuration
.bAutodetectAddress
== 1)
200 bPASet
= AutodetectPhysicalAddress();
202 // try to use physical address setting
203 if (!bPASet
&& CLibCEC::IsValidPhysicalAddress(configuration
.iPhysicalAddress
))
204 bPASet
= SetPhysicalAddress(configuration
.iPhysicalAddress
);
206 // use the base device + hdmi port settings
208 bPASet
= SetHDMIPort(configuration
.baseDevice
, configuration
.iHDMIPort
);
210 // reset to defaults if something went wrong
213 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s - resetting HDMI port and base device to defaults", __FUNCTION__
);
214 m_configuration
.baseDevice
= CECDEVICE_UNKNOWN
;
215 m_configuration
.iHDMIPort
= CEC_HDMI_PORTNUMBER_NONE
;
219 bool CCECClient::SetPhysicalAddress(const uint16_t iPhysicalAddress
)
221 // update the configuration
223 CLockObject
lock(m_mutex
);
224 if (m_configuration
.iPhysicalAddress
== iPhysicalAddress
)
226 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "physical address unchanged (%04X)", iPhysicalAddress
);
231 m_configuration
.iPhysicalAddress
= iPhysicalAddress
;
232 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "setting physical address to '%04X'", iPhysicalAddress
);
236 // persist the new configuration
237 if (m_processor
->IsRunning())
238 m_processor
->PersistConfiguration(m_configuration
);
240 // set the physical address for each device
241 SetDevicePhysicalAddress(iPhysicalAddress
);
243 // and send back the updated configuration
244 ConfigurationChanged(m_configuration
);
249 bool CCECClient::AllocateLogicalAddresses(void)
251 // reset all previous LAs that were set
252 m_configuration
.logicalAddresses
.Clear();
254 // display an error if no device types are set
255 if (m_configuration
.deviceTypes
.IsEmpty())
257 LIB_CEC
->AddLog(CEC_LOG_ERROR
, "no device types given");
261 // check each entry of the list
262 for (uint8_t iPtr
= 0; iPtr
< 5; iPtr
++)
264 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
267 // find an LA for this type
268 cec_logical_address
address(CECDEVICE_UNKNOWN
);
269 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
270 address
= AllocateLogicalAddressRecordingDevice();
271 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_TUNER
)
272 address
= AllocateLogicalAddressTuner();
273 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
)
274 address
= AllocateLogicalAddressPlaybackDevice();
275 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
276 address
= AllocateLogicalAddressAudioSystem();
278 // display an error if no LA could be allocated
279 if (address
== CECDEVICE_UNKNOWN
)
281 LIB_CEC
->AddLog(CEC_LOG_ERROR
, "%s - failed to allocate device '%d', type '%s'", __FUNCTION__
, iPtr
, ToString(m_configuration
.deviceTypes
.types
[iPtr
]));
285 // display the registered LA
286 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s - device '%d', type '%s', LA '%X'", __FUNCTION__
, iPtr
, ToString(m_configuration
.deviceTypes
.types
[iPtr
]), address
);
287 m_configuration
.logicalAddresses
.Set(address
);
293 cec_logical_address
CCECClient::AllocateLogicalAddressRecordingDevice(void)
295 cec_logical_address
retVal(CECDEVICE_UNKNOWN
);
297 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'recording device'");
298 if (m_processor
->TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1
))
299 retVal
= CECDEVICE_RECORDINGDEVICE1
;
300 else if (m_processor
->TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2
))
301 retVal
= CECDEVICE_RECORDINGDEVICE2
;
302 else if (m_processor
->TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3
))
303 retVal
= CECDEVICE_RECORDINGDEVICE3
;
308 cec_logical_address
CCECClient::AllocateLogicalAddressTuner(void)
310 cec_logical_address
retVal(CECDEVICE_UNKNOWN
);
312 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'tuner'");
313 if (m_processor
->TryLogicalAddress(CECDEVICE_TUNER1
))
314 retVal
= CECDEVICE_TUNER1
;
315 else if (m_processor
->TryLogicalAddress(CECDEVICE_TUNER2
))
316 retVal
= CECDEVICE_TUNER2
;
317 else if (m_processor
->TryLogicalAddress(CECDEVICE_TUNER3
))
318 retVal
= CECDEVICE_TUNER3
;
319 else if (m_processor
->TryLogicalAddress(CECDEVICE_TUNER4
))
320 retVal
= CECDEVICE_TUNER4
;
325 cec_logical_address
CCECClient::AllocateLogicalAddressPlaybackDevice(void)
327 cec_logical_address
retVal(CECDEVICE_UNKNOWN
);
329 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'playback device'");
330 if (m_processor
->TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1
))
331 retVal
= CECDEVICE_PLAYBACKDEVICE1
;
332 else if (m_processor
->TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2
))
333 retVal
= CECDEVICE_PLAYBACKDEVICE2
;
334 else if (m_processor
->TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3
))
335 retVal
= CECDEVICE_PLAYBACKDEVICE3
;
340 cec_logical_address
CCECClient::AllocateLogicalAddressAudioSystem(void)
342 cec_logical_address
retVal(CECDEVICE_UNKNOWN
);
344 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'audiosystem'");
345 if (m_processor
->TryLogicalAddress(CECDEVICE_AUDIOSYSTEM
))
346 retVal
= CECDEVICE_AUDIOSYSTEM
;
351 CCECBusDevice
*CCECClient::GetDeviceByType(const cec_device_type type
) const
353 // get all devices that match our logical addresses
354 CECDEVICEVEC devices
;
355 m_processor
->GetDevices()->GetByLogicalAddresses(devices
, m_configuration
.logicalAddresses
);
357 // filter the type we need
358 CCECDeviceMap::FilterType(type
, devices
);
360 return devices
.empty() ?
365 bool CCECClient::ChangeDeviceType(const cec_device_type from
, const cec_device_type to
)
367 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "changing device type '%s' into '%s'", ToString(from
), ToString(to
));
369 CLockObject
lock(m_mutex
);
371 // get the previous device that was allocated
372 CCECBusDevice
*previousDevice
= GetDeviceByType(from
);
376 // change the type in the device type list
377 bool bChanged(false);
378 for (uint8_t iPtr
= 0; iPtr
< 5; iPtr
++)
380 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
383 if (m_configuration
.deviceTypes
.types
[iPtr
] == from
)
386 m_configuration
.deviceTypes
.types
[iPtr
] = to
;
388 else if (m_configuration
.deviceTypes
.types
[iPtr
] == to
&& bChanged
)
390 // ensure that dupes are removed
391 m_configuration
.deviceTypes
.types
[iPtr
] = CEC_DEVICE_TYPE_RESERVED
;
395 // re-register the client to set the new ackmask
396 if (!m_processor
->RegisterClient(this))
402 bool CCECClient::SetLogicalAddress(const cec_logical_address iLogicalAddress
)
404 CLockObject
lock(m_mutex
);
405 if (GetPrimaryLogicalAdddress() != iLogicalAddress
)
408 CLockObject
lock(m_mutex
);
409 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< setting primary logical address to %1x", iLogicalAddress
);
410 m_configuration
.logicalAddresses
.primary
= iLogicalAddress
;
411 m_configuration
.logicalAddresses
.Set(iLogicalAddress
);
413 return m_processor
->RegisterClient(this);
419 bool CCECClient::Transmit(const cec_command
&data
)
421 return m_processor
? m_processor
->Transmit(data
) : false;
424 bool CCECClient::SendPowerOnDevices(const cec_logical_address address
/* = CECDEVICE_TV */)
426 // if the broadcast address if set as destination and the client version >=1.5.0, read the wakeDevices setting
427 if (address
== CECDEVICE_BROADCAST
&& m_configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_5_0
)
429 CECDEVICEVEC devices
;
430 m_processor
->GetDevices()->GetWakeDevices(m_configuration
, devices
);
431 return m_processor
->PowerOnDevices(GetPrimaryLogicalAdddress(), devices
);
434 return m_processor
->PowerOnDevice(GetPrimaryLogicalAdddress(), address
);
437 bool CCECClient::SendStandbyDevices(const cec_logical_address address
/* = CECDEVICE_BROADCAST */)
439 // if the broadcast address if set as destination and the client version >=1.5.0, read the standbyDevices setting
440 if (address
== CECDEVICE_BROADCAST
&& m_configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_5_0
)
442 CECDEVICEVEC devices
;
443 m_processor
->GetDevices()->GetPowerOffDevices(m_configuration
, devices
);
444 return m_processor
->StandbyDevices(GetPrimaryLogicalAdddress(), devices
);
447 return m_processor
->StandbyDevice(GetPrimaryLogicalAdddress(), address
);
450 bool CCECClient::SendSetActiveSource(const cec_device_type type
/* = CEC_DEVICE_TYPE_RESERVED */)
452 // get the devices that are controlled by us
453 CECDEVICEVEC devices
;
454 m_processor
->GetDevices()->GetByLogicalAddresses(devices
, m_configuration
.logicalAddresses
);
456 // filter out the device that matches the given type
457 if (type
!= CEC_DEVICE_TYPE_RESERVED
)
458 CCECDeviceMap::FilterType(type
, devices
);
460 // no devices left, re-fetch the list of devices that are controlled by us
462 m_processor
->GetDevices()->GetByLogicalAddresses(devices
, m_configuration
.logicalAddresses
);
464 if (!devices
.empty())
466 // get the first device from the list
467 CCECBusDevice
*device
= *devices
.begin();
470 if (!m_processor
->IsRunning())
471 device
->MarkAsActiveSource();
472 else if (device
->HasValidPhysicalAddress())
473 return device
->ActivateSource();
479 CCECPlaybackDevice
*CCECClient::GetPlaybackDevice(void)
481 CCECPlaybackDevice
*device(NULL
);
482 CECDEVICEVEC devices
;
484 // get the playback devices
485 m_processor
->GetDevices()->GetByLogicalAddresses(devices
, m_configuration
.logicalAddresses
);
486 CCECDeviceMap::FilterType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
, devices
);
488 // no matches, get the recording devices
491 m_processor
->GetDevices()->GetByLogicalAddresses(devices
, m_configuration
.logicalAddresses
);
492 CCECDeviceMap::FilterType(CEC_DEVICE_TYPE_RECORDING_DEVICE
, devices
);
495 // get the first device that matches, and cast it to CCECPlaybackDevice
496 if (!devices
.empty())
497 device
= (*devices
.begin())->AsPlaybackDevice();
502 cec_logical_address
CCECClient::GetPrimaryLogicalAdddress(void)
504 CLockObject
lock(m_mutex
);
505 return m_configuration
.logicalAddresses
.primary
;
508 CCECBusDevice
*CCECClient::GetPrimaryDevice(void)
510 return m_processor
->GetDevice(GetPrimaryLogicalAdddress());
513 bool CCECClient::SendSetDeckControlMode(const cec_deck_control_mode mode
, bool bSendUpdate
/* = true */)
515 // find a playback device that we control
516 CCECPlaybackDevice
*device
= GetPlaybackDevice();
519 // and set the deck control mode if there is a match
520 device
->SetDeckControlMode(mode
);
522 return device
->TransmitDeckStatus(CECDEVICE_TV
);
530 bool CCECClient::SendSetDeckInfo(const cec_deck_info info
, bool bSendUpdate
/* = true */)
532 // find a playback device that we control
533 CCECPlaybackDevice
*device
= GetPlaybackDevice();
536 // and set the deck status if there is a match
537 device
->SetDeckStatus(info
);
539 return device
->AsPlaybackDevice()->TransmitDeckStatus(CECDEVICE_TV
);
547 bool CCECClient::SendSetMenuState(const cec_menu_state state
, bool bSendUpdate
/* = true */)
549 CECDEVICEVEC devices
;
551 // set the menu state for all devices that are controlled by us
552 m_processor
->GetDevices()->GetByLogicalAddresses(devices
, m_configuration
.logicalAddresses
);
553 for (CECDEVICEVEC::iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
555 (*it
)->SetMenuState(state
);
557 (*it
)->TransmitMenuState(CECDEVICE_TV
);
563 bool CCECClient::SendSetInactiveView(void)
565 CECDEVICEVEC devices
;
567 // mark all devices that are controlled by us as inactive source
568 m_processor
->GetDevices()->GetByLogicalAddresses(devices
, m_configuration
.logicalAddresses
);
569 for (CECDEVICEVEC::iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
571 if ((*it
)->IsActiveSource())
573 (*it
)->MarkAsInactiveSource();
574 return (*it
)->TransmitInactiveSource();
581 bool CCECClient::SendSetOSDString(const cec_logical_address iLogicalAddress
, const cec_display_control duration
, const char *strMessage
)
583 CCECBusDevice
*primary
= GetPrimaryDevice();
585 return primary
->TransmitOSDString(iLogicalAddress
, duration
, strMessage
);
590 cec_version
CCECClient::GetDeviceCecVersion(const cec_logical_address iAddress
)
592 CCECBusDevice
*device
= m_processor
->GetDevice(iAddress
);
594 return device
->GetCecVersion(GetPrimaryLogicalAdddress());
595 return CEC_VERSION_UNKNOWN
;
598 bool CCECClient::GetDeviceMenuLanguage(const cec_logical_address iAddress
, cec_menu_language
&language
)
600 CCECBusDevice
*device
= m_processor
->GetDevice(iAddress
);
603 language
= device
->GetMenuLanguage(GetPrimaryLogicalAdddress());
604 return (strcmp(language
.language
, "???") != 0);
609 cec_osd_name
CCECClient::GetDeviceOSDName(const cec_logical_address iAddress
)
612 retVal
.device
= iAddress
;
615 CCECBusDevice
*device
= m_processor
->GetDevice(iAddress
);
618 CStdString strOSDName
= device
->GetOSDName(GetPrimaryLogicalAdddress());
619 snprintf(retVal
.name
, sizeof(retVal
.name
), "%s", strOSDName
.c_str());
620 retVal
.device
= iAddress
;
626 uint16_t CCECClient::GetDevicePhysicalAddress(const cec_logical_address iAddress
)
628 CCECBusDevice
*device
= m_processor
->GetDevice(iAddress
);
630 return device
->GetPhysicalAddress(GetPrimaryLogicalAdddress());
631 return CEC_INVALID_PHYSICAL_ADDRESS
;
634 cec_power_status
CCECClient::GetDevicePowerStatus(const cec_logical_address iAddress
)
636 CCECBusDevice
*device
= m_processor
->GetDevice(iAddress
);
638 return device
->GetPowerStatus(GetPrimaryLogicalAdddress());
639 return CEC_POWER_STATUS_UNKNOWN
;
642 uint64_t CCECClient::GetDeviceVendorId(const cec_logical_address iAddress
)
644 CCECBusDevice
*device
= m_processor
->GetDevice(iAddress
);
646 return device
->GetVendorId(GetPrimaryLogicalAdddress());
647 return CEC_VENDOR_UNKNOWN
;
650 uint8_t CCECClient::SendVolumeUp(bool bSendRelease
/* = true */)
652 CCECBusDevice
*device
= GetPrimaryDevice();
653 CCECAudioSystem
*audio
= m_processor
->GetAudioSystem();
655 return device
&& audio
&& audio
->IsPresent() ?
656 audio
->VolumeUp(device
->GetLogicalAddress(), bSendRelease
) :
657 (uint8_t)CEC_AUDIO_VOLUME_STATUS_UNKNOWN
;
660 uint8_t CCECClient::SendVolumeDown(bool bSendRelease
/* = true */)
662 CCECBusDevice
*device
= GetPrimaryDevice();
663 CCECAudioSystem
*audio
= m_processor
->GetAudioSystem();
665 return device
&& audio
&& audio
->IsPresent() ?
666 audio
->VolumeDown(device
->GetLogicalAddress(), bSendRelease
) :
667 (uint8_t)CEC_AUDIO_VOLUME_STATUS_UNKNOWN
;
670 uint8_t CCECClient::SendMuteAudio(void)
672 CCECBusDevice
*device
= GetPrimaryDevice();
673 CCECAudioSystem
*audio
= m_processor
->GetAudioSystem();
675 return device
&& audio
&& audio
->IsPresent() ?
676 audio
->MuteAudio(device
->GetLogicalAddress()) :
677 (uint8_t)CEC_AUDIO_VOLUME_STATUS_UNKNOWN
;
680 bool CCECClient::SendKeypress(const cec_logical_address iDestination
, const cec_user_control_code key
, bool bWait
/* = true */)
682 CCECBusDevice
*device
= GetPrimaryDevice();
683 CCECBusDevice
*dest
= m_processor
->GetDevice(iDestination
);
685 return device
&& dest
?
686 device
->TransmitKeypress(GetPrimaryLogicalAdddress(), key
, bWait
) :
690 bool CCECClient::SendKeyRelease(const cec_logical_address iDestination
, bool bWait
/* = true */)
692 CCECBusDevice
*device
= GetPrimaryDevice();
693 CCECBusDevice
*dest
= m_processor
->GetDevice(iDestination
);
695 return device
&& dest
?
696 device
->TransmitKeyRelease(GetPrimaryLogicalAdddress(), bWait
) :
700 bool CCECClient::GetCurrentConfiguration(libcec_configuration
&configuration
)
702 CLockObject
lock(m_mutex
);
704 // client version 1.5.0
705 snprintf(configuration
.strDeviceName
, 13, "%s", m_configuration
.strDeviceName
);
706 configuration
.deviceTypes
= m_configuration
.deviceTypes
;
707 configuration
.bAutodetectAddress
= m_configuration
.bAutodetectAddress
;
708 configuration
.iPhysicalAddress
= m_configuration
.iPhysicalAddress
;
709 configuration
.baseDevice
= m_configuration
.baseDevice
;
710 configuration
.iHDMIPort
= m_configuration
.iHDMIPort
;
711 configuration
.clientVersion
= m_configuration
.clientVersion
;
712 configuration
.serverVersion
= m_configuration
.serverVersion
;
713 configuration
.tvVendor
= m_configuration
.tvVendor
;
715 configuration
.bGetSettingsFromROM
= m_configuration
.bGetSettingsFromROM
;
716 configuration
.bUseTVMenuLanguage
= m_configuration
.bUseTVMenuLanguage
;
717 configuration
.bActivateSource
= m_configuration
.bActivateSource
;
718 configuration
.wakeDevices
= m_configuration
.wakeDevices
;
719 configuration
.powerOffDevices
= m_configuration
.powerOffDevices
;
720 configuration
.bPowerOffScreensaver
= m_configuration
.bPowerOffScreensaver
;
721 configuration
.bPowerOffOnStandby
= m_configuration
.bPowerOffOnStandby
;
723 // client version 1.5.1
724 if (configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_5_1
)
725 configuration
.bSendInactiveSource
= m_configuration
.bSendInactiveSource
;
727 // client version 1.5.3
728 if (configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_5_3
)
729 configuration
.logicalAddresses
= m_configuration
.logicalAddresses
;
731 // client version 1.6.0
732 if (configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_6_0
)
734 configuration
.iFirmwareVersion
= m_configuration
.iFirmwareVersion
;
735 configuration
.bPowerOffDevicesOnStandby
= m_configuration
.bPowerOffDevicesOnStandby
;
736 configuration
.bShutdownOnStandby
= m_configuration
.bShutdownOnStandby
;
739 // client version 1.6.2
740 if (configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_6_2
)
742 memcpy(configuration
.strDeviceLanguage
, m_configuration
.strDeviceLanguage
, 3);
743 configuration
.iFirmwareBuildDate
= m_configuration
.iFirmwareBuildDate
;
748 bool CCECClient::SetConfiguration(const libcec_configuration
&configuration
)
750 bool bIsRunning(m_processor
&& m_processor
->IsRunning());
751 CCECBusDevice
*primary
= bIsRunning
? GetPrimaryDevice() : NULL
;
752 uint16_t iPA
= primary
? primary
->GetCurrentPhysicalAddress() : CEC_INVALID_PHYSICAL_ADDRESS
;
754 // update the callbacks
755 if (configuration
.callbacks
)
756 EnableCallbacks(configuration
.callbackParam
, configuration
.callbacks
);
758 // update the client version
759 SetClientVersion((cec_client_version
)configuration
.clientVersion
);
761 // update the OSD name
762 CStdString
strOSDName(configuration
.strDeviceName
);
763 SetOSDName(strOSDName
);
765 // update the TV vendor override
766 SetTVVendorOverride((cec_vendor_id
)configuration
.tvVendor
);
770 CLockObject
lock(m_mutex
);
771 m_configuration
.bUseTVMenuLanguage
= configuration
.bUseTVMenuLanguage
;
772 m_configuration
.bActivateSource
= configuration
.bActivateSource
;
773 m_configuration
.bGetSettingsFromROM
= configuration
.bGetSettingsFromROM
;
774 m_configuration
.wakeDevices
= configuration
.wakeDevices
;
775 m_configuration
.powerOffDevices
= configuration
.powerOffDevices
;
776 m_configuration
.bPowerOffScreensaver
= configuration
.bPowerOffScreensaver
;
777 m_configuration
.bPowerOffOnStandby
= configuration
.bPowerOffOnStandby
;
779 // client version 1.5.1
780 if (configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_5_1
)
781 m_configuration
.bSendInactiveSource
= configuration
.bSendInactiveSource
;
783 // client version 1.6.0
784 if (configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_6_0
)
786 m_configuration
.bPowerOffDevicesOnStandby
= configuration
.bPowerOffDevicesOnStandby
;
787 m_configuration
.bShutdownOnStandby
= configuration
.bShutdownOnStandby
;
790 // client version 1.6.2
791 if (configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_6_2
)
793 memcpy(m_configuration
.strDeviceLanguage
, configuration
.strDeviceLanguage
, 3);
796 // ensure that there is at least 1 device type set
797 if (m_configuration
.deviceTypes
.IsEmpty())
798 m_configuration
.deviceTypes
.Add(CEC_DEVICE_TYPE_RECORDING_DEVICE
);
801 bool bNeedReinit(false);
804 if (SetDeviceTypes(configuration
.deviceTypes
))
806 // the device type changed. just copy the rest, and re-register
808 CLockObject
lock(m_mutex
);
809 m_configuration
.iPhysicalAddress
= configuration
.iPhysicalAddress
;
810 m_configuration
.baseDevice
= configuration
.baseDevice
;
811 m_configuration
.iHDMIPort
= configuration
.iHDMIPort
;
817 // set the physical address
818 SetPhysicalAddress(configuration
);
822 m_processor
->PersistConfiguration(m_configuration
);
825 primary
= GetPrimaryDevice();
827 if (bNeedReinit
|| !primary
|| primary
->GetCurrentPhysicalAddress() != iPA
)
829 // PA or device type changed
830 m_processor
->RegisterClient(this);
832 else if (primary
&& configuration
.bActivateSource
== 1 && bIsRunning
&& !primary
->IsActiveSource())
834 // activate the source if we're not already the active source
835 primary
->ActivateSource();
841 void CCECClient::AddCommand(const cec_command
&command
)
843 CLockObject
lock(m_mutex
);
845 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, ">> %s (%X) -> %s (%X): %s (%2X)", ToString(command
.initiator
), command
.initiator
, ToString(command
.destination
), command
.destination
, ToString(command
.opcode
), command
.opcode
);
847 if (m_configuration
.callbacks
&& m_configuration
.callbacks
->CBCecCommand
)
848 m_configuration
.callbacks
->CBCecCommand(m_configuration
.callbackParam
, command
);
849 else if (!m_commandBuffer
.Push(command
))
850 LIB_CEC
->AddLog(CEC_LOG_WARNING
, "command buffer is full");
853 int CCECClient::MenuStateChanged(const cec_menu_state newState
)
855 CLockObject
lock(m_mutex
);
857 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, ">> %s: %s", ToString(CEC_OPCODE_MENU_REQUEST
), ToString(newState
));
859 if (m_configuration
.callbacks
&&
860 m_configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_6_2
&&
861 m_configuration
.callbacks
->CBCecMenuStateChanged
)
862 return m_configuration
.callbacks
->CBCecMenuStateChanged(m_configuration
.callbackParam
, newState
);
867 void CCECClient::Alert(const libcec_alert type
, const libcec_parameter
¶m
)
869 CLockObject
lock(m_mutex
);
871 if (m_configuration
.callbacks
&&
872 m_configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_6_0
&&
873 m_configuration
.callbacks
->CBCecAlert
)
874 m_configuration
.callbacks
->CBCecAlert(m_configuration
.callbackParam
, type
, param
);
877 void CCECClient::AddLog(const cec_log_message
&message
)
879 CLockObject
lock(m_logMutex
);
880 if (m_configuration
.callbacks
&& m_configuration
.callbacks
->CBCecLogMessage
)
881 m_configuration
.callbacks
->CBCecLogMessage(m_configuration
.callbackParam
, message
);
883 m_logBuffer
.Push(message
);
886 void CCECClient::AddKey(void)
888 CLockObject
lock(m_mutex
);
890 if (m_iCurrentButton
!= CEC_USER_CONTROL_CODE_UNKNOWN
)
894 key
.duration
= (unsigned int) (GetTimeMs() - m_buttontime
);
895 key
.keycode
= m_iCurrentButton
;
896 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "key released: %1x", key
.keycode
);
898 if (m_configuration
.callbacks
&& m_configuration
.callbacks
->CBCecKeyPress
)
899 m_configuration
.callbacks
->CBCecKeyPress(m_configuration
.callbackParam
, key
);
901 m_keyBuffer
.Push(key
);
902 m_iCurrentButton
= CEC_USER_CONTROL_CODE_UNKNOWN
;
908 void CCECClient::AddKey(const cec_keypress
&key
)
910 CLockObject
lock(m_mutex
);
912 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "key pressed: %1x", key
.keycode
);
914 if (m_configuration
.callbacks
&& m_configuration
.callbacks
->CBCecKeyPress
)
915 m_configuration
.callbacks
->CBCecKeyPress(m_configuration
.callbackParam
, key
);
917 m_keyBuffer
.Push(key
);
919 m_iCurrentButton
= key
.duration
> 0 ? CEC_USER_CONTROL_CODE_UNKNOWN
: key
.keycode
;
920 m_buttontime
= key
.duration
> 0 ? 0 : GetTimeMs();
923 void CCECClient::SetCurrentButton(const cec_user_control_code iButtonCode
)
925 // push a keypress to the buffer with 0 duration and another with the duration set when released
928 key
.keycode
= iButtonCode
;
933 void CCECClient::CheckKeypressTimeout(void)
935 if (m_iCurrentButton
!= CEC_USER_CONTROL_CODE_UNKNOWN
&& GetTimeMs() - m_buttontime
> CEC_BUTTON_TIMEOUT
)
938 m_iCurrentButton
= CEC_USER_CONTROL_CODE_UNKNOWN
;
942 void CCECClient::ConfigurationChanged(const libcec_configuration
&config
)
944 CLockObject
lock(m_mutex
);
946 if (m_configuration
.callbacks
&&
947 m_configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_5_0
&&
948 m_configuration
.callbacks
->CBCecConfigurationChanged
&&
949 m_processor
->CECInitialised())
950 m_configuration
.callbacks
->CBCecConfigurationChanged(m_configuration
.callbackParam
, config
);
953 bool CCECClient::EnableCallbacks(void *cbParam
, ICECCallbacks
*callbacks
)
955 CLockObject
lock(m_mutex
);
956 m_configuration
.callbackParam
= cbParam
;
957 m_configuration
.callbacks
= callbacks
;
961 bool CCECClient::PingAdapter(void)
963 return m_processor
? m_processor
->PingAdapter() : false;
966 bool CCECClient::GetNextLogMessage(cec_log_message
*message
)
968 return (m_logBuffer
.Pop(*message
));
971 bool CCECClient::GetNextKeypress(cec_keypress
*key
)
973 return m_keyBuffer
.Pop(*key
);
976 bool CCECClient::GetNextCommand(cec_command
*command
)
978 return m_commandBuffer
.Pop(*command
);
981 CStdString
CCECClient::GetConnectionInfo(void)
984 strLog
.Format("libCEC version = %s, client version = %s, firmware version = %d", ToString((cec_server_version
)m_configuration
.serverVersion
), ToString((cec_client_version
)m_configuration
.clientVersion
), m_configuration
.iFirmwareVersion
);
985 if (m_configuration
.iFirmwareBuildDate
!= CEC_FW_BUILD_UNKNOWN
)
987 time_t buildTime
= (time_t)m_configuration
.iFirmwareBuildDate
;
988 strLog
.AppendFormat(", firmware build date: %s", asctime(gmtime(&buildTime
)));
989 strLog
= strLog
.Left((int)strLog
.length() - 1); // strip \n added by asctime
990 strLog
.append(" +0000");
993 // log the addresses that are being used
994 if (!m_configuration
.logicalAddresses
.IsEmpty())
996 strLog
.append(", logical address(es) = ");
997 CECDEVICEVEC devices
;
998 m_processor
->GetDevices()->GetByLogicalAddresses(devices
, m_configuration
.logicalAddresses
);
999 for (CECDEVICEVEC::iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
1000 strLog
.AppendFormat("%s (%X) ", (*it
)->GetLogicalAddressName(), (*it
)->GetLogicalAddress());
1003 if (!CLibCEC::IsValidPhysicalAddress(m_configuration
.iPhysicalAddress
))
1004 strLog
.AppendFormat(", base device: %s (%X), HDMI port number: %d", ToString(m_configuration
.baseDevice
), m_configuration
.baseDevice
, m_configuration
.iHDMIPort
);
1006 strLog
.AppendFormat(", physical address: %04x", m_configuration
.iPhysicalAddress
);
1011 void CCECClient::SetTVVendorOverride(const cec_vendor_id id
)
1014 CLockObject
lock(m_mutex
);
1015 m_configuration
.tvVendor
= id
;
1018 if (id
!= CEC_VENDOR_UNKNOWN
)
1020 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s - vendor id '%s'", __FUNCTION__
, ToString(id
));
1022 CCECBusDevice
*tv
= m_processor
? m_processor
->GetTV() : NULL
;
1024 tv
->SetVendorId((uint64_t)id
);
1028 cec_vendor_id
CCECClient::GetTVVendorOverride(void)
1030 CLockObject
lock(m_mutex
);
1031 return (cec_vendor_id
)m_configuration
.tvVendor
;
1034 void CCECClient::SetOSDName(const CStdString
&strDeviceName
)
1037 CLockObject
lock(m_mutex
);
1038 snprintf(m_configuration
.strDeviceName
, 13, "%s", strDeviceName
.c_str());
1041 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s - using OSD name '%s'", __FUNCTION__
, strDeviceName
.c_str());
1043 CCECBusDevice
*primary
= GetPrimaryDevice();
1044 if (primary
&& !primary
->GetCurrentOSDName().Equals(strDeviceName
))
1046 primary
->SetOSDName(strDeviceName
);
1047 if (m_processor
&& m_processor
->IsRunning())
1048 primary
->TransmitOSDName(CECDEVICE_TV
);
1052 CStdString
CCECClient::GetOSDName(void)
1054 CLockObject
lock(m_mutex
);
1055 CStdString
strOSDName(m_configuration
.strDeviceName
);
1059 void CCECClient::SetWakeDevices(const cec_logical_addresses
&addresses
)
1061 CLockObject
lock(m_mutex
);
1062 m_configuration
.wakeDevices
= addresses
;
1065 cec_logical_addresses
CCECClient::GetWakeDevices(void)
1067 CLockObject
lock(m_mutex
);
1068 return m_configuration
.wakeDevices
;
1071 bool CCECClient::AutodetectPhysicalAddress(void)
1073 bool bPhysicalAutodetected(false);
1074 uint16_t iPhysicalAddress
= m_processor
? m_processor
->GetDetectedPhysicalAddress() : CEC_INVALID_PHYSICAL_ADDRESS
;
1076 if (CLibCEC::IsValidPhysicalAddress(iPhysicalAddress
))
1078 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s - autodetected physical address '%04X'", __FUNCTION__
, iPhysicalAddress
);
1080 CLockObject
lock(m_mutex
);
1081 m_configuration
.iPhysicalAddress
= iPhysicalAddress
;
1082 m_configuration
.iHDMIPort
= CEC_HDMI_PORTNUMBER_NONE
;
1083 m_configuration
.baseDevice
= CECDEVICE_UNKNOWN
;
1084 bPhysicalAutodetected
= true;
1087 SetDevicePhysicalAddress(iPhysicalAddress
);
1089 return bPhysicalAutodetected
;
1092 void CCECClient::SetClientVersion(const cec_client_version version
)
1094 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s - using client version '%s'", __FUNCTION__
, ToString(version
));
1096 CLockObject
lock(m_mutex
);
1097 m_configuration
.clientVersion
= (uint32_t)version
;
1100 cec_client_version
CCECClient::GetClientVersion(void)
1102 CLockObject
lock(m_mutex
);
1103 return (cec_client_version
)m_configuration
.clientVersion
;
1106 bool CCECClient::SetDeviceTypes(const cec_device_type_list
&deviceTypes
)
1108 bool bNeedReinit(false);
1111 CLockObject
lock(m_mutex
);
1112 bNeedReinit
= m_processor
&& m_processor
->IsRunning() &&
1113 (m_configuration
.deviceTypes
!= deviceTypes
);
1114 m_configuration
.deviceTypes
= deviceTypes
;
1118 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s - using primary device type '%s'", __FUNCTION__
, ToString(deviceTypes
[0]));
1123 cec_device_type_list
CCECClient::GetDeviceTypes(void)
1125 cec_device_type_list retVal
;
1126 CLockObject
lock(m_mutex
);
1127 retVal
= m_configuration
.deviceTypes
;
1131 bool CCECClient::SetDevicePhysicalAddress(const uint16_t iPhysicalAddress
)
1133 if (!CLibCEC::IsValidPhysicalAddress(iPhysicalAddress
))
1136 // reconfigure all devices
1137 cec_logical_address
reactivateSource(CECDEVICE_UNKNOWN
);
1138 CECDEVICEVEC devices
;
1139 m_processor
->GetDevices()->GetByLogicalAddresses(devices
, m_configuration
.logicalAddresses
);
1140 for (CECDEVICEVEC::iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
1142 // if this device was the active source, reactivate it afterwards
1143 if ((*it
)->IsActiveSource())
1144 reactivateSource
= (*it
)->GetLogicalAddress();
1146 // mark the device as inactive source
1147 if (IsInitialised())
1148 (*it
)->MarkAsInactiveSource();
1150 // set the new physical address
1151 (*it
)->SetPhysicalAddress(iPhysicalAddress
);
1154 if (IsInitialised())
1155 (*it
)->TransmitPhysicalAddress();
1158 // reactivate the previous active source
1159 if (reactivateSource
!= CECDEVICE_UNKNOWN
&&
1160 m_processor
->IsRunning() &&
1163 CCECBusDevice
*device
= m_processor
->GetDevice(reactivateSource
);
1165 device
->ActivateSource();
1171 bool CCECClient::SwitchMonitoring(bool bEnable
)
1173 return m_processor
? m_processor
->SwitchMonitoring(bEnable
) : false;
1176 bool CCECClient::PollDevice(const cec_logical_address iAddress
)
1178 // try to find the primary device
1179 CCECBusDevice
*primary
= GetPrimaryDevice();
1180 // poll the destination, with the primary as source
1182 return primary
->TransmitPoll(iAddress
);
1184 return m_processor
? m_processor
->PollDevice(iAddress
) : false;
1187 cec_logical_addresses
CCECClient::GetActiveDevices(void)
1189 CECDEVICEVEC activeDevices
;
1191 m_processor
->GetDevices()->GetActive(activeDevices
);
1192 return CCECDeviceMap::ToLogicalAddresses(activeDevices
);
1195 bool CCECClient::IsActiveDevice(const cec_logical_address iAddress
)
1197 cec_logical_addresses activeDevices
= GetActiveDevices();
1198 return activeDevices
.IsSet(iAddress
);
1201 bool CCECClient::IsActiveDeviceType(const cec_device_type type
)
1203 CECDEVICEVEC activeDevices
;
1205 m_processor
->GetDevices()->GetActive(activeDevices
);
1206 CCECDeviceMap::FilterType(type
, activeDevices
);
1207 return !activeDevices
.empty();
1210 cec_logical_address
CCECClient::GetActiveSource(void)
1212 return m_processor
? m_processor
->GetActiveSource() : CECDEVICE_UNKNOWN
;
1215 bool CCECClient::IsActiveSource(const cec_logical_address iAddress
)
1217 return m_processor
? m_processor
->IsActiveSource(iAddress
) : false;
1220 bool CCECClient::SetStreamPath(const cec_logical_address iAddress
)
1222 uint16_t iPhysicalAddress
= GetDevicePhysicalAddress(iAddress
);
1223 if (iPhysicalAddress
!= CEC_INVALID_PHYSICAL_ADDRESS
)
1224 return SetStreamPath(iPhysicalAddress
);
1228 bool CCECClient::SetStreamPath(const uint16_t iPhysicalAddress
)
1230 return m_processor
? m_processor
->SetStreamPath(iPhysicalAddress
) : false;
1233 cec_logical_addresses
CCECClient::GetLogicalAddresses(void)
1235 cec_logical_addresses addresses
;
1236 CLockObject
lock(m_mutex
);
1237 addresses
= m_configuration
.logicalAddresses
;
1241 bool CCECClient::CanPersistConfiguration(void)
1243 return m_processor
? m_processor
->CanPersistConfiguration() : false;
1246 bool CCECClient::PersistConfiguration(const libcec_configuration
&configuration
)
1248 return m_processor
? m_processor
->PersistConfiguration(configuration
) : false;
1251 void CCECClient::RescanActiveDevices(void)
1254 m_processor
->RescanActiveDevices();
1257 bool CCECClient::IsLibCECActiveSource(void)
1259 bool bReturn(false);
1262 cec_logical_address activeSource
= m_processor
->GetActiveSource();
1263 CCECBusDevice
*device
= m_processor
->GetDevice(activeSource
);
1265 bReturn
= device
->IsHandledByLibCEC();