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"
41 #define SL_COMMAND_UNKNOWN_01 0x01
42 #define SL_COMMAND_UNKNOWN_02 0x02
43 #define SL_COMMAND_UNKNOWN_03 0x05
45 #define SL_COMMAND_REQUEST_POWER_STATUS 0xa0
46 #define SL_COMMAND_POWER_ON 0x03
47 #define SL_COMMAND_CONNECT_REQUEST 0x04
48 #define SL_COMMAND_SET_DEVICE_MODE 0x05
50 CSLCommandHandler::CSLCommandHandler(CCECBusDevice
*busDevice
) :
51 CCECCommandHandler(busDevice
),
53 m_bPowerStateReset(false),
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 */
97 m_bVendorIdSent
= true;
98 m_processor
->GetPrimaryDevice()->TransmitVendorID(CECDEVICE_BROADCAST
, false);
103 CLibCEC::AddLog(CEC_LOG_NOTICE
, "not activating the source until SL has been initialised");
107 if (m_bActiveSourceSent
)
109 m_bActiveSourceSent
= true;
111 CCECBusDevice
*primary
= m_processor
->GetPrimaryDevice();
112 primary
->SetActiveSource();
113 primary
->TransmitActiveSource();
117 bool CSLCommandHandler::HandleActiveSource(const cec_command
&command
)
119 if (command
.parameters
.size
== 2)
121 uint16_t iAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
122 if (iAddress
!= m_busDevice
->GetPhysicalAddress(false))
124 CLibCEC::AddLog(CEC_LOG_NOTICE
, "resetting SL initialised state");
125 m_bSLEnabled
= false;
127 return m_processor
->SetActiveSource(iAddress
);
133 bool CSLCommandHandler::HandleDeviceVendorId(const cec_command
&command
)
135 SetVendorId(command
);
139 cec_command response
;
140 cec_command::Format(response
, m_processor
->GetLogicalAddress(), command
.initiator
, CEC_OPCODE_FEATURE_ABORT
);
141 return Transmit(response
);
146 bool CSLCommandHandler::HandleGivePhysicalAddress(const cec_command
&command
)
148 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
150 CCECBusDevice
*device
= GetDevice(command
.destination
);
152 return device
->TransmitPhysicalAddress(); // only the physical address, don't send image view on
158 bool CSLCommandHandler::HandleVendorCommand(const cec_command
&command
)
160 if (command
.parameters
.size
== 1 &&
161 command
.parameters
[0] == SL_COMMAND_UNKNOWN_01
)
163 HandleVendorCommand01(command
);
166 else if (command
.parameters
.size
== 2 &&
167 command
.parameters
[0] == SL_COMMAND_POWER_ON
)
169 HandleVendorCommandPowerOn(command
);
172 else if (command
.parameters
.size
== 2 &&
173 command
.parameters
[0] == SL_COMMAND_CONNECT_REQUEST
)
175 HandleVendorCommandSLConnect(command
);
178 else if (command
.parameters
.size
== 1 &&
179 command
.parameters
[0] == SL_COMMAND_REQUEST_POWER_STATUS
)
181 HandleVendorCommandPowerOnStatus(command
);
188 void CSLCommandHandler::HandleVendorCommand01(const cec_command
&command
)
190 m_processor
->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
191 TransmitVendorCommand0205(command
.destination
, command
.initiator
);
194 void CSLCommandHandler::TransmitVendorCommand0205(const cec_logical_address iSource
, const cec_logical_address iDestination
)
196 cec_command response
;
197 cec_command::Format(response
, iSource
, iDestination
, CEC_OPCODE_VENDOR_COMMAND
);
198 response
.PushBack(SL_COMMAND_UNKNOWN_02
);
199 response
.PushBack(SL_COMMAND_UNKNOWN_03
);
201 Transmit(response
, false);
204 void CSLCommandHandler::HandleVendorCommandPowerOn(const cec_command
&command
)
206 CCECBusDevice
*device
= m_processor
->GetPrimaryDevice();
211 device
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
); //XXX
212 device
->TransmitPowerState(command
.initiator
);
213 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
215 device
->SetActiveSource();
216 TransmitImageViewOn(device
->GetLogicalAddress(), command
.initiator
);
219 void CSLCommandHandler::HandleVendorCommandPowerOnStatus(const cec_command
&command
)
221 if (command
.destination
!= CECDEVICE_BROADCAST
)
223 CCECBusDevice
*device
= m_processor
->m_busDevices
[m_processor
->GetLogicalAddresses().primary
];
224 device
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
225 device
->TransmitPowerState(command
.initiator
);
226 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
230 void CSLCommandHandler::HandleVendorCommandSLConnect(const cec_command
&command
)
233 TransmitVendorCommandSetDeviceMode(m_processor
->GetLogicalAddress(), command
.initiator
, CEC_DEVICE_TYPE_RECORDING_DEVICE
);
238 void CSLCommandHandler::TransmitVendorCommandSetDeviceMode(const cec_logical_address iSource
, const cec_logical_address iDestination
, const cec_device_type type
)
240 cec_command response
;
241 cec_command::Format(response
, iSource
, iDestination
, CEC_OPCODE_VENDOR_COMMAND
);
242 response
.PushBack(SL_COMMAND_SET_DEVICE_MODE
);
243 response
.PushBack((uint8_t)type
);
244 Transmit(response
, false);
247 bool CSLCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
249 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
251 CCECBusDevice
*device
= GetDevice(command
.destination
);
252 if (device
&& (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
))
254 if (command
.parameters
.size
> 0)
256 if (command
.parameters
[0] == CEC_STATUS_REQUEST_ON
)
258 ((CCECPlaybackDevice
*) device
)->SetDeckStatus(CEC_DECK_INFO_STOP
);
259 return ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(command
.initiator
) &&
260 device
->TransmitImageViewOn() &&
261 device
->TransmitPhysicalAddress();
263 else if (command
.parameters
[0] == CEC_STATUS_REQUEST_ONCE
)
265 ((CCECPlaybackDevice
*) device
)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG
);
266 return ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(command
.initiator
);
270 return CCECCommandHandler::HandleGiveDeckStatus(command
);
276 bool CSLCommandHandler::HandleGiveDevicePowerStatus(const cec_command
&command
)
279 if (m_processor
->IsRunning() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
281 CCECBusDevice
*device
= GetDevice(command
.destination
);
282 if (device
&& device
->GetPowerStatus(false) != CEC_POWER_STATUS_ON
)
284 bReturn
= device
->TransmitPowerState(command
.initiator
);
285 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
289 if (!m_bActiveSourceSent
)
291 device
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
292 bReturn
= device
->TransmitPowerState(command
.initiator
);
295 else if (m_resetPowerState
.IsSet() && m_resetPowerState
.TimeLeft() > 0)
297 /* assume that we've bugged out */
298 CLibCEC::AddLog(CEC_LOG_NOTICE
, "LG seems to have bugged out. resetting to 'in transition standby to on'");
299 m_bActiveSourceSent
= false;
300 device
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
301 bReturn
= device
->TransmitPowerState(command
.initiator
);
302 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
303 m_resetPowerState
.Init(5000);
307 bReturn
= device
->TransmitPowerState(command
.initiator
);
308 m_resetPowerState
.Init(5000);
316 bool CSLCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
318 if (m_processor
->IsRunning())
320 CLibCEC::AddLog(CEC_LOG_DEBUG
, ">> %i requests active source, ignored", (uint8_t) command
.initiator
);
326 bool CSLCommandHandler::HandleFeatureAbort(const cec_command
&command
)
328 if (command
.parameters
.size
== 0 && m_processor
->GetPrimaryDevice()->GetPowerStatus() == CEC_POWER_STATUS_ON
&& !m_bSLEnabled
)
330 m_processor
->GetPrimaryDevice()->TransmitPowerState(command
.initiator
);
331 m_processor
->GetPrimaryDevice()->TransmitVendorID(CECDEVICE_BROADCAST
, false);
334 return CCECCommandHandler::HandleFeatureAbort(command
);
337 bool CSLCommandHandler::HandleStandby(const cec_command
&command
)
339 if (command
.initiator
== CECDEVICE_TV
)
341 CLibCEC::AddLog(CEC_LOG_NOTICE
, "resetting SL initialised state");
342 m_bSLEnabled
= false;
343 m_bPowerStateReset
= false;
344 m_bActiveSourceSent
= false;
347 CCECBusDevice
*device
= GetDevice(command
.initiator
);
349 device
->SetPowerStatus(CEC_POWER_STATUS_STANDBY
);