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
->CECInitialised() && !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
->CECInitialised() && 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 m_processor
->PersistConfiguration(m_configuration
);
239 // set the physical address for each device
240 SetDevicePhysicalAddress(iPhysicalAddress
);
242 // and send back the updated configuration
243 ConfigurationChanged(m_configuration
);
248 bool CCECClient::AllocateLogicalAddresses(void)
250 // reset all previous LAs that were set
251 m_configuration
.logicalAddresses
.Clear();
253 // display an error if no device types are set
254 if (m_configuration
.deviceTypes
.IsEmpty())
256 LIB_CEC
->AddLog(CEC_LOG_ERROR
, "no device types given");
260 // check each entry of the list
261 for (uint8_t iPtr
= 0; iPtr
< 5; iPtr
++)
263 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
266 // find an LA for this type
267 cec_logical_address
address(CECDEVICE_UNKNOWN
);
268 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
269 address
= AllocateLogicalAddressRecordingDevice();
270 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_TUNER
)
271 address
= AllocateLogicalAddressTuner();
272 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
)
273 address
= AllocateLogicalAddressPlaybackDevice();
274 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
275 address
= AllocateLogicalAddressAudioSystem();
277 // display an error if no LA could be allocated
278 if (address
== CECDEVICE_UNKNOWN
)
280 LIB_CEC
->AddLog(CEC_LOG_ERROR
, "%s - failed to allocate device '%d', type '%s'", __FUNCTION__
, iPtr
, ToString(m_configuration
.deviceTypes
.types
[iPtr
]));
284 // display the registered LA
285 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s - device '%d', type '%s', LA '%X'", __FUNCTION__
, iPtr
, ToString(m_configuration
.deviceTypes
.types
[iPtr
]), address
);
286 m_configuration
.logicalAddresses
.Set(address
);
292 cec_logical_address
CCECClient::AllocateLogicalAddressRecordingDevice(void)
294 cec_logical_address
retVal(CECDEVICE_UNKNOWN
);
296 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'recording device'");
297 if (m_processor
->TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1
))
298 retVal
= CECDEVICE_RECORDINGDEVICE1
;
299 else if (m_processor
->TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2
))
300 retVal
= CECDEVICE_RECORDINGDEVICE2
;
301 else if (m_processor
->TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3
))
302 retVal
= CECDEVICE_RECORDINGDEVICE3
;
307 cec_logical_address
CCECClient::AllocateLogicalAddressTuner(void)
309 cec_logical_address
retVal(CECDEVICE_UNKNOWN
);
311 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'tuner'");
312 if (m_processor
->TryLogicalAddress(CECDEVICE_TUNER1
))
313 retVal
= CECDEVICE_TUNER1
;
314 else if (m_processor
->TryLogicalAddress(CECDEVICE_TUNER2
))
315 retVal
= CECDEVICE_TUNER2
;
316 else if (m_processor
->TryLogicalAddress(CECDEVICE_TUNER3
))
317 retVal
= CECDEVICE_TUNER3
;
318 else if (m_processor
->TryLogicalAddress(CECDEVICE_TUNER4
))
319 retVal
= CECDEVICE_TUNER4
;
324 cec_logical_address
CCECClient::AllocateLogicalAddressPlaybackDevice(void)
326 cec_logical_address
retVal(CECDEVICE_UNKNOWN
);
328 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'playback device'");
329 if (m_processor
->TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1
))
330 retVal
= CECDEVICE_PLAYBACKDEVICE1
;
331 else if (m_processor
->TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2
))
332 retVal
= CECDEVICE_PLAYBACKDEVICE2
;
333 else if (m_processor
->TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3
))
334 retVal
= CECDEVICE_PLAYBACKDEVICE3
;
339 cec_logical_address
CCECClient::AllocateLogicalAddressAudioSystem(void)
341 cec_logical_address
retVal(CECDEVICE_UNKNOWN
);
343 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'audiosystem'");
344 if (m_processor
->TryLogicalAddress(CECDEVICE_AUDIOSYSTEM
))
345 retVal
= CECDEVICE_AUDIOSYSTEM
;
350 CCECBusDevice
*CCECClient::GetDeviceByType(const cec_device_type type
) const
352 // get all devices that match our logical addresses
353 CECDEVICEVEC devices
;
354 m_processor
->GetDevices()->GetByLogicalAddresses(devices
, m_configuration
.logicalAddresses
);
356 // filter the type we need
357 CCECDeviceMap::FilterType(type
, devices
);
359 return devices
.empty() ?
364 bool CCECClient::ChangeDeviceType(const cec_device_type from
, const cec_device_type to
)
366 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "changing device type '%s' into '%s'", ToString(from
), ToString(to
));
368 CLockObject
lock(m_mutex
);
370 // get the previous device that was allocated
371 CCECBusDevice
*previousDevice
= GetDeviceByType(from
);
375 // change the type in the device type list
376 bool bChanged(false);
377 for (uint8_t iPtr
= 0; iPtr
< 5; iPtr
++)
379 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
382 if (m_configuration
.deviceTypes
.types
[iPtr
] == from
)
385 m_configuration
.deviceTypes
.types
[iPtr
] = to
;
387 else if (m_configuration
.deviceTypes
.types
[iPtr
] == to
&& bChanged
)
389 // ensure that dupes are removed
390 m_configuration
.deviceTypes
.types
[iPtr
] = CEC_DEVICE_TYPE_RESERVED
;
394 // re-register the client to set the new ackmask
395 if (!m_processor
->RegisterClient(this))
401 bool CCECClient::SetLogicalAddress(const cec_logical_address iLogicalAddress
)
403 CLockObject
lock(m_mutex
);
404 if (GetPrimaryLogicalAdddress() != iLogicalAddress
)
407 CLockObject
lock(m_mutex
);
408 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< setting primary logical address to %1x", iLogicalAddress
);
409 m_configuration
.logicalAddresses
.primary
= iLogicalAddress
;
410 m_configuration
.logicalAddresses
.Set(iLogicalAddress
);
412 return m_processor
->RegisterClient(this);
418 bool CCECClient::Transmit(const cec_command
&data
)
420 return m_processor
? m_processor
->Transmit(data
) : false;
423 bool CCECClient::SendPowerOnDevices(const cec_logical_address address
/* = CECDEVICE_TV */)
425 // if the broadcast address if set as destination and the client version >=1.5.0, read the wakeDevices setting
426 if (address
== CECDEVICE_BROADCAST
&& m_configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_5_0
)
428 CECDEVICEVEC devices
;
429 m_processor
->GetDevices()->GetWakeDevices(m_configuration
, devices
);
430 return m_processor
->PowerOnDevices(GetPrimaryLogicalAdddress(), devices
);
433 return m_processor
->PowerOnDevice(GetPrimaryLogicalAdddress(), address
);
436 bool CCECClient::SendStandbyDevices(const cec_logical_address address
/* = CECDEVICE_BROADCAST */)
438 // if the broadcast address if set as destination and the client version >=1.5.0, read the standbyDevices setting
439 if (address
== CECDEVICE_BROADCAST
&& m_configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_5_0
)
441 CECDEVICEVEC devices
;
442 m_processor
->GetDevices()->GetPowerOffDevices(m_configuration
, devices
);
443 return m_processor
->StandbyDevices(GetPrimaryLogicalAdddress(), devices
);
446 return m_processor
->StandbyDevice(GetPrimaryLogicalAdddress(), address
);
449 bool CCECClient::SendSetActiveSource(const cec_device_type type
/* = CEC_DEVICE_TYPE_RESERVED */)
451 // get the devices that are controlled by us
452 CECDEVICEVEC devices
;
453 m_processor
->GetDevices()->GetByLogicalAddresses(devices
, m_configuration
.logicalAddresses
);
455 // filter out the device that matches the given type
456 if (type
!= CEC_DEVICE_TYPE_RESERVED
)
457 CCECDeviceMap::FilterType(type
, devices
);
459 // no devices left, re-fetch the list of devices that are controlled by us
461 m_processor
->GetDevices()->GetByLogicalAddresses(devices
, m_configuration
.logicalAddresses
);
463 if (!devices
.empty())
465 // get the first device from the list
466 CCECBusDevice
*device
= *devices
.begin();
469 if (!m_processor
->CECInitialised())
470 device
->MarkAsActiveSource();
471 else if (device
->HasValidPhysicalAddress())
472 return device
->ActivateSource();
478 CCECPlaybackDevice
*CCECClient::GetPlaybackDevice(void)
480 CCECPlaybackDevice
*device(NULL
);
481 CECDEVICEVEC devices
;
483 // get the playback devices
484 m_processor
->GetDevices()->GetByLogicalAddresses(devices
, m_configuration
.logicalAddresses
);
485 CCECDeviceMap::FilterType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
, devices
);
487 // no matches, get the recording devices
490 m_processor
->GetDevices()->GetByLogicalAddresses(devices
, m_configuration
.logicalAddresses
);
491 CCECDeviceMap::FilterType(CEC_DEVICE_TYPE_RECORDING_DEVICE
, devices
);
494 // get the first device that matches, and cast it to CCECPlaybackDevice
495 if (!devices
.empty())
496 device
= (*devices
.begin())->AsPlaybackDevice();
501 cec_logical_address
CCECClient::GetPrimaryLogicalAdddress(void)
503 CLockObject
lock(m_mutex
);
504 return m_configuration
.logicalAddresses
.primary
;
507 CCECBusDevice
*CCECClient::GetPrimaryDevice(void)
509 return m_processor
->GetDevice(GetPrimaryLogicalAdddress());
512 bool CCECClient::SendSetDeckControlMode(const cec_deck_control_mode mode
, bool bSendUpdate
/* = true */)
514 // find a playback device that we control
515 CCECPlaybackDevice
*device
= GetPlaybackDevice();
518 // and set the deck control mode if there is a match
519 device
->SetDeckControlMode(mode
);
521 return device
->TransmitDeckStatus(CECDEVICE_TV
);
529 bool CCECClient::SendSetDeckInfo(const cec_deck_info info
, bool bSendUpdate
/* = true */)
531 // find a playback device that we control
532 CCECPlaybackDevice
*device
= GetPlaybackDevice();
535 // and set the deck status if there is a match
536 device
->SetDeckStatus(info
);
538 return device
->AsPlaybackDevice()->TransmitDeckStatus(CECDEVICE_TV
);
546 bool CCECClient::SendSetMenuState(const cec_menu_state state
, bool bSendUpdate
/* = true */)
548 CECDEVICEVEC devices
;
550 // set the menu state for all devices that are controlled by us
551 m_processor
->GetDevices()->GetByLogicalAddresses(devices
, m_configuration
.logicalAddresses
);
552 for (CECDEVICEVEC::iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
554 (*it
)->SetMenuState(state
);
556 (*it
)->TransmitMenuState(CECDEVICE_TV
);
562 bool CCECClient::SendSetInactiveView(void)
564 CECDEVICEVEC devices
;
566 // mark all devices that are controlled by us as inactive source
567 m_processor
->GetDevices()->GetByLogicalAddresses(devices
, m_configuration
.logicalAddresses
);
568 for (CECDEVICEVEC::iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
570 if ((*it
)->IsActiveSource())
572 (*it
)->MarkAsInactiveSource();
573 return (*it
)->TransmitInactiveSource();
580 bool CCECClient::SendSetOSDString(const cec_logical_address iLogicalAddress
, const cec_display_control duration
, const char *strMessage
)
582 CCECBusDevice
*primary
= GetPrimaryDevice();
584 return primary
->TransmitOSDString(iLogicalAddress
, duration
, strMessage
);
589 cec_version
CCECClient::GetDeviceCecVersion(const cec_logical_address iAddress
)
591 CCECBusDevice
*device
= m_processor
->GetDevice(iAddress
);
593 return device
->GetCecVersion(GetPrimaryLogicalAdddress());
594 return CEC_VERSION_UNKNOWN
;
597 bool CCECClient::GetDeviceMenuLanguage(const cec_logical_address iAddress
, cec_menu_language
&language
)
599 CCECBusDevice
*device
= m_processor
->GetDevice(iAddress
);
602 language
= device
->GetMenuLanguage(GetPrimaryLogicalAdddress());
603 return (strcmp(language
.language
, "???") != 0);
608 cec_osd_name
CCECClient::GetDeviceOSDName(const cec_logical_address iAddress
)
611 retVal
.device
= iAddress
;
614 CCECBusDevice
*device
= m_processor
->GetDevice(iAddress
);
617 CStdString strOSDName
= device
->GetOSDName(GetPrimaryLogicalAdddress());
618 snprintf(retVal
.name
, sizeof(retVal
.name
), "%s", strOSDName
.c_str());
619 retVal
.device
= iAddress
;
625 uint16_t CCECClient::GetDevicePhysicalAddress(const cec_logical_address iAddress
)
627 CCECBusDevice
*device
= m_processor
->GetDevice(iAddress
);
629 return device
->GetPhysicalAddress(GetPrimaryLogicalAdddress());
630 return CEC_INVALID_PHYSICAL_ADDRESS
;
633 cec_power_status
CCECClient::GetDevicePowerStatus(const cec_logical_address iAddress
)
635 CCECBusDevice
*device
= m_processor
->GetDevice(iAddress
);
637 return device
->GetPowerStatus(GetPrimaryLogicalAdddress());
638 return CEC_POWER_STATUS_UNKNOWN
;
641 uint64_t CCECClient::GetDeviceVendorId(const cec_logical_address iAddress
)
643 CCECBusDevice
*device
= m_processor
->GetDevice(iAddress
);
645 return device
->GetVendorId(GetPrimaryLogicalAdddress());
646 return CEC_VENDOR_UNKNOWN
;
649 uint8_t CCECClient::SendVolumeUp(bool bSendRelease
/* = true */)
651 CCECBusDevice
*device
= GetPrimaryDevice();
652 CCECAudioSystem
*audio
= m_processor
->GetAudioSystem();
654 return device
&& audio
&& audio
->IsPresent() ?
655 audio
->VolumeUp(device
->GetLogicalAddress(), bSendRelease
) :
656 (uint8_t)CEC_AUDIO_VOLUME_STATUS_UNKNOWN
;
659 uint8_t CCECClient::SendVolumeDown(bool bSendRelease
/* = true */)
661 CCECBusDevice
*device
= GetPrimaryDevice();
662 CCECAudioSystem
*audio
= m_processor
->GetAudioSystem();
664 return device
&& audio
&& audio
->IsPresent() ?
665 audio
->VolumeDown(device
->GetLogicalAddress(), bSendRelease
) :
666 (uint8_t)CEC_AUDIO_VOLUME_STATUS_UNKNOWN
;
669 uint8_t CCECClient::SendMuteAudio(void)
671 CCECBusDevice
*device
= GetPrimaryDevice();
672 CCECAudioSystem
*audio
= m_processor
->GetAudioSystem();
674 return device
&& audio
&& audio
->IsPresent() ?
675 audio
->MuteAudio(device
->GetLogicalAddress()) :
676 (uint8_t)CEC_AUDIO_VOLUME_STATUS_UNKNOWN
;
679 bool CCECClient::SendKeypress(const cec_logical_address iDestination
, const cec_user_control_code key
, bool bWait
/* = true */)
681 CCECBusDevice
*device
= GetPrimaryDevice();
682 CCECBusDevice
*dest
= m_processor
->GetDevice(iDestination
);
684 return device
&& dest
?
685 device
->TransmitKeypress(GetPrimaryLogicalAdddress(), key
, bWait
) :
689 bool CCECClient::SendKeyRelease(const cec_logical_address iDestination
, bool bWait
/* = true */)
691 CCECBusDevice
*device
= GetPrimaryDevice();
692 CCECBusDevice
*dest
= m_processor
->GetDevice(iDestination
);
694 return device
&& dest
?
695 device
->TransmitKeyRelease(GetPrimaryLogicalAdddress(), bWait
) :
699 bool CCECClient::GetCurrentConfiguration(libcec_configuration
&configuration
)
701 CLockObject
lock(m_mutex
);
703 // client version 1.5.0
704 snprintf(configuration
.strDeviceName
, 13, "%s", m_configuration
.strDeviceName
);
705 configuration
.deviceTypes
= m_configuration
.deviceTypes
;
706 configuration
.bAutodetectAddress
= m_configuration
.bAutodetectAddress
;
707 configuration
.iPhysicalAddress
= m_configuration
.iPhysicalAddress
;
708 configuration
.baseDevice
= m_configuration
.baseDevice
;
709 configuration
.iHDMIPort
= m_configuration
.iHDMIPort
;
710 configuration
.clientVersion
= m_configuration
.clientVersion
;
711 configuration
.serverVersion
= m_configuration
.serverVersion
;
712 configuration
.tvVendor
= m_configuration
.tvVendor
;
714 configuration
.bGetSettingsFromROM
= m_configuration
.bGetSettingsFromROM
;
715 configuration
.bUseTVMenuLanguage
= m_configuration
.bUseTVMenuLanguage
;
716 configuration
.bActivateSource
= m_configuration
.bActivateSource
;
717 configuration
.wakeDevices
= m_configuration
.wakeDevices
;
718 configuration
.powerOffDevices
= m_configuration
.powerOffDevices
;
719 configuration
.bPowerOffScreensaver
= m_configuration
.bPowerOffScreensaver
;
720 configuration
.bPowerOffOnStandby
= m_configuration
.bPowerOffOnStandby
;
722 // client version 1.5.1
723 if (configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_5_1
)
724 configuration
.bSendInactiveSource
= m_configuration
.bSendInactiveSource
;
726 // client version 1.5.3
727 if (configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_5_3
)
728 configuration
.logicalAddresses
= m_configuration
.logicalAddresses
;
730 // client version 1.6.0
731 if (configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_6_0
)
733 configuration
.iFirmwareVersion
= m_configuration
.iFirmwareVersion
;
734 configuration
.bPowerOffDevicesOnStandby
= m_configuration
.bPowerOffDevicesOnStandby
;
735 configuration
.bShutdownOnStandby
= m_configuration
.bShutdownOnStandby
;
738 // client version 1.6.2
739 if (configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_6_2
)
741 memcpy(configuration
.strDeviceLanguage
, m_configuration
.strDeviceLanguage
, 3);
742 configuration
.iFirmwareBuildDate
= m_configuration
.iFirmwareBuildDate
;
747 bool CCECClient::SetConfiguration(const libcec_configuration
&configuration
)
749 bool bIsRunning(m_processor
&& m_processor
->CECInitialised());
750 CCECBusDevice
*primary
= bIsRunning
? GetPrimaryDevice() : NULL
;
751 uint16_t iPA
= primary
? primary
->GetCurrentPhysicalAddress() : CEC_INVALID_PHYSICAL_ADDRESS
;
753 // update the callbacks
754 if (configuration
.callbacks
)
755 EnableCallbacks(configuration
.callbackParam
, configuration
.callbacks
);
757 // update the client version
758 SetClientVersion((cec_client_version
)configuration
.clientVersion
);
760 // update the OSD name
761 CStdString
strOSDName(configuration
.strDeviceName
);
762 SetOSDName(strOSDName
);
764 // update the TV vendor override
765 SetTVVendorOverride((cec_vendor_id
)configuration
.tvVendor
);
769 CLockObject
lock(m_mutex
);
770 m_configuration
.bUseTVMenuLanguage
= configuration
.bUseTVMenuLanguage
;
771 m_configuration
.bActivateSource
= configuration
.bActivateSource
;
772 m_configuration
.bGetSettingsFromROM
= configuration
.bGetSettingsFromROM
;
773 m_configuration
.wakeDevices
= configuration
.wakeDevices
;
774 m_configuration
.powerOffDevices
= configuration
.powerOffDevices
;
775 m_configuration
.bPowerOffScreensaver
= configuration
.bPowerOffScreensaver
;
776 m_configuration
.bPowerOffOnStandby
= configuration
.bPowerOffOnStandby
;
778 // client version 1.5.1
779 if (configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_5_1
)
780 m_configuration
.bSendInactiveSource
= configuration
.bSendInactiveSource
;
782 // client version 1.6.0
783 if (configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_6_0
)
785 m_configuration
.bPowerOffDevicesOnStandby
= configuration
.bPowerOffDevicesOnStandby
;
786 m_configuration
.bShutdownOnStandby
= configuration
.bShutdownOnStandby
;
789 // client version 1.6.2
790 if (configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_6_2
)
792 memcpy(m_configuration
.strDeviceLanguage
, configuration
.strDeviceLanguage
, 3);
795 // ensure that there is at least 1 device type set
796 if (m_configuration
.deviceTypes
.IsEmpty())
797 m_configuration
.deviceTypes
.Add(CEC_DEVICE_TYPE_RECORDING_DEVICE
);
800 bool bNeedReinit(false);
803 if (SetDeviceTypes(configuration
.deviceTypes
))
805 // the device type changed. just copy the rest, and re-register
807 CLockObject
lock(m_mutex
);
808 m_configuration
.iPhysicalAddress
= configuration
.iPhysicalAddress
;
809 m_configuration
.baseDevice
= configuration
.baseDevice
;
810 m_configuration
.iHDMIPort
= configuration
.iHDMIPort
;
816 // set the physical address
817 SetPhysicalAddress(configuration
);
820 m_processor
->PersistConfiguration(m_configuration
);
823 primary
= GetPrimaryDevice();
825 if (bNeedReinit
|| !primary
|| primary
->GetCurrentPhysicalAddress() != iPA
)
827 // PA or device type changed
828 m_processor
->RegisterClient(this);
830 else if (primary
&& configuration
.bActivateSource
== 1 && bIsRunning
&& !primary
->IsActiveSource())
832 // activate the source if we're not already the active source
833 primary
->ActivateSource();
839 void CCECClient::AddCommand(const cec_command
&command
)
841 CLockObject
lock(m_mutex
);
843 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
);
845 if (m_configuration
.callbacks
&& m_configuration
.callbacks
->CBCecCommand
)
846 m_configuration
.callbacks
->CBCecCommand(m_configuration
.callbackParam
, command
);
847 else if (!m_commandBuffer
.Push(command
))
848 LIB_CEC
->AddLog(CEC_LOG_WARNING
, "command buffer is full");
851 int CCECClient::MenuStateChanged(const cec_menu_state newState
)
853 CLockObject
lock(m_mutex
);
855 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, ">> %s: %s", ToString(CEC_OPCODE_MENU_REQUEST
), ToString(newState
));
857 if (m_configuration
.callbacks
&&
858 m_configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_6_2
&&
859 m_configuration
.callbacks
->CBCecMenuStateChanged
)
860 return m_configuration
.callbacks
->CBCecMenuStateChanged(m_configuration
.callbackParam
, newState
);
865 void CCECClient::Alert(const libcec_alert type
, const libcec_parameter
¶m
)
867 CLockObject
lock(m_mutex
);
869 if (m_configuration
.callbacks
&&
870 m_configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_6_0
&&
871 m_configuration
.callbacks
->CBCecAlert
)
872 m_configuration
.callbacks
->CBCecAlert(m_configuration
.callbackParam
, type
, param
);
875 void CCECClient::AddLog(const cec_log_message
&message
)
877 CLockObject
lock(m_logMutex
);
878 if (m_configuration
.callbacks
&& m_configuration
.callbacks
->CBCecLogMessage
)
879 m_configuration
.callbacks
->CBCecLogMessage(m_configuration
.callbackParam
, message
);
881 m_logBuffer
.Push(message
);
884 void CCECClient::AddKey(void)
886 CLockObject
lock(m_mutex
);
888 if (m_iCurrentButton
!= CEC_USER_CONTROL_CODE_UNKNOWN
)
892 key
.duration
= (unsigned int) (GetTimeMs() - m_buttontime
);
893 key
.keycode
= m_iCurrentButton
;
894 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "key released: %1x", key
.keycode
);
896 if (m_configuration
.callbacks
&& m_configuration
.callbacks
->CBCecKeyPress
)
897 m_configuration
.callbacks
->CBCecKeyPress(m_configuration
.callbackParam
, key
);
899 m_keyBuffer
.Push(key
);
900 m_iCurrentButton
= CEC_USER_CONTROL_CODE_UNKNOWN
;
906 void CCECClient::AddKey(const cec_keypress
&key
)
908 CLockObject
lock(m_mutex
);
910 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "key pressed: %1x", key
.keycode
);
912 if (m_configuration
.callbacks
&& m_configuration
.callbacks
->CBCecKeyPress
)
913 m_configuration
.callbacks
->CBCecKeyPress(m_configuration
.callbackParam
, key
);
915 m_keyBuffer
.Push(key
);
917 m_iCurrentButton
= key
.duration
> 0 ? CEC_USER_CONTROL_CODE_UNKNOWN
: key
.keycode
;
918 m_buttontime
= key
.duration
> 0 ? 0 : GetTimeMs();
921 void CCECClient::SetCurrentButton(const cec_user_control_code iButtonCode
)
923 // push a keypress to the buffer with 0 duration and another with the duration set when released
926 key
.keycode
= iButtonCode
;
931 void CCECClient::CheckKeypressTimeout(void)
933 if (m_iCurrentButton
!= CEC_USER_CONTROL_CODE_UNKNOWN
&& GetTimeMs() - m_buttontime
> CEC_BUTTON_TIMEOUT
)
936 m_iCurrentButton
= CEC_USER_CONTROL_CODE_UNKNOWN
;
940 void CCECClient::ConfigurationChanged(const libcec_configuration
&config
)
942 CLockObject
lock(m_mutex
);
944 if (m_configuration
.callbacks
&&
945 m_configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_5_0
&&
946 m_configuration
.callbacks
->CBCecConfigurationChanged
&&
947 m_processor
->CECInitialised())
948 m_configuration
.callbacks
->CBCecConfigurationChanged(m_configuration
.callbackParam
, config
);
951 bool CCECClient::EnableCallbacks(void *cbParam
, ICECCallbacks
*callbacks
)
953 CLockObject
lock(m_mutex
);
954 m_configuration
.callbackParam
= cbParam
;
955 m_configuration
.callbacks
= callbacks
;
959 bool CCECClient::PingAdapter(void)
961 return m_processor
? m_processor
->PingAdapter() : false;
964 bool CCECClient::GetNextLogMessage(cec_log_message
*message
)
966 return (m_logBuffer
.Pop(*message
));
969 bool CCECClient::GetNextKeypress(cec_keypress
*key
)
971 return m_keyBuffer
.Pop(*key
);
974 bool CCECClient::GetNextCommand(cec_command
*command
)
976 return m_commandBuffer
.Pop(*command
);
979 CStdString
CCECClient::GetConnectionInfo(void)
982 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
);
983 if (m_configuration
.iFirmwareBuildDate
!= CEC_FW_BUILD_UNKNOWN
)
985 time_t buildTime
= (time_t)m_configuration
.iFirmwareBuildDate
;
986 strLog
.AppendFormat(", firmware build date: %s", asctime(gmtime(&buildTime
)));
987 strLog
= strLog
.Left((int)strLog
.length() - 1); // strip \n added by asctime
988 strLog
.append(" +0000");
991 // log the addresses that are being used
992 if (!m_configuration
.logicalAddresses
.IsEmpty())
994 strLog
.append(", logical address(es) = ");
995 CECDEVICEVEC devices
;
996 m_processor
->GetDevices()->GetByLogicalAddresses(devices
, m_configuration
.logicalAddresses
);
997 for (CECDEVICEVEC::iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
998 strLog
.AppendFormat("%s (%X) ", (*it
)->GetLogicalAddressName(), (*it
)->GetLogicalAddress());
1001 if (!CLibCEC::IsValidPhysicalAddress(m_configuration
.iPhysicalAddress
))
1002 strLog
.AppendFormat(", base device: %s (%X), HDMI port number: %d", ToString(m_configuration
.baseDevice
), m_configuration
.baseDevice
, m_configuration
.iHDMIPort
);
1004 strLog
.AppendFormat(", physical address: %04x", m_configuration
.iPhysicalAddress
);
1009 void CCECClient::SetTVVendorOverride(const cec_vendor_id id
)
1012 CLockObject
lock(m_mutex
);
1013 m_configuration
.tvVendor
= id
;
1016 if (id
!= CEC_VENDOR_UNKNOWN
)
1018 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s - vendor id '%s'", __FUNCTION__
, ToString(id
));
1020 CCECBusDevice
*tv
= m_processor
? m_processor
->GetTV() : NULL
;
1022 tv
->SetVendorId((uint64_t)id
);
1026 cec_vendor_id
CCECClient::GetTVVendorOverride(void)
1028 CLockObject
lock(m_mutex
);
1029 return (cec_vendor_id
)m_configuration
.tvVendor
;
1032 void CCECClient::SetOSDName(const CStdString
&strDeviceName
)
1035 CLockObject
lock(m_mutex
);
1036 snprintf(m_configuration
.strDeviceName
, 13, "%s", strDeviceName
.c_str());
1039 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s - using OSD name '%s'", __FUNCTION__
, strDeviceName
.c_str());
1041 CCECBusDevice
*primary
= GetPrimaryDevice();
1042 if (primary
&& !primary
->GetCurrentOSDName().Equals(strDeviceName
))
1044 primary
->SetOSDName(strDeviceName
);
1045 if (m_processor
&& m_processor
->CECInitialised())
1046 primary
->TransmitOSDName(CECDEVICE_TV
);
1050 CStdString
CCECClient::GetOSDName(void)
1052 CLockObject
lock(m_mutex
);
1053 CStdString
strOSDName(m_configuration
.strDeviceName
);
1057 void CCECClient::SetWakeDevices(const cec_logical_addresses
&addresses
)
1059 CLockObject
lock(m_mutex
);
1060 m_configuration
.wakeDevices
= addresses
;
1063 cec_logical_addresses
CCECClient::GetWakeDevices(void)
1065 CLockObject
lock(m_mutex
);
1066 return m_configuration
.wakeDevices
;
1069 bool CCECClient::AutodetectPhysicalAddress(void)
1071 bool bPhysicalAutodetected(false);
1072 uint16_t iPhysicalAddress
= m_processor
? m_processor
->GetDetectedPhysicalAddress() : CEC_INVALID_PHYSICAL_ADDRESS
;
1074 if (CLibCEC::IsValidPhysicalAddress(iPhysicalAddress
))
1076 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s - autodetected physical address '%04X'", __FUNCTION__
, iPhysicalAddress
);
1078 CLockObject
lock(m_mutex
);
1079 m_configuration
.iPhysicalAddress
= iPhysicalAddress
;
1080 m_configuration
.iHDMIPort
= CEC_HDMI_PORTNUMBER_NONE
;
1081 m_configuration
.baseDevice
= CECDEVICE_UNKNOWN
;
1082 bPhysicalAutodetected
= true;
1085 SetDevicePhysicalAddress(iPhysicalAddress
);
1087 return bPhysicalAutodetected
;
1090 void CCECClient::SetClientVersion(const cec_client_version version
)
1092 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s - using client version '%s'", __FUNCTION__
, ToString(version
));
1094 CLockObject
lock(m_mutex
);
1095 m_configuration
.clientVersion
= (uint32_t)version
;
1098 cec_client_version
CCECClient::GetClientVersion(void)
1100 CLockObject
lock(m_mutex
);
1101 return (cec_client_version
)m_configuration
.clientVersion
;
1104 bool CCECClient::SetDeviceTypes(const cec_device_type_list
&deviceTypes
)
1106 bool bNeedReinit(false);
1109 CLockObject
lock(m_mutex
);
1110 bNeedReinit
= m_processor
&& m_processor
->CECInitialised() &&
1111 (m_configuration
.deviceTypes
!= deviceTypes
);
1112 m_configuration
.deviceTypes
= deviceTypes
;
1116 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s - using primary device type '%s'", __FUNCTION__
, ToString(deviceTypes
[0]));
1121 cec_device_type_list
CCECClient::GetDeviceTypes(void)
1123 cec_device_type_list retVal
;
1124 CLockObject
lock(m_mutex
);
1125 retVal
= m_configuration
.deviceTypes
;
1129 bool CCECClient::SetDevicePhysicalAddress(const uint16_t iPhysicalAddress
)
1131 if (!CLibCEC::IsValidPhysicalAddress(iPhysicalAddress
))
1134 // reconfigure all devices
1135 cec_logical_address
reactivateSource(CECDEVICE_UNKNOWN
);
1136 CECDEVICEVEC devices
;
1137 m_processor
->GetDevices()->GetByLogicalAddresses(devices
, m_configuration
.logicalAddresses
);
1138 for (CECDEVICEVEC::iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
1140 // if this device was the active source, reactivate it afterwards
1141 if ((*it
)->IsActiveSource())
1142 reactivateSource
= (*it
)->GetLogicalAddress();
1144 // mark the device as inactive source
1145 if (IsInitialised())
1146 (*it
)->MarkAsInactiveSource();
1148 // set the new physical address
1149 (*it
)->SetPhysicalAddress(iPhysicalAddress
);
1152 if (IsInitialised())
1153 (*it
)->TransmitPhysicalAddress();
1156 // reactivate the previous active source
1157 if (reactivateSource
!= CECDEVICE_UNKNOWN
&&
1158 m_processor
->CECInitialised() &&
1161 CCECBusDevice
*device
= m_processor
->GetDevice(reactivateSource
);
1163 device
->ActivateSource();
1169 bool CCECClient::SwitchMonitoring(bool bEnable
)
1171 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "== %s monitoring mode ==", bEnable
? "enabling" : "disabling");
1176 return m_processor
->UnregisterClient(this);
1178 return m_processor
->RegisterClient(this);
1184 bool CCECClient::PollDevice(const cec_logical_address iAddress
)
1186 // try to find the primary device
1187 CCECBusDevice
*primary
= GetPrimaryDevice();
1188 // poll the destination, with the primary as source
1190 return primary
->TransmitPoll(iAddress
);
1192 return m_processor
? m_processor
->PollDevice(iAddress
) : false;
1195 cec_logical_addresses
CCECClient::GetActiveDevices(void)
1197 CECDEVICEVEC activeDevices
;
1199 m_processor
->GetDevices()->GetActive(activeDevices
);
1200 return CCECDeviceMap::ToLogicalAddresses(activeDevices
);
1203 bool CCECClient::IsActiveDevice(const cec_logical_address iAddress
)
1205 cec_logical_addresses activeDevices
= GetActiveDevices();
1206 return activeDevices
.IsSet(iAddress
);
1209 bool CCECClient::IsActiveDeviceType(const cec_device_type type
)
1211 CECDEVICEVEC activeDevices
;
1213 m_processor
->GetDevices()->GetActive(activeDevices
);
1214 CCECDeviceMap::FilterType(type
, activeDevices
);
1215 return !activeDevices
.empty();
1218 cec_logical_address
CCECClient::GetActiveSource(void)
1220 return m_processor
? m_processor
->GetActiveSource() : CECDEVICE_UNKNOWN
;
1223 bool CCECClient::IsActiveSource(const cec_logical_address iAddress
)
1225 return m_processor
? m_processor
->IsActiveSource(iAddress
) : false;
1228 bool CCECClient::SetStreamPath(const cec_logical_address iAddress
)
1230 uint16_t iPhysicalAddress
= GetDevicePhysicalAddress(iAddress
);
1231 if (iPhysicalAddress
!= CEC_INVALID_PHYSICAL_ADDRESS
)
1232 return SetStreamPath(iPhysicalAddress
);
1236 bool CCECClient::SetStreamPath(const uint16_t iPhysicalAddress
)
1238 return m_processor
? m_processor
->SetStreamPath(iPhysicalAddress
) : false;
1241 cec_logical_addresses
CCECClient::GetLogicalAddresses(void)
1243 cec_logical_addresses addresses
;
1244 CLockObject
lock(m_mutex
);
1245 addresses
= m_configuration
.logicalAddresses
;
1249 bool CCECClient::CanPersistConfiguration(void)
1251 return m_processor
? m_processor
->CanPersistConfiguration() : false;
1254 bool CCECClient::PersistConfiguration(const libcec_configuration
&configuration
)
1256 return m_processor
? m_processor
->PersistConfiguration(configuration
) : false;
1259 void CCECClient::RescanActiveDevices(void)
1262 m_processor
->RescanActiveDevices();
1265 bool CCECClient::IsLibCECActiveSource(void)
1267 bool bReturn(false);
1270 cec_logical_address activeSource
= m_processor
->GetActiveSource();
1271 CCECBusDevice
*device
= m_processor
->GetDevice(activeSource
);
1273 bReturn
= device
->IsHandledByLibCEC();