no need to cache the physical address of the active source in the device map; the...
[deb_libcec.git] / src / lib / LibCEC.cpp
CommitLineData
2abe74eb
LOK
1/*
2 * This file is part of the libCEC(R) library.
3 *
b492c10e 4 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
2abe74eb
LOK
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
2b44051c 33#include "env.h"
2abe74eb
LOK
34#include "LibCEC.h"
35
2b44051c
LOK
36#include "adapter/AdapterFactory.h"
37#include "adapter/AdapterCommunication.h"
2abe74eb 38#include "CECProcessor.h"
004b8382 39#include "devices/CECAudioSystem.h"
0f23c85c 40#include "devices/CECBusDevice.h"
004b8382
LOK
41#include "devices/CECPlaybackDevice.h"
42#include "devices/CECTV.h"
ba65909d
LOK
43#include "platform/util/timeutils.h"
44#include "platform/util/StdString.h"
c9d15485 45#include "platform/util/util.h"
2abe74eb 46
004b8382
LOK
47#include "CECClient.h"
48
2abe74eb
LOK
49using namespace std;
50using namespace CEC;
f00ff009 51using namespace PLATFORM;
2abe74eb 52
cd7a6c70 53CLibCEC::CLibCEC(void) :
c30acafa
LOK
54 m_iStartTime(GetTimeMs()),
55 m_client(NULL)
caca2d81 56{
004b8382 57 m_cec = new CCECProcessor(this);
f8513317
LOK
58}
59
2abe74eb
LOK
60CLibCEC::~CLibCEC(void)
61{
c0152c09
LOK
62 // unregister all clients client
63 UnregisterClients();
64
65 // delete the adapter connection
c9d15485 66 DELETE_AND_NULL(m_cec);
2abe74eb
LOK
67}
68
b32ffd87 69bool CLibCEC::Open(const char *strPort, uint32_t iTimeoutMs /* = CEC_DEFAULT_CONNECT_TIMEOUT */)
2abe74eb 70{
c0152c09 71 if (!m_cec || !strPort)
99aeafb9
LOK
72 return false;
73
c0152c09 74 // open a new connection
b32ffd87 75 if (!m_cec->Start(strPort, CEC_SERIAL_DEFAULT_BAUDRATE, iTimeoutMs))
2abe74eb
LOK
76 {
77 AddLog(CEC_LOG_ERROR, "could not start CEC communications");
78 return false;
79 }
80
c0152c09 81 // register all clients
004b8382
LOK
82 for (vector<CCECClient *>::iterator it = m_clients.begin(); it != m_clients.end(); it++)
83 {
84 if (!m_cec->RegisterClient(*it))
85 {
86 AddLog(CEC_LOG_ERROR, "failed to register a CEC client");
87 return false;
88 }
89 }
90
2abe74eb
LOK
91 return true;
92}
93
94void CLibCEC::Close(void)
95{
af205cf4
LOK
96 if (!m_cec)
97 return;
98
c0152c09 99 // unregister all clients
af205cf4 100 m_cec->UnregisterClients();
c0152c09
LOK
101
102 // close the connection
af205cf4 103 m_cec->Close();
fa4798bd
LOK
104}
105
25701fa6 106int8_t CLibCEC::FindAdapters(cec_adapter *deviceList, uint8_t iBufSize, const char *strDevicePath /* = NULL */)
2abe74eb 107{
2b44051c 108 return CAdapterFactory(this).FindAdapters(deviceList, iBufSize, strDevicePath);
2abe74eb
LOK
109}
110
2abe74eb
LOK
111bool CLibCEC::StartBootloader(void)
112{
1113cb7d 113 return m_cec ? m_cec->StartBootloader() : false;
2abe74eb
LOK
114}
115
c0152c09 116bool CLibCEC::PingAdapter(void)
2abe74eb 117{
c0152c09 118 return m_client ? m_client->PingAdapter() : false;
2abe74eb
LOK
119}
120
c0152c09 121bool CLibCEC::EnableCallbacks(void *cbParam, ICECCallbacks *callbacks)
a9232a79 122{
c0152c09 123 return m_client ? m_client->EnableCallbacks(cbParam, callbacks) : false;
a9232a79
LOK
124}
125
c0152c09 126bool CLibCEC::GetCurrentConfiguration(libcec_configuration *configuration)
2abe74eb 127{
c0152c09 128 return m_client ? m_client->GetCurrentConfiguration(*configuration) : false;
2abe74eb
LOK
129}
130
c0152c09 131bool CLibCEC::SetConfiguration(const libcec_configuration *configuration)
28fa6c97 132{
c0152c09 133 return m_client ? m_client->SetConfiguration(*configuration) : false;
28fa6c97
LOK
134}
135
004b8382 136bool CLibCEC::CanPersistConfiguration(void)
1969b140 137{
c0152c09 138 return m_client ? m_client->CanPersistConfiguration() : false;
1969b140
LOK
139}
140
004b8382 141bool CLibCEC::PersistConfiguration(libcec_configuration *configuration)
8b7e5ff6 142{
c0152c09 143 return m_client ? m_client->PersistConfiguration(*configuration) : false;
8b7e5ff6
LOK
144}
145
004b8382 146void CLibCEC::RescanActiveDevices(void)
6a1c0009 147{
c0152c09
LOK
148 if (m_client)
149 m_client->RescanActiveDevices();
44c74256
LOK
150}
151
c0152c09 152bool CLibCEC::IsLibCECActiveSource(void)
eab72c40 153{
c0152c09 154 return m_client ? m_client->IsLibCECActiveSource() : false;
eab72c40
LOK
155}
156
004b8382 157bool CLibCEC::Transmit(const cec_command &data)
b4b1b49b 158{
2b44051c 159 return m_client ? m_client->Transmit(data, false) : false;
b4b1b49b
LOK
160}
161
004b8382 162bool CLibCEC::SetLogicalAddress(cec_logical_address iLogicalAddress)
b4b1b49b 163{
004b8382 164 return m_client ? m_client->SetLogicalAddress(iLogicalAddress) : false;
b4b1b49b
LOK
165}
166
004b8382 167bool CLibCEC::SetPhysicalAddress(uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */)
e55f3f70 168{
004b8382 169 return m_client ? m_client->SetPhysicalAddress(iPhysicalAddress) : false;
e55f3f70 170}
44c74256 171
004b8382 172bool CLibCEC::SetHDMIPort(cec_logical_address iBaseDevice, uint8_t iPort /* = CEC_DEFAULT_HDMI_PORT */)
57f45e6c 173{
004b8382 174 return m_client ? m_client->SetHDMIPort(iBaseDevice, iPort) : false;
57f45e6c
LOK
175}
176
004b8382 177bool CLibCEC::PowerOnDevices(cec_logical_address address /* = CECDEVICE_TV */)
6d858ba4 178{
004b8382 179 return m_client ? m_client->SendPowerOnDevices(address) : false;
6d858ba4
LOK
180}
181
004b8382 182bool CLibCEC::StandbyDevices(cec_logical_address address /* = CECDEVICE_BROADCAST */)
6d858ba4 183{
004b8382 184 return m_client ? m_client->SendStandbyDevices(address) : false;
6d858ba4
LOK
185}
186
004b8382 187bool CLibCEC::SetActiveSource(cec_device_type type /* = CEC_DEVICE_TYPE_RESERVED */)
6d858ba4 188{
004b8382 189 return m_client ? m_client->SendSetActiveSource(type) : false;
6d858ba4
LOK
190}
191
004b8382 192bool CLibCEC::SetDeckControlMode(cec_deck_control_mode mode, bool bSendUpdate /* = true */)
04e637f9 193{
004b8382 194 return m_client ? m_client->SendSetDeckControlMode(mode, bSendUpdate) : false;
04e637f9
LOK
195}
196
004b8382 197bool CLibCEC::SetDeckInfo(cec_deck_info info, bool bSendUpdate /* = true */)
04e637f9 198{
004b8382 199 return m_client ? m_client->SendSetDeckInfo(info, bSendUpdate) : false;
04e637f9
LOK
200}
201
004b8382 202bool CLibCEC::SetInactiveView(void)
04e637f9 203{
004b8382 204 return m_client ? m_client->SendSetInactiveView() : false;
04e637f9
LOK
205}
206
004b8382 207bool CLibCEC::SetMenuState(cec_menu_state state, bool bSendUpdate /* = true */)
a33794d8 208{
004b8382 209 return m_client ? m_client->SendSetMenuState(state, bSendUpdate) : false;
a33794d8
LOK
210}
211
004b8382 212bool CLibCEC::SetOSDString(cec_logical_address iLogicalAddress, cec_display_control duration, const char *strMessage)
a33794d8 213{
004b8382 214 return m_client ? m_client->SendSetOSDString(iLogicalAddress, duration, strMessage) : false;
a33794d8
LOK
215}
216
c0152c09
LOK
217bool CLibCEC::SwitchMonitoring(bool bEnable)
218{
219 return m_client ? m_client->SwitchMonitoring(bEnable) : false;
220}
221
004b8382 222cec_version CLibCEC::GetDeviceCecVersion(cec_logical_address iAddress)
ed21be2a 223{
004b8382 224 return m_client ? m_client->GetDeviceCecVersion(iAddress) : CEC_VERSION_UNKNOWN;
ed21be2a
LOK
225}
226
004b8382 227bool CLibCEC::GetDeviceMenuLanguage(cec_logical_address iAddress, cec_menu_language *language)
2abe74eb 228{
c0152c09 229 return m_client ? m_client->GetDeviceMenuLanguage(iAddress, *language) : false;
2abe74eb
LOK
230}
231
004b8382 232uint64_t CLibCEC::GetDeviceVendorId(cec_logical_address iAddress)
95ba7a09 233{
004b8382 234 return m_client ? m_client->GetDeviceVendorId(iAddress) : (uint64_t)CEC_VENDOR_UNKNOWN;
02e7043e
LOK
235}
236
004b8382 237uint16_t CLibCEC::GetDevicePhysicalAddress(cec_logical_address iAddress)
32403cc3 238{
004b8382 239 return m_client ? m_client->GetDevicePhysicalAddress(iAddress) : CEC_INVALID_PHYSICAL_ADDRESS;
32403cc3
LOK
240}
241
004b8382 242cec_power_status CLibCEC::GetDevicePowerStatus(cec_logical_address iAddress)
02e7043e 243{
004b8382 244 return m_client ? m_client->GetDevicePowerStatus(iAddress) : CEC_POWER_STATUS_UNKNOWN;
95ba7a09
LOK
245}
246
c0152c09
LOK
247bool CLibCEC::PollDevice(cec_logical_address iAddress)
248{
249 return m_client ? m_client->PollDevice(iAddress) : false;
250}
251
252cec_logical_addresses CLibCEC::GetActiveDevices(void)
253{
254 cec_logical_addresses addresses;
255 addresses.Clear();
256 if (m_client)
257 addresses = m_client->GetActiveDevices();
258 return addresses;
259}
260
261bool CLibCEC::IsActiveDevice(cec_logical_address iAddress)
262{
263 return m_client ? m_client->IsActiveDevice(iAddress) : false;
264}
265
266bool CLibCEC::IsActiveDeviceType(cec_device_type type)
267{
268 return m_client ? m_client->IsActiveDeviceType(type) : false;
269}
270
004b8382 271uint8_t CLibCEC::VolumeUp(bool bSendRelease /* = true */)
2abe74eb 272{
004b8382 273 return m_client ? m_client->SendVolumeUp(bSendRelease) : (uint8_t)CEC_AUDIO_VOLUME_STATUS_UNKNOWN;
2abe74eb
LOK
274}
275
004b8382 276uint8_t CLibCEC::VolumeDown(bool bSendRelease /* = true */)
2abe74eb 277{
004b8382 278 return m_client ? m_client->SendVolumeDown(bSendRelease) : (uint8_t)CEC_AUDIO_VOLUME_STATUS_UNKNOWN;
2abe74eb
LOK
279}
280
004b8382 281uint8_t CLibCEC::MuteAudio(bool UNUSED(bSendRelease) /* = true */)
b1c47f9d 282{
004b8382 283 return m_client ? m_client->SendMuteAudio() : (uint8_t)CEC_AUDIO_VOLUME_STATUS_UNKNOWN;
b1c47f9d
LOK
284}
285
004b8382 286bool CLibCEC::SendKeypress(cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = true */)
2abe74eb 287{
004b8382 288 return m_client ? m_client->SendKeypress(iDestination, key, bWait) : false;
2abe74eb
LOK
289}
290
004b8382 291bool CLibCEC::SendKeyRelease(cec_logical_address iDestination, bool bWait /* = true */)
e1804a4e 292{
004b8382 293 return m_client ? m_client->SendKeyRelease(iDestination, bWait) : false;
e1804a4e
LOK
294}
295
004b8382 296cec_osd_name CLibCEC::GetDeviceOSDName(cec_logical_address iAddress)
f42d3e0f 297{
004b8382 298 cec_osd_name retVal;
20505741
LOK
299 retVal.device = CECDEVICE_UNKNOWN;
300 memset(retVal.name, 0, 14);
301
004b8382
LOK
302 if (m_client)
303 retVal = m_client->GetDeviceOSDName(iAddress);
304 return retVal;
f42d3e0f
LOK
305}
306
c0152c09
LOK
307cec_logical_address CLibCEC::GetActiveSource(void)
308{
309 return m_client ? m_client->GetActiveSource() : CECDEVICE_UNKNOWN;
310}
311
312bool CLibCEC::IsActiveSource(cec_logical_address iAddress)
313{
314 return m_client ? m_client->IsActiveSource(iAddress) : false;
315}
316bool CLibCEC::SetStreamPath(cec_logical_address iAddress)
317{
318 return m_client ? m_client->SetStreamPath(iAddress) : false;
319}
320
321bool CLibCEC::SetStreamPath(uint16_t iPhysicalAddress)
322{
323 return m_client ? m_client->SetStreamPath(iPhysicalAddress) : false;
324}
325
004b8382 326cec_logical_addresses CLibCEC::GetLogicalAddresses(void)
f42d3e0f 327{
004b8382 328 cec_logical_addresses addresses;
c30acafa 329 addresses.Clear();
c0152c09
LOK
330 if (m_client)
331 m_client->GetLogicalAddresses();
004b8382 332 return addresses;
f42d3e0f
LOK
333}
334
004b8382 335cec_device_type CLibCEC::GetType(cec_logical_address address)
f8513317 336{
0d800fe5 337 return CCECTypeUtils::GetType(address);
f8513317
LOK
338}
339
004b8382 340uint16_t CLibCEC::GetMaskForType(cec_logical_address address)
caca2d81 341{
0d800fe5 342 return CCECTypeUtils::GetMaskForType(address);
caca2d81
LOK
343}
344
004b8382 345uint16_t CLibCEC::GetMaskForType(cec_device_type type)
a2198e5e 346{
0d800fe5 347 return CCECTypeUtils::GetMaskForType(type);
004b8382 348}
08d80226 349
004b8382
LOK
350bool CLibCEC::IsValidPhysicalAddress(uint16_t iPhysicalAddress)
351{
352 return iPhysicalAddress >= CEC_MIN_PHYSICAL_ADDRESS &&
353 iPhysicalAddress <= CEC_MAX_PHYSICAL_ADDRESS;
a2198e5e
LOK
354}
355
004b8382 356void CLibCEC::CheckKeypressTimeout(void)
a75e3a5a 357{
004b8382
LOK
358 // check all clients
359 for (vector<CCECClient *>::iterator it = m_clients.begin(); it != m_clients.end(); it++)
360 (*it)->CheckKeypressTimeout();
a75e3a5a
LOK
361}
362
004b8382 363void CLibCEC::AddLog(const cec_log_level level, const char *strFormat, ...)
d40928b5 364{
004b8382
LOK
365 CStdString strLog;
366
c0152c09 367 // format the message
004b8382
LOK
368 va_list argList;
369 va_start(argList, strFormat);
370 strLog.FormatV(strFormat, argList);
371 va_end(argList);
372
373 cec_log_message message;
374 message.level = level;
375 message.time = GetTimeMs() - m_iStartTime;
376 snprintf(message.message, sizeof(message.message), "%s", strLog.c_str());
377
378 // send the message to all clients
379 for (vector<CCECClient *>::iterator it = m_clients.begin(); it != m_clients.end(); it++)
380 (*it)->AddLog(message);
224ea877
LOK
381}
382
5d19989f
LOK
383void CLibCEC::AddCommand(const cec_command &command)
384{
385 // send the command to all clients
386 for (vector<CCECClient *>::iterator it = m_clients.begin(); it != m_clients.end(); it++)
387 (*it)->AddCommand(command);
388}
389
004b8382 390void CLibCEC::Alert(const libcec_alert type, const libcec_parameter &param)
30b4aac0 391{
004b8382
LOK
392 // send the alert to all clients
393 for (vector<CCECClient *>::iterator it = m_clients.begin(); it != m_clients.end(); it++)
394 (*it)->Alert(type, param);
30b4aac0
LOK
395}
396
c0152c09 397CCECClient *CLibCEC::RegisterClient(libcec_configuration &configuration)
3efda01a 398{
004b8382
LOK
399 if (!m_cec)
400 return NULL;
401
c0152c09 402 // create a new client instance
004b8382
LOK
403 CCECClient *newClient = new CCECClient(m_cec, configuration);
404 if (!newClient)
405 return NULL;
004b8382 406 m_clients.push_back(newClient);
c0152c09
LOK
407
408 // if the default client isn't set, set it
004b8382
LOK
409 if (!m_client)
410 m_client = newClient;
411
c0152c09 412 // register the new client
0b8c7eab 413 if (m_cec->CECInitialised())
004b8382
LOK
414 m_cec->RegisterClient(newClient);
415
99aeafb9 416 return newClient;
3efda01a 417}
c9549d35 418
004b8382 419void CLibCEC::UnregisterClients(void)
c9549d35 420{
aaff5cee 421 if (m_cec && m_cec->IsRunning())
c0152c09
LOK
422 m_cec->UnregisterClients();
423
004b8382 424 m_clients.clear();
c0152c09 425
c9d15485 426 DELETE_AND_NULL(m_client);
c9549d35 427}
9878069e 428
004b8382 429void * CECInitialise(libcec_configuration *configuration)
9878069e 430{
004b8382
LOK
431 if (!configuration)
432 return NULL;
433
c0152c09 434 // create a new libCEC instance
cd7a6c70 435 CLibCEC *lib = new CLibCEC;
c0152c09
LOK
436
437 // register a new client
99aeafb9 438 CCECClient *client(NULL);
c0152c09
LOK
439 if (lib && configuration)
440 client = lib->RegisterClient(*configuration);
99aeafb9 441
c0152c09 442 // update the current configuration
99aeafb9 443 if (client)
c0152c09 444 client->GetCurrentConfiguration(*configuration);
99aeafb9
LOK
445
446 // ensure that the correct server version is set
447 configuration->serverVersion = LIBCEC_VERSION_CURRENT;
004b8382
LOK
448
449 return static_cast< void* > (lib);
9878069e 450}
209b7a5c 451
38f1fbcc 452void * CECInit(const char *strDeviceName, CEC::cec_device_type_list types)
209b7a5c 453{
38f1fbcc 454 libcec_configuration configuration; configuration.Clear();
004b8382
LOK
455
456 // client version < 1.5.0
457 snprintf(configuration.strDeviceName, 13, "%s", strDeviceName);
458 configuration.deviceTypes = types;
aa169fd7 459 configuration.iPhysicalAddress = CEC_INVALID_PHYSICAL_ADDRESS;
004b8382
LOK
460
461 if (configuration.deviceTypes.IsEmpty())
462 configuration.deviceTypes.Add(CEC_DEVICE_TYPE_RECORDING_DEVICE);
463
464 return CECInitialise(&configuration);
209b7a5c
LOK
465}
466
004b8382 467bool CECStartBootloader(void)
209b7a5c 468{
004b8382
LOK
469 bool bReturn(false);
470 cec_adapter deviceList[1];
2b44051c 471 if (CAdapterFactory(NULL).FindAdapters(deviceList, 1, 0) > 0)
209b7a5c 472 {
2b44051c
LOK
473 CAdapterFactory factory(NULL);
474 IAdapterCommunication *comm = factory.GetInstance(deviceList[0].comm);
475 if (comm)
209b7a5c 476 {
2b44051c
LOK
477 CTimeout timeout(CEC_DEFAULT_CONNECT_TIMEOUT);
478 while (timeout.TimeLeft() > 0 &&
479 (bReturn = comm->Open(timeout.TimeLeft() / CEC_CONNECT_TRIES, true)) == false)
480 {
481 comm->Close();
482 CEvent::Sleep(500);
483 }
484 if (comm->IsOpen())
485 bReturn = comm->StartBootloader();
486
487 delete comm;
209b7a5c 488 }
209b7a5c 489 }
004b8382
LOK
490
491 return bReturn;
492}
493
494void CECDestroy(CEC::ICECAdapter *instance)
495{
c9d15485 496 DELETE_AND_NULL(instance);
209b7a5c 497}
f80cd208 498
b32ffd87 499bool CLibCEC::GetDeviceInformation(const char *strPort, libcec_configuration *config, uint32_t iTimeoutMs /* = CEC_DEFAULT_CONNECT_TIMEOUT */)
f80cd208
LOK
500{
501 if (m_cec->IsRunning())
502 return false;
004b8382 503
f80cd208 504 return m_cec->GetDeviceInformation(strPort, config, iTimeoutMs);
a75e3a5a 505}
c30acafa 506
2b44051c
LOK
507const char *CLibCEC::GetLibInfo(void)
508{
509#ifndef LIB_INFO
510#ifdef _WIN32
511#define FEATURES "'P8 USB' 'P8 USB detect'"
512#ifdef _WIN64
513#define HOST_TYPE "Windows (x64)"
514#else
515#define HOST_TYPE "Windows (x86)"
516#endif
517#else
518#define HOST_TYPE "unknown"
519#define FEATURES "unknown"
520#endif
521
522 return "host: " HOST_TYPE ", features: " FEATURES ", compiled: " __DATE__;
523#else
524 return LIB_INFO;
525#endif
526}
527
2b44051c
LOK
528void CLibCEC::InitVideoStandalone(void)
529{
530 CAdapterFactory::InitVideoStandalone();
531}
8768aeca
LOK
532uint16_t CLibCEC::GetAdapterVendorId(void) const
533{
534 return m_cec && m_cec->IsRunning() ? m_cec->GetAdapterVendorId() : 0;
535}
536
537uint16_t CLibCEC::GetAdapterProductId(void) const
538{
539 return m_cec && m_cec->IsRunning() ? m_cec->GetAdapterProductId() : 0;
540}