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