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