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),
52 m_vendorId(CEC_VENDOR_UNKNOWN
)
56 CCECCommandHandler::~CCECCommandHandler(void)
58 CLockObject
lock(&m_processor
->m_transmitMutex
);
59 CLockObject
receiveLock(&m_receiveMutex
);
60 m_condition
.Broadcast();
63 bool CCECCommandHandler::HandleCommand(const cec_command
&command
)
69 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
);
70 m_busDevice
->AddLog(CEC_LOG_NOTICE
, strLog
);
72 m_processor
->AddCommand(command
);
74 switch(command
.opcode
)
76 case CEC_OPCODE_REPORT_POWER_STATUS
:
77 HandleReportPowerStatus(command
);
79 case CEC_OPCODE_CEC_VERSION
:
80 HandleDeviceCecVersion(command
);
82 case CEC_OPCODE_SET_MENU_LANGUAGE
:
83 HandleSetMenuLanguage(command
);
85 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
86 if (m_processor
->IsInitialised())
87 HandleGivePhysicalAddress(command
);
89 case CEC_OPCODE_GIVE_OSD_NAME
:
90 if (m_processor
->IsInitialised())
91 HandleGiveOSDName(command
);
93 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
94 if (m_processor
->IsInitialised())
95 HandleGiveDeviceVendorId(command
);
97 case CEC_OPCODE_DEVICE_VENDOR_ID
:
98 HandleDeviceVendorId(command
);
100 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
101 HandleDeviceVendorCommandWithId(command
);
103 case CEC_OPCODE_GIVE_DECK_STATUS
:
104 if (m_processor
->IsInitialised())
105 HandleGiveDeckStatus(command
);
107 case CEC_OPCODE_DECK_CONTROL
:
108 HandleDeckControl(command
);
110 case CEC_OPCODE_MENU_REQUEST
:
111 if (m_processor
->IsInitialised())
112 HandleMenuRequest(command
);
114 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
115 if (m_processor
->IsInitialised())
116 HandleGiveDevicePowerStatus(command
);
118 case CEC_OPCODE_GET_CEC_VERSION
:
119 if (m_processor
->IsInitialised())
120 HandleGetCecVersion(command
);
122 case CEC_OPCODE_USER_CONTROL_PRESSED
:
123 if (m_processor
->IsInitialised())
124 HandleUserControlPressed(command
);
126 case CEC_OPCODE_USER_CONTROL_RELEASE
:
127 if (m_processor
->IsInitialised())
128 HandleUserControlRelease(command
);
130 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
131 if (m_processor
->IsInitialised())
132 HandleGiveAudioStatus(command
);
134 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
135 if (m_processor
->IsInitialised())
136 HandleGiveSystemAudioModeStatus(command
);
138 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
139 if (m_processor
->IsInitialised())
140 HandleSystemAudioModeRequest(command
);
142 case CEC_OPCODE_REPORT_AUDIO_STATUS
:
143 HandleReportAudioStatus(command
);
145 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
:
146 HandleSystemAudioModeStatus(command
);
148 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
:
149 HandleSetSystemAudioMode(command
);
151 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
152 if (m_processor
->IsInitialised())
153 HandleRequestActiveSource(command
);
155 case CEC_OPCODE_SET_STREAM_PATH
:
156 HandleSetStreamPath(command
);
158 case CEC_OPCODE_ROUTING_CHANGE
:
159 HandleRoutingChange(command
);
161 case CEC_OPCODE_ROUTING_INFORMATION
:
162 HandleRoutingInformation(command
);
164 case CEC_OPCODE_STANDBY
:
165 if (m_processor
->IsInitialised())
166 HandleStandby(command
);
168 case CEC_OPCODE_ACTIVE_SOURCE
:
169 HandleActiveSource(command
);
171 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
:
172 HandleReportPhysicalAddress(command
);
174 case CEC_OPCODE_SET_OSD_NAME
:
175 HandleSetOSDName(command
);
177 case CEC_OPCODE_IMAGE_VIEW_ON
:
178 HandleImageViewOn(command
);
180 case CEC_OPCODE_TEXT_VIEW_ON
:
181 HandleTextViewOn(command
);
183 case CEC_OPCODE_FEATURE_ABORT
:
184 HandleFeatureAbort(command
);
186 case CEC_OPCODE_VENDOR_COMMAND
:
187 HandleVendorCommand(command
);
190 UnhandledCommand(command
);
197 CLockObject
lock(&m_receiveMutex
);
198 if (m_expectedResponse
== CEC_OPCODE_NONE
||
199 m_expectedResponse
== command
.opcode
)
200 m_condition
.Signal();
207 bool CCECCommandHandler::HandleActiveSource(const cec_command
&command
)
209 if (command
.parameters
.size
== 2)
211 uint16_t iAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
212 return m_processor
->SetActiveSource(iAddress
);
218 bool CCECCommandHandler::HandleDeckControl(const cec_command
&command
)
220 CCECBusDevice
*device
= GetDevice(command
.destination
);
221 if (device
&& (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
) && command
.parameters
.size
> 0)
223 ((CCECPlaybackDevice
*) device
)->SetDeckControlMode((cec_deck_control_mode
) command
.parameters
[0]);
230 bool CCECCommandHandler::HandleDeviceCecVersion(const cec_command
&command
)
232 if (command
.parameters
.size
== 1)
234 CCECBusDevice
*device
= GetDevice(command
.initiator
);
236 device
->SetCecVersion((cec_version
) command
.parameters
[0]);
242 bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command
&command
)
244 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
245 m_processor
->TransmitAbort(command
.initiator
, command
.opcode
, CEC_ABORT_REASON_REFUSED
);
250 bool CCECCommandHandler::HandleDeviceVendorId(const cec_command
&command
)
252 return SetVendorId(command
);
255 bool CCECCommandHandler::HandleFeatureAbort(const cec_command
&command
)
257 if (command
.parameters
.size
== 2)
259 m_processor
->m_busDevices
[command
.initiator
]->SetUnsupportedFeature((cec_opcode
)command
.parameters
[0]);
264 bool CCECCommandHandler::HandleGetCecVersion(const cec_command
&command
)
266 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
268 CCECBusDevice
*device
= GetDevice(command
.destination
);
270 return device
->TransmitCECVersion(command
.initiator
);
276 bool CCECCommandHandler::HandleGiveAudioStatus(const cec_command
&command
)
278 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
280 CCECBusDevice
*device
= GetDevice(command
.destination
);
281 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
282 return ((CCECAudioSystem
*) device
)->TransmitAudioStatus(command
.initiator
);
288 bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
290 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
292 CCECBusDevice
*device
= GetDevice(command
.destination
);
293 if (device
&& (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
))
294 return ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(command
.initiator
);
300 bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command
&command
)
302 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
304 CCECBusDevice
*device
= GetDevice(command
.destination
);
306 return device
->TransmitPowerState(command
.initiator
);
312 bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command
&command
)
314 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
316 CCECBusDevice
*device
= GetDevice(command
.destination
);
318 return device
->TransmitVendorID(command
.initiator
);
324 bool CCECCommandHandler::HandleGiveOSDName(const cec_command
&command
)
326 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
328 CCECBusDevice
*device
= GetDevice(command
.destination
);
330 return device
->TransmitOSDName(command
.initiator
);
336 bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command
&command
)
338 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
340 CCECBusDevice
*device
= GetDevice(command
.destination
);
343 device
->SetActiveSource();
344 return device
->TransmitPhysicalAddress() &&
345 device
->TransmitActiveSource();
352 bool CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command
&command
)
354 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
356 CCECBusDevice
*device
= GetDevice(command
.destination
);
357 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
358 return ((CCECAudioSystem
*) device
)->TransmitSystemAudioModeStatus(command
.initiator
);
364 bool CCECCommandHandler::HandleImageViewOn(const cec_command
&command
)
366 m_processor
->m_busDevices
[command
.initiator
]->SetActiveSource();
370 bool CCECCommandHandler::HandleMenuRequest(const cec_command
&command
)
372 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
374 if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_QUERY
)
376 CCECBusDevice
*device
= GetDevice(command
.destination
);
378 return device
->TransmitMenuState(command
.initiator
);
385 bool CCECCommandHandler::HandleReportAudioStatus(const cec_command
&command
)
387 if (command
.parameters
.size
== 1)
389 CCECBusDevice
*device
= GetDevice(command
.initiator
);
390 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
392 ((CCECAudioSystem
*)device
)->SetAudioStatus(command
.parameters
[0]);
399 bool CCECCommandHandler::HandleReportPhysicalAddress(const cec_command
&command
)
401 if (command
.parameters
.size
== 3)
403 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
404 SetPhysicalAddress(command
.initiator
, iNewAddress
);
409 bool CCECCommandHandler::HandleReportPowerStatus(const cec_command
&command
)
411 if (command
.parameters
.size
== 1)
413 CCECBusDevice
*device
= GetDevice(command
.initiator
);
415 device
->SetPowerStatus((cec_power_status
) command
.parameters
[0]);
420 bool CCECCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
422 if (m_processor
->IsStarted())
425 strLog
.Format(">> %i requests active source", (uint8_t) command
.initiator
);
426 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
428 vector
<CCECBusDevice
*> devices
;
429 for (int iDevicePtr
= (int)GetMyDevices(devices
)-1; iDevicePtr
>=0; iDevicePtr
--)
430 devices
[iDevicePtr
]->TransmitActiveSource();
437 bool CCECCommandHandler::HandleRoutingChange(const cec_command
&command
)
439 if (command
.parameters
.size
== 4)
441 uint16_t iOldAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
442 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[2] << 8) | ((uint16_t)command
.parameters
[3]);
444 CCECBusDevice
*device
= GetDevice(command
.initiator
);
446 device
->SetStreamPath(iNewAddress
, iOldAddress
);
451 bool CCECCommandHandler::HandleRoutingInformation(const cec_command
&command
)
453 if (command
.parameters
.size
== 2)
455 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
456 m_processor
->SetActiveSource(iNewAddress
);
462 bool CCECCommandHandler::HandleSetMenuLanguage(const cec_command
&command
)
464 if (command
.parameters
.size
== 3)
466 CCECBusDevice
*device
= GetDevice(command
.initiator
);
469 cec_menu_language language
;
470 language
.device
= command
.initiator
;
471 for (uint8_t iPtr
= 0; iPtr
< 4; iPtr
++)
472 language
.language
[iPtr
] = command
.parameters
[iPtr
];
473 language
.language
[3] = 0;
474 device
->SetMenuLanguage(language
);
481 bool CCECCommandHandler::HandleSetOSDName(const cec_command
&command
)
483 if (command
.parameters
.size
> 0)
485 CCECBusDevice
*device
= GetDevice(command
.initiator
);
489 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
490 buf
[iPtr
] = (char)command
.parameters
[iPtr
];
491 buf
[command
.parameters
.size
] = 0;
493 CStdString
strName(buf
);
494 device
->SetOSDName(strName
);
502 bool CCECCommandHandler::HandleSetStreamPath(const cec_command
&command
)
504 if (m_processor
->IsStarted() && command
.parameters
.size
>= 2)
506 uint16_t iStreamAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
508 strLog
.Format(">> %i sets stream path to physical address %04x", command
.initiator
, iStreamAddress
);
509 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
511 /* one of the device handled by libCEC has been made active */
512 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamAddress
);
513 if (device
&& m_busDevice
->MyLogicalAddressContains(device
->GetLogicalAddress()))
515 device
->SetActiveSource();
516 device
->TransmitActiveSource();
522 bool CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command
&command
)
524 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
526 CCECBusDevice
*device
= GetDevice(command
.destination
);
527 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
529 if (command
.parameters
.size
>= 2)
531 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
532 ((CCECAudioSystem
*) device
)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON
);
533 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
534 CCECBusDevice
*newActiveDevice
= GetDeviceByPhysicalAddress(iNewAddress
);
536 newActiveDevice
->SetActiveSource();
537 return ((CCECAudioSystem
*) device
)->TransmitSetSystemAudioMode(command
.initiator
);
541 ((CCECAudioSystem
*) device
)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF
);
542 return ((CCECAudioSystem
*) device
)->TransmitSetSystemAudioMode(command
.initiator
);
549 bool CCECCommandHandler::HandleStandby(const cec_command
&command
)
551 CCECBusDevice
*device
= GetDevice(command
.initiator
);
553 device
->SetPowerStatus(CEC_POWER_STATUS_STANDBY
);
558 bool CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command
&command
)
560 if (command
.parameters
.size
== 1)
562 CCECBusDevice
*device
= GetDevice(command
.initiator
);
563 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
565 ((CCECAudioSystem
*)device
)->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
573 bool CCECCommandHandler::HandleSetSystemAudioMode(const cec_command
&command
)
575 if (command
.parameters
.size
== 1)
577 CCECBusDevice
*device
= GetDevice(command
.initiator
);
578 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
580 ((CCECAudioSystem
*)device
)->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
588 bool CCECCommandHandler::HandleTextViewOn(const cec_command
&command
)
590 m_processor
->m_busDevices
[command
.initiator
]->SetActiveSource();
594 bool CCECCommandHandler::HandleUserControlPressed(const cec_command
&command
)
596 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
) && command
.parameters
.size
> 0)
598 m_processor
->AddKey();
600 if (command
.parameters
[0] <= CEC_USER_CONTROL_CODE_MAX
)
603 strLog
.Format("key pressed: %x", command
.parameters
[0]);
604 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
606 if (command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER
||
607 command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION
)
609 CCECBusDevice
*device
= GetDevice(command
.destination
);
612 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
613 if (device
->MyLogicalAddressContains(device
->GetLogicalAddress()))
615 device
->SetActiveSource();
616 device
->TransmitActiveSource();
618 if (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
||
619 device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
620 ((CCECPlaybackDevice
*)device
)->TransmitDeckStatus(command
.initiator
);
626 m_processor
->SetCurrentButton((cec_user_control_code
) command
.parameters
[0]);
634 bool CCECCommandHandler::HandleUserControlRelease(const cec_command
&command
)
636 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
637 m_processor
->AddKey();
642 void CCECCommandHandler::UnhandledCommand(const cec_command
&command
)
645 strLog
.Format("unhandled command with opcode %02x from address %d", command
.opcode
, command
.initiator
);
646 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
);
649 unsigned int CCECCommandHandler::GetMyDevices(vector
<CCECBusDevice
*> &devices
) const
651 unsigned int iReturn(0);
653 cec_logical_addresses addresses
= m_processor
->GetLogicalAddresses();
654 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
658 devices
.push_back(GetDevice((cec_logical_address
) iPtr
));
666 CCECBusDevice
*CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress
) const
668 CCECBusDevice
*device
= NULL
;
670 if (iLogicalAddress
>= CECDEVICE_TV
&& iLogicalAddress
<= CECDEVICE_BROADCAST
)
671 device
= m_processor
->m_busDevices
[iLogicalAddress
];
676 CCECBusDevice
*CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
) const
678 return m_processor
->GetDeviceByPhysicalAddress(iPhysicalAddress
);
681 CCECBusDevice
*CCECCommandHandler::GetDeviceByType(cec_device_type type
) const
683 return m_processor
->GetDeviceByType(type
);
686 bool CCECCommandHandler::SetVendorId(const cec_command
&command
)
688 bool bChanged(false);
689 if (command
.parameters
.size
< 3)
691 m_busDevice
->AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
695 uint64_t iVendorId
= ((uint64_t)command
.parameters
[0] << 16) +
696 ((uint64_t)command
.parameters
[1] << 8) +
697 (uint64_t)command
.parameters
[2];
699 CCECBusDevice
*device
= GetDevice((cec_logical_address
) command
.initiator
);
701 bChanged
= device
->SetVendorId(iVendorId
);
705 void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress
, uint16_t iNewAddress
)
707 if (!m_busDevice
->MyLogicalAddressContains(iAddress
))
709 bool bOurAddress(m_processor
->GetPhysicalAddress() == iNewAddress
);
710 GetDevice(iAddress
)->SetPhysicalAddress(iNewAddress
);
713 /* another device reported the same physical address as ours
714 * since we don't have physical address detection yet, we'll just use the
715 * given address, increased by 0x100 for now */
716 m_processor
->SetPhysicalAddress(iNewAddress
+ 0x100);
721 void CCECCommandHandler::HandlePoll(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
724 strLog
.Format("<< POLL: %s (%x) -> %s (%x)", m_processor
->ToString(iInitiator
), iInitiator
, m_processor
->ToString(iDestination
), iDestination
);
725 m_processor
->AddLog(CEC_LOG_DEBUG
, strLog
);
728 bool CCECCommandHandler::HandleReceiveFailed(void)
730 /* default = error */
734 bool CCECCommandHandler::TransmitImageViewOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
737 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_IMAGE_VIEW_ON
);
739 return Transmit(command
, false);
742 bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
745 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_STANDBY
);
747 return Transmit(command
, false);
750 bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
753 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_CEC_VERSION
);
755 return Transmit(command
, true, CEC_OPCODE_CEC_VERSION
);
758 bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
761 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_MENU_LANGUAGE
);
763 return Transmit(command
, true, CEC_OPCODE_SET_MENU_LANGUAGE
);
766 bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
769 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_OSD_NAME
);
771 return Transmit(command
, true, CEC_OPCODE_SET_OSD_NAME
);
774 bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
777 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
);
779 return Transmit(command
, true, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
782 bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
785 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
);
787 return Transmit(command
, true, CEC_OPCODE_REPORT_POWER_STATUS
);
790 bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
793 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
795 return Transmit(command
, true, CEC_OPCODE_DEVICE_VENDOR_ID
);
798 bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
801 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
802 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
803 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
805 return Transmit(command
, false);
808 bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_version cecVersion
)
811 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_CEC_VERSION
);
812 command
.parameters
.PushBack((uint8_t)cecVersion
);
814 return Transmit(command
, false);
817 bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
820 cec_command::Format(command
, iInitiator
, CECDEVICE_TV
, CEC_OPCODE_INACTIVE_SOURCE
);
821 command
.parameters
.PushBack((iPhysicalAddress
>> 8) & 0xFF);
822 command
.parameters
.PushBack(iPhysicalAddress
& 0xFF);
824 return Transmit(command
, false);
827 bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_menu_state menuState
)
830 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_MENU_STATUS
);
831 command
.parameters
.PushBack((uint8_t)menuState
);
833 return Transmit(command
, false);
836 bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, CStdString strDeviceName
)
839 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_NAME
);
840 for (unsigned int iPtr
= 0; iPtr
< strDeviceName
.length(); iPtr
++)
841 command
.parameters
.PushBack(strDeviceName
.at(iPtr
));
843 return Transmit(command
, false);
846 bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_display_control duration
, const char *strMessage
)
849 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_STRING
);
850 command
.parameters
.PushBack((uint8_t)duration
);
852 unsigned int iLen
= strlen(strMessage
);
853 if (iLen
> 13) iLen
= 13;
855 for (unsigned int iPtr
= 0; iPtr
< iLen
; iPtr
++)
856 command
.parameters
.PushBack(strMessage
[iPtr
]);
858 return Transmit(command
, false);
861 bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
, cec_device_type type
)
864 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
865 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
866 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
867 command
.parameters
.PushBack((uint8_t) (type
));
869 return Transmit(command
, false);
872 bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
875 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_NONE
);
877 return Transmit(command
, false);
880 bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_power_status state
)
883 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_POWER_STATUS
);
884 command
.parameters
.PushBack((uint8_t) state
);
886 return Transmit(command
, false);
889 bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator
, uint64_t iVendorId
)
892 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_DEVICE_VENDOR_ID
);
894 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 16) & 0xFF));
895 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 8) & 0xFF));
896 command
.parameters
.PushBack((uint8_t) ((uint64_t)iVendorId
& 0xFF));
898 return Transmit(command
, false);
901 bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, uint8_t state
)
904 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_AUDIO_STATUS
);
905 command
.parameters
.PushBack(state
);
907 return Transmit(command
, false);
910 bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
)
913 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
);
914 command
.parameters
.PushBack((uint8_t)state
);
916 return Transmit(command
, false);
919 bool CCECCommandHandler::TransmitSystemAudioModeStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
)
922 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
);
923 command
.parameters
.PushBack((uint8_t)state
);
925 return Transmit(command
, false);
928 bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_deck_info state
)
931 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_DECK_STATUS
);
932 command
.PushBack((uint8_t)state
);
934 return Transmit(command
, false);
937 bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = true */)
940 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_PRESSED
);
941 command
.parameters
.PushBack((uint8_t)key
);
943 return Transmit(command
, bWait
);
946 bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWait
/* = true */)
949 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_RELEASE
);
951 return Transmit(command
, bWait
);
954 bool CCECCommandHandler::Transmit(cec_command
&command
, bool bExpectResponse
/* = true */, cec_opcode expectedResponse
/* = CEC_OPCODE_NONE */)
958 command
.transmit_timeout
= m_iTransmitTimeout
;
961 uint8_t iTries(0), iMaxTries(command
.opcode
== CEC_OPCODE_NONE
? 1 : m_iTransmitRetries
+ 1);
962 CLockObject
writeLock(&m_processor
->m_transmitMutex
);
963 CLockObject
receiveLock(&m_receiveMutex
);
965 while (!bReturn
&& ++iTries
<= iMaxTries
)
967 m_expectedResponse
= expectedResponse
;
968 if (m_processor
->Transmit(command
))
970 m_processor
->AddLog(CEC_LOG_DEBUG
, "command transmitted");
971 bReturn
= bExpectResponse
?
972 m_condition
.Wait(&m_receiveMutex
, m_iTransmitWait
) :
983 bool CCECCommandHandler::ActivateSource(void)
985 if (m_busDevice
->GetLogicalAddress() == CECDEVICE_TV
)
987 CCECBusDevice
*primary
= m_processor
->GetPrimaryDevice();
988 primary
->SetPowerStatus(CEC_POWER_STATUS_ON
);
989 primary
->SetMenuState(CEC_MENU_STATE_ACTIVATED
);
991 if ((m_busDevice
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
||
992 m_busDevice
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
) &&
993 SendDeckStatusUpdateOnActiveSource())
994 ((CCECPlaybackDevice
*)m_busDevice
)->TransmitDeckStatus(CECDEVICE_TV
);
996 if (m_processor
->GetPrimaryDevice()->GetPhysicalAddress(false) != 0xffff)
998 m_processor
->SetActiveSource();
999 primary
->TransmitMenuState(m_busDevice
->GetLogicalAddress());
1000 m_bHandlerInited
= true;
1006 void CCECCommandHandler::MarkBusy(void)
1008 CLockObject
receiveLock(&m_receiveMutex
);
1012 bool CCECCommandHandler::MarkReady(void)
1014 CLockObject
receiveLock(&m_receiveMutex
);
1015 return m_iUseCounter
> 0 ? (--m_iUseCounter
== 0) : true;
1018 bool CCECCommandHandler::InUse(void)
1020 CLockObject
receiveLock(&m_receiveMutex
);
1021 return m_iUseCounter
> 0;