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 &&
246 (command
.parameters
[1] == CEC_ABORT_REASON_UNRECOGNIZED_OPCODE
||
247 command
.parameters
[1] == CEC_ABORT_REASON_REFUSED
))
248 m_processor
->m_busDevices
[command
.initiator
]->SetUnsupportedFeature((cec_opcode
)command
.parameters
[0]);
252 bool CCECCommandHandler::HandleGetCecVersion(const cec_command
&command
)
254 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
256 CCECBusDevice
*device
= GetDevice(command
.destination
);
258 return device
->TransmitCECVersion(command
.initiator
);
264 bool CCECCommandHandler::HandleGiveAudioStatus(const cec_command
&command
)
266 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
268 CCECBusDevice
*device
= GetDevice(command
.destination
);
269 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
270 return ((CCECAudioSystem
*) device
)->TransmitAudioStatus(command
.initiator
);
276 bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
278 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
280 CCECBusDevice
*device
= GetDevice(command
.destination
);
281 if (device
&& (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
))
282 return ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(command
.initiator
);
288 bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command
&command
)
290 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
292 CCECBusDevice
*device
= GetDevice(command
.destination
);
294 return device
->TransmitPowerState(command
.initiator
);
300 bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command
&command
)
302 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
304 CCECBusDevice
*device
= GetDevice(command
.destination
);
306 return device
->TransmitVendorID(command
.initiator
);
312 bool CCECCommandHandler::HandleGiveOSDName(const cec_command
&command
)
314 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
316 CCECBusDevice
*device
= GetDevice(command
.destination
);
318 return device
->TransmitOSDName(command
.initiator
);
324 bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command
&command
)
326 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
328 CCECBusDevice
*device
= GetDevice(command
.destination
);
331 device
->SetActiveSource();
332 return device
->TransmitPhysicalAddress() &&
333 device
->TransmitImageViewOn() &&
334 device
->TransmitActiveSource();
341 bool CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command
&command
)
343 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
345 CCECBusDevice
*device
= GetDevice(command
.destination
);
346 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
347 return ((CCECAudioSystem
*) device
)->TransmitSystemAudioModeStatus(command
.initiator
);
353 bool CCECCommandHandler::HandleImageViewOn(const cec_command
&command
)
355 m_processor
->m_busDevices
[command
.initiator
]->SetActiveSource();
359 bool CCECCommandHandler::HandleMenuRequest(const cec_command
&command
)
361 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
363 if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_QUERY
)
365 CCECBusDevice
*device
= GetDevice(command
.destination
);
367 return device
->TransmitMenuState(command
.initiator
);
374 bool CCECCommandHandler::HandleReportAudioStatus(const cec_command
&command
)
376 if (command
.parameters
.size
== 1)
378 CCECBusDevice
*device
= GetDevice(command
.initiator
);
379 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
381 ((CCECAudioSystem
*)device
)->SetAudioStatus(command
.parameters
[0]);
388 bool CCECCommandHandler::HandleReportPhysicalAddress(const cec_command
&command
)
390 if (command
.parameters
.size
== 3)
392 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
393 SetPhysicalAddress(command
.initiator
, iNewAddress
);
398 bool CCECCommandHandler::HandleReportPowerStatus(const cec_command
&command
)
400 if (command
.parameters
.size
== 1)
402 CCECBusDevice
*device
= GetDevice(command
.initiator
);
404 device
->SetPowerStatus((cec_power_status
) command
.parameters
[0]);
409 bool CCECCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
411 if (m_processor
->IsRunning())
413 CLibCEC::AddLog(CEC_LOG_DEBUG
, ">> %i requests active source", (uint8_t) command
.initiator
);
415 vector
<CCECBusDevice
*> devices
;
416 for (size_t iDevicePtr
= 0; iDevicePtr
< GetMyDevices(devices
); iDevicePtr
++)
417 devices
[iDevicePtr
]->TransmitActiveSource();
424 bool CCECCommandHandler::HandleRoutingChange(const cec_command
&command
)
426 if (command
.parameters
.size
== 4)
428 uint16_t iOldAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
429 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[2] << 8) | ((uint16_t)command
.parameters
[3]);
431 CCECBusDevice
*device
= GetDevice(command
.initiator
);
433 device
->SetStreamPath(iNewAddress
, iOldAddress
);
438 bool CCECCommandHandler::HandleRoutingInformation(const cec_command
&command
)
440 if (command
.parameters
.size
== 2)
442 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
443 m_processor
->SetActiveSource(iNewAddress
);
449 bool CCECCommandHandler::HandleSetMenuLanguage(const cec_command
&command
)
451 if (command
.parameters
.size
== 3)
453 CCECBusDevice
*device
= GetDevice(command
.initiator
);
456 cec_menu_language language
;
457 language
.device
= command
.initiator
;
458 for (uint8_t iPtr
= 0; iPtr
< 4; iPtr
++)
459 language
.language
[iPtr
] = command
.parameters
[iPtr
];
460 language
.language
[3] = 0;
461 device
->SetMenuLanguage(language
);
468 bool CCECCommandHandler::HandleSetOSDName(const cec_command
&command
)
470 if (command
.parameters
.size
> 0)
472 CCECBusDevice
*device
= GetDevice(command
.initiator
);
476 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
477 buf
[iPtr
] = (char)command
.parameters
[iPtr
];
478 buf
[command
.parameters
.size
] = 0;
480 CStdString
strName(buf
);
481 device
->SetOSDName(strName
);
489 bool CCECCommandHandler::HandleSetStreamPath(const cec_command
&command
)
491 if (m_processor
->IsRunning() && command
.parameters
.size
>= 2)
493 uint16_t iStreamAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
494 CLibCEC::AddLog(CEC_LOG_DEBUG
, ">> %i sets stream path to physical address %04x", command
.initiator
, iStreamAddress
);
496 /* one of the device handled by libCEC has been made active */
497 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamAddress
);
498 if (device
&& m_busDevice
->MyLogicalAddressContains(device
->GetLogicalAddress()))
500 device
->SetActiveSource();
501 device
->TransmitImageViewOn();
502 device
->TransmitActiveSource();
504 device
->SetMenuState(CEC_MENU_STATE_ACTIVATED
);
505 device
->TransmitMenuState(command
.initiator
);
511 bool CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command
&command
)
513 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
515 CCECBusDevice
*device
= GetDevice(command
.destination
);
516 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
518 if (command
.parameters
.size
>= 2)
520 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
521 ((CCECAudioSystem
*) device
)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON
);
522 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
523 CCECBusDevice
*newActiveDevice
= GetDeviceByPhysicalAddress(iNewAddress
);
525 newActiveDevice
->SetActiveSource();
526 return ((CCECAudioSystem
*) device
)->TransmitSetSystemAudioMode(command
.initiator
);
530 ((CCECAudioSystem
*) device
)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF
);
531 return ((CCECAudioSystem
*) device
)->TransmitSetSystemAudioMode(command
.initiator
);
538 bool CCECCommandHandler::HandleStandby(const cec_command
&command
)
540 CCECBusDevice
*device
= GetDevice(command
.initiator
);
542 device
->SetPowerStatus(CEC_POWER_STATUS_STANDBY
);
547 bool CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command
&command
)
549 if (command
.parameters
.size
== 1)
551 CCECBusDevice
*device
= GetDevice(command
.initiator
);
552 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
554 ((CCECAudioSystem
*)device
)->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
562 bool CCECCommandHandler::HandleSetSystemAudioMode(const cec_command
&command
)
564 if (command
.parameters
.size
== 1)
566 CCECBusDevice
*device
= GetDevice(command
.initiator
);
567 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
569 ((CCECAudioSystem
*)device
)->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
577 bool CCECCommandHandler::HandleTextViewOn(const cec_command
&command
)
579 m_processor
->m_busDevices
[command
.initiator
]->SetActiveSource();
583 bool CCECCommandHandler::HandleUserControlPressed(const cec_command
&command
)
585 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
) && command
.parameters
.size
> 0)
589 if (command
.parameters
[0] <= CEC_USER_CONTROL_CODE_MAX
)
591 if (command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER
||
592 command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION
)
594 CCECBusDevice
*device
= GetDevice(command
.destination
);
597 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
598 if (device
->MyLogicalAddressContains(device
->GetLogicalAddress()))
600 device
->SetActiveSource();
601 device
->TransmitImageViewOn();
602 device
->TransmitActiveSource();
604 if (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
||
605 device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
606 ((CCECPlaybackDevice
*)device
)->TransmitDeckStatus(command
.initiator
);
612 CLibCEC::SetCurrentButton((cec_user_control_code
) command
.parameters
[0]);
620 bool CCECCommandHandler::HandleUserControlRelease(const cec_command
&command
)
622 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
628 bool CCECCommandHandler::HandleVendorCommand(const cec_command
& UNUSED(command
))
633 void CCECCommandHandler::UnhandledCommand(const cec_command
&command
)
635 CLibCEC::AddLog(CEC_LOG_DEBUG
, "unhandled command with opcode %02x from address %d", command
.opcode
, command
.initiator
);
638 size_t CCECCommandHandler::GetMyDevices(vector
<CCECBusDevice
*> &devices
) const
642 cec_logical_addresses addresses
= m_processor
->GetLogicalAddresses();
643 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
647 devices
.push_back(GetDevice((cec_logical_address
) iPtr
));
655 CCECBusDevice
*CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress
) const
657 CCECBusDevice
*device
= NULL
;
659 if (iLogicalAddress
>= CECDEVICE_TV
&& iLogicalAddress
<= CECDEVICE_BROADCAST
)
660 device
= m_processor
->m_busDevices
[iLogicalAddress
];
665 CCECBusDevice
*CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
) const
667 return m_processor
->GetDeviceByPhysicalAddress(iPhysicalAddress
);
670 CCECBusDevice
*CCECCommandHandler::GetDeviceByType(cec_device_type type
) const
672 return m_processor
->GetDeviceByType(type
);
675 bool CCECCommandHandler::SetVendorId(const cec_command
&command
)
677 bool bChanged(false);
678 if (command
.parameters
.size
< 3)
680 CLibCEC::AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
684 uint64_t iVendorId
= ((uint64_t)command
.parameters
[0] << 16) +
685 ((uint64_t)command
.parameters
[1] << 8) +
686 (uint64_t)command
.parameters
[2];
688 CCECBusDevice
*device
= GetDevice((cec_logical_address
) command
.initiator
);
690 bChanged
= device
->SetVendorId(iVendorId
);
694 void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress
, uint16_t iNewAddress
)
696 if (!m_busDevice
->MyLogicalAddressContains(iAddress
))
698 bool bOurAddress(m_processor
->GetPhysicalAddress() == iNewAddress
);
699 GetDevice(iAddress
)->SetPhysicalAddress(iNewAddress
);
702 /* another device reported the same physical address as ours
703 * since we don't have physical address detection yet, we'll just use the
704 * given address, increased by 0x100 for now */
705 m_processor
->SetPhysicalAddress(iNewAddress
+ 0x100);
710 bool CCECCommandHandler::PowerOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
712 if (iDestination
== CECDEVICE_TV
)
713 return TransmitImageViewOn(iInitiator
, iDestination
);
715 return TransmitKeypress(iInitiator
, iDestination
, CEC_USER_CONTROL_CODE_POWER
) &&
716 TransmitKeyRelease(iInitiator
, iDestination
);
719 bool CCECCommandHandler::TransmitImageViewOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
722 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_IMAGE_VIEW_ON
);
724 return Transmit(command
, false);
727 bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
730 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_STANDBY
);
732 return Transmit(command
, false);
735 bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
738 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_CEC_VERSION
);
740 return Transmit(command
, true, CEC_OPCODE_CEC_VERSION
);
743 bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
746 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_MENU_LANGUAGE
);
748 return Transmit(command
, true, CEC_OPCODE_SET_MENU_LANGUAGE
);
751 bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
754 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_OSD_NAME
);
756 return Transmit(command
, true, CEC_OPCODE_SET_OSD_NAME
);
759 bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
762 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
);
764 return Transmit(command
, true, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
767 bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
770 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
);
772 return Transmit(command
, true, CEC_OPCODE_REPORT_POWER_STATUS
);
775 bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
778 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
780 return Transmit(command
, true, CEC_OPCODE_DEVICE_VENDOR_ID
);
783 bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
786 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
787 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
788 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
790 return Transmit(command
, false);
793 bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_version cecVersion
)
796 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_CEC_VERSION
);
797 command
.parameters
.PushBack((uint8_t)cecVersion
);
799 return Transmit(command
, false);
802 bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
805 cec_command::Format(command
, iInitiator
, CECDEVICE_TV
, CEC_OPCODE_INACTIVE_SOURCE
);
806 command
.parameters
.PushBack((iPhysicalAddress
>> 8) & 0xFF);
807 command
.parameters
.PushBack(iPhysicalAddress
& 0xFF);
809 return Transmit(command
, false);
812 bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_menu_state menuState
)
815 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_MENU_STATUS
);
816 command
.parameters
.PushBack((uint8_t)menuState
);
818 return Transmit(command
, false);
821 bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, CStdString strDeviceName
)
824 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_NAME
);
825 for (size_t iPtr
= 0; iPtr
< strDeviceName
.length(); iPtr
++)
826 command
.parameters
.PushBack(strDeviceName
.at(iPtr
));
828 return Transmit(command
, false);
831 bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_display_control duration
, const char *strMessage
)
834 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_STRING
);
835 command
.parameters
.PushBack((uint8_t)duration
);
837 size_t iLen
= strlen(strMessage
);
838 if (iLen
> 13) iLen
= 13;
840 for (size_t iPtr
= 0; iPtr
< iLen
; iPtr
++)
841 command
.parameters
.PushBack(strMessage
[iPtr
]);
843 return Transmit(command
, false);
846 bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
, cec_device_type type
)
849 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
850 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
851 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
852 command
.parameters
.PushBack((uint8_t) (type
));
854 return Transmit(command
, false);
857 bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
860 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_NONE
);
862 return Transmit(command
, false);
865 bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_power_status state
)
868 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_POWER_STATUS
);
869 command
.parameters
.PushBack((uint8_t) state
);
871 return Transmit(command
, false);
874 bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator
, uint64_t iVendorId
)
877 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_DEVICE_VENDOR_ID
);
879 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 16) & 0xFF));
880 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 8) & 0xFF));
881 command
.parameters
.PushBack((uint8_t) ((uint64_t)iVendorId
& 0xFF));
883 return Transmit(command
, false);
886 bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, uint8_t state
)
889 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_AUDIO_STATUS
);
890 command
.parameters
.PushBack(state
);
892 return Transmit(command
, false);
895 bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
)
898 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
);
899 command
.parameters
.PushBack((uint8_t)state
);
901 return Transmit(command
, false);
904 bool CCECCommandHandler::TransmitSetStreamPath(uint16_t iStreamPath
)
907 cec_command::Format(command
, m_busDevice
->GetLogicalAddress(), CECDEVICE_BROADCAST
, CEC_OPCODE_SET_STREAM_PATH
);
908 command
.parameters
.PushBack((uint8_t) ((iStreamPath
>> 8) & 0xFF));
909 command
.parameters
.PushBack((uint8_t) (iStreamPath
& 0xFF));
911 return Transmit(command
, false);
914 bool CCECCommandHandler::TransmitSystemAudioModeStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
)
917 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
);
918 command
.parameters
.PushBack((uint8_t)state
);
920 return Transmit(command
, false);
923 bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_deck_info state
)
926 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_DECK_STATUS
);
927 command
.PushBack((uint8_t)state
);
929 return Transmit(command
, false);
932 bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = true */)
935 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_PRESSED
);
936 command
.parameters
.PushBack((uint8_t)key
);
938 return Transmit(command
, bWait
);
941 bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWait
/* = true */)
944 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_RELEASE
);
946 return Transmit(command
, bWait
);
949 bool CCECCommandHandler::Transmit(cec_command
&command
, bool bExpectResponse
/* = true */, cec_opcode expectedResponse
/* = CEC_OPCODE_NONE */)
952 command
.transmit_timeout
= m_iTransmitTimeout
;
955 uint8_t iTries(0), iMaxTries(command
.opcode
== CEC_OPCODE_NONE
? 1 : m_iTransmitRetries
+ 1);
956 CLockObject
writeLock(m_processor
->m_transmitMutex
);
957 while (!bReturn
&& ++iTries
<= iMaxTries
)
959 if ((bReturn
= m_processor
->Transmit(command
)) == true)
961 CLibCEC::AddLog(CEC_LOG_DEBUG
, "command transmitted");
964 bReturn
= m_waitForResponse
->Wait(expectedResponse
);
965 CLibCEC::AddLog(CEC_LOG_DEBUG
, bReturn
? "expected response received (%X: %s)" : "expected response not received (%X: %s)", (int)expectedResponse
, m_processor
->ToString(expectedResponse
));
974 bool CCECCommandHandler::ActivateSource(void)
976 if (m_busDevice
->IsActiveSource() &&
977 m_busDevice
->GetStatus(false) == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
)
979 m_busDevice
->SetPowerStatus(CEC_POWER_STATUS_ON
);
980 m_busDevice
->SetMenuState(CEC_MENU_STATE_ACTIVATED
);
982 m_busDevice
->TransmitImageViewOn();
983 m_busDevice
->TransmitActiveSource();
984 m_busDevice
->TransmitMenuState(CECDEVICE_TV
);
985 if ((m_busDevice
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
||
986 m_busDevice
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
) &&
987 SendDeckStatusUpdateOnActiveSource())
988 ((CCECPlaybackDevice
*)m_busDevice
)->TransmitDeckStatus(CECDEVICE_TV
);
989 m_bHandlerInited
= true;