2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011 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/AdapterCommunication.h"
36 #include "adapter/AdapterDetection.h"
37 #include "CECProcessor.h"
38 #include "devices/CECBusDevice.h"
39 #include "platform/util/timeutils.h"
40 #include "platform/util/StdString.h"
44 using namespace PLATFORM
;
46 CLibCEC::CLibCEC(const char *strDeviceName
, cec_device_type_list types
) :
47 m_iStartTime(GetTimeMs()),
48 m_iCurrentButton(CEC_USER_CONTROL_CODE_UNKNOWN
),
53 m_cec
= new CCECProcessor(this, strDeviceName
, types
);
56 CLibCEC::CLibCEC(const char *strDeviceName
, cec_logical_address iLogicalAddress
/* = CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress
/* = CEC_DEFAULT_PHYSICAL_ADDRESS */) :
57 m_iStartTime(GetTimeMs()),
58 m_iCurrentButton(CEC_USER_CONTROL_CODE_UNKNOWN
),
63 m_cec
= new CCECProcessor(this, strDeviceName
, iLogicalAddress
, iPhysicalAddress
);
66 CLibCEC::~CLibCEC(void)
72 bool CLibCEC::Open(const char *strPort
, uint32_t iTimeoutMs
/* = 10000 */)
74 if (m_cec
->IsRunning())
76 AddLog(CEC_LOG_ERROR
, "connection already open");
80 if (!m_cec
->Start(strPort
, 38400, iTimeoutMs
))
82 AddLog(CEC_LOG_ERROR
, "could not start CEC communications");
89 void CLibCEC::Close(void)
95 bool CLibCEC::EnableCallbacks(void *cbParam
, ICECCallbacks
*callbacks
)
97 CLockObject
lock(m_mutex
);
101 m_callbacks
= callbacks
;
106 int8_t CLibCEC::FindAdapters(cec_adapter
*deviceList
, uint8_t iBufSize
, const char *strDevicePath
/* = NULL */)
110 strDebug
.Format("trying to autodetect the com port for device path '%s'", strDevicePath
);
112 strDebug
.Format("trying to autodetect all CEC adapters");
113 AddLog(CEC_LOG_DEBUG
, strDebug
);
115 return CAdapterDetection::FindAdapters(deviceList
, iBufSize
, strDevicePath
);
118 bool CLibCEC::PingAdapter(void)
120 return m_cec
? m_cec
->PingAdapter() : false;
123 bool CLibCEC::StartBootloader(void)
125 return m_cec
? m_cec
->StartBootloader() : false;
128 bool CLibCEC::GetNextLogMessage(cec_log_message
*message
)
130 return (m_logBuffer
.Pop(*message
));
133 bool CLibCEC::GetNextKeypress(cec_keypress
*key
)
135 return m_keyBuffer
.Pop(*key
);
138 bool CLibCEC::GetNextCommand(cec_command
*command
)
140 return m_commandBuffer
.Pop(*command
);
143 bool CLibCEC::Transmit(const cec_command
&data
)
145 return m_cec
? m_cec
->Transmit(data
) : false;
148 bool CLibCEC::SetLogicalAddress(cec_logical_address iLogicalAddress
)
150 return m_cec
? m_cec
->SetLogicalAddress(iLogicalAddress
) : false;
153 bool CLibCEC::SetPhysicalAddress(uint16_t iPhysicalAddress
/* = CEC_DEFAULT_PHYSICAL_ADDRESS */)
155 return m_cec
? m_cec
->SetPhysicalAddress(iPhysicalAddress
) : false;
158 bool CLibCEC::SetHDMIPort(cec_logical_address iBaseDevice
, uint8_t iPort
/* = CEC_DEFAULT_HDMI_PORT */)
160 return m_cec
? m_cec
->SetHDMIPort(iBaseDevice
, iPort
) : false;
163 bool CLibCEC::EnablePhysicalAddressDetection(void)
165 return m_cec
? m_cec
->EnablePhysicalAddressDetection() : false;
168 bool CLibCEC::PowerOnDevices(cec_logical_address address
/* = CECDEVICE_TV */)
170 return m_cec
&& address
>= CECDEVICE_TV
&& address
<= CECDEVICE_BROADCAST
? m_cec
->m_busDevices
[(uint8_t)address
]->PowerOn() : false;
173 bool CLibCEC::StandbyDevices(cec_logical_address address
/* = CECDEVICE_BROADCAST */)
175 return m_cec
&& address
>= CECDEVICE_TV
&& address
<= CECDEVICE_BROADCAST
? m_cec
->m_busDevices
[(uint8_t)address
]->Standby() : false;
178 bool CLibCEC::SetActiveSource(cec_device_type type
/* = CEC_DEVICE_TYPE_RESERVED */)
180 return m_cec
? m_cec
->SetActiveSource(type
) : false;
183 bool CLibCEC::SetActiveView(void)
185 return m_cec
? m_cec
->SetActiveView() : false;
188 bool CLibCEC::SetDeckControlMode(cec_deck_control_mode mode
, bool bSendUpdate
/* = true */)
190 return m_cec
? m_cec
->SetDeckControlMode(mode
, bSendUpdate
) : false;
193 bool CLibCEC::SetDeckInfo(cec_deck_info info
, bool bSendUpdate
/* = true */)
195 return m_cec
? m_cec
->SetDeckInfo(info
, bSendUpdate
) : false;
198 bool CLibCEC::SetInactiveView(void)
200 return m_cec
? m_cec
->TransmitInactiveSource() : false;
203 bool CLibCEC::SetMenuState(cec_menu_state state
, bool bSendUpdate
/* = true */)
205 return m_cec
? m_cec
->SetMenuState(state
, bSendUpdate
) : false;
208 bool CLibCEC::SetOSDString(cec_logical_address iLogicalAddress
, cec_display_control duration
, const char *strMessage
)
210 return m_cec
&& iLogicalAddress
>= CECDEVICE_TV
&& iLogicalAddress
<= CECDEVICE_BROADCAST
?
211 m_cec
->m_busDevices
[m_cec
->GetLogicalAddress()]->TransmitOSDString(iLogicalAddress
, duration
, strMessage
) :
215 bool CLibCEC::SwitchMonitoring(bool bEnable
)
217 return m_cec
? m_cec
->SwitchMonitoring(bEnable
) : false;
220 cec_version
CLibCEC::GetDeviceCecVersion(cec_logical_address iAddress
)
222 if (m_cec
&& iAddress
>= CECDEVICE_TV
&& iAddress
< CECDEVICE_BROADCAST
)
223 return m_cec
->GetDeviceCecVersion(iAddress
);
224 return CEC_VERSION_UNKNOWN
;
227 bool CLibCEC::GetDeviceMenuLanguage(cec_logical_address iAddress
, cec_menu_language
*language
)
229 if (m_cec
&& iAddress
>= CECDEVICE_TV
&& iAddress
< CECDEVICE_BROADCAST
)
230 return m_cec
->GetDeviceMenuLanguage(iAddress
, language
);
234 uint64_t CLibCEC::GetDeviceVendorId(cec_logical_address iAddress
)
236 if (m_cec
&& iAddress
>= CECDEVICE_TV
&& iAddress
< CECDEVICE_BROADCAST
)
237 return m_cec
->GetDeviceVendorId(iAddress
);
241 uint16_t CLibCEC::GetDevicePhysicalAddress(cec_logical_address iAddress
)
243 if (m_cec
&& iAddress
>= CECDEVICE_TV
&& iAddress
< CECDEVICE_BROADCAST
)
244 return m_cec
->GetDevicePhysicalAddress(iAddress
);
248 cec_logical_address
CLibCEC::GetActiveSource(void)
250 return m_cec
? m_cec
->GetActiveSource() : CECDEVICE_UNKNOWN
;
253 bool CLibCEC::IsActiveSource(cec_logical_address iAddress
)
255 if (m_cec
&& iAddress
>= CECDEVICE_TV
&& iAddress
< CECDEVICE_BROADCAST
)
256 return m_cec
->IsActiveSource(iAddress
);
260 cec_power_status
CLibCEC::GetDevicePowerStatus(cec_logical_address iAddress
)
262 if (m_cec
&& iAddress
>= CECDEVICE_TV
&& iAddress
< CECDEVICE_BROADCAST
)
263 return m_cec
->GetDevicePowerStatus(iAddress
);
264 return CEC_POWER_STATUS_UNKNOWN
;
267 bool CLibCEC::PollDevice(cec_logical_address iAddress
)
269 if (m_cec
&& iAddress
>= CECDEVICE_TV
&& iAddress
< CECDEVICE_BROADCAST
)
270 return m_cec
->PollDevice(iAddress
);
274 cec_logical_addresses
CLibCEC::GetActiveDevices(void)
276 cec_logical_addresses addresses
;
279 addresses
= m_cec
->GetActiveDevices();
283 bool CLibCEC::IsActiveDevice(cec_logical_address iAddress
)
285 if (m_cec
&& iAddress
>= CECDEVICE_TV
&& iAddress
< CECDEVICE_BROADCAST
)
286 return m_cec
->IsPresentDevice(iAddress
);
290 bool CLibCEC::IsActiveDeviceType(cec_device_type type
)
292 if (m_cec
&& type
>= CEC_DEVICE_TYPE_TV
&& type
<= CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
293 return m_cec
->IsPresentDeviceType(type
);
297 uint8_t CLibCEC::VolumeUp(bool bSendRelease
/* = true */)
300 return m_cec
->VolumeUp(bSendRelease
);
304 uint8_t CLibCEC::VolumeDown(bool bSendRelease
/* = true */)
307 return m_cec
->VolumeDown(bSendRelease
);
312 uint8_t CLibCEC::MuteAudio(bool bSendRelease
/* = true */)
315 return m_cec
->MuteAudio(bSendRelease
);
319 bool CLibCEC::SendKeypress(cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = true */)
322 return m_cec
->TransmitKeypress(iDestination
, key
, bWait
);
326 bool CLibCEC::SendKeyRelease(cec_logical_address iDestination
, bool bWait
/* = true */)
329 return m_cec
->TransmitKeyRelease(iDestination
, bWait
);
333 cec_osd_name
CLibCEC::GetDeviceOSDName(cec_logical_address iAddress
)
336 retVal
.device
= iAddress
;
340 retVal
= m_cec
->GetDeviceOSDName(iAddress
);
345 void CLibCEC::AddLog(cec_log_level level
, const char *strFormat
, ...)
350 va_start(argList
, strFormat
);
351 strLog
.FormatV(strFormat
, argList
);
354 CLibCEC
*instance
= CLibCEC::GetInstance();
355 CLockObject
lock(instance
->m_mutex
);
357 cec_log_message message
;
358 message
.level
= level
;
359 message
.time
= GetTimeMs() - instance
->m_iStartTime
;
360 snprintf(message
.message
, sizeof(message
.message
), "%s", strLog
.c_str());
362 if (instance
->m_callbacks
)
363 instance
->m_callbacks
->CBCecLogMessage(instance
->m_cbParam
, message
);
365 instance
->m_logBuffer
.Push(message
);
368 void CLibCEC::AddKey(cec_keypress
&key
)
370 CLibCEC
*instance
= CLibCEC::GetInstance();
371 CLockObject
lock(instance
->m_mutex
);
373 AddLog(CEC_LOG_DEBUG
, "key pressed: %1x", key
.keycode
);
375 if (instance
->m_callbacks
)
376 instance
->m_callbacks
->CBCecKeyPress(instance
->m_cbParam
, key
);
378 instance
->m_keyBuffer
.Push(key
);
380 instance
->m_iCurrentButton
= key
.duration
> 0 ? CEC_USER_CONTROL_CODE_UNKNOWN
: key
.keycode
;
381 instance
->m_buttontime
= key
.duration
> 0 ? 0 : GetTimeMs();
384 void CLibCEC::SetCurrentButton(cec_user_control_code iButtonCode
)
386 /* push keypress to the keybuffer with 0 duration.
387 push another press to the keybuffer with the duration set when the button is released */
390 key
.keycode
= iButtonCode
;
395 void CLibCEC::AddKey(void)
397 CLibCEC
*instance
= CLibCEC::GetInstance();
398 CLockObject
lock(instance
->m_mutex
);
400 if (instance
->m_iCurrentButton
!= CEC_USER_CONTROL_CODE_UNKNOWN
)
404 key
.duration
= (unsigned int) (GetTimeMs() - instance
->m_buttontime
);
405 key
.keycode
= instance
->m_iCurrentButton
;
406 AddLog(CEC_LOG_DEBUG
, "key released: %1x", key
.keycode
);
408 if (instance
->m_callbacks
)
409 instance
->m_callbacks
->CBCecKeyPress(instance
->m_cbParam
, key
);
411 instance
->m_keyBuffer
.Push(key
);
412 instance
->m_iCurrentButton
= CEC_USER_CONTROL_CODE_UNKNOWN
;
414 instance
->m_buttontime
= 0;
417 void CLibCEC::AddCommand(const cec_command
&command
)
419 CLibCEC
*instance
= CLibCEC::GetInstance();
420 CLockObject
lock(instance
->m_mutex
);
422 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
);
424 if (instance
->m_callbacks
)
425 instance
->m_callbacks
->CBCecCommand(instance
->m_cbParam
, command
);
426 else if (!instance
->m_commandBuffer
.Push(command
))
427 AddLog(CEC_LOG_WARNING
, "command buffer is full");
430 void CLibCEC::CheckKeypressTimeout(void)
432 if (m_iCurrentButton
!= CEC_USER_CONTROL_CODE_UNKNOWN
&& GetTimeMs() - m_buttontime
> CEC_BUTTON_TIMEOUT
)
435 m_iCurrentButton
= CEC_USER_CONTROL_CODE_UNKNOWN
;
439 static CLibCEC
*g_libCEC_instance(NULL
);
440 CLibCEC
*CLibCEC::GetInstance(void)
442 return g_libCEC_instance
;
445 void CLibCEC::SetInstance(CLibCEC
*instance
)
447 if (g_libCEC_instance
)
448 delete g_libCEC_instance
;
449 g_libCEC_instance
= instance
;
452 void * CECCreate(const char *strDeviceName
, CEC::cec_logical_address iLogicalAddress
/*= CEC::CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress
/* = CEC_DEFAULT_PHYSICAL_ADDRESS */)
454 CLibCEC
*lib
= new CLibCEC(strDeviceName
, iLogicalAddress
, iPhysicalAddress
);
455 CLibCEC::SetInstance(lib
);
456 return static_cast< void* > (lib
);
459 void * CECInit(const char *strDeviceName
, CEC::cec_device_type_list types
)
461 CLibCEC
*lib
= new CLibCEC(strDeviceName
, types
);
462 CLibCEC::SetInstance(lib
);
463 return static_cast< void* > (lib
);
466 void CECDestroy(CEC::ICECAdapter
*UNUSED(instance
))
468 CLibCEC::SetInstance(NULL
);
471 const char *CLibCEC::ToString(const cec_menu_state state
)
473 return m_cec
->ToString(state
);
476 const char *CLibCEC::ToString(const cec_version version
)
478 return m_cec
->ToString(version
);
481 const char *CLibCEC::ToString(const cec_power_status status
)
483 return m_cec
->ToString(status
);
486 const char *CLibCEC::ToString(const cec_logical_address address
)
488 return m_cec
->ToString(address
);
491 const char *CLibCEC::ToString(const cec_deck_control_mode mode
)
493 return m_cec
->ToString(mode
);
496 const char *CLibCEC::ToString(const cec_deck_info status
)
498 return m_cec
->ToString(status
);
501 const char *CLibCEC::ToString(const cec_opcode opcode
)
503 return m_cec
->ToString(opcode
);
506 const char *CLibCEC::ToString(const cec_system_audio_status mode
)
508 return m_cec
->ToString(mode
);
511 const char *CLibCEC::ToString(const cec_audio_status status
)
513 return m_cec
->ToString(status
);
516 const char *CLibCEC::ToString(const cec_vendor_id vendor
)
518 return m_cec
->ToString(vendor
);