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
44 #define SL_COMMAND_UNKNOWN_03 0x05
46 #define SL_COMMAND_REQUEST_POWER_STATUS 0xa0
47 #define SL_COMMAND_POWER_ON 0x03
48 #define SL_COMMAND_CONNECT_REQUEST 0x04
49 #define SL_COMMAND_SET_DEVICE_MODE 0x05
51 CSLCommandHandler::CSLCommandHandler(CCECBusDevice
*busDevice
) :
52 CCECCommandHandler(busDevice
),
54 m_bActiveSourceSent(false),
55 m_bVendorIdSent(false)
57 m_vendorId
= CEC_VENDOR_LG
;
58 CCECBusDevice
*primary
= m_processor
->GetPrimaryDevice();
60 /* imitate LG devices */
61 if (primary
&& m_busDevice
->GetLogicalAddress() != primary
->GetLogicalAddress())
62 primary
->SetVendorId(CEC_VENDOR_LG
);
64 /* LG TVs don't always reply to CEC version requests, so just set it to 1.3a */
65 if (m_busDevice
->GetLogicalAddress() == CECDEVICE_TV
)
66 m_busDevice
->SetCecVersion(CEC_VERSION_1_3A
);
68 /* LG devices always return "korean" as language */
69 cec_menu_language lang
;
70 lang
.device
= m_busDevice
->GetLogicalAddress();
71 snprintf(lang
.language
, 4, "eng");
72 m_busDevice
->SetMenuLanguage(lang
);
75 bool CSLCommandHandler::InitHandler(void)
79 m_bHandlerInited
= true;
81 CCECBusDevice
*primary
= m_processor
->GetPrimaryDevice();
82 if (m_busDevice
->GetLogicalAddress() != primary
->GetLogicalAddress())
84 /* start as 'in transition standby->on' */
85 primary
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
86 primary
->TransmitPowerState(CECDEVICE_TV
);
92 bool CSLCommandHandler::ActivateSource(void)
94 /* reply with LGs vendor id */
95 bool bSendVendorId(false);
97 CLockObject
lock(m_SLMutex
);
98 bSendVendorId
= !m_bVendorIdSent
;
99 m_bVendorIdSent
= true;
102 m_processor
->GetPrimaryDevice()->TransmitVendorID(CECDEVICE_BROADCAST
, false);
104 if (!SLInitialised())
106 CLibCEC::AddLog(CEC_LOG_NOTICE
, "not activating the source until SL has been initialised");
111 CLockObject
lock(m_SLMutex
);
112 if (m_bActiveSourceSent
)
114 m_bActiveSourceSent
= true;
117 CCECBusDevice
*primary
= m_processor
->GetPrimaryDevice();
118 primary
->SetActiveSource();
119 primary
->TransmitActiveSource();
123 bool CSLCommandHandler::HandleActiveSource(const cec_command
&command
)
125 if (command
.parameters
.size
== 2)
127 uint16_t iAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
128 if (iAddress
!= m_busDevice
->GetPhysicalAddress(false))
132 return m_processor
->SetActiveSource(iAddress
);
138 bool CSLCommandHandler::HandleDeviceVendorId(const cec_command
&command
)
140 SetVendorId(command
);
142 if (!SLInitialised())
144 cec_command response
;
145 cec_command::Format(response
, m_processor
->GetLogicalAddress(), command
.initiator
, CEC_OPCODE_FEATURE_ABORT
);
146 return Transmit(response
);
151 bool CSLCommandHandler::HandleGivePhysicalAddress(const cec_command
&command
)
153 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
155 CCECBusDevice
*device
= GetDevice(command
.destination
);
157 return device
->TransmitPhysicalAddress(); // only the physical address, don't send image view on
163 bool CSLCommandHandler::HandleVendorCommand(const cec_command
&command
)
165 if (command
.parameters
.size
== 1 &&
166 command
.parameters
[0] == SL_COMMAND_UNKNOWN_01
)
168 HandleVendorCommand01(command
);
171 else if (command
.parameters
.size
== 2 &&
172 command
.parameters
[0] == SL_COMMAND_POWER_ON
)
174 HandleVendorCommandPowerOn(command
);
177 else if (command
.parameters
.size
== 2 &&
178 command
.parameters
[0] == SL_COMMAND_CONNECT_REQUEST
)
180 HandleVendorCommandSLConnect(command
);
183 else if (command
.parameters
.size
== 1 &&
184 command
.parameters
[0] == SL_COMMAND_REQUEST_POWER_STATUS
)
186 HandleVendorCommandPowerOnStatus(command
);
193 void CSLCommandHandler::HandleVendorCommand01(const cec_command
&command
)
195 m_processor
->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
196 TransmitVendorCommand0205(command
.destination
, command
.initiator
);
199 void CSLCommandHandler::TransmitVendorCommand0205(const cec_logical_address iSource
, const cec_logical_address iDestination
)
201 cec_command response
;
202 cec_command::Format(response
, iSource
, iDestination
, CEC_OPCODE_VENDOR_COMMAND
);
203 response
.PushBack(SL_COMMAND_UNKNOWN_02
);
204 response
.PushBack(SL_COMMAND_UNKNOWN_03
);
206 Transmit(response
, false);
209 void CSLCommandHandler::HandleVendorCommandPowerOn(const cec_command
&command
)
211 CCECBusDevice
*device
= m_processor
->GetPrimaryDevice();
216 device
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
); //XXX
217 device
->TransmitPowerState(command
.initiator
);
218 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
220 device
->SetActiveSource();
221 TransmitImageViewOn(device
->GetLogicalAddress(), command
.initiator
);
224 void CSLCommandHandler::HandleVendorCommandPowerOnStatus(const cec_command
&command
)
226 if (command
.destination
!= CECDEVICE_BROADCAST
)
228 CCECBusDevice
*device
= m_processor
->m_busDevices
[m_processor
->GetLogicalAddresses().primary
];
229 device
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
230 device
->TransmitPowerState(command
.initiator
);
231 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
235 void CSLCommandHandler::HandleVendorCommandSLConnect(const cec_command
&command
)
238 TransmitVendorCommandSetDeviceMode(m_processor
->GetLogicalAddress(), command
.initiator
, CEC_DEVICE_TYPE_RECORDING_DEVICE
);
243 void CSLCommandHandler::TransmitVendorCommandSetDeviceMode(const cec_logical_address iSource
, const cec_logical_address iDestination
, const cec_device_type type
)
245 cec_command response
;
246 cec_command::Format(response
, iSource
, iDestination
, CEC_OPCODE_VENDOR_COMMAND
);
247 response
.PushBack(SL_COMMAND_SET_DEVICE_MODE
);
248 response
.PushBack((uint8_t)type
);
249 Transmit(response
, false);
252 bool CSLCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
254 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
256 CCECBusDevice
*device
= GetDevice(command
.destination
);
257 if (device
&& (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
))
259 if (command
.parameters
.size
> 0)
261 if (command
.parameters
[0] == CEC_STATUS_REQUEST_ON
)
263 ((CCECPlaybackDevice
*) device
)->SetDeckStatus(CEC_DECK_INFO_STOP
);
264 return ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(command
.initiator
) &&
265 device
->TransmitImageViewOn() &&
266 device
->TransmitPhysicalAddress();
268 else if (command
.parameters
[0] == CEC_STATUS_REQUEST_ONCE
)
270 ((CCECPlaybackDevice
*) device
)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG
);
271 return ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(command
.initiator
);
275 return CCECCommandHandler::HandleGiveDeckStatus(command
);
281 bool CSLCommandHandler::HandleGiveDevicePowerStatus(const cec_command
&command
)
284 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
286 CCECBusDevice
*device
= GetDevice(command
.destination
);
287 if (device
&& device
->GetPowerStatus(false) != CEC_POWER_STATUS_ON
)
289 bReturn
= device
->TransmitPowerState(command
.initiator
);
290 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
294 if (!ActiveSourceSent())
296 device
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
297 bReturn
= device
->TransmitPowerState(command
.initiator
);
300 else if (m_resetPowerState
.IsSet() && m_resetPowerState
.TimeLeft() > 0)
302 /* assume that we've bugged out */
303 CLibCEC::AddLog(CEC_LOG_NOTICE
, "LG seems to have bugged out. resetting to 'in transition standby to on'");
305 CLockObject
lock(m_SLMutex
);
306 m_bActiveSourceSent
= false;
308 device
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
309 bReturn
= device
->TransmitPowerState(command
.initiator
);
310 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
311 m_resetPowerState
.Init(5000);
315 bReturn
= device
->TransmitPowerState(command
.initiator
);
316 m_resetPowerState
.Init(5000);
324 bool CSLCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
326 if (m_processor
->IsRunning())
328 CLibCEC::AddLog(CEC_LOG_DEBUG
, ">> %i requests active source, ignored", (uint8_t) command
.initiator
);
334 bool CSLCommandHandler::HandleFeatureAbort(const cec_command
&command
)
336 if (command
.parameters
.size
== 0 && m_processor
->GetPrimaryDevice()->GetPowerStatus() == CEC_POWER_STATUS_ON
&& !SLInitialised())
338 m_processor
->GetPrimaryDevice()->TransmitPowerState(command
.initiator
);
339 m_processor
->GetPrimaryDevice()->TransmitVendorID(CECDEVICE_BROADCAST
, false);
342 return CCECCommandHandler::HandleFeatureAbort(command
);
345 bool CSLCommandHandler::HandleStandby(const cec_command
&command
)
347 if (command
.initiator
== CECDEVICE_TV
)
350 return CCECCommandHandler::HandleStandby(command
);
353 void CSLCommandHandler::ResetSLState(void)
355 CLibCEC::AddLog(CEC_LOG_NOTICE
, "resetting SL initialised state");
356 CLockObject
lock(m_SLMutex
);
357 m_bSLEnabled
= false;
358 m_bActiveSourceSent
= false;
359 m_processor
->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
362 void CSLCommandHandler::SetSLInitialised(void)
364 CLibCEC::AddLog(CEC_LOG_NOTICE
, "SL initialised");
365 CLockObject
lock(m_SLMutex
);
369 bool CSLCommandHandler::SLInitialised(void)
371 CLockObject
lock(m_SLMutex
);
375 bool CSLCommandHandler::ActiveSourceSent(void)
377 CLockObject
lock(m_SLMutex
);
378 return m_bActiveSourceSent
;