2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
9 * This program is dual-licensed; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
27 * For more information contact:
28 * Pulse-Eight Licensing <license@pulse-eight.com>
29 * http://www.pulse-eight.com/
30 * http://www.pulse-eight.net/
33 #include "CECCommandHandler.h"
34 #include "../devices/CECBusDevice.h"
35 #include "../devices/CECAudioSystem.h"
36 #include "../devices/CECPlaybackDevice.h"
37 #include "../CECClient.h"
38 #include "../CECProcessor.h"
39 #include "../LibCEC.h"
40 #include "../CECTypeUtils.h"
41 #include "../platform/util/util.h"
45 using namespace PLATFORM
;
47 #define LIB_CEC m_busDevice->GetProcessor()->GetLib()
48 #define ToString(p) CCECTypeUtils::ToString(p)
50 CCECCommandHandler::CCECCommandHandler(CCECBusDevice
*busDevice
) :
51 m_busDevice(busDevice
),
52 m_processor(m_busDevice
->GetProcessor()),
53 m_iTransmitTimeout(CEC_DEFAULT_TRANSMIT_TIMEOUT
),
54 m_iTransmitWait(CEC_DEFAULT_TRANSMIT_WAIT
),
55 m_iTransmitRetries(CEC_DEFAULT_TRANSMIT_RETRIES
),
56 m_bHandlerInited(false),
57 m_bOPTSendDeckStatusUpdateOnActiveSource(false),
58 m_vendorId(CEC_VENDOR_UNKNOWN
),
59 m_waitForResponse(new CWaitForResponse
),
60 m_iActiveSourcePending(0)
64 CCECCommandHandler::~CCECCommandHandler(void)
66 DELETE_AND_NULL(m_waitForResponse
);
69 bool CCECCommandHandler::HandleCommand(const cec_command
&command
)
71 if (command
.opcode_set
== 0)
72 return HandlePoll(command
);
74 int iHandled(CEC_ABORT_REASON_UNRECOGNIZED_OPCODE
);
76 LIB_CEC
->AddCommand(command
);
78 switch(command
.opcode
)
80 case CEC_OPCODE_REPORT_POWER_STATUS
:
81 iHandled
= HandleReportPowerStatus(command
);
83 case CEC_OPCODE_CEC_VERSION
:
84 iHandled
= HandleDeviceCecVersion(command
);
86 case CEC_OPCODE_SET_MENU_LANGUAGE
:
87 iHandled
= HandleSetMenuLanguage(command
);
89 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
90 iHandled
= HandleGivePhysicalAddress(command
);
92 case CEC_OPCODE_GET_MENU_LANGUAGE
:
93 iHandled
= HandleGiveMenuLanguage(command
);
95 case CEC_OPCODE_GIVE_OSD_NAME
:
96 iHandled
= HandleGiveOSDName(command
);
98 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
99 iHandled
= HandleGiveDeviceVendorId(command
);
101 case CEC_OPCODE_DEVICE_VENDOR_ID
:
102 iHandled
= HandleDeviceVendorId(command
);
104 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
105 iHandled
= HandleDeviceVendorCommandWithId(command
);
107 case CEC_OPCODE_GIVE_DECK_STATUS
:
108 iHandled
= HandleGiveDeckStatus(command
);
110 case CEC_OPCODE_DECK_CONTROL
:
111 iHandled
= HandleDeckControl(command
);
113 case CEC_OPCODE_MENU_REQUEST
:
114 iHandled
= HandleMenuRequest(command
);
116 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
117 iHandled
= HandleGiveDevicePowerStatus(command
);
119 case CEC_OPCODE_GET_CEC_VERSION
:
120 iHandled
= HandleGetCecVersion(command
);
122 case CEC_OPCODE_USER_CONTROL_PRESSED
:
123 iHandled
= HandleUserControlPressed(command
);
125 case CEC_OPCODE_USER_CONTROL_RELEASE
:
126 iHandled
= HandleUserControlRelease(command
);
128 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
129 iHandled
= HandleGiveAudioStatus(command
);
131 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
132 iHandled
= HandleGiveSystemAudioModeStatus(command
);
134 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
135 iHandled
= HandleSystemAudioModeRequest(command
);
137 case CEC_OPCODE_REPORT_AUDIO_STATUS
:
138 iHandled
= HandleReportAudioStatus(command
);
140 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
:
141 iHandled
= HandleSystemAudioModeStatus(command
);
143 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
:
144 iHandled
= HandleSetSystemAudioMode(command
);
146 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
147 iHandled
= HandleRequestActiveSource(command
);
149 case CEC_OPCODE_SET_STREAM_PATH
:
150 iHandled
= HandleSetStreamPath(command
);
152 case CEC_OPCODE_ROUTING_CHANGE
:
153 iHandled
= HandleRoutingChange(command
);
155 case CEC_OPCODE_ROUTING_INFORMATION
:
156 iHandled
= HandleRoutingInformation(command
);
158 case CEC_OPCODE_STANDBY
:
159 iHandled
= HandleStandby(command
);
161 case CEC_OPCODE_ACTIVE_SOURCE
:
162 iHandled
= HandleActiveSource(command
);
164 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
:
165 iHandled
= HandleReportPhysicalAddress(command
);
167 case CEC_OPCODE_SET_OSD_NAME
:
168 iHandled
= HandleSetOSDName(command
);
170 case CEC_OPCODE_IMAGE_VIEW_ON
:
171 iHandled
= HandleImageViewOn(command
);
173 case CEC_OPCODE_TEXT_VIEW_ON
:
174 iHandled
= HandleTextViewOn(command
);
176 case CEC_OPCODE_FEATURE_ABORT
:
177 iHandled
= HandleFeatureAbort(command
);
179 case CEC_OPCODE_VENDOR_COMMAND
:
180 iHandled
= HandleVendorCommand(command
);
182 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN
:
183 iHandled
= HandleVendorRemoteButtonDown(command
);
185 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP
:
186 iHandled
= HandleVendorRemoteButtonUp(command
);
188 case CEC_OPCODE_PLAY
:
189 // libCEC (currently) doesn't need to do anything with this, since player applications handle it
190 // but it should not respond with a feature abort
191 iHandled
= COMMAND_HANDLED
;
197 if (iHandled
== COMMAND_HANDLED
)
198 m_waitForResponse
->Received((command
.opcode
== CEC_OPCODE_FEATURE_ABORT
&& command
.parameters
.size
> 0) ? (cec_opcode
)command
.parameters
[0] : command
.opcode
);
200 UnhandledCommand(command
, (cec_abort_reason
)iHandled
);
202 return iHandled
== COMMAND_HANDLED
;
205 int CCECCommandHandler::HandleActiveSource(const cec_command
&command
)
207 if (command
.parameters
.size
== 2)
209 uint16_t iAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
210 CCECBusDevice
*device
= m_processor
->GetDeviceByPhysicalAddress(iAddress
);
213 device
->MarkAsActiveSource();
214 return COMMAND_HANDLED
;
218 return CEC_ABORT_REASON_INVALID_OPERAND
;
221 int CCECCommandHandler::HandleDeckControl(const cec_command
&command
)
223 CCECPlaybackDevice
*device
= CCECBusDevice::AsPlaybackDevice(GetDevice(command
.destination
));
224 if (device
&& command
.parameters
.size
> 0)
226 device
->SetDeckControlMode((cec_deck_control_mode
) command
.parameters
[0]);
227 return COMMAND_HANDLED
;
230 return CEC_ABORT_REASON_INVALID_OPERAND
;
233 int CCECCommandHandler::HandleDeviceCecVersion(const cec_command
&command
)
235 if (command
.parameters
.size
== 1)
237 CCECBusDevice
*device
= GetDevice(command
.initiator
);
239 device
->SetCecVersion((cec_version
) command
.parameters
[0]);
241 return COMMAND_HANDLED
;
244 return CEC_ABORT_REASON_INVALID_OPERAND
;
247 int CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command
& UNUSED(command
))
249 return CEC_ABORT_REASON_INVALID_OPERAND
;
252 int CCECCommandHandler::HandleDeviceVendorId(const cec_command
&command
)
254 SetVendorId(command
);
255 return COMMAND_HANDLED
;
258 int CCECCommandHandler::HandleFeatureAbort(const cec_command
&command
)
260 if (command
.parameters
.size
== 2 &&
261 (command
.parameters
[1] == CEC_ABORT_REASON_UNRECOGNIZED_OPCODE
||
262 command
.parameters
[1] == CEC_ABORT_REASON_REFUSED
))
263 m_processor
->GetDevice(command
.initiator
)->SetUnsupportedFeature((cec_opcode
)command
.parameters
[0]);
264 return COMMAND_HANDLED
;
267 int CCECCommandHandler::HandleGetCecVersion(const cec_command
&command
)
269 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
271 CCECBusDevice
*device
= GetDevice(command
.destination
);
272 if (device
&& device
->TransmitCECVersion(command
.initiator
))
273 return COMMAND_HANDLED
;
274 return CEC_ABORT_REASON_INVALID_OPERAND
;
277 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
280 int CCECCommandHandler::HandleGiveAudioStatus(const cec_command
&command
)
282 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
284 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.destination
));
285 if (device
&& device
->TransmitAudioStatus(command
.initiator
))
286 return COMMAND_HANDLED
;
287 return CEC_ABORT_REASON_INVALID_OPERAND
;
290 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
293 int CCECCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
295 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
297 CCECPlaybackDevice
*device
= CCECBusDevice::AsPlaybackDevice(GetDevice(command
.destination
));
298 if (device
&& device
->TransmitDeckStatus(command
.initiator
))
299 return COMMAND_HANDLED
;
300 return CEC_ABORT_REASON_INVALID_OPERAND
;
303 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
306 int CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command
&command
)
308 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
310 CCECBusDevice
*device
= GetDevice(command
.destination
);
311 if (device
&& device
->TransmitPowerState(command
.initiator
))
312 return COMMAND_HANDLED
;
313 return CEC_ABORT_REASON_INVALID_OPERAND
;
316 return CEC_ABORT_REASON_INVALID_OPERAND
;
319 int CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command
&command
)
321 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
323 CCECBusDevice
*device
= GetDevice(command
.destination
);
324 if (device
&& device
->TransmitVendorID(command
.initiator
))
325 return COMMAND_HANDLED
;
328 return CEC_ABORT_REASON_INVALID_OPERAND
;
331 int CCECCommandHandler::HandleGiveOSDName(const cec_command
&command
)
333 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
335 CCECBusDevice
*device
= GetDevice(command
.destination
);
336 if (device
&& device
->TransmitOSDName(command
.initiator
))
337 return COMMAND_HANDLED
;
340 return CEC_ABORT_REASON_INVALID_OPERAND
;
343 int CCECCommandHandler::HandleGivePhysicalAddress(const cec_command
&command
)
345 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
347 CCECBusDevice
*device
= GetDevice(command
.destination
);
348 if (device
&& device
->TransmitPhysicalAddress())
349 return COMMAND_HANDLED
;
350 return CEC_ABORT_REASON_INVALID_OPERAND
;
353 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
356 int CCECCommandHandler::HandleGiveMenuLanguage(const cec_command
&command
)
358 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
360 CCECBusDevice
*device
= GetDevice(command
.destination
);
361 if (device
&& device
->TransmitSetMenuLanguage(command
.initiator
))
362 return COMMAND_HANDLED
;
363 return CEC_ABORT_REASON_INVALID_OPERAND
;
366 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
369 int CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command
&command
)
371 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
373 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.destination
));
374 if (device
&& device
->TransmitSystemAudioModeStatus(command
.initiator
))
375 return COMMAND_HANDLED
;
376 return CEC_ABORT_REASON_INVALID_OPERAND
;
379 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
382 int CCECCommandHandler::HandleImageViewOn(const cec_command
&command
)
384 m_processor
->GetDevice(command
.initiator
)->MarkAsActiveSource();
385 return COMMAND_HANDLED
;
388 int CCECCommandHandler::HandleMenuRequest(const cec_command
&command
)
390 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
392 CCECBusDevice
*device
= GetDevice(command
.destination
);
395 CCECClient
*client
= device
->GetClient();
398 if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_ACTIVATE
)
400 if (client
->MenuStateChanged(CEC_MENU_STATE_ACTIVATED
) == 1)
401 device
->SetMenuState(CEC_MENU_STATE_ACTIVATED
);
403 else if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_DEACTIVATE
)
405 if (client
->MenuStateChanged(CEC_MENU_STATE_DEACTIVATED
) == 1)
406 device
->SetMenuState(CEC_MENU_STATE_DEACTIVATED
);
409 if (device
->TransmitMenuState(command
.initiator
))
410 return COMMAND_HANDLED
;
412 return CEC_ABORT_REASON_INVALID_OPERAND
;
415 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
418 bool CCECCommandHandler::HandlePoll(const cec_command
&command
)
420 m_busDevice
->HandlePoll(command
.destination
);
424 int CCECCommandHandler::HandleReportAudioStatus(const cec_command
&command
)
426 if (command
.parameters
.size
== 1)
428 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.initiator
));
431 device
->SetAudioStatus(command
.parameters
[0]);
432 return COMMAND_HANDLED
;
435 return CEC_ABORT_REASON_INVALID_OPERAND
;
438 int CCECCommandHandler::HandleReportPhysicalAddress(const cec_command
&command
)
440 if (command
.parameters
.size
== 3)
442 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
443 SetPhysicalAddress(command
.initiator
, iNewAddress
);
444 return COMMAND_HANDLED
;
446 return CEC_ABORT_REASON_INVALID_OPERAND
;
449 int CCECCommandHandler::HandleReportPowerStatus(const cec_command
&command
)
451 if (command
.parameters
.size
== 1)
453 CCECBusDevice
*device
= GetDevice(command
.initiator
);
456 device
->SetPowerStatus((cec_power_status
) command
.parameters
[0]);
457 return COMMAND_HANDLED
;
460 return CEC_ABORT_REASON_INVALID_OPERAND
;
463 int CCECCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
465 if (m_processor
->CECInitialised())
467 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, ">> %i requests active source", (uint8_t) command
.initiator
);
468 m_processor
->GetDevice(command
.initiator
)->SetPowerStatus(CEC_POWER_STATUS_ON
);
470 vector
<CCECBusDevice
*> devices
;
471 for (size_t iDevicePtr
= 0; iDevicePtr
< GetMyDevices(devices
); iDevicePtr
++)
472 devices
[iDevicePtr
]->TransmitActiveSource();
475 return COMMAND_HANDLED
;
478 int CCECCommandHandler::HandleRoutingChange(const cec_command
&command
)
480 if (command
.parameters
.size
== 4)
482 uint16_t iOldAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
483 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[2] << 8) | ((uint16_t)command
.parameters
[3]);
485 CCECBusDevice
*device
= GetDevice(command
.initiator
);
488 device
->SetStreamPath(iNewAddress
, iOldAddress
);
489 return COMMAND_HANDLED
;
493 return CEC_ABORT_REASON_INVALID_OPERAND
;
496 int CCECCommandHandler::HandleRoutingInformation(const cec_command
&command
)
498 if (command
.parameters
.size
== 2)
500 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
501 CCECBusDevice
*device
= m_processor
->GetDeviceByPhysicalAddress(iNewAddress
);
504 device
->MarkAsActiveSource();
505 return COMMAND_HANDLED
;
509 return CEC_ABORT_REASON_INVALID_OPERAND
;
512 int CCECCommandHandler::HandleSetMenuLanguage(const cec_command
&command
)
514 if (command
.parameters
.size
== 3)
516 CCECBusDevice
*device
= GetDevice(command
.initiator
);
519 cec_menu_language language
;
520 language
.device
= command
.initiator
;
521 for (uint8_t iPtr
= 0; iPtr
< 4; iPtr
++)
522 language
.language
[iPtr
] = command
.parameters
[iPtr
];
523 language
.language
[3] = 0;
524 device
->SetMenuLanguage(language
);
525 return COMMAND_HANDLED
;
529 return CEC_ABORT_REASON_INVALID_OPERAND
;
532 int CCECCommandHandler::HandleSetOSDName(const cec_command
&command
)
534 if (command
.parameters
.size
> 0)
536 CCECBusDevice
*device
= GetDevice(command
.initiator
);
540 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
541 buf
[iPtr
] = (char)command
.parameters
[iPtr
];
542 buf
[command
.parameters
.size
] = 0;
544 CStdString
strName(buf
);
545 device
->SetOSDName(strName
);
547 return COMMAND_HANDLED
;
551 return CEC_ABORT_REASON_INVALID_OPERAND
;
554 int CCECCommandHandler::HandleSetStreamPath(const cec_command
&command
)
556 if (!m_processor
->CECInitialised())
557 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
559 if (command
.parameters
.size
>= 2)
561 uint16_t iStreamAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
562 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, ">> %s (%x) sets stream path to physical address %04x", ToString(command
.initiator
), command
.initiator
, iStreamAddress
);
564 // a device will only change the stream path when it's powered on
565 m_busDevice
->SetPowerStatus(CEC_POWER_STATUS_ON
);
567 /* one of the device handled by libCEC has been made active */
568 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamAddress
);
569 if (device
&& device
->IsHandledByLibCEC())
571 device
->ActivateSource();
572 return COMMAND_HANDLED
;
576 return CEC_ABORT_REASON_INVALID_OPERAND
;
579 int CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command
&command
)
581 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
583 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.destination
));
586 if (command
.parameters
.size
>= 2)
588 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
589 device
->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON
);
590 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
591 CCECBusDevice
*newActiveDevice
= GetDeviceByPhysicalAddress(iNewAddress
);
593 newActiveDevice
->MarkAsActiveSource();
594 if (device
->TransmitSetSystemAudioMode(command
.initiator
))
595 return COMMAND_HANDLED
;
599 device
->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF
);
600 if (device
->TransmitSetSystemAudioMode(command
.initiator
))
601 return COMMAND_HANDLED
;
606 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
609 int CCECCommandHandler::HandleStandby(const cec_command
&command
)
611 CCECBusDevice
*device
= GetDevice(command
.initiator
);
613 device
->SetPowerStatus(CEC_POWER_STATUS_STANDBY
);
615 return COMMAND_HANDLED
;
618 int CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command
&command
)
620 if (command
.parameters
.size
== 1)
622 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.initiator
));
625 device
->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
626 return COMMAND_HANDLED
;
630 return CEC_ABORT_REASON_INVALID_OPERAND
;
633 int CCECCommandHandler::HandleSetSystemAudioMode(const cec_command
&command
)
635 if (command
.parameters
.size
== 1)
637 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.initiator
));
640 device
->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
641 return COMMAND_HANDLED
;
645 return CEC_ABORT_REASON_INVALID_OPERAND
;
648 int CCECCommandHandler::HandleTextViewOn(const cec_command
&command
)
650 m_processor
->GetDevice(command
.initiator
)->MarkAsActiveSource();
651 return COMMAND_HANDLED
;
654 int CCECCommandHandler::HandleUserControlPressed(const cec_command
&command
)
656 if (!m_processor
->CECInitialised() ||
657 !m_processor
->IsHandledByLibCEC(command
.destination
))
658 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
660 if (command
.parameters
.size
== 0)
661 return CEC_ABORT_REASON_INVALID_OPERAND
;
663 CCECBusDevice
*device
= GetDevice(command
.destination
);
665 return CEC_ABORT_REASON_INVALID_OPERAND
;
667 CCECClient
*client
= device
->GetClient();
671 if (command
.parameters
[0] <= CEC_USER_CONTROL_CODE_MAX
)
672 client
->SetCurrentButton((cec_user_control_code
) command
.parameters
[0]);
674 if (command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER
||
675 command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION
)
679 // CEC_USER_CONTROL_CODE_POWER operates as a toggle
680 // assume CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION does not
681 if (command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER
)
683 cec_power_status status
= device
->GetCurrentPowerStatus();
684 bPowerOn
= !(status
== CEC_POWER_STATUS_ON
|| status
== CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
689 device
->ActivateSource();
693 device
->MarkAsInactiveSource();
694 device
->TransmitInactiveSource();
695 device
->SetMenuState(CEC_MENU_STATE_DEACTIVATED
);
699 return COMMAND_HANDLED
;
702 int CCECCommandHandler::HandleUserControlRelease(const cec_command
&command
)
704 if (!m_processor
->CECInitialised() ||
705 !m_processor
->IsHandledByLibCEC(command
.destination
))
706 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
708 CCECClient
*client
= m_processor
->GetClient(command
.destination
);
712 return COMMAND_HANDLED
;
715 int CCECCommandHandler::HandleVendorCommand(const cec_command
& UNUSED(command
))
717 return CEC_ABORT_REASON_INVALID_OPERAND
;
720 void CCECCommandHandler::UnhandledCommand(const cec_command
&command
, const cec_abort_reason reason
)
722 if (m_processor
->IsHandledByLibCEC(command
.destination
))
724 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "sending abort with opcode %02x and reason '%s' to %s", command
.opcode
, ToString(reason
), ToString(command
.initiator
));
725 m_processor
->TransmitAbort(command
.destination
, command
.initiator
, command
.opcode
, reason
);
729 size_t CCECCommandHandler::GetMyDevices(vector
<CCECBusDevice
*> &devices
) const
733 cec_logical_addresses addresses
= m_processor
->GetLogicalAddresses();
734 for (uint8_t iPtr
= CECDEVICE_TV
; iPtr
< CECDEVICE_BROADCAST
; iPtr
++)
738 devices
.push_back(GetDevice((cec_logical_address
) iPtr
));
746 CCECBusDevice
*CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress
) const
748 return m_processor
->GetDevice(iLogicalAddress
);
751 CCECBusDevice
*CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
) const
753 return m_processor
->GetDeviceByPhysicalAddress(iPhysicalAddress
);
756 bool CCECCommandHandler::SetVendorId(const cec_command
&command
)
758 bool bChanged(false);
759 if (command
.parameters
.size
< 3)
761 LIB_CEC
->AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
765 uint64_t iVendorId
= ((uint64_t)command
.parameters
[0] << 16) +
766 ((uint64_t)command
.parameters
[1] << 8) +
767 (uint64_t)command
.parameters
[2];
769 CCECBusDevice
*device
= GetDevice((cec_logical_address
) command
.initiator
);
771 bChanged
= device
->SetVendorId(iVendorId
);
775 void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress
, uint16_t iNewAddress
)
777 if (!m_processor
->IsHandledByLibCEC(iAddress
))
779 CCECBusDevice
*otherDevice
= m_processor
->GetDeviceByPhysicalAddress(iNewAddress
);
780 CCECClient
*client
= otherDevice
? otherDevice
->GetClient() : NULL
;
782 CCECBusDevice
*device
= m_processor
->GetDevice(iAddress
);
784 device
->SetPhysicalAddress(iNewAddress
);
787 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "device with logical address %X not found", iAddress
);
790 /* another device reported the same physical address as ours */
792 client
->ResetPhysicalAddress();
796 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
);
800 bool CCECCommandHandler::PowerOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
802 if (iDestination
== CECDEVICE_TV
)
803 return TransmitImageViewOn(iInitiator
, iDestination
);
805 return TransmitKeypress(iInitiator
, iDestination
, CEC_USER_CONTROL_CODE_POWER
) &&
806 TransmitKeyRelease(iInitiator
, iDestination
);
809 bool CCECCommandHandler::TransmitImageViewOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
812 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_IMAGE_VIEW_ON
);
814 return Transmit(command
);
817 bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
820 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_STANDBY
);
822 return Transmit(command
);
825 bool CCECCommandHandler::TransmitRequestActiveSource(const cec_logical_address iInitiator
, bool bWaitForResponse
/* = true */)
828 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_REQUEST_ACTIVE_SOURCE
);
830 return Transmit(command
, !bWaitForResponse
);
833 bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
836 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_CEC_VERSION
);
838 return Transmit(command
, !bWaitForResponse
);
841 bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
844 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_MENU_LANGUAGE
);
846 return Transmit(command
, !bWaitForResponse
);
849 bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
852 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_OSD_NAME
);
854 return Transmit(command
, !bWaitForResponse
);
857 bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
860 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
);
862 return Transmit(command
, !bWaitForResponse
);
865 bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
868 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
);
870 return Transmit(command
, !bWaitForResponse
);
873 bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
876 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
878 return Transmit(command
, !bWaitForResponse
);
881 bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
884 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
885 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
886 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
888 return Transmit(command
);
891 bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_version cecVersion
)
894 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_CEC_VERSION
);
895 command
.parameters
.PushBack((uint8_t)cecVersion
);
897 return Transmit(command
);
900 bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
903 cec_command::Format(command
, iInitiator
, CECDEVICE_TV
, CEC_OPCODE_INACTIVE_SOURCE
);
904 command
.parameters
.PushBack((iPhysicalAddress
>> 8) & 0xFF);
905 command
.parameters
.PushBack(iPhysicalAddress
& 0xFF);
907 return Transmit(command
);
910 bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_menu_state menuState
)
913 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_MENU_STATUS
);
914 command
.parameters
.PushBack((uint8_t)menuState
);
916 return Transmit(command
);
919 bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, CStdString strDeviceName
)
922 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_NAME
);
923 for (size_t iPtr
= 0; iPtr
< strDeviceName
.length(); iPtr
++)
924 command
.parameters
.PushBack(strDeviceName
.at(iPtr
));
926 return Transmit(command
);
929 bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_display_control duration
, const char *strMessage
)
932 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_STRING
);
933 command
.parameters
.PushBack((uint8_t)duration
);
935 size_t iLen
= strlen(strMessage
);
936 if (iLen
> 13) iLen
= 13;
938 for (size_t iPtr
= 0; iPtr
< iLen
; iPtr
++)
939 command
.parameters
.PushBack(strMessage
[iPtr
]);
941 return Transmit(command
);
944 bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
, cec_device_type type
)
947 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
948 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
949 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
950 command
.parameters
.PushBack((uint8_t) (type
));
952 return Transmit(command
);
955 bool CCECCommandHandler::TransmitSetMenuLanguage(const cec_logical_address iInitiator
, const char lang
[3])
958 command
.Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_SET_MENU_LANGUAGE
);
959 command
.parameters
.PushBack((uint8_t) lang
[0]);
960 command
.parameters
.PushBack((uint8_t) lang
[1]);
961 command
.parameters
.PushBack((uint8_t) lang
[2]);
963 return Transmit(command
);
966 bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
969 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_NONE
);
971 return Transmit(command
);
974 bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_power_status state
)
977 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_POWER_STATUS
);
978 command
.parameters
.PushBack((uint8_t) state
);
980 return Transmit(command
);
983 bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator
, uint64_t iVendorId
)
986 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_DEVICE_VENDOR_ID
);
988 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 16) & 0xFF));
989 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 8) & 0xFF));
990 command
.parameters
.PushBack((uint8_t) ((uint64_t)iVendorId
& 0xFF));
992 return Transmit(command
);
995 bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, uint8_t state
)
998 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_AUDIO_STATUS
);
999 command
.parameters
.PushBack(state
);
1001 return Transmit(command
);
1004 bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
)
1006 cec_command command
;
1007 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
);
1008 command
.parameters
.PushBack((uint8_t)state
);
1010 return Transmit(command
);
1013 bool CCECCommandHandler::TransmitSetStreamPath(uint16_t iStreamPath
)
1015 cec_command command
;
1016 cec_command::Format(command
, m_busDevice
->GetLogicalAddress(), CECDEVICE_BROADCAST
, CEC_OPCODE_SET_STREAM_PATH
);
1017 command
.parameters
.PushBack((uint8_t) ((iStreamPath
>> 8) & 0xFF));
1018 command
.parameters
.PushBack((uint8_t) (iStreamPath
& 0xFF));
1020 return Transmit(command
);
1023 bool CCECCommandHandler::TransmitSystemAudioModeStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
)
1025 cec_command command
;
1026 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
);
1027 command
.parameters
.PushBack((uint8_t)state
);
1029 return Transmit(command
);
1032 bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_deck_info state
)
1034 cec_command command
;
1035 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_DECK_STATUS
);
1036 command
.PushBack((uint8_t)state
);
1038 return Transmit(command
);
1041 bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = true */)
1043 cec_command command
;
1044 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_PRESSED
);
1045 command
.parameters
.PushBack((uint8_t)key
);
1047 return Transmit(command
, !bWait
);
1050 bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWait
/* = true */)
1052 cec_command command
;
1053 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_RELEASE
);
1055 return Transmit(command
, !bWait
);
1058 bool CCECCommandHandler::Transmit(cec_command
&command
, bool bSuppressWait
/* = false */)
1060 bool bReturn(false);
1061 cec_opcode
expectedResponse(cec_command::GetResponseOpcode(command
.opcode
));
1062 bool bExpectResponse(expectedResponse
!= CEC_OPCODE_NONE
&& !bSuppressWait
);
1063 command
.transmit_timeout
= m_iTransmitTimeout
;
1065 if (command
.initiator
== CECDEVICE_UNKNOWN
)
1067 LIB_CEC
->AddLog(CEC_LOG_ERROR
, "not transmitting a command without a valid initiator");
1072 uint8_t iTries(0), iMaxTries(!command
.opcode_set
? 1 : m_iTransmitRetries
+ 1);
1073 while (!bReturn
&& ++iTries
<= iMaxTries
&& !m_busDevice
->IsUnsupportedFeature(command
.opcode
))
1075 if ((bReturn
= m_processor
->Transmit(command
)) == true)
1077 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "command transmitted");
1078 if (bExpectResponse
)
1080 bReturn
= m_waitForResponse
->Wait(expectedResponse
);
1081 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, bReturn
? "expected response received (%X: %s)" : "expected response not received (%X: %s)", (int)expectedResponse
, ToString(expectedResponse
));
1090 bool CCECCommandHandler::ActivateSource(bool bTransmitDelayedCommandsOnly
/* = false */)
1092 if (m_busDevice
->IsActiveSource() &&
1093 m_busDevice
->IsHandledByLibCEC())
1096 CLockObject
lock(m_mutex
);
1097 // check if we need to send a delayed source switch
1098 if (bTransmitDelayedCommandsOnly
)
1100 if (m_iActiveSourcePending
== 0 || GetTimeMs() < m_iActiveSourcePending
)
1103 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "transmitting delayed activate source command");
1106 // clear previous pending active source command
1107 m_iActiveSourcePending
= 0;
1110 // update the power state and menu state
1111 m_busDevice
->SetPowerStatus(CEC_POWER_STATUS_ON
);
1112 m_busDevice
->SetMenuState(CEC_MENU_STATE_ACTIVATED
); // TODO: LG
1115 bool bActiveSourceFailed
= !m_busDevice
->TransmitImageViewOn();
1117 // check if we're allowed to switch sources
1118 bool bSourceSwitchAllowed
= SourceSwitchAllowed();
1119 if (!bSourceSwitchAllowed
)
1120 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "source switch is currently not allowed by command handler");
1122 // switch sources (if allowed)
1123 if (!bActiveSourceFailed
&& bSourceSwitchAllowed
)
1125 bActiveSourceFailed
= !m_busDevice
->TransmitActiveSource() ||
1126 !m_busDevice
->TransmitMenuState(CECDEVICE_TV
);
1128 // update the deck status for playback devices
1129 if (!bActiveSourceFailed
)
1131 CCECPlaybackDevice
*playbackDevice
= m_busDevice
->AsPlaybackDevice();
1132 if (playbackDevice
&& SendDeckStatusUpdateOnActiveSource())
1133 bActiveSourceFailed
= !playbackDevice
->TransmitDeckStatus(CECDEVICE_TV
);
1138 if (bActiveSourceFailed
|| !bSourceSwitchAllowed
)
1140 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "failed to make '%s' the active source. will retry later", m_busDevice
->GetLogicalAddressName());
1141 CLockObject
lock(m_mutex
);
1142 m_iActiveSourcePending
= GetTimeMs() + (int64_t)CEC_ACTIVE_SOURCE_SWITCH_RETRY_TIME_MS
;
1146 // mark the handler as initialised
1147 CLockObject
lock(m_mutex
);
1148 m_bHandlerInited
= true;
1153 void CCECCommandHandler::SignalOpcode(cec_opcode opcode
)
1155 m_waitForResponse
->Received(opcode
);