cec: fixed another deadlock on exit
[deb_libcec.git] / src / lib / implementations / SLCommandHandler.cpp
CommitLineData
e9de9629
LOK
1/*
2 * This file is part of the libCEC(R) library.
3 *
4 * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
6 *
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
8 *
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.
13 *
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.
18 *
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.
22 *
23 *
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
26 *
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/
31 */
32
33#include "SLCommandHandler.h"
e54fd7d2 34#include "../devices/CECBusDevice.h"
8d915412 35#include "../devices/CECPlaybackDevice.h"
e54fd7d2 36#include "../CECProcessor.h"
b4c4ef7d 37#include "../platform/timeutils.h"
e9de9629
LOK
38
39using namespace CEC;
40
0d4c3a7b
LOK
41#define SL_COMMAND_UNKNOWN_01 0x01
42#define SL_COMMAND_UNKNOWN_02 0x02
43#define SL_COMMAND_UNKNOWN_03 0x05
11d13a02 44
0d4c3a7b 45#define SL_COMMAND_REQUEST_POWER_STATUS 0xa0
9a0d7b9f 46#define SL_COMMAND_POWER_ON 0x03
0d4c3a7b
LOK
47#define SL_COMMAND_CONNECT_REQUEST 0x04
48#define SL_COMMAND_CONNECT_ACCEPT 0x05
11d13a02 49
e9de9629 50CSLCommandHandler::CSLCommandHandler(CCECBusDevice *busDevice) :
855a3a98 51 CCECCommandHandler(busDevice),
b4c4ef7d
LOK
52 m_bAwaitingReceiveFailed(false),
53 m_bSLEnabled(false),
5f316715 54 m_bVendorIdSent(false)
e9de9629 55{
fe6f8e37 56 m_processor->m_busDevices[m_processor->GetLogicalAddresses().primary]->m_powerStatus = CEC_POWER_STATUS_STANDBY;
e9de9629 57}
e54fd7d2
LOK
58
59bool CSLCommandHandler::HandleVendorCommand(const cec_command &command)
60{
61 if (command.parameters.size == 1 &&
11d13a02 62 command.parameters[0] == SL_COMMAND_UNKNOWN_01)
e54fd7d2 63 {
5f316715 64 HandleVendorCommand01(command);
8d915412 65 return true;
e54fd7d2 66 }
9a0d7b9f
LOK
67 else if (command.parameters.size == 2 &&
68 command.parameters[0] == SL_COMMAND_POWER_ON)
69 {
70 HandleVendorCommand03(command);
71 return true;
72 }
797dd7c4 73 else if (command.parameters.size == 2 &&
11d13a02 74 command.parameters[0] == SL_COMMAND_CONNECT_REQUEST)
9902f4e8 75 {
5f316715 76 HandleVendorCommand04(command);
8d915412 77 return true;
9902f4e8
LOK
78 }
79 else if (command.parameters.size == 1 &&
0d4c3a7b 80 command.parameters[0] == SL_COMMAND_REQUEST_POWER_STATUS)
9902f4e8 81 {
5f316715 82 HandleVendorCommandA0(command);
8d915412 83 return true;
9902f4e8 84 }
e54fd7d2
LOK
85
86 return false;
87}
88
5f316715
LOK
89bool CSLCommandHandler::HandleGiveDeckStatus(const cec_command &command)
90{
91 if (command.parameters.size == 1)
92 {
93 if (command.parameters[0] == CEC_STATUS_REQUEST_ONCE ||
94 command.parameters[0] == CEC_STATUS_REQUEST_ON)
95 {
96 TransmitDeckStatus(command.initiator);
97 }
98 else
99 {
100 CCECCommandHandler::HandleGiveDeckStatus(command);
101 }
102 }
103 return true;
104}
105
106void CSLCommandHandler::HandleVendorCommand01(const cec_command &command)
fe6f8e37
LOK
107{
108 TransmitVendorCommand0205(command.destination, command.initiator);
109}
110
111void CSLCommandHandler::TransmitVendorCommand0205(const cec_logical_address iSource, const cec_logical_address iDestination)
5f316715
LOK
112{
113 cec_command response;
fe6f8e37 114 cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
5f316715
LOK
115 response.PushBack(SL_COMMAND_UNKNOWN_02);
116 response.PushBack(SL_COMMAND_UNKNOWN_03);
117
118 Transmit(response);
fe6f8e37 119}
5f316715 120
fe6f8e37
LOK
121void CSLCommandHandler::TransmitVendorCommand04(const cec_logical_address iSource, const cec_logical_address iDestination)
122{
123 m_bSLEnabled = true;
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);
128 Transmit(response);
5f316715
LOK
129}
130
9a0d7b9f
LOK
131void CSLCommandHandler::HandleVendorCommand03(const cec_command &command)
132{
133 CCECBusDevice *device = m_processor->m_busDevices[m_processor->GetLogicalAddresses().primary];
134 if (device)
135 {
fe6f8e37
LOK
136 m_bSLEnabled = true;
137 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
9a0d7b9f 138 device->TransmitPowerState(command.initiator);
fe6f8e37 139 device->TransmitVendorID(command.initiator);
9a0d7b9f 140 TransmitPowerOn(device->GetLogicalAddress(), command.initiator);
9a0d7b9f
LOK
141 }
142}
143
5f316715
LOK
144void CSLCommandHandler::HandleVendorCommand04(const cec_command &command)
145{
fe6f8e37
LOK
146 m_bSLEnabled = true;
147 TransmitVendorCommand04(command.destination, command.initiator);
5f316715
LOK
148 TransmitDeckStatus(command.initiator);
149}
150
151void CSLCommandHandler::HandleVendorCommandA0(const cec_command &command)
152{
153 if (command.destination != CECDEVICE_BROADCAST)
154 {
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);
159 }
160}
161
b4c4ef7d
LOK
162void CSLCommandHandler::TransmitDeckStatus(const cec_logical_address iDestination)
163{
164 /* set deck status for the playback device */
5f316715 165 CCECBusDevice *primary = m_processor->m_busDevices[m_processor->GetLogicalAddresses().primary];
b4c4ef7d
LOK
166 if (primary->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || primary->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE)
167 {
168 ((CCECPlaybackDevice *)primary)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG);
169 ((CCECPlaybackDevice *)primary)->TransmitDeckStatus(iDestination);
170 }
171}
172
8d915412
LOK
173bool CSLCommandHandler::TransmitLGVendorId(const cec_logical_address iInitiator, const cec_logical_address iDestination)
174{
175 cec_command response;
0d4c3a7b 176 cec_command::Format(response, iInitiator, iDestination, CEC_OPCODE_DEVICE_VENDOR_ID);
8d915412
LOK
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));
180
8fa35473 181 Transmit(response);
b4c4ef7d 182 return true;
8d915412
LOK
183}
184
e54fd7d2
LOK
185bool CSLCommandHandler::HandleGiveDeviceVendorId(const cec_command &command)
186{
187 /* imitate LG devices */
188 CCECBusDevice *device = GetDevice(command.destination);
189 if (device)
190 device->SetVendorId(CEC_VENDOR_LG);
191
192 return CCECCommandHandler::HandleGiveDeviceVendorId(command);
193}
194
195bool CSLCommandHandler::HandleCommand(const cec_command &command)
196{
197 bool bHandled(false);
5f316715 198
fe6f8e37
LOK
199 if (m_processor->IsStarted() && (m_busDevice->MyLogicalAddressContains(command.destination) ||
200 command.destination == CECDEVICE_BROADCAST))
e54fd7d2
LOK
201 {
202 switch(command.opcode)
203 {
204 case CEC_OPCODE_VENDOR_COMMAND:
205 bHandled = HandleVendorCommand(command);
206 break;
b4c4ef7d
LOK
207 case CEC_OPCODE_FEATURE_ABORT:
208 {
5f316715 209 if (!m_bVendorIdSent)
b4c4ef7d 210 {
5f316715
LOK
211 m_bVendorIdSent = true;
212 TransmitLGVendorId(m_processor->GetLogicalAddresses().primary, CECDEVICE_BROADCAST);
45b97de7 213 }
b4c4ef7d
LOK
214 }
215 bHandled = true;
e54fd7d2
LOK
216 default:
217 break;
218 }
219 }
220
221 if (!bHandled)
222 bHandled = CCECCommandHandler::HandleCommand(command);
223
224 return bHandled;
225}
855a3a98 226
855a3a98
LOK
227void CSLCommandHandler::HandlePoll(const cec_logical_address iInitiator, const cec_logical_address iDestination)
228{
229 CCECCommandHandler::HandlePoll(iInitiator, iDestination);
230 m_bAwaitingReceiveFailed = true;
231}
232
233bool CSLCommandHandler::HandleReceiveFailed(void)
234{
235 if (m_bAwaitingReceiveFailed)
236 {
237 m_bAwaitingReceiveFailed = false;
238 return false;
239 }
240
241 return true;
242}
8d915412
LOK
243
244bool CSLCommandHandler::InitHandler(void)
245{
5f316715
LOK
246 m_processor->SetStandardLineTimeout(3);
247 m_processor->SetRetryLineTimeout(3);
dcd240b2 248
ae693aaa
LOK
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;
253
5f316715 254 CCECBusDevice *primary = m_processor->m_busDevices[m_processor->GetLogicalAddresses().primary];
d211708b
LOK
255 if (m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress())
256 primary->SetVendorId(CEC_VENDOR_LG, false);
b4c4ef7d 257
37acf382
LOK
258 if (m_busDevice->GetLogicalAddress() == CECDEVICE_TV)
259 {
37acf382
LOK
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);
262 }
caaf64d7 263
37acf382
LOK
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);
caaf64d7 269
37acf382
LOK
270 if (m_busDevice->GetLogicalAddress() == CECDEVICE_TV)
271 {
fe6f8e37
LOK
272 m_processor->SetActiveSource();
273
37acf382 274 /* LG TVs only route keypresses when the deck status is set to 0x20 */
5f316715 275 cec_logical_addresses addr = m_processor->GetLogicalAddresses();
37acf382 276 for (uint8_t iPtr = 0; iPtr < 15; iPtr++)
8d915412 277 {
5f316715 278 CCECBusDevice *device = m_processor->m_busDevices[iPtr];
37acf382 279
37acf382 280 if (addr[iPtr])
8d915412 281 {
37acf382
LOK
282 if (device && (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE ||
283 device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE))
284 {
285 ((CCECPlaybackDevice *)device)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG);
37acf382 286 }
8d915412
LOK
287 }
288 }
289 }
37acf382 290
8d915412
LOK
291 return true;
292}
0d4c3a7b 293
0d4c3a7b
LOK
294bool CSLCommandHandler::TransmitPowerOn(const cec_logical_address iInitiator, const cec_logical_address iDestination)
295{
5f316715 296 if (iDestination != CECDEVICE_BROADCAST &&
9a0d7b9f
LOK
297 iDestination != CECDEVICE_TV &&
298 m_processor->m_busDevices[iDestination]->GetVendorId(false) == CEC_VENDOR_LG)
5f316715
LOK
299 {
300 cec_command command;
301 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_VENDOR_COMMAND);
9a0d7b9f
LOK
302 command.parameters.PushBack((uint8_t)SL_COMMAND_POWER_ON);
303 command.parameters.PushBack(0x00);
5f316715
LOK
304 return Transmit(command);
305 }
0d4c3a7b 306
5f316715 307 return CCECCommandHandler::TransmitPowerOn(iInitiator, iDestination);
0d4c3a7b 308}
fe6f8e37 309