added CCECTypeUtils::PhysicalAddressIsIncluded()
[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
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
42d28d15
LOK
89 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
90 if (primary && primary->GetLogicalAddress() != CECDEVICE_UNREGISTERED)
871934d3 91 {
42d28d15
LOK
92 /* imitate LG devices */
93 if (m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress())
94 {
95 primary->SetVendorId(CEC_VENDOR_LG);
96 primary->ReplaceHandler(false);
97 }
98
99 if (m_busDevice->GetLogicalAddress() == CECDEVICE_TV)
100 {
101 /* start as 'in transition standby->on' */
102 primary->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
2b44051c 103 primary->TransmitPowerState(CECDEVICE_TV, false);
42d28d15
LOK
104
105 /* send the vendor id */
2b44051c 106 primary->TransmitVendorID(CECDEVICE_BROADCAST, false, false);
42d28d15 107 }
871934d3 108 }
468a1414 109
468a1414
LOK
110 return true;
111}
112
9a54dc82 113int CSLCommandHandler::HandleActiveSource(const cec_command &command)
468a1414
LOK
114{
115 if (command.parameters.size == 2)
116 {
117 uint16_t iAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
bbc71623 118 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
004b8382 119 bool bSendPowerOffState(iAddress != primary->GetCurrentPhysicalAddress() && primary->IsActiveSource());
bbc71623 120
004b8382
LOK
121 CCECBusDevice *device = m_processor->GetDeviceByPhysicalAddress(iAddress);
122 if (device)
123 device->MarkAsActiveSource();
bbc71623 124 if (bSendPowerOffState)
682b21de 125 {
bbc71623
LOK
126 {
127 CLockObject lock(m_SLMutex);
128 m_bActiveSourceSent = false;
129 }
2b44051c 130 primary->TransmitPowerState(CECDEVICE_TV, false);
682b21de 131 }
9a54dc82
LOK
132
133 return COMMAND_HANDLED;
468a1414
LOK
134 }
135
9a54dc82 136 return CEC_ABORT_REASON_INVALID_OPERAND;
bbc71623 137
468a1414
LOK
138}
139
9a54dc82 140int CSLCommandHandler::HandleDeviceVendorId(const cec_command &command)
a463f954
LOK
141{
142 SetVendorId(command);
143
632f5810 144 if (!SLInitialised() && command.initiator == CECDEVICE_TV)
468a1414 145 {
63047ed3
LOK
146 CCECBusDevice *destination = m_processor->GetDevice(command.destination);
147 if (destination && (destination->GetLogicalAddress() == CECDEVICE_BROADCAST || destination->IsHandledByLibCEC()))
148 {
149 cec_logical_address initiator = destination->GetLogicalAddress();
150 if (initiator == CECDEVICE_BROADCAST)
151 initiator = m_processor->GetPrimaryDevice()->GetLogicalAddress();
152
153 cec_command response;
154 cec_command::Format(response, initiator, command.initiator, CEC_OPCODE_FEATURE_ABORT);
2b44051c 155 Transmit(response, false, true);
9a54dc82 156 return COMMAND_HANDLED;
63047ed3 157 }
468a1414 158 }
9a54dc82
LOK
159
160 return CCECCommandHandler::HandleDeviceVendorId(command);
468a1414
LOK
161}
162
9a54dc82 163int CSLCommandHandler::HandleVendorCommand(const cec_command &command)
e54fd7d2 164{
004b8382 165 if (!m_processor->IsHandledByLibCEC(command.destination))
415e9bad
LOK
166 return true;
167
e54fd7d2 168 if (command.parameters.size == 1 &&
11d13a02 169 command.parameters[0] == SL_COMMAND_UNKNOWN_01)
e54fd7d2 170 {
5f316715 171 HandleVendorCommand01(command);
9a54dc82 172 return COMMAND_HANDLED;
e54fd7d2 173 }
9a0d7b9f
LOK
174 else if (command.parameters.size == 2 &&
175 command.parameters[0] == SL_COMMAND_POWER_ON)
176 {
0ecbcd4d 177 HandleVendorCommandPowerOn(command);
9a54dc82 178 return COMMAND_HANDLED;
9a0d7b9f 179 }
797dd7c4 180 else if (command.parameters.size == 2 &&
11d13a02 181 command.parameters[0] == SL_COMMAND_CONNECT_REQUEST)
9902f4e8 182 {
0ecbcd4d 183 HandleVendorCommandSLConnect(command);
9a54dc82 184 return COMMAND_HANDLED;
9902f4e8
LOK
185 }
186 else if (command.parameters.size == 1 &&
0d4c3a7b 187 command.parameters[0] == SL_COMMAND_REQUEST_POWER_STATUS)
9902f4e8 188 {
0ecbcd4d 189 HandleVendorCommandPowerOnStatus(command);
9a54dc82 190 return COMMAND_HANDLED;
9902f4e8 191 }
e54fd7d2 192
9a54dc82 193 return CCECCommandHandler::HandleVendorCommand(command);
e54fd7d2
LOK
194}
195
5f316715 196void CSLCommandHandler::HandleVendorCommand01(const cec_command &command)
fe6f8e37 197{
b818fb5a 198 m_processor->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
fe6f8e37
LOK
199 TransmitVendorCommand0205(command.destination, command.initiator);
200}
201
202void CSLCommandHandler::TransmitVendorCommand0205(const cec_logical_address iSource, const cec_logical_address iDestination)
5f316715
LOK
203{
204 cec_command response;
fe6f8e37 205 cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
5f316715 206 response.PushBack(SL_COMMAND_UNKNOWN_02);
415e9bad 207 response.PushBack(SL_COMMAND_TYPE_HDDRECORDER);
5f316715 208
2b44051c 209 Transmit(response, false, true);
fe6f8e37 210}
5f316715 211
0ecbcd4d 212void CSLCommandHandler::HandleVendorCommandPowerOn(const cec_command &command)
9a0d7b9f 213{
632f5810
LOK
214 if (command.initiator != CECDEVICE_TV)
215 return;
216
842262d8 217 CCECBusDevice *device = m_processor->GetPrimaryDevice();
9a0d7b9f
LOK
218 if (device)
219 {
cc1b9bc4 220 SetSLInitialised();
004b8382 221 device->MarkAsActiveSource();
60383b11 222 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
2b44051c 223 device->TransmitPowerState(command.initiator, true);
60383b11
LOK
224
225 CEvent::Sleep(2000);
226 device->SetPowerStatus(CEC_POWER_STATUS_ON);
2b44051c
LOK
227 device->TransmitPowerState(command.initiator, false);
228 device->TransmitPhysicalAddress(false);
60383b11
LOK
229 {
230 CLockObject lock(m_SLMutex);
231 m_bActiveSourceSent = false;
232 }
468a1414 233 }
5f316715 234}
0ecbcd4d 235void CSLCommandHandler::HandleVendorCommandPowerOnStatus(const cec_command &command)
5f316715
LOK
236{
237 if (command.destination != CECDEVICE_BROADCAST)
238 {
004b8382
LOK
239 CCECBusDevice *device = m_processor->GetPrimaryDevice();
240 if (device)
241 {
242 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
2b44051c 243 device->TransmitPowerState(command.initiator, true);
004b8382
LOK
244 device->SetPowerStatus(CEC_POWER_STATUS_ON);
245 }
5f316715
LOK
246 }
247}
248
468a1414 249void CSLCommandHandler::HandleVendorCommandSLConnect(const cec_command &command)
8d915412 250{
cc1b9bc4 251 SetSLInitialised();
004b8382 252 TransmitVendorCommandSetDeviceMode(command.destination, command.initiator, CEC_DEVICE_TYPE_RECORDING_DEVICE);
37acf382 253
2abe628c 254 ActivateSource();
8d915412 255}
0d4c3a7b 256
5abb18f3 257void CSLCommandHandler::TransmitVendorCommandSetDeviceMode(const cec_logical_address iSource, const cec_logical_address iDestination, const cec_device_type type)
0d4c3a7b 258{
468a1414
LOK
259 cec_command response;
260 cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
5abb18f3
LOK
261 response.PushBack(SL_COMMAND_SET_DEVICE_MODE);
262 response.PushBack((uint8_t)type);
2b44051c 263 Transmit(response, false, true);
0d4c3a7b 264}
fe6f8e37 265
9a54dc82 266int CSLCommandHandler::HandleGiveDeckStatus(const cec_command &command)
9f65e017 267{
9a54dc82
LOK
268 if (!m_processor->CECInitialised() ||
269 !m_processor->IsHandledByLibCEC(command.destination))
270 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
271
272 CCECPlaybackDevice *device = CCECBusDevice::AsPlaybackDevice(GetDevice(command.destination));
273 if (!device || command.parameters.size == 0)
274 return CEC_ABORT_REASON_INVALID_OPERAND;
275
276 device->SetDeckStatus(!device->IsActiveSource() ? CEC_DECK_INFO_OTHER_STATUS : CEC_DECK_INFO_OTHER_STATUS_LG);
277 if (command.parameters[0] == CEC_STATUS_REQUEST_ON)
b818fb5a 278 {
2b44051c 279 device->TransmitDeckStatus(command.initiator, true);
9a54dc82
LOK
280 if (!ActiveSourceSent())
281 ActivateSource();
282 return COMMAND_HANDLED;
283 }
284 else if (command.parameters[0] == CEC_STATUS_REQUEST_ONCE)
285 {
2b44051c 286 device->TransmitDeckStatus(command.initiator, true);
9a54dc82 287 return COMMAND_HANDLED;
b818fb5a 288 }
9f65e017 289
9a54dc82 290 return CCECCommandHandler::HandleGiveDeckStatus(command);
9f65e017 291}
718b3632 292
9a54dc82 293int CSLCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command)
718b3632 294{
0b8c7eab 295 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination) && command.initiator == CECDEVICE_TV)
718b3632
LOK
296 {
297 CCECBusDevice *device = GetDevice(command.destination);
004b8382 298 if (device && device->GetCurrentPowerStatus() != CEC_POWER_STATUS_ON)
2abe628c 299 {
2b44051c 300 device->TransmitPowerState(command.initiator, true);
2abe628c
LOK
301 device->SetPowerStatus(CEC_POWER_STATUS_ON);
302 }
2abe628c
LOK
303 else
304 {
cc1b9bc4 305 if (!ActiveSourceSent())
3faa971c
LOK
306 {
307 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
2b44051c 308 device->TransmitPowerState(command.initiator, true);
3faa971c
LOK
309 ActivateSource();
310 }
311 else if (m_resetPowerState.IsSet() && m_resetPowerState.TimeLeft() > 0)
312 {
415e9bad 313 /* TODO assume that we've bugged out. the return button no longer works after this */
004b8382 314 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
315 {
316 CLockObject lock(m_SLMutex);
317 m_bActiveSourceSent = false;
318 }
3faa971c 319 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
2b44051c 320 device->TransmitPowerState(command.initiator, true);
3faa971c
LOK
321 device->SetPowerStatus(CEC_POWER_STATUS_ON);
322 m_resetPowerState.Init(5000);
323 }
324 else
325 {
2b44051c 326 device->TransmitPowerState(command.initiator, true);
3faa971c
LOK
327 m_resetPowerState.Init(5000);
328 }
b818fb5a 329 }
9a54dc82
LOK
330
331 return COMMAND_HANDLED;
718b3632
LOK
332 }
333
9a54dc82 334 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
2abe628c
LOK
335}
336
9a54dc82 337int CSLCommandHandler::HandleRequestActiveSource(const cec_command &command)
2abe628c 338{
0b8c7eab 339 if (m_processor->CECInitialised())
2abe628c 340 {
1fee1b3d 341 if (ActiveSourceSent())
004b8382 342 LIB_CEC->AddLog(CEC_LOG_DEBUG, ">> %i requests active source, ignored", (uint8_t) command.initiator);
1fee1b3d
LOK
343 else
344 ActivateSource();
9a54dc82 345 return COMMAND_HANDLED;
2abe628c 346 }
9a54dc82 347 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
718b3632 348}
5c3b1f92 349
9a54dc82 350int CSLCommandHandler::HandleFeatureAbort(const cec_command &command)
5c3b1f92 351{
004b8382 352 if (command.parameters.size == 0 && m_processor->GetPrimaryDevice()->GetCurrentPowerStatus() == CEC_POWER_STATUS_ON && !SLInitialised() &&
632f5810 353 command.initiator == CECDEVICE_TV)
5c3b1f92 354 {
2b44051c
LOK
355 m_processor->GetPrimaryDevice()->TransmitPowerState(command.initiator, false);
356 m_processor->GetPrimaryDevice()->TransmitVendorID(CECDEVICE_BROADCAST, false, false);
5c3b1f92
LOK
357 }
358
359 return CCECCommandHandler::HandleFeatureAbort(command);
360}
361
9a54dc82 362int CSLCommandHandler::HandleStandby(const cec_command &command)
5c3b1f92
LOK
363{
364 if (command.initiator == CECDEVICE_TV)
1fee1b3d
LOK
365 {
366 CLockObject lock(m_SLMutex);
367 m_bActiveSourceSent = false;
368 }
5c3b1f92 369
cc1b9bc4
LOK
370 return CCECCommandHandler::HandleStandby(command);
371}
5c3b1f92 372
cc1b9bc4
LOK
373void CSLCommandHandler::ResetSLState(void)
374{
004b8382 375 LIB_CEC->AddLog(CEC_LOG_NOTICE, "resetting SL initialised state");
cc1b9bc4
LOK
376 CLockObject lock(m_SLMutex);
377 m_bSLEnabled = false;
378 m_bActiveSourceSent = false;
379 m_processor->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
380}
381
382void CSLCommandHandler::SetSLInitialised(void)
383{
004b8382 384 LIB_CEC->AddLog(CEC_LOG_NOTICE, "SL initialised");
cc1b9bc4
LOK
385 CLockObject lock(m_SLMutex);
386 m_bSLEnabled = true;
387}
388
389bool CSLCommandHandler::SLInitialised(void)
390{
391 CLockObject lock(m_SLMutex);
392 return m_bSLEnabled;
393}
394
395bool CSLCommandHandler::ActiveSourceSent(void)
396{
397 CLockObject lock(m_SLMutex);
398 return m_bActiveSourceSent;
5c3b1f92 399}
632f5810
LOK
400
401bool CSLCommandHandler::PowerOn(const cec_logical_address iInitiator, const cec_logical_address iDestination)
402{
403 if (iDestination != CECDEVICE_TV)
404 {
bbc71623 405 /* LG devices only allow themselves to be woken up by the TV with a vendor command */
632f5810 406 cec_command command;
8b5e50ec
LOK
407
408 if (!m_bSLEnabled)
2b44051c 409 TransmitVendorID(CECDEVICE_TV, CEC_VENDOR_LG, false);
8b5e50ec 410
632f5810
LOK
411 cec_command::Format(command, CECDEVICE_TV, iDestination, CEC_OPCODE_VENDOR_COMMAND);
412 command.PushBack(SL_COMMAND_POWER_ON);
413 command.PushBack(0);
2b44051c 414 return Transmit(command, false, false);
632f5810
LOK
415 }
416
417 return CCECCommandHandler::PowerOn(iInitiator, iDestination);
418}
7b01619d
LOK
419
420void CSLCommandHandler::VendorPreActivateSourceHook(void)
421{
422 CCECPlaybackDevice *device = m_busDevice->AsPlaybackDevice();
423 if (device)
424 device->SetDeckStatus(!device->IsActiveSource() ? CEC_DECK_INFO_OTHER_STATUS : CEC_DECK_INFO_OTHER_STATUS_LG);
425}