cec: protect the SL state vars with a mutex
[deb_libcec.git] / src / lib / implementations / SLCommandHandler.cpp
CommitLineData
e9de9629
LOK
1/*
2 * This file is part of the libCEC(R) library.
3 *
b492c10e 4 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
e9de9629
LOK
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"
2abe628c 37#include "../LibCEC.h"
e9de9629
LOK
38
39using namespace CEC;
cc1b9bc4 40using namespace PLATFORM;
e9de9629 41
0d4c3a7b
LOK
42#define SL_COMMAND_UNKNOWN_01 0x01
43#define SL_COMMAND_UNKNOWN_02 0x02
44#define SL_COMMAND_UNKNOWN_03 0x05
11d13a02 45
0d4c3a7b 46#define SL_COMMAND_REQUEST_POWER_STATUS 0xa0
9a0d7b9f 47#define SL_COMMAND_POWER_ON 0x03
0d4c3a7b 48#define SL_COMMAND_CONNECT_REQUEST 0x04
5abb18f3 49#define SL_COMMAND_SET_DEVICE_MODE 0x05
11d13a02 50
e9de9629 51CSLCommandHandler::CSLCommandHandler(CCECBusDevice *busDevice) :
855a3a98 52 CCECCommandHandler(busDevice),
b4c4ef7d 53 m_bSLEnabled(false),
1c38352a
LOK
54 m_bActiveSourceSent(false),
55 m_bVendorIdSent(false)
e9de9629 56{
cf4931be 57 m_vendorId = CEC_VENDOR_LG;
79f01d26
LOK
58 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
59
60 /* imitate LG devices */
ef583662 61 if (primary && m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress())
3e61b350 62 primary->SetVendorId(CEC_VENDOR_LG);
79f01d26
LOK
63
64 /* LG TVs don't always reply to CEC version requests, so just set it to 1.3a */
65 if (m_busDevice->GetLogicalAddress() == CECDEVICE_TV)
66 m_busDevice->SetCecVersion(CEC_VERSION_1_3A);
67
68 /* LG devices always return "korean" as language */
69 cec_menu_language lang;
70 lang.device = m_busDevice->GetLogicalAddress();
71 snprintf(lang.language, 4, "eng");
72 m_busDevice->SetMenuLanguage(lang);
9f65e017
LOK
73}
74
468a1414
LOK
75bool CSLCommandHandler::InitHandler(void)
76{
77 if (m_bHandlerInited)
78 return true;
79 m_bHandlerInited = true;
80
468a1414
LOK
81 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
82 if (m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress())
871934d3
LOK
83 {
84 /* start as 'in transition standby->on' */
85 primary->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
86 primary->TransmitPowerState(CECDEVICE_TV);
871934d3 87 }
468a1414 88
468a1414
LOK
89 return true;
90}
91
3e61b350
LOK
92bool CSLCommandHandler::ActivateSource(void)
93{
1c38352a 94 /* reply with LGs vendor id */
cc1b9bc4 95 bool bSendVendorId(false);
1c38352a 96 {
cc1b9bc4
LOK
97 CLockObject lock(m_SLMutex);
98 bSendVendorId = !m_bVendorIdSent;
1c38352a 99 m_bVendorIdSent = true;
1c38352a 100 }
cc1b9bc4
LOK
101 if (bSendVendorId)
102 m_processor->GetPrimaryDevice()->TransmitVendorID(CECDEVICE_BROADCAST, false);
1c38352a 103
cc1b9bc4 104 if (!SLInitialised())
682b21de
LOK
105 {
106 CLibCEC::AddLog(CEC_LOG_NOTICE, "not activating the source until SL has been initialised");
3faa971c 107 return true;
682b21de 108 }
3faa971c 109
cc1b9bc4
LOK
110 {
111 CLockObject lock(m_SLMutex);
112 if (m_bActiveSourceSent)
113 return true;
114 m_bActiveSourceSent = true;
115 }
b818fb5a 116
3e61b350
LOK
117 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
118 primary->SetActiveSource();
119 primary->TransmitActiveSource();
120 return true;
121}
122
468a1414
LOK
123bool CSLCommandHandler::HandleActiveSource(const cec_command &command)
124{
125 if (command.parameters.size == 2)
126 {
127 uint16_t iAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
128 if (iAddress != m_busDevice->GetPhysicalAddress(false))
682b21de 129 {
cc1b9bc4 130 ResetSLState();
682b21de 131 }
468a1414
LOK
132 return m_processor->SetActiveSource(iAddress);
133 }
134
135 return true;
136}
137
a463f954
LOK
138bool CSLCommandHandler::HandleDeviceVendorId(const cec_command &command)
139{
140 SetVendorId(command);
141
cc1b9bc4 142 if (!SLInitialised())
468a1414 143 {
2abe628c
LOK
144 cec_command response;
145 cec_command::Format(response, m_processor->GetLogicalAddress(), command.initiator, CEC_OPCODE_FEATURE_ABORT);
146 return Transmit(response);
468a1414 147 }
2abe628c 148 return true;
468a1414
LOK
149}
150
151bool CSLCommandHandler::HandleGivePhysicalAddress(const cec_command &command)
152{
8e57f83c 153 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
468a1414
LOK
154 {
155 CCECBusDevice *device = GetDevice(command.destination);
156 if (device)
b818fb5a 157 return device->TransmitPhysicalAddress(); // only the physical address, don't send image view on
468a1414
LOK
158 }
159
160 return false;
e9de9629 161}
e54fd7d2
LOK
162
163bool CSLCommandHandler::HandleVendorCommand(const cec_command &command)
164{
165 if (command.parameters.size == 1 &&
11d13a02 166 command.parameters[0] == SL_COMMAND_UNKNOWN_01)
e54fd7d2 167 {
5f316715 168 HandleVendorCommand01(command);
8d915412 169 return true;
e54fd7d2 170 }
9a0d7b9f
LOK
171 else if (command.parameters.size == 2 &&
172 command.parameters[0] == SL_COMMAND_POWER_ON)
173 {
0ecbcd4d 174 HandleVendorCommandPowerOn(command);
9a0d7b9f
LOK
175 return true;
176 }
797dd7c4 177 else if (command.parameters.size == 2 &&
11d13a02 178 command.parameters[0] == SL_COMMAND_CONNECT_REQUEST)
9902f4e8 179 {
0ecbcd4d 180 HandleVendorCommandSLConnect(command);
8d915412 181 return true;
9902f4e8
LOK
182 }
183 else if (command.parameters.size == 1 &&
0d4c3a7b 184 command.parameters[0] == SL_COMMAND_REQUEST_POWER_STATUS)
9902f4e8 185 {
0ecbcd4d 186 HandleVendorCommandPowerOnStatus(command);
8d915412 187 return true;
9902f4e8 188 }
e54fd7d2
LOK
189
190 return false;
191}
192
5f316715 193void CSLCommandHandler::HandleVendorCommand01(const cec_command &command)
fe6f8e37 194{
b818fb5a 195 m_processor->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
fe6f8e37
LOK
196 TransmitVendorCommand0205(command.destination, command.initiator);
197}
198
199void CSLCommandHandler::TransmitVendorCommand0205(const cec_logical_address iSource, const cec_logical_address iDestination)
5f316715
LOK
200{
201 cec_command response;
fe6f8e37 202 cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
5f316715
LOK
203 response.PushBack(SL_COMMAND_UNKNOWN_02);
204 response.PushBack(SL_COMMAND_UNKNOWN_03);
205
19cbfa8f 206 Transmit(response, false);
fe6f8e37 207}
5f316715 208
0ecbcd4d 209void CSLCommandHandler::HandleVendorCommandPowerOn(const cec_command &command)
9a0d7b9f 210{
842262d8 211 CCECBusDevice *device = m_processor->GetPrimaryDevice();
9a0d7b9f
LOK
212 if (device)
213 {
cc1b9bc4 214 SetSLInitialised();
468a1414
LOK
215
216 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); //XXX
9a0d7b9f 217 device->TransmitPowerState(command.initiator);
468a1414 218 device->SetPowerStatus(CEC_POWER_STATUS_ON);
9a0d7b9f 219
468a1414
LOK
220 device->SetActiveSource();
221 TransmitImageViewOn(device->GetLogicalAddress(), command.initiator);
222 }
5f316715 223}
0ecbcd4d 224void CSLCommandHandler::HandleVendorCommandPowerOnStatus(const cec_command &command)
5f316715
LOK
225{
226 if (command.destination != CECDEVICE_BROADCAST)
227 {
228 CCECBusDevice *device = m_processor->m_busDevices[m_processor->GetLogicalAddresses().primary];
229 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
230 device->TransmitPowerState(command.initiator);
231 device->SetPowerStatus(CEC_POWER_STATUS_ON);
232 }
233}
234
468a1414 235void CSLCommandHandler::HandleVendorCommandSLConnect(const cec_command &command)
8d915412 236{
cc1b9bc4 237 SetSLInitialised();
5abb18f3 238 TransmitVendorCommandSetDeviceMode(m_processor->GetLogicalAddress(), command.initiator, CEC_DEVICE_TYPE_RECORDING_DEVICE);
37acf382 239
2abe628c 240 ActivateSource();
8d915412 241}
0d4c3a7b 242
5abb18f3 243void CSLCommandHandler::TransmitVendorCommandSetDeviceMode(const cec_logical_address iSource, const cec_logical_address iDestination, const cec_device_type type)
0d4c3a7b 244{
468a1414
LOK
245 cec_command response;
246 cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
5abb18f3
LOK
247 response.PushBack(SL_COMMAND_SET_DEVICE_MODE);
248 response.PushBack((uint8_t)type);
468a1414 249 Transmit(response, false);
0d4c3a7b 250}
fe6f8e37 251
b818fb5a 252bool CSLCommandHandler::HandleGiveDeckStatus(const cec_command &command)
9f65e017 253{
b818fb5a
LOK
254 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
255 {
256 CCECBusDevice *device = GetDevice(command.destination);
257 if (device && (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE))
258 {
259 if (command.parameters.size > 0)
260 {
b818fb5a
LOK
261 if (command.parameters[0] == CEC_STATUS_REQUEST_ON)
262 {
2abe628c 263 ((CCECPlaybackDevice *) device)->SetDeckStatus(CEC_DECK_INFO_STOP);
b818fb5a
LOK
264 return ((CCECPlaybackDevice *) device)->TransmitDeckStatus(command.initiator) &&
265 device->TransmitImageViewOn() &&
266 device->TransmitPhysicalAddress();
267 }
268 else if (command.parameters[0] == CEC_STATUS_REQUEST_ONCE)
269 {
2abe628c 270 ((CCECPlaybackDevice *) device)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG);
b818fb5a
LOK
271 return ((CCECPlaybackDevice *) device)->TransmitDeckStatus(command.initiator);
272 }
273 }
274 }
275 return CCECCommandHandler::HandleGiveDeckStatus(command);
276 }
9f65e017 277
b818fb5a 278 return false;
9f65e017 279}
718b3632 280
718b3632
LOK
281bool CSLCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command)
282{
2abe628c 283 bool bReturn(false);
718b3632
LOK
284 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
285 {
286 CCECBusDevice *device = GetDevice(command.destination);
287 if (device && device->GetPowerStatus(false) != CEC_POWER_STATUS_ON)
2abe628c
LOK
288 {
289 bReturn = device->TransmitPowerState(command.initiator);
290 device->SetPowerStatus(CEC_POWER_STATUS_ON);
291 }
2abe628c
LOK
292 else
293 {
cc1b9bc4 294 if (!ActiveSourceSent())
3faa971c
LOK
295 {
296 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
297 bReturn = device->TransmitPowerState(command.initiator);
298 ActivateSource();
299 }
300 else if (m_resetPowerState.IsSet() && m_resetPowerState.TimeLeft() > 0)
301 {
302 /* assume that we've bugged out */
303 CLibCEC::AddLog(CEC_LOG_NOTICE, "LG seems to have bugged out. resetting to 'in transition standby to on'");
cc1b9bc4
LOK
304 {
305 CLockObject lock(m_SLMutex);
306 m_bActiveSourceSent = false;
307 }
3faa971c
LOK
308 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
309 bReturn = device->TransmitPowerState(command.initiator);
310 device->SetPowerStatus(CEC_POWER_STATUS_ON);
311 m_resetPowerState.Init(5000);
312 }
313 else
314 {
315 bReturn = device->TransmitPowerState(command.initiator);
316 m_resetPowerState.Init(5000);
317 }
b818fb5a 318 }
718b3632
LOK
319 }
320
2abe628c
LOK
321 return bReturn;
322}
323
324bool CSLCommandHandler::HandleRequestActiveSource(const cec_command &command)
325{
326 if (m_processor->IsRunning())
327 {
328 CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %i requests active source, ignored", (uint8_t) command.initiator);
329 return true;
330 }
718b3632
LOK
331 return false;
332}
5c3b1f92
LOK
333
334bool CSLCommandHandler::HandleFeatureAbort(const cec_command &command)
335{
cc1b9bc4 336 if (command.parameters.size == 0 && m_processor->GetPrimaryDevice()->GetPowerStatus() == CEC_POWER_STATUS_ON && !SLInitialised())
5c3b1f92
LOK
337 {
338 m_processor->GetPrimaryDevice()->TransmitPowerState(command.initiator);
339 m_processor->GetPrimaryDevice()->TransmitVendorID(CECDEVICE_BROADCAST, false);
340 }
341
342 return CCECCommandHandler::HandleFeatureAbort(command);
343}
344
345bool CSLCommandHandler::HandleStandby(const cec_command &command)
346{
347 if (command.initiator == CECDEVICE_TV)
cc1b9bc4 348 ResetSLState();
5c3b1f92 349
cc1b9bc4
LOK
350 return CCECCommandHandler::HandleStandby(command);
351}
5c3b1f92 352
cc1b9bc4
LOK
353void CSLCommandHandler::ResetSLState(void)
354{
355 CLibCEC::AddLog(CEC_LOG_NOTICE, "resetting SL initialised state");
356 CLockObject lock(m_SLMutex);
357 m_bSLEnabled = false;
358 m_bActiveSourceSent = false;
359 m_processor->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
360}
361
362void CSLCommandHandler::SetSLInitialised(void)
363{
364 CLibCEC::AddLog(CEC_LOG_NOTICE, "SL initialised");
365 CLockObject lock(m_SLMutex);
366 m_bSLEnabled = true;
367}
368
369bool CSLCommandHandler::SLInitialised(void)
370{
371 CLockObject lock(m_SLMutex);
372 return m_bSLEnabled;
373}
374
375bool CSLCommandHandler::ActiveSourceSent(void)
376{
377 CLockObject lock(m_SLMutex);
378 return m_bActiveSourceSent;
5c3b1f92 379}