Merge branch 'master' into release
[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 "env.h"
34 #include "SLCommandHandler.h"
35
36 #include "lib/platform/util/timeutils.h"
37 #include "lib/devices/CECBusDevice.h"
38 #include "lib/devices/CECPlaybackDevice.h"
39 #include "lib/CECProcessor.h"
40 #include "lib/LibCEC.h"
41
42 using namespace CEC;
43 using namespace PLATFORM;
44
45 #define SL_COMMAND_UNKNOWN_01 0x01
46 #define SL_COMMAND_UNKNOWN_02 0x02
47
48 #define SL_COMMAND_TYPE_HDDRECORDER_DISC 0x01
49 #define SL_COMMAND_TYPE_VCR 0x02
50 #define SL_COMMAND_TYPE_DVDPLAYER 0x03
51 #define SL_COMMAND_TYPE_HDDRECORDER_DISC2 0x04
52 #define SL_COMMAND_TYPE_HDDRECORDER 0x05
53
54 #define SL_COMMAND_REQUEST_POWER_STATUS 0xa0
55 #define SL_COMMAND_POWER_ON 0x03
56 #define SL_COMMAND_CONNECT_REQUEST 0x04
57 #define SL_COMMAND_SET_DEVICE_MODE 0x05
58
59 #define LIB_CEC m_busDevice->GetProcessor()->GetLib()
60 #define ToString(p) LIB_CEC->ToString(p)
61
62 CSLCommandHandler::CSLCommandHandler(CCECBusDevice *busDevice,
63 int32_t iTransmitTimeout /* = CEC_DEFAULT_TRANSMIT_TIMEOUT */,
64 int32_t iTransmitWait /* = CEC_DEFAULT_TRANSMIT_WAIT */,
65 int8_t iTransmitRetries /* = CEC_DEFAULT_TRANSMIT_RETRIES */,
66 int64_t iActiveSourcePending /* = 0 */) :
67 CCECCommandHandler(busDevice, iTransmitTimeout, iTransmitWait, iTransmitRetries, iActiveSourcePending),
68 m_bSLEnabled(false),
69 m_bActiveSourceSent(false)
70 {
71 m_vendorId = CEC_VENDOR_LG;
72
73 /* LG devices don't always reply to CEC version requests, so just set it to 1.3a */
74 m_busDevice->SetCecVersion(CEC_VERSION_1_3A);
75
76 /* LG devices always return "korean" as language */
77 cec_menu_language lang;
78 lang.device = m_busDevice->GetLogicalAddress();
79 snprintf(lang.language, 4, "eng");
80 m_busDevice->SetMenuLanguage(lang);
81 }
82
83 bool CSLCommandHandler::InitHandler(void)
84 {
85 if (m_bHandlerInited)
86 return true;
87 m_bHandlerInited = true;
88
89 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
90 if (primary && primary->GetLogicalAddress() != CECDEVICE_UNREGISTERED)
91 {
92 /* imitate LG devices */
93 if (m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress())
94 {
95 primary->SetVendorId(CEC_VENDOR_LG);
96 primary->ReplaceHandler(false);
97 }
98
99 if (m_busDevice->GetLogicalAddress() == CECDEVICE_TV)
100 {
101 /* start as 'in transition standby->on' */
102 primary->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
103 primary->TransmitPowerState(CECDEVICE_TV, false);
104
105 /* send the vendor id */
106 primary->TransmitVendorID(CECDEVICE_BROADCAST, false, false);
107 }
108 }
109
110 return true;
111 }
112
113 int CSLCommandHandler::HandleActiveSource(const cec_command &command)
114 {
115 if (command.parameters.size == 2)
116 {
117 uint16_t iAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
118 CCECBusDevice *device = m_processor->GetDeviceByPhysicalAddress(iAddress);
119 if (device)
120 device->MarkAsActiveSource();
121
122 {
123 CLockObject lock(m_SLMutex);
124 m_bActiveSourceSent = false;
125 }
126
127 return COMMAND_HANDLED;
128 }
129
130 return CEC_ABORT_REASON_INVALID_OPERAND;
131
132 }
133
134 int CSLCommandHandler::HandleDeviceVendorId(const cec_command &command)
135 {
136 SetVendorId(command);
137
138 if (!SLInitialised() && command.initiator == CECDEVICE_TV)
139 {
140 CCECBusDevice *destination = m_processor->GetDevice(command.destination);
141 if (destination && (destination->GetLogicalAddress() == CECDEVICE_BROADCAST || destination->IsHandledByLibCEC()))
142 {
143 cec_logical_address initiator = destination->GetLogicalAddress();
144 if (initiator == CECDEVICE_BROADCAST)
145 initiator = m_processor->GetPrimaryDevice()->GetLogicalAddress();
146
147 cec_command response;
148 cec_command::Format(response, initiator, command.initiator, CEC_OPCODE_FEATURE_ABORT);
149 Transmit(response, false, true);
150 return COMMAND_HANDLED;
151 }
152 }
153
154 return CCECCommandHandler::HandleDeviceVendorId(command);
155 }
156
157 int CSLCommandHandler::HandleVendorCommand(const cec_command &command)
158 {
159 if (!m_processor->IsHandledByLibCEC(command.destination))
160 return true;
161
162 if (command.parameters.size == 1 &&
163 command.parameters[0] == SL_COMMAND_UNKNOWN_01)
164 {
165 HandleVendorCommand01(command);
166 return COMMAND_HANDLED;
167 }
168 else if (command.parameters.size == 2 &&
169 command.parameters[0] == SL_COMMAND_POWER_ON)
170 {
171 HandleVendorCommandPowerOn(command);
172 return COMMAND_HANDLED;
173 }
174 else if (command.parameters.size == 2 &&
175 command.parameters[0] == SL_COMMAND_CONNECT_REQUEST)
176 {
177 HandleVendorCommandSLConnect(command);
178 return COMMAND_HANDLED;
179 }
180 else if (command.parameters.size == 1 &&
181 command.parameters[0] == SL_COMMAND_REQUEST_POWER_STATUS)
182 {
183 HandleVendorCommandPowerOnStatus(command);
184 return COMMAND_HANDLED;
185 }
186
187 return CCECCommandHandler::HandleVendorCommand(command);
188 }
189
190 void CSLCommandHandler::HandleVendorCommand01(const cec_command &command)
191 {
192 m_processor->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
193 TransmitVendorCommand0205(command.destination, command.initiator);
194 }
195
196 void CSLCommandHandler::TransmitVendorCommand0205(const cec_logical_address iSource, const cec_logical_address iDestination)
197 {
198 cec_command response;
199 cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
200 response.PushBack(SL_COMMAND_UNKNOWN_02);
201 response.PushBack(SL_COMMAND_TYPE_HDDRECORDER);
202
203 Transmit(response, false, true);
204 }
205
206 void CSLCommandHandler::HandleVendorCommandPowerOn(const cec_command &command)
207 {
208 if (command.initiator != CECDEVICE_TV)
209 return;
210
211 CCECBusDevice *device = m_processor->GetPrimaryDevice();
212 if (device)
213 {
214 SetSLInitialised();
215 device->MarkAsActiveSource();
216 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
217 device->TransmitPowerState(command.initiator, true);
218
219 CEvent::Sleep(2000);
220 device->SetPowerStatus(CEC_POWER_STATUS_ON);
221 device->TransmitPowerState(command.initiator, false);
222 device->TransmitPhysicalAddress(false);
223 {
224 CLockObject lock(m_SLMutex);
225 m_bActiveSourceSent = false;
226 }
227 }
228 }
229 void CSLCommandHandler::HandleVendorCommandPowerOnStatus(const cec_command &command)
230 {
231 if (command.destination != CECDEVICE_BROADCAST)
232 {
233 CCECBusDevice *device = m_processor->GetPrimaryDevice();
234 if (device)
235 {
236 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
237 device->TransmitPowerState(command.initiator, true);
238 device->SetPowerStatus(CEC_POWER_STATUS_ON);
239 }
240 }
241 }
242
243 void CSLCommandHandler::HandleVendorCommandSLConnect(const cec_command &command)
244 {
245 SetSLInitialised();
246 TransmitVendorCommandSetDeviceMode(command.destination, command.initiator, CEC_DEVICE_TYPE_RECORDING_DEVICE);
247
248 ActivateSource();
249 }
250
251 void CSLCommandHandler::TransmitVendorCommandSetDeviceMode(const cec_logical_address iSource, const cec_logical_address iDestination, const cec_device_type type)
252 {
253 cec_command response;
254 cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
255 response.PushBack(SL_COMMAND_SET_DEVICE_MODE);
256 response.PushBack((uint8_t)type);
257 Transmit(response, false, true);
258 }
259
260 int CSLCommandHandler::HandleGiveDeckStatus(const cec_command &command)
261 {
262 if (!m_processor->CECInitialised() ||
263 !m_processor->IsHandledByLibCEC(command.destination))
264 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
265
266 CCECPlaybackDevice *device = CCECBusDevice::AsPlaybackDevice(GetDevice(command.destination));
267 if (!device || command.parameters.size == 0)
268 return CEC_ABORT_REASON_INVALID_OPERAND;
269
270 device->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG);
271 if (command.parameters[0] == CEC_STATUS_REQUEST_ON)
272 {
273 device->TransmitDeckStatus(command.initiator, true);
274 if (!ActiveSourceSent())
275 ActivateSource();
276 return COMMAND_HANDLED;
277 }
278 else if (command.parameters[0] == CEC_STATUS_REQUEST_ONCE)
279 {
280 device->TransmitDeckStatus(command.initiator, true);
281 return COMMAND_HANDLED;
282 }
283
284 return CCECCommandHandler::HandleGiveDeckStatus(command);
285 }
286
287 int CSLCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command)
288 {
289 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination) && command.initiator == CECDEVICE_TV)
290 {
291 CCECBusDevice *device = GetDevice(command.destination);
292 if (device && device->GetCurrentPowerStatus() != CEC_POWER_STATUS_ON)
293 {
294 device->TransmitPowerState(command.initiator, true);
295 device->SetPowerStatus(CEC_POWER_STATUS_ON);
296 }
297 else
298 {
299 if (!ActiveSourceSent())
300 {
301 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
302 device->TransmitPowerState(command.initiator, true);
303 ActivateSource();
304 }
305 else if (m_resetPowerState.IsSet() && m_resetPowerState.TimeLeft() > 0)
306 {
307 /* TODO assume that we've bugged out. the return button no longer works after this */
308 LIB_CEC->AddLog(CEC_LOG_WARNING, "FIXME: LG seems to have bugged out. resetting to 'in transition standby to on'. the return button will not work");
309 {
310 CLockObject lock(m_SLMutex);
311 m_bActiveSourceSent = false;
312 }
313 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
314 device->TransmitPowerState(command.initiator, true);
315 device->SetPowerStatus(CEC_POWER_STATUS_ON);
316 m_resetPowerState.Init(5000);
317 }
318 else
319 {
320 device->TransmitPowerState(command.initiator, true);
321 m_resetPowerState.Init(5000);
322 }
323 }
324
325 return COMMAND_HANDLED;
326 }
327
328 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
329 }
330
331 int CSLCommandHandler::HandleRequestActiveSource(const cec_command &command)
332 {
333 if (m_processor->CECInitialised())
334 {
335 if (ActiveSourceSent())
336 LIB_CEC->AddLog(CEC_LOG_DEBUG, ">> %i requests active source, ignored", (uint8_t) command.initiator);
337 else
338 ActivateSource();
339 return COMMAND_HANDLED;
340 }
341 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
342 }
343
344 int CSLCommandHandler::HandleFeatureAbort(const cec_command &command)
345 {
346 if (command.parameters.size == 0 && m_processor->GetPrimaryDevice()->GetCurrentPowerStatus() == CEC_POWER_STATUS_ON && !SLInitialised() &&
347 command.initiator == CECDEVICE_TV)
348 {
349 m_processor->GetPrimaryDevice()->TransmitPowerState(command.initiator, false);
350 m_processor->GetPrimaryDevice()->TransmitVendorID(CECDEVICE_BROADCAST, false, false);
351 }
352
353 return CCECCommandHandler::HandleFeatureAbort(command);
354 }
355
356 int CSLCommandHandler::HandleStandby(const cec_command &command)
357 {
358 if (command.initiator == CECDEVICE_TV)
359 {
360 CLockObject lock(m_SLMutex);
361 m_bActiveSourceSent = false;
362 }
363
364 return CCECCommandHandler::HandleStandby(command);
365 }
366
367 void CSLCommandHandler::ResetSLState(void)
368 {
369 LIB_CEC->AddLog(CEC_LOG_NOTICE, "resetting SL initialised state");
370 CLockObject lock(m_SLMutex);
371 m_bSLEnabled = false;
372 m_bActiveSourceSent = false;
373 m_processor->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
374 }
375
376 void CSLCommandHandler::SetSLInitialised(void)
377 {
378 LIB_CEC->AddLog(CEC_LOG_NOTICE, "SL initialised");
379 CLockObject lock(m_SLMutex);
380 m_bSLEnabled = true;
381 }
382
383 bool CSLCommandHandler::SLInitialised(void)
384 {
385 CLockObject lock(m_SLMutex);
386 return m_bSLEnabled;
387 }
388
389 bool CSLCommandHandler::ActiveSourceSent(void)
390 {
391 CLockObject lock(m_SLMutex);
392 return m_bActiveSourceSent;
393 }
394
395 bool CSLCommandHandler::PowerOn(const cec_logical_address iInitiator, const cec_logical_address iDestination)
396 {
397 if (iDestination != CECDEVICE_TV)
398 {
399 /* LG devices only allow themselves to be woken up by the TV with a vendor command */
400 cec_command command;
401
402 if (!m_bSLEnabled)
403 TransmitVendorID(CECDEVICE_TV, CEC_VENDOR_LG, false);
404
405 cec_command::Format(command, CECDEVICE_TV, iDestination, CEC_OPCODE_VENDOR_COMMAND);
406 command.PushBack(SL_COMMAND_POWER_ON);
407 command.PushBack(0);
408 return Transmit(command, false, false);
409 }
410
411 return CCECCommandHandler::PowerOn(iInitiator, iDestination);
412 }
413
414 void CSLCommandHandler::VendorPreActivateSourceHook(void)
415 {
416 CCECPlaybackDevice *device = m_busDevice->AsPlaybackDevice();
417 if (device)
418 device->SetDeckStatus(!device->IsActiveSource() ? CEC_DECK_INFO_OTHER_STATUS : CEC_DECK_INFO_OTHER_STATUS_LG);
419 }