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