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),
65 m_bPowerStatusRequested(false)
67 m_vendorId
= CEC_VENDOR_PANASONIC
;
70 bool CVLCommandHandler::InitHandler(void)
72 CCECBusDevice
*primary
= m_processor
->GetPrimaryDevice();
73 if (primary
&& primary
->GetLogicalAddress() != CECDEVICE_UNREGISTERED
)
75 /* use the VL commandhandler for the primary device that is handled by libCEC */
76 if (m_busDevice
->GetLogicalAddress() == CECDEVICE_TV
)
78 if (primary
&& m_busDevice
->GetLogicalAddress() != primary
->GetLogicalAddress())
80 primary
->SetVendorId(CEC_VENDOR_PANASONIC
);
81 primary
->ReplaceHandler(false);
85 if (primary
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
86 return m_processor
->GetPrimaryClient()->ChangeDeviceType(CEC_DEVICE_TYPE_RECORDING_DEVICE
, CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
89 return CCECCommandHandler::InitHandler();
92 int CVLCommandHandler::HandleDeviceVendorCommandWithId(const cec_command
&command
)
94 if (!m_processor
->IsHandledByLibCEC(command
.destination
))
95 return CEC_ABORT_REASON_INVALID_OPERAND
;
97 if (command
.parameters
[0] != 0x00 ||
98 command
.parameters
[1] != 0x80 ||
99 command
.parameters
[2] != 0x45)
100 return CEC_ABORT_REASON_INVALID_OPERAND
;
102 // XXX this is also sent when the TV is powered off
104 if (command
.initiator
== CECDEVICE_TV
&&
105 command
.parameters
.At(3) == VL_UNKNOWN1
)
107 // set the power up event time
109 CLockObject
lock(m_mutex
);
110 if (m_iPowerUpEventReceived
== 0)
111 m_iPowerUpEventReceived
= GetTimeMs();
113 // mark the TV as powered on
114 m_processor
->GetTV()->SetPowerStatus(CEC_POWER_STATUS_ON
);
118 if (command
.initiator
== CECDEVICE_TV
&&
119 command
.destination
== CECDEVICE_BROADCAST
&&
120 command
.parameters
.At(3) == VL_POWER_CHANGE
)
122 if (command
.parameters
.At(4) == VL_POWERED_UP
)
124 // set the power up event time
126 CLockObject
lock(m_mutex
);
127 if (m_iPowerUpEventReceived
== 0)
128 m_iPowerUpEventReceived
= GetTimeMs();
130 // mark the TV as powered on
131 m_processor
->GetTV()->SetPowerStatus(CEC_POWER_STATUS_ON
);
134 SendVendorCommandCapabilities(m_processor
->GetLogicalAddress(), command
.initiator
);
136 // reactivate the source, so the tv switches channels
137 if (m_processor
->IsActiveSource(m_processor
->GetLogicalAddress()))
138 m_processor
->GetDevice(m_processor
->GetLogicalAddress())->TransmitActiveSource(false);
140 else if (command
.parameters
.At(4) == VL_POWERED_DOWN
)
142 // reset the power up event time
144 CLockObject
lock(m_mutex
);
145 m_iPowerUpEventReceived
= 0;
147 // mark the TV as powered off
148 m_processor
->GetTV()->SetPowerStatus(CEC_POWER_STATUS_STANDBY
);
151 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "skipping unknown vendor command");
153 return COMMAND_HANDLED
;
156 return CCECCommandHandler::HandleDeviceVendorCommandWithId(command
);
159 bool CVLCommandHandler::PowerUpEventReceived(void)
161 bool bPowerUpEventReceived(true);
163 if (m_busDevice
->GetLogicalAddress() != CECDEVICE_TV
)
165 // get the status from the TV
166 CCECBusDevice
*tv
= m_processor
->GetTV();
167 if (tv
&& tv
->GetCurrentVendorId() == CEC_VENDOR_PANASONIC
)
169 CVLCommandHandler
*handler
= static_cast<CVLCommandHandler
*>(tv
->GetHandler());
170 bPowerUpEventReceived
= handler
? handler
->PowerUpEventReceived() : false;
171 tv
->MarkHandlerReady();
176 // get the current status
178 CLockObject
lock(m_mutex
);
179 bPowerUpEventReceived
= m_iPowerUpEventReceived
> 0 &&
180 GetTimeMs() - m_iPowerUpEventReceived
> SOURCE_SWITCH_DELAY_MS
;
183 // if we didn't receive the event, check if the TV is already marked as powered on
184 if (!bPowerUpEventReceived
&& m_busDevice
->GetCurrentPowerStatus() == CEC_POWER_STATUS_ON
)
186 CLockObject
lock(m_mutex
);
187 m_iPowerUpEventReceived
= GetTimeMs();
188 bPowerUpEventReceived
= true;
192 return bPowerUpEventReceived
;
195 int CVLCommandHandler::HandleStandby(const cec_command
&command
)
197 // reset the power up event time
199 CLockObject
lock(m_mutex
);
200 m_iPowerUpEventReceived
= 0;
203 return CCECCommandHandler::HandleStandby(command
);
206 void CVLCommandHandler::VendorPreActivateSourceHook(void)
208 bool bTransmit(false);
210 CLockObject
lock(m_mutex
);
211 bTransmit
= m_bCapabilitiesSent
;
214 SendVendorCommandCapabilities(m_processor
->GetLogicalAddress(), CECDEVICE_TV
);
217 void CVLCommandHandler::SendVendorCommandCapabilities(const cec_logical_address initiator
, const cec_logical_address destination
)
219 cec_command response
;
220 cec_command::Format(response
, initiator
, destination
, CEC_OPCODE_VENDOR_COMMAND
);
221 uint8_t iResponseData
[] = {0x10, 0x02, 0xFF, 0xFF, 0x00, 0x05, 0x05, 0x45, 0x55, 0x5c, 0x58, 0x32};
222 response
.PushArray(12, iResponseData
);
224 if (Transmit(response
, false, true))
226 if (PowerUpEventReceived())
228 CLockObject
lock(m_mutex
);
229 m_bCapabilitiesSent
= true;
234 int CVLCommandHandler::HandleVendorCommand(const cec_command
&command
)
236 // some vendor command voodoo that will enable more buttons on the remote
237 if (command
.parameters
.size
== 3 &&
238 command
.parameters
[0] == 0x10 &&
239 command
.parameters
[1] == 0x01 &&
240 command
.parameters
[2] == 0x05 &&
241 m_processor
->IsHandledByLibCEC(command
.destination
))
243 SendVendorCommandCapabilities(m_processor
->GetLogicalAddress(), command
.initiator
);
244 return COMMAND_HANDLED
;
247 return CEC_ABORT_REASON_INVALID_OPERAND
;
250 bool CVLCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
252 m_bPowerStatusRequested
= true;
253 return CCECCommandHandler::TransmitRequestPowerStatus(iInitiator
, iDestination
, bWaitForResponse
);
256 bool CVLCommandHandler::SourceSwitchAllowed(void)
258 if (!PowerUpEventReceived() && !m_bPowerStatusRequested
)
259 TransmitRequestPowerStatus(m_processor
->GetPrimaryDevice()->GetLogicalAddress(), CECDEVICE_TV
, false);
261 return PowerUpEventReceived();
264 int CVLCommandHandler::HandleSystemAudioModeRequest(const cec_command
&command
)
266 if (command
.initiator
== CECDEVICE_TV
)
268 // set the power up event time
270 CLockObject
lock(m_mutex
);
271 if (m_iPowerUpEventReceived
== 0)
272 m_iPowerUpEventReceived
= GetTimeMs();
274 // mark the TV as powered on
275 m_processor
->GetTV()->SetPowerStatus(CEC_POWER_STATUS_ON
);
278 return CCECCommandHandler::HandleSystemAudioModeRequest(command
);
281 int CVLCommandHandler::HandleReportPowerStatus(const cec_command
&command
)
283 if (command
.initiator
== m_busDevice
->GetLogicalAddress() &&
284 command
.parameters
.size
== 1 &&
285 (cec_power_status
)command
.parameters
[0] == CEC_POWER_STATUS_ON
)
287 CLockObject
lock(m_mutex
);
288 if (m_iPowerUpEventReceived
== 0)
289 m_iPowerUpEventReceived
= GetTimeMs();
292 return CCECCommandHandler::HandleReportPowerStatus(command
);