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
);
205 m_waitForResponse
->Received((command
.opcode
== CEC_OPCODE_FEATURE_ABORT
&& command
.parameters
.size
> 0) ? (cec_opcode
)command
.parameters
[0] : command
.opcode
);
207 UnhandledCommand(command
);
212 bool CCECCommandHandler::HandleActiveSource(const cec_command
&command
)
214 if (command
.parameters
.size
== 2)
216 uint16_t iAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
217 CCECBusDevice
*device
= m_processor
->GetDeviceByPhysicalAddress(iAddress
);
219 device
->MarkAsActiveSource();
225 bool CCECCommandHandler::HandleDeckControl(const cec_command
&command
)
227 CCECPlaybackDevice
*device
= CCECBusDevice::AsPlaybackDevice(GetDevice(command
.destination
));
228 if (device
&& command
.parameters
.size
> 0)
230 device
->SetDeckControlMode((cec_deck_control_mode
) command
.parameters
[0]);
237 bool CCECCommandHandler::HandleDeviceCecVersion(const cec_command
&command
)
239 if (command
.parameters
.size
== 1)
241 CCECBusDevice
*device
= GetDevice(command
.initiator
);
243 device
->SetCecVersion((cec_version
) command
.parameters
[0]);
249 bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command
&command
)
251 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
252 m_processor
->TransmitAbort(command
.destination
, command
.initiator
, command
.opcode
, CEC_ABORT_REASON_REFUSED
);
257 bool CCECCommandHandler::HandleDeviceVendorId(const cec_command
&command
)
259 return SetVendorId(command
);
262 bool CCECCommandHandler::HandleFeatureAbort(const cec_command
&command
)
264 if (command
.parameters
.size
== 2 &&
265 (command
.parameters
[1] == CEC_ABORT_REASON_UNRECOGNIZED_OPCODE
||
266 command
.parameters
[1] == CEC_ABORT_REASON_REFUSED
))
267 m_processor
->GetDevice(command
.initiator
)->SetUnsupportedFeature((cec_opcode
)command
.parameters
[0]);
271 bool CCECCommandHandler::HandleGetCecVersion(const cec_command
&command
)
273 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
275 CCECBusDevice
*device
= GetDevice(command
.destination
);
277 return device
->TransmitCECVersion(command
.initiator
);
283 bool CCECCommandHandler::HandleGiveAudioStatus(const cec_command
&command
)
285 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
287 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.destination
));
289 return device
->TransmitAudioStatus(command
.initiator
);
295 bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
297 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
299 CCECPlaybackDevice
*device
= CCECBusDevice::AsPlaybackDevice(GetDevice(command
.destination
));
301 return device
->TransmitDeckStatus(command
.initiator
);
307 bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command
&command
)
309 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
311 CCECBusDevice
*device
= GetDevice(command
.destination
);
313 return device
->TransmitPowerState(command
.initiator
);
319 bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command
&command
)
321 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
323 CCECBusDevice
*device
= GetDevice(command
.destination
);
325 return device
->TransmitVendorID(command
.initiator
);
331 bool CCECCommandHandler::HandleGiveOSDName(const cec_command
&command
)
333 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
335 CCECBusDevice
*device
= GetDevice(command
.destination
);
337 return device
->TransmitOSDName(command
.initiator
);
343 bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command
&command
)
345 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
347 CCECBusDevice
*device
= GetDevice(command
.destination
);
349 return device
->TransmitPhysicalAddress();
355 bool CCECCommandHandler::HandleGiveMenuLanguage(const cec_command
&command
)
357 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
359 CCECBusDevice
*device
= GetDevice(command
.destination
);
361 return device
->TransmitSetMenuLanguage(command
.initiator
);
367 bool CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command
&command
)
369 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
371 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.destination
));
373 return device
->TransmitSystemAudioModeStatus(command
.initiator
);
379 bool CCECCommandHandler::HandleImageViewOn(const cec_command
&command
)
381 m_processor
->GetDevice(command
.initiator
)->MarkAsActiveSource();
385 bool CCECCommandHandler::HandleMenuRequest(const cec_command
&command
)
387 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
389 CCECBusDevice
*device
= GetDevice(command
.destination
);
392 CCECClient
*client
= device
->GetClient();
395 if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_ACTIVATE
)
397 if (client
->MenuStateChanged(CEC_MENU_STATE_ACTIVATED
) == 1)
398 device
->SetMenuState(CEC_MENU_STATE_ACTIVATED
);
400 else if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_DEACTIVATE
)
402 if (client
->MenuStateChanged(CEC_MENU_STATE_DEACTIVATED
) == 1)
403 device
->SetMenuState(CEC_MENU_STATE_DEACTIVATED
);
406 return device
->TransmitMenuState(command
.initiator
);
413 bool CCECCommandHandler::HandlePoll(const cec_command
&command
)
415 m_busDevice
->HandlePoll(command
.destination
);
419 bool CCECCommandHandler::HandleReportAudioStatus(const cec_command
&command
)
421 if (command
.parameters
.size
== 1)
423 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.initiator
));
426 device
->SetAudioStatus(command
.parameters
[0]);
433 bool CCECCommandHandler::HandleReportPhysicalAddress(const cec_command
&command
)
435 if (command
.parameters
.size
== 3)
437 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
438 SetPhysicalAddress(command
.initiator
, iNewAddress
);
443 bool CCECCommandHandler::HandleReportPowerStatus(const cec_command
&command
)
445 if (command
.parameters
.size
== 1)
447 CCECBusDevice
*device
= GetDevice(command
.initiator
);
449 device
->SetPowerStatus((cec_power_status
) command
.parameters
[0]);
454 bool CCECCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
456 if (m_processor
->CECInitialised())
458 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, ">> %i requests active source", (uint8_t) command
.initiator
);
459 m_processor
->GetDevice(command
.initiator
)->SetPowerStatus(CEC_POWER_STATUS_ON
);
461 vector
<CCECBusDevice
*> devices
;
462 for (size_t iDevicePtr
= 0; iDevicePtr
< GetMyDevices(devices
); iDevicePtr
++)
463 devices
[iDevicePtr
]->TransmitActiveSource();
470 bool CCECCommandHandler::HandleRoutingChange(const cec_command
&command
)
472 if (command
.parameters
.size
== 4)
474 uint16_t iOldAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
475 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[2] << 8) | ((uint16_t)command
.parameters
[3]);
477 CCECBusDevice
*device
= GetDevice(command
.initiator
);
479 device
->SetStreamPath(iNewAddress
, iOldAddress
);
484 bool CCECCommandHandler::HandleRoutingInformation(const cec_command
&command
)
486 if (command
.parameters
.size
== 2)
488 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
489 CCECBusDevice
*device
= m_processor
->GetDeviceByPhysicalAddress(iNewAddress
);
491 device
->MarkAsActiveSource();
497 bool CCECCommandHandler::HandleSetMenuLanguage(const cec_command
&command
)
499 if (command
.parameters
.size
== 3)
501 CCECBusDevice
*device
= GetDevice(command
.initiator
);
504 cec_menu_language language
;
505 language
.device
= command
.initiator
;
506 for (uint8_t iPtr
= 0; iPtr
< 4; iPtr
++)
507 language
.language
[iPtr
] = command
.parameters
[iPtr
];
508 language
.language
[3] = 0;
509 device
->SetMenuLanguage(language
);
516 bool CCECCommandHandler::HandleSetOSDName(const cec_command
&command
)
518 if (command
.parameters
.size
> 0)
520 CCECBusDevice
*device
= GetDevice(command
.initiator
);
524 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
525 buf
[iPtr
] = (char)command
.parameters
[iPtr
];
526 buf
[command
.parameters
.size
] = 0;
528 CStdString
strName(buf
);
529 device
->SetOSDName(strName
);
537 bool CCECCommandHandler::HandleSetStreamPath(const cec_command
&command
)
539 if (m_processor
->CECInitialised() && command
.parameters
.size
>= 2)
541 uint16_t iStreamAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
542 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, ">> %i sets stream path to physical address %04x", command
.initiator
, iStreamAddress
);
544 /* one of the device handled by libCEC has been made active */
545 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamAddress
);
546 if (device
&& device
->IsHandledByLibCEC())
547 device
->ActivateSource();
552 bool CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command
&command
)
554 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
556 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.destination
));
559 if (command
.parameters
.size
>= 2)
561 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
562 device
->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON
);
563 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
564 CCECBusDevice
*newActiveDevice
= GetDeviceByPhysicalAddress(iNewAddress
);
566 newActiveDevice
->MarkAsActiveSource();
567 return device
->TransmitSetSystemAudioMode(command
.initiator
);
571 device
->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF
);
572 return device
->TransmitSetSystemAudioMode(command
.initiator
);
579 bool CCECCommandHandler::HandleStandby(const cec_command
&command
)
581 CCECBusDevice
*device
= GetDevice(command
.initiator
);
583 device
->SetPowerStatus(CEC_POWER_STATUS_STANDBY
);
588 bool CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command
&command
)
590 if (command
.parameters
.size
== 1)
592 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.initiator
));
595 device
->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
603 bool CCECCommandHandler::HandleSetSystemAudioMode(const cec_command
&command
)
605 if (command
.parameters
.size
== 1)
607 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.initiator
));
610 device
->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
618 bool CCECCommandHandler::HandleTextViewOn(const cec_command
&command
)
620 m_processor
->GetDevice(command
.initiator
)->MarkAsActiveSource();
624 bool CCECCommandHandler::HandleUserControlPressed(const cec_command
&command
)
626 if (m_processor
->CECInitialised() &&
627 m_processor
->IsHandledByLibCEC(command
.destination
) &&
628 command
.parameters
.size
> 0)
630 CCECBusDevice
*device
= GetDevice(command
.destination
);
634 CCECClient
*client
= device
->GetClient();
638 if (command
.parameters
[0] <= CEC_USER_CONTROL_CODE_MAX
)
639 client
->SetCurrentButton((cec_user_control_code
) command
.parameters
[0]);
641 if (command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER
||
642 command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION
)
648 // CEC_USER_CONTROL_CODE_POWER operates as a toggle
649 // assume CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION does not
650 if (command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER
)
652 cec_power_status status
= device
->GetCurrentPowerStatus();
653 bPowerOn
= !(status
== CEC_POWER_STATUS_ON
|| status
== CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
658 device
->ActivateSource();
662 device
->MarkAsInactiveSource();
663 device
->TransmitInactiveSource();
664 device
->SetMenuState(CEC_MENU_STATE_DEACTIVATED
);
673 bool CCECCommandHandler::HandleUserControlRelease(const cec_command
&command
)
675 CCECClient
*client
= m_processor
->GetClient(command
.destination
);
681 bool CCECCommandHandler::HandleVendorCommand(const cec_command
& UNUSED(command
))
686 void CCECCommandHandler::UnhandledCommand(const cec_command
&command
)
688 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "unhandled command with opcode %02x from address %d", command
.opcode
, command
.initiator
);
690 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
691 m_processor
->TransmitAbort(m_busDevice
->GetLogicalAddress(), command
.initiator
, command
.opcode
, CEC_ABORT_REASON_UNRECOGNIZED_OPCODE
);
694 size_t CCECCommandHandler::GetMyDevices(vector
<CCECBusDevice
*> &devices
) const
698 cec_logical_addresses addresses
= m_processor
->GetLogicalAddresses();
699 for (uint8_t iPtr
= CECDEVICE_TV
; iPtr
< CECDEVICE_BROADCAST
; iPtr
++)
703 devices
.push_back(GetDevice((cec_logical_address
) iPtr
));
711 CCECBusDevice
*CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress
) const
713 return m_processor
->GetDevice(iLogicalAddress
);
716 CCECBusDevice
*CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
) const
718 return m_processor
->GetDeviceByPhysicalAddress(iPhysicalAddress
);
721 bool CCECCommandHandler::SetVendorId(const cec_command
&command
)
723 bool bChanged(false);
724 if (command
.parameters
.size
< 3)
726 LIB_CEC
->AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
730 uint64_t iVendorId
= ((uint64_t)command
.parameters
[0] << 16) +
731 ((uint64_t)command
.parameters
[1] << 8) +
732 (uint64_t)command
.parameters
[2];
734 CCECBusDevice
*device
= GetDevice((cec_logical_address
) command
.initiator
);
736 bChanged
= device
->SetVendorId(iVendorId
);
740 void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress
, uint16_t iNewAddress
)
742 if (!m_processor
->IsHandledByLibCEC(iAddress
))
744 CCECBusDevice
*otherDevice
= m_processor
->GetDeviceByPhysicalAddress(iNewAddress
);
745 CCECClient
*client
= otherDevice
? otherDevice
->GetClient() : NULL
;
747 CCECBusDevice
*device
= m_processor
->GetDevice(iAddress
);
749 device
->SetPhysicalAddress(iNewAddress
);
752 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "device with logical address %X not found", iAddress
);
755 /* another device reported the same physical address as ours */
757 client
->ResetPhysicalAddress();
761 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
);
765 bool CCECCommandHandler::PowerOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
767 if (iDestination
== CECDEVICE_TV
)
768 return TransmitImageViewOn(iInitiator
, iDestination
);
770 return TransmitKeypress(iInitiator
, iDestination
, CEC_USER_CONTROL_CODE_POWER
) &&
771 TransmitKeyRelease(iInitiator
, iDestination
);
774 bool CCECCommandHandler::TransmitImageViewOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
777 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_IMAGE_VIEW_ON
);
779 return Transmit(command
);
782 bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
785 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_STANDBY
);
787 return Transmit(command
);
790 bool CCECCommandHandler::TransmitRequestActiveSource(const cec_logical_address iInitiator
, bool bWaitForResponse
/* = true */)
793 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_REQUEST_ACTIVE_SOURCE
);
795 return Transmit(command
, !bWaitForResponse
);
798 bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
801 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_CEC_VERSION
);
803 return Transmit(command
, !bWaitForResponse
);
806 bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
809 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_MENU_LANGUAGE
);
811 return Transmit(command
, !bWaitForResponse
);
814 bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
817 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_OSD_NAME
);
819 return Transmit(command
, !bWaitForResponse
);
822 bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
825 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
);
827 return Transmit(command
, !bWaitForResponse
);
830 bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
833 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
);
835 return Transmit(command
, !bWaitForResponse
);
838 bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
841 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
843 return Transmit(command
, !bWaitForResponse
);
846 bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
849 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
850 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
851 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
853 return Transmit(command
);
856 bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_version cecVersion
)
859 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_CEC_VERSION
);
860 command
.parameters
.PushBack((uint8_t)cecVersion
);
862 return Transmit(command
);
865 bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
868 cec_command::Format(command
, iInitiator
, CECDEVICE_TV
, CEC_OPCODE_INACTIVE_SOURCE
);
869 command
.parameters
.PushBack((iPhysicalAddress
>> 8) & 0xFF);
870 command
.parameters
.PushBack(iPhysicalAddress
& 0xFF);
872 return Transmit(command
);
875 bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_menu_state menuState
)
878 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_MENU_STATUS
);
879 command
.parameters
.PushBack((uint8_t)menuState
);
881 return Transmit(command
);
884 bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, CStdString strDeviceName
)
887 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_NAME
);
888 for (size_t iPtr
= 0; iPtr
< strDeviceName
.length(); iPtr
++)
889 command
.parameters
.PushBack(strDeviceName
.at(iPtr
));
891 return Transmit(command
);
894 bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_display_control duration
, const char *strMessage
)
897 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_STRING
);
898 command
.parameters
.PushBack((uint8_t)duration
);
900 size_t iLen
= strlen(strMessage
);
901 if (iLen
> 13) iLen
= 13;
903 for (size_t iPtr
= 0; iPtr
< iLen
; iPtr
++)
904 command
.parameters
.PushBack(strMessage
[iPtr
]);
906 return Transmit(command
);
909 bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
, cec_device_type type
)
912 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
913 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
914 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
915 command
.parameters
.PushBack((uint8_t) (type
));
917 return Transmit(command
);
920 bool CCECCommandHandler::TransmitSetMenuLanguage(const cec_logical_address iInitiator
, const char lang
[3])
923 command
.Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_SET_MENU_LANGUAGE
);
924 command
.parameters
.PushBack((uint8_t) lang
[0]);
925 command
.parameters
.PushBack((uint8_t) lang
[1]);
926 command
.parameters
.PushBack((uint8_t) lang
[2]);
928 return Transmit(command
);
931 bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
934 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_NONE
);
936 return Transmit(command
);
939 bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_power_status state
)
942 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_POWER_STATUS
);
943 command
.parameters
.PushBack((uint8_t) state
);
945 return Transmit(command
);
948 bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator
, uint64_t iVendorId
)
951 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_DEVICE_VENDOR_ID
);
953 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 16) & 0xFF));
954 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 8) & 0xFF));
955 command
.parameters
.PushBack((uint8_t) ((uint64_t)iVendorId
& 0xFF));
957 return Transmit(command
);
960 bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, uint8_t state
)
963 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_AUDIO_STATUS
);
964 command
.parameters
.PushBack(state
);
966 return Transmit(command
);
969 bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
)
972 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
);
973 command
.parameters
.PushBack((uint8_t)state
);
975 return Transmit(command
);
978 bool CCECCommandHandler::TransmitSetStreamPath(uint16_t iStreamPath
)
981 cec_command::Format(command
, m_busDevice
->GetLogicalAddress(), CECDEVICE_BROADCAST
, CEC_OPCODE_SET_STREAM_PATH
);
982 command
.parameters
.PushBack((uint8_t) ((iStreamPath
>> 8) & 0xFF));
983 command
.parameters
.PushBack((uint8_t) (iStreamPath
& 0xFF));
985 return Transmit(command
);
988 bool CCECCommandHandler::TransmitSystemAudioModeStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
)
991 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
);
992 command
.parameters
.PushBack((uint8_t)state
);
994 return Transmit(command
);
997 bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_deck_info state
)
1000 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_DECK_STATUS
);
1001 command
.PushBack((uint8_t)state
);
1003 return Transmit(command
);
1006 bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = true */)
1008 cec_command command
;
1009 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_PRESSED
);
1010 command
.parameters
.PushBack((uint8_t)key
);
1012 return Transmit(command
, !bWait
);
1015 bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWait
/* = true */)
1017 cec_command command
;
1018 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_RELEASE
);
1020 return Transmit(command
, !bWait
);
1023 bool CCECCommandHandler::Transmit(cec_command
&command
, bool bSuppressWait
/* = false */)
1025 bool bReturn(false);
1026 cec_opcode
expectedResponse(cec_command::GetResponseOpcode(command
.opcode
));
1027 bool bExpectResponse(expectedResponse
!= CEC_OPCODE_NONE
&& !bSuppressWait
);
1028 command
.transmit_timeout
= m_iTransmitTimeout
;
1030 if (command
.initiator
== CECDEVICE_UNKNOWN
)
1032 LIB_CEC
->AddLog(CEC_LOG_ERROR
, "not transmitting a command without a valid initiator");
1037 uint8_t iTries(0), iMaxTries(!command
.opcode_set
? 1 : m_iTransmitRetries
+ 1);
1038 while (!bReturn
&& ++iTries
<= iMaxTries
&& !m_busDevice
->IsUnsupportedFeature(command
.opcode
))
1040 if ((bReturn
= m_processor
->Transmit(command
)) == true)
1042 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "command transmitted");
1043 if (bExpectResponse
)
1045 bReturn
= m_waitForResponse
->Wait(expectedResponse
);
1046 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, bReturn
? "expected response received (%X: %s)" : "expected response not received (%X: %s)", (int)expectedResponse
, ToString(expectedResponse
));
1055 bool CCECCommandHandler::ActivateSource(void)
1057 if (m_busDevice
->IsActiveSource() &&
1058 m_busDevice
->IsHandledByLibCEC())
1061 CLockObject
lock(m_mutex
);
1062 m_bActiveSourcePending
= false;
1065 m_busDevice
->SetPowerStatus(CEC_POWER_STATUS_ON
);
1066 m_busDevice
->SetMenuState(CEC_MENU_STATE_ACTIVATED
);
1068 if (!m_busDevice
->TransmitImageViewOn() ||
1069 !m_busDevice
->TransmitActiveSource() ||
1070 !m_busDevice
->TransmitMenuState(CECDEVICE_TV
))
1072 CLockObject
lock(m_mutex
);
1073 m_bActiveSourcePending
= true;
1077 CCECPlaybackDevice
*playbackDevice
= m_busDevice
->AsPlaybackDevice();
1078 if (playbackDevice
&& SendDeckStatusUpdateOnActiveSource())
1079 playbackDevice
->TransmitDeckStatus(CECDEVICE_TV
);
1081 m_bHandlerInited
= true;
1086 void CCECCommandHandler::SignalOpcode(cec_opcode opcode
)
1088 m_waitForResponse
->Received(opcode
);
1091 bool CCECCommandHandler::ActiveSourcePending(void)
1093 CLockObject
lock(m_mutex
);
1094 return m_bActiveSourcePending
;