Commit | Line | Data |
---|---|---|
cbbe90dd JB |
1 | /* |
2 | * This file is part of the libCEC(R) library. | |
3 | * | |
4 | * libCEC(R) is Copyright (C) 2011-2013 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 | libcec_configuration config; | |
80 | m_processor->GetPrimaryClient()->GetCurrentConfiguration(config); | |
81 | if (config.iDoubleTapTimeout50Ms == 0) | |
82 | { | |
83 | config.iDoubleTapTimeout50Ms = config.clientVersion >= CEC_CLIENT_VERSION_2_2_0 ? CEC_DOUBLE_TAP_TIMEOUT_50_MS : CEC_DOUBLE_TAP_TIMEOUT_MS_OLD; | |
84 | m_processor->GetPrimaryClient()->SetConfiguration(config); | |
85 | } | |
86 | ||
87 | primary->SetVendorId(CEC_VENDOR_PANASONIC); | |
88 | primary->ReplaceHandler(false); | |
89 | } | |
90 | ||
91 | if (primary->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE) | |
92 | return m_processor->GetPrimaryClient()->ChangeDeviceType(CEC_DEVICE_TYPE_RECORDING_DEVICE, CEC_DEVICE_TYPE_PLAYBACK_DEVICE); | |
93 | } | |
94 | } | |
95 | ||
96 | return CCECCommandHandler::InitHandler(); | |
97 | } | |
98 | ||
99 | int CVLCommandHandler::HandleDeviceVendorCommandWithId(const cec_command &command) | |
100 | { | |
101 | if (!m_processor->IsHandledByLibCEC(command.destination)) | |
102 | return CEC_ABORT_REASON_INVALID_OPERAND; | |
103 | ||
104 | if (command.parameters[0] != 0x00 || | |
105 | command.parameters[1] != 0x80 || | |
106 | command.parameters[2] != 0x45) | |
107 | return CEC_ABORT_REASON_INVALID_OPERAND; | |
108 | ||
109 | if (command.initiator == CECDEVICE_TV && | |
110 | command.parameters.At(3) == VL_UNKNOWN1) | |
111 | { | |
112 | if (command.parameters.size >= 5 && command.parameters.At(4) == 0x05) | |
113 | { | |
114 | // set the power up event time | |
115 | { | |
116 | CLockObject lock(m_mutex); | |
117 | if (m_iPowerUpEventReceived == 0) | |
118 | m_iPowerUpEventReceived = GetTimeMs(); | |
119 | } | |
120 | // mark the TV as powered on | |
121 | m_processor->GetTV()->SetPowerStatus(CEC_POWER_STATUS_ON); | |
122 | ||
123 | CCECBusDevice* dev = m_processor->GetPrimaryDevice(); | |
124 | if (dev && dev->IsActiveSource()) | |
125 | dev->TransmitActiveSource(false); | |
126 | ||
127 | return COMMAND_HANDLED; | |
128 | } | |
129 | } | |
130 | else if (command.initiator == CECDEVICE_TV && | |
131 | command.destination == CECDEVICE_BROADCAST && | |
132 | command.parameters.At(3) == VL_POWER_CHANGE) | |
133 | { | |
134 | if (command.parameters.At(4) == VL_POWERED_UP) | |
135 | { | |
136 | // set the power up event time | |
137 | { | |
138 | CLockObject lock(m_mutex); | |
139 | if (m_iPowerUpEventReceived == 0) | |
140 | m_iPowerUpEventReceived = GetTimeMs(); | |
141 | } | |
142 | // mark the TV as powered on | |
143 | m_processor->GetTV()->SetPowerStatus(CEC_POWER_STATUS_ON); | |
144 | ||
145 | // send capabilties | |
146 | SendVendorCommandCapabilities(m_processor->GetLogicalAddress(), command.initiator); | |
147 | ||
148 | // reactivate the source, so the tv switches channels | |
149 | if (m_processor->IsActiveSource(m_processor->GetLogicalAddress())) | |
150 | m_processor->GetDevice(m_processor->GetLogicalAddress())->TransmitActiveSource(false); | |
151 | } | |
152 | else if (command.parameters.At(4) == VL_POWERED_DOWN) | |
153 | { | |
154 | // reset the power up event time | |
155 | { | |
156 | CLockObject lock(m_mutex); | |
157 | m_iPowerUpEventReceived = 0; | |
158 | } | |
159 | // mark the TV as powered off | |
160 | m_processor->GetTV()->SetPowerStatus(CEC_POWER_STATUS_STANDBY); | |
161 | } | |
162 | else | |
163 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "skipping unknown vendor command"); | |
164 | ||
165 | return COMMAND_HANDLED; | |
166 | } | |
167 | ||
168 | return CCECCommandHandler::HandleDeviceVendorCommandWithId(command); | |
169 | } | |
170 | ||
171 | bool CVLCommandHandler::PowerUpEventReceived(void) | |
172 | { | |
173 | bool bPowerUpEventReceived(true); | |
174 | ||
175 | if (m_busDevice->GetLogicalAddress() != CECDEVICE_TV) | |
176 | { | |
177 | CCECBusDevice* tv = m_processor->GetTV(); | |
178 | if (tv && tv->GetStatus() != CEC_DEVICE_STATUS_PRESENT) | |
179 | return true; | |
180 | ||
181 | // get the status from the TV | |
182 | if (tv && tv->GetCurrentVendorId() == CEC_VENDOR_PANASONIC) | |
183 | { | |
184 | CVLCommandHandler *handler = static_cast<CVLCommandHandler *>(tv->GetHandler()); | |
185 | bPowerUpEventReceived = handler ? handler->PowerUpEventReceived() : false; | |
186 | tv->MarkHandlerReady(); | |
187 | } | |
188 | } | |
189 | else | |
190 | { | |
191 | // get the current status | |
192 | { | |
193 | CLockObject lock(m_mutex); | |
194 | bPowerUpEventReceived = m_iPowerUpEventReceived > 0 && | |
195 | GetTimeMs() - m_iPowerUpEventReceived > SOURCE_SWITCH_DELAY_MS; | |
196 | } | |
197 | ||
198 | // if we didn't receive the event, check if the TV is already marked as powered on | |
199 | if (!bPowerUpEventReceived && m_busDevice->GetCurrentPowerStatus() == CEC_POWER_STATUS_ON) | |
200 | { | |
201 | CLockObject lock(m_mutex); | |
202 | m_iPowerUpEventReceived = GetTimeMs(); | |
203 | bPowerUpEventReceived = true; | |
204 | } | |
205 | } | |
206 | ||
207 | return bPowerUpEventReceived; | |
208 | } | |
209 | ||
210 | int CVLCommandHandler::HandleStandby(const cec_command &command) | |
211 | { | |
212 | // reset the power up event time | |
213 | { | |
214 | CLockObject lock(m_mutex); | |
215 | m_iPowerUpEventReceived = 0; | |
216 | m_bCapabilitiesSent = false; | |
217 | } | |
218 | ||
219 | return CCECCommandHandler::HandleStandby(command); | |
220 | } | |
221 | ||
222 | void CVLCommandHandler::VendorPreActivateSourceHook(void) | |
223 | { | |
224 | bool bTransmit(false); | |
225 | { | |
226 | CLockObject lock(m_mutex); | |
227 | bTransmit = !m_bCapabilitiesSent; | |
228 | } | |
229 | if (bTransmit) | |
230 | SendVendorCommandCapabilities(m_processor->GetLogicalAddress(), CECDEVICE_TV); | |
231 | } | |
232 | ||
233 | void CVLCommandHandler::SendVendorCommandCapabilities(const cec_logical_address initiator, const cec_logical_address destination) | |
234 | { | |
235 | if (PowerUpEventReceived()) | |
236 | { | |
237 | cec_command response; | |
238 | cec_command::Format(response, initiator, destination, CEC_OPCODE_VENDOR_COMMAND); | |
239 | uint8_t iResponseData[] = {0x10, 0x02, 0xFF, 0xFF, 0x00, 0x05, 0x05, 0x45, 0x55, 0x5c, 0x58, 0x32}; | |
240 | response.PushArray(12, iResponseData); | |
241 | ||
242 | if (Transmit(response, false, true)) | |
243 | { | |
244 | CLockObject lock(m_mutex); | |
245 | m_bCapabilitiesSent = true; | |
246 | } | |
247 | } | |
248 | } | |
249 | ||
250 | int CVLCommandHandler::HandleVendorCommand(const cec_command &command) | |
251 | { | |
252 | // some vendor command voodoo that will enable more buttons on the remote | |
253 | if (command.parameters.size == 3 && | |
254 | command.parameters[0] == 0x10 && | |
255 | command.parameters[1] == 0x01 && | |
256 | m_processor->IsHandledByLibCEC(command.destination)) | |
257 | { | |
258 | // XXX i've seen 0x05 and 0x03 as third param. these probably indicate different types of TVs/capabilities | |
259 | // when we feature abort this, then the TV will try the same thing with a vendor command with id | |
260 | SendVendorCommandCapabilities(m_processor->GetLogicalAddress(), command.initiator); | |
261 | ||
262 | CCECBusDevice* dev = m_processor->GetDevice(command.destination); | |
263 | if (dev && dev->IsActiveSource()) | |
264 | dev->ActivateSource(500); | |
265 | return COMMAND_HANDLED; | |
266 | } | |
267 | ||
268 | return CEC_ABORT_REASON_INVALID_OPERAND; | |
269 | } | |
270 | ||
271 | bool CVLCommandHandler::SourceSwitchAllowed(void) | |
272 | { | |
273 | if (!PowerUpEventReceived()) | |
274 | TransmitRequestPowerStatus(m_processor->GetPrimaryDevice()->GetLogicalAddress(), CECDEVICE_TV, false, false); | |
275 | ||
276 | return PowerUpEventReceived(); | |
277 | } | |
278 | ||
279 | int CVLCommandHandler::HandleSystemAudioModeRequest(const cec_command &command) | |
280 | { | |
281 | if (command.initiator == CECDEVICE_TV) | |
282 | { | |
283 | // set the power up event time | |
284 | { | |
285 | CLockObject lock(m_mutex); | |
286 | if (m_iPowerUpEventReceived == 0) | |
287 | m_iPowerUpEventReceived = GetTimeMs(); | |
288 | } | |
289 | // mark the TV as powered on | |
290 | m_processor->GetTV()->SetPowerStatus(CEC_POWER_STATUS_ON); | |
291 | } | |
292 | ||
293 | return CCECCommandHandler::HandleSystemAudioModeRequest(command); | |
294 | } | |
295 | ||
296 | int CVLCommandHandler::HandleReportPowerStatus(const cec_command &command) | |
297 | { | |
298 | if (command.initiator == m_busDevice->GetLogicalAddress() && | |
299 | command.parameters.size == 1 && | |
300 | (cec_power_status)command.parameters[0] == CEC_POWER_STATUS_ON) | |
301 | { | |
302 | CLockObject lock(m_mutex); | |
303 | if (m_iPowerUpEventReceived == 0) | |
304 | m_iPowerUpEventReceived = GetTimeMs(); | |
305 | } | |
306 | ||
307 | return CCECCommandHandler::HandleReportPowerStatus(command); | |
308 | } |