2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011-2012 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/
34 #include "VLCommandHandler.h"
36 #include "lib/devices/CECBusDevice.h"
37 #include "lib/devices/CECPlaybackDevice.h"
38 #include "lib/devices/CECTV.h"
39 #include "lib/CECProcessor.h"
40 #include "lib/LibCEC.h"
41 #include "lib/CECClient.h"
43 #define VL_POWER_CHANGE 0x20
44 #define VL_POWERED_UP 0x00
45 #define VL_POWERED_DOWN 0x01
46 #define VL_UNKNOWN1 0x06
49 using namespace PLATFORM
;
51 #define LIB_CEC m_busDevice->GetProcessor()->GetLib()
52 #define ToString(p) LIB_CEC->ToString(p)
54 // wait this amount of ms before trying to switch sources after receiving the message from the TV that it's powered on
55 #define SOURCE_SWITCH_DELAY_MS 3000
57 CVLCommandHandler::CVLCommandHandler(CCECBusDevice
*busDevice
,
58 int32_t iTransmitTimeout
/* = CEC_DEFAULT_TRANSMIT_TIMEOUT */,
59 int32_t iTransmitWait
/* = CEC_DEFAULT_TRANSMIT_WAIT */,
60 int8_t iTransmitRetries
/* = CEC_DEFAULT_TRANSMIT_RETRIES */,
61 int64_t iActiveSourcePending
/* = 0 */) :
62 CCECCommandHandler(busDevice
, iTransmitTimeout
, iTransmitWait
, iTransmitRetries
, iActiveSourcePending
),
63 m_iPowerUpEventReceived(0),
64 m_bCapabilitiesSent(false)
66 m_vendorId
= CEC_VENDOR_PANASONIC
;
69 bool CVLCommandHandler::InitHandler(void)
71 CCECBusDevice
*primary
= m_processor
->GetPrimaryDevice();
72 if (primary
&& primary
->GetLogicalAddress() != CECDEVICE_UNREGISTERED
)
74 /* use the VL commandhandler for the primary device that is handled by libCEC */
75 if (m_busDevice
->GetLogicalAddress() == CECDEVICE_TV
)
77 if (primary
&& m_busDevice
->GetLogicalAddress() != primary
->GetLogicalAddress())
79 primary
->SetVendorId(CEC_VENDOR_PANASONIC
);
80 primary
->ReplaceHandler(false);
84 if (primary
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
85 return m_processor
->GetPrimaryClient()->ChangeDeviceType(CEC_DEVICE_TYPE_RECORDING_DEVICE
, CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
88 return CCECCommandHandler::InitHandler();
91 int CVLCommandHandler::HandleDeviceVendorCommandWithId(const cec_command
&command
)
93 if (!m_processor
->IsHandledByLibCEC(command
.destination
))
94 return CEC_ABORT_REASON_INVALID_OPERAND
;
96 if (command
.parameters
[0] != 0x00 ||
97 command
.parameters
[1] != 0x80 ||
98 command
.parameters
[2] != 0x45)
99 return CEC_ABORT_REASON_INVALID_OPERAND
;
101 // XXX this is also sent when the TV is powered off
103 if (command
.initiator
== CECDEVICE_TV
&&
104 command
.parameters
.At(3) == VL_UNKNOWN1
)
106 // set the power up event time
108 CLockObject
lock(m_mutex
);
109 if (m_iPowerUpEventReceived
== 0)
110 m_iPowerUpEventReceived
= GetTimeMs();
112 // mark the TV as powered on
113 m_processor
->GetTV()->SetPowerStatus(CEC_POWER_STATUS_ON
);
117 if (command
.initiator
== CECDEVICE_TV
&&
118 command
.destination
== CECDEVICE_BROADCAST
&&
119 command
.parameters
.At(3) == VL_POWER_CHANGE
)
121 if (command
.parameters
.At(4) == VL_POWERED_UP
)
123 // set the power up event time
125 CLockObject
lock(m_mutex
);
126 if (m_iPowerUpEventReceived
== 0)
127 m_iPowerUpEventReceived
= GetTimeMs();
129 // mark the TV as powered on
130 m_processor
->GetTV()->SetPowerStatus(CEC_POWER_STATUS_ON
);
133 SendVendorCommandCapabilities(m_processor
->GetLogicalAddress(), command
.initiator
);
135 // reactivate the source, so the tv switches channels
136 if (m_processor
->IsActiveSource(m_processor
->GetLogicalAddress()))
137 m_processor
->GetDevice(m_processor
->GetLogicalAddress())->TransmitActiveSource(false);
139 else if (command
.parameters
.At(4) == VL_POWERED_DOWN
)
141 // reset the power up event time
143 CLockObject
lock(m_mutex
);
144 m_iPowerUpEventReceived
= 0;
146 // mark the TV as powered off
147 m_processor
->GetTV()->SetPowerStatus(CEC_POWER_STATUS_STANDBY
);
150 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "skipping unknown vendor command");
152 return COMMAND_HANDLED
;
155 return CCECCommandHandler::HandleDeviceVendorCommandWithId(command
);
158 bool CVLCommandHandler::PowerUpEventReceived(void)
160 bool bPowerUpEventReceived(true);
162 if (m_busDevice
->GetLogicalAddress() != CECDEVICE_TV
)
164 CCECBusDevice
* tv
= m_processor
->GetTV();
165 if (tv
&& tv
->GetStatus() != CEC_DEVICE_STATUS_PRESENT
)
168 // get the status from the TV
169 if (tv
&& tv
->GetCurrentVendorId() == CEC_VENDOR_PANASONIC
)
171 CVLCommandHandler
*handler
= static_cast<CVLCommandHandler
*>(tv
->GetHandler());
172 bPowerUpEventReceived
= handler
? handler
->PowerUpEventReceived() : false;
173 tv
->MarkHandlerReady();
178 // get the current status
180 CLockObject
lock(m_mutex
);
181 bPowerUpEventReceived
= m_iPowerUpEventReceived
> 0 &&
182 GetTimeMs() - m_iPowerUpEventReceived
> SOURCE_SWITCH_DELAY_MS
;
185 // if we didn't receive the event, check if the TV is already marked as powered on
186 if (!bPowerUpEventReceived
&& m_busDevice
->GetCurrentPowerStatus() == CEC_POWER_STATUS_ON
)
188 CLockObject
lock(m_mutex
);
189 m_iPowerUpEventReceived
= GetTimeMs();
190 bPowerUpEventReceived
= true;
194 return bPowerUpEventReceived
;
197 int CVLCommandHandler::HandleStandby(const cec_command
&command
)
199 // reset the power up event time
201 CLockObject
lock(m_mutex
);
202 m_iPowerUpEventReceived
= 0;
203 m_bCapabilitiesSent
= false;
206 return CCECCommandHandler::HandleStandby(command
);
209 void CVLCommandHandler::VendorPreActivateSourceHook(void)
211 bool bTransmit(false);
213 CLockObject
lock(m_mutex
);
214 bTransmit
= !m_bCapabilitiesSent
;
217 SendVendorCommandCapabilities(m_processor
->GetLogicalAddress(), CECDEVICE_TV
);
220 void CVLCommandHandler::SendVendorCommandCapabilities(const cec_logical_address initiator
, const cec_logical_address destination
)
222 if (PowerUpEventReceived())
224 cec_command response
;
225 cec_command::Format(response
, initiator
, destination
, CEC_OPCODE_VENDOR_COMMAND
);
226 uint8_t iResponseData
[] = {0x10, 0x02, 0xFF, 0xFF, 0x00, 0x05, 0x05, 0x45, 0x55, 0x5c, 0x58, 0x32};
227 response
.PushArray(12, iResponseData
);
229 if (Transmit(response
, false, true))
231 CLockObject
lock(m_mutex
);
232 m_bCapabilitiesSent
= true;
237 int CVLCommandHandler::HandleVendorCommand(const cec_command
&command
)
239 // some vendor command voodoo that will enable more buttons on the remote
240 if (command
.parameters
.size
== 3 &&
241 command
.parameters
[0] == 0x10 &&
242 command
.parameters
[1] == 0x01 &&
243 command
.parameters
[2] == 0x05 &&
244 m_processor
->IsHandledByLibCEC(command
.destination
))
246 SendVendorCommandCapabilities(m_processor
->GetLogicalAddress(), command
.initiator
);
247 return COMMAND_HANDLED
;
250 return CEC_ABORT_REASON_INVALID_OPERAND
;
253 bool CVLCommandHandler::SourceSwitchAllowed(void)
255 if (!PowerUpEventReceived())
256 TransmitRequestPowerStatus(m_processor
->GetPrimaryDevice()->GetLogicalAddress(), CECDEVICE_TV
, false);
258 return PowerUpEventReceived();
261 int CVLCommandHandler::HandleSystemAudioModeRequest(const cec_command
&command
)
263 if (command
.initiator
== CECDEVICE_TV
)
265 // set the power up event time
267 CLockObject
lock(m_mutex
);
268 if (m_iPowerUpEventReceived
== 0)
269 m_iPowerUpEventReceived
= GetTimeMs();
271 // mark the TV as powered on
272 m_processor
->GetTV()->SetPowerStatus(CEC_POWER_STATUS_ON
);
275 return CCECCommandHandler::HandleSystemAudioModeRequest(command
);
278 int CVLCommandHandler::HandleReportPowerStatus(const cec_command
&command
)
280 if (command
.initiator
== m_busDevice
->GetLogicalAddress() &&
281 command
.parameters
.size
== 1 &&
282 (cec_power_status
)command
.parameters
[0] == CEC_POWER_STATUS_ON
)
284 CLockObject
lock(m_mutex
);
285 if (m_iPowerUpEventReceived
== 0)
286 m_iPowerUpEventReceived
= GetTimeMs();
289 return CCECCommandHandler::HandleReportPowerStatus(command
);