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