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 CCECClient
*client
= m_busDevice
->GetClient();
78 client
->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_waitForResponse
->Received((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 CCECBusDevice
*device
= m_processor
->GetDeviceByPhysicalAddress(iAddress
);
215 device
->MarkAsActiveSource();
216 return COMMAND_HANDLED
;
220 return CEC_ABORT_REASON_INVALID_OPERAND
;
223 int CCECCommandHandler::HandleDeckControl(const cec_command
&command
)
225 CCECPlaybackDevice
*device
= CCECBusDevice::AsPlaybackDevice(GetDevice(command
.destination
));
226 if (device
&& command
.parameters
.size
> 0)
228 device
->SetDeckControlMode((cec_deck_control_mode
) command
.parameters
[0]);
229 return COMMAND_HANDLED
;
232 return CEC_ABORT_REASON_INVALID_OPERAND
;
235 int CCECCommandHandler::HandleDeviceCecVersion(const cec_command
&command
)
237 if (command
.parameters
.size
== 1)
239 CCECBusDevice
*device
= GetDevice(command
.initiator
);
241 device
->SetCecVersion((cec_version
) command
.parameters
[0]);
243 return COMMAND_HANDLED
;
246 return CEC_ABORT_REASON_INVALID_OPERAND
;
249 int CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command
& UNUSED(command
))
251 return CEC_ABORT_REASON_INVALID_OPERAND
;
254 int CCECCommandHandler::HandleDeviceVendorId(const cec_command
&command
)
256 SetVendorId(command
);
257 return COMMAND_HANDLED
;
260 int CCECCommandHandler::HandleFeatureAbort(const cec_command
&command
)
262 if (command
.parameters
.size
== 2 &&
263 (command
.parameters
[1] == CEC_ABORT_REASON_UNRECOGNIZED_OPCODE
||
264 command
.parameters
[1] == CEC_ABORT_REASON_REFUSED
))
265 m_processor
->GetDevice(command
.initiator
)->SetUnsupportedFeature((cec_opcode
)command
.parameters
[0]);
266 return COMMAND_HANDLED
;
269 int CCECCommandHandler::HandleGetCecVersion(const cec_command
&command
)
271 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
273 CCECBusDevice
*device
= GetDevice(command
.destination
);
274 if (device
&& device
->TransmitCECVersion(command
.initiator
))
275 return COMMAND_HANDLED
;
276 return CEC_ABORT_REASON_INVALID_OPERAND
;
279 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
282 int CCECCommandHandler::HandleGiveAudioStatus(const cec_command
&command
)
284 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
286 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.destination
));
287 if (device
&& device
->TransmitAudioStatus(command
.initiator
))
288 return COMMAND_HANDLED
;
289 return CEC_ABORT_REASON_INVALID_OPERAND
;
292 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
295 int CCECCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
297 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
299 CCECPlaybackDevice
*device
= CCECBusDevice::AsPlaybackDevice(GetDevice(command
.destination
));
300 if (device
&& device
->TransmitDeckStatus(command
.initiator
))
301 return COMMAND_HANDLED
;
302 return CEC_ABORT_REASON_INVALID_OPERAND
;
305 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
308 int CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command
&command
)
310 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
312 CCECBusDevice
*device
= GetDevice(command
.destination
);
313 if (device
&& device
->TransmitPowerState(command
.initiator
))
314 return COMMAND_HANDLED
;
315 return CEC_ABORT_REASON_INVALID_OPERAND
;
318 return CEC_ABORT_REASON_INVALID_OPERAND
;
321 int CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command
&command
)
323 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
325 CCECBusDevice
*device
= GetDevice(command
.destination
);
326 if (device
&& device
->TransmitVendorID(command
.initiator
))
327 return COMMAND_HANDLED
;
330 return CEC_ABORT_REASON_INVALID_OPERAND
;
333 int CCECCommandHandler::HandleGiveOSDName(const cec_command
&command
)
335 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
337 CCECBusDevice
*device
= GetDevice(command
.destination
);
338 if (device
&& device
->TransmitOSDName(command
.initiator
))
339 return COMMAND_HANDLED
;
342 return CEC_ABORT_REASON_INVALID_OPERAND
;
345 int CCECCommandHandler::HandleGivePhysicalAddress(const cec_command
&command
)
347 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
349 CCECBusDevice
*device
= GetDevice(command
.destination
);
350 if (device
&& device
->TransmitPhysicalAddress())
351 return COMMAND_HANDLED
;
352 return CEC_ABORT_REASON_INVALID_OPERAND
;
355 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
358 int CCECCommandHandler::HandleGiveMenuLanguage(const cec_command
&command
)
360 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
362 CCECBusDevice
*device
= GetDevice(command
.destination
);
363 if (device
&& device
->TransmitSetMenuLanguage(command
.initiator
))
364 return COMMAND_HANDLED
;
365 return CEC_ABORT_REASON_INVALID_OPERAND
;
368 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
371 int CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command
&command
)
373 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
375 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.destination
));
376 if (device
&& device
->TransmitSystemAudioModeStatus(command
.initiator
))
377 return COMMAND_HANDLED
;
378 return CEC_ABORT_REASON_INVALID_OPERAND
;
381 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
384 int CCECCommandHandler::HandleImageViewOn(const cec_command
&command
)
386 m_processor
->GetDevice(command
.initiator
)->MarkAsActiveSource();
387 return COMMAND_HANDLED
;
390 int CCECCommandHandler::HandleMenuRequest(const cec_command
&command
)
392 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
394 CCECBusDevice
*device
= GetDevice(command
.destination
);
397 CCECClient
*client
= device
->GetClient();
400 if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_ACTIVATE
)
402 if (client
->MenuStateChanged(CEC_MENU_STATE_ACTIVATED
) == 1)
403 device
->SetMenuState(CEC_MENU_STATE_ACTIVATED
);
405 else if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_DEACTIVATE
)
407 if (client
->MenuStateChanged(CEC_MENU_STATE_DEACTIVATED
) == 1)
408 device
->SetMenuState(CEC_MENU_STATE_DEACTIVATED
);
411 if (device
->TransmitMenuState(command
.initiator
))
412 return COMMAND_HANDLED
;
414 return CEC_ABORT_REASON_INVALID_OPERAND
;
417 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
420 bool CCECCommandHandler::HandlePoll(const cec_command
&command
)
422 m_busDevice
->HandlePoll(command
.destination
);
426 int CCECCommandHandler::HandleReportAudioStatus(const cec_command
&command
)
428 if (command
.parameters
.size
== 1)
430 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.initiator
));
433 device
->SetAudioStatus(command
.parameters
[0]);
434 return COMMAND_HANDLED
;
437 return CEC_ABORT_REASON_INVALID_OPERAND
;
440 int CCECCommandHandler::HandleReportPhysicalAddress(const cec_command
&command
)
442 if (command
.parameters
.size
== 3)
444 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
445 SetPhysicalAddress(command
.initiator
, iNewAddress
);
446 return COMMAND_HANDLED
;
448 return CEC_ABORT_REASON_INVALID_OPERAND
;
451 int CCECCommandHandler::HandleReportPowerStatus(const cec_command
&command
)
453 if (command
.parameters
.size
== 1)
455 CCECBusDevice
*device
= GetDevice(command
.initiator
);
458 device
->SetPowerStatus((cec_power_status
) command
.parameters
[0]);
459 return COMMAND_HANDLED
;
462 return CEC_ABORT_REASON_INVALID_OPERAND
;
465 int CCECCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
467 if (m_processor
->CECInitialised())
469 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, ">> %i requests active source", (uint8_t) command
.initiator
);
470 m_processor
->GetDevice(command
.initiator
)->SetPowerStatus(CEC_POWER_STATUS_ON
);
472 vector
<CCECBusDevice
*> devices
;
473 for (size_t iDevicePtr
= 0; iDevicePtr
< GetMyDevices(devices
); iDevicePtr
++)
474 devices
[iDevicePtr
]->TransmitActiveSource();
477 return COMMAND_HANDLED
;
480 int CCECCommandHandler::HandleRoutingChange(const cec_command
&command
)
482 if (command
.parameters
.size
== 4)
484 uint16_t iOldAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
485 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[2] << 8) | ((uint16_t)command
.parameters
[3]);
487 CCECBusDevice
*device
= GetDevice(command
.initiator
);
490 device
->SetStreamPath(iNewAddress
, iOldAddress
);
491 return COMMAND_HANDLED
;
495 return CEC_ABORT_REASON_INVALID_OPERAND
;
498 int CCECCommandHandler::HandleRoutingInformation(const cec_command
&command
)
500 if (command
.parameters
.size
== 2)
502 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
503 CCECBusDevice
*device
= m_processor
->GetDeviceByPhysicalAddress(iNewAddress
);
506 device
->MarkAsActiveSource();
507 return COMMAND_HANDLED
;
511 return CEC_ABORT_REASON_INVALID_OPERAND
;
514 int CCECCommandHandler::HandleSetMenuLanguage(const cec_command
&command
)
516 if (command
.parameters
.size
== 3)
518 CCECBusDevice
*device
= GetDevice(command
.initiator
);
521 cec_menu_language language
;
522 language
.device
= command
.initiator
;
523 for (uint8_t iPtr
= 0; iPtr
< 4; iPtr
++)
524 language
.language
[iPtr
] = command
.parameters
[iPtr
];
525 language
.language
[3] = 0;
526 device
->SetMenuLanguage(language
);
527 return COMMAND_HANDLED
;
531 return CEC_ABORT_REASON_INVALID_OPERAND
;
534 int CCECCommandHandler::HandleSetOSDName(const cec_command
&command
)
536 if (command
.parameters
.size
> 0)
538 CCECBusDevice
*device
= GetDevice(command
.initiator
);
542 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
543 buf
[iPtr
] = (char)command
.parameters
[iPtr
];
544 buf
[command
.parameters
.size
] = 0;
546 CStdString
strName(buf
);
547 device
->SetOSDName(strName
);
549 return COMMAND_HANDLED
;
553 return CEC_ABORT_REASON_INVALID_OPERAND
;
556 int CCECCommandHandler::HandleSetStreamPath(const cec_command
&command
)
558 if (!m_processor
->CECInitialised())
559 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
561 if (command
.parameters
.size
>= 2)
563 uint16_t iStreamAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
564 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, ">> %s (%x) sets stream path to physical address %04x", ToString(command
.initiator
), command
.initiator
, iStreamAddress
);
566 // a device will only change the stream path when it's powered on
567 m_busDevice
->SetPowerStatus(CEC_POWER_STATUS_ON
);
569 /* one of the device handled by libCEC has been made active */
570 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamAddress
);
571 if (device
&& device
->IsHandledByLibCEC())
573 device
->ActivateSource();
574 return COMMAND_HANDLED
;
578 return CEC_ABORT_REASON_INVALID_OPERAND
;
581 int CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command
&command
)
583 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
585 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.destination
));
588 if (command
.parameters
.size
>= 2)
590 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
591 device
->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON
);
592 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
593 CCECBusDevice
*newActiveDevice
= GetDeviceByPhysicalAddress(iNewAddress
);
595 newActiveDevice
->MarkAsActiveSource();
596 if (device
->TransmitSetSystemAudioMode(command
.initiator
))
597 return COMMAND_HANDLED
;
601 device
->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF
);
602 if (device
->TransmitSetSystemAudioMode(command
.initiator
))
603 return COMMAND_HANDLED
;
608 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
611 int CCECCommandHandler::HandleStandby(const cec_command
&command
)
613 CCECBusDevice
*device
= GetDevice(command
.initiator
);
615 device
->SetPowerStatus(CEC_POWER_STATUS_STANDBY
);
617 return COMMAND_HANDLED
;
620 int CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command
&command
)
622 if (command
.parameters
.size
== 1)
624 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.initiator
));
627 device
->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
628 return COMMAND_HANDLED
;
632 return CEC_ABORT_REASON_INVALID_OPERAND
;
635 int CCECCommandHandler::HandleSetSystemAudioMode(const cec_command
&command
)
637 if (command
.parameters
.size
== 1)
639 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.initiator
));
642 device
->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
643 return COMMAND_HANDLED
;
647 return CEC_ABORT_REASON_INVALID_OPERAND
;
650 int CCECCommandHandler::HandleTextViewOn(const cec_command
&command
)
652 m_processor
->GetDevice(command
.initiator
)->MarkAsActiveSource();
653 return COMMAND_HANDLED
;
656 int CCECCommandHandler::HandleUserControlPressed(const cec_command
&command
)
658 if (!m_processor
->CECInitialised() ||
659 !m_processor
->IsHandledByLibCEC(command
.destination
))
660 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
662 if (command
.parameters
.size
== 0)
663 return CEC_ABORT_REASON_INVALID_OPERAND
;
665 CCECBusDevice
*device
= GetDevice(command
.destination
);
667 return CEC_ABORT_REASON_INVALID_OPERAND
;
669 CCECClient
*client
= device
->GetClient();
673 if (command
.parameters
[0] <= CEC_USER_CONTROL_CODE_MAX
)
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
)
681 // CEC_USER_CONTROL_CODE_POWER operates as a toggle
682 // assume CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION does not
683 if (command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER
)
685 cec_power_status status
= device
->GetCurrentPowerStatus();
686 bPowerOn
= !(status
== CEC_POWER_STATUS_ON
|| status
== CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
691 device
->ActivateSource();
695 device
->MarkAsInactiveSource();
696 device
->TransmitInactiveSource();
697 device
->SetMenuState(CEC_MENU_STATE_DEACTIVATED
);
701 return COMMAND_HANDLED
;
704 int CCECCommandHandler::HandleUserControlRelease(const cec_command
&command
)
706 if (!m_processor
->CECInitialised() ||
707 !m_processor
->IsHandledByLibCEC(command
.destination
))
708 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
710 CCECClient
*client
= m_processor
->GetClient(command
.destination
);
714 return COMMAND_HANDLED
;
717 int CCECCommandHandler::HandleVendorCommand(const cec_command
& UNUSED(command
))
719 return CEC_ABORT_REASON_INVALID_OPERAND
;
722 void CCECCommandHandler::UnhandledCommand(const cec_command
&command
, const cec_abort_reason reason
)
724 if (m_processor
->IsHandledByLibCEC(command
.destination
))
726 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "sending abort with opcode %02x and reason '%s' to %s", command
.opcode
, ToString(reason
), ToString(command
.initiator
));
727 m_processor
->TransmitAbort(command
.destination
, command
.initiator
, command
.opcode
, reason
);
731 size_t CCECCommandHandler::GetMyDevices(vector
<CCECBusDevice
*> &devices
) const
735 cec_logical_addresses addresses
= m_processor
->GetLogicalAddresses();
736 for (uint8_t iPtr
= CECDEVICE_TV
; iPtr
< CECDEVICE_BROADCAST
; iPtr
++)
740 devices
.push_back(GetDevice((cec_logical_address
) iPtr
));
748 CCECBusDevice
*CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress
) const
750 return m_processor
->GetDevice(iLogicalAddress
);
753 CCECBusDevice
*CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
) const
755 return m_processor
->GetDeviceByPhysicalAddress(iPhysicalAddress
);
758 bool CCECCommandHandler::SetVendorId(const cec_command
&command
)
760 bool bChanged(false);
761 if (command
.parameters
.size
< 3)
763 LIB_CEC
->AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
767 uint64_t iVendorId
= ((uint64_t)command
.parameters
[0] << 16) +
768 ((uint64_t)command
.parameters
[1] << 8) +
769 (uint64_t)command
.parameters
[2];
771 CCECBusDevice
*device
= GetDevice((cec_logical_address
) command
.initiator
);
773 bChanged
= device
->SetVendorId(iVendorId
);
777 void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress
, uint16_t iNewAddress
)
779 if (!m_processor
->IsHandledByLibCEC(iAddress
))
781 CCECBusDevice
*otherDevice
= m_processor
->GetDeviceByPhysicalAddress(iNewAddress
);
782 CCECClient
*client
= otherDevice
? otherDevice
->GetClient() : NULL
;
784 CCECBusDevice
*device
= m_processor
->GetDevice(iAddress
);
786 device
->SetPhysicalAddress(iNewAddress
);
789 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "device with logical address %X not found", iAddress
);
792 /* another device reported the same physical address as ours */
794 client
->ResetPhysicalAddress();
798 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
);
802 bool CCECCommandHandler::PowerOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
804 if (iDestination
== CECDEVICE_TV
)
805 return TransmitImageViewOn(iInitiator
, iDestination
);
807 return TransmitKeypress(iInitiator
, iDestination
, CEC_USER_CONTROL_CODE_POWER
) &&
808 TransmitKeyRelease(iInitiator
, iDestination
);
811 bool CCECCommandHandler::TransmitImageViewOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
814 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_IMAGE_VIEW_ON
);
816 return Transmit(command
);
819 bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
822 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_STANDBY
);
824 return Transmit(command
);
827 bool CCECCommandHandler::TransmitRequestActiveSource(const cec_logical_address iInitiator
, bool bWaitForResponse
/* = true */)
830 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_REQUEST_ACTIVE_SOURCE
);
832 return Transmit(command
, !bWaitForResponse
);
835 bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
838 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_CEC_VERSION
);
840 return Transmit(command
, !bWaitForResponse
);
843 bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
846 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_MENU_LANGUAGE
);
848 return Transmit(command
, !bWaitForResponse
);
851 bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
854 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_OSD_NAME
);
856 return Transmit(command
, !bWaitForResponse
);
859 bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
862 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
);
864 return Transmit(command
, !bWaitForResponse
);
867 bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
870 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
);
872 return Transmit(command
, !bWaitForResponse
);
875 bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
878 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
880 return Transmit(command
, !bWaitForResponse
);
883 bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
886 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
887 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
888 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
890 return Transmit(command
);
893 bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_version cecVersion
)
896 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_CEC_VERSION
);
897 command
.parameters
.PushBack((uint8_t)cecVersion
);
899 return Transmit(command
);
902 bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
905 cec_command::Format(command
, iInitiator
, CECDEVICE_TV
, CEC_OPCODE_INACTIVE_SOURCE
);
906 command
.parameters
.PushBack((iPhysicalAddress
>> 8) & 0xFF);
907 command
.parameters
.PushBack(iPhysicalAddress
& 0xFF);
909 return Transmit(command
);
912 bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_menu_state menuState
)
915 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_MENU_STATUS
);
916 command
.parameters
.PushBack((uint8_t)menuState
);
918 return Transmit(command
);
921 bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, CStdString strDeviceName
)
924 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_NAME
);
925 for (size_t iPtr
= 0; iPtr
< strDeviceName
.length(); iPtr
++)
926 command
.parameters
.PushBack(strDeviceName
.at(iPtr
));
928 return Transmit(command
);
931 bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_display_control duration
, const char *strMessage
)
934 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_STRING
);
935 command
.parameters
.PushBack((uint8_t)duration
);
937 size_t iLen
= strlen(strMessage
);
938 if (iLen
> 13) iLen
= 13;
940 for (size_t iPtr
= 0; iPtr
< iLen
; iPtr
++)
941 command
.parameters
.PushBack(strMessage
[iPtr
]);
943 return Transmit(command
);
946 bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
, cec_device_type type
)
949 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
950 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
951 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
952 command
.parameters
.PushBack((uint8_t) (type
));
954 return Transmit(command
);
957 bool CCECCommandHandler::TransmitSetMenuLanguage(const cec_logical_address iInitiator
, const char lang
[3])
960 command
.Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_SET_MENU_LANGUAGE
);
961 command
.parameters
.PushBack((uint8_t) lang
[0]);
962 command
.parameters
.PushBack((uint8_t) lang
[1]);
963 command
.parameters
.PushBack((uint8_t) lang
[2]);
965 return Transmit(command
);
968 bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
971 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_NONE
);
973 return Transmit(command
);
976 bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_power_status state
)
979 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_POWER_STATUS
);
980 command
.parameters
.PushBack((uint8_t) state
);
982 return Transmit(command
);
985 bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator
, uint64_t iVendorId
)
988 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_DEVICE_VENDOR_ID
);
990 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 16) & 0xFF));
991 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 8) & 0xFF));
992 command
.parameters
.PushBack((uint8_t) ((uint64_t)iVendorId
& 0xFF));
994 return Transmit(command
);
997 bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, uint8_t state
)
1000 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_AUDIO_STATUS
);
1001 command
.parameters
.PushBack(state
);
1003 return Transmit(command
);
1006 bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
)
1008 cec_command command
;
1009 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
);
1010 command
.parameters
.PushBack((uint8_t)state
);
1012 return Transmit(command
);
1015 bool CCECCommandHandler::TransmitSetStreamPath(uint16_t iStreamPath
)
1017 cec_command command
;
1018 cec_command::Format(command
, m_busDevice
->GetLogicalAddress(), CECDEVICE_BROADCAST
, CEC_OPCODE_SET_STREAM_PATH
);
1019 command
.parameters
.PushBack((uint8_t) ((iStreamPath
>> 8) & 0xFF));
1020 command
.parameters
.PushBack((uint8_t) (iStreamPath
& 0xFF));
1022 return Transmit(command
);
1025 bool CCECCommandHandler::TransmitSystemAudioModeStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
)
1027 cec_command command
;
1028 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
);
1029 command
.parameters
.PushBack((uint8_t)state
);
1031 return Transmit(command
);
1034 bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_deck_info state
)
1036 cec_command command
;
1037 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_DECK_STATUS
);
1038 command
.PushBack((uint8_t)state
);
1040 return Transmit(command
);
1043 bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = true */)
1045 cec_command command
;
1046 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_PRESSED
);
1047 command
.parameters
.PushBack((uint8_t)key
);
1049 return Transmit(command
, !bWait
);
1052 bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWait
/* = true */)
1054 cec_command command
;
1055 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_RELEASE
);
1057 return Transmit(command
, !bWait
);
1060 bool CCECCommandHandler::Transmit(cec_command
&command
, bool bSuppressWait
/* = false */)
1062 bool bReturn(false);
1063 cec_opcode
expectedResponse(cec_command::GetResponseOpcode(command
.opcode
));
1064 bool bExpectResponse(expectedResponse
!= CEC_OPCODE_NONE
&& !bSuppressWait
);
1065 command
.transmit_timeout
= m_iTransmitTimeout
;
1067 if (command
.initiator
== CECDEVICE_UNKNOWN
)
1069 LIB_CEC
->AddLog(CEC_LOG_ERROR
, "not transmitting a command without a valid initiator");
1074 uint8_t iTries(0), iMaxTries(!command
.opcode_set
? 1 : m_iTransmitRetries
+ 1);
1075 while (!bReturn
&& ++iTries
<= iMaxTries
&& !m_busDevice
->IsUnsupportedFeature(command
.opcode
))
1077 if ((bReturn
= m_processor
->Transmit(command
)) == true)
1079 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "command transmitted");
1080 if (bExpectResponse
)
1082 bReturn
= m_waitForResponse
->Wait(expectedResponse
);
1083 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, bReturn
? "expected response received (%X: %s)" : "expected response not received (%X: %s)", (int)expectedResponse
, ToString(expectedResponse
));
1092 bool CCECCommandHandler::ActivateSource(bool bTransmitDelayedCommandsOnly
/* = false */)
1094 if (m_busDevice
->IsActiveSource() &&
1095 m_busDevice
->IsHandledByLibCEC())
1098 CLockObject
lock(m_mutex
);
1099 // check if we need to send a delayed source switch
1100 if (bTransmitDelayedCommandsOnly
)
1102 if (m_iActiveSourcePending
== 0 || GetTimeMs() < m_iActiveSourcePending
)
1105 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "transmitting delayed activate source command");
1108 // clear previous pending active source command
1109 m_iActiveSourcePending
= 0;
1112 // update the power state and menu state
1113 m_busDevice
->SetPowerStatus(CEC_POWER_STATUS_ON
);
1114 m_busDevice
->SetMenuState(CEC_MENU_STATE_ACTIVATED
); // TODO: LG
1117 bool bActiveSourceFailed
= !m_busDevice
->TransmitImageViewOn();
1119 // check if we're allowed to switch sources
1120 bool bSourceSwitchAllowed
= SourceSwitchAllowed();
1121 if (!bSourceSwitchAllowed
)
1122 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "source switch is currently not allowed by command handler");
1124 // switch sources (if allowed)
1125 if (!bActiveSourceFailed
&& bSourceSwitchAllowed
)
1127 bActiveSourceFailed
= !m_busDevice
->TransmitActiveSource() ||
1128 !m_busDevice
->TransmitMenuState(CECDEVICE_TV
);
1130 // update the deck status for playback devices
1131 if (!bActiveSourceFailed
)
1133 CCECPlaybackDevice
*playbackDevice
= m_busDevice
->AsPlaybackDevice();
1134 if (playbackDevice
&& SendDeckStatusUpdateOnActiveSource())
1135 bActiveSourceFailed
= !playbackDevice
->TransmitDeckStatus(CECDEVICE_TV
);
1140 if (bActiveSourceFailed
|| !bSourceSwitchAllowed
)
1142 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "failed to make '%s' the active source. will retry later", m_busDevice
->GetLogicalAddressName());
1143 CLockObject
lock(m_mutex
);
1144 m_iActiveSourcePending
= GetTimeMs() + CEC_ACTIVE_SOURCE_SWITCH_RETRY_TIME_MS
;
1148 // mark the handler as initialised
1149 CLockObject
lock(m_mutex
);
1150 m_bHandlerInited
= true;
1155 void CCECCommandHandler::SignalOpcode(cec_opcode opcode
)
1157 m_waitForResponse
->Received(opcode
);