Commit | Line | Data |
---|---|---|
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 | ||
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 LOK |
63 | m_iPowerUpEventReceived(0), |
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 | { | |
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 | } | |
b78b4e33 | 87 | |
3e61b350 LOK |
88 | return CCECCommandHandler::InitHandler(); |
89 | } | |
b78b4e33 | 90 | |
9a54dc82 | 91 | int CVLCommandHandler::HandleDeviceVendorCommandWithId(const cec_command &command) |
b78b4e33 | 92 | { |
b57695e8 LOK |
93 | if (!m_processor->IsHandledByLibCEC(command.destination)) |
94 | return CEC_ABORT_REASON_INVALID_OPERAND; | |
95 | ||
344cddb9 LOK |
96 | if (command.parameters[0] != 0x00 || |
97 | command.parameters[1] != 0x80 || | |
98 | command.parameters[2] != 0x45) | |
99 | return CEC_ABORT_REASON_INVALID_OPERAND; | |
100 | ||
7b01619d LOK |
101 | // XXX this is also sent when the TV is powered off |
102 | #if 0 | |
b78b4e33 | 103 | if (command.initiator == CECDEVICE_TV && |
9ee5e8fd LOK |
104 | command.parameters.At(3) == VL_UNKNOWN1) |
105 | { | |
106 | // set the power up event time | |
107 | { | |
108 | CLockObject lock(m_mutex); | |
109 | if (m_iPowerUpEventReceived == 0) | |
110 | m_iPowerUpEventReceived = GetTimeMs(); | |
111 | } | |
112 | // mark the TV as powered on | |
113 | m_processor->GetTV()->SetPowerStatus(CEC_POWER_STATUS_ON); | |
114 | } | |
7b01619d LOK |
115 | else |
116 | #endif | |
117 | if (command.initiator == CECDEVICE_TV && | |
b78b4e33 LOK |
118 | command.destination == CECDEVICE_BROADCAST && |
119 | command.parameters.At(3) == VL_POWER_CHANGE) | |
120 | { | |
121 | if (command.parameters.At(4) == VL_POWERED_UP) | |
122 | { | |
aa4c0d34 | 123 | // set the power up event time |
b78b4e33 LOK |
124 | { |
125 | CLockObject lock(m_mutex); | |
aa4c0d34 LOK |
126 | if (m_iPowerUpEventReceived == 0) |
127 | m_iPowerUpEventReceived = GetTimeMs(); | |
b78b4e33 | 128 | } |
aa4c0d34 LOK |
129 | // mark the TV as powered on |
130 | m_processor->GetTV()->SetPowerStatus(CEC_POWER_STATUS_ON); | |
7b01619d LOK |
131 | |
132 | // send capabilties | |
133 | SendVendorCommandCapabilities(m_processor->GetLogicalAddress(), command.initiator); | |
5e9d49d8 LOK |
134 | |
135 | // reactivate the source, so the tv switches channels | |
136 | if (m_processor->IsActiveSource(m_processor->GetLogicalAddress())) | |
137 | m_processor->GetDevice(m_processor->GetLogicalAddress())->TransmitActiveSource(false); | |
b78b4e33 LOK |
138 | } |
139 | else if (command.parameters.At(4) == VL_POWERED_DOWN) | |
aa4c0d34 LOK |
140 | { |
141 | // reset the power up event time | |
142 | { | |
143 | CLockObject lock(m_mutex); | |
144 | m_iPowerUpEventReceived = 0; | |
145 | } | |
146 | // mark the TV as powered off | |
147 | m_processor->GetTV()->SetPowerStatus(CEC_POWER_STATUS_STANDBY); | |
148 | } | |
149 | else | |
150 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "skipping unknown vendor command"); | |
b78b4e33 | 151 | |
9a54dc82 | 152 | return COMMAND_HANDLED; |
b78b4e33 LOK |
153 | } |
154 | ||
155 | return CCECCommandHandler::HandleDeviceVendorCommandWithId(command); | |
156 | } | |
157 | ||
aa4c0d34 | 158 | bool CVLCommandHandler::PowerUpEventReceived(void) |
b78b4e33 | 159 | { |
aa4c0d34 | 160 | bool bPowerUpEventReceived(true); |
b78b4e33 | 161 | |
aa4c0d34 | 162 | if (m_busDevice->GetLogicalAddress() != CECDEVICE_TV) |
b78b4e33 | 163 | { |
aa4c0d34 LOK |
164 | // get the status from the TV |
165 | CCECBusDevice *tv = m_processor->GetTV(); | |
166 | if (tv && tv->GetCurrentVendorId() == CEC_VENDOR_PANASONIC) | |
167 | { | |
168 | CVLCommandHandler *handler = static_cast<CVLCommandHandler *>(tv->GetHandler()); | |
169 | bPowerUpEventReceived = handler ? handler->PowerUpEventReceived() : false; | |
170 | tv->MarkHandlerReady(); | |
171 | } | |
b78b4e33 LOK |
172 | } |
173 | else | |
174 | { | |
aa4c0d34 LOK |
175 | // get the current status |
176 | { | |
177 | CLockObject lock(m_mutex); | |
178 | bPowerUpEventReceived = m_iPowerUpEventReceived > 0 && | |
179 | GetTimeMs() - m_iPowerUpEventReceived > SOURCE_SWITCH_DELAY_MS; | |
180 | } | |
b47f66af | 181 | |
aa4c0d34 LOK |
182 | // if we didn't receive the event, check if the TV is already marked as powered on |
183 | if (!bPowerUpEventReceived && m_busDevice->GetCurrentPowerStatus() == CEC_POWER_STATUS_ON) | |
184 | { | |
185 | CLockObject lock(m_mutex); | |
186 | m_iPowerUpEventReceived = GetTimeMs(); | |
187 | bPowerUpEventReceived = true; | |
188 | } | |
b47f66af LOK |
189 | } |
190 | ||
aa4c0d34 | 191 | return bPowerUpEventReceived; |
b47f66af | 192 | } |
344cddb9 | 193 | |
f8a669d0 LOK |
194 | int CVLCommandHandler::HandleStandby(const cec_command &command) |
195 | { | |
aa4c0d34 | 196 | // reset the power up event time |
f8a669d0 LOK |
197 | { |
198 | CLockObject lock(m_mutex); | |
aa4c0d34 | 199 | m_iPowerUpEventReceived = 0; |
f8a669d0 LOK |
200 | } |
201 | ||
202 | return CCECCommandHandler::HandleStandby(command); | |
203 | } | |
204 | ||
7b01619d LOK |
205 | void CVLCommandHandler::VendorPreActivateSourceHook(void) |
206 | { | |
207 | bool bTransmit(false); | |
208 | { | |
209 | CLockObject lock(m_mutex); | |
210 | bTransmit = m_bCapabilitiesSent; | |
211 | } | |
212 | if (bTransmit) | |
213 | SendVendorCommandCapabilities(m_processor->GetLogicalAddress(), CECDEVICE_TV); | |
214 | } | |
215 | ||
216 | void CVLCommandHandler::SendVendorCommandCapabilities(const cec_logical_address initiator, const cec_logical_address destination) | |
217 | { | |
218 | cec_command response; | |
219 | cec_command::Format(response, initiator, destination, CEC_OPCODE_VENDOR_COMMAND); | |
220 | uint8_t iResponseData[] = {0x10, 0x02, 0xFF, 0xFF, 0x00, 0x05, 0x05, 0x45, 0x55, 0x5c, 0x58, 0x32}; | |
221 | response.PushArray(12, iResponseData); | |
222 | ||
223 | if (Transmit(response, false, true)) | |
224 | { | |
225 | if (PowerUpEventReceived()) | |
226 | { | |
227 | CLockObject lock(m_mutex); | |
228 | m_bCapabilitiesSent = true; | |
229 | } | |
230 | } | |
231 | } | |
232 | ||
344cddb9 LOK |
233 | int CVLCommandHandler::HandleVendorCommand(const cec_command &command) |
234 | { | |
235 | // some vendor command voodoo that will enable more buttons on the remote | |
236 | if (command.parameters.size == 3 && | |
237 | command.parameters[0] == 0x10 && | |
238 | command.parameters[1] == 0x01 && | |
b57695e8 LOK |
239 | command.parameters[2] == 0x05 && |
240 | m_processor->IsHandledByLibCEC(command.destination)) | |
344cddb9 | 241 | { |
7b01619d | 242 | SendVendorCommandCapabilities(m_processor->GetLogicalAddress(), command.initiator); |
344cddb9 LOK |
243 | return COMMAND_HANDLED; |
244 | } | |
245 | ||
246 | return CEC_ABORT_REASON_INVALID_OPERAND; | |
247 | } | |
248 | ||
aa4c0d34 LOK |
249 | bool CVLCommandHandler::SourceSwitchAllowed(void) |
250 | { | |
251 | return PowerUpEventReceived(); | |
252 | } | |
9ee5e8fd LOK |
253 | |
254 | int CVLCommandHandler::HandleSystemAudioModeRequest(const cec_command &command) | |
255 | { | |
256 | if (command.initiator == CECDEVICE_TV) | |
257 | { | |
258 | // set the power up event time | |
259 | { | |
260 | CLockObject lock(m_mutex); | |
261 | if (m_iPowerUpEventReceived == 0) | |
262 | m_iPowerUpEventReceived = GetTimeMs(); | |
263 | } | |
264 | // mark the TV as powered on | |
265 | m_processor->GetTV()->SetPowerStatus(CEC_POWER_STATUS_ON); | |
266 | } | |
267 | ||
268 | return CCECCommandHandler::HandleSystemAudioModeRequest(command); | |
269 | } | |
7b01619d LOK |
270 | |
271 | int CVLCommandHandler::HandleReportPowerStatus(const cec_command &command) | |
272 | { | |
273 | if (command.initiator == m_busDevice->GetLogicalAddress() && | |
274 | command.parameters.size == 1 && | |
275 | (cec_power_status)command.parameters[0] == CEC_POWER_STATUS_ON) | |
276 | { | |
277 | CLockObject lock(m_mutex); | |
278 | if (m_iPowerUpEventReceived == 0) | |
279 | m_iPowerUpEventReceived = GetTimeMs(); | |
280 | } | |
281 | ||
282 | return CCECCommandHandler::HandleReportPowerStatus(command); | |
283 | } |