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 "CECTypeUtils.h"
37 #include "devices/CECPlaybackDevice.h"
38 #include "devices/CECAudioSystem.h"
39 #include "devices/CECTV.h"
42 using namespace PLATFORM
;
44 #define LIB_CEC m_processor->GetLib()
45 #define ToString(x) CCECTypeUtils::ToString(x)
47 CCECClient::CCECClient(CCECProcessor
*processor
, const libcec_configuration
&configuration
) :
48 m_processor(processor
),
49 m_bInitialised(false),
51 m_iCurrentButton(CEC_USER_CONTROL_CODE_UNKNOWN
),
54 // set the initial configuration
55 SetConfiguration(configuration
);
58 CCECClient::~CCECClient(void)
60 // unregister the client
61 if (m_processor
&& IsRegistered())
62 m_processor
->UnregisterClient(this);
65 bool CCECClient::IsInitialised(void)
67 CLockObject
lock(m_mutex
);
68 return m_bInitialised
&& m_processor
;
71 void CCECClient::SetInitialised(bool bSetTo
)
73 CLockObject
lock(m_mutex
);
74 m_bInitialised
= bSetTo
;
77 bool CCECClient::IsRegistered(void)
79 CLockObject
lock(m_mutex
);
80 return m_bRegistered
&& m_processor
;
83 void CCECClient::SetRegistered(bool bSetTo
)
85 CLockObject
lock(m_mutex
);
86 m_bRegistered
= bSetTo
;
89 bool CCECClient::OnRegister(void)
91 // return false if already initialised
95 // get all device we control
97 m_processor
->GetDevices()->GetByLogicalAddresses(devices
, m_configuration
.logicalAddresses
);
99 // return false when no devices were found
102 LIB_CEC
->AddLog(CEC_LOG_WARNING
, "cannot find the primary device (logical address %x)", GetPrimaryLogicalAdddress());
106 // mark as initialised
107 SetInitialised(true);
109 // configure all devices
110 for (CECDEVICEVEC::iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
112 // only set our OSD name for the primary device
113 if ((*it
)->GetLogicalAddress() == GetPrimaryLogicalAdddress())
114 (*it
)->SetOSDName(m_configuration
.strDeviceName
);
116 // set the default menu language for devices we control
117 (*it
)->SetMenuLanguage(m_configuration
.strDeviceLanguage
);
120 // set the physical address
121 SetPhysicalAddress(m_configuration
);
123 // ensure that we know the vendor id of the TV, so we are using the correct handler
124 m_processor
->GetTV()->GetVendorId(GetPrimaryLogicalAdddress());
126 // make the primary device the active source if the option is set
127 if (m_configuration
.bActivateSource
== 1)
128 GetPrimaryDevice()->ActivateSource();
133 bool CCECClient::SetHDMIPort(const cec_logical_address iBaseDevice
, const uint8_t iPort
, bool bForce
/* = false */)
137 // limit the HDMI port range to 1-15
138 if (iPort
< CEC_MIN_HDMI_PORTNUMBER
||
139 iPort
> CEC_MAX_HDMI_PORTNUMBER
)
142 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "setting HDMI port to %d on device %s (%d)", iPort
, ToString(iBaseDevice
), (int)iBaseDevice
);
144 // update the configuration
146 CLockObject
lock(m_mutex
);
147 m_configuration
.baseDevice
= iBaseDevice
;
148 m_configuration
.iHDMIPort
= iPort
;
151 // don't continue if the connection isn't opened
152 if (!m_processor
->CECInitialised() && !bForce
)
155 // get the PA of the base device
156 uint16_t iPhysicalAddress(CEC_INVALID_PHYSICAL_ADDRESS
);
157 CCECBusDevice
*baseDevice
= m_processor
->GetDevice(iBaseDevice
);
159 iPhysicalAddress
= baseDevice
->GetPhysicalAddress(GetPrimaryLogicalAdddress());
161 // add our port number
162 if (iPhysicalAddress
<= CEC_MAX_PHYSICAL_ADDRESS
)
164 if (iPhysicalAddress
== 0)
165 iPhysicalAddress
+= 0x1000 * iPort
;
166 else if (iPhysicalAddress
% 0x1000 == 0)
167 iPhysicalAddress
+= 0x100 * iPort
;
168 else if (iPhysicalAddress
% 0x100 == 0)
169 iPhysicalAddress
+= 0x10 * iPort
;
170 else if (iPhysicalAddress
% 0x10 == 0)
171 iPhysicalAddress
+= iPort
;
176 // set the default address when something went wrong
179 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
);
180 iPhysicalAddress
= CEC_DEFAULT_PHYSICAL_ADDRESS
;
183 // and set the address
184 SetDevicePhysicalAddress(iPhysicalAddress
);
186 ConfigurationChanged(m_configuration
);
191 void CCECClient::ResetPhysicalAddress(void)
193 SetPhysicalAddress(m_configuration
);
196 void CCECClient::SetPhysicalAddress(const libcec_configuration
&configuration
)
200 // override the physical address from configuration.iPhysicalAddress if it's set
201 if (!bPASet
&& CLibCEC::IsValidPhysicalAddress(configuration
.iPhysicalAddress
))
202 bPASet
= SetPhysicalAddress(configuration
.iPhysicalAddress
);
204 // try to autodetect the address
205 if (!bPASet
&& m_processor
->CECInitialised())
207 bPASet
= AutodetectPhysicalAddress();
208 m_configuration
.bAutodetectAddress
= bPASet
? 1 : 0;
211 // use the base device + hdmi port settings
213 bPASet
= SetHDMIPort(configuration
.baseDevice
, configuration
.iHDMIPort
);
215 // reset to defaults if something went wrong
218 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s - resetting HDMI port and base device to defaults", __FUNCTION__
);
219 m_configuration
.baseDevice
= CECDEVICE_UNKNOWN
;
220 m_configuration
.iHDMIPort
= CEC_HDMI_PORTNUMBER_NONE
;
224 bool CCECClient::SetPhysicalAddress(const uint16_t iPhysicalAddress
)
226 // update the configuration
228 CLockObject
lock(m_mutex
);
229 if (m_configuration
.iPhysicalAddress
== iPhysicalAddress
)
231 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "physical address unchanged (%04X)", iPhysicalAddress
);
236 m_configuration
.iPhysicalAddress
= iPhysicalAddress
;
237 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "setting physical address to '%04X'", iPhysicalAddress
);
241 // persist the new configuration
242 m_processor
->PersistConfiguration(m_configuration
);
244 // set the physical address for each device
245 SetDevicePhysicalAddress(iPhysicalAddress
);
247 // and send back the updated configuration
248 ConfigurationChanged(m_configuration
);
253 bool CCECClient::AllocateLogicalAddresses(void)
255 // reset all previous LAs that were set
256 m_configuration
.logicalAddresses
.Clear();
258 // display an error if no device types are set
259 if (m_configuration
.deviceTypes
.IsEmpty())
261 LIB_CEC
->AddLog(CEC_LOG_ERROR
, "no device types given");
265 // check each entry of the list
266 for (uint8_t iPtr
= 0; iPtr
< 5; iPtr
++)
268 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
271 // find an LA for this type
272 cec_logical_address
address(CECDEVICE_UNKNOWN
);
273 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
274 address
= AllocateLogicalAddressRecordingDevice();
275 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_TUNER
)
276 address
= AllocateLogicalAddressTuner();
277 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
)
278 address
= AllocateLogicalAddressPlaybackDevice();
279 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
280 address
= AllocateLogicalAddressAudioSystem();
282 // display an error if no LA could be allocated
283 if (address
== CECDEVICE_UNKNOWN
)
285 LIB_CEC
->AddLog(CEC_LOG_ERROR
, "%s - failed to allocate device '%d', type '%s'", __FUNCTION__
, iPtr
, ToString(m_configuration
.deviceTypes
.types
[iPtr
]));
289 // display the registered LA
290 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s - device '%d', type '%s', LA '%X'", __FUNCTION__
, iPtr
, ToString(m_configuration
.deviceTypes
.types
[iPtr
]), address
);
291 m_configuration
.logicalAddresses
.Set(address
);
297 cec_logical_address
CCECClient::AllocateLogicalAddressRecordingDevice(void)
299 cec_logical_address
retVal(CECDEVICE_UNKNOWN
);
301 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'recording device'");
302 if (m_processor
->TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1
))
303 retVal
= CECDEVICE_RECORDINGDEVICE1
;
304 else if (m_processor
->TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2
))
305 retVal
= CECDEVICE_RECORDINGDEVICE2
;
306 else if (m_processor
->TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3
))
307 retVal
= CECDEVICE_RECORDINGDEVICE3
;
312 cec_logical_address
CCECClient::AllocateLogicalAddressTuner(void)
314 cec_logical_address
retVal(CECDEVICE_UNKNOWN
);
316 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'tuner'");
317 if (m_processor
->TryLogicalAddress(CECDEVICE_TUNER1
))
318 retVal
= CECDEVICE_TUNER1
;
319 else if (m_processor
->TryLogicalAddress(CECDEVICE_TUNER2
))
320 retVal
= CECDEVICE_TUNER2
;
321 else if (m_processor
->TryLogicalAddress(CECDEVICE_TUNER3
))
322 retVal
= CECDEVICE_TUNER3
;
323 else if (m_processor
->TryLogicalAddress(CECDEVICE_TUNER4
))
324 retVal
= CECDEVICE_TUNER4
;
329 cec_logical_address
CCECClient::AllocateLogicalAddressPlaybackDevice(void)
331 cec_logical_address
retVal(CECDEVICE_UNKNOWN
);
333 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'playback device'");
334 if (m_processor
->TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1
))
335 retVal
= CECDEVICE_PLAYBACKDEVICE1
;
336 else if (m_processor
->TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2
))
337 retVal
= CECDEVICE_PLAYBACKDEVICE2
;
338 else if (m_processor
->TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3
))
339 retVal
= CECDEVICE_PLAYBACKDEVICE3
;
344 cec_logical_address
CCECClient::AllocateLogicalAddressAudioSystem(void)
346 cec_logical_address
retVal(CECDEVICE_UNKNOWN
);
348 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'audiosystem'");
349 if (m_processor
->TryLogicalAddress(CECDEVICE_AUDIOSYSTEM
))
350 retVal
= CECDEVICE_AUDIOSYSTEM
;
355 CCECBusDevice
*CCECClient::GetDeviceByType(const cec_device_type type
) const
357 // get all devices that match our logical addresses
358 CECDEVICEVEC devices
;
359 m_processor
->GetDevices()->GetByLogicalAddresses(devices
, m_configuration
.logicalAddresses
);
361 // filter the type we need
362 CCECDeviceMap::FilterType(type
, devices
);
364 return devices
.empty() ?
369 bool CCECClient::ChangeDeviceType(const cec_device_type from
, const cec_device_type to
)
371 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "changing device type '%s' into '%s'", ToString(from
), ToString(to
));
373 CLockObject
lock(m_mutex
);
375 // get the previous device that was allocated
376 CCECBusDevice
*previousDevice
= GetDeviceByType(from
);
380 // change the type in the device type list
381 bool bChanged(false);
382 for (uint8_t iPtr
= 0; iPtr
< 5; iPtr
++)
384 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
387 if (m_configuration
.deviceTypes
.types
[iPtr
] == from
)
390 m_configuration
.deviceTypes
.types
[iPtr
] = to
;
392 else if (m_configuration
.deviceTypes
.types
[iPtr
] == to
&& bChanged
)
394 // ensure that dupes are removed
395 m_configuration
.deviceTypes
.types
[iPtr
] = CEC_DEVICE_TYPE_RESERVED
;
399 // re-register the client to set the new ackmask
400 if (!m_processor
->RegisterClient(this))
406 bool CCECClient::SetLogicalAddress(const cec_logical_address iLogicalAddress
)
408 CLockObject
lock(m_mutex
);
409 if (GetPrimaryLogicalAdddress() != iLogicalAddress
)
412 CLockObject
lock(m_mutex
);
413 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< setting primary logical address to %1x", iLogicalAddress
);
414 m_configuration
.logicalAddresses
.primary
= iLogicalAddress
;
415 m_configuration
.logicalAddresses
.Set(iLogicalAddress
);
417 return m_processor
->RegisterClient(this);
423 bool CCECClient::Transmit(const cec_command
&data
)
425 return m_processor
? m_processor
->Transmit(data
) : false;
428 bool CCECClient::SendPowerOnDevices(const cec_logical_address address
/* = CECDEVICE_TV */)
430 // if the broadcast address if set as destination and the client version >=1.5.0, read the wakeDevices setting
431 if (address
== CECDEVICE_BROADCAST
&& m_configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_5_0
)
433 CECDEVICEVEC devices
;
434 m_processor
->GetDevices()->GetWakeDevices(m_configuration
, devices
);
435 return m_processor
->PowerOnDevices(GetPrimaryLogicalAdddress(), devices
);
438 return m_processor
->PowerOnDevice(GetPrimaryLogicalAdddress(), address
);
441 bool CCECClient::SendStandbyDevices(const cec_logical_address address
/* = CECDEVICE_BROADCAST */)
443 // if the broadcast address if set as destination and the client version >=1.5.0, read the standbyDevices setting
444 if (address
== CECDEVICE_BROADCAST
&& m_configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_5_0
)
446 CECDEVICEVEC devices
;
447 m_processor
->GetDevices()->GetPowerOffDevices(m_configuration
, devices
);
448 return m_processor
->StandbyDevices(GetPrimaryLogicalAdddress(), devices
);
451 return m_processor
->StandbyDevice(GetPrimaryLogicalAdddress(), address
);
454 bool CCECClient::SendSetActiveSource(const cec_device_type type
/* = CEC_DEVICE_TYPE_RESERVED */)
456 // get the devices that are controlled by us
457 CECDEVICEVEC devices
;
458 m_processor
->GetDevices()->GetByLogicalAddresses(devices
, m_configuration
.logicalAddresses
);
460 // filter out the device that matches the given type
461 if (type
!= CEC_DEVICE_TYPE_RESERVED
)
462 CCECDeviceMap::FilterType(type
, devices
);
464 // no devices left, re-fetch the list of devices that are controlled by us
466 m_processor
->GetDevices()->GetByLogicalAddresses(devices
, m_configuration
.logicalAddresses
);
468 if (!devices
.empty())
470 // get the first device from the list
471 CCECBusDevice
*device
= *devices
.begin();
474 if (!m_processor
->CECInitialised())
475 device
->MarkAsActiveSource();
476 else if (device
->HasValidPhysicalAddress())
477 return device
->ActivateSource();
483 CCECPlaybackDevice
*CCECClient::GetPlaybackDevice(void)
485 CCECPlaybackDevice
*device(NULL
);
486 CECDEVICEVEC devices
;
488 // get the playback devices
489 m_processor
->GetDevices()->GetByLogicalAddresses(devices
, m_configuration
.logicalAddresses
);
490 CCECDeviceMap::FilterType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
, devices
);
492 // no matches, get the recording devices
495 m_processor
->GetDevices()->GetByLogicalAddresses(devices
, m_configuration
.logicalAddresses
);
496 CCECDeviceMap::FilterType(CEC_DEVICE_TYPE_RECORDING_DEVICE
, devices
);
499 // get the first device that matches, and cast it to CCECPlaybackDevice
500 if (!devices
.empty())
501 device
= (*devices
.begin())->AsPlaybackDevice();
506 cec_logical_address
CCECClient::GetPrimaryLogicalAdddress(void)
508 CLockObject
lock(m_mutex
);
509 return m_configuration
.logicalAddresses
.primary
;
512 CCECBusDevice
*CCECClient::GetPrimaryDevice(void)
514 return m_processor
->GetDevice(GetPrimaryLogicalAdddress());
517 bool CCECClient::SendSetDeckControlMode(const cec_deck_control_mode mode
, bool bSendUpdate
/* = true */)
519 // find a playback device that we control
520 CCECPlaybackDevice
*device
= GetPlaybackDevice();
523 // and set the deck control mode if there is a match
524 device
->SetDeckControlMode(mode
);
526 return device
->TransmitDeckStatus(CECDEVICE_TV
);
534 bool CCECClient::SendSetDeckInfo(const cec_deck_info info
, bool bSendUpdate
/* = true */)
536 // find a playback device that we control
537 CCECPlaybackDevice
*device
= GetPlaybackDevice();
540 // and set the deck status if there is a match
541 device
->SetDeckStatus(info
);
543 return device
->AsPlaybackDevice()->TransmitDeckStatus(CECDEVICE_TV
);
551 bool CCECClient::SendSetMenuState(const cec_menu_state state
, bool bSendUpdate
/* = true */)
553 CECDEVICEVEC devices
;
555 // set the menu state for all devices that are controlled by us
556 m_processor
->GetDevices()->GetByLogicalAddresses(devices
, m_configuration
.logicalAddresses
);
557 for (CECDEVICEVEC::iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
559 (*it
)->SetMenuState(state
);
561 (*it
)->TransmitMenuState(CECDEVICE_TV
);
567 bool CCECClient::SendSetInactiveView(void)
569 CECDEVICEVEC devices
;
571 // mark all devices that are controlled by us as inactive source
572 m_processor
->GetDevices()->GetByLogicalAddresses(devices
, m_configuration
.logicalAddresses
);
573 for (CECDEVICEVEC::iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
575 if ((*it
)->IsActiveSource())
577 (*it
)->MarkAsInactiveSource();
578 return (*it
)->TransmitInactiveSource();
585 bool CCECClient::SendSetOSDString(const cec_logical_address iLogicalAddress
, const cec_display_control duration
, const char *strMessage
)
587 CCECBusDevice
*primary
= GetPrimaryDevice();
589 return primary
->TransmitOSDString(iLogicalAddress
, duration
, strMessage
);
594 cec_version
CCECClient::GetDeviceCecVersion(const cec_logical_address iAddress
)
596 CCECBusDevice
*device
= m_processor
->GetDevice(iAddress
);
598 return device
->GetCecVersion(GetPrimaryLogicalAdddress());
599 return CEC_VERSION_UNKNOWN
;
602 bool CCECClient::GetDeviceMenuLanguage(const cec_logical_address iAddress
, cec_menu_language
&language
)
604 CCECBusDevice
*device
= m_processor
->GetDevice(iAddress
);
607 language
= device
->GetMenuLanguage(GetPrimaryLogicalAdddress());
608 return (strcmp(language
.language
, "???") != 0);
613 cec_osd_name
CCECClient::GetDeviceOSDName(const cec_logical_address iAddress
)
616 retVal
.device
= iAddress
;
619 CCECBusDevice
*device
= m_processor
->GetDevice(iAddress
);
622 CStdString strOSDName
= device
->GetOSDName(GetPrimaryLogicalAdddress());
623 snprintf(retVal
.name
, sizeof(retVal
.name
), "%s", strOSDName
.c_str());
624 retVal
.device
= iAddress
;
630 uint16_t CCECClient::GetDevicePhysicalAddress(const cec_logical_address iAddress
)
632 CCECBusDevice
*device
= m_processor
->GetDevice(iAddress
);
634 return device
->GetPhysicalAddress(GetPrimaryLogicalAdddress());
635 return CEC_INVALID_PHYSICAL_ADDRESS
;
638 cec_power_status
CCECClient::GetDevicePowerStatus(const cec_logical_address iAddress
)
640 CCECBusDevice
*device
= m_processor
->GetDevice(iAddress
);
642 return device
->GetPowerStatus(GetPrimaryLogicalAdddress());
643 return CEC_POWER_STATUS_UNKNOWN
;
646 uint64_t CCECClient::GetDeviceVendorId(const cec_logical_address iAddress
)
648 CCECBusDevice
*device
= m_processor
->GetDevice(iAddress
);
650 return device
->GetVendorId(GetPrimaryLogicalAdddress());
651 return CEC_VENDOR_UNKNOWN
;
654 uint8_t CCECClient::SendVolumeUp(bool bSendRelease
/* = true */)
656 CCECBusDevice
*device
= GetPrimaryDevice();
657 CCECAudioSystem
*audio
= m_processor
->GetAudioSystem();
659 return device
&& audio
&& audio
->IsPresent() ?
660 audio
->VolumeUp(device
->GetLogicalAddress(), bSendRelease
) :
661 (uint8_t)CEC_AUDIO_VOLUME_STATUS_UNKNOWN
;
664 uint8_t CCECClient::SendVolumeDown(bool bSendRelease
/* = true */)
666 CCECBusDevice
*device
= GetPrimaryDevice();
667 CCECAudioSystem
*audio
= m_processor
->GetAudioSystem();
669 return device
&& audio
&& audio
->IsPresent() ?
670 audio
->VolumeDown(device
->GetLogicalAddress(), bSendRelease
) :
671 (uint8_t)CEC_AUDIO_VOLUME_STATUS_UNKNOWN
;
674 uint8_t CCECClient::SendMuteAudio(void)
676 CCECBusDevice
*device
= GetPrimaryDevice();
677 CCECAudioSystem
*audio
= m_processor
->GetAudioSystem();
679 return device
&& audio
&& audio
->IsPresent() ?
680 audio
->MuteAudio(device
->GetLogicalAddress()) :
681 (uint8_t)CEC_AUDIO_VOLUME_STATUS_UNKNOWN
;
684 bool CCECClient::SendKeypress(const cec_logical_address iDestination
, const cec_user_control_code key
, bool bWait
/* = true */)
686 CCECBusDevice
*dest
= m_processor
->GetDevice(iDestination
);
689 dest
->TransmitKeypress(GetPrimaryLogicalAdddress(), key
, bWait
) :
693 bool CCECClient::SendKeyRelease(const cec_logical_address iDestination
, bool bWait
/* = true */)
695 CCECBusDevice
*dest
= m_processor
->GetDevice(iDestination
);
698 dest
->TransmitKeyRelease(GetPrimaryLogicalAdddress(), bWait
) :
702 bool CCECClient::GetCurrentConfiguration(libcec_configuration
&configuration
)
704 CLockObject
lock(m_mutex
);
706 // client version 1.5.0
707 snprintf(configuration
.strDeviceName
, 13, "%s", m_configuration
.strDeviceName
);
708 configuration
.deviceTypes
= m_configuration
.deviceTypes
;
709 configuration
.bAutodetectAddress
= m_configuration
.bAutodetectAddress
;
710 configuration
.iPhysicalAddress
= m_configuration
.iPhysicalAddress
;
711 configuration
.baseDevice
= m_configuration
.baseDevice
;
712 configuration
.iHDMIPort
= m_configuration
.iHDMIPort
;
713 configuration
.clientVersion
= m_configuration
.clientVersion
;
714 configuration
.serverVersion
= m_configuration
.serverVersion
;
715 configuration
.tvVendor
= m_configuration
.tvVendor
;
717 configuration
.bGetSettingsFromROM
= m_configuration
.bGetSettingsFromROM
;
718 configuration
.bUseTVMenuLanguage
= m_configuration
.bUseTVMenuLanguage
;
719 configuration
.bActivateSource
= m_configuration
.bActivateSource
;
720 configuration
.wakeDevices
= m_configuration
.wakeDevices
;
721 configuration
.powerOffDevices
= m_configuration
.powerOffDevices
;
722 configuration
.bPowerOffScreensaver
= m_configuration
.bPowerOffScreensaver
;
723 configuration
.bPowerOffOnStandby
= m_configuration
.bPowerOffOnStandby
;
725 // client version 1.5.1
726 if (configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_5_1
)
727 configuration
.bSendInactiveSource
= m_configuration
.bSendInactiveSource
;
729 // client version 1.5.3
730 if (configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_5_3
)
731 configuration
.logicalAddresses
= m_configuration
.logicalAddresses
;
733 // client version 1.6.0
734 if (configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_6_0
)
736 configuration
.iFirmwareVersion
= m_configuration
.iFirmwareVersion
;
737 configuration
.bPowerOffDevicesOnStandby
= m_configuration
.bPowerOffDevicesOnStandby
;
738 configuration
.bShutdownOnStandby
= m_configuration
.bShutdownOnStandby
;
741 // client version 1.6.2
742 if (configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_6_2
)
744 memcpy(configuration
.strDeviceLanguage
, m_configuration
.strDeviceLanguage
, 3);
745 configuration
.iFirmwareBuildDate
= m_configuration
.iFirmwareBuildDate
;
748 // client version 1.6.3
749 if (configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_6_3
)
751 configuration
.bMonitorOnly
= m_configuration
.bMonitorOnly
;
757 bool CCECClient::SetConfiguration(const libcec_configuration
&configuration
)
759 bool bIsRunning(m_processor
&& m_processor
->CECInitialised());
760 CCECBusDevice
*primary
= bIsRunning
? GetPrimaryDevice() : NULL
;
761 uint16_t iPA
= primary
? primary
->GetCurrentPhysicalAddress() : CEC_INVALID_PHYSICAL_ADDRESS
;
763 // update the callbacks
764 if (configuration
.callbacks
)
765 EnableCallbacks(configuration
.callbackParam
, configuration
.callbacks
);
767 // update the client version
768 SetClientVersion((cec_client_version
)configuration
.clientVersion
);
770 // update the OSD name
771 CStdString
strOSDName(configuration
.strDeviceName
);
772 SetOSDName(strOSDName
);
774 // update the TV vendor override
775 SetTVVendorOverride((cec_vendor_id
)configuration
.tvVendor
);
779 CLockObject
lock(m_mutex
);
780 m_configuration
.bUseTVMenuLanguage
= configuration
.bUseTVMenuLanguage
;
781 m_configuration
.bActivateSource
= configuration
.bActivateSource
;
782 m_configuration
.bGetSettingsFromROM
= configuration
.bGetSettingsFromROM
;
783 m_configuration
.wakeDevices
= configuration
.wakeDevices
;
784 m_configuration
.powerOffDevices
= configuration
.powerOffDevices
;
785 m_configuration
.bPowerOffScreensaver
= configuration
.bPowerOffScreensaver
;
786 m_configuration
.bPowerOffOnStandby
= configuration
.bPowerOffOnStandby
;
788 // client version 1.5.1
789 if (configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_5_1
)
790 m_configuration
.bSendInactiveSource
= configuration
.bSendInactiveSource
;
792 // client version 1.6.0
793 if (configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_6_0
)
795 m_configuration
.bPowerOffDevicesOnStandby
= configuration
.bPowerOffDevicesOnStandby
;
796 m_configuration
.bShutdownOnStandby
= configuration
.bShutdownOnStandby
;
799 // client version 1.6.2
800 if (configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_6_2
)
802 memcpy(m_configuration
.strDeviceLanguage
, configuration
.strDeviceLanguage
, 3);
805 // client version 1.6.3
806 if (configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_6_3
)
808 m_configuration
.bMonitorOnly
= configuration
.bMonitorOnly
;
811 // ensure that there is at least 1 device type set
812 if (m_configuration
.deviceTypes
.IsEmpty())
813 m_configuration
.deviceTypes
.Add(CEC_DEVICE_TYPE_RECORDING_DEVICE
);
816 bool bNeedReinit(false);
819 if (SetDeviceTypes(configuration
.deviceTypes
))
821 // the device type changed. just copy the rest, and re-register
823 CLockObject
lock(m_mutex
);
824 m_configuration
.iPhysicalAddress
= configuration
.iPhysicalAddress
;
825 m_configuration
.baseDevice
= configuration
.baseDevice
;
826 m_configuration
.iHDMIPort
= configuration
.iHDMIPort
;
832 // set the physical address
833 SetPhysicalAddress(configuration
);
836 m_processor
->PersistConfiguration(m_configuration
);
839 primary
= GetPrimaryDevice();
841 if (bNeedReinit
|| !primary
|| primary
->GetCurrentPhysicalAddress() != iPA
)
843 // PA or device type changed
844 m_processor
->RegisterClient(this);
846 else if (primary
&& configuration
.bActivateSource
== 1 && bIsRunning
&& !primary
->IsActiveSource())
848 // activate the source if we're not already the active source
849 primary
->ActivateSource();
855 void CCECClient::AddCommand(const cec_command
&command
)
857 CLockObject
lock(m_mutex
);
859 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
);
861 if (m_configuration
.callbacks
&& m_configuration
.callbacks
->CBCecCommand
)
862 m_configuration
.callbacks
->CBCecCommand(m_configuration
.callbackParam
, command
);
863 else if (!m_commandBuffer
.Push(command
))
864 LIB_CEC
->AddLog(CEC_LOG_WARNING
, "command buffer is full");
867 int CCECClient::MenuStateChanged(const cec_menu_state newState
)
869 CLockObject
lock(m_mutex
);
871 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, ">> %s: %s", ToString(CEC_OPCODE_MENU_REQUEST
), ToString(newState
));
873 if (m_configuration
.callbacks
&&
874 m_configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_6_2
&&
875 m_configuration
.callbacks
->CBCecMenuStateChanged
)
876 return m_configuration
.callbacks
->CBCecMenuStateChanged(m_configuration
.callbackParam
, newState
);
881 void CCECClient::Alert(const libcec_alert type
, const libcec_parameter
¶m
)
883 CLockObject
lock(m_mutex
);
885 if (m_configuration
.callbacks
&&
886 m_configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_6_0
&&
887 m_configuration
.callbacks
->CBCecAlert
)
888 m_configuration
.callbacks
->CBCecAlert(m_configuration
.callbackParam
, type
, param
);
891 void CCECClient::AddLog(const cec_log_message
&message
)
893 CLockObject
lock(m_logMutex
);
894 if (m_configuration
.callbacks
&& m_configuration
.callbacks
->CBCecLogMessage
)
895 m_configuration
.callbacks
->CBCecLogMessage(m_configuration
.callbackParam
, message
);
897 m_logBuffer
.Push(message
);
900 void CCECClient::AddKey(void)
902 CLockObject
lock(m_mutex
);
904 if (m_iCurrentButton
!= CEC_USER_CONTROL_CODE_UNKNOWN
)
908 key
.duration
= (unsigned int) (GetTimeMs() - m_buttontime
);
909 key
.keycode
= m_iCurrentButton
;
910 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "key released: %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
);
916 m_iCurrentButton
= CEC_USER_CONTROL_CODE_UNKNOWN
;
922 void CCECClient::AddKey(const cec_keypress
&key
)
924 CLockObject
lock(m_mutex
);
926 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "key pressed: %1x", key
.keycode
);
928 if (m_configuration
.callbacks
&& m_configuration
.callbacks
->CBCecKeyPress
)
929 m_configuration
.callbacks
->CBCecKeyPress(m_configuration
.callbackParam
, key
);
931 m_keyBuffer
.Push(key
);
933 m_iCurrentButton
= key
.duration
> 0 ? CEC_USER_CONTROL_CODE_UNKNOWN
: key
.keycode
;
934 m_buttontime
= key
.duration
> 0 ? 0 : GetTimeMs();
937 void CCECClient::SetCurrentButton(const cec_user_control_code iButtonCode
)
939 // push a keypress to the buffer with 0 duration and another with the duration set when released
942 key
.keycode
= iButtonCode
;
947 void CCECClient::CheckKeypressTimeout(void)
949 if (m_iCurrentButton
!= CEC_USER_CONTROL_CODE_UNKNOWN
&& GetTimeMs() - m_buttontime
> CEC_BUTTON_TIMEOUT
)
952 m_iCurrentButton
= CEC_USER_CONTROL_CODE_UNKNOWN
;
956 void CCECClient::ConfigurationChanged(const libcec_configuration
&config
)
958 CLockObject
lock(m_mutex
);
960 if (m_configuration
.callbacks
&&
961 m_configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_5_0
&&
962 m_configuration
.callbacks
->CBCecConfigurationChanged
&&
963 m_processor
->CECInitialised())
964 m_configuration
.callbacks
->CBCecConfigurationChanged(m_configuration
.callbackParam
, config
);
967 bool CCECClient::EnableCallbacks(void *cbParam
, ICECCallbacks
*callbacks
)
969 CLockObject
lock(m_mutex
);
970 m_configuration
.callbackParam
= cbParam
;
971 m_configuration
.callbacks
= callbacks
;
975 bool CCECClient::PingAdapter(void)
977 return m_processor
? m_processor
->PingAdapter() : false;
980 bool CCECClient::GetNextLogMessage(cec_log_message
*message
)
982 return (m_logBuffer
.Pop(*message
));
985 bool CCECClient::GetNextKeypress(cec_keypress
*key
)
987 return m_keyBuffer
.Pop(*key
);
990 bool CCECClient::GetNextCommand(cec_command
*command
)
992 return m_commandBuffer
.Pop(*command
);
995 CStdString
CCECClient::GetConnectionInfo(void)
998 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
);
999 if (m_configuration
.iFirmwareBuildDate
!= CEC_FW_BUILD_UNKNOWN
)
1001 time_t buildTime
= (time_t)m_configuration
.iFirmwareBuildDate
;
1002 strLog
.AppendFormat(", firmware build date: %s", asctime(gmtime(&buildTime
)));
1003 strLog
= strLog
.Left((int)strLog
.length() - 1); // strip \n added by asctime
1004 strLog
.append(" +0000");
1007 // log the addresses that are being used
1008 if (!m_configuration
.logicalAddresses
.IsEmpty())
1010 strLog
.append(", logical address(es) = ");
1011 CECDEVICEVEC devices
;
1012 m_processor
->GetDevices()->GetByLogicalAddresses(devices
, m_configuration
.logicalAddresses
);
1013 for (CECDEVICEVEC::iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
1014 strLog
.AppendFormat("%s (%X) ", (*it
)->GetLogicalAddressName(), (*it
)->GetLogicalAddress());
1017 if (!CLibCEC::IsValidPhysicalAddress(m_configuration
.iPhysicalAddress
))
1018 strLog
.AppendFormat(", base device: %s (%X), HDMI port number: %d", ToString(m_configuration
.baseDevice
), m_configuration
.baseDevice
, m_configuration
.iHDMIPort
);
1020 strLog
.AppendFormat(", physical address: %04x", m_configuration
.iPhysicalAddress
);
1025 void CCECClient::SetTVVendorOverride(const cec_vendor_id id
)
1028 CLockObject
lock(m_mutex
);
1029 m_configuration
.tvVendor
= id
;
1032 if (id
!= CEC_VENDOR_UNKNOWN
)
1034 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s - vendor id '%s'", __FUNCTION__
, ToString(id
));
1036 CCECBusDevice
*tv
= m_processor
? m_processor
->GetTV() : NULL
;
1038 tv
->SetVendorId((uint64_t)id
);
1042 cec_vendor_id
CCECClient::GetTVVendorOverride(void)
1044 CLockObject
lock(m_mutex
);
1045 return (cec_vendor_id
)m_configuration
.tvVendor
;
1048 void CCECClient::SetOSDName(const CStdString
&strDeviceName
)
1051 CLockObject
lock(m_mutex
);
1052 snprintf(m_configuration
.strDeviceName
, 13, "%s", strDeviceName
.c_str());
1055 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s - using OSD name '%s'", __FUNCTION__
, strDeviceName
.c_str());
1057 CCECBusDevice
*primary
= GetPrimaryDevice();
1058 if (primary
&& !primary
->GetCurrentOSDName().Equals(strDeviceName
))
1060 primary
->SetOSDName(strDeviceName
);
1061 if (m_processor
&& m_processor
->CECInitialised())
1062 primary
->TransmitOSDName(CECDEVICE_TV
);
1066 CStdString
CCECClient::GetOSDName(void)
1068 CLockObject
lock(m_mutex
);
1069 CStdString
strOSDName(m_configuration
.strDeviceName
);
1073 void CCECClient::SetWakeDevices(const cec_logical_addresses
&addresses
)
1075 CLockObject
lock(m_mutex
);
1076 m_configuration
.wakeDevices
= addresses
;
1079 cec_logical_addresses
CCECClient::GetWakeDevices(void)
1081 CLockObject
lock(m_mutex
);
1082 return m_configuration
.wakeDevices
;
1085 bool CCECClient::AutodetectPhysicalAddress(void)
1087 bool bPhysicalAutodetected(false);
1088 uint16_t iPhysicalAddress
= m_processor
? m_processor
->GetDetectedPhysicalAddress() : CEC_INVALID_PHYSICAL_ADDRESS
;
1090 if (CLibCEC::IsValidPhysicalAddress(iPhysicalAddress
))
1092 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s - autodetected physical address '%04X'", __FUNCTION__
, iPhysicalAddress
);
1094 CLockObject
lock(m_mutex
);
1095 m_configuration
.iPhysicalAddress
= iPhysicalAddress
;
1096 m_configuration
.iHDMIPort
= CEC_HDMI_PORTNUMBER_NONE
;
1097 m_configuration
.baseDevice
= CECDEVICE_UNKNOWN
;
1098 bPhysicalAutodetected
= true;
1101 SetDevicePhysicalAddress(iPhysicalAddress
);
1103 return bPhysicalAutodetected
;
1106 void CCECClient::SetClientVersion(const cec_client_version version
)
1108 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s - using client version '%s'", __FUNCTION__
, ToString(version
));
1110 CLockObject
lock(m_mutex
);
1111 m_configuration
.clientVersion
= (uint32_t)version
;
1114 cec_client_version
CCECClient::GetClientVersion(void)
1116 CLockObject
lock(m_mutex
);
1117 return (cec_client_version
)m_configuration
.clientVersion
;
1120 bool CCECClient::SetDeviceTypes(const cec_device_type_list
&deviceTypes
)
1122 bool bNeedReinit(false);
1125 CLockObject
lock(m_mutex
);
1126 bNeedReinit
= m_processor
&& m_processor
->CECInitialised() &&
1127 (m_configuration
.deviceTypes
!= deviceTypes
);
1128 m_configuration
.deviceTypes
= deviceTypes
;
1132 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s - using primary device type '%s'", __FUNCTION__
, ToString(deviceTypes
[0]));
1137 cec_device_type_list
CCECClient::GetDeviceTypes(void)
1139 cec_device_type_list retVal
;
1140 CLockObject
lock(m_mutex
);
1141 retVal
= m_configuration
.deviceTypes
;
1145 bool CCECClient::SetDevicePhysicalAddress(const uint16_t iPhysicalAddress
)
1147 if (!CLibCEC::IsValidPhysicalAddress(iPhysicalAddress
))
1150 // reconfigure all devices
1151 cec_logical_address
reactivateSource(CECDEVICE_UNKNOWN
);
1152 CECDEVICEVEC devices
;
1153 m_processor
->GetDevices()->GetByLogicalAddresses(devices
, m_configuration
.logicalAddresses
);
1154 for (CECDEVICEVEC::iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
1156 // if this device was the active source, reactivate it afterwards
1157 if ((*it
)->IsActiveSource())
1158 reactivateSource
= (*it
)->GetLogicalAddress();
1160 // mark the device as inactive source
1161 if (IsInitialised())
1162 (*it
)->MarkAsInactiveSource();
1164 // set the new physical address
1165 (*it
)->SetPhysicalAddress(iPhysicalAddress
);
1168 if (IsInitialised())
1169 (*it
)->TransmitPhysicalAddress();
1172 // reactivate the previous active source
1173 if (reactivateSource
!= CECDEVICE_UNKNOWN
&&
1174 m_processor
->CECInitialised() &&
1177 CCECBusDevice
*device
= m_processor
->GetDevice(reactivateSource
);
1179 device
->ActivateSource();
1185 bool CCECClient::SwitchMonitoring(bool bEnable
)
1187 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "== %s monitoring mode ==", bEnable
? "enabling" : "disabling");
1192 return m_processor
->UnregisterClient(this);
1195 m_configuration
.bMonitorOnly
= false;
1196 return m_processor
->RegisterClient(this);
1203 bool CCECClient::PollDevice(const cec_logical_address iAddress
)
1205 // try to find the primary device
1206 CCECBusDevice
*primary
= GetPrimaryDevice();
1207 // poll the destination, with the primary as source
1209 return primary
->TransmitPoll(iAddress
);
1211 return m_processor
? m_processor
->PollDevice(iAddress
) : false;
1214 cec_logical_addresses
CCECClient::GetActiveDevices(void)
1216 CECDEVICEVEC activeDevices
;
1218 m_processor
->GetDevices()->GetActive(activeDevices
);
1219 return CCECDeviceMap::ToLogicalAddresses(activeDevices
);
1222 bool CCECClient::IsActiveDevice(const cec_logical_address iAddress
)
1224 cec_logical_addresses activeDevices
= GetActiveDevices();
1225 return activeDevices
.IsSet(iAddress
);
1228 bool CCECClient::IsActiveDeviceType(const cec_device_type type
)
1230 CECDEVICEVEC activeDevices
;
1232 m_processor
->GetDevices()->GetActive(activeDevices
);
1233 CCECDeviceMap::FilterType(type
, activeDevices
);
1234 return !activeDevices
.empty();
1237 cec_logical_address
CCECClient::GetActiveSource(void)
1239 return m_processor
? m_processor
->GetActiveSource() : CECDEVICE_UNKNOWN
;
1242 bool CCECClient::IsActiveSource(const cec_logical_address iAddress
)
1244 return m_processor
? m_processor
->IsActiveSource(iAddress
) : false;
1247 bool CCECClient::SetStreamPath(const cec_logical_address iAddress
)
1249 uint16_t iPhysicalAddress
= GetDevicePhysicalAddress(iAddress
);
1250 if (iPhysicalAddress
!= CEC_INVALID_PHYSICAL_ADDRESS
)
1251 return SetStreamPath(iPhysicalAddress
);
1255 bool CCECClient::SetStreamPath(const uint16_t iPhysicalAddress
)
1257 return m_processor
? m_processor
->SetStreamPath(iPhysicalAddress
) : false;
1260 cec_logical_addresses
CCECClient::GetLogicalAddresses(void)
1262 cec_logical_addresses addresses
;
1263 CLockObject
lock(m_mutex
);
1264 addresses
= m_configuration
.logicalAddresses
;
1268 bool CCECClient::CanPersistConfiguration(void)
1270 return m_processor
? m_processor
->CanPersistConfiguration() : false;
1273 bool CCECClient::PersistConfiguration(const libcec_configuration
&configuration
)
1275 return m_processor
? m_processor
->PersistConfiguration(configuration
) : false;
1278 void CCECClient::RescanActiveDevices(void)
1281 m_processor
->RescanActiveDevices();
1284 bool CCECClient::IsLibCECActiveSource(void)
1286 bool bReturn(false);
1289 cec_logical_address activeSource
= m_processor
->GetActiveSource();
1290 CCECBusDevice
*device
= m_processor
->GetDevice(activeSource
);
1292 bReturn
= device
->IsHandledByLibCEC();