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),
53 CCECCommandHandler::~CCECCommandHandler(void)
55 CLockObject
lock(&m_processor
->m_transmitMutex
);
56 CLockObject
receiveLock(&m_receiveMutex
);
57 m_condition
.Broadcast();
60 bool CCECCommandHandler::HandleCommand(const cec_command
&command
)
62 bool bHandled(true), bHandlerChanged(false);
66 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
);
67 m_busDevice
->AddLog(CEC_LOG_NOTICE
, strLog
);
69 m_processor
->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 HandleGivePhysicalAddress(command
);
85 case CEC_OPCODE_GIVE_OSD_NAME
:
86 HandleGiveOSDName(command
);
88 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
89 HandleGiveDeviceVendorId(command
);
91 case CEC_OPCODE_DEVICE_VENDOR_ID
:
92 bHandlerChanged
= HandleDeviceVendorId(command
);
94 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
95 HandleDeviceVendorCommandWithId(command
);
97 case CEC_OPCODE_GIVE_DECK_STATUS
:
98 HandleGiveDeckStatus(command
);
100 case CEC_OPCODE_DECK_CONTROL
:
101 HandleDeckControl(command
);
103 case CEC_OPCODE_MENU_REQUEST
:
104 HandleMenuRequest(command
);
106 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
107 HandleGiveDevicePowerStatus(command
);
109 case CEC_OPCODE_GET_CEC_VERSION
:
110 HandleGetCecVersion(command
);
112 case CEC_OPCODE_USER_CONTROL_PRESSED
:
113 HandleUserControlPressed(command
);
115 case CEC_OPCODE_USER_CONTROL_RELEASE
:
116 HandleUserControlRelease(command
);
118 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
119 HandleGiveAudioStatus(command
);
121 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
122 HandleGiveSystemAudioModeStatus(command
);
124 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
125 HandleSystemAudioModeRequest(command
);
127 case CEC_OPCODE_REPORT_AUDIO_STATUS
:
128 HandleReportAudioStatus(command
);
130 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
:
131 HandleSystemAudioModeStatus(command
);
133 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
:
134 HandleSetSystemAudioMode(command
);
136 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
137 HandleRequestActiveSource(command
);
139 case CEC_OPCODE_SET_STREAM_PATH
:
140 HandleSetStreamPath(command
);
142 case CEC_OPCODE_ROUTING_CHANGE
:
143 HandleRoutingChange(command
);
145 case CEC_OPCODE_ROUTING_INFORMATION
:
146 HandleRoutingInformation(command
);
148 case CEC_OPCODE_STANDBY
:
149 HandleStandby(command
);
151 case CEC_OPCODE_ACTIVE_SOURCE
:
152 HandleActiveSource(command
);
154 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
:
155 HandleReportPhysicalAddress(command
);
157 case CEC_OPCODE_SET_OSD_NAME
:
158 HandleSetOSDName(command
);
160 case CEC_OPCODE_IMAGE_VIEW_ON
:
161 HandleImageViewOn(command
);
163 case CEC_OPCODE_TEXT_VIEW_ON
:
164 HandleTextViewOn(command
);
166 case CEC_OPCODE_FEATURE_ABORT
:
167 HandleFeatureAbort(command
);
170 UnhandledCommand(command
);
175 if (bHandled
&& !bHandlerChanged
)
177 CLockObject
lock(&m_receiveMutex
);
178 m_condition
.Signal();
185 bool CCECCommandHandler::HandleActiveSource(const cec_command
&command
)
187 if (command
.parameters
.size
== 2)
189 uint16_t iAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
190 return m_processor
->SetActiveSource(iAddress
);
196 bool CCECCommandHandler::HandleDeckControl(const cec_command
&command
)
198 CCECBusDevice
*device
= GetDevice(command
.destination
);
199 if (device
&& (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
) && command
.parameters
.size
> 0)
201 ((CCECPlaybackDevice
*) device
)->SetDeckControlMode((cec_deck_control_mode
) command
.parameters
[0]);
208 bool CCECCommandHandler::HandleDeviceCecVersion(const cec_command
&command
)
210 if (command
.parameters
.size
== 1)
212 CCECBusDevice
*device
= GetDevice(command
.initiator
);
214 device
->SetCecVersion((cec_version
) command
.parameters
[0]);
220 bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command
&command
)
222 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
223 m_processor
->TransmitAbort(command
.initiator
, command
.opcode
, CEC_ABORT_REASON_REFUSED
);
228 bool CCECCommandHandler::HandleDeviceVendorId(const cec_command
&command
)
230 return SetVendorId(command
);
233 bool CCECCommandHandler::HandleFeatureAbort(const cec_command
&command
)
235 if (command
.parameters
.size
== 2)
237 m_processor
->m_busDevices
[command
.initiator
]->SetUnsupportedFeature((cec_opcode
)command
.parameters
[0]);
242 bool CCECCommandHandler::HandleGetCecVersion(const cec_command
&command
)
244 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
246 CCECBusDevice
*device
= GetDevice(command
.destination
);
248 return device
->TransmitCECVersion(command
.initiator
);
254 bool CCECCommandHandler::HandleGiveAudioStatus(const cec_command
&command
)
256 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
258 CCECBusDevice
*device
= GetDevice(command
.destination
);
259 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
260 return ((CCECAudioSystem
*) device
)->TransmitAudioStatus(command
.initiator
);
266 bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
268 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
270 CCECBusDevice
*device
= GetDevice(command
.destination
);
271 if (device
&& (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
))
272 return ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(command
.initiator
);
278 bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command
&command
)
280 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
282 CCECBusDevice
*device
= GetDevice(command
.destination
);
284 return device
->TransmitPowerState(command
.initiator
);
290 bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command
&command
)
292 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
294 CCECBusDevice
*device
= GetDevice(command
.destination
);
296 return device
->TransmitVendorID(command
.initiator
);
302 bool CCECCommandHandler::HandleGiveOSDName(const cec_command
&command
)
304 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
306 CCECBusDevice
*device
= GetDevice(command
.destination
);
308 return device
->TransmitOSDName(command
.initiator
);
314 bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command
&command
)
316 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
318 CCECBusDevice
*device
= GetDevice(command
.destination
);
321 device
->SetActiveSource();
322 return device
->TransmitPhysicalAddress() &&
323 device
->TransmitActiveSource();
330 bool CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command
&command
)
332 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
334 CCECBusDevice
*device
= GetDevice(command
.destination
);
335 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
336 return ((CCECAudioSystem
*) device
)->TransmitSystemAudioModeStatus(command
.initiator
);
342 bool CCECCommandHandler::HandleImageViewOn(const cec_command
&command
)
344 m_processor
->m_busDevices
[command
.initiator
]->SetActiveSource();
348 bool CCECCommandHandler::HandleMenuRequest(const cec_command
&command
)
350 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
352 if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_QUERY
)
354 CCECBusDevice
*device
= GetDevice(command
.destination
);
356 return device
->TransmitMenuState(command
.initiator
);
363 bool CCECCommandHandler::HandleReportAudioStatus(const cec_command
&command
)
365 if (command
.parameters
.size
== 1)
367 CCECBusDevice
*device
= GetDevice(command
.initiator
);
368 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
370 ((CCECAudioSystem
*)device
)->SetAudioStatus(command
.parameters
[0]);
377 bool CCECCommandHandler::HandleReportPhysicalAddress(const cec_command
&command
)
379 if (command
.parameters
.size
== 3)
381 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
382 SetPhysicalAddress(command
.initiator
, iNewAddress
);
387 bool CCECCommandHandler::HandleReportPowerStatus(const cec_command
&command
)
389 if (command
.parameters
.size
== 1)
391 CCECBusDevice
*device
= GetDevice(command
.initiator
);
393 device
->SetPowerStatus((cec_power_status
) command
.parameters
[0]);
398 bool CCECCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
400 if (m_processor
->IsStarted())
403 strLog
.Format(">> %i requests active source", (uint8_t) command
.initiator
);
404 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
406 vector
<CCECBusDevice
*> devices
;
407 for (int iDevicePtr
= (int)GetMyDevices(devices
)-1; iDevicePtr
>=0; iDevicePtr
--)
408 devices
[iDevicePtr
]->TransmitActiveSource();
415 bool CCECCommandHandler::HandleRoutingChange(const cec_command
&command
)
417 if (command
.parameters
.size
== 4)
419 uint16_t iOldAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
420 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[2] << 8) | ((uint16_t)command
.parameters
[3]);
422 CCECBusDevice
*device
= GetDevice(command
.initiator
);
424 device
->SetStreamPath(iNewAddress
, iOldAddress
);
429 bool CCECCommandHandler::HandleRoutingInformation(const cec_command
&command
)
431 if (command
.parameters
.size
== 2)
433 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
434 m_processor
->SetActiveSource(iNewAddress
);
440 bool CCECCommandHandler::HandleSetMenuLanguage(const cec_command
&command
)
442 if (command
.parameters
.size
== 3)
444 CCECBusDevice
*device
= GetDevice(command
.initiator
);
447 cec_menu_language language
;
448 language
.device
= command
.initiator
;
449 for (uint8_t iPtr
= 0; iPtr
< 4; iPtr
++)
450 language
.language
[iPtr
] = command
.parameters
[iPtr
];
451 language
.language
[3] = 0;
452 device
->SetMenuLanguage(language
);
459 bool CCECCommandHandler::HandleSetOSDName(const cec_command
&command
)
461 if (command
.parameters
.size
> 0)
463 CCECBusDevice
*device
= GetDevice(command
.initiator
);
467 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
468 buf
[iPtr
] = (char)command
.parameters
[iPtr
];
469 buf
[command
.parameters
.size
] = 0;
471 CStdString
strName(buf
);
472 device
->SetOSDName(strName
);
480 bool CCECCommandHandler::HandleSetStreamPath(const cec_command
&command
)
482 if (m_processor
->IsStarted() && command
.parameters
.size
>= 2)
484 uint16_t iStreamAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
486 strLog
.Format(">> %i sets stream path to physical address %04x", command
.initiator
, iStreamAddress
);
487 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
489 /* one of the device handled by libCEC has been made active */
490 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamAddress
);
491 if (device
&& m_busDevice
->MyLogicalAddressContains(device
->GetLogicalAddress()))
493 device
->SetActiveSource();
494 device
->TransmitActiveSource();
500 bool CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command
&command
)
502 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
504 CCECBusDevice
*device
= GetDevice(command
.destination
);
505 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
507 if (command
.parameters
.size
>= 2)
509 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
510 ((CCECAudioSystem
*) device
)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON
);
511 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
512 CCECBusDevice
*newActiveDevice
= GetDeviceByPhysicalAddress(iNewAddress
);
514 newActiveDevice
->SetActiveSource();
515 return ((CCECAudioSystem
*) device
)->TransmitSetSystemAudioMode(command
.initiator
);
519 ((CCECAudioSystem
*) device
)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF
);
520 return ((CCECAudioSystem
*) device
)->TransmitSetSystemAudioMode(command
.initiator
);
527 bool CCECCommandHandler::HandleStandby(const cec_command
&command
)
529 CCECBusDevice
*device
= GetDevice(command
.initiator
);
531 device
->SetPowerStatus(CEC_POWER_STATUS_STANDBY
);
536 bool CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command
&command
)
538 if (command
.parameters
.size
== 1)
540 CCECBusDevice
*device
= GetDevice(command
.initiator
);
541 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
543 ((CCECAudioSystem
*)device
)->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
551 bool CCECCommandHandler::HandleSetSystemAudioMode(const cec_command
&command
)
553 if (command
.parameters
.size
== 1)
555 CCECBusDevice
*device
= GetDevice(command
.initiator
);
556 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
558 ((CCECAudioSystem
*)device
)->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
566 bool CCECCommandHandler::HandleTextViewOn(const cec_command
&command
)
568 m_processor
->m_busDevices
[command
.initiator
]->SetActiveSource();
572 bool CCECCommandHandler::HandleUserControlPressed(const cec_command
&command
)
574 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
) && command
.parameters
.size
> 0)
576 m_processor
->AddKey();
578 if (command
.parameters
[0] <= CEC_USER_CONTROL_CODE_MAX
)
581 strLog
.Format("key pressed: %x", command
.parameters
[0]);
582 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
584 if (command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER
||
585 command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION
)
587 CCECBusDevice
*device
= GetDevice(command
.destination
);
590 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
591 if (device
->MyLogicalAddressContains(device
->GetLogicalAddress()))
593 device
->SetActiveSource();
594 device
->TransmitActiveSource();
596 if (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
||
597 device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
598 ((CCECPlaybackDevice
*)device
)->TransmitDeckStatus(command
.initiator
);
604 m_processor
->SetCurrentButton((cec_user_control_code
) command
.parameters
[0]);
612 bool CCECCommandHandler::HandleUserControlRelease(const cec_command
&command
)
614 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
615 m_processor
->AddKey();
620 void CCECCommandHandler::UnhandledCommand(const cec_command
&command
)
623 strLog
.Format("unhandled command with opcode %02x from address %d", command
.opcode
, command
.initiator
);
624 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
);
627 unsigned int CCECCommandHandler::GetMyDevices(vector
<CCECBusDevice
*> &devices
) const
629 unsigned int iReturn(0);
631 cec_logical_addresses addresses
= m_processor
->GetLogicalAddresses();
632 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
636 devices
.push_back(GetDevice((cec_logical_address
) iPtr
));
644 CCECBusDevice
*CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress
) const
646 CCECBusDevice
*device
= NULL
;
648 if (iLogicalAddress
>= CECDEVICE_TV
&& iLogicalAddress
<= CECDEVICE_BROADCAST
)
649 device
= m_processor
->m_busDevices
[iLogicalAddress
];
654 CCECBusDevice
*CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
) const
656 return m_processor
->GetDeviceByPhysicalAddress(iPhysicalAddress
);
659 CCECBusDevice
*CCECCommandHandler::GetDeviceByType(cec_device_type type
) const
661 return m_processor
->GetDeviceByType(type
);
664 bool CCECCommandHandler::SetVendorId(const cec_command
&command
)
666 bool bChanged(false);
667 if (command
.parameters
.size
< 3)
669 m_busDevice
->AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
673 uint64_t iVendorId
= ((uint64_t)command
.parameters
[0] << 16) +
674 ((uint64_t)command
.parameters
[1] << 8) +
675 (uint64_t)command
.parameters
[2];
677 CCECBusDevice
*device
= GetDevice((cec_logical_address
) command
.initiator
);
679 bChanged
= device
->SetVendorId(iVendorId
);
683 void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress
, uint16_t iNewAddress
)
685 if (!m_busDevice
->MyLogicalAddressContains(iAddress
))
687 bool bOurAddress(m_processor
->GetPhysicalAddress() == iNewAddress
);
688 GetDevice(iAddress
)->SetPhysicalAddress(iNewAddress
);
691 /* another device reported the same physical address as ours
692 * since we don't have physical address detection yet, we'll just use the
693 * given address, increased by 0x100 for now */
694 m_processor
->SetPhysicalAddress(iNewAddress
+ 0x100);
699 void CCECCommandHandler::HandlePoll(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
702 strLog
.Format("<< POLL: %s (%x) -> %s (%x)", m_processor
->ToString(iInitiator
), iInitiator
, m_processor
->ToString(iDestination
), iDestination
);
703 m_processor
->AddLog(CEC_LOG_DEBUG
, strLog
);
706 bool CCECCommandHandler::HandleReceiveFailed(void)
708 /* default = error */
712 bool CCECCommandHandler::TransmitImageViewOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
715 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_IMAGE_VIEW_ON
);
717 return Transmit(command
);
720 bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
723 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_STANDBY
);
725 return Transmit(command
);
728 bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
731 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_CEC_VERSION
);
733 return Transmit(command
);
736 bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
739 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_MENU_LANGUAGE
);
741 return Transmit(command
);
744 bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
747 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_OSD_NAME
);
749 return Transmit(command
);
752 bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
755 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
);
757 return Transmit(command
);
760 bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
763 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
);
765 return Transmit(command
);
768 bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
771 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
773 return Transmit(command
);
776 bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
779 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
780 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
781 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
783 return Transmit(command
);
786 bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_version cecVersion
)
789 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_CEC_VERSION
);
790 command
.parameters
.PushBack((uint8_t)cecVersion
);
792 return Transmit(command
);
795 bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
798 cec_command::Format(command
, iInitiator
, CECDEVICE_TV
, CEC_OPCODE_INACTIVE_SOURCE
);
799 command
.parameters
.PushBack((iPhysicalAddress
>> 8) & 0xFF);
800 command
.parameters
.PushBack(iPhysicalAddress
& 0xFF);
802 return Transmit(command
);
805 bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_menu_state menuState
)
808 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_MENU_STATUS
);
809 command
.parameters
.PushBack((uint8_t)menuState
);
811 return Transmit(command
);
814 bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, CStdString strDeviceName
)
817 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_NAME
);
818 for (unsigned int iPtr
= 0; iPtr
< strDeviceName
.length(); iPtr
++)
819 command
.parameters
.PushBack(strDeviceName
.at(iPtr
));
821 return Transmit(command
);
824 bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_display_control duration
, const char *strMessage
)
827 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_STRING
);
828 command
.parameters
.PushBack((uint8_t)duration
);
830 unsigned int iLen
= strlen(strMessage
);
831 if (iLen
> 13) iLen
= 13;
833 for (unsigned int iPtr
= 0; iPtr
< iLen
; iPtr
++)
834 command
.parameters
.PushBack(strMessage
[iPtr
]);
836 return Transmit(command
);
839 bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
, cec_device_type type
)
842 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
843 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
844 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
845 command
.parameters
.PushBack((uint8_t) (type
));
847 return Transmit(command
);
850 bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
853 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_NONE
);
855 return Transmit(command
, false);
858 bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_power_status state
)
861 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_POWER_STATUS
);
862 command
.parameters
.PushBack((uint8_t) state
);
864 return Transmit(command
);
867 bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator
, uint64_t iVendorId
)
870 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_DEVICE_VENDOR_ID
);
872 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 16) & 0xFF));
873 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 8) & 0xFF));
874 command
.parameters
.PushBack((uint8_t) ((uint64_t)iVendorId
& 0xFF));
876 return Transmit(command
);
879 bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, uint8_t state
)
882 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_AUDIO_STATUS
);
883 command
.parameters
.PushBack(state
);
885 return Transmit(command
);
888 bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
)
891 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
);
892 command
.parameters
.PushBack((uint8_t)state
);
894 return Transmit(command
);
897 bool CCECCommandHandler::TransmitSystemAudioModeStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
)
900 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
);
901 command
.parameters
.PushBack((uint8_t)state
);
903 return Transmit(command
);
906 bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_deck_info state
)
909 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_DECK_STATUS
);
910 command
.PushBack((uint8_t)state
);
912 return Transmit(command
);
915 bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = true */)
918 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_PRESSED
);
919 command
.parameters
.PushBack((uint8_t)key
);
921 return Transmit(command
, bWait
);
924 bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWait
/* = true */)
927 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_RELEASE
);
929 return Transmit(command
, bWait
);
932 bool CCECCommandHandler::Transmit(cec_command
&command
, bool bExpectResponse
/* = true */)
935 command
.transmit_timeout
= m_iTransmitTimeout
;
938 CLockObject
writeLock(&m_processor
->m_transmitMutex
);
939 CLockObject
receiveLock(&m_receiveMutex
);
941 if (m_processor
->Transmit(command
))
943 bReturn
= bExpectResponse
?
944 m_condition
.Wait(&m_receiveMutex
, m_iTransmitWait
) :
953 bool CCECCommandHandler::InitHandler(void)
955 if (m_busDevice
->GetLogicalAddress() == CECDEVICE_TV
)
957 CCECBusDevice
*primary
= m_processor
->GetPrimaryDevice();
958 primary
->SetPowerStatus(CEC_POWER_STATUS_ON
);
959 primary
->SetMenuState(CEC_MENU_STATE_ACTIVATED
);
961 if (m_processor
->GetPrimaryDevice()->GetPhysicalAddress(false) != 0xffff)
963 m_processor
->SetActiveSource();
964 primary
->TransmitMenuState(m_busDevice
->GetLogicalAddress());
965 m_bHandlerInited
= true;
971 void CCECCommandHandler::MarkBusy(void)
973 CLockObject
receiveLock(&m_receiveMutex
);
977 bool CCECCommandHandler::MarkReady(void)
979 CLockObject
receiveLock(&m_receiveMutex
);
980 return m_iUseCounter
> 0 ? (--m_iUseCounter
== 0) : true;
983 bool CCECCommandHandler::InUse(void)
985 CLockObject
receiveLock(&m_receiveMutex
);
986 return m_iUseCounter
> 0;