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
);
151 UnhandledCommand(command
);
152 /* pass to listeners */
153 m_busDevice
->GetProcessor()->AddCommand(command
);
161 strLog
.Format("ignoring frame: we're not at destination %x", command
.destination
);
162 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
169 bool CCECCommandHandler::HandleDeviceCecVersion(const cec_command
&command
)
171 if (command
.parameters
.size
== 1)
173 CCECBusDevice
*device
= GetDevice(command
.initiator
);
175 device
->SetCecVersion((cec_version
) command
.parameters
[0]);
181 bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command
&command
)
183 SetVendorId(command
);
187 bool CCECCommandHandler::HandleDeviceVendorId(const cec_command
&command
)
189 SetVendorId(command
);
193 bool CCECCommandHandler::HandleGetCecVersion(const cec_command
&command
)
195 CCECBusDevice
*device
= GetDevice(command
.destination
);
197 return device
->TransmitCECVersion(command
.initiator
);
202 bool CCECCommandHandler::HandleGiveAudioStatus(const cec_command
&command
)
204 CCECBusDevice
*device
= GetDevice(command
.destination
);
205 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
206 return ((CCECAudioSystem
*) device
)->TransmitAudioStatus(command
.initiator
);
211 bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
213 CCECBusDevice
*device
= GetDevice(command
.destination
);
214 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
)
215 return ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(command
.initiator
);
220 bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command
&command
)
222 CCECBusDevice
*device
= GetDevice(command
.destination
);
224 return device
->TransmitPowerState(command
.initiator
);
229 bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command
&command
)
231 CCECBusDevice
*device
= GetDevice(command
.destination
);
233 return device
->TransmitVendorID(command
.initiator
);
238 bool CCECCommandHandler::HandleGiveOSDName(const cec_command
&command
)
240 CCECBusDevice
*device
= GetDevice(command
.destination
);
242 return device
->TransmitOSDName(command
.initiator
);
247 bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command
&command
)
249 CCECBusDevice
*device
= GetDevice(command
.destination
);
251 return device
->TransmitPhysicalAddress();
256 bool CCECCommandHandler::HandleMenuRequest(const cec_command
&command
)
258 if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_QUERY
)
260 CCECBusDevice
*device
= GetDevice(command
.destination
);
262 return device
->TransmitMenuState(command
.initiator
);
267 bool CCECCommandHandler::HandleReportPowerStatus(const cec_command
&command
)
269 if (command
.parameters
.size
== 1)
271 CCECBusDevice
*device
= GetDevice(command
.initiator
);
273 device
->SetPowerStatus((cec_power_status
) command
.parameters
[0]);
278 bool CCECCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
281 strLog
.Format(">> %i requests active source", (uint8_t) command
.initiator
);
282 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
284 vector
<CCECBusDevice
*> devices
;
285 for (int iDevicePtr
= (int)GetMyDevices(devices
)-1; iDevicePtr
>=0; iDevicePtr
--)
286 devices
[iDevicePtr
]->TransmitActiveSource();
290 bool CCECCommandHandler::HandleRoutingChange(const cec_command
&command
)
292 if (command
.parameters
.size
== 4)
294 uint16_t iOldAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
295 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[2] << 8) | ((uint16_t)command
.parameters
[3]);
297 CCECBusDevice
*device
= GetDevice(command
.initiator
);
299 device
->SetStreamPath(iNewAddress
, iOldAddress
);
304 bool CCECCommandHandler::HandleSetMenuLanguage(const cec_command
&command
)
306 if (command
.parameters
.size
== 3)
308 CCECBusDevice
*device
= GetDevice(command
.initiator
);
311 cec_menu_language language
;
312 language
.device
= command
.initiator
;
313 for (uint8_t iPtr
= 0; iPtr
< 4; iPtr
++)
314 language
.language
[iPtr
] = command
.parameters
[iPtr
];
315 language
.language
[3] = 0;
316 device
->SetMenuLanguage(language
);
322 bool CCECCommandHandler::HandleSetStreamPath(const cec_command
&command
)
324 if (command
.parameters
.size
>= 2)
326 int streamaddr
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
328 strLog
.Format(">> %i requests stream path from physical address %04x", command
.initiator
, streamaddr
);
329 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
330 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(streamaddr
);
332 return device
->TransmitActiveSource();
337 bool CCECCommandHandler::HandleSetSystemAudioModeRequest(const cec_command
&command
)
339 if (command
.parameters
.size
>= 1)
341 CCECBusDevice
*device
= GetDevice(command
.destination
);
342 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
343 return ((CCECAudioSystem
*) device
)->SetSystemAudioMode(command
);
348 bool CCECCommandHandler::HandleStandby(const cec_command
&command
)
350 CCECBusDevice
*device
= GetDevice(command
.initiator
);
352 device
->SetPowerStatus(CEC_POWER_STATUS_STANDBY
);
356 bool CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command
&command
)
358 CCECBusDevice
*device
= GetDevice(command
.destination
);
359 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
360 return ((CCECAudioSystem
*) device
)->TransmitSystemAudioModeStatus(command
.initiator
);
365 bool CCECCommandHandler::HandleUserControlPressed(const cec_command
&command
)
367 if (command
.parameters
.size
> 0)
369 m_busDevice
->GetProcessor()->AddKey();
371 if (command
.parameters
[0] <= CEC_USER_CONTROL_CODE_MAX
)
374 strLog
.Format("key pressed: %1x", command
.parameters
[0]);
375 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
377 m_busDevice
->GetProcessor()->SetCurrentButton((cec_user_control_code
) command
.parameters
[0]);
383 bool CCECCommandHandler::HandleUserControlRelease(const cec_command
&command
)
385 m_busDevice
->GetProcessor()->AddKey();
389 void CCECCommandHandler::UnhandledCommand(const cec_command
&command
)
392 strLog
.Format("unhandled command with opcode %02x from address %d", command
.opcode
, command
.initiator
);
393 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
);
396 unsigned int CCECCommandHandler::GetMyDevices(vector
<CCECBusDevice
*> &devices
) const
398 unsigned int iReturn(0);
400 cec_logical_addresses addresses
= m_busDevice
->GetProcessor()->GetLogicalAddresses();
401 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
405 devices
.push_back(GetDevice((cec_logical_address
) iPtr
));
413 CCECBusDevice
*CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress
) const
415 CCECBusDevice
*device
= NULL
;
417 if (iLogicalAddress
>= CECDEVICE_TV
&& iLogicalAddress
<= CECDEVICE_BROADCAST
)
418 device
= m_busDevice
->GetProcessor()->m_busDevices
[iLogicalAddress
];
423 CCECBusDevice
*CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
) const
425 return m_busDevice
->GetProcessor()->GetDeviceByPhysicalAddress(iPhysicalAddress
);
428 void CCECCommandHandler::SetVendorId(const cec_command
&command
)
430 if (command
.parameters
.size
< 3)
432 m_busDevice
->AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
436 uint64_t iVendorId
= ((uint64_t)command
.parameters
[0] << 16) +
437 ((uint64_t)command
.parameters
[1] << 8) +
438 (uint64_t)command
.parameters
[2];
440 CCECBusDevice
*device
= GetDevice((cec_logical_address
) command
.initiator
);
442 device
->SetVendorId(iVendorId
, command
.parameters
.size
> 3 ? command
.parameters
[3] : 0);
445 const char *CCECCommandHandler::ToString(const cec_logical_address address
)
449 case CECDEVICE_AUDIOSYSTEM
:
451 case CECDEVICE_BROADCAST
:
453 case CECDEVICE_FREEUSE
:
455 case CECDEVICE_PLAYBACKDEVICE1
:
457 case CECDEVICE_PLAYBACKDEVICE2
:
459 case CECDEVICE_PLAYBACKDEVICE3
:
461 case CECDEVICE_RECORDINGDEVICE1
:
463 case CECDEVICE_RECORDINGDEVICE2
:
465 case CECDEVICE_RECORDINGDEVICE3
:
467 case CECDEVICE_RESERVED1
:
469 case CECDEVICE_RESERVED2
:
471 case CECDEVICE_TUNER1
:
473 case CECDEVICE_TUNER2
:
475 case CECDEVICE_TUNER3
:
477 case CECDEVICE_TUNER4
:
486 const char *CCECCommandHandler::ToString(const cec_deck_info status
)
490 case CEC_DECK_INFO_PLAY
:
492 case CEC_DECK_INFO_RECORD
:
494 case CEC_DECK_INFO_PLAY_REVERSE
:
495 return "play reverse";
496 case CEC_DECK_INFO_STILL
:
498 case CEC_DECK_INFO_SLOW
:
500 case CEC_DECK_INFO_SLOW_REVERSE
:
501 return "slow reverse";
502 case CEC_DECK_INFO_FAST_FORWARD
:
503 return "fast forward";
504 case CEC_DECK_INFO_FAST_REVERSE
:
505 return "fast reverse";
506 case CEC_DECK_INFO_NO_MEDIA
:
508 case CEC_DECK_INFO_STOP
:
510 case CEC_DECK_INFO_SKIP_FORWARD_WIND
:
511 return "info skip forward wind";
512 case CEC_DECK_INFO_SKIP_REVERSE_REWIND
:
513 return "info skip reverse rewind";
514 case CEC_DECK_INFO_INDEX_SEARCH_FORWARD
:
515 return "info index search forward";
516 case CEC_DECK_INFO_INDEX_SEARCH_REVERSE
:
517 return "info index search reverse";
518 case CEC_DECK_INFO_OTHER_STATUS
:
525 const char *CCECCommandHandler::ToString(const cec_opcode opcode
)
529 case CEC_OPCODE_ACTIVE_SOURCE
:
530 return "active source";
531 case CEC_OPCODE_IMAGE_VIEW_ON
:
532 return "image view on";
533 case CEC_OPCODE_TEXT_VIEW_ON
:
534 return "text view on";
535 case CEC_OPCODE_INACTIVE_SOURCE
:
536 return "inactive source";
537 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
538 return "request active source";
539 case CEC_OPCODE_ROUTING_CHANGE
:
540 return "routing change";
541 case CEC_OPCODE_ROUTING_INFORMATION
:
542 return "routing information";
543 case CEC_OPCODE_SET_STREAM_PATH
:
544 return "set stream path";
545 case CEC_OPCODE_STANDBY
:
547 case CEC_OPCODE_RECORD_OFF
:
549 case CEC_OPCODE_RECORD_ON
:
551 case CEC_OPCODE_RECORD_STATUS
:
552 return "record status";
553 case CEC_OPCODE_RECORD_TV_SCREEN
:
554 return "record tv screen";
555 case CEC_OPCODE_CLEAR_ANALOGUE_TIMER
:
556 return "clear analogue timer";
557 case CEC_OPCODE_CLEAR_DIGITAL_TIMER
:
558 return "clear digital timer";
559 case CEC_OPCODE_CLEAR_EXTERNAL_TIMER
:
560 return "clear external timer";
561 case CEC_OPCODE_SET_ANALOGUE_TIMER
:
562 return "set analogue timer";
563 case CEC_OPCODE_SET_DIGITAL_TIMER
:
564 return "set digital timer";
565 case CEC_OPCODE_SET_EXTERNAL_TIMER
:
566 return "set external timer";
567 case CEC_OPCODE_SET_TIMER_PROGRAM_TITLE
:
568 return "set timer program title";
569 case CEC_OPCODE_TIMER_CLEARED_STATUS
:
570 return "timer cleared status";
571 case CEC_OPCODE_TIMER_STATUS
:
572 return "timer status";
573 case CEC_OPCODE_CEC_VERSION
:
574 return "cec version";
575 case CEC_OPCODE_GET_CEC_VERSION
:
576 return "get cec version";
577 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
578 return "give physical address";
579 case CEC_OPCODE_GET_MENU_LANGUAGE
:
580 return "get menu language";
581 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
:
582 return "report physical address";
583 case CEC_OPCODE_SET_MENU_LANGUAGE
:
584 return "set menu language";
585 case CEC_OPCODE_DECK_CONTROL
:
586 return "deck control";
587 case CEC_OPCODE_DECK_STATUS
:
588 return "deck status";
589 case CEC_OPCODE_GIVE_DECK_STATUS
:
590 return "give deck status";
591 case CEC_OPCODE_PLAY
:
593 case CEC_OPCODE_GIVE_TUNER_DEVICE_STATUS
:
594 return "give tuner status";
595 case CEC_OPCODE_SELECT_ANALOGUE_SERVICE
:
596 return "select analogue service";
597 case CEC_OPCODE_SELECT_DIGITAL_SERVICE
:
598 return "set digital service";
599 case CEC_OPCODE_TUNER_DEVICE_STATUS
:
600 return "tuner device status";
601 case CEC_OPCODE_TUNER_STEP_DECREMENT
:
602 return "tuner step decrement";
603 case CEC_OPCODE_TUNER_STEP_INCREMENT
:
604 return "tuner step increment";
605 case CEC_OPCODE_DEVICE_VENDOR_ID
:
606 return "device vendor id";
607 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
608 return "give device vendor id";
609 case CEC_OPCODE_VENDOR_COMMAND
:
610 return "vendor command";
611 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
612 return "vendor command with id";
613 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN
:
614 return "vendor remote button down";
615 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP
:
616 return "vendor remote button up";
617 case CEC_OPCODE_SET_OSD_STRING
:
618 return "set osd string";
619 case CEC_OPCODE_GIVE_OSD_NAME
:
620 return "give osd name";
621 case CEC_OPCODE_SET_OSD_NAME
:
622 return "set osd name";
623 case CEC_OPCODE_MENU_REQUEST
:
624 return "menu request";
625 case CEC_OPCODE_MENU_STATUS
:
626 return "menu status";
627 case CEC_OPCODE_USER_CONTROL_PRESSED
:
628 return "user control pressed";
629 case CEC_OPCODE_USER_CONTROL_RELEASE
:
630 return "user control release";
631 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
632 return "give device power status";
633 case CEC_OPCODE_REPORT_POWER_STATUS
:
634 return "report power status";
635 case CEC_OPCODE_FEATURE_ABORT
:
636 return "feature abort";
637 case CEC_OPCODE_ABORT
:
639 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
640 return "give audio status";
641 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
642 return "give audio mode status";
643 case CEC_OPCODE_REPORT_AUDIO_STATUS
:
644 return "report audio status";
645 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
:
646 return "set system audio mode";
647 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
648 return "system audio mode request";
649 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
:
650 return "system audio mode status";
651 case CEC_OPCODE_SET_AUDIO_RATE
:
652 return "set audio rate";