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
)
63 CCECCommandHandler::~CCECCommandHandler(void)
65 DELETE_AND_NULL(m_waitForResponse
);
68 bool CCECCommandHandler::HandleCommand(const cec_command
&command
)
70 if (command
.opcode_set
== 0)
71 return HandlePoll(command
);
75 CCECClient
*client
= m_busDevice
->GetClient();
77 client
->AddCommand(command
);
79 switch(command
.opcode
)
81 case CEC_OPCODE_REPORT_POWER_STATUS
:
82 HandleReportPowerStatus(command
);
84 case CEC_OPCODE_CEC_VERSION
:
85 HandleDeviceCecVersion(command
);
87 case CEC_OPCODE_SET_MENU_LANGUAGE
:
88 HandleSetMenuLanguage(command
);
90 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
91 if (m_processor
->CECInitialised())
92 HandleGivePhysicalAddress(command
);
94 case CEC_OPCODE_GET_MENU_LANGUAGE
:
95 if (m_processor
->CECInitialised())
96 HandleGiveMenuLanguage(command
);
98 case CEC_OPCODE_GIVE_OSD_NAME
:
99 if (m_processor
->CECInitialised())
100 HandleGiveOSDName(command
);
102 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
103 if (m_processor
->CECInitialised())
104 HandleGiveDeviceVendorId(command
);
106 case CEC_OPCODE_DEVICE_VENDOR_ID
:
107 HandleDeviceVendorId(command
);
109 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
110 HandleDeviceVendorCommandWithId(command
);
112 case CEC_OPCODE_GIVE_DECK_STATUS
:
113 if (m_processor
->CECInitialised())
114 HandleGiveDeckStatus(command
);
116 case CEC_OPCODE_DECK_CONTROL
:
117 HandleDeckControl(command
);
119 case CEC_OPCODE_MENU_REQUEST
:
120 if (m_processor
->CECInitialised())
121 HandleMenuRequest(command
);
123 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
124 if (m_processor
->CECInitialised())
125 HandleGiveDevicePowerStatus(command
);
127 case CEC_OPCODE_GET_CEC_VERSION
:
128 if (m_processor
->CECInitialised())
129 HandleGetCecVersion(command
);
131 case CEC_OPCODE_USER_CONTROL_PRESSED
:
132 if (m_processor
->CECInitialised())
133 HandleUserControlPressed(command
);
135 case CEC_OPCODE_USER_CONTROL_RELEASE
:
136 if (m_processor
->CECInitialised())
137 HandleUserControlRelease(command
);
139 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
140 if (m_processor
->CECInitialised())
141 HandleGiveAudioStatus(command
);
143 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
144 if (m_processor
->CECInitialised())
145 HandleGiveSystemAudioModeStatus(command
);
147 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
148 if (m_processor
->CECInitialised())
149 HandleSystemAudioModeRequest(command
);
151 case CEC_OPCODE_REPORT_AUDIO_STATUS
:
152 HandleReportAudioStatus(command
);
154 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
:
155 HandleSystemAudioModeStatus(command
);
157 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
:
158 HandleSetSystemAudioMode(command
);
160 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
161 if (m_processor
->CECInitialised())
162 HandleRequestActiveSource(command
);
164 case CEC_OPCODE_SET_STREAM_PATH
:
165 HandleSetStreamPath(command
);
167 case CEC_OPCODE_ROUTING_CHANGE
:
168 HandleRoutingChange(command
);
170 case CEC_OPCODE_ROUTING_INFORMATION
:
171 HandleRoutingInformation(command
);
173 case CEC_OPCODE_STANDBY
:
174 if (m_processor
->CECInitialised())
175 HandleStandby(command
);
177 case CEC_OPCODE_ACTIVE_SOURCE
:
178 HandleActiveSource(command
);
180 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
:
181 HandleReportPhysicalAddress(command
);
183 case CEC_OPCODE_SET_OSD_NAME
:
184 HandleSetOSDName(command
);
186 case CEC_OPCODE_IMAGE_VIEW_ON
:
187 HandleImageViewOn(command
);
189 case CEC_OPCODE_TEXT_VIEW_ON
:
190 HandleTextViewOn(command
);
192 case CEC_OPCODE_FEATURE_ABORT
:
193 HandleFeatureAbort(command
);
195 case CEC_OPCODE_VENDOR_COMMAND
:
196 HandleVendorCommand(command
);
204 m_waitForResponse
->Received((command
.opcode
== CEC_OPCODE_FEATURE_ABORT
&& command
.parameters
.size
> 0) ? (cec_opcode
)command
.parameters
[0] : command
.opcode
);
206 UnhandledCommand(command
);
211 bool CCECCommandHandler::HandleActiveSource(const cec_command
&command
)
213 if (command
.parameters
.size
== 2)
215 uint16_t iAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
216 CCECBusDevice
*device
= m_processor
->GetDeviceByPhysicalAddress(iAddress
);
218 device
->MarkAsActiveSource();
224 bool CCECCommandHandler::HandleDeckControl(const cec_command
&command
)
226 CCECPlaybackDevice
*device
= CCECBusDevice::AsPlaybackDevice(GetDevice(command
.destination
));
227 if (device
&& command
.parameters
.size
> 0)
229 device
->SetDeckControlMode((cec_deck_control_mode
) command
.parameters
[0]);
236 bool CCECCommandHandler::HandleDeviceCecVersion(const cec_command
&command
)
238 if (command
.parameters
.size
== 1)
240 CCECBusDevice
*device
= GetDevice(command
.initiator
);
242 device
->SetCecVersion((cec_version
) command
.parameters
[0]);
248 bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command
&command
)
250 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
251 m_processor
->TransmitAbort(command
.destination
, command
.initiator
, command
.opcode
, CEC_ABORT_REASON_REFUSED
);
256 bool CCECCommandHandler::HandleDeviceVendorId(const cec_command
&command
)
258 return SetVendorId(command
);
261 bool CCECCommandHandler::HandleFeatureAbort(const cec_command
&command
)
263 if (command
.parameters
.size
== 2 &&
264 (command
.parameters
[1] == CEC_ABORT_REASON_UNRECOGNIZED_OPCODE
||
265 command
.parameters
[1] == CEC_ABORT_REASON_REFUSED
))
266 m_processor
->GetDevice(command
.initiator
)->SetUnsupportedFeature((cec_opcode
)command
.parameters
[0]);
270 bool CCECCommandHandler::HandleGetCecVersion(const cec_command
&command
)
272 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
274 CCECBusDevice
*device
= GetDevice(command
.destination
);
276 return device
->TransmitCECVersion(command
.initiator
);
282 bool CCECCommandHandler::HandleGiveAudioStatus(const cec_command
&command
)
284 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
286 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.destination
));
288 return device
->TransmitAudioStatus(command
.initiator
);
294 bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
296 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
298 CCECPlaybackDevice
*device
= CCECBusDevice::AsPlaybackDevice(GetDevice(command
.destination
));
300 return device
->TransmitDeckStatus(command
.initiator
);
306 bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command
&command
)
308 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
310 CCECBusDevice
*device
= GetDevice(command
.destination
);
312 return device
->TransmitPowerState(command
.initiator
);
318 bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command
&command
)
320 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
322 CCECBusDevice
*device
= GetDevice(command
.destination
);
324 return device
->TransmitVendorID(command
.initiator
);
330 bool CCECCommandHandler::HandleGiveOSDName(const cec_command
&command
)
332 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
334 CCECBusDevice
*device
= GetDevice(command
.destination
);
336 return device
->TransmitOSDName(command
.initiator
);
342 bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command
&command
)
344 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
346 CCECBusDevice
*device
= GetDevice(command
.destination
);
348 return device
->TransmitPhysicalAddress();
354 bool CCECCommandHandler::HandleGiveMenuLanguage(const cec_command
&command
)
356 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
358 CCECBusDevice
*device
= GetDevice(command
.destination
);
360 return device
->TransmitSetMenuLanguage(command
.initiator
);
366 bool CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command
&command
)
368 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
370 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.destination
));
372 return device
->TransmitSystemAudioModeStatus(command
.initiator
);
378 bool CCECCommandHandler::HandleImageViewOn(const cec_command
&command
)
380 m_processor
->GetDevice(command
.initiator
)->MarkAsActiveSource();
384 bool CCECCommandHandler::HandleMenuRequest(const cec_command
&command
)
386 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
388 CCECBusDevice
*device
= GetDevice(command
.destination
);
391 CCECClient
*client
= device
->GetClient();
394 if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_ACTIVATE
)
396 if (client
->MenuStateChanged(CEC_MENU_STATE_ACTIVATED
) == 1)
397 device
->SetMenuState(CEC_MENU_STATE_ACTIVATED
);
399 else if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_DEACTIVATE
)
401 if (client
->MenuStateChanged(CEC_MENU_STATE_DEACTIVATED
) == 1)
402 device
->SetMenuState(CEC_MENU_STATE_DEACTIVATED
);
405 return device
->TransmitMenuState(command
.initiator
);
412 bool CCECCommandHandler::HandlePoll(const cec_command
&command
)
414 m_busDevice
->HandlePoll(command
.destination
);
418 bool CCECCommandHandler::HandleReportAudioStatus(const cec_command
&command
)
420 if (command
.parameters
.size
== 1)
422 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.initiator
));
425 device
->SetAudioStatus(command
.parameters
[0]);
432 bool CCECCommandHandler::HandleReportPhysicalAddress(const cec_command
&command
)
434 if (command
.parameters
.size
== 3)
436 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
437 SetPhysicalAddress(command
.initiator
, iNewAddress
);
442 bool CCECCommandHandler::HandleReportPowerStatus(const cec_command
&command
)
444 if (command
.parameters
.size
== 1)
446 CCECBusDevice
*device
= GetDevice(command
.initiator
);
448 device
->SetPowerStatus((cec_power_status
) command
.parameters
[0]);
453 bool CCECCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
455 if (m_processor
->CECInitialised())
457 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, ">> %i requests active source", (uint8_t) command
.initiator
);
458 m_processor
->GetDevice(command
.initiator
)->SetPowerStatus(CEC_POWER_STATUS_ON
);
460 vector
<CCECBusDevice
*> devices
;
461 for (size_t iDevicePtr
= 0; iDevicePtr
< GetMyDevices(devices
); iDevicePtr
++)
462 devices
[iDevicePtr
]->TransmitActiveSource();
469 bool CCECCommandHandler::HandleRoutingChange(const cec_command
&command
)
471 if (command
.parameters
.size
== 4)
473 uint16_t iOldAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
474 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[2] << 8) | ((uint16_t)command
.parameters
[3]);
476 CCECBusDevice
*device
= GetDevice(command
.initiator
);
478 device
->SetStreamPath(iNewAddress
, iOldAddress
);
483 bool CCECCommandHandler::HandleRoutingInformation(const cec_command
&command
)
485 if (command
.parameters
.size
== 2)
487 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
488 CCECBusDevice
*device
= m_processor
->GetDeviceByPhysicalAddress(iNewAddress
);
490 device
->MarkAsActiveSource();
496 bool CCECCommandHandler::HandleSetMenuLanguage(const cec_command
&command
)
498 if (command
.parameters
.size
== 3)
500 CCECBusDevice
*device
= GetDevice(command
.initiator
);
503 cec_menu_language language
;
504 language
.device
= command
.initiator
;
505 for (uint8_t iPtr
= 0; iPtr
< 4; iPtr
++)
506 language
.language
[iPtr
] = command
.parameters
[iPtr
];
507 language
.language
[3] = 0;
508 device
->SetMenuLanguage(language
);
515 bool CCECCommandHandler::HandleSetOSDName(const cec_command
&command
)
517 if (command
.parameters
.size
> 0)
519 CCECBusDevice
*device
= GetDevice(command
.initiator
);
523 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
524 buf
[iPtr
] = (char)command
.parameters
[iPtr
];
525 buf
[command
.parameters
.size
] = 0;
527 CStdString
strName(buf
);
528 device
->SetOSDName(strName
);
536 bool CCECCommandHandler::HandleSetStreamPath(const cec_command
&command
)
538 if (m_processor
->CECInitialised() && command
.parameters
.size
>= 2)
540 uint16_t iStreamAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
541 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, ">> %i sets stream path to physical address %04x", command
.initiator
, iStreamAddress
);
543 /* one of the device handled by libCEC has been made active */
544 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamAddress
);
545 if (device
&& device
->IsHandledByLibCEC())
546 device
->ActivateSource();
551 bool CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command
&command
)
553 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
555 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.destination
));
558 if (command
.parameters
.size
>= 2)
560 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
561 device
->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON
);
562 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
563 CCECBusDevice
*newActiveDevice
= GetDeviceByPhysicalAddress(iNewAddress
);
565 newActiveDevice
->MarkAsActiveSource();
566 return device
->TransmitSetSystemAudioMode(command
.initiator
);
570 device
->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF
);
571 return device
->TransmitSetSystemAudioMode(command
.initiator
);
578 bool CCECCommandHandler::HandleStandby(const cec_command
&command
)
580 CCECBusDevice
*device
= GetDevice(command
.initiator
);
582 device
->SetPowerStatus(CEC_POWER_STATUS_STANDBY
);
587 bool CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command
&command
)
589 if (command
.parameters
.size
== 1)
591 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.initiator
));
594 device
->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
602 bool CCECCommandHandler::HandleSetSystemAudioMode(const cec_command
&command
)
604 if (command
.parameters
.size
== 1)
606 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.initiator
));
609 device
->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
617 bool CCECCommandHandler::HandleTextViewOn(const cec_command
&command
)
619 m_processor
->GetDevice(command
.initiator
)->MarkAsActiveSource();
623 bool CCECCommandHandler::HandleUserControlPressed(const cec_command
&command
)
625 if (m_processor
->CECInitialised() &&
626 m_processor
->IsHandledByLibCEC(command
.destination
) &&
627 command
.parameters
.size
> 0)
629 CCECBusDevice
*device
= GetDevice(command
.destination
);
633 CCECClient
*client
= device
->GetClient();
637 if (command
.parameters
[0] <= CEC_USER_CONTROL_CODE_MAX
)
638 client
->SetCurrentButton((cec_user_control_code
) command
.parameters
[0]);
640 if (command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER
||
641 command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION
)
647 // CEC_USER_CONTROL_CODE_POWER operates as a toggle
648 // assume CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION does not
649 if (command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER
)
651 cec_power_status status
= device
->GetCurrentPowerStatus();
652 bPowerOn
= !(status
== CEC_POWER_STATUS_ON
|| status
== CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
657 device
->ActivateSource();
661 device
->MarkAsInactiveSource();
662 device
->TransmitInactiveSource();
663 device
->SetMenuState(CEC_MENU_STATE_DEACTIVATED
);
672 bool CCECCommandHandler::HandleUserControlRelease(const cec_command
&command
)
674 CCECClient
*client
= m_processor
->GetClient(command
.destination
);
680 bool CCECCommandHandler::HandleVendorCommand(const cec_command
& UNUSED(command
))
685 void CCECCommandHandler::UnhandledCommand(const cec_command
&command
)
687 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "unhandled command with opcode %02x from address %d", command
.opcode
, command
.initiator
);
689 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
690 m_processor
->TransmitAbort(m_busDevice
->GetLogicalAddress(), command
.initiator
, command
.opcode
, CEC_ABORT_REASON_UNRECOGNIZED_OPCODE
);
693 size_t CCECCommandHandler::GetMyDevices(vector
<CCECBusDevice
*> &devices
) const
697 cec_logical_addresses addresses
= m_processor
->GetLogicalAddresses();
698 for (uint8_t iPtr
= CECDEVICE_TV
; iPtr
< CECDEVICE_BROADCAST
; iPtr
++)
702 devices
.push_back(GetDevice((cec_logical_address
) iPtr
));
710 CCECBusDevice
*CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress
) const
712 return m_processor
->GetDevice(iLogicalAddress
);
715 CCECBusDevice
*CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
) const
717 return m_processor
->GetDeviceByPhysicalAddress(iPhysicalAddress
);
720 bool CCECCommandHandler::SetVendorId(const cec_command
&command
)
722 bool bChanged(false);
723 if (command
.parameters
.size
< 3)
725 LIB_CEC
->AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
729 uint64_t iVendorId
= ((uint64_t)command
.parameters
[0] << 16) +
730 ((uint64_t)command
.parameters
[1] << 8) +
731 (uint64_t)command
.parameters
[2];
733 CCECBusDevice
*device
= GetDevice((cec_logical_address
) command
.initiator
);
735 bChanged
= device
->SetVendorId(iVendorId
);
739 void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress
, uint16_t iNewAddress
)
741 if (!m_processor
->IsHandledByLibCEC(iAddress
))
743 CCECBusDevice
*otherDevice
= m_processor
->GetDeviceByPhysicalAddress(iNewAddress
);
744 CCECClient
*client
= otherDevice
? otherDevice
->GetClient() : NULL
;
746 CCECBusDevice
*device
= m_processor
->GetDevice(iAddress
);
748 device
->SetPhysicalAddress(iNewAddress
);
751 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "device with logical address %X not found", iAddress
);
754 /* another device reported the same physical address as ours */
756 client
->ResetPhysicalAddress();
760 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
);
764 bool CCECCommandHandler::PowerOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
766 if (iDestination
== CECDEVICE_TV
)
767 return TransmitImageViewOn(iInitiator
, iDestination
);
769 return TransmitKeypress(iInitiator
, iDestination
, CEC_USER_CONTROL_CODE_POWER
) &&
770 TransmitKeyRelease(iInitiator
, iDestination
);
773 bool CCECCommandHandler::TransmitImageViewOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
776 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_IMAGE_VIEW_ON
);
778 return Transmit(command
);
781 bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
784 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_STANDBY
);
786 return Transmit(command
);
789 bool CCECCommandHandler::TransmitRequestActiveSource(const cec_logical_address iInitiator
, bool bWaitForResponse
/* = true */)
792 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_REQUEST_ACTIVE_SOURCE
);
794 return Transmit(command
, !bWaitForResponse
);
797 bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
800 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_CEC_VERSION
);
802 return Transmit(command
, !bWaitForResponse
);
805 bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
808 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_MENU_LANGUAGE
);
810 return Transmit(command
, !bWaitForResponse
);
813 bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
816 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_OSD_NAME
);
818 return Transmit(command
, !bWaitForResponse
);
821 bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
824 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
);
826 return Transmit(command
, !bWaitForResponse
);
829 bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
832 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
);
834 return Transmit(command
, !bWaitForResponse
);
837 bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
840 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
842 return Transmit(command
, !bWaitForResponse
);
845 bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
848 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
849 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
850 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
852 return Transmit(command
);
855 bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_version cecVersion
)
858 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_CEC_VERSION
);
859 command
.parameters
.PushBack((uint8_t)cecVersion
);
861 return Transmit(command
);
864 bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
867 cec_command::Format(command
, iInitiator
, CECDEVICE_TV
, CEC_OPCODE_INACTIVE_SOURCE
);
868 command
.parameters
.PushBack((iPhysicalAddress
>> 8) & 0xFF);
869 command
.parameters
.PushBack(iPhysicalAddress
& 0xFF);
871 return Transmit(command
);
874 bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_menu_state menuState
)
877 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_MENU_STATUS
);
878 command
.parameters
.PushBack((uint8_t)menuState
);
880 return Transmit(command
);
883 bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, CStdString strDeviceName
)
886 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_NAME
);
887 for (size_t iPtr
= 0; iPtr
< strDeviceName
.length(); iPtr
++)
888 command
.parameters
.PushBack(strDeviceName
.at(iPtr
));
890 return Transmit(command
);
893 bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_display_control duration
, const char *strMessage
)
896 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_STRING
);
897 command
.parameters
.PushBack((uint8_t)duration
);
899 size_t iLen
= strlen(strMessage
);
900 if (iLen
> 13) iLen
= 13;
902 for (size_t iPtr
= 0; iPtr
< iLen
; iPtr
++)
903 command
.parameters
.PushBack(strMessage
[iPtr
]);
905 return Transmit(command
);
908 bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
, cec_device_type type
)
911 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
912 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
913 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
914 command
.parameters
.PushBack((uint8_t) (type
));
916 return Transmit(command
);
919 bool CCECCommandHandler::TransmitSetMenuLanguage(const cec_logical_address iInitiator
, const char lang
[3])
922 command
.Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_SET_MENU_LANGUAGE
);
923 command
.parameters
.PushBack((uint8_t) lang
[0]);
924 command
.parameters
.PushBack((uint8_t) lang
[1]);
925 command
.parameters
.PushBack((uint8_t) lang
[2]);
927 return Transmit(command
);
930 bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
933 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_NONE
);
935 return Transmit(command
);
938 bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_power_status state
)
941 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_POWER_STATUS
);
942 command
.parameters
.PushBack((uint8_t) state
);
944 return Transmit(command
);
947 bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator
, uint64_t iVendorId
)
950 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_DEVICE_VENDOR_ID
);
952 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 16) & 0xFF));
953 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 8) & 0xFF));
954 command
.parameters
.PushBack((uint8_t) ((uint64_t)iVendorId
& 0xFF));
956 return Transmit(command
);
959 bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, uint8_t state
)
962 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_AUDIO_STATUS
);
963 command
.parameters
.PushBack(state
);
965 return Transmit(command
);
968 bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
)
971 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
);
972 command
.parameters
.PushBack((uint8_t)state
);
974 return Transmit(command
);
977 bool CCECCommandHandler::TransmitSetStreamPath(uint16_t iStreamPath
)
980 cec_command::Format(command
, m_busDevice
->GetLogicalAddress(), CECDEVICE_BROADCAST
, CEC_OPCODE_SET_STREAM_PATH
);
981 command
.parameters
.PushBack((uint8_t) ((iStreamPath
>> 8) & 0xFF));
982 command
.parameters
.PushBack((uint8_t) (iStreamPath
& 0xFF));
984 return Transmit(command
);
987 bool CCECCommandHandler::TransmitSystemAudioModeStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
)
990 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
);
991 command
.parameters
.PushBack((uint8_t)state
);
993 return Transmit(command
);
996 bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_deck_info state
)
999 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_DECK_STATUS
);
1000 command
.PushBack((uint8_t)state
);
1002 return Transmit(command
);
1005 bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = true */)
1007 cec_command command
;
1008 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_PRESSED
);
1009 command
.parameters
.PushBack((uint8_t)key
);
1011 return Transmit(command
, !bWait
);
1014 bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWait
/* = true */)
1016 cec_command command
;
1017 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_RELEASE
);
1019 return Transmit(command
, !bWait
);
1022 bool CCECCommandHandler::Transmit(cec_command
&command
, bool bSuppressWait
/* = false */)
1024 bool bReturn(false);
1025 cec_opcode
expectedResponse(cec_command::GetResponseOpcode(command
.opcode
));
1026 bool bExpectResponse(expectedResponse
!= CEC_OPCODE_NONE
&& !bSuppressWait
);
1027 command
.transmit_timeout
= m_iTransmitTimeout
;
1029 if (command
.initiator
== CECDEVICE_UNKNOWN
)
1031 LIB_CEC
->AddLog(CEC_LOG_ERROR
, "not transmitting a command without a valid initiator");
1036 uint8_t iTries(0), iMaxTries(!command
.opcode_set
? 1 : m_iTransmitRetries
+ 1);
1037 while (!bReturn
&& ++iTries
<= iMaxTries
&& !m_busDevice
->IsUnsupportedFeature(command
.opcode
))
1039 if ((bReturn
= m_processor
->Transmit(command
)) == true)
1041 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "command transmitted");
1042 if (bExpectResponse
)
1044 bReturn
= m_waitForResponse
->Wait(expectedResponse
);
1045 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, bReturn
? "expected response received (%X: %s)" : "expected response not received (%X: %s)", (int)expectedResponse
, ToString(expectedResponse
));
1054 bool CCECCommandHandler::ActivateSource(void)
1056 if (m_busDevice
->IsActiveSource() &&
1057 m_busDevice
->IsHandledByLibCEC())
1059 m_busDevice
->SetPowerStatus(CEC_POWER_STATUS_ON
);
1060 m_busDevice
->SetMenuState(CEC_MENU_STATE_ACTIVATED
);
1062 m_busDevice
->TransmitImageViewOn();
1063 m_busDevice
->TransmitActiveSource();
1064 m_busDevice
->TransmitMenuState(CECDEVICE_TV
);
1066 CCECPlaybackDevice
*playbackDevice
= m_busDevice
->AsPlaybackDevice();
1067 if (playbackDevice
&& SendDeckStatusUpdateOnActiveSource())
1068 playbackDevice
->TransmitDeckStatus(CECDEVICE_TV
);
1069 m_bHandlerInited
= true;
1074 void CCECCommandHandler::SignalOpcode(cec_opcode opcode
)
1076 m_waitForResponse
->Received(opcode
);