cec: added guard so ReplaceHandler() doesn't accidently try to replace a handler...
[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
LOK
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 1000
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),
aa4c0d34 63 m_iPowerUpEventReceived(0)
bb5b4874 64{
cf4931be 65 m_vendorId = CEC_VENDOR_PANASONIC;
bb5b4874 66}
3e61b350
LOK
67
68bool CVLCommandHandler::InitHandler(void)
69{
70 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
42d28d15
LOK
71 if (primary && primary->GetLogicalAddress() != CECDEVICE_UNREGISTERED)
72 {
73 /* use the VL commandhandler for the primary device that is handled by libCEC */
74 if (m_busDevice->GetLogicalAddress() == CECDEVICE_TV)
75 {
76 if (primary && m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress())
77 {
78 primary->SetVendorId(CEC_VENDOR_PANASONIC);
79 primary->ReplaceHandler(false);
80 }
81 }
82
83 if (primary->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE)
84 return m_processor->GetPrimaryClient()->ChangeDeviceType(CEC_DEVICE_TYPE_RECORDING_DEVICE, CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
aa4c0d34
LOK
85
86 m_processor->GetTV()->RequestPowerStatus(primary->GetLogicalAddress(), false);
42d28d15 87 }
b78b4e33 88
3e61b350
LOK
89 return CCECCommandHandler::InitHandler();
90}
b78b4e33 91
9a54dc82 92int CVLCommandHandler::HandleDeviceVendorCommandWithId(const cec_command &command)
b78b4e33 93{
344cddb9
LOK
94 if (command.parameters[0] != 0x00 ||
95 command.parameters[1] != 0x80 ||
96 command.parameters[2] != 0x45)
97 return CEC_ABORT_REASON_INVALID_OPERAND;
98
b78b4e33 99 if (command.initiator == CECDEVICE_TV &&
9ee5e8fd
LOK
100 command.parameters.At(3) == VL_UNKNOWN1)
101 {
102 // set the power up event time
103 {
104 CLockObject lock(m_mutex);
105 if (m_iPowerUpEventReceived == 0)
106 m_iPowerUpEventReceived = GetTimeMs();
107 }
108 // mark the TV as powered on
109 m_processor->GetTV()->SetPowerStatus(CEC_POWER_STATUS_ON);
110 }
111 else if (command.initiator == CECDEVICE_TV &&
b78b4e33
LOK
112 command.destination == CECDEVICE_BROADCAST &&
113 command.parameters.At(3) == VL_POWER_CHANGE)
114 {
115 if (command.parameters.At(4) == VL_POWERED_UP)
116 {
aa4c0d34 117 // set the power up event time
b78b4e33
LOK
118 {
119 CLockObject lock(m_mutex);
aa4c0d34
LOK
120 if (m_iPowerUpEventReceived == 0)
121 m_iPowerUpEventReceived = GetTimeMs();
b78b4e33 122 }
aa4c0d34
LOK
123 // mark the TV as powered on
124 m_processor->GetTV()->SetPowerStatus(CEC_POWER_STATUS_ON);
b78b4e33
LOK
125 }
126 else if (command.parameters.At(4) == VL_POWERED_DOWN)
aa4c0d34
LOK
127 {
128 // reset the power up event time
129 {
130 CLockObject lock(m_mutex);
131 m_iPowerUpEventReceived = 0;
132 }
133 // mark the TV as powered off
134 m_processor->GetTV()->SetPowerStatus(CEC_POWER_STATUS_STANDBY);
135 }
136 else
137 LIB_CEC->AddLog(CEC_LOG_DEBUG, "skipping unknown vendor command");
b78b4e33 138
9a54dc82 139 return COMMAND_HANDLED;
b78b4e33
LOK
140 }
141
142 return CCECCommandHandler::HandleDeviceVendorCommandWithId(command);
143}
144
aa4c0d34 145bool CVLCommandHandler::PowerUpEventReceived(void)
b78b4e33 146{
aa4c0d34 147 bool bPowerUpEventReceived(true);
b78b4e33 148
aa4c0d34 149 if (m_busDevice->GetLogicalAddress() != CECDEVICE_TV)
b78b4e33 150 {
aa4c0d34
LOK
151 // get the status from the TV
152 CCECBusDevice *tv = m_processor->GetTV();
153 if (tv && tv->GetCurrentVendorId() == CEC_VENDOR_PANASONIC)
154 {
155 CVLCommandHandler *handler = static_cast<CVLCommandHandler *>(tv->GetHandler());
156 bPowerUpEventReceived = handler ? handler->PowerUpEventReceived() : false;
157 tv->MarkHandlerReady();
158 }
b78b4e33
LOK
159 }
160 else
161 {
aa4c0d34
LOK
162 // get the current status
163 {
164 CLockObject lock(m_mutex);
165 bPowerUpEventReceived = m_iPowerUpEventReceived > 0 &&
166 GetTimeMs() - m_iPowerUpEventReceived > SOURCE_SWITCH_DELAY_MS;
167 }
b47f66af 168
aa4c0d34
LOK
169 // if we didn't receive the event, check if the TV is already marked as powered on
170 if (!bPowerUpEventReceived && m_busDevice->GetCurrentPowerStatus() == CEC_POWER_STATUS_ON)
171 {
172 CLockObject lock(m_mutex);
173 m_iPowerUpEventReceived = GetTimeMs();
174 bPowerUpEventReceived = true;
175 }
b47f66af
LOK
176 }
177
aa4c0d34 178 return bPowerUpEventReceived;
b47f66af 179}
344cddb9 180
f8a669d0
LOK
181int CVLCommandHandler::HandleStandby(const cec_command &command)
182{
aa4c0d34 183 // reset the power up event time
f8a669d0
LOK
184 {
185 CLockObject lock(m_mutex);
aa4c0d34 186 m_iPowerUpEventReceived = 0;
f8a669d0
LOK
187 }
188
189 return CCECCommandHandler::HandleStandby(command);
190}
191
344cddb9
LOK
192int CVLCommandHandler::HandleVendorCommand(const cec_command &command)
193{
194 // some vendor command voodoo that will enable more buttons on the remote
195 if (command.parameters.size == 3 &&
196 command.parameters[0] == 0x10 &&
197 command.parameters[1] == 0x01 &&
198 command.parameters[2] == 0x05)
199 {
200 cec_command response;
201 cec_command::Format(response, command.destination, command.initiator, CEC_OPCODE_VENDOR_COMMAND);
202 uint8_t iResponseData[] = {0x10, 0x02, 0xFF, 0xFF, 0x00, 0x05, 0x05, 0x45, 0x55, 0x5c, 0x58, 0x32};
203 response.PushArray(12, iResponseData);
204
2b44051c 205 Transmit(response, false, true);
344cddb9
LOK
206
207 return COMMAND_HANDLED;
208 }
209
210 return CEC_ABORT_REASON_INVALID_OPERAND;
211}
212
aa4c0d34
LOK
213bool CVLCommandHandler::SourceSwitchAllowed(void)
214{
215 return PowerUpEventReceived();
216}
9ee5e8fd
LOK
217
218int CVLCommandHandler::HandleSystemAudioModeRequest(const cec_command &command)
219{
220 if (command.initiator == CECDEVICE_TV)
221 {
222 // set the power up event time
223 {
224 CLockObject lock(m_mutex);
225 if (m_iPowerUpEventReceived == 0)
226 m_iPowerUpEventReceived = GetTimeMs();
227 }
228 // mark the TV as powered on
229 m_processor->GetTV()->SetPowerStatus(CEC_POWER_STATUS_ON);
230 }
231
232 return CCECCommandHandler::HandleSystemAudioModeRequest(command);
233}