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 TVs don't always reply to CEC version requests, so just set it to 1.3a */
72 if (m_busDevice
->GetLogicalAddress() == CECDEVICE_TV
)
73 m_busDevice
->SetCecVersion(CEC_VERSION_1_3A
);
75 /* LG devices always return "korean" as language */
76 cec_menu_language lang
;
77 lang
.device
= m_busDevice
->GetLogicalAddress();
78 snprintf(lang
.language
, 4, "eng");
79 m_busDevice
->SetMenuLanguage(lang
);
82 bool CSLCommandHandler::InitHandler(void)
86 m_bHandlerInited
= true;
88 if (m_busDevice
->GetLogicalAddress() == CECDEVICE_TV
)
90 CCECBusDevice
*primary
= m_processor
->GetPrimaryDevice();
92 /* start as 'in transition standby->on' */
93 primary
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
94 primary
->TransmitPowerState(CECDEVICE_TV
);
96 /* send the vendor id */
97 primary
->TransmitVendorID(CECDEVICE_BROADCAST
);
103 bool CSLCommandHandler::ActivateSource(void)
105 if (!SLInitialised())
107 CLibCEC::AddLog(CEC_LOG_NOTICE
, "not activating the source until SL has been initialised");
112 CLockObject
lock(m_SLMutex
);
113 if (m_bActiveSourceSent
)
115 m_bActiveSourceSent
= true;
118 CCECBusDevice
*primary
= m_processor
->GetPrimaryDevice();
119 primary
->SetActiveSource();
120 primary
->TransmitImageViewOn();
121 primary
->TransmitActiveSource();
125 bool CSLCommandHandler::HandleActiveSource(const cec_command
&command
)
127 if (command
.parameters
.size
== 2)
129 uint16_t iAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
130 if (iAddress
!= m_processor
->GetPrimaryDevice()->GetPhysicalAddress(false))
134 return m_processor
->SetActiveSource(iAddress
);
140 bool CSLCommandHandler::HandleDeviceVendorId(const cec_command
&command
)
142 SetVendorId(command
);
144 if (!SLInitialised() && command
.initiator
== CECDEVICE_TV
)
146 cec_command response
;
147 cec_command::Format(response
, m_processor
->GetLogicalAddress(), command
.initiator
, CEC_OPCODE_FEATURE_ABORT
);
148 return Transmit(response
);
153 bool CSLCommandHandler::HandleGivePhysicalAddress(const cec_command
&command
)
155 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
157 CCECBusDevice
*device
= GetDevice(command
.destination
);
159 return device
->TransmitPhysicalAddress(); // only the physical address, don't send image view on
165 bool CSLCommandHandler::HandleVendorCommand(const cec_command
&command
)
167 if (!m_busDevice
->MyLogicalAddressContains(command
.destination
))
170 if (command
.parameters
.size
== 1 &&
171 command
.parameters
[0] == SL_COMMAND_UNKNOWN_01
)
173 HandleVendorCommand01(command
);
176 else if (command
.parameters
.size
== 2 &&
177 command
.parameters
[0] == SL_COMMAND_POWER_ON
)
179 HandleVendorCommandPowerOn(command
);
182 else if (command
.parameters
.size
== 2 &&
183 command
.parameters
[0] == SL_COMMAND_CONNECT_REQUEST
)
185 HandleVendorCommandSLConnect(command
);
188 else if (command
.parameters
.size
== 1 &&
189 command
.parameters
[0] == SL_COMMAND_REQUEST_POWER_STATUS
)
191 HandleVendorCommandPowerOnStatus(command
);
198 void CSLCommandHandler::HandleVendorCommand01(const cec_command
&command
)
200 m_processor
->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
201 TransmitVendorCommand0205(command
.destination
, command
.initiator
);
204 void CSLCommandHandler::TransmitVendorCommand0205(const cec_logical_address iSource
, const cec_logical_address iDestination
)
206 cec_command response
;
207 cec_command::Format(response
, iSource
, iDestination
, CEC_OPCODE_VENDOR_COMMAND
);
208 response
.PushBack(SL_COMMAND_UNKNOWN_02
);
209 response
.PushBack(SL_COMMAND_TYPE_HDDRECORDER
);
211 Transmit(response
, false);
214 void CSLCommandHandler::HandleVendorCommandPowerOn(const cec_command
&command
)
216 if (command
.initiator
!= CECDEVICE_TV
)
219 CCECBusDevice
*device
= m_processor
->GetPrimaryDevice();
224 device
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
); //XXX
225 device
->TransmitPowerState(command
.initiator
);
226 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
228 device
->SetActiveSource();
229 TransmitImageViewOn(device
->GetLogicalAddress(), command
.initiator
);
232 void CSLCommandHandler::HandleVendorCommandPowerOnStatus(const cec_command
&command
)
234 if (command
.destination
!= CECDEVICE_BROADCAST
)
236 CCECBusDevice
*device
= m_processor
->m_busDevices
[m_processor
->GetLogicalAddresses().primary
];
237 device
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
238 device
->TransmitPowerState(command
.initiator
);
239 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
243 void CSLCommandHandler::HandleVendorCommandSLConnect(const cec_command
&command
)
246 TransmitVendorCommandSetDeviceMode(m_processor
->GetLogicalAddress(), 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);
260 bool CSLCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
262 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
264 CCECBusDevice
*device
= GetDevice(command
.destination
);
265 if (device
&& (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
))
267 if (command
.parameters
.size
> 0)
269 if (command
.parameters
[0] == CEC_STATUS_REQUEST_ON
)
271 ((CCECPlaybackDevice
*) device
)->SetDeckStatus(CEC_DECK_INFO_STOP
);
272 return ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(command
.initiator
) &&
273 device
->TransmitImageViewOn() &&
274 device
->TransmitPhysicalAddress();
276 else if (command
.parameters
[0] == CEC_STATUS_REQUEST_ONCE
)
278 ((CCECPlaybackDevice
*) device
)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG
);
279 return ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(command
.initiator
);
283 return CCECCommandHandler::HandleGiveDeckStatus(command
);
289 bool CSLCommandHandler::HandleGiveDevicePowerStatus(const cec_command
&command
)
292 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
) && command
.initiator
== CECDEVICE_TV
)
294 CCECBusDevice
*device
= GetDevice(command
.destination
);
295 if (device
&& device
->GetPowerStatus(false) != CEC_POWER_STATUS_ON
)
297 bReturn
= device
->TransmitPowerState(command
.initiator
);
298 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
302 if (!ActiveSourceSent())
304 device
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
305 bReturn
= device
->TransmitPowerState(command
.initiator
);
308 else if (m_resetPowerState
.IsSet() && m_resetPowerState
.TimeLeft() > 0)
310 /* TODO assume that we've bugged out. the return button no longer works after this */
311 CLibCEC::AddLog(CEC_LOG_NOTICE
, "LG seems to have bugged out. resetting to 'in transition standby to on'");
313 CLockObject
lock(m_SLMutex
);
314 m_bActiveSourceSent
= false;
316 device
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
317 bReturn
= device
->TransmitPowerState(command
.initiator
);
318 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
319 m_resetPowerState
.Init(5000);
323 bReturn
= device
->TransmitPowerState(command
.initiator
);
324 m_resetPowerState
.Init(5000);
332 bool CSLCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
334 if (m_processor
->IsRunning())
336 CLibCEC::AddLog(CEC_LOG_DEBUG
, ">> %i requests active source, ignored", (uint8_t) command
.initiator
);
342 bool CSLCommandHandler::HandleFeatureAbort(const cec_command
&command
)
344 if (command
.parameters
.size
== 0 && m_processor
->GetPrimaryDevice()->GetPowerStatus() == CEC_POWER_STATUS_ON
&& !SLInitialised() &&
345 command
.initiator
== CECDEVICE_TV
)
347 m_processor
->GetPrimaryDevice()->TransmitPowerState(command
.initiator
);
348 m_processor
->GetPrimaryDevice()->TransmitVendorID(CECDEVICE_BROADCAST
, false);
351 return CCECCommandHandler::HandleFeatureAbort(command
);
354 bool CSLCommandHandler::HandleStandby(const cec_command
&command
)
356 if (command
.initiator
== CECDEVICE_TV
)
359 return CCECCommandHandler::HandleStandby(command
);
362 void CSLCommandHandler::ResetSLState(void)
364 CLibCEC::AddLog(CEC_LOG_NOTICE
, "resetting SL initialised state");
365 CLockObject
lock(m_SLMutex
);
366 m_bSLEnabled
= false;
367 m_bActiveSourceSent
= false;
368 m_processor
->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
371 void CSLCommandHandler::SetSLInitialised(void)
373 CLibCEC::AddLog(CEC_LOG_NOTICE
, "SL initialised");
374 CLockObject
lock(m_SLMutex
);
378 bool CSLCommandHandler::SLInitialised(void)
380 CLockObject
lock(m_SLMutex
);
384 bool CSLCommandHandler::ActiveSourceSent(void)
386 CLockObject
lock(m_SLMutex
);
387 return m_bActiveSourceSent
;
390 bool CSLCommandHandler::PowerOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
392 if (iDestination
!= CECDEVICE_TV
)
395 cec_command::Format(command
, CECDEVICE_TV
, iDestination
, CEC_OPCODE_VENDOR_COMMAND
);
396 command
.PushBack(SL_COMMAND_POWER_ON
);
398 return Transmit(command
);
401 return CCECCommandHandler::PowerOn(iInitiator
, iDestination
);