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