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
);
169 case CEC_OPCODE_VENDOR_COMMAND
:
170 HandleVendorCommand(command
);
173 UnhandledCommand(command
);
178 if (bHandled
&& !bHandlerChanged
)
180 CLockObject
lock(&m_receiveMutex
);
181 m_condition
.Signal();
188 bool CCECCommandHandler::HandleActiveSource(const cec_command
&command
)
190 if (command
.parameters
.size
== 2)
192 uint16_t iAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
193 return m_processor
->SetActiveSource(iAddress
);
199 bool CCECCommandHandler::HandleDeckControl(const cec_command
&command
)
201 CCECBusDevice
*device
= GetDevice(command
.destination
);
202 if (device
&& (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
) && command
.parameters
.size
> 0)
204 ((CCECPlaybackDevice
*) device
)->SetDeckControlMode((cec_deck_control_mode
) command
.parameters
[0]);
211 bool CCECCommandHandler::HandleDeviceCecVersion(const cec_command
&command
)
213 if (command
.parameters
.size
== 1)
215 CCECBusDevice
*device
= GetDevice(command
.initiator
);
217 device
->SetCecVersion((cec_version
) command
.parameters
[0]);
223 bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command
&command
)
225 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
226 m_processor
->TransmitAbort(command
.initiator
, command
.opcode
, CEC_ABORT_REASON_REFUSED
);
231 bool CCECCommandHandler::HandleDeviceVendorId(const cec_command
&command
)
233 return SetVendorId(command
);
236 bool CCECCommandHandler::HandleFeatureAbort(const cec_command
&command
)
238 if (command
.parameters
.size
== 2)
240 m_processor
->m_busDevices
[command
.initiator
]->SetUnsupportedFeature((cec_opcode
)command
.parameters
[0]);
245 bool CCECCommandHandler::HandleGetCecVersion(const cec_command
&command
)
247 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
249 CCECBusDevice
*device
= GetDevice(command
.destination
);
251 return device
->TransmitCECVersion(command
.initiator
);
257 bool CCECCommandHandler::HandleGiveAudioStatus(const cec_command
&command
)
259 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
261 CCECBusDevice
*device
= GetDevice(command
.destination
);
262 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
263 return ((CCECAudioSystem
*) device
)->TransmitAudioStatus(command
.initiator
);
269 bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
271 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
273 CCECBusDevice
*device
= GetDevice(command
.destination
);
274 if (device
&& (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
))
275 return ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(command
.initiator
);
281 bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command
&command
)
283 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
285 CCECBusDevice
*device
= GetDevice(command
.destination
);
287 return device
->TransmitPowerState(command
.initiator
);
293 bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command
&command
)
295 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
297 CCECBusDevice
*device
= GetDevice(command
.destination
);
299 return device
->TransmitVendorID(command
.initiator
);
305 bool CCECCommandHandler::HandleGiveOSDName(const cec_command
&command
)
307 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
309 CCECBusDevice
*device
= GetDevice(command
.destination
);
311 return device
->TransmitOSDName(command
.initiator
);
317 bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command
&command
)
319 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
321 CCECBusDevice
*device
= GetDevice(command
.destination
);
324 device
->SetActiveSource();
325 return device
->TransmitPhysicalAddress() &&
326 device
->TransmitActiveSource();
333 bool CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command
&command
)
335 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
337 CCECBusDevice
*device
= GetDevice(command
.destination
);
338 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
339 return ((CCECAudioSystem
*) device
)->TransmitSystemAudioModeStatus(command
.initiator
);
345 bool CCECCommandHandler::HandleImageViewOn(const cec_command
&command
)
347 m_processor
->m_busDevices
[command
.initiator
]->SetActiveSource();
351 bool CCECCommandHandler::HandleMenuRequest(const cec_command
&command
)
353 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
355 if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_QUERY
)
357 CCECBusDevice
*device
= GetDevice(command
.destination
);
359 return device
->TransmitMenuState(command
.initiator
);
366 bool CCECCommandHandler::HandleReportAudioStatus(const cec_command
&command
)
368 if (command
.parameters
.size
== 1)
370 CCECBusDevice
*device
= GetDevice(command
.initiator
);
371 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
373 ((CCECAudioSystem
*)device
)->SetAudioStatus(command
.parameters
[0]);
380 bool CCECCommandHandler::HandleReportPhysicalAddress(const cec_command
&command
)
382 if (command
.parameters
.size
== 3)
384 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
385 SetPhysicalAddress(command
.initiator
, iNewAddress
);
390 bool CCECCommandHandler::HandleReportPowerStatus(const cec_command
&command
)
392 if (command
.parameters
.size
== 1)
394 CCECBusDevice
*device
= GetDevice(command
.initiator
);
396 device
->SetPowerStatus((cec_power_status
) command
.parameters
[0]);
401 bool CCECCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
403 if (m_processor
->IsStarted())
406 strLog
.Format(">> %i requests active source", (uint8_t) command
.initiator
);
407 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
409 vector
<CCECBusDevice
*> devices
;
410 for (int iDevicePtr
= (int)GetMyDevices(devices
)-1; iDevicePtr
>=0; iDevicePtr
--)
411 devices
[iDevicePtr
]->TransmitActiveSource();
418 bool CCECCommandHandler::HandleRoutingChange(const cec_command
&command
)
420 if (command
.parameters
.size
== 4)
422 uint16_t iOldAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
423 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[2] << 8) | ((uint16_t)command
.parameters
[3]);
425 CCECBusDevice
*device
= GetDevice(command
.initiator
);
427 device
->SetStreamPath(iNewAddress
, iOldAddress
);
432 bool CCECCommandHandler::HandleRoutingInformation(const cec_command
&command
)
434 if (command
.parameters
.size
== 2)
436 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
437 m_processor
->SetActiveSource(iNewAddress
);
443 bool CCECCommandHandler::HandleSetMenuLanguage(const cec_command
&command
)
445 if (command
.parameters
.size
== 3)
447 CCECBusDevice
*device
= GetDevice(command
.initiator
);
450 cec_menu_language language
;
451 language
.device
= command
.initiator
;
452 for (uint8_t iPtr
= 0; iPtr
< 4; iPtr
++)
453 language
.language
[iPtr
] = command
.parameters
[iPtr
];
454 language
.language
[3] = 0;
455 device
->SetMenuLanguage(language
);
462 bool CCECCommandHandler::HandleSetOSDName(const cec_command
&command
)
464 if (command
.parameters
.size
> 0)
466 CCECBusDevice
*device
= GetDevice(command
.initiator
);
470 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
471 buf
[iPtr
] = (char)command
.parameters
[iPtr
];
472 buf
[command
.parameters
.size
] = 0;
474 CStdString
strName(buf
);
475 device
->SetOSDName(strName
);
483 bool CCECCommandHandler::HandleSetStreamPath(const cec_command
&command
)
485 if (m_processor
->IsStarted() && command
.parameters
.size
>= 2)
487 uint16_t iStreamAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
489 strLog
.Format(">> %i sets stream path to physical address %04x", command
.initiator
, iStreamAddress
);
490 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
492 /* one of the device handled by libCEC has been made active */
493 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamAddress
);
494 if (device
&& m_busDevice
->MyLogicalAddressContains(device
->GetLogicalAddress()))
496 device
->SetActiveSource();
497 device
->TransmitActiveSource();
503 bool CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command
&command
)
505 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
507 CCECBusDevice
*device
= GetDevice(command
.destination
);
508 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
510 if (command
.parameters
.size
>= 2)
512 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
513 ((CCECAudioSystem
*) device
)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON
);
514 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
515 CCECBusDevice
*newActiveDevice
= GetDeviceByPhysicalAddress(iNewAddress
);
517 newActiveDevice
->SetActiveSource();
518 return ((CCECAudioSystem
*) device
)->TransmitSetSystemAudioMode(command
.initiator
);
522 ((CCECAudioSystem
*) device
)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF
);
523 return ((CCECAudioSystem
*) device
)->TransmitSetSystemAudioMode(command
.initiator
);
530 bool CCECCommandHandler::HandleStandby(const cec_command
&command
)
532 CCECBusDevice
*device
= GetDevice(command
.initiator
);
534 device
->SetPowerStatus(CEC_POWER_STATUS_STANDBY
);
539 bool CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command
&command
)
541 if (command
.parameters
.size
== 1)
543 CCECBusDevice
*device
= GetDevice(command
.initiator
);
544 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
546 ((CCECAudioSystem
*)device
)->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
554 bool CCECCommandHandler::HandleSetSystemAudioMode(const cec_command
&command
)
556 if (command
.parameters
.size
== 1)
558 CCECBusDevice
*device
= GetDevice(command
.initiator
);
559 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
561 ((CCECAudioSystem
*)device
)->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
569 bool CCECCommandHandler::HandleTextViewOn(const cec_command
&command
)
571 m_processor
->m_busDevices
[command
.initiator
]->SetActiveSource();
575 bool CCECCommandHandler::HandleUserControlPressed(const cec_command
&command
)
577 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
) && command
.parameters
.size
> 0)
579 m_processor
->AddKey();
581 if (command
.parameters
[0] <= CEC_USER_CONTROL_CODE_MAX
)
584 strLog
.Format("key pressed: %x", command
.parameters
[0]);
585 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
587 if (command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER
||
588 command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION
)
590 CCECBusDevice
*device
= GetDevice(command
.destination
);
593 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
594 if (device
->MyLogicalAddressContains(device
->GetLogicalAddress()))
596 device
->SetActiveSource();
597 device
->TransmitActiveSource();
599 if (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
||
600 device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
601 ((CCECPlaybackDevice
*)device
)->TransmitDeckStatus(command
.initiator
);
607 m_processor
->SetCurrentButton((cec_user_control_code
) command
.parameters
[0]);
615 bool CCECCommandHandler::HandleUserControlRelease(const cec_command
&command
)
617 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
618 m_processor
->AddKey();
623 void CCECCommandHandler::UnhandledCommand(const cec_command
&command
)
626 strLog
.Format("unhandled command with opcode %02x from address %d", command
.opcode
, command
.initiator
);
627 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
);
630 unsigned int CCECCommandHandler::GetMyDevices(vector
<CCECBusDevice
*> &devices
) const
632 unsigned int iReturn(0);
634 cec_logical_addresses addresses
= m_processor
->GetLogicalAddresses();
635 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
639 devices
.push_back(GetDevice((cec_logical_address
) iPtr
));
647 CCECBusDevice
*CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress
) const
649 CCECBusDevice
*device
= NULL
;
651 if (iLogicalAddress
>= CECDEVICE_TV
&& iLogicalAddress
<= CECDEVICE_BROADCAST
)
652 device
= m_processor
->m_busDevices
[iLogicalAddress
];
657 CCECBusDevice
*CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
) const
659 return m_processor
->GetDeviceByPhysicalAddress(iPhysicalAddress
);
662 CCECBusDevice
*CCECCommandHandler::GetDeviceByType(cec_device_type type
) const
664 return m_processor
->GetDeviceByType(type
);
667 bool CCECCommandHandler::SetVendorId(const cec_command
&command
)
669 bool bChanged(false);
670 if (command
.parameters
.size
< 3)
672 m_busDevice
->AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
676 uint64_t iVendorId
= ((uint64_t)command
.parameters
[0] << 16) +
677 ((uint64_t)command
.parameters
[1] << 8) +
678 (uint64_t)command
.parameters
[2];
680 CCECBusDevice
*device
= GetDevice((cec_logical_address
) command
.initiator
);
682 bChanged
= device
->SetVendorId(iVendorId
);
686 void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress
, uint16_t iNewAddress
)
688 if (!m_busDevice
->MyLogicalAddressContains(iAddress
))
690 bool bOurAddress(m_processor
->GetPhysicalAddress() == iNewAddress
);
691 GetDevice(iAddress
)->SetPhysicalAddress(iNewAddress
);
694 /* another device reported the same physical address as ours
695 * since we don't have physical address detection yet, we'll just use the
696 * given address, increased by 0x100 for now */
697 m_processor
->SetPhysicalAddress(iNewAddress
+ 0x100);
702 void CCECCommandHandler::HandlePoll(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
705 strLog
.Format("<< POLL: %s (%x) -> %s (%x)", m_processor
->ToString(iInitiator
), iInitiator
, m_processor
->ToString(iDestination
), iDestination
);
706 m_processor
->AddLog(CEC_LOG_DEBUG
, strLog
);
709 bool CCECCommandHandler::HandleReceiveFailed(void)
711 /* default = error */
715 bool CCECCommandHandler::TransmitImageViewOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
718 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_IMAGE_VIEW_ON
);
720 return Transmit(command
, false);
723 bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
726 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_STANDBY
);
728 return Transmit(command
, false);
731 bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
734 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_CEC_VERSION
);
736 return Transmit(command
);
739 bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
742 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_MENU_LANGUAGE
);
744 return Transmit(command
);
747 bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
750 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_OSD_NAME
);
752 return Transmit(command
);
755 bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
758 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
);
760 return Transmit(command
);
763 bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
766 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
);
768 return Transmit(command
);
771 bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
774 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
776 return Transmit(command
);
779 bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
782 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
783 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
784 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
786 return Transmit(command
, false);
789 bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_version cecVersion
)
792 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_CEC_VERSION
);
793 command
.parameters
.PushBack((uint8_t)cecVersion
);
795 return Transmit(command
, false);
798 bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
801 cec_command::Format(command
, iInitiator
, CECDEVICE_TV
, CEC_OPCODE_INACTIVE_SOURCE
);
802 command
.parameters
.PushBack((iPhysicalAddress
>> 8) & 0xFF);
803 command
.parameters
.PushBack(iPhysicalAddress
& 0xFF);
805 return Transmit(command
, false);
808 bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_menu_state menuState
)
811 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_MENU_STATUS
);
812 command
.parameters
.PushBack((uint8_t)menuState
);
814 return Transmit(command
, false);
817 bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, CStdString strDeviceName
)
820 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_NAME
);
821 for (unsigned int iPtr
= 0; iPtr
< strDeviceName
.length(); iPtr
++)
822 command
.parameters
.PushBack(strDeviceName
.at(iPtr
));
824 return Transmit(command
, false);
827 bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_display_control duration
, const char *strMessage
)
830 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_STRING
);
831 command
.parameters
.PushBack((uint8_t)duration
);
833 unsigned int iLen
= strlen(strMessage
);
834 if (iLen
> 13) iLen
= 13;
836 for (unsigned int iPtr
= 0; iPtr
< iLen
; iPtr
++)
837 command
.parameters
.PushBack(strMessage
[iPtr
]);
839 return Transmit(command
, false);
842 bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
, cec_device_type type
)
845 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
846 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
847 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
848 command
.parameters
.PushBack((uint8_t) (type
));
850 return Transmit(command
, false);
853 bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
856 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_NONE
);
858 return Transmit(command
, false);
861 bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_power_status state
)
864 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_POWER_STATUS
);
865 command
.parameters
.PushBack((uint8_t) state
);
867 return Transmit(command
, false);
870 bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator
, uint64_t iVendorId
)
873 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_DEVICE_VENDOR_ID
);
875 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 16) & 0xFF));
876 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 8) & 0xFF));
877 command
.parameters
.PushBack((uint8_t) ((uint64_t)iVendorId
& 0xFF));
879 return Transmit(command
, false);
882 bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, uint8_t state
)
885 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_AUDIO_STATUS
);
886 command
.parameters
.PushBack(state
);
888 return Transmit(command
, false);
891 bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
)
894 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
);
895 command
.parameters
.PushBack((uint8_t)state
);
897 return Transmit(command
, false);
900 bool CCECCommandHandler::TransmitSystemAudioModeStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
)
903 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
);
904 command
.parameters
.PushBack((uint8_t)state
);
906 return Transmit(command
, false);
909 bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_deck_info state
)
912 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_DECK_STATUS
);
913 command
.PushBack((uint8_t)state
);
915 return Transmit(command
, false);
918 bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = true */)
921 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_PRESSED
);
922 command
.parameters
.PushBack((uint8_t)key
);
924 return Transmit(command
, bWait
);
927 bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWait
/* = true */)
930 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_RELEASE
);
932 return Transmit(command
, bWait
);
935 bool CCECCommandHandler::Transmit(cec_command
&command
, bool bExpectResponse
/* = true */)
938 command
.transmit_timeout
= m_iTransmitTimeout
;
941 uint8_t iTries(0), iMaxTries(command
.opcode
== CEC_OPCODE_NONE
? 1 : m_iTransmitRetries
+ 1);
942 CLockObject
writeLock(&m_processor
->m_transmitMutex
);
943 CLockObject
receiveLock(&m_receiveMutex
);
945 while (!bReturn
&& ++iTries
<= iMaxTries
)
947 if (m_processor
->Transmit(command
))
949 m_processor
->AddLog(CEC_LOG_DEBUG
, "command transmitted");
950 bReturn
= bExpectResponse
?
951 m_condition
.Wait(&m_receiveMutex
, m_iTransmitWait
) :
961 bool CCECCommandHandler::InitHandler(void)
963 if (m_busDevice
->GetLogicalAddress() == CECDEVICE_TV
)
965 CCECBusDevice
*primary
= m_processor
->GetPrimaryDevice();
966 primary
->SetPowerStatus(CEC_POWER_STATUS_ON
);
967 primary
->SetMenuState(CEC_MENU_STATE_ACTIVATED
);
969 if (m_processor
->GetPrimaryDevice()->GetPhysicalAddress(false) != 0xffff)
971 m_processor
->SetActiveSource();
972 primary
->TransmitMenuState(m_busDevice
->GetLogicalAddress());
973 m_bHandlerInited
= true;
979 void CCECCommandHandler::MarkBusy(void)
981 CLockObject
receiveLock(&m_receiveMutex
);
985 bool CCECCommandHandler::MarkReady(void)
987 CLockObject
receiveLock(&m_receiveMutex
);
988 return m_iUseCounter
> 0 ? (--m_iUseCounter
== 0) : true;
991 bool CCECCommandHandler::InUse(void)
993 CLockObject
receiveLock(&m_receiveMutex
);
994 return m_iUseCounter
> 0;