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/
34 #include "CECCommandHandler.h"
36 #include "lib/devices/CECBusDevice.h"
37 #include "lib/devices/CECAudioSystem.h"
38 #include "lib/devices/CECPlaybackDevice.h"
39 #include "lib/CECClient.h"
40 #include "lib/CECProcessor.h"
41 #include "lib/LibCEC.h"
42 #include "lib/CECTypeUtils.h"
43 #include "lib/platform/util/util.h"
47 using namespace PLATFORM
;
49 #define LIB_CEC m_busDevice->GetProcessor()->GetLib()
50 #define ToString(p) CCECTypeUtils::ToString(p)
51 #define REQUEST_POWER_STATUS_TIMEOUT 5000
53 CCECCommandHandler::CCECCommandHandler(CCECBusDevice
*busDevice
,
54 int32_t iTransmitTimeout
/* = CEC_DEFAULT_TRANSMIT_TIMEOUT */,
55 int32_t iTransmitWait
/* = CEC_DEFAULT_TRANSMIT_WAIT */,
56 int8_t iTransmitRetries
/* = CEC_DEFAULT_TRANSMIT_RETRIES */,
57 int64_t iActiveSourcePending
/* = 0 */) :
58 m_busDevice(busDevice
),
59 m_processor(m_busDevice
->GetProcessor()),
60 m_iTransmitTimeout(iTransmitTimeout
),
61 m_iTransmitWait(iTransmitWait
),
62 m_iTransmitRetries(iTransmitRetries
),
63 m_bHandlerInited(false),
64 m_bOPTSendDeckStatusUpdateOnActiveSource(false),
65 m_vendorId(CEC_VENDOR_UNKNOWN
),
66 m_iActiveSourcePending(iActiveSourcePending
),
67 m_iPowerStatusRequested(0)
71 bool CCECCommandHandler::HandleCommand(const cec_command
&command
)
73 if (command
.opcode_set
== 0)
74 return HandlePoll(command
);
76 int iHandled(CEC_ABORT_REASON_UNRECOGNIZED_OPCODE
);
78 LIB_CEC
->AddCommand(command
);
80 switch(command
.opcode
)
82 case CEC_OPCODE_REPORT_POWER_STATUS
:
83 iHandled
= HandleReportPowerStatus(command
);
85 case CEC_OPCODE_CEC_VERSION
:
86 iHandled
= HandleDeviceCecVersion(command
);
88 case CEC_OPCODE_SET_MENU_LANGUAGE
:
89 iHandled
= HandleSetMenuLanguage(command
);
91 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
92 iHandled
= HandleGivePhysicalAddress(command
);
94 case CEC_OPCODE_GET_MENU_LANGUAGE
:
95 iHandled
= HandleGiveMenuLanguage(command
);
97 case CEC_OPCODE_GIVE_OSD_NAME
:
98 iHandled
= HandleGiveOSDName(command
);
100 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
101 iHandled
= HandleGiveDeviceVendorId(command
);
103 case CEC_OPCODE_DEVICE_VENDOR_ID
:
104 iHandled
= HandleDeviceVendorId(command
);
106 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
107 iHandled
= HandleDeviceVendorCommandWithId(command
);
109 case CEC_OPCODE_GIVE_DECK_STATUS
:
110 iHandled
= HandleGiveDeckStatus(command
);
112 case CEC_OPCODE_DECK_CONTROL
:
113 iHandled
= HandleDeckControl(command
);
115 case CEC_OPCODE_MENU_REQUEST
:
116 iHandled
= HandleMenuRequest(command
);
118 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
119 iHandled
= HandleGiveDevicePowerStatus(command
);
121 case CEC_OPCODE_GET_CEC_VERSION
:
122 iHandled
= HandleGetCecVersion(command
);
124 case CEC_OPCODE_USER_CONTROL_PRESSED
:
125 iHandled
= HandleUserControlPressed(command
);
127 case CEC_OPCODE_USER_CONTROL_RELEASE
:
128 iHandled
= HandleUserControlRelease(command
);
130 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
131 iHandled
= HandleGiveAudioStatus(command
);
133 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
134 iHandled
= HandleGiveSystemAudioModeStatus(command
);
136 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
137 iHandled
= HandleSystemAudioModeRequest(command
);
139 case CEC_OPCODE_REPORT_AUDIO_STATUS
:
140 iHandled
= HandleReportAudioStatus(command
);
142 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
:
143 iHandled
= HandleSystemAudioModeStatus(command
);
145 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
:
146 iHandled
= HandleSetSystemAudioMode(command
);
148 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
149 iHandled
= HandleRequestActiveSource(command
);
151 case CEC_OPCODE_SET_STREAM_PATH
:
152 iHandled
= HandleSetStreamPath(command
);
154 case CEC_OPCODE_ROUTING_CHANGE
:
155 iHandled
= HandleRoutingChange(command
);
157 case CEC_OPCODE_ROUTING_INFORMATION
:
158 iHandled
= HandleRoutingInformation(command
);
160 case CEC_OPCODE_STANDBY
:
161 iHandled
= HandleStandby(command
);
163 case CEC_OPCODE_ACTIVE_SOURCE
:
164 iHandled
= HandleActiveSource(command
);
166 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
:
167 iHandled
= HandleReportPhysicalAddress(command
);
169 case CEC_OPCODE_SET_OSD_NAME
:
170 iHandled
= HandleSetOSDName(command
);
172 case CEC_OPCODE_IMAGE_VIEW_ON
:
173 iHandled
= HandleImageViewOn(command
);
175 case CEC_OPCODE_TEXT_VIEW_ON
:
176 iHandled
= HandleTextViewOn(command
);
178 case CEC_OPCODE_FEATURE_ABORT
:
179 iHandled
= HandleFeatureAbort(command
);
181 case CEC_OPCODE_VENDOR_COMMAND
:
182 iHandled
= HandleVendorCommand(command
);
184 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN
:
185 iHandled
= HandleVendorRemoteButtonDown(command
);
187 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP
:
188 iHandled
= HandleVendorRemoteButtonUp(command
);
190 case CEC_OPCODE_PLAY
:
191 // libCEC (currently) doesn't need to do anything with this, since player applications handle it
192 // but it should not respond with a feature abort
193 iHandled
= COMMAND_HANDLED
;
199 if (iHandled
== COMMAND_HANDLED
)
200 m_busDevice
->SignalOpcode((command
.opcode
== CEC_OPCODE_FEATURE_ABORT
&& command
.parameters
.size
> 0) ? (cec_opcode
)command
.parameters
[0] : command
.opcode
);
202 UnhandledCommand(command
, (cec_abort_reason
)iHandled
);
204 return iHandled
== COMMAND_HANDLED
;
207 int CCECCommandHandler::HandleActiveSource(const cec_command
&command
)
209 if (command
.parameters
.size
== 2)
211 uint16_t iAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
212 m_processor
->GetDevices()->SetActiveSource(iAddress
);
213 CCECBusDevice
*device
= m_processor
->GetDeviceByPhysicalAddress(iAddress
);
215 device
->MarkAsActiveSource();
217 m_processor
->GetDevices()->SignalAll(command
.opcode
);
218 return COMMAND_HANDLED
;
221 return CEC_ABORT_REASON_INVALID_OPERAND
;
224 int 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]);
230 return COMMAND_HANDLED
;
233 return CEC_ABORT_REASON_INVALID_OPERAND
;
236 int 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]);
244 return COMMAND_HANDLED
;
247 return CEC_ABORT_REASON_INVALID_OPERAND
;
250 int CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command
& UNUSED(command
))
252 return CEC_ABORT_REASON_INVALID_OPERAND
;
255 int CCECCommandHandler::HandleDeviceVendorId(const cec_command
&command
)
257 SetVendorId(command
);
258 return COMMAND_HANDLED
;
261 int 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]);
267 return COMMAND_HANDLED
;
270 int CCECCommandHandler::HandleGetCecVersion(const cec_command
&command
)
272 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
274 CCECBusDevice
*device
= GetDevice(command
.destination
);
275 if (device
&& device
->TransmitCECVersion(command
.initiator
, true))
276 return COMMAND_HANDLED
;
277 return CEC_ABORT_REASON_INVALID_OPERAND
;
280 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
283 int CCECCommandHandler::HandleGiveAudioStatus(const cec_command
&command
)
285 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
287 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.destination
));
288 if (device
&& device
->TransmitAudioStatus(command
.initiator
, true))
289 return COMMAND_HANDLED
;
290 return CEC_ABORT_REASON_INVALID_OPERAND
;
293 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
296 int CCECCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
298 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
300 CCECPlaybackDevice
*device
= CCECBusDevice::AsPlaybackDevice(GetDevice(command
.destination
));
301 if (device
&& device
->TransmitDeckStatus(command
.initiator
, true))
302 return COMMAND_HANDLED
;
303 return CEC_ABORT_REASON_INVALID_OPERAND
;
306 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
309 int CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command
&command
)
311 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
313 CCECBusDevice
*device
= GetDevice(command
.destination
);
314 if (device
&& device
->TransmitPowerState(command
.initiator
, true))
315 return COMMAND_HANDLED
;
316 return CEC_ABORT_REASON_INVALID_OPERAND
;
319 return CEC_ABORT_REASON_INVALID_OPERAND
;
322 int CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command
&command
)
324 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
326 CCECBusDevice
*device
= GetDevice(command
.destination
);
327 if (device
&& device
->TransmitVendorID(command
.initiator
, true, true))
328 return COMMAND_HANDLED
;
331 return CEC_ABORT_REASON_INVALID_OPERAND
;
334 int CCECCommandHandler::HandleGiveOSDName(const cec_command
&command
)
336 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
338 CCECBusDevice
*device
= GetDevice(command
.destination
);
339 if (device
&& device
->TransmitOSDName(command
.initiator
, true))
340 return COMMAND_HANDLED
;
343 return CEC_ABORT_REASON_INVALID_OPERAND
;
346 int CCECCommandHandler::HandleGivePhysicalAddress(const cec_command
&command
)
348 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
350 CCECBusDevice
*device
= GetDevice(command
.destination
);
351 if (device
&& device
->TransmitPhysicalAddress(true))
352 return COMMAND_HANDLED
;
353 return CEC_ABORT_REASON_INVALID_OPERAND
;
356 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
359 int CCECCommandHandler::HandleGiveMenuLanguage(const cec_command
&command
)
361 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
363 CCECBusDevice
*device
= GetDevice(command
.destination
);
364 if (device
&& device
->TransmitSetMenuLanguage(command
.initiator
, true))
365 return COMMAND_HANDLED
;
366 return CEC_ABORT_REASON_INVALID_OPERAND
;
369 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
372 int CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command
&command
)
374 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
376 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.destination
));
377 if (device
&& device
->TransmitSystemAudioModeStatus(command
.initiator
, true))
378 return COMMAND_HANDLED
;
379 return CEC_ABORT_REASON_INVALID_OPERAND
;
382 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
385 int CCECCommandHandler::HandleImageViewOn(const cec_command
&command
)
387 CCECBusDevice
*device
= GetDevice(command
.destination
);
388 if (device
&& (device
->GetCurrentStatus() == CEC_DEVICE_STATUS_PRESENT
||
389 device
->GetCurrentStatus() == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
))
391 if (device
->GetCurrentPowerStatus() == CEC_POWER_STATUS_STANDBY
||
392 device
->GetCurrentPowerStatus() == CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY
)
393 device
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
395 return COMMAND_HANDLED
;
398 int CCECCommandHandler::HandleMenuRequest(const cec_command
&command
)
400 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
402 CCECBusDevice
*device
= GetDevice(command
.destination
);
405 CCECClient
*client
= device
->GetClient();
408 if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_ACTIVATE
)
410 if (client
->MenuStateChanged(CEC_MENU_STATE_ACTIVATED
) == 1)
411 device
->SetMenuState(CEC_MENU_STATE_ACTIVATED
);
413 else if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_DEACTIVATE
)
415 if (client
->MenuStateChanged(CEC_MENU_STATE_DEACTIVATED
) == 1)
416 device
->SetMenuState(CEC_MENU_STATE_DEACTIVATED
);
419 if (device
->TransmitMenuState(command
.initiator
, true))
420 return COMMAND_HANDLED
;
422 return CEC_ABORT_REASON_INVALID_OPERAND
;
425 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
428 bool CCECCommandHandler::HandlePoll(const cec_command
&command
)
430 m_busDevice
->HandlePoll(command
.destination
);
434 int CCECCommandHandler::HandleReportAudioStatus(const cec_command
&command
)
436 if (command
.parameters
.size
== 1)
438 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.initiator
));
441 device
->SetAudioStatus(command
.parameters
[0]);
442 return COMMAND_HANDLED
;
445 return CEC_ABORT_REASON_INVALID_OPERAND
;
448 int CCECCommandHandler::HandleReportPhysicalAddress(const cec_command
&command
)
450 if (command
.parameters
.size
== 3)
452 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
453 SetPhysicalAddress(command
.initiator
, iNewAddress
);
454 return COMMAND_HANDLED
;
456 return CEC_ABORT_REASON_INVALID_OPERAND
;
459 int CCECCommandHandler::HandleReportPowerStatus(const cec_command
&command
)
461 if (command
.parameters
.size
== 1)
463 CCECBusDevice
*device
= GetDevice(command
.initiator
);
466 device
->SetPowerStatus((cec_power_status
) command
.parameters
[0]);
467 return COMMAND_HANDLED
;
470 return CEC_ABORT_REASON_INVALID_OPERAND
;
473 int CCECCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
475 if (m_processor
->CECInitialised())
477 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, ">> %i requests active source", (uint8_t) command
.initiator
);
478 m_processor
->GetDevice(command
.initiator
)->SetPowerStatus(CEC_POWER_STATUS_ON
);
480 vector
<CCECBusDevice
*> devices
;
481 for (size_t iDevicePtr
= 0; iDevicePtr
< GetMyDevices(devices
); iDevicePtr
++)
482 devices
[iDevicePtr
]->TransmitActiveSource(true);
485 return COMMAND_HANDLED
;
488 int CCECCommandHandler::HandleRoutingChange(const cec_command
&command
)
490 if (command
.parameters
.size
== 4)
492 CCECBusDevice
*device
= GetDevice(command
.initiator
);
495 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[2] << 8) | ((uint16_t)command
.parameters
[3]);
496 device
->SetActiveRoute(iNewAddress
);
497 return COMMAND_HANDLED
;
501 return CEC_ABORT_REASON_INVALID_OPERAND
;
504 int CCECCommandHandler::HandleRoutingInformation(const cec_command
&command
)
506 if (command
.parameters
.size
== 2)
508 CCECBusDevice
*device
= GetDevice(command
.initiator
);
511 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
512 device
->SetActiveRoute(iNewAddress
);
513 return COMMAND_HANDLED
;
517 return CEC_ABORT_REASON_INVALID_OPERAND
;
520 int CCECCommandHandler::HandleSetMenuLanguage(const cec_command
&command
)
522 if (command
.parameters
.size
== 3)
524 CCECBusDevice
*device
= GetDevice(command
.initiator
);
527 cec_menu_language language
;
528 language
.device
= command
.initiator
;
529 for (uint8_t iPtr
= 0; iPtr
< 4; iPtr
++)
530 language
.language
[iPtr
] = command
.parameters
[iPtr
];
531 language
.language
[3] = 0;
532 device
->SetMenuLanguage(language
);
533 return COMMAND_HANDLED
;
537 return CEC_ABORT_REASON_INVALID_OPERAND
;
540 int CCECCommandHandler::HandleSetOSDName(const cec_command
&command
)
542 if (command
.parameters
.size
> 0)
544 CCECBusDevice
*device
= GetDevice(command
.initiator
);
548 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
549 buf
[iPtr
] = (char)command
.parameters
[iPtr
];
550 buf
[command
.parameters
.size
] = 0;
552 CStdString
strName(buf
);
553 device
->SetOSDName(strName
);
555 return COMMAND_HANDLED
;
559 return CEC_ABORT_REASON_INVALID_OPERAND
;
562 int CCECCommandHandler::HandleSetStreamPath(const cec_command
&command
)
564 if (!m_processor
->CECInitialised())
565 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
567 if (command
.parameters
.size
>= 2)
569 uint16_t iStreamAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
570 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, ">> %s (%x) sets stream path to physical address %04x", ToString(command
.initiator
), command
.initiator
, iStreamAddress
);
572 /* one of the device handled by libCEC has been made active */
573 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamAddress
);
574 if (device
&& device
->IsHandledByLibCEC())
576 device
->ActivateSource();
577 return COMMAND_HANDLED
;
581 return CEC_ABORT_REASON_INVALID_OPERAND
;
584 int CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command
&command
)
586 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
588 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.destination
));
591 if (command
.parameters
.size
>= 2)
593 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
594 device
->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON
);
595 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
596 CCECBusDevice
*newActiveDevice
= GetDeviceByPhysicalAddress(iNewAddress
);
598 newActiveDevice
->MarkAsActiveSource();
599 if (device
->TransmitSetSystemAudioMode(command
.initiator
, true))
600 return COMMAND_HANDLED
;
604 device
->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF
);
605 if (device
->TransmitSetSystemAudioMode(command
.initiator
, true))
606 return COMMAND_HANDLED
;
611 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
614 int CCECCommandHandler::HandleStandby(const cec_command
&command
)
616 CCECBusDevice
*device
= GetDevice(command
.initiator
);
618 device
->SetPowerStatus(CEC_POWER_STATUS_STANDBY
);
620 return COMMAND_HANDLED
;
623 int CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command
&command
)
625 if (command
.parameters
.size
== 1)
627 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.initiator
));
630 device
->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
631 return COMMAND_HANDLED
;
635 return CEC_ABORT_REASON_INVALID_OPERAND
;
638 int CCECCommandHandler::HandleSetSystemAudioMode(const cec_command
&command
)
640 if (command
.parameters
.size
== 1)
642 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.initiator
));
645 device
->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
646 return COMMAND_HANDLED
;
650 return CEC_ABORT_REASON_INVALID_OPERAND
;
653 int CCECCommandHandler::HandleTextViewOn(const cec_command
&command
)
655 m_processor
->GetDevice(command
.initiator
)->MarkAsActiveSource();
656 return COMMAND_HANDLED
;
659 int CCECCommandHandler::HandleUserControlPressed(const cec_command
&command
)
661 if (!m_processor
->CECInitialised() ||
662 !m_processor
->IsHandledByLibCEC(command
.destination
))
663 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
665 if (command
.parameters
.size
== 0)
666 return CEC_ABORT_REASON_INVALID_OPERAND
;
668 CCECBusDevice
*device
= GetDevice(command
.destination
);
670 return CEC_ABORT_REASON_INVALID_OPERAND
;
672 CCECClient
*client
= device
->GetClient();
674 client
->SetCurrentButton((cec_user_control_code
) command
.parameters
[0]);
676 if (command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER
||
677 command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION
||
678 command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER_TOGGLE_FUNCTION
)
682 // CEC_USER_CONTROL_CODE_POWER and CEC_USER_CONTROL_CODE_POWER_TOGGLE_FUNCTION operate as a toggle
683 // assume CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION does not
684 if (command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER
||
685 command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER_TOGGLE_FUNCTION
)
687 cec_power_status status
= device
->GetCurrentPowerStatus();
688 bPowerOn
= !(status
== CEC_POWER_STATUS_ON
|| status
== CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
693 device
->ActivateSource();
697 device
->MarkAsInactiveSource();
698 device
->TransmitInactiveSource();
699 device
->SetMenuState(CEC_MENU_STATE_DEACTIVATED
);
702 else if (command
.parameters
[0] != CEC_USER_CONTROL_CODE_POWER_OFF_FUNCTION
)
704 // we're not marked as active source, but the tv sends keypresses to us, so assume it forgot to activate us
705 if (!device
->IsActiveSource() && command
.initiator
== CECDEVICE_TV
)
706 device
->ActivateSource();
709 return COMMAND_HANDLED
;
712 int CCECCommandHandler::HandleUserControlRelease(const cec_command
&command
)
714 if (!m_processor
->CECInitialised() ||
715 !m_processor
->IsHandledByLibCEC(command
.destination
))
716 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
718 CCECClient
*client
= m_processor
->GetClient(command
.destination
);
722 return COMMAND_HANDLED
;
725 int CCECCommandHandler::HandleVendorCommand(const cec_command
& UNUSED(command
))
727 return CEC_ABORT_REASON_INVALID_OPERAND
;
730 int CCECCommandHandler::HandleVendorRemoteButtonDown(const cec_command
& command
)
732 if (command
.parameters
.size
== 0)
733 return CEC_ABORT_REASON_INVALID_OPERAND
;
735 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "unhandled vendor remote button received with keycode %x", command
.parameters
[0]);
736 return COMMAND_HANDLED
;
739 void CCECCommandHandler::UnhandledCommand(const cec_command
&command
, const cec_abort_reason reason
)
741 if (m_processor
->IsHandledByLibCEC(command
.destination
))
743 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "sending abort with opcode %02x and reason '%s' to %s", command
.opcode
, ToString(reason
), ToString(command
.initiator
));
744 m_processor
->TransmitAbort(command
.destination
, command
.initiator
, command
.opcode
, reason
);
748 size_t CCECCommandHandler::GetMyDevices(vector
<CCECBusDevice
*> &devices
) const
752 cec_logical_addresses addresses
= m_processor
->GetLogicalAddresses();
753 for (uint8_t iPtr
= CECDEVICE_TV
; iPtr
< CECDEVICE_BROADCAST
; iPtr
++)
757 devices
.push_back(GetDevice((cec_logical_address
) iPtr
));
765 CCECBusDevice
*CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress
) const
767 return m_processor
->GetDevice(iLogicalAddress
);
770 CCECBusDevice
*CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
) const
772 return m_processor
->GetDeviceByPhysicalAddress(iPhysicalAddress
);
775 bool CCECCommandHandler::SetVendorId(const cec_command
&command
)
777 bool bChanged(false);
778 if (command
.parameters
.size
< 3)
780 LIB_CEC
->AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
784 uint64_t iVendorId
= ((uint64_t)command
.parameters
[0] << 16) +
785 ((uint64_t)command
.parameters
[1] << 8) +
786 (uint64_t)command
.parameters
[2];
788 CCECBusDevice
*device
= GetDevice((cec_logical_address
) command
.initiator
);
790 bChanged
= device
->SetVendorId(iVendorId
);
794 void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress
, uint16_t iNewAddress
)
796 if (!m_processor
->IsHandledByLibCEC(iAddress
))
798 CCECBusDevice
*otherDevice
= m_processor
->GetDeviceByPhysicalAddress(iNewAddress
);
799 CCECClient
*client
= otherDevice
? otherDevice
->GetClient() : NULL
;
801 CCECBusDevice
*device
= m_processor
->GetDevice(iAddress
);
803 device
->SetPhysicalAddress(iNewAddress
);
806 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "device with logical address %X not found", iAddress
);
809 /* another device reported the same physical address as ours */
811 client
->ResetPhysicalAddress();
815 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
);
819 bool CCECCommandHandler::PowerOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
821 if (iDestination
== CECDEVICE_TV
)
822 return TransmitImageViewOn(iInitiator
, iDestination
);
824 return TransmitKeypress(iInitiator
, iDestination
, CEC_USER_CONTROL_CODE_POWER
) &&
825 TransmitKeyRelease(iInitiator
, iDestination
);
828 bool CCECCommandHandler::TransmitImageViewOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
831 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_IMAGE_VIEW_ON
);
833 if (Transmit(command
, false, false))
835 CCECBusDevice
* dest
= m_processor
->GetDevice(iDestination
);
836 if (dest
&& dest
->GetCurrentPowerStatus() != CEC_POWER_STATUS_ON
)
837 dest
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
843 bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
846 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_STANDBY
);
848 return Transmit(command
, false, false);
851 bool CCECCommandHandler::TransmitRequestActiveSource(const cec_logical_address iInitiator
, bool bWaitForResponse
/* = true */)
854 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_REQUEST_ACTIVE_SOURCE
);
856 return Transmit(command
, !bWaitForResponse
, false);
859 bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
862 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_CEC_VERSION
);
864 return Transmit(command
, !bWaitForResponse
, false);
867 bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
870 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_MENU_LANGUAGE
);
872 return Transmit(command
, !bWaitForResponse
, false);
875 bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
878 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_OSD_NAME
);
880 return Transmit(command
, !bWaitForResponse
, false);
883 bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
886 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
);
888 return Transmit(command
, !bWaitForResponse
, false);
891 bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
893 if (iDestination
== CECDEVICE_TV
)
895 int64_t now(GetTimeMs());
896 if (now
- m_iPowerStatusRequested
< REQUEST_POWER_STATUS_TIMEOUT
)
898 m_iPowerStatusRequested
= now
;
901 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "<< requesting power status of '%s' (%X)", m_busDevice
->GetLogicalAddressName(), iDestination
);
904 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
);
906 return Transmit(command
, !bWaitForResponse
, false);
909 bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
912 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
914 return Transmit(command
, !bWaitForResponse
, false);
917 bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
, bool bIsReply
)
920 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
921 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
922 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
924 return Transmit(command
, false, bIsReply
);
927 bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_version cecVersion
, bool bIsReply
)
930 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_CEC_VERSION
);
931 command
.parameters
.PushBack((uint8_t)cecVersion
);
933 return Transmit(command
, false, bIsReply
);
936 bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
939 cec_command::Format(command
, iInitiator
, CECDEVICE_TV
, CEC_OPCODE_INACTIVE_SOURCE
);
940 command
.parameters
.PushBack((iPhysicalAddress
>> 8) & 0xFF);
941 command
.parameters
.PushBack(iPhysicalAddress
& 0xFF);
943 return Transmit(command
, false, false);
946 bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_menu_state menuState
, bool bIsReply
)
949 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_MENU_STATUS
);
950 command
.parameters
.PushBack((uint8_t)menuState
);
952 return Transmit(command
, false, bIsReply
);
955 bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, std::string strDeviceName
, bool bIsReply
)
958 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_NAME
);
959 for (size_t iPtr
= 0; iPtr
< strDeviceName
.length(); iPtr
++)
960 command
.parameters
.PushBack(strDeviceName
.at(iPtr
));
962 return Transmit(command
, false, bIsReply
);
965 bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_display_control duration
, const char *strMessage
, bool bIsReply
)
968 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_STRING
);
969 command
.parameters
.PushBack((uint8_t)duration
);
971 size_t iLen
= strlen(strMessage
);
972 if (iLen
> 13) iLen
= 13;
974 for (size_t iPtr
= 0; iPtr
< iLen
; iPtr
++)
975 command
.parameters
.PushBack(strMessage
[iPtr
]);
977 return Transmit(command
, false, bIsReply
);
980 bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
, cec_device_type type
, bool bIsReply
)
983 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
984 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
985 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
986 command
.parameters
.PushBack((uint8_t) (type
));
988 return Transmit(command
, false, bIsReply
);
991 bool CCECCommandHandler::TransmitSetMenuLanguage(const cec_logical_address iInitiator
, const char lang
[3], bool bIsReply
)
994 command
.Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_SET_MENU_LANGUAGE
);
995 command
.parameters
.PushBack((uint8_t) lang
[0]);
996 command
.parameters
.PushBack((uint8_t) lang
[1]);
997 command
.parameters
.PushBack((uint8_t) lang
[2]);
999 return Transmit(command
, false, bIsReply
);
1002 bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bIsReply
)
1004 cec_command command
;
1005 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_NONE
);
1007 return Transmit(command
, false, bIsReply
);
1010 bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_power_status state
, bool bIsReply
)
1012 cec_command command
;
1013 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_POWER_STATUS
);
1014 command
.parameters
.PushBack((uint8_t) state
);
1016 return Transmit(command
, false, bIsReply
);
1019 bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator
, uint64_t iVendorId
, bool bIsReply
)
1021 cec_command command
;
1022 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_DEVICE_VENDOR_ID
);
1024 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 16) & 0xFF));
1025 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 8) & 0xFF));
1026 command
.parameters
.PushBack((uint8_t) ((uint64_t)iVendorId
& 0xFF));
1028 return Transmit(command
, false, bIsReply
);
1031 bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, uint8_t state
, bool bIsReply
)
1033 cec_command command
;
1034 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_AUDIO_STATUS
);
1035 command
.parameters
.PushBack(state
);
1037 return Transmit(command
, false, bIsReply
);
1040 bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
, bool bIsReply
)
1042 cec_command command
;
1043 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
);
1044 command
.parameters
.PushBack((uint8_t)state
);
1046 return Transmit(command
, false, bIsReply
);
1049 bool CCECCommandHandler::TransmitSetStreamPath(uint16_t iStreamPath
, bool bIsReply
)
1051 if (m_busDevice
->GetLogicalAddress() != CECDEVICE_TV
)
1053 LIB_CEC
->AddLog(CEC_LOG_ERROR
, "only the TV is allowed to send CEC_OPCODE_SET_STREAM_PATH");
1056 cec_command command
;
1057 cec_command::Format(command
, m_busDevice
->GetLogicalAddress(), CECDEVICE_BROADCAST
, CEC_OPCODE_SET_STREAM_PATH
);
1058 command
.parameters
.PushBack((uint8_t) ((iStreamPath
>> 8) & 0xFF));
1059 command
.parameters
.PushBack((uint8_t) (iStreamPath
& 0xFF));
1061 return Transmit(command
, false, bIsReply
);
1064 bool CCECCommandHandler::TransmitSystemAudioModeStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
, bool bIsReply
)
1066 cec_command command
;
1067 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
);
1068 command
.parameters
.PushBack((uint8_t)state
);
1070 return Transmit(command
, false, bIsReply
);
1073 bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_deck_info state
, bool bIsReply
)
1075 cec_command command
;
1076 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_DECK_STATUS
);
1077 command
.PushBack((uint8_t)state
);
1079 return Transmit(command
, false, bIsReply
);
1082 bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = true */)
1084 cec_command command
;
1085 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_PRESSED
);
1086 command
.parameters
.PushBack((uint8_t)key
);
1088 return Transmit(command
, !bWait
, false);
1091 bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWait
/* = true */)
1093 cec_command command
;
1094 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_RELEASE
);
1096 return Transmit(command
, !bWait
, false);
1099 bool CCECCommandHandler::Transmit(cec_command
&command
, bool bSuppressWait
, bool bIsReply
)
1101 bool bReturn(false);
1102 cec_opcode
expectedResponse(cec_command::GetResponseOpcode(command
.opcode
));
1103 bool bExpectResponse(expectedResponse
!= CEC_OPCODE_NONE
&& !bSuppressWait
);
1104 command
.transmit_timeout
= m_iTransmitTimeout
;
1106 if (command
.initiator
== CECDEVICE_UNKNOWN
)
1108 LIB_CEC
->AddLog(CEC_LOG_ERROR
, "not transmitting a command without a valid initiator");
1112 // check whether the destination is not marked as not present or handled by libCEC
1113 if (command
.destination
!= CECDEVICE_BROADCAST
&& command
.opcode_set
)
1115 CCECBusDevice
* destinationDevice
= m_processor
->GetDevice(command
.destination
);
1116 cec_bus_device_status status
= destinationDevice
? destinationDevice
->GetStatus() : CEC_DEVICE_STATUS_NOT_PRESENT
;
1117 if (status
== CEC_DEVICE_STATUS_NOT_PRESENT
)
1119 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "not sending command '%s': destination device '%s' marked as not present", ToString(command
.opcode
),ToString(command
.destination
));
1122 else if (status
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
)
1124 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "not sending command '%s': destination device '%s' marked as handled by libCEC", ToString(command
.opcode
),ToString(command
.destination
));
1130 uint8_t iTries(0), iMaxTries(!command
.opcode_set
? 1 : m_iTransmitRetries
+ 1);
1131 while (!bReturn
&& ++iTries
<= iMaxTries
&& !m_busDevice
->IsUnsupportedFeature(command
.opcode
))
1133 if ((bReturn
= m_processor
->Transmit(command
, bIsReply
)) == true)
1135 #ifdef CEC_DEBUGGING
1136 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "command transmitted");
1138 if (bExpectResponse
)
1140 bReturn
= m_busDevice
->WaitForOpcode(expectedResponse
);
1141 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, bReturn
? "expected response received (%X: %s)" : "expected response not received (%X: %s)", (int)expectedResponse
, ToString(expectedResponse
));
1150 bool CCECCommandHandler::ActivateSource(bool bTransmitDelayedCommandsOnly
/* = false */)
1152 if (m_busDevice
->IsActiveSource() &&
1153 m_busDevice
->IsHandledByLibCEC())
1156 CLockObject
lock(m_mutex
);
1157 // check if we need to send a delayed source switch
1158 if (bTransmitDelayedCommandsOnly
)
1160 if (m_iActiveSourcePending
== 0 || GetTimeMs() < m_iActiveSourcePending
)
1163 #ifdef CEC_DEBUGGING
1164 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "transmitting delayed activate source command");
1169 // update the power state and menu state
1170 if (!bTransmitDelayedCommandsOnly
)
1172 m_busDevice
->SetPowerStatus(CEC_POWER_STATUS_ON
);
1173 m_busDevice
->SetMenuState(CEC_MENU_STATE_ACTIVATED
);
1176 // vendor specific hook
1177 VendorPreActivateSourceHook();
1180 CCECBusDevice
* tv
= m_processor
->GetDevice(CECDEVICE_TV
);
1181 bool bTvPresent
= (tv
&& tv
->GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
1182 bool bActiveSourceFailed(false);
1185 if (tv
->GetCurrentPowerStatus() != CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
)
1186 bActiveSourceFailed
= !m_busDevice
->TransmitImageViewOn();
1189 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "TV not present, not sending 'image view on'");
1191 // check if we're allowed to switch sources
1192 bool bSourceSwitchAllowed
= SourceSwitchAllowed();
1193 if (!bSourceSwitchAllowed
)
1194 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "source switch is currently not allowed by command handler");
1196 // switch sources (if allowed)
1197 if (!bActiveSourceFailed
&& bSourceSwitchAllowed
)
1199 bActiveSourceFailed
= !m_busDevice
->TransmitActiveSource(false);
1200 if (bTvPresent
&& !bActiveSourceFailed
)
1201 bActiveSourceFailed
= !m_busDevice
->TransmitMenuState(CECDEVICE_TV
, false);
1203 // update the deck status for playback devices
1204 if (bTvPresent
&& !bActiveSourceFailed
)
1206 CCECPlaybackDevice
*playbackDevice
= m_busDevice
->AsPlaybackDevice();
1207 if (playbackDevice
&& SendDeckStatusUpdateOnActiveSource())
1208 bActiveSourceFailed
= !playbackDevice
->TransmitDeckStatus(CECDEVICE_TV
, false);
1213 if (bActiveSourceFailed
|| !bSourceSwitchAllowed
)
1215 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "failed to make '%s' the active source. will retry later", m_busDevice
->GetLogicalAddressName());
1216 int64_t now(GetTimeMs());
1217 CLockObject
lock(m_mutex
);
1218 if (m_iActiveSourcePending
== 0 || m_iActiveSourcePending
< now
)
1219 m_iActiveSourcePending
= now
+ (int64_t)CEC_ACTIVE_SOURCE_SWITCH_RETRY_TIME_MS
;
1224 CLockObject
lock(m_mutex
);
1225 // clear previous pending active source command
1226 m_iActiveSourcePending
= 0;
1229 // mark the handler as initialised
1230 CLockObject
lock(m_mutex
);
1231 m_bHandlerInited
= true;
1236 void CCECCommandHandler::ScheduleActivateSource(uint64_t iDelay
)
1238 CLockObject
lock(m_mutex
);
1239 m_iActiveSourcePending
= GetTimeMs() + iDelay
;