290a4876c45b99acf2a396bee02b74710fac1d82
[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 #include "../LibCEC.h"
38
39 using namespace CEC;
40 using namespace PLATFORM;
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_SET_DEVICE_MODE 0x05
50
51 CSLCommandHandler::CSLCommandHandler(CCECBusDevice *busDevice) :
52 CCECCommandHandler(busDevice),
53 m_bSLEnabled(false),
54 m_bActiveSourceSent(false),
55 m_bVendorIdSent(false)
56 {
57 m_vendorId = CEC_VENDOR_LG;
58 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
59
60 /* imitate LG devices */
61 if (primary && m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress())
62 {
63 primary->SetVendorId(CEC_VENDOR_LG);
64 primary->ReplaceHandler(false);
65 }
66
67 /* LG TVs don't always reply to CEC version requests, so just set it to 1.3a */
68 if (m_busDevice->GetLogicalAddress() == CECDEVICE_TV)
69 m_busDevice->SetCecVersion(CEC_VERSION_1_3A);
70
71 /* LG devices always return "korean" as language */
72 cec_menu_language lang;
73 lang.device = m_busDevice->GetLogicalAddress();
74 snprintf(lang.language, 4, "eng");
75 m_busDevice->SetMenuLanguage(lang);
76 }
77
78 bool CSLCommandHandler::InitHandler(void)
79 {
80 if (m_bHandlerInited)
81 return true;
82 m_bHandlerInited = true;
83
84 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
85 if (m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress())
86 {
87 /* start as 'in transition standby->on' */
88 primary->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
89 primary->TransmitPowerState(CECDEVICE_TV);
90 }
91
92 return true;
93 }
94
95 bool CSLCommandHandler::ActivateSource(void)
96 {
97 /* reply with LGs vendor id */
98 bool bSendVendorId(false);
99 {
100 CLockObject lock(m_SLMutex);
101 bSendVendorId = !m_bVendorIdSent;
102 m_bVendorIdSent = true;
103 }
104 if (bSendVendorId)
105 m_processor->GetPrimaryDevice()->TransmitVendorID(CECDEVICE_BROADCAST, false);
106
107 if (!SLInitialised())
108 {
109 CLibCEC::AddLog(CEC_LOG_NOTICE, "not activating the source until SL has been initialised");
110 return true;
111 }
112
113 {
114 CLockObject lock(m_SLMutex);
115 if (m_bActiveSourceSent)
116 return true;
117 m_bActiveSourceSent = true;
118 }
119
120 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
121 primary->SetActiveSource();
122 primary->TransmitActiveSource();
123 return true;
124 }
125
126 bool CSLCommandHandler::HandleActiveSource(const cec_command &command)
127 {
128 if (command.parameters.size == 2)
129 {
130 uint16_t iAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
131 if (iAddress != m_busDevice->GetPhysicalAddress(false))
132 {
133 ResetSLState();
134 }
135 return m_processor->SetActiveSource(iAddress);
136 }
137
138 return true;
139 }
140
141 bool CSLCommandHandler::HandleDeviceVendorId(const cec_command &command)
142 {
143 SetVendorId(command);
144
145 if (!SLInitialised())
146 {
147 cec_command response;
148 cec_command::Format(response, m_processor->GetLogicalAddress(), command.initiator, CEC_OPCODE_FEATURE_ABORT);
149 return Transmit(response);
150 }
151 return true;
152 }
153
154 bool CSLCommandHandler::HandleGivePhysicalAddress(const cec_command &command)
155 {
156 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
157 {
158 CCECBusDevice *device = GetDevice(command.destination);
159 if (device)
160 return device->TransmitPhysicalAddress(); // only the physical address, don't send image view on
161 }
162
163 return false;
164 }
165
166 bool CSLCommandHandler::HandleVendorCommand(const cec_command &command)
167 {
168 if (command.parameters.size == 1 &&
169 command.parameters[0] == SL_COMMAND_UNKNOWN_01)
170 {
171 HandleVendorCommand01(command);
172 return true;
173 }
174 else if (command.parameters.size == 2 &&
175 command.parameters[0] == SL_COMMAND_POWER_ON)
176 {
177 HandleVendorCommandPowerOn(command);
178 return true;
179 }
180 else if (command.parameters.size == 2 &&
181 command.parameters[0] == SL_COMMAND_CONNECT_REQUEST)
182 {
183 HandleVendorCommandSLConnect(command);
184 return true;
185 }
186 else if (command.parameters.size == 1 &&
187 command.parameters[0] == SL_COMMAND_REQUEST_POWER_STATUS)
188 {
189 HandleVendorCommandPowerOnStatus(command);
190 return true;
191 }
192
193 return false;
194 }
195
196 void CSLCommandHandler::HandleVendorCommand01(const cec_command &command)
197 {
198 m_processor->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
199 TransmitVendorCommand0205(command.destination, command.initiator);
200 }
201
202 void CSLCommandHandler::TransmitVendorCommand0205(const cec_logical_address iSource, const cec_logical_address iDestination)
203 {
204 cec_command response;
205 cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
206 response.PushBack(SL_COMMAND_UNKNOWN_02);
207 response.PushBack(SL_COMMAND_UNKNOWN_03);
208
209 Transmit(response, false);
210 }
211
212 void CSLCommandHandler::HandleVendorCommandPowerOn(const cec_command &command)
213 {
214 CCECBusDevice *device = m_processor->GetPrimaryDevice();
215 if (device)
216 {
217 SetSLInitialised();
218
219 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); //XXX
220 device->TransmitPowerState(command.initiator);
221 device->SetPowerStatus(CEC_POWER_STATUS_ON);
222
223 device->SetActiveSource();
224 TransmitImageViewOn(device->GetLogicalAddress(), command.initiator);
225 }
226 }
227 void CSLCommandHandler::HandleVendorCommandPowerOnStatus(const cec_command &command)
228 {
229 if (command.destination != CECDEVICE_BROADCAST)
230 {
231 CCECBusDevice *device = m_processor->m_busDevices[m_processor->GetLogicalAddresses().primary];
232 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
233 device->TransmitPowerState(command.initiator);
234 device->SetPowerStatus(CEC_POWER_STATUS_ON);
235 }
236 }
237
238 void CSLCommandHandler::HandleVendorCommandSLConnect(const cec_command &command)
239 {
240 SetSLInitialised();
241 TransmitVendorCommandSetDeviceMode(m_processor->GetLogicalAddress(), command.initiator, CEC_DEVICE_TYPE_RECORDING_DEVICE);
242
243 ActivateSource();
244 }
245
246 void CSLCommandHandler::TransmitVendorCommandSetDeviceMode(const cec_logical_address iSource, const cec_logical_address iDestination, const cec_device_type type)
247 {
248 cec_command response;
249 cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
250 response.PushBack(SL_COMMAND_SET_DEVICE_MODE);
251 response.PushBack((uint8_t)type);
252 Transmit(response, false);
253 }
254
255 bool CSLCommandHandler::HandleGiveDeckStatus(const cec_command &command)
256 {
257 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
258 {
259 CCECBusDevice *device = GetDevice(command.destination);
260 if (device && (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE))
261 {
262 if (command.parameters.size > 0)
263 {
264 if (command.parameters[0] == CEC_STATUS_REQUEST_ON)
265 {
266 ((CCECPlaybackDevice *) device)->SetDeckStatus(CEC_DECK_INFO_STOP);
267 return ((CCECPlaybackDevice *) device)->TransmitDeckStatus(command.initiator) &&
268 device->TransmitImageViewOn() &&
269 device->TransmitPhysicalAddress();
270 }
271 else if (command.parameters[0] == CEC_STATUS_REQUEST_ONCE)
272 {
273 ((CCECPlaybackDevice *) device)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG);
274 return ((CCECPlaybackDevice *) device)->TransmitDeckStatus(command.initiator);
275 }
276 }
277 }
278 return CCECCommandHandler::HandleGiveDeckStatus(command);
279 }
280
281 return false;
282 }
283
284 bool CSLCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command)
285 {
286 bool bReturn(false);
287 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
288 {
289 CCECBusDevice *device = GetDevice(command.destination);
290 if (device && device->GetPowerStatus(false) != CEC_POWER_STATUS_ON)
291 {
292 bReturn = device->TransmitPowerState(command.initiator);
293 device->SetPowerStatus(CEC_POWER_STATUS_ON);
294 }
295 else
296 {
297 if (!ActiveSourceSent())
298 {
299 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
300 bReturn = device->TransmitPowerState(command.initiator);
301 ActivateSource();
302 }
303 else if (m_resetPowerState.IsSet() && m_resetPowerState.TimeLeft() > 0)
304 {
305 /* assume that we've bugged out */
306 CLibCEC::AddLog(CEC_LOG_NOTICE, "LG seems to have bugged out. resetting to 'in transition standby to on'");
307 {
308 CLockObject lock(m_SLMutex);
309 m_bActiveSourceSent = false;
310 }
311 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
312 bReturn = device->TransmitPowerState(command.initiator);
313 device->SetPowerStatus(CEC_POWER_STATUS_ON);
314 m_resetPowerState.Init(5000);
315 }
316 else
317 {
318 bReturn = device->TransmitPowerState(command.initiator);
319 m_resetPowerState.Init(5000);
320 }
321 }
322 }
323
324 return bReturn;
325 }
326
327 bool CSLCommandHandler::HandleRequestActiveSource(const cec_command &command)
328 {
329 if (m_processor->IsRunning())
330 {
331 CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %i requests active source, ignored", (uint8_t) command.initiator);
332 return true;
333 }
334 return false;
335 }
336
337 bool CSLCommandHandler::HandleFeatureAbort(const cec_command &command)
338 {
339 if (command.parameters.size == 0 && m_processor->GetPrimaryDevice()->GetPowerStatus() == CEC_POWER_STATUS_ON && !SLInitialised())
340 {
341 m_processor->GetPrimaryDevice()->TransmitPowerState(command.initiator);
342 m_processor->GetPrimaryDevice()->TransmitVendorID(CECDEVICE_BROADCAST, false);
343 }
344
345 return CCECCommandHandler::HandleFeatureAbort(command);
346 }
347
348 bool CSLCommandHandler::HandleStandby(const cec_command &command)
349 {
350 if (command.initiator == CECDEVICE_TV)
351 ResetSLState();
352
353 return CCECCommandHandler::HandleStandby(command);
354 }
355
356 void CSLCommandHandler::ResetSLState(void)
357 {
358 CLibCEC::AddLog(CEC_LOG_NOTICE, "resetting SL initialised state");
359 CLockObject lock(m_SLMutex);
360 m_bSLEnabled = false;
361 m_bActiveSourceSent = false;
362 m_processor->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
363 }
364
365 void CSLCommandHandler::SetSLInitialised(void)
366 {
367 CLibCEC::AddLog(CEC_LOG_NOTICE, "SL initialised");
368 CLockObject lock(m_SLMutex);
369 m_bSLEnabled = true;
370 }
371
372 bool CSLCommandHandler::SLInitialised(void)
373 {
374 CLockObject lock(m_SLMutex);
375 return m_bSLEnabled;
376 }
377
378 bool CSLCommandHandler::ActiveSourceSent(void)
379 {
380 CLockObject lock(m_SLMutex);
381 return m_bActiveSourceSent;
382 }