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_VENDOR_ID 0xa0
46 #define SL_COMMAND_CONNECT_REQUEST 0x04
47 #define SL_COMMAND_CONNECT_ACCEPT 0x05
49 CSLCommandHandler::CSLCommandHandler(CCECBusDevice
*busDevice
) :
50 CCECCommandHandler(busDevice
),
51 m_bAwaitingReceiveFailed(false),
53 m_bSkipNextVendorId(false)
57 bool CSLCommandHandler::HandleVendorCommand(const cec_command
&command
)
59 if (command
.parameters
.size
== 1 &&
60 command
.parameters
[0] == SL_COMMAND_UNKNOWN_01
)
64 cec_command::Format(response
, command
.destination
, command
.initiator
, CEC_OPCODE_VENDOR_COMMAND
, m_iTransmitTimeout
);
65 response
.PushBack(SL_COMMAND_UNKNOWN_02
);
66 response
.PushBack(SL_COMMAND_UNKNOWN_03
);
71 else if (command
.parameters
.size
== 2 &&
72 command
.parameters
[0] == SL_COMMAND_CONNECT_REQUEST
)
76 cec_command::Format(response
, command
.destination
, command
.initiator
, CEC_OPCODE_VENDOR_COMMAND
, m_iTransmitTimeout
);
77 response
.PushBack(SL_COMMAND_CONNECT_ACCEPT
);
78 response
.PushBack(m_busDevice
->GetProcessor()->GetLogicalAddresses().primary
);
81 /* set deck status for the playback device */
82 TransmitDeckStatus(command
.initiator
);
85 else if (command
.parameters
.size
== 1 &&
86 command
.parameters
[0] == SL_COMMAND_REQUEST_VENDOR_ID
)
88 // if (command.destination != CECDEVICE_BROADCAST)
89 // m_busDevice->GetProcessor()->m_busDevices[m_busDevice->GetProcessor()->GetLogicalAddresses().primary]->TransmitPowerState(command.initiator);
96 void CSLCommandHandler::TransmitDeckStatus(const cec_logical_address iDestination
)
98 /* set deck status for the playback device */
99 CCECBusDevice
*primary
= m_busDevice
->GetProcessor()->m_busDevices
[m_busDevice
->GetProcessor()->GetLogicalAddresses().primary
];
100 if (primary
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| primary
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
102 ((CCECPlaybackDevice
*)primary
)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG
);
103 ((CCECPlaybackDevice
*)primary
)->TransmitDeckStatus(iDestination
);
107 bool CSLCommandHandler::TransmitLGVendorId(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
109 cec_command response
;
110 cec_command::Format(response
, iInitiator
, iDestination
, CEC_OPCODE_DEVICE_VENDOR_ID
, m_iTransmitTimeout
);
111 response
.parameters
.PushBack((uint8_t) (((uint64_t)CEC_VENDOR_LG
>> 16) & 0xFF));
112 response
.parameters
.PushBack((uint8_t) (((uint64_t)CEC_VENDOR_LG
>> 8) & 0xFF));
113 response
.parameters
.PushBack((uint8_t) ((uint64_t)CEC_VENDOR_LG
& 0xFF));
119 bool CSLCommandHandler::HandleGiveDeviceVendorId(const cec_command
&command
)
121 /* imitate LG devices */
122 CCECBusDevice
*device
= GetDevice(command
.destination
);
124 device
->SetVendorId(CEC_VENDOR_LG
);
126 return CCECCommandHandler::HandleGiveDeviceVendorId(command
);
129 bool CSLCommandHandler::HandleCommand(const cec_command
&command
)
131 bool bHandled(false);
132 if (m_busDevice
->MyLogicalAddressContains(command
.destination
) ||
133 command
.destination
== CECDEVICE_BROADCAST
)
135 switch(command
.opcode
)
137 case CEC_OPCODE_VENDOR_COMMAND
:
138 bHandled
= HandleVendorCommand(command
);
140 case CEC_OPCODE_GIVE_DECK_STATUS
:
142 if (command
.parameters
.size
== 1)
144 if (command
.parameters
[0] == CEC_STATUS_REQUEST_ONCE
||
145 command
.parameters
[0] == CEC_STATUS_REQUEST_ON
)
146 TransmitDeckStatus(command
.initiator
);
148 CCECCommandHandler::HandleCommand(command
);
153 case CEC_OPCODE_FEATURE_ABORT
:
155 if (!m_bSkipNextVendorId
)
157 m_bSkipNextVendorId
= true;
158 TransmitLGVendorId(m_busDevice
->GetProcessor()->GetLogicalAddresses().primary
, CECDEVICE_BROADCAST
);
162 m_bSkipNextVendorId
= false;
164 m_bSLEnabled
= false;
173 bHandled
= CCECCommandHandler::HandleCommand(command
);
178 void CSLCommandHandler::HandlePoll(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
180 CCECCommandHandler::HandlePoll(iInitiator
, iDestination
);
181 m_bAwaitingReceiveFailed
= true;
184 bool CSLCommandHandler::HandleReceiveFailed(void)
186 if (m_bAwaitingReceiveFailed
)
188 m_bAwaitingReceiveFailed
= false;
195 bool CSLCommandHandler::InitHandler(void)
201 m_busDevice
->GetProcessor()->SetStandardLineTimeout(3);
202 m_busDevice
->GetProcessor()->SetRetryLineTimeout(3);
204 /* increase the number of retries because the tv is keeping the bus busy at times */
205 m_iTransmitWait
= 2000;
206 m_iTransmitRetries
= 4;
207 m_iTransmitTimeout
= 500;
209 CCECBusDevice
*primary
= m_busDevice
->GetProcessor()->m_busDevices
[m_busDevice
->GetProcessor()->GetLogicalAddresses().primary
];
210 if (m_busDevice
->GetLogicalAddress() != primary
->GetLogicalAddress())
211 primary
->SetVendorId(CEC_VENDOR_LG
, false);
213 if (m_busDevice
->GetLogicalAddress() == CECDEVICE_TV
)
215 primary
->TransmitVendorID(CECDEVICE_TV
);
216 primary
->TransmitPhysicalAddress();
219 cec_command::Format(command
, m_busDevice
->GetProcessor()->GetLogicalAddresses().primary
, CECDEVICE_TV
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
, m_iTransmitTimeout
);
222 /* LG TVs don't always reply to CEC version requests, so just set it to 1.3a */
223 m_busDevice
->SetCecVersion(CEC_VERSION_1_3A
);
226 /* LG devices always return "korean" as language */
227 cec_menu_language lang
;
228 lang
.device
= m_busDevice
->GetLogicalAddress();
229 snprintf(lang
.language
, 4, "eng");
230 m_busDevice
->SetMenuLanguage(lang
);
232 if (m_busDevice
->GetLogicalAddress() == CECDEVICE_TV
)
234 m_busDevice
->GetProcessor()->SetActiveSource(m_busDevice
->GetProcessor()->GetLogicalAddresses().primary
);
235 /* LG TVs only route keypresses when the deck status is set to 0x20 */
236 cec_logical_addresses addr
= m_busDevice
->GetProcessor()->GetLogicalAddresses();
237 for (uint8_t iPtr
= 0; iPtr
< 15; iPtr
++)
239 CCECBusDevice
*device
= m_busDevice
->GetProcessor()->m_busDevices
[iPtr
];
243 if (device
&& (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
||
244 device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
))
246 ((CCECPlaybackDevice
*)device
)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG
);
247 TransmitDeckStatus(CECDEVICE_TV
);