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 CSLCommandHandler::CSLCommandHandler(CCECBusDevice
*busDevice
) :
57 CCECCommandHandler(busDevice
),
59 m_bActiveSourceSent(false)
61 m_vendorId
= CEC_VENDOR_LG
;
62 CCECBusDevice
*primary
= m_processor
->GetPrimaryDevice();
64 /* imitate LG devices */
65 if (primary
&& m_busDevice
->GetLogicalAddress() != primary
->GetLogicalAddress())
67 primary
->SetVendorId(CEC_VENDOR_LG
);
68 primary
->ReplaceHandler(false);
71 /* LG devices don't always reply to CEC version requests, so just set it to 1.3a */
72 m_busDevice
->SetCecVersion(CEC_VERSION_1_3A
);
74 /* LG devices always return "korean" as language */
75 cec_menu_language lang
;
76 lang
.device
= m_busDevice
->GetLogicalAddress();
77 snprintf(lang
.language
, 4, "eng");
78 m_busDevice
->SetMenuLanguage(lang
);
81 bool CSLCommandHandler::InitHandler(void)
85 m_bHandlerInited
= true;
87 if (m_busDevice
->GetLogicalAddress() == CECDEVICE_TV
)
89 CCECBusDevice
*primary
= m_processor
->GetPrimaryDevice();
91 /* start as 'in transition standby->on' */
92 primary
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
93 primary
->TransmitPowerState(CECDEVICE_TV
);
95 /* send the vendor id */
96 primary
->TransmitVendorID(CECDEVICE_BROADCAST
);
102 bool CSLCommandHandler::ActivateSource(void)
104 if (!m_processor
->GetPrimaryDevice()->IsActiveSource())
106 CLibCEC::AddLog(CEC_LOG_NOTICE
, "not activating the source because we're not marked as active");
111 CLockObject
lock(m_SLMutex
);
112 m_bActiveSourceSent
= true;
115 CCECBusDevice
*primary
= m_processor
->GetPrimaryDevice();
116 primary
->SetActiveSource();
117 primary
->SetPowerStatus(CEC_POWER_STATUS_ON
);
118 primary
->TransmitPowerState(CECDEVICE_TV
);
119 primary
->TransmitImageViewOn();
120 primary
->TransmitActiveSource();
124 bool CSLCommandHandler::HandleActiveSource(const cec_command
&command
)
126 if (command
.parameters
.size
== 2)
128 uint16_t iAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
129 CCECBusDevice
*primary
= m_processor
->GetPrimaryDevice();
130 bool bSendPowerOffState(iAddress
!= primary
->GetPhysicalAddress(false) && primary
->IsActiveSource());
132 m_processor
->SetActiveSource(iAddress
);
133 if (bSendPowerOffState
)
136 CLockObject
lock(m_SLMutex
);
137 m_bActiveSourceSent
= false;
139 primary
->TransmitPowerState(CECDEVICE_TV
);
147 bool CSLCommandHandler::HandleDeviceVendorId(const cec_command
&command
)
149 SetVendorId(command
);
151 if (!SLInitialised() && command
.initiator
== CECDEVICE_TV
)
153 cec_command response
;
154 cec_command::Format(response
, m_processor
->GetLogicalAddress(), command
.initiator
, CEC_OPCODE_FEATURE_ABORT
);
155 return Transmit(response
);
160 bool CSLCommandHandler::HandleGivePhysicalAddress(const cec_command
&command
)
162 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
164 CCECBusDevice
*device
= GetDevice(command
.destination
);
166 return device
->TransmitPhysicalAddress(); // only the physical address, don't send image view on
172 bool CSLCommandHandler::HandleVendorCommand(const cec_command
&command
)
174 if (!m_busDevice
->MyLogicalAddressContains(command
.destination
))
177 if (command
.parameters
.size
== 1 &&
178 command
.parameters
[0] == SL_COMMAND_UNKNOWN_01
)
180 HandleVendorCommand01(command
);
183 else if (command
.parameters
.size
== 2 &&
184 command
.parameters
[0] == SL_COMMAND_POWER_ON
)
186 HandleVendorCommandPowerOn(command
);
189 else if (command
.parameters
.size
== 2 &&
190 command
.parameters
[0] == SL_COMMAND_CONNECT_REQUEST
)
192 HandleVendorCommandSLConnect(command
);
195 else if (command
.parameters
.size
== 1 &&
196 command
.parameters
[0] == SL_COMMAND_REQUEST_POWER_STATUS
)
198 HandleVendorCommandPowerOnStatus(command
);
205 void CSLCommandHandler::HandleVendorCommand01(const cec_command
&command
)
207 m_processor
->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
208 TransmitVendorCommand0205(command
.destination
, command
.initiator
);
211 void CSLCommandHandler::TransmitVendorCommand0205(const cec_logical_address iSource
, const cec_logical_address iDestination
)
213 cec_command response
;
214 cec_command::Format(response
, iSource
, iDestination
, CEC_OPCODE_VENDOR_COMMAND
);
215 response
.PushBack(SL_COMMAND_UNKNOWN_02
);
216 response
.PushBack(SL_COMMAND_TYPE_HDDRECORDER
);
218 Transmit(response
, false);
221 void CSLCommandHandler::HandleVendorCommandPowerOn(const cec_command
&command
)
223 if (command
.initiator
!= CECDEVICE_TV
)
226 CCECBusDevice
*device
= m_processor
->GetPrimaryDevice();
230 device
->SetActiveSource();
231 device
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
232 device
->TransmitPowerState(command
.initiator
);
235 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
236 device
->TransmitPowerState(command
.initiator
);
237 device
->TransmitPhysicalAddress();
239 CLockObject
lock(m_SLMutex
);
240 m_bActiveSourceSent
= false;
244 void CSLCommandHandler::HandleVendorCommandPowerOnStatus(const cec_command
&command
)
246 if (command
.destination
!= CECDEVICE_BROADCAST
)
248 CCECBusDevice
*device
= m_processor
->m_busDevices
[m_processor
->GetLogicalAddresses().primary
];
249 device
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
250 device
->TransmitPowerState(command
.initiator
);
251 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
255 void CSLCommandHandler::HandleVendorCommandSLConnect(const cec_command
&command
)
258 TransmitVendorCommandSetDeviceMode(m_processor
->GetLogicalAddress(), command
.initiator
, CEC_DEVICE_TYPE_RECORDING_DEVICE
);
263 void CSLCommandHandler::TransmitVendorCommandSetDeviceMode(const cec_logical_address iSource
, const cec_logical_address iDestination
, const cec_device_type type
)
265 cec_command response
;
266 cec_command::Format(response
, iSource
, iDestination
, CEC_OPCODE_VENDOR_COMMAND
);
267 response
.PushBack(SL_COMMAND_SET_DEVICE_MODE
);
268 response
.PushBack((uint8_t)type
);
269 Transmit(response
, false);
272 bool CSLCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
274 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
276 CCECBusDevice
*device
= GetDevice(command
.destination
);
277 if (device
&& (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
))
279 if (command
.parameters
.size
> 0)
281 ((CCECPlaybackDevice
*) device
)->SetDeckStatus(!device
->IsActiveSource() ? CEC_DECK_INFO_OTHER_STATUS
: CEC_DECK_INFO_OTHER_STATUS_LG
);
282 if (command
.parameters
[0] == CEC_STATUS_REQUEST_ON
)
284 bool bReturn
= ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(command
.initiator
);
285 if (!ActiveSourceSent())
289 else if (command
.parameters
[0] == CEC_STATUS_REQUEST_ONCE
)
291 return ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(command
.initiator
);
295 return CCECCommandHandler::HandleGiveDeckStatus(command
);
301 bool CSLCommandHandler::HandleGiveDevicePowerStatus(const cec_command
&command
)
304 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
) && command
.initiator
== CECDEVICE_TV
)
306 CCECBusDevice
*device
= GetDevice(command
.destination
);
307 if (device
&& device
->GetPowerStatus(false) != CEC_POWER_STATUS_ON
)
309 bReturn
= device
->TransmitPowerState(command
.initiator
);
310 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
314 if (!ActiveSourceSent())
316 device
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
317 bReturn
= device
->TransmitPowerState(command
.initiator
);
320 else if (m_resetPowerState
.IsSet() && m_resetPowerState
.TimeLeft() > 0)
322 /* TODO assume that we've bugged out. the return button no longer works after this */
323 CLibCEC::AddLog(CEC_LOG_WARNING
, "FIXME: LG seems to have bugged out. resetting to 'in transition standby to on'. the return button will not work");
325 CLockObject
lock(m_SLMutex
);
326 m_bActiveSourceSent
= false;
328 device
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
329 bReturn
= device
->TransmitPowerState(command
.initiator
);
330 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
331 m_resetPowerState
.Init(5000);
335 bReturn
= device
->TransmitPowerState(command
.initiator
);
336 m_resetPowerState
.Init(5000);
344 bool CSLCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
346 if (m_processor
->IsRunning())
348 if (ActiveSourceSent())
349 CLibCEC::AddLog(CEC_LOG_DEBUG
, ">> %i requests active source, ignored", (uint8_t) command
.initiator
);
357 bool CSLCommandHandler::HandleFeatureAbort(const cec_command
&command
)
359 if (command
.parameters
.size
== 0 && m_processor
->GetPrimaryDevice()->GetPowerStatus() == CEC_POWER_STATUS_ON
&& !SLInitialised() &&
360 command
.initiator
== CECDEVICE_TV
)
362 m_processor
->GetPrimaryDevice()->TransmitPowerState(command
.initiator
);
363 m_processor
->GetPrimaryDevice()->TransmitVendorID(CECDEVICE_BROADCAST
, false);
366 return CCECCommandHandler::HandleFeatureAbort(command
);
369 bool CSLCommandHandler::HandleStandby(const cec_command
&command
)
371 if (command
.initiator
== CECDEVICE_TV
)
373 CLockObject
lock(m_SLMutex
);
374 m_bActiveSourceSent
= false;
377 return CCECCommandHandler::HandleStandby(command
);
380 void CSLCommandHandler::ResetSLState(void)
382 CLibCEC::AddLog(CEC_LOG_NOTICE
, "resetting SL initialised state");
383 CLockObject
lock(m_SLMutex
);
384 m_bSLEnabled
= false;
385 m_bActiveSourceSent
= false;
386 m_processor
->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
389 void CSLCommandHandler::SetSLInitialised(void)
391 CLibCEC::AddLog(CEC_LOG_NOTICE
, "SL initialised");
392 CLockObject
lock(m_SLMutex
);
396 bool CSLCommandHandler::SLInitialised(void)
398 CLockObject
lock(m_SLMutex
);
402 bool CSLCommandHandler::ActiveSourceSent(void)
404 CLockObject
lock(m_SLMutex
);
405 return m_bActiveSourceSent
;
408 bool CSLCommandHandler::PowerOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
410 if (iDestination
!= CECDEVICE_TV
)
412 /* LG devices only allow themselves to be woken up by the TV with a vendor command */
414 cec_command::Format(command
, CECDEVICE_TV
, iDestination
, CEC_OPCODE_VENDOR_COMMAND
);
415 command
.PushBack(SL_COMMAND_POWER_ON
);
417 return Transmit(command
);
420 return CCECCommandHandler::PowerOn(iInitiator
, iDestination
);