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