2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
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.
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.
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.
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
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/
33 #include "CECCommandHandler.h"
34 #include "../devices/CECBusDevice.h"
35 #include "../devices/CECAudioSystem.h"
36 #include "../devices/CECPlaybackDevice.h"
37 #include "../CECProcessor.h"
42 CCECCommandHandler::CCECCommandHandler(CCECBusDevice
*busDevice
)
44 m_busDevice
= busDevice
;
47 bool CCECCommandHandler::HandleCommand(const cec_command
&command
)
52 strLog
.Format(">> %s (%X) -> %s (%X): %s (%2X)", ToString(command
.initiator
), command
.initiator
, ToString(command
.destination
), command
.destination
, ToString(command
.opcode
), command
.opcode
);
53 m_busDevice
->AddLog(CEC_LOG_NOTICE
, strLog
);
55 if (m_busDevice
->MyLogicalAddressContains(command
.destination
))
57 switch(command
.opcode
)
59 case CEC_OPCODE_REPORT_POWER_STATUS
:
60 HandleReportPowerStatus(command
);
62 case CEC_OPCODE_CEC_VERSION
:
63 HandleDeviceCecVersion(command
);
65 case CEC_OPCODE_SET_MENU_LANGUAGE
:
66 HandleSetMenuLanguage(command
);
67 /* pass to listeners */
68 m_busDevice
->GetProcessor()->AddCommand(command
);
70 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
71 HandleGivePhysicalAddress(command
);
73 case CEC_OPCODE_GIVE_OSD_NAME
:
74 HandleGiveOSDName(command
);
76 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
77 HandleGiveDeviceVendorId(command
);
79 case CEC_OPCODE_DEVICE_VENDOR_ID
:
80 HandleDeviceVendorId(command
);
82 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
83 HandleDeviceVendorCommandWithId(command
);
85 case CEC_OPCODE_GIVE_DECK_STATUS
:
86 HandleGiveDeckStatus(command
);
88 case CEC_OPCODE_MENU_REQUEST
:
89 HandleMenuRequest(command
);
91 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
92 HandleGiveDevicePowerStatus(command
);
94 case CEC_OPCODE_GET_CEC_VERSION
:
95 HandleGetCecVersion(command
);
97 case CEC_OPCODE_USER_CONTROL_PRESSED
:
98 HandleUserControlPressed(command
);
100 case CEC_OPCODE_USER_CONTROL_RELEASE
:
101 HandleUserControlRelease(command
);
103 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
104 HandleGiveAudioStatus(command
);
106 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
107 HandleGiveSystemAudioModeStatus(command
);
109 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
110 HandleSetSystemAudioModeRequest(command
);
113 UnhandledCommand(command
);
114 /* pass to listeners */
115 m_busDevice
->GetProcessor()->AddCommand(command
);
120 else if (command
.destination
== CECDEVICE_BROADCAST
)
123 switch (command
.opcode
)
125 case CEC_OPCODE_SET_MENU_LANGUAGE
:
126 HandleSetMenuLanguage(command
);
127 /* pass to listeners */
128 m_busDevice
->GetProcessor()->AddCommand(command
);
130 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
131 HandleRequestActiveSource(command
);
133 case CEC_OPCODE_SET_STREAM_PATH
:
134 HandleSetStreamPath(command
);
136 case CEC_OPCODE_ROUTING_CHANGE
:
137 HandleRoutingChange(command
);
139 case CEC_OPCODE_DEVICE_VENDOR_ID
:
140 HandleDeviceVendorId(command
);
142 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
143 HandleDeviceVendorCommandWithId(command
);
145 case CEC_OPCODE_STANDBY
:
146 HandleStandby(command
);
147 /* pass to listeners */
148 m_busDevice
->GetProcessor()->AddCommand(command
);
150 case CEC_OPCODE_ACTIVE_SOURCE
:
151 HandleActiveSource(command
);
152 /* pass to listeners */
153 m_busDevice
->GetProcessor()->AddCommand(command
);
156 UnhandledCommand(command
);
157 /* pass to listeners */
158 m_busDevice
->GetProcessor()->AddCommand(command
);
166 strLog
.Format("ignoring frame: we're not at destination %x", command
.destination
);
167 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
174 bool CCECCommandHandler::HandleActiveSource(const cec_command
&command
)
176 if (command
.parameters
.size
== 2)
178 uint16_t iAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
179 return m_busDevice
->GetProcessor()->SetStreamPath(iAddress
);
185 bool CCECCommandHandler::HandleDeviceCecVersion(const cec_command
&command
)
187 if (command
.parameters
.size
== 1)
189 CCECBusDevice
*device
= GetDevice(command
.initiator
);
191 device
->SetCecVersion((cec_version
) command
.parameters
[0]);
197 bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command
&command
)
199 SetVendorId(command
);
203 bool CCECCommandHandler::HandleDeviceVendorId(const cec_command
&command
)
205 SetVendorId(command
);
209 bool CCECCommandHandler::HandleGetCecVersion(const cec_command
&command
)
211 CCECBusDevice
*device
= GetDevice(command
.destination
);
213 return device
->TransmitCECVersion(command
.initiator
);
218 bool CCECCommandHandler::HandleGiveAudioStatus(const cec_command
&command
)
220 CCECBusDevice
*device
= GetDevice(command
.destination
);
221 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
222 return ((CCECAudioSystem
*) device
)->TransmitAudioStatus(command
.initiator
);
227 bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
229 CCECBusDevice
*device
= GetDevice(command
.destination
);
230 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
)
231 return ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(command
.initiator
);
236 bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command
&command
)
238 CCECBusDevice
*device
= GetDevice(command
.destination
);
240 return device
->TransmitPowerState(command
.initiator
);
245 bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command
&command
)
247 CCECBusDevice
*device
= GetDevice(command
.destination
);
249 return device
->TransmitVendorID(command
.initiator
);
254 bool CCECCommandHandler::HandleGiveOSDName(const cec_command
&command
)
256 CCECBusDevice
*device
= GetDevice(command
.destination
);
258 return device
->TransmitOSDName(command
.initiator
);
263 bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command
&command
)
265 CCECBusDevice
*device
= GetDevice(command
.destination
);
267 return device
->TransmitPhysicalAddress();
272 bool CCECCommandHandler::HandleMenuRequest(const cec_command
&command
)
274 if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_QUERY
)
276 CCECBusDevice
*device
= GetDevice(command
.destination
);
278 return device
->TransmitMenuState(command
.initiator
);
283 bool CCECCommandHandler::HandleReportPowerStatus(const cec_command
&command
)
285 if (command
.parameters
.size
== 1)
287 CCECBusDevice
*device
= GetDevice(command
.initiator
);
289 device
->SetPowerStatus((cec_power_status
) command
.parameters
[0]);
294 bool CCECCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
297 strLog
.Format(">> %i requests active source", (uint8_t) command
.initiator
);
298 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
300 vector
<CCECBusDevice
*> devices
;
301 for (int iDevicePtr
= (int)GetMyDevices(devices
)-1; iDevicePtr
>=0; iDevicePtr
--)
302 devices
[iDevicePtr
]->TransmitActiveSource();
306 bool CCECCommandHandler::HandleRoutingChange(const cec_command
&command
)
308 if (command
.parameters
.size
== 4)
310 uint16_t iOldAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
311 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[2] << 8) | ((uint16_t)command
.parameters
[3]);
313 CCECBusDevice
*device
= GetDevice(command
.initiator
);
315 device
->SetStreamPath(iNewAddress
, iOldAddress
);
320 bool CCECCommandHandler::HandleSetMenuLanguage(const cec_command
&command
)
322 if (command
.parameters
.size
== 3)
324 CCECBusDevice
*device
= GetDevice(command
.initiator
);
327 cec_menu_language language
;
328 language
.device
= command
.initiator
;
329 for (uint8_t iPtr
= 0; iPtr
< 4; iPtr
++)
330 language
.language
[iPtr
] = command
.parameters
[iPtr
];
331 language
.language
[3] = 0;
332 device
->SetMenuLanguage(language
);
338 bool CCECCommandHandler::HandleSetStreamPath(const cec_command
&command
)
340 if (command
.parameters
.size
>= 2)
342 int streamaddr
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
344 strLog
.Format(">> %i sets stream path to physical address %04x", command
.initiator
, streamaddr
);
345 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
347 return m_busDevice
->GetProcessor()->SetStreamPath(streamaddr
);
352 bool CCECCommandHandler::HandleSetSystemAudioModeRequest(const cec_command
&command
)
354 if (command
.parameters
.size
>= 1)
356 CCECBusDevice
*device
= GetDevice(command
.destination
);
357 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
358 return ((CCECAudioSystem
*) device
)->SetSystemAudioMode(command
);
363 bool CCECCommandHandler::HandleStandby(const cec_command
&command
)
365 CCECBusDevice
*device
= GetDevice(command
.initiator
);
367 device
->SetPowerStatus(CEC_POWER_STATUS_STANDBY
);
371 bool CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command
&command
)
373 CCECBusDevice
*device
= GetDevice(command
.destination
);
374 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
375 return ((CCECAudioSystem
*) device
)->TransmitSystemAudioModeStatus(command
.initiator
);
380 bool CCECCommandHandler::HandleUserControlPressed(const cec_command
&command
)
382 if (command
.parameters
.size
> 0)
384 m_busDevice
->GetProcessor()->AddKey();
386 if (command
.parameters
[0] <= CEC_USER_CONTROL_CODE_MAX
)
389 strLog
.Format("key pressed: %1x", command
.parameters
[0]);
390 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
392 m_busDevice
->GetProcessor()->SetCurrentButton((cec_user_control_code
) command
.parameters
[0]);
398 bool CCECCommandHandler::HandleUserControlRelease(const cec_command
&command
)
400 m_busDevice
->GetProcessor()->AddKey();
404 void CCECCommandHandler::UnhandledCommand(const cec_command
&command
)
407 strLog
.Format("unhandled command with opcode %02x from address %d", command
.opcode
, command
.initiator
);
408 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
);
411 unsigned int CCECCommandHandler::GetMyDevices(vector
<CCECBusDevice
*> &devices
) const
413 unsigned int iReturn(0);
415 cec_logical_addresses addresses
= m_busDevice
->GetProcessor()->GetLogicalAddresses();
416 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
420 devices
.push_back(GetDevice((cec_logical_address
) iPtr
));
428 CCECBusDevice
*CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress
) const
430 CCECBusDevice
*device
= NULL
;
432 if (iLogicalAddress
>= CECDEVICE_TV
&& iLogicalAddress
<= CECDEVICE_BROADCAST
)
433 device
= m_busDevice
->GetProcessor()->m_busDevices
[iLogicalAddress
];
438 CCECBusDevice
*CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
) const
440 return m_busDevice
->GetProcessor()->GetDeviceByPhysicalAddress(iPhysicalAddress
);
443 void CCECCommandHandler::SetVendorId(const cec_command
&command
)
445 if (command
.parameters
.size
< 3)
447 m_busDevice
->AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
451 uint64_t iVendorId
= ((uint64_t)command
.parameters
[0] << 16) +
452 ((uint64_t)command
.parameters
[1] << 8) +
453 (uint64_t)command
.parameters
[2];
455 CCECBusDevice
*device
= GetDevice((cec_logical_address
) command
.initiator
);
457 device
->SetVendorId(iVendorId
, command
.parameters
.size
> 3 ? command
.parameters
[3] : 0);
460 const char *CCECCommandHandler::ToString(const cec_logical_address address
)
464 case CECDEVICE_AUDIOSYSTEM
:
466 case CECDEVICE_BROADCAST
:
468 case CECDEVICE_FREEUSE
:
470 case CECDEVICE_PLAYBACKDEVICE1
:
472 case CECDEVICE_PLAYBACKDEVICE2
:
474 case CECDEVICE_PLAYBACKDEVICE3
:
476 case CECDEVICE_RECORDINGDEVICE1
:
478 case CECDEVICE_RECORDINGDEVICE2
:
480 case CECDEVICE_RECORDINGDEVICE3
:
482 case CECDEVICE_RESERVED1
:
484 case CECDEVICE_RESERVED2
:
486 case CECDEVICE_TUNER1
:
488 case CECDEVICE_TUNER2
:
490 case CECDEVICE_TUNER3
:
492 case CECDEVICE_TUNER4
:
501 const char *CCECCommandHandler::ToString(const cec_deck_info status
)
505 case CEC_DECK_INFO_PLAY
:
507 case CEC_DECK_INFO_RECORD
:
509 case CEC_DECK_INFO_PLAY_REVERSE
:
510 return "play reverse";
511 case CEC_DECK_INFO_STILL
:
513 case CEC_DECK_INFO_SLOW
:
515 case CEC_DECK_INFO_SLOW_REVERSE
:
516 return "slow reverse";
517 case CEC_DECK_INFO_FAST_FORWARD
:
518 return "fast forward";
519 case CEC_DECK_INFO_FAST_REVERSE
:
520 return "fast reverse";
521 case CEC_DECK_INFO_NO_MEDIA
:
523 case CEC_DECK_INFO_STOP
:
525 case CEC_DECK_INFO_SKIP_FORWARD_WIND
:
526 return "info skip forward wind";
527 case CEC_DECK_INFO_SKIP_REVERSE_REWIND
:
528 return "info skip reverse rewind";
529 case CEC_DECK_INFO_INDEX_SEARCH_FORWARD
:
530 return "info index search forward";
531 case CEC_DECK_INFO_INDEX_SEARCH_REVERSE
:
532 return "info index search reverse";
533 case CEC_DECK_INFO_OTHER_STATUS
:
540 const char *CCECCommandHandler::ToString(const cec_opcode opcode
)
544 case CEC_OPCODE_ACTIVE_SOURCE
:
545 return "active source";
546 case CEC_OPCODE_IMAGE_VIEW_ON
:
547 return "image view on";
548 case CEC_OPCODE_TEXT_VIEW_ON
:
549 return "text view on";
550 case CEC_OPCODE_INACTIVE_SOURCE
:
551 return "inactive source";
552 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
553 return "request active source";
554 case CEC_OPCODE_ROUTING_CHANGE
:
555 return "routing change";
556 case CEC_OPCODE_ROUTING_INFORMATION
:
557 return "routing information";
558 case CEC_OPCODE_SET_STREAM_PATH
:
559 return "set stream path";
560 case CEC_OPCODE_STANDBY
:
562 case CEC_OPCODE_RECORD_OFF
:
564 case CEC_OPCODE_RECORD_ON
:
566 case CEC_OPCODE_RECORD_STATUS
:
567 return "record status";
568 case CEC_OPCODE_RECORD_TV_SCREEN
:
569 return "record tv screen";
570 case CEC_OPCODE_CLEAR_ANALOGUE_TIMER
:
571 return "clear analogue timer";
572 case CEC_OPCODE_CLEAR_DIGITAL_TIMER
:
573 return "clear digital timer";
574 case CEC_OPCODE_CLEAR_EXTERNAL_TIMER
:
575 return "clear external timer";
576 case CEC_OPCODE_SET_ANALOGUE_TIMER
:
577 return "set analogue timer";
578 case CEC_OPCODE_SET_DIGITAL_TIMER
:
579 return "set digital timer";
580 case CEC_OPCODE_SET_EXTERNAL_TIMER
:
581 return "set external timer";
582 case CEC_OPCODE_SET_TIMER_PROGRAM_TITLE
:
583 return "set timer program title";
584 case CEC_OPCODE_TIMER_CLEARED_STATUS
:
585 return "timer cleared status";
586 case CEC_OPCODE_TIMER_STATUS
:
587 return "timer status";
588 case CEC_OPCODE_CEC_VERSION
:
589 return "cec version";
590 case CEC_OPCODE_GET_CEC_VERSION
:
591 return "get cec version";
592 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
593 return "give physical address";
594 case CEC_OPCODE_GET_MENU_LANGUAGE
:
595 return "get menu language";
596 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
:
597 return "report physical address";
598 case CEC_OPCODE_SET_MENU_LANGUAGE
:
599 return "set menu language";
600 case CEC_OPCODE_DECK_CONTROL
:
601 return "deck control";
602 case CEC_OPCODE_DECK_STATUS
:
603 return "deck status";
604 case CEC_OPCODE_GIVE_DECK_STATUS
:
605 return "give deck status";
606 case CEC_OPCODE_PLAY
:
608 case CEC_OPCODE_GIVE_TUNER_DEVICE_STATUS
:
609 return "give tuner status";
610 case CEC_OPCODE_SELECT_ANALOGUE_SERVICE
:
611 return "select analogue service";
612 case CEC_OPCODE_SELECT_DIGITAL_SERVICE
:
613 return "set digital service";
614 case CEC_OPCODE_TUNER_DEVICE_STATUS
:
615 return "tuner device status";
616 case CEC_OPCODE_TUNER_STEP_DECREMENT
:
617 return "tuner step decrement";
618 case CEC_OPCODE_TUNER_STEP_INCREMENT
:
619 return "tuner step increment";
620 case CEC_OPCODE_DEVICE_VENDOR_ID
:
621 return "device vendor id";
622 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
623 return "give device vendor id";
624 case CEC_OPCODE_VENDOR_COMMAND
:
625 return "vendor command";
626 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
627 return "vendor command with id";
628 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN
:
629 return "vendor remote button down";
630 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP
:
631 return "vendor remote button up";
632 case CEC_OPCODE_SET_OSD_STRING
:
633 return "set osd string";
634 case CEC_OPCODE_GIVE_OSD_NAME
:
635 return "give osd name";
636 case CEC_OPCODE_SET_OSD_NAME
:
637 return "set osd name";
638 case CEC_OPCODE_MENU_REQUEST
:
639 return "menu request";
640 case CEC_OPCODE_MENU_STATUS
:
641 return "menu status";
642 case CEC_OPCODE_USER_CONTROL_PRESSED
:
643 return "user control pressed";
644 case CEC_OPCODE_USER_CONTROL_RELEASE
:
645 return "user control release";
646 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
647 return "give device power status";
648 case CEC_OPCODE_REPORT_POWER_STATUS
:
649 return "report power status";
650 case CEC_OPCODE_FEATURE_ABORT
:
651 return "feature abort";
652 case CEC_OPCODE_ABORT
:
654 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
655 return "give audio status";
656 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
657 return "give audio mode status";
658 case CEC_OPCODE_REPORT_AUDIO_STATUS
:
659 return "report audio status";
660 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
:
661 return "set system audio mode";
662 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
663 return "system audio mode request";
664 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
:
665 return "system audio mode status";
666 case CEC_OPCODE_SET_AUDIO_RATE
:
667 return "set audio rate";