cec: replace the handler for the primary device directly after setting the type to LG
[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
44#define SL_COMMAND_UNKNOWN_03 0x05
11d13a02 45
0d4c3a7b 46#define SL_COMMAND_REQUEST_POWER_STATUS 0xa0
9a0d7b9f 47#define SL_COMMAND_POWER_ON 0x03
0d4c3a7b 48#define SL_COMMAND_CONNECT_REQUEST 0x04
5abb18f3 49#define SL_COMMAND_SET_DEVICE_MODE 0x05
11d13a02 50
e9de9629 51CSLCommandHandler::CSLCommandHandler(CCECBusDevice *busDevice) :
855a3a98 52 CCECCommandHandler(busDevice),
b4c4ef7d 53 m_bSLEnabled(false),
1c38352a
LOK
54 m_bActiveSourceSent(false),
55 m_bVendorIdSent(false)
e9de9629 56{
cf4931be 57 m_vendorId = CEC_VENDOR_LG;
79f01d26
LOK
58 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
59
60 /* imitate LG devices */
ef583662 61 if (primary && m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress())
686740b9 62 {
3e61b350 63 primary->SetVendorId(CEC_VENDOR_LG);
686740b9
LOK
64 primary->ReplaceHandler(false);
65 }
79f01d26
LOK
66
67 /* LG TVs don't always reply to CEC version requests, so just set it to 1.3a */
68 if (m_busDevice->GetLogicalAddress() == CECDEVICE_TV)
69 m_busDevice->SetCecVersion(CEC_VERSION_1_3A);
70
71 /* LG devices always return "korean" as language */
72 cec_menu_language lang;
73 lang.device = m_busDevice->GetLogicalAddress();
74 snprintf(lang.language, 4, "eng");
75 m_busDevice->SetMenuLanguage(lang);
9f65e017
LOK
76}
77
468a1414
LOK
78bool CSLCommandHandler::InitHandler(void)
79{
80 if (m_bHandlerInited)
81 return true;
82 m_bHandlerInited = true;
83
468a1414
LOK
84 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
85 if (m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress())
871934d3
LOK
86 {
87 /* start as 'in transition standby->on' */
88 primary->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
89 primary->TransmitPowerState(CECDEVICE_TV);
871934d3 90 }
468a1414 91
468a1414
LOK
92 return true;
93}
94
3e61b350
LOK
95bool CSLCommandHandler::ActivateSource(void)
96{
1c38352a 97 /* reply with LGs vendor id */
cc1b9bc4 98 bool bSendVendorId(false);
1c38352a 99 {
cc1b9bc4
LOK
100 CLockObject lock(m_SLMutex);
101 bSendVendorId = !m_bVendorIdSent;
1c38352a 102 m_bVendorIdSent = true;
1c38352a 103 }
cc1b9bc4
LOK
104 if (bSendVendorId)
105 m_processor->GetPrimaryDevice()->TransmitVendorID(CECDEVICE_BROADCAST, false);
1c38352a 106
cc1b9bc4 107 if (!SLInitialised())
682b21de
LOK
108 {
109 CLibCEC::AddLog(CEC_LOG_NOTICE, "not activating the source until SL has been initialised");
3faa971c 110 return true;
682b21de 111 }
3faa971c 112
cc1b9bc4
LOK
113 {
114 CLockObject lock(m_SLMutex);
115 if (m_bActiveSourceSent)
116 return true;
117 m_bActiveSourceSent = true;
118 }
b818fb5a 119
3e61b350
LOK
120 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
121 primary->SetActiveSource();
122 primary->TransmitActiveSource();
123 return true;
124}
125
468a1414
LOK
126bool CSLCommandHandler::HandleActiveSource(const cec_command &command)
127{
128 if (command.parameters.size == 2)
129 {
130 uint16_t iAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
131 if (iAddress != m_busDevice->GetPhysicalAddress(false))
682b21de 132 {
cc1b9bc4 133 ResetSLState();
682b21de 134 }
468a1414
LOK
135 return m_processor->SetActiveSource(iAddress);
136 }
137
138 return true;
139}
140
a463f954
LOK
141bool CSLCommandHandler::HandleDeviceVendorId(const cec_command &command)
142{
143 SetVendorId(command);
144
cc1b9bc4 145 if (!SLInitialised())
468a1414 146 {
2abe628c
LOK
147 cec_command response;
148 cec_command::Format(response, m_processor->GetLogicalAddress(), command.initiator, CEC_OPCODE_FEATURE_ABORT);
149 return Transmit(response);
468a1414 150 }
2abe628c 151 return true;
468a1414
LOK
152}
153
154bool CSLCommandHandler::HandleGivePhysicalAddress(const cec_command &command)
155{
8e57f83c 156 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
468a1414
LOK
157 {
158 CCECBusDevice *device = GetDevice(command.destination);
159 if (device)
b818fb5a 160 return device->TransmitPhysicalAddress(); // only the physical address, don't send image view on
468a1414
LOK
161 }
162
163 return false;
e9de9629 164}
e54fd7d2
LOK
165
166bool CSLCommandHandler::HandleVendorCommand(const cec_command &command)
167{
168 if (command.parameters.size == 1 &&
11d13a02 169 command.parameters[0] == SL_COMMAND_UNKNOWN_01)
e54fd7d2 170 {
5f316715 171 HandleVendorCommand01(command);
8d915412 172 return true;
e54fd7d2 173 }
9a0d7b9f
LOK
174 else if (command.parameters.size == 2 &&
175 command.parameters[0] == SL_COMMAND_POWER_ON)
176 {
0ecbcd4d 177 HandleVendorCommandPowerOn(command);
9a0d7b9f
LOK
178 return true;
179 }
797dd7c4 180 else if (command.parameters.size == 2 &&
11d13a02 181 command.parameters[0] == SL_COMMAND_CONNECT_REQUEST)
9902f4e8 182 {
0ecbcd4d 183 HandleVendorCommandSLConnect(command);
8d915412 184 return true;
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);
8d915412 190 return true;
9902f4e8 191 }
e54fd7d2
LOK
192
193 return false;
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
LOK
206 response.PushBack(SL_COMMAND_UNKNOWN_02);
207 response.PushBack(SL_COMMAND_UNKNOWN_03);
208
19cbfa8f 209 Transmit(response, false);
fe6f8e37 210}
5f316715 211
0ecbcd4d 212void CSLCommandHandler::HandleVendorCommandPowerOn(const cec_command &command)
9a0d7b9f 213{
842262d8 214 CCECBusDevice *device = m_processor->GetPrimaryDevice();
9a0d7b9f
LOK
215 if (device)
216 {
cc1b9bc4 217 SetSLInitialised();
468a1414
LOK
218
219 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); //XXX
9a0d7b9f 220 device->TransmitPowerState(command.initiator);
468a1414 221 device->SetPowerStatus(CEC_POWER_STATUS_ON);
9a0d7b9f 222
468a1414
LOK
223 device->SetActiveSource();
224 TransmitImageViewOn(device->GetLogicalAddress(), command.initiator);
225 }
5f316715 226}
0ecbcd4d 227void CSLCommandHandler::HandleVendorCommandPowerOnStatus(const cec_command &command)
5f316715
LOK
228{
229 if (command.destination != CECDEVICE_BROADCAST)
230 {
231 CCECBusDevice *device = m_processor->m_busDevices[m_processor->GetLogicalAddresses().primary];
232 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
233 device->TransmitPowerState(command.initiator);
234 device->SetPowerStatus(CEC_POWER_STATUS_ON);
235 }
236}
237
468a1414 238void CSLCommandHandler::HandleVendorCommandSLConnect(const cec_command &command)
8d915412 239{
cc1b9bc4 240 SetSLInitialised();
5abb18f3 241 TransmitVendorCommandSetDeviceMode(m_processor->GetLogicalAddress(), command.initiator, CEC_DEVICE_TYPE_RECORDING_DEVICE);
37acf382 242
2abe628c 243 ActivateSource();
8d915412 244}
0d4c3a7b 245
5abb18f3 246void CSLCommandHandler::TransmitVendorCommandSetDeviceMode(const cec_logical_address iSource, const cec_logical_address iDestination, const cec_device_type type)
0d4c3a7b 247{
468a1414
LOK
248 cec_command response;
249 cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
5abb18f3
LOK
250 response.PushBack(SL_COMMAND_SET_DEVICE_MODE);
251 response.PushBack((uint8_t)type);
468a1414 252 Transmit(response, false);
0d4c3a7b 253}
fe6f8e37 254
b818fb5a 255bool CSLCommandHandler::HandleGiveDeckStatus(const cec_command &command)
9f65e017 256{
b818fb5a
LOK
257 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
258 {
259 CCECBusDevice *device = GetDevice(command.destination);
260 if (device && (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE))
261 {
262 if (command.parameters.size > 0)
263 {
b818fb5a
LOK
264 if (command.parameters[0] == CEC_STATUS_REQUEST_ON)
265 {
2abe628c 266 ((CCECPlaybackDevice *) device)->SetDeckStatus(CEC_DECK_INFO_STOP);
b818fb5a
LOK
267 return ((CCECPlaybackDevice *) device)->TransmitDeckStatus(command.initiator) &&
268 device->TransmitImageViewOn() &&
269 device->TransmitPhysicalAddress();
270 }
271 else if (command.parameters[0] == CEC_STATUS_REQUEST_ONCE)
272 {
2abe628c 273 ((CCECPlaybackDevice *) device)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG);
b818fb5a
LOK
274 return ((CCECPlaybackDevice *) device)->TransmitDeckStatus(command.initiator);
275 }
276 }
277 }
278 return CCECCommandHandler::HandleGiveDeckStatus(command);
279 }
9f65e017 280
b818fb5a 281 return false;
9f65e017 282}
718b3632 283
718b3632
LOK
284bool CSLCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command)
285{
2abe628c 286 bool bReturn(false);
718b3632
LOK
287 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
288 {
289 CCECBusDevice *device = GetDevice(command.destination);
290 if (device && device->GetPowerStatus(false) != CEC_POWER_STATUS_ON)
2abe628c
LOK
291 {
292 bReturn = device->TransmitPowerState(command.initiator);
293 device->SetPowerStatus(CEC_POWER_STATUS_ON);
294 }
2abe628c
LOK
295 else
296 {
cc1b9bc4 297 if (!ActiveSourceSent())
3faa971c
LOK
298 {
299 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
300 bReturn = device->TransmitPowerState(command.initiator);
301 ActivateSource();
302 }
303 else if (m_resetPowerState.IsSet() && m_resetPowerState.TimeLeft() > 0)
304 {
305 /* assume that we've bugged out */
306 CLibCEC::AddLog(CEC_LOG_NOTICE, "LG seems to have bugged out. resetting to 'in transition standby to on'");
cc1b9bc4
LOK
307 {
308 CLockObject lock(m_SLMutex);
309 m_bActiveSourceSent = false;
310 }
3faa971c
LOK
311 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
312 bReturn = device->TransmitPowerState(command.initiator);
313 device->SetPowerStatus(CEC_POWER_STATUS_ON);
314 m_resetPowerState.Init(5000);
315 }
316 else
317 {
318 bReturn = device->TransmitPowerState(command.initiator);
319 m_resetPowerState.Init(5000);
320 }
b818fb5a 321 }
718b3632
LOK
322 }
323
2abe628c
LOK
324 return bReturn;
325}
326
327bool CSLCommandHandler::HandleRequestActiveSource(const cec_command &command)
328{
329 if (m_processor->IsRunning())
330 {
331 CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %i requests active source, ignored", (uint8_t) command.initiator);
332 return true;
333 }
718b3632
LOK
334 return false;
335}
5c3b1f92
LOK
336
337bool CSLCommandHandler::HandleFeatureAbort(const cec_command &command)
338{
cc1b9bc4 339 if (command.parameters.size == 0 && m_processor->GetPrimaryDevice()->GetPowerStatus() == CEC_POWER_STATUS_ON && !SLInitialised())
5c3b1f92
LOK
340 {
341 m_processor->GetPrimaryDevice()->TransmitPowerState(command.initiator);
342 m_processor->GetPrimaryDevice()->TransmitVendorID(CECDEVICE_BROADCAST, false);
343 }
344
345 return CCECCommandHandler::HandleFeatureAbort(command);
346}
347
348bool CSLCommandHandler::HandleStandby(const cec_command &command)
349{
350 if (command.initiator == CECDEVICE_TV)
cc1b9bc4 351 ResetSLState();
5c3b1f92 352
cc1b9bc4
LOK
353 return CCECCommandHandler::HandleStandby(command);
354}
5c3b1f92 355
cc1b9bc4
LOK
356void CSLCommandHandler::ResetSLState(void)
357{
358 CLibCEC::AddLog(CEC_LOG_NOTICE, "resetting SL initialised state");
359 CLockObject lock(m_SLMutex);
360 m_bSLEnabled = false;
361 m_bActiveSourceSent = false;
362 m_processor->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
363}
364
365void CSLCommandHandler::SetSLInitialised(void)
366{
367 CLibCEC::AddLog(CEC_LOG_NOTICE, "SL initialised");
368 CLockObject lock(m_SLMutex);
369 m_bSLEnabled = true;
370}
371
372bool CSLCommandHandler::SLInitialised(void)
373{
374 CLockObject lock(m_SLMutex);
375 return m_bSLEnabled;
376}
377
378bool CSLCommandHandler::ActiveSourceSent(void)
379{
380 CLockObject lock(m_SLMutex);
381 return m_bActiveSourceSent;
5c3b1f92 382}