a289b75c47fe9bda4b0cfc051c85b74a4fd88364
[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);
53 }
54
55 CLibCEC::CLibCEC(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 configuration->serverVersion = CEC_SERVER_VERSION_1_5_0;
64 }
65
66 CLibCEC::~CLibCEC(void)
67 {
68 Close();
69 delete m_cec;
70 }
71
72 bool CLibCEC::Open(const char *strPort, uint32_t iTimeoutMs /* = 10000 */)
73 {
74 if (m_cec->IsRunning())
75 {
76 AddLog(CEC_LOG_ERROR, "connection already open");
77 return false;
78 }
79
80 if (!m_cec->Start(strPort, 38400, iTimeoutMs))
81 {
82 AddLog(CEC_LOG_ERROR, "could not start CEC communications");
83 return false;
84 }
85
86 return true;
87 }
88
89 void CLibCEC::Close(void)
90 {
91 if (m_cec)
92 m_cec->Close();
93 }
94
95 bool CLibCEC::EnableCallbacks(void *cbParam, ICECCallbacks *callbacks)
96 {
97 CLockObject lock(m_mutex);
98 if (m_cec)
99 {
100 m_cbParam = cbParam;
101 m_callbacks = callbacks;
102 }
103 return false;
104 }
105
106 int8_t CLibCEC::FindAdapters(cec_adapter *deviceList, uint8_t iBufSize, const char *strDevicePath /* = NULL */)
107 {
108 CStdString strDebug;
109 if (strDevicePath)
110 strDebug.Format("trying to autodetect the com port for device path '%s'", strDevicePath);
111 else
112 strDebug.Format("trying to autodetect all CEC adapters");
113 AddLog(CEC_LOG_DEBUG, strDebug);
114
115 return CUSBCECAdapterDetection::FindAdapters(deviceList, iBufSize, strDevicePath);
116 }
117
118 bool CLibCEC::PingAdapter(void)
119 {
120 return m_cec ? m_cec->PingAdapter() : false;
121 }
122
123 bool CLibCEC::StartBootloader(void)
124 {
125 return m_cec ? m_cec->StartBootloader() : false;
126 }
127
128 bool CLibCEC::GetNextLogMessage(cec_log_message *message)
129 {
130 return (m_logBuffer.Pop(*message));
131 }
132
133 bool CLibCEC::GetNextKeypress(cec_keypress *key)
134 {
135 return m_keyBuffer.Pop(*key);
136 }
137
138 bool CLibCEC::GetNextCommand(cec_command *command)
139 {
140 return m_commandBuffer.Pop(*command);
141 }
142
143 bool CLibCEC::Transmit(const cec_command &data)
144 {
145 return m_cec ? m_cec->Transmit(data) : false;
146 }
147
148 bool CLibCEC::SetLogicalAddress(cec_logical_address iLogicalAddress)
149 {
150 return m_cec ? m_cec->SetLogicalAddress(iLogicalAddress) : false;
151 }
152
153 bool CLibCEC::SetPhysicalAddress(uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */)
154 {
155 return m_cec ? m_cec->SetPhysicalAddress(iPhysicalAddress) : false;
156 }
157
158 bool CLibCEC::SetHDMIPort(cec_logical_address iBaseDevice, uint8_t iPort /* = CEC_DEFAULT_HDMI_PORT */)
159 {
160 return m_cec ? m_cec->SetHDMIPort(iBaseDevice, iPort) : false;
161 }
162
163 bool CLibCEC::EnablePhysicalAddressDetection(void)
164 {
165 return m_cec ? m_cec->EnablePhysicalAddressDetection() : false;
166 }
167
168 bool CLibCEC::PowerOnDevices(cec_logical_address address /* = CECDEVICE_TV */)
169 {
170 return m_cec && address >= CECDEVICE_TV && address <= CECDEVICE_BROADCAST ? m_cec->PowerOnDevices(address) : false;
171 }
172
173 bool CLibCEC::StandbyDevices(cec_logical_address address /* = CECDEVICE_BROADCAST */)
174 {
175 return m_cec && address >= CECDEVICE_TV && address <= CECDEVICE_BROADCAST ? m_cec->StandbyDevices(address) : false;
176 }
177
178 bool CLibCEC::SetActiveSource(cec_device_type type /* = CEC_DEVICE_TYPE_RESERVED */)
179 {
180 return m_cec ? m_cec->SetActiveSource(type) : false;
181 }
182
183 bool CLibCEC::SetActiveView(void)
184 {
185 return m_cec ? m_cec->SetActiveView() : false;
186 }
187
188 bool CLibCEC::SetDeckControlMode(cec_deck_control_mode mode, bool bSendUpdate /* = true */)
189 {
190 return m_cec ? m_cec->SetDeckControlMode(mode, bSendUpdate) : false;
191 }
192
193 bool CLibCEC::SetDeckInfo(cec_deck_info info, bool bSendUpdate /* = true */)
194 {
195 return m_cec ? m_cec->SetDeckInfo(info, bSendUpdate) : false;
196 }
197
198 bool CLibCEC::SetInactiveView(void)
199 {
200 return m_cec ? m_cec->TransmitInactiveSource() : false;
201 }
202
203 bool CLibCEC::SetMenuState(cec_menu_state state, bool bSendUpdate /* = true */)
204 {
205 return m_cec ? m_cec->SetMenuState(state, bSendUpdate) : false;
206 }
207
208 bool CLibCEC::SetOSDString(cec_logical_address iLogicalAddress, cec_display_control duration, const char *strMessage)
209 {
210 return m_cec && iLogicalAddress >= CECDEVICE_TV && iLogicalAddress <= CECDEVICE_BROADCAST ?
211 m_cec->m_busDevices[m_cec->GetLogicalAddress()]->TransmitOSDString(iLogicalAddress, duration, strMessage) :
212 false;
213 }
214
215 bool CLibCEC::SwitchMonitoring(bool bEnable)
216 {
217 return m_cec ? m_cec->SwitchMonitoring(bEnable) : false;
218 }
219
220 cec_version CLibCEC::GetDeviceCecVersion(cec_logical_address iAddress)
221 {
222 if (m_cec && iAddress >= CECDEVICE_TV && iAddress < CECDEVICE_BROADCAST)
223 return m_cec->GetDeviceCecVersion(iAddress);
224 return CEC_VERSION_UNKNOWN;
225 }
226
227 bool CLibCEC::GetDeviceMenuLanguage(cec_logical_address iAddress, cec_menu_language *language)
228 {
229 if (m_cec && iAddress >= CECDEVICE_TV && iAddress < CECDEVICE_BROADCAST)
230 return m_cec->GetDeviceMenuLanguage(iAddress, language);
231 return false;
232 }
233
234 uint64_t CLibCEC::GetDeviceVendorId(cec_logical_address iAddress)
235 {
236 if (m_cec && iAddress >= CECDEVICE_TV && iAddress < CECDEVICE_BROADCAST)
237 return m_cec->GetDeviceVendorId(iAddress);
238 return 0;
239 }
240
241 uint16_t CLibCEC::GetDevicePhysicalAddress(cec_logical_address iAddress)
242 {
243 if (m_cec && iAddress >= CECDEVICE_TV && iAddress < CECDEVICE_BROADCAST)
244 return m_cec->GetDevicePhysicalAddress(iAddress);
245 return 0;
246 }
247
248 cec_logical_address CLibCEC::GetActiveSource(void)
249 {
250 return m_cec ? m_cec->GetActiveSource() : CECDEVICE_UNKNOWN;
251 }
252
253 bool CLibCEC::IsActiveSource(cec_logical_address iAddress)
254 {
255 if (m_cec && iAddress >= CECDEVICE_TV && iAddress < CECDEVICE_BROADCAST)
256 return m_cec->IsActiveSource(iAddress);
257 return false;
258 }
259
260 cec_power_status CLibCEC::GetDevicePowerStatus(cec_logical_address iAddress)
261 {
262 if (m_cec && iAddress >= CECDEVICE_TV && iAddress < CECDEVICE_BROADCAST)
263 return m_cec->GetDevicePowerStatus(iAddress);
264 return CEC_POWER_STATUS_UNKNOWN;
265 }
266
267 bool CLibCEC::PollDevice(cec_logical_address iAddress)
268 {
269 if (m_cec && iAddress >= CECDEVICE_TV && iAddress < CECDEVICE_BROADCAST)
270 return m_cec->PollDevice(iAddress);
271 return false;
272 }
273
274 cec_logical_addresses CLibCEC::GetActiveDevices(void)
275 {
276 cec_logical_addresses addresses;
277 addresses.Clear();
278 if (m_cec)
279 addresses = m_cec->GetActiveDevices();
280 return addresses;
281 }
282
283 bool CLibCEC::IsActiveDevice(cec_logical_address iAddress)
284 {
285 if (m_cec && iAddress >= CECDEVICE_TV && iAddress < CECDEVICE_BROADCAST)
286 return m_cec->IsPresentDevice(iAddress);
287 return false;
288 }
289
290 bool CLibCEC::IsActiveDeviceType(cec_device_type type)
291 {
292 if (m_cec && type >= CEC_DEVICE_TYPE_TV && type <= CEC_DEVICE_TYPE_AUDIO_SYSTEM)
293 return m_cec->IsPresentDeviceType(type);
294 return false;
295 }
296
297 uint8_t CLibCEC::VolumeUp(bool bSendRelease /* = true */)
298 {
299 if (m_cec)
300 return m_cec->VolumeUp(bSendRelease);
301 return 0;
302 }
303
304 uint8_t CLibCEC::VolumeDown(bool bSendRelease /* = true */)
305 {
306 if (m_cec)
307 return m_cec->VolumeDown(bSendRelease);
308 return 0;
309 }
310
311
312 uint8_t CLibCEC::MuteAudio(bool bSendRelease /* = true */)
313 {
314 if (m_cec)
315 return m_cec->MuteAudio(bSendRelease);
316 return 0;
317 }
318
319 bool CLibCEC::SendKeypress(cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = true */)
320 {
321 if (m_cec)
322 return m_cec->TransmitKeypress(iDestination, key, bWait);
323 return false;
324 }
325
326 bool CLibCEC::SendKeyRelease(cec_logical_address iDestination, bool bWait /* = true */)
327 {
328 if (m_cec)
329 return m_cec->TransmitKeyRelease(iDestination, bWait);
330 return false;
331 }
332
333 cec_osd_name CLibCEC::GetDeviceOSDName(cec_logical_address iAddress)
334 {
335 cec_osd_name retVal;
336 retVal.device = iAddress;
337 retVal.name[0] = 0;
338
339 if (m_cec)
340 retVal = m_cec->GetDeviceOSDName(iAddress);
341
342 return retVal;
343 }
344
345 void CLibCEC::AddLog(const cec_log_level level, const char *strFormat, ...)
346 {
347 CStdString strLog;
348
349 va_list argList;
350 va_start(argList, strFormat);
351 strLog.FormatV(strFormat, argList);
352 va_end(argList);
353
354 CLibCEC *instance = CLibCEC::GetInstance();
355 CLockObject lock(instance->m_mutex);
356
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());
361
362 if (instance->m_callbacks)
363 instance->m_callbacks->CBCecLogMessage(instance->m_cbParam, message);
364 else
365 instance->m_logBuffer.Push(message);
366 }
367
368 void CLibCEC::AddKey(const cec_keypress &key)
369 {
370 CLibCEC *instance = CLibCEC::GetInstance();
371 CLockObject lock(instance->m_mutex);
372
373 AddLog(CEC_LOG_DEBUG, "key pressed: %1x", key.keycode);
374
375 if (instance->m_callbacks)
376 instance->m_callbacks->CBCecKeyPress(instance->m_cbParam, key);
377 else
378 instance->m_keyBuffer.Push(key);
379
380 instance->m_iCurrentButton = key.duration > 0 ? CEC_USER_CONTROL_CODE_UNKNOWN : key.keycode;
381 instance->m_buttontime = key.duration > 0 ? 0 : GetTimeMs();
382 }
383
384 void CLibCEC::ConfigurationChanged(const libcec_configuration &config)
385 {
386 CLibCEC *instance = CLibCEC::GetInstance();
387 CLockObject lock(instance->m_mutex);
388
389 if (instance->m_callbacks &&
390 config.clientVersion >= CEC_CLIENT_VERSION_1_5_0 &&
391 instance->m_callbacks->CBCecConfigurationChanged != NULL &&
392 instance->m_cec->IsInitialised())
393 instance->m_callbacks->CBCecConfigurationChanged(instance->m_cbParam, config);
394 }
395
396 void CLibCEC::SetCurrentButton(cec_user_control_code iButtonCode)
397 {
398 /* push keypress to the keybuffer with 0 duration.
399 push another press to the keybuffer with the duration set when the button is released */
400 cec_keypress key;
401 key.duration = 0;
402 key.keycode = iButtonCode;
403
404 AddKey(key);
405 }
406
407 void CLibCEC::AddKey(void)
408 {
409 CLibCEC *instance = CLibCEC::GetInstance();
410 CLockObject lock(instance->m_mutex);
411
412 if (instance->m_iCurrentButton != CEC_USER_CONTROL_CODE_UNKNOWN)
413 {
414 cec_keypress key;
415
416 key.duration = (unsigned int) (GetTimeMs() - instance->m_buttontime);
417 key.keycode = instance->m_iCurrentButton;
418 AddLog(CEC_LOG_DEBUG, "key released: %1x", key.keycode);
419
420 if (instance->m_callbacks)
421 instance->m_callbacks->CBCecKeyPress(instance->m_cbParam, key);
422 else
423 instance->m_keyBuffer.Push(key);
424 instance->m_iCurrentButton = CEC_USER_CONTROL_CODE_UNKNOWN;
425 }
426 instance->m_buttontime = 0;
427 }
428
429 void CLibCEC::AddCommand(const cec_command &command)
430 {
431 CLibCEC *instance = CLibCEC::GetInstance();
432 CLockObject lock(instance->m_mutex);
433
434 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);
435
436 if (instance->m_callbacks)
437 instance->m_callbacks->CBCecCommand(instance->m_cbParam, command);
438 else if (!instance->m_commandBuffer.Push(command))
439 AddLog(CEC_LOG_WARNING, "command buffer is full");
440 }
441
442 void CLibCEC::CheckKeypressTimeout(void)
443 {
444 if (m_iCurrentButton != CEC_USER_CONTROL_CODE_UNKNOWN && GetTimeMs() - m_buttontime > CEC_BUTTON_TIMEOUT)
445 {
446 AddKey();
447 m_iCurrentButton = CEC_USER_CONTROL_CODE_UNKNOWN;
448 }
449 }
450
451 bool CLibCEC::SetStreamPath(cec_logical_address iAddress)
452 {
453 uint16_t iPhysicalAddress = GetDevicePhysicalAddress(iAddress);
454 if (iPhysicalAddress != 0xFFFF)
455 return SetStreamPath(iPhysicalAddress);
456 return false;
457 }
458
459 bool CLibCEC::SetStreamPath(uint16_t iPhysicalAddress)
460 {
461 return m_cec->SetStreamPath(iPhysicalAddress);
462 }
463
464 cec_logical_addresses CLibCEC::GetLogicalAddresses(void)
465 {
466 cec_logical_addresses addr = m_cec->GetLogicalAddresses();
467 return addr;
468 }
469
470 static CLibCEC *g_libCEC_instance(NULL);
471 CLibCEC *CLibCEC::GetInstance(void)
472 {
473 return g_libCEC_instance;
474 }
475
476 void CLibCEC::SetInstance(CLibCEC *instance)
477 {
478 if (g_libCEC_instance)
479 delete g_libCEC_instance;
480 g_libCEC_instance = instance;
481 }
482
483 void * CECInit(const char *strDeviceName, CEC::cec_device_type_list types, uint16_t UNUSED(iPhysicalAddress) /* = 0 */)
484 {
485 CLibCEC *lib = new CLibCEC(strDeviceName, types);
486 CLibCEC::SetInstance(lib);
487 return static_cast< void* > (lib);
488 }
489
490 void * CECInitialise(libcec_configuration *configuration)
491 {
492 CLibCEC *lib = new CLibCEC(configuration);
493 CLibCEC::SetInstance(lib);
494 return static_cast< void* > (lib);
495 }
496
497 void CECDestroy(CEC::ICECAdapter *UNUSED(instance))
498 {
499 CLibCEC::SetInstance(NULL);
500 }
501
502 const char *CLibCEC::ToString(const cec_menu_state state)
503 {
504 return m_cec->ToString(state);
505 }
506
507 const char *CLibCEC::ToString(const cec_version version)
508 {
509 return m_cec->ToString(version);
510 }
511
512 const char *CLibCEC::ToString(const cec_power_status status)
513 {
514 return m_cec->ToString(status);
515 }
516
517 const char *CLibCEC::ToString(const cec_logical_address address)
518 {
519 return m_cec->ToString(address);
520 }
521
522 const char *CLibCEC::ToString(const cec_deck_control_mode mode)
523 {
524 return m_cec->ToString(mode);
525 }
526
527 const char *CLibCEC::ToString(const cec_deck_info status)
528 {
529 return m_cec->ToString(status);
530 }
531
532 const char *CLibCEC::ToString(const cec_opcode opcode)
533 {
534 return m_cec->ToString(opcode);
535 }
536
537 const char *CLibCEC::ToString(const cec_system_audio_status mode)
538 {
539 return m_cec->ToString(mode);
540 }
541
542 const char *CLibCEC::ToString(const cec_audio_status status)
543 {
544 return m_cec->ToString(status);
545 }
546
547 const char *CLibCEC::ToString(const cec_vendor_id vendor)
548 {
549 return m_cec->ToString(vendor);
550 }
551
552 const char *CLibCEC::ToString(const cec_client_version version)
553 {
554 return m_cec->ToString(version);
555 }
556
557 const char *CLibCEC::ToString(const cec_server_version version)
558 {
559 return m_cec->ToString(version);
560 }
561
562 bool CLibCEC::GetCurrentConfiguration(libcec_configuration *configuration)
563 {
564 return m_cec->IsInitialised() && m_cec->GetCurrentConfiguration(configuration);
565 }
566
567 bool CLibCEC::SetConfiguration(const libcec_configuration *configuration)
568 {
569 return m_cec->SetConfiguration(configuration);
570 }
571
572 bool CLibCEC::CanPersistConfiguration(void)
573 {
574 return m_cec->CanPersistConfiguration();
575 }
576
577 bool CLibCEC::PersistConfiguration(libcec_configuration *configuration)
578 {
579 return m_cec->PersistConfiguration(configuration);
580 }
581
582 void CLibCEC::RescanActiveDevices(void)
583 {
584 return m_cec->RescanActiveDevices();
585 }
586
587 bool CLibCEC::IsLibCECActiveSource(void)
588 {
589 return m_cec ?
590 m_cec->m_busDevices[m_cec->GetActiveSource()]->GetStatus(false) == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC :
591 false;
592 }