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