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