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