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"
43 using namespace PLATFORM
;
45 #define LIB_CEC m_busDevice->GetProcessor()->GetLib()
46 #define ToString(p) LIB_CEC->ToString(p)
48 CCECCommandHandler::CCECCommandHandler(CCECBusDevice
*busDevice
) :
49 m_busDevice(busDevice
),
50 m_processor(m_busDevice
->GetProcessor()),
51 m_iTransmitTimeout(CEC_DEFAULT_TRANSMIT_TIMEOUT
),
52 m_iTransmitWait(CEC_DEFAULT_TRANSMIT_WAIT
),
53 m_iTransmitRetries(CEC_DEFAULT_TRANSMIT_RETRIES
),
54 m_bHandlerInited(false),
55 m_bOPTSendDeckStatusUpdateOnActiveSource(false),
56 m_vendorId(CEC_VENDOR_UNKNOWN
),
57 m_waitForResponse(new CWaitForResponse
)
61 CCECCommandHandler::~CCECCommandHandler(void)
63 delete m_waitForResponse
;
66 bool CCECCommandHandler::HandleCommand(const cec_command
&command
)
68 if (command
.opcode_set
== 0)
69 return HandlePoll(command
);
73 CCECClient
*client
= m_busDevice
->GetClient();
75 client
->AddCommand(command
);
77 switch(command
.opcode
)
79 case CEC_OPCODE_REPORT_POWER_STATUS
:
80 HandleReportPowerStatus(command
);
82 case CEC_OPCODE_CEC_VERSION
:
83 HandleDeviceCecVersion(command
);
85 case CEC_OPCODE_SET_MENU_LANGUAGE
:
86 HandleSetMenuLanguage(command
);
88 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
89 if (m_processor
->CECInitialised())
90 HandleGivePhysicalAddress(command
);
92 case CEC_OPCODE_GET_MENU_LANGUAGE
:
93 if (m_processor
->CECInitialised())
94 HandleGiveMenuLanguage(command
);
96 case CEC_OPCODE_GIVE_OSD_NAME
:
97 if (m_processor
->CECInitialised())
98 HandleGiveOSDName(command
);
100 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
101 if (m_processor
->CECInitialised())
102 HandleGiveDeviceVendorId(command
);
104 case CEC_OPCODE_DEVICE_VENDOR_ID
:
105 HandleDeviceVendorId(command
);
107 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
108 HandleDeviceVendorCommandWithId(command
);
110 case CEC_OPCODE_GIVE_DECK_STATUS
:
111 if (m_processor
->CECInitialised())
112 HandleGiveDeckStatus(command
);
114 case CEC_OPCODE_DECK_CONTROL
:
115 HandleDeckControl(command
);
117 case CEC_OPCODE_MENU_REQUEST
:
118 if (m_processor
->CECInitialised())
119 HandleMenuRequest(command
);
121 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
122 if (m_processor
->CECInitialised())
123 HandleGiveDevicePowerStatus(command
);
125 case CEC_OPCODE_GET_CEC_VERSION
:
126 if (m_processor
->CECInitialised())
127 HandleGetCecVersion(command
);
129 case CEC_OPCODE_USER_CONTROL_PRESSED
:
130 if (m_processor
->CECInitialised())
131 HandleUserControlPressed(command
);
133 case CEC_OPCODE_USER_CONTROL_RELEASE
:
134 if (m_processor
->CECInitialised())
135 HandleUserControlRelease(command
);
137 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
138 if (m_processor
->CECInitialised())
139 HandleGiveAudioStatus(command
);
141 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
142 if (m_processor
->CECInitialised())
143 HandleGiveSystemAudioModeStatus(command
);
145 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
146 if (m_processor
->CECInitialised())
147 HandleSystemAudioModeRequest(command
);
149 case CEC_OPCODE_REPORT_AUDIO_STATUS
:
150 HandleReportAudioStatus(command
);
152 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
:
153 HandleSystemAudioModeStatus(command
);
155 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
:
156 HandleSetSystemAudioMode(command
);
158 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
159 if (m_processor
->CECInitialised())
160 HandleRequestActiveSource(command
);
162 case CEC_OPCODE_SET_STREAM_PATH
:
163 HandleSetStreamPath(command
);
165 case CEC_OPCODE_ROUTING_CHANGE
:
166 HandleRoutingChange(command
);
168 case CEC_OPCODE_ROUTING_INFORMATION
:
169 HandleRoutingInformation(command
);
171 case CEC_OPCODE_STANDBY
:
172 if (m_processor
->CECInitialised())
173 HandleStandby(command
);
175 case CEC_OPCODE_ACTIVE_SOURCE
:
176 HandleActiveSource(command
);
178 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
:
179 HandleReportPhysicalAddress(command
);
181 case CEC_OPCODE_SET_OSD_NAME
:
182 HandleSetOSDName(command
);
184 case CEC_OPCODE_IMAGE_VIEW_ON
:
185 HandleImageViewOn(command
);
187 case CEC_OPCODE_TEXT_VIEW_ON
:
188 HandleTextViewOn(command
);
190 case CEC_OPCODE_FEATURE_ABORT
:
191 HandleFeatureAbort(command
);
193 case CEC_OPCODE_VENDOR_COMMAND
:
194 HandleVendorCommand(command
);
202 m_waitForResponse
->Received((command
.opcode
== CEC_OPCODE_FEATURE_ABORT
&& command
.parameters
.size
> 0) ? (cec_opcode
)command
.parameters
[0] : command
.opcode
);
204 UnhandledCommand(command
);
209 bool CCECCommandHandler::HandleActiveSource(const cec_command
&command
)
211 if (command
.parameters
.size
== 2)
213 uint16_t iAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
214 CCECBusDevice
*device
= m_processor
->GetDeviceByPhysicalAddress(iAddress
);
216 device
->MarkAsActiveSource();
222 bool CCECCommandHandler::HandleDeckControl(const cec_command
&command
)
224 CCECPlaybackDevice
*device
= CCECBusDevice::AsPlaybackDevice(GetDevice(command
.destination
));
225 if (device
&& command
.parameters
.size
> 0)
227 device
->SetDeckControlMode((cec_deck_control_mode
) command
.parameters
[0]);
234 bool CCECCommandHandler::HandleDeviceCecVersion(const cec_command
&command
)
236 if (command
.parameters
.size
== 1)
238 CCECBusDevice
*device
= GetDevice(command
.initiator
);
240 device
->SetCecVersion((cec_version
) command
.parameters
[0]);
246 bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command
&command
)
248 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
249 m_processor
->TransmitAbort(command
.destination
, command
.initiator
, command
.opcode
, CEC_ABORT_REASON_REFUSED
);
254 bool CCECCommandHandler::HandleDeviceVendorId(const cec_command
&command
)
256 return SetVendorId(command
);
259 bool CCECCommandHandler::HandleFeatureAbort(const cec_command
&command
)
261 if (command
.parameters
.size
== 2 &&
262 (command
.parameters
[1] == CEC_ABORT_REASON_UNRECOGNIZED_OPCODE
||
263 command
.parameters
[1] == CEC_ABORT_REASON_REFUSED
))
264 m_processor
->GetDevice(command
.initiator
)->SetUnsupportedFeature((cec_opcode
)command
.parameters
[0]);
268 bool CCECCommandHandler::HandleGetCecVersion(const cec_command
&command
)
270 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
272 CCECBusDevice
*device
= GetDevice(command
.destination
);
274 return device
->TransmitCECVersion(command
.initiator
);
280 bool CCECCommandHandler::HandleGiveAudioStatus(const cec_command
&command
)
282 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
284 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.destination
));
286 return device
->TransmitAudioStatus(command
.initiator
);
292 bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
294 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
296 CCECPlaybackDevice
*device
= CCECBusDevice::AsPlaybackDevice(GetDevice(command
.destination
));
298 return device
->TransmitDeckStatus(command
.initiator
);
304 bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command
&command
)
306 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
308 CCECBusDevice
*device
= GetDevice(command
.destination
);
310 return device
->TransmitPowerState(command
.initiator
);
316 bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command
&command
)
318 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
320 CCECBusDevice
*device
= GetDevice(command
.destination
);
322 return device
->TransmitVendorID(command
.initiator
);
328 bool CCECCommandHandler::HandleGiveOSDName(const cec_command
&command
)
330 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
332 CCECBusDevice
*device
= GetDevice(command
.destination
);
334 return device
->TransmitOSDName(command
.initiator
);
340 bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command
&command
)
342 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
344 CCECBusDevice
*device
= GetDevice(command
.destination
);
346 return device
->TransmitPhysicalAddress();
352 bool CCECCommandHandler::HandleGiveMenuLanguage(const cec_command
&command
)
354 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
356 CCECBusDevice
*device
= GetDevice(command
.destination
);
358 return device
->TransmitSetMenuLanguage(command
.initiator
);
364 bool CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command
&command
)
366 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
368 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.destination
));
370 return device
->TransmitSystemAudioModeStatus(command
.initiator
);
376 bool CCECCommandHandler::HandleImageViewOn(const cec_command
&command
)
378 m_processor
->GetDevice(command
.initiator
)->MarkAsActiveSource();
382 bool CCECCommandHandler::HandleMenuRequest(const cec_command
&command
)
384 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
386 CCECBusDevice
*device
= GetDevice(command
.destination
);
389 CCECClient
*client
= device
->GetClient();
392 if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_ACTIVATE
)
394 if (client
->MenuStateChanged(CEC_MENU_STATE_ACTIVATED
) == 1)
395 device
->SetMenuState(CEC_MENU_STATE_ACTIVATED
);
397 else if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_DEACTIVATE
)
399 if (client
->MenuStateChanged(CEC_MENU_STATE_DEACTIVATED
) == 1)
400 device
->SetMenuState(CEC_MENU_STATE_DEACTIVATED
);
403 return device
->TransmitMenuState(command
.initiator
);
410 bool CCECCommandHandler::HandlePoll(const cec_command
&command
)
412 m_busDevice
->HandlePoll(command
.destination
);
416 bool CCECCommandHandler::HandleReportAudioStatus(const cec_command
&command
)
418 if (command
.parameters
.size
== 1)
420 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.initiator
));
423 device
->SetAudioStatus(command
.parameters
[0]);
430 bool CCECCommandHandler::HandleReportPhysicalAddress(const cec_command
&command
)
432 if (command
.parameters
.size
== 3)
434 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
435 SetPhysicalAddress(command
.initiator
, iNewAddress
);
440 bool CCECCommandHandler::HandleReportPowerStatus(const cec_command
&command
)
442 if (command
.parameters
.size
== 1)
444 CCECBusDevice
*device
= GetDevice(command
.initiator
);
446 device
->SetPowerStatus((cec_power_status
) command
.parameters
[0]);
451 bool CCECCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
453 if (m_processor
->CECInitialised())
455 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, ">> %i requests active source", (uint8_t) command
.initiator
);
456 m_processor
->GetDevice(command
.initiator
)->SetPowerStatus(CEC_POWER_STATUS_ON
);
458 vector
<CCECBusDevice
*> devices
;
459 for (size_t iDevicePtr
= 0; iDevicePtr
< GetMyDevices(devices
); iDevicePtr
++)
460 devices
[iDevicePtr
]->TransmitActiveSource();
467 bool CCECCommandHandler::HandleRoutingChange(const cec_command
&command
)
469 if (command
.parameters
.size
== 4)
471 uint16_t iOldAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
472 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[2] << 8) | ((uint16_t)command
.parameters
[3]);
474 CCECBusDevice
*device
= GetDevice(command
.initiator
);
476 device
->SetStreamPath(iNewAddress
, iOldAddress
);
481 bool CCECCommandHandler::HandleRoutingInformation(const cec_command
&command
)
483 if (command
.parameters
.size
== 2)
485 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
486 CCECBusDevice
*device
= m_processor
->GetDeviceByPhysicalAddress(iNewAddress
);
488 device
->MarkAsActiveSource();
494 bool CCECCommandHandler::HandleSetMenuLanguage(const cec_command
&command
)
496 if (command
.parameters
.size
== 3)
498 CCECBusDevice
*device
= GetDevice(command
.initiator
);
501 cec_menu_language language
;
502 language
.device
= command
.initiator
;
503 for (uint8_t iPtr
= 0; iPtr
< 4; iPtr
++)
504 language
.language
[iPtr
] = command
.parameters
[iPtr
];
505 language
.language
[3] = 0;
506 device
->SetMenuLanguage(language
);
513 bool CCECCommandHandler::HandleSetOSDName(const cec_command
&command
)
515 if (command
.parameters
.size
> 0)
517 CCECBusDevice
*device
= GetDevice(command
.initiator
);
521 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
522 buf
[iPtr
] = (char)command
.parameters
[iPtr
];
523 buf
[command
.parameters
.size
] = 0;
525 CStdString
strName(buf
);
526 device
->SetOSDName(strName
);
534 bool CCECCommandHandler::HandleSetStreamPath(const cec_command
&command
)
536 if (m_processor
->CECInitialised() && command
.parameters
.size
>= 2)
538 uint16_t iStreamAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
539 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, ">> %i sets stream path to physical address %04x", command
.initiator
, iStreamAddress
);
541 /* one of the device handled by libCEC has been made active */
542 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamAddress
);
543 if (device
&& device
->IsHandledByLibCEC())
544 device
->ActivateSource();
549 bool CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command
&command
)
551 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
553 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.destination
));
556 if (command
.parameters
.size
>= 2)
558 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
559 device
->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON
);
560 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
561 CCECBusDevice
*newActiveDevice
= GetDeviceByPhysicalAddress(iNewAddress
);
563 newActiveDevice
->MarkAsActiveSource();
564 return device
->TransmitSetSystemAudioMode(command
.initiator
);
568 device
->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF
);
569 return device
->TransmitSetSystemAudioMode(command
.initiator
);
576 bool CCECCommandHandler::HandleStandby(const cec_command
&command
)
578 CCECBusDevice
*device
= GetDevice(command
.initiator
);
580 device
->SetPowerStatus(CEC_POWER_STATUS_STANDBY
);
585 bool CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command
&command
)
587 if (command
.parameters
.size
== 1)
589 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.initiator
));
592 device
->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
600 bool CCECCommandHandler::HandleSetSystemAudioMode(const cec_command
&command
)
602 if (command
.parameters
.size
== 1)
604 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.initiator
));
607 device
->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
615 bool CCECCommandHandler::HandleTextViewOn(const cec_command
&command
)
617 m_processor
->GetDevice(command
.initiator
)->MarkAsActiveSource();
621 bool CCECCommandHandler::HandleUserControlPressed(const cec_command
&command
)
623 if (m_processor
->CECInitialised() &&
624 m_processor
->IsHandledByLibCEC(command
.destination
) &&
625 command
.parameters
.size
> 0)
627 CCECBusDevice
*device
= GetDevice(command
.destination
);
631 CCECClient
*client
= device
->GetClient();
635 if (command
.parameters
[0] <= CEC_USER_CONTROL_CODE_MAX
)
636 client
->SetCurrentButton((cec_user_control_code
) command
.parameters
[0]);
638 if (command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER
||
639 command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION
)
645 // CEC_USER_CONTROL_CODE_POWER operates as a toggle
646 // assume CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION does not
647 if (command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER
)
649 cec_power_status status
= device
->GetCurrentPowerStatus();
650 bPowerOn
= !(status
== CEC_POWER_STATUS_ON
|| status
== CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
655 device
->ActivateSource();
659 device
->MarkAsInactiveSource();
660 device
->TransmitInactiveSource();
661 device
->SetMenuState(CEC_MENU_STATE_DEACTIVATED
);
670 bool CCECCommandHandler::HandleUserControlRelease(const cec_command
&command
)
672 CCECClient
*client
= m_processor
->GetClient(command
.destination
);
678 bool CCECCommandHandler::HandleVendorCommand(const cec_command
& UNUSED(command
))
683 void CCECCommandHandler::UnhandledCommand(const cec_command
&command
)
685 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "unhandled command with opcode %02x from address %d", command
.opcode
, command
.initiator
);
687 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
688 m_processor
->TransmitAbort(m_busDevice
->GetLogicalAddress(), command
.initiator
, command
.opcode
, CEC_ABORT_REASON_UNRECOGNIZED_OPCODE
);
691 size_t CCECCommandHandler::GetMyDevices(vector
<CCECBusDevice
*> &devices
) const
695 cec_logical_addresses addresses
= m_processor
->GetLogicalAddresses();
696 for (uint8_t iPtr
= CECDEVICE_TV
; iPtr
< CECDEVICE_BROADCAST
; iPtr
++)
700 devices
.push_back(GetDevice((cec_logical_address
) iPtr
));
708 CCECBusDevice
*CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress
) const
710 return m_processor
->GetDevice(iLogicalAddress
);
713 CCECBusDevice
*CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
) const
715 return m_processor
->GetDeviceByPhysicalAddress(iPhysicalAddress
);
718 bool CCECCommandHandler::SetVendorId(const cec_command
&command
)
720 bool bChanged(false);
721 if (command
.parameters
.size
< 3)
723 LIB_CEC
->AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
727 uint64_t iVendorId
= ((uint64_t)command
.parameters
[0] << 16) +
728 ((uint64_t)command
.parameters
[1] << 8) +
729 (uint64_t)command
.parameters
[2];
731 CCECBusDevice
*device
= GetDevice((cec_logical_address
) command
.initiator
);
733 bChanged
= device
->SetVendorId(iVendorId
);
737 void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress
, uint16_t iNewAddress
)
739 if (!m_processor
->IsHandledByLibCEC(iAddress
))
741 CCECBusDevice
*otherDevice
= m_processor
->GetDeviceByPhysicalAddress(iNewAddress
);
742 CCECClient
*client
= otherDevice
? otherDevice
->GetClient() : NULL
;
744 CCECBusDevice
*device
= m_processor
->GetDevice(iAddress
);
746 device
->SetPhysicalAddress(iNewAddress
);
749 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "device with logical address %X not found", iAddress
);
752 /* another device reported the same physical address as ours */
754 client
->ResetPhysicalAddress();
758 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
);
762 bool CCECCommandHandler::PowerOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
764 if (iDestination
== CECDEVICE_TV
)
765 return TransmitImageViewOn(iInitiator
, iDestination
);
767 return TransmitKeypress(iInitiator
, iDestination
, CEC_USER_CONTROL_CODE_POWER
) &&
768 TransmitKeyRelease(iInitiator
, iDestination
);
771 bool CCECCommandHandler::TransmitImageViewOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
774 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_IMAGE_VIEW_ON
);
776 return Transmit(command
);
779 bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
782 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_STANDBY
);
784 return Transmit(command
);
787 bool CCECCommandHandler::TransmitRequestActiveSource(const cec_logical_address iInitiator
, bool bWaitForResponse
/* = true */)
790 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_REQUEST_ACTIVE_SOURCE
);
792 return Transmit(command
, !bWaitForResponse
);
795 bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
798 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_CEC_VERSION
);
800 return Transmit(command
, !bWaitForResponse
);
803 bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
806 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_MENU_LANGUAGE
);
808 return Transmit(command
, !bWaitForResponse
);
811 bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
814 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_OSD_NAME
);
816 return Transmit(command
, !bWaitForResponse
);
819 bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
822 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
);
824 return Transmit(command
, !bWaitForResponse
);
827 bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
830 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
);
832 return Transmit(command
, !bWaitForResponse
);
835 bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
838 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
840 return Transmit(command
, !bWaitForResponse
);
843 bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
846 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
847 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
848 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
850 return Transmit(command
);
853 bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_version cecVersion
)
856 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_CEC_VERSION
);
857 command
.parameters
.PushBack((uint8_t)cecVersion
);
859 return Transmit(command
);
862 bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
865 cec_command::Format(command
, iInitiator
, CECDEVICE_TV
, CEC_OPCODE_INACTIVE_SOURCE
);
866 command
.parameters
.PushBack((iPhysicalAddress
>> 8) & 0xFF);
867 command
.parameters
.PushBack(iPhysicalAddress
& 0xFF);
869 return Transmit(command
);
872 bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_menu_state menuState
)
875 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_MENU_STATUS
);
876 command
.parameters
.PushBack((uint8_t)menuState
);
878 return Transmit(command
);
881 bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, CStdString strDeviceName
)
884 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_NAME
);
885 for (size_t iPtr
= 0; iPtr
< strDeviceName
.length(); iPtr
++)
886 command
.parameters
.PushBack(strDeviceName
.at(iPtr
));
888 return Transmit(command
);
891 bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_display_control duration
, const char *strMessage
)
894 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_STRING
);
895 command
.parameters
.PushBack((uint8_t)duration
);
897 size_t iLen
= strlen(strMessage
);
898 if (iLen
> 13) iLen
= 13;
900 for (size_t iPtr
= 0; iPtr
< iLen
; iPtr
++)
901 command
.parameters
.PushBack(strMessage
[iPtr
]);
903 return Transmit(command
);
906 bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
, cec_device_type type
)
909 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
910 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
911 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
912 command
.parameters
.PushBack((uint8_t) (type
));
914 return Transmit(command
);
917 bool CCECCommandHandler::TransmitSetMenuLanguage(const cec_logical_address iInitiator
, const char lang
[3])
920 command
.Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_SET_MENU_LANGUAGE
);
921 command
.parameters
.PushBack((uint8_t) lang
[0]);
922 command
.parameters
.PushBack((uint8_t) lang
[1]);
923 command
.parameters
.PushBack((uint8_t) lang
[2]);
925 return Transmit(command
);
928 bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
931 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_NONE
);
933 return Transmit(command
);
936 bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_power_status state
)
939 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_POWER_STATUS
);
940 command
.parameters
.PushBack((uint8_t) state
);
942 return Transmit(command
);
945 bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator
, uint64_t iVendorId
)
948 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_DEVICE_VENDOR_ID
);
950 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 16) & 0xFF));
951 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 8) & 0xFF));
952 command
.parameters
.PushBack((uint8_t) ((uint64_t)iVendorId
& 0xFF));
954 return Transmit(command
);
957 bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, uint8_t state
)
960 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_AUDIO_STATUS
);
961 command
.parameters
.PushBack(state
);
963 return Transmit(command
);
966 bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
)
969 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
);
970 command
.parameters
.PushBack((uint8_t)state
);
972 return Transmit(command
);
975 bool CCECCommandHandler::TransmitSetStreamPath(uint16_t iStreamPath
)
978 cec_command::Format(command
, m_busDevice
->GetLogicalAddress(), CECDEVICE_BROADCAST
, CEC_OPCODE_SET_STREAM_PATH
);
979 command
.parameters
.PushBack((uint8_t) ((iStreamPath
>> 8) & 0xFF));
980 command
.parameters
.PushBack((uint8_t) (iStreamPath
& 0xFF));
982 return Transmit(command
);
985 bool CCECCommandHandler::TransmitSystemAudioModeStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
)
988 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
);
989 command
.parameters
.PushBack((uint8_t)state
);
991 return Transmit(command
);
994 bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_deck_info state
)
997 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_DECK_STATUS
);
998 command
.PushBack((uint8_t)state
);
1000 return Transmit(command
);
1003 bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = true */)
1005 cec_command command
;
1006 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_PRESSED
);
1007 command
.parameters
.PushBack((uint8_t)key
);
1009 return Transmit(command
, !bWait
);
1012 bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWait
/* = true */)
1014 cec_command command
;
1015 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_RELEASE
);
1017 return Transmit(command
, !bWait
);
1020 bool CCECCommandHandler::Transmit(cec_command
&command
, bool bSuppressWait
/* = false */)
1022 bool bReturn(false);
1023 cec_opcode
expectedResponse(cec_command::GetResponseOpcode(command
.opcode
));
1024 bool bExpectResponse(expectedResponse
!= CEC_OPCODE_NONE
&& !bSuppressWait
);
1025 command
.transmit_timeout
= m_iTransmitTimeout
;
1027 if (command
.initiator
== CECDEVICE_UNKNOWN
)
1029 LIB_CEC
->AddLog(CEC_LOG_ERROR
, "not transmitting a command without a valid initiator");
1034 uint8_t iTries(0), iMaxTries(!command
.opcode_set
? 1 : m_iTransmitRetries
+ 1);
1035 while (!bReturn
&& ++iTries
<= iMaxTries
&& !m_busDevice
->IsUnsupportedFeature(command
.opcode
))
1037 if ((bReturn
= m_processor
->Transmit(command
)) == true)
1039 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "command transmitted");
1040 if (bExpectResponse
)
1042 bReturn
= m_waitForResponse
->Wait(expectedResponse
);
1043 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, bReturn
? "expected response received (%X: %s)" : "expected response not received (%X: %s)", (int)expectedResponse
, ToString(expectedResponse
));
1052 bool CCECCommandHandler::ActivateSource(void)
1054 if (m_busDevice
->IsActiveSource() &&
1055 m_busDevice
->IsHandledByLibCEC())
1057 m_busDevice
->SetPowerStatus(CEC_POWER_STATUS_ON
);
1058 m_busDevice
->SetMenuState(CEC_MENU_STATE_ACTIVATED
);
1060 m_busDevice
->TransmitImageViewOn();
1061 m_busDevice
->TransmitActiveSource();
1062 m_busDevice
->TransmitMenuState(CECDEVICE_TV
);
1064 CCECPlaybackDevice
*playbackDevice
= m_busDevice
->AsPlaybackDevice();
1065 if (playbackDevice
&& SendDeckStatusUpdateOnActiveSource())
1066 playbackDevice
->TransmitDeckStatus(CECDEVICE_TV
);
1067 m_bHandlerInited
= true;
1072 void CCECCommandHandler::SignalOpcode(cec_opcode opcode
)
1074 m_waitForResponse
->Received(opcode
);