1646041da0dd9759ee91f22674a587611d877d87
[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 retVal.device = CECDEVICE_UNKNOWN;
342 memset(retVal.name, 0, 14);
343
344 if (m_client)
345 retVal = m_client->GetDeviceOSDName(iAddress);
346 return retVal;
347 }
348
349 cec_logical_addresses CLibCEC::GetLogicalAddresses(void)
350 {
351 cec_logical_addresses addresses;
352 addresses.Clear();
353 if (m_cec)
354 addresses = m_cec->GetLogicalAddresses();
355 return addresses;
356 }
357
358 bool CLibCEC::GetNextLogMessage(cec_log_message *message)
359 {
360 return m_client ? m_client->GetNextLogMessage(message) : false;
361 }
362
363 bool CLibCEC::GetNextKeypress(cec_keypress *key)
364 {
365 return m_client ? m_client->GetNextKeypress(key) : false;
366 }
367
368 bool CLibCEC::GetNextCommand(cec_command *command)
369 {
370 return m_client ? m_client->GetNextCommand(command) : false;
371 }
372
373 cec_device_type CLibCEC::GetType(cec_logical_address address)
374 {
375 switch (address)
376 {
377 case CECDEVICE_AUDIOSYSTEM:
378 return CEC_DEVICE_TYPE_AUDIO_SYSTEM;
379 case CECDEVICE_PLAYBACKDEVICE1:
380 case CECDEVICE_PLAYBACKDEVICE2:
381 case CECDEVICE_PLAYBACKDEVICE3:
382 return CEC_DEVICE_TYPE_PLAYBACK_DEVICE;
383 case CECDEVICE_RECORDINGDEVICE1:
384 case CECDEVICE_RECORDINGDEVICE2:
385 case CECDEVICE_RECORDINGDEVICE3:
386 return CEC_DEVICE_TYPE_RECORDING_DEVICE;
387 case CECDEVICE_TUNER1:
388 case CECDEVICE_TUNER2:
389 case CECDEVICE_TUNER3:
390 case CECDEVICE_TUNER4:
391 return CEC_DEVICE_TYPE_TUNER;
392 case CECDEVICE_TV:
393 return CEC_DEVICE_TYPE_TV;
394 default:
395 return CEC_DEVICE_TYPE_RESERVED;
396 }
397 }
398
399 uint16_t CLibCEC::GetMaskForType(cec_logical_address address)
400 {
401 return GetMaskForType(GetType(address));
402 }
403
404 uint16_t CLibCEC::GetMaskForType(cec_device_type type)
405 {
406 switch (type)
407 {
408 case CEC_DEVICE_TYPE_AUDIO_SYSTEM:
409 {
410 cec_logical_addresses addr;
411 addr.Clear();
412 addr.Set(CECDEVICE_AUDIOSYSTEM);
413 return addr.AckMask();
414 }
415 case CEC_DEVICE_TYPE_PLAYBACK_DEVICE:
416 {
417 cec_logical_addresses addr;
418 addr.Clear();
419 addr.Set(CECDEVICE_PLAYBACKDEVICE1);
420 addr.Set(CECDEVICE_PLAYBACKDEVICE2);
421 addr.Set(CECDEVICE_PLAYBACKDEVICE3);
422 return addr.AckMask();
423 }
424 case CEC_DEVICE_TYPE_RECORDING_DEVICE:
425 {
426 cec_logical_addresses addr;
427 addr.Clear();
428 addr.Set(CECDEVICE_RECORDINGDEVICE1);
429 addr.Set(CECDEVICE_RECORDINGDEVICE2);
430 addr.Set(CECDEVICE_RECORDINGDEVICE3);
431 return addr.AckMask();
432 }
433 case CEC_DEVICE_TYPE_TUNER:
434 {
435 cec_logical_addresses addr;
436 addr.Clear();
437 addr.Set(CECDEVICE_TUNER1);
438 addr.Set(CECDEVICE_TUNER2);
439 addr.Set(CECDEVICE_TUNER3);
440 addr.Set(CECDEVICE_TUNER4);
441 return addr.AckMask();
442 }
443 case CEC_DEVICE_TYPE_TV:
444 {
445 cec_logical_addresses addr;
446 addr.Clear();
447 addr.Set(CECDEVICE_TV);
448 return addr.AckMask();
449 }
450 default:
451 return 0;
452 }
453 }
454
455 bool CLibCEC::IsValidPhysicalAddress(uint16_t iPhysicalAddress)
456 {
457 return iPhysicalAddress >= CEC_MIN_PHYSICAL_ADDRESS &&
458 iPhysicalAddress <= CEC_MAX_PHYSICAL_ADDRESS;
459 }
460
461 const char *CLibCEC::ToString(const cec_device_type type)
462 {
463 switch (type)
464 {
465 case CEC_DEVICE_TYPE_AUDIO_SYSTEM:
466 return "audio system";
467 case CEC_DEVICE_TYPE_PLAYBACK_DEVICE:
468 return "playback device";
469 case CEC_DEVICE_TYPE_RECORDING_DEVICE:
470 return "recording device";
471 case CEC_DEVICE_TYPE_RESERVED:
472 return "reserved";
473 case CEC_DEVICE_TYPE_TUNER:
474 return "tuner";
475 case CEC_DEVICE_TYPE_TV:
476 return "TV";
477 default:
478 return "unknown";
479 }
480 }
481
482 const char *CLibCEC::ToString(const cec_menu_state state)
483 {
484 switch (state)
485 {
486 case CEC_MENU_STATE_ACTIVATED:
487 return "activated";
488 case CEC_MENU_STATE_DEACTIVATED:
489 return "deactivated";
490 default:
491 return "unknown";
492 }
493 }
494
495 const char *CLibCEC::ToString(const cec_version version)
496 {
497 switch (version)
498 {
499 case CEC_VERSION_1_2:
500 return "1.2";
501 case CEC_VERSION_1_2A:
502 return "1.2a";
503 case CEC_VERSION_1_3:
504 return "1.3";
505 case CEC_VERSION_1_3A:
506 return "1.3a";
507 case CEC_VERSION_1_4:
508 return "1.4";
509 default:
510 return "unknown";
511 }
512 }
513
514 const char *CLibCEC::ToString(const cec_power_status status)
515 {
516 switch (status)
517 {
518 case CEC_POWER_STATUS_ON:
519 return "on";
520 case CEC_POWER_STATUS_STANDBY:
521 return "standby";
522 case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY:
523 return "in transition from on to standby";
524 case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON:
525 return "in transition from standby to on";
526 default:
527 return "unknown";
528 }
529 }
530
531 const char *CLibCEC::ToString(const cec_logical_address address)
532 {
533 switch(address)
534 {
535 case CECDEVICE_AUDIOSYSTEM:
536 return "Audio";
537 case CECDEVICE_BROADCAST:
538 return "Broadcast";
539 case CECDEVICE_FREEUSE:
540 return "Free use";
541 case CECDEVICE_PLAYBACKDEVICE1:
542 return "Playback 1";
543 case CECDEVICE_PLAYBACKDEVICE2:
544 return "Playback 2";
545 case CECDEVICE_PLAYBACKDEVICE3:
546 return "Playback 3";
547 case CECDEVICE_RECORDINGDEVICE1:
548 return "Recorder 1";
549 case CECDEVICE_RECORDINGDEVICE2:
550 return "Recorder 2";
551 case CECDEVICE_RECORDINGDEVICE3:
552 return "Recorder 3";
553 case CECDEVICE_RESERVED1:
554 return "Reserved 1";
555 case CECDEVICE_RESERVED2:
556 return "Reserved 2";
557 case CECDEVICE_TUNER1:
558 return "Tuner 1";
559 case CECDEVICE_TUNER2:
560 return "Tuner 2";
561 case CECDEVICE_TUNER3:
562 return "Tuner 3";
563 case CECDEVICE_TUNER4:
564 return "Tuner 4";
565 case CECDEVICE_TV:
566 return "TV";
567 default:
568 return "unknown";
569 }
570 }
571
572 const char *CLibCEC::ToString(const cec_deck_control_mode mode)
573 {
574 switch (mode)
575 {
576 case CEC_DECK_CONTROL_MODE_SKIP_FORWARD_WIND:
577 return "skip forward wind";
578 case CEC_DECK_CONTROL_MODE_EJECT:
579 return "eject";
580 case CEC_DECK_CONTROL_MODE_SKIP_REVERSE_REWIND:
581 return "reverse rewind";
582 case CEC_DECK_CONTROL_MODE_STOP:
583 return "stop";
584 default:
585 return "unknown";
586 }
587 }
588
589 const char *CLibCEC::ToString(const cec_deck_info status)
590 {
591 switch (status)
592 {
593 case CEC_DECK_INFO_PLAY:
594 return "play";
595 case CEC_DECK_INFO_RECORD:
596 return "record";
597 case CEC_DECK_INFO_PLAY_REVERSE:
598 return "play reverse";
599 case CEC_DECK_INFO_STILL:
600 return "still";
601 case CEC_DECK_INFO_SLOW:
602 return "slow";
603 case CEC_DECK_INFO_SLOW_REVERSE:
604 return "slow reverse";
605 case CEC_DECK_INFO_FAST_FORWARD:
606 return "fast forward";
607 case CEC_DECK_INFO_FAST_REVERSE:
608 return "fast reverse";
609 case CEC_DECK_INFO_NO_MEDIA:
610 return "no media";
611 case CEC_DECK_INFO_STOP:
612 return "stop";
613 case CEC_DECK_INFO_SKIP_FORWARD_WIND:
614 return "info skip forward wind";
615 case CEC_DECK_INFO_SKIP_REVERSE_REWIND:
616 return "info skip reverse rewind";
617 case CEC_DECK_INFO_INDEX_SEARCH_FORWARD:
618 return "info index search forward";
619 case CEC_DECK_INFO_INDEX_SEARCH_REVERSE:
620 return "info index search reverse";
621 case CEC_DECK_INFO_OTHER_STATUS:
622 return "other";
623 case CEC_DECK_INFO_OTHER_STATUS_LG:
624 return "LG other";
625 default:
626 return "unknown";
627 }
628 }
629
630 const char *CLibCEC::ToString(const cec_opcode opcode)
631 {
632 switch (opcode)
633 {
634 case CEC_OPCODE_ACTIVE_SOURCE:
635 return "active source";
636 case CEC_OPCODE_IMAGE_VIEW_ON:
637 return "image view on";
638 case CEC_OPCODE_TEXT_VIEW_ON:
639 return "text view on";
640 case CEC_OPCODE_INACTIVE_SOURCE:
641 return "inactive source";
642 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE:
643 return "request active source";
644 case CEC_OPCODE_ROUTING_CHANGE:
645 return "routing change";
646 case CEC_OPCODE_ROUTING_INFORMATION:
647 return "routing information";
648 case CEC_OPCODE_SET_STREAM_PATH:
649 return "set stream path";
650 case CEC_OPCODE_STANDBY:
651 return "standby";
652 case CEC_OPCODE_RECORD_OFF:
653 return "record off";
654 case CEC_OPCODE_RECORD_ON:
655 return "record on";
656 case CEC_OPCODE_RECORD_STATUS:
657 return "record status";
658 case CEC_OPCODE_RECORD_TV_SCREEN:
659 return "record tv screen";
660 case CEC_OPCODE_CLEAR_ANALOGUE_TIMER:
661 return "clear analogue timer";
662 case CEC_OPCODE_CLEAR_DIGITAL_TIMER:
663 return "clear digital timer";
664 case CEC_OPCODE_CLEAR_EXTERNAL_TIMER:
665 return "clear external timer";
666 case CEC_OPCODE_SET_ANALOGUE_TIMER:
667 return "set analogue timer";
668 case CEC_OPCODE_SET_DIGITAL_TIMER:
669 return "set digital timer";
670 case CEC_OPCODE_SET_EXTERNAL_TIMER:
671 return "set external timer";
672 case CEC_OPCODE_SET_TIMER_PROGRAM_TITLE:
673 return "set timer program title";
674 case CEC_OPCODE_TIMER_CLEARED_STATUS:
675 return "timer cleared status";
676 case CEC_OPCODE_TIMER_STATUS:
677 return "timer status";
678 case CEC_OPCODE_CEC_VERSION:
679 return "cec version";
680 case CEC_OPCODE_GET_CEC_VERSION:
681 return "get cec version";
682 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS:
683 return "give physical address";
684 case CEC_OPCODE_GET_MENU_LANGUAGE:
685 return "get menu language";
686 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS:
687 return "report physical address";
688 case CEC_OPCODE_SET_MENU_LANGUAGE:
689 return "set menu language";
690 case CEC_OPCODE_DECK_CONTROL:
691 return "deck control";
692 case CEC_OPCODE_DECK_STATUS:
693 return "deck status";
694 case CEC_OPCODE_GIVE_DECK_STATUS:
695 return "give deck status";
696 case CEC_OPCODE_PLAY:
697 return "play";
698 case CEC_OPCODE_GIVE_TUNER_DEVICE_STATUS:
699 return "give tuner status";
700 case CEC_OPCODE_SELECT_ANALOGUE_SERVICE:
701 return "select analogue service";
702 case CEC_OPCODE_SELECT_DIGITAL_SERVICE:
703 return "set digital service";
704 case CEC_OPCODE_TUNER_DEVICE_STATUS:
705 return "tuner device status";
706 case CEC_OPCODE_TUNER_STEP_DECREMENT:
707 return "tuner step decrement";
708 case CEC_OPCODE_TUNER_STEP_INCREMENT:
709 return "tuner step increment";
710 case CEC_OPCODE_DEVICE_VENDOR_ID:
711 return "device vendor id";
712 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID:
713 return "give device vendor id";
714 case CEC_OPCODE_VENDOR_COMMAND:
715 return "vendor command";
716 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID:
717 return "vendor command with id";
718 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN:
719 return "vendor remote button down";
720 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP:
721 return "vendor remote button up";
722 case CEC_OPCODE_SET_OSD_STRING:
723 return "set osd string";
724 case CEC_OPCODE_GIVE_OSD_NAME:
725 return "give osd name";
726 case CEC_OPCODE_SET_OSD_NAME:
727 return "set osd name";
728 case CEC_OPCODE_MENU_REQUEST:
729 return "menu request";
730 case CEC_OPCODE_MENU_STATUS:
731 return "menu status";
732 case CEC_OPCODE_USER_CONTROL_PRESSED:
733 return "user control pressed";
734 case CEC_OPCODE_USER_CONTROL_RELEASE:
735 return "user control release";
736 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS:
737 return "give device power status";
738 case CEC_OPCODE_REPORT_POWER_STATUS:
739 return "report power status";
740 case CEC_OPCODE_FEATURE_ABORT:
741 return "feature abort";
742 case CEC_OPCODE_ABORT:
743 return "abort";
744 case CEC_OPCODE_GIVE_AUDIO_STATUS:
745 return "give audio status";
746 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS:
747 return "give audio mode status";
748 case CEC_OPCODE_REPORT_AUDIO_STATUS:
749 return "report audio status";
750 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE:
751 return "set system audio mode";
752 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST:
753 return "system audio mode request";
754 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS:
755 return "system audio mode status";
756 case CEC_OPCODE_SET_AUDIO_RATE:
757 return "set audio rate";
758 case CEC_OPCODE_START_ARC:
759 return "start ARC";
760 case CEC_OPCODE_REPORT_ARC_STARTED:
761 return "report ARC started";
762 case CEC_OPCODE_REPORT_ARC_ENDED:
763 return "report ARC ended";
764 case CEC_OPCODE_REQUEST_ARC_START:
765 return "request ARC start";
766 case CEC_OPCODE_REQUEST_ARC_END:
767 return "request ARC end";
768 case CEC_OPCODE_END_ARC:
769 return "end ARC";
770 case CEC_OPCODE_CDC:
771 return "CDC";
772 case CEC_OPCODE_NONE:
773 return "poll";
774 default:
775 return "UNKNOWN";
776 }
777 }
778
779 const char *CLibCEC::ToString(const cec_system_audio_status mode)
780 {
781 switch(mode)
782 {
783 case CEC_SYSTEM_AUDIO_STATUS_ON:
784 return "on";
785 case CEC_SYSTEM_AUDIO_STATUS_OFF:
786 return "off";
787 default:
788 return "unknown";
789 }
790 }
791
792 const char *CLibCEC::ToString(const cec_audio_status UNUSED(status))
793 {
794 // TODO this is a mask
795 return "TODO";
796 }
797
798 const char *CLibCEC::ToString(const cec_vendor_id vendor)
799 {
800 switch (vendor)
801 {
802 case CEC_VENDOR_SAMSUNG:
803 return "Samsung";
804 case CEC_VENDOR_LG:
805 return "LG";
806 case CEC_VENDOR_PANASONIC:
807 return "Panasonic";
808 case CEC_VENDOR_PIONEER:
809 return "Pioneer";
810 case CEC_VENDOR_ONKYO:
811 return "Onkyo";
812 case CEC_VENDOR_YAMAHA:
813 return "Yamaha";
814 case CEC_VENDOR_PHILIPS:
815 return "Philips";
816 case CEC_VENDOR_SONY:
817 return "Sony";
818 case CEC_VENDOR_TOSHIBA:
819 return "Toshiba";
820 default:
821 return "Unknown";
822 }
823 }
824
825 const char *CLibCEC::ToString(const cec_client_version version)
826 {
827 switch (version)
828 {
829 case CEC_CLIENT_VERSION_PRE_1_5:
830 return "pre-1.5";
831 case CEC_CLIENT_VERSION_1_5_0:
832 return "1.5.0";
833 case CEC_CLIENT_VERSION_1_5_1:
834 return "1.5.1";
835 case CEC_CLIENT_VERSION_1_5_2:
836 return "1.5.2";
837 case CEC_CLIENT_VERSION_1_5_3:
838 return "1.5.3";
839 case CEC_CLIENT_VERSION_1_6_0:
840 return "1.6.0";
841 case CEC_CLIENT_VERSION_1_6_1:
842 return "1.6.1";
843 case CEC_CLIENT_VERSION_1_6_2:
844 return "1.6.2";
845 default:
846 return "Unknown";
847 }
848 }
849
850 const char *CLibCEC::ToString(const cec_server_version version)
851 {
852 switch (version)
853 {
854 case CEC_SERVER_VERSION_PRE_1_5:
855 return "pre-1.5";
856 case CEC_SERVER_VERSION_1_5_0:
857 return "1.5.0";
858 case CEC_SERVER_VERSION_1_5_1:
859 return "1.5.1";
860 case CEC_SERVER_VERSION_1_5_2:
861 return "1.5.2";
862 case CEC_SERVER_VERSION_1_5_3:
863 return "1.5.3";
864 case CEC_SERVER_VERSION_1_6_0:
865 return "1.6.0";
866 case CEC_SERVER_VERSION_1_6_1:
867 return "1.6.1";
868 case CEC_SERVER_VERSION_1_6_2:
869 return "1.6.2";
870 default:
871 return "Unknown";
872 }
873 }
874
875 void CLibCEC::CheckKeypressTimeout(void)
876 {
877 // check all clients
878 for (vector<CCECClient *>::iterator it = m_clients.begin(); it != m_clients.end(); it++)
879 (*it)->CheckKeypressTimeout();
880 }
881
882 void CLibCEC::AddLog(const cec_log_level level, const char *strFormat, ...)
883 {
884 CStdString strLog;
885
886 va_list argList;
887 va_start(argList, strFormat);
888 strLog.FormatV(strFormat, argList);
889 va_end(argList);
890
891 cec_log_message message;
892 message.level = level;
893 message.time = GetTimeMs() - m_iStartTime;
894 snprintf(message.message, sizeof(message.message), "%s", strLog.c_str());
895
896 // send the message to all clients
897 for (vector<CCECClient *>::iterator it = m_clients.begin(); it != m_clients.end(); it++)
898 (*it)->AddLog(message);
899 }
900
901 void CLibCEC::Alert(const libcec_alert type, const libcec_parameter &param)
902 {
903 // send the alert to all clients
904 for (vector<CCECClient *>::iterator it = m_clients.begin(); it != m_clients.end(); it++)
905 (*it)->Alert(type, param);
906 }
907
908 bool CLibCEC::SetActiveView(void)
909 {
910 AddLog(CEC_LOG_WARNING, "deprecated method %s called", __FUNCTION__);
911 return SetActiveSource();
912 }
913
914 bool CLibCEC::EnablePhysicalAddressDetection(void)
915 {
916 AddLog(CEC_LOG_WARNING, "deprecated method %s called", __FUNCTION__);
917 return true;
918 }
919
920 CCECClient *CLibCEC::RegisterClient(libcec_configuration *configuration)
921 {
922 if (!m_cec)
923 return NULL;
924
925 CCECClient *newClient = new CCECClient(m_cec, configuration);
926 if (!newClient)
927 return NULL;
928
929 m_clients.push_back(newClient);
930 if (!m_client)
931 m_client = newClient;
932
933 if (m_cec->IsRunning())
934 m_cec->RegisterClient(newClient);
935
936 return newClient;
937 }
938
939 void CLibCEC::UnregisterClients(void)
940 {
941 m_clients.clear();
942 delete m_client;
943 m_client = NULL;
944 }
945
946 void * CECInitialise(libcec_configuration *configuration)
947 {
948 if (!configuration)
949 return NULL;
950
951 CLibCEC *lib = new CLibCEC(NULL);
952 CCECClient *client(NULL);
953 if (lib)
954 client = lib->RegisterClient(configuration);
955
956 if (client)
957 client->GetCurrentConfiguration(configuration);
958
959 // ensure that the correct server version is set
960 configuration->serverVersion = LIBCEC_VERSION_CURRENT;
961
962 return static_cast< void* > (lib);
963 }
964
965 void * CECInit(const char *strDeviceName, CEC::cec_device_type_list types, uint16_t iPhysicalAddress /* = 0 */)
966 {
967 libcec_configuration configuration;
968
969 // client version < 1.5.0
970 snprintf(configuration.strDeviceName, 13, "%s", strDeviceName);
971 configuration.deviceTypes = types;
972 configuration.iPhysicalAddress = iPhysicalAddress;
973
974 if (configuration.deviceTypes.IsEmpty())
975 configuration.deviceTypes.Add(CEC_DEVICE_TYPE_RECORDING_DEVICE);
976
977 return CECInitialise(&configuration);
978 }
979
980 bool CECStartBootloader(void)
981 {
982 bool bReturn(false);
983 cec_adapter deviceList[1];
984 if (CUSBCECAdapterDetection::FindAdapters(deviceList, 1) > 0)
985 {
986 CUSBCECAdapterCommunication comm(NULL, deviceList[0].comm);
987 CTimeout timeout(CEC_DEFAULT_CONNECT_TIMEOUT);
988 while (timeout.TimeLeft() > 0 && (bReturn = comm.Open(timeout.TimeLeft() / CEC_CONNECT_TRIES, true)) == false)
989 {
990 comm.Close();
991 CEvent::Sleep(500);
992 }
993 if (comm.IsOpen())
994 bReturn = comm.StartBootloader();
995 }
996
997 return bReturn;
998 }
999
1000 void CECDestroy(CEC::ICECAdapter *instance)
1001 {
1002 delete instance;
1003 }
1004
1005 bool CLibCEC::GetDeviceInformation(const char *strPort, libcec_configuration *config, uint32_t iTimeoutMs /* = CEC_DEFAULT_CONNECT_TIMEOUT */)
1006 {
1007 if (m_cec->IsRunning())
1008 return false;
1009
1010 return m_cec->GetDeviceInformation(strPort, config, iTimeoutMs);
1011 }
1012
1013 void CLibCEC::AddKey(const cec_keypress &UNUSED(key)) {}
1014 void CLibCEC::AddCommand(const cec_command &UNUSED(command)) {}
1015 void CLibCEC::ConfigurationChanged(const libcec_configuration &UNUSED(config)) {}
1016 void CLibCEC::SetCurrentButton(cec_user_control_code UNUSED(iButtonCode)) {}
1017 CLibCEC *CLibCEC::GetInstance(void) { return NULL; }
1018 void CLibCEC::SetInstance(CLibCEC *UNUSED(instance)) {}