cec: send feature abort to LG devices when they send a vendor id. start with status...
[deb_libcec.git] / src / lib / implementations / SLCommandHandler.cpp
1 /*
2 * This file is part of the libCEC(R) library.
3 *
4 * libCEC(R) is Copyright (C) 2011-2012 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"
34 #include "../devices/CECBusDevice.h"
35 #include "../devices/CECPlaybackDevice.h"
36 #include "../CECProcessor.h"
37
38 using namespace CEC;
39
40 #define SL_COMMAND_UNKNOWN_01 0x01
41 #define SL_COMMAND_UNKNOWN_02 0x02
42 #define SL_COMMAND_UNKNOWN_03 0x05
43
44 #define SL_COMMAND_REQUEST_POWER_STATUS 0xa0
45 #define SL_COMMAND_POWER_ON 0x03
46 #define SL_COMMAND_CONNECT_REQUEST 0x04
47 #define SL_COMMAND_CONNECT_ACCEPT 0x05
48
49 CSLCommandHandler::CSLCommandHandler(CCECBusDevice *busDevice) :
50 CCECCommandHandler(busDevice),
51 m_bSLEnabled(false),
52 m_bPowerStateReset(false)
53 {
54 m_vendorId = CEC_VENDOR_LG;
55 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
56
57 /* imitate LG devices */
58 if (primary && m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress())
59 primary->SetVendorId(CEC_VENDOR_LG);
60 SetLGDeckStatus();
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);
71 }
72
73 bool 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
84 primary->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
85 return true;
86 }
87
88 bool CSLCommandHandler::ActivateSource(void)
89 {
90 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
91 primary->SetActiveSource();
92 primary->TransmitActiveSource();
93 return true;
94 }
95
96 bool CSLCommandHandler::HandleActiveSource(const cec_command &command)
97 {
98 if (command.parameters.size == 2)
99 {
100 uint16_t iAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
101 if (iAddress != m_busDevice->GetPhysicalAddress(false))
102 m_bSLEnabled = false;
103 return m_processor->SetActiveSource(iAddress);
104 }
105
106 return true;
107 }
108
109 bool CSLCommandHandler::HandleDeviceVendorId(const cec_command &command)
110 {
111 SetVendorId(command);
112
113 cec_command response;
114 cec_command::Format(response, m_processor->GetLogicalAddress(), command.initiator, CEC_OPCODE_FEATURE_ABORT);
115 return Transmit(response);
116 }
117
118 bool CSLCommandHandler::HandleFeatureAbort(const cec_command &command)
119 {
120 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
121 if (primary->GetPowerStatus(false) == CEC_POWER_STATUS_ON && !m_bPowerStateReset && !m_bSLEnabled)
122 {
123 m_bPowerStateReset = true;
124 primary->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
125 }
126
127 return CCECCommandHandler::HandleFeatureAbort(command);
128 }
129
130 bool CSLCommandHandler::HandleGivePhysicalAddress(const cec_command &command)
131 {
132 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
133 {
134 CCECBusDevice *device = GetDevice(command.destination);
135 if (device)
136 return device->TransmitPhysicalAddress();
137 }
138
139 return false;
140 }
141
142 bool CSLCommandHandler::HandleVendorCommand(const cec_command &command)
143 {
144 if (command.parameters.size == 1 &&
145 command.parameters[0] == SL_COMMAND_UNKNOWN_01)
146 {
147 HandleVendorCommand01(command);
148 return true;
149 }
150 else if (command.parameters.size == 2 &&
151 command.parameters[0] == SL_COMMAND_POWER_ON)
152 {
153 HandleVendorCommandPowerOn(command);
154 return true;
155 }
156 else if (command.parameters.size == 2 &&
157 command.parameters[0] == SL_COMMAND_CONNECT_REQUEST)
158 {
159 HandleVendorCommandSLConnect(command);
160 return true;
161 }
162 else if (command.parameters.size == 1 &&
163 command.parameters[0] == SL_COMMAND_REQUEST_POWER_STATUS)
164 {
165 HandleVendorCommandPowerOnStatus(command);
166 return true;
167 }
168
169 return false;
170 }
171
172 void CSLCommandHandler::HandleVendorCommand01(const cec_command &command)
173 {
174 TransmitVendorCommand0205(command.destination, command.initiator);
175 }
176
177 void CSLCommandHandler::TransmitVendorCommand0205(const cec_logical_address iSource, const cec_logical_address iDestination)
178 {
179 cec_command response;
180 cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
181 response.PushBack(SL_COMMAND_UNKNOWN_02);
182 response.PushBack(SL_COMMAND_UNKNOWN_03);
183
184 Transmit(response, false);
185 }
186
187 void CSLCommandHandler::HandleVendorCommandPowerOn(const cec_command &command)
188 {
189 CCECBusDevice *device = m_processor->GetPrimaryDevice();
190 if (device)
191 {
192 m_bSLEnabled = true;
193
194 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); //XXX
195 device->TransmitPowerState(command.initiator);
196 device->SetPowerStatus(CEC_POWER_STATUS_ON);
197
198 SetLGDeckStatus();
199 device->SetActiveSource();
200 TransmitImageViewOn(device->GetLogicalAddress(), command.initiator);
201 }
202 }
203 void CSLCommandHandler::HandleVendorCommandPowerOnStatus(const cec_command &command)
204 {
205 if (command.destination != CECDEVICE_BROADCAST)
206 {
207 CCECBusDevice *device = m_processor->m_busDevices[m_processor->GetLogicalAddresses().primary];
208 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
209 device->TransmitPowerState(command.initiator);
210 device->SetPowerStatus(CEC_POWER_STATUS_ON);
211 }
212 }
213
214 void CSLCommandHandler::HandleVendorCommandSLConnect(const cec_command &command)
215 {
216 m_bSLEnabled = true;
217 SetLGDeckStatus();
218
219 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
220
221 primary->SetActiveSource();
222 TransmitImageViewOn(primary->GetLogicalAddress(), command.initiator);
223 TransmitVendorCommand05(primary->GetLogicalAddress(), command.initiator);
224 }
225
226 void CSLCommandHandler::TransmitVendorCommand05(const cec_logical_address iSource, const cec_logical_address iDestination)
227 {
228 cec_command response;
229 cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
230 response.PushBack(SL_COMMAND_CONNECT_ACCEPT);
231 response.PushBack((uint8_t)iSource);
232 Transmit(response, false);
233 }
234
235 void CSLCommandHandler::SetLGDeckStatus(void)
236 {
237 /* LG TVs only route keypresses when the deck status is set to 0x20 */
238 CCECBusDevice *device = m_processor->GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
239 if (device)
240 ((CCECPlaybackDevice *)device)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG);
241
242 device = m_processor->GetDeviceByType(CEC_DEVICE_TYPE_RECORDING_DEVICE);
243 if (device)
244 ((CCECPlaybackDevice *)device)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG);
245 }