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)
52 CCECCommandHandler::CCECCommandHandler(CCECBusDevice
*busDevice
,
53 int32_t iTransmitTimeout
/* = CEC_DEFAULT_TRANSMIT_TIMEOUT */,
54 int32_t iTransmitWait
/* = CEC_DEFAULT_TRANSMIT_WAIT */,
55 int8_t iTransmitRetries
/* = CEC_DEFAULT_TRANSMIT_RETRIES */,
56 int64_t iActiveSourcePending
/* = 0 */) :
57 m_busDevice(busDevice
),
58 m_processor(m_busDevice
->GetProcessor()),
59 m_iTransmitTimeout(iTransmitTimeout
),
60 m_iTransmitWait(iTransmitWait
),
61 m_iTransmitRetries(iTransmitRetries
),
62 m_bHandlerInited(false),
63 m_bOPTSendDeckStatusUpdateOnActiveSource(false),
64 m_vendorId(CEC_VENDOR_UNKNOWN
),
65 m_iActiveSourcePending(iActiveSourcePending
)
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_busDevice
->SignalOpcode((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
, true))
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
, true))
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
, true))
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
, true))
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
, true, true))
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
, true))
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(true))
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
, true))
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
, true))
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 CCECBusDevice
*device
= GetDevice(command
.destination
);
385 if (device
&& (device
->GetCurrentStatus() == CEC_DEVICE_STATUS_PRESENT
||
386 device
->GetCurrentStatus() == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
))
388 if (device
->GetCurrentPowerStatus() == CEC_POWER_STATUS_STANDBY
||
389 device
->GetCurrentPowerStatus() == CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY
)
390 device
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
392 return COMMAND_HANDLED
;
395 int CCECCommandHandler::HandleMenuRequest(const cec_command
&command
)
397 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
399 CCECBusDevice
*device
= GetDevice(command
.destination
);
402 CCECClient
*client
= device
->GetClient();
405 if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_ACTIVATE
)
407 if (client
->MenuStateChanged(CEC_MENU_STATE_ACTIVATED
) == 1)
408 device
->SetMenuState(CEC_MENU_STATE_ACTIVATED
);
410 else if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_DEACTIVATE
)
412 if (client
->MenuStateChanged(CEC_MENU_STATE_DEACTIVATED
) == 1)
413 device
->SetMenuState(CEC_MENU_STATE_DEACTIVATED
);
416 if (device
->TransmitMenuState(command
.initiator
, true))
417 return COMMAND_HANDLED
;
419 return CEC_ABORT_REASON_INVALID_OPERAND
;
422 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
425 bool CCECCommandHandler::HandlePoll(const cec_command
&command
)
427 m_busDevice
->HandlePoll(command
.destination
);
431 int CCECCommandHandler::HandleReportAudioStatus(const cec_command
&command
)
433 if (command
.parameters
.size
== 1)
435 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.initiator
));
438 device
->SetAudioStatus(command
.parameters
[0]);
439 return COMMAND_HANDLED
;
442 return CEC_ABORT_REASON_INVALID_OPERAND
;
445 int CCECCommandHandler::HandleReportPhysicalAddress(const cec_command
&command
)
447 if (command
.parameters
.size
== 3)
449 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
450 SetPhysicalAddress(command
.initiator
, iNewAddress
);
451 return COMMAND_HANDLED
;
453 return CEC_ABORT_REASON_INVALID_OPERAND
;
456 int CCECCommandHandler::HandleReportPowerStatus(const cec_command
&command
)
458 if (command
.parameters
.size
== 1)
460 CCECBusDevice
*device
= GetDevice(command
.initiator
);
463 device
->SetPowerStatus((cec_power_status
) command
.parameters
[0]);
464 return COMMAND_HANDLED
;
467 return CEC_ABORT_REASON_INVALID_OPERAND
;
470 int CCECCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
472 if (m_processor
->CECInitialised())
474 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, ">> %i requests active source", (uint8_t) command
.initiator
);
475 m_processor
->GetDevice(command
.initiator
)->SetPowerStatus(CEC_POWER_STATUS_ON
);
477 vector
<CCECBusDevice
*> devices
;
478 for (size_t iDevicePtr
= 0; iDevicePtr
< GetMyDevices(devices
); iDevicePtr
++)
479 devices
[iDevicePtr
]->TransmitActiveSource(true);
482 return COMMAND_HANDLED
;
485 int CCECCommandHandler::HandleRoutingChange(const cec_command
&command
)
487 if (command
.parameters
.size
== 4)
489 uint16_t iOldAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
490 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[2] << 8) | ((uint16_t)command
.parameters
[3]);
492 CCECBusDevice
*device
= GetDevice(command
.initiator
);
495 device
->SetStreamPath(iNewAddress
, iOldAddress
);
496 return COMMAND_HANDLED
;
500 return CEC_ABORT_REASON_INVALID_OPERAND
;
503 int CCECCommandHandler::HandleRoutingInformation(const cec_command
&command
)
505 if (command
.parameters
.size
== 2)
507 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
508 CCECBusDevice
*device
= m_processor
->GetDeviceByPhysicalAddress(iNewAddress
);
511 device
->MarkAsActiveSource();
512 return COMMAND_HANDLED
;
516 return CEC_ABORT_REASON_INVALID_OPERAND
;
519 int CCECCommandHandler::HandleSetMenuLanguage(const cec_command
&command
)
521 if (command
.parameters
.size
== 3)
523 CCECBusDevice
*device
= GetDevice(command
.initiator
);
526 cec_menu_language language
;
527 language
.device
= command
.initiator
;
528 for (uint8_t iPtr
= 0; iPtr
< 4; iPtr
++)
529 language
.language
[iPtr
] = command
.parameters
[iPtr
];
530 language
.language
[3] = 0;
531 device
->SetMenuLanguage(language
);
532 return COMMAND_HANDLED
;
536 return CEC_ABORT_REASON_INVALID_OPERAND
;
539 int CCECCommandHandler::HandleSetOSDName(const cec_command
&command
)
541 if (command
.parameters
.size
> 0)
543 CCECBusDevice
*device
= GetDevice(command
.initiator
);
547 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
548 buf
[iPtr
] = (char)command
.parameters
[iPtr
];
549 buf
[command
.parameters
.size
] = 0;
551 CStdString
strName(buf
);
552 device
->SetOSDName(strName
);
554 return COMMAND_HANDLED
;
558 return CEC_ABORT_REASON_INVALID_OPERAND
;
561 int CCECCommandHandler::HandleSetStreamPath(const cec_command
&command
)
563 if (!m_processor
->CECInitialised())
564 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
566 if (command
.parameters
.size
>= 2)
568 uint16_t iStreamAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
569 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, ">> %s (%x) sets stream path to physical address %04x", ToString(command
.initiator
), command
.initiator
, iStreamAddress
);
571 // a device will only change the stream path when it's powered on
572 m_busDevice
->SetPowerStatus(CEC_POWER_STATUS_ON
);
574 /* one of the device handled by libCEC has been made active */
575 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamAddress
);
576 if (device
&& device
->IsHandledByLibCEC())
578 device
->ActivateSource();
579 return COMMAND_HANDLED
;
583 return CEC_ABORT_REASON_INVALID_OPERAND
;
586 int CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command
&command
)
588 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
590 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.destination
));
593 if (command
.parameters
.size
>= 2)
595 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
596 device
->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON
);
597 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
598 CCECBusDevice
*newActiveDevice
= GetDeviceByPhysicalAddress(iNewAddress
);
600 newActiveDevice
->MarkAsActiveSource();
601 if (device
->TransmitSetSystemAudioMode(command
.initiator
, true))
602 return COMMAND_HANDLED
;
606 device
->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF
);
607 if (device
->TransmitSetSystemAudioMode(command
.initiator
, true))
608 return COMMAND_HANDLED
;
613 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
616 int CCECCommandHandler::HandleStandby(const cec_command
&command
)
618 CCECBusDevice
*device
= GetDevice(command
.initiator
);
620 device
->SetPowerStatus(CEC_POWER_STATUS_STANDBY
);
622 return COMMAND_HANDLED
;
625 int CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command
&command
)
627 if (command
.parameters
.size
== 1)
629 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.initiator
));
632 device
->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
633 return COMMAND_HANDLED
;
637 return CEC_ABORT_REASON_INVALID_OPERAND
;
640 int CCECCommandHandler::HandleSetSystemAudioMode(const cec_command
&command
)
642 if (command
.parameters
.size
== 1)
644 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.initiator
));
647 device
->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
648 return COMMAND_HANDLED
;
652 return CEC_ABORT_REASON_INVALID_OPERAND
;
655 int CCECCommandHandler::HandleTextViewOn(const cec_command
&command
)
657 m_processor
->GetDevice(command
.initiator
)->MarkAsActiveSource();
658 return COMMAND_HANDLED
;
661 int CCECCommandHandler::HandleUserControlPressed(const cec_command
&command
)
663 if (!m_processor
->CECInitialised() ||
664 !m_processor
->IsHandledByLibCEC(command
.destination
))
665 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
667 if (command
.parameters
.size
== 0)
668 return CEC_ABORT_REASON_INVALID_OPERAND
;
670 CCECBusDevice
*device
= GetDevice(command
.destination
);
672 return CEC_ABORT_REASON_INVALID_OPERAND
;
674 CCECClient
*client
= device
->GetClient();
678 if (command
.parameters
[0] <= CEC_USER_CONTROL_CODE_MAX
)
679 client
->SetCurrentButton((cec_user_control_code
) command
.parameters
[0]);
681 if (command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER
||
682 command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION
)
686 // CEC_USER_CONTROL_CODE_POWER operates as a toggle
687 // assume CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION does not
688 if (command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER
)
690 cec_power_status status
= device
->GetCurrentPowerStatus();
691 bPowerOn
= !(status
== CEC_POWER_STATUS_ON
|| status
== CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
696 device
->ActivateSource();
700 device
->MarkAsInactiveSource();
701 device
->TransmitInactiveSource();
702 device
->SetMenuState(CEC_MENU_STATE_DEACTIVATED
);
706 return COMMAND_HANDLED
;
709 int CCECCommandHandler::HandleUserControlRelease(const cec_command
&command
)
711 if (!m_processor
->CECInitialised() ||
712 !m_processor
->IsHandledByLibCEC(command
.destination
))
713 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
715 CCECClient
*client
= m_processor
->GetClient(command
.destination
);
719 return COMMAND_HANDLED
;
722 int CCECCommandHandler::HandleVendorCommand(const cec_command
& UNUSED(command
))
724 return CEC_ABORT_REASON_INVALID_OPERAND
;
727 void CCECCommandHandler::UnhandledCommand(const cec_command
&command
, const cec_abort_reason reason
)
729 if (m_processor
->IsHandledByLibCEC(command
.destination
))
731 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "sending abort with opcode %02x and reason '%s' to %s", command
.opcode
, ToString(reason
), ToString(command
.initiator
));
732 m_processor
->TransmitAbort(command
.destination
, command
.initiator
, command
.opcode
, reason
);
736 size_t CCECCommandHandler::GetMyDevices(vector
<CCECBusDevice
*> &devices
) const
740 cec_logical_addresses addresses
= m_processor
->GetLogicalAddresses();
741 for (uint8_t iPtr
= CECDEVICE_TV
; iPtr
< CECDEVICE_BROADCAST
; iPtr
++)
745 devices
.push_back(GetDevice((cec_logical_address
) iPtr
));
753 CCECBusDevice
*CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress
) const
755 return m_processor
->GetDevice(iLogicalAddress
);
758 CCECBusDevice
*CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
) const
760 return m_processor
->GetDeviceByPhysicalAddress(iPhysicalAddress
);
763 bool CCECCommandHandler::SetVendorId(const cec_command
&command
)
765 bool bChanged(false);
766 if (command
.parameters
.size
< 3)
768 LIB_CEC
->AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
772 uint64_t iVendorId
= ((uint64_t)command
.parameters
[0] << 16) +
773 ((uint64_t)command
.parameters
[1] << 8) +
774 (uint64_t)command
.parameters
[2];
776 CCECBusDevice
*device
= GetDevice((cec_logical_address
) command
.initiator
);
778 bChanged
= device
->SetVendorId(iVendorId
);
782 void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress
, uint16_t iNewAddress
)
784 if (!m_processor
->IsHandledByLibCEC(iAddress
))
786 CCECBusDevice
*otherDevice
= m_processor
->GetDeviceByPhysicalAddress(iNewAddress
);
787 CCECClient
*client
= otherDevice
? otherDevice
->GetClient() : NULL
;
789 CCECBusDevice
*device
= m_processor
->GetDevice(iAddress
);
791 device
->SetPhysicalAddress(iNewAddress
);
794 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "device with logical address %X not found", iAddress
);
797 /* another device reported the same physical address as ours */
799 client
->ResetPhysicalAddress();
803 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
);
807 bool CCECCommandHandler::PowerOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
809 if (iDestination
== CECDEVICE_TV
)
810 return TransmitImageViewOn(iInitiator
, iDestination
);
812 return TransmitKeypress(iInitiator
, iDestination
, CEC_USER_CONTROL_CODE_POWER
) &&
813 TransmitKeyRelease(iInitiator
, iDestination
);
816 bool CCECCommandHandler::TransmitImageViewOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
819 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_IMAGE_VIEW_ON
);
821 return Transmit(command
, false, false);
824 bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
827 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_STANDBY
);
829 return Transmit(command
, false, false);
832 bool CCECCommandHandler::TransmitRequestActiveSource(const cec_logical_address iInitiator
, bool bWaitForResponse
/* = true */)
835 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_REQUEST_ACTIVE_SOURCE
);
837 return Transmit(command
, !bWaitForResponse
, false);
840 bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
843 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_CEC_VERSION
);
845 return Transmit(command
, !bWaitForResponse
, false);
848 bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
851 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_MENU_LANGUAGE
);
853 return Transmit(command
, !bWaitForResponse
, false);
856 bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
859 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_OSD_NAME
);
861 return Transmit(command
, !bWaitForResponse
, false);
864 bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
867 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
);
869 return Transmit(command
, !bWaitForResponse
, false);
872 bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
875 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
);
877 return Transmit(command
, !bWaitForResponse
, false);
880 bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
883 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
885 return Transmit(command
, !bWaitForResponse
, false);
888 bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
, bool bIsReply
)
891 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
892 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
893 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
895 return Transmit(command
, false, bIsReply
);
898 bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_version cecVersion
, bool bIsReply
)
901 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_CEC_VERSION
);
902 command
.parameters
.PushBack((uint8_t)cecVersion
);
904 return Transmit(command
, false, bIsReply
);
907 bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
910 cec_command::Format(command
, iInitiator
, CECDEVICE_TV
, CEC_OPCODE_INACTIVE_SOURCE
);
911 command
.parameters
.PushBack((iPhysicalAddress
>> 8) & 0xFF);
912 command
.parameters
.PushBack(iPhysicalAddress
& 0xFF);
914 return Transmit(command
, false, false);
917 bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_menu_state menuState
, bool bIsReply
)
920 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_MENU_STATUS
);
921 command
.parameters
.PushBack((uint8_t)menuState
);
923 return Transmit(command
, false, bIsReply
);
926 bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, std::string strDeviceName
, bool bIsReply
)
929 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_NAME
);
930 for (size_t iPtr
= 0; iPtr
< strDeviceName
.length(); iPtr
++)
931 command
.parameters
.PushBack(strDeviceName
.at(iPtr
));
933 return Transmit(command
, false, bIsReply
);
936 bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_display_control duration
, const char *strMessage
, bool bIsReply
)
939 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_STRING
);
940 command
.parameters
.PushBack((uint8_t)duration
);
942 size_t iLen
= strlen(strMessage
);
943 if (iLen
> 13) iLen
= 13;
945 for (size_t iPtr
= 0; iPtr
< iLen
; iPtr
++)
946 command
.parameters
.PushBack(strMessage
[iPtr
]);
948 return Transmit(command
, false, bIsReply
);
951 bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
, cec_device_type type
, bool bIsReply
)
954 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
955 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
956 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
957 command
.parameters
.PushBack((uint8_t) (type
));
959 return Transmit(command
, false, bIsReply
);
962 bool CCECCommandHandler::TransmitSetMenuLanguage(const cec_logical_address iInitiator
, const char lang
[3], bool bIsReply
)
965 command
.Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_SET_MENU_LANGUAGE
);
966 command
.parameters
.PushBack((uint8_t) lang
[0]);
967 command
.parameters
.PushBack((uint8_t) lang
[1]);
968 command
.parameters
.PushBack((uint8_t) lang
[2]);
970 return Transmit(command
, false, bIsReply
);
973 bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bIsReply
)
976 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_NONE
);
978 return Transmit(command
, false, bIsReply
);
981 bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_power_status state
, bool bIsReply
)
984 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_POWER_STATUS
);
985 command
.parameters
.PushBack((uint8_t) state
);
987 return Transmit(command
, false, bIsReply
);
990 bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator
, uint64_t iVendorId
, bool bIsReply
)
993 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_DEVICE_VENDOR_ID
);
995 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 16) & 0xFF));
996 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 8) & 0xFF));
997 command
.parameters
.PushBack((uint8_t) ((uint64_t)iVendorId
& 0xFF));
999 return Transmit(command
, false, bIsReply
);
1002 bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, uint8_t state
, bool bIsReply
)
1004 cec_command command
;
1005 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_AUDIO_STATUS
);
1006 command
.parameters
.PushBack(state
);
1008 return Transmit(command
, false, bIsReply
);
1011 bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
, bool bIsReply
)
1013 cec_command command
;
1014 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
);
1015 command
.parameters
.PushBack((uint8_t)state
);
1017 return Transmit(command
, false, bIsReply
);
1020 bool CCECCommandHandler::TransmitSetStreamPath(uint16_t iStreamPath
, bool bIsReply
)
1022 if (m_busDevice
->GetLogicalAddress() != CECDEVICE_TV
)
1024 LIB_CEC
->AddLog(CEC_LOG_ERROR
, "only the TV is allowed to send CEC_OPCODE_SET_STREAM_PATH");
1027 cec_command command
;
1028 cec_command::Format(command
, m_busDevice
->GetLogicalAddress(), CECDEVICE_BROADCAST
, CEC_OPCODE_SET_STREAM_PATH
);
1029 command
.parameters
.PushBack((uint8_t) ((iStreamPath
>> 8) & 0xFF));
1030 command
.parameters
.PushBack((uint8_t) (iStreamPath
& 0xFF));
1032 return Transmit(command
, false, bIsReply
);
1035 bool CCECCommandHandler::TransmitSystemAudioModeStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
, bool bIsReply
)
1037 cec_command command
;
1038 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
);
1039 command
.parameters
.PushBack((uint8_t)state
);
1041 return Transmit(command
, false, bIsReply
);
1044 bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_deck_info state
, bool bIsReply
)
1046 cec_command command
;
1047 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_DECK_STATUS
);
1048 command
.PushBack((uint8_t)state
);
1050 return Transmit(command
, false, bIsReply
);
1053 bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = true */)
1055 cec_command command
;
1056 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_PRESSED
);
1057 command
.parameters
.PushBack((uint8_t)key
);
1059 return Transmit(command
, !bWait
, false);
1062 bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWait
/* = true */)
1064 cec_command command
;
1065 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_RELEASE
);
1067 return Transmit(command
, !bWait
, false);
1070 bool CCECCommandHandler::Transmit(cec_command
&command
, bool bSuppressWait
, bool bIsReply
)
1072 bool bReturn(false);
1073 cec_opcode
expectedResponse(cec_command::GetResponseOpcode(command
.opcode
));
1074 bool bExpectResponse(expectedResponse
!= CEC_OPCODE_NONE
&& !bSuppressWait
);
1075 command
.transmit_timeout
= m_iTransmitTimeout
;
1077 if (command
.initiator
== CECDEVICE_UNKNOWN
)
1079 LIB_CEC
->AddLog(CEC_LOG_ERROR
, "not transmitting a command without a valid initiator");
1084 uint8_t iTries(0), iMaxTries(!command
.opcode_set
? 1 : m_iTransmitRetries
+ 1);
1085 while (!bReturn
&& ++iTries
<= iMaxTries
&& !m_busDevice
->IsUnsupportedFeature(command
.opcode
))
1087 if ((bReturn
= m_processor
->Transmit(command
, bIsReply
)) == true)
1089 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "command transmitted");
1090 if (bExpectResponse
)
1092 bReturn
= m_busDevice
->WaitForOpcode(expectedResponse
);
1093 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, bReturn
? "expected response received (%X: %s)" : "expected response not received (%X: %s)", (int)expectedResponse
, ToString(expectedResponse
));
1102 bool CCECCommandHandler::ActivateSource(bool bTransmitDelayedCommandsOnly
/* = false */)
1104 if (m_busDevice
->IsActiveSource() &&
1105 m_busDevice
->IsHandledByLibCEC())
1108 CLockObject
lock(m_mutex
);
1109 // check if we need to send a delayed source switch
1110 if (bTransmitDelayedCommandsOnly
)
1112 if (m_iActiveSourcePending
== 0 || GetTimeMs() < m_iActiveSourcePending
)
1115 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "transmitting delayed activate source command");
1119 // update the power state and menu state
1120 m_busDevice
->SetPowerStatus(CEC_POWER_STATUS_ON
);
1121 m_busDevice
->SetMenuState(CEC_MENU_STATE_ACTIVATED
);
1123 // vendor specific hook
1124 VendorPreActivateSourceHook();
1127 bool bActiveSourceFailed(false);
1128 if (m_processor
->GetDevice(CECDEVICE_TV
)->GetPowerStatus(m_busDevice
->GetLogicalAddress()) != CEC_POWER_STATUS_ON
)
1129 bActiveSourceFailed
= !m_busDevice
->TransmitImageViewOn();
1131 // check if we're allowed to switch sources
1132 bool bSourceSwitchAllowed
= SourceSwitchAllowed();
1133 if (!bSourceSwitchAllowed
)
1134 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "source switch is currently not allowed by command handler");
1136 // switch sources (if allowed)
1137 if (!bActiveSourceFailed
&& bSourceSwitchAllowed
)
1139 bActiveSourceFailed
= !m_busDevice
->TransmitActiveSource(false) ||
1140 !m_busDevice
->TransmitMenuState(CECDEVICE_TV
, false);
1142 // update the deck status for playback devices
1143 if (!bActiveSourceFailed
)
1145 CCECPlaybackDevice
*playbackDevice
= m_busDevice
->AsPlaybackDevice();
1146 if (playbackDevice
&& SendDeckStatusUpdateOnActiveSource())
1147 bActiveSourceFailed
= !playbackDevice
->TransmitDeckStatus(CECDEVICE_TV
, false);
1152 if (bActiveSourceFailed
|| !bSourceSwitchAllowed
)
1154 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "failed to make '%s' the active source. will retry later", m_busDevice
->GetLogicalAddressName());
1159 CLockObject
lock(m_mutex
);
1160 // clear previous pending active source command
1161 m_iActiveSourcePending
= 0;
1164 // mark the handler as initialised
1165 CLockObject
lock(m_mutex
);
1166 m_bHandlerInited
= true;
1171 void CCECCommandHandler::ScheduleActivateSource(uint64_t iDelay
)
1173 CLockObject
lock(m_mutex
);
1174 m_iActiveSourcePending
= GetTimeMs() + iDelay
;