cec: cleanups/fixes in the LG command handler
[deb_libcec.git] / src / lib / implementations / SLCommandHandler.cpp
CommitLineData
e9de9629
LOK
1/*
2 * This file is part of the libCEC(R) library.
3 *
4 * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited. All rights reserved.
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"
b4c4ef7d 37#include "../platform/timeutils.h"
e9de9629
LOK
38
39using namespace CEC;
40
0d4c3a7b
LOK
41#define SL_COMMAND_UNKNOWN_01 0x01
42#define SL_COMMAND_UNKNOWN_02 0x02
43#define SL_COMMAND_UNKNOWN_03 0x05
11d13a02 44
0d4c3a7b 45#define SL_COMMAND_REQUEST_POWER_STATUS 0xa0
9a0d7b9f 46#define SL_COMMAND_POWER_ON 0x03
0d4c3a7b
LOK
47#define SL_COMMAND_CONNECT_REQUEST 0x04
48#define SL_COMMAND_CONNECT_ACCEPT 0x05
11d13a02 49
e9de9629 50CSLCommandHandler::CSLCommandHandler(CCECBusDevice *busDevice) :
855a3a98 51 CCECCommandHandler(busDevice),
b4c4ef7d
LOK
52 m_bAwaitingReceiveFailed(false),
53 m_bSLEnabled(false),
5f316715 54 m_bVendorIdSent(false)
e9de9629 55{
9f65e017
LOK
56}
57
58void CSLCommandHandler::SetLGDeckStatus(void)
59{
60 /* LG TVs only route keypresses when the deck status is set to 0x20 */
61 CCECBusDevice *device = m_processor->GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
62 if (device)
63 ((CCECPlaybackDevice *)device)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG);
64
65 device = m_processor->GetDeviceByType(CEC_DEVICE_TYPE_RECORDING_DEVICE);
66 if (device)
67 ((CCECPlaybackDevice *)device)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG);
e9de9629 68}
e54fd7d2
LOK
69
70bool CSLCommandHandler::HandleVendorCommand(const cec_command &command)
71{
72 if (command.parameters.size == 1 &&
11d13a02 73 command.parameters[0] == SL_COMMAND_UNKNOWN_01)
e54fd7d2 74 {
5f316715 75 HandleVendorCommand01(command);
8d915412 76 return true;
e54fd7d2 77 }
9a0d7b9f
LOK
78 else if (command.parameters.size == 2 &&
79 command.parameters[0] == SL_COMMAND_POWER_ON)
80 {
0ecbcd4d 81 HandleVendorCommandPowerOn(command);
9a0d7b9f
LOK
82 return true;
83 }
797dd7c4 84 else if (command.parameters.size == 2 &&
11d13a02 85 command.parameters[0] == SL_COMMAND_CONNECT_REQUEST)
9902f4e8 86 {
0ecbcd4d 87 HandleVendorCommandSLConnect(command);
8d915412 88 return true;
9902f4e8
LOK
89 }
90 else if (command.parameters.size == 1 &&
0d4c3a7b 91 command.parameters[0] == SL_COMMAND_REQUEST_POWER_STATUS)
9902f4e8 92 {
0ecbcd4d 93 HandleVendorCommandPowerOnStatus(command);
8d915412 94 return true;
9902f4e8 95 }
e54fd7d2
LOK
96
97 return false;
98}
99
5f316715 100void CSLCommandHandler::HandleVendorCommand01(const cec_command &command)
fe6f8e37
LOK
101{
102 TransmitVendorCommand0205(command.destination, command.initiator);
103}
104
105void CSLCommandHandler::TransmitVendorCommand0205(const cec_logical_address iSource, const cec_logical_address iDestination)
5f316715
LOK
106{
107 cec_command response;
fe6f8e37 108 cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
5f316715
LOK
109 response.PushBack(SL_COMMAND_UNKNOWN_02);
110 response.PushBack(SL_COMMAND_UNKNOWN_03);
111
112 Transmit(response);
fe6f8e37 113}
5f316715 114
0e54ee2e 115void CSLCommandHandler::TransmitVendorCommand05(const cec_logical_address iSource, const cec_logical_address iDestination)
fe6f8e37
LOK
116{
117 m_bSLEnabled = true;
118 cec_command response;
119 cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
120 response.PushBack(SL_COMMAND_CONNECT_ACCEPT);
121 response.PushBack((uint8_t)iSource);
122 Transmit(response);
5f316715
LOK
123}
124
0ecbcd4d 125void CSLCommandHandler::HandleVendorCommandPowerOn(const cec_command &command)
9a0d7b9f 126{
842262d8 127 CCECBusDevice *device = m_processor->GetPrimaryDevice();
9a0d7b9f
LOK
128 if (device)
129 {
fe6f8e37 130 m_bSLEnabled = true;
9f65e017 131 SetLGDeckStatus();
fe6f8e37 132 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
9a0d7b9f 133 device->TransmitPowerState(command.initiator);
fe6f8e37 134 device->TransmitVendorID(command.initiator);
9a0d7b9f 135 TransmitPowerOn(device->GetLogicalAddress(), command.initiator);
9a0d7b9f
LOK
136 }
137}
138
0ecbcd4d 139void CSLCommandHandler::HandleVendorCommandSLConnect(const cec_command &command)
5f316715 140{
9f65e017 141 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
fe6f8e37 142 m_bSLEnabled = true;
37b0c572 143 m_processor->m_busDevices[command.initiator]->SetActiveSource();
0ecbcd4d 144 m_processor->m_busDevices[command.destination]->TransmitActiveSource();
0e54ee2e 145 TransmitVendorCommand05(command.destination, command.initiator);
9f65e017 146 SetLGDeckStatus();
5f316715
LOK
147}
148
0ecbcd4d 149void CSLCommandHandler::HandleVendorCommandPowerOnStatus(const cec_command &command)
5f316715
LOK
150{
151 if (command.destination != CECDEVICE_BROADCAST)
152 {
153 CCECBusDevice *device = m_processor->m_busDevices[m_processor->GetLogicalAddresses().primary];
154 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
155 device->TransmitPowerState(command.initiator);
156 device->SetPowerStatus(CEC_POWER_STATUS_ON);
157 }
158}
159
8d915412
LOK
160bool CSLCommandHandler::TransmitLGVendorId(const cec_logical_address iInitiator, const cec_logical_address iDestination)
161{
162 cec_command response;
0d4c3a7b 163 cec_command::Format(response, iInitiator, iDestination, CEC_OPCODE_DEVICE_VENDOR_ID);
8d915412
LOK
164 response.parameters.PushBack((uint8_t) (((uint64_t)CEC_VENDOR_LG >> 16) & 0xFF));
165 response.parameters.PushBack((uint8_t) (((uint64_t)CEC_VENDOR_LG >> 8) & 0xFF));
166 response.parameters.PushBack((uint8_t) ((uint64_t)CEC_VENDOR_LG & 0xFF));
167
8fa35473 168 Transmit(response);
8d915412 169
9f65e017
LOK
170 cec_command::Format(response, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
171 Transmit(response);
e54fd7d2 172
9f65e017 173 return true;
e54fd7d2
LOK
174}
175
176bool CSLCommandHandler::HandleCommand(const cec_command &command)
177{
178 bool bHandled(false);
5f316715 179
fe6f8e37
LOK
180 if (m_processor->IsStarted() && (m_busDevice->MyLogicalAddressContains(command.destination) ||
181 command.destination == CECDEVICE_BROADCAST))
e54fd7d2
LOK
182 {
183 switch(command.opcode)
184 {
185 case CEC_OPCODE_VENDOR_COMMAND:
186 bHandled = HandleVendorCommand(command);
187 break;
b4c4ef7d
LOK
188 case CEC_OPCODE_FEATURE_ABORT:
189 {
5f316715 190 if (!m_bVendorIdSent)
b4c4ef7d 191 {
5f316715
LOK
192 m_bVendorIdSent = true;
193 TransmitLGVendorId(m_processor->GetLogicalAddresses().primary, CECDEVICE_BROADCAST);
9f65e017 194 m_processor->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_STANDBY);
45b97de7 195 }
b4c4ef7d
LOK
196 }
197 bHandled = true;
9f65e017 198 break;
e54fd7d2
LOK
199 default:
200 break;
201 }
202 }
203
204 if (!bHandled)
205 bHandled = CCECCommandHandler::HandleCommand(command);
206
207 return bHandled;
208}
855a3a98 209
855a3a98
LOK
210void CSLCommandHandler::HandlePoll(const cec_logical_address iInitiator, const cec_logical_address iDestination)
211{
212 CCECCommandHandler::HandlePoll(iInitiator, iDestination);
213 m_bAwaitingReceiveFailed = true;
214}
215
216bool CSLCommandHandler::HandleReceiveFailed(void)
217{
218 if (m_bAwaitingReceiveFailed)
219 {
220 m_bAwaitingReceiveFailed = false;
221 return false;
222 }
223
224 return true;
225}
8d915412
LOK
226
227bool CSLCommandHandler::InitHandler(void)
228{
89a726fa
LOK
229 if (m_bHandlerInited)
230 return true;
231 m_bHandlerInited = true;
232
842262d8 233 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
9f65e017
LOK
234
235 /* imitate LG devices */
d211708b
LOK
236 if (m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress())
237 primary->SetVendorId(CEC_VENDOR_LG, false);
b4c4ef7d 238
9f65e017 239 /* LG TVs don't always reply to CEC version requests, so just set it to 1.3a */
37acf382 240 if (m_busDevice->GetLogicalAddress() == CECDEVICE_TV)
37acf382 241 m_busDevice->SetCecVersion(CEC_VERSION_1_3A);
caaf64d7 242
37acf382
LOK
243 /* LG devices always return "korean" as language */
244 cec_menu_language lang;
245 lang.device = m_busDevice->GetLogicalAddress();
246 snprintf(lang.language, 4, "eng");
247 m_busDevice->SetMenuLanguage(lang);
caaf64d7 248
9f65e017
LOK
249 /* reply with LGs vendor id */
250 if (m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress())
251 primary->TransmitVendorID(CECDEVICE_TV, false);
37acf382 252
8d915412
LOK
253 return true;
254}
0d4c3a7b 255
0d4c3a7b
LOK
256bool CSLCommandHandler::TransmitPowerOn(const cec_logical_address iInitiator, const cec_logical_address iDestination)
257{
5f316715 258 if (iDestination != CECDEVICE_BROADCAST &&
5827f4aa 259 iDestination != CECDEVICE_TV &&
9a0d7b9f 260 m_processor->m_busDevices[iDestination]->GetVendorId(false) == CEC_VENDOR_LG)
5f316715
LOK
261 {
262 cec_command command;
263 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_VENDOR_COMMAND);
9a0d7b9f
LOK
264 command.parameters.PushBack((uint8_t)SL_COMMAND_POWER_ON);
265 command.parameters.PushBack(0x00);
5f316715
LOK
266 return Transmit(command);
267 }
0d4c3a7b 268
9f65e017 269 return CCECCommandHandler::TransmitImageViewOn(iInitiator, iDestination);
0d4c3a7b 270}
fe6f8e37 271
9f65e017
LOK
272bool CSLCommandHandler::HandleActiveSource(const cec_command &command)
273{
274 if (command.parameters.size == 2)
275 {
276 uint16_t iAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
277 if (iAddress == 0)
278 return m_processor->GetPrimaryDevice()->TransmitPhysicalAddress();
279 else
280 return m_processor->SetActiveSource(iAddress);
281 }
282
283 return true;
284}