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 "../platform/util/util.h"
44 using namespace PLATFORM
;
46 #define LIB_CEC m_busDevice->GetProcessor()->GetLib()
47 #define ToString(p) LIB_CEC->ToString(p)
49 CCECCommandHandler::CCECCommandHandler(CCECBusDevice
*busDevice
) :
50 m_busDevice(busDevice
),
51 m_processor(m_busDevice
->GetProcessor()),
52 m_iTransmitTimeout(CEC_DEFAULT_TRANSMIT_TIMEOUT
),
53 m_iTransmitWait(CEC_DEFAULT_TRANSMIT_WAIT
),
54 m_iTransmitRetries(CEC_DEFAULT_TRANSMIT_RETRIES
),
55 m_bHandlerInited(false),
56 m_bOPTSendDeckStatusUpdateOnActiveSource(false),
57 m_vendorId(CEC_VENDOR_UNKNOWN
),
58 m_waitForResponse(new CWaitForResponse
)
62 CCECCommandHandler::~CCECCommandHandler(void)
64 DELETE_AND_NULL(m_waitForResponse
);
67 bool CCECCommandHandler::HandleCommand(const cec_command
&command
)
69 if (command
.opcode_set
== 0)
70 return HandlePoll(command
);
74 CCECClient
*client
= m_busDevice
->GetClient();
76 client
->AddCommand(command
);
78 switch(command
.opcode
)
80 case CEC_OPCODE_REPORT_POWER_STATUS
:
81 HandleReportPowerStatus(command
);
83 case CEC_OPCODE_CEC_VERSION
:
84 HandleDeviceCecVersion(command
);
86 case CEC_OPCODE_SET_MENU_LANGUAGE
:
87 HandleSetMenuLanguage(command
);
89 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
90 if (m_processor
->CECInitialised())
91 HandleGivePhysicalAddress(command
);
93 case CEC_OPCODE_GET_MENU_LANGUAGE
:
94 if (m_processor
->CECInitialised())
95 HandleGiveMenuLanguage(command
);
97 case CEC_OPCODE_GIVE_OSD_NAME
:
98 if (m_processor
->CECInitialised())
99 HandleGiveOSDName(command
);
101 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
102 if (m_processor
->CECInitialised())
103 HandleGiveDeviceVendorId(command
);
105 case CEC_OPCODE_DEVICE_VENDOR_ID
:
106 HandleDeviceVendorId(command
);
108 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
109 HandleDeviceVendorCommandWithId(command
);
111 case CEC_OPCODE_GIVE_DECK_STATUS
:
112 if (m_processor
->CECInitialised())
113 HandleGiveDeckStatus(command
);
115 case CEC_OPCODE_DECK_CONTROL
:
116 HandleDeckControl(command
);
118 case CEC_OPCODE_MENU_REQUEST
:
119 if (m_processor
->CECInitialised())
120 HandleMenuRequest(command
);
122 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
123 if (m_processor
->CECInitialised())
124 HandleGiveDevicePowerStatus(command
);
126 case CEC_OPCODE_GET_CEC_VERSION
:
127 if (m_processor
->CECInitialised())
128 HandleGetCecVersion(command
);
130 case CEC_OPCODE_USER_CONTROL_PRESSED
:
131 if (m_processor
->CECInitialised())
132 HandleUserControlPressed(command
);
134 case CEC_OPCODE_USER_CONTROL_RELEASE
:
135 if (m_processor
->CECInitialised())
136 HandleUserControlRelease(command
);
138 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
139 if (m_processor
->CECInitialised())
140 HandleGiveAudioStatus(command
);
142 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
143 if (m_processor
->CECInitialised())
144 HandleGiveSystemAudioModeStatus(command
);
146 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
147 if (m_processor
->CECInitialised())
148 HandleSystemAudioModeRequest(command
);
150 case CEC_OPCODE_REPORT_AUDIO_STATUS
:
151 HandleReportAudioStatus(command
);
153 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
:
154 HandleSystemAudioModeStatus(command
);
156 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
:
157 HandleSetSystemAudioMode(command
);
159 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
160 if (m_processor
->CECInitialised())
161 HandleRequestActiveSource(command
);
163 case CEC_OPCODE_SET_STREAM_PATH
:
164 HandleSetStreamPath(command
);
166 case CEC_OPCODE_ROUTING_CHANGE
:
167 HandleRoutingChange(command
);
169 case CEC_OPCODE_ROUTING_INFORMATION
:
170 HandleRoutingInformation(command
);
172 case CEC_OPCODE_STANDBY
:
173 if (m_processor
->CECInitialised())
174 HandleStandby(command
);
176 case CEC_OPCODE_ACTIVE_SOURCE
:
177 HandleActiveSource(command
);
179 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
:
180 HandleReportPhysicalAddress(command
);
182 case CEC_OPCODE_SET_OSD_NAME
:
183 HandleSetOSDName(command
);
185 case CEC_OPCODE_IMAGE_VIEW_ON
:
186 HandleImageViewOn(command
);
188 case CEC_OPCODE_TEXT_VIEW_ON
:
189 HandleTextViewOn(command
);
191 case CEC_OPCODE_FEATURE_ABORT
:
192 HandleFeatureAbort(command
);
194 case CEC_OPCODE_VENDOR_COMMAND
:
195 HandleVendorCommand(command
);
203 m_waitForResponse
->Received((command
.opcode
== CEC_OPCODE_FEATURE_ABORT
&& command
.parameters
.size
> 0) ? (cec_opcode
)command
.parameters
[0] : command
.opcode
);
205 UnhandledCommand(command
);
210 bool CCECCommandHandler::HandleActiveSource(const cec_command
&command
)
212 if (command
.parameters
.size
== 2)
214 uint16_t iAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
215 CCECBusDevice
*device
= m_processor
->GetDeviceByPhysicalAddress(iAddress
);
217 device
->MarkAsActiveSource();
223 bool CCECCommandHandler::HandleDeckControl(const cec_command
&command
)
225 CCECPlaybackDevice
*device
= CCECBusDevice::AsPlaybackDevice(GetDevice(command
.destination
));
226 if (device
&& command
.parameters
.size
> 0)
228 device
->SetDeckControlMode((cec_deck_control_mode
) command
.parameters
[0]);
235 bool CCECCommandHandler::HandleDeviceCecVersion(const cec_command
&command
)
237 if (command
.parameters
.size
== 1)
239 CCECBusDevice
*device
= GetDevice(command
.initiator
);
241 device
->SetCecVersion((cec_version
) command
.parameters
[0]);
247 bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command
&command
)
249 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
250 m_processor
->TransmitAbort(command
.destination
, command
.initiator
, command
.opcode
, CEC_ABORT_REASON_REFUSED
);
255 bool CCECCommandHandler::HandleDeviceVendorId(const cec_command
&command
)
257 return SetVendorId(command
);
260 bool CCECCommandHandler::HandleFeatureAbort(const cec_command
&command
)
262 if (command
.parameters
.size
== 2 &&
263 (command
.parameters
[1] == CEC_ABORT_REASON_UNRECOGNIZED_OPCODE
||
264 command
.parameters
[1] == CEC_ABORT_REASON_REFUSED
))
265 m_processor
->GetDevice(command
.initiator
)->SetUnsupportedFeature((cec_opcode
)command
.parameters
[0]);
269 bool CCECCommandHandler::HandleGetCecVersion(const cec_command
&command
)
271 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
273 CCECBusDevice
*device
= GetDevice(command
.destination
);
275 return device
->TransmitCECVersion(command
.initiator
);
281 bool CCECCommandHandler::HandleGiveAudioStatus(const cec_command
&command
)
283 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
285 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.destination
));
287 return device
->TransmitAudioStatus(command
.initiator
);
293 bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
295 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
297 CCECPlaybackDevice
*device
= CCECBusDevice::AsPlaybackDevice(GetDevice(command
.destination
));
299 return device
->TransmitDeckStatus(command
.initiator
);
305 bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command
&command
)
307 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
309 CCECBusDevice
*device
= GetDevice(command
.destination
);
311 return device
->TransmitPowerState(command
.initiator
);
317 bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command
&command
)
319 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
321 CCECBusDevice
*device
= GetDevice(command
.destination
);
323 return device
->TransmitVendorID(command
.initiator
);
329 bool CCECCommandHandler::HandleGiveOSDName(const cec_command
&command
)
331 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
333 CCECBusDevice
*device
= GetDevice(command
.destination
);
335 return device
->TransmitOSDName(command
.initiator
);
341 bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command
&command
)
343 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
345 CCECBusDevice
*device
= GetDevice(command
.destination
);
347 return device
->TransmitPhysicalAddress();
353 bool CCECCommandHandler::HandleGiveMenuLanguage(const cec_command
&command
)
355 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
357 CCECBusDevice
*device
= GetDevice(command
.destination
);
359 return device
->TransmitSetMenuLanguage(command
.initiator
);
365 bool CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command
&command
)
367 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
369 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.destination
));
371 return device
->TransmitSystemAudioModeStatus(command
.initiator
);
377 bool CCECCommandHandler::HandleImageViewOn(const cec_command
&command
)
379 m_processor
->GetDevice(command
.initiator
)->MarkAsActiveSource();
383 bool CCECCommandHandler::HandleMenuRequest(const cec_command
&command
)
385 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
387 CCECBusDevice
*device
= GetDevice(command
.destination
);
390 CCECClient
*client
= device
->GetClient();
393 if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_ACTIVATE
)
395 if (client
->MenuStateChanged(CEC_MENU_STATE_ACTIVATED
) == 1)
396 device
->SetMenuState(CEC_MENU_STATE_ACTIVATED
);
398 else if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_DEACTIVATE
)
400 if (client
->MenuStateChanged(CEC_MENU_STATE_DEACTIVATED
) == 1)
401 device
->SetMenuState(CEC_MENU_STATE_DEACTIVATED
);
404 return device
->TransmitMenuState(command
.initiator
);
411 bool CCECCommandHandler::HandlePoll(const cec_command
&command
)
413 m_busDevice
->HandlePoll(command
.destination
);
417 bool CCECCommandHandler::HandleReportAudioStatus(const cec_command
&command
)
419 if (command
.parameters
.size
== 1)
421 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.initiator
));
424 device
->SetAudioStatus(command
.parameters
[0]);
431 bool CCECCommandHandler::HandleReportPhysicalAddress(const cec_command
&command
)
433 if (command
.parameters
.size
== 3)
435 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
436 SetPhysicalAddress(command
.initiator
, iNewAddress
);
441 bool CCECCommandHandler::HandleReportPowerStatus(const cec_command
&command
)
443 if (command
.parameters
.size
== 1)
445 CCECBusDevice
*device
= GetDevice(command
.initiator
);
447 device
->SetPowerStatus((cec_power_status
) command
.parameters
[0]);
452 bool CCECCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
454 if (m_processor
->CECInitialised())
456 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, ">> %i requests active source", (uint8_t) command
.initiator
);
457 m_processor
->GetDevice(command
.initiator
)->SetPowerStatus(CEC_POWER_STATUS_ON
);
459 vector
<CCECBusDevice
*> devices
;
460 for (size_t iDevicePtr
= 0; iDevicePtr
< GetMyDevices(devices
); iDevicePtr
++)
461 devices
[iDevicePtr
]->TransmitActiveSource();
468 bool CCECCommandHandler::HandleRoutingChange(const cec_command
&command
)
470 if (command
.parameters
.size
== 4)
472 uint16_t iOldAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
473 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[2] << 8) | ((uint16_t)command
.parameters
[3]);
475 CCECBusDevice
*device
= GetDevice(command
.initiator
);
477 device
->SetStreamPath(iNewAddress
, iOldAddress
);
482 bool CCECCommandHandler::HandleRoutingInformation(const cec_command
&command
)
484 if (command
.parameters
.size
== 2)
486 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
487 CCECBusDevice
*device
= m_processor
->GetDeviceByPhysicalAddress(iNewAddress
);
489 device
->MarkAsActiveSource();
495 bool CCECCommandHandler::HandleSetMenuLanguage(const cec_command
&command
)
497 if (command
.parameters
.size
== 3)
499 CCECBusDevice
*device
= GetDevice(command
.initiator
);
502 cec_menu_language language
;
503 language
.device
= command
.initiator
;
504 for (uint8_t iPtr
= 0; iPtr
< 4; iPtr
++)
505 language
.language
[iPtr
] = command
.parameters
[iPtr
];
506 language
.language
[3] = 0;
507 device
->SetMenuLanguage(language
);
514 bool CCECCommandHandler::HandleSetOSDName(const cec_command
&command
)
516 if (command
.parameters
.size
> 0)
518 CCECBusDevice
*device
= GetDevice(command
.initiator
);
522 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
523 buf
[iPtr
] = (char)command
.parameters
[iPtr
];
524 buf
[command
.parameters
.size
] = 0;
526 CStdString
strName(buf
);
527 device
->SetOSDName(strName
);
535 bool CCECCommandHandler::HandleSetStreamPath(const cec_command
&command
)
537 if (m_processor
->CECInitialised() && command
.parameters
.size
>= 2)
539 uint16_t iStreamAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
540 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, ">> %i sets stream path to physical address %04x", command
.initiator
, iStreamAddress
);
542 /* one of the device handled by libCEC has been made active */
543 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamAddress
);
544 if (device
&& device
->IsHandledByLibCEC())
545 device
->ActivateSource();
550 bool CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command
&command
)
552 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
554 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.destination
));
557 if (command
.parameters
.size
>= 2)
559 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
560 device
->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON
);
561 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
562 CCECBusDevice
*newActiveDevice
= GetDeviceByPhysicalAddress(iNewAddress
);
564 newActiveDevice
->MarkAsActiveSource();
565 return device
->TransmitSetSystemAudioMode(command
.initiator
);
569 device
->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF
);
570 return device
->TransmitSetSystemAudioMode(command
.initiator
);
577 bool CCECCommandHandler::HandleStandby(const cec_command
&command
)
579 CCECBusDevice
*device
= GetDevice(command
.initiator
);
581 device
->SetPowerStatus(CEC_POWER_STATUS_STANDBY
);
586 bool CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command
&command
)
588 if (command
.parameters
.size
== 1)
590 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.initiator
));
593 device
->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
601 bool CCECCommandHandler::HandleSetSystemAudioMode(const cec_command
&command
)
603 if (command
.parameters
.size
== 1)
605 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.initiator
));
608 device
->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
616 bool CCECCommandHandler::HandleTextViewOn(const cec_command
&command
)
618 m_processor
->GetDevice(command
.initiator
)->MarkAsActiveSource();
622 bool CCECCommandHandler::HandleUserControlPressed(const cec_command
&command
)
624 if (m_processor
->CECInitialised() &&
625 m_processor
->IsHandledByLibCEC(command
.destination
) &&
626 command
.parameters
.size
> 0)
628 CCECBusDevice
*device
= GetDevice(command
.destination
);
632 CCECClient
*client
= device
->GetClient();
636 if (command
.parameters
[0] <= CEC_USER_CONTROL_CODE_MAX
)
637 client
->SetCurrentButton((cec_user_control_code
) command
.parameters
[0]);
639 if (command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER
||
640 command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION
)
646 // CEC_USER_CONTROL_CODE_POWER operates as a toggle
647 // assume CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION does not
648 if (command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER
)
650 cec_power_status status
= device
->GetCurrentPowerStatus();
651 bPowerOn
= !(status
== CEC_POWER_STATUS_ON
|| status
== CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
656 device
->ActivateSource();
660 device
->MarkAsInactiveSource();
661 device
->TransmitInactiveSource();
662 device
->SetMenuState(CEC_MENU_STATE_DEACTIVATED
);
671 bool CCECCommandHandler::HandleUserControlRelease(const cec_command
&command
)
673 CCECClient
*client
= m_processor
->GetClient(command
.destination
);
679 bool CCECCommandHandler::HandleVendorCommand(const cec_command
& UNUSED(command
))
684 void CCECCommandHandler::UnhandledCommand(const cec_command
&command
)
686 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "unhandled command with opcode %02x from address %d", command
.opcode
, command
.initiator
);
688 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
689 m_processor
->TransmitAbort(m_busDevice
->GetLogicalAddress(), command
.initiator
, command
.opcode
, CEC_ABORT_REASON_UNRECOGNIZED_OPCODE
);
692 size_t CCECCommandHandler::GetMyDevices(vector
<CCECBusDevice
*> &devices
) const
696 cec_logical_addresses addresses
= m_processor
->GetLogicalAddresses();
697 for (uint8_t iPtr
= CECDEVICE_TV
; iPtr
< CECDEVICE_BROADCAST
; iPtr
++)
701 devices
.push_back(GetDevice((cec_logical_address
) iPtr
));
709 CCECBusDevice
*CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress
) const
711 return m_processor
->GetDevice(iLogicalAddress
);
714 CCECBusDevice
*CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
) const
716 return m_processor
->GetDeviceByPhysicalAddress(iPhysicalAddress
);
719 bool CCECCommandHandler::SetVendorId(const cec_command
&command
)
721 bool bChanged(false);
722 if (command
.parameters
.size
< 3)
724 LIB_CEC
->AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
728 uint64_t iVendorId
= ((uint64_t)command
.parameters
[0] << 16) +
729 ((uint64_t)command
.parameters
[1] << 8) +
730 (uint64_t)command
.parameters
[2];
732 CCECBusDevice
*device
= GetDevice((cec_logical_address
) command
.initiator
);
734 bChanged
= device
->SetVendorId(iVendorId
);
738 void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress
, uint16_t iNewAddress
)
740 if (!m_processor
->IsHandledByLibCEC(iAddress
))
742 CCECBusDevice
*otherDevice
= m_processor
->GetDeviceByPhysicalAddress(iNewAddress
);
743 CCECClient
*client
= otherDevice
? otherDevice
->GetClient() : NULL
;
745 CCECBusDevice
*device
= m_processor
->GetDevice(iAddress
);
747 device
->SetPhysicalAddress(iNewAddress
);
750 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "device with logical address %X not found", iAddress
);
753 /* another device reported the same physical address as ours */
755 client
->ResetPhysicalAddress();
759 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
);
763 bool CCECCommandHandler::PowerOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
765 if (iDestination
== CECDEVICE_TV
)
766 return TransmitImageViewOn(iInitiator
, iDestination
);
768 return TransmitKeypress(iInitiator
, iDestination
, CEC_USER_CONTROL_CODE_POWER
) &&
769 TransmitKeyRelease(iInitiator
, iDestination
);
772 bool CCECCommandHandler::TransmitImageViewOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
775 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_IMAGE_VIEW_ON
);
777 return Transmit(command
);
780 bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
783 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_STANDBY
);
785 return Transmit(command
);
788 bool CCECCommandHandler::TransmitRequestActiveSource(const cec_logical_address iInitiator
, bool bWaitForResponse
/* = true */)
791 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_REQUEST_ACTIVE_SOURCE
);
793 return Transmit(command
, !bWaitForResponse
);
796 bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
799 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_CEC_VERSION
);
801 return Transmit(command
, !bWaitForResponse
);
804 bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
807 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_MENU_LANGUAGE
);
809 return Transmit(command
, !bWaitForResponse
);
812 bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
815 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_OSD_NAME
);
817 return Transmit(command
, !bWaitForResponse
);
820 bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
823 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
);
825 return Transmit(command
, !bWaitForResponse
);
828 bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
831 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
);
833 return Transmit(command
, !bWaitForResponse
);
836 bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
839 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
841 return Transmit(command
, !bWaitForResponse
);
844 bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
847 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
848 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
849 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
851 return Transmit(command
);
854 bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_version cecVersion
)
857 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_CEC_VERSION
);
858 command
.parameters
.PushBack((uint8_t)cecVersion
);
860 return Transmit(command
);
863 bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
866 cec_command::Format(command
, iInitiator
, CECDEVICE_TV
, CEC_OPCODE_INACTIVE_SOURCE
);
867 command
.parameters
.PushBack((iPhysicalAddress
>> 8) & 0xFF);
868 command
.parameters
.PushBack(iPhysicalAddress
& 0xFF);
870 return Transmit(command
);
873 bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_menu_state menuState
)
876 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_MENU_STATUS
);
877 command
.parameters
.PushBack((uint8_t)menuState
);
879 return Transmit(command
);
882 bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, CStdString strDeviceName
)
885 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_NAME
);
886 for (size_t iPtr
= 0; iPtr
< strDeviceName
.length(); iPtr
++)
887 command
.parameters
.PushBack(strDeviceName
.at(iPtr
));
889 return Transmit(command
);
892 bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_display_control duration
, const char *strMessage
)
895 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_STRING
);
896 command
.parameters
.PushBack((uint8_t)duration
);
898 size_t iLen
= strlen(strMessage
);
899 if (iLen
> 13) iLen
= 13;
901 for (size_t iPtr
= 0; iPtr
< iLen
; iPtr
++)
902 command
.parameters
.PushBack(strMessage
[iPtr
]);
904 return Transmit(command
);
907 bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
, cec_device_type type
)
910 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
911 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
912 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
913 command
.parameters
.PushBack((uint8_t) (type
));
915 return Transmit(command
);
918 bool CCECCommandHandler::TransmitSetMenuLanguage(const cec_logical_address iInitiator
, const char lang
[3])
921 command
.Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_SET_MENU_LANGUAGE
);
922 command
.parameters
.PushBack((uint8_t) lang
[0]);
923 command
.parameters
.PushBack((uint8_t) lang
[1]);
924 command
.parameters
.PushBack((uint8_t) lang
[2]);
926 return Transmit(command
);
929 bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
932 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_NONE
);
934 return Transmit(command
);
937 bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_power_status state
)
940 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_POWER_STATUS
);
941 command
.parameters
.PushBack((uint8_t) state
);
943 return Transmit(command
);
946 bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator
, uint64_t iVendorId
)
949 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_DEVICE_VENDOR_ID
);
951 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 16) & 0xFF));
952 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 8) & 0xFF));
953 command
.parameters
.PushBack((uint8_t) ((uint64_t)iVendorId
& 0xFF));
955 return Transmit(command
);
958 bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, uint8_t state
)
961 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_AUDIO_STATUS
);
962 command
.parameters
.PushBack(state
);
964 return Transmit(command
);
967 bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
)
970 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
);
971 command
.parameters
.PushBack((uint8_t)state
);
973 return Transmit(command
);
976 bool CCECCommandHandler::TransmitSetStreamPath(uint16_t iStreamPath
)
979 cec_command::Format(command
, m_busDevice
->GetLogicalAddress(), CECDEVICE_BROADCAST
, CEC_OPCODE_SET_STREAM_PATH
);
980 command
.parameters
.PushBack((uint8_t) ((iStreamPath
>> 8) & 0xFF));
981 command
.parameters
.PushBack((uint8_t) (iStreamPath
& 0xFF));
983 return Transmit(command
);
986 bool CCECCommandHandler::TransmitSystemAudioModeStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
)
989 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
);
990 command
.parameters
.PushBack((uint8_t)state
);
992 return Transmit(command
);
995 bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_deck_info state
)
998 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_DECK_STATUS
);
999 command
.PushBack((uint8_t)state
);
1001 return Transmit(command
);
1004 bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = true */)
1006 cec_command command
;
1007 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_PRESSED
);
1008 command
.parameters
.PushBack((uint8_t)key
);
1010 return Transmit(command
, !bWait
);
1013 bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWait
/* = true */)
1015 cec_command command
;
1016 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_RELEASE
);
1018 return Transmit(command
, !bWait
);
1021 bool CCECCommandHandler::Transmit(cec_command
&command
, bool bSuppressWait
/* = false */)
1023 bool bReturn(false);
1024 cec_opcode
expectedResponse(cec_command::GetResponseOpcode(command
.opcode
));
1025 bool bExpectResponse(expectedResponse
!= CEC_OPCODE_NONE
&& !bSuppressWait
);
1026 command
.transmit_timeout
= m_iTransmitTimeout
;
1028 if (command
.initiator
== CECDEVICE_UNKNOWN
)
1030 LIB_CEC
->AddLog(CEC_LOG_ERROR
, "not transmitting a command without a valid initiator");
1035 uint8_t iTries(0), iMaxTries(!command
.opcode_set
? 1 : m_iTransmitRetries
+ 1);
1036 while (!bReturn
&& ++iTries
<= iMaxTries
&& !m_busDevice
->IsUnsupportedFeature(command
.opcode
))
1038 if ((bReturn
= m_processor
->Transmit(command
)) == true)
1040 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "command transmitted");
1041 if (bExpectResponse
)
1043 bReturn
= m_waitForResponse
->Wait(expectedResponse
);
1044 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, bReturn
? "expected response received (%X: %s)" : "expected response not received (%X: %s)", (int)expectedResponse
, ToString(expectedResponse
));
1053 bool CCECCommandHandler::ActivateSource(void)
1055 if (m_busDevice
->IsActiveSource() &&
1056 m_busDevice
->IsHandledByLibCEC())
1058 m_busDevice
->SetPowerStatus(CEC_POWER_STATUS_ON
);
1059 m_busDevice
->SetMenuState(CEC_MENU_STATE_ACTIVATED
);
1061 m_busDevice
->TransmitImageViewOn();
1062 m_busDevice
->TransmitActiveSource();
1063 m_busDevice
->TransmitMenuState(CECDEVICE_TV
);
1065 CCECPlaybackDevice
*playbackDevice
= m_busDevice
->AsPlaybackDevice();
1066 if (playbackDevice
&& SendDeckStatusUpdateOnActiveSource())
1067 playbackDevice
->TransmitDeckStatus(CECDEVICE_TV
);
1068 m_bHandlerInited
= true;
1073 void CCECCommandHandler::SignalOpcode(cec_opcode opcode
)
1075 m_waitForResponse
->Received(opcode
);