cec: stop replying to power status requests from LG when the power state is 'on'
[deb_libcec.git] / src / lib / LibCEC.cpp
1 /*
2 * This file is part of the libCEC(R) library.
3 *
4 * libCEC(R) is Copyright (C) 2011-2012 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 "adapter/USBCECAdapterDetection.h"
36 #include "CECProcessor.h"
37 #include "devices/CECBusDevice.h"
38 #include "platform/util/timeutils.h"
39 #include "platform/util/StdString.h"
40
41 using namespace std;
42 using namespace CEC;
43 using namespace PLATFORM;
44
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),
48 m_buttontime(0),
49 m_callbacks(NULL),
50 m_cbParam(NULL)
51 {
52 m_cec = new CCECProcessor(this, strDeviceName, types, iPhysicalAddress, CEC_CLIENT_VERSION_PRE_1_5);
53 }
54
55 CLibCEC::CLibCEC(const libcec_configuration *configuration) :
56 m_iStartTime(GetTimeMs()),
57 m_iCurrentButton(CEC_USER_CONTROL_CODE_UNKNOWN),
58 m_buttontime(0),
59 m_callbacks(configuration->callbacks),
60 m_cbParam(configuration->callbackParam)
61 {
62 m_cec = new CCECProcessor(this, configuration);
63 }
64
65 CLibCEC::~CLibCEC(void)
66 {
67 Close();
68 delete m_cec;
69 }
70
71 bool CLibCEC::Open(const char *strPort, uint32_t iTimeoutMs /* = 10000 */)
72 {
73 if (m_cec->IsRunning())
74 {
75 AddLog(CEC_LOG_ERROR, "connection already open");
76 return false;
77 }
78
79 if (!m_cec->Start(strPort, 38400, iTimeoutMs))
80 {
81 AddLog(CEC_LOG_ERROR, "could not start CEC communications");
82 return false;
83 }
84
85 return true;
86 }
87
88 void CLibCEC::Close(void)
89 {
90 if (m_cec)
91 m_cec->Close();
92 }
93
94 bool CLibCEC::EnableCallbacks(void *cbParam, ICECCallbacks *callbacks)
95 {
96 CLockObject lock(m_mutex);
97 if (m_cec)
98 {
99 m_cbParam = cbParam;
100 m_callbacks = callbacks;
101 }
102 return false;
103 }
104
105 int8_t CLibCEC::FindAdapters(cec_adapter *deviceList, uint8_t iBufSize, const char *strDevicePath /* = NULL */)
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
114 return CUSBCECAdapterDetection::FindAdapters(deviceList, iBufSize, strDevicePath);
115 }
116
117 bool CLibCEC::PingAdapter(void)
118 {
119 return m_cec ? m_cec->PingAdapter() : false;
120 }
121
122 bool CLibCEC::StartBootloader(void)
123 {
124 return m_cec ? m_cec->StartBootloader() : false;
125 }
126
127 bool CLibCEC::GetNextLogMessage(cec_log_message *message)
128 {
129 return (m_logBuffer.Pop(*message));
130 }
131
132 bool CLibCEC::GetNextKeypress(cec_keypress *key)
133 {
134 return m_keyBuffer.Pop(*key);
135 }
136
137 bool CLibCEC::GetNextCommand(cec_command *command)
138 {
139 return m_commandBuffer.Pop(*command);
140 }
141
142 bool CLibCEC::Transmit(const cec_command &data)
143 {
144 return m_cec ? m_cec->Transmit(data) : false;
145 }
146
147 bool CLibCEC::SetLogicalAddress(cec_logical_address iLogicalAddress)
148 {
149 return m_cec ? m_cec->SetLogicalAddress(iLogicalAddress) : false;
150 }
151
152 bool CLibCEC::SetPhysicalAddress(uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */)
153 {
154 return m_cec ? m_cec->SetPhysicalAddress(iPhysicalAddress) : false;
155 }
156
157 bool CLibCEC::SetHDMIPort(cec_logical_address iBaseDevice, uint8_t iPort /* = CEC_DEFAULT_HDMI_PORT */)
158 {
159 return m_cec ? m_cec->SetHDMIPort(iBaseDevice, iPort) : false;
160 }
161
162 bool CLibCEC::EnablePhysicalAddressDetection(void)
163 {
164 return m_cec ? m_cec->EnablePhysicalAddressDetection() : false;
165 }
166
167 bool CLibCEC::PowerOnDevices(cec_logical_address address /* = CECDEVICE_TV */)
168 {
169 return m_cec && address >= CECDEVICE_TV && address <= CECDEVICE_BROADCAST ? m_cec->m_busDevices[(uint8_t)address]->PowerOn() : false;
170 }
171
172 bool CLibCEC::StandbyDevices(cec_logical_address address /* = CECDEVICE_BROADCAST */)
173 {
174 return m_cec && address >= CECDEVICE_TV && address <= CECDEVICE_BROADCAST ? m_cec->m_busDevices[(uint8_t)address]->Standby() : false;
175 }
176
177 bool CLibCEC::SetActiveSource(cec_device_type type /* = CEC_DEVICE_TYPE_RESERVED */)
178 {
179 return m_cec ? m_cec->SetActiveSource(type) : false;
180 }
181
182 bool CLibCEC::SetActiveView(void)
183 {
184 return m_cec ? m_cec->SetActiveView() : false;
185 }
186
187 bool CLibCEC::SetDeckControlMode(cec_deck_control_mode mode, bool bSendUpdate /* = true */)
188 {
189 return m_cec ? m_cec->SetDeckControlMode(mode, bSendUpdate) : false;
190 }
191
192 bool CLibCEC::SetDeckInfo(cec_deck_info info, bool bSendUpdate /* = true */)
193 {
194 return m_cec ? m_cec->SetDeckInfo(info, bSendUpdate) : false;
195 }
196
197 bool CLibCEC::SetInactiveView(void)
198 {
199 return m_cec ? m_cec->TransmitInactiveSource() : false;
200 }
201
202 bool CLibCEC::SetMenuState(cec_menu_state state, bool bSendUpdate /* = true */)
203 {
204 return m_cec ? m_cec->SetMenuState(state, bSendUpdate) : false;
205 }
206
207 bool CLibCEC::SetOSDString(cec_logical_address iLogicalAddress, cec_display_control duration, const char *strMessage)
208 {
209 return m_cec && iLogicalAddress >= CECDEVICE_TV && iLogicalAddress <= CECDEVICE_BROADCAST ?
210 m_cec->m_busDevices[m_cec->GetLogicalAddress()]->TransmitOSDString(iLogicalAddress, duration, strMessage) :
211 false;
212 }
213
214 bool CLibCEC::SwitchMonitoring(bool bEnable)
215 {
216 return m_cec ? m_cec->SwitchMonitoring(bEnable) : false;
217 }
218
219 cec_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
226 bool 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
233 uint64_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
240 uint16_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
247 cec_logical_address CLibCEC::GetActiveSource(void)
248 {
249 return m_cec ? m_cec->GetActiveSource() : CECDEVICE_UNKNOWN;
250 }
251
252 bool 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
259 cec_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 }
265
266 bool 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
273 cec_logical_addresses CLibCEC::GetActiveDevices(void)
274 {
275 cec_logical_addresses addresses;
276 addresses.Clear();
277 if (m_cec)
278 addresses = m_cec->GetActiveDevices();
279 return addresses;
280 }
281
282 bool CLibCEC::IsActiveDevice(cec_logical_address iAddress)
283 {
284 if (m_cec && iAddress >= CECDEVICE_TV && iAddress < CECDEVICE_BROADCAST)
285 return m_cec->IsPresentDevice(iAddress);
286 return false;
287 }
288
289 bool CLibCEC::IsActiveDeviceType(cec_device_type type)
290 {
291 if (m_cec && type >= CEC_DEVICE_TYPE_TV && type <= CEC_DEVICE_TYPE_AUDIO_SYSTEM)
292 return m_cec->IsPresentDeviceType(type);
293 return false;
294 }
295
296 uint8_t CLibCEC::VolumeUp(bool bSendRelease /* = true */)
297 {
298 if (m_cec)
299 return m_cec->VolumeUp(bSendRelease);
300 return 0;
301 }
302
303 uint8_t CLibCEC::VolumeDown(bool bSendRelease /* = true */)
304 {
305 if (m_cec)
306 return m_cec->VolumeDown(bSendRelease);
307 return 0;
308 }
309
310
311 uint8_t CLibCEC::MuteAudio(bool bSendRelease /* = true */)
312 {
313 if (m_cec)
314 return m_cec->MuteAudio(bSendRelease);
315 return 0;
316 }
317
318 bool CLibCEC::SendKeypress(cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = true */)
319 {
320 if (m_cec)
321 return m_cec->TransmitKeypress(iDestination, key, bWait);
322 return false;
323 }
324
325 bool CLibCEC::SendKeyRelease(cec_logical_address iDestination, bool bWait /* = true */)
326 {
327 if (m_cec)
328 return m_cec->TransmitKeyRelease(iDestination, bWait);
329 return false;
330 }
331
332 cec_osd_name CLibCEC::GetDeviceOSDName(cec_logical_address iAddress)
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
344 void CLibCEC::AddLog(cec_log_level level, const char *strFormat, ...)
345 {
346 CStdString strLog;
347
348 va_list argList;
349 va_start(argList, strFormat);
350 strLog.FormatV(strFormat, argList);
351 va_end(argList);
352
353 CLibCEC *instance = CLibCEC::GetInstance();
354 CLockObject lock(instance->m_mutex);
355
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());
360
361 if (instance->m_callbacks)
362 instance->m_callbacks->CBCecLogMessage(instance->m_cbParam, message);
363 else
364 instance->m_logBuffer.Push(message);
365 }
366
367 void CLibCEC::AddKey(cec_keypress &key)
368 {
369 CLibCEC *instance = CLibCEC::GetInstance();
370 CLockObject lock(instance->m_mutex);
371
372 AddLog(CEC_LOG_DEBUG, "key pressed: %1x", key.keycode);
373
374 if (instance->m_callbacks)
375 instance->m_callbacks->CBCecKeyPress(instance->m_cbParam, key);
376 else
377 instance->m_keyBuffer.Push(key);
378
379 instance->m_iCurrentButton = key.duration > 0 ? CEC_USER_CONTROL_CODE_UNKNOWN : key.keycode;
380 instance->m_buttontime = key.duration > 0 ? 0 : GetTimeMs();
381 }
382
383 void CLibCEC::SetCurrentButton(cec_user_control_code iButtonCode)
384 {
385 /* push keypress to the keybuffer with 0 duration.
386 push another press to the keybuffer with the duration set when the button is released */
387 cec_keypress key;
388 key.duration = 0;
389 key.keycode = iButtonCode;
390
391 AddKey(key);
392 }
393
394 void CLibCEC::AddKey(void)
395 {
396 CLibCEC *instance = CLibCEC::GetInstance();
397 CLockObject lock(instance->m_mutex);
398
399 if (instance->m_iCurrentButton != CEC_USER_CONTROL_CODE_UNKNOWN)
400 {
401 cec_keypress key;
402
403 key.duration = (unsigned int) (GetTimeMs() - instance->m_buttontime);
404 key.keycode = instance->m_iCurrentButton;
405 AddLog(CEC_LOG_DEBUG, "key released: %1x", key.keycode);
406
407 if (instance->m_callbacks)
408 instance->m_callbacks->CBCecKeyPress(instance->m_cbParam, key);
409 else
410 instance->m_keyBuffer.Push(key);
411 instance->m_iCurrentButton = CEC_USER_CONTROL_CODE_UNKNOWN;
412 }
413 instance->m_buttontime = 0;
414 }
415
416 void CLibCEC::AddCommand(const cec_command &command)
417 {
418 CLibCEC *instance = CLibCEC::GetInstance();
419 CLockObject lock(instance->m_mutex);
420
421 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);
422
423 if (instance->m_callbacks)
424 instance->m_callbacks->CBCecCommand(instance->m_cbParam, command);
425 else if (!instance->m_commandBuffer.Push(command))
426 AddLog(CEC_LOG_WARNING, "command buffer is full");
427 }
428
429 void CLibCEC::CheckKeypressTimeout(void)
430 {
431 if (m_iCurrentButton != CEC_USER_CONTROL_CODE_UNKNOWN && GetTimeMs() - m_buttontime > CEC_BUTTON_TIMEOUT)
432 {
433 AddKey();
434 m_iCurrentButton = CEC_USER_CONTROL_CODE_UNKNOWN;
435 }
436 }
437
438 bool CLibCEC::SetStreamPath(cec_logical_address iAddress)
439 {
440 uint16_t iPhysicalAddress = GetDevicePhysicalAddress(iAddress);
441 if (iPhysicalAddress != 0xFFFF)
442 return SetStreamPath(iPhysicalAddress);
443 return false;
444 }
445
446 bool CLibCEC::SetStreamPath(uint16_t iPhysicalAddress)
447 {
448 return m_cec->SetStreamPath(iPhysicalAddress);
449 }
450
451 cec_logical_addresses CLibCEC::GetLogicalAddresses(void)
452 {
453 cec_logical_addresses addr = m_cec->GetLogicalAddresses();
454 return addr;
455 }
456
457 static CLibCEC *g_libCEC_instance(NULL);
458 CLibCEC *CLibCEC::GetInstance(void)
459 {
460 return g_libCEC_instance;
461 }
462
463 void CLibCEC::SetInstance(CLibCEC *instance)
464 {
465 if (g_libCEC_instance)
466 delete g_libCEC_instance;
467 g_libCEC_instance = instance;
468 }
469
470 void * CECInit(const char *strDeviceName, CEC::cec_device_type_list types, uint16_t iPhysicalAddress /* = 0 */)
471 {
472 CLibCEC *lib = new CLibCEC(strDeviceName, types);
473 CLibCEC::SetInstance(lib);
474 return static_cast< void* > (lib);
475 }
476
477 void * CECInitialise(const libcec_configuration *configuration)
478 {
479 CLibCEC *lib = new CLibCEC(configuration);
480 CLibCEC::SetInstance(lib);
481 return static_cast< void* > (lib);
482 }
483
484 void CECDestroy(CEC::ICECAdapter *UNUSED(instance))
485 {
486 CLibCEC::SetInstance(NULL);
487 }
488
489 const char *CLibCEC::ToString(const cec_menu_state state)
490 {
491 return m_cec->ToString(state);
492 }
493
494 const char *CLibCEC::ToString(const cec_version version)
495 {
496 return m_cec->ToString(version);
497 }
498
499 const char *CLibCEC::ToString(const cec_power_status status)
500 {
501 return m_cec->ToString(status);
502 }
503
504 const char *CLibCEC::ToString(const cec_logical_address address)
505 {
506 return m_cec->ToString(address);
507 }
508
509 const char *CLibCEC::ToString(const cec_deck_control_mode mode)
510 {
511 return m_cec->ToString(mode);
512 }
513
514 const char *CLibCEC::ToString(const cec_deck_info status)
515 {
516 return m_cec->ToString(status);
517 }
518
519 const char *CLibCEC::ToString(const cec_opcode opcode)
520 {
521 return m_cec->ToString(opcode);
522 }
523
524 const char *CLibCEC::ToString(const cec_system_audio_status mode)
525 {
526 return m_cec->ToString(mode);
527 }
528
529 const char *CLibCEC::ToString(const cec_audio_status status)
530 {
531 return m_cec->ToString(status);
532 }
533
534 const char *CLibCEC::ToString(const cec_vendor_id vendor)
535 {
536 return m_cec->ToString(vendor);
537 }
538
539 const char *CLibCEC::ToString(const cec_client_version version)
540 {
541 return m_cec->ToString(version);
542 }
543
544 bool CLibCEC::GetCurrentConfiguration(libcec_configuration *configuration)
545 {
546 return m_cec->GetCurrentConfiguration(configuration);
547 }
548
549 bool CLibCEC::CanPersistConfiguration(void)
550 {
551 return m_cec->CanPersistConfiguration();
552 }
553
554 bool CLibCEC::PersistConfiguration(libcec_configuration *configuration)
555 {
556 return m_cec->PersistConfiguration(configuration);
557 }