cec: don't send power on, acitve source and standby commands by default in cec-client
[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),
b8952342 54 m_bActiveSourceSent(false)
e9de9629 55{
cf4931be 56 m_vendorId = CEC_VENDOR_LG;
79f01d26
LOK
57 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
58
59 /* imitate LG devices */
ef583662 60 if (primary && m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress())
686740b9 61 {
3e61b350 62 primary->SetVendorId(CEC_VENDOR_LG);
686740b9
LOK
63 primary->ReplaceHandler(false);
64 }
79f01d26
LOK
65
66 /* LG TVs don't always reply to CEC version requests, so just set it to 1.3a */
67 if (m_busDevice->GetLogicalAddress() == CECDEVICE_TV)
68 m_busDevice->SetCecVersion(CEC_VERSION_1_3A);
69
70 /* LG devices always return "korean" as language */
71 cec_menu_language lang;
72 lang.device = m_busDevice->GetLogicalAddress();
73 snprintf(lang.language, 4, "eng");
74 m_busDevice->SetMenuLanguage(lang);
9f65e017
LOK
75}
76
468a1414
LOK
77bool CSLCommandHandler::InitHandler(void)
78{
79 if (m_bHandlerInited)
80 return true;
81 m_bHandlerInited = true;
82
632f5810 83 if (m_busDevice->GetLogicalAddress() == CECDEVICE_TV)
871934d3 84 {
632f5810
LOK
85 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
86
871934d3
LOK
87 /* start as 'in transition standby->on' */
88 primary->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
89 primary->TransmitPowerState(CECDEVICE_TV);
b8952342
LOK
90
91 /* send the vendor id */
92 primary->TransmitVendorID(CECDEVICE_BROADCAST);
871934d3 93 }
468a1414 94
468a1414
LOK
95 return true;
96}
97
3e61b350
LOK
98bool CSLCommandHandler::ActivateSource(void)
99{
cc1b9bc4 100 if (!SLInitialised())
682b21de
LOK
101 {
102 CLibCEC::AddLog(CEC_LOG_NOTICE, "not activating the source until SL has been initialised");
3faa971c 103 return true;
682b21de 104 }
3faa971c 105
cc1b9bc4
LOK
106 {
107 CLockObject lock(m_SLMutex);
108 if (m_bActiveSourceSent)
109 return true;
110 m_bActiveSourceSent = true;
111 }
b818fb5a 112
3e61b350
LOK
113 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
114 primary->SetActiveSource();
115 primary->TransmitActiveSource();
116 return true;
117}
118
468a1414
LOK
119bool CSLCommandHandler::HandleActiveSource(const cec_command &command)
120{
121 if (command.parameters.size == 2)
122 {
123 uint16_t iAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
124 if (iAddress != m_busDevice->GetPhysicalAddress(false))
682b21de 125 {
cc1b9bc4 126 ResetSLState();
682b21de 127 }
468a1414
LOK
128 return m_processor->SetActiveSource(iAddress);
129 }
130
131 return true;
132}
133
a463f954
LOK
134bool CSLCommandHandler::HandleDeviceVendorId(const cec_command &command)
135{
136 SetVendorId(command);
137
632f5810 138 if (!SLInitialised() && command.initiator == CECDEVICE_TV)
468a1414 139 {
2abe628c
LOK
140 cec_command response;
141 cec_command::Format(response, m_processor->GetLogicalAddress(), command.initiator, CEC_OPCODE_FEATURE_ABORT);
142 return Transmit(response);
468a1414 143 }
2abe628c 144 return true;
468a1414
LOK
145}
146
147bool CSLCommandHandler::HandleGivePhysicalAddress(const cec_command &command)
148{
8e57f83c 149 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
468a1414
LOK
150 {
151 CCECBusDevice *device = GetDevice(command.destination);
152 if (device)
b818fb5a 153 return device->TransmitPhysicalAddress(); // only the physical address, don't send image view on
468a1414
LOK
154 }
155
156 return false;
e9de9629 157}
e54fd7d2
LOK
158
159bool CSLCommandHandler::HandleVendorCommand(const cec_command &command)
160{
161 if (command.parameters.size == 1 &&
11d13a02 162 command.parameters[0] == SL_COMMAND_UNKNOWN_01)
e54fd7d2 163 {
5f316715 164 HandleVendorCommand01(command);
8d915412 165 return true;
e54fd7d2 166 }
9a0d7b9f
LOK
167 else if (command.parameters.size == 2 &&
168 command.parameters[0] == SL_COMMAND_POWER_ON)
169 {
0ecbcd4d 170 HandleVendorCommandPowerOn(command);
9a0d7b9f
LOK
171 return true;
172 }
797dd7c4 173 else if (command.parameters.size == 2 &&
11d13a02 174 command.parameters[0] == SL_COMMAND_CONNECT_REQUEST)
9902f4e8 175 {
0ecbcd4d 176 HandleVendorCommandSLConnect(command);
8d915412 177 return true;
9902f4e8
LOK
178 }
179 else if (command.parameters.size == 1 &&
0d4c3a7b 180 command.parameters[0] == SL_COMMAND_REQUEST_POWER_STATUS)
9902f4e8 181 {
0ecbcd4d 182 HandleVendorCommandPowerOnStatus(command);
8d915412 183 return true;
9902f4e8 184 }
e54fd7d2
LOK
185
186 return false;
187}
188
5f316715 189void CSLCommandHandler::HandleVendorCommand01(const cec_command &command)
fe6f8e37 190{
b818fb5a 191 m_processor->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
fe6f8e37
LOK
192 TransmitVendorCommand0205(command.destination, command.initiator);
193}
194
195void CSLCommandHandler::TransmitVendorCommand0205(const cec_logical_address iSource, const cec_logical_address iDestination)
5f316715
LOK
196{
197 cec_command response;
fe6f8e37 198 cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
5f316715
LOK
199 response.PushBack(SL_COMMAND_UNKNOWN_02);
200 response.PushBack(SL_COMMAND_UNKNOWN_03);
201
19cbfa8f 202 Transmit(response, false);
fe6f8e37 203}
5f316715 204
0ecbcd4d 205void CSLCommandHandler::HandleVendorCommandPowerOn(const cec_command &command)
9a0d7b9f 206{
632f5810
LOK
207 if (command.initiator != CECDEVICE_TV)
208 return;
209
842262d8 210 CCECBusDevice *device = m_processor->GetPrimaryDevice();
9a0d7b9f
LOK
211 if (device)
212 {
cc1b9bc4 213 SetSLInitialised();
468a1414
LOK
214
215 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); //XXX
9a0d7b9f 216 device->TransmitPowerState(command.initiator);
468a1414 217 device->SetPowerStatus(CEC_POWER_STATUS_ON);
9a0d7b9f 218
468a1414
LOK
219 device->SetActiveSource();
220 TransmitImageViewOn(device->GetLogicalAddress(), command.initiator);
221 }
5f316715 222}
0ecbcd4d 223void CSLCommandHandler::HandleVendorCommandPowerOnStatus(const cec_command &command)
5f316715
LOK
224{
225 if (command.destination != CECDEVICE_BROADCAST)
226 {
227 CCECBusDevice *device = m_processor->m_busDevices[m_processor->GetLogicalAddresses().primary];
228 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
229 device->TransmitPowerState(command.initiator);
230 device->SetPowerStatus(CEC_POWER_STATUS_ON);
231 }
232}
233
468a1414 234void CSLCommandHandler::HandleVendorCommandSLConnect(const cec_command &command)
8d915412 235{
cc1b9bc4 236 SetSLInitialised();
5abb18f3 237 TransmitVendorCommandSetDeviceMode(m_processor->GetLogicalAddress(), command.initiator, CEC_DEVICE_TYPE_RECORDING_DEVICE);
37acf382 238
2abe628c 239 ActivateSource();
8d915412 240}
0d4c3a7b 241
5abb18f3 242void CSLCommandHandler::TransmitVendorCommandSetDeviceMode(const cec_logical_address iSource, const cec_logical_address iDestination, const cec_device_type type)
0d4c3a7b 243{
468a1414
LOK
244 cec_command response;
245 cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
5abb18f3
LOK
246 response.PushBack(SL_COMMAND_SET_DEVICE_MODE);
247 response.PushBack((uint8_t)type);
468a1414 248 Transmit(response, false);
0d4c3a7b 249}
fe6f8e37 250
b818fb5a 251bool CSLCommandHandler::HandleGiveDeckStatus(const cec_command &command)
9f65e017 252{
b818fb5a
LOK
253 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
254 {
255 CCECBusDevice *device = GetDevice(command.destination);
256 if (device && (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE))
257 {
258 if (command.parameters.size > 0)
259 {
b818fb5a
LOK
260 if (command.parameters[0] == CEC_STATUS_REQUEST_ON)
261 {
2abe628c 262 ((CCECPlaybackDevice *) device)->SetDeckStatus(CEC_DECK_INFO_STOP);
b818fb5a
LOK
263 return ((CCECPlaybackDevice *) device)->TransmitDeckStatus(command.initiator) &&
264 device->TransmitImageViewOn() &&
265 device->TransmitPhysicalAddress();
266 }
267 else if (command.parameters[0] == CEC_STATUS_REQUEST_ONCE)
268 {
2abe628c 269 ((CCECPlaybackDevice *) device)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG);
b818fb5a
LOK
270 return ((CCECPlaybackDevice *) device)->TransmitDeckStatus(command.initiator);
271 }
272 }
273 }
274 return CCECCommandHandler::HandleGiveDeckStatus(command);
275 }
9f65e017 276
b818fb5a 277 return false;
9f65e017 278}
718b3632 279
718b3632
LOK
280bool CSLCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command)
281{
2abe628c 282 bool bReturn(false);
632f5810 283 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination) && command.initiator == CECDEVICE_TV)
718b3632
LOK
284 {
285 CCECBusDevice *device = GetDevice(command.destination);
286 if (device && device->GetPowerStatus(false) != CEC_POWER_STATUS_ON)
2abe628c
LOK
287 {
288 bReturn = device->TransmitPowerState(command.initiator);
289 device->SetPowerStatus(CEC_POWER_STATUS_ON);
290 }
2abe628c
LOK
291 else
292 {
cc1b9bc4 293 if (!ActiveSourceSent())
3faa971c
LOK
294 {
295 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
296 bReturn = device->TransmitPowerState(command.initiator);
297 ActivateSource();
298 }
299 else if (m_resetPowerState.IsSet() && m_resetPowerState.TimeLeft() > 0)
300 {
301 /* assume that we've bugged out */
302 CLibCEC::AddLog(CEC_LOG_NOTICE, "LG seems to have bugged out. resetting to 'in transition standby to on'");
cc1b9bc4
LOK
303 {
304 CLockObject lock(m_SLMutex);
305 m_bActiveSourceSent = false;
306 }
3faa971c
LOK
307 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
308 bReturn = device->TransmitPowerState(command.initiator);
309 device->SetPowerStatus(CEC_POWER_STATUS_ON);
310 m_resetPowerState.Init(5000);
311 }
312 else
313 {
314 bReturn = device->TransmitPowerState(command.initiator);
315 m_resetPowerState.Init(5000);
316 }
b818fb5a 317 }
718b3632
LOK
318 }
319
2abe628c
LOK
320 return bReturn;
321}
322
323bool CSLCommandHandler::HandleRequestActiveSource(const cec_command &command)
324{
325 if (m_processor->IsRunning())
326 {
327 CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %i requests active source, ignored", (uint8_t) command.initiator);
328 return true;
329 }
718b3632
LOK
330 return false;
331}
5c3b1f92
LOK
332
333bool CSLCommandHandler::HandleFeatureAbort(const cec_command &command)
334{
632f5810
LOK
335 if (command.parameters.size == 0 && m_processor->GetPrimaryDevice()->GetPowerStatus() == CEC_POWER_STATUS_ON && !SLInitialised() &&
336 command.initiator == CECDEVICE_TV)
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}
632f5810
LOK
380
381bool CSLCommandHandler::PowerOn(const cec_logical_address iInitiator, const cec_logical_address iDestination)
382{
383 if (iDestination != CECDEVICE_TV)
384 {
385 cec_command command;
386 cec_command::Format(command, CECDEVICE_TV, iDestination, CEC_OPCODE_VENDOR_COMMAND);
387 command.PushBack(SL_COMMAND_POWER_ON);
388 command.PushBack(0);
389 return Transmit(command);
390 }
391
392 return CCECCommandHandler::PowerOn(iInitiator, iDestination);
393}