cec: more LG loop fix hacks
[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"
e9de9629
LOK
37
38using namespace CEC;
39
0d4c3a7b
LOK
40#define SL_COMMAND_UNKNOWN_01 0x01
41#define SL_COMMAND_UNKNOWN_02 0x02
42#define SL_COMMAND_UNKNOWN_03 0x05
11d13a02 43
0d4c3a7b 44#define SL_COMMAND_REQUEST_POWER_STATUS 0xa0
9a0d7b9f 45#define SL_COMMAND_POWER_ON 0x03
0d4c3a7b
LOK
46#define SL_COMMAND_CONNECT_REQUEST 0x04
47#define SL_COMMAND_CONNECT_ACCEPT 0x05
11d13a02 48
e9de9629 49CSLCommandHandler::CSLCommandHandler(CCECBusDevice *busDevice) :
855a3a98 50 CCECCommandHandler(busDevice),
b4c4ef7d 51 m_bSLEnabled(false),
b818fb5a
LOK
52 m_bPowerStateReset(false),
53 m_bActiveSourceSent(false)
e9de9629 54{
cf4931be 55 m_vendorId = CEC_VENDOR_LG;
79f01d26
LOK
56 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
57
58 /* imitate LG devices */
ef583662 59 if (primary && m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress())
3e61b350 60 primary->SetVendorId(CEC_VENDOR_LG);
79f01d26
LOK
61
62 /* LG TVs don't always reply to CEC version requests, so just set it to 1.3a */
63 if (m_busDevice->GetLogicalAddress() == CECDEVICE_TV)
64 m_busDevice->SetCecVersion(CEC_VERSION_1_3A);
65
66 /* LG devices always return "korean" as language */
67 cec_menu_language lang;
68 lang.device = m_busDevice->GetLogicalAddress();
69 snprintf(lang.language, 4, "eng");
70 m_busDevice->SetMenuLanguage(lang);
9f65e017
LOK
71}
72
468a1414
LOK
73bool CSLCommandHandler::InitHandler(void)
74{
75 if (m_bHandlerInited)
76 return true;
77 m_bHandlerInited = true;
78
79 /* reply with LGs vendor id */
80 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
81 if (m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress())
82 primary->TransmitVendorID(CECDEVICE_TV, false);
83
a463f954 84 primary->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
468a1414
LOK
85 return true;
86}
87
3e61b350
LOK
88bool CSLCommandHandler::ActivateSource(void)
89{
b818fb5a
LOK
90 if (m_bActiveSourceSent)
91 return false;
92 m_bActiveSourceSent = true;
93
3e61b350
LOK
94 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
95 primary->SetActiveSource();
96 primary->TransmitActiveSource();
97 return true;
98}
99
468a1414
LOK
100bool CSLCommandHandler::HandleActiveSource(const cec_command &command)
101{
102 if (command.parameters.size == 2)
103 {
104 uint16_t iAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
105 if (iAddress != m_busDevice->GetPhysicalAddress(false))
106 m_bSLEnabled = false;
107 return m_processor->SetActiveSource(iAddress);
108 }
109
110 return true;
111}
112
a463f954
LOK
113bool CSLCommandHandler::HandleDeviceVendorId(const cec_command &command)
114{
115 SetVendorId(command);
116
117 cec_command response;
118 cec_command::Format(response, m_processor->GetLogicalAddress(), command.initiator, CEC_OPCODE_FEATURE_ABORT);
119 return Transmit(response);
120}
121
468a1414
LOK
122bool CSLCommandHandler::HandleFeatureAbort(const cec_command &command)
123{
124 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
125 if (primary->GetPowerStatus(false) == CEC_POWER_STATUS_ON && !m_bPowerStateReset && !m_bSLEnabled)
126 {
b818fb5a 127 // reset to standby->on while SL hasn't been initialised
468a1414 128 m_bPowerStateReset = true;
b818fb5a 129 m_bActiveSourceSent = false;
a463f954 130 primary->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
468a1414
LOK
131 }
132
133 return CCECCommandHandler::HandleFeatureAbort(command);
134}
135
136bool CSLCommandHandler::HandleGivePhysicalAddress(const cec_command &command)
137{
8e57f83c 138 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
468a1414
LOK
139 {
140 CCECBusDevice *device = GetDevice(command.destination);
141 if (device)
b818fb5a 142 return device->TransmitPhysicalAddress(); // only the physical address, don't send image view on
468a1414
LOK
143 }
144
145 return false;
e9de9629 146}
e54fd7d2
LOK
147
148bool CSLCommandHandler::HandleVendorCommand(const cec_command &command)
149{
150 if (command.parameters.size == 1 &&
11d13a02 151 command.parameters[0] == SL_COMMAND_UNKNOWN_01)
e54fd7d2 152 {
5f316715 153 HandleVendorCommand01(command);
8d915412 154 return true;
e54fd7d2 155 }
9a0d7b9f
LOK
156 else if (command.parameters.size == 2 &&
157 command.parameters[0] == SL_COMMAND_POWER_ON)
158 {
0ecbcd4d 159 HandleVendorCommandPowerOn(command);
9a0d7b9f
LOK
160 return true;
161 }
797dd7c4 162 else if (command.parameters.size == 2 &&
11d13a02 163 command.parameters[0] == SL_COMMAND_CONNECT_REQUEST)
9902f4e8 164 {
0ecbcd4d 165 HandleVendorCommandSLConnect(command);
8d915412 166 return true;
9902f4e8
LOK
167 }
168 else if (command.parameters.size == 1 &&
0d4c3a7b 169 command.parameters[0] == SL_COMMAND_REQUEST_POWER_STATUS)
9902f4e8 170 {
0ecbcd4d 171 HandleVendorCommandPowerOnStatus(command);
8d915412 172 return true;
9902f4e8 173 }
e54fd7d2
LOK
174
175 return false;
176}
177
5f316715 178void CSLCommandHandler::HandleVendorCommand01(const cec_command &command)
fe6f8e37 179{
b818fb5a 180 m_processor->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
fe6f8e37
LOK
181 TransmitVendorCommand0205(command.destination, command.initiator);
182}
183
184void CSLCommandHandler::TransmitVendorCommand0205(const cec_logical_address iSource, const cec_logical_address iDestination)
5f316715
LOK
185{
186 cec_command response;
fe6f8e37 187 cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
5f316715
LOK
188 response.PushBack(SL_COMMAND_UNKNOWN_02);
189 response.PushBack(SL_COMMAND_UNKNOWN_03);
190
19cbfa8f 191 Transmit(response, false);
fe6f8e37 192}
5f316715 193
0ecbcd4d 194void CSLCommandHandler::HandleVendorCommandPowerOn(const cec_command &command)
9a0d7b9f 195{
842262d8 196 CCECBusDevice *device = m_processor->GetPrimaryDevice();
9a0d7b9f
LOK
197 if (device)
198 {
fe6f8e37 199 m_bSLEnabled = true;
468a1414
LOK
200
201 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); //XXX
9a0d7b9f 202 device->TransmitPowerState(command.initiator);
468a1414 203 device->SetPowerStatus(CEC_POWER_STATUS_ON);
9a0d7b9f 204
468a1414
LOK
205 device->SetActiveSource();
206 TransmitImageViewOn(device->GetLogicalAddress(), command.initiator);
207 }
5f316715 208}
0ecbcd4d 209void CSLCommandHandler::HandleVendorCommandPowerOnStatus(const cec_command &command)
5f316715
LOK
210{
211 if (command.destination != CECDEVICE_BROADCAST)
212 {
213 CCECBusDevice *device = m_processor->m_busDevices[m_processor->GetLogicalAddresses().primary];
214 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
215 device->TransmitPowerState(command.initiator);
216 device->SetPowerStatus(CEC_POWER_STATUS_ON);
217 }
218}
219
468a1414 220void CSLCommandHandler::HandleVendorCommandSLConnect(const cec_command &command)
8d915412 221{
468a1414 222 m_bSLEnabled = true;
79f01d26 223 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
b818fb5a 224 primary->SetPowerStatus(CEC_POWER_STATUS_ON);
37acf382 225
468a1414 226 TransmitVendorCommand05(primary->GetLogicalAddress(), command.initiator);
8d915412 227}
0d4c3a7b 228
468a1414 229void CSLCommandHandler::TransmitVendorCommand05(const cec_logical_address iSource, const cec_logical_address iDestination)
0d4c3a7b 230{
468a1414
LOK
231 cec_command response;
232 cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
233 response.PushBack(SL_COMMAND_CONNECT_ACCEPT);
199034ab 234 response.PushBack((uint8_t)m_processor->m_busDevices[iSource]->GetType());
468a1414 235 Transmit(response, false);
0d4c3a7b 236}
fe6f8e37 237
b818fb5a 238bool CSLCommandHandler::HandleGiveDeckStatus(const cec_command &command)
9f65e017 239{
b818fb5a
LOK
240 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
241 {
242 CCECBusDevice *device = GetDevice(command.destination);
243 if (device && (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE))
244 {
245 if (command.parameters.size > 0)
246 {
247 ((CCECPlaybackDevice *) device)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG);
248 if (command.parameters[0] == CEC_STATUS_REQUEST_ON)
249 {
250 return ((CCECPlaybackDevice *) device)->TransmitDeckStatus(command.initiator) &&
251 device->TransmitImageViewOn() &&
252 device->TransmitPhysicalAddress();
253 }
254 else if (command.parameters[0] == CEC_STATUS_REQUEST_ONCE)
255 {
256 return ((CCECPlaybackDevice *) device)->TransmitDeckStatus(command.initiator);
257 }
258 }
259 }
260 return CCECCommandHandler::HandleGiveDeckStatus(command);
261 }
9f65e017 262
b818fb5a 263 return false;
9f65e017 264}
718b3632 265
718b3632
LOK
266bool CSLCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command)
267{
268 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
269 {
270 CCECBusDevice *device = GetDevice(command.destination);
271 if (device && device->GetPowerStatus(false) != CEC_POWER_STATUS_ON)
272 return device->TransmitPowerState(command.initiator);
b818fb5a
LOK
273 else if (!ActivateSource())
274 {
275 // assume that we've bugged out, reset
276 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
277 }
718b3632
LOK
278 }
279
280 return false;
281}