Merge branch 'master' into release
[deb_libcec.git] / src / lib / implementations / VLCommandHandler.cpp
1 /*
2 * This file is part of the libCEC(R) library.
3 *
4 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
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 "env.h"
34 #include "VLCommandHandler.h"
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"
42
43 #define VL_POWER_CHANGE 0x20
44 #define VL_POWERED_UP 0x00
45 #define VL_POWERED_DOWN 0x01
46 #define VL_UNKNOWN1 0x06
47
48 using namespace CEC;
49 using namespace PLATFORM;
50
51 #define LIB_CEC m_busDevice->GetProcessor()->GetLib()
52 #define ToString(p) LIB_CEC->ToString(p)
53
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 3000
56
57 CVLCommandHandler::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),
63 m_iPowerUpEventReceived(0),
64 m_bCapabilitiesSent(false)
65 {
66 m_vendorId = CEC_VENDOR_PANASONIC;
67 }
68
69 bool CVLCommandHandler::InitHandler(void)
70 {
71 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
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 }
87
88 return CCECCommandHandler::InitHandler();
89 }
90
91 int CVLCommandHandler::HandleDeviceVendorCommandWithId(const cec_command &command)
92 {
93 if (command.parameters[0] != 0x00 ||
94 command.parameters[1] != 0x80 ||
95 command.parameters[2] != 0x45)
96 return CEC_ABORT_REASON_INVALID_OPERAND;
97
98 // XXX this is also sent when the TV is powered off
99 #if 0
100 if (command.initiator == CECDEVICE_TV &&
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 }
112 else
113 #endif
114 if (command.initiator == CECDEVICE_TV &&
115 command.destination == CECDEVICE_BROADCAST &&
116 command.parameters.At(3) == VL_POWER_CHANGE)
117 {
118 if (command.parameters.At(4) == VL_POWERED_UP)
119 {
120 // set the power up event time
121 {
122 CLockObject lock(m_mutex);
123 if (m_iPowerUpEventReceived == 0)
124 m_iPowerUpEventReceived = GetTimeMs();
125 }
126 // mark the TV as powered on
127 m_processor->GetTV()->SetPowerStatus(CEC_POWER_STATUS_ON);
128
129 // send capabilties
130 SendVendorCommandCapabilities(m_processor->GetLogicalAddress(), command.initiator);
131
132 // reactivate the source, so the tv switches channels
133 if (m_processor->IsActiveSource(m_processor->GetLogicalAddress()))
134 m_processor->GetDevice(m_processor->GetLogicalAddress())->TransmitActiveSource(false);
135 }
136 else if (command.parameters.At(4) == VL_POWERED_DOWN)
137 {
138 // reset the power up event time
139 {
140 CLockObject lock(m_mutex);
141 m_iPowerUpEventReceived = 0;
142 }
143 // mark the TV as powered off
144 m_processor->GetTV()->SetPowerStatus(CEC_POWER_STATUS_STANDBY);
145 }
146 else
147 LIB_CEC->AddLog(CEC_LOG_DEBUG, "skipping unknown vendor command");
148
149 return COMMAND_HANDLED;
150 }
151
152 return CCECCommandHandler::HandleDeviceVendorCommandWithId(command);
153 }
154
155 bool CVLCommandHandler::PowerUpEventReceived(void)
156 {
157 bool bPowerUpEventReceived(true);
158
159 if (m_busDevice->GetLogicalAddress() != CECDEVICE_TV)
160 {
161 // get the status from the TV
162 CCECBusDevice *tv = m_processor->GetTV();
163 if (tv && tv->GetCurrentVendorId() == CEC_VENDOR_PANASONIC)
164 {
165 CVLCommandHandler *handler = static_cast<CVLCommandHandler *>(tv->GetHandler());
166 bPowerUpEventReceived = handler ? handler->PowerUpEventReceived() : false;
167 tv->MarkHandlerReady();
168 }
169 }
170 else
171 {
172 // get the current status
173 {
174 CLockObject lock(m_mutex);
175 bPowerUpEventReceived = m_iPowerUpEventReceived > 0 &&
176 GetTimeMs() - m_iPowerUpEventReceived > SOURCE_SWITCH_DELAY_MS;
177 }
178
179 // if we didn't receive the event, check if the TV is already marked as powered on
180 if (!bPowerUpEventReceived && m_busDevice->GetCurrentPowerStatus() == CEC_POWER_STATUS_ON)
181 {
182 CLockObject lock(m_mutex);
183 m_iPowerUpEventReceived = GetTimeMs();
184 bPowerUpEventReceived = true;
185 }
186 }
187
188 return bPowerUpEventReceived;
189 }
190
191 int CVLCommandHandler::HandleStandby(const cec_command &command)
192 {
193 // reset the power up event time
194 {
195 CLockObject lock(m_mutex);
196 m_iPowerUpEventReceived = 0;
197 }
198
199 return CCECCommandHandler::HandleStandby(command);
200 }
201
202 void CVLCommandHandler::VendorPreActivateSourceHook(void)
203 {
204 bool bTransmit(false);
205 {
206 CLockObject lock(m_mutex);
207 bTransmit = m_bCapabilitiesSent;
208 }
209 if (bTransmit)
210 SendVendorCommandCapabilities(m_processor->GetLogicalAddress(), CECDEVICE_TV);
211 }
212
213 void CVLCommandHandler::SendVendorCommandCapabilities(const cec_logical_address initiator, const cec_logical_address destination)
214 {
215 cec_command response;
216 cec_command::Format(response, initiator, destination, CEC_OPCODE_VENDOR_COMMAND);
217 uint8_t iResponseData[] = {0x10, 0x02, 0xFF, 0xFF, 0x00, 0x05, 0x05, 0x45, 0x55, 0x5c, 0x58, 0x32};
218 response.PushArray(12, iResponseData);
219
220 if (Transmit(response, false, true))
221 {
222 if (PowerUpEventReceived())
223 {
224 CLockObject lock(m_mutex);
225 m_bCapabilitiesSent = true;
226 }
227 }
228 }
229
230 int CVLCommandHandler::HandleVendorCommand(const cec_command &command)
231 {
232 // some vendor command voodoo that will enable more buttons on the remote
233 if (command.parameters.size == 3 &&
234 command.parameters[0] == 0x10 &&
235 command.parameters[1] == 0x01 &&
236 command.parameters[2] == 0x05)
237 {
238 SendVendorCommandCapabilities(m_processor->GetLogicalAddress(), command.initiator);
239 return COMMAND_HANDLED;
240 }
241
242 return CEC_ABORT_REASON_INVALID_OPERAND;
243 }
244
245 bool CVLCommandHandler::SourceSwitchAllowed(void)
246 {
247 return PowerUpEventReceived();
248 }
249
250 int CVLCommandHandler::HandleSystemAudioModeRequest(const cec_command &command)
251 {
252 if (command.initiator == CECDEVICE_TV)
253 {
254 // set the power up event time
255 {
256 CLockObject lock(m_mutex);
257 if (m_iPowerUpEventReceived == 0)
258 m_iPowerUpEventReceived = GetTimeMs();
259 }
260 // mark the TV as powered on
261 m_processor->GetTV()->SetPowerStatus(CEC_POWER_STATUS_ON);
262 }
263
264 return CCECCommandHandler::HandleSystemAudioModeRequest(command);
265 }
266
267 int CVLCommandHandler::HandleReportPowerStatus(const cec_command &command)
268 {
269 if (command.initiator == m_busDevice->GetLogicalAddress() &&
270 command.parameters.size == 1 &&
271 (cec_power_status)command.parameters[0] == CEC_POWER_STATUS_ON)
272 {
273 CLockObject lock(m_mutex);
274 if (m_iPowerUpEventReceived == 0)
275 m_iPowerUpEventReceived = GetTimeMs();
276 }
277
278 return CCECCommandHandler::HandleReportPowerStatus(command);
279 }