2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011 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 "../platform/timeutils.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_CONNECT_ACCEPT 0x05
50 CSLCommandHandler::CSLCommandHandler(CCECBusDevice
*busDevice
) :
51 CCECCommandHandler(busDevice
),
52 m_bAwaitingReceiveFailed(false),
54 m_bVendorIdSent(false)
58 void CSLCommandHandler::SetLGDeckStatus(void)
60 /* LG TVs only route keypresses when the deck status is set to 0x20 */
61 CCECBusDevice
*device
= m_processor
->GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
63 ((CCECPlaybackDevice
*)device
)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG
);
65 device
= m_processor
->GetDeviceByType(CEC_DEVICE_TYPE_RECORDING_DEVICE
);
67 ((CCECPlaybackDevice
*)device
)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG
);
70 bool CSLCommandHandler::HandleVendorCommand(const cec_command
&command
)
72 if (command
.parameters
.size
== 1 &&
73 command
.parameters
[0] == SL_COMMAND_UNKNOWN_01
)
75 HandleVendorCommand01(command
);
78 else if (command
.parameters
.size
== 2 &&
79 command
.parameters
[0] == SL_COMMAND_POWER_ON
)
81 HandleVendorCommandPowerOn(command
);
84 else if (command
.parameters
.size
== 2 &&
85 command
.parameters
[0] == SL_COMMAND_CONNECT_REQUEST
)
87 HandleVendorCommandSLConnect(command
);
90 else if (command
.parameters
.size
== 1 &&
91 command
.parameters
[0] == SL_COMMAND_REQUEST_POWER_STATUS
)
93 HandleVendorCommandPowerOnStatus(command
);
100 void CSLCommandHandler::HandleVendorCommand01(const cec_command
&command
)
102 TransmitVendorCommand0205(command
.destination
, command
.initiator
);
105 void CSLCommandHandler::TransmitVendorCommand0205(const cec_logical_address iSource
, const cec_logical_address iDestination
)
107 cec_command response
;
108 cec_command::Format(response
, iSource
, iDestination
, CEC_OPCODE_VENDOR_COMMAND
);
109 response
.PushBack(SL_COMMAND_UNKNOWN_02
);
110 response
.PushBack(SL_COMMAND_UNKNOWN_03
);
112 Transmit(response
, false);
115 void CSLCommandHandler::TransmitVendorCommand05(const cec_logical_address iSource
, const cec_logical_address iDestination
)
118 cec_command response
;
119 cec_command::Format(response
, iSource
, iDestination
, CEC_OPCODE_VENDOR_COMMAND
);
120 response
.PushBack(SL_COMMAND_CONNECT_ACCEPT
);
121 response
.PushBack((uint8_t)iSource
);
122 Transmit(response
, false);
125 void CSLCommandHandler::HandleVendorCommandPowerOn(const cec_command
&command
)
127 CCECBusDevice
*device
= m_processor
->GetPrimaryDevice();
132 device
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
133 device
->TransmitPowerState(command
.initiator
);
134 device
->TransmitVendorID(command
.initiator
);
135 TransmitPowerOn(device
->GetLogicalAddress(), command
.initiator
);
139 void CSLCommandHandler::HandleVendorCommandSLConnect(const cec_command
&command
)
142 m_processor
->m_busDevices
[command
.initiator
]->SetActiveSource();
143 m_processor
->m_busDevices
[command
.destination
]->TransmitActiveSource();
144 TransmitVendorCommand05(command
.destination
, command
.initiator
);
148 void CSLCommandHandler::HandleVendorCommandPowerOnStatus(const cec_command
&command
)
150 if (command
.destination
!= CECDEVICE_BROADCAST
)
152 CCECBusDevice
*device
= m_processor
->m_busDevices
[m_processor
->GetLogicalAddresses().primary
];
153 device
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
154 device
->TransmitPowerState(command
.initiator
);
155 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
159 bool CSLCommandHandler::TransmitLGVendorId(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
161 cec_command response
;
162 cec_command::Format(response
, iInitiator
, iDestination
, CEC_OPCODE_DEVICE_VENDOR_ID
);
163 response
.parameters
.PushBack((uint8_t) (((uint64_t)CEC_VENDOR_LG
>> 16) & 0xFF));
164 response
.parameters
.PushBack((uint8_t) (((uint64_t)CEC_VENDOR_LG
>> 8) & 0xFF));
165 response
.parameters
.PushBack((uint8_t) ((uint64_t)CEC_VENDOR_LG
& 0xFF));
167 Transmit(response
, false);
169 cec_command::Format(response
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
175 bool CSLCommandHandler::HandleCommand(const cec_command
&command
)
177 bool bHandled(false);
179 if (m_processor
->IsStarted() && (m_busDevice
->MyLogicalAddressContains(command
.destination
) ||
180 command
.destination
== CECDEVICE_BROADCAST
))
182 switch(command
.opcode
)
184 case CEC_OPCODE_VENDOR_COMMAND
:
185 bHandled
= HandleVendorCommand(command
);
187 case CEC_OPCODE_FEATURE_ABORT
:
189 if (!m_bVendorIdSent
)
191 m_bVendorIdSent
= true;
192 TransmitLGVendorId(m_processor
->GetLogicalAddresses().primary
, CECDEVICE_BROADCAST
);
193 m_processor
->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_STANDBY
);
204 bHandled
= CCECCommandHandler::HandleCommand(command
);
209 void CSLCommandHandler::HandlePoll(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
211 CCECCommandHandler::HandlePoll(iInitiator
, iDestination
);
212 m_bAwaitingReceiveFailed
= true;
215 bool CSLCommandHandler::HandleReceiveFailed(void)
217 if (m_bAwaitingReceiveFailed
)
219 m_bAwaitingReceiveFailed
= false;
226 bool CSLCommandHandler::InitHandler(void)
228 if (m_bHandlerInited
)
230 m_bHandlerInited
= true;
232 CCECBusDevice
*primary
= m_processor
->GetPrimaryDevice();
234 /* imitate LG devices */
235 if (m_busDevice
->GetLogicalAddress() != primary
->GetLogicalAddress())
236 primary
->SetVendorId(CEC_VENDOR_LG
, false);
238 /* LG TVs don't always reply to CEC version requests, so just set it to 1.3a */
239 if (m_busDevice
->GetLogicalAddress() == CECDEVICE_TV
)
240 m_busDevice
->SetCecVersion(CEC_VERSION_1_3A
);
242 /* LG devices always return "korean" as language */
243 cec_menu_language lang
;
244 lang
.device
= m_busDevice
->GetLogicalAddress();
245 snprintf(lang
.language
, 4, "eng");
246 m_busDevice
->SetMenuLanguage(lang
);
248 /* reply with LGs vendor id */
249 if (m_busDevice
->GetLogicalAddress() != primary
->GetLogicalAddress())
250 primary
->TransmitVendorID(CECDEVICE_TV
, false);
255 bool CSLCommandHandler::TransmitPowerOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
257 if (iDestination
!= CECDEVICE_BROADCAST
&&
258 iDestination
!= CECDEVICE_TV
&&
259 m_processor
->m_busDevices
[iDestination
]->GetVendorId(false) == CEC_VENDOR_LG
)
262 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_VENDOR_COMMAND
);
263 command
.parameters
.PushBack((uint8_t)SL_COMMAND_POWER_ON
);
264 command
.parameters
.PushBack(0x00);
265 return Transmit(command
, false);
268 return CCECCommandHandler::TransmitImageViewOn(iInitiator
, iDestination
);
271 bool CSLCommandHandler::HandleActiveSource(const cec_command
&command
)
273 if (command
.parameters
.size
== 2)
275 uint16_t iAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
277 return m_processor
->GetPrimaryDevice()->TransmitPhysicalAddress();
279 return m_processor
->SetActiveSource(iAddress
);