2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
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.
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.
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.
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
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/
34 #include "SLCommandHandler.h"
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"
43 using namespace PLATFORM
;
45 #define SL_COMMAND_UNKNOWN_01 0x01
46 #define SL_COMMAND_UNKNOWN_02 0x02
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
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
59 #define LIB_CEC m_busDevice->GetProcessor()->GetLib()
60 #define ToString(p) LIB_CEC->ToString(p)
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
),
69 m_bActiveSourceSent(false)
71 m_vendorId
= CEC_VENDOR_LG
;
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
);
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
);
83 bool CSLCommandHandler::InitHandler(void)
87 m_bHandlerInited
= true;
89 CCECBusDevice
*primary
= m_processor
->GetPrimaryDevice();
90 if (primary
&& primary
->GetLogicalAddress() != CECDEVICE_UNREGISTERED
)
92 /* imitate LG devices */
93 if (m_busDevice
->GetLogicalAddress() != primary
->GetLogicalAddress())
95 primary
->SetVendorId(CEC_VENDOR_LG
);
96 primary
->ReplaceHandler(false);
99 if (m_busDevice
->GetLogicalAddress() == CECDEVICE_TV
)
101 /* start as 'in transition standby->on' */
102 primary
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
103 primary
->TransmitPowerState(CECDEVICE_TV
, false);
105 /* send the vendor id */
106 primary
->TransmitVendorID(CECDEVICE_BROADCAST
, false, false);
113 int CSLCommandHandler::HandleActiveSource(const cec_command
&command
)
115 if (command
.parameters
.size
== 2)
117 uint16_t iAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
118 CCECBusDevice
*device
= m_processor
->GetDeviceByPhysicalAddress(iAddress
);
120 device
->MarkAsActiveSource();
123 CLockObject
lock(m_SLMutex
);
124 m_bActiveSourceSent
= false;
127 return COMMAND_HANDLED
;
130 return CEC_ABORT_REASON_INVALID_OPERAND
;
134 int CSLCommandHandler::HandleDeviceVendorId(const cec_command
&command
)
136 SetVendorId(command
);
138 if (!SLInitialised() && command
.initiator
== CECDEVICE_TV
)
140 CCECBusDevice
*destination
= m_processor
->GetDevice(command
.destination
);
141 if (destination
&& (destination
->GetLogicalAddress() == CECDEVICE_BROADCAST
|| destination
->IsHandledByLibCEC()))
143 cec_logical_address initiator
= destination
->GetLogicalAddress();
144 if (initiator
== CECDEVICE_BROADCAST
)
145 initiator
= m_processor
->GetPrimaryDevice()->GetLogicalAddress();
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
;
154 return CCECCommandHandler::HandleDeviceVendorId(command
);
157 int CSLCommandHandler::HandleVendorCommand(const cec_command
&command
)
159 if (!m_processor
->IsHandledByLibCEC(command
.destination
))
162 if (command
.parameters
.size
== 1 &&
163 command
.parameters
[0] == SL_COMMAND_UNKNOWN_01
)
165 HandleVendorCommand01(command
);
166 return COMMAND_HANDLED
;
168 else if (command
.parameters
.size
== 2 &&
169 command
.parameters
[0] == SL_COMMAND_POWER_ON
)
171 HandleVendorCommandPowerOn(command
);
172 return COMMAND_HANDLED
;
174 else if (command
.parameters
.size
== 2 &&
175 command
.parameters
[0] == SL_COMMAND_CONNECT_REQUEST
)
177 HandleVendorCommandSLConnect(command
);
178 return COMMAND_HANDLED
;
180 else if (command
.parameters
.size
== 1 &&
181 command
.parameters
[0] == SL_COMMAND_REQUEST_POWER_STATUS
)
183 HandleVendorCommandPowerOnStatus(command
);
184 return COMMAND_HANDLED
;
187 return CCECCommandHandler::HandleVendorCommand(command
);
190 void CSLCommandHandler::HandleVendorCommand01(const cec_command
&command
)
192 m_processor
->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
193 TransmitVendorCommand0205(command
.destination
, command
.initiator
);
196 void CSLCommandHandler::TransmitVendorCommand0205(const cec_logical_address iSource
, const cec_logical_address iDestination
)
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
);
203 Transmit(response
, false, true);
206 void CSLCommandHandler::HandleVendorCommandPowerOn(const cec_command
&command
)
208 if (command
.initiator
!= CECDEVICE_TV
)
211 CCECBusDevice
*device
= m_processor
->GetPrimaryDevice();
215 device
->MarkAsActiveSource();
216 device
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
217 device
->TransmitPowerState(command
.initiator
, true);
220 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
221 device
->TransmitPowerState(command
.initiator
, false);
222 device
->TransmitPhysicalAddress(false);
224 CLockObject
lock(m_SLMutex
);
225 m_bActiveSourceSent
= false;
229 void CSLCommandHandler::HandleVendorCommandPowerOnStatus(const cec_command
&command
)
231 if (command
.destination
!= CECDEVICE_BROADCAST
)
233 CCECBusDevice
*device
= m_processor
->GetPrimaryDevice();
236 device
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
237 device
->TransmitPowerState(command
.initiator
, true);
238 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
243 void CSLCommandHandler::HandleVendorCommandSLConnect(const cec_command
&command
)
246 TransmitVendorCommandSetDeviceMode(command
.destination
, command
.initiator
, CEC_DEVICE_TYPE_RECORDING_DEVICE
);
251 void CSLCommandHandler::TransmitVendorCommandSetDeviceMode(const cec_logical_address iSource
, const cec_logical_address iDestination
, const cec_device_type type
)
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);
260 int CSLCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
262 if (!m_processor
->CECInitialised() ||
263 !m_processor
->IsHandledByLibCEC(command
.destination
))
264 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
266 CCECPlaybackDevice
*device
= CCECBusDevice::AsPlaybackDevice(GetDevice(command
.destination
));
267 if (!device
|| command
.parameters
.size
== 0)
268 return CEC_ABORT_REASON_INVALID_OPERAND
;
270 device
->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG
);
271 if (command
.parameters
[0] == CEC_STATUS_REQUEST_ON
)
273 device
->TransmitDeckStatus(command
.initiator
, true);
274 if (!ActiveSourceSent())
276 return COMMAND_HANDLED
;
278 else if (command
.parameters
[0] == CEC_STATUS_REQUEST_ONCE
)
280 device
->TransmitDeckStatus(command
.initiator
, true);
281 return COMMAND_HANDLED
;
284 return CCECCommandHandler::HandleGiveDeckStatus(command
);
287 int CSLCommandHandler::HandleGiveDevicePowerStatus(const cec_command
&command
)
289 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
) && command
.initiator
== CECDEVICE_TV
)
291 CCECBusDevice
*device
= GetDevice(command
.destination
);
292 if (device
&& device
->GetCurrentPowerStatus() != CEC_POWER_STATUS_ON
)
294 device
->TransmitPowerState(command
.initiator
, true);
295 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
299 if (!ActiveSourceSent())
301 device
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
302 device
->TransmitPowerState(command
.initiator
, true);
305 else if (m_resetPowerState
.IsSet() && m_resetPowerState
.TimeLeft() > 0)
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");
310 CLockObject
lock(m_SLMutex
);
311 m_bActiveSourceSent
= false;
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);
320 device
->TransmitPowerState(command
.initiator
, true);
321 m_resetPowerState
.Init(5000);
325 return COMMAND_HANDLED
;
328 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
331 int CSLCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
333 if (m_processor
->CECInitialised())
335 if (ActiveSourceSent())
336 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, ">> %i requests active source, ignored", (uint8_t) command
.initiator
);
339 return COMMAND_HANDLED
;
341 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
344 int CSLCommandHandler::HandleFeatureAbort(const cec_command
&command
)
346 if (command
.parameters
.size
== 0 && m_processor
->GetPrimaryDevice()->GetCurrentPowerStatus() == CEC_POWER_STATUS_ON
&& !SLInitialised() &&
347 command
.initiator
== CECDEVICE_TV
)
349 m_processor
->GetPrimaryDevice()->TransmitPowerState(command
.initiator
, false);
350 m_processor
->GetPrimaryDevice()->TransmitVendorID(CECDEVICE_BROADCAST
, false, false);
353 return CCECCommandHandler::HandleFeatureAbort(command
);
356 int CSLCommandHandler::HandleStandby(const cec_command
&command
)
358 if (command
.initiator
== CECDEVICE_TV
)
360 CLockObject
lock(m_SLMutex
);
361 m_bActiveSourceSent
= false;
364 return CCECCommandHandler::HandleStandby(command
);
367 void CSLCommandHandler::ResetSLState(void)
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
);
376 void CSLCommandHandler::SetSLInitialised(void)
378 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "SL initialised");
379 CLockObject
lock(m_SLMutex
);
383 bool CSLCommandHandler::SLInitialised(void)
385 CLockObject
lock(m_SLMutex
);
389 bool CSLCommandHandler::ActiveSourceSent(void)
391 CLockObject
lock(m_SLMutex
);
392 return m_bActiveSourceSent
;
395 bool CSLCommandHandler::PowerOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
397 if (iDestination
!= CECDEVICE_TV
)
399 /* LG devices only allow themselves to be woken up by the TV with a vendor command */
403 TransmitVendorID(CECDEVICE_TV
, CEC_VENDOR_LG
, false);
405 cec_command::Format(command
, CECDEVICE_TV
, iDestination
, CEC_OPCODE_VENDOR_COMMAND
);
406 command
.PushBack(SL_COMMAND_POWER_ON
);
408 return Transmit(command
, false, false);
411 return CCECCommandHandler::PowerOn(iInitiator
, iDestination
);
414 void CSLCommandHandler::VendorPreActivateSourceHook(void)
416 CCECPlaybackDevice
*device
= m_busDevice
->AsPlaybackDevice();
418 device
->SetDeckStatus(!device
->IsActiveSource() ? CEC_DECK_INFO_OTHER_STATUS
: CEC_DECK_INFO_OTHER_STATUS_LG
);