fixed - don't keep trying to send 'image view on' commands to the TV when no TV is...
[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
LOK
47
48using namespace CEC;
b78b4e33 49using namespace PLATFORM;
bb5b4874 50
004b8382
LOK
51#define LIB_CEC m_busDevice->GetProcessor()->GetLib()
52#define ToString(p) LIB_CEC->ToString(p)
53
aa4c0d34 54// wait this amount of ms before trying to switch sources after receiving the message from the TV that it's powered on
7b01619d 55#define SOURCE_SWITCH_DELAY_MS 3000
aa4c0d34 56
060a7b5e
LOK
57CVLCommandHandler::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),
7b01619d 63 m_iPowerUpEventReceived(0),
cf176869
LOK
64 m_bCapabilitiesSent(false),
65 m_bPowerStatusRequested(false)
bb5b4874 66{
cf4931be 67 m_vendorId = CEC_VENDOR_PANASONIC;
bb5b4874 68}
3e61b350
LOK
69
70bool CVLCommandHandler::InitHandler(void)
71{
72 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
42d28d15
LOK
73 if (primary && primary->GetLogicalAddress() != CECDEVICE_UNREGISTERED)
74 {
75 /* use the VL commandhandler for the primary device that is handled by libCEC */
76 if (m_busDevice->GetLogicalAddress() == CECDEVICE_TV)
77 {
78 if (primary && m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress())
79 {
80 primary->SetVendorId(CEC_VENDOR_PANASONIC);
81 primary->ReplaceHandler(false);
82 }
83 }
84
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);
87 }
b78b4e33 88
3e61b350
LOK
89 return CCECCommandHandler::InitHandler();
90}
b78b4e33 91
9a54dc82 92int CVLCommandHandler::HandleDeviceVendorCommandWithId(const cec_command &command)
b78b4e33 93{
b57695e8
LOK
94 if (!m_processor->IsHandledByLibCEC(command.destination))
95 return CEC_ABORT_REASON_INVALID_OPERAND;
96
344cddb9
LOK
97 if (command.parameters[0] != 0x00 ||
98 command.parameters[1] != 0x80 ||
99 command.parameters[2] != 0x45)
100 return CEC_ABORT_REASON_INVALID_OPERAND;
101
7b01619d
LOK
102 // XXX this is also sent when the TV is powered off
103#if 0
b78b4e33 104 if (command.initiator == CECDEVICE_TV &&
9ee5e8fd
LOK
105 command.parameters.At(3) == VL_UNKNOWN1)
106 {
107 // set the power up event time
108 {
109 CLockObject lock(m_mutex);
110 if (m_iPowerUpEventReceived == 0)
111 m_iPowerUpEventReceived = GetTimeMs();
112 }
113 // mark the TV as powered on
114 m_processor->GetTV()->SetPowerStatus(CEC_POWER_STATUS_ON);
115 }
7b01619d
LOK
116 else
117#endif
118 if (command.initiator == CECDEVICE_TV &&
b78b4e33
LOK
119 command.destination == CECDEVICE_BROADCAST &&
120 command.parameters.At(3) == VL_POWER_CHANGE)
121 {
122 if (command.parameters.At(4) == VL_POWERED_UP)
123 {
aa4c0d34 124 // set the power up event time
b78b4e33
LOK
125 {
126 CLockObject lock(m_mutex);
aa4c0d34
LOK
127 if (m_iPowerUpEventReceived == 0)
128 m_iPowerUpEventReceived = GetTimeMs();
b78b4e33 129 }
aa4c0d34
LOK
130 // mark the TV as powered on
131 m_processor->GetTV()->SetPowerStatus(CEC_POWER_STATUS_ON);
7b01619d
LOK
132
133 // send capabilties
134 SendVendorCommandCapabilities(m_processor->GetLogicalAddress(), command.initiator);
5e9d49d8
LOK
135
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);
b78b4e33
LOK
139 }
140 else if (command.parameters.At(4) == VL_POWERED_DOWN)
aa4c0d34
LOK
141 {
142 // reset the power up event time
143 {
144 CLockObject lock(m_mutex);
145 m_iPowerUpEventReceived = 0;
146 }
147 // mark the TV as powered off
148 m_processor->GetTV()->SetPowerStatus(CEC_POWER_STATUS_STANDBY);
149 }
150 else
151 LIB_CEC->AddLog(CEC_LOG_DEBUG, "skipping unknown vendor command");
b78b4e33 152
9a54dc82 153 return COMMAND_HANDLED;
b78b4e33
LOK
154 }
155
156 return CCECCommandHandler::HandleDeviceVendorCommandWithId(command);
157}
158
aa4c0d34 159bool CVLCommandHandler::PowerUpEventReceived(void)
b78b4e33 160{
aa4c0d34 161 bool bPowerUpEventReceived(true);
b78b4e33 162
aa4c0d34 163 if (m_busDevice->GetLogicalAddress() != CECDEVICE_TV)
b78b4e33 164 {
c4a070be
LOK
165 CCECBusDevice* tv = m_processor->GetTV();
166 if (tv && tv->GetStatus() != CEC_DEVICE_STATUS_PRESENT)
167 return true;
168
aa4c0d34 169 // get the status from the TV
aa4c0d34
LOK
170 if (tv && tv->GetCurrentVendorId() == CEC_VENDOR_PANASONIC)
171 {
172 CVLCommandHandler *handler = static_cast<CVLCommandHandler *>(tv->GetHandler());
173 bPowerUpEventReceived = handler ? handler->PowerUpEventReceived() : false;
174 tv->MarkHandlerReady();
175 }
b78b4e33
LOK
176 }
177 else
178 {
aa4c0d34
LOK
179 // get the current status
180 {
181 CLockObject lock(m_mutex);
182 bPowerUpEventReceived = m_iPowerUpEventReceived > 0 &&
183 GetTimeMs() - m_iPowerUpEventReceived > SOURCE_SWITCH_DELAY_MS;
184 }
b47f66af 185
aa4c0d34
LOK
186 // if we didn't receive the event, check if the TV is already marked as powered on
187 if (!bPowerUpEventReceived && m_busDevice->GetCurrentPowerStatus() == CEC_POWER_STATUS_ON)
188 {
189 CLockObject lock(m_mutex);
190 m_iPowerUpEventReceived = GetTimeMs();
191 bPowerUpEventReceived = true;
192 }
b47f66af
LOK
193 }
194
aa4c0d34 195 return bPowerUpEventReceived;
b47f66af 196}
344cddb9 197
f8a669d0
LOK
198int CVLCommandHandler::HandleStandby(const cec_command &command)
199{
aa4c0d34 200 // reset the power up event time
f8a669d0
LOK
201 {
202 CLockObject lock(m_mutex);
aa4c0d34 203 m_iPowerUpEventReceived = 0;
f8a669d0
LOK
204 }
205
206 return CCECCommandHandler::HandleStandby(command);
207}
208
7b01619d
LOK
209void CVLCommandHandler::VendorPreActivateSourceHook(void)
210{
211 bool bTransmit(false);
212 {
213 CLockObject lock(m_mutex);
214 bTransmit = m_bCapabilitiesSent;
215 }
216 if (bTransmit)
217 SendVendorCommandCapabilities(m_processor->GetLogicalAddress(), CECDEVICE_TV);
218}
219
220void CVLCommandHandler::SendVendorCommandCapabilities(const cec_logical_address initiator, const cec_logical_address destination)
221{
222 cec_command response;
223 cec_command::Format(response, initiator, destination, CEC_OPCODE_VENDOR_COMMAND);
224 uint8_t iResponseData[] = {0x10, 0x02, 0xFF, 0xFF, 0x00, 0x05, 0x05, 0x45, 0x55, 0x5c, 0x58, 0x32};
225 response.PushArray(12, iResponseData);
226
227 if (Transmit(response, false, true))
228 {
229 if (PowerUpEventReceived())
230 {
231 CLockObject lock(m_mutex);
232 m_bCapabilitiesSent = true;
233 }
234 }
235}
236
344cddb9
LOK
237int CVLCommandHandler::HandleVendorCommand(const cec_command &command)
238{
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 &&
b57695e8
LOK
243 command.parameters[2] == 0x05 &&
244 m_processor->IsHandledByLibCEC(command.destination))
344cddb9 245 {
7b01619d 246 SendVendorCommandCapabilities(m_processor->GetLogicalAddress(), command.initiator);
344cddb9
LOK
247 return COMMAND_HANDLED;
248 }
249
250 return CEC_ABORT_REASON_INVALID_OPERAND;
251}
252
cf176869
LOK
253bool CVLCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
254{
255 m_bPowerStatusRequested = true;
256 return CCECCommandHandler::TransmitRequestPowerStatus(iInitiator, iDestination, bWaitForResponse);
257}
258
aa4c0d34
LOK
259bool CVLCommandHandler::SourceSwitchAllowed(void)
260{
cf176869
LOK
261 if (!PowerUpEventReceived() && !m_bPowerStatusRequested)
262 TransmitRequestPowerStatus(m_processor->GetPrimaryDevice()->GetLogicalAddress(), CECDEVICE_TV, false);
263
aa4c0d34
LOK
264 return PowerUpEventReceived();
265}
9ee5e8fd
LOK
266
267int CVLCommandHandler::HandleSystemAudioModeRequest(const cec_command &command)
268{
269 if (command.initiator == CECDEVICE_TV)
270 {
271 // set the power up event time
272 {
273 CLockObject lock(m_mutex);
274 if (m_iPowerUpEventReceived == 0)
275 m_iPowerUpEventReceived = GetTimeMs();
276 }
277 // mark the TV as powered on
278 m_processor->GetTV()->SetPowerStatus(CEC_POWER_STATUS_ON);
279 }
280
281 return CCECCommandHandler::HandleSystemAudioModeRequest(command);
282}
7b01619d
LOK
283
284int CVLCommandHandler::HandleReportPowerStatus(const cec_command &command)
285{
286 if (command.initiator == m_busDevice->GetLogicalAddress() &&
287 command.parameters.size == 1 &&
288 (cec_power_status)command.parameters[0] == CEC_POWER_STATUS_ON)
289 {
290 CLockObject lock(m_mutex);
291 if (m_iPowerUpEventReceived == 0)
292 m_iPowerUpEventReceived = GetTimeMs();
293 }
294
295 return CCECCommandHandler::HandleReportPowerStatus(command);
296}