2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011 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"
42 CCECCommandHandler::CCECCommandHandler(CCECBusDevice
*busDevice
) :
43 m_busDevice(busDevice
),
44 m_processor(m_busDevice
->GetProcessor()),
45 m_iTransmitTimeout(CEC_DEFAULT_TRANSMIT_TIMEOUT
),
46 m_iTransmitWait(CEC_DEFAULT_TRANSMIT_WAIT
),
47 m_iTransmitRetries(CEC_DEFAULT_TRANSMIT_RETRIES
),
48 m_bHandlerInited(false)
52 CCECCommandHandler::~CCECCommandHandler(void)
54 m_condition
.Broadcast();
57 bool CCECCommandHandler::HandleCommand(const cec_command
&command
)
59 bool bHandled(true), bHandlerChanged(false);
62 strLog
.Format(">> %s (%X) -> %s (%X): %s (%2X)", m_processor
->ToString(command
.initiator
), command
.initiator
, m_processor
->ToString(command
.destination
), command
.destination
, m_processor
->ToString(command
.opcode
), command
.opcode
);
63 m_busDevice
->AddLog(CEC_LOG_NOTICE
, strLog
);
65 m_processor
->AddCommand(command
);
67 switch(command
.opcode
)
69 case CEC_OPCODE_REPORT_POWER_STATUS
:
70 HandleReportPowerStatus(command
);
72 case CEC_OPCODE_CEC_VERSION
:
73 HandleDeviceCecVersion(command
);
75 case CEC_OPCODE_SET_MENU_LANGUAGE
:
76 HandleSetMenuLanguage(command
);
78 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
79 HandleGivePhysicalAddress(command
);
81 case CEC_OPCODE_GIVE_OSD_NAME
:
82 HandleGiveOSDName(command
);
84 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
85 HandleGiveDeviceVendorId(command
);
87 case CEC_OPCODE_DEVICE_VENDOR_ID
:
88 bHandlerChanged
= HandleDeviceVendorId(command
);
90 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
91 HandleDeviceVendorCommandWithId(command
);
93 case CEC_OPCODE_GIVE_DECK_STATUS
:
94 HandleGiveDeckStatus(command
);
96 case CEC_OPCODE_DECK_CONTROL
:
97 HandleDeckControl(command
);
99 case CEC_OPCODE_MENU_REQUEST
:
100 HandleMenuRequest(command
);
102 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
103 HandleGiveDevicePowerStatus(command
);
105 case CEC_OPCODE_GET_CEC_VERSION
:
106 HandleGetCecVersion(command
);
108 case CEC_OPCODE_USER_CONTROL_PRESSED
:
109 HandleUserControlPressed(command
);
111 case CEC_OPCODE_USER_CONTROL_RELEASE
:
112 HandleUserControlRelease(command
);
114 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
115 HandleGiveAudioStatus(command
);
117 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
118 HandleGiveSystemAudioModeStatus(command
);
120 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
121 HandleSystemAudioModeRequest(command
);
123 case CEC_OPCODE_REPORT_AUDIO_STATUS
:
124 HandleReportAudioStatus(command
);
126 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
:
127 HandleSystemAudioModeStatus(command
);
129 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
:
130 HandleSetSystemAudioMode(command
);
132 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
133 HandleRequestActiveSource(command
);
135 case CEC_OPCODE_SET_STREAM_PATH
:
136 HandleSetStreamPath(command
);
138 case CEC_OPCODE_ROUTING_CHANGE
:
139 HandleRoutingChange(command
);
141 case CEC_OPCODE_ROUTING_INFORMATION
:
142 HandleRoutingInformation(command
);
144 case CEC_OPCODE_STANDBY
:
145 HandleStandby(command
);
147 case CEC_OPCODE_ACTIVE_SOURCE
:
148 HandleActiveSource(command
);
150 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
:
151 HandleReportPhysicalAddress(command
);
153 case CEC_OPCODE_SET_OSD_NAME
:
154 HandleSetOSDName(command
);
156 case CEC_OPCODE_IMAGE_VIEW_ON
:
157 HandleImageViewOn(command
);
159 case CEC_OPCODE_TEXT_VIEW_ON
:
160 HandleTextViewOn(command
);
162 case CEC_OPCODE_FEATURE_ABORT
:
163 HandleFeatureAbort(command
);
166 UnhandledCommand(command
);
171 if (bHandled
&& !bHandlerChanged
)
173 CLockObject
lock(&m_receiveMutex
);
174 m_condition
.Signal();
180 bool CCECCommandHandler::HandleActiveSource(const cec_command
&command
)
182 if (command
.parameters
.size
== 2)
184 uint16_t iAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
185 return m_processor
->SetActiveSource(iAddress
);
191 bool CCECCommandHandler::HandleDeckControl(const cec_command
&command
)
193 CCECBusDevice
*device
= GetDevice(command
.destination
);
194 if (device
&& (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
) && command
.parameters
.size
> 0)
196 ((CCECPlaybackDevice
*) device
)->SetDeckControlMode((cec_deck_control_mode
) command
.parameters
[0]);
203 bool CCECCommandHandler::HandleDeviceCecVersion(const cec_command
&command
)
205 if (command
.parameters
.size
== 1)
207 CCECBusDevice
*device
= GetDevice(command
.initiator
);
209 device
->SetCecVersion((cec_version
) command
.parameters
[0]);
215 bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command
&command
)
217 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
218 m_processor
->TransmitAbort(command
.initiator
, command
.opcode
, CEC_ABORT_REASON_REFUSED
);
223 bool CCECCommandHandler::HandleDeviceVendorId(const cec_command
&command
)
225 return SetVendorId(command
);
228 bool CCECCommandHandler::HandleFeatureAbort(const cec_command
&command
)
230 if (command
.parameters
.size
== 2)
232 m_processor
->m_busDevices
[command
.initiator
]->SetUnsupportedFeature((cec_opcode
)command
.parameters
[0]);
237 bool CCECCommandHandler::HandleGetCecVersion(const cec_command
&command
)
239 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
241 CCECBusDevice
*device
= GetDevice(command
.destination
);
243 return device
->TransmitCECVersion(command
.initiator
);
249 bool CCECCommandHandler::HandleGiveAudioStatus(const cec_command
&command
)
251 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
253 CCECBusDevice
*device
= GetDevice(command
.destination
);
254 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
255 return ((CCECAudioSystem
*) device
)->TransmitAudioStatus(command
.initiator
);
261 bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
263 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
265 CCECBusDevice
*device
= GetDevice(command
.destination
);
266 if (device
&& (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
))
267 return ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(command
.initiator
);
273 bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command
&command
)
275 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
277 CCECBusDevice
*device
= GetDevice(command
.destination
);
279 return device
->TransmitPowerState(command
.initiator
);
285 bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command
&command
)
287 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
289 CCECBusDevice
*device
= GetDevice(command
.destination
);
291 return device
->TransmitVendorID(command
.initiator
);
297 bool CCECCommandHandler::HandleGiveOSDName(const cec_command
&command
)
299 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
301 CCECBusDevice
*device
= GetDevice(command
.destination
);
303 return device
->TransmitOSDName(command
.initiator
);
309 bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command
&command
)
311 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
313 CCECBusDevice
*device
= GetDevice(command
.destination
);
316 device
->SetActiveSource();
317 return device
->TransmitPhysicalAddress() &&
318 device
->TransmitActiveSource();
325 bool CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command
&command
)
327 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
329 CCECBusDevice
*device
= GetDevice(command
.destination
);
330 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
331 return ((CCECAudioSystem
*) device
)->TransmitSystemAudioModeStatus(command
.initiator
);
337 bool CCECCommandHandler::HandleImageViewOn(const cec_command
&command
)
339 m_processor
->m_busDevices
[command
.initiator
]->SetActiveSource();
343 bool CCECCommandHandler::HandleMenuRequest(const cec_command
&command
)
345 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
347 if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_QUERY
)
349 CCECBusDevice
*device
= GetDevice(command
.destination
);
351 return device
->TransmitMenuState(command
.initiator
);
358 bool CCECCommandHandler::HandleReportAudioStatus(const cec_command
&command
)
360 if (command
.parameters
.size
== 1)
362 CCECBusDevice
*device
= GetDevice(command
.initiator
);
363 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
365 ((CCECAudioSystem
*)device
)->SetAudioStatus(command
.parameters
[0]);
372 bool CCECCommandHandler::HandleReportPhysicalAddress(const cec_command
&command
)
374 if (command
.parameters
.size
== 3)
376 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
377 SetPhysicalAddress(command
.initiator
, iNewAddress
);
382 bool CCECCommandHandler::HandleReportPowerStatus(const cec_command
&command
)
384 if (command
.parameters
.size
== 1)
386 CCECBusDevice
*device
= GetDevice(command
.initiator
);
388 device
->SetPowerStatus((cec_power_status
) command
.parameters
[0]);
393 bool CCECCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
395 if (m_processor
->IsStarted())
398 strLog
.Format(">> %i requests active source", (uint8_t) command
.initiator
);
399 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
401 vector
<CCECBusDevice
*> devices
;
402 for (int iDevicePtr
= (int)GetMyDevices(devices
)-1; iDevicePtr
>=0; iDevicePtr
--)
403 devices
[iDevicePtr
]->TransmitActiveSource();
410 bool CCECCommandHandler::HandleRoutingChange(const cec_command
&command
)
412 if (command
.parameters
.size
== 4)
414 uint16_t iOldAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
415 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[2] << 8) | ((uint16_t)command
.parameters
[3]);
417 CCECBusDevice
*device
= GetDevice(command
.initiator
);
419 device
->SetStreamPath(iNewAddress
, iOldAddress
);
424 bool CCECCommandHandler::HandleRoutingInformation(const cec_command
&command
)
426 if (command
.parameters
.size
== 2)
428 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
429 m_processor
->SetActiveSource(iNewAddress
);
435 bool CCECCommandHandler::HandleSetMenuLanguage(const cec_command
&command
)
437 if (command
.parameters
.size
== 3)
439 CCECBusDevice
*device
= GetDevice(command
.initiator
);
442 cec_menu_language language
;
443 language
.device
= command
.initiator
;
444 for (uint8_t iPtr
= 0; iPtr
< 4; iPtr
++)
445 language
.language
[iPtr
] = command
.parameters
[iPtr
];
446 language
.language
[3] = 0;
447 device
->SetMenuLanguage(language
);
454 bool CCECCommandHandler::HandleSetOSDName(const cec_command
&command
)
456 if (command
.parameters
.size
> 0)
458 CCECBusDevice
*device
= GetDevice(command
.initiator
);
462 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
463 buf
[iPtr
] = (char)command
.parameters
[iPtr
];
464 buf
[command
.parameters
.size
] = 0;
466 CStdString
strName(buf
);
467 device
->SetOSDName(strName
);
475 bool CCECCommandHandler::HandleSetStreamPath(const cec_command
&command
)
477 if (m_processor
->IsStarted() && command
.parameters
.size
>= 2)
479 uint16_t iStreamAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
481 strLog
.Format(">> %i sets stream path to physical address %04x", command
.initiator
, iStreamAddress
);
482 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
484 /* one of the device handled by libCEC has been made active */
485 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamAddress
);
486 if (device
&& m_busDevice
->MyLogicalAddressContains(device
->GetLogicalAddress()))
488 device
->SetActiveSource();
489 device
->TransmitActiveSource();
495 bool CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command
&command
)
497 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
499 CCECBusDevice
*device
= GetDevice(command
.destination
);
500 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
502 if (command
.parameters
.size
>= 2)
504 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
505 ((CCECAudioSystem
*) device
)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON
);
506 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
507 CCECBusDevice
*newActiveDevice
= GetDeviceByPhysicalAddress(iNewAddress
);
509 newActiveDevice
->SetActiveSource();
510 return ((CCECAudioSystem
*) device
)->TransmitSetSystemAudioMode(command
.initiator
);
514 ((CCECAudioSystem
*) device
)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF
);
515 return ((CCECAudioSystem
*) device
)->TransmitSetSystemAudioMode(command
.initiator
);
522 bool CCECCommandHandler::HandleStandby(const cec_command
&command
)
524 CCECBusDevice
*device
= GetDevice(command
.initiator
);
526 device
->SetPowerStatus(CEC_POWER_STATUS_STANDBY
);
531 bool CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command
&command
)
533 if (command
.parameters
.size
== 1)
535 CCECBusDevice
*device
= GetDevice(command
.initiator
);
536 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
538 ((CCECAudioSystem
*)device
)->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
546 bool CCECCommandHandler::HandleSetSystemAudioMode(const cec_command
&command
)
548 if (command
.parameters
.size
== 1)
550 CCECBusDevice
*device
= GetDevice(command
.initiator
);
551 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
553 ((CCECAudioSystem
*)device
)->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
561 bool CCECCommandHandler::HandleTextViewOn(const cec_command
&command
)
563 m_processor
->m_busDevices
[command
.initiator
]->SetActiveSource();
567 bool CCECCommandHandler::HandleUserControlPressed(const cec_command
&command
)
569 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
) && command
.parameters
.size
> 0)
571 m_processor
->AddKey();
573 if (command
.parameters
[0] <= CEC_USER_CONTROL_CODE_MAX
)
576 strLog
.Format("key pressed: %x", command
.parameters
[0]);
577 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
579 if (command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER
||
580 command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION
)
582 CCECBusDevice
*device
= GetDevice(command
.destination
);
585 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
586 if (device
->MyLogicalAddressContains(device
->GetLogicalAddress()))
588 device
->SetActiveSource();
589 device
->TransmitActiveSource();
591 if (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
||
592 device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
593 ((CCECPlaybackDevice
*)device
)->TransmitDeckStatus(command
.initiator
);
599 m_processor
->SetCurrentButton((cec_user_control_code
) command
.parameters
[0]);
607 bool CCECCommandHandler::HandleUserControlRelease(const cec_command
&command
)
609 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
610 m_processor
->AddKey();
615 void CCECCommandHandler::UnhandledCommand(const cec_command
&command
)
618 strLog
.Format("unhandled command with opcode %02x from address %d", command
.opcode
, command
.initiator
);
619 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
);
622 unsigned int CCECCommandHandler::GetMyDevices(vector
<CCECBusDevice
*> &devices
) const
624 unsigned int iReturn(0);
626 cec_logical_addresses addresses
= m_processor
->GetLogicalAddresses();
627 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
631 devices
.push_back(GetDevice((cec_logical_address
) iPtr
));
639 CCECBusDevice
*CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress
) const
641 CCECBusDevice
*device
= NULL
;
643 if (iLogicalAddress
>= CECDEVICE_TV
&& iLogicalAddress
<= CECDEVICE_BROADCAST
)
644 device
= m_processor
->m_busDevices
[iLogicalAddress
];
649 CCECBusDevice
*CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
) const
651 return m_processor
->GetDeviceByPhysicalAddress(iPhysicalAddress
);
654 CCECBusDevice
*CCECCommandHandler::GetDeviceByType(cec_device_type type
) const
656 return m_processor
->GetDeviceByType(type
);
659 bool CCECCommandHandler::SetVendorId(const cec_command
&command
)
661 bool bChanged(false);
662 if (command
.parameters
.size
< 3)
664 m_busDevice
->AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
668 uint64_t iVendorId
= ((uint64_t)command
.parameters
[0] << 16) +
669 ((uint64_t)command
.parameters
[1] << 8) +
670 (uint64_t)command
.parameters
[2];
672 CCECBusDevice
*device
= GetDevice((cec_logical_address
) command
.initiator
);
674 bChanged
= device
->SetVendorId(iVendorId
);
678 void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress
, uint16_t iNewAddress
)
680 if (!m_busDevice
->MyLogicalAddressContains(iAddress
))
682 bool bOurAddress(m_processor
->GetPhysicalAddress() == iNewAddress
);
683 GetDevice(iAddress
)->SetPhysicalAddress(iNewAddress
);
686 /* another device reported the same physical address as ours
687 * since we don't have physical address detection yet, we'll just use the
688 * given address, increased by 0x100 for now */
689 m_processor
->SetPhysicalAddress(iNewAddress
+ 0x100);
694 void CCECCommandHandler::HandlePoll(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
697 strLog
.Format("<< POLL: %s (%x) -> %s (%x)", m_processor
->ToString(iInitiator
), iInitiator
, m_processor
->ToString(iDestination
), iDestination
);
698 m_processor
->AddLog(CEC_LOG_DEBUG
, strLog
);
701 bool CCECCommandHandler::HandleReceiveFailed(void)
703 /* default = error */
707 bool CCECCommandHandler::TransmitPowerOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
710 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_IMAGE_VIEW_ON
);
712 return Transmit(command
);
715 bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
718 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_STANDBY
);
720 return Transmit(command
);
723 bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
726 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_CEC_VERSION
);
728 return Transmit(command
);
731 bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
734 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_MENU_LANGUAGE
);
736 return Transmit(command
);
739 bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
742 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_OSD_NAME
);
744 return Transmit(command
);
747 bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
750 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
);
752 return Transmit(command
);
755 bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
758 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
);
760 return Transmit(command
);
763 bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
766 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
768 return Transmit(command
);
771 bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
774 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
775 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
776 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
778 return Transmit(command
);
781 bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_version cecVersion
)
784 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_CEC_VERSION
);
785 command
.parameters
.PushBack((uint8_t)cecVersion
);
787 return Transmit(command
);
790 bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
793 cec_command::Format(command
, iInitiator
, CECDEVICE_TV
, CEC_OPCODE_INACTIVE_SOURCE
);
794 command
.parameters
.PushBack((iPhysicalAddress
>> 8) & 0xFF);
795 command
.parameters
.PushBack(iPhysicalAddress
& 0xFF);
797 return Transmit(command
);
800 bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_menu_state menuState
)
803 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_MENU_STATUS
);
804 command
.parameters
.PushBack((uint8_t)menuState
);
806 return Transmit(command
);
809 bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, CStdString strDeviceName
)
812 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_NAME
);
813 for (unsigned int iPtr
= 0; iPtr
< strDeviceName
.length(); iPtr
++)
814 command
.parameters
.PushBack(strDeviceName
.at(iPtr
));
816 return Transmit(command
);
819 bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_display_control duration
, const char *strMessage
)
822 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_STRING
);
823 command
.parameters
.PushBack((uint8_t)duration
);
825 unsigned int iLen
= strlen(strMessage
);
826 if (iLen
> 13) iLen
= 13;
828 for (unsigned int iPtr
= 0; iPtr
< iLen
; iPtr
++)
829 command
.parameters
.PushBack(strMessage
[iPtr
]);
831 return Transmit(command
);
834 bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
, cec_device_type type
)
837 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
838 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
839 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
840 command
.parameters
.PushBack((uint8_t) (type
));
842 return Transmit(command
);
845 bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
848 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_NONE
);
850 return Transmit(command
, false);
853 bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_power_status state
)
856 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_POWER_STATUS
);
857 command
.parameters
.PushBack((uint8_t) state
);
859 return Transmit(command
);
862 bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator
, uint64_t iVendorId
)
865 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_DEVICE_VENDOR_ID
);
867 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 16) & 0xFF));
868 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 8) & 0xFF));
869 command
.parameters
.PushBack((uint8_t) ((uint64_t)iVendorId
& 0xFF));
871 return Transmit(command
);
874 bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, uint8_t state
)
877 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_AUDIO_STATUS
);
878 command
.parameters
.PushBack(state
);
880 return Transmit(command
);
883 bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
)
886 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
);
887 command
.parameters
.PushBack((uint8_t)state
);
889 return Transmit(command
);
892 bool CCECCommandHandler::TransmitSystemAudioModeStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
)
895 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
);
896 command
.parameters
.PushBack((uint8_t)state
);
898 return Transmit(command
);
901 bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_deck_info state
)
904 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_DECK_STATUS
);
905 command
.PushBack((uint8_t)state
);
907 return Transmit(command
);
910 bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = true */)
913 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_PRESSED
);
914 command
.parameters
.PushBack((uint8_t)key
);
916 return Transmit(command
, bWait
);
919 bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWait
/* = true */)
922 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_RELEASE
);
924 return Transmit(command
, bWait
);
927 bool CCECCommandHandler::Transmit(cec_command
&command
, bool bExpectResponse
/* = true */)
929 command
.transmit_timeout
= m_iTransmitTimeout
;
931 CLockObject
writeLock(&m_processor
->m_transmitMutex
);
932 CLockObject
receiveLock(&m_receiveMutex
);
933 if (m_processor
->Transmit(command
))
936 return m_condition
.Wait(&m_receiveMutex
, m_iTransmitWait
);
943 bool CCECCommandHandler::InitHandler(void)
945 if (m_busDevice
->GetLogicalAddress() == CECDEVICE_TV
)
947 CCECBusDevice
*primary
= m_processor
->GetPrimaryDevice();
948 primary
->SetPowerStatus(CEC_POWER_STATUS_ON
);
949 primary
->SetMenuState(CEC_MENU_STATE_ACTIVATED
);
951 if (m_processor
->GetPrimaryDevice()->GetPhysicalAddress(false) != 0xffff)
953 m_processor
->SetActiveSource();
954 primary
->TransmitMenuState(m_busDevice
->GetLogicalAddress());
955 m_bHandlerInited
= true;