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_GIVE_OSD_NAME
:
87 if (m_processor
->IsInitialised())
88 HandleGiveOSDName(command
);
90 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
91 if (m_processor
->IsInitialised())
92 HandleGiveDeviceVendorId(command
);
94 case CEC_OPCODE_DEVICE_VENDOR_ID
:
95 HandleDeviceVendorId(command
);
97 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
98 HandleDeviceVendorCommandWithId(command
);
100 case CEC_OPCODE_GIVE_DECK_STATUS
:
101 if (m_processor
->IsInitialised())
102 HandleGiveDeckStatus(command
);
104 case CEC_OPCODE_DECK_CONTROL
:
105 HandleDeckControl(command
);
107 case CEC_OPCODE_MENU_REQUEST
:
108 if (m_processor
->IsInitialised())
109 HandleMenuRequest(command
);
111 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
112 if (m_processor
->IsInitialised())
113 HandleGiveDevicePowerStatus(command
);
115 case CEC_OPCODE_GET_CEC_VERSION
:
116 if (m_processor
->IsInitialised())
117 HandleGetCecVersion(command
);
119 case CEC_OPCODE_USER_CONTROL_PRESSED
:
120 if (m_processor
->IsInitialised())
121 HandleUserControlPressed(command
);
123 case CEC_OPCODE_USER_CONTROL_RELEASE
:
124 if (m_processor
->IsInitialised())
125 HandleUserControlRelease(command
);
127 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
128 if (m_processor
->IsInitialised())
129 HandleGiveAudioStatus(command
);
131 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
132 if (m_processor
->IsInitialised())
133 HandleGiveSystemAudioModeStatus(command
);
135 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
136 if (m_processor
->IsInitialised())
137 HandleSystemAudioModeRequest(command
);
139 case CEC_OPCODE_REPORT_AUDIO_STATUS
:
140 HandleReportAudioStatus(command
);
142 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
:
143 HandleSystemAudioModeStatus(command
);
145 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
:
146 HandleSetSystemAudioMode(command
);
148 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
149 if (m_processor
->IsInitialised())
150 HandleRequestActiveSource(command
);
152 case CEC_OPCODE_SET_STREAM_PATH
:
153 HandleSetStreamPath(command
);
155 case CEC_OPCODE_ROUTING_CHANGE
:
156 HandleRoutingChange(command
);
158 case CEC_OPCODE_ROUTING_INFORMATION
:
159 HandleRoutingInformation(command
);
161 case CEC_OPCODE_STANDBY
:
162 if (m_processor
->IsInitialised())
163 HandleStandby(command
);
165 case CEC_OPCODE_ACTIVE_SOURCE
:
166 HandleActiveSource(command
);
168 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
:
169 HandleReportPhysicalAddress(command
);
171 case CEC_OPCODE_SET_OSD_NAME
:
172 HandleSetOSDName(command
);
174 case CEC_OPCODE_IMAGE_VIEW_ON
:
175 HandleImageViewOn(command
);
177 case CEC_OPCODE_TEXT_VIEW_ON
:
178 HandleTextViewOn(command
);
180 case CEC_OPCODE_FEATURE_ABORT
:
181 HandleFeatureAbort(command
);
183 case CEC_OPCODE_VENDOR_COMMAND
:
184 HandleVendorCommand(command
);
187 UnhandledCommand(command
);
193 m_waitForResponse
->Received((command
.opcode
== CEC_OPCODE_FEATURE_ABORT
&& command
.parameters
.size
> 0) ? (cec_opcode
)command
.parameters
[0] : command
.opcode
);
198 bool CCECCommandHandler::HandleActiveSource(const cec_command
&command
)
200 if (command
.parameters
.size
== 2)
202 uint16_t iAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
203 return m_processor
->SetActiveSource(iAddress
);
209 bool CCECCommandHandler::HandleDeckControl(const cec_command
&command
)
211 CCECBusDevice
*device
= GetDevice(command
.destination
);
212 if (device
&& (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
) && command
.parameters
.size
> 0)
214 ((CCECPlaybackDevice
*) device
)->SetDeckControlMode((cec_deck_control_mode
) command
.parameters
[0]);
221 bool CCECCommandHandler::HandleDeviceCecVersion(const cec_command
&command
)
223 if (command
.parameters
.size
== 1)
225 CCECBusDevice
*device
= GetDevice(command
.initiator
);
227 device
->SetCecVersion((cec_version
) command
.parameters
[0]);
233 bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command
&command
)
235 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
236 m_processor
->TransmitAbort(command
.initiator
, command
.opcode
, CEC_ABORT_REASON_REFUSED
);
241 bool CCECCommandHandler::HandleDeviceVendorId(const cec_command
&command
)
243 return SetVendorId(command
);
246 bool CCECCommandHandler::HandleFeatureAbort(const cec_command
&command
)
248 if (command
.parameters
.size
== 2 &&
249 (command
.parameters
[1] == CEC_ABORT_REASON_UNRECOGNIZED_OPCODE
||
250 command
.parameters
[1] == CEC_ABORT_REASON_REFUSED
))
251 m_processor
->m_busDevices
[command
.initiator
]->SetUnsupportedFeature((cec_opcode
)command
.parameters
[0]);
255 bool CCECCommandHandler::HandleGetCecVersion(const cec_command
&command
)
257 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
259 CCECBusDevice
*device
= GetDevice(command
.destination
);
261 return device
->TransmitCECVersion(command
.initiator
);
267 bool CCECCommandHandler::HandleGiveAudioStatus(const cec_command
&command
)
269 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
271 CCECBusDevice
*device
= GetDevice(command
.destination
);
272 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
273 return ((CCECAudioSystem
*) device
)->TransmitAudioStatus(command
.initiator
);
279 bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
281 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
283 CCECBusDevice
*device
= GetDevice(command
.destination
);
284 if (device
&& (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
))
285 return ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(command
.initiator
);
291 bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command
&command
)
293 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
295 CCECBusDevice
*device
= GetDevice(command
.destination
);
297 return device
->TransmitPowerState(command
.initiator
);
303 bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command
&command
)
305 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
307 CCECBusDevice
*device
= GetDevice(command
.destination
);
309 return device
->TransmitVendorID(command
.initiator
);
315 bool CCECCommandHandler::HandleGiveOSDName(const cec_command
&command
)
317 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
319 CCECBusDevice
*device
= GetDevice(command
.destination
);
321 return device
->TransmitOSDName(command
.initiator
);
327 bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command
&command
)
329 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
331 CCECBusDevice
*device
= GetDevice(command
.destination
);
334 device
->SetActiveSource();
335 return device
->TransmitPhysicalAddress() &&
336 device
->TransmitImageViewOn() &&
337 device
->TransmitActiveSource();
344 bool CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command
&command
)
346 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
348 CCECBusDevice
*device
= GetDevice(command
.destination
);
349 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
350 return ((CCECAudioSystem
*) device
)->TransmitSystemAudioModeStatus(command
.initiator
);
356 bool CCECCommandHandler::HandleImageViewOn(const cec_command
&command
)
358 m_processor
->m_busDevices
[command
.initiator
]->SetActiveSource();
362 bool CCECCommandHandler::HandleMenuRequest(const cec_command
&command
)
364 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
366 if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_QUERY
)
368 CCECBusDevice
*device
= GetDevice(command
.destination
);
370 return device
->TransmitMenuState(command
.initiator
);
377 bool CCECCommandHandler::HandlePoll(const cec_command
&command
)
379 m_busDevice
->HandlePoll(command
.initiator
);
383 bool CCECCommandHandler::HandleReportAudioStatus(const cec_command
&command
)
385 if (command
.parameters
.size
== 1)
387 CCECBusDevice
*device
= GetDevice(command
.initiator
);
388 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
390 ((CCECAudioSystem
*)device
)->SetAudioStatus(command
.parameters
[0]);
397 bool CCECCommandHandler::HandleReportPhysicalAddress(const cec_command
&command
)
399 if (command
.parameters
.size
== 3)
401 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
402 SetPhysicalAddress(command
.initiator
, iNewAddress
);
407 bool CCECCommandHandler::HandleReportPowerStatus(const cec_command
&command
)
409 if (command
.parameters
.size
== 1)
411 CCECBusDevice
*device
= GetDevice(command
.initiator
);
413 device
->SetPowerStatus((cec_power_status
) command
.parameters
[0]);
418 bool CCECCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
420 if (m_processor
->IsRunning())
422 CLibCEC::AddLog(CEC_LOG_DEBUG
, ">> %i requests active source", (uint8_t) command
.initiator
);
424 vector
<CCECBusDevice
*> devices
;
425 for (size_t iDevicePtr
= 0; iDevicePtr
< GetMyDevices(devices
); iDevicePtr
++)
426 devices
[iDevicePtr
]->TransmitActiveSource();
433 bool CCECCommandHandler::HandleRoutingChange(const cec_command
&command
)
435 if (command
.parameters
.size
== 4)
437 uint16_t iOldAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
438 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[2] << 8) | ((uint16_t)command
.parameters
[3]);
440 CCECBusDevice
*device
= GetDevice(command
.initiator
);
442 device
->SetStreamPath(iNewAddress
, iOldAddress
);
447 bool CCECCommandHandler::HandleRoutingInformation(const cec_command
&command
)
449 if (command
.parameters
.size
== 2)
451 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
452 m_processor
->SetActiveSource(iNewAddress
);
458 bool CCECCommandHandler::HandleSetMenuLanguage(const cec_command
&command
)
460 if (command
.parameters
.size
== 3)
462 CCECBusDevice
*device
= GetDevice(command
.initiator
);
465 cec_menu_language language
;
466 language
.device
= command
.initiator
;
467 for (uint8_t iPtr
= 0; iPtr
< 4; iPtr
++)
468 language
.language
[iPtr
] = command
.parameters
[iPtr
];
469 language
.language
[3] = 0;
470 device
->SetMenuLanguage(language
);
477 bool CCECCommandHandler::HandleSetOSDName(const cec_command
&command
)
479 if (command
.parameters
.size
> 0)
481 CCECBusDevice
*device
= GetDevice(command
.initiator
);
485 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
486 buf
[iPtr
] = (char)command
.parameters
[iPtr
];
487 buf
[command
.parameters
.size
] = 0;
489 CStdString
strName(buf
);
490 device
->SetOSDName(strName
);
498 bool CCECCommandHandler::HandleSetStreamPath(const cec_command
&command
)
500 if (m_processor
->IsRunning() && command
.parameters
.size
>= 2)
502 uint16_t iStreamAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
503 CLibCEC::AddLog(CEC_LOG_DEBUG
, ">> %i sets stream path to physical address %04x", command
.initiator
, iStreamAddress
);
505 /* one of the device handled by libCEC has been made active */
506 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamAddress
);
507 if (device
&& m_busDevice
->MyLogicalAddressContains(device
->GetLogicalAddress()))
509 device
->SetActiveSource();
510 device
->TransmitImageViewOn();
511 device
->TransmitActiveSource();
513 device
->SetMenuState(CEC_MENU_STATE_ACTIVATED
);
514 device
->TransmitMenuState(command
.initiator
);
520 bool CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command
&command
)
522 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
524 CCECBusDevice
*device
= GetDevice(command
.destination
);
525 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
527 if (command
.parameters
.size
>= 2)
529 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
530 ((CCECAudioSystem
*) device
)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON
);
531 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
532 CCECBusDevice
*newActiveDevice
= GetDeviceByPhysicalAddress(iNewAddress
);
534 newActiveDevice
->SetActiveSource();
535 return ((CCECAudioSystem
*) device
)->TransmitSetSystemAudioMode(command
.initiator
);
539 ((CCECAudioSystem
*) device
)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF
);
540 return ((CCECAudioSystem
*) device
)->TransmitSetSystemAudioMode(command
.initiator
);
547 bool CCECCommandHandler::HandleStandby(const cec_command
&command
)
549 CCECBusDevice
*device
= GetDevice(command
.initiator
);
551 device
->SetPowerStatus(CEC_POWER_STATUS_STANDBY
);
556 bool CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command
&command
)
558 if (command
.parameters
.size
== 1)
560 CCECBusDevice
*device
= GetDevice(command
.initiator
);
561 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
563 ((CCECAudioSystem
*)device
)->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
571 bool CCECCommandHandler::HandleSetSystemAudioMode(const cec_command
&command
)
573 if (command
.parameters
.size
== 1)
575 CCECBusDevice
*device
= GetDevice(command
.initiator
);
576 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
578 ((CCECAudioSystem
*)device
)->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
586 bool CCECCommandHandler::HandleTextViewOn(const cec_command
&command
)
588 m_processor
->m_busDevices
[command
.initiator
]->SetActiveSource();
592 bool CCECCommandHandler::HandleUserControlPressed(const cec_command
&command
)
594 if (m_processor
->IsRunning() &&
595 m_busDevice
->MyLogicalAddressContains(command
.destination
) &&
596 command
.parameters
.size
> 0)
599 if (command
.parameters
[0] <= CEC_USER_CONTROL_CODE_MAX
)
600 CLibCEC::SetCurrentButton((cec_user_control_code
) command
.parameters
[0]);
602 if (command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER
||
603 command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION
)
606 CCECBusDevice
*device
= GetDevice(command
.destination
);
610 // CEC_USER_CONTROL_CODE_POWER operates as a toggle
611 // assume CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION does not
612 if (command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER
)
614 cec_power_status status
= device
->GetPowerStatus();
615 bPowerOn
= !(status
== CEC_POWER_STATUS_ON
|| status
== CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
620 device
->SetActiveSource();
621 device
->TransmitImageViewOn();
622 device
->TransmitActiveSource();
624 if (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
||
625 device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
626 ((CCECPlaybackDevice
*)device
)->TransmitDeckStatus(command
.initiator
);
630 device
->SetInactiveSource();
631 device
->TransmitInactiveSource();
632 device
->SetMenuState(CEC_MENU_STATE_DEACTIVATED
);
641 bool CCECCommandHandler::HandleUserControlRelease(const cec_command
&command
)
643 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
649 bool CCECCommandHandler::HandleVendorCommand(const cec_command
& UNUSED(command
))
654 void CCECCommandHandler::UnhandledCommand(const cec_command
&command
)
656 CLibCEC::AddLog(CEC_LOG_DEBUG
, "unhandled command with opcode %02x from address %d", command
.opcode
, command
.initiator
);
659 size_t CCECCommandHandler::GetMyDevices(vector
<CCECBusDevice
*> &devices
) const
663 cec_logical_addresses addresses
= m_processor
->GetLogicalAddresses();
664 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
668 devices
.push_back(GetDevice((cec_logical_address
) iPtr
));
676 CCECBusDevice
*CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress
) const
678 CCECBusDevice
*device
= NULL
;
680 if (iLogicalAddress
>= CECDEVICE_TV
&& iLogicalAddress
<= CECDEVICE_BROADCAST
)
681 device
= m_processor
->m_busDevices
[iLogicalAddress
];
686 CCECBusDevice
*CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
) const
688 return m_processor
->GetDeviceByPhysicalAddress(iPhysicalAddress
);
691 CCECBusDevice
*CCECCommandHandler::GetDeviceByType(cec_device_type type
) const
693 return m_processor
->GetDeviceByType(type
);
696 bool CCECCommandHandler::SetVendorId(const cec_command
&command
)
698 bool bChanged(false);
699 if (command
.parameters
.size
< 3)
701 CLibCEC::AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
705 uint64_t iVendorId
= ((uint64_t)command
.parameters
[0] << 16) +
706 ((uint64_t)command
.parameters
[1] << 8) +
707 (uint64_t)command
.parameters
[2];
709 CCECBusDevice
*device
= GetDevice((cec_logical_address
) command
.initiator
);
711 bChanged
= device
->SetVendorId(iVendorId
);
715 void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress
, uint16_t iNewAddress
)
717 if (!m_busDevice
->MyLogicalAddressContains(iAddress
))
719 bool bOurAddress(m_processor
->GetPhysicalAddress() == iNewAddress
);
720 GetDevice(iAddress
)->SetPhysicalAddress(iNewAddress
);
723 /* another device reported the same physical address as ours
724 * since we don't have physical address detection yet, we'll just use the
725 * given address, increased by 0x100 for now */
726 m_processor
->SetPhysicalAddress(iNewAddress
+ 0x100);
731 bool CCECCommandHandler::PowerOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
733 if (iDestination
== CECDEVICE_TV
)
734 return TransmitImageViewOn(iInitiator
, iDestination
);
736 return TransmitKeypress(iInitiator
, iDestination
, CEC_USER_CONTROL_CODE_POWER
) &&
737 TransmitKeyRelease(iInitiator
, iDestination
);
740 bool CCECCommandHandler::TransmitImageViewOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
743 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_IMAGE_VIEW_ON
);
745 return Transmit(command
, false);
748 bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
751 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_STANDBY
);
753 return Transmit(command
, false);
756 bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
759 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_CEC_VERSION
);
761 return Transmit(command
, true, CEC_OPCODE_CEC_VERSION
);
764 bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
767 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_MENU_LANGUAGE
);
769 return Transmit(command
, true, CEC_OPCODE_SET_MENU_LANGUAGE
);
772 bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
775 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_OSD_NAME
);
777 return Transmit(command
, true, CEC_OPCODE_SET_OSD_NAME
);
780 bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
783 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
);
785 return Transmit(command
, true, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
788 bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
791 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
);
793 return Transmit(command
, true, CEC_OPCODE_REPORT_POWER_STATUS
);
796 bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
799 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
801 return Transmit(command
, true, CEC_OPCODE_DEVICE_VENDOR_ID
);
804 bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
807 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
808 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
809 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
811 return Transmit(command
, false);
814 bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_version cecVersion
)
817 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_CEC_VERSION
);
818 command
.parameters
.PushBack((uint8_t)cecVersion
);
820 return Transmit(command
, false);
823 bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
826 cec_command::Format(command
, iInitiator
, CECDEVICE_TV
, CEC_OPCODE_INACTIVE_SOURCE
);
827 command
.parameters
.PushBack((iPhysicalAddress
>> 8) & 0xFF);
828 command
.parameters
.PushBack(iPhysicalAddress
& 0xFF);
830 return Transmit(command
, false);
833 bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_menu_state menuState
)
836 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_MENU_STATUS
);
837 command
.parameters
.PushBack((uint8_t)menuState
);
839 return Transmit(command
, false);
842 bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, CStdString strDeviceName
)
845 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_NAME
);
846 for (size_t iPtr
= 0; iPtr
< strDeviceName
.length(); iPtr
++)
847 command
.parameters
.PushBack(strDeviceName
.at(iPtr
));
849 return Transmit(command
, false);
852 bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_display_control duration
, const char *strMessage
)
855 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_STRING
);
856 command
.parameters
.PushBack((uint8_t)duration
);
858 size_t iLen
= strlen(strMessage
);
859 if (iLen
> 13) iLen
= 13;
861 for (size_t iPtr
= 0; iPtr
< iLen
; iPtr
++)
862 command
.parameters
.PushBack(strMessage
[iPtr
]);
864 return Transmit(command
, false);
867 bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
, cec_device_type type
)
870 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
871 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
872 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
873 command
.parameters
.PushBack((uint8_t) (type
));
875 return Transmit(command
, false);
878 bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
881 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_NONE
);
883 return Transmit(command
, false);
886 bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_power_status state
)
889 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_POWER_STATUS
);
890 command
.parameters
.PushBack((uint8_t) state
);
892 return Transmit(command
, false);
895 bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator
, uint64_t iVendorId
)
898 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_DEVICE_VENDOR_ID
);
900 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 16) & 0xFF));
901 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 8) & 0xFF));
902 command
.parameters
.PushBack((uint8_t) ((uint64_t)iVendorId
& 0xFF));
904 return Transmit(command
, false);
907 bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, uint8_t state
)
910 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_AUDIO_STATUS
);
911 command
.parameters
.PushBack(state
);
913 return Transmit(command
, false);
916 bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
)
919 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
);
920 command
.parameters
.PushBack((uint8_t)state
);
922 return Transmit(command
, false);
925 bool CCECCommandHandler::TransmitSetStreamPath(uint16_t iStreamPath
)
928 cec_command::Format(command
, m_busDevice
->GetLogicalAddress(), CECDEVICE_BROADCAST
, CEC_OPCODE_SET_STREAM_PATH
);
929 command
.parameters
.PushBack((uint8_t) ((iStreamPath
>> 8) & 0xFF));
930 command
.parameters
.PushBack((uint8_t) (iStreamPath
& 0xFF));
932 return Transmit(command
, false);
935 bool CCECCommandHandler::TransmitSystemAudioModeStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
)
938 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
);
939 command
.parameters
.PushBack((uint8_t)state
);
941 return Transmit(command
, false);
944 bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_deck_info state
)
947 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_DECK_STATUS
);
948 command
.PushBack((uint8_t)state
);
950 return Transmit(command
, false);
953 bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = true */)
956 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_PRESSED
);
957 command
.parameters
.PushBack((uint8_t)key
);
959 return Transmit(command
, bWait
);
962 bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWait
/* = true */)
965 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_RELEASE
);
967 return Transmit(command
, bWait
);
970 bool CCECCommandHandler::Transmit(cec_command
&command
, bool bExpectResponse
/* = true */, cec_opcode expectedResponse
/* = CEC_OPCODE_NONE */)
973 command
.transmit_timeout
= m_iTransmitTimeout
;
976 uint8_t iTries(0), iMaxTries(command
.opcode
== CEC_OPCODE_NONE
? 1 : m_iTransmitRetries
+ 1);
977 CLockObject
writeLock(m_processor
->m_transmitMutex
);
978 while (!bReturn
&& ++iTries
<= iMaxTries
)
980 if ((bReturn
= m_processor
->Transmit(command
)) == true)
982 CLibCEC::AddLog(CEC_LOG_DEBUG
, "command transmitted");
985 bReturn
= m_waitForResponse
->Wait(expectedResponse
);
986 CLibCEC::AddLog(CEC_LOG_DEBUG
, bReturn
? "expected response received (%X: %s)" : "expected response not received (%X: %s)", (int)expectedResponse
, m_processor
->ToString(expectedResponse
));
995 bool CCECCommandHandler::ActivateSource(void)
997 if (m_busDevice
->IsActiveSource() &&
998 m_busDevice
->GetStatus(false) == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
)
1000 m_busDevice
->SetPowerStatus(CEC_POWER_STATUS_ON
);
1001 m_busDevice
->SetMenuState(CEC_MENU_STATE_ACTIVATED
);
1003 m_busDevice
->TransmitImageViewOn();
1004 m_busDevice
->TransmitActiveSource();
1005 m_busDevice
->TransmitMenuState(CECDEVICE_TV
);
1006 if ((m_busDevice
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
||
1007 m_busDevice
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
) &&
1008 SendDeckStatusUpdateOnActiveSource())
1009 ((CCECPlaybackDevice
*)m_busDevice
)->TransmitDeckStatus(CECDEVICE_TV
);
1010 m_bHandlerInited
= true;