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