cec: send power state 'standby->on' to the tv when initing the SL handler
[deb_libcec.git] / src / lib / implementations / SLCommandHandler.cpp
CommitLineData
e9de9629
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.
e9de9629
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
33#include "SLCommandHandler.h"
e54fd7d2 34#include "../devices/CECBusDevice.h"
8d915412 35#include "../devices/CECPlaybackDevice.h"
e54fd7d2 36#include "../CECProcessor.h"
2abe628c 37#include "../LibCEC.h"
e9de9629
LOK
38
39using namespace CEC;
40
0d4c3a7b
LOK
41#define SL_COMMAND_UNKNOWN_01 0x01
42#define SL_COMMAND_UNKNOWN_02 0x02
43#define SL_COMMAND_UNKNOWN_03 0x05
11d13a02 44
0d4c3a7b 45#define SL_COMMAND_REQUEST_POWER_STATUS 0xa0
9a0d7b9f 46#define SL_COMMAND_POWER_ON 0x03
0d4c3a7b 47#define SL_COMMAND_CONNECT_REQUEST 0x04
5abb18f3 48#define SL_COMMAND_SET_DEVICE_MODE 0x05
11d13a02 49
e9de9629 50CSLCommandHandler::CSLCommandHandler(CCECBusDevice *busDevice) :
855a3a98 51 CCECCommandHandler(busDevice),
b4c4ef7d 52 m_bSLEnabled(false),
b818fb5a
LOK
53 m_bPowerStateReset(false),
54 m_bActiveSourceSent(false)
e9de9629 55{
cf4931be 56 m_vendorId = CEC_VENDOR_LG;
79f01d26
LOK
57 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
58
59 /* imitate LG devices */
ef583662 60 if (primary && m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress())
3e61b350 61 primary->SetVendorId(CEC_VENDOR_LG);
79f01d26
LOK
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);
9f65e017
LOK
72}
73
468a1414
LOK
74bool CSLCommandHandler::InitHandler(void)
75{
76 if (m_bHandlerInited)
77 return true;
78 m_bHandlerInited = true;
79
468a1414
LOK
80 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
81 if (m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress())
871934d3
LOK
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 */
7e5a449a 88 primary->TransmitVendorID(CECDEVICE_BROADCAST, false);
871934d3 89 }
468a1414 90
468a1414
LOK
91 return true;
92}
93
3e61b350
LOK
94bool CSLCommandHandler::ActivateSource(void)
95{
3faa971c
LOK
96 if (!m_bSLEnabled)
97 return true;
98
b818fb5a 99 if (m_bActiveSourceSent)
3faa971c 100 return true;
b818fb5a
LOK
101 m_bActiveSourceSent = true;
102
3e61b350
LOK
103 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
104 primary->SetActiveSource();
105 primary->TransmitActiveSource();
106 return true;
107}
108
468a1414
LOK
109bool 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
a463f954
LOK
122bool CSLCommandHandler::HandleDeviceVendorId(const cec_command &command)
123{
124 SetVendorId(command);
125
2abe628c 126 if (!m_bSLEnabled)
468a1414 127 {
2abe628c
LOK
128 cec_command response;
129 cec_command::Format(response, m_processor->GetLogicalAddress(), command.initiator, CEC_OPCODE_FEATURE_ABORT);
130 return Transmit(response);
468a1414 131 }
2abe628c 132 return true;
468a1414
LOK
133}
134
135bool CSLCommandHandler::HandleGivePhysicalAddress(const cec_command &command)
136{
8e57f83c 137 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
468a1414
LOK
138 {
139 CCECBusDevice *device = GetDevice(command.destination);
140 if (device)
b818fb5a 141 return device->TransmitPhysicalAddress(); // only the physical address, don't send image view on
468a1414
LOK
142 }
143
144 return false;
e9de9629 145}
e54fd7d2
LOK
146
147bool CSLCommandHandler::HandleVendorCommand(const cec_command &command)
148{
149 if (command.parameters.size == 1 &&
11d13a02 150 command.parameters[0] == SL_COMMAND_UNKNOWN_01)
e54fd7d2 151 {
5f316715 152 HandleVendorCommand01(command);
8d915412 153 return true;
e54fd7d2 154 }
9a0d7b9f
LOK
155 else if (command.parameters.size == 2 &&
156 command.parameters[0] == SL_COMMAND_POWER_ON)
157 {
0ecbcd4d 158 HandleVendorCommandPowerOn(command);
9a0d7b9f
LOK
159 return true;
160 }
797dd7c4 161 else if (command.parameters.size == 2 &&
11d13a02 162 command.parameters[0] == SL_COMMAND_CONNECT_REQUEST)
9902f4e8 163 {
0ecbcd4d 164 HandleVendorCommandSLConnect(command);
8d915412 165 return true;
9902f4e8
LOK
166 }
167 else if (command.parameters.size == 1 &&
0d4c3a7b 168 command.parameters[0] == SL_COMMAND_REQUEST_POWER_STATUS)
9902f4e8 169 {
0ecbcd4d 170 HandleVendorCommandPowerOnStatus(command);
8d915412 171 return true;
9902f4e8 172 }
e54fd7d2
LOK
173
174 return false;
175}
176
5f316715 177void CSLCommandHandler::HandleVendorCommand01(const cec_command &command)
fe6f8e37 178{
b818fb5a 179 m_processor->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
fe6f8e37
LOK
180 TransmitVendorCommand0205(command.destination, command.initiator);
181}
182
183void CSLCommandHandler::TransmitVendorCommand0205(const cec_logical_address iSource, const cec_logical_address iDestination)
5f316715
LOK
184{
185 cec_command response;
fe6f8e37 186 cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
5f316715
LOK
187 response.PushBack(SL_COMMAND_UNKNOWN_02);
188 response.PushBack(SL_COMMAND_UNKNOWN_03);
189
19cbfa8f 190 Transmit(response, false);
fe6f8e37 191}
5f316715 192
0ecbcd4d 193void CSLCommandHandler::HandleVendorCommandPowerOn(const cec_command &command)
9a0d7b9f 194{
842262d8 195 CCECBusDevice *device = m_processor->GetPrimaryDevice();
9a0d7b9f
LOK
196 if (device)
197 {
fe6f8e37 198 m_bSLEnabled = true;
468a1414
LOK
199
200 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); //XXX
9a0d7b9f 201 device->TransmitPowerState(command.initiator);
468a1414 202 device->SetPowerStatus(CEC_POWER_STATUS_ON);
9a0d7b9f 203
468a1414
LOK
204 device->SetActiveSource();
205 TransmitImageViewOn(device->GetLogicalAddress(), command.initiator);
206 }
5f316715 207}
0ecbcd4d 208void CSLCommandHandler::HandleVendorCommandPowerOnStatus(const cec_command &command)
5f316715
LOK
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
468a1414 219void CSLCommandHandler::HandleVendorCommandSLConnect(const cec_command &command)
8d915412 220{
468a1414 221 m_bSLEnabled = true;
5abb18f3 222 TransmitVendorCommandSetDeviceMode(m_processor->GetLogicalAddress(), command.initiator, CEC_DEVICE_TYPE_RECORDING_DEVICE);
37acf382 223
2abe628c 224 ActivateSource();
8d915412 225}
0d4c3a7b 226
5abb18f3 227void CSLCommandHandler::TransmitVendorCommandSetDeviceMode(const cec_logical_address iSource, const cec_logical_address iDestination, const cec_device_type type)
0d4c3a7b 228{
468a1414
LOK
229 cec_command response;
230 cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
5abb18f3
LOK
231 response.PushBack(SL_COMMAND_SET_DEVICE_MODE);
232 response.PushBack((uint8_t)type);
468a1414 233 Transmit(response, false);
0d4c3a7b 234}
fe6f8e37 235
b818fb5a 236bool CSLCommandHandler::HandleGiveDeckStatus(const cec_command &command)
9f65e017 237{
b818fb5a
LOK
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 {
b818fb5a
LOK
245 if (command.parameters[0] == CEC_STATUS_REQUEST_ON)
246 {
2abe628c 247 ((CCECPlaybackDevice *) device)->SetDeckStatus(CEC_DECK_INFO_STOP);
b818fb5a
LOK
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 {
2abe628c 254 ((CCECPlaybackDevice *) device)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG);
b818fb5a
LOK
255 return ((CCECPlaybackDevice *) device)->TransmitDeckStatus(command.initiator);
256 }
257 }
258 }
259 return CCECCommandHandler::HandleGiveDeckStatus(command);
260 }
9f65e017 261
b818fb5a 262 return false;
9f65e017 263}
718b3632 264
718b3632
LOK
265bool CSLCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command)
266{
2abe628c 267 bool bReturn(false);
718b3632
LOK
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)
2abe628c
LOK
272 {
273 bReturn = device->TransmitPowerState(command.initiator);
274 device->SetPowerStatus(CEC_POWER_STATUS_ON);
275 }
2abe628c
LOK
276 else
277 {
3faa971c
LOK
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 }
b818fb5a 299 }
718b3632
LOK
300 }
301
2abe628c
LOK
302 return bReturn;
303}
304
305bool 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 }
718b3632
LOK
312 return false;
313}
5c3b1f92
LOK
314
315bool 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
326bool 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}