cec: added typedefs for the callback methods
[deb_libcec.git] / src / lib / LibCEC.cpp
CommitLineData
2abe74eb
LOK
1/*
2 * This file is part of the libCEC(R) library.
3 *
4 * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
6 *
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
8 *
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.
13 *
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.
18 *
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.
22 *
23 *
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
26 *
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/
31 */
32
33#include "LibCEC.h"
34
35#include "AdapterCommunication.h"
36#include "AdapterDetection.h"
37#include "CECProcessor.h"
0f23c85c 38#include "devices/CECBusDevice.h"
2abe74eb 39#include "util/StdString.h"
b9187cc6 40#include "platform/timeutils.h"
2abe74eb
LOK
41
42using namespace std;
43using namespace CEC;
44
f8513317
LOK
45CLibCEC::CLibCEC(const char *strDeviceName, cec_device_type_list types) :
46 m_iStartTime(GetTimeMs()),
47 m_iCurrentButton(CEC_USER_CONTROL_CODE_UNKNOWN),
fa4798bd
LOK
48 m_buttontime(0),
49 m_callbacks(NULL)
f8513317 50{
1113cb7d 51 m_cec = new CCECProcessor(this, strDeviceName, types);
f8513317
LOK
52}
53
2b4d8297 54CLibCEC::CLibCEC(const char *strDeviceName, cec_logical_address iLogicalAddress /* = CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */) :
40339852 55 m_iStartTime(GetTimeMs()),
2abe74eb 56 m_iCurrentButton(CEC_USER_CONTROL_CODE_UNKNOWN),
fa4798bd
LOK
57 m_buttontime(0),
58 m_callbacks(NULL)
2abe74eb 59{
1113cb7d 60 m_cec = new CCECProcessor(this, strDeviceName, iLogicalAddress, iPhysicalAddress);
2abe74eb
LOK
61}
62
63CLibCEC::~CLibCEC(void)
64{
12027dbe 65 Close();
2abe74eb 66 delete m_cec;
2abe74eb
LOK
67}
68
25701fa6 69bool CLibCEC::Open(const char *strPort, uint32_t iTimeoutMs /* = 10000 */)
2abe74eb 70{
1113cb7d 71 if (m_cec->IsRunning())
2abe74eb
LOK
72 {
73 AddLog(CEC_LOG_ERROR, "connection already open");
74 return false;
75 }
76
1113cb7d 77 if (!m_cec->Start(strPort, 38400, iTimeoutMs))
2abe74eb
LOK
78 {
79 AddLog(CEC_LOG_ERROR, "could not start CEC communications");
80 return false;
81 }
82
83 return true;
84}
85
86void CLibCEC::Close(void)
87{
25701fa6
LOK
88 if (m_cec)
89 m_cec->StopThread();
2abe74eb
LOK
90}
91
fa4798bd
LOK
92bool CLibCEC::EnableCallbacks(ICECCallbacks *callbacks)
93{
94 CLockObject lock(&m_mutex);
95 if (m_cec)
96 m_callbacks = callbacks;
97 return false;
98}
99
25701fa6 100int8_t CLibCEC::FindAdapters(cec_adapter *deviceList, uint8_t iBufSize, const char *strDevicePath /* = NULL */)
2abe74eb
LOK
101{
102 CStdString strDebug;
103 if (strDevicePath)
104 strDebug.Format("trying to autodetect the com port for device path '%s'", strDevicePath);
105 else
106 strDebug.Format("trying to autodetect all CEC adapters");
107 AddLog(CEC_LOG_DEBUG, strDebug);
108
25701fa6 109 return CAdapterDetection::FindAdapters(deviceList, iBufSize, strDevicePath);
2abe74eb
LOK
110}
111
112bool CLibCEC::PingAdapter(void)
113{
1113cb7d 114 return m_cec ? m_cec->PingAdapter() : false;
2abe74eb
LOK
115}
116
117bool CLibCEC::StartBootloader(void)
118{
1113cb7d 119 return m_cec ? m_cec->StartBootloader() : false;
2abe74eb
LOK
120}
121
2abe74eb
LOK
122bool CLibCEC::GetNextLogMessage(cec_log_message *message)
123{
25701fa6 124 return (m_logBuffer.Pop(*message));
2abe74eb
LOK
125}
126
127bool CLibCEC::GetNextKeypress(cec_keypress *key)
128{
129 return m_keyBuffer.Pop(*key);
130}
131
132bool CLibCEC::GetNextCommand(cec_command *command)
133{
134 return m_commandBuffer.Pop(*command);
135}
136
8d84e2c0 137bool CLibCEC::Transmit(const cec_command &data)
2abe74eb 138{
8d84e2c0 139 return m_cec ? m_cec->Transmit(data) : false;
2abe74eb
LOK
140}
141
142bool CLibCEC::SetLogicalAddress(cec_logical_address iLogicalAddress)
143{
144 return m_cec ? m_cec->SetLogicalAddress(iLogicalAddress) : false;
145}
146
16b1e052 147bool CLibCEC::SetPhysicalAddress(uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */)
2492216a
LOK
148{
149 return m_cec ? m_cec->SetPhysicalAddress(iPhysicalAddress) : false;
150}
151
d2f1c157 152bool CLibCEC::SetHDMIPort(cec_logical_address iBaseDevice, uint8_t iPort /* = CEC_DEFAULT_HDMI_PORT */)
16b1e052 153{
d2f1c157 154 return m_cec ? m_cec->SetHDMIPort(iBaseDevice, iPort) : false;
16b1e052
LOK
155}
156
2dbd78f8
LOK
157bool CLibCEC::EnablePhysicalAddressDetection(void)
158{
159 return m_cec ? m_cec->EnablePhysicalAddressDetection() : false;
160}
161
2abe74eb
LOK
162bool CLibCEC::PowerOnDevices(cec_logical_address address /* = CECDEVICE_TV */)
163{
0f23c85c 164 return m_cec && address >= CECDEVICE_TV && address <= CECDEVICE_BROADCAST ? m_cec->m_busDevices[(uint8_t)address]->PowerOn() : false;
2abe74eb
LOK
165}
166
167bool CLibCEC::StandbyDevices(cec_logical_address address /* = CECDEVICE_BROADCAST */)
168{
0f23c85c 169 return m_cec && address >= CECDEVICE_TV && address <= CECDEVICE_BROADCAST ? m_cec->m_busDevices[(uint8_t)address]->Standby() : false;
2abe74eb
LOK
170}
171
18203d17
LOK
172bool CLibCEC::SetActiveSource(cec_device_type type /* = CEC_DEVICE_TYPE_RESERVED */)
173{
174 return m_cec ? m_cec->SetActiveSource(type) : false;
175}
176
2abe74eb
LOK
177bool CLibCEC::SetActiveView(void)
178{
179 return m_cec ? m_cec->SetActiveView() : false;
180}
181
28fa6c97 182bool CLibCEC::SetDeckControlMode(cec_deck_control_mode mode, bool bSendUpdate /* = true */)
a9232a79 183{
28fa6c97 184 return m_cec ? m_cec->SetDeckControlMode(mode, bSendUpdate) : false;
a9232a79
LOK
185}
186
28fa6c97 187bool CLibCEC::SetDeckInfo(cec_deck_info info, bool bSendUpdate /* = true */)
a9232a79 188{
28fa6c97 189 return m_cec ? m_cec->SetDeckInfo(info, bSendUpdate) : false;
a9232a79
LOK
190}
191
2abe74eb
LOK
192bool CLibCEC::SetInactiveView(void)
193{
8fb8355c 194 return m_cec ? m_cec->TransmitInactiveSource() : false;
2abe74eb
LOK
195}
196
28fa6c97
LOK
197bool CLibCEC::SetMenuState(cec_menu_state state, bool bSendUpdate /* = true */)
198{
199 return m_cec ? m_cec->SetMenuState(state, bSendUpdate) : false;
200}
201
1969b140
LOK
202bool CLibCEC::SetOSDString(cec_logical_address iLogicalAddress, cec_display_control duration, const char *strMessage)
203{
38bdb943
LOK
204 return m_cec && iLogicalAddress >= CECDEVICE_TV && iLogicalAddress <= CECDEVICE_BROADCAST ?
205 m_cec->m_busDevices[m_cec->GetLogicalAddress()]->TransmitOSDString(iLogicalAddress, duration, strMessage) :
206 false;
1969b140
LOK
207}
208
8b7e5ff6
LOK
209bool CLibCEC::SwitchMonitoring(bool bEnable)
210{
211 return m_cec ? m_cec->SwitchMonitoring(bEnable) : false;
212}
213
6a1c0009
LOK
214cec_version CLibCEC::GetDeviceCecVersion(cec_logical_address iAddress)
215{
216 if (m_cec && iAddress >= CECDEVICE_TV && iAddress < CECDEVICE_BROADCAST)
217 return m_cec->GetDeviceCecVersion(iAddress);
218 return CEC_VERSION_UNKNOWN;
219}
220
a3269a0a
LOK
221bool CLibCEC::GetDeviceMenuLanguage(cec_logical_address iAddress, cec_menu_language *language)
222{
223 if (m_cec && iAddress >= CECDEVICE_TV && iAddress < CECDEVICE_BROADCAST)
224 return m_cec->GetDeviceMenuLanguage(iAddress, language);
225 return false;
226}
227
44c74256
LOK
228uint64_t CLibCEC::GetDeviceVendorId(cec_logical_address iAddress)
229{
230 if (m_cec && iAddress >= CECDEVICE_TV && iAddress < CECDEVICE_BROADCAST)
231 return m_cec->GetDeviceVendorId(iAddress);
232 return 0;
233}
234
eab72c40
LOK
235uint16_t CLibCEC::GetDevicePhysicalAddress(cec_logical_address iAddress)
236{
237 if (m_cec && iAddress >= CECDEVICE_TV && iAddress < CECDEVICE_BROADCAST)
238 return m_cec->GetDevicePhysicalAddress(iAddress);
239 return 0;
240}
241
b4b1b49b
LOK
242cec_logical_address CLibCEC::GetActiveSource(void)
243{
244 return m_cec ? m_cec->GetActiveSource() : CECDEVICE_UNKNOWN;
245}
246
247bool CLibCEC::IsActiveSource(cec_logical_address iAddress)
248{
249 if (m_cec && iAddress >= CECDEVICE_TV && iAddress < CECDEVICE_BROADCAST)
250 return m_cec->IsActiveSource(iAddress);
251 return false;
252}
253
e55f3f70
LOK
254cec_power_status CLibCEC::GetDevicePowerStatus(cec_logical_address iAddress)
255{
256 if (m_cec && iAddress >= CECDEVICE_TV && iAddress < CECDEVICE_BROADCAST)
257 return m_cec->GetDevicePowerStatus(iAddress);
258 return CEC_POWER_STATUS_UNKNOWN;
259}
44c74256 260
57f45e6c
LOK
261bool CLibCEC::PollDevice(cec_logical_address iAddress)
262{
263 if (m_cec && iAddress >= CECDEVICE_TV && iAddress < CECDEVICE_BROADCAST)
264 return m_cec->PollDevice(iAddress);
265 return false;
266}
267
6d858ba4
LOK
268cec_logical_addresses CLibCEC::GetActiveDevices(void)
269{
270 cec_logical_addresses addresses;
988de7b9 271 addresses.Clear();
6d858ba4
LOK
272 if (m_cec)
273 addresses = m_cec->GetActiveDevices();
274 return addresses;
275}
276
277bool CLibCEC::IsActiveDevice(cec_logical_address iAddress)
278{
279 if (m_cec && iAddress >= CECDEVICE_TV && iAddress < CECDEVICE_BROADCAST)
37b0c572 280 return m_cec->IsPresentDevice(iAddress);
6d858ba4
LOK
281 return false;
282}
283
284bool CLibCEC::IsActiveDeviceType(cec_device_type type)
285{
286 if (m_cec && type >= CEC_DEVICE_TYPE_TV && type <= CEC_DEVICE_TYPE_AUDIO_SYSTEM)
37b0c572 287 return m_cec->IsPresentDeviceType(type);
6d858ba4
LOK
288 return false;
289}
290
5c73f7f7 291uint8_t CLibCEC::VolumeUp(bool bSendRelease /* = true */)
04e637f9
LOK
292{
293 if (m_cec)
5c73f7f7 294 return m_cec->VolumeUp(bSendRelease);
04e637f9
LOK
295 return 0;
296}
297
5c73f7f7 298uint8_t CLibCEC::VolumeDown(bool bSendRelease /* = true */)
04e637f9
LOK
299{
300 if (m_cec)
5c73f7f7 301 return m_cec->VolumeDown(bSendRelease);
04e637f9
LOK
302 return 0;
303}
304
305
5c73f7f7 306uint8_t CLibCEC::MuteAudio(bool bSendRelease /* = true */)
04e637f9
LOK
307{
308 if (m_cec)
5c73f7f7 309 return m_cec->MuteAudio(bSendRelease);
04e637f9
LOK
310 return 0;
311}
312
4bec9d79 313bool CLibCEC::SendKeypress(cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = true */)
a33794d8
LOK
314{
315 if (m_cec)
4bec9d79 316 return m_cec->TransmitKeypress(iDestination, key, bWait);
a33794d8
LOK
317 return false;
318}
319
4bec9d79 320bool CLibCEC::SendKeyRelease(cec_logical_address iDestination, bool bWait /* = true */)
a33794d8
LOK
321{
322 if (m_cec)
4bec9d79 323 return m_cec->TransmitKeyRelease(iDestination, bWait);
a33794d8
LOK
324 return false;
325}
326
f71a1df9 327cec_osd_name CLibCEC::GetDeviceOSDName(cec_logical_address iAddress)
ed21be2a
LOK
328{
329 cec_osd_name retVal;
330 retVal.device = iAddress;
331 retVal.name[0] = 0;
332
333 if (m_cec)
334 retVal = m_cec->GetDeviceOSDName(iAddress);
335
336 return retVal;
337}
338
2abe74eb
LOK
339void CLibCEC::AddLog(cec_log_level level, const string &strMessage)
340{
fa4798bd 341 CLockObject lock(&m_mutex);
13fd6a66 342 if (m_cec)
25701fa6
LOK
343 {
344 cec_log_message message;
345 message.level = level;
40339852 346 message.time = GetTimeMs() - m_iStartTime;
25701fa6 347 snprintf(message.message, sizeof(message.message), "%s", strMessage.c_str());
fa4798bd
LOK
348
349 if (m_callbacks)
c3039d2e 350 m_callbacks->CBCecLogMessage(message);
fa4798bd
LOK
351 else
352 m_logBuffer.Push(message);
25701fa6 353 }
2abe74eb
LOK
354}
355
95ba7a09
LOK
356void CLibCEC::AddKey(cec_keypress &key)
357{
fa4798bd
LOK
358 CLockObject lock(&m_mutex);
359 if (m_callbacks)
c3039d2e 360 m_callbacks->CBCecKeyPress(key);
fa4798bd
LOK
361 else
362 m_keyBuffer.Push(key);
95ba7a09
LOK
363 m_iCurrentButton = CEC_USER_CONTROL_CODE_UNKNOWN;
364 m_buttontime = 0;
365}
366
2abe74eb
LOK
367void CLibCEC::AddKey(void)
368{
fa4798bd 369 CLockObject lock(&m_mutex);
13fd6a66 370 if (m_iCurrentButton != CEC_USER_CONTROL_CODE_UNKNOWN)
2abe74eb
LOK
371 {
372 cec_keypress key;
25701fa6 373
2abe74eb
LOK
374 key.duration = (unsigned int) (GetTimeMs() - m_buttontime);
375 key.keycode = m_iCurrentButton;
fa4798bd
LOK
376
377 if (m_callbacks)
c3039d2e 378 m_callbacks->CBCecKeyPress(key);
fa4798bd
LOK
379 else
380 m_keyBuffer.Push(key);
2abe74eb 381 m_iCurrentButton = CEC_USER_CONTROL_CODE_UNKNOWN;
2abe74eb 382 }
56aa08d0 383 m_buttontime = 0;
2abe74eb
LOK
384}
385
e9de9629 386void CLibCEC::AddCommand(const cec_command &command)
2abe74eb 387{
fa4798bd
LOK
388 CLockObject lock(&m_mutex);
389 if (m_callbacks)
390 {
c3039d2e 391 m_callbacks->CBCecCommand(command);
fa4798bd
LOK
392 }
393 else if (m_commandBuffer.Push(command))
2abe74eb
LOK
394 {
395 CStdString strDebug;
9dee1670 396 strDebug.Format("stored command '%2x' in the command buffer. buffer size = %d", command.opcode, m_commandBuffer.Size());
2abe74eb
LOK
397 AddLog(CEC_LOG_DEBUG, strDebug);
398 }
399 else
400 {
401 AddLog(CEC_LOG_WARNING, "command buffer is full");
402 }
403}
404
405void CLibCEC::CheckKeypressTimeout(void)
406{
407 if (m_iCurrentButton != CEC_USER_CONTROL_CODE_UNKNOWN && GetTimeMs() - m_buttontime > CEC_BUTTON_TIMEOUT)
408 {
409 AddKey();
410 m_iCurrentButton = CEC_USER_CONTROL_CODE_UNKNOWN;
411 }
412}
413
414void CLibCEC::SetCurrentButton(cec_user_control_code iButtonCode)
415{
416 m_iCurrentButton = iButtonCode;
417 m_buttontime = GetTimeMs();
6710d300
LOK
418
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 */
421 cec_keypress key;
422 key.duration = 0;
423 key.keycode = m_iCurrentButton;
424 m_keyBuffer.Push(key);
2abe74eb
LOK
425}
426
25701fa6 427void * CECCreate(const char *strDeviceName, CEC::cec_logical_address iLogicalAddress /*= CEC::CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */)
2abe74eb
LOK
428{
429 return static_cast< void* > (new CLibCEC(strDeviceName, iLogicalAddress, iPhysicalAddress));
430}
25701fa6 431
f8513317
LOK
432void * CECInit(const char *strDeviceName, CEC::cec_device_type_list types)
433{
434 return static_cast< void* > (new CLibCEC(strDeviceName, types));
435}
436
25701fa6
LOK
437void CECDestroy(CEC::ICECAdapter *instance)
438{
439 CLibCEC *lib = static_cast< CLibCEC* > (instance);
440 if (lib)
441 delete lib;
442}
03ae897d
LOK
443
444const char *CLibCEC::ToString(const cec_menu_state state)
445{
446 return m_cec->ToString(state);
447}
448
449const char *CLibCEC::ToString(const cec_version version)
450{
451 return m_cec->ToString(version);
452}
453
454const char *CLibCEC::ToString(const cec_power_status status)
455{
456 return m_cec->ToString(status);
457}
458
459const char *CLibCEC::ToString(const cec_logical_address address)
460{
461 return m_cec->ToString(address);
462}
463
464const char *CLibCEC::ToString(const cec_deck_control_mode mode)
465{
466 return m_cec->ToString(mode);
467}
468
469const char *CLibCEC::ToString(const cec_deck_info status)
470{
471 return m_cec->ToString(status);
472}
473
474const char *CLibCEC::ToString(const cec_opcode opcode)
475{
476 return m_cec->ToString(opcode);
477}
478
479const char *CLibCEC::ToString(const cec_system_audio_status mode)
480{
481 return m_cec->ToString(mode);
482}
483
484const char *CLibCEC::ToString(const cec_audio_status status)
485{
486 return m_cec->ToString(status);
487}
488
489const char *CLibCEC::ToString(const cec_vendor_id vendor)
490{
491 return m_cec->ToString(vendor);
492}