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)
56 m_processor
->m_busDevices
[m_processor
->GetLogicalAddresses().primary
]->m_powerStatus
= CEC_POWER_STATUS_STANDBY
;
59 bool CSLCommandHandler::HandleVendorCommand(const cec_command
&command
)
61 if (command
.parameters
.size
== 1 &&
62 command
.parameters
[0] == SL_COMMAND_UNKNOWN_01
)
64 HandleVendorCommand01(command
);
67 else if (command
.parameters
.size
== 2 &&
68 command
.parameters
[0] == SL_COMMAND_POWER_ON
)
70 HandleVendorCommand03(command
);
73 else if (command
.parameters
.size
== 2 &&
74 command
.parameters
[0] == SL_COMMAND_CONNECT_REQUEST
)
76 HandleVendorCommand04(command
);
79 else if (command
.parameters
.size
== 1 &&
80 command
.parameters
[0] == SL_COMMAND_REQUEST_POWER_STATUS
)
82 HandleVendorCommandA0(command
);
89 bool CSLCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
91 if (command
.parameters
.size
== 1)
93 if (command
.parameters
[0] == CEC_STATUS_REQUEST_ONCE
||
94 command
.parameters
[0] == CEC_STATUS_REQUEST_ON
)
96 TransmitDeckStatus(command
.initiator
);
100 CCECCommandHandler::HandleGiveDeckStatus(command
);
106 void CSLCommandHandler::HandleVendorCommand01(const cec_command
&command
)
108 TransmitVendorCommand0205(command
.destination
, command
.initiator
);
111 void CSLCommandHandler::TransmitVendorCommand0205(const cec_logical_address iSource
, const cec_logical_address iDestination
)
113 cec_command response
;
114 cec_command::Format(response
, iSource
, iDestination
, CEC_OPCODE_VENDOR_COMMAND
);
115 response
.PushBack(SL_COMMAND_UNKNOWN_02
);
116 response
.PushBack(SL_COMMAND_UNKNOWN_03
);
121 void CSLCommandHandler::TransmitVendorCommand04(const cec_logical_address iSource
, const cec_logical_address iDestination
)
124 cec_command response
;
125 cec_command::Format(response
, iSource
, iDestination
, CEC_OPCODE_VENDOR_COMMAND
);
126 response
.PushBack(SL_COMMAND_CONNECT_ACCEPT
);
127 response
.PushBack((uint8_t)iSource
);
131 void CSLCommandHandler::HandleVendorCommand03(const cec_command
&command
)
133 CCECBusDevice
*device
= m_processor
->m_busDevices
[m_processor
->GetLogicalAddresses().primary
];
137 device
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
138 device
->TransmitPowerState(command
.initiator
);
139 device
->TransmitVendorID(command
.initiator
);
140 TransmitPowerOn(device
->GetLogicalAddress(), command
.initiator
);
144 void CSLCommandHandler::HandleVendorCommand04(const cec_command
&command
)
147 TransmitVendorCommand04(command
.destination
, command
.initiator
);
148 TransmitDeckStatus(command
.initiator
);
151 void CSLCommandHandler::HandleVendorCommandA0(const cec_command
&command
)
153 if (command
.destination
!= CECDEVICE_BROADCAST
)
155 CCECBusDevice
*device
= m_processor
->m_busDevices
[m_processor
->GetLogicalAddresses().primary
];
156 device
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
157 device
->TransmitPowerState(command
.initiator
);
158 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
162 void CSLCommandHandler::TransmitDeckStatus(const cec_logical_address iDestination
)
164 /* set deck status for the playback device */
165 CCECBusDevice
*primary
= m_processor
->m_busDevices
[m_processor
->GetLogicalAddresses().primary
];
166 if (primary
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| primary
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
168 ((CCECPlaybackDevice
*)primary
)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG
);
169 ((CCECPlaybackDevice
*)primary
)->TransmitDeckStatus(iDestination
);
173 bool CSLCommandHandler::TransmitLGVendorId(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
175 cec_command response
;
176 cec_command::Format(response
, iInitiator
, iDestination
, CEC_OPCODE_DEVICE_VENDOR_ID
);
177 response
.parameters
.PushBack((uint8_t) (((uint64_t)CEC_VENDOR_LG
>> 16) & 0xFF));
178 response
.parameters
.PushBack((uint8_t) (((uint64_t)CEC_VENDOR_LG
>> 8) & 0xFF));
179 response
.parameters
.PushBack((uint8_t) ((uint64_t)CEC_VENDOR_LG
& 0xFF));
185 bool CSLCommandHandler::HandleGiveDeviceVendorId(const cec_command
&command
)
187 /* imitate LG devices */
188 CCECBusDevice
*device
= GetDevice(command
.destination
);
190 device
->SetVendorId(CEC_VENDOR_LG
);
192 return CCECCommandHandler::HandleGiveDeviceVendorId(command
);
195 bool CSLCommandHandler::HandleCommand(const cec_command
&command
)
197 bool bHandled(false);
199 if (m_processor
->IsStarted() && (m_busDevice
->MyLogicalAddressContains(command
.destination
) ||
200 command
.destination
== CECDEVICE_BROADCAST
))
202 switch(command
.opcode
)
204 case CEC_OPCODE_VENDOR_COMMAND
:
205 bHandled
= HandleVendorCommand(command
);
207 case CEC_OPCODE_FEATURE_ABORT
:
209 if (!m_bVendorIdSent
)
211 m_bVendorIdSent
= true;
212 TransmitLGVendorId(m_processor
->GetLogicalAddresses().primary
, CECDEVICE_BROADCAST
);
222 bHandled
= CCECCommandHandler::HandleCommand(command
);
227 void CSLCommandHandler::HandlePoll(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
229 CCECCommandHandler::HandlePoll(iInitiator
, iDestination
);
230 m_bAwaitingReceiveFailed
= true;
233 bool CSLCommandHandler::HandleReceiveFailed(void)
235 if (m_bAwaitingReceiveFailed
)
237 m_bAwaitingReceiveFailed
= false;
244 bool CSLCommandHandler::InitHandler(void)
246 m_processor
->SetStandardLineTimeout(3);
247 m_processor
->SetRetryLineTimeout(3);
249 /* increase the number of retries because the tv is keeping the bus busy at times */
250 m_iTransmitWait
= 2000;
251 m_iTransmitRetries
= 4;
252 m_iTransmitTimeout
= 500;
254 CCECBusDevice
*primary
= m_processor
->m_busDevices
[m_processor
->GetLogicalAddresses().primary
];
255 if (m_busDevice
->GetLogicalAddress() != primary
->GetLogicalAddress())
256 primary
->SetVendorId(CEC_VENDOR_LG
, false);
258 if (m_busDevice
->GetLogicalAddress() == CECDEVICE_TV
)
260 /* LG TVs don't always reply to CEC version requests, so just set it to 1.3a */
261 m_busDevice
->SetCecVersion(CEC_VERSION_1_3A
);
264 /* LG devices always return "korean" as language */
265 cec_menu_language lang
;
266 lang
.device
= m_busDevice
->GetLogicalAddress();
267 snprintf(lang
.language
, 4, "eng");
268 m_busDevice
->SetMenuLanguage(lang
);
270 if (m_busDevice
->GetLogicalAddress() == CECDEVICE_TV
)
272 m_processor
->SetActiveSource();
274 /* LG TVs only route keypresses when the deck status is set to 0x20 */
275 cec_logical_addresses addr
= m_processor
->GetLogicalAddresses();
276 for (uint8_t iPtr
= 0; iPtr
< 15; iPtr
++)
278 CCECBusDevice
*device
= m_processor
->m_busDevices
[iPtr
];
282 if (device
&& (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
||
283 device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
))
285 ((CCECPlaybackDevice
*)device
)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG
);
294 bool CSLCommandHandler::TransmitPowerOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
296 if (iDestination
!= CECDEVICE_BROADCAST
&&
297 iDestination
!= CECDEVICE_TV
&&
298 m_processor
->m_busDevices
[iDestination
]->GetVendorId(false) == CEC_VENDOR_LG
)
301 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_VENDOR_COMMAND
);
302 command
.parameters
.PushBack((uint8_t)SL_COMMAND_POWER_ON
);
303 command
.parameters
.PushBack(0x00);
304 return Transmit(command
);
307 return CCECCommandHandler::TransmitPowerOn(iInitiator
, iDestination
);