LG: newer firmwares don't send a second power status request after it's been requeste...
[deb_libcec.git] / src / lib / implementations / SLCommandHandler.cpp
CommitLineData
e9de9629
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.
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
2b44051c 33#include "env.h"
e9de9629 34#include "SLCommandHandler.h"
2b44051c
LOK
35
36#include "lib/platform/util/timeutils.h"
37#include "lib/devices/CECBusDevice.h"
38#include "lib/devices/CECPlaybackDevice.h"
39#include "lib/CECProcessor.h"
40#include "lib/LibCEC.h"
e9de9629
LOK
41
42using namespace CEC;
cc1b9bc4 43using namespace PLATFORM;
e9de9629 44
0d4c3a7b
LOK
45#define SL_COMMAND_UNKNOWN_01 0x01
46#define SL_COMMAND_UNKNOWN_02 0x02
415e9bad
LOK
47
48#define SL_COMMAND_TYPE_HDDRECORDER_DISC 0x01
49#define SL_COMMAND_TYPE_VCR 0x02
50#define SL_COMMAND_TYPE_DVDPLAYER 0x03
51#define SL_COMMAND_TYPE_HDDRECORDER_DISC2 0x04
52#define SL_COMMAND_TYPE_HDDRECORDER 0x05
11d13a02 53
0d4c3a7b 54#define SL_COMMAND_REQUEST_POWER_STATUS 0xa0
9a0d7b9f 55#define SL_COMMAND_POWER_ON 0x03
0d4c3a7b 56#define SL_COMMAND_CONNECT_REQUEST 0x04
5abb18f3 57#define SL_COMMAND_SET_DEVICE_MODE 0x05
11d13a02 58
004b8382
LOK
59#define LIB_CEC m_busDevice->GetProcessor()->GetLib()
60#define ToString(p) LIB_CEC->ToString(p)
61
060a7b5e
LOK
62CSLCommandHandler::CSLCommandHandler(CCECBusDevice *busDevice,
63 int32_t iTransmitTimeout /* = CEC_DEFAULT_TRANSMIT_TIMEOUT */,
64 int32_t iTransmitWait /* = CEC_DEFAULT_TRANSMIT_WAIT */,
65 int8_t iTransmitRetries /* = CEC_DEFAULT_TRANSMIT_RETRIES */,
66 int64_t iActiveSourcePending /* = 0 */) :
67 CCECCommandHandler(busDevice, iTransmitTimeout, iTransmitWait, iTransmitRetries, iActiveSourcePending),
b4c4ef7d 68 m_bSLEnabled(false),
b8952342 69 m_bActiveSourceSent(false)
e9de9629 70{
cf4931be 71 m_vendorId = CEC_VENDOR_LG;
79f01d26 72
6b87ae0c
LOK
73 /* LG devices don't always reply to CEC version requests, so just set it to 1.3a */
74 m_busDevice->SetCecVersion(CEC_VERSION_1_3A);
79f01d26
LOK
75
76 /* LG devices always return "korean" as language */
77 cec_menu_language lang;
78 lang.device = m_busDevice->GetLogicalAddress();
79 snprintf(lang.language, 4, "eng");
80 m_busDevice->SetMenuLanguage(lang);
9f65e017
LOK
81}
82
468a1414
LOK
83bool CSLCommandHandler::InitHandler(void)
84{
85 if (m_bHandlerInited)
86 return true;
87 m_bHandlerInited = true;
88
0b835513
LOK
89 if (m_busDevice->GetLogicalAddress() != CECDEVICE_TV)
90 return true;
91
42d28d15
LOK
92 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
93 if (primary && primary->GetLogicalAddress() != CECDEVICE_UNREGISTERED)
871934d3 94 {
42d28d15
LOK
95 /* imitate LG devices */
96 if (m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress())
97 {
98 primary->SetVendorId(CEC_VENDOR_LG);
99 primary->ReplaceHandler(false);
100 }
101
102 if (m_busDevice->GetLogicalAddress() == CECDEVICE_TV)
103 {
104 /* start as 'in transition standby->on' */
105 primary->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
2b44051c 106 primary->TransmitPowerState(CECDEVICE_TV, false);
42d28d15
LOK
107
108 /* send the vendor id */
2b44051c 109 primary->TransmitVendorID(CECDEVICE_BROADCAST, false, false);
42d28d15 110 }
871934d3 111 }
468a1414 112
468a1414
LOK
113 return true;
114}
115
9a54dc82 116int CSLCommandHandler::HandleActiveSource(const cec_command &command)
468a1414
LOK
117{
118 if (command.parameters.size == 2)
119 {
120 uint16_t iAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
004b8382
LOK
121 CCECBusDevice *device = m_processor->GetDeviceByPhysicalAddress(iAddress);
122 if (device)
123 device->MarkAsActiveSource();
aab5cd7a 124
682b21de 125 {
aab5cd7a
LOK
126 CLockObject lock(m_SLMutex);
127 m_bActiveSourceSent = false;
682b21de 128 }
9a54dc82
LOK
129
130 return COMMAND_HANDLED;
468a1414
LOK
131 }
132
9a54dc82 133 return CEC_ABORT_REASON_INVALID_OPERAND;
bbc71623 134
468a1414
LOK
135}
136
9a54dc82 137int CSLCommandHandler::HandleDeviceVendorId(const cec_command &command)
a463f954
LOK
138{
139 SetVendorId(command);
140
632f5810 141 if (!SLInitialised() && command.initiator == CECDEVICE_TV)
468a1414 142 {
63047ed3
LOK
143 CCECBusDevice *destination = m_processor->GetDevice(command.destination);
144 if (destination && (destination->GetLogicalAddress() == CECDEVICE_BROADCAST || destination->IsHandledByLibCEC()))
145 {
146 cec_logical_address initiator = destination->GetLogicalAddress();
147 if (initiator == CECDEVICE_BROADCAST)
148 initiator = m_processor->GetPrimaryDevice()->GetLogicalAddress();
149
150 cec_command response;
151 cec_command::Format(response, initiator, command.initiator, CEC_OPCODE_FEATURE_ABORT);
2b44051c 152 Transmit(response, false, true);
9a54dc82 153 return COMMAND_HANDLED;
63047ed3 154 }
468a1414 155 }
9a54dc82
LOK
156
157 return CCECCommandHandler::HandleDeviceVendorId(command);
468a1414
LOK
158}
159
9a54dc82 160int CSLCommandHandler::HandleVendorCommand(const cec_command &command)
e54fd7d2 161{
004b8382 162 if (!m_processor->IsHandledByLibCEC(command.destination))
415e9bad
LOK
163 return true;
164
e54fd7d2 165 if (command.parameters.size == 1 &&
11d13a02 166 command.parameters[0] == SL_COMMAND_UNKNOWN_01)
e54fd7d2 167 {
5f316715 168 HandleVendorCommand01(command);
9a54dc82 169 return COMMAND_HANDLED;
e54fd7d2 170 }
9a0d7b9f
LOK
171 else if (command.parameters.size == 2 &&
172 command.parameters[0] == SL_COMMAND_POWER_ON)
173 {
0ecbcd4d 174 HandleVendorCommandPowerOn(command);
9a54dc82 175 return COMMAND_HANDLED;
9a0d7b9f 176 }
797dd7c4 177 else if (command.parameters.size == 2 &&
11d13a02 178 command.parameters[0] == SL_COMMAND_CONNECT_REQUEST)
9902f4e8 179 {
0ecbcd4d 180 HandleVendorCommandSLConnect(command);
9a54dc82 181 return COMMAND_HANDLED;
9902f4e8
LOK
182 }
183 else if (command.parameters.size == 1 &&
0d4c3a7b 184 command.parameters[0] == SL_COMMAND_REQUEST_POWER_STATUS)
9902f4e8 185 {
0ecbcd4d 186 HandleVendorCommandPowerOnStatus(command);
9a54dc82 187 return COMMAND_HANDLED;
9902f4e8 188 }
e54fd7d2 189
9a54dc82 190 return CCECCommandHandler::HandleVendorCommand(command);
e54fd7d2
LOK
191}
192
5f316715 193void CSLCommandHandler::HandleVendorCommand01(const cec_command &command)
fe6f8e37 194{
b818fb5a 195 m_processor->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
fe6f8e37 196 TransmitVendorCommand0205(command.destination, command.initiator);
34232c92
LOK
197
198 CCECBusDevice* dev = m_processor->GetDevice(command.destination);
199 if (dev && dev->IsHandledByLibCEC() && dev->IsActiveSource())
200 dev->TransmitActiveSource(false);
fe6f8e37
LOK
201}
202
203void CSLCommandHandler::TransmitVendorCommand0205(const cec_logical_address iSource, const cec_logical_address iDestination)
5f316715
LOK
204{
205 cec_command response;
fe6f8e37 206 cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
5f316715 207 response.PushBack(SL_COMMAND_UNKNOWN_02);
415e9bad 208 response.PushBack(SL_COMMAND_TYPE_HDDRECORDER);
5f316715 209
2b44051c 210 Transmit(response, false, true);
34232c92 211 SetSLInitialised();
fe6f8e37 212}
5f316715 213
0ecbcd4d 214void CSLCommandHandler::HandleVendorCommandPowerOn(const cec_command &command)
9a0d7b9f 215{
632f5810
LOK
216 if (command.initiator != CECDEVICE_TV)
217 return;
218
842262d8 219 CCECBusDevice *device = m_processor->GetPrimaryDevice();
9a0d7b9f
LOK
220 if (device)
221 {
cc1b9bc4 222 SetSLInitialised();
004b8382 223 device->MarkAsActiveSource();
60383b11 224 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
2b44051c 225 device->TransmitPowerState(command.initiator, true);
60383b11
LOK
226
227 CEvent::Sleep(2000);
228 device->SetPowerStatus(CEC_POWER_STATUS_ON);
2b44051c
LOK
229 device->TransmitPowerState(command.initiator, false);
230 device->TransmitPhysicalAddress(false);
60383b11
LOK
231 {
232 CLockObject lock(m_SLMutex);
233 m_bActiveSourceSent = false;
234 }
e8fca5f8
LOK
235 if (device->IsActiveSource())
236 ActivateSource();
468a1414 237 }
5f316715 238}
0ecbcd4d 239void CSLCommandHandler::HandleVendorCommandPowerOnStatus(const cec_command &command)
5f316715
LOK
240{
241 if (command.destination != CECDEVICE_BROADCAST)
242 {
004b8382
LOK
243 CCECBusDevice *device = m_processor->GetPrimaryDevice();
244 if (device)
245 {
246 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
2b44051c 247 device->TransmitPowerState(command.initiator, true);
004b8382
LOK
248 device->SetPowerStatus(CEC_POWER_STATUS_ON);
249 }
5f316715
LOK
250 }
251}
252
468a1414 253void CSLCommandHandler::HandleVendorCommandSLConnect(const cec_command &command)
8d915412 254{
cc1b9bc4 255 SetSLInitialised();
004b8382 256 TransmitVendorCommandSetDeviceMode(command.destination, command.initiator, CEC_DEVICE_TYPE_RECORDING_DEVICE);
37acf382 257
2abe628c 258 ActivateSource();
8d915412 259}
0d4c3a7b 260
5abb18f3 261void CSLCommandHandler::TransmitVendorCommandSetDeviceMode(const cec_logical_address iSource, const cec_logical_address iDestination, const cec_device_type type)
0d4c3a7b 262{
468a1414
LOK
263 cec_command response;
264 cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
5abb18f3
LOK
265 response.PushBack(SL_COMMAND_SET_DEVICE_MODE);
266 response.PushBack((uint8_t)type);
2b44051c 267 Transmit(response, false, true);
0d4c3a7b 268}
fe6f8e37 269
9a54dc82 270int CSLCommandHandler::HandleGiveDeckStatus(const cec_command &command)
9f65e017 271{
9a54dc82
LOK
272 if (!m_processor->CECInitialised() ||
273 !m_processor->IsHandledByLibCEC(command.destination))
274 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
275
276 CCECPlaybackDevice *device = CCECBusDevice::AsPlaybackDevice(GetDevice(command.destination));
277 if (!device || command.parameters.size == 0)
278 return CEC_ABORT_REASON_INVALID_OPERAND;
279
aab5cd7a 280 device->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG);
9a54dc82 281 if (command.parameters[0] == CEC_STATUS_REQUEST_ON)
b818fb5a 282 {
2b44051c 283 device->TransmitDeckStatus(command.initiator, true);
9a54dc82
LOK
284 if (!ActiveSourceSent())
285 ActivateSource();
286 return COMMAND_HANDLED;
287 }
288 else if (command.parameters[0] == CEC_STATUS_REQUEST_ONCE)
289 {
2b44051c 290 device->TransmitDeckStatus(command.initiator, true);
9a54dc82 291 return COMMAND_HANDLED;
b818fb5a 292 }
9f65e017 293
9a54dc82 294 return CCECCommandHandler::HandleGiveDeckStatus(command);
9f65e017 295}
718b3632 296
9a54dc82 297int CSLCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command)
718b3632 298{
0b8c7eab 299 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination) && command.initiator == CECDEVICE_TV)
718b3632
LOK
300 {
301 CCECBusDevice *device = GetDevice(command.destination);
004b8382 302 if (device && device->GetCurrentPowerStatus() != CEC_POWER_STATUS_ON)
2abe628c 303 {
2b44051c 304 device->TransmitPowerState(command.initiator, true);
2abe628c
LOK
305 device->SetPowerStatus(CEC_POWER_STATUS_ON);
306 }
2abe628c
LOK
307 else
308 {
cc1b9bc4 309 if (!ActiveSourceSent())
3faa971c
LOK
310 {
311 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
2b44051c 312 device->TransmitPowerState(command.initiator, true);
3faa971c
LOK
313 ActivateSource();
314 }
315 else if (m_resetPowerState.IsSet() && m_resetPowerState.TimeLeft() > 0)
316 {
415e9bad 317 /* TODO assume that we've bugged out. the return button no longer works after this */
004b8382 318 LIB_CEC->AddLog(CEC_LOG_WARNING, "FIXME: LG seems to have bugged out. resetting to 'in transition standby to on'. the return button will not work");
cc1b9bc4
LOK
319 {
320 CLockObject lock(m_SLMutex);
321 m_bActiveSourceSent = false;
322 }
3faa971c 323 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
2b44051c 324 device->TransmitPowerState(command.initiator, true);
3faa971c
LOK
325 device->SetPowerStatus(CEC_POWER_STATUS_ON);
326 m_resetPowerState.Init(5000);
327 }
328 else
329 {
2b44051c 330 device->TransmitPowerState(command.initiator, true);
3faa971c
LOK
331 m_resetPowerState.Init(5000);
332 }
b818fb5a 333 }
9a54dc82
LOK
334
335 return COMMAND_HANDLED;
718b3632
LOK
336 }
337
9a54dc82 338 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
2abe628c
LOK
339}
340
9a54dc82 341int CSLCommandHandler::HandleRequestActiveSource(const cec_command &command)
2abe628c 342{
0b8c7eab 343 if (m_processor->CECInitialised())
2abe628c 344 {
1fee1b3d 345 if (ActiveSourceSent())
004b8382 346 LIB_CEC->AddLog(CEC_LOG_DEBUG, ">> %i requests active source, ignored", (uint8_t) command.initiator);
1fee1b3d
LOK
347 else
348 ActivateSource();
9a54dc82 349 return COMMAND_HANDLED;
2abe628c 350 }
9a54dc82 351 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
718b3632 352}
5c3b1f92 353
9a54dc82 354int CSLCommandHandler::HandleFeatureAbort(const cec_command &command)
5c3b1f92 355{
004b8382 356 if (command.parameters.size == 0 && m_processor->GetPrimaryDevice()->GetCurrentPowerStatus() == CEC_POWER_STATUS_ON && !SLInitialised() &&
632f5810 357 command.initiator == CECDEVICE_TV)
5c3b1f92 358 {
2b44051c
LOK
359 m_processor->GetPrimaryDevice()->TransmitPowerState(command.initiator, false);
360 m_processor->GetPrimaryDevice()->TransmitVendorID(CECDEVICE_BROADCAST, false, false);
5c3b1f92
LOK
361 }
362
363 return CCECCommandHandler::HandleFeatureAbort(command);
364}
365
9a54dc82 366int CSLCommandHandler::HandleStandby(const cec_command &command)
5c3b1f92
LOK
367{
368 if (command.initiator == CECDEVICE_TV)
1fee1b3d
LOK
369 {
370 CLockObject lock(m_SLMutex);
371 m_bActiveSourceSent = false;
372 }
5c3b1f92 373
cc1b9bc4
LOK
374 return CCECCommandHandler::HandleStandby(command);
375}
5c3b1f92 376
cc1b9bc4
LOK
377void CSLCommandHandler::ResetSLState(void)
378{
004b8382 379 LIB_CEC->AddLog(CEC_LOG_NOTICE, "resetting SL initialised state");
cc1b9bc4
LOK
380 CLockObject lock(m_SLMutex);
381 m_bSLEnabled = false;
382 m_bActiveSourceSent = false;
383 m_processor->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
384}
385
386void CSLCommandHandler::SetSLInitialised(void)
387{
004b8382 388 LIB_CEC->AddLog(CEC_LOG_NOTICE, "SL initialised");
cc1b9bc4
LOK
389 CLockObject lock(m_SLMutex);
390 m_bSLEnabled = true;
391}
392
393bool CSLCommandHandler::SLInitialised(void)
394{
395 CLockObject lock(m_SLMutex);
396 return m_bSLEnabled;
397}
398
399bool CSLCommandHandler::ActiveSourceSent(void)
400{
401 CLockObject lock(m_SLMutex);
402 return m_bActiveSourceSent;
5c3b1f92 403}
632f5810
LOK
404
405bool CSLCommandHandler::PowerOn(const cec_logical_address iInitiator, const cec_logical_address iDestination)
406{
407 if (iDestination != CECDEVICE_TV)
408 {
bbc71623 409 /* LG devices only allow themselves to be woken up by the TV with a vendor command */
632f5810 410 cec_command command;
8b5e50ec
LOK
411
412 if (!m_bSLEnabled)
869ff027 413 TransmitVendorID(CECDEVICE_TV, iDestination, CEC_VENDOR_LG, false);
8b5e50ec 414
632f5810
LOK
415 cec_command::Format(command, CECDEVICE_TV, iDestination, CEC_OPCODE_VENDOR_COMMAND);
416 command.PushBack(SL_COMMAND_POWER_ON);
417 command.PushBack(0);
2b44051c 418 return Transmit(command, false, false);
632f5810
LOK
419 }
420
421 return CCECCommandHandler::PowerOn(iInitiator, iDestination);
422}
7b01619d
LOK
423
424void CSLCommandHandler::VendorPreActivateSourceHook(void)
425{
426 CCECPlaybackDevice *device = m_busDevice->AsPlaybackDevice();
427 if (device)
428 device->SetDeckStatus(!device->IsActiveSource() ? CEC_DECK_INFO_OTHER_STATUS : CEC_DECK_INFO_OTHER_STATUS_LG);
429}