fixed - reset CVLCommandHandler::m_bCapabilitiesSent when the TV goes to standby
[deb_libcec.git] / src / lib / implementations / VLCommandHandler.cpp
CommitLineData
bb5b4874
LOK
1/*
2 * This file is part of the libCEC(R) library.
3 *
b492c10e 4 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
bb5b4874
LOK
5 * libCEC(R) is an original work, containing original code.
6 *
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
8 *
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.
13 *
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.
18 *
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.
22 *
23 *
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
26 *
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/
31 */
32
2b44051c 33#include "env.h"
bb5b4874 34#include "VLCommandHandler.h"
2b44051c
LOK
35
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"
b78b4e33
LOK
42
43#define VL_POWER_CHANGE 0x20
44#define VL_POWERED_UP 0x00
45#define VL_POWERED_DOWN 0x01
9ee5e8fd 46#define VL_UNKNOWN1 0x06
bb5b4874 47
5ce83fff
LOK
48#define VL_REQUEST_POWER_STATUS_TIMEOUT 5000
49
bb5b4874 50using namespace CEC;
b78b4e33 51using namespace PLATFORM;
bb5b4874 52
004b8382
LOK
53#define LIB_CEC m_busDevice->GetProcessor()->GetLib()
54#define ToString(p) LIB_CEC->ToString(p)
55
aa4c0d34 56// wait this amount of ms before trying to switch sources after receiving the message from the TV that it's powered on
7b01619d 57#define SOURCE_SWITCH_DELAY_MS 3000
aa4c0d34 58
060a7b5e
LOK
59CVLCommandHandler::CVLCommandHandler(CCECBusDevice *busDevice,
60 int32_t iTransmitTimeout /* = CEC_DEFAULT_TRANSMIT_TIMEOUT */,
61 int32_t iTransmitWait /* = CEC_DEFAULT_TRANSMIT_WAIT */,
62 int8_t iTransmitRetries /* = CEC_DEFAULT_TRANSMIT_RETRIES */,
63 int64_t iActiveSourcePending /* = 0 */) :
64 CCECCommandHandler(busDevice, iTransmitTimeout, iTransmitWait, iTransmitRetries, iActiveSourcePending),
7b01619d 65 m_iPowerUpEventReceived(0),
cf176869 66 m_bCapabilitiesSent(false),
5ce83fff 67 m_iPowerStatusRequested(0)
bb5b4874 68{
cf4931be 69 m_vendorId = CEC_VENDOR_PANASONIC;
bb5b4874 70}
3e61b350
LOK
71
72bool CVLCommandHandler::InitHandler(void)
73{
74 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
42d28d15
LOK
75 if (primary && primary->GetLogicalAddress() != CECDEVICE_UNREGISTERED)
76 {
77 /* use the VL commandhandler for the primary device that is handled by libCEC */
78 if (m_busDevice->GetLogicalAddress() == CECDEVICE_TV)
79 {
80 if (primary && m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress())
81 {
82 primary->SetVendorId(CEC_VENDOR_PANASONIC);
83 primary->ReplaceHandler(false);
84 }
85 }
86
87 if (primary->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE)
88 return m_processor->GetPrimaryClient()->ChangeDeviceType(CEC_DEVICE_TYPE_RECORDING_DEVICE, CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
89 }
b78b4e33 90
3e61b350
LOK
91 return CCECCommandHandler::InitHandler();
92}
b78b4e33 93
9a54dc82 94int CVLCommandHandler::HandleDeviceVendorCommandWithId(const cec_command &command)
b78b4e33 95{
b57695e8
LOK
96 if (!m_processor->IsHandledByLibCEC(command.destination))
97 return CEC_ABORT_REASON_INVALID_OPERAND;
98
344cddb9
LOK
99 if (command.parameters[0] != 0x00 ||
100 command.parameters[1] != 0x80 ||
101 command.parameters[2] != 0x45)
102 return CEC_ABORT_REASON_INVALID_OPERAND;
103
7b01619d
LOK
104 // XXX this is also sent when the TV is powered off
105#if 0
b78b4e33 106 if (command.initiator == CECDEVICE_TV &&
9ee5e8fd
LOK
107 command.parameters.At(3) == VL_UNKNOWN1)
108 {
109 // set the power up event time
110 {
111 CLockObject lock(m_mutex);
112 if (m_iPowerUpEventReceived == 0)
113 m_iPowerUpEventReceived = GetTimeMs();
114 }
115 // mark the TV as powered on
116 m_processor->GetTV()->SetPowerStatus(CEC_POWER_STATUS_ON);
117 }
7b01619d
LOK
118 else
119#endif
120 if (command.initiator == CECDEVICE_TV &&
b78b4e33
LOK
121 command.destination == CECDEVICE_BROADCAST &&
122 command.parameters.At(3) == VL_POWER_CHANGE)
123 {
124 if (command.parameters.At(4) == VL_POWERED_UP)
125 {
aa4c0d34 126 // set the power up event time
b78b4e33
LOK
127 {
128 CLockObject lock(m_mutex);
aa4c0d34
LOK
129 if (m_iPowerUpEventReceived == 0)
130 m_iPowerUpEventReceived = GetTimeMs();
b78b4e33 131 }
aa4c0d34
LOK
132 // mark the TV as powered on
133 m_processor->GetTV()->SetPowerStatus(CEC_POWER_STATUS_ON);
7b01619d
LOK
134
135 // send capabilties
136 SendVendorCommandCapabilities(m_processor->GetLogicalAddress(), command.initiator);
5e9d49d8
LOK
137
138 // reactivate the source, so the tv switches channels
139 if (m_processor->IsActiveSource(m_processor->GetLogicalAddress()))
140 m_processor->GetDevice(m_processor->GetLogicalAddress())->TransmitActiveSource(false);
b78b4e33
LOK
141 }
142 else if (command.parameters.At(4) == VL_POWERED_DOWN)
aa4c0d34
LOK
143 {
144 // reset the power up event time
145 {
146 CLockObject lock(m_mutex);
147 m_iPowerUpEventReceived = 0;
148 }
149 // mark the TV as powered off
150 m_processor->GetTV()->SetPowerStatus(CEC_POWER_STATUS_STANDBY);
151 }
152 else
153 LIB_CEC->AddLog(CEC_LOG_DEBUG, "skipping unknown vendor command");
b78b4e33 154
9a54dc82 155 return COMMAND_HANDLED;
b78b4e33
LOK
156 }
157
158 return CCECCommandHandler::HandleDeviceVendorCommandWithId(command);
159}
160
aa4c0d34 161bool CVLCommandHandler::PowerUpEventReceived(void)
b78b4e33 162{
aa4c0d34 163 bool bPowerUpEventReceived(true);
b78b4e33 164
aa4c0d34 165 if (m_busDevice->GetLogicalAddress() != CECDEVICE_TV)
b78b4e33 166 {
c4a070be
LOK
167 CCECBusDevice* tv = m_processor->GetTV();
168 if (tv && tv->GetStatus() != CEC_DEVICE_STATUS_PRESENT)
169 return true;
170
aa4c0d34 171 // get the status from the TV
aa4c0d34
LOK
172 if (tv && tv->GetCurrentVendorId() == CEC_VENDOR_PANASONIC)
173 {
174 CVLCommandHandler *handler = static_cast<CVLCommandHandler *>(tv->GetHandler());
175 bPowerUpEventReceived = handler ? handler->PowerUpEventReceived() : false;
176 tv->MarkHandlerReady();
177 }
b78b4e33
LOK
178 }
179 else
180 {
aa4c0d34
LOK
181 // get the current status
182 {
183 CLockObject lock(m_mutex);
184 bPowerUpEventReceived = m_iPowerUpEventReceived > 0 &&
185 GetTimeMs() - m_iPowerUpEventReceived > SOURCE_SWITCH_DELAY_MS;
186 }
b47f66af 187
aa4c0d34
LOK
188 // if we didn't receive the event, check if the TV is already marked as powered on
189 if (!bPowerUpEventReceived && m_busDevice->GetCurrentPowerStatus() == CEC_POWER_STATUS_ON)
190 {
191 CLockObject lock(m_mutex);
192 m_iPowerUpEventReceived = GetTimeMs();
193 bPowerUpEventReceived = true;
194 }
b47f66af
LOK
195 }
196
aa4c0d34 197 return bPowerUpEventReceived;
b47f66af 198}
344cddb9 199
f8a669d0
LOK
200int CVLCommandHandler::HandleStandby(const cec_command &command)
201{
aa4c0d34 202 // reset the power up event time
f8a669d0
LOK
203 {
204 CLockObject lock(m_mutex);
aa4c0d34 205 m_iPowerUpEventReceived = 0;
d404793f 206 m_bCapabilitiesSent = false;
f8a669d0
LOK
207 }
208
209 return CCECCommandHandler::HandleStandby(command);
210}
211
7b01619d
LOK
212void CVLCommandHandler::VendorPreActivateSourceHook(void)
213{
214 bool bTransmit(false);
215 {
216 CLockObject lock(m_mutex);
277dfbd8 217 bTransmit = !m_bCapabilitiesSent;
7b01619d
LOK
218 }
219 if (bTransmit)
220 SendVendorCommandCapabilities(m_processor->GetLogicalAddress(), CECDEVICE_TV);
221}
222
223void CVLCommandHandler::SendVendorCommandCapabilities(const cec_logical_address initiator, const cec_logical_address destination)
224{
cca65ba4 225 if (PowerUpEventReceived())
7b01619d 226 {
cca65ba4
LOK
227 cec_command response;
228 cec_command::Format(response, initiator, destination, CEC_OPCODE_VENDOR_COMMAND);
229 uint8_t iResponseData[] = {0x10, 0x02, 0xFF, 0xFF, 0x00, 0x05, 0x05, 0x45, 0x55, 0x5c, 0x58, 0x32};
230 response.PushArray(12, iResponseData);
231
232 if (Transmit(response, false, true))
7b01619d
LOK
233 {
234 CLockObject lock(m_mutex);
235 m_bCapabilitiesSent = true;
236 }
237 }
238}
239
344cddb9
LOK
240int CVLCommandHandler::HandleVendorCommand(const cec_command &command)
241{
242 // some vendor command voodoo that will enable more buttons on the remote
243 if (command.parameters.size == 3 &&
244 command.parameters[0] == 0x10 &&
245 command.parameters[1] == 0x01 &&
b57695e8
LOK
246 command.parameters[2] == 0x05 &&
247 m_processor->IsHandledByLibCEC(command.destination))
344cddb9 248 {
7b01619d 249 SendVendorCommandCapabilities(m_processor->GetLogicalAddress(), command.initiator);
344cddb9
LOK
250 return COMMAND_HANDLED;
251 }
252
253 return CEC_ABORT_REASON_INVALID_OPERAND;
254}
255
cf176869
LOK
256bool CVLCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
257{
5ce83fff 258 m_iPowerStatusRequested = GetTimeMs();
cf176869
LOK
259 return CCECCommandHandler::TransmitRequestPowerStatus(iInitiator, iDestination, bWaitForResponse);
260}
261
aa4c0d34
LOK
262bool CVLCommandHandler::SourceSwitchAllowed(void)
263{
5ce83fff
LOK
264 int64_t now(GetTimeMs());
265 if (!PowerUpEventReceived() && now - m_iPowerStatusRequested > VL_REQUEST_POWER_STATUS_TIMEOUT)
cf176869
LOK
266 TransmitRequestPowerStatus(m_processor->GetPrimaryDevice()->GetLogicalAddress(), CECDEVICE_TV, false);
267
aa4c0d34
LOK
268 return PowerUpEventReceived();
269}
9ee5e8fd
LOK
270
271int CVLCommandHandler::HandleSystemAudioModeRequest(const cec_command &command)
272{
273 if (command.initiator == CECDEVICE_TV)
274 {
275 // set the power up event time
276 {
277 CLockObject lock(m_mutex);
278 if (m_iPowerUpEventReceived == 0)
279 m_iPowerUpEventReceived = GetTimeMs();
280 }
281 // mark the TV as powered on
282 m_processor->GetTV()->SetPowerStatus(CEC_POWER_STATUS_ON);
283 }
284
285 return CCECCommandHandler::HandleSystemAudioModeRequest(command);
286}
7b01619d
LOK
287
288int CVLCommandHandler::HandleReportPowerStatus(const cec_command &command)
289{
290 if (command.initiator == m_busDevice->GetLogicalAddress() &&
291 command.parameters.size == 1 &&
292 (cec_power_status)command.parameters[0] == CEC_POWER_STATUS_ON)
293 {
294 CLockObject lock(m_mutex);
295 if (m_iPowerUpEventReceived == 0)
296 m_iPowerUpEventReceived = GetTimeMs();
297 }
298
299 return CCECCommandHandler::HandleReportPowerStatus(command);
300}