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