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_expectedResponse(CEC_OPCODE_NONE
),
52 m_bOPTSendDeckStatusUpdateOnActiveSource(false),
53 m_vendorId(CEC_VENDOR_UNKNOWN
),
58 CCECCommandHandler::~CCECCommandHandler(void)
60 CLockObject
lock(m_processor
->m_transmitMutex
);
61 CLockObject
receiveLock(m_receiveMutex
);
62 m_condition
.Broadcast();
65 bool CCECCommandHandler::HandleCommand(const cec_command
&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
);
194 CLockObject
lock(m_receiveMutex
);
195 if (m_expectedResponse
== CEC_OPCODE_NONE
||
196 m_expectedResponse
== command
.opcode
)
199 m_condition
.Signal();
206 bool CCECCommandHandler::HandleActiveSource(const cec_command
&command
)
208 if (command
.parameters
.size
== 2)
210 uint16_t iAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
211 return m_processor
->SetActiveSource(iAddress
);
217 bool CCECCommandHandler::HandleDeckControl(const cec_command
&command
)
219 CCECBusDevice
*device
= GetDevice(command
.destination
);
220 if (device
&& (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
) && command
.parameters
.size
> 0)
222 ((CCECPlaybackDevice
*) device
)->SetDeckControlMode((cec_deck_control_mode
) command
.parameters
[0]);
229 bool CCECCommandHandler::HandleDeviceCecVersion(const cec_command
&command
)
231 if (command
.parameters
.size
== 1)
233 CCECBusDevice
*device
= GetDevice(command
.initiator
);
235 device
->SetCecVersion((cec_version
) command
.parameters
[0]);
241 bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command
&command
)
243 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
244 m_processor
->TransmitAbort(command
.initiator
, command
.opcode
, CEC_ABORT_REASON_REFUSED
);
249 bool CCECCommandHandler::HandleDeviceVendorId(const cec_command
&command
)
251 return SetVendorId(command
);
254 bool CCECCommandHandler::HandleFeatureAbort(const cec_command
&command
)
256 if (command
.parameters
.size
== 2)
258 m_processor
->m_busDevices
[command
.initiator
]->SetUnsupportedFeature((cec_opcode
)command
.parameters
[0]);
263 bool CCECCommandHandler::HandleGetCecVersion(const cec_command
&command
)
265 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
267 CCECBusDevice
*device
= GetDevice(command
.destination
);
269 return device
->TransmitCECVersion(command
.initiator
);
275 bool CCECCommandHandler::HandleGiveAudioStatus(const cec_command
&command
)
277 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
279 CCECBusDevice
*device
= GetDevice(command
.destination
);
280 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
281 return ((CCECAudioSystem
*) device
)->TransmitAudioStatus(command
.initiator
);
287 bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
289 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
291 CCECBusDevice
*device
= GetDevice(command
.destination
);
292 if (device
&& (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
))
293 return ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(command
.initiator
);
299 bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command
&command
)
301 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
303 CCECBusDevice
*device
= GetDevice(command
.destination
);
305 return device
->TransmitPowerState(command
.initiator
);
311 bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command
&command
)
313 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
315 CCECBusDevice
*device
= GetDevice(command
.destination
);
317 return device
->TransmitVendorID(command
.initiator
);
323 bool CCECCommandHandler::HandleGiveOSDName(const cec_command
&command
)
325 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
327 CCECBusDevice
*device
= GetDevice(command
.destination
);
329 return device
->TransmitOSDName(command
.initiator
);
335 bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command
&command
)
337 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
339 CCECBusDevice
*device
= GetDevice(command
.destination
);
342 device
->SetActiveSource();
343 return device
->TransmitPhysicalAddress() &&
344 device
->TransmitActiveSource();
351 bool CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command
&command
)
353 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
355 CCECBusDevice
*device
= GetDevice(command
.destination
);
356 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
357 return ((CCECAudioSystem
*) device
)->TransmitSystemAudioModeStatus(command
.initiator
);
363 bool CCECCommandHandler::HandleImageViewOn(const cec_command
&command
)
365 m_processor
->m_busDevices
[command
.initiator
]->SetActiveSource();
369 bool CCECCommandHandler::HandleMenuRequest(const cec_command
&command
)
371 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
373 if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_QUERY
)
375 CCECBusDevice
*device
= GetDevice(command
.destination
);
377 return device
->TransmitMenuState(command
.initiator
);
384 bool CCECCommandHandler::HandleReportAudioStatus(const cec_command
&command
)
386 if (command
.parameters
.size
== 1)
388 CCECBusDevice
*device
= GetDevice(command
.initiator
);
389 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
391 ((CCECAudioSystem
*)device
)->SetAudioStatus(command
.parameters
[0]);
398 bool CCECCommandHandler::HandleReportPhysicalAddress(const cec_command
&command
)
400 if (command
.parameters
.size
== 3)
402 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
403 SetPhysicalAddress(command
.initiator
, iNewAddress
);
408 bool CCECCommandHandler::HandleReportPowerStatus(const cec_command
&command
)
410 if (command
.parameters
.size
== 1)
412 CCECBusDevice
*device
= GetDevice(command
.initiator
);
414 device
->SetPowerStatus((cec_power_status
) command
.parameters
[0]);
419 bool CCECCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
421 if (m_processor
->IsRunning())
423 CLibCEC::AddLog(CEC_LOG_DEBUG
, ">> %i requests active source", (uint8_t) command
.initiator
);
425 vector
<CCECBusDevice
*> devices
;
426 for (size_t iDevicePtr
= 0; iDevicePtr
< GetMyDevices(devices
); iDevicePtr
++)
427 devices
[iDevicePtr
]->TransmitActiveSource();
434 bool CCECCommandHandler::HandleRoutingChange(const cec_command
&command
)
436 if (command
.parameters
.size
== 4)
438 uint16_t iOldAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
439 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[2] << 8) | ((uint16_t)command
.parameters
[3]);
441 CCECBusDevice
*device
= GetDevice(command
.initiator
);
443 device
->SetStreamPath(iNewAddress
, iOldAddress
);
448 bool CCECCommandHandler::HandleRoutingInformation(const cec_command
&command
)
450 if (command
.parameters
.size
== 2)
452 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
453 m_processor
->SetActiveSource(iNewAddress
);
459 bool CCECCommandHandler::HandleSetMenuLanguage(const cec_command
&command
)
461 if (command
.parameters
.size
== 3)
463 CCECBusDevice
*device
= GetDevice(command
.initiator
);
466 cec_menu_language language
;
467 language
.device
= command
.initiator
;
468 for (uint8_t iPtr
= 0; iPtr
< 4; iPtr
++)
469 language
.language
[iPtr
] = command
.parameters
[iPtr
];
470 language
.language
[3] = 0;
471 device
->SetMenuLanguage(language
);
478 bool CCECCommandHandler::HandleSetOSDName(const cec_command
&command
)
480 if (command
.parameters
.size
> 0)
482 CCECBusDevice
*device
= GetDevice(command
.initiator
);
486 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
487 buf
[iPtr
] = (char)command
.parameters
[iPtr
];
488 buf
[command
.parameters
.size
] = 0;
490 CStdString
strName(buf
);
491 device
->SetOSDName(strName
);
499 bool CCECCommandHandler::HandleSetStreamPath(const cec_command
&command
)
501 if (m_processor
->IsRunning() && command
.parameters
.size
>= 2)
503 uint16_t iStreamAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
504 CLibCEC::AddLog(CEC_LOG_DEBUG
, ">> %i sets stream path to physical address %04x", command
.initiator
, iStreamAddress
);
506 /* one of the device handled by libCEC has been made active */
507 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamAddress
);
508 if (device
&& m_busDevice
->MyLogicalAddressContains(device
->GetLogicalAddress()))
510 device
->SetActiveSource();
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() && m_busDevice
->MyLogicalAddressContains(command
.destination
) && command
.parameters
.size
> 0)
598 if (command
.parameters
[0] <= CEC_USER_CONTROL_CODE_MAX
)
600 if (command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER
||
601 command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION
)
603 CCECBusDevice
*device
= GetDevice(command
.destination
);
606 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
607 if (device
->MyLogicalAddressContains(device
->GetLogicalAddress()))
609 device
->SetActiveSource();
610 device
->TransmitActiveSource();
612 if (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
||
613 device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
614 ((CCECPlaybackDevice
*)device
)->TransmitDeckStatus(command
.initiator
);
620 CLibCEC::SetCurrentButton((cec_user_control_code
) command
.parameters
[0]);
628 bool CCECCommandHandler::HandleUserControlRelease(const cec_command
&command
)
630 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
636 bool CCECCommandHandler::HandleVendorCommand(const cec_command
& UNUSED(command
))
641 void CCECCommandHandler::UnhandledCommand(const cec_command
&command
)
643 CLibCEC::AddLog(CEC_LOG_DEBUG
, "unhandled command with opcode %02x from address %d", command
.opcode
, command
.initiator
);
646 size_t CCECCommandHandler::GetMyDevices(vector
<CCECBusDevice
*> &devices
) const
650 cec_logical_addresses addresses
= m_processor
->GetLogicalAddresses();
651 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
655 devices
.push_back(GetDevice((cec_logical_address
) iPtr
));
663 CCECBusDevice
*CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress
) const
665 CCECBusDevice
*device
= NULL
;
667 if (iLogicalAddress
>= CECDEVICE_TV
&& iLogicalAddress
<= CECDEVICE_BROADCAST
)
668 device
= m_processor
->m_busDevices
[iLogicalAddress
];
673 CCECBusDevice
*CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
) const
675 return m_processor
->GetDeviceByPhysicalAddress(iPhysicalAddress
);
678 CCECBusDevice
*CCECCommandHandler::GetDeviceByType(cec_device_type type
) const
680 return m_processor
->GetDeviceByType(type
);
683 bool CCECCommandHandler::SetVendorId(const cec_command
&command
)
685 bool bChanged(false);
686 if (command
.parameters
.size
< 3)
688 CLibCEC::AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
692 uint64_t iVendorId
= ((uint64_t)command
.parameters
[0] << 16) +
693 ((uint64_t)command
.parameters
[1] << 8) +
694 (uint64_t)command
.parameters
[2];
696 CCECBusDevice
*device
= GetDevice((cec_logical_address
) command
.initiator
);
698 bChanged
= device
->SetVendorId(iVendorId
);
702 void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress
, uint16_t iNewAddress
)
704 if (!m_busDevice
->MyLogicalAddressContains(iAddress
))
706 bool bOurAddress(m_processor
->GetPhysicalAddress() == iNewAddress
);
707 GetDevice(iAddress
)->SetPhysicalAddress(iNewAddress
);
710 /* another device reported the same physical address as ours
711 * since we don't have physical address detection yet, we'll just use the
712 * given address, increased by 0x100 for now */
713 m_processor
->SetPhysicalAddress(iNewAddress
+ 0x100);
718 bool CCECCommandHandler::TransmitImageViewOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
721 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_IMAGE_VIEW_ON
);
723 return Transmit(command
, false);
726 bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
729 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_STANDBY
);
731 return Transmit(command
, false);
734 bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
737 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_CEC_VERSION
);
739 return Transmit(command
, true, CEC_OPCODE_CEC_VERSION
);
742 bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
745 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_MENU_LANGUAGE
);
747 return Transmit(command
, true, CEC_OPCODE_SET_MENU_LANGUAGE
);
750 bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
753 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_OSD_NAME
);
755 return Transmit(command
, true, CEC_OPCODE_SET_OSD_NAME
);
758 bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
761 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
);
763 return Transmit(command
, true, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
766 bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
769 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
);
771 return Transmit(command
, true, CEC_OPCODE_REPORT_POWER_STATUS
);
774 bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
777 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
779 return Transmit(command
, true, CEC_OPCODE_DEVICE_VENDOR_ID
);
782 bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
785 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
786 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
787 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
789 return Transmit(command
, false);
792 bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_version cecVersion
)
795 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_CEC_VERSION
);
796 command
.parameters
.PushBack((uint8_t)cecVersion
);
798 return Transmit(command
, false);
801 bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
804 cec_command::Format(command
, iInitiator
, CECDEVICE_TV
, CEC_OPCODE_INACTIVE_SOURCE
);
805 command
.parameters
.PushBack((iPhysicalAddress
>> 8) & 0xFF);
806 command
.parameters
.PushBack(iPhysicalAddress
& 0xFF);
808 return Transmit(command
, false);
811 bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_menu_state menuState
)
814 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_MENU_STATUS
);
815 command
.parameters
.PushBack((uint8_t)menuState
);
817 return Transmit(command
, false);
820 bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, CStdString strDeviceName
)
823 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_NAME
);
824 for (size_t iPtr
= 0; iPtr
< strDeviceName
.length(); iPtr
++)
825 command
.parameters
.PushBack(strDeviceName
.at(iPtr
));
827 return Transmit(command
, false);
830 bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_display_control duration
, const char *strMessage
)
833 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_STRING
);
834 command
.parameters
.PushBack((uint8_t)duration
);
836 size_t iLen
= strlen(strMessage
);
837 if (iLen
> 13) iLen
= 13;
839 for (size_t iPtr
= 0; iPtr
< iLen
; iPtr
++)
840 command
.parameters
.PushBack(strMessage
[iPtr
]);
842 return Transmit(command
, false);
845 bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
, cec_device_type type
)
848 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
849 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
850 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
851 command
.parameters
.PushBack((uint8_t) (type
));
853 return Transmit(command
, false);
856 bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
859 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_NONE
);
861 return Transmit(command
, false);
864 bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_power_status state
)
867 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_POWER_STATUS
);
868 command
.parameters
.PushBack((uint8_t) state
);
870 return Transmit(command
, false);
873 bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator
, uint64_t iVendorId
)
876 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_DEVICE_VENDOR_ID
);
878 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 16) & 0xFF));
879 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 8) & 0xFF));
880 command
.parameters
.PushBack((uint8_t) ((uint64_t)iVendorId
& 0xFF));
882 return Transmit(command
, false);
885 bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, uint8_t state
)
888 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_AUDIO_STATUS
);
889 command
.parameters
.PushBack(state
);
891 return Transmit(command
, false);
894 bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
)
897 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
);
898 command
.parameters
.PushBack((uint8_t)state
);
900 return Transmit(command
, false);
903 bool CCECCommandHandler::TransmitSetStreamPath(uint16_t iStreamPath
)
906 cec_command::Format(command
, m_busDevice
->GetLogicalAddress(), CECDEVICE_BROADCAST
, CEC_OPCODE_SET_STREAM_PATH
);
907 command
.parameters
.PushBack((uint8_t) ((iStreamPath
>> 8) & 0xFF));
908 command
.parameters
.PushBack((uint8_t) (iStreamPath
& 0xFF));
910 return Transmit(command
, false);
913 bool CCECCommandHandler::TransmitSystemAudioModeStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
)
916 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
);
917 command
.parameters
.PushBack((uint8_t)state
);
919 return Transmit(command
, false);
922 bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_deck_info state
)
925 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_DECK_STATUS
);
926 command
.PushBack((uint8_t)state
);
928 return Transmit(command
, false);
931 bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = true */)
934 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_PRESSED
);
935 command
.parameters
.PushBack((uint8_t)key
);
937 return Transmit(command
, bWait
);
940 bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWait
/* = true */)
943 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_RELEASE
);
945 return Transmit(command
, bWait
);
948 bool CCECCommandHandler::Transmit(cec_command
&command
, bool bExpectResponse
/* = true */, cec_opcode expectedResponse
/* = CEC_OPCODE_NONE */)
951 command
.transmit_timeout
= m_iTransmitTimeout
;
954 uint8_t iTries(0), iMaxTries(command
.opcode
== CEC_OPCODE_NONE
? 1 : m_iTransmitRetries
+ 1);
955 CLockObject
writeLock(m_processor
->m_transmitMutex
);
956 CLockObject
receiveLock(m_receiveMutex
);
957 while (!bReturn
&& ++iTries
<= iMaxTries
)
959 m_expectedResponse
= expectedResponse
;
960 if ((bReturn
= m_processor
->Transmit(command
)) == true)
962 CLibCEC::AddLog(CEC_LOG_DEBUG
, "command transmitted");
964 bReturn
= m_condition
.Wait(m_receiveMutex
, m_bRcvSignal
, m_iTransmitWait
);
965 m_bRcvSignal
= false;
966 CLibCEC::AddLog(CEC_LOG_DEBUG
, bReturn
? "expected response received" : "expected response not received");
974 bool CCECCommandHandler::ActivateSource(void)
976 if (m_busDevice
->GetLogicalAddress() == CECDEVICE_TV
)
978 CCECBusDevice
*primary
= m_processor
->GetPrimaryDevice();
979 primary
->SetPowerStatus(CEC_POWER_STATUS_ON
);
980 primary
->SetMenuState(CEC_MENU_STATE_ACTIVATED
);
982 if (m_processor
->GetPrimaryDevice()->GetPhysicalAddress(false) != 0xffff)
984 m_processor
->SetActiveSource();
985 primary
->TransmitMenuState(m_busDevice
->GetLogicalAddress());
986 m_bHandlerInited
= true;