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