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
)
66 CLibCEC::AddCommand(command
);
68 switch(command
.opcode
)
70 case CEC_OPCODE_REPORT_POWER_STATUS
:
71 HandleReportPowerStatus(command
);
73 case CEC_OPCODE_CEC_VERSION
:
74 HandleDeviceCecVersion(command
);
76 case CEC_OPCODE_SET_MENU_LANGUAGE
:
77 HandleSetMenuLanguage(command
);
79 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
80 if (m_processor
->IsInitialised())
81 HandleGivePhysicalAddress(command
);
83 case CEC_OPCODE_GIVE_OSD_NAME
:
84 if (m_processor
->IsInitialised())
85 HandleGiveOSDName(command
);
87 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
88 if (m_processor
->IsInitialised())
89 HandleGiveDeviceVendorId(command
);
91 case CEC_OPCODE_DEVICE_VENDOR_ID
:
92 HandleDeviceVendorId(command
);
94 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
95 HandleDeviceVendorCommandWithId(command
);
97 case CEC_OPCODE_GIVE_DECK_STATUS
:
98 if (m_processor
->IsInitialised())
99 HandleGiveDeckStatus(command
);
101 case CEC_OPCODE_DECK_CONTROL
:
102 HandleDeckControl(command
);
104 case CEC_OPCODE_MENU_REQUEST
:
105 if (m_processor
->IsInitialised())
106 HandleMenuRequest(command
);
108 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
109 if (m_processor
->IsInitialised())
110 HandleGiveDevicePowerStatus(command
);
112 case CEC_OPCODE_GET_CEC_VERSION
:
113 if (m_processor
->IsInitialised())
114 HandleGetCecVersion(command
);
116 case CEC_OPCODE_USER_CONTROL_PRESSED
:
117 if (m_processor
->IsInitialised())
118 HandleUserControlPressed(command
);
120 case CEC_OPCODE_USER_CONTROL_RELEASE
:
121 if (m_processor
->IsInitialised())
122 HandleUserControlRelease(command
);
124 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
125 if (m_processor
->IsInitialised())
126 HandleGiveAudioStatus(command
);
128 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
129 if (m_processor
->IsInitialised())
130 HandleGiveSystemAudioModeStatus(command
);
132 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
133 if (m_processor
->IsInitialised())
134 HandleSystemAudioModeRequest(command
);
136 case CEC_OPCODE_REPORT_AUDIO_STATUS
:
137 HandleReportAudioStatus(command
);
139 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
:
140 HandleSystemAudioModeStatus(command
);
142 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
:
143 HandleSetSystemAudioMode(command
);
145 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
146 if (m_processor
->IsInitialised())
147 HandleRequestActiveSource(command
);
149 case CEC_OPCODE_SET_STREAM_PATH
:
150 HandleSetStreamPath(command
);
152 case CEC_OPCODE_ROUTING_CHANGE
:
153 HandleRoutingChange(command
);
155 case CEC_OPCODE_ROUTING_INFORMATION
:
156 HandleRoutingInformation(command
);
158 case CEC_OPCODE_STANDBY
:
159 if (m_processor
->IsInitialised())
160 HandleStandby(command
);
162 case CEC_OPCODE_ACTIVE_SOURCE
:
163 HandleActiveSource(command
);
165 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
:
166 HandleReportPhysicalAddress(command
);
168 case CEC_OPCODE_SET_OSD_NAME
:
169 HandleSetOSDName(command
);
171 case CEC_OPCODE_IMAGE_VIEW_ON
:
172 HandleImageViewOn(command
);
174 case CEC_OPCODE_TEXT_VIEW_ON
:
175 HandleTextViewOn(command
);
177 case CEC_OPCODE_FEATURE_ABORT
:
178 HandleFeatureAbort(command
);
180 case CEC_OPCODE_VENDOR_COMMAND
:
181 HandleVendorCommand(command
);
184 UnhandledCommand(command
);
190 m_waitForResponse
->Received((command
.opcode
== CEC_OPCODE_FEATURE_ABORT
&& command
.parameters
.size
> 0) ? (cec_opcode
)command
.parameters
[0] : command
.opcode
);
195 bool CCECCommandHandler::HandleActiveSource(const cec_command
&command
)
197 if (command
.parameters
.size
== 2)
199 uint16_t iAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
200 return m_processor
->SetActiveSource(iAddress
);
206 bool CCECCommandHandler::HandleDeckControl(const cec_command
&command
)
208 CCECBusDevice
*device
= GetDevice(command
.destination
);
209 if (device
&& (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
) && command
.parameters
.size
> 0)
211 ((CCECPlaybackDevice
*) device
)->SetDeckControlMode((cec_deck_control_mode
) command
.parameters
[0]);
218 bool CCECCommandHandler::HandleDeviceCecVersion(const cec_command
&command
)
220 if (command
.parameters
.size
== 1)
222 CCECBusDevice
*device
= GetDevice(command
.initiator
);
224 device
->SetCecVersion((cec_version
) command
.parameters
[0]);
230 bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command
&command
)
232 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
233 m_processor
->TransmitAbort(command
.initiator
, command
.opcode
, CEC_ABORT_REASON_REFUSED
);
238 bool CCECCommandHandler::HandleDeviceVendorId(const cec_command
&command
)
240 return SetVendorId(command
);
243 bool CCECCommandHandler::HandleFeatureAbort(const cec_command
&command
)
245 if (command
.parameters
.size
== 2 && command
.parameters
[1] == CEC_ABORT_REASON_UNRECOGNIZED_OPCODE
)
246 m_processor
->m_busDevices
[command
.initiator
]->SetUnsupportedFeature((cec_opcode
)command
.parameters
[0]);
250 bool CCECCommandHandler::HandleGetCecVersion(const cec_command
&command
)
252 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
254 CCECBusDevice
*device
= GetDevice(command
.destination
);
256 return device
->TransmitCECVersion(command
.initiator
);
262 bool CCECCommandHandler::HandleGiveAudioStatus(const cec_command
&command
)
264 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
266 CCECBusDevice
*device
= GetDevice(command
.destination
);
267 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
268 return ((CCECAudioSystem
*) device
)->TransmitAudioStatus(command
.initiator
);
274 bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
276 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
278 CCECBusDevice
*device
= GetDevice(command
.destination
);
279 if (device
&& (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
))
280 return ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(command
.initiator
);
286 bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command
&command
)
288 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
290 CCECBusDevice
*device
= GetDevice(command
.destination
);
292 return device
->TransmitPowerState(command
.initiator
);
298 bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command
&command
)
300 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
302 CCECBusDevice
*device
= GetDevice(command
.destination
);
304 return device
->TransmitVendorID(command
.initiator
);
310 bool CCECCommandHandler::HandleGiveOSDName(const cec_command
&command
)
312 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
314 CCECBusDevice
*device
= GetDevice(command
.destination
);
316 return device
->TransmitOSDName(command
.initiator
);
322 bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command
&command
)
324 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
326 CCECBusDevice
*device
= GetDevice(command
.destination
);
329 device
->SetActiveSource();
330 return device
->TransmitPhysicalAddress() &&
331 device
->TransmitImageViewOn() &&
332 device
->TransmitActiveSource();
339 bool CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command
&command
)
341 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
343 CCECBusDevice
*device
= GetDevice(command
.destination
);
344 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
345 return ((CCECAudioSystem
*) device
)->TransmitSystemAudioModeStatus(command
.initiator
);
351 bool CCECCommandHandler::HandleImageViewOn(const cec_command
&command
)
353 m_processor
->m_busDevices
[command
.initiator
]->SetActiveSource();
357 bool CCECCommandHandler::HandleMenuRequest(const cec_command
&command
)
359 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
361 if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_QUERY
)
363 CCECBusDevice
*device
= GetDevice(command
.destination
);
365 return device
->TransmitMenuState(command
.initiator
);
372 bool CCECCommandHandler::HandleReportAudioStatus(const cec_command
&command
)
374 if (command
.parameters
.size
== 1)
376 CCECBusDevice
*device
= GetDevice(command
.initiator
);
377 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
379 ((CCECAudioSystem
*)device
)->SetAudioStatus(command
.parameters
[0]);
386 bool CCECCommandHandler::HandleReportPhysicalAddress(const cec_command
&command
)
388 if (command
.parameters
.size
== 3)
390 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
391 SetPhysicalAddress(command
.initiator
, iNewAddress
);
396 bool CCECCommandHandler::HandleReportPowerStatus(const cec_command
&command
)
398 if (command
.parameters
.size
== 1)
400 CCECBusDevice
*device
= GetDevice(command
.initiator
);
402 device
->SetPowerStatus((cec_power_status
) command
.parameters
[0]);
407 bool CCECCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
409 if (m_processor
->IsRunning())
411 CLibCEC::AddLog(CEC_LOG_DEBUG
, ">> %i requests active source", (uint8_t) command
.initiator
);
413 vector
<CCECBusDevice
*> devices
;
414 for (size_t iDevicePtr
= 0; iDevicePtr
< GetMyDevices(devices
); iDevicePtr
++)
415 devices
[iDevicePtr
]->TransmitActiveSource();
422 bool CCECCommandHandler::HandleRoutingChange(const cec_command
&command
)
424 if (command
.parameters
.size
== 4)
426 uint16_t iOldAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
427 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[2] << 8) | ((uint16_t)command
.parameters
[3]);
429 CCECBusDevice
*device
= GetDevice(command
.initiator
);
431 device
->SetStreamPath(iNewAddress
, iOldAddress
);
436 bool CCECCommandHandler::HandleRoutingInformation(const cec_command
&command
)
438 if (command
.parameters
.size
== 2)
440 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
441 m_processor
->SetActiveSource(iNewAddress
);
447 bool CCECCommandHandler::HandleSetMenuLanguage(const cec_command
&command
)
449 if (command
.parameters
.size
== 3)
451 CCECBusDevice
*device
= GetDevice(command
.initiator
);
454 cec_menu_language language
;
455 language
.device
= command
.initiator
;
456 for (uint8_t iPtr
= 0; iPtr
< 4; iPtr
++)
457 language
.language
[iPtr
] = command
.parameters
[iPtr
];
458 language
.language
[3] = 0;
459 device
->SetMenuLanguage(language
);
466 bool CCECCommandHandler::HandleSetOSDName(const cec_command
&command
)
468 if (command
.parameters
.size
> 0)
470 CCECBusDevice
*device
= GetDevice(command
.initiator
);
474 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
475 buf
[iPtr
] = (char)command
.parameters
[iPtr
];
476 buf
[command
.parameters
.size
] = 0;
478 CStdString
strName(buf
);
479 device
->SetOSDName(strName
);
487 bool CCECCommandHandler::HandleSetStreamPath(const cec_command
&command
)
489 if (m_processor
->IsRunning() && command
.parameters
.size
>= 2)
491 uint16_t iStreamAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
492 CLibCEC::AddLog(CEC_LOG_DEBUG
, ">> %i sets stream path to physical address %04x", command
.initiator
, iStreamAddress
);
494 /* one of the device handled by libCEC has been made active */
495 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamAddress
);
496 if (device
&& m_busDevice
->MyLogicalAddressContains(device
->GetLogicalAddress()))
498 device
->SetActiveSource();
499 device
->TransmitImageViewOn();
500 device
->TransmitActiveSource();
502 device
->SetMenuState(CEC_MENU_STATE_ACTIVATED
);
503 device
->TransmitMenuState(command
.initiator
);
509 bool CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command
&command
)
511 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
513 CCECBusDevice
*device
= GetDevice(command
.destination
);
514 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
516 if (command
.parameters
.size
>= 2)
518 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
519 ((CCECAudioSystem
*) device
)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON
);
520 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
521 CCECBusDevice
*newActiveDevice
= GetDeviceByPhysicalAddress(iNewAddress
);
523 newActiveDevice
->SetActiveSource();
524 return ((CCECAudioSystem
*) device
)->TransmitSetSystemAudioMode(command
.initiator
);
528 ((CCECAudioSystem
*) device
)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF
);
529 return ((CCECAudioSystem
*) device
)->TransmitSetSystemAudioMode(command
.initiator
);
536 bool CCECCommandHandler::HandleStandby(const cec_command
&command
)
538 CCECBusDevice
*device
= GetDevice(command
.initiator
);
540 device
->SetPowerStatus(CEC_POWER_STATUS_STANDBY
);
545 bool CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command
&command
)
547 if (command
.parameters
.size
== 1)
549 CCECBusDevice
*device
= GetDevice(command
.initiator
);
550 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
552 ((CCECAudioSystem
*)device
)->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
560 bool CCECCommandHandler::HandleSetSystemAudioMode(const cec_command
&command
)
562 if (command
.parameters
.size
== 1)
564 CCECBusDevice
*device
= GetDevice(command
.initiator
);
565 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
567 ((CCECAudioSystem
*)device
)->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
575 bool CCECCommandHandler::HandleTextViewOn(const cec_command
&command
)
577 m_processor
->m_busDevices
[command
.initiator
]->SetActiveSource();
581 bool CCECCommandHandler::HandleUserControlPressed(const cec_command
&command
)
583 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
) && command
.parameters
.size
> 0)
587 if (command
.parameters
[0] <= CEC_USER_CONTROL_CODE_MAX
)
589 if (command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER
||
590 command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION
)
592 CCECBusDevice
*device
= GetDevice(command
.destination
);
595 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
596 if (device
->MyLogicalAddressContains(device
->GetLogicalAddress()))
598 device
->SetActiveSource();
599 device
->TransmitImageViewOn();
600 device
->TransmitActiveSource();
602 if (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
||
603 device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
604 ((CCECPlaybackDevice
*)device
)->TransmitDeckStatus(command
.initiator
);
610 CLibCEC::SetCurrentButton((cec_user_control_code
) command
.parameters
[0]);
618 bool CCECCommandHandler::HandleUserControlRelease(const cec_command
&command
)
620 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
626 bool CCECCommandHandler::HandleVendorCommand(const cec_command
& UNUSED(command
))
631 void CCECCommandHandler::UnhandledCommand(const cec_command
&command
)
633 CLibCEC::AddLog(CEC_LOG_DEBUG
, "unhandled command with opcode %02x from address %d", command
.opcode
, command
.initiator
);
636 size_t CCECCommandHandler::GetMyDevices(vector
<CCECBusDevice
*> &devices
) const
640 cec_logical_addresses addresses
= m_processor
->GetLogicalAddresses();
641 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
645 devices
.push_back(GetDevice((cec_logical_address
) iPtr
));
653 CCECBusDevice
*CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress
) const
655 CCECBusDevice
*device
= NULL
;
657 if (iLogicalAddress
>= CECDEVICE_TV
&& iLogicalAddress
<= CECDEVICE_BROADCAST
)
658 device
= m_processor
->m_busDevices
[iLogicalAddress
];
663 CCECBusDevice
*CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
) const
665 return m_processor
->GetDeviceByPhysicalAddress(iPhysicalAddress
);
668 CCECBusDevice
*CCECCommandHandler::GetDeviceByType(cec_device_type type
) const
670 return m_processor
->GetDeviceByType(type
);
673 bool CCECCommandHandler::SetVendorId(const cec_command
&command
)
675 bool bChanged(false);
676 if (command
.parameters
.size
< 3)
678 CLibCEC::AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
682 uint64_t iVendorId
= ((uint64_t)command
.parameters
[0] << 16) +
683 ((uint64_t)command
.parameters
[1] << 8) +
684 (uint64_t)command
.parameters
[2];
686 CCECBusDevice
*device
= GetDevice((cec_logical_address
) command
.initiator
);
688 bChanged
= device
->SetVendorId(iVendorId
);
692 void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress
, uint16_t iNewAddress
)
694 if (!m_busDevice
->MyLogicalAddressContains(iAddress
))
696 bool bOurAddress(m_processor
->GetPhysicalAddress() == iNewAddress
);
697 GetDevice(iAddress
)->SetPhysicalAddress(iNewAddress
);
700 /* another device reported the same physical address as ours
701 * since we don't have physical address detection yet, we'll just use the
702 * given address, increased by 0x100 for now */
703 m_processor
->SetPhysicalAddress(iNewAddress
+ 0x100);
708 bool CCECCommandHandler::PowerOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
710 if (iDestination
== CECDEVICE_TV
)
711 return TransmitImageViewOn(iInitiator
, iDestination
);
713 return TransmitKeypress(iInitiator
, iDestination
, CEC_USER_CONTROL_CODE_POWER
) &&
714 TransmitKeyRelease(iInitiator
, iDestination
);
717 bool CCECCommandHandler::TransmitImageViewOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
720 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_IMAGE_VIEW_ON
);
722 return Transmit(command
, false);
725 bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
728 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_STANDBY
);
730 return Transmit(command
, false);
733 bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
736 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_CEC_VERSION
);
738 return Transmit(command
, true, CEC_OPCODE_CEC_VERSION
);
741 bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
744 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_MENU_LANGUAGE
);
746 return Transmit(command
, true, CEC_OPCODE_SET_MENU_LANGUAGE
);
749 bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
752 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_OSD_NAME
);
754 return Transmit(command
, true, CEC_OPCODE_SET_OSD_NAME
);
757 bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
760 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
);
762 return Transmit(command
, true, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
765 bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
768 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
);
770 return Transmit(command
, true, CEC_OPCODE_REPORT_POWER_STATUS
);
773 bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
776 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
778 return Transmit(command
, true, CEC_OPCODE_DEVICE_VENDOR_ID
);
781 bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
784 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
785 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
786 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
788 return Transmit(command
, false);
791 bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_version cecVersion
)
794 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_CEC_VERSION
);
795 command
.parameters
.PushBack((uint8_t)cecVersion
);
797 return Transmit(command
, false);
800 bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
803 cec_command::Format(command
, iInitiator
, CECDEVICE_TV
, CEC_OPCODE_INACTIVE_SOURCE
);
804 command
.parameters
.PushBack((iPhysicalAddress
>> 8) & 0xFF);
805 command
.parameters
.PushBack(iPhysicalAddress
& 0xFF);
807 return Transmit(command
, false);
810 bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_menu_state menuState
)
813 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_MENU_STATUS
);
814 command
.parameters
.PushBack((uint8_t)menuState
);
816 return Transmit(command
, false);
819 bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, CStdString strDeviceName
)
822 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_NAME
);
823 for (size_t iPtr
= 0; iPtr
< strDeviceName
.length(); iPtr
++)
824 command
.parameters
.PushBack(strDeviceName
.at(iPtr
));
826 return Transmit(command
, false);
829 bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_display_control duration
, const char *strMessage
)
832 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_STRING
);
833 command
.parameters
.PushBack((uint8_t)duration
);
835 size_t iLen
= strlen(strMessage
);
836 if (iLen
> 13) iLen
= 13;
838 for (size_t iPtr
= 0; iPtr
< iLen
; iPtr
++)
839 command
.parameters
.PushBack(strMessage
[iPtr
]);
841 return Transmit(command
, false);
844 bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
, cec_device_type type
)
847 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
848 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
849 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
850 command
.parameters
.PushBack((uint8_t) (type
));
852 return Transmit(command
, false);
855 bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
858 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_NONE
);
860 return Transmit(command
, false);
863 bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_power_status state
)
866 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_POWER_STATUS
);
867 command
.parameters
.PushBack((uint8_t) state
);
869 return Transmit(command
, false);
872 bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator
, uint64_t iVendorId
)
875 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_DEVICE_VENDOR_ID
);
877 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 16) & 0xFF));
878 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 8) & 0xFF));
879 command
.parameters
.PushBack((uint8_t) ((uint64_t)iVendorId
& 0xFF));
881 return Transmit(command
, false);
884 bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, uint8_t state
)
887 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_AUDIO_STATUS
);
888 command
.parameters
.PushBack(state
);
890 return Transmit(command
, false);
893 bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
)
896 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
);
897 command
.parameters
.PushBack((uint8_t)state
);
899 return Transmit(command
, false);
902 bool CCECCommandHandler::TransmitSetStreamPath(uint16_t iStreamPath
)
905 cec_command::Format(command
, m_busDevice
->GetLogicalAddress(), CECDEVICE_BROADCAST
, CEC_OPCODE_SET_STREAM_PATH
);
906 command
.parameters
.PushBack((uint8_t) ((iStreamPath
>> 8) & 0xFF));
907 command
.parameters
.PushBack((uint8_t) (iStreamPath
& 0xFF));
909 return Transmit(command
, false);
912 bool CCECCommandHandler::TransmitSystemAudioModeStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
)
915 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
);
916 command
.parameters
.PushBack((uint8_t)state
);
918 return Transmit(command
, false);
921 bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_deck_info state
)
924 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_DECK_STATUS
);
925 command
.PushBack((uint8_t)state
);
927 return Transmit(command
, false);
930 bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = true */)
933 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_PRESSED
);
934 command
.parameters
.PushBack((uint8_t)key
);
936 return Transmit(command
, bWait
);
939 bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWait
/* = true */)
942 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_RELEASE
);
944 return Transmit(command
, bWait
);
947 bool CCECCommandHandler::Transmit(cec_command
&command
, bool bExpectResponse
/* = true */, cec_opcode expectedResponse
/* = CEC_OPCODE_NONE */)
950 command
.transmit_timeout
= m_iTransmitTimeout
;
953 uint8_t iTries(0), iMaxTries(command
.opcode
== CEC_OPCODE_NONE
? 1 : m_iTransmitRetries
+ 1);
954 CLockObject
writeLock(m_processor
->m_transmitMutex
);
955 while (!bReturn
&& ++iTries
<= iMaxTries
)
957 if ((bReturn
= m_processor
->Transmit(command
)) == true)
959 CLibCEC::AddLog(CEC_LOG_DEBUG
, "command transmitted");
962 bReturn
= m_waitForResponse
->Wait(expectedResponse
);
963 CLibCEC::AddLog(CEC_LOG_DEBUG
, bReturn
? "expected response received (%X: %s)" : "expected response not received (%X: %s)", (int)expectedResponse
, m_processor
->ToString(expectedResponse
));
972 bool CCECCommandHandler::ActivateSource(void)
974 if (m_busDevice
->IsActiveSource() &&
975 m_busDevice
->GetStatus(false) == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
)
977 m_busDevice
->SetPowerStatus(CEC_POWER_STATUS_ON
);
978 m_busDevice
->SetMenuState(CEC_MENU_STATE_ACTIVATED
);
980 m_busDevice
->TransmitImageViewOn();
981 m_busDevice
->TransmitActiveSource();
982 m_busDevice
->TransmitMenuState(CECDEVICE_TV
);
983 if ((m_busDevice
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
||
984 m_busDevice
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
) &&
985 SendDeckStatusUpdateOnActiveSource())
986 ((CCECPlaybackDevice
*)m_busDevice
)->TransmitDeckStatus(CECDEVICE_TV
);
987 m_bHandlerInited
= true;