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