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