updated changelog
[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
33#include "VLCommandHandler.h"
34#include "../devices/CECBusDevice.h"
aa4c0d34
LOK
35#include "../devices/CECPlaybackDevice.h"
36#include "../devices/CECTV.h"
3e61b350 37#include "../CECProcessor.h"
b78b4e33 38#include "../LibCEC.h"
004b8382 39#include "../CECClient.h"
b78b4e33
LOK
40
41#define VL_POWER_CHANGE 0x20
42#define VL_POWERED_UP 0x00
43#define VL_POWERED_DOWN 0x01
bb5b4874
LOK
44
45using namespace CEC;
b78b4e33 46using namespace PLATFORM;
bb5b4874 47
004b8382
LOK
48#define LIB_CEC m_busDevice->GetProcessor()->GetLib()
49#define ToString(p) LIB_CEC->ToString(p)
50
aa4c0d34
LOK
51// wait this amount of ms before trying to switch sources after receiving the message from the TV that it's powered on
52#define SOURCE_SWITCH_DELAY_MS 1000
53
bb5b4874 54CVLCommandHandler::CVLCommandHandler(CCECBusDevice *busDevice) :
b78b4e33 55 CCECCommandHandler(busDevice),
aa4c0d34 56 m_iPowerUpEventReceived(0)
bb5b4874 57{
cf4931be 58 m_vendorId = CEC_VENDOR_PANASONIC;
bb5b4874 59}
3e61b350
LOK
60
61bool CVLCommandHandler::InitHandler(void)
62{
63 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
42d28d15
LOK
64 if (primary && primary->GetLogicalAddress() != CECDEVICE_UNREGISTERED)
65 {
66 /* use the VL commandhandler for the primary device that is handled by libCEC */
67 if (m_busDevice->GetLogicalAddress() == CECDEVICE_TV)
68 {
69 if (primary && m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress())
70 {
71 primary->SetVendorId(CEC_VENDOR_PANASONIC);
72 primary->ReplaceHandler(false);
73 }
74 }
75
76 if (primary->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE)
77 return m_processor->GetPrimaryClient()->ChangeDeviceType(CEC_DEVICE_TYPE_RECORDING_DEVICE, CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
aa4c0d34
LOK
78
79 m_processor->GetTV()->RequestPowerStatus(primary->GetLogicalAddress(), false);
42d28d15 80 }
b78b4e33 81
3e61b350
LOK
82 return CCECCommandHandler::InitHandler();
83}
b78b4e33 84
9a54dc82 85int CVLCommandHandler::HandleDeviceVendorCommandWithId(const cec_command &command)
b78b4e33 86{
344cddb9
LOK
87 if (command.parameters[0] != 0x00 ||
88 command.parameters[1] != 0x80 ||
89 command.parameters[2] != 0x45)
90 return CEC_ABORT_REASON_INVALID_OPERAND;
91
b78b4e33
LOK
92 if (command.initiator == CECDEVICE_TV &&
93 command.destination == CECDEVICE_BROADCAST &&
94 command.parameters.At(3) == VL_POWER_CHANGE)
95 {
96 if (command.parameters.At(4) == VL_POWERED_UP)
97 {
aa4c0d34 98 // set the power up event time
b78b4e33
LOK
99 {
100 CLockObject lock(m_mutex);
aa4c0d34
LOK
101 if (m_iPowerUpEventReceived == 0)
102 m_iPowerUpEventReceived = GetTimeMs();
b78b4e33 103 }
aa4c0d34
LOK
104 // mark the TV as powered on
105 m_processor->GetTV()->SetPowerStatus(CEC_POWER_STATUS_ON);
b78b4e33
LOK
106 }
107 else if (command.parameters.At(4) == VL_POWERED_DOWN)
aa4c0d34
LOK
108 {
109 // reset the power up event time
110 {
111 CLockObject lock(m_mutex);
112 m_iPowerUpEventReceived = 0;
113 }
114 // mark the TV as powered off
115 m_processor->GetTV()->SetPowerStatus(CEC_POWER_STATUS_STANDBY);
116 }
117 else
118 LIB_CEC->AddLog(CEC_LOG_DEBUG, "skipping unknown vendor command");
b78b4e33 119
9a54dc82 120 return COMMAND_HANDLED;
b78b4e33
LOK
121 }
122
123 return CCECCommandHandler::HandleDeviceVendorCommandWithId(command);
124}
125
aa4c0d34 126bool CVLCommandHandler::PowerUpEventReceived(void)
b78b4e33 127{
aa4c0d34 128 bool bPowerUpEventReceived(true);
b78b4e33 129
aa4c0d34 130 if (m_busDevice->GetLogicalAddress() != CECDEVICE_TV)
b78b4e33 131 {
aa4c0d34
LOK
132 // get the status from the TV
133 CCECBusDevice *tv = m_processor->GetTV();
134 if (tv && tv->GetCurrentVendorId() == CEC_VENDOR_PANASONIC)
135 {
136 CVLCommandHandler *handler = static_cast<CVLCommandHandler *>(tv->GetHandler());
137 bPowerUpEventReceived = handler ? handler->PowerUpEventReceived() : false;
138 tv->MarkHandlerReady();
139 }
b78b4e33
LOK
140 }
141 else
142 {
aa4c0d34
LOK
143 // get the current status
144 {
145 CLockObject lock(m_mutex);
146 bPowerUpEventReceived = m_iPowerUpEventReceived > 0 &&
147 GetTimeMs() - m_iPowerUpEventReceived > SOURCE_SWITCH_DELAY_MS;
148 }
b47f66af 149
aa4c0d34
LOK
150 // if we didn't receive the event, check if the TV is already marked as powered on
151 if (!bPowerUpEventReceived && m_busDevice->GetCurrentPowerStatus() == CEC_POWER_STATUS_ON)
152 {
153 CLockObject lock(m_mutex);
154 m_iPowerUpEventReceived = GetTimeMs();
155 bPowerUpEventReceived = true;
156 }
b47f66af
LOK
157 }
158
aa4c0d34 159 return bPowerUpEventReceived;
b47f66af 160}
344cddb9 161
f8a669d0
LOK
162int CVLCommandHandler::HandleStandby(const cec_command &command)
163{
aa4c0d34 164 // reset the power up event time
f8a669d0
LOK
165 {
166 CLockObject lock(m_mutex);
aa4c0d34 167 m_iPowerUpEventReceived = 0;
f8a669d0
LOK
168 }
169
170 return CCECCommandHandler::HandleStandby(command);
171}
172
344cddb9
LOK
173int CVLCommandHandler::HandleVendorCommand(const cec_command &command)
174{
175 // some vendor command voodoo that will enable more buttons on the remote
176 if (command.parameters.size == 3 &&
177 command.parameters[0] == 0x10 &&
178 command.parameters[1] == 0x01 &&
179 command.parameters[2] == 0x05)
180 {
181 cec_command response;
182 cec_command::Format(response, command.destination, command.initiator, CEC_OPCODE_VENDOR_COMMAND);
183 uint8_t iResponseData[] = {0x10, 0x02, 0xFF, 0xFF, 0x00, 0x05, 0x05, 0x45, 0x55, 0x5c, 0x58, 0x32};
184 response.PushArray(12, iResponseData);
185
186 Transmit(response, true);
187
188 return COMMAND_HANDLED;
189 }
190
191 return CEC_ABORT_REASON_INVALID_OPERAND;
192}
193
aa4c0d34
LOK
194bool CVLCommandHandler::SourceSwitchAllowed(void)
195{
196 return PowerUpEventReceived();
197}