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