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 "../CECProcessor.h"
38 #include "../LibCEC.h"
42 using namespace PLATFORM
;
44 CCECCommandHandler::CCECCommandHandler(CCECBusDevice
*busDevice
) :
45 m_busDevice(busDevice
),
46 m_processor(m_busDevice
->GetProcessor()),
47 m_iTransmitTimeout(CEC_DEFAULT_TRANSMIT_TIMEOUT
),
48 m_iTransmitWait(CEC_DEFAULT_TRANSMIT_WAIT
),
49 m_iTransmitRetries(CEC_DEFAULT_TRANSMIT_RETRIES
),
50 m_bHandlerInited(false),
51 m_bOPTSendDeckStatusUpdateOnActiveSource(false),
52 m_vendorId(CEC_VENDOR_UNKNOWN
),
53 m_waitForResponse(new CWaitForResponse
)
57 CCECCommandHandler::~CCECCommandHandler(void)
59 delete m_waitForResponse
;
62 bool CCECCommandHandler::HandleCommand(const cec_command
&command
)
64 if (command
.opcode_set
== 0)
65 return HandlePoll(command
);
69 CLibCEC::AddCommand(command
);
71 switch(command
.opcode
)
73 case CEC_OPCODE_REPORT_POWER_STATUS
:
74 HandleReportPowerStatus(command
);
76 case CEC_OPCODE_CEC_VERSION
:
77 HandleDeviceCecVersion(command
);
79 case CEC_OPCODE_SET_MENU_LANGUAGE
:
80 HandleSetMenuLanguage(command
);
82 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
83 if (m_processor
->IsInitialised())
84 HandleGivePhysicalAddress(command
);
86 case CEC_OPCODE_GET_MENU_LANGUAGE
:
87 if (m_processor
->IsInitialised())
88 HandleGiveMenuLanguage(command
);
90 case CEC_OPCODE_GIVE_OSD_NAME
:
91 if (m_processor
->IsInitialised())
92 HandleGiveOSDName(command
);
94 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
95 if (m_processor
->IsInitialised())
96 HandleGiveDeviceVendorId(command
);
98 case CEC_OPCODE_DEVICE_VENDOR_ID
:
99 HandleDeviceVendorId(command
);
101 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
102 HandleDeviceVendorCommandWithId(command
);
104 case CEC_OPCODE_GIVE_DECK_STATUS
:
105 if (m_processor
->IsInitialised())
106 HandleGiveDeckStatus(command
);
108 case CEC_OPCODE_DECK_CONTROL
:
109 HandleDeckControl(command
);
111 case CEC_OPCODE_MENU_REQUEST
:
112 if (m_processor
->IsInitialised())
113 HandleMenuRequest(command
);
115 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
116 if (m_processor
->IsInitialised())
117 HandleGiveDevicePowerStatus(command
);
119 case CEC_OPCODE_GET_CEC_VERSION
:
120 if (m_processor
->IsInitialised())
121 HandleGetCecVersion(command
);
123 case CEC_OPCODE_USER_CONTROL_PRESSED
:
124 if (m_processor
->IsInitialised())
125 HandleUserControlPressed(command
);
127 case CEC_OPCODE_USER_CONTROL_RELEASE
:
128 if (m_processor
->IsInitialised())
129 HandleUserControlRelease(command
);
131 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
132 if (m_processor
->IsInitialised())
133 HandleGiveAudioStatus(command
);
135 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
136 if (m_processor
->IsInitialised())
137 HandleGiveSystemAudioModeStatus(command
);
139 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
140 if (m_processor
->IsInitialised())
141 HandleSystemAudioModeRequest(command
);
143 case CEC_OPCODE_REPORT_AUDIO_STATUS
:
144 HandleReportAudioStatus(command
);
146 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
:
147 HandleSystemAudioModeStatus(command
);
149 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
:
150 HandleSetSystemAudioMode(command
);
152 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
153 if (m_processor
->IsInitialised())
154 HandleRequestActiveSource(command
);
156 case CEC_OPCODE_SET_STREAM_PATH
:
157 HandleSetStreamPath(command
);
159 case CEC_OPCODE_ROUTING_CHANGE
:
160 HandleRoutingChange(command
);
162 case CEC_OPCODE_ROUTING_INFORMATION
:
163 HandleRoutingInformation(command
);
165 case CEC_OPCODE_STANDBY
:
166 if (m_processor
->IsInitialised())
167 HandleStandby(command
);
169 case CEC_OPCODE_ACTIVE_SOURCE
:
170 HandleActiveSource(command
);
172 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
:
173 HandleReportPhysicalAddress(command
);
175 case CEC_OPCODE_SET_OSD_NAME
:
176 HandleSetOSDName(command
);
178 case CEC_OPCODE_IMAGE_VIEW_ON
:
179 HandleImageViewOn(command
);
181 case CEC_OPCODE_TEXT_VIEW_ON
:
182 HandleTextViewOn(command
);
184 case CEC_OPCODE_FEATURE_ABORT
:
185 HandleFeatureAbort(command
);
187 case CEC_OPCODE_VENDOR_COMMAND
:
188 HandleVendorCommand(command
);
191 UnhandledCommand(command
);
197 m_waitForResponse
->Received((command
.opcode
== CEC_OPCODE_FEATURE_ABORT
&& command
.parameters
.size
> 0) ? (cec_opcode
)command
.parameters
[0] : command
.opcode
);
202 bool CCECCommandHandler::HandleActiveSource(const cec_command
&command
)
204 if (command
.parameters
.size
== 2)
206 uint16_t iAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
207 return m_processor
->SetActiveSource(iAddress
);
213 bool CCECCommandHandler::HandleDeckControl(const cec_command
&command
)
215 CCECBusDevice
*device
= GetDevice(command
.destination
);
216 if (device
&& (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
) && command
.parameters
.size
> 0)
218 ((CCECPlaybackDevice
*) device
)->SetDeckControlMode((cec_deck_control_mode
) command
.parameters
[0]);
225 bool CCECCommandHandler::HandleDeviceCecVersion(const cec_command
&command
)
227 if (command
.parameters
.size
== 1)
229 CCECBusDevice
*device
= GetDevice(command
.initiator
);
231 device
->SetCecVersion((cec_version
) command
.parameters
[0]);
237 bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command
&command
)
239 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
240 m_processor
->TransmitAbort(command
.initiator
, command
.opcode
, CEC_ABORT_REASON_REFUSED
);
245 bool CCECCommandHandler::HandleDeviceVendorId(const cec_command
&command
)
247 return SetVendorId(command
);
250 bool CCECCommandHandler::HandleFeatureAbort(const cec_command
&command
)
252 if (command
.parameters
.size
== 2 &&
253 (command
.parameters
[1] == CEC_ABORT_REASON_UNRECOGNIZED_OPCODE
||
254 command
.parameters
[1] == CEC_ABORT_REASON_REFUSED
))
255 m_processor
->m_busDevices
[command
.initiator
]->SetUnsupportedFeature((cec_opcode
)command
.parameters
[0]);
259 bool CCECCommandHandler::HandleGetCecVersion(const cec_command
&command
)
261 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
263 CCECBusDevice
*device
= GetDevice(command
.destination
);
265 return device
->TransmitCECVersion(command
.initiator
);
271 bool CCECCommandHandler::HandleGiveAudioStatus(const cec_command
&command
)
273 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
275 CCECBusDevice
*device
= GetDevice(command
.destination
);
276 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
277 return ((CCECAudioSystem
*) device
)->TransmitAudioStatus(command
.initiator
);
283 bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
285 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
287 CCECBusDevice
*device
= GetDevice(command
.destination
);
288 if (device
&& (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
))
289 return ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(command
.initiator
);
295 bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command
&command
)
297 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
299 CCECBusDevice
*device
= GetDevice(command
.destination
);
301 return device
->TransmitPowerState(command
.initiator
);
307 bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command
&command
)
309 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
311 CCECBusDevice
*device
= GetDevice(command
.destination
);
313 return device
->TransmitVendorID(command
.initiator
);
319 bool CCECCommandHandler::HandleGiveOSDName(const cec_command
&command
)
321 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
323 CCECBusDevice
*device
= GetDevice(command
.destination
);
325 return device
->TransmitOSDName(command
.initiator
);
331 bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command
&command
)
333 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
335 CCECBusDevice
*device
= GetDevice(command
.destination
);
337 return device
->TransmitPhysicalAddress();
343 bool CCECCommandHandler::HandleGiveMenuLanguage(const cec_command
&command
)
345 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
347 CCECBusDevice
*device
= GetDevice(command
.destination
);
349 return device
->TransmitSetMenuLanguage(command
.initiator
);
355 bool CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command
&command
)
357 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
359 CCECBusDevice
*device
= GetDevice(command
.destination
);
360 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
361 return ((CCECAudioSystem
*) device
)->TransmitSystemAudioModeStatus(command
.initiator
);
367 bool CCECCommandHandler::HandleImageViewOn(const cec_command
&command
)
369 m_processor
->m_busDevices
[command
.initiator
]->SetActiveSource();
373 bool CCECCommandHandler::HandleMenuRequest(const cec_command
&command
)
375 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
377 CCECBusDevice
*device
= GetDevice(command
.destination
);
380 if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_ACTIVATE
)
382 if (CLibCEC::MenuStateChanged(CEC_MENU_STATE_ACTIVATED
) == 1)
383 device
->SetMenuState(CEC_MENU_STATE_ACTIVATED
);
385 else if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_DEACTIVATE
)
387 if (CLibCEC::MenuStateChanged(CEC_MENU_STATE_DEACTIVATED
) == 1)
388 device
->SetMenuState(CEC_MENU_STATE_DEACTIVATED
);
390 return device
->TransmitMenuState(command
.initiator
);
397 bool CCECCommandHandler::HandlePoll(const cec_command
&command
)
399 m_busDevice
->HandlePoll(command
.destination
);
403 bool CCECCommandHandler::HandleReportAudioStatus(const cec_command
&command
)
405 if (command
.parameters
.size
== 1)
407 CCECBusDevice
*device
= GetDevice(command
.initiator
);
408 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
410 ((CCECAudioSystem
*)device
)->SetAudioStatus(command
.parameters
[0]);
417 bool CCECCommandHandler::HandleReportPhysicalAddress(const cec_command
&command
)
419 if (command
.parameters
.size
== 3)
421 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
422 SetPhysicalAddress(command
.initiator
, iNewAddress
);
427 bool CCECCommandHandler::HandleReportPowerStatus(const cec_command
&command
)
429 if (command
.parameters
.size
== 1)
431 CCECBusDevice
*device
= GetDevice(command
.initiator
);
433 device
->SetPowerStatus((cec_power_status
) command
.parameters
[0]);
438 bool CCECCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
440 if (m_processor
->IsRunning())
442 CLibCEC::AddLog(CEC_LOG_DEBUG
, ">> %i requests active source", (uint8_t) command
.initiator
);
444 vector
<CCECBusDevice
*> devices
;
445 for (size_t iDevicePtr
= 0; iDevicePtr
< GetMyDevices(devices
); iDevicePtr
++)
446 devices
[iDevicePtr
]->TransmitActiveSource();
453 bool CCECCommandHandler::HandleRoutingChange(const cec_command
&command
)
455 if (command
.parameters
.size
== 4)
457 uint16_t iOldAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
458 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[2] << 8) | ((uint16_t)command
.parameters
[3]);
460 CCECBusDevice
*device
= GetDevice(command
.initiator
);
462 device
->SetStreamPath(iNewAddress
, iOldAddress
);
464 CLibCEC::AddLog(CEC_LOG_DEBUG
, "initiator device not found");
469 bool CCECCommandHandler::HandleRoutingInformation(const cec_command
&command
)
471 if (command
.parameters
.size
== 2)
473 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
474 m_processor
->SetActiveSource(iNewAddress
);
480 bool CCECCommandHandler::HandleSetMenuLanguage(const cec_command
&command
)
482 if (command
.parameters
.size
== 3)
484 CCECBusDevice
*device
= GetDevice(command
.initiator
);
487 cec_menu_language language
;
488 language
.device
= command
.initiator
;
489 for (uint8_t iPtr
= 0; iPtr
< 4; iPtr
++)
490 language
.language
[iPtr
] = command
.parameters
[iPtr
];
491 language
.language
[3] = 0;
492 device
->SetMenuLanguage(language
);
499 bool CCECCommandHandler::HandleSetOSDName(const cec_command
&command
)
501 if (command
.parameters
.size
> 0)
503 CCECBusDevice
*device
= GetDevice(command
.initiator
);
507 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
508 buf
[iPtr
] = (char)command
.parameters
[iPtr
];
509 buf
[command
.parameters
.size
] = 0;
511 CStdString
strName(buf
);
512 device
->SetOSDName(strName
);
520 bool CCECCommandHandler::HandleSetStreamPath(const cec_command
&command
)
522 if (m_processor
->IsRunning() && command
.parameters
.size
>= 2)
524 uint16_t iStreamAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
525 CLibCEC::AddLog(CEC_LOG_DEBUG
, ">> %i sets stream path to physical address %04x", command
.initiator
, iStreamAddress
);
527 /* one of the device handled by libCEC has been made active */
528 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamAddress
);
529 if (device
&& m_busDevice
->MyLogicalAddressContains(device
->GetLogicalAddress()))
531 device
->SetActiveSource();
532 device
->TransmitImageViewOn();
533 device
->TransmitActiveSource();
535 device
->SetMenuState(CEC_MENU_STATE_ACTIVATED
);
536 device
->TransmitMenuState(command
.initiator
);
542 bool CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command
&command
)
544 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
546 CCECBusDevice
*device
= GetDevice(command
.destination
);
547 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
549 if (command
.parameters
.size
>= 2)
551 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
552 ((CCECAudioSystem
*) device
)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON
);
553 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
554 CCECBusDevice
*newActiveDevice
= GetDeviceByPhysicalAddress(iNewAddress
);
556 newActiveDevice
->SetActiveSource();
557 return ((CCECAudioSystem
*) device
)->TransmitSetSystemAudioMode(command
.initiator
);
561 ((CCECAudioSystem
*) device
)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF
);
562 return ((CCECAudioSystem
*) device
)->TransmitSetSystemAudioMode(command
.initiator
);
569 bool CCECCommandHandler::HandleStandby(const cec_command
&command
)
571 CCECBusDevice
*device
= GetDevice(command
.initiator
);
573 device
->SetPowerStatus(CEC_POWER_STATUS_STANDBY
);
578 bool CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command
&command
)
580 if (command
.parameters
.size
== 1)
582 CCECBusDevice
*device
= GetDevice(command
.initiator
);
583 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
585 ((CCECAudioSystem
*)device
)->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
593 bool CCECCommandHandler::HandleSetSystemAudioMode(const cec_command
&command
)
595 if (command
.parameters
.size
== 1)
597 CCECBusDevice
*device
= GetDevice(command
.initiator
);
598 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
600 ((CCECAudioSystem
*)device
)->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
608 bool CCECCommandHandler::HandleTextViewOn(const cec_command
&command
)
610 m_processor
->m_busDevices
[command
.initiator
]->SetActiveSource();
614 bool CCECCommandHandler::HandleUserControlPressed(const cec_command
&command
)
616 if (m_processor
->IsRunning() &&
617 m_busDevice
->MyLogicalAddressContains(command
.destination
) &&
618 command
.parameters
.size
> 0)
621 if (command
.parameters
[0] <= CEC_USER_CONTROL_CODE_MAX
)
622 CLibCEC::SetCurrentButton((cec_user_control_code
) command
.parameters
[0]);
624 if (command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER
||
625 command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION
)
628 CCECBusDevice
*device
= GetDevice(command
.destination
);
632 // CEC_USER_CONTROL_CODE_POWER operates as a toggle
633 // assume CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION does not
634 if (command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER
)
636 cec_power_status status
= device
->GetPowerStatus();
637 bPowerOn
= !(status
== CEC_POWER_STATUS_ON
|| status
== CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
642 device
->SetActiveSource();
643 device
->TransmitImageViewOn();
644 device
->TransmitActiveSource();
646 if (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
||
647 device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
648 ((CCECPlaybackDevice
*)device
)->TransmitDeckStatus(command
.initiator
);
652 device
->SetInactiveSource();
653 device
->TransmitInactiveSource();
654 device
->SetMenuState(CEC_MENU_STATE_DEACTIVATED
);
663 bool CCECCommandHandler::HandleUserControlRelease(const cec_command
&command
)
665 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
671 bool CCECCommandHandler::HandleVendorCommand(const cec_command
& UNUSED(command
))
676 void CCECCommandHandler::UnhandledCommand(const cec_command
&command
)
678 CLibCEC::AddLog(CEC_LOG_DEBUG
, "unhandled command with opcode %02x from address %d", command
.opcode
, command
.initiator
);
681 size_t CCECCommandHandler::GetMyDevices(vector
<CCECBusDevice
*> &devices
) const
685 cec_logical_addresses addresses
= m_processor
->GetLogicalAddresses();
686 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
690 devices
.push_back(GetDevice((cec_logical_address
) iPtr
));
698 CCECBusDevice
*CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress
) const
700 CCECBusDevice
*device
= NULL
;
702 if (iLogicalAddress
>= CECDEVICE_TV
&& iLogicalAddress
<= CECDEVICE_BROADCAST
)
703 device
= m_processor
->m_busDevices
[iLogicalAddress
];
708 CCECBusDevice
*CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
) const
710 return m_processor
->GetDeviceByPhysicalAddress(iPhysicalAddress
);
713 CCECBusDevice
*CCECCommandHandler::GetDeviceByType(cec_device_type type
) const
715 return m_processor
->GetDeviceByType(type
);
718 bool CCECCommandHandler::SetVendorId(const cec_command
&command
)
720 bool bChanged(false);
721 if (command
.parameters
.size
< 3)
723 CLibCEC::AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
727 uint64_t iVendorId
= ((uint64_t)command
.parameters
[0] << 16) +
728 ((uint64_t)command
.parameters
[1] << 8) +
729 (uint64_t)command
.parameters
[2];
731 CCECBusDevice
*device
= GetDevice((cec_logical_address
) command
.initiator
);
733 bChanged
= device
->SetVendorId(iVendorId
);
737 void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress
, uint16_t iNewAddress
)
739 if (!m_busDevice
->MyLogicalAddressContains(iAddress
))
741 bool bOurAddress(m_processor
->GetPhysicalAddress() == iNewAddress
);
742 GetDevice(iAddress
)->SetPhysicalAddress(iNewAddress
);
745 /* another device reported the same physical address as ours
746 * since we don't have physical address detection yet, we'll just use the
747 * given address, increased by 0x100 for now */
748 m_processor
->SetPhysicalAddress(iNewAddress
+ 0x100);
753 bool CCECCommandHandler::PowerOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
755 if (iDestination
== CECDEVICE_TV
)
756 return TransmitImageViewOn(iInitiator
, iDestination
);
758 return TransmitKeypress(iInitiator
, iDestination
, CEC_USER_CONTROL_CODE_POWER
) &&
759 TransmitKeyRelease(iInitiator
, iDestination
);
762 bool CCECCommandHandler::TransmitImageViewOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
765 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_IMAGE_VIEW_ON
);
767 return Transmit(command
);
770 bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
773 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_STANDBY
);
775 return Transmit(command
);
778 bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
781 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_CEC_VERSION
);
783 return Transmit(command
, !bWaitForResponse
);
786 bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
789 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_MENU_LANGUAGE
);
791 return Transmit(command
, !bWaitForResponse
);
794 bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
797 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_OSD_NAME
);
799 return Transmit(command
, !bWaitForResponse
);
802 bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
805 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
);
807 return Transmit(command
, !bWaitForResponse
);
810 bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
813 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
);
815 return Transmit(command
, !bWaitForResponse
);
818 bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
821 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
823 return Transmit(command
, !bWaitForResponse
);
826 bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
829 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
830 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
831 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
833 return Transmit(command
);
836 bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_version cecVersion
)
839 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_CEC_VERSION
);
840 command
.parameters
.PushBack((uint8_t)cecVersion
);
842 return Transmit(command
);
845 bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
848 cec_command::Format(command
, iInitiator
, CECDEVICE_TV
, CEC_OPCODE_INACTIVE_SOURCE
);
849 command
.parameters
.PushBack((iPhysicalAddress
>> 8) & 0xFF);
850 command
.parameters
.PushBack(iPhysicalAddress
& 0xFF);
852 return Transmit(command
);
855 bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_menu_state menuState
)
858 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_MENU_STATUS
);
859 command
.parameters
.PushBack((uint8_t)menuState
);
861 return Transmit(command
);
864 bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, CStdString strDeviceName
)
867 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_NAME
);
868 for (size_t iPtr
= 0; iPtr
< strDeviceName
.length(); iPtr
++)
869 command
.parameters
.PushBack(strDeviceName
.at(iPtr
));
871 return Transmit(command
);
874 bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_display_control duration
, const char *strMessage
)
877 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_STRING
);
878 command
.parameters
.PushBack((uint8_t)duration
);
880 size_t iLen
= strlen(strMessage
);
881 if (iLen
> 13) iLen
= 13;
883 for (size_t iPtr
= 0; iPtr
< iLen
; iPtr
++)
884 command
.parameters
.PushBack(strMessage
[iPtr
]);
886 return Transmit(command
);
889 bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
, cec_device_type type
)
892 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
893 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
894 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
895 command
.parameters
.PushBack((uint8_t) (type
));
897 return Transmit(command
);
900 bool CCECCommandHandler::TransmitSetMenuLanguage(const cec_logical_address iInitiator
, const char lang
[3])
903 command
.Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_SET_MENU_LANGUAGE
);
904 command
.parameters
.PushBack((uint8_t) lang
[0]);
905 command
.parameters
.PushBack((uint8_t) lang
[1]);
906 command
.parameters
.PushBack((uint8_t) lang
[2]);
908 return Transmit(command
);
911 bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
914 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_NONE
);
916 return Transmit(command
);
919 bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_power_status state
)
922 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_POWER_STATUS
);
923 command
.parameters
.PushBack((uint8_t) state
);
925 return Transmit(command
);
928 bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator
, uint64_t iVendorId
)
931 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_DEVICE_VENDOR_ID
);
933 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 16) & 0xFF));
934 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 8) & 0xFF));
935 command
.parameters
.PushBack((uint8_t) ((uint64_t)iVendorId
& 0xFF));
937 return Transmit(command
);
940 bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, uint8_t state
)
943 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_AUDIO_STATUS
);
944 command
.parameters
.PushBack(state
);
946 return Transmit(command
);
949 bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
)
952 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
);
953 command
.parameters
.PushBack((uint8_t)state
);
955 return Transmit(command
);
958 bool CCECCommandHandler::TransmitSetStreamPath(uint16_t iStreamPath
)
961 cec_command::Format(command
, m_busDevice
->GetLogicalAddress(), CECDEVICE_BROADCAST
, CEC_OPCODE_SET_STREAM_PATH
);
962 command
.parameters
.PushBack((uint8_t) ((iStreamPath
>> 8) & 0xFF));
963 command
.parameters
.PushBack((uint8_t) (iStreamPath
& 0xFF));
965 return Transmit(command
);
968 bool CCECCommandHandler::TransmitSystemAudioModeStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
)
971 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
);
972 command
.parameters
.PushBack((uint8_t)state
);
974 return Transmit(command
);
977 bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_deck_info state
)
980 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_DECK_STATUS
);
981 command
.PushBack((uint8_t)state
);
983 return Transmit(command
);
986 bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = true */)
989 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_PRESSED
);
990 command
.parameters
.PushBack((uint8_t)key
);
992 return Transmit(command
, !bWait
);
995 bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWait
/* = true */)
998 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_RELEASE
);
1000 return Transmit(command
, !bWait
);
1003 bool CCECCommandHandler::Transmit(cec_command
&command
, bool bSuppressWait
/* = false */)
1005 bool bReturn(false);
1006 cec_opcode
expectedResponse(cec_command::GetResponseOpcode(command
.opcode
));
1007 bool bExpectResponse(expectedResponse
!= CEC_OPCODE_NONE
&& !bSuppressWait
);
1008 command
.transmit_timeout
= m_iTransmitTimeout
;
1010 if (command
.initiator
== CECDEVICE_UNKNOWN
)
1012 CLibCEC::AddLog(CEC_LOG_ERROR
, "not transmitting a command without a valid initiator");
1017 uint8_t iTries(0), iMaxTries(!command
.opcode_set
? 1 : m_iTransmitRetries
+ 1);
1018 while (!bReturn
&& ++iTries
<= iMaxTries
&& !m_busDevice
->IsUnsupportedFeature(command
.opcode
))
1020 if ((bReturn
= m_processor
->Transmit(command
)) == true)
1022 CLibCEC::AddLog(CEC_LOG_DEBUG
, "command transmitted");
1023 if (bExpectResponse
)
1025 bReturn
= m_waitForResponse
->Wait(expectedResponse
);
1026 CLibCEC::AddLog(CEC_LOG_DEBUG
, bReturn
? "expected response received (%X: %s)" : "expected response not received (%X: %s)", (int)expectedResponse
, m_processor
->ToString(expectedResponse
));
1035 bool CCECCommandHandler::ActivateSource(void)
1037 if (m_busDevice
->IsActiveSource() &&
1038 m_busDevice
->GetStatus(false) == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
)
1040 m_busDevice
->SetPowerStatus(CEC_POWER_STATUS_ON
);
1041 m_busDevice
->SetMenuState(CEC_MENU_STATE_ACTIVATED
);
1043 m_busDevice
->TransmitImageViewOn();
1044 m_busDevice
->TransmitActiveSource();
1045 m_busDevice
->TransmitMenuState(CECDEVICE_TV
);
1046 if ((m_busDevice
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
||
1047 m_busDevice
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
) &&
1048 SendDeckStatusUpdateOnActiveSource())
1049 ((CCECPlaybackDevice
*)m_busDevice
)->TransmitDeckStatus(CECDEVICE_TV
);
1050 m_bHandlerInited
= true;
1055 void CCECCommandHandler::SignalOpcode(cec_opcode opcode
)
1057 m_waitForResponse
->Received(opcode
);