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