updated changelog for 1.9.0
[deb_libcec.git] / src / lib / LibCEC.cpp
... / ...
CommitLineData
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 "env.h"
34#include "LibCEC.h"
35
36#include "adapter/AdapterFactory.h"
37#include "adapter/AdapterCommunication.h"
38#include "CECProcessor.h"
39#include "CECTypeUtils.h"
40#include "devices/CECAudioSystem.h"
41#include "devices/CECBusDevice.h"
42#include "devices/CECPlaybackDevice.h"
43#include "devices/CECTV.h"
44#include "platform/util/timeutils.h"
45#include "platform/util/StdString.h"
46#include "platform/util/util.h"
47
48#include "CECClient.h"
49
50using namespace std;
51using namespace CEC;
52using namespace PLATFORM;
53
54//TODO replace deprecated constructor in 2.0
55CLibCEC::CLibCEC(const char *UNUSED(strDeviceName), cec_device_type_list UNUSED(types), uint16_t UNUSED(iPhysicalAddress) /* = 0 */) :
56 m_iStartTime(GetTimeMs()),
57 m_client(NULL)
58{
59 m_cec = new CCECProcessor(this);
60}
61
62//TODO replace deprecated constructor in 2.0
63CLibCEC::CLibCEC(libcec_configuration *UNUSED(configuration)) :
64 m_iStartTime(GetTimeMs()),
65 m_client(NULL)
66{
67 m_cec = new CCECProcessor(this);
68}
69
70CLibCEC::~CLibCEC(void)
71{
72 // unregister all clients client
73 UnregisterClients();
74
75 // delete the adapter connection
76 DELETE_AND_NULL(m_cec);
77}
78
79bool CLibCEC::Open(const char *strPort, uint32_t iTimeoutMs /* = CEC_DEFAULT_CONNECT_TIMEOUT */)
80{
81 if (!m_cec || !strPort)
82 return false;
83
84 // open a new connection
85 if (!m_cec->Start(strPort, CEC_SERIAL_DEFAULT_BAUDRATE, iTimeoutMs))
86 {
87 AddLog(CEC_LOG_ERROR, "could not start CEC communications");
88 return false;
89 }
90
91 // register all clients
92 for (vector<CCECClient *>::iterator it = m_clients.begin(); it != m_clients.end(); it++)
93 {
94 if (!m_cec->RegisterClient(*it))
95 {
96 AddLog(CEC_LOG_ERROR, "failed to register a CEC client");
97 return false;
98 }
99 }
100
101 return true;
102}
103
104void CLibCEC::Close(void)
105{
106 if (!m_cec)
107 return;
108
109 // unregister all clients
110 m_cec->UnregisterClients();
111
112 // close the connection
113 m_cec->Close();
114}
115
116int8_t CLibCEC::FindAdapters(cec_adapter *deviceList, uint8_t iBufSize, const char *strDevicePath /* = NULL */)
117{
118 return CAdapterFactory(this).FindAdapters(deviceList, iBufSize, strDevicePath);
119}
120
121bool CLibCEC::StartBootloader(void)
122{
123 return m_cec ? m_cec->StartBootloader() : false;
124}
125
126bool CLibCEC::PingAdapter(void)
127{
128 return m_client ? m_client->PingAdapter() : false;
129}
130
131bool CLibCEC::EnableCallbacks(void *cbParam, ICECCallbacks *callbacks)
132{
133 return m_client ? m_client->EnableCallbacks(cbParam, callbacks) : false;
134}
135
136bool CLibCEC::GetCurrentConfiguration(libcec_configuration *configuration)
137{
138 return m_client ? m_client->GetCurrentConfiguration(*configuration) : false;
139}
140
141bool CLibCEC::SetConfiguration(const libcec_configuration *configuration)
142{
143 return m_client ? m_client->SetConfiguration(*configuration) : false;
144}
145
146bool CLibCEC::CanPersistConfiguration(void)
147{
148 return m_client ? m_client->CanPersistConfiguration() : false;
149}
150
151bool CLibCEC::PersistConfiguration(libcec_configuration *configuration)
152{
153 return m_client ? m_client->PersistConfiguration(*configuration) : false;
154}
155
156void CLibCEC::RescanActiveDevices(void)
157{
158 if (m_client)
159 m_client->RescanActiveDevices();
160}
161
162bool CLibCEC::IsLibCECActiveSource(void)
163{
164 return m_client ? m_client->IsLibCECActiveSource() : false;
165}
166
167bool CLibCEC::Transmit(const cec_command &data)
168{
169 return m_client ? m_client->Transmit(data, false) : false;
170}
171
172bool CLibCEC::SetLogicalAddress(cec_logical_address iLogicalAddress)
173{
174 return m_client ? m_client->SetLogicalAddress(iLogicalAddress) : false;
175}
176
177bool CLibCEC::SetPhysicalAddress(uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */)
178{
179 return m_client ? m_client->SetPhysicalAddress(iPhysicalAddress) : false;
180}
181
182bool CLibCEC::SetHDMIPort(cec_logical_address iBaseDevice, uint8_t iPort /* = CEC_DEFAULT_HDMI_PORT */)
183{
184 return m_client ? m_client->SetHDMIPort(iBaseDevice, iPort) : false;
185}
186
187bool CLibCEC::PowerOnDevices(cec_logical_address address /* = CECDEVICE_TV */)
188{
189 return m_client ? m_client->SendPowerOnDevices(address) : false;
190}
191
192bool CLibCEC::StandbyDevices(cec_logical_address address /* = CECDEVICE_BROADCAST */)
193{
194 return m_client ? m_client->SendStandbyDevices(address) : false;
195}
196
197bool CLibCEC::SetActiveSource(cec_device_type type /* = CEC_DEVICE_TYPE_RESERVED */)
198{
199 return m_client ? m_client->SendSetActiveSource(type) : false;
200}
201
202bool CLibCEC::SetDeckControlMode(cec_deck_control_mode mode, bool bSendUpdate /* = true */)
203{
204 return m_client ? m_client->SendSetDeckControlMode(mode, bSendUpdate) : false;
205}
206
207bool CLibCEC::SetDeckInfo(cec_deck_info info, bool bSendUpdate /* = true */)
208{
209 return m_client ? m_client->SendSetDeckInfo(info, bSendUpdate) : false;
210}
211
212bool CLibCEC::SetInactiveView(void)
213{
214 return m_client ? m_client->SendSetInactiveView() : false;
215}
216
217bool CLibCEC::SetMenuState(cec_menu_state state, bool bSendUpdate /* = true */)
218{
219 return m_client ? m_client->SendSetMenuState(state, bSendUpdate) : false;
220}
221
222bool CLibCEC::SetOSDString(cec_logical_address iLogicalAddress, cec_display_control duration, const char *strMessage)
223{
224 return m_client ? m_client->SendSetOSDString(iLogicalAddress, duration, strMessage) : false;
225}
226
227bool CLibCEC::SwitchMonitoring(bool bEnable)
228{
229 return m_client ? m_client->SwitchMonitoring(bEnable) : false;
230}
231
232cec_version CLibCEC::GetDeviceCecVersion(cec_logical_address iAddress)
233{
234 return m_client ? m_client->GetDeviceCecVersion(iAddress) : CEC_VERSION_UNKNOWN;
235}
236
237bool CLibCEC::GetDeviceMenuLanguage(cec_logical_address iAddress, cec_menu_language *language)
238{
239 return m_client ? m_client->GetDeviceMenuLanguage(iAddress, *language) : false;
240}
241
242uint64_t CLibCEC::GetDeviceVendorId(cec_logical_address iAddress)
243{
244 return m_client ? m_client->GetDeviceVendorId(iAddress) : (uint64_t)CEC_VENDOR_UNKNOWN;
245}
246
247uint16_t CLibCEC::GetDevicePhysicalAddress(cec_logical_address iAddress)
248{
249 return m_client ? m_client->GetDevicePhysicalAddress(iAddress) : CEC_INVALID_PHYSICAL_ADDRESS;
250}
251
252cec_power_status CLibCEC::GetDevicePowerStatus(cec_logical_address iAddress)
253{
254 return m_client ? m_client->GetDevicePowerStatus(iAddress) : CEC_POWER_STATUS_UNKNOWN;
255}
256
257bool CLibCEC::PollDevice(cec_logical_address iAddress)
258{
259 return m_client ? m_client->PollDevice(iAddress) : false;
260}
261
262cec_logical_addresses CLibCEC::GetActiveDevices(void)
263{
264 cec_logical_addresses addresses;
265 addresses.Clear();
266 if (m_client)
267 addresses = m_client->GetActiveDevices();
268 return addresses;
269}
270
271bool CLibCEC::IsActiveDevice(cec_logical_address iAddress)
272{
273 return m_client ? m_client->IsActiveDevice(iAddress) : false;
274}
275
276bool CLibCEC::IsActiveDeviceType(cec_device_type type)
277{
278 return m_client ? m_client->IsActiveDeviceType(type) : false;
279}
280
281uint8_t CLibCEC::VolumeUp(bool bSendRelease /* = true */)
282{
283 return m_client ? m_client->SendVolumeUp(bSendRelease) : (uint8_t)CEC_AUDIO_VOLUME_STATUS_UNKNOWN;
284}
285
286uint8_t CLibCEC::VolumeDown(bool bSendRelease /* = true */)
287{
288 return m_client ? m_client->SendVolumeDown(bSendRelease) : (uint8_t)CEC_AUDIO_VOLUME_STATUS_UNKNOWN;
289}
290
291uint8_t CLibCEC::MuteAudio(bool UNUSED(bSendRelease) /* = true */)
292{
293 return m_client ? m_client->SendMuteAudio() : (uint8_t)CEC_AUDIO_VOLUME_STATUS_UNKNOWN;
294}
295
296bool CLibCEC::SendKeypress(cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = true */)
297{
298 return m_client ? m_client->SendKeypress(iDestination, key, bWait) : false;
299}
300
301bool CLibCEC::SendKeyRelease(cec_logical_address iDestination, bool bWait /* = true */)
302{
303 return m_client ? m_client->SendKeyRelease(iDestination, bWait) : false;
304}
305
306cec_osd_name CLibCEC::GetDeviceOSDName(cec_logical_address iAddress)
307{
308 cec_osd_name retVal;
309 retVal.device = CECDEVICE_UNKNOWN;
310 memset(retVal.name, 0, 14);
311
312 if (m_client)
313 retVal = m_client->GetDeviceOSDName(iAddress);
314 return retVal;
315}
316
317cec_logical_address CLibCEC::GetActiveSource(void)
318{
319 return m_client ? m_client->GetActiveSource() : CECDEVICE_UNKNOWN;
320}
321
322bool CLibCEC::IsActiveSource(cec_logical_address iAddress)
323{
324 return m_client ? m_client->IsActiveSource(iAddress) : false;
325}
326bool CLibCEC::SetStreamPath(cec_logical_address iAddress)
327{
328 return m_client ? m_client->SetStreamPath(iAddress) : false;
329}
330
331bool CLibCEC::SetStreamPath(uint16_t iPhysicalAddress)
332{
333 return m_client ? m_client->SetStreamPath(iPhysicalAddress) : false;
334}
335
336cec_logical_addresses CLibCEC::GetLogicalAddresses(void)
337{
338 cec_logical_addresses addresses;
339 addresses.Clear();
340 if (m_client)
341 m_client->GetLogicalAddresses();
342 return addresses;
343}
344
345bool CLibCEC::GetNextLogMessage(cec_log_message *message)
346{
347 return m_client ? m_client->GetNextLogMessage(message) : false;
348}
349
350bool CLibCEC::GetNextKeypress(cec_keypress *key)
351{
352 return m_client ? m_client->GetNextKeypress(key) : false;
353}
354
355bool CLibCEC::GetNextCommand(cec_command *command)
356{
357 return m_client ? m_client->GetNextCommand(command) : false;
358}
359
360cec_device_type CLibCEC::GetType(cec_logical_address address)
361{
362 return CCECTypeUtils::GetType(address);
363}
364
365uint16_t CLibCEC::GetMaskForType(cec_logical_address address)
366{
367 return CCECTypeUtils::GetMaskForType(address);
368}
369
370uint16_t CLibCEC::GetMaskForType(cec_device_type type)
371{
372 return CCECTypeUtils::GetMaskForType(type);
373}
374
375bool CLibCEC::IsValidPhysicalAddress(uint16_t iPhysicalAddress)
376{
377 return iPhysicalAddress >= CEC_MIN_PHYSICAL_ADDRESS &&
378 iPhysicalAddress <= CEC_MAX_PHYSICAL_ADDRESS;
379}
380
381const char *CLibCEC::ToString(const cec_device_type type)
382{
383 return CCECTypeUtils::ToString(type);
384}
385
386const char *CLibCEC::ToString(const cec_menu_state state)
387{
388 return CCECTypeUtils::ToString(state);
389}
390
391const char *CLibCEC::ToString(const cec_version version)
392{
393 return CCECTypeUtils::ToString(version);
394}
395
396const char *CLibCEC::ToString(const cec_power_status status)
397{
398 return CCECTypeUtils::ToString(status);
399}
400
401const char *CLibCEC::ToString(const cec_logical_address address)
402{
403 return CCECTypeUtils::ToString(address);
404}
405
406const char *CLibCEC::ToString(const cec_deck_control_mode mode)
407{
408 return CCECTypeUtils::ToString(mode);
409}
410
411const char *CLibCEC::ToString(const cec_deck_info status)
412{
413 return CCECTypeUtils::ToString(status);
414}
415
416const char *CLibCEC::ToString(const cec_opcode opcode)
417{
418 return CCECTypeUtils::ToString(opcode);
419}
420
421const char *CLibCEC::ToString(const cec_system_audio_status mode)
422{
423 return CCECTypeUtils::ToString(mode);
424}
425
426const char *CLibCEC::ToString(const cec_audio_status status)
427{
428 return CCECTypeUtils::ToString(status);
429}
430
431const char *CLibCEC::ToString(const cec_vendor_id vendor)
432{
433 return CCECTypeUtils::ToString(vendor);
434}
435
436const char *CLibCEC::ToString(const cec_client_version version)
437{
438 return CCECTypeUtils::ToString(version);
439}
440
441const char *CLibCEC::ToString(const cec_server_version version)
442{
443 return CCECTypeUtils::ToString(version);
444}
445
446void CLibCEC::CheckKeypressTimeout(void)
447{
448 // check all clients
449 for (vector<CCECClient *>::iterator it = m_clients.begin(); it != m_clients.end(); it++)
450 (*it)->CheckKeypressTimeout();
451}
452
453void CLibCEC::AddLog(const cec_log_level level, const char *strFormat, ...)
454{
455 CStdString strLog;
456
457 // format the message
458 va_list argList;
459 va_start(argList, strFormat);
460 strLog.FormatV(strFormat, argList);
461 va_end(argList);
462
463 cec_log_message message;
464 message.level = level;
465 message.time = GetTimeMs() - m_iStartTime;
466 snprintf(message.message, sizeof(message.message), "%s", strLog.c_str());
467
468 // send the message to all clients
469 for (vector<CCECClient *>::iterator it = m_clients.begin(); it != m_clients.end(); it++)
470 (*it)->AddLog(message);
471}
472
473void CLibCEC::AddCommand(const cec_command &command)
474{
475 // send the command to all clients
476 for (vector<CCECClient *>::iterator it = m_clients.begin(); it != m_clients.end(); it++)
477 (*it)->AddCommand(command);
478}
479
480void CLibCEC::Alert(const libcec_alert type, const libcec_parameter &param)
481{
482 // send the alert to all clients
483 for (vector<CCECClient *>::iterator it = m_clients.begin(); it != m_clients.end(); it++)
484 (*it)->Alert(type, param);
485}
486
487bool CLibCEC::SetActiveView(void)
488{
489 AddLog(CEC_LOG_WARNING, "deprecated method %s called", __FUNCTION__);
490 return SetActiveSource();
491}
492
493bool CLibCEC::EnablePhysicalAddressDetection(void)
494{
495 AddLog(CEC_LOG_WARNING, "deprecated method %s called", __FUNCTION__);
496 return true;
497}
498
499CCECClient *CLibCEC::RegisterClient(libcec_configuration &configuration)
500{
501 if (!m_cec)
502 return NULL;
503
504 // create a new client instance
505 CCECClient *newClient = new CCECClient(m_cec, configuration);
506 if (!newClient)
507 return NULL;
508 m_clients.push_back(newClient);
509
510 // if the default client isn't set, set it
511 if (!m_client)
512 m_client = newClient;
513
514 // register the new client
515 if (m_cec->CECInitialised())
516 m_cec->RegisterClient(newClient);
517
518 return newClient;
519}
520
521void CLibCEC::UnregisterClients(void)
522{
523 if (m_cec)
524 m_cec->UnregisterClients();
525
526 m_clients.clear();
527
528 DELETE_AND_NULL(m_client);
529}
530
531void * CECInitialise(libcec_configuration *configuration)
532{
533 if (!configuration)
534 return NULL;
535
536 // create a new libCEC instance
537 CLibCEC *lib = new CLibCEC(NULL);
538
539 // register a new client
540 CCECClient *client(NULL);
541 if (lib && configuration)
542 client = lib->RegisterClient(*configuration);
543
544 // update the current configuration
545 if (client)
546 client->GetCurrentConfiguration(*configuration);
547
548 // ensure that the correct server version is set
549 configuration->serverVersion = LIBCEC_VERSION_CURRENT;
550
551 return static_cast< void* > (lib);
552}
553
554void * CECInit(const char *strDeviceName, CEC::cec_device_type_list types)
555{
556 libcec_configuration configuration; configuration.Clear();
557
558 // client version < 1.5.0
559 snprintf(configuration.strDeviceName, 13, "%s", strDeviceName);
560 configuration.deviceTypes = types;
561 configuration.iPhysicalAddress = CEC_INVALID_PHYSICAL_ADDRESS;
562
563 if (configuration.deviceTypes.IsEmpty())
564 configuration.deviceTypes.Add(CEC_DEVICE_TYPE_RECORDING_DEVICE);
565
566 return CECInitialise(&configuration);
567}
568
569void * CECCreate(const char *strDeviceName, CEC::cec_logical_address iLogicalAddress /* = CEC::CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */)
570{
571 libcec_configuration configuration; configuration.Clear();
572
573 // client version < 1.5.0
574 snprintf(configuration.strDeviceName, 13, "%s", strDeviceName);
575 configuration.iPhysicalAddress = iPhysicalAddress;
576 configuration.deviceTypes.Add(CCECTypeUtils::GetType(iLogicalAddress));
577
578 return CECInitialise(&configuration);
579}
580
581bool CECStartBootloader(void)
582{
583 bool bReturn(false);
584 cec_adapter deviceList[1];
585 if (CAdapterFactory(NULL).FindAdapters(deviceList, 1, 0) > 0)
586 {
587 CAdapterFactory factory(NULL);
588 IAdapterCommunication *comm = factory.GetInstance(deviceList[0].comm);
589 if (comm)
590 {
591 CTimeout timeout(CEC_DEFAULT_CONNECT_TIMEOUT);
592 while (timeout.TimeLeft() > 0 &&
593 (bReturn = comm->Open(timeout.TimeLeft() / CEC_CONNECT_TRIES, true)) == false)
594 {
595 comm->Close();
596 CEvent::Sleep(500);
597 }
598 if (comm->IsOpen())
599 bReturn = comm->StartBootloader();
600
601 delete comm;
602 }
603 }
604
605 return bReturn;
606}
607
608void CECDestroy(CEC::ICECAdapter *instance)
609{
610 DELETE_AND_NULL(instance);
611}
612
613bool CLibCEC::GetDeviceInformation(const char *strPort, libcec_configuration *config, uint32_t iTimeoutMs /* = CEC_DEFAULT_CONNECT_TIMEOUT */)
614{
615 if (m_cec->IsRunning())
616 return false;
617
618 return m_cec->GetDeviceInformation(strPort, config, iTimeoutMs);
619}
620
621const char *CLibCEC::GetLibInfo(void)
622{
623#ifndef LIB_INFO
624#ifdef _WIN32
625#define FEATURES "'P8 USB' 'P8 USB detect'"
626#ifdef _WIN64
627#define HOST_TYPE "Windows (x64)"
628#else
629#define HOST_TYPE "Windows (x86)"
630#endif
631#else
632#define HOST_TYPE "unknown"
633#define FEATURES "unknown"
634#endif
635
636 return "host: " HOST_TYPE ", features: " FEATURES ", compiled: " __DATE__;
637#else
638 return LIB_INFO;
639#endif
640}
641
642const char *CLibCEC::ToString(const cec_user_control_code key)
643{
644 return CCECTypeUtils::ToString(key);
645}
646
647void CLibCEC::InitVideoStandalone(void)
648{
649 CAdapterFactory::InitVideoStandalone();
650}
651
652const char *CLibCEC::ToString(const cec_adapter_type type)
653{
654 return CCECTypeUtils::ToString(type);
655}
656
657// no longer being used
658void CLibCEC::AddKey(const cec_keypress &UNUSED(key)) {}
659void CLibCEC::ConfigurationChanged(const libcec_configuration &UNUSED(config)) {}
660void CLibCEC::SetCurrentButton(cec_user_control_code UNUSED(iButtonCode)) {}
661CLibCEC *CLibCEC::GetInstance(void) { return NULL; }
662void CLibCEC::SetInstance(CLibCEC *UNUSED(instance)) {}