cec: fix power on of LG devices
[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
LOK
45#define SL_COMMAND_REQUEST_POWER_STATUS 0xa0
46#define SL_COMMAND_POWER_ON 0x0300
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),
54 m_bSkipNextVendorId(false)
e9de9629
LOK
55{
56}
e54fd7d2
LOK
57
58bool CSLCommandHandler::HandleVendorCommand(const cec_command &command)
59{
60 if (command.parameters.size == 1 &&
11d13a02 61 command.parameters[0] == SL_COMMAND_UNKNOWN_01)
e54fd7d2
LOK
62 {
63 /* enable SL */
64 cec_command response;
0d4c3a7b 65 cec_command::Format(response, command.destination, command.initiator, CEC_OPCODE_VENDOR_COMMAND);
11d13a02
LOK
66 response.PushBack(SL_COMMAND_UNKNOWN_02);
67 response.PushBack(SL_COMMAND_UNKNOWN_03);
e54fd7d2 68
8fa35473 69 Transmit(response);
8d915412 70 return true;
e54fd7d2 71 }
797dd7c4 72 else if (command.parameters.size == 2 &&
11d13a02 73 command.parameters[0] == SL_COMMAND_CONNECT_REQUEST)
9902f4e8 74 {
797dd7c4
LOK
75 /* enable SL */
76 cec_command response;
0d4c3a7b 77 cec_command::Format(response, command.destination, command.initiator, CEC_OPCODE_VENDOR_COMMAND);
11d13a02 78 response.PushBack(SL_COMMAND_CONNECT_ACCEPT);
8b86fb7a 79 response.PushBack((uint8_t)m_busDevice->GetProcessor()->GetLogicalAddresses().primary);
8fa35473 80 Transmit(response);
797dd7c4 81
11d13a02 82 /* set deck status for the playback device */
b4c4ef7d 83 TransmitDeckStatus(command.initiator);
8d915412 84 return true;
9902f4e8
LOK
85 }
86 else if (command.parameters.size == 1 &&
0d4c3a7b 87 command.parameters[0] == SL_COMMAND_REQUEST_POWER_STATUS)
9902f4e8 88 {
0d4c3a7b
LOK
89 if (command.destination != CECDEVICE_BROADCAST)
90 m_busDevice->GetProcessor()->m_busDevices[m_busDevice->GetProcessor()->GetLogicalAddresses().primary]->TransmitPowerState(command.initiator);
8d915412 91 return true;
9902f4e8 92 }
e54fd7d2
LOK
93
94 return false;
95}
96
b4c4ef7d
LOK
97void CSLCommandHandler::TransmitDeckStatus(const cec_logical_address iDestination)
98{
99 /* set deck status for the playback device */
100 CCECBusDevice *primary = m_busDevice->GetProcessor()->m_busDevices[m_busDevice->GetProcessor()->GetLogicalAddresses().primary];
101 if (primary->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || primary->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE)
102 {
103 ((CCECPlaybackDevice *)primary)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG);
104 ((CCECPlaybackDevice *)primary)->TransmitDeckStatus(iDestination);
105 }
106}
107
8d915412
LOK
108bool CSLCommandHandler::TransmitLGVendorId(const cec_logical_address iInitiator, const cec_logical_address iDestination)
109{
110 cec_command response;
0d4c3a7b 111 cec_command::Format(response, iInitiator, iDestination, CEC_OPCODE_DEVICE_VENDOR_ID);
8d915412
LOK
112 response.parameters.PushBack((uint8_t) (((uint64_t)CEC_VENDOR_LG >> 16) & 0xFF));
113 response.parameters.PushBack((uint8_t) (((uint64_t)CEC_VENDOR_LG >> 8) & 0xFF));
114 response.parameters.PushBack((uint8_t) ((uint64_t)CEC_VENDOR_LG & 0xFF));
115
8fa35473 116 Transmit(response);
b4c4ef7d 117 return true;
8d915412
LOK
118}
119
e54fd7d2
LOK
120bool CSLCommandHandler::HandleGiveDeviceVendorId(const cec_command &command)
121{
122 /* imitate LG devices */
123 CCECBusDevice *device = GetDevice(command.destination);
124 if (device)
125 device->SetVendorId(CEC_VENDOR_LG);
126
127 return CCECCommandHandler::HandleGiveDeviceVendorId(command);
128}
129
130bool CSLCommandHandler::HandleCommand(const cec_command &command)
131{
132 bool bHandled(false);
b4c4ef7d
LOK
133 if (m_busDevice->MyLogicalAddressContains(command.destination) ||
134 command.destination == CECDEVICE_BROADCAST)
e54fd7d2
LOK
135 {
136 switch(command.opcode)
137 {
138 case CEC_OPCODE_VENDOR_COMMAND:
139 bHandled = HandleVendorCommand(command);
140 break;
b4c4ef7d 141 case CEC_OPCODE_GIVE_DECK_STATUS:
8d915412 142 {
b4c4ef7d 143 if (command.parameters.size == 1)
8d915412 144 {
b4c4ef7d
LOK
145 if (command.parameters[0] == CEC_STATUS_REQUEST_ONCE ||
146 command.parameters[0] == CEC_STATUS_REQUEST_ON)
147 TransmitDeckStatus(command.initiator);
148 else
149 CCECCommandHandler::HandleCommand(command);
8d915412 150 }
b4c4ef7d 151 bHandled = true;
8d915412
LOK
152 }
153 break;
b4c4ef7d
LOK
154 case CEC_OPCODE_FEATURE_ABORT:
155 {
156 if (!m_bSkipNextVendorId)
157 {
158 m_bSkipNextVendorId = true;
159 TransmitLGVendorId(m_busDevice->GetProcessor()->GetLogicalAddresses().primary, CECDEVICE_BROADCAST);
160 }
45b97de7
LOK
161 else
162 {
163 m_bSkipNextVendorId = false;
164 }
b4c4ef7d
LOK
165 m_bSLEnabled = false;
166 }
167 bHandled = true;
e54fd7d2
LOK
168 default:
169 break;
170 }
171 }
172
173 if (!bHandled)
174 bHandled = CCECCommandHandler::HandleCommand(command);
175
176 return bHandled;
177}
855a3a98 178
855a3a98
LOK
179void CSLCommandHandler::HandlePoll(const cec_logical_address iInitiator, const cec_logical_address iDestination)
180{
181 CCECCommandHandler::HandlePoll(iInitiator, iDestination);
182 m_bAwaitingReceiveFailed = true;
183}
184
185bool CSLCommandHandler::HandleReceiveFailed(void)
186{
187 if (m_bAwaitingReceiveFailed)
188 {
189 m_bAwaitingReceiveFailed = false;
190 return false;
191 }
192
193 return true;
194}
8d915412
LOK
195
196bool CSLCommandHandler::InitHandler(void)
197{
b4c4ef7d
LOK
198 if (m_bSLEnabled)
199 return true;
200 m_bSLEnabled = true;
201
dcd240b2
LOK
202 m_busDevice->GetProcessor()->SetStandardLineTimeout(3);
203 m_busDevice->GetProcessor()->SetRetryLineTimeout(3);
204
ae693aaa
LOK
205 /* increase the number of retries because the tv is keeping the bus busy at times */
206 m_iTransmitWait = 2000;
207 m_iTransmitRetries = 4;
208 m_iTransmitTimeout = 500;
209
b4c4ef7d 210 CCECBusDevice *primary = m_busDevice->GetProcessor()->m_busDevices[m_busDevice->GetProcessor()->GetLogicalAddresses().primary];
d211708b
LOK
211 if (m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress())
212 primary->SetVendorId(CEC_VENDOR_LG, false);
b4c4ef7d 213
37acf382
LOK
214 if (m_busDevice->GetLogicalAddress() == CECDEVICE_TV)
215 {
216 primary->TransmitVendorID(CECDEVICE_TV);
217 primary->TransmitPhysicalAddress();
218
219 cec_command command;
0d4c3a7b 220 cec_command::Format(command, m_busDevice->GetProcessor()->GetLogicalAddresses().primary, CECDEVICE_TV, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
8fa35473 221 Transmit(command);
b4c4ef7d 222
37acf382
LOK
223 /* LG TVs don't always reply to CEC version requests, so just set it to 1.3a */
224 m_busDevice->SetCecVersion(CEC_VERSION_1_3A);
225 }
caaf64d7 226
37acf382
LOK
227 /* LG devices always return "korean" as language */
228 cec_menu_language lang;
229 lang.device = m_busDevice->GetLogicalAddress();
230 snprintf(lang.language, 4, "eng");
231 m_busDevice->SetMenuLanguage(lang);
caaf64d7 232
37acf382
LOK
233 if (m_busDevice->GetLogicalAddress() == CECDEVICE_TV)
234 {
b750a5c3 235 m_busDevice->GetProcessor()->SetActiveSource(m_busDevice->GetProcessor()->GetLogicalAddresses().primary);
37acf382
LOK
236 /* LG TVs only route keypresses when the deck status is set to 0x20 */
237 cec_logical_addresses addr = m_busDevice->GetProcessor()->GetLogicalAddresses();
238 for (uint8_t iPtr = 0; iPtr < 15; iPtr++)
8d915412 239 {
37acf382
LOK
240 CCECBusDevice *device = m_busDevice->GetProcessor()->m_busDevices[iPtr];
241
37acf382 242 if (addr[iPtr])
8d915412 243 {
37acf382
LOK
244 if (device && (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE ||
245 device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE))
246 {
247 ((CCECPlaybackDevice *)device)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG);
248 TransmitDeckStatus(CECDEVICE_TV);
249 }
8d915412
LOK
250 }
251 }
252 }
37acf382 253
8d915412
LOK
254 return true;
255}
0d4c3a7b
LOK
256
257
258bool CSLCommandHandler::TransmitPowerOn(const cec_logical_address iInitiator, const cec_logical_address iDestination)
259{
260 cec_command command;
261 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_VENDOR_COMMAND);
262 command.parameters.PushBack((uint8_t) (((uint16_t)SL_COMMAND_POWER_ON >> 8) & 0xFF));
263 command.parameters.PushBack((uint8_t) ((uint16_t)SL_COMMAND_POWER_ON & 0xFF));
264
265 return Transmit(command);
266}