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_bActiveSourcePending(false)
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
&command
)
251 return CEC_ABORT_REASON_REFUSED
;
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
, ">> %i sets stream path to physical address %04x", command
.initiator
, iStreamAddress
);
566 /* one of the device handled by libCEC has been made active */
567 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamAddress
);
568 if (device
&& device
->IsHandledByLibCEC())
570 device
->ActivateSource();
571 return COMMAND_HANDLED
;
575 return CEC_ABORT_REASON_INVALID_OPERAND
;
578 int CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command
&command
)
580 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
582 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.destination
));
585 if (command
.parameters
.size
>= 2)
587 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
588 device
->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON
);
589 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
590 CCECBusDevice
*newActiveDevice
= GetDeviceByPhysicalAddress(iNewAddress
);
592 newActiveDevice
->MarkAsActiveSource();
593 if (device
->TransmitSetSystemAudioMode(command
.initiator
))
594 return COMMAND_HANDLED
;
598 device
->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF
);
599 if (device
->TransmitSetSystemAudioMode(command
.initiator
))
600 return COMMAND_HANDLED
;
605 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
608 int CCECCommandHandler::HandleStandby(const cec_command
&command
)
610 CCECBusDevice
*device
= GetDevice(command
.initiator
);
612 device
->SetPowerStatus(CEC_POWER_STATUS_STANDBY
);
614 return COMMAND_HANDLED
;
617 int CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command
&command
)
619 if (command
.parameters
.size
== 1)
621 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.initiator
));
624 device
->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
625 return COMMAND_HANDLED
;
629 return CEC_ABORT_REASON_INVALID_OPERAND
;
632 int CCECCommandHandler::HandleSetSystemAudioMode(const cec_command
&command
)
634 if (command
.parameters
.size
== 1)
636 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.initiator
));
639 device
->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
640 return COMMAND_HANDLED
;
644 return CEC_ABORT_REASON_INVALID_OPERAND
;
647 int CCECCommandHandler::HandleTextViewOn(const cec_command
&command
)
649 m_processor
->GetDevice(command
.initiator
)->MarkAsActiveSource();
650 return COMMAND_HANDLED
;
653 int CCECCommandHandler::HandleUserControlPressed(const cec_command
&command
)
655 if (!m_processor
->CECInitialised() ||
656 !m_processor
->IsHandledByLibCEC(command
.destination
))
657 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
659 if (command
.parameters
.size
== 0)
660 return CEC_ABORT_REASON_INVALID_OPERAND
;
662 CCECBusDevice
*device
= GetDevice(command
.destination
);
664 return CEC_ABORT_REASON_INVALID_OPERAND
;
666 CCECClient
*client
= device
->GetClient();
670 if (command
.parameters
[0] <= CEC_USER_CONTROL_CODE_MAX
)
671 client
->SetCurrentButton((cec_user_control_code
) command
.parameters
[0]);
673 if (command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER
||
674 command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION
)
678 // CEC_USER_CONTROL_CODE_POWER operates as a toggle
679 // assume CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION does not
680 if (command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER
)
682 cec_power_status status
= device
->GetCurrentPowerStatus();
683 bPowerOn
= !(status
== CEC_POWER_STATUS_ON
|| status
== CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
688 device
->ActivateSource();
692 device
->MarkAsInactiveSource();
693 device
->TransmitInactiveSource();
694 device
->SetMenuState(CEC_MENU_STATE_DEACTIVATED
);
698 return COMMAND_HANDLED
;
701 int CCECCommandHandler::HandleUserControlRelease(const cec_command
&command
)
703 if (!m_processor
->CECInitialised() ||
704 !m_processor
->IsHandledByLibCEC(command
.destination
))
705 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
707 CCECClient
*client
= m_processor
->GetClient(command
.destination
);
711 return COMMAND_HANDLED
;
714 int CCECCommandHandler::HandleVendorCommand(const cec_command
&command
)
716 return CEC_ABORT_REASON_REFUSED
;
719 void CCECCommandHandler::UnhandledCommand(const cec_command
&command
, const cec_abort_reason reason
)
721 if (m_processor
->IsHandledByLibCEC(command
.destination
))
723 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "sending abort with opcode %02x and reason '%s' to %s", command
.opcode
, ToString(reason
), ToString(command
.initiator
));
724 m_processor
->TransmitAbort(command
.destination
, command
.initiator
, command
.opcode
, reason
);
728 size_t CCECCommandHandler::GetMyDevices(vector
<CCECBusDevice
*> &devices
) const
732 cec_logical_addresses addresses
= m_processor
->GetLogicalAddresses();
733 for (uint8_t iPtr
= CECDEVICE_TV
; iPtr
< CECDEVICE_BROADCAST
; iPtr
++)
737 devices
.push_back(GetDevice((cec_logical_address
) iPtr
));
745 CCECBusDevice
*CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress
) const
747 return m_processor
->GetDevice(iLogicalAddress
);
750 CCECBusDevice
*CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
) const
752 return m_processor
->GetDeviceByPhysicalAddress(iPhysicalAddress
);
755 bool CCECCommandHandler::SetVendorId(const cec_command
&command
)
757 bool bChanged(false);
758 if (command
.parameters
.size
< 3)
760 LIB_CEC
->AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
764 uint64_t iVendorId
= ((uint64_t)command
.parameters
[0] << 16) +
765 ((uint64_t)command
.parameters
[1] << 8) +
766 (uint64_t)command
.parameters
[2];
768 CCECBusDevice
*device
= GetDevice((cec_logical_address
) command
.initiator
);
770 bChanged
= device
->SetVendorId(iVendorId
);
774 void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress
, uint16_t iNewAddress
)
776 if (!m_processor
->IsHandledByLibCEC(iAddress
))
778 CCECBusDevice
*otherDevice
= m_processor
->GetDeviceByPhysicalAddress(iNewAddress
);
779 CCECClient
*client
= otherDevice
? otherDevice
->GetClient() : NULL
;
781 CCECBusDevice
*device
= m_processor
->GetDevice(iAddress
);
783 device
->SetPhysicalAddress(iNewAddress
);
786 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "device with logical address %X not found", iAddress
);
789 /* another device reported the same physical address as ours */
791 client
->ResetPhysicalAddress();
795 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
);
799 bool CCECCommandHandler::PowerOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
801 if (iDestination
== CECDEVICE_TV
)
802 return TransmitImageViewOn(iInitiator
, iDestination
);
804 return TransmitKeypress(iInitiator
, iDestination
, CEC_USER_CONTROL_CODE_POWER
) &&
805 TransmitKeyRelease(iInitiator
, iDestination
);
808 bool CCECCommandHandler::TransmitImageViewOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
811 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_IMAGE_VIEW_ON
);
813 return Transmit(command
);
816 bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
819 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_STANDBY
);
821 return Transmit(command
);
824 bool CCECCommandHandler::TransmitRequestActiveSource(const cec_logical_address iInitiator
, bool bWaitForResponse
/* = true */)
827 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_REQUEST_ACTIVE_SOURCE
);
829 return Transmit(command
, !bWaitForResponse
);
832 bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
835 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_CEC_VERSION
);
837 return Transmit(command
, !bWaitForResponse
);
840 bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
843 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_MENU_LANGUAGE
);
845 return Transmit(command
, !bWaitForResponse
);
848 bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
851 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_OSD_NAME
);
853 return Transmit(command
, !bWaitForResponse
);
856 bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
859 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
);
861 return Transmit(command
, !bWaitForResponse
);
864 bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
867 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
);
869 return Transmit(command
, !bWaitForResponse
);
872 bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
875 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
877 return Transmit(command
, !bWaitForResponse
);
880 bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
883 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
884 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
885 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
887 return Transmit(command
);
890 bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_version cecVersion
)
893 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_CEC_VERSION
);
894 command
.parameters
.PushBack((uint8_t)cecVersion
);
896 return Transmit(command
);
899 bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
902 cec_command::Format(command
, iInitiator
, CECDEVICE_TV
, CEC_OPCODE_INACTIVE_SOURCE
);
903 command
.parameters
.PushBack((iPhysicalAddress
>> 8) & 0xFF);
904 command
.parameters
.PushBack(iPhysicalAddress
& 0xFF);
906 return Transmit(command
);
909 bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_menu_state menuState
)
912 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_MENU_STATUS
);
913 command
.parameters
.PushBack((uint8_t)menuState
);
915 return Transmit(command
);
918 bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, CStdString strDeviceName
)
921 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_NAME
);
922 for (size_t iPtr
= 0; iPtr
< strDeviceName
.length(); iPtr
++)
923 command
.parameters
.PushBack(strDeviceName
.at(iPtr
));
925 return Transmit(command
);
928 bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_display_control duration
, const char *strMessage
)
931 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_STRING
);
932 command
.parameters
.PushBack((uint8_t)duration
);
934 size_t iLen
= strlen(strMessage
);
935 if (iLen
> 13) iLen
= 13;
937 for (size_t iPtr
= 0; iPtr
< iLen
; iPtr
++)
938 command
.parameters
.PushBack(strMessage
[iPtr
]);
940 return Transmit(command
);
943 bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
, cec_device_type type
)
946 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
947 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
948 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
949 command
.parameters
.PushBack((uint8_t) (type
));
951 return Transmit(command
);
954 bool CCECCommandHandler::TransmitSetMenuLanguage(const cec_logical_address iInitiator
, const char lang
[3])
957 command
.Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_SET_MENU_LANGUAGE
);
958 command
.parameters
.PushBack((uint8_t) lang
[0]);
959 command
.parameters
.PushBack((uint8_t) lang
[1]);
960 command
.parameters
.PushBack((uint8_t) lang
[2]);
962 return Transmit(command
);
965 bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
968 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_NONE
);
970 return Transmit(command
);
973 bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_power_status state
)
976 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_POWER_STATUS
);
977 command
.parameters
.PushBack((uint8_t) state
);
979 return Transmit(command
);
982 bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator
, uint64_t iVendorId
)
985 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_DEVICE_VENDOR_ID
);
987 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 16) & 0xFF));
988 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 8) & 0xFF));
989 command
.parameters
.PushBack((uint8_t) ((uint64_t)iVendorId
& 0xFF));
991 return Transmit(command
);
994 bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, uint8_t state
)
997 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_AUDIO_STATUS
);
998 command
.parameters
.PushBack(state
);
1000 return Transmit(command
);
1003 bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
)
1005 cec_command command
;
1006 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
);
1007 command
.parameters
.PushBack((uint8_t)state
);
1009 return Transmit(command
);
1012 bool CCECCommandHandler::TransmitSetStreamPath(uint16_t iStreamPath
)
1014 cec_command command
;
1015 cec_command::Format(command
, m_busDevice
->GetLogicalAddress(), CECDEVICE_BROADCAST
, CEC_OPCODE_SET_STREAM_PATH
);
1016 command
.parameters
.PushBack((uint8_t) ((iStreamPath
>> 8) & 0xFF));
1017 command
.parameters
.PushBack((uint8_t) (iStreamPath
& 0xFF));
1019 return Transmit(command
);
1022 bool CCECCommandHandler::TransmitSystemAudioModeStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
)
1024 cec_command command
;
1025 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
);
1026 command
.parameters
.PushBack((uint8_t)state
);
1028 return Transmit(command
);
1031 bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_deck_info state
)
1033 cec_command command
;
1034 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_DECK_STATUS
);
1035 command
.PushBack((uint8_t)state
);
1037 return Transmit(command
);
1040 bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = true */)
1042 cec_command command
;
1043 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_PRESSED
);
1044 command
.parameters
.PushBack((uint8_t)key
);
1046 return Transmit(command
, !bWait
);
1049 bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWait
/* = true */)
1051 cec_command command
;
1052 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_RELEASE
);
1054 return Transmit(command
, !bWait
);
1057 bool CCECCommandHandler::Transmit(cec_command
&command
, bool bSuppressWait
/* = false */)
1059 bool bReturn(false);
1060 cec_opcode
expectedResponse(cec_command::GetResponseOpcode(command
.opcode
));
1061 bool bExpectResponse(expectedResponse
!= CEC_OPCODE_NONE
&& !bSuppressWait
);
1062 command
.transmit_timeout
= m_iTransmitTimeout
;
1064 if (command
.initiator
== CECDEVICE_UNKNOWN
)
1066 LIB_CEC
->AddLog(CEC_LOG_ERROR
, "not transmitting a command without a valid initiator");
1071 uint8_t iTries(0), iMaxTries(!command
.opcode_set
? 1 : m_iTransmitRetries
+ 1);
1072 while (!bReturn
&& ++iTries
<= iMaxTries
&& !m_busDevice
->IsUnsupportedFeature(command
.opcode
))
1074 if ((bReturn
= m_processor
->Transmit(command
)) == true)
1076 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "command transmitted");
1077 if (bExpectResponse
)
1079 bReturn
= m_waitForResponse
->Wait(expectedResponse
);
1080 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, bReturn
? "expected response received (%X: %s)" : "expected response not received (%X: %s)", (int)expectedResponse
, ToString(expectedResponse
));
1089 bool CCECCommandHandler::ActivateSource(void)
1091 if (m_busDevice
->IsActiveSource() &&
1092 m_busDevice
->IsHandledByLibCEC())
1095 CLockObject
lock(m_mutex
);
1096 m_bActiveSourcePending
= false;
1099 m_busDevice
->SetPowerStatus(CEC_POWER_STATUS_ON
);
1100 m_busDevice
->SetMenuState(CEC_MENU_STATE_ACTIVATED
);
1102 bool bActiveSourceFailed
= !m_busDevice
->TransmitImageViewOn() ||
1103 !m_busDevice
->TransmitActiveSource() ||
1104 !m_busDevice
->TransmitMenuState(CECDEVICE_TV
);
1106 if (!bActiveSourceFailed
)
1108 CCECPlaybackDevice
*playbackDevice
= m_busDevice
->AsPlaybackDevice();
1109 if (playbackDevice
&& SendDeckStatusUpdateOnActiveSource())
1110 bActiveSourceFailed
= !playbackDevice
->TransmitDeckStatus(CECDEVICE_TV
);
1113 if (bActiveSourceFailed
)
1115 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "failed to make '%s' the active source. will retry later", m_busDevice
->GetLogicalAddressName());
1116 CLockObject
lock(m_mutex
);
1117 m_bActiveSourcePending
= true;
1121 m_bHandlerInited
= true;
1126 void CCECCommandHandler::SignalOpcode(cec_opcode opcode
)
1128 m_waitForResponse
->Received(opcode
);
1131 bool CCECCommandHandler::ActiveSourcePending(void)
1133 CLockObject
lock(m_mutex
);
1134 return m_bActiveSourcePending
;