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"
40 #include "implementations/CECCommandHandler.h"
43 using namespace PLATFORM
;
45 #define LIB_CEC m_processor->GetLib()
46 #define ToString(x) CCECTypeUtils::ToString(x)
48 CCECClient::CCECClient(CCECProcessor
*processor
, const libcec_configuration
&configuration
) :
49 m_processor(processor
),
50 m_bInitialised(false),
52 m_iCurrentButton(CEC_USER_CONTROL_CODE_UNKNOWN
),
55 // set the initial configuration
56 SetConfiguration(configuration
);
59 CCECClient::~CCECClient(void)
61 // unregister the client
62 if (m_processor
&& IsRegistered())
63 m_processor
->UnregisterClient(this);
66 bool CCECClient::IsInitialised(void)
68 CLockObject
lock(m_mutex
);
69 return m_bInitialised
&& m_processor
;
72 void CCECClient::SetInitialised(bool bSetTo
)
74 CLockObject
lock(m_mutex
);
75 m_bInitialised
= bSetTo
;
78 bool CCECClient::IsRegistered(void)
80 CLockObject
lock(m_mutex
);
81 return m_bRegistered
&& m_processor
;
84 void CCECClient::SetRegistered(bool bSetTo
)
86 CLockObject
lock(m_mutex
);
87 m_bRegistered
= bSetTo
;
90 bool CCECClient::OnRegister(void)
92 // return false if already initialised
96 // get all device we control
98 m_processor
->GetDevices()->GetByLogicalAddresses(devices
, m_configuration
.logicalAddresses
);
100 // return false when no devices were found
103 LIB_CEC
->AddLog(CEC_LOG_WARNING
, "cannot find the primary device (logical address %x)", GetPrimaryLogicalAdddress());
107 // mark as initialised
108 SetInitialised(true);
110 // configure all devices
111 for (CECDEVICEVEC::iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
113 // only set our OSD name for the primary device
114 if ((*it
)->GetLogicalAddress() == GetPrimaryLogicalAdddress())
115 (*it
)->SetOSDName(m_configuration
.strDeviceName
);
117 // set the default menu language for devices we control
118 (*it
)->SetMenuLanguage(m_configuration
.strDeviceLanguage
);
121 // set the physical address
122 SetPhysicalAddress(m_configuration
);
124 // make the primary device the active source if the option is set
125 if (m_configuration
.bActivateSource
== 1)
126 GetPrimaryDevice()->ActivateSource();
131 bool CCECClient::SetHDMIPort(const cec_logical_address iBaseDevice
, const uint8_t iPort
, bool bForce
/* = false */)
135 // limit the HDMI port range to 1-15
136 if (iPort
< CEC_MIN_HDMI_PORTNUMBER
||
137 iPort
> CEC_MAX_HDMI_PORTNUMBER
)
140 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "setting HDMI port to %d on device %s (%d)", iPort
, ToString(iBaseDevice
), (int)iBaseDevice
);
142 // update the configuration
144 CLockObject
lock(m_mutex
);
145 m_configuration
.baseDevice
= iBaseDevice
;
146 m_configuration
.iHDMIPort
= iPort
;
149 // don't continue if the connection isn't opened
150 if (!m_processor
->CECInitialised() && !bForce
)
153 // get the PA of the base device
154 uint16_t iPhysicalAddress(CEC_INVALID_PHYSICAL_ADDRESS
);
155 CCECBusDevice
*baseDevice
= m_processor
->GetDevice(iBaseDevice
);
157 iPhysicalAddress
= baseDevice
->GetPhysicalAddress(GetPrimaryLogicalAdddress());
159 // add our port number
160 if (iPhysicalAddress
<= CEC_MAX_PHYSICAL_ADDRESS
)
162 if (iPhysicalAddress
== 0)
163 iPhysicalAddress
+= 0x1000 * iPort
;
164 else if (iPhysicalAddress
% 0x1000 == 0)
165 iPhysicalAddress
+= 0x100 * iPort
;
166 else if (iPhysicalAddress
% 0x100 == 0)
167 iPhysicalAddress
+= 0x10 * iPort
;
168 else if (iPhysicalAddress
% 0x10 == 0)
169 iPhysicalAddress
+= iPort
;
174 // set the default address when something went wrong
177 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
);
178 iPhysicalAddress
= CEC_DEFAULT_PHYSICAL_ADDRESS
;
181 // and set the address
182 SetDevicePhysicalAddress(iPhysicalAddress
);
184 ConfigurationChanged(m_configuration
);
189 void CCECClient::ResetPhysicalAddress(void)
191 SetPhysicalAddress(m_configuration
);
194 void CCECClient::SetPhysicalAddress(const libcec_configuration
&configuration
)
198 // override the physical address from configuration.iPhysicalAddress if it's set
199 if (!bPASet
&& CLibCEC::IsValidPhysicalAddress(configuration
.iPhysicalAddress
))
200 bPASet
= SetPhysicalAddress(configuration
.iPhysicalAddress
);
202 // try to autodetect the address
203 if (!bPASet
&& m_processor
->CECInitialised())
205 bPASet
= AutodetectPhysicalAddress();
206 m_configuration
.bAutodetectAddress
= bPASet
? 1 : 0;
209 // use the base device + hdmi port settings
211 bPASet
= SetHDMIPort(configuration
.baseDevice
, configuration
.iHDMIPort
);
213 // reset to defaults if something went wrong
216 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s - resetting HDMI port and base device to defaults", __FUNCTION__
);
217 m_configuration
.baseDevice
= CECDEVICE_UNKNOWN
;
218 m_configuration
.iHDMIPort
= CEC_HDMI_PORTNUMBER_NONE
;
222 bool CCECClient::SetPhysicalAddress(const uint16_t iPhysicalAddress
)
224 // update the configuration
226 CLockObject
lock(m_mutex
);
227 if (m_configuration
.iPhysicalAddress
== iPhysicalAddress
)
229 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "physical address unchanged (%04X)", iPhysicalAddress
);
234 m_configuration
.iPhysicalAddress
= iPhysicalAddress
;
235 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "setting physical address to '%04X'", iPhysicalAddress
);
239 // persist the new configuration
240 m_processor
->PersistConfiguration(m_configuration
);
242 // set the physical address for each device
243 SetDevicePhysicalAddress(iPhysicalAddress
);
245 // and send back the updated configuration
246 ConfigurationChanged(m_configuration
);
251 void CCECClient::SetSupportedDeviceTypes(void)
253 cec_device_type_list types
;
256 // get the command handler for the tv
257 CCECCommandHandler
*tvHandler
= m_processor
->GetTV()->GetHandler();
261 // check all device types
262 for (uint8_t iPtr
= 0; iPtr
< 5; iPtr
++)
264 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
267 // get the supported device type. the handler will replace types it doesn't support by one it does support
268 cec_device_type type
= tvHandler
->GetReplacementDeviceType(m_configuration
.deviceTypes
.types
[iPtr
]);
269 if (!types
.IsSet(type
))
273 // set the new type list
274 m_configuration
.deviceTypes
= types
;
277 bool CCECClient::AllocateLogicalAddresses(void)
279 // reset all previous LAs that were set
280 m_configuration
.logicalAddresses
.Clear();
282 // get the supported device types from the command handler of the TV
283 SetSupportedDeviceTypes();
285 // display an error if no device types are set
286 if (m_configuration
.deviceTypes
.IsEmpty())
288 LIB_CEC
->AddLog(CEC_LOG_ERROR
, "no device types given");
292 // check each entry of the list
293 for (uint8_t iPtr
= 0; iPtr
< 5; iPtr
++)
295 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
298 // find an LA for this type
299 cec_logical_address
address(CECDEVICE_UNKNOWN
);
300 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
301 address
= AllocateLogicalAddressRecordingDevice();
302 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_TUNER
)
303 address
= AllocateLogicalAddressTuner();
304 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
)
305 address
= AllocateLogicalAddressPlaybackDevice();
306 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
307 address
= AllocateLogicalAddressAudioSystem();
309 // display an error if no LA could be allocated
310 if (address
== CECDEVICE_UNKNOWN
)
312 LIB_CEC
->AddLog(CEC_LOG_ERROR
, "%s - failed to allocate device '%d', type '%s'", __FUNCTION__
, iPtr
, ToString(m_configuration
.deviceTypes
.types
[iPtr
]));
316 // display the registered LA
317 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s - device '%d', type '%s', LA '%X'", __FUNCTION__
, iPtr
, ToString(m_configuration
.deviceTypes
.types
[iPtr
]), address
);
318 m_configuration
.logicalAddresses
.Set(address
);
324 cec_logical_address
CCECClient::AllocateLogicalAddressRecordingDevice(void)
326 cec_logical_address
retVal(CECDEVICE_UNKNOWN
);
328 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'recording device'");
329 if (m_processor
->TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1
))
330 retVal
= CECDEVICE_RECORDINGDEVICE1
;
331 else if (m_processor
->TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2
))
332 retVal
= CECDEVICE_RECORDINGDEVICE2
;
333 else if (m_processor
->TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3
))
334 retVal
= CECDEVICE_RECORDINGDEVICE3
;
339 cec_logical_address
CCECClient::AllocateLogicalAddressTuner(void)
341 cec_logical_address
retVal(CECDEVICE_UNKNOWN
);
343 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'tuner'");
344 if (m_processor
->TryLogicalAddress(CECDEVICE_TUNER1
))
345 retVal
= CECDEVICE_TUNER1
;
346 else if (m_processor
->TryLogicalAddress(CECDEVICE_TUNER2
))
347 retVal
= CECDEVICE_TUNER2
;
348 else if (m_processor
->TryLogicalAddress(CECDEVICE_TUNER3
))
349 retVal
= CECDEVICE_TUNER3
;
350 else if (m_processor
->TryLogicalAddress(CECDEVICE_TUNER4
))
351 retVal
= CECDEVICE_TUNER4
;
356 cec_logical_address
CCECClient::AllocateLogicalAddressPlaybackDevice(void)
358 cec_logical_address
retVal(CECDEVICE_UNKNOWN
);
360 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'playback device'");
361 if (m_processor
->TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1
))
362 retVal
= CECDEVICE_PLAYBACKDEVICE1
;
363 else if (m_processor
->TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2
))
364 retVal
= CECDEVICE_PLAYBACKDEVICE2
;
365 else if (m_processor
->TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3
))
366 retVal
= CECDEVICE_PLAYBACKDEVICE3
;
371 cec_logical_address
CCECClient::AllocateLogicalAddressAudioSystem(void)
373 cec_logical_address
retVal(CECDEVICE_UNKNOWN
);
375 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'audiosystem'");
376 if (m_processor
->TryLogicalAddress(CECDEVICE_AUDIOSYSTEM
))
377 retVal
= CECDEVICE_AUDIOSYSTEM
;
382 CCECBusDevice
*CCECClient::GetDeviceByType(const cec_device_type type
) const
384 // get all devices that match our logical addresses
385 CECDEVICEVEC devices
;
386 m_processor
->GetDevices()->GetByLogicalAddresses(devices
, m_configuration
.logicalAddresses
);
388 // filter the type we need
389 CCECDeviceMap::FilterType(type
, devices
);
391 return devices
.empty() ?
396 bool CCECClient::ChangeDeviceType(const cec_device_type from
, const cec_device_type to
)
398 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "changing device type '%s' into '%s'", ToString(from
), ToString(to
));
400 CLockObject
lock(m_mutex
);
402 // get the previous device that was allocated
403 CCECBusDevice
*previousDevice
= GetDeviceByType(from
);
407 // change the type in the device type list
408 bool bChanged(false);
409 for (uint8_t iPtr
= 0; iPtr
< 5; iPtr
++)
411 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
414 if (m_configuration
.deviceTypes
.types
[iPtr
] == from
)
417 m_configuration
.deviceTypes
.types
[iPtr
] = to
;
419 else if (m_configuration
.deviceTypes
.types
[iPtr
] == to
&& bChanged
)
421 // ensure that dupes are removed
422 m_configuration
.deviceTypes
.types
[iPtr
] = CEC_DEVICE_TYPE_RESERVED
;
426 // re-register the client to set the new ackmask
427 if (!m_processor
->RegisterClient(this))
433 bool CCECClient::SetLogicalAddress(const cec_logical_address iLogicalAddress
)
435 CLockObject
lock(m_mutex
);
436 if (GetPrimaryLogicalAdddress() != iLogicalAddress
)
439 CLockObject
lock(m_mutex
);
440 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "<< setting primary logical address to %1x", iLogicalAddress
);
441 m_configuration
.logicalAddresses
.primary
= iLogicalAddress
;
442 m_configuration
.logicalAddresses
.Set(iLogicalAddress
);
444 return m_processor
->RegisterClient(this);
450 bool CCECClient::Transmit(const cec_command
&data
)
452 return m_processor
? m_processor
->Transmit(data
) : false;
455 bool CCECClient::SendPowerOnDevices(const cec_logical_address address
/* = CECDEVICE_TV */)
457 // if the broadcast address if set as destination and the client version >=1.5.0, read the wakeDevices setting
458 if (address
== CECDEVICE_BROADCAST
&& m_configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_5_0
)
460 CECDEVICEVEC devices
;
461 m_processor
->GetDevices()->GetWakeDevices(m_configuration
, devices
);
462 return m_processor
->PowerOnDevices(GetPrimaryLogicalAdddress(), devices
);
465 return m_processor
->PowerOnDevice(GetPrimaryLogicalAdddress(), address
);
468 bool CCECClient::SendStandbyDevices(const cec_logical_address address
/* = CECDEVICE_BROADCAST */)
470 // if the broadcast address if set as destination and the client version >=1.5.0, read the standbyDevices setting
471 if (address
== CECDEVICE_BROADCAST
&& m_configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_5_0
)
473 CECDEVICEVEC devices
;
474 m_processor
->GetDevices()->GetPowerOffDevices(m_configuration
, devices
);
475 return m_processor
->StandbyDevices(GetPrimaryLogicalAdddress(), devices
);
478 return m_processor
->StandbyDevice(GetPrimaryLogicalAdddress(), address
);
481 bool CCECClient::SendSetActiveSource(const cec_device_type type
/* = CEC_DEVICE_TYPE_RESERVED */)
483 // get the devices that are controlled by us
484 CECDEVICEVEC devices
;
485 m_processor
->GetDevices()->GetByLogicalAddresses(devices
, m_configuration
.logicalAddresses
);
487 // filter out the device that matches the given type
488 if (type
!= CEC_DEVICE_TYPE_RESERVED
)
489 CCECDeviceMap::FilterType(type
, devices
);
491 // no devices left, re-fetch the list of devices that are controlled by us
493 m_processor
->GetDevices()->GetByLogicalAddresses(devices
, m_configuration
.logicalAddresses
);
495 if (!devices
.empty())
497 // get the first device from the list
498 CCECBusDevice
*device
= *devices
.begin();
501 if (!m_processor
->CECInitialised())
502 device
->MarkAsActiveSource();
503 else if (device
->HasValidPhysicalAddress())
504 return device
->ActivateSource();
510 CCECPlaybackDevice
*CCECClient::GetPlaybackDevice(void)
512 CCECPlaybackDevice
*device(NULL
);
513 CECDEVICEVEC devices
;
515 // get the playback devices
516 m_processor
->GetDevices()->GetByLogicalAddresses(devices
, m_configuration
.logicalAddresses
);
517 CCECDeviceMap::FilterType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
, devices
);
519 // no matches, get the recording devices
522 m_processor
->GetDevices()->GetByLogicalAddresses(devices
, m_configuration
.logicalAddresses
);
523 CCECDeviceMap::FilterType(CEC_DEVICE_TYPE_RECORDING_DEVICE
, devices
);
526 // get the first device that matches, and cast it to CCECPlaybackDevice
527 if (!devices
.empty())
528 device
= (*devices
.begin())->AsPlaybackDevice();
533 cec_logical_address
CCECClient::GetPrimaryLogicalAdddress(void)
535 CLockObject
lock(m_mutex
);
536 return m_configuration
.logicalAddresses
.primary
;
539 CCECBusDevice
*CCECClient::GetPrimaryDevice(void)
541 return m_processor
->GetDevice(GetPrimaryLogicalAdddress());
544 bool CCECClient::SendSetDeckControlMode(const cec_deck_control_mode mode
, bool bSendUpdate
/* = true */)
546 // find a playback device that we control
547 CCECPlaybackDevice
*device
= GetPlaybackDevice();
550 // and set the deck control mode if there is a match
551 device
->SetDeckControlMode(mode
);
553 return device
->TransmitDeckStatus(CECDEVICE_TV
);
561 bool CCECClient::SendSetDeckInfo(const cec_deck_info info
, bool bSendUpdate
/* = true */)
563 // find a playback device that we control
564 CCECPlaybackDevice
*device
= GetPlaybackDevice();
567 // and set the deck status if there is a match
568 device
->SetDeckStatus(info
);
570 return device
->AsPlaybackDevice()->TransmitDeckStatus(CECDEVICE_TV
);
578 bool CCECClient::SendSetMenuState(const cec_menu_state state
, bool bSendUpdate
/* = true */)
580 CECDEVICEVEC devices
;
582 // set the menu state for all devices that are controlled by us
583 m_processor
->GetDevices()->GetByLogicalAddresses(devices
, m_configuration
.logicalAddresses
);
584 for (CECDEVICEVEC::iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
586 (*it
)->SetMenuState(state
);
588 (*it
)->TransmitMenuState(CECDEVICE_TV
);
594 bool CCECClient::SendSetInactiveView(void)
596 CECDEVICEVEC devices
;
598 // mark all devices that are controlled by us as inactive source
599 m_processor
->GetDevices()->GetByLogicalAddresses(devices
, m_configuration
.logicalAddresses
);
600 for (CECDEVICEVEC::iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
602 if ((*it
)->IsActiveSource())
604 (*it
)->MarkAsInactiveSource();
605 return (*it
)->TransmitInactiveSource();
612 bool CCECClient::SendSetOSDString(const cec_logical_address iLogicalAddress
, const cec_display_control duration
, const char *strMessage
)
614 CCECBusDevice
*primary
= GetPrimaryDevice();
616 return primary
->TransmitOSDString(iLogicalAddress
, duration
, strMessage
);
621 cec_version
CCECClient::GetDeviceCecVersion(const cec_logical_address iAddress
)
623 CCECBusDevice
*device
= m_processor
->GetDevice(iAddress
);
625 return device
->GetCecVersion(GetPrimaryLogicalAdddress());
626 return CEC_VERSION_UNKNOWN
;
629 bool CCECClient::GetDeviceMenuLanguage(const cec_logical_address iAddress
, cec_menu_language
&language
)
631 CCECBusDevice
*device
= m_processor
->GetDevice(iAddress
);
634 language
= device
->GetMenuLanguage(GetPrimaryLogicalAdddress());
635 return (strcmp(language
.language
, "???") != 0);
640 cec_osd_name
CCECClient::GetDeviceOSDName(const cec_logical_address iAddress
)
643 retVal
.device
= iAddress
;
646 CCECBusDevice
*device
= m_processor
->GetDevice(iAddress
);
649 CStdString strOSDName
= device
->GetOSDName(GetPrimaryLogicalAdddress());
650 snprintf(retVal
.name
, sizeof(retVal
.name
), "%s", strOSDName
.c_str());
651 retVal
.device
= iAddress
;
657 uint16_t CCECClient::GetDevicePhysicalAddress(const cec_logical_address iAddress
)
659 CCECBusDevice
*device
= m_processor
->GetDevice(iAddress
);
661 return device
->GetPhysicalAddress(GetPrimaryLogicalAdddress());
662 return CEC_INVALID_PHYSICAL_ADDRESS
;
665 cec_power_status
CCECClient::GetDevicePowerStatus(const cec_logical_address iAddress
)
667 CCECBusDevice
*device
= m_processor
->GetDevice(iAddress
);
669 return device
->GetPowerStatus(GetPrimaryLogicalAdddress());
670 return CEC_POWER_STATUS_UNKNOWN
;
673 uint64_t CCECClient::GetDeviceVendorId(const cec_logical_address iAddress
)
675 CCECBusDevice
*device
= m_processor
->GetDevice(iAddress
);
677 return device
->GetVendorId(GetPrimaryLogicalAdddress());
678 return CEC_VENDOR_UNKNOWN
;
681 uint8_t CCECClient::SendVolumeUp(bool bSendRelease
/* = true */)
683 CCECBusDevice
*device
= GetPrimaryDevice();
684 CCECAudioSystem
*audio
= m_processor
->GetAudioSystem();
686 return device
&& audio
&& audio
->IsPresent() ?
687 audio
->VolumeUp(device
->GetLogicalAddress(), bSendRelease
) :
688 (uint8_t)CEC_AUDIO_VOLUME_STATUS_UNKNOWN
;
691 uint8_t CCECClient::SendVolumeDown(bool bSendRelease
/* = true */)
693 CCECBusDevice
*device
= GetPrimaryDevice();
694 CCECAudioSystem
*audio
= m_processor
->GetAudioSystem();
696 return device
&& audio
&& audio
->IsPresent() ?
697 audio
->VolumeDown(device
->GetLogicalAddress(), bSendRelease
) :
698 (uint8_t)CEC_AUDIO_VOLUME_STATUS_UNKNOWN
;
701 uint8_t CCECClient::SendMuteAudio(void)
703 CCECBusDevice
*device
= GetPrimaryDevice();
704 CCECAudioSystem
*audio
= m_processor
->GetAudioSystem();
706 return device
&& audio
&& audio
->IsPresent() ?
707 audio
->MuteAudio(device
->GetLogicalAddress()) :
708 (uint8_t)CEC_AUDIO_VOLUME_STATUS_UNKNOWN
;
711 bool CCECClient::SendKeypress(const cec_logical_address iDestination
, const cec_user_control_code key
, bool bWait
/* = true */)
713 CCECBusDevice
*dest
= m_processor
->GetDevice(iDestination
);
716 dest
->TransmitKeypress(GetPrimaryLogicalAdddress(), key
, bWait
) :
720 bool CCECClient::SendKeyRelease(const cec_logical_address iDestination
, bool bWait
/* = true */)
722 CCECBusDevice
*dest
= m_processor
->GetDevice(iDestination
);
725 dest
->TransmitKeyRelease(GetPrimaryLogicalAdddress(), bWait
) :
729 bool CCECClient::GetCurrentConfiguration(libcec_configuration
&configuration
)
731 CLockObject
lock(m_mutex
);
733 // client version 1.5.0
734 snprintf(configuration
.strDeviceName
, 13, "%s", m_configuration
.strDeviceName
);
735 configuration
.deviceTypes
= m_configuration
.deviceTypes
;
736 configuration
.bAutodetectAddress
= m_configuration
.bAutodetectAddress
;
737 configuration
.iPhysicalAddress
= m_configuration
.iPhysicalAddress
;
738 configuration
.baseDevice
= m_configuration
.baseDevice
;
739 configuration
.iHDMIPort
= m_configuration
.iHDMIPort
;
740 configuration
.clientVersion
= m_configuration
.clientVersion
;
741 configuration
.serverVersion
= m_configuration
.serverVersion
;
742 configuration
.tvVendor
= m_configuration
.tvVendor
;
744 configuration
.bGetSettingsFromROM
= m_configuration
.bGetSettingsFromROM
;
745 configuration
.bUseTVMenuLanguage
= m_configuration
.bUseTVMenuLanguage
;
746 configuration
.bActivateSource
= m_configuration
.bActivateSource
;
747 configuration
.wakeDevices
= m_configuration
.wakeDevices
;
748 configuration
.powerOffDevices
= m_configuration
.powerOffDevices
;
749 configuration
.bPowerOffScreensaver
= m_configuration
.bPowerOffScreensaver
;
750 configuration
.bPowerOffOnStandby
= m_configuration
.bPowerOffOnStandby
;
752 // client version 1.5.1
753 if (configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_5_1
)
754 configuration
.bSendInactiveSource
= m_configuration
.bSendInactiveSource
;
756 // client version 1.5.3
757 if (configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_5_3
)
758 configuration
.logicalAddresses
= m_configuration
.logicalAddresses
;
760 // client version 1.6.0
761 if (configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_6_0
)
763 configuration
.iFirmwareVersion
= m_configuration
.iFirmwareVersion
;
764 configuration
.bPowerOffDevicesOnStandby
= m_configuration
.bPowerOffDevicesOnStandby
;
765 configuration
.bShutdownOnStandby
= m_configuration
.bShutdownOnStandby
;
768 // client version 1.6.2
769 if (configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_6_2
)
771 memcpy(configuration
.strDeviceLanguage
, m_configuration
.strDeviceLanguage
, 3);
772 configuration
.iFirmwareBuildDate
= m_configuration
.iFirmwareBuildDate
;
775 // client version 1.6.3
776 if (configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_6_3
)
778 configuration
.bMonitorOnly
= m_configuration
.bMonitorOnly
;
784 bool CCECClient::SetConfiguration(const libcec_configuration
&configuration
)
786 bool bIsRunning(m_processor
&& m_processor
->CECInitialised());
787 CCECBusDevice
*primary
= bIsRunning
? GetPrimaryDevice() : NULL
;
788 uint16_t iPA
= primary
? primary
->GetCurrentPhysicalAddress() : CEC_INVALID_PHYSICAL_ADDRESS
;
790 // update the callbacks
791 if (configuration
.callbacks
)
792 EnableCallbacks(configuration
.callbackParam
, configuration
.callbacks
);
794 // update the client version
795 SetClientVersion((cec_client_version
)configuration
.clientVersion
);
797 // update the OSD name
798 CStdString
strOSDName(configuration
.strDeviceName
);
799 SetOSDName(strOSDName
);
801 // update the TV vendor override
802 SetTVVendorOverride((cec_vendor_id
)configuration
.tvVendor
);
806 CLockObject
lock(m_mutex
);
807 m_configuration
.bUseTVMenuLanguage
= configuration
.bUseTVMenuLanguage
;
808 m_configuration
.bActivateSource
= configuration
.bActivateSource
;
809 m_configuration
.bGetSettingsFromROM
= configuration
.bGetSettingsFromROM
;
810 m_configuration
.wakeDevices
= configuration
.wakeDevices
;
811 m_configuration
.powerOffDevices
= configuration
.powerOffDevices
;
812 m_configuration
.bPowerOffScreensaver
= configuration
.bPowerOffScreensaver
;
813 m_configuration
.bPowerOffOnStandby
= configuration
.bPowerOffOnStandby
;
815 // client version 1.5.1
816 if (configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_5_1
)
817 m_configuration
.bSendInactiveSource
= configuration
.bSendInactiveSource
;
819 // client version 1.6.0
820 if (configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_6_0
)
822 m_configuration
.bPowerOffDevicesOnStandby
= configuration
.bPowerOffDevicesOnStandby
;
823 m_configuration
.bShutdownOnStandby
= configuration
.bShutdownOnStandby
;
826 // client version 1.6.2
827 if (configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_6_2
)
829 memcpy(m_configuration
.strDeviceLanguage
, configuration
.strDeviceLanguage
, 3);
832 // client version 1.6.3
833 if (configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_6_3
)
835 m_configuration
.bMonitorOnly
= configuration
.bMonitorOnly
;
838 // ensure that there is at least 1 device type set
839 if (m_configuration
.deviceTypes
.IsEmpty())
840 m_configuration
.deviceTypes
.Add(CEC_DEVICE_TYPE_RECORDING_DEVICE
);
843 bool bNeedReinit(false);
846 if (SetDeviceTypes(configuration
.deviceTypes
))
848 // the device type changed. just copy the rest, and re-register
850 CLockObject
lock(m_mutex
);
851 m_configuration
.iPhysicalAddress
= configuration
.iPhysicalAddress
;
852 m_configuration
.baseDevice
= configuration
.baseDevice
;
853 m_configuration
.iHDMIPort
= configuration
.iHDMIPort
;
859 // set the physical address
860 SetPhysicalAddress(configuration
);
863 m_processor
->PersistConfiguration(m_configuration
);
866 primary
= GetPrimaryDevice();
868 if (bNeedReinit
|| !primary
|| primary
->GetCurrentPhysicalAddress() != iPA
)
870 // PA or device type changed
871 m_processor
->RegisterClient(this);
873 else if (primary
&& configuration
.bActivateSource
== 1 && bIsRunning
&& !primary
->IsActiveSource())
875 // activate the source if we're not already the active source
876 primary
->ActivateSource();
882 void CCECClient::AddCommand(const cec_command
&command
)
884 CLockObject
lock(m_mutex
);
886 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
);
888 if (m_configuration
.callbacks
&& m_configuration
.callbacks
->CBCecCommand
)
889 m_configuration
.callbacks
->CBCecCommand(m_configuration
.callbackParam
, command
);
890 else if (!m_commandBuffer
.Push(command
))
891 LIB_CEC
->AddLog(CEC_LOG_WARNING
, "command buffer is full");
894 int CCECClient::MenuStateChanged(const cec_menu_state newState
)
896 CLockObject
lock(m_mutex
);
898 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, ">> %s: %s", ToString(CEC_OPCODE_MENU_REQUEST
), ToString(newState
));
900 if (m_configuration
.callbacks
&&
901 m_configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_6_2
&&
902 m_configuration
.callbacks
->CBCecMenuStateChanged
)
903 return m_configuration
.callbacks
->CBCecMenuStateChanged(m_configuration
.callbackParam
, newState
);
908 void CCECClient::Alert(const libcec_alert type
, const libcec_parameter
¶m
)
910 CLockObject
lock(m_mutex
);
912 if (m_configuration
.callbacks
&&
913 m_configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_6_0
&&
914 m_configuration
.callbacks
->CBCecAlert
)
915 m_configuration
.callbacks
->CBCecAlert(m_configuration
.callbackParam
, type
, param
);
918 void CCECClient::AddLog(const cec_log_message
&message
)
920 CLockObject
lock(m_logMutex
);
921 if (m_configuration
.callbacks
&& m_configuration
.callbacks
->CBCecLogMessage
)
922 m_configuration
.callbacks
->CBCecLogMessage(m_configuration
.callbackParam
, message
);
924 m_logBuffer
.Push(message
);
927 void CCECClient::AddKey(void)
929 CLockObject
lock(m_mutex
);
931 if (m_iCurrentButton
!= CEC_USER_CONTROL_CODE_UNKNOWN
)
935 key
.duration
= (unsigned int) (GetTimeMs() - m_buttontime
);
936 key
.keycode
= m_iCurrentButton
;
937 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "key released: %1x", key
.keycode
);
939 if (m_configuration
.callbacks
&& m_configuration
.callbacks
->CBCecKeyPress
)
940 m_configuration
.callbacks
->CBCecKeyPress(m_configuration
.callbackParam
, key
);
942 m_keyBuffer
.Push(key
);
943 m_iCurrentButton
= CEC_USER_CONTROL_CODE_UNKNOWN
;
949 void CCECClient::AddKey(const cec_keypress
&key
)
951 CLockObject
lock(m_mutex
);
953 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "key pressed: %1x", key
.keycode
);
955 if (m_configuration
.callbacks
&& m_configuration
.callbacks
->CBCecKeyPress
)
956 m_configuration
.callbacks
->CBCecKeyPress(m_configuration
.callbackParam
, key
);
958 m_keyBuffer
.Push(key
);
960 m_iCurrentButton
= key
.duration
> 0 ? CEC_USER_CONTROL_CODE_UNKNOWN
: key
.keycode
;
961 m_buttontime
= key
.duration
> 0 ? 0 : GetTimeMs();
964 void CCECClient::SetCurrentButton(const cec_user_control_code iButtonCode
)
966 // push a keypress to the buffer with 0 duration and another with the duration set when released
969 key
.keycode
= iButtonCode
;
974 void CCECClient::CheckKeypressTimeout(void)
976 if (m_iCurrentButton
!= CEC_USER_CONTROL_CODE_UNKNOWN
&& GetTimeMs() - m_buttontime
> CEC_BUTTON_TIMEOUT
)
979 m_iCurrentButton
= CEC_USER_CONTROL_CODE_UNKNOWN
;
983 void CCECClient::ConfigurationChanged(const libcec_configuration
&config
)
985 CLockObject
lock(m_mutex
);
987 if (m_configuration
.callbacks
&&
988 m_configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_5_0
&&
989 m_configuration
.callbacks
->CBCecConfigurationChanged
&&
990 m_processor
->CECInitialised())
991 m_configuration
.callbacks
->CBCecConfigurationChanged(m_configuration
.callbackParam
, config
);
994 bool CCECClient::EnableCallbacks(void *cbParam
, ICECCallbacks
*callbacks
)
996 CLockObject
lock(m_mutex
);
997 m_configuration
.callbackParam
= cbParam
;
998 m_configuration
.callbacks
= callbacks
;
1002 bool CCECClient::PingAdapter(void)
1004 return m_processor
? m_processor
->PingAdapter() : false;
1007 bool CCECClient::GetNextLogMessage(cec_log_message
*message
)
1009 return (m_logBuffer
.Pop(*message
));
1012 bool CCECClient::GetNextKeypress(cec_keypress
*key
)
1014 return m_keyBuffer
.Pop(*key
);
1017 bool CCECClient::GetNextCommand(cec_command
*command
)
1019 return m_commandBuffer
.Pop(*command
);
1022 CStdString
CCECClient::GetConnectionInfo(void)
1025 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
);
1026 if (m_configuration
.iFirmwareBuildDate
!= CEC_FW_BUILD_UNKNOWN
)
1028 time_t buildTime
= (time_t)m_configuration
.iFirmwareBuildDate
;
1029 strLog
.AppendFormat(", firmware build date: %s", asctime(gmtime(&buildTime
)));
1030 strLog
= strLog
.Left((int)strLog
.length() - 1); // strip \n added by asctime
1031 strLog
.append(" +0000");
1034 // log the addresses that are being used
1035 if (!m_configuration
.logicalAddresses
.IsEmpty())
1037 strLog
.append(", logical address(es) = ");
1038 CECDEVICEVEC devices
;
1039 m_processor
->GetDevices()->GetByLogicalAddresses(devices
, m_configuration
.logicalAddresses
);
1040 for (CECDEVICEVEC::iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
1041 strLog
.AppendFormat("%s (%X) ", (*it
)->GetLogicalAddressName(), (*it
)->GetLogicalAddress());
1044 if (!CLibCEC::IsValidPhysicalAddress(m_configuration
.iPhysicalAddress
))
1045 strLog
.AppendFormat(", base device: %s (%X), HDMI port number: %d", ToString(m_configuration
.baseDevice
), m_configuration
.baseDevice
, m_configuration
.iHDMIPort
);
1047 strLog
.AppendFormat(", physical address: %04x", m_configuration
.iPhysicalAddress
);
1052 void CCECClient::SetTVVendorOverride(const cec_vendor_id id
)
1055 CLockObject
lock(m_mutex
);
1056 m_configuration
.tvVendor
= id
;
1059 if (id
!= CEC_VENDOR_UNKNOWN
)
1061 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s - vendor id '%s'", __FUNCTION__
, ToString(id
));
1063 CCECBusDevice
*tv
= m_processor
? m_processor
->GetTV() : NULL
;
1065 tv
->SetVendorId((uint64_t)id
);
1069 cec_vendor_id
CCECClient::GetTVVendorOverride(void)
1071 CLockObject
lock(m_mutex
);
1072 return (cec_vendor_id
)m_configuration
.tvVendor
;
1075 void CCECClient::SetOSDName(const CStdString
&strDeviceName
)
1078 CLockObject
lock(m_mutex
);
1079 snprintf(m_configuration
.strDeviceName
, 13, "%s", strDeviceName
.c_str());
1082 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s - using OSD name '%s'", __FUNCTION__
, strDeviceName
.c_str());
1084 CCECBusDevice
*primary
= GetPrimaryDevice();
1085 if (primary
&& !primary
->GetCurrentOSDName().Equals(strDeviceName
))
1087 primary
->SetOSDName(strDeviceName
);
1088 if (m_processor
&& m_processor
->CECInitialised())
1089 primary
->TransmitOSDName(CECDEVICE_TV
);
1093 CStdString
CCECClient::GetOSDName(void)
1095 CLockObject
lock(m_mutex
);
1096 CStdString
strOSDName(m_configuration
.strDeviceName
);
1100 void CCECClient::SetWakeDevices(const cec_logical_addresses
&addresses
)
1102 CLockObject
lock(m_mutex
);
1103 m_configuration
.wakeDevices
= addresses
;
1106 cec_logical_addresses
CCECClient::GetWakeDevices(void)
1108 CLockObject
lock(m_mutex
);
1109 return m_configuration
.wakeDevices
;
1112 bool CCECClient::AutodetectPhysicalAddress(void)
1114 bool bPhysicalAutodetected(false);
1115 uint16_t iPhysicalAddress
= m_processor
? m_processor
->GetDetectedPhysicalAddress() : CEC_INVALID_PHYSICAL_ADDRESS
;
1117 if (CLibCEC::IsValidPhysicalAddress(iPhysicalAddress
))
1119 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s - autodetected physical address '%04X'", __FUNCTION__
, iPhysicalAddress
);
1121 CLockObject
lock(m_mutex
);
1122 m_configuration
.iPhysicalAddress
= iPhysicalAddress
;
1123 m_configuration
.iHDMIPort
= CEC_HDMI_PORTNUMBER_NONE
;
1124 m_configuration
.baseDevice
= CECDEVICE_UNKNOWN
;
1125 bPhysicalAutodetected
= true;
1128 SetDevicePhysicalAddress(iPhysicalAddress
);
1130 return bPhysicalAutodetected
;
1133 void CCECClient::SetClientVersion(const cec_client_version version
)
1135 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s - using client version '%s'", __FUNCTION__
, ToString(version
));
1137 CLockObject
lock(m_mutex
);
1138 m_configuration
.clientVersion
= (uint32_t)version
;
1141 cec_client_version
CCECClient::GetClientVersion(void)
1143 CLockObject
lock(m_mutex
);
1144 return (cec_client_version
)m_configuration
.clientVersion
;
1147 bool CCECClient::SetDeviceTypes(const cec_device_type_list
&deviceTypes
)
1149 bool bNeedReinit(false);
1152 CLockObject
lock(m_mutex
);
1153 bNeedReinit
= m_processor
&& m_processor
->CECInitialised() &&
1154 (m_configuration
.deviceTypes
!= deviceTypes
);
1155 m_configuration
.deviceTypes
= deviceTypes
;
1159 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s - using primary device type '%s'", __FUNCTION__
, ToString(deviceTypes
[0]));
1164 cec_device_type_list
CCECClient::GetDeviceTypes(void)
1166 cec_device_type_list retVal
;
1167 CLockObject
lock(m_mutex
);
1168 retVal
= m_configuration
.deviceTypes
;
1172 bool CCECClient::SetDevicePhysicalAddress(const uint16_t iPhysicalAddress
)
1174 if (!CLibCEC::IsValidPhysicalAddress(iPhysicalAddress
))
1177 // reconfigure all devices
1178 cec_logical_address
reactivateSource(CECDEVICE_UNKNOWN
);
1179 CECDEVICEVEC devices
;
1180 m_processor
->GetDevices()->GetByLogicalAddresses(devices
, m_configuration
.logicalAddresses
);
1181 for (CECDEVICEVEC::iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
1183 // if this device was the active source, reactivate it afterwards
1184 if ((*it
)->IsActiveSource())
1185 reactivateSource
= (*it
)->GetLogicalAddress();
1187 // mark the device as inactive source
1188 if (IsInitialised())
1189 (*it
)->MarkAsInactiveSource();
1191 // set the new physical address
1192 (*it
)->SetPhysicalAddress(iPhysicalAddress
);
1195 if (IsInitialised())
1196 (*it
)->TransmitPhysicalAddress();
1199 // reactivate the previous active source
1200 if (reactivateSource
!= CECDEVICE_UNKNOWN
&&
1201 m_processor
->CECInitialised() &&
1204 CCECBusDevice
*device
= m_processor
->GetDevice(reactivateSource
);
1206 device
->ActivateSource();
1212 bool CCECClient::SwitchMonitoring(bool bEnable
)
1214 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "== %s monitoring mode ==", bEnable
? "enabling" : "disabling");
1219 return m_processor
->UnregisterClient(this);
1222 m_configuration
.bMonitorOnly
= false;
1223 return m_processor
->RegisterClient(this);
1230 bool CCECClient::PollDevice(const cec_logical_address iAddress
)
1232 // try to find the primary device
1233 CCECBusDevice
*primary
= GetPrimaryDevice();
1234 // poll the destination, with the primary as source
1236 return primary
->TransmitPoll(iAddress
);
1238 return m_processor
? m_processor
->PollDevice(iAddress
) : false;
1241 cec_logical_addresses
CCECClient::GetActiveDevices(void)
1243 CECDEVICEVEC activeDevices
;
1245 m_processor
->GetDevices()->GetActive(activeDevices
);
1246 return CCECDeviceMap::ToLogicalAddresses(activeDevices
);
1249 bool CCECClient::IsActiveDevice(const cec_logical_address iAddress
)
1251 cec_logical_addresses activeDevices
= GetActiveDevices();
1252 return activeDevices
.IsSet(iAddress
);
1255 bool CCECClient::IsActiveDeviceType(const cec_device_type type
)
1257 CECDEVICEVEC activeDevices
;
1259 m_processor
->GetDevices()->GetActive(activeDevices
);
1260 CCECDeviceMap::FilterType(type
, activeDevices
);
1261 return !activeDevices
.empty();
1264 cec_logical_address
CCECClient::GetActiveSource(void)
1266 return m_processor
? m_processor
->GetActiveSource() : CECDEVICE_UNKNOWN
;
1269 bool CCECClient::IsActiveSource(const cec_logical_address iAddress
)
1271 return m_processor
? m_processor
->IsActiveSource(iAddress
) : false;
1274 bool CCECClient::SetStreamPath(const cec_logical_address iAddress
)
1276 uint16_t iPhysicalAddress
= GetDevicePhysicalAddress(iAddress
);
1277 if (iPhysicalAddress
!= CEC_INVALID_PHYSICAL_ADDRESS
)
1278 return SetStreamPath(iPhysicalAddress
);
1282 bool CCECClient::SetStreamPath(const uint16_t iPhysicalAddress
)
1284 return m_processor
? m_processor
->SetStreamPath(iPhysicalAddress
) : false;
1287 cec_logical_addresses
CCECClient::GetLogicalAddresses(void)
1289 cec_logical_addresses addresses
;
1290 CLockObject
lock(m_mutex
);
1291 addresses
= m_configuration
.logicalAddresses
;
1295 bool CCECClient::CanPersistConfiguration(void)
1297 return m_processor
? m_processor
->CanPersistConfiguration() : false;
1300 bool CCECClient::PersistConfiguration(const libcec_configuration
&configuration
)
1302 return m_processor
? m_processor
->PersistConfiguration(configuration
) : false;
1305 void CCECClient::RescanActiveDevices(void)
1308 m_processor
->RescanActiveDevices();
1311 bool CCECClient::IsLibCECActiveSource(void)
1313 bool bReturn(false);
1316 cec_logical_address activeSource
= m_processor
->GetActiveSource();
1317 CCECBusDevice
*device
= m_processor
->GetDevice(activeSource
);
1319 bReturn
= device
->IsHandledByLibCEC();