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/
35 #include "adapter/USBCECAdapterDetection.h"
36 #include "CECProcessor.h"
37 #include "devices/CECBusDevice.h"
38 #include "platform/util/timeutils.h"
39 #include "platform/util/StdString.h"
43 using namespace PLATFORM
;
45 CLibCEC::CLibCEC(const char *strDeviceName
, cec_device_type_list types
, uint16_t iPhysicalAddress
/* = 0 */) :
46 m_iStartTime(GetTimeMs()),
47 m_iCurrentButton(CEC_USER_CONTROL_CODE_UNKNOWN
),
52 m_cec
= new CCECProcessor(this, strDeviceName
, types
, iPhysicalAddress
);
55 CLibCEC::CLibCEC(libcec_configuration
*configuration
) :
56 m_iStartTime(GetTimeMs()),
57 m_iCurrentButton(CEC_USER_CONTROL_CODE_UNKNOWN
),
59 m_callbacks(configuration
->callbacks
),
60 m_cbParam(configuration
->callbackParam
)
62 m_cec
= new CCECProcessor(this, configuration
);
65 CLibCEC::~CLibCEC(void)
71 bool CLibCEC::Open(const char *strPort
, uint32_t iTimeoutMs
/* = 10000 */)
73 if (m_cec
->IsRunning())
75 AddLog(CEC_LOG_ERROR
, "connection already open");
79 if (!m_cec
->Start(strPort
, 38400, iTimeoutMs
))
81 AddLog(CEC_LOG_ERROR
, "could not start CEC communications");
88 void CLibCEC::Close(void)
94 bool CLibCEC::EnableCallbacks(void *cbParam
, ICECCallbacks
*callbacks
)
96 CLockObject
lock(m_mutex
);
100 m_callbacks
= callbacks
;
105 int8_t CLibCEC::FindAdapters(cec_adapter
*deviceList
, uint8_t iBufSize
, const char *strDevicePath
/* = NULL */)
109 strDebug
.Format("trying to autodetect the com port for device path '%s'", strDevicePath
);
111 strDebug
.Format("trying to autodetect all CEC adapters");
112 AddLog(CEC_LOG_DEBUG
, strDebug
);
114 return CUSBCECAdapterDetection::FindAdapters(deviceList
, iBufSize
, strDevicePath
);
117 bool CLibCEC::PingAdapter(void)
119 return m_cec
? m_cec
->PingAdapter() : false;
122 bool CLibCEC::StartBootloader(void)
124 return m_cec
? m_cec
->StartBootloader() : false;
127 bool CLibCEC::GetNextLogMessage(cec_log_message
*message
)
129 return (m_logBuffer
.Pop(*message
));
132 bool CLibCEC::GetNextKeypress(cec_keypress
*key
)
134 return m_keyBuffer
.Pop(*key
);
137 bool CLibCEC::GetNextCommand(cec_command
*command
)
139 return m_commandBuffer
.Pop(*command
);
142 bool CLibCEC::Transmit(const cec_command
&data
)
144 return m_cec
? m_cec
->Transmit(data
) : false;
147 bool CLibCEC::SetLogicalAddress(cec_logical_address iLogicalAddress
)
149 return m_cec
? m_cec
->SetLogicalAddress(iLogicalAddress
) : false;
152 bool CLibCEC::SetPhysicalAddress(uint16_t iPhysicalAddress
/* = CEC_DEFAULT_PHYSICAL_ADDRESS */)
154 return m_cec
? m_cec
->SetPhysicalAddress(iPhysicalAddress
) : false;
157 bool CLibCEC::SetHDMIPort(cec_logical_address iBaseDevice
, uint8_t iPort
/* = CEC_DEFAULT_HDMI_PORT */)
159 return m_cec
? m_cec
->SetHDMIPort(iBaseDevice
, iPort
) : false;
162 bool CLibCEC::EnablePhysicalAddressDetection(void)
164 return m_cec
? m_cec
->EnablePhysicalAddressDetection() : false;
167 bool CLibCEC::PowerOnDevices(cec_logical_address address
/* = CECDEVICE_TV */)
169 return m_cec
&& address
>= CECDEVICE_TV
&& address
<= CECDEVICE_BROADCAST
? m_cec
->PowerOnDevices(address
) : false;
172 bool CLibCEC::StandbyDevices(cec_logical_address address
/* = CECDEVICE_BROADCAST */)
174 return m_cec
&& address
>= CECDEVICE_TV
&& address
<= CECDEVICE_BROADCAST
? m_cec
->StandbyDevices(address
) : false;
177 bool CLibCEC::SetActiveSource(cec_device_type type
/* = CEC_DEVICE_TYPE_RESERVED */)
179 return m_cec
? m_cec
->SetActiveSource(type
) : false;
182 bool CLibCEC::SetActiveView(void)
184 return m_cec
? m_cec
->SetActiveView() : false;
187 bool CLibCEC::SetDeckControlMode(cec_deck_control_mode mode
, bool bSendUpdate
/* = true */)
189 return m_cec
? m_cec
->SetDeckControlMode(mode
, bSendUpdate
) : false;
192 bool CLibCEC::SetDeckInfo(cec_deck_info info
, bool bSendUpdate
/* = true */)
194 return m_cec
? m_cec
->SetDeckInfo(info
, bSendUpdate
) : false;
197 bool CLibCEC::SetInactiveView(void)
199 return m_cec
? m_cec
->TransmitInactiveSource() : false;
202 bool CLibCEC::SetMenuState(cec_menu_state state
, bool bSendUpdate
/* = true */)
204 return m_cec
? m_cec
->SetMenuState(state
, bSendUpdate
) : false;
207 bool CLibCEC::SetOSDString(cec_logical_address iLogicalAddress
, cec_display_control duration
, const char *strMessage
)
209 return m_cec
&& iLogicalAddress
>= CECDEVICE_TV
&& iLogicalAddress
<= CECDEVICE_BROADCAST
?
210 m_cec
->m_busDevices
[m_cec
->GetLogicalAddress()]->TransmitOSDString(iLogicalAddress
, duration
, strMessage
) :
214 bool CLibCEC::SwitchMonitoring(bool bEnable
)
216 return m_cec
? m_cec
->SwitchMonitoring(bEnable
) : false;
219 cec_version
CLibCEC::GetDeviceCecVersion(cec_logical_address iAddress
)
221 if (m_cec
&& iAddress
>= CECDEVICE_TV
&& iAddress
< CECDEVICE_BROADCAST
)
222 return m_cec
->GetDeviceCecVersion(iAddress
);
223 return CEC_VERSION_UNKNOWN
;
226 bool CLibCEC::GetDeviceMenuLanguage(cec_logical_address iAddress
, cec_menu_language
*language
)
228 if (m_cec
&& iAddress
>= CECDEVICE_TV
&& iAddress
< CECDEVICE_BROADCAST
)
229 return m_cec
->GetDeviceMenuLanguage(iAddress
, language
);
233 uint64_t CLibCEC::GetDeviceVendorId(cec_logical_address iAddress
)
235 if (m_cec
&& iAddress
>= CECDEVICE_TV
&& iAddress
< CECDEVICE_BROADCAST
)
236 return m_cec
->GetDeviceVendorId(iAddress
);
240 uint16_t CLibCEC::GetDevicePhysicalAddress(cec_logical_address iAddress
)
242 if (m_cec
&& iAddress
>= CECDEVICE_TV
&& iAddress
< CECDEVICE_BROADCAST
)
243 return m_cec
->GetDevicePhysicalAddress(iAddress
);
247 cec_logical_address
CLibCEC::GetActiveSource(void)
249 return m_cec
? m_cec
->GetActiveSource() : CECDEVICE_UNKNOWN
;
252 bool CLibCEC::IsActiveSource(cec_logical_address iAddress
)
254 if (m_cec
&& iAddress
>= CECDEVICE_TV
&& iAddress
< CECDEVICE_BROADCAST
)
255 return m_cec
->IsActiveSource(iAddress
);
259 cec_power_status
CLibCEC::GetDevicePowerStatus(cec_logical_address iAddress
)
261 if (m_cec
&& iAddress
>= CECDEVICE_TV
&& iAddress
< CECDEVICE_BROADCAST
)
262 return m_cec
->GetDevicePowerStatus(iAddress
);
263 return CEC_POWER_STATUS_UNKNOWN
;
266 bool CLibCEC::PollDevice(cec_logical_address iAddress
)
268 if (m_cec
&& iAddress
>= CECDEVICE_TV
&& iAddress
< CECDEVICE_BROADCAST
)
269 return m_cec
->PollDevice(iAddress
);
273 cec_logical_addresses
CLibCEC::GetActiveDevices(void)
275 cec_logical_addresses addresses
;
278 addresses
= m_cec
->GetActiveDevices();
282 bool CLibCEC::IsActiveDevice(cec_logical_address iAddress
)
284 if (m_cec
&& iAddress
>= CECDEVICE_TV
&& iAddress
< CECDEVICE_BROADCAST
)
285 return m_cec
->IsPresentDevice(iAddress
);
289 bool CLibCEC::IsActiveDeviceType(cec_device_type type
)
291 if (m_cec
&& type
>= CEC_DEVICE_TYPE_TV
&& type
<= CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
292 return m_cec
->IsPresentDeviceType(type
);
296 uint8_t CLibCEC::VolumeUp(bool bSendRelease
/* = true */)
299 return m_cec
->VolumeUp(bSendRelease
);
303 uint8_t CLibCEC::VolumeDown(bool bSendRelease
/* = true */)
306 return m_cec
->VolumeDown(bSendRelease
);
311 uint8_t CLibCEC::MuteAudio(bool bSendRelease
/* = true */)
314 return m_cec
->MuteAudio(bSendRelease
);
318 bool CLibCEC::SendKeypress(cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = true */)
321 return m_cec
->TransmitKeypress(iDestination
, key
, bWait
);
325 bool CLibCEC::SendKeyRelease(cec_logical_address iDestination
, bool bWait
/* = true */)
328 return m_cec
->TransmitKeyRelease(iDestination
, bWait
);
332 cec_osd_name
CLibCEC::GetDeviceOSDName(cec_logical_address iAddress
)
335 retVal
.device
= iAddress
;
339 retVal
= m_cec
->GetDeviceOSDName(iAddress
);
344 void CLibCEC::AddLog(const cec_log_level level
, const char *strFormat
, ...)
349 va_start(argList
, strFormat
);
350 strLog
.FormatV(strFormat
, argList
);
353 CLibCEC
*instance
= CLibCEC::GetInstance();
354 CLockObject
lock(instance
->m_mutex
);
356 cec_log_message message
;
357 message
.level
= level
;
358 message
.time
= GetTimeMs() - instance
->m_iStartTime
;
359 snprintf(message
.message
, sizeof(message
.message
), "%s", strLog
.c_str());
361 if (instance
->m_callbacks
)
362 instance
->m_callbacks
->CBCecLogMessage(instance
->m_cbParam
, message
);
364 instance
->m_logBuffer
.Push(message
);
367 void CLibCEC::AddKey(const cec_keypress
&key
)
369 CLibCEC
*instance
= CLibCEC::GetInstance();
370 CLockObject
lock(instance
->m_mutex
);
372 AddLog(CEC_LOG_DEBUG
, "key pressed: %1x", key
.keycode
);
374 if (instance
->m_callbacks
)
375 instance
->m_callbacks
->CBCecKeyPress(instance
->m_cbParam
, key
);
377 instance
->m_keyBuffer
.Push(key
);
379 instance
->m_iCurrentButton
= key
.duration
> 0 ? CEC_USER_CONTROL_CODE_UNKNOWN
: key
.keycode
;
380 instance
->m_buttontime
= key
.duration
> 0 ? 0 : GetTimeMs();
383 void CLibCEC::ConfigurationChanged(const libcec_configuration
&config
)
385 CLibCEC
*instance
= CLibCEC::GetInstance();
386 CLockObject
lock(instance
->m_mutex
);
388 if (instance
->m_callbacks
&&
389 config
.clientVersion
>= CEC_CLIENT_VERSION_1_5_0
&&
390 instance
->m_callbacks
->CBCecConfigurationChanged
!= NULL
)
391 instance
->m_callbacks
->CBCecConfigurationChanged(instance
->m_cbParam
, config
);
394 void CLibCEC::SetCurrentButton(cec_user_control_code iButtonCode
)
396 /* push keypress to the keybuffer with 0 duration.
397 push another press to the keybuffer with the duration set when the button is released */
400 key
.keycode
= iButtonCode
;
405 void CLibCEC::AddKey(void)
407 CLibCEC
*instance
= CLibCEC::GetInstance();
408 CLockObject
lock(instance
->m_mutex
);
410 if (instance
->m_iCurrentButton
!= CEC_USER_CONTROL_CODE_UNKNOWN
)
414 key
.duration
= (unsigned int) (GetTimeMs() - instance
->m_buttontime
);
415 key
.keycode
= instance
->m_iCurrentButton
;
416 AddLog(CEC_LOG_DEBUG
, "key released: %1x", key
.keycode
);
418 if (instance
->m_callbacks
)
419 instance
->m_callbacks
->CBCecKeyPress(instance
->m_cbParam
, key
);
421 instance
->m_keyBuffer
.Push(key
);
422 instance
->m_iCurrentButton
= CEC_USER_CONTROL_CODE_UNKNOWN
;
424 instance
->m_buttontime
= 0;
427 void CLibCEC::AddCommand(const cec_command
&command
)
429 CLibCEC
*instance
= CLibCEC::GetInstance();
430 CLockObject
lock(instance
->m_mutex
);
432 AddLog(CEC_LOG_NOTICE
, ">> %s (%X) -> %s (%X): %s (%2X)", instance
->m_cec
->ToString(command
.initiator
), command
.initiator
, instance
->m_cec
->ToString(command
.destination
), command
.destination
, instance
->m_cec
->ToString(command
.opcode
), command
.opcode
);
434 if (instance
->m_callbacks
)
435 instance
->m_callbacks
->CBCecCommand(instance
->m_cbParam
, command
);
436 else if (!instance
->m_commandBuffer
.Push(command
))
437 AddLog(CEC_LOG_WARNING
, "command buffer is full");
440 void CLibCEC::CheckKeypressTimeout(void)
442 if (m_iCurrentButton
!= CEC_USER_CONTROL_CODE_UNKNOWN
&& GetTimeMs() - m_buttontime
> CEC_BUTTON_TIMEOUT
)
445 m_iCurrentButton
= CEC_USER_CONTROL_CODE_UNKNOWN
;
449 bool CLibCEC::SetStreamPath(cec_logical_address iAddress
)
451 uint16_t iPhysicalAddress
= GetDevicePhysicalAddress(iAddress
);
452 if (iPhysicalAddress
!= 0xFFFF)
453 return SetStreamPath(iPhysicalAddress
);
457 bool CLibCEC::SetStreamPath(uint16_t iPhysicalAddress
)
459 return m_cec
->SetStreamPath(iPhysicalAddress
);
462 cec_logical_addresses
CLibCEC::GetLogicalAddresses(void)
464 cec_logical_addresses addr
= m_cec
->GetLogicalAddresses();
468 static CLibCEC
*g_libCEC_instance(NULL
);
469 CLibCEC
*CLibCEC::GetInstance(void)
471 return g_libCEC_instance
;
474 void CLibCEC::SetInstance(CLibCEC
*instance
)
476 if (g_libCEC_instance
)
477 delete g_libCEC_instance
;
478 g_libCEC_instance
= instance
;
481 void * CECInit(const char *strDeviceName
, CEC::cec_device_type_list types
, uint16_t iPhysicalAddress
/* = 0 */)
483 CLibCEC
*lib
= new CLibCEC(strDeviceName
, types
);
484 CLibCEC::SetInstance(lib
);
485 return static_cast< void* > (lib
);
488 void * CECInitialise(libcec_configuration
*configuration
)
490 CLibCEC
*lib
= new CLibCEC(configuration
);
491 CLibCEC::SetInstance(lib
);
492 return static_cast< void* > (lib
);
495 void CECDestroy(CEC::ICECAdapter
*UNUSED(instance
))
497 CLibCEC::SetInstance(NULL
);
500 const char *CLibCEC::ToString(const cec_menu_state state
)
502 return m_cec
->ToString(state
);
505 const char *CLibCEC::ToString(const cec_version version
)
507 return m_cec
->ToString(version
);
510 const char *CLibCEC::ToString(const cec_power_status status
)
512 return m_cec
->ToString(status
);
515 const char *CLibCEC::ToString(const cec_logical_address address
)
517 return m_cec
->ToString(address
);
520 const char *CLibCEC::ToString(const cec_deck_control_mode mode
)
522 return m_cec
->ToString(mode
);
525 const char *CLibCEC::ToString(const cec_deck_info status
)
527 return m_cec
->ToString(status
);
530 const char *CLibCEC::ToString(const cec_opcode opcode
)
532 return m_cec
->ToString(opcode
);
535 const char *CLibCEC::ToString(const cec_system_audio_status mode
)
537 return m_cec
->ToString(mode
);
540 const char *CLibCEC::ToString(const cec_audio_status status
)
542 return m_cec
->ToString(status
);
545 const char *CLibCEC::ToString(const cec_vendor_id vendor
)
547 return m_cec
->ToString(vendor
);
550 const char *CLibCEC::ToString(const cec_client_version version
)
552 return m_cec
->ToString(version
);
555 const char *CLibCEC::ToString(const cec_server_version version
)
557 return m_cec
->ToString(version
);
560 bool CLibCEC::GetCurrentConfiguration(libcec_configuration
*configuration
)
562 return m_cec
->GetCurrentConfiguration(configuration
);
565 bool CLibCEC::SetConfiguration(const libcec_configuration
*configuration
)
567 return m_cec
->SetConfiguration(configuration
);
570 bool CLibCEC::CanPersistConfiguration(void)
572 return m_cec
->CanPersistConfiguration();
575 bool CLibCEC::PersistConfiguration(libcec_configuration
*configuration
)
577 return m_cec
->PersistConfiguration(configuration
);
580 void CLibCEC::RescanActiveDevices(void)
582 return m_cec
->RescanActiveDevices();
585 bool CLibCEC::IsLibCECActiveSource(void)
588 m_cec
->m_busDevices
[m_cec
->GetActiveSource()]->GetStatus(false) == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
: