make cec-client include cecloader.h from our project, not from system
[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
LOK
63 m_iPowerUpEventReceived(0),
64 m_bCapabilitiesSent(false)
bb5b4874 65{
cf4931be 66 m_vendorId = CEC_VENDOR_PANASONIC;
bb5b4874 67}
3e61b350
LOK
68
69bool CVLCommandHandler::InitHandler(void)
70{
71 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
42d28d15
LOK
72 if (primary && primary->GetLogicalAddress() != CECDEVICE_UNREGISTERED)
73 {
74 /* use the VL commandhandler for the primary device that is handled by libCEC */
75 if (m_busDevice->GetLogicalAddress() == CECDEVICE_TV)
76 {
77 if (primary && m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress())
78 {
79 primary->SetVendorId(CEC_VENDOR_PANASONIC);
80 primary->ReplaceHandler(false);
81 }
82 }
83
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);
86 }
b78b4e33 87
3e61b350
LOK
88 return CCECCommandHandler::InitHandler();
89}
b78b4e33 90
9a54dc82 91int CVLCommandHandler::HandleDeviceVendorCommandWithId(const cec_command &command)
b78b4e33 92{
344cddb9
LOK
93 if (command.parameters[0] != 0x00 ||
94 command.parameters[1] != 0x80 ||
95 command.parameters[2] != 0x45)
96 return CEC_ABORT_REASON_INVALID_OPERAND;
97
7b01619d
LOK
98 // XXX this is also sent when the TV is powered off
99#if 0
b78b4e33 100 if (command.initiator == CECDEVICE_TV &&
9ee5e8fd
LOK
101 command.parameters.At(3) == VL_UNKNOWN1)
102 {
103 // set the power up event time
104 {
105 CLockObject lock(m_mutex);
106 if (m_iPowerUpEventReceived == 0)
107 m_iPowerUpEventReceived = GetTimeMs();
108 }
109 // mark the TV as powered on
110 m_processor->GetTV()->SetPowerStatus(CEC_POWER_STATUS_ON);
111 }
7b01619d
LOK
112 else
113#endif
114 if (command.initiator == CECDEVICE_TV &&
b78b4e33
LOK
115 command.destination == CECDEVICE_BROADCAST &&
116 command.parameters.At(3) == VL_POWER_CHANGE)
117 {
118 if (command.parameters.At(4) == VL_POWERED_UP)
119 {
aa4c0d34 120 // set the power up event time
b78b4e33
LOK
121 {
122 CLockObject lock(m_mutex);
aa4c0d34
LOK
123 if (m_iPowerUpEventReceived == 0)
124 m_iPowerUpEventReceived = GetTimeMs();
b78b4e33 125 }
aa4c0d34
LOK
126 // mark the TV as powered on
127 m_processor->GetTV()->SetPowerStatus(CEC_POWER_STATUS_ON);
7b01619d
LOK
128
129 // send capabilties
130 SendVendorCommandCapabilities(m_processor->GetLogicalAddress(), command.initiator);
b78b4e33
LOK
131 }
132 else if (command.parameters.At(4) == VL_POWERED_DOWN)
aa4c0d34
LOK
133 {
134 // reset the power up event time
135 {
136 CLockObject lock(m_mutex);
137 m_iPowerUpEventReceived = 0;
138 }
139 // mark the TV as powered off
140 m_processor->GetTV()->SetPowerStatus(CEC_POWER_STATUS_STANDBY);
141 }
142 else
143 LIB_CEC->AddLog(CEC_LOG_DEBUG, "skipping unknown vendor command");
b78b4e33 144
9a54dc82 145 return COMMAND_HANDLED;
b78b4e33
LOK
146 }
147
148 return CCECCommandHandler::HandleDeviceVendorCommandWithId(command);
149}
150
aa4c0d34 151bool CVLCommandHandler::PowerUpEventReceived(void)
b78b4e33 152{
aa4c0d34 153 bool bPowerUpEventReceived(true);
b78b4e33 154
aa4c0d34 155 if (m_busDevice->GetLogicalAddress() != CECDEVICE_TV)
b78b4e33 156 {
aa4c0d34
LOK
157 // get the status from the TV
158 CCECBusDevice *tv = m_processor->GetTV();
159 if (tv && tv->GetCurrentVendorId() == CEC_VENDOR_PANASONIC)
160 {
161 CVLCommandHandler *handler = static_cast<CVLCommandHandler *>(tv->GetHandler());
162 bPowerUpEventReceived = handler ? handler->PowerUpEventReceived() : false;
163 tv->MarkHandlerReady();
164 }
b78b4e33
LOK
165 }
166 else
167 {
aa4c0d34
LOK
168 // get the current status
169 {
170 CLockObject lock(m_mutex);
171 bPowerUpEventReceived = m_iPowerUpEventReceived > 0 &&
172 GetTimeMs() - m_iPowerUpEventReceived > SOURCE_SWITCH_DELAY_MS;
173 }
b47f66af 174
aa4c0d34
LOK
175 // if we didn't receive the event, check if the TV is already marked as powered on
176 if (!bPowerUpEventReceived && m_busDevice->GetCurrentPowerStatus() == CEC_POWER_STATUS_ON)
177 {
178 CLockObject lock(m_mutex);
179 m_iPowerUpEventReceived = GetTimeMs();
180 bPowerUpEventReceived = true;
181 }
b47f66af
LOK
182 }
183
aa4c0d34 184 return bPowerUpEventReceived;
b47f66af 185}
344cddb9 186
f8a669d0
LOK
187int CVLCommandHandler::HandleStandby(const cec_command &command)
188{
aa4c0d34 189 // reset the power up event time
f8a669d0
LOK
190 {
191 CLockObject lock(m_mutex);
aa4c0d34 192 m_iPowerUpEventReceived = 0;
f8a669d0
LOK
193 }
194
195 return CCECCommandHandler::HandleStandby(command);
196}
197
7b01619d
LOK
198void CVLCommandHandler::VendorPreActivateSourceHook(void)
199{
200 bool bTransmit(false);
201 {
202 CLockObject lock(m_mutex);
203 bTransmit = m_bCapabilitiesSent;
204 }
205 if (bTransmit)
206 SendVendorCommandCapabilities(m_processor->GetLogicalAddress(), CECDEVICE_TV);
207}
208
209void CVLCommandHandler::SendVendorCommandCapabilities(const cec_logical_address initiator, const cec_logical_address destination)
210{
211 cec_command response;
212 cec_command::Format(response, initiator, destination, CEC_OPCODE_VENDOR_COMMAND);
213 uint8_t iResponseData[] = {0x10, 0x02, 0xFF, 0xFF, 0x00, 0x05, 0x05, 0x45, 0x55, 0x5c, 0x58, 0x32};
214 response.PushArray(12, iResponseData);
215
216 if (Transmit(response, false, true))
217 {
218 if (PowerUpEventReceived())
219 {
220 CLockObject lock(m_mutex);
221 m_bCapabilitiesSent = true;
222 }
223 }
224}
225
344cddb9
LOK
226int CVLCommandHandler::HandleVendorCommand(const cec_command &command)
227{
228 // some vendor command voodoo that will enable more buttons on the remote
229 if (command.parameters.size == 3 &&
230 command.parameters[0] == 0x10 &&
231 command.parameters[1] == 0x01 &&
232 command.parameters[2] == 0x05)
233 {
7b01619d 234 SendVendorCommandCapabilities(m_processor->GetLogicalAddress(), command.initiator);
344cddb9
LOK
235 return COMMAND_HANDLED;
236 }
237
238 return CEC_ABORT_REASON_INVALID_OPERAND;
239}
240
aa4c0d34
LOK
241bool CVLCommandHandler::SourceSwitchAllowed(void)
242{
243 return PowerUpEventReceived();
244}
9ee5e8fd
LOK
245
246int CVLCommandHandler::HandleSystemAudioModeRequest(const cec_command &command)
247{
248 if (command.initiator == CECDEVICE_TV)
249 {
250 // set the power up event time
251 {
252 CLockObject lock(m_mutex);
253 if (m_iPowerUpEventReceived == 0)
254 m_iPowerUpEventReceived = GetTimeMs();
255 }
256 // mark the TV as powered on
257 m_processor->GetTV()->SetPowerStatus(CEC_POWER_STATUS_ON);
258 }
259
260 return CCECCommandHandler::HandleSystemAudioModeRequest(command);
261}
7b01619d
LOK
262
263int CVLCommandHandler::HandleReportPowerStatus(const cec_command &command)
264{
265 if (command.initiator == m_busDevice->GetLogicalAddress() &&
266 command.parameters.size == 1 &&
267 (cec_power_status)command.parameters[0] == CEC_POWER_STATUS_ON)
268 {
269 CLockObject lock(m_mutex);
270 if (m_iPowerUpEventReceived == 0)
271 m_iPowerUpEventReceived = GetTimeMs();
272 }
273
274 return CCECCommandHandler::HandleReportPowerStatus(command);
275}