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/
33 #include "SLCommandHandler.h"
34 #include "../devices/CECBusDevice.h"
35 #include "../devices/CECPlaybackDevice.h"
36 #include "../CECProcessor.h"
37 #include "../LibCEC.h"
40 using namespace PLATFORM
;
42 #define SL_COMMAND_UNKNOWN_01 0x01
43 #define SL_COMMAND_UNKNOWN_02 0x02
45 #define SL_COMMAND_TYPE_HDDRECORDER_DISC 0x01
46 #define SL_COMMAND_TYPE_VCR 0x02
47 #define SL_COMMAND_TYPE_DVDPLAYER 0x03
48 #define SL_COMMAND_TYPE_HDDRECORDER_DISC2 0x04
49 #define SL_COMMAND_TYPE_HDDRECORDER 0x05
51 #define SL_COMMAND_REQUEST_POWER_STATUS 0xa0
52 #define SL_COMMAND_POWER_ON 0x03
53 #define SL_COMMAND_CONNECT_REQUEST 0x04
54 #define SL_COMMAND_SET_DEVICE_MODE 0x05
56 #define LIB_CEC m_busDevice->GetProcessor()->GetLib()
57 #define ToString(p) LIB_CEC->ToString(p)
59 CSLCommandHandler::CSLCommandHandler(CCECBusDevice
*busDevice
) :
60 CCECCommandHandler(busDevice
),
62 m_bActiveSourceSent(false)
64 m_vendorId
= CEC_VENDOR_LG
;
66 /* LG devices don't always reply to CEC version requests, so just set it to 1.3a */
67 m_busDevice
->SetCecVersion(CEC_VERSION_1_3A
);
69 /* LG devices always return "korean" as language */
70 cec_menu_language lang
;
71 lang
.device
= m_busDevice
->GetLogicalAddress();
72 snprintf(lang
.language
, 4, "eng");
73 m_busDevice
->SetMenuLanguage(lang
);
76 bool CSLCommandHandler::InitHandler(void)
80 m_bHandlerInited
= true;
82 CCECBusDevice
*primary
= m_processor
->GetPrimaryDevice();
83 if (primary
&& primary
->GetLogicalAddress() != CECDEVICE_UNREGISTERED
)
85 /* imitate LG devices */
86 if (m_busDevice
->GetLogicalAddress() != primary
->GetLogicalAddress())
88 primary
->SetVendorId(CEC_VENDOR_LG
);
89 primary
->ReplaceHandler(false);
92 if (m_busDevice
->GetLogicalAddress() == CECDEVICE_TV
)
94 /* start as 'in transition standby->on' */
95 primary
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
96 primary
->TransmitPowerState(CECDEVICE_TV
);
98 /* send the vendor id */
99 primary
->TransmitVendorID(CECDEVICE_BROADCAST
);
106 bool CSLCommandHandler::HandleActiveSource(const cec_command
&command
)
108 if (command
.parameters
.size
== 2)
110 uint16_t iAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
111 CCECBusDevice
*primary
= m_processor
->GetPrimaryDevice();
112 bool bSendPowerOffState(iAddress
!= primary
->GetCurrentPhysicalAddress() && primary
->IsActiveSource());
114 CCECBusDevice
*device
= m_processor
->GetDeviceByPhysicalAddress(iAddress
);
116 device
->MarkAsActiveSource();
117 if (bSendPowerOffState
)
120 CLockObject
lock(m_SLMutex
);
121 m_bActiveSourceSent
= false;
123 primary
->TransmitPowerState(CECDEVICE_TV
);
131 bool CSLCommandHandler::HandleDeviceVendorId(const cec_command
&command
)
133 SetVendorId(command
);
135 if (!SLInitialised() && command
.initiator
== CECDEVICE_TV
)
137 CCECBusDevice
*destination
= m_processor
->GetDevice(command
.destination
);
138 if (destination
&& (destination
->GetLogicalAddress() == CECDEVICE_BROADCAST
|| destination
->IsHandledByLibCEC()))
140 cec_logical_address initiator
= destination
->GetLogicalAddress();
141 if (initiator
== CECDEVICE_BROADCAST
)
142 initiator
= m_processor
->GetPrimaryDevice()->GetLogicalAddress();
144 cec_command response
;
145 cec_command::Format(response
, initiator
, command
.initiator
, CEC_OPCODE_FEATURE_ABORT
);
146 return Transmit(response
);
152 bool CSLCommandHandler::HandleVendorCommand(const cec_command
&command
)
154 if (!m_processor
->IsHandledByLibCEC(command
.destination
))
157 if (command
.parameters
.size
== 1 &&
158 command
.parameters
[0] == SL_COMMAND_UNKNOWN_01
)
160 HandleVendorCommand01(command
);
163 else if (command
.parameters
.size
== 2 &&
164 command
.parameters
[0] == SL_COMMAND_POWER_ON
)
166 HandleVendorCommandPowerOn(command
);
169 else if (command
.parameters
.size
== 2 &&
170 command
.parameters
[0] == SL_COMMAND_CONNECT_REQUEST
)
172 HandleVendorCommandSLConnect(command
);
175 else if (command
.parameters
.size
== 1 &&
176 command
.parameters
[0] == SL_COMMAND_REQUEST_POWER_STATUS
)
178 HandleVendorCommandPowerOnStatus(command
);
185 void CSLCommandHandler::HandleVendorCommand01(const cec_command
&command
)
187 m_processor
->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
188 TransmitVendorCommand0205(command
.destination
, command
.initiator
);
191 void CSLCommandHandler::TransmitVendorCommand0205(const cec_logical_address iSource
, const cec_logical_address iDestination
)
193 cec_command response
;
194 cec_command::Format(response
, iSource
, iDestination
, CEC_OPCODE_VENDOR_COMMAND
);
195 response
.PushBack(SL_COMMAND_UNKNOWN_02
);
196 response
.PushBack(SL_COMMAND_TYPE_HDDRECORDER
);
201 void CSLCommandHandler::HandleVendorCommandPowerOn(const cec_command
&command
)
203 if (command
.initiator
!= CECDEVICE_TV
)
206 CCECBusDevice
*device
= m_processor
->GetPrimaryDevice();
210 device
->MarkAsActiveSource();
211 device
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
212 device
->TransmitPowerState(command
.initiator
);
215 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
216 device
->TransmitPowerState(command
.initiator
);
217 device
->TransmitPhysicalAddress();
219 CLockObject
lock(m_SLMutex
);
220 m_bActiveSourceSent
= false;
224 void CSLCommandHandler::HandleVendorCommandPowerOnStatus(const cec_command
&command
)
226 if (command
.destination
!= CECDEVICE_BROADCAST
)
228 CCECBusDevice
*device
= m_processor
->GetPrimaryDevice();
231 device
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
232 device
->TransmitPowerState(command
.initiator
);
233 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
238 void CSLCommandHandler::HandleVendorCommandSLConnect(const cec_command
&command
)
241 TransmitVendorCommandSetDeviceMode(command
.destination
, command
.initiator
, CEC_DEVICE_TYPE_RECORDING_DEVICE
);
246 void CSLCommandHandler::TransmitVendorCommandSetDeviceMode(const cec_logical_address iSource
, const cec_logical_address iDestination
, const cec_device_type type
)
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
);
255 bool CSLCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
257 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
259 CCECBusDevice
*device
= GetDevice(command
.destination
);
260 if (device
&& (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
))
262 if (command
.parameters
.size
> 0)
264 ((CCECPlaybackDevice
*) device
)->SetDeckStatus(!device
->IsActiveSource() ? CEC_DECK_INFO_OTHER_STATUS
: CEC_DECK_INFO_OTHER_STATUS_LG
);
265 if (command
.parameters
[0] == CEC_STATUS_REQUEST_ON
)
267 bool bReturn
= ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(command
.initiator
);
268 if (!ActiveSourceSent())
272 else if (command
.parameters
[0] == CEC_STATUS_REQUEST_ONCE
)
274 return ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(command
.initiator
);
278 return CCECCommandHandler::HandleGiveDeckStatus(command
);
284 bool CSLCommandHandler::HandleGiveDevicePowerStatus(const cec_command
&command
)
287 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
) && command
.initiator
== CECDEVICE_TV
)
289 CCECBusDevice
*device
= GetDevice(command
.destination
);
290 if (device
&& device
->GetCurrentPowerStatus() != CEC_POWER_STATUS_ON
)
292 bReturn
= device
->TransmitPowerState(command
.initiator
);
293 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
297 if (!ActiveSourceSent())
299 device
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
300 bReturn
= device
->TransmitPowerState(command
.initiator
);
303 else if (m_resetPowerState
.IsSet() && m_resetPowerState
.TimeLeft() > 0)
305 /* TODO assume that we've bugged out. the return button no longer works after this */
306 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");
308 CLockObject
lock(m_SLMutex
);
309 m_bActiveSourceSent
= false;
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);
318 bReturn
= device
->TransmitPowerState(command
.initiator
);
319 m_resetPowerState
.Init(5000);
327 bool CSLCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
329 if (m_processor
->CECInitialised())
331 if (ActiveSourceSent())
332 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, ">> %i requests active source, ignored", (uint8_t) command
.initiator
);
340 bool CSLCommandHandler::HandleFeatureAbort(const cec_command
&command
)
342 if (command
.parameters
.size
== 0 && m_processor
->GetPrimaryDevice()->GetCurrentPowerStatus() == CEC_POWER_STATUS_ON
&& !SLInitialised() &&
343 command
.initiator
== CECDEVICE_TV
)
345 m_processor
->GetPrimaryDevice()->TransmitPowerState(command
.initiator
);
346 m_processor
->GetPrimaryDevice()->TransmitVendorID(CECDEVICE_BROADCAST
, false);
349 return CCECCommandHandler::HandleFeatureAbort(command
);
352 bool CSLCommandHandler::HandleStandby(const cec_command
&command
)
354 if (command
.initiator
== CECDEVICE_TV
)
356 CLockObject
lock(m_SLMutex
);
357 m_bActiveSourceSent
= false;
360 return CCECCommandHandler::HandleStandby(command
);
363 void CSLCommandHandler::ResetSLState(void)
365 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "resetting SL initialised state");
366 CLockObject
lock(m_SLMutex
);
367 m_bSLEnabled
= false;
368 m_bActiveSourceSent
= false;
369 m_processor
->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
372 void CSLCommandHandler::SetSLInitialised(void)
374 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "SL initialised");
375 CLockObject
lock(m_SLMutex
);
379 bool CSLCommandHandler::SLInitialised(void)
381 CLockObject
lock(m_SLMutex
);
385 bool CSLCommandHandler::ActiveSourceSent(void)
387 CLockObject
lock(m_SLMutex
);
388 return m_bActiveSourceSent
;
391 bool CSLCommandHandler::PowerOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
393 if (iDestination
!= CECDEVICE_TV
)
395 /* LG devices only allow themselves to be woken up by the TV with a vendor command */
399 TransmitVendorID(CECDEVICE_TV
, CEC_VENDOR_LG
);
401 cec_command::Format(command
, CECDEVICE_TV
, iDestination
, CEC_OPCODE_VENDOR_COMMAND
);
402 command
.PushBack(SL_COMMAND_POWER_ON
);
404 return Transmit(command
);
407 return CCECCommandHandler::PowerOn(iInitiator
, iDestination
);