2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011-2012 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 "../CECClient.h"
38 #include "../CECProcessor.h"
39 #include "../LibCEC.h"
40 #include "../CECTypeUtils.h"
41 #include "../platform/util/util.h"
45 using namespace PLATFORM
;
47 #define LIB_CEC m_busDevice->GetProcessor()->GetLib()
48 #define ToString(p) CCECTypeUtils::ToString(p)
50 CCECCommandHandler::CCECCommandHandler(CCECBusDevice
*busDevice
) :
51 m_busDevice(busDevice
),
52 m_processor(m_busDevice
->GetProcessor()),
53 m_iTransmitTimeout(CEC_DEFAULT_TRANSMIT_TIMEOUT
),
54 m_iTransmitWait(CEC_DEFAULT_TRANSMIT_WAIT
),
55 m_iTransmitRetries(CEC_DEFAULT_TRANSMIT_RETRIES
),
56 m_bHandlerInited(false),
57 m_bOPTSendDeckStatusUpdateOnActiveSource(false),
58 m_vendorId(CEC_VENDOR_UNKNOWN
),
59 m_waitForResponse(new CWaitForResponse
),
60 m_bActiveSourcePending(false)
64 CCECCommandHandler::~CCECCommandHandler(void)
66 DELETE_AND_NULL(m_waitForResponse
);
69 bool CCECCommandHandler::HandleCommand(const cec_command
&command
)
71 if (command
.opcode_set
== 0)
72 return HandlePoll(command
);
76 CCECClient
*client
= m_busDevice
->GetClient();
78 client
->AddCommand(command
);
80 switch(command
.opcode
)
82 case CEC_OPCODE_REPORT_POWER_STATUS
:
83 HandleReportPowerStatus(command
);
85 case CEC_OPCODE_CEC_VERSION
:
86 HandleDeviceCecVersion(command
);
88 case CEC_OPCODE_SET_MENU_LANGUAGE
:
89 HandleSetMenuLanguage(command
);
91 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
92 if (m_processor
->CECInitialised())
93 HandleGivePhysicalAddress(command
);
95 case CEC_OPCODE_GET_MENU_LANGUAGE
:
96 if (m_processor
->CECInitialised())
97 HandleGiveMenuLanguage(command
);
99 case CEC_OPCODE_GIVE_OSD_NAME
:
100 if (m_processor
->CECInitialised())
101 HandleGiveOSDName(command
);
103 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
104 if (m_processor
->CECInitialised())
105 HandleGiveDeviceVendorId(command
);
107 case CEC_OPCODE_DEVICE_VENDOR_ID
:
108 HandleDeviceVendorId(command
);
110 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
111 HandleDeviceVendorCommandWithId(command
);
113 case CEC_OPCODE_GIVE_DECK_STATUS
:
114 if (m_processor
->CECInitialised())
115 HandleGiveDeckStatus(command
);
117 case CEC_OPCODE_DECK_CONTROL
:
118 HandleDeckControl(command
);
120 case CEC_OPCODE_MENU_REQUEST
:
121 if (m_processor
->CECInitialised())
122 HandleMenuRequest(command
);
124 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
125 if (m_processor
->CECInitialised())
126 HandleGiveDevicePowerStatus(command
);
128 case CEC_OPCODE_GET_CEC_VERSION
:
129 if (m_processor
->CECInitialised())
130 HandleGetCecVersion(command
);
132 case CEC_OPCODE_USER_CONTROL_PRESSED
:
133 if (m_processor
->CECInitialised())
134 HandleUserControlPressed(command
);
136 case CEC_OPCODE_USER_CONTROL_RELEASE
:
137 if (m_processor
->CECInitialised())
138 HandleUserControlRelease(command
);
140 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
141 if (m_processor
->CECInitialised())
142 HandleGiveAudioStatus(command
);
144 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
145 if (m_processor
->CECInitialised())
146 HandleGiveSystemAudioModeStatus(command
);
148 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
149 if (m_processor
->CECInitialised())
150 HandleSystemAudioModeRequest(command
);
152 case CEC_OPCODE_REPORT_AUDIO_STATUS
:
153 HandleReportAudioStatus(command
);
155 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
:
156 HandleSystemAudioModeStatus(command
);
158 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
:
159 HandleSetSystemAudioMode(command
);
161 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
162 if (m_processor
->CECInitialised())
163 HandleRequestActiveSource(command
);
165 case CEC_OPCODE_SET_STREAM_PATH
:
166 HandleSetStreamPath(command
);
168 case CEC_OPCODE_ROUTING_CHANGE
:
169 HandleRoutingChange(command
);
171 case CEC_OPCODE_ROUTING_INFORMATION
:
172 HandleRoutingInformation(command
);
174 case CEC_OPCODE_STANDBY
:
175 if (m_processor
->CECInitialised())
176 HandleStandby(command
);
178 case CEC_OPCODE_ACTIVE_SOURCE
:
179 HandleActiveSource(command
);
181 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
:
182 HandleReportPhysicalAddress(command
);
184 case CEC_OPCODE_SET_OSD_NAME
:
185 HandleSetOSDName(command
);
187 case CEC_OPCODE_IMAGE_VIEW_ON
:
188 HandleImageViewOn(command
);
190 case CEC_OPCODE_TEXT_VIEW_ON
:
191 HandleTextViewOn(command
);
193 case CEC_OPCODE_FEATURE_ABORT
:
194 HandleFeatureAbort(command
);
196 case CEC_OPCODE_VENDOR_COMMAND
:
197 HandleVendorCommand(command
);
199 case CEC_OPCODE_PLAY
:
200 // libCEC (currently) doesn't need to do anything with this, since player applications handle it
201 // but it should not respond with a feature abort
209 m_waitForResponse
->Received((command
.opcode
== CEC_OPCODE_FEATURE_ABORT
&& command
.parameters
.size
> 0) ? (cec_opcode
)command
.parameters
[0] : command
.opcode
);
211 UnhandledCommand(command
);
216 bool CCECCommandHandler::HandleActiveSource(const cec_command
&command
)
218 if (command
.parameters
.size
== 2)
220 uint16_t iAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
221 CCECBusDevice
*device
= m_processor
->GetDeviceByPhysicalAddress(iAddress
);
223 device
->MarkAsActiveSource();
229 bool CCECCommandHandler::HandleDeckControl(const cec_command
&command
)
231 CCECPlaybackDevice
*device
= CCECBusDevice::AsPlaybackDevice(GetDevice(command
.destination
));
232 if (device
&& command
.parameters
.size
> 0)
234 device
->SetDeckControlMode((cec_deck_control_mode
) command
.parameters
[0]);
241 bool CCECCommandHandler::HandleDeviceCecVersion(const cec_command
&command
)
243 if (command
.parameters
.size
== 1)
245 CCECBusDevice
*device
= GetDevice(command
.initiator
);
247 device
->SetCecVersion((cec_version
) command
.parameters
[0]);
253 bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command
&command
)
255 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
256 m_processor
->TransmitAbort(command
.destination
, command
.initiator
, command
.opcode
, CEC_ABORT_REASON_REFUSED
);
261 bool CCECCommandHandler::HandleDeviceVendorId(const cec_command
&command
)
263 return SetVendorId(command
);
266 bool CCECCommandHandler::HandleFeatureAbort(const cec_command
&command
)
268 if (command
.parameters
.size
== 2 &&
269 (command
.parameters
[1] == CEC_ABORT_REASON_UNRECOGNIZED_OPCODE
||
270 command
.parameters
[1] == CEC_ABORT_REASON_REFUSED
))
271 m_processor
->GetDevice(command
.initiator
)->SetUnsupportedFeature((cec_opcode
)command
.parameters
[0]);
275 bool CCECCommandHandler::HandleGetCecVersion(const cec_command
&command
)
277 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
279 CCECBusDevice
*device
= GetDevice(command
.destination
);
281 return device
->TransmitCECVersion(command
.initiator
);
287 bool CCECCommandHandler::HandleGiveAudioStatus(const cec_command
&command
)
289 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
291 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.destination
));
293 return device
->TransmitAudioStatus(command
.initiator
);
299 bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
301 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
303 CCECPlaybackDevice
*device
= CCECBusDevice::AsPlaybackDevice(GetDevice(command
.destination
));
305 return device
->TransmitDeckStatus(command
.initiator
);
311 bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command
&command
)
313 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
315 CCECBusDevice
*device
= GetDevice(command
.destination
);
317 return device
->TransmitPowerState(command
.initiator
);
323 bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command
&command
)
325 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
327 CCECBusDevice
*device
= GetDevice(command
.destination
);
329 return device
->TransmitVendorID(command
.initiator
);
335 bool CCECCommandHandler::HandleGiveOSDName(const cec_command
&command
)
337 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
339 CCECBusDevice
*device
= GetDevice(command
.destination
);
341 return device
->TransmitOSDName(command
.initiator
);
347 bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command
&command
)
349 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
351 CCECBusDevice
*device
= GetDevice(command
.destination
);
353 return device
->TransmitPhysicalAddress();
359 bool CCECCommandHandler::HandleGiveMenuLanguage(const cec_command
&command
)
361 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
363 CCECBusDevice
*device
= GetDevice(command
.destination
);
365 return device
->TransmitSetMenuLanguage(command
.initiator
);
371 bool CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command
&command
)
373 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
375 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.destination
));
377 return device
->TransmitSystemAudioModeStatus(command
.initiator
);
383 bool CCECCommandHandler::HandleImageViewOn(const cec_command
&command
)
385 m_processor
->GetDevice(command
.initiator
)->MarkAsActiveSource();
389 bool CCECCommandHandler::HandleMenuRequest(const cec_command
&command
)
391 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
393 CCECBusDevice
*device
= GetDevice(command
.destination
);
396 CCECClient
*client
= device
->GetClient();
399 if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_ACTIVATE
)
401 if (client
->MenuStateChanged(CEC_MENU_STATE_ACTIVATED
) == 1)
402 device
->SetMenuState(CEC_MENU_STATE_ACTIVATED
);
404 else if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_DEACTIVATE
)
406 if (client
->MenuStateChanged(CEC_MENU_STATE_DEACTIVATED
) == 1)
407 device
->SetMenuState(CEC_MENU_STATE_DEACTIVATED
);
410 return device
->TransmitMenuState(command
.initiator
);
417 bool CCECCommandHandler::HandlePoll(const cec_command
&command
)
419 m_busDevice
->HandlePoll(command
.destination
);
423 bool CCECCommandHandler::HandleReportAudioStatus(const cec_command
&command
)
425 if (command
.parameters
.size
== 1)
427 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.initiator
));
430 device
->SetAudioStatus(command
.parameters
[0]);
437 bool CCECCommandHandler::HandleReportPhysicalAddress(const cec_command
&command
)
439 if (command
.parameters
.size
== 3)
441 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
442 SetPhysicalAddress(command
.initiator
, iNewAddress
);
447 bool CCECCommandHandler::HandleReportPowerStatus(const cec_command
&command
)
449 if (command
.parameters
.size
== 1)
451 CCECBusDevice
*device
= GetDevice(command
.initiator
);
453 device
->SetPowerStatus((cec_power_status
) command
.parameters
[0]);
458 bool CCECCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
460 if (m_processor
->CECInitialised())
462 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, ">> %i requests active source", (uint8_t) command
.initiator
);
463 m_processor
->GetDevice(command
.initiator
)->SetPowerStatus(CEC_POWER_STATUS_ON
);
465 vector
<CCECBusDevice
*> devices
;
466 for (size_t iDevicePtr
= 0; iDevicePtr
< GetMyDevices(devices
); iDevicePtr
++)
467 devices
[iDevicePtr
]->TransmitActiveSource();
474 bool CCECCommandHandler::HandleRoutingChange(const cec_command
&command
)
476 if (command
.parameters
.size
== 4)
478 uint16_t iOldAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
479 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[2] << 8) | ((uint16_t)command
.parameters
[3]);
481 CCECBusDevice
*device
= GetDevice(command
.initiator
);
483 device
->SetStreamPath(iNewAddress
, iOldAddress
);
488 bool CCECCommandHandler::HandleRoutingInformation(const cec_command
&command
)
490 if (command
.parameters
.size
== 2)
492 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
493 CCECBusDevice
*device
= m_processor
->GetDeviceByPhysicalAddress(iNewAddress
);
495 device
->MarkAsActiveSource();
501 bool CCECCommandHandler::HandleSetMenuLanguage(const cec_command
&command
)
503 if (command
.parameters
.size
== 3)
505 CCECBusDevice
*device
= GetDevice(command
.initiator
);
508 cec_menu_language language
;
509 language
.device
= command
.initiator
;
510 for (uint8_t iPtr
= 0; iPtr
< 4; iPtr
++)
511 language
.language
[iPtr
] = command
.parameters
[iPtr
];
512 language
.language
[3] = 0;
513 device
->SetMenuLanguage(language
);
520 bool CCECCommandHandler::HandleSetOSDName(const cec_command
&command
)
522 if (command
.parameters
.size
> 0)
524 CCECBusDevice
*device
= GetDevice(command
.initiator
);
528 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
529 buf
[iPtr
] = (char)command
.parameters
[iPtr
];
530 buf
[command
.parameters
.size
] = 0;
532 CStdString
strName(buf
);
533 device
->SetOSDName(strName
);
541 bool CCECCommandHandler::HandleSetStreamPath(const cec_command
&command
)
543 if (m_processor
->CECInitialised() && command
.parameters
.size
>= 2)
545 uint16_t iStreamAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
546 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, ">> %i sets stream path to physical address %04x", command
.initiator
, iStreamAddress
);
548 /* one of the device handled by libCEC has been made active */
549 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamAddress
);
550 if (device
&& device
->IsHandledByLibCEC())
551 device
->ActivateSource();
556 bool CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command
&command
)
558 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
560 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.destination
));
563 if (command
.parameters
.size
>= 2)
565 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
566 device
->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON
);
567 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
568 CCECBusDevice
*newActiveDevice
= GetDeviceByPhysicalAddress(iNewAddress
);
570 newActiveDevice
->MarkAsActiveSource();
571 return device
->TransmitSetSystemAudioMode(command
.initiator
);
575 device
->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF
);
576 return device
->TransmitSetSystemAudioMode(command
.initiator
);
583 bool CCECCommandHandler::HandleStandby(const cec_command
&command
)
585 CCECBusDevice
*device
= GetDevice(command
.initiator
);
587 device
->SetPowerStatus(CEC_POWER_STATUS_STANDBY
);
592 bool CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command
&command
)
594 if (command
.parameters
.size
== 1)
596 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.initiator
));
599 device
->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
607 bool CCECCommandHandler::HandleSetSystemAudioMode(const cec_command
&command
)
609 if (command
.parameters
.size
== 1)
611 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.initiator
));
614 device
->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
622 bool CCECCommandHandler::HandleTextViewOn(const cec_command
&command
)
624 m_processor
->GetDevice(command
.initiator
)->MarkAsActiveSource();
628 bool CCECCommandHandler::HandleUserControlPressed(const cec_command
&command
)
630 if (m_processor
->CECInitialised() &&
631 m_processor
->IsHandledByLibCEC(command
.destination
) &&
632 command
.parameters
.size
> 0)
634 CCECBusDevice
*device
= GetDevice(command
.destination
);
638 CCECClient
*client
= device
->GetClient();
642 if (command
.parameters
[0] <= CEC_USER_CONTROL_CODE_MAX
)
643 client
->SetCurrentButton((cec_user_control_code
) command
.parameters
[0]);
645 if (command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER
||
646 command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION
)
652 // CEC_USER_CONTROL_CODE_POWER operates as a toggle
653 // assume CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION does not
654 if (command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER
)
656 cec_power_status status
= device
->GetCurrentPowerStatus();
657 bPowerOn
= !(status
== CEC_POWER_STATUS_ON
|| status
== CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
662 device
->ActivateSource();
666 device
->MarkAsInactiveSource();
667 device
->TransmitInactiveSource();
668 device
->SetMenuState(CEC_MENU_STATE_DEACTIVATED
);
677 bool CCECCommandHandler::HandleUserControlRelease(const cec_command
&command
)
679 CCECClient
*client
= m_processor
->GetClient(command
.destination
);
685 bool CCECCommandHandler::HandleVendorCommand(const cec_command
&command
)
687 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
688 m_processor
->TransmitAbort(command
.destination
, command
.initiator
, command
.opcode
, CEC_ABORT_REASON_REFUSED
);
692 void CCECCommandHandler::UnhandledCommand(const cec_command
&command
)
694 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "unhandled command with opcode %02x from address %d", command
.opcode
, command
.initiator
);
696 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
697 m_processor
->TransmitAbort(m_busDevice
->GetLogicalAddress(), command
.initiator
, command
.opcode
, CEC_ABORT_REASON_UNRECOGNIZED_OPCODE
);
700 size_t CCECCommandHandler::GetMyDevices(vector
<CCECBusDevice
*> &devices
) const
704 cec_logical_addresses addresses
= m_processor
->GetLogicalAddresses();
705 for (uint8_t iPtr
= CECDEVICE_TV
; iPtr
< CECDEVICE_BROADCAST
; iPtr
++)
709 devices
.push_back(GetDevice((cec_logical_address
) iPtr
));
717 CCECBusDevice
*CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress
) const
719 return m_processor
->GetDevice(iLogicalAddress
);
722 CCECBusDevice
*CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
) const
724 return m_processor
->GetDeviceByPhysicalAddress(iPhysicalAddress
);
727 bool CCECCommandHandler::SetVendorId(const cec_command
&command
)
729 bool bChanged(false);
730 if (command
.parameters
.size
< 3)
732 LIB_CEC
->AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
736 uint64_t iVendorId
= ((uint64_t)command
.parameters
[0] << 16) +
737 ((uint64_t)command
.parameters
[1] << 8) +
738 (uint64_t)command
.parameters
[2];
740 CCECBusDevice
*device
= GetDevice((cec_logical_address
) command
.initiator
);
742 bChanged
= device
->SetVendorId(iVendorId
);
746 void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress
, uint16_t iNewAddress
)
748 if (!m_processor
->IsHandledByLibCEC(iAddress
))
750 CCECBusDevice
*otherDevice
= m_processor
->GetDeviceByPhysicalAddress(iNewAddress
);
751 CCECClient
*client
= otherDevice
? otherDevice
->GetClient() : NULL
;
753 CCECBusDevice
*device
= m_processor
->GetDevice(iAddress
);
755 device
->SetPhysicalAddress(iNewAddress
);
758 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "device with logical address %X not found", iAddress
);
761 /* another device reported the same physical address as ours */
763 client
->ResetPhysicalAddress();
767 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "ignore physical address report for device %s (%X) because it's marked as handled by libCEC", ToString(iAddress
), iAddress
);
771 bool CCECCommandHandler::PowerOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
773 if (iDestination
== CECDEVICE_TV
)
774 return TransmitImageViewOn(iInitiator
, iDestination
);
776 return TransmitKeypress(iInitiator
, iDestination
, CEC_USER_CONTROL_CODE_POWER
) &&
777 TransmitKeyRelease(iInitiator
, iDestination
);
780 bool CCECCommandHandler::TransmitImageViewOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
783 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_IMAGE_VIEW_ON
);
785 return Transmit(command
);
788 bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
791 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_STANDBY
);
793 return Transmit(command
);
796 bool CCECCommandHandler::TransmitRequestActiveSource(const cec_logical_address iInitiator
, bool bWaitForResponse
/* = true */)
799 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_REQUEST_ACTIVE_SOURCE
);
801 return Transmit(command
, !bWaitForResponse
);
804 bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
807 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_CEC_VERSION
);
809 return Transmit(command
, !bWaitForResponse
);
812 bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
815 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_MENU_LANGUAGE
);
817 return Transmit(command
, !bWaitForResponse
);
820 bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
823 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_OSD_NAME
);
825 return Transmit(command
, !bWaitForResponse
);
828 bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
831 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
);
833 return Transmit(command
, !bWaitForResponse
);
836 bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
839 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
);
841 return Transmit(command
, !bWaitForResponse
);
844 bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
847 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
849 return Transmit(command
, !bWaitForResponse
);
852 bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
855 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
856 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
857 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
859 return Transmit(command
);
862 bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_version cecVersion
)
865 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_CEC_VERSION
);
866 command
.parameters
.PushBack((uint8_t)cecVersion
);
868 return Transmit(command
);
871 bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
874 cec_command::Format(command
, iInitiator
, CECDEVICE_TV
, CEC_OPCODE_INACTIVE_SOURCE
);
875 command
.parameters
.PushBack((iPhysicalAddress
>> 8) & 0xFF);
876 command
.parameters
.PushBack(iPhysicalAddress
& 0xFF);
878 return Transmit(command
);
881 bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_menu_state menuState
)
884 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_MENU_STATUS
);
885 command
.parameters
.PushBack((uint8_t)menuState
);
887 return Transmit(command
);
890 bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, CStdString strDeviceName
)
893 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_NAME
);
894 for (size_t iPtr
= 0; iPtr
< strDeviceName
.length(); iPtr
++)
895 command
.parameters
.PushBack(strDeviceName
.at(iPtr
));
897 return Transmit(command
);
900 bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_display_control duration
, const char *strMessage
)
903 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_STRING
);
904 command
.parameters
.PushBack((uint8_t)duration
);
906 size_t iLen
= strlen(strMessage
);
907 if (iLen
> 13) iLen
= 13;
909 for (size_t iPtr
= 0; iPtr
< iLen
; iPtr
++)
910 command
.parameters
.PushBack(strMessage
[iPtr
]);
912 return Transmit(command
);
915 bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
, cec_device_type type
)
918 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
919 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
920 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
921 command
.parameters
.PushBack((uint8_t) (type
));
923 return Transmit(command
);
926 bool CCECCommandHandler::TransmitSetMenuLanguage(const cec_logical_address iInitiator
, const char lang
[3])
929 command
.Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_SET_MENU_LANGUAGE
);
930 command
.parameters
.PushBack((uint8_t) lang
[0]);
931 command
.parameters
.PushBack((uint8_t) lang
[1]);
932 command
.parameters
.PushBack((uint8_t) lang
[2]);
934 return Transmit(command
);
937 bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
940 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_NONE
);
942 return Transmit(command
);
945 bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_power_status state
)
948 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_POWER_STATUS
);
949 command
.parameters
.PushBack((uint8_t) state
);
951 return Transmit(command
);
954 bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator
, uint64_t iVendorId
)
957 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_DEVICE_VENDOR_ID
);
959 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 16) & 0xFF));
960 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 8) & 0xFF));
961 command
.parameters
.PushBack((uint8_t) ((uint64_t)iVendorId
& 0xFF));
963 return Transmit(command
);
966 bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, uint8_t state
)
969 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_AUDIO_STATUS
);
970 command
.parameters
.PushBack(state
);
972 return Transmit(command
);
975 bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
)
978 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
);
979 command
.parameters
.PushBack((uint8_t)state
);
981 return Transmit(command
);
984 bool CCECCommandHandler::TransmitSetStreamPath(uint16_t iStreamPath
)
987 cec_command::Format(command
, m_busDevice
->GetLogicalAddress(), CECDEVICE_BROADCAST
, CEC_OPCODE_SET_STREAM_PATH
);
988 command
.parameters
.PushBack((uint8_t) ((iStreamPath
>> 8) & 0xFF));
989 command
.parameters
.PushBack((uint8_t) (iStreamPath
& 0xFF));
991 return Transmit(command
);
994 bool CCECCommandHandler::TransmitSystemAudioModeStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
)
997 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
);
998 command
.parameters
.PushBack((uint8_t)state
);
1000 return Transmit(command
);
1003 bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_deck_info state
)
1005 cec_command command
;
1006 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_DECK_STATUS
);
1007 command
.PushBack((uint8_t)state
);
1009 return Transmit(command
);
1012 bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = true */)
1014 cec_command command
;
1015 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_PRESSED
);
1016 command
.parameters
.PushBack((uint8_t)key
);
1018 return Transmit(command
, !bWait
);
1021 bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWait
/* = true */)
1023 cec_command command
;
1024 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_RELEASE
);
1026 return Transmit(command
, !bWait
);
1029 bool CCECCommandHandler::Transmit(cec_command
&command
, bool bSuppressWait
/* = false */)
1031 bool bReturn(false);
1032 cec_opcode
expectedResponse(cec_command::GetResponseOpcode(command
.opcode
));
1033 bool bExpectResponse(expectedResponse
!= CEC_OPCODE_NONE
&& !bSuppressWait
);
1034 command
.transmit_timeout
= m_iTransmitTimeout
;
1036 if (command
.initiator
== CECDEVICE_UNKNOWN
)
1038 LIB_CEC
->AddLog(CEC_LOG_ERROR
, "not transmitting a command without a valid initiator");
1043 uint8_t iTries(0), iMaxTries(!command
.opcode_set
? 1 : m_iTransmitRetries
+ 1);
1044 while (!bReturn
&& ++iTries
<= iMaxTries
&& !m_busDevice
->IsUnsupportedFeature(command
.opcode
))
1046 if ((bReturn
= m_processor
->Transmit(command
)) == true)
1048 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "command transmitted");
1049 if (bExpectResponse
)
1051 bReturn
= m_waitForResponse
->Wait(expectedResponse
);
1052 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, bReturn
? "expected response received (%X: %s)" : "expected response not received (%X: %s)", (int)expectedResponse
, ToString(expectedResponse
));
1061 bool CCECCommandHandler::ActivateSource(void)
1063 if (m_busDevice
->IsActiveSource() &&
1064 m_busDevice
->IsHandledByLibCEC())
1067 CLockObject
lock(m_mutex
);
1068 m_bActiveSourcePending
= false;
1071 m_busDevice
->SetPowerStatus(CEC_POWER_STATUS_ON
);
1072 m_busDevice
->SetMenuState(CEC_MENU_STATE_ACTIVATED
);
1074 bool bActiveSourceFailed
= !m_busDevice
->TransmitImageViewOn() ||
1075 !m_busDevice
->TransmitActiveSource() ||
1076 !m_busDevice
->TransmitMenuState(CECDEVICE_TV
);
1078 if (!bActiveSourceFailed
)
1080 CCECPlaybackDevice
*playbackDevice
= m_busDevice
->AsPlaybackDevice();
1081 if (playbackDevice
&& SendDeckStatusUpdateOnActiveSource())
1082 bActiveSourceFailed
= !playbackDevice
->TransmitDeckStatus(CECDEVICE_TV
);
1085 if (bActiveSourceFailed
)
1087 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "failed to make '%s' the active source. will retry later", m_busDevice
->GetLogicalAddressName());
1088 CLockObject
lock(m_mutex
);
1089 m_bActiveSourcePending
= true;
1093 m_bHandlerInited
= true;
1098 void CCECCommandHandler::SignalOpcode(cec_opcode opcode
)
1100 m_waitForResponse
->Received(opcode
);
1103 bool CCECCommandHandler::ActiveSourcePending(void)
1105 CLockObject
lock(m_mutex
);
1106 return m_bActiveSourcePending
;