154452d412161fc9c9a414abdc107986e5af3fcf
[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
39 using namespace CEC;
40
41 #define SL_COMMAND_UNKNOWN_01 0x01
42 #define SL_COMMAND_UNKNOWN_02 0x02
43 #define SL_COMMAND_UNKNOWN_03 0x05
44
45 #define SL_COMMAND_REQUEST_POWER_STATUS 0xa0
46 #define SL_COMMAND_POWER_ON 0x03
47 #define SL_COMMAND_CONNECT_REQUEST 0x04
48 #define SL_COMMAND_CONNECT_ACCEPT 0x05
49
50 CSLCommandHandler::CSLCommandHandler(CCECBusDevice *busDevice) :
51 CCECCommandHandler(busDevice),
52 m_bAwaitingReceiveFailed(false),
53 m_bSLEnabled(false),
54 m_bVendorIdSent(false)
55 {
56 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
57
58 /* imitate LG devices */
59 if (m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress())
60 primary->SetVendorId(CEC_VENDOR_LG, false);
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 void CSLCommandHandler::SetLGDeckStatus(void)
74 {
75 /* LG TVs only route keypresses when the deck status is set to 0x20 */
76 CCECBusDevice *device = m_processor->GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
77 if (device)
78 ((CCECPlaybackDevice *)device)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG);
79
80 device = m_processor->GetDeviceByType(CEC_DEVICE_TYPE_RECORDING_DEVICE);
81 if (device)
82 ((CCECPlaybackDevice *)device)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG);
83 }
84
85 bool CSLCommandHandler::HandleVendorCommand(const cec_command &command)
86 {
87 if (command.parameters.size == 1 &&
88 command.parameters[0] == SL_COMMAND_UNKNOWN_01)
89 {
90 HandleVendorCommand01(command);
91 return true;
92 }
93 else if (command.parameters.size == 2 &&
94 command.parameters[0] == SL_COMMAND_POWER_ON)
95 {
96 HandleVendorCommandPowerOn(command);
97 return true;
98 }
99 else if (command.parameters.size == 2 &&
100 command.parameters[0] == SL_COMMAND_CONNECT_REQUEST)
101 {
102 HandleVendorCommandSLConnect(command);
103 return true;
104 }
105 else if (command.parameters.size == 1 &&
106 command.parameters[0] == SL_COMMAND_REQUEST_POWER_STATUS)
107 {
108 HandleVendorCommandPowerOnStatus(command);
109 return true;
110 }
111
112 return false;
113 }
114
115 void CSLCommandHandler::HandleVendorCommand01(const cec_command &command)
116 {
117 TransmitVendorCommand0205(command.destination, command.initiator);
118 }
119
120 void CSLCommandHandler::TransmitVendorCommand0205(const cec_logical_address iSource, const cec_logical_address iDestination)
121 {
122 cec_command response;
123 cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
124 response.PushBack(SL_COMMAND_UNKNOWN_02);
125 response.PushBack(SL_COMMAND_UNKNOWN_03);
126
127 Transmit(response, false);
128 }
129
130 void CSLCommandHandler::TransmitVendorCommand05(const cec_logical_address iSource, const cec_logical_address iDestination)
131 {
132 m_bSLEnabled = true;
133 cec_command response;
134 cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
135 response.PushBack(SL_COMMAND_CONNECT_ACCEPT);
136 response.PushBack((uint8_t)iSource);
137 Transmit(response, false);
138 }
139
140 void CSLCommandHandler::HandleVendorCommandPowerOn(const cec_command &command)
141 {
142 CCECBusDevice *device = m_processor->GetPrimaryDevice();
143 if (device)
144 {
145 m_bSLEnabled = true;
146 SetLGDeckStatus();
147 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
148 device->TransmitPowerState(command.initiator);
149 device->TransmitVendorID(command.initiator);
150 TransmitPowerOn(device->GetLogicalAddress(), command.initiator);
151 }
152 }
153
154 void CSLCommandHandler::HandleVendorCommandSLConnect(const cec_command &command)
155 {
156 m_bSLEnabled = true;
157 m_processor->m_busDevices[command.initiator]->SetActiveSource();
158 m_processor->m_busDevices[command.destination]->TransmitActiveSource();
159 TransmitVendorCommand05(command.destination, command.initiator);
160 SetLGDeckStatus();
161 }
162
163 void CSLCommandHandler::HandleVendorCommandPowerOnStatus(const cec_command &command)
164 {
165 if (command.destination != CECDEVICE_BROADCAST)
166 {
167 CCECBusDevice *device = m_processor->m_busDevices[m_processor->GetLogicalAddresses().primary];
168 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
169 device->TransmitPowerState(command.initiator);
170 device->SetPowerStatus(CEC_POWER_STATUS_ON);
171 }
172 }
173
174 bool CSLCommandHandler::TransmitLGVendorId(const cec_logical_address iInitiator, const cec_logical_address iDestination)
175 {
176 cec_command response;
177 cec_command::Format(response, iInitiator, iDestination, CEC_OPCODE_DEVICE_VENDOR_ID);
178 response.parameters.PushBack((uint8_t) (((uint64_t)CEC_VENDOR_LG >> 16) & 0xFF));
179 response.parameters.PushBack((uint8_t) (((uint64_t)CEC_VENDOR_LG >> 8) & 0xFF));
180 response.parameters.PushBack((uint8_t) ((uint64_t)CEC_VENDOR_LG & 0xFF));
181
182 Transmit(response, false);
183
184 cec_command::Format(response, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
185 Transmit(response);
186
187 return true;
188 }
189
190 bool CSLCommandHandler::HandleCommand(const cec_command &command)
191 {
192 bool bHandled(false);
193
194 if (m_processor->IsStarted() && (m_busDevice->MyLogicalAddressContains(command.destination) ||
195 command.destination == CECDEVICE_BROADCAST))
196 {
197 switch(command.opcode)
198 {
199 case CEC_OPCODE_VENDOR_COMMAND:
200 bHandled = HandleVendorCommand(command);
201 break;
202 case CEC_OPCODE_FEATURE_ABORT:
203 {
204 if (!m_bVendorIdSent)
205 {
206 m_bVendorIdSent = true;
207 TransmitLGVendorId(m_processor->GetLogicalAddresses().primary, CECDEVICE_BROADCAST);
208 m_processor->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_STANDBY);
209 }
210 }
211 bHandled = true;
212 break;
213 default:
214 break;
215 }
216 }
217
218 if (!bHandled)
219 bHandled = CCECCommandHandler::HandleCommand(command);
220
221 return bHandled;
222 }
223
224 void CSLCommandHandler::HandlePoll(const cec_logical_address iInitiator, const cec_logical_address iDestination)
225 {
226 CCECCommandHandler::HandlePoll(iInitiator, iDestination);
227 m_bAwaitingReceiveFailed = true;
228 }
229
230 bool CSLCommandHandler::HandleReceiveFailed(void)
231 {
232 if (m_bAwaitingReceiveFailed)
233 {
234 m_bAwaitingReceiveFailed = false;
235 return false;
236 }
237
238 return true;
239 }
240
241 bool CSLCommandHandler::InitHandler(void)
242 {
243 if (m_bHandlerInited)
244 return true;
245 m_bHandlerInited = true;
246
247 /* reply with LGs vendor id */
248 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
249 if (m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress())
250 primary->TransmitVendorID(CECDEVICE_TV, false);
251
252 return true;
253 }
254
255 bool CSLCommandHandler::TransmitPowerOn(const cec_logical_address iInitiator, const cec_logical_address iDestination)
256 {
257 if (iDestination != CECDEVICE_BROADCAST &&
258 iDestination != CECDEVICE_TV &&
259 m_processor->m_busDevices[iDestination]->GetVendorId(false) == CEC_VENDOR_LG)
260 {
261 cec_command command;
262 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_VENDOR_COMMAND);
263 command.parameters.PushBack((uint8_t)SL_COMMAND_POWER_ON);
264 command.parameters.PushBack(0x00);
265 return Transmit(command, false);
266 }
267
268 return CCECCommandHandler::TransmitImageViewOn(iInitiator, iDestination);
269 }
270
271 bool CSLCommandHandler::HandleActiveSource(const cec_command &command)
272 {
273 if (command.parameters.size == 2)
274 {
275 uint16_t iAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
276 if (iAddress == 0)
277 return m_processor->GetPrimaryDevice()->TransmitPhysicalAddress();
278 else
279 return m_processor->SetActiveSource(iAddress);
280 }
281
282 return true;
283 }