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 "AdapterCommunication.h"
36 #include "AdapterDetection.h"
37 #include "CECProcessor.h"
38 #include "devices/CECBusDevice.h"
39 #include "util/StdString.h"
40 #include "platform/timeutils.h"
45 CLibCEC::CLibCEC(const char *strDeviceName
, cec_device_type_list types
) :
46 m_iStartTime(GetTimeMs()),
47 m_iCurrentButton(CEC_USER_CONTROL_CODE_UNKNOWN
),
51 m_cec
= new CCECProcessor(this, strDeviceName
, types
);
54 CLibCEC::CLibCEC(const char *strDeviceName
, cec_logical_address iLogicalAddress
/* = CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress
/* = CEC_DEFAULT_PHYSICAL_ADDRESS */) :
55 m_iStartTime(GetTimeMs()),
56 m_iCurrentButton(CEC_USER_CONTROL_CODE_UNKNOWN
),
60 m_cec
= new CCECProcessor(this, strDeviceName
, iLogicalAddress
, iPhysicalAddress
);
63 CLibCEC::~CLibCEC(void)
69 bool CLibCEC::Open(const char *strPort
, uint32_t iTimeoutMs
/* = 10000 */)
71 if (m_cec
->IsRunning())
73 AddLog(CEC_LOG_ERROR
, "connection already open");
77 if (!m_cec
->Start(strPort
, 38400, iTimeoutMs
))
79 AddLog(CEC_LOG_ERROR
, "could not start CEC communications");
86 void CLibCEC::Close(void)
92 bool CLibCEC::EnableCallbacks(ICECCallbacks
*callbacks
)
94 CLockObject
lock(&m_mutex
);
96 m_callbacks
= callbacks
;
100 int8_t CLibCEC::FindAdapters(cec_adapter
*deviceList
, uint8_t iBufSize
, const char *strDevicePath
/* = NULL */)
104 strDebug
.Format("trying to autodetect the com port for device path '%s'", strDevicePath
);
106 strDebug
.Format("trying to autodetect all CEC adapters");
107 AddLog(CEC_LOG_DEBUG
, strDebug
);
109 return CAdapterDetection::FindAdapters(deviceList
, iBufSize
, strDevicePath
);
112 bool CLibCEC::PingAdapter(void)
114 return m_cec
? m_cec
->PingAdapter() : false;
117 bool CLibCEC::StartBootloader(void)
119 return m_cec
? m_cec
->StartBootloader() : false;
122 bool CLibCEC::GetNextLogMessage(cec_log_message
*message
)
124 return (m_logBuffer
.Pop(*message
));
127 bool CLibCEC::GetNextKeypress(cec_keypress
*key
)
129 return m_keyBuffer
.Pop(*key
);
132 bool CLibCEC::GetNextCommand(cec_command
*command
)
134 return m_commandBuffer
.Pop(*command
);
137 bool CLibCEC::Transmit(const cec_command
&data
)
139 return m_cec
? m_cec
->Transmit(data
) : false;
142 bool CLibCEC::SetLogicalAddress(cec_logical_address iLogicalAddress
)
144 return m_cec
? m_cec
->SetLogicalAddress(iLogicalAddress
) : false;
147 bool CLibCEC::SetPhysicalAddress(uint16_t iPhysicalAddress
/* = CEC_DEFAULT_PHYSICAL_ADDRESS */)
149 return m_cec
? m_cec
->SetPhysicalAddress(iPhysicalAddress
) : false;
152 bool CLibCEC::SetHDMIPort(cec_logical_address iBaseDevice
, uint8_t iPort
/* = CEC_DEFAULT_HDMI_PORT */)
154 return m_cec
? m_cec
->SetHDMIPort(iBaseDevice
, iPort
) : false;
157 bool CLibCEC::EnablePhysicalAddressDetection(void)
159 return m_cec
? m_cec
->EnablePhysicalAddressDetection() : false;
162 bool CLibCEC::PowerOnDevices(cec_logical_address address
/* = CECDEVICE_TV */)
164 return m_cec
&& address
>= CECDEVICE_TV
&& address
<= CECDEVICE_BROADCAST
? m_cec
->m_busDevices
[(uint8_t)address
]->PowerOn() : false;
167 bool CLibCEC::StandbyDevices(cec_logical_address address
/* = CECDEVICE_BROADCAST */)
169 return m_cec
&& address
>= CECDEVICE_TV
&& address
<= CECDEVICE_BROADCAST
? m_cec
->m_busDevices
[(uint8_t)address
]->Standby() : false;
172 bool CLibCEC::SetActiveSource(cec_device_type type
/* = CEC_DEVICE_TYPE_RESERVED */)
174 return m_cec
? m_cec
->SetActiveSource(type
) : false;
177 bool CLibCEC::SetActiveView(void)
179 return m_cec
? m_cec
->SetActiveView() : false;
182 bool CLibCEC::SetDeckControlMode(cec_deck_control_mode mode
, bool bSendUpdate
/* = true */)
184 return m_cec
? m_cec
->SetDeckControlMode(mode
, bSendUpdate
) : false;
187 bool CLibCEC::SetDeckInfo(cec_deck_info info
, bool bSendUpdate
/* = true */)
189 return m_cec
? m_cec
->SetDeckInfo(info
, bSendUpdate
) : false;
192 bool CLibCEC::SetInactiveView(void)
194 return m_cec
? m_cec
->TransmitInactiveSource() : false;
197 bool CLibCEC::SetMenuState(cec_menu_state state
, bool bSendUpdate
/* = true */)
199 return m_cec
? m_cec
->SetMenuState(state
, bSendUpdate
) : false;
202 bool CLibCEC::SetOSDString(cec_logical_address iLogicalAddress
, cec_display_control duration
, const char *strMessage
)
204 return m_cec
&& iLogicalAddress
>= CECDEVICE_TV
&& iLogicalAddress
<= CECDEVICE_BROADCAST
?
205 m_cec
->m_busDevices
[m_cec
->GetLogicalAddress()]->TransmitOSDString(iLogicalAddress
, duration
, strMessage
) :
209 bool CLibCEC::SwitchMonitoring(bool bEnable
)
211 return m_cec
? m_cec
->SwitchMonitoring(bEnable
) : false;
214 cec_version
CLibCEC::GetDeviceCecVersion(cec_logical_address iAddress
)
216 if (m_cec
&& iAddress
>= CECDEVICE_TV
&& iAddress
< CECDEVICE_BROADCAST
)
217 return m_cec
->GetDeviceCecVersion(iAddress
);
218 return CEC_VERSION_UNKNOWN
;
221 bool CLibCEC::GetDeviceMenuLanguage(cec_logical_address iAddress
, cec_menu_language
*language
)
223 if (m_cec
&& iAddress
>= CECDEVICE_TV
&& iAddress
< CECDEVICE_BROADCAST
)
224 return m_cec
->GetDeviceMenuLanguage(iAddress
, language
);
228 uint64_t CLibCEC::GetDeviceVendorId(cec_logical_address iAddress
)
230 if (m_cec
&& iAddress
>= CECDEVICE_TV
&& iAddress
< CECDEVICE_BROADCAST
)
231 return m_cec
->GetDeviceVendorId(iAddress
);
235 uint16_t CLibCEC::GetDevicePhysicalAddress(cec_logical_address iAddress
)
237 if (m_cec
&& iAddress
>= CECDEVICE_TV
&& iAddress
< CECDEVICE_BROADCAST
)
238 return m_cec
->GetDevicePhysicalAddress(iAddress
);
242 cec_logical_address
CLibCEC::GetActiveSource(void)
244 return m_cec
? m_cec
->GetActiveSource() : CECDEVICE_UNKNOWN
;
247 bool CLibCEC::IsActiveSource(cec_logical_address iAddress
)
249 if (m_cec
&& iAddress
>= CECDEVICE_TV
&& iAddress
< CECDEVICE_BROADCAST
)
250 return m_cec
->IsActiveSource(iAddress
);
254 cec_power_status
CLibCEC::GetDevicePowerStatus(cec_logical_address iAddress
)
256 if (m_cec
&& iAddress
>= CECDEVICE_TV
&& iAddress
< CECDEVICE_BROADCAST
)
257 return m_cec
->GetDevicePowerStatus(iAddress
);
258 return CEC_POWER_STATUS_UNKNOWN
;
261 bool CLibCEC::PollDevice(cec_logical_address iAddress
)
263 if (m_cec
&& iAddress
>= CECDEVICE_TV
&& iAddress
< CECDEVICE_BROADCAST
)
264 return m_cec
->PollDevice(iAddress
);
268 cec_logical_addresses
CLibCEC::GetActiveDevices(void)
270 cec_logical_addresses addresses
;
273 addresses
= m_cec
->GetActiveDevices();
277 bool CLibCEC::IsActiveDevice(cec_logical_address iAddress
)
279 if (m_cec
&& iAddress
>= CECDEVICE_TV
&& iAddress
< CECDEVICE_BROADCAST
)
280 return m_cec
->IsPresentDevice(iAddress
);
284 bool CLibCEC::IsActiveDeviceType(cec_device_type type
)
286 if (m_cec
&& type
>= CEC_DEVICE_TYPE_TV
&& type
<= CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
287 return m_cec
->IsPresentDeviceType(type
);
291 uint8_t CLibCEC::VolumeUp(bool bSendRelease
/* = true */)
294 return m_cec
->VolumeUp(bSendRelease
);
298 uint8_t CLibCEC::VolumeDown(bool bSendRelease
/* = true */)
301 return m_cec
->VolumeDown(bSendRelease
);
306 uint8_t CLibCEC::MuteAudio(bool bSendRelease
/* = true */)
309 return m_cec
->MuteAudio(bSendRelease
);
313 bool CLibCEC::SendKeypress(cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = true */)
316 return m_cec
->TransmitKeypress(iDestination
, key
, bWait
);
320 bool CLibCEC::SendKeyRelease(cec_logical_address iDestination
, bool bWait
/* = true */)
323 return m_cec
->TransmitKeyRelease(iDestination
, bWait
);
327 cec_osd_name
CLibCEC::GetDeviceOSDName(cec_logical_address iAddress
)
330 retVal
.device
= iAddress
;
334 retVal
= m_cec
->GetDeviceOSDName(iAddress
);
339 void CLibCEC::AddLog(cec_log_level level
, const string
&strMessage
)
341 CLockObject
lock(&m_mutex
);
344 cec_log_message message
;
345 message
.level
= level
;
346 message
.time
= GetTimeMs() - m_iStartTime
;
347 snprintf(message
.message
, sizeof(message
.message
), "%s", strMessage
.c_str());
350 m_callbacks
->CecLogMessage(message
);
352 m_logBuffer
.Push(message
);
356 void CLibCEC::AddKey(cec_keypress
&key
)
358 CLockObject
lock(&m_mutex
);
360 m_callbacks
->CecKeyPress(key
);
362 m_keyBuffer
.Push(key
);
363 m_iCurrentButton
= CEC_USER_CONTROL_CODE_UNKNOWN
;
367 void CLibCEC::AddKey(void)
369 CLockObject
lock(&m_mutex
);
370 if (m_iCurrentButton
!= CEC_USER_CONTROL_CODE_UNKNOWN
)
374 key
.duration
= (unsigned int) (GetTimeMs() - m_buttontime
);
375 key
.keycode
= m_iCurrentButton
;
378 m_callbacks
->CecKeyPress(key
);
380 m_keyBuffer
.Push(key
);
381 m_iCurrentButton
= CEC_USER_CONTROL_CODE_UNKNOWN
;
386 void CLibCEC::AddCommand(const cec_command
&command
)
388 CLockObject
lock(&m_mutex
);
391 m_callbacks
->CecCommand(command
);
393 else if (m_commandBuffer
.Push(command
))
396 strDebug
.Format("stored command '%2x' in the command buffer. buffer size = %d", command
.opcode
, m_commandBuffer
.Size());
397 AddLog(CEC_LOG_DEBUG
, strDebug
);
401 AddLog(CEC_LOG_WARNING
, "command buffer is full");
405 void CLibCEC::CheckKeypressTimeout(void)
407 if (m_iCurrentButton
!= CEC_USER_CONTROL_CODE_UNKNOWN
&& GetTimeMs() - m_buttontime
> CEC_BUTTON_TIMEOUT
)
410 m_iCurrentButton
= CEC_USER_CONTROL_CODE_UNKNOWN
;
414 void CLibCEC::SetCurrentButton(cec_user_control_code iButtonCode
)
416 m_iCurrentButton
= iButtonCode
;
417 m_buttontime
= GetTimeMs();
419 /* push keypress to the keybuffer with 0 duration.
420 push another press to the keybuffer with the duration set when the button is released */
423 key
.keycode
= m_iCurrentButton
;
424 m_keyBuffer
.Push(key
);
427 void * CECCreate(const char *strDeviceName
, CEC::cec_logical_address iLogicalAddress
/*= CEC::CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress
/* = CEC_DEFAULT_PHYSICAL_ADDRESS */)
429 return static_cast< void* > (new CLibCEC(strDeviceName
, iLogicalAddress
, iPhysicalAddress
));
432 void * CECInit(const char *strDeviceName
, CEC::cec_device_type_list types
)
434 return static_cast< void* > (new CLibCEC(strDeviceName
, types
));
437 void CECDestroy(CEC::ICECAdapter
*instance
)
439 CLibCEC
*lib
= static_cast< CLibCEC
* > (instance
);
444 const char *CLibCEC::ToString(const cec_menu_state state
)
446 return m_cec
->ToString(state
);
449 const char *CLibCEC::ToString(const cec_version version
)
451 return m_cec
->ToString(version
);
454 const char *CLibCEC::ToString(const cec_power_status status
)
456 return m_cec
->ToString(status
);
459 const char *CLibCEC::ToString(const cec_logical_address address
)
461 return m_cec
->ToString(address
);
464 const char *CLibCEC::ToString(const cec_deck_control_mode mode
)
466 return m_cec
->ToString(mode
);
469 const char *CLibCEC::ToString(const cec_deck_info status
)
471 return m_cec
->ToString(status
);
474 const char *CLibCEC::ToString(const cec_opcode opcode
)
476 return m_cec
->ToString(opcode
);
479 const char *CLibCEC::ToString(const cec_system_audio_status mode
)
481 return m_cec
->ToString(mode
);
484 const char *CLibCEC::ToString(const cec_audio_status status
)
486 return m_cec
->ToString(status
);
489 const char *CLibCEC::ToString(const cec_vendor_id vendor
)
491 return m_cec
->ToString(vendor
);