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),
50 m_expectedResponse(CEC_OPCODE_NONE
),
51 m_bOPTSendDeckStatusUpdateOnActiveSource(true)
55 CCECCommandHandler::~CCECCommandHandler(void)
57 CLockObject
lock(&m_processor
->m_transmitMutex
);
58 CLockObject
receiveLock(&m_receiveMutex
);
59 m_condition
.Broadcast();
62 bool CCECCommandHandler::HandleCommand(const cec_command
&command
)
68 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
);
69 m_busDevice
->AddLog(CEC_LOG_NOTICE
, strLog
);
71 m_processor
->AddCommand(command
);
73 switch(command
.opcode
)
75 case CEC_OPCODE_REPORT_POWER_STATUS
:
76 HandleReportPowerStatus(command
);
78 case CEC_OPCODE_CEC_VERSION
:
79 HandleDeviceCecVersion(command
);
81 case CEC_OPCODE_SET_MENU_LANGUAGE
:
82 HandleSetMenuLanguage(command
);
84 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
85 if (m_processor
->IsInitialised())
86 HandleGivePhysicalAddress(command
);
88 case CEC_OPCODE_GIVE_OSD_NAME
:
89 if (m_processor
->IsInitialised())
90 HandleGiveOSDName(command
);
92 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
93 if (m_processor
->IsInitialised())
94 HandleGiveDeviceVendorId(command
);
96 case CEC_OPCODE_DEVICE_VENDOR_ID
:
97 HandleDeviceVendorId(command
);
99 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
100 HandleDeviceVendorCommandWithId(command
);
102 case CEC_OPCODE_GIVE_DECK_STATUS
:
103 if (m_processor
->IsInitialised())
104 HandleGiveDeckStatus(command
);
106 case CEC_OPCODE_DECK_CONTROL
:
107 HandleDeckControl(command
);
109 case CEC_OPCODE_MENU_REQUEST
:
110 if (m_processor
->IsInitialised())
111 HandleMenuRequest(command
);
113 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
114 if (m_processor
->IsInitialised())
115 HandleGiveDevicePowerStatus(command
);
117 case CEC_OPCODE_GET_CEC_VERSION
:
118 if (m_processor
->IsInitialised())
119 HandleGetCecVersion(command
);
121 case CEC_OPCODE_USER_CONTROL_PRESSED
:
122 if (m_processor
->IsInitialised())
123 HandleUserControlPressed(command
);
125 case CEC_OPCODE_USER_CONTROL_RELEASE
:
126 if (m_processor
->IsInitialised())
127 HandleUserControlRelease(command
);
129 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
130 if (m_processor
->IsInitialised())
131 HandleGiveAudioStatus(command
);
133 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
134 if (m_processor
->IsInitialised())
135 HandleGiveSystemAudioModeStatus(command
);
137 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
138 if (m_processor
->IsInitialised())
139 HandleSystemAudioModeRequest(command
);
141 case CEC_OPCODE_REPORT_AUDIO_STATUS
:
142 HandleReportAudioStatus(command
);
144 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
:
145 HandleSystemAudioModeStatus(command
);
147 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
:
148 HandleSetSystemAudioMode(command
);
150 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
151 if (m_processor
->IsInitialised())
152 HandleRequestActiveSource(command
);
154 case CEC_OPCODE_SET_STREAM_PATH
:
155 HandleSetStreamPath(command
);
157 case CEC_OPCODE_ROUTING_CHANGE
:
158 HandleRoutingChange(command
);
160 case CEC_OPCODE_ROUTING_INFORMATION
:
161 HandleRoutingInformation(command
);
163 case CEC_OPCODE_STANDBY
:
164 if (m_processor
->IsInitialised())
165 HandleStandby(command
);
167 case CEC_OPCODE_ACTIVE_SOURCE
:
168 HandleActiveSource(command
);
170 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
:
171 HandleReportPhysicalAddress(command
);
173 case CEC_OPCODE_SET_OSD_NAME
:
174 HandleSetOSDName(command
);
176 case CEC_OPCODE_IMAGE_VIEW_ON
:
177 HandleImageViewOn(command
);
179 case CEC_OPCODE_TEXT_VIEW_ON
:
180 HandleTextViewOn(command
);
182 case CEC_OPCODE_FEATURE_ABORT
:
183 HandleFeatureAbort(command
);
185 case CEC_OPCODE_VENDOR_COMMAND
:
186 HandleVendorCommand(command
);
189 UnhandledCommand(command
);
196 CLockObject
lock(&m_receiveMutex
);
197 if (m_expectedResponse
== CEC_OPCODE_NONE
||
198 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
->IsStarted() && 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
->IsStarted() && 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
->IsStarted() && 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
->IsStarted() && 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
->IsStarted() && 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
->IsStarted() && 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
->IsStarted() && 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
->IsStarted() && 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
->IsStarted() && 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
->IsStarted() && 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
->IsStarted())
424 strLog
.Format(">> %i requests active source", (uint8_t) command
.initiator
);
425 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
427 vector
<CCECBusDevice
*> devices
;
428 for (int iDevicePtr
= (int)GetMyDevices(devices
)-1; iDevicePtr
>=0; iDevicePtr
--)
429 devices
[iDevicePtr
]->TransmitActiveSource();
436 bool CCECCommandHandler::HandleRoutingChange(const cec_command
&command
)
438 if (command
.parameters
.size
== 4)
440 uint16_t iOldAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
441 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[2] << 8) | ((uint16_t)command
.parameters
[3]);
443 CCECBusDevice
*device
= GetDevice(command
.initiator
);
445 device
->SetStreamPath(iNewAddress
, iOldAddress
);
450 bool CCECCommandHandler::HandleRoutingInformation(const cec_command
&command
)
452 if (command
.parameters
.size
== 2)
454 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
455 m_processor
->SetActiveSource(iNewAddress
);
461 bool CCECCommandHandler::HandleSetMenuLanguage(const cec_command
&command
)
463 if (command
.parameters
.size
== 3)
465 CCECBusDevice
*device
= GetDevice(command
.initiator
);
468 cec_menu_language language
;
469 language
.device
= command
.initiator
;
470 for (uint8_t iPtr
= 0; iPtr
< 4; iPtr
++)
471 language
.language
[iPtr
] = command
.parameters
[iPtr
];
472 language
.language
[3] = 0;
473 device
->SetMenuLanguage(language
);
480 bool CCECCommandHandler::HandleSetOSDName(const cec_command
&command
)
482 if (command
.parameters
.size
> 0)
484 CCECBusDevice
*device
= GetDevice(command
.initiator
);
488 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
489 buf
[iPtr
] = (char)command
.parameters
[iPtr
];
490 buf
[command
.parameters
.size
] = 0;
492 CStdString
strName(buf
);
493 device
->SetOSDName(strName
);
501 bool CCECCommandHandler::HandleSetStreamPath(const cec_command
&command
)
503 if (m_processor
->IsStarted() && command
.parameters
.size
>= 2)
505 uint16_t iStreamAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
507 strLog
.Format(">> %i sets stream path to physical address %04x", command
.initiator
, iStreamAddress
);
508 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
510 /* one of the device handled by libCEC has been made active */
511 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamAddress
);
512 if (device
&& m_busDevice
->MyLogicalAddressContains(device
->GetLogicalAddress()))
514 device
->SetActiveSource();
515 device
->TransmitActiveSource();
521 bool CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command
&command
)
523 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
525 CCECBusDevice
*device
= GetDevice(command
.destination
);
526 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
528 if (command
.parameters
.size
>= 2)
530 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
531 ((CCECAudioSystem
*) device
)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON
);
532 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
533 CCECBusDevice
*newActiveDevice
= GetDeviceByPhysicalAddress(iNewAddress
);
535 newActiveDevice
->SetActiveSource();
536 return ((CCECAudioSystem
*) device
)->TransmitSetSystemAudioMode(command
.initiator
);
540 ((CCECAudioSystem
*) device
)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF
);
541 return ((CCECAudioSystem
*) device
)->TransmitSetSystemAudioMode(command
.initiator
);
548 bool CCECCommandHandler::HandleStandby(const cec_command
&command
)
550 CCECBusDevice
*device
= GetDevice(command
.initiator
);
552 device
->SetPowerStatus(CEC_POWER_STATUS_STANDBY
);
557 bool CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command
&command
)
559 if (command
.parameters
.size
== 1)
561 CCECBusDevice
*device
= GetDevice(command
.initiator
);
562 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
564 ((CCECAudioSystem
*)device
)->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
572 bool CCECCommandHandler::HandleSetSystemAudioMode(const cec_command
&command
)
574 if (command
.parameters
.size
== 1)
576 CCECBusDevice
*device
= GetDevice(command
.initiator
);
577 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
579 ((CCECAudioSystem
*)device
)->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
587 bool CCECCommandHandler::HandleTextViewOn(const cec_command
&command
)
589 m_processor
->m_busDevices
[command
.initiator
]->SetActiveSource();
593 bool CCECCommandHandler::HandleUserControlPressed(const cec_command
&command
)
595 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
) && command
.parameters
.size
> 0)
597 m_processor
->AddKey();
599 if (command
.parameters
[0] <= CEC_USER_CONTROL_CODE_MAX
)
602 strLog
.Format("key pressed: %x", command
.parameters
[0]);
603 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
605 if (command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER
||
606 command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION
)
608 CCECBusDevice
*device
= GetDevice(command
.destination
);
611 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
612 if (device
->MyLogicalAddressContains(device
->GetLogicalAddress()))
614 device
->SetActiveSource();
615 device
->TransmitActiveSource();
617 if (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
||
618 device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
619 ((CCECPlaybackDevice
*)device
)->TransmitDeckStatus(command
.initiator
);
625 m_processor
->SetCurrentButton((cec_user_control_code
) command
.parameters
[0]);
633 bool CCECCommandHandler::HandleUserControlRelease(const cec_command
&command
)
635 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
636 m_processor
->AddKey();
641 void CCECCommandHandler::UnhandledCommand(const cec_command
&command
)
644 strLog
.Format("unhandled command with opcode %02x from address %d", command
.opcode
, command
.initiator
);
645 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
);
648 unsigned int CCECCommandHandler::GetMyDevices(vector
<CCECBusDevice
*> &devices
) const
650 unsigned int iReturn(0);
652 cec_logical_addresses addresses
= m_processor
->GetLogicalAddresses();
653 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
657 devices
.push_back(GetDevice((cec_logical_address
) iPtr
));
665 CCECBusDevice
*CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress
) const
667 CCECBusDevice
*device
= NULL
;
669 if (iLogicalAddress
>= CECDEVICE_TV
&& iLogicalAddress
<= CECDEVICE_BROADCAST
)
670 device
= m_processor
->m_busDevices
[iLogicalAddress
];
675 CCECBusDevice
*CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
) const
677 return m_processor
->GetDeviceByPhysicalAddress(iPhysicalAddress
);
680 CCECBusDevice
*CCECCommandHandler::GetDeviceByType(cec_device_type type
) const
682 return m_processor
->GetDeviceByType(type
);
685 bool CCECCommandHandler::SetVendorId(const cec_command
&command
)
687 bool bChanged(false);
688 if (command
.parameters
.size
< 3)
690 m_busDevice
->AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
694 uint64_t iVendorId
= ((uint64_t)command
.parameters
[0] << 16) +
695 ((uint64_t)command
.parameters
[1] << 8) +
696 (uint64_t)command
.parameters
[2];
698 CCECBusDevice
*device
= GetDevice((cec_logical_address
) command
.initiator
);
700 bChanged
= device
->SetVendorId(iVendorId
);
704 void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress
, uint16_t iNewAddress
)
706 if (!m_busDevice
->MyLogicalAddressContains(iAddress
))
708 bool bOurAddress(m_processor
->GetPhysicalAddress() == iNewAddress
);
709 GetDevice(iAddress
)->SetPhysicalAddress(iNewAddress
);
712 /* another device reported the same physical address as ours
713 * since we don't have physical address detection yet, we'll just use the
714 * given address, increased by 0x100 for now */
715 m_processor
->SetPhysicalAddress(iNewAddress
+ 0x100);
720 void CCECCommandHandler::HandlePoll(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
723 strLog
.Format("<< POLL: %s (%x) -> %s (%x)", m_processor
->ToString(iInitiator
), iInitiator
, m_processor
->ToString(iDestination
), iDestination
);
724 m_processor
->AddLog(CEC_LOG_DEBUG
, strLog
);
727 bool CCECCommandHandler::HandleReceiveFailed(void)
729 /* default = error */
733 bool CCECCommandHandler::TransmitImageViewOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
736 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_IMAGE_VIEW_ON
);
738 return Transmit(command
, false);
741 bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
744 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_STANDBY
);
746 return Transmit(command
, false);
749 bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
752 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_CEC_VERSION
);
754 return Transmit(command
, true, CEC_OPCODE_CEC_VERSION
);
757 bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
760 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_MENU_LANGUAGE
);
762 return Transmit(command
, true, CEC_OPCODE_SET_MENU_LANGUAGE
);
765 bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
768 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_OSD_NAME
);
770 return Transmit(command
, true, CEC_OPCODE_SET_OSD_NAME
);
773 bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
776 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
);
778 return Transmit(command
, true, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
781 bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
784 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
);
786 return Transmit(command
, true, CEC_OPCODE_REPORT_POWER_STATUS
);
789 bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
792 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
794 return Transmit(command
, true, CEC_OPCODE_DEVICE_VENDOR_ID
);
797 bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
800 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
801 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
802 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
804 return Transmit(command
, false);
807 bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_version cecVersion
)
810 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_CEC_VERSION
);
811 command
.parameters
.PushBack((uint8_t)cecVersion
);
813 return Transmit(command
, false);
816 bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
819 cec_command::Format(command
, iInitiator
, CECDEVICE_TV
, CEC_OPCODE_INACTIVE_SOURCE
);
820 command
.parameters
.PushBack((iPhysicalAddress
>> 8) & 0xFF);
821 command
.parameters
.PushBack(iPhysicalAddress
& 0xFF);
823 return Transmit(command
, false);
826 bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_menu_state menuState
)
829 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_MENU_STATUS
);
830 command
.parameters
.PushBack((uint8_t)menuState
);
832 return Transmit(command
, false);
835 bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, CStdString strDeviceName
)
838 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_NAME
);
839 for (unsigned int iPtr
= 0; iPtr
< strDeviceName
.length(); iPtr
++)
840 command
.parameters
.PushBack(strDeviceName
.at(iPtr
));
842 return Transmit(command
, false);
845 bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_display_control duration
, const char *strMessage
)
848 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_STRING
);
849 command
.parameters
.PushBack((uint8_t)duration
);
851 unsigned int iLen
= strlen(strMessage
);
852 if (iLen
> 13) iLen
= 13;
854 for (unsigned int iPtr
= 0; iPtr
< iLen
; iPtr
++)
855 command
.parameters
.PushBack(strMessage
[iPtr
]);
857 return Transmit(command
, false);
860 bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
, cec_device_type type
)
863 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
864 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
865 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
866 command
.parameters
.PushBack((uint8_t) (type
));
868 return Transmit(command
, false);
871 bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
874 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_NONE
);
876 return Transmit(command
, false);
879 bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_power_status state
)
882 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_POWER_STATUS
);
883 command
.parameters
.PushBack((uint8_t) state
);
885 return Transmit(command
, false);
888 bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator
, uint64_t iVendorId
)
891 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_DEVICE_VENDOR_ID
);
893 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 16) & 0xFF));
894 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 8) & 0xFF));
895 command
.parameters
.PushBack((uint8_t) ((uint64_t)iVendorId
& 0xFF));
897 return Transmit(command
, false);
900 bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, uint8_t state
)
903 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_AUDIO_STATUS
);
904 command
.parameters
.PushBack(state
);
906 return Transmit(command
, false);
909 bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
)
912 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
);
913 command
.parameters
.PushBack((uint8_t)state
);
915 return Transmit(command
, false);
918 bool CCECCommandHandler::TransmitSystemAudioModeStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
)
921 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
);
922 command
.parameters
.PushBack((uint8_t)state
);
924 return Transmit(command
, false);
927 bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_deck_info state
)
930 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_DECK_STATUS
);
931 command
.PushBack((uint8_t)state
);
933 return Transmit(command
, false);
936 bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = true */)
939 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_PRESSED
);
940 command
.parameters
.PushBack((uint8_t)key
);
942 return Transmit(command
, bWait
);
945 bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWait
/* = true */)
948 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_RELEASE
);
950 return Transmit(command
, bWait
);
953 bool CCECCommandHandler::Transmit(cec_command
&command
, bool bExpectResponse
/* = true */, cec_opcode expectedResponse
/* = CEC_OPCODE_NONE */)
957 command
.transmit_timeout
= m_iTransmitTimeout
;
960 uint8_t iTries(0), iMaxTries(command
.opcode
== CEC_OPCODE_NONE
? 1 : m_iTransmitRetries
+ 1);
961 CLockObject
writeLock(&m_processor
->m_transmitMutex
);
962 CLockObject
receiveLock(&m_receiveMutex
);
964 while (!bReturn
&& ++iTries
<= iMaxTries
)
966 m_expectedResponse
= expectedResponse
;
967 if (m_processor
->Transmit(command
))
969 m_processor
->AddLog(CEC_LOG_DEBUG
, "command transmitted");
970 bReturn
= bExpectResponse
?
971 m_condition
.Wait(&m_receiveMutex
, m_iTransmitWait
) :
982 bool CCECCommandHandler::ActivateSource(void)
984 if (m_busDevice
->GetLogicalAddress() == CECDEVICE_TV
)
986 CCECBusDevice
*primary
= m_processor
->GetPrimaryDevice();
987 primary
->SetPowerStatus(CEC_POWER_STATUS_ON
);
988 primary
->SetMenuState(CEC_MENU_STATE_ACTIVATED
);
990 if (m_processor
->GetPrimaryDevice()->GetPhysicalAddress(false) != 0xffff)
992 m_processor
->SetActiveSource();
993 primary
->TransmitMenuState(m_busDevice
->GetLogicalAddress());
994 m_bHandlerInited
= true;
1000 void CCECCommandHandler::MarkBusy(void)
1002 CLockObject
receiveLock(&m_receiveMutex
);
1006 bool CCECCommandHandler::MarkReady(void)
1008 CLockObject
receiveLock(&m_receiveMutex
);
1009 return m_iUseCounter
> 0 ? (--m_iUseCounter
== 0) : true;
1012 bool CCECCommandHandler::InUse(void)
1014 CLockObject
receiveLock(&m_receiveMutex
);
1015 return m_iUseCounter
> 0;