cec: send power state 'standby->on' to the tv when initing the SL handler
[deb_libcec.git] / src / lib / implementations / SLCommandHandler.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 "SLCommandHandler.h"
34 #include "../devices/CECBusDevice.h"
35 #include "../devices/CECPlaybackDevice.h"
36 #include "../CECProcessor.h"
37 #include "../LibCEC.h"
38
39 using namespace CEC;
40
41 #define SL_COMMAND_UNKNOWN_01 0x01
42 #define SL_COMMAND_UNKNOWN_02 0x02
43 #define SL_COMMAND_UNKNOWN_03 0x05
44
45 #define SL_COMMAND_REQUEST_POWER_STATUS 0xa0
46 #define SL_COMMAND_POWER_ON 0x03
47 #define SL_COMMAND_CONNECT_REQUEST 0x04
48 #define SL_COMMAND_SET_DEVICE_MODE 0x05
49
50 CSLCommandHandler::CSLCommandHandler(CCECBusDevice *busDevice) :
51 CCECCommandHandler(busDevice),
52 m_bSLEnabled(false),
53 m_bPowerStateReset(false),
54 m_bActiveSourceSent(false)
55 {
56 m_vendorId = CEC_VENDOR_LG;
57 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
58
59 /* imitate LG devices */
60 if (primary && m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress())
61 primary->SetVendorId(CEC_VENDOR_LG);
62
63 /* LG TVs don't always reply to CEC version requests, so just set it to 1.3a */
64 if (m_busDevice->GetLogicalAddress() == CECDEVICE_TV)
65 m_busDevice->SetCecVersion(CEC_VERSION_1_3A);
66
67 /* LG devices always return "korean" as language */
68 cec_menu_language lang;
69 lang.device = m_busDevice->GetLogicalAddress();
70 snprintf(lang.language, 4, "eng");
71 m_busDevice->SetMenuLanguage(lang);
72 }
73
74 bool CSLCommandHandler::InitHandler(void)
75 {
76 if (m_bHandlerInited)
77 return true;
78 m_bHandlerInited = true;
79
80 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
81 if (m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress())
82 {
83 /* start as 'in transition standby->on' */
84 primary->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
85 primary->TransmitPowerState(CECDEVICE_TV);
86
87 /* reply with LGs vendor id */
88 primary->TransmitVendorID(CECDEVICE_BROADCAST, false);
89 }
90
91 return true;
92 }
93
94 bool CSLCommandHandler::ActivateSource(void)
95 {
96 if (!m_bSLEnabled)
97 return true;
98
99 if (m_bActiveSourceSent)
100 return true;
101 m_bActiveSourceSent = true;
102
103 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
104 primary->SetActiveSource();
105 primary->TransmitActiveSource();
106 return true;
107 }
108
109 bool CSLCommandHandler::HandleActiveSource(const cec_command &command)
110 {
111 if (command.parameters.size == 2)
112 {
113 uint16_t iAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
114 if (iAddress != m_busDevice->GetPhysicalAddress(false))
115 m_bSLEnabled = false;
116 return m_processor->SetActiveSource(iAddress);
117 }
118
119 return true;
120 }
121
122 bool CSLCommandHandler::HandleDeviceVendorId(const cec_command &command)
123 {
124 SetVendorId(command);
125
126 if (!m_bSLEnabled)
127 {
128 cec_command response;
129 cec_command::Format(response, m_processor->GetLogicalAddress(), command.initiator, CEC_OPCODE_FEATURE_ABORT);
130 return Transmit(response);
131 }
132 return true;
133 }
134
135 bool CSLCommandHandler::HandleGivePhysicalAddress(const cec_command &command)
136 {
137 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
138 {
139 CCECBusDevice *device = GetDevice(command.destination);
140 if (device)
141 return device->TransmitPhysicalAddress(); // only the physical address, don't send image view on
142 }
143
144 return false;
145 }
146
147 bool CSLCommandHandler::HandleVendorCommand(const cec_command &command)
148 {
149 if (command.parameters.size == 1 &&
150 command.parameters[0] == SL_COMMAND_UNKNOWN_01)
151 {
152 HandleVendorCommand01(command);
153 return true;
154 }
155 else if (command.parameters.size == 2 &&
156 command.parameters[0] == SL_COMMAND_POWER_ON)
157 {
158 HandleVendorCommandPowerOn(command);
159 return true;
160 }
161 else if (command.parameters.size == 2 &&
162 command.parameters[0] == SL_COMMAND_CONNECT_REQUEST)
163 {
164 HandleVendorCommandSLConnect(command);
165 return true;
166 }
167 else if (command.parameters.size == 1 &&
168 command.parameters[0] == SL_COMMAND_REQUEST_POWER_STATUS)
169 {
170 HandleVendorCommandPowerOnStatus(command);
171 return true;
172 }
173
174 return false;
175 }
176
177 void CSLCommandHandler::HandleVendorCommand01(const cec_command &command)
178 {
179 m_processor->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
180 TransmitVendorCommand0205(command.destination, command.initiator);
181 }
182
183 void CSLCommandHandler::TransmitVendorCommand0205(const cec_logical_address iSource, const cec_logical_address iDestination)
184 {
185 cec_command response;
186 cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
187 response.PushBack(SL_COMMAND_UNKNOWN_02);
188 response.PushBack(SL_COMMAND_UNKNOWN_03);
189
190 Transmit(response, false);
191 }
192
193 void CSLCommandHandler::HandleVendorCommandPowerOn(const cec_command &command)
194 {
195 CCECBusDevice *device = m_processor->GetPrimaryDevice();
196 if (device)
197 {
198 m_bSLEnabled = true;
199
200 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); //XXX
201 device->TransmitPowerState(command.initiator);
202 device->SetPowerStatus(CEC_POWER_STATUS_ON);
203
204 device->SetActiveSource();
205 TransmitImageViewOn(device->GetLogicalAddress(), command.initiator);
206 }
207 }
208 void CSLCommandHandler::HandleVendorCommandPowerOnStatus(const cec_command &command)
209 {
210 if (command.destination != CECDEVICE_BROADCAST)
211 {
212 CCECBusDevice *device = m_processor->m_busDevices[m_processor->GetLogicalAddresses().primary];
213 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
214 device->TransmitPowerState(command.initiator);
215 device->SetPowerStatus(CEC_POWER_STATUS_ON);
216 }
217 }
218
219 void CSLCommandHandler::HandleVendorCommandSLConnect(const cec_command &command)
220 {
221 m_bSLEnabled = true;
222 TransmitVendorCommandSetDeviceMode(m_processor->GetLogicalAddress(), command.initiator, CEC_DEVICE_TYPE_RECORDING_DEVICE);
223
224 ActivateSource();
225 }
226
227 void CSLCommandHandler::TransmitVendorCommandSetDeviceMode(const cec_logical_address iSource, const cec_logical_address iDestination, const cec_device_type type)
228 {
229 cec_command response;
230 cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
231 response.PushBack(SL_COMMAND_SET_DEVICE_MODE);
232 response.PushBack((uint8_t)type);
233 Transmit(response, false);
234 }
235
236 bool CSLCommandHandler::HandleGiveDeckStatus(const cec_command &command)
237 {
238 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
239 {
240 CCECBusDevice *device = GetDevice(command.destination);
241 if (device && (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE))
242 {
243 if (command.parameters.size > 0)
244 {
245 if (command.parameters[0] == CEC_STATUS_REQUEST_ON)
246 {
247 ((CCECPlaybackDevice *) device)->SetDeckStatus(CEC_DECK_INFO_STOP);
248 return ((CCECPlaybackDevice *) device)->TransmitDeckStatus(command.initiator) &&
249 device->TransmitImageViewOn() &&
250 device->TransmitPhysicalAddress();
251 }
252 else if (command.parameters[0] == CEC_STATUS_REQUEST_ONCE)
253 {
254 ((CCECPlaybackDevice *) device)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG);
255 return ((CCECPlaybackDevice *) device)->TransmitDeckStatus(command.initiator);
256 }
257 }
258 }
259 return CCECCommandHandler::HandleGiveDeckStatus(command);
260 }
261
262 return false;
263 }
264
265 bool CSLCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command)
266 {
267 bool bReturn(false);
268 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
269 {
270 CCECBusDevice *device = GetDevice(command.destination);
271 if (device && device->GetPowerStatus(false) != CEC_POWER_STATUS_ON)
272 {
273 bReturn = device->TransmitPowerState(command.initiator);
274 device->SetPowerStatus(CEC_POWER_STATUS_ON);
275 }
276 else
277 {
278 if (!m_bActiveSourceSent)
279 {
280 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
281 bReturn = device->TransmitPowerState(command.initiator);
282 ActivateSource();
283 }
284 else if (m_resetPowerState.IsSet() && m_resetPowerState.TimeLeft() > 0)
285 {
286 /* assume that we've bugged out */
287 CLibCEC::AddLog(CEC_LOG_NOTICE, "LG seems to have bugged out. resetting to 'in transition standby to on'");
288 m_bActiveSourceSent = false;
289 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
290 bReturn = device->TransmitPowerState(command.initiator);
291 device->SetPowerStatus(CEC_POWER_STATUS_ON);
292 m_resetPowerState.Init(5000);
293 }
294 else
295 {
296 bReturn = device->TransmitPowerState(command.initiator);
297 m_resetPowerState.Init(5000);
298 }
299 }
300 }
301
302 return bReturn;
303 }
304
305 bool CSLCommandHandler::HandleRequestActiveSource(const cec_command &command)
306 {
307 if (m_processor->IsRunning())
308 {
309 CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %i requests active source, ignored", (uint8_t) command.initiator);
310 return true;
311 }
312 return false;
313 }
314
315 bool CSLCommandHandler::HandleFeatureAbort(const cec_command &command)
316 {
317 if (command.parameters.size == 0 && m_processor->GetPrimaryDevice()->GetPowerStatus() == CEC_POWER_STATUS_ON && !m_bSLEnabled)
318 {
319 m_processor->GetPrimaryDevice()->TransmitPowerState(command.initiator);
320 m_processor->GetPrimaryDevice()->TransmitVendorID(CECDEVICE_BROADCAST, false);
321 }
322
323 return CCECCommandHandler::HandleFeatureAbort(command);
324 }
325
326 bool CSLCommandHandler::HandleStandby(const cec_command &command)
327 {
328 if (command.initiator == CECDEVICE_TV)
329 {
330 m_bSLEnabled = false;
331 m_bPowerStateReset = false;
332 m_bActiveSourceSent = false;
333 }
334
335 CCECBusDevice *device = GetDevice(command.initiator);
336 if (device)
337 device->SetPowerStatus(CEC_POWER_STATUS_STANDBY);
338
339 return true;
340 }