daa0b551d4749f1f5bfdffd2d50139662e2066ed
[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 "devices/CECAudioSystem.h"
39 #include "devices/CECBusDevice.h"
40 #include "devices/CECPlaybackDevice.h"
41 #include "devices/CECTV.h"
42 #include "platform/util/timeutils.h"
43 #include "platform/util/StdString.h"
44
45 #include "CECClient.h"
46
47 using namespace std;
48 using namespace CEC;
49 using namespace PLATFORM;
50
51 CLibCEC::CLibCEC(void) :
52 m_client(NULL),
53 m_iStartTime(GetTimeMs())
54 {
55 m_cec = new CCECProcessor(this);
56 }
57
58 CLibCEC::~CLibCEC(void)
59 {
60 delete m_client;
61 m_client = NULL;
62 delete m_cec;
63 m_cec = NULL;
64 }
65
66 bool CLibCEC::Open(const char *strPort, uint32_t iTimeoutMs /* = CEC_DEFAULT_CONNECT_TIMEOUT */)
67 {
68 if (!m_cec)
69 return false;
70
71 if (m_cec->IsRunning())
72 {
73 AddLog(CEC_LOG_ERROR, "connection already open");
74 return false;
75 }
76
77 if (!m_cec->Start(strPort, CEC_SERIAL_DEFAULT_BAUDRATE, iTimeoutMs))
78 {
79 AddLog(CEC_LOG_ERROR, "could not start CEC communications");
80 return false;
81 }
82
83 for (vector<CCECClient *>::iterator it = m_clients.begin(); it != m_clients.end(); it++)
84 {
85 if (!m_cec->RegisterClient(*it))
86 {
87 AddLog(CEC_LOG_ERROR, "failed to register a CEC client");
88 return false;
89 }
90 }
91
92 return true;
93 }
94
95 void CLibCEC::Close(void)
96 {
97 if (m_cec)
98 m_cec->Close();
99
100 for (vector<CCECClient *>::iterator it = m_clients.begin(); it != m_clients.end(); it++)
101 {
102 (*it)->SetRegistered(false);
103 (*it)->SetInitialised(false);
104 }
105 }
106
107 int8_t CLibCEC::FindAdapters(cec_adapter *deviceList, uint8_t iBufSize, const char *strDevicePath /* = NULL */)
108 {
109 return CUSBCECAdapterDetection::FindAdapters(deviceList, iBufSize, strDevicePath);
110 }
111
112 bool CLibCEC::PingAdapter(void)
113 {
114 return m_cec ? m_cec->PingAdapter() : false;
115 }
116
117 bool CLibCEC::StartBootloader(void)
118 {
119 return m_cec ? m_cec->StartBootloader() : false;
120 }
121
122 bool CLibCEC::SwitchMonitoring(bool bEnable)
123 {
124 return m_cec ? m_cec->SwitchMonitoring(bEnable) : false;
125 }
126
127 cec_logical_address CLibCEC::GetActiveSource(void)
128 {
129 return m_cec ? m_cec->GetActiveSource() : CECDEVICE_UNKNOWN;
130 }
131
132 bool CLibCEC::IsActiveSource(cec_logical_address iAddress)
133 {
134 return m_cec ? m_cec->IsActiveSource(iAddress) : false;
135 }
136
137 bool CLibCEC::PollDevice(cec_logical_address iAddress)
138 {
139 return m_cec ? m_cec->PollDevice(iAddress) : false;
140 }
141
142 cec_logical_addresses CLibCEC::GetActiveDevices(void)
143 {
144 CECDEVICEVEC activeDevices;
145 if (m_cec)
146 m_cec->GetDevices()->GetActive(activeDevices);
147 return CCECDeviceMap::ToLogicalAddresses(activeDevices);
148 }
149
150 bool CLibCEC::IsActiveDevice(cec_logical_address iAddress)
151 {
152 cec_logical_addresses activeDevices = GetActiveDevices();
153 return activeDevices.IsSet(iAddress);
154 }
155
156 bool CLibCEC::IsActiveDeviceType(cec_device_type type)
157 {
158 CECDEVICEVEC activeDevices;
159 if (m_cec)
160 m_cec->GetDevices()->GetActive(activeDevices);
161 CCECDeviceMap::FilterType(type, activeDevices);
162 return !activeDevices.empty();
163 }
164
165 bool CLibCEC::SetStreamPath(cec_logical_address iAddress)
166 {
167 uint16_t iPhysicalAddress = GetDevicePhysicalAddress(iAddress);
168 if (iPhysicalAddress != CEC_INVALID_PHYSICAL_ADDRESS)
169 return SetStreamPath(iPhysicalAddress);
170 return false;
171 }
172
173 bool CLibCEC::SetStreamPath(uint16_t iPhysicalAddress)
174 {
175 return m_cec->SetStreamPath(iPhysicalAddress);
176 }
177
178 bool CLibCEC::IsLibCECActiveSource(void)
179 {
180 bool bReturn(false);
181 if (m_cec)
182 {
183 cec_logical_address activeSource = m_cec->GetActiveSource();
184 CCECBusDevice *device = m_cec->GetDevice(activeSource);
185 if (device)
186 bReturn = device->IsHandledByLibCEC();
187 }
188 return bReturn;
189 }
190
191 bool CLibCEC::CanPersistConfiguration(void)
192 {
193 return m_cec->CanPersistConfiguration();
194 }
195
196 bool CLibCEC::PersistConfiguration(libcec_configuration *configuration)
197 {
198 return m_cec->PersistConfiguration(configuration);
199 }
200
201 void CLibCEC::RescanActiveDevices(void)
202 {
203 return m_cec->RescanActiveDevices();
204 }
205
206 bool CLibCEC::EnableCallbacks(void *cbParam, ICECCallbacks *callbacks)
207 {
208 return m_client ? m_client->EnableCallbacks(cbParam, callbacks) : false;
209 }
210
211 bool CLibCEC::GetCurrentConfiguration(libcec_configuration *configuration)
212 {
213 return m_client ? m_client->GetCurrentConfiguration(configuration) : false;
214 }
215
216 bool CLibCEC::SetConfiguration(const libcec_configuration *configuration)
217 {
218 return m_client ? m_client->SetConfiguration(configuration) : false;
219 }
220
221 bool CLibCEC::Transmit(const cec_command &data)
222 {
223 return m_client ? m_client->Transmit(data) : false;
224 }
225
226 bool CLibCEC::SetLogicalAddress(cec_logical_address iLogicalAddress)
227 {
228 return m_client ? m_client->SetLogicalAddress(iLogicalAddress) : false;
229 }
230
231 bool CLibCEC::SetPhysicalAddress(uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */)
232 {
233 return m_client ? m_client->SetPhysicalAddress(iPhysicalAddress) : false;
234 }
235
236 bool CLibCEC::SetHDMIPort(cec_logical_address iBaseDevice, uint8_t iPort /* = CEC_DEFAULT_HDMI_PORT */)
237 {
238 return m_client ? m_client->SetHDMIPort(iBaseDevice, iPort) : false;
239 }
240
241 bool CLibCEC::PowerOnDevices(cec_logical_address address /* = CECDEVICE_TV */)
242 {
243 return m_client ? m_client->SendPowerOnDevices(address) : false;
244 }
245
246 bool CLibCEC::StandbyDevices(cec_logical_address address /* = CECDEVICE_BROADCAST */)
247 {
248 return m_client ? m_client->SendStandbyDevices(address) : false;
249 }
250
251 bool CLibCEC::SetActiveSource(cec_device_type type /* = CEC_DEVICE_TYPE_RESERVED */)
252 {
253 return m_client ? m_client->SendSetActiveSource(type) : false;
254 }
255
256 bool CLibCEC::SetDeckControlMode(cec_deck_control_mode mode, bool bSendUpdate /* = true */)
257 {
258 return m_client ? m_client->SendSetDeckControlMode(mode, bSendUpdate) : false;
259 }
260
261 bool CLibCEC::SetDeckInfo(cec_deck_info info, bool bSendUpdate /* = true */)
262 {
263 return m_client ? m_client->SendSetDeckInfo(info, bSendUpdate) : false;
264 }
265
266 bool CLibCEC::SetInactiveView(void)
267 {
268 return m_client ? m_client->SendSetInactiveView() : false;
269 }
270
271 bool CLibCEC::SetMenuState(cec_menu_state state, bool bSendUpdate /* = true */)
272 {
273 return m_client ? m_client->SendSetMenuState(state, bSendUpdate) : false;
274 }
275
276 bool CLibCEC::SetOSDString(cec_logical_address iLogicalAddress, cec_display_control duration, const char *strMessage)
277 {
278 return m_client ? m_client->SendSetOSDString(iLogicalAddress, duration, strMessage) : false;
279 }
280
281 cec_version CLibCEC::GetDeviceCecVersion(cec_logical_address iAddress)
282 {
283 return m_client ? m_client->GetDeviceCecVersion(iAddress) : CEC_VERSION_UNKNOWN;
284 }
285
286 bool CLibCEC::GetDeviceMenuLanguage(cec_logical_address iAddress, cec_menu_language *language)
287 {
288 return m_client ? m_client->GetDeviceMenuLanguage(iAddress, language) : false;
289 }
290
291 uint64_t CLibCEC::GetDeviceVendorId(cec_logical_address iAddress)
292 {
293 return m_client ? m_client->GetDeviceVendorId(iAddress) : (uint64_t)CEC_VENDOR_UNKNOWN;
294 }
295
296 uint16_t CLibCEC::GetDevicePhysicalAddress(cec_logical_address iAddress)
297 {
298 return m_client ? m_client->GetDevicePhysicalAddress(iAddress) : CEC_INVALID_PHYSICAL_ADDRESS;
299 }
300
301 cec_power_status CLibCEC::GetDevicePowerStatus(cec_logical_address iAddress)
302 {
303 return m_client ? m_client->GetDevicePowerStatus(iAddress) : CEC_POWER_STATUS_UNKNOWN;
304 }
305
306 uint8_t CLibCEC::VolumeUp(bool bSendRelease /* = true */)
307 {
308 return m_client ? m_client->SendVolumeUp(bSendRelease) : (uint8_t)CEC_AUDIO_VOLUME_STATUS_UNKNOWN;
309 }
310
311 uint8_t CLibCEC::VolumeDown(bool bSendRelease /* = true */)
312 {
313 return m_client ? m_client->SendVolumeDown(bSendRelease) : (uint8_t)CEC_AUDIO_VOLUME_STATUS_UNKNOWN;
314 }
315
316 uint8_t CLibCEC::MuteAudio(bool UNUSED(bSendRelease) /* = true */)
317 {
318 return m_client ? m_client->SendMuteAudio() : (uint8_t)CEC_AUDIO_VOLUME_STATUS_UNKNOWN;
319 }
320
321 bool CLibCEC::SendKeypress(cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = true */)
322 {
323 return m_client ? m_client->SendKeypress(iDestination, key, bWait) : false;
324 }
325
326 bool CLibCEC::SendKeyRelease(cec_logical_address iDestination, bool bWait /* = true */)
327 {
328 return m_client ? m_client->SendKeyRelease(iDestination, bWait) : false;
329 }
330
331 cec_osd_name CLibCEC::GetDeviceOSDName(cec_logical_address iAddress)
332 {
333 cec_osd_name retVal;
334 if (m_client)
335 retVal = m_client->GetDeviceOSDName(iAddress);
336 return retVal;
337 }
338
339 cec_logical_addresses CLibCEC::GetLogicalAddresses(void)
340 {
341 cec_logical_addresses addresses;
342 if (m_cec)
343 addresses = m_cec->GetLogicalAddresses();
344 return addresses;
345 }
346
347 bool CLibCEC::GetNextLogMessage(cec_log_message *message)
348 {
349 return m_client ? m_client->GetNextLogMessage(message) : false;
350 }
351
352 bool CLibCEC::GetNextKeypress(cec_keypress *key)
353 {
354 return m_client ? m_client->GetNextKeypress(key) : false;
355 }
356
357 bool CLibCEC::GetNextCommand(cec_command *command)
358 {
359 return m_client ? m_client->GetNextCommand(command) : false;
360 }
361
362 cec_device_type CLibCEC::GetType(cec_logical_address address)
363 {
364 switch (address)
365 {
366 case CECDEVICE_AUDIOSYSTEM:
367 return CEC_DEVICE_TYPE_AUDIO_SYSTEM;
368 case CECDEVICE_PLAYBACKDEVICE1:
369 case CECDEVICE_PLAYBACKDEVICE2:
370 case CECDEVICE_PLAYBACKDEVICE3:
371 return CEC_DEVICE_TYPE_PLAYBACK_DEVICE;
372 case CECDEVICE_RECORDINGDEVICE1:
373 case CECDEVICE_RECORDINGDEVICE2:
374 case CECDEVICE_RECORDINGDEVICE3:
375 return CEC_DEVICE_TYPE_RECORDING_DEVICE;
376 case CECDEVICE_TUNER1:
377 case CECDEVICE_TUNER2:
378 case CECDEVICE_TUNER3:
379 case CECDEVICE_TUNER4:
380 return CEC_DEVICE_TYPE_TUNER;
381 case CECDEVICE_TV:
382 return CEC_DEVICE_TYPE_TV;
383 default:
384 return CEC_DEVICE_TYPE_RESERVED;
385 }
386 }
387
388 uint16_t CLibCEC::GetMaskForType(cec_logical_address address)
389 {
390 return GetMaskForType(GetType(address));
391 }
392
393 uint16_t CLibCEC::GetMaskForType(cec_device_type type)
394 {
395 switch (type)
396 {
397 case CEC_DEVICE_TYPE_AUDIO_SYSTEM:
398 {
399 cec_logical_addresses addr;
400 addr.Clear();
401 addr.Set(CECDEVICE_AUDIOSYSTEM);
402 return addr.AckMask();
403 }
404 case CEC_DEVICE_TYPE_PLAYBACK_DEVICE:
405 {
406 cec_logical_addresses addr;
407 addr.Clear();
408 addr.Set(CECDEVICE_PLAYBACKDEVICE1);
409 addr.Set(CECDEVICE_PLAYBACKDEVICE2);
410 addr.Set(CECDEVICE_PLAYBACKDEVICE3);
411 return addr.AckMask();
412 }
413 case CEC_DEVICE_TYPE_RECORDING_DEVICE:
414 {
415 cec_logical_addresses addr;
416 addr.Clear();
417 addr.Set(CECDEVICE_RECORDINGDEVICE1);
418 addr.Set(CECDEVICE_RECORDINGDEVICE2);
419 addr.Set(CECDEVICE_RECORDINGDEVICE3);
420 return addr.AckMask();
421 }
422 case CEC_DEVICE_TYPE_TUNER:
423 {
424 cec_logical_addresses addr;
425 addr.Clear();
426 addr.Set(CECDEVICE_TUNER1);
427 addr.Set(CECDEVICE_TUNER2);
428 addr.Set(CECDEVICE_TUNER3);
429 addr.Set(CECDEVICE_TUNER4);
430 return addr.AckMask();
431 }
432 case CEC_DEVICE_TYPE_TV:
433 {
434 cec_logical_addresses addr;
435 addr.Clear();
436 addr.Set(CECDEVICE_TV);
437 return addr.AckMask();
438 }
439 default:
440 return 0;
441 }
442 }
443
444 bool CLibCEC::IsValidPhysicalAddress(uint16_t iPhysicalAddress)
445 {
446 return iPhysicalAddress >= CEC_MIN_PHYSICAL_ADDRESS &&
447 iPhysicalAddress <= CEC_MAX_PHYSICAL_ADDRESS;
448 }
449
450 const char *CLibCEC::ToString(const cec_device_type type)
451 {
452 switch (type)
453 {
454 case CEC_DEVICE_TYPE_AUDIO_SYSTEM:
455 return "audio system";
456 case CEC_DEVICE_TYPE_PLAYBACK_DEVICE:
457 return "playback device";
458 case CEC_DEVICE_TYPE_RECORDING_DEVICE:
459 return "recording device";
460 case CEC_DEVICE_TYPE_RESERVED:
461 return "reserved";
462 case CEC_DEVICE_TYPE_TUNER:
463 return "tuner";
464 case CEC_DEVICE_TYPE_TV:
465 return "TV";
466 default:
467 return "unknown";
468 }
469 }
470
471 const char *CLibCEC::ToString(const cec_menu_state state)
472 {
473 switch (state)
474 {
475 case CEC_MENU_STATE_ACTIVATED:
476 return "activated";
477 case CEC_MENU_STATE_DEACTIVATED:
478 return "deactivated";
479 default:
480 return "unknown";
481 }
482 }
483
484 const char *CLibCEC::ToString(const cec_version version)
485 {
486 switch (version)
487 {
488 case CEC_VERSION_1_2:
489 return "1.2";
490 case CEC_VERSION_1_2A:
491 return "1.2a";
492 case CEC_VERSION_1_3:
493 return "1.3";
494 case CEC_VERSION_1_3A:
495 return "1.3a";
496 case CEC_VERSION_1_4:
497 return "1.4";
498 default:
499 return "unknown";
500 }
501 }
502
503 const char *CLibCEC::ToString(const cec_power_status status)
504 {
505 switch (status)
506 {
507 case CEC_POWER_STATUS_ON:
508 return "on";
509 case CEC_POWER_STATUS_STANDBY:
510 return "standby";
511 case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY:
512 return "in transition from on to standby";
513 case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON:
514 return "in transition from standby to on";
515 default:
516 return "unknown";
517 }
518 }
519
520 const char *CLibCEC::ToString(const cec_logical_address address)
521 {
522 switch(address)
523 {
524 case CECDEVICE_AUDIOSYSTEM:
525 return "Audio";
526 case CECDEVICE_BROADCAST:
527 return "Broadcast";
528 case CECDEVICE_FREEUSE:
529 return "Free use";
530 case CECDEVICE_PLAYBACKDEVICE1:
531 return "Playback 1";
532 case CECDEVICE_PLAYBACKDEVICE2:
533 return "Playback 2";
534 case CECDEVICE_PLAYBACKDEVICE3:
535 return "Playback 3";
536 case CECDEVICE_RECORDINGDEVICE1:
537 return "Recorder 1";
538 case CECDEVICE_RECORDINGDEVICE2:
539 return "Recorder 2";
540 case CECDEVICE_RECORDINGDEVICE3:
541 return "Recorder 3";
542 case CECDEVICE_RESERVED1:
543 return "Reserved 1";
544 case CECDEVICE_RESERVED2:
545 return "Reserved 2";
546 case CECDEVICE_TUNER1:
547 return "Tuner 1";
548 case CECDEVICE_TUNER2:
549 return "Tuner 2";
550 case CECDEVICE_TUNER3:
551 return "Tuner 3";
552 case CECDEVICE_TUNER4:
553 return "Tuner 4";
554 case CECDEVICE_TV:
555 return "TV";
556 default:
557 return "unknown";
558 }
559 }
560
561 const char *CLibCEC::ToString(const cec_deck_control_mode mode)
562 {
563 switch (mode)
564 {
565 case CEC_DECK_CONTROL_MODE_SKIP_FORWARD_WIND:
566 return "skip forward wind";
567 case CEC_DECK_CONTROL_MODE_EJECT:
568 return "eject";
569 case CEC_DECK_CONTROL_MODE_SKIP_REVERSE_REWIND:
570 return "reverse rewind";
571 case CEC_DECK_CONTROL_MODE_STOP:
572 return "stop";
573 default:
574 return "unknown";
575 }
576 }
577
578 const char *CLibCEC::ToString(const cec_deck_info status)
579 {
580 switch (status)
581 {
582 case CEC_DECK_INFO_PLAY:
583 return "play";
584 case CEC_DECK_INFO_RECORD:
585 return "record";
586 case CEC_DECK_INFO_PLAY_REVERSE:
587 return "play reverse";
588 case CEC_DECK_INFO_STILL:
589 return "still";
590 case CEC_DECK_INFO_SLOW:
591 return "slow";
592 case CEC_DECK_INFO_SLOW_REVERSE:
593 return "slow reverse";
594 case CEC_DECK_INFO_FAST_FORWARD:
595 return "fast forward";
596 case CEC_DECK_INFO_FAST_REVERSE:
597 return "fast reverse";
598 case CEC_DECK_INFO_NO_MEDIA:
599 return "no media";
600 case CEC_DECK_INFO_STOP:
601 return "stop";
602 case CEC_DECK_INFO_SKIP_FORWARD_WIND:
603 return "info skip forward wind";
604 case CEC_DECK_INFO_SKIP_REVERSE_REWIND:
605 return "info skip reverse rewind";
606 case CEC_DECK_INFO_INDEX_SEARCH_FORWARD:
607 return "info index search forward";
608 case CEC_DECK_INFO_INDEX_SEARCH_REVERSE:
609 return "info index search reverse";
610 case CEC_DECK_INFO_OTHER_STATUS:
611 return "other";
612 case CEC_DECK_INFO_OTHER_STATUS_LG:
613 return "LG other";
614 default:
615 return "unknown";
616 }
617 }
618
619 const char *CLibCEC::ToString(const cec_opcode opcode)
620 {
621 switch (opcode)
622 {
623 case CEC_OPCODE_ACTIVE_SOURCE:
624 return "active source";
625 case CEC_OPCODE_IMAGE_VIEW_ON:
626 return "image view on";
627 case CEC_OPCODE_TEXT_VIEW_ON:
628 return "text view on";
629 case CEC_OPCODE_INACTIVE_SOURCE:
630 return "inactive source";
631 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE:
632 return "request active source";
633 case CEC_OPCODE_ROUTING_CHANGE:
634 return "routing change";
635 case CEC_OPCODE_ROUTING_INFORMATION:
636 return "routing information";
637 case CEC_OPCODE_SET_STREAM_PATH:
638 return "set stream path";
639 case CEC_OPCODE_STANDBY:
640 return "standby";
641 case CEC_OPCODE_RECORD_OFF:
642 return "record off";
643 case CEC_OPCODE_RECORD_ON:
644 return "record on";
645 case CEC_OPCODE_RECORD_STATUS:
646 return "record status";
647 case CEC_OPCODE_RECORD_TV_SCREEN:
648 return "record tv screen";
649 case CEC_OPCODE_CLEAR_ANALOGUE_TIMER:
650 return "clear analogue timer";
651 case CEC_OPCODE_CLEAR_DIGITAL_TIMER:
652 return "clear digital timer";
653 case CEC_OPCODE_CLEAR_EXTERNAL_TIMER:
654 return "clear external timer";
655 case CEC_OPCODE_SET_ANALOGUE_TIMER:
656 return "set analogue timer";
657 case CEC_OPCODE_SET_DIGITAL_TIMER:
658 return "set digital timer";
659 case CEC_OPCODE_SET_EXTERNAL_TIMER:
660 return "set external timer";
661 case CEC_OPCODE_SET_TIMER_PROGRAM_TITLE:
662 return "set timer program title";
663 case CEC_OPCODE_TIMER_CLEARED_STATUS:
664 return "timer cleared status";
665 case CEC_OPCODE_TIMER_STATUS:
666 return "timer status";
667 case CEC_OPCODE_CEC_VERSION:
668 return "cec version";
669 case CEC_OPCODE_GET_CEC_VERSION:
670 return "get cec version";
671 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS:
672 return "give physical address";
673 case CEC_OPCODE_GET_MENU_LANGUAGE:
674 return "get menu language";
675 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS:
676 return "report physical address";
677 case CEC_OPCODE_SET_MENU_LANGUAGE:
678 return "set menu language";
679 case CEC_OPCODE_DECK_CONTROL:
680 return "deck control";
681 case CEC_OPCODE_DECK_STATUS:
682 return "deck status";
683 case CEC_OPCODE_GIVE_DECK_STATUS:
684 return "give deck status";
685 case CEC_OPCODE_PLAY:
686 return "play";
687 case CEC_OPCODE_GIVE_TUNER_DEVICE_STATUS:
688 return "give tuner status";
689 case CEC_OPCODE_SELECT_ANALOGUE_SERVICE:
690 return "select analogue service";
691 case CEC_OPCODE_SELECT_DIGITAL_SERVICE:
692 return "set digital service";
693 case CEC_OPCODE_TUNER_DEVICE_STATUS:
694 return "tuner device status";
695 case CEC_OPCODE_TUNER_STEP_DECREMENT:
696 return "tuner step decrement";
697 case CEC_OPCODE_TUNER_STEP_INCREMENT:
698 return "tuner step increment";
699 case CEC_OPCODE_DEVICE_VENDOR_ID:
700 return "device vendor id";
701 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID:
702 return "give device vendor id";
703 case CEC_OPCODE_VENDOR_COMMAND:
704 return "vendor command";
705 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID:
706 return "vendor command with id";
707 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN:
708 return "vendor remote button down";
709 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP:
710 return "vendor remote button up";
711 case CEC_OPCODE_SET_OSD_STRING:
712 return "set osd string";
713 case CEC_OPCODE_GIVE_OSD_NAME:
714 return "give osd name";
715 case CEC_OPCODE_SET_OSD_NAME:
716 return "set osd name";
717 case CEC_OPCODE_MENU_REQUEST:
718 return "menu request";
719 case CEC_OPCODE_MENU_STATUS:
720 return "menu status";
721 case CEC_OPCODE_USER_CONTROL_PRESSED:
722 return "user control pressed";
723 case CEC_OPCODE_USER_CONTROL_RELEASE:
724 return "user control release";
725 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS:
726 return "give device power status";
727 case CEC_OPCODE_REPORT_POWER_STATUS:
728 return "report power status";
729 case CEC_OPCODE_FEATURE_ABORT:
730 return "feature abort";
731 case CEC_OPCODE_ABORT:
732 return "abort";
733 case CEC_OPCODE_GIVE_AUDIO_STATUS:
734 return "give audio status";
735 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS:
736 return "give audio mode status";
737 case CEC_OPCODE_REPORT_AUDIO_STATUS:
738 return "report audio status";
739 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE:
740 return "set system audio mode";
741 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST:
742 return "system audio mode request";
743 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS:
744 return "system audio mode status";
745 case CEC_OPCODE_SET_AUDIO_RATE:
746 return "set audio rate";
747 case CEC_OPCODE_START_ARC:
748 return "start ARC";
749 case CEC_OPCODE_REPORT_ARC_STARTED:
750 return "report ARC started";
751 case CEC_OPCODE_REPORT_ARC_ENDED:
752 return "report ARC ended";
753 case CEC_OPCODE_REQUEST_ARC_START:
754 return "request ARC start";
755 case CEC_OPCODE_REQUEST_ARC_END:
756 return "request ARC end";
757 case CEC_OPCODE_END_ARC:
758 return "end ARC";
759 case CEC_OPCODE_CDC:
760 return "CDC";
761 case CEC_OPCODE_NONE:
762 return "poll";
763 default:
764 return "UNKNOWN";
765 }
766 }
767
768 const char *CLibCEC::ToString(const cec_system_audio_status mode)
769 {
770 switch(mode)
771 {
772 case CEC_SYSTEM_AUDIO_STATUS_ON:
773 return "on";
774 case CEC_SYSTEM_AUDIO_STATUS_OFF:
775 return "off";
776 default:
777 return "unknown";
778 }
779 }
780
781 const char *CLibCEC::ToString(const cec_audio_status UNUSED(status))
782 {
783 // TODO this is a mask
784 return "TODO";
785 }
786
787 const char *CLibCEC::ToString(const cec_vendor_id vendor)
788 {
789 switch (vendor)
790 {
791 case CEC_VENDOR_SAMSUNG:
792 return "Samsung";
793 case CEC_VENDOR_LG:
794 return "LG";
795 case CEC_VENDOR_PANASONIC:
796 return "Panasonic";
797 case CEC_VENDOR_PIONEER:
798 return "Pioneer";
799 case CEC_VENDOR_ONKYO:
800 return "Onkyo";
801 case CEC_VENDOR_YAMAHA:
802 return "Yamaha";
803 case CEC_VENDOR_PHILIPS:
804 return "Philips";
805 case CEC_VENDOR_SONY:
806 return "Sony";
807 case CEC_VENDOR_TOSHIBA:
808 return "Toshiba";
809 default:
810 return "Unknown";
811 }
812 }
813
814 const char *CLibCEC::ToString(const cec_client_version version)
815 {
816 switch (version)
817 {
818 case CEC_CLIENT_VERSION_PRE_1_5:
819 return "pre-1.5";
820 case CEC_CLIENT_VERSION_1_5_0:
821 return "1.5.0";
822 case CEC_CLIENT_VERSION_1_5_1:
823 return "1.5.1";
824 case CEC_CLIENT_VERSION_1_5_2:
825 return "1.5.2";
826 case CEC_CLIENT_VERSION_1_5_3:
827 return "1.5.3";
828 case CEC_CLIENT_VERSION_1_6_0:
829 return "1.6.0";
830 case CEC_CLIENT_VERSION_1_6_1:
831 return "1.6.1";
832 case CEC_CLIENT_VERSION_1_6_2:
833 return "1.6.2";
834 default:
835 return "Unknown";
836 }
837 }
838
839 const char *CLibCEC::ToString(const cec_server_version version)
840 {
841 switch (version)
842 {
843 case CEC_SERVER_VERSION_PRE_1_5:
844 return "pre-1.5";
845 case CEC_SERVER_VERSION_1_5_0:
846 return "1.5.0";
847 case CEC_SERVER_VERSION_1_5_1:
848 return "1.5.1";
849 case CEC_SERVER_VERSION_1_5_2:
850 return "1.5.2";
851 case CEC_SERVER_VERSION_1_5_3:
852 return "1.5.3";
853 case CEC_SERVER_VERSION_1_6_0:
854 return "1.6.0";
855 case CEC_SERVER_VERSION_1_6_1:
856 return "1.6.1";
857 case CEC_SERVER_VERSION_1_6_2:
858 return "1.6.2";
859 default:
860 return "Unknown";
861 }
862 }
863
864 void CLibCEC::CheckKeypressTimeout(void)
865 {
866 // check all clients
867 for (vector<CCECClient *>::iterator it = m_clients.begin(); it != m_clients.end(); it++)
868 (*it)->CheckKeypressTimeout();
869 }
870
871 void CLibCEC::AddLog(const cec_log_level level, const char *strFormat, ...)
872 {
873 CStdString strLog;
874
875 va_list argList;
876 va_start(argList, strFormat);
877 strLog.FormatV(strFormat, argList);
878 va_end(argList);
879
880 cec_log_message message;
881 message.level = level;
882 message.time = GetTimeMs() - m_iStartTime;
883 snprintf(message.message, sizeof(message.message), "%s", strLog.c_str());
884
885 // send the message to all clients
886 for (vector<CCECClient *>::iterator it = m_clients.begin(); it != m_clients.end(); it++)
887 (*it)->AddLog(message);
888 }
889
890 void CLibCEC::Alert(const libcec_alert type, const libcec_parameter &param)
891 {
892 // send the alert to all clients
893 for (vector<CCECClient *>::iterator it = m_clients.begin(); it != m_clients.end(); it++)
894 (*it)->Alert(type, param);
895 }
896
897 bool CLibCEC::SetActiveView(void)
898 {
899 AddLog(CEC_LOG_WARNING, "deprecated method %s called", __FUNCTION__);
900 return SetActiveSource();
901 }
902
903 bool CLibCEC::EnablePhysicalAddressDetection(void)
904 {
905 AddLog(CEC_LOG_WARNING, "deprecated method %s called", __FUNCTION__);
906 return true;
907 }
908
909 CCECClient *CLibCEC::RegisterClient(libcec_configuration *configuration)
910 {
911 if (!m_cec)
912 return NULL;
913
914 CCECClient *newClient = new CCECClient(m_cec, configuration);
915 if (!newClient)
916 return NULL;
917
918 m_clients.push_back(newClient);
919 if (!m_client)
920 m_client = newClient;
921
922 if (m_cec->IsRunning())
923 m_cec->RegisterClient(newClient);
924
925 return newClient;
926 }
927
928 void CLibCEC::UnregisterClients(void)
929 {
930 m_clients.clear();
931 delete m_client;
932 m_client = NULL;
933 }
934
935 void * CECInitialise(libcec_configuration *configuration)
936 {
937 if (!configuration)
938 return NULL;
939
940 CLibCEC *lib = new CLibCEC;
941 CCECClient *client(NULL);
942 if (lib)
943 client = lib->RegisterClient(configuration);
944
945 if (client)
946 client->GetCurrentConfiguration(configuration);
947
948 // ensure that the correct server version is set
949 configuration->serverVersion = LIBCEC_VERSION_CURRENT;
950
951 return static_cast< void* > (lib);
952 }
953
954 void * CECInit(const char *strDeviceName, CEC::cec_device_type_list types, uint16_t iPhysicalAddress /* = 0 */)
955 {
956 libcec_configuration configuration;
957
958 // client version < 1.5.0
959 snprintf(configuration.strDeviceName, 13, "%s", strDeviceName);
960 configuration.deviceTypes = types;
961 configuration.iPhysicalAddress = iPhysicalAddress;
962
963 if (configuration.deviceTypes.IsEmpty())
964 configuration.deviceTypes.Add(CEC_DEVICE_TYPE_RECORDING_DEVICE);
965
966 return CECInitialise(&configuration);
967 }
968
969 bool CECStartBootloader(void)
970 {
971 bool bReturn(false);
972 cec_adapter deviceList[1];
973 if (CUSBCECAdapterDetection::FindAdapters(deviceList, 1) > 0)
974 {
975 CUSBCECAdapterCommunication comm(NULL, deviceList[0].comm);
976 CTimeout timeout(CEC_DEFAULT_CONNECT_TIMEOUT);
977 while (timeout.TimeLeft() > 0 && (bReturn = comm.Open(timeout.TimeLeft() / CEC_CONNECT_TRIES, true)) == false)
978 {
979 comm.Close();
980 CEvent::Sleep(500);
981 }
982 if (comm.IsOpen())
983 bReturn = comm.StartBootloader();
984 }
985
986 return bReturn;
987 }
988
989 void CECDestroy(CEC::ICECAdapter *instance)
990 {
991 delete instance;
992 }
993
994 bool CLibCEC::GetDeviceInformation(const char *strPort, libcec_configuration *config, uint32_t iTimeoutMs /* = CEC_DEFAULT_CONNECT_TIMEOUT */)
995 {
996 if (m_cec->IsRunning())
997 return false;
998
999 return m_cec->GetDeviceInformation(strPort, config, iTimeoutMs);
1000 }