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