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