2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011-2013 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/
34 #include "SLCommandHandler.h"
36 #include "lib/platform/util/timeutils.h"
37 #include "lib/devices/CECBusDevice.h"
38 #include "lib/devices/CECPlaybackDevice.h"
39 #include "lib/CECProcessor.h"
40 #include "lib/LibCEC.h"
43 using namespace PLATFORM
;
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_INIT 0x01
52 #define SL_COMMAND_ACK_INIT 0x02
53 #define SL_COMMAND_POWER_ON 0x03
54 #define SL_COMMAND_CONNECT_REQUEST 0x04
55 #define SL_COMMAND_SET_DEVICE_MODE 0x05
56 #define SL_COMMAND_REQUEST_POWER_STATUS 0xa0
58 #define LIB_CEC m_busDevice->GetProcessor()->GetLib()
59 #define ToString(p) LIB_CEC->ToString(p)
61 CSLCommandHandler::CSLCommandHandler(CCECBusDevice
*busDevice
,
62 int32_t iTransmitTimeout
/* = CEC_DEFAULT_TRANSMIT_TIMEOUT */,
63 int32_t iTransmitWait
/* = CEC_DEFAULT_TRANSMIT_WAIT */,
64 int8_t iTransmitRetries
/* = CEC_DEFAULT_TRANSMIT_RETRIES */,
65 int64_t iActiveSourcePending
/* = 0 */) :
66 CCECCommandHandler(busDevice
, iTransmitTimeout
, iTransmitWait
, iTransmitRetries
, iActiveSourcePending
),
69 m_vendorId
= CEC_VENDOR_LG
;
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
)
90 CCECBusDevice
*primary
= m_processor
->GetPrimaryDevice();
91 if (primary
&& primary
->GetLogicalAddress() != CECDEVICE_UNREGISTERED
)
93 /* imitate LG devices */
94 if (m_busDevice
->GetLogicalAddress() != primary
->GetLogicalAddress())
96 primary
->SetVendorId(CEC_VENDOR_LG
);
97 primary
->ReplaceHandler(false);
104 int CSLCommandHandler::HandleVendorCommand(const cec_command
&command
)
106 if (!m_processor
->IsHandledByLibCEC(command
.destination
))
109 if (command
.parameters
.size
== 1 &&
110 command
.parameters
[0] == SL_COMMAND_INIT
)
112 HandleVendorCommandSLInit(command
);
113 return COMMAND_HANDLED
;
115 else if (command
.parameters
.size
== 2 &&
116 command
.parameters
[0] == SL_COMMAND_POWER_ON
)
118 HandleVendorCommandPowerOn(command
);
119 return COMMAND_HANDLED
;
121 else if (command
.parameters
.size
== 2 &&
122 command
.parameters
[0] == SL_COMMAND_CONNECT_REQUEST
)
124 HandleVendorCommandSLConnect(command
);
125 return COMMAND_HANDLED
;
127 else if (command
.parameters
.size
== 1 &&
128 command
.parameters
[0] == SL_COMMAND_REQUEST_POWER_STATUS
)
130 HandleVendorCommandPowerOnStatus(command
);
131 return COMMAND_HANDLED
;
134 return CCECCommandHandler::HandleVendorCommand(command
);
137 void CSLCommandHandler::HandleVendorCommandSLInit(const cec_command
&command
)
139 CCECBusDevice
* dev
= m_processor
->GetDevice(command
.destination
);
140 if (dev
&& dev
->IsHandledByLibCEC())
142 if (!dev
->IsActiveSource())
144 dev
->SetPowerStatus(CEC_POWER_STATUS_STANDBY
);
145 dev
->TransmitPowerState(command
.initiator
, true);
148 TransmitVendorCommandSLAckInit(command
.destination
, command
.initiator
);
152 void CSLCommandHandler::TransmitVendorCommandSLAckInit(const cec_logical_address iSource
, const cec_logical_address iDestination
)
154 cec_command response
;
155 cec_command::Format(response
, iSource
, iDestination
, CEC_OPCODE_VENDOR_COMMAND
);
156 response
.PushBack(SL_COMMAND_ACK_INIT
);
157 response
.PushBack(SL_COMMAND_TYPE_HDDRECORDER
);
159 Transmit(response
, false, true);
163 void CSLCommandHandler::HandleVendorCommandPowerOn(const cec_command
&command
)
165 if (command
.initiator
!= CECDEVICE_TV
)
168 CCECBusDevice
*device
= m_processor
->GetPrimaryDevice();
172 device
->MarkAsActiveSource();
173 device
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
174 device
->TransmitPowerState(command
.initiator
, true);
177 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
178 device
->TransmitPowerState(command
.initiator
, false);
179 device
->TransmitPhysicalAddress(false);
181 if (device
->IsActiveSource())
185 void CSLCommandHandler::HandleVendorCommandPowerOnStatus(const cec_command
&command
)
187 if (command
.destination
!= CECDEVICE_BROADCAST
)
189 CCECBusDevice
*device
= m_processor
->GetPrimaryDevice();
192 device
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
193 device
->TransmitPowerState(command
.initiator
, true);
194 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
199 void CSLCommandHandler::HandleVendorCommandSLConnect(const cec_command
&command
)
202 TransmitVendorCommandSetDeviceMode(command
.destination
, command
.initiator
, CEC_DEVICE_TYPE_RECORDING_DEVICE
);
205 if (m_processor
->IsActiveSource(command
.destination
) && m_processor
->IsHandledByLibCEC(command
.destination
))
207 CCECBusDevice
* dev
= m_processor
->GetDevice(command
.destination
);
208 CCECPlaybackDevice
* pb
= dev
->AsPlaybackDevice();
210 pb
->TransmitDeckStatus(command
.initiator
, true);
211 dev
->TransmitPowerState(command
.initiator
, true);
215 void CSLCommandHandler::TransmitVendorCommandSetDeviceMode(const cec_logical_address iSource
, const cec_logical_address iDestination
, const cec_device_type type
)
217 cec_command response
;
218 cec_command::Format(response
, iSource
, iDestination
, CEC_OPCODE_VENDOR_COMMAND
);
219 response
.PushBack(SL_COMMAND_SET_DEVICE_MODE
);
220 response
.PushBack((uint8_t)type
);
221 Transmit(response
, false, true);
224 int CSLCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
226 if (!m_processor
->CECInitialised() ||
227 !m_processor
->IsHandledByLibCEC(command
.destination
))
228 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
230 CCECPlaybackDevice
*device
= CCECBusDevice::AsPlaybackDevice(GetDevice(command
.destination
));
231 if (!device
|| command
.parameters
.size
== 0)
232 return CEC_ABORT_REASON_INVALID_OPERAND
;
234 device
->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG
);
235 if (command
.parameters
[0] == CEC_STATUS_REQUEST_ON
)
237 device
->TransmitDeckStatus(command
.initiator
, true);
239 return COMMAND_HANDLED
;
241 else if (command
.parameters
[0] == CEC_STATUS_REQUEST_ONCE
)
243 device
->TransmitDeckStatus(command
.initiator
, true);
244 return COMMAND_HANDLED
;
247 return CCECCommandHandler::HandleGiveDeckStatus(command
);
250 int CSLCommandHandler::HandleGiveDevicePowerStatus(const cec_command
&command
)
252 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
) && command
.initiator
== CECDEVICE_TV
)
254 CCECBusDevice
*device
= GetDevice(command
.destination
);
255 if (device
&& device
->GetCurrentPowerStatus() != CEC_POWER_STATUS_ON
)
257 device
->TransmitPowerState(command
.initiator
, true);
258 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
262 if (m_resetPowerState
.IsSet() && m_resetPowerState
.TimeLeft() > 0)
264 /* TODO assume that we've bugged out. the return button no longer works after this */
265 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");
266 device
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
267 device
->TransmitPowerState(command
.initiator
, true);
268 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
269 m_resetPowerState
.Init(5000);
273 device
->TransmitPowerState(command
.initiator
, true);
274 m_resetPowerState
.Init(5000);
278 return COMMAND_HANDLED
;
281 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
284 int CSLCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
286 if (m_processor
->CECInitialised())
288 if (!SLInitialised())
289 TransmitVendorCommandSLAckInit(m_processor
->GetPrimaryDevice()->GetLogicalAddress(), command
.initiator
);
290 CCECCommandHandler::HandleRequestActiveSource(command
);
292 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
295 int CSLCommandHandler::HandleFeatureAbort(const cec_command
&command
)
297 CCECBusDevice
* primary
= m_processor
->GetPrimaryDevice();
298 if (command
.parameters
.size
== 0 && primary
->GetLogicalAddress() != CECDEVICE_UNKNOWN
&&
299 primary
->GetCurrentPowerStatus() == CEC_POWER_STATUS_ON
&& !SLInitialised() &&
300 command
.initiator
== CECDEVICE_TV
)
302 if (!SLInitialised() && m_processor
->IsActiveSource(command
.destination
))
304 TransmitVendorCommandSLAckInit(command
.destination
, command
.initiator
);
305 return COMMAND_HANDLED
;
309 return CCECCommandHandler::HandleFeatureAbort(command
);
312 int CSLCommandHandler::HandleStandby(const cec_command
&command
)
316 return CCECCommandHandler::HandleStandby(command
);
319 void CSLCommandHandler::ResetSLState(void)
321 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "resetting SL initialised state");
322 CLockObject
lock(m_SLMutex
);
323 m_bSLEnabled
= false;
324 m_processor
->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
327 void CSLCommandHandler::SetSLInitialised(void)
329 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "SL initialised");
330 CLockObject
lock(m_SLMutex
);
334 bool CSLCommandHandler::SLInitialised(void)
336 CLockObject
lock(m_SLMutex
);
340 bool CSLCommandHandler::PowerOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
342 if (iDestination
!= CECDEVICE_TV
)
344 /* LG devices only allow themselves to be woken up by the TV with a vendor command */
348 TransmitVendorID(CECDEVICE_TV
, iDestination
, CEC_VENDOR_LG
, false);
350 cec_command::Format(command
, CECDEVICE_TV
, iDestination
, CEC_OPCODE_VENDOR_COMMAND
);
351 command
.PushBack(SL_COMMAND_POWER_ON
);
353 return Transmit(command
, false, false);
356 return CCECCommandHandler::PowerOn(iInitiator
, iDestination
);
359 bool CSLCommandHandler::ActivateSource(bool bTransmitDelayedCommandsOnly
/* = false */)
361 if (m_busDevice
->IsActiveSource() &&
362 m_busDevice
->IsHandledByLibCEC())
365 CLockObject
lock(m_mutex
);
366 // check if we need to send a delayed source switch
367 if (bTransmitDelayedCommandsOnly
)
369 if (m_iActiveSourcePending
== 0 || GetTimeMs() < m_iActiveSourcePending
)
373 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "transmitting delayed activate source command");
378 CCECPlaybackDevice
*device
= m_busDevice
->AsPlaybackDevice();
380 device
->SetDeckStatus(!device
->IsActiveSource() ? CEC_DECK_INFO_OTHER_STATUS
: CEC_DECK_INFO_OTHER_STATUS_LG
);
383 CCECBusDevice
* tv
= m_processor
->GetDevice(CECDEVICE_TV
);
384 bool bTvPresent
= (tv
&& tv
->GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
385 bool bActiveSourceFailed(false);
388 bActiveSourceFailed
= !device
->TransmitImageViewOn();
392 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "TV not present, not sending 'image view on'");
395 // check if we're allowed to switch sources
396 bool bSourceSwitchAllowed
= SourceSwitchAllowed();
397 if (!bSourceSwitchAllowed
)
398 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "source switch is currently not allowed by command handler");
400 // switch sources (if allowed)
401 if (!bActiveSourceFailed
&& bSourceSwitchAllowed
)
403 bActiveSourceFailed
= !m_busDevice
->TransmitActiveSource(false);
407 if (bActiveSourceFailed
|| !bSourceSwitchAllowed
)
409 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "failed to make '%s' the active source. will retry later", m_busDevice
->GetLogicalAddressName());
410 int64_t now(GetTimeMs());
411 CLockObject
lock(m_mutex
);
412 if (m_iActiveSourcePending
== 0 || m_iActiveSourcePending
< now
)
413 m_iActiveSourcePending
= now
+ (int64_t)CEC_ACTIVE_SOURCE_SWITCH_RETRY_TIME_MS
;
418 CLockObject
lock(m_mutex
);
419 // clear previous pending active source command
420 m_iActiveSourcePending
= 0;
423 // mark the handler as initialised
424 CLockObject
lock(m_mutex
);
425 m_bHandlerInited
= true;