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