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
)
54 CCECCommandHandler::~CCECCommandHandler(void)
56 CLockObject
lock(&m_processor
->m_transmitMutex
);
57 CLockObject
receiveLock(&m_receiveMutex
);
58 m_condition
.Broadcast();
61 bool CCECCommandHandler::HandleCommand(const cec_command
&command
)
63 bool bHandled(true), bHandlerChanged(false);
67 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
);
68 m_busDevice
->AddLog(CEC_LOG_NOTICE
, strLog
);
70 m_processor
->AddCommand(command
);
72 switch(command
.opcode
)
74 case CEC_OPCODE_REPORT_POWER_STATUS
:
75 HandleReportPowerStatus(command
);
77 case CEC_OPCODE_CEC_VERSION
:
78 HandleDeviceCecVersion(command
);
80 case CEC_OPCODE_SET_MENU_LANGUAGE
:
81 HandleSetMenuLanguage(command
);
83 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
84 HandleGivePhysicalAddress(command
);
86 case CEC_OPCODE_GIVE_OSD_NAME
:
87 HandleGiveOSDName(command
);
89 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
90 HandleGiveDeviceVendorId(command
);
92 case CEC_OPCODE_DEVICE_VENDOR_ID
:
93 bHandlerChanged
= HandleDeviceVendorId(command
);
95 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
96 HandleDeviceVendorCommandWithId(command
);
98 case CEC_OPCODE_GIVE_DECK_STATUS
:
99 HandleGiveDeckStatus(command
);
101 case CEC_OPCODE_DECK_CONTROL
:
102 HandleDeckControl(command
);
104 case CEC_OPCODE_MENU_REQUEST
:
105 HandleMenuRequest(command
);
107 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
108 HandleGiveDevicePowerStatus(command
);
110 case CEC_OPCODE_GET_CEC_VERSION
:
111 HandleGetCecVersion(command
);
113 case CEC_OPCODE_USER_CONTROL_PRESSED
:
114 HandleUserControlPressed(command
);
116 case CEC_OPCODE_USER_CONTROL_RELEASE
:
117 HandleUserControlRelease(command
);
119 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
120 HandleGiveAudioStatus(command
);
122 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
123 HandleGiveSystemAudioModeStatus(command
);
125 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
126 HandleSystemAudioModeRequest(command
);
128 case CEC_OPCODE_REPORT_AUDIO_STATUS
:
129 HandleReportAudioStatus(command
);
131 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
:
132 HandleSystemAudioModeStatus(command
);
134 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
:
135 HandleSetSystemAudioMode(command
);
137 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
138 HandleRequestActiveSource(command
);
140 case CEC_OPCODE_SET_STREAM_PATH
:
141 HandleSetStreamPath(command
);
143 case CEC_OPCODE_ROUTING_CHANGE
:
144 HandleRoutingChange(command
);
146 case CEC_OPCODE_ROUTING_INFORMATION
:
147 HandleRoutingInformation(command
);
149 case CEC_OPCODE_STANDBY
:
150 HandleStandby(command
);
152 case CEC_OPCODE_ACTIVE_SOURCE
:
153 HandleActiveSource(command
);
155 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
:
156 HandleReportPhysicalAddress(command
);
158 case CEC_OPCODE_SET_OSD_NAME
:
159 HandleSetOSDName(command
);
161 case CEC_OPCODE_IMAGE_VIEW_ON
:
162 HandleImageViewOn(command
);
164 case CEC_OPCODE_TEXT_VIEW_ON
:
165 HandleTextViewOn(command
);
167 case CEC_OPCODE_FEATURE_ABORT
:
168 HandleFeatureAbort(command
);
170 case CEC_OPCODE_VENDOR_COMMAND
:
171 HandleVendorCommand(command
);
174 UnhandledCommand(command
);
179 if (bHandled
&& !bHandlerChanged
)
181 CLockObject
lock(&m_receiveMutex
);
182 if (m_expectedResponse
== CEC_OPCODE_NONE
||
183 m_expectedResponse
== command
.opcode
)
184 m_condition
.Signal();
191 bool CCECCommandHandler::HandleActiveSource(const cec_command
&command
)
193 if (command
.parameters
.size
== 2)
195 uint16_t iAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
196 return m_processor
->SetActiveSource(iAddress
);
202 bool CCECCommandHandler::HandleDeckControl(const cec_command
&command
)
204 CCECBusDevice
*device
= GetDevice(command
.destination
);
205 if (device
&& (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
) && command
.parameters
.size
> 0)
207 ((CCECPlaybackDevice
*) device
)->SetDeckControlMode((cec_deck_control_mode
) command
.parameters
[0]);
214 bool CCECCommandHandler::HandleDeviceCecVersion(const cec_command
&command
)
216 if (command
.parameters
.size
== 1)
218 CCECBusDevice
*device
= GetDevice(command
.initiator
);
220 device
->SetCecVersion((cec_version
) command
.parameters
[0]);
226 bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command
&command
)
228 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
229 m_processor
->TransmitAbort(command
.initiator
, command
.opcode
, CEC_ABORT_REASON_REFUSED
);
234 bool CCECCommandHandler::HandleDeviceVendorId(const cec_command
&command
)
236 return SetVendorId(command
);
239 bool CCECCommandHandler::HandleFeatureAbort(const cec_command
&command
)
241 if (command
.parameters
.size
== 2)
243 m_processor
->m_busDevices
[command
.initiator
]->SetUnsupportedFeature((cec_opcode
)command
.parameters
[0]);
248 bool CCECCommandHandler::HandleGetCecVersion(const cec_command
&command
)
250 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
252 CCECBusDevice
*device
= GetDevice(command
.destination
);
254 return device
->TransmitCECVersion(command
.initiator
);
260 bool CCECCommandHandler::HandleGiveAudioStatus(const cec_command
&command
)
262 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
264 CCECBusDevice
*device
= GetDevice(command
.destination
);
265 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
266 return ((CCECAudioSystem
*) device
)->TransmitAudioStatus(command
.initiator
);
272 bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
274 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
276 CCECBusDevice
*device
= GetDevice(command
.destination
);
277 if (device
&& (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
))
278 return ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(command
.initiator
);
284 bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command
&command
)
286 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
288 CCECBusDevice
*device
= GetDevice(command
.destination
);
290 return device
->TransmitPowerState(command
.initiator
);
296 bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command
&command
)
298 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
300 CCECBusDevice
*device
= GetDevice(command
.destination
);
302 return device
->TransmitVendorID(command
.initiator
);
308 bool CCECCommandHandler::HandleGiveOSDName(const cec_command
&command
)
310 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
312 CCECBusDevice
*device
= GetDevice(command
.destination
);
314 return device
->TransmitOSDName(command
.initiator
);
320 bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command
&command
)
322 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
324 CCECBusDevice
*device
= GetDevice(command
.destination
);
327 device
->SetActiveSource();
328 return device
->TransmitPhysicalAddress() &&
329 device
->TransmitActiveSource();
336 bool CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command
&command
)
338 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
340 CCECBusDevice
*device
= GetDevice(command
.destination
);
341 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
342 return ((CCECAudioSystem
*) device
)->TransmitSystemAudioModeStatus(command
.initiator
);
348 bool CCECCommandHandler::HandleImageViewOn(const cec_command
&command
)
350 m_processor
->m_busDevices
[command
.initiator
]->SetActiveSource();
354 bool CCECCommandHandler::HandleMenuRequest(const cec_command
&command
)
356 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
358 if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_QUERY
)
360 CCECBusDevice
*device
= GetDevice(command
.destination
);
362 return device
->TransmitMenuState(command
.initiator
);
369 bool CCECCommandHandler::HandleReportAudioStatus(const cec_command
&command
)
371 if (command
.parameters
.size
== 1)
373 CCECBusDevice
*device
= GetDevice(command
.initiator
);
374 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
376 ((CCECAudioSystem
*)device
)->SetAudioStatus(command
.parameters
[0]);
383 bool CCECCommandHandler::HandleReportPhysicalAddress(const cec_command
&command
)
385 if (command
.parameters
.size
== 3)
387 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
388 SetPhysicalAddress(command
.initiator
, iNewAddress
);
393 bool CCECCommandHandler::HandleReportPowerStatus(const cec_command
&command
)
395 if (command
.parameters
.size
== 1)
397 CCECBusDevice
*device
= GetDevice(command
.initiator
);
399 device
->SetPowerStatus((cec_power_status
) command
.parameters
[0]);
404 bool CCECCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
406 if (m_processor
->IsStarted())
409 strLog
.Format(">> %i requests active source", (uint8_t) command
.initiator
);
410 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
412 vector
<CCECBusDevice
*> devices
;
413 for (int iDevicePtr
= (int)GetMyDevices(devices
)-1; iDevicePtr
>=0; iDevicePtr
--)
414 devices
[iDevicePtr
]->TransmitActiveSource();
421 bool CCECCommandHandler::HandleRoutingChange(const cec_command
&command
)
423 if (command
.parameters
.size
== 4)
425 uint16_t iOldAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
426 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[2] << 8) | ((uint16_t)command
.parameters
[3]);
428 CCECBusDevice
*device
= GetDevice(command
.initiator
);
430 device
->SetStreamPath(iNewAddress
, iOldAddress
);
435 bool CCECCommandHandler::HandleRoutingInformation(const cec_command
&command
)
437 if (command
.parameters
.size
== 2)
439 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
440 m_processor
->SetActiveSource(iNewAddress
);
446 bool CCECCommandHandler::HandleSetMenuLanguage(const cec_command
&command
)
448 if (command
.parameters
.size
== 3)
450 CCECBusDevice
*device
= GetDevice(command
.initiator
);
453 cec_menu_language language
;
454 language
.device
= command
.initiator
;
455 for (uint8_t iPtr
= 0; iPtr
< 4; iPtr
++)
456 language
.language
[iPtr
] = command
.parameters
[iPtr
];
457 language
.language
[3] = 0;
458 device
->SetMenuLanguage(language
);
465 bool CCECCommandHandler::HandleSetOSDName(const cec_command
&command
)
467 if (command
.parameters
.size
> 0)
469 CCECBusDevice
*device
= GetDevice(command
.initiator
);
473 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
474 buf
[iPtr
] = (char)command
.parameters
[iPtr
];
475 buf
[command
.parameters
.size
] = 0;
477 CStdString
strName(buf
);
478 device
->SetOSDName(strName
);
486 bool CCECCommandHandler::HandleSetStreamPath(const cec_command
&command
)
488 if (m_processor
->IsStarted() && command
.parameters
.size
>= 2)
490 uint16_t iStreamAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
492 strLog
.Format(">> %i sets stream path to physical address %04x", command
.initiator
, iStreamAddress
);
493 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
495 /* one of the device handled by libCEC has been made active */
496 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamAddress
);
497 if (device
&& m_busDevice
->MyLogicalAddressContains(device
->GetLogicalAddress()))
499 device
->SetActiveSource();
500 device
->TransmitActiveSource();
506 bool CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command
&command
)
508 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
510 CCECBusDevice
*device
= GetDevice(command
.destination
);
511 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
513 if (command
.parameters
.size
>= 2)
515 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
516 ((CCECAudioSystem
*) device
)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON
);
517 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
518 CCECBusDevice
*newActiveDevice
= GetDeviceByPhysicalAddress(iNewAddress
);
520 newActiveDevice
->SetActiveSource();
521 return ((CCECAudioSystem
*) device
)->TransmitSetSystemAudioMode(command
.initiator
);
525 ((CCECAudioSystem
*) device
)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF
);
526 return ((CCECAudioSystem
*) device
)->TransmitSetSystemAudioMode(command
.initiator
);
533 bool CCECCommandHandler::HandleStandby(const cec_command
&command
)
535 CCECBusDevice
*device
= GetDevice(command
.initiator
);
537 device
->SetPowerStatus(CEC_POWER_STATUS_STANDBY
);
542 bool CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command
&command
)
544 if (command
.parameters
.size
== 1)
546 CCECBusDevice
*device
= GetDevice(command
.initiator
);
547 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
549 ((CCECAudioSystem
*)device
)->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
557 bool CCECCommandHandler::HandleSetSystemAudioMode(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::HandleTextViewOn(const cec_command
&command
)
574 m_processor
->m_busDevices
[command
.initiator
]->SetActiveSource();
578 bool CCECCommandHandler::HandleUserControlPressed(const cec_command
&command
)
580 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
) && command
.parameters
.size
> 0)
582 m_processor
->AddKey();
584 if (command
.parameters
[0] <= CEC_USER_CONTROL_CODE_MAX
)
587 strLog
.Format("key pressed: %x", command
.parameters
[0]);
588 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
590 if (command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER
||
591 command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION
)
593 CCECBusDevice
*device
= GetDevice(command
.destination
);
596 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
597 if (device
->MyLogicalAddressContains(device
->GetLogicalAddress()))
599 device
->SetActiveSource();
600 device
->TransmitActiveSource();
602 if (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
||
603 device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
604 ((CCECPlaybackDevice
*)device
)->TransmitDeckStatus(command
.initiator
);
610 m_processor
->SetCurrentButton((cec_user_control_code
) command
.parameters
[0]);
618 bool CCECCommandHandler::HandleUserControlRelease(const cec_command
&command
)
620 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
621 m_processor
->AddKey();
626 void CCECCommandHandler::UnhandledCommand(const cec_command
&command
)
629 strLog
.Format("unhandled command with opcode %02x from address %d", command
.opcode
, command
.initiator
);
630 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
);
633 unsigned int CCECCommandHandler::GetMyDevices(vector
<CCECBusDevice
*> &devices
) const
635 unsigned int iReturn(0);
637 cec_logical_addresses addresses
= m_processor
->GetLogicalAddresses();
638 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
642 devices
.push_back(GetDevice((cec_logical_address
) iPtr
));
650 CCECBusDevice
*CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress
) const
652 CCECBusDevice
*device
= NULL
;
654 if (iLogicalAddress
>= CECDEVICE_TV
&& iLogicalAddress
<= CECDEVICE_BROADCAST
)
655 device
= m_processor
->m_busDevices
[iLogicalAddress
];
660 CCECBusDevice
*CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
) const
662 return m_processor
->GetDeviceByPhysicalAddress(iPhysicalAddress
);
665 CCECBusDevice
*CCECCommandHandler::GetDeviceByType(cec_device_type type
) const
667 return m_processor
->GetDeviceByType(type
);
670 bool CCECCommandHandler::SetVendorId(const cec_command
&command
)
672 bool bChanged(false);
673 if (command
.parameters
.size
< 3)
675 m_busDevice
->AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
679 uint64_t iVendorId
= ((uint64_t)command
.parameters
[0] << 16) +
680 ((uint64_t)command
.parameters
[1] << 8) +
681 (uint64_t)command
.parameters
[2];
683 CCECBusDevice
*device
= GetDevice((cec_logical_address
) command
.initiator
);
685 bChanged
= device
->SetVendorId(iVendorId
);
689 void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress
, uint16_t iNewAddress
)
691 if (!m_busDevice
->MyLogicalAddressContains(iAddress
))
693 bool bOurAddress(m_processor
->GetPhysicalAddress() == iNewAddress
);
694 GetDevice(iAddress
)->SetPhysicalAddress(iNewAddress
);
697 /* another device reported the same physical address as ours
698 * since we don't have physical address detection yet, we'll just use the
699 * given address, increased by 0x100 for now */
700 m_processor
->SetPhysicalAddress(iNewAddress
+ 0x100);
705 void CCECCommandHandler::HandlePoll(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
708 strLog
.Format("<< POLL: %s (%x) -> %s (%x)", m_processor
->ToString(iInitiator
), iInitiator
, m_processor
->ToString(iDestination
), iDestination
);
709 m_processor
->AddLog(CEC_LOG_DEBUG
, strLog
);
712 bool CCECCommandHandler::HandleReceiveFailed(void)
714 /* default = error */
718 bool CCECCommandHandler::TransmitImageViewOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
721 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_IMAGE_VIEW_ON
);
723 return Transmit(command
, false);
726 bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
729 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_STANDBY
);
731 return Transmit(command
, false);
734 bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
737 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_CEC_VERSION
);
739 return Transmit(command
, true, CEC_OPCODE_CEC_VERSION
);
742 bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
745 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_MENU_LANGUAGE
);
747 return Transmit(command
, true, CEC_OPCODE_SET_MENU_LANGUAGE
);
750 bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
753 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_OSD_NAME
);
755 return Transmit(command
, true, CEC_OPCODE_SET_OSD_NAME
);
758 bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
761 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
);
763 return Transmit(command
, true, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
766 bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
769 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
);
771 return Transmit(command
, true, CEC_OPCODE_REPORT_POWER_STATUS
);
774 bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
777 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
779 return Transmit(command
, true, CEC_OPCODE_DEVICE_VENDOR_ID
);
782 bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
785 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
786 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
787 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
789 return Transmit(command
, false);
792 bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_version cecVersion
)
795 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_CEC_VERSION
);
796 command
.parameters
.PushBack((uint8_t)cecVersion
);
798 return Transmit(command
, false);
801 bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
804 cec_command::Format(command
, iInitiator
, CECDEVICE_TV
, CEC_OPCODE_INACTIVE_SOURCE
);
805 command
.parameters
.PushBack((iPhysicalAddress
>> 8) & 0xFF);
806 command
.parameters
.PushBack(iPhysicalAddress
& 0xFF);
808 return Transmit(command
, false);
811 bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_menu_state menuState
)
814 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_MENU_STATUS
);
815 command
.parameters
.PushBack((uint8_t)menuState
);
817 return Transmit(command
, false);
820 bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, CStdString strDeviceName
)
823 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_NAME
);
824 for (unsigned int iPtr
= 0; iPtr
< strDeviceName
.length(); iPtr
++)
825 command
.parameters
.PushBack(strDeviceName
.at(iPtr
));
827 return Transmit(command
, false);
830 bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_display_control duration
, const char *strMessage
)
833 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_STRING
);
834 command
.parameters
.PushBack((uint8_t)duration
);
836 unsigned int iLen
= strlen(strMessage
);
837 if (iLen
> 13) iLen
= 13;
839 for (unsigned int iPtr
= 0; iPtr
< iLen
; iPtr
++)
840 command
.parameters
.PushBack(strMessage
[iPtr
]);
842 return Transmit(command
, false);
845 bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
, cec_device_type type
)
848 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
849 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
850 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
851 command
.parameters
.PushBack((uint8_t) (type
));
853 return Transmit(command
, false);
856 bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
859 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_NONE
);
861 return Transmit(command
, false);
864 bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_power_status state
)
867 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_POWER_STATUS
);
868 command
.parameters
.PushBack((uint8_t) state
);
870 return Transmit(command
, false);
873 bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator
, uint64_t iVendorId
)
876 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_DEVICE_VENDOR_ID
);
878 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 16) & 0xFF));
879 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 8) & 0xFF));
880 command
.parameters
.PushBack((uint8_t) ((uint64_t)iVendorId
& 0xFF));
882 return Transmit(command
, false);
885 bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, uint8_t state
)
888 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_AUDIO_STATUS
);
889 command
.parameters
.PushBack(state
);
891 return Transmit(command
, false);
894 bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
)
897 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
);
898 command
.parameters
.PushBack((uint8_t)state
);
900 return Transmit(command
, false);
903 bool CCECCommandHandler::TransmitSystemAudioModeStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
)
906 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
);
907 command
.parameters
.PushBack((uint8_t)state
);
909 return Transmit(command
, false);
912 bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_deck_info state
)
915 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_DECK_STATUS
);
916 command
.PushBack((uint8_t)state
);
918 return Transmit(command
, false);
921 bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = true */)
924 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_PRESSED
);
925 command
.parameters
.PushBack((uint8_t)key
);
927 return Transmit(command
, bWait
);
930 bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWait
/* = true */)
933 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_RELEASE
);
935 return Transmit(command
, bWait
);
938 bool CCECCommandHandler::Transmit(cec_command
&command
, bool bExpectResponse
/* = true */, cec_opcode expectedResponse
/* = CEC_OPCODE_NONE */)
941 command
.transmit_timeout
= m_iTransmitTimeout
;
944 uint8_t iTries(0), iMaxTries(command
.opcode
== CEC_OPCODE_NONE
? 1 : m_iTransmitRetries
+ 1);
945 CLockObject
writeLock(&m_processor
->m_transmitMutex
);
946 CLockObject
receiveLock(&m_receiveMutex
);
948 while (!bReturn
&& ++iTries
<= iMaxTries
)
950 m_expectedResponse
= expectedResponse
;
951 if (m_processor
->Transmit(command
))
953 m_processor
->AddLog(CEC_LOG_DEBUG
, "command transmitted");
954 bReturn
= bExpectResponse
?
955 m_condition
.Wait(&m_receiveMutex
, m_iTransmitWait
) :
965 bool CCECCommandHandler::InitHandler(void)
967 if (m_busDevice
->GetLogicalAddress() == CECDEVICE_TV
)
969 CCECBusDevice
*primary
= m_processor
->GetPrimaryDevice();
970 primary
->SetPowerStatus(CEC_POWER_STATUS_ON
);
971 primary
->SetMenuState(CEC_MENU_STATE_ACTIVATED
);
973 if (m_processor
->GetPrimaryDevice()->GetPhysicalAddress(false) != 0xffff)
975 m_processor
->SetActiveSource();
976 primary
->TransmitMenuState(m_busDevice
->GetLogicalAddress());
977 m_bHandlerInited
= true;
983 void CCECCommandHandler::MarkBusy(void)
985 CLockObject
receiveLock(&m_receiveMutex
);
989 bool CCECCommandHandler::MarkReady(void)
991 CLockObject
receiveLock(&m_receiveMutex
);
992 return m_iUseCounter
> 0 ? (--m_iUseCounter
== 0) : true;
995 bool CCECCommandHandler::InUse(void)
997 CLockObject
receiveLock(&m_receiveMutex
);
998 return m_iUseCounter
> 0;