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 CCECBusDevice
*device
= NULL
;
427 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
429 if (m_busDevice
->GetProcessor()->m_busDevices
[iPtr
]->GetPhysicalAddress() == iPhysicalAddress
)
431 device
= m_busDevice
->GetProcessor()->m_busDevices
[iPtr
];
439 void CCECCommandHandler::SetVendorId(const cec_command
&command
)
441 if (command
.parameters
.size
< 3)
443 m_busDevice
->AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
447 uint64_t iVendorId
= ((uint64_t)command
.parameters
[0] << 16) +
448 ((uint64_t)command
.parameters
[1] << 8) +
449 (uint64_t)command
.parameters
[2];
451 CCECBusDevice
*device
= GetDevice((cec_logical_address
) command
.initiator
);
453 device
->SetVendorId(iVendorId
, command
.parameters
.size
> 3 ? command
.parameters
[3] : 0);
456 const char *CCECCommandHandler::ToString(const cec_logical_address address
)
460 case CECDEVICE_AUDIOSYSTEM
:
462 case CECDEVICE_BROADCAST
:
464 case CECDEVICE_FREEUSE
:
466 case CECDEVICE_PLAYBACKDEVICE1
:
468 case CECDEVICE_PLAYBACKDEVICE2
:
470 case CECDEVICE_PLAYBACKDEVICE3
:
472 case CECDEVICE_RECORDINGDEVICE1
:
474 case CECDEVICE_RECORDINGDEVICE2
:
476 case CECDEVICE_RECORDINGDEVICE3
:
478 case CECDEVICE_RESERVED1
:
480 case CECDEVICE_RESERVED2
:
482 case CECDEVICE_TUNER1
:
484 case CECDEVICE_TUNER2
:
486 case CECDEVICE_TUNER3
:
488 case CECDEVICE_TUNER4
:
497 const char *CCECCommandHandler::ToString(const cec_opcode opcode
)
501 case CEC_OPCODE_ACTIVE_SOURCE
:
502 return "active source";
503 case CEC_OPCODE_IMAGE_VIEW_ON
:
504 return "image view on";
505 case CEC_OPCODE_TEXT_VIEW_ON
:
506 return "text view on";
507 case CEC_OPCODE_INACTIVE_SOURCE
:
508 return "inactive source";
509 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
510 return "request active source";
511 case CEC_OPCODE_ROUTING_CHANGE
:
512 return "routing change";
513 case CEC_OPCODE_ROUTING_INFORMATION
:
514 return "routing information";
515 case CEC_OPCODE_SET_STREAM_PATH
:
516 return "set stream path";
517 case CEC_OPCODE_STANDBY
:
519 case CEC_OPCODE_RECORD_OFF
:
521 case CEC_OPCODE_RECORD_ON
:
523 case CEC_OPCODE_RECORD_STATUS
:
524 return "record status";
525 case CEC_OPCODE_RECORD_TV_SCREEN
:
526 return "record tv screen";
527 case CEC_OPCODE_CLEAR_ANALOGUE_TIMER
:
528 return "clear analogue timer";
529 case CEC_OPCODE_CLEAR_DIGITAL_TIMER
:
530 return "clear digital timer";
531 case CEC_OPCODE_CLEAR_EXTERNAL_TIMER
:
532 return "clear external timer";
533 case CEC_OPCODE_SET_ANALOGUE_TIMER
:
534 return "set analogue timer";
535 case CEC_OPCODE_SET_DIGITAL_TIMER
:
536 return "set digital timer";
537 case CEC_OPCODE_SET_EXTERNAL_TIMER
:
538 return "set external timer";
539 case CEC_OPCODE_SET_TIMER_PROGRAM_TITLE
:
540 return "set timer program title";
541 case CEC_OPCODE_TIMER_CLEARED_STATUS
:
542 return "timer cleared status";
543 case CEC_OPCODE_TIMER_STATUS
:
544 return "timer status";
545 case CEC_OPCODE_CEC_VERSION
:
546 return "cec version";
547 case CEC_OPCODE_GET_CEC_VERSION
:
548 return "get cec version";
549 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
550 return "give physical address";
551 case CEC_OPCODE_GET_MENU_LANGUAGE
:
552 return "get menu language";
553 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
:
554 return "report physical address";
555 case CEC_OPCODE_SET_MENU_LANGUAGE
:
556 return "set menu language";
557 case CEC_OPCODE_DECK_CONTROL
:
558 return "deck control";
559 case CEC_OPCODE_DECK_STATUS
:
560 return "deck status";
561 case CEC_OPCODE_GIVE_DECK_STATUS
:
562 return "give deck status";
563 case CEC_OPCODE_PLAY
:
565 case CEC_OPCODE_GIVE_TUNER_DEVICE_STATUS
:
566 return "give tuner status";
567 case CEC_OPCODE_SELECT_ANALOGUE_SERVICE
:
568 return "select analogue service";
569 case CEC_OPCODE_SELECT_DIGITAL_SERVICE
:
570 return "set digital service";
571 case CEC_OPCODE_TUNER_DEVICE_STATUS
:
572 return "tuner device status";
573 case CEC_OPCODE_TUNER_STEP_DECREMENT
:
574 return "tuner step decrement";
575 case CEC_OPCODE_TUNER_STEP_INCREMENT
:
576 return "tuner step increment";
577 case CEC_OPCODE_DEVICE_VENDOR_ID
:
578 return "device vendor id";
579 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
580 return "give device vendor id";
581 case CEC_OPCODE_VENDOR_COMMAND
:
582 return "vendor command";
583 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
584 return "vendor command with id";
585 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN
:
586 return "vendor remote button down";
587 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP
:
588 return "vendor remote button up";
589 case CEC_OPCODE_SET_OSD_STRING
:
590 return "set osd string";
591 case CEC_OPCODE_GIVE_OSD_NAME
:
592 return "give osd name";
593 case CEC_OPCODE_SET_OSD_NAME
:
594 return "set osd name";
595 case CEC_OPCODE_MENU_REQUEST
:
596 return "menu request";
597 case CEC_OPCODE_MENU_STATUS
:
598 return "menu status";
599 case CEC_OPCODE_USER_CONTROL_PRESSED
:
600 return "user control pressed";
601 case CEC_OPCODE_USER_CONTROL_RELEASE
:
602 return "user control release";
603 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
604 return "give device power status";
605 case CEC_OPCODE_REPORT_POWER_STATUS
:
606 return "report power status";
607 case CEC_OPCODE_FEATURE_ABORT
:
608 return "feature abort";
609 case CEC_OPCODE_ABORT
:
611 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
612 return "give audio status";
613 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
614 return "give audio mode status";
615 case CEC_OPCODE_REPORT_AUDIO_STATUS
:
616 return "report audio status";
617 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
:
618 return "set system audio mode";
619 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
620 return "system audio mode request";
621 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
:
622 return "system audio mode status";
623 case CEC_OPCODE_SET_AUDIO_RATE
:
624 return "set audio rate";