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
*primary
= m_processor
->GetPrimaryDevice();
119 bool bSendPowerOffState(iAddress
!= primary
->GetCurrentPhysicalAddress() && primary
->IsActiveSource());
121 CCECBusDevice
*device
= m_processor
->GetDeviceByPhysicalAddress(iAddress
);
123 device
->MarkAsActiveSource();
124 if (bSendPowerOffState
)
127 CLockObject
lock(m_SLMutex
);
128 m_bActiveSourceSent
= false;
130 primary
->TransmitPowerState(CECDEVICE_TV
, false);
133 return COMMAND_HANDLED
;
136 return CEC_ABORT_REASON_INVALID_OPERAND
;
140 int CSLCommandHandler::HandleDeviceVendorId(const cec_command
&command
)
142 SetVendorId(command
);
144 if (!SLInitialised() && command
.initiator
== CECDEVICE_TV
)
146 CCECBusDevice
*destination
= m_processor
->GetDevice(command
.destination
);
147 if (destination
&& (destination
->GetLogicalAddress() == CECDEVICE_BROADCAST
|| destination
->IsHandledByLibCEC()))
149 cec_logical_address initiator
= destination
->GetLogicalAddress();
150 if (initiator
== CECDEVICE_BROADCAST
)
151 initiator
= m_processor
->GetPrimaryDevice()->GetLogicalAddress();
153 cec_command response
;
154 cec_command::Format(response
, initiator
, command
.initiator
, CEC_OPCODE_FEATURE_ABORT
);
155 Transmit(response
, false, true);
156 return COMMAND_HANDLED
;
160 return CCECCommandHandler::HandleDeviceVendorId(command
);
163 int CSLCommandHandler::HandleVendorCommand(const cec_command
&command
)
165 if (!m_processor
->IsHandledByLibCEC(command
.destination
))
168 if (command
.parameters
.size
== 1 &&
169 command
.parameters
[0] == SL_COMMAND_UNKNOWN_01
)
171 HandleVendorCommand01(command
);
172 return COMMAND_HANDLED
;
174 else if (command
.parameters
.size
== 2 &&
175 command
.parameters
[0] == SL_COMMAND_POWER_ON
)
177 HandleVendorCommandPowerOn(command
);
178 return COMMAND_HANDLED
;
180 else if (command
.parameters
.size
== 2 &&
181 command
.parameters
[0] == SL_COMMAND_CONNECT_REQUEST
)
183 HandleVendorCommandSLConnect(command
);
184 return COMMAND_HANDLED
;
186 else if (command
.parameters
.size
== 1 &&
187 command
.parameters
[0] == SL_COMMAND_REQUEST_POWER_STATUS
)
189 HandleVendorCommandPowerOnStatus(command
);
190 return COMMAND_HANDLED
;
193 return CCECCommandHandler::HandleVendorCommand(command
);
196 void CSLCommandHandler::HandleVendorCommand01(const cec_command
&command
)
198 m_processor
->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
199 TransmitVendorCommand0205(command
.destination
, command
.initiator
);
202 void CSLCommandHandler::TransmitVendorCommand0205(const cec_logical_address iSource
, const cec_logical_address iDestination
)
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_TYPE_HDDRECORDER
);
209 Transmit(response
, false, true);
212 void CSLCommandHandler::HandleVendorCommandPowerOn(const cec_command
&command
)
214 if (command
.initiator
!= CECDEVICE_TV
)
217 CCECBusDevice
*device
= m_processor
->GetPrimaryDevice();
221 device
->MarkAsActiveSource();
222 device
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
223 device
->TransmitPowerState(command
.initiator
, true);
226 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
227 device
->TransmitPowerState(command
.initiator
, false);
228 device
->TransmitPhysicalAddress(false);
230 CLockObject
lock(m_SLMutex
);
231 m_bActiveSourceSent
= false;
235 void CSLCommandHandler::HandleVendorCommandPowerOnStatus(const cec_command
&command
)
237 if (command
.destination
!= CECDEVICE_BROADCAST
)
239 CCECBusDevice
*device
= m_processor
->GetPrimaryDevice();
242 device
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
243 device
->TransmitPowerState(command
.initiator
, true);
244 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
249 void CSLCommandHandler::HandleVendorCommandSLConnect(const cec_command
&command
)
252 TransmitVendorCommandSetDeviceMode(command
.destination
, command
.initiator
, CEC_DEVICE_TYPE_RECORDING_DEVICE
);
257 void CSLCommandHandler::TransmitVendorCommandSetDeviceMode(const cec_logical_address iSource
, const cec_logical_address iDestination
, const cec_device_type type
)
259 cec_command response
;
260 cec_command::Format(response
, iSource
, iDestination
, CEC_OPCODE_VENDOR_COMMAND
);
261 response
.PushBack(SL_COMMAND_SET_DEVICE_MODE
);
262 response
.PushBack((uint8_t)type
);
263 Transmit(response
, false, true);
266 int CSLCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
268 if (!m_processor
->CECInitialised() ||
269 !m_processor
->IsHandledByLibCEC(command
.destination
))
270 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
272 CCECPlaybackDevice
*device
= CCECBusDevice::AsPlaybackDevice(GetDevice(command
.destination
));
273 if (!device
|| command
.parameters
.size
== 0)
274 return CEC_ABORT_REASON_INVALID_OPERAND
;
276 device
->SetDeckStatus(!device
->IsActiveSource() ? CEC_DECK_INFO_OTHER_STATUS
: CEC_DECK_INFO_OTHER_STATUS_LG
);
277 if (command
.parameters
[0] == CEC_STATUS_REQUEST_ON
)
279 device
->TransmitDeckStatus(command
.initiator
, true);
280 if (!ActiveSourceSent())
282 return COMMAND_HANDLED
;
284 else if (command
.parameters
[0] == CEC_STATUS_REQUEST_ONCE
)
286 device
->TransmitDeckStatus(command
.initiator
, true);
287 return COMMAND_HANDLED
;
290 return CCECCommandHandler::HandleGiveDeckStatus(command
);
293 int CSLCommandHandler::HandleGiveDevicePowerStatus(const cec_command
&command
)
295 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
) && command
.initiator
== CECDEVICE_TV
)
297 CCECBusDevice
*device
= GetDevice(command
.destination
);
298 if (device
&& device
->GetCurrentPowerStatus() != CEC_POWER_STATUS_ON
)
300 device
->TransmitPowerState(command
.initiator
, true);
301 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
305 if (!ActiveSourceSent())
307 device
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
308 device
->TransmitPowerState(command
.initiator
, true);
311 else if (m_resetPowerState
.IsSet() && m_resetPowerState
.TimeLeft() > 0)
313 /* TODO assume that we've bugged out. the return button no longer works after this */
314 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");
316 CLockObject
lock(m_SLMutex
);
317 m_bActiveSourceSent
= false;
319 device
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
320 device
->TransmitPowerState(command
.initiator
, true);
321 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
322 m_resetPowerState
.Init(5000);
326 device
->TransmitPowerState(command
.initiator
, true);
327 m_resetPowerState
.Init(5000);
331 return COMMAND_HANDLED
;
334 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
337 int CSLCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
339 if (m_processor
->CECInitialised())
341 if (ActiveSourceSent())
342 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, ">> %i requests active source, ignored", (uint8_t) command
.initiator
);
345 return COMMAND_HANDLED
;
347 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
350 int CSLCommandHandler::HandleFeatureAbort(const cec_command
&command
)
352 if (command
.parameters
.size
== 0 && m_processor
->GetPrimaryDevice()->GetCurrentPowerStatus() == CEC_POWER_STATUS_ON
&& !SLInitialised() &&
353 command
.initiator
== CECDEVICE_TV
)
355 m_processor
->GetPrimaryDevice()->TransmitPowerState(command
.initiator
, false);
356 m_processor
->GetPrimaryDevice()->TransmitVendorID(CECDEVICE_BROADCAST
, false, false);
359 return CCECCommandHandler::HandleFeatureAbort(command
);
362 int CSLCommandHandler::HandleStandby(const cec_command
&command
)
364 if (command
.initiator
== CECDEVICE_TV
)
366 CLockObject
lock(m_SLMutex
);
367 m_bActiveSourceSent
= false;
370 return CCECCommandHandler::HandleStandby(command
);
373 void CSLCommandHandler::ResetSLState(void)
375 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "resetting SL initialised state");
376 CLockObject
lock(m_SLMutex
);
377 m_bSLEnabled
= false;
378 m_bActiveSourceSent
= false;
379 m_processor
->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
382 void CSLCommandHandler::SetSLInitialised(void)
384 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "SL initialised");
385 CLockObject
lock(m_SLMutex
);
389 bool CSLCommandHandler::SLInitialised(void)
391 CLockObject
lock(m_SLMutex
);
395 bool CSLCommandHandler::ActiveSourceSent(void)
397 CLockObject
lock(m_SLMutex
);
398 return m_bActiveSourceSent
;
401 bool CSLCommandHandler::PowerOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
403 if (iDestination
!= CECDEVICE_TV
)
405 /* LG devices only allow themselves to be woken up by the TV with a vendor command */
409 TransmitVendorID(CECDEVICE_TV
, CEC_VENDOR_LG
, false);
411 cec_command::Format(command
, CECDEVICE_TV
, iDestination
, CEC_OPCODE_VENDOR_COMMAND
);
412 command
.PushBack(SL_COMMAND_POWER_ON
);
414 return Transmit(command
, false, false);
417 return CCECCommandHandler::PowerOn(iInitiator
, iDestination
);
420 void CSLCommandHandler::VendorPreActivateSourceHook(void)
422 CCECPlaybackDevice
*device
= m_busDevice
->AsPlaybackDevice();
424 device
->SetDeckStatus(!device
->IsActiveSource() ? CEC_DECK_INFO_OTHER_STATUS
: CEC_DECK_INFO_OTHER_STATUS_LG
);