cec: just send the vendor id in InitHandler()
[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
468a1414
LOK
83 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
84 if (m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress())
871934d3
LOK
85 {
86 /* start as 'in transition standby->on' */
87 primary->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
88 primary->TransmitPowerState(CECDEVICE_TV);
b8952342
LOK
89
90 /* send the vendor id */
91 primary->TransmitVendorID(CECDEVICE_BROADCAST);
871934d3 92 }
468a1414 93
468a1414
LOK
94 return true;
95}
96
3e61b350
LOK
97bool CSLCommandHandler::ActivateSource(void)
98{
cc1b9bc4 99 if (!SLInitialised())
682b21de
LOK
100 {
101 CLibCEC::AddLog(CEC_LOG_NOTICE, "not activating the source until SL has been initialised");
3faa971c 102 return true;
682b21de 103 }
3faa971c 104
cc1b9bc4
LOK
105 {
106 CLockObject lock(m_SLMutex);
107 if (m_bActiveSourceSent)
108 return true;
109 m_bActiveSourceSent = true;
110 }
b818fb5a 111
3e61b350
LOK
112 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
113 primary->SetActiveSource();
114 primary->TransmitActiveSource();
115 return true;
116}
117
468a1414
LOK
118bool CSLCommandHandler::HandleActiveSource(const cec_command &command)
119{
120 if (command.parameters.size == 2)
121 {
122 uint16_t iAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
123 if (iAddress != m_busDevice->GetPhysicalAddress(false))
682b21de 124 {
cc1b9bc4 125 ResetSLState();
682b21de 126 }
468a1414
LOK
127 return m_processor->SetActiveSource(iAddress);
128 }
129
130 return true;
131}
132
a463f954
LOK
133bool CSLCommandHandler::HandleDeviceVendorId(const cec_command &command)
134{
135 SetVendorId(command);
136
cc1b9bc4 137 if (!SLInitialised())
468a1414 138 {
2abe628c
LOK
139 cec_command response;
140 cec_command::Format(response, m_processor->GetLogicalAddress(), command.initiator, CEC_OPCODE_FEATURE_ABORT);
141 return Transmit(response);
468a1414 142 }
2abe628c 143 return true;
468a1414
LOK
144}
145
146bool CSLCommandHandler::HandleGivePhysicalAddress(const cec_command &command)
147{
8e57f83c 148 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
468a1414
LOK
149 {
150 CCECBusDevice *device = GetDevice(command.destination);
151 if (device)
b818fb5a 152 return device->TransmitPhysicalAddress(); // only the physical address, don't send image view on
468a1414
LOK
153 }
154
155 return false;
e9de9629 156}
e54fd7d2
LOK
157
158bool CSLCommandHandler::HandleVendorCommand(const cec_command &command)
159{
160 if (command.parameters.size == 1 &&
11d13a02 161 command.parameters[0] == SL_COMMAND_UNKNOWN_01)
e54fd7d2 162 {
5f316715 163 HandleVendorCommand01(command);
8d915412 164 return true;
e54fd7d2 165 }
9a0d7b9f
LOK
166 else if (command.parameters.size == 2 &&
167 command.parameters[0] == SL_COMMAND_POWER_ON)
168 {
0ecbcd4d 169 HandleVendorCommandPowerOn(command);
9a0d7b9f
LOK
170 return true;
171 }
797dd7c4 172 else if (command.parameters.size == 2 &&
11d13a02 173 command.parameters[0] == SL_COMMAND_CONNECT_REQUEST)
9902f4e8 174 {
0ecbcd4d 175 HandleVendorCommandSLConnect(command);
8d915412 176 return true;
9902f4e8
LOK
177 }
178 else if (command.parameters.size == 1 &&
0d4c3a7b 179 command.parameters[0] == SL_COMMAND_REQUEST_POWER_STATUS)
9902f4e8 180 {
0ecbcd4d 181 HandleVendorCommandPowerOnStatus(command);
8d915412 182 return true;
9902f4e8 183 }
e54fd7d2
LOK
184
185 return false;
186}
187
5f316715 188void CSLCommandHandler::HandleVendorCommand01(const cec_command &command)
fe6f8e37 189{
b818fb5a 190 m_processor->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
fe6f8e37
LOK
191 TransmitVendorCommand0205(command.destination, command.initiator);
192}
193
194void CSLCommandHandler::TransmitVendorCommand0205(const cec_logical_address iSource, const cec_logical_address iDestination)
5f316715
LOK
195{
196 cec_command response;
fe6f8e37 197 cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
5f316715
LOK
198 response.PushBack(SL_COMMAND_UNKNOWN_02);
199 response.PushBack(SL_COMMAND_UNKNOWN_03);
200
19cbfa8f 201 Transmit(response, false);
fe6f8e37 202}
5f316715 203
0ecbcd4d 204void CSLCommandHandler::HandleVendorCommandPowerOn(const cec_command &command)
9a0d7b9f 205{
842262d8 206 CCECBusDevice *device = m_processor->GetPrimaryDevice();
9a0d7b9f
LOK
207 if (device)
208 {
cc1b9bc4 209 SetSLInitialised();
468a1414
LOK
210
211 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); //XXX
9a0d7b9f 212 device->TransmitPowerState(command.initiator);
468a1414 213 device->SetPowerStatus(CEC_POWER_STATUS_ON);
9a0d7b9f 214
468a1414
LOK
215 device->SetActiveSource();
216 TransmitImageViewOn(device->GetLogicalAddress(), command.initiator);
217 }
5f316715 218}
0ecbcd4d 219void CSLCommandHandler::HandleVendorCommandPowerOnStatus(const cec_command &command)
5f316715
LOK
220{
221 if (command.destination != CECDEVICE_BROADCAST)
222 {
223 CCECBusDevice *device = m_processor->m_busDevices[m_processor->GetLogicalAddresses().primary];
224 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
225 device->TransmitPowerState(command.initiator);
226 device->SetPowerStatus(CEC_POWER_STATUS_ON);
227 }
228}
229
468a1414 230void CSLCommandHandler::HandleVendorCommandSLConnect(const cec_command &command)
8d915412 231{
cc1b9bc4 232 SetSLInitialised();
5abb18f3 233 TransmitVendorCommandSetDeviceMode(m_processor->GetLogicalAddress(), command.initiator, CEC_DEVICE_TYPE_RECORDING_DEVICE);
37acf382 234
2abe628c 235 ActivateSource();
8d915412 236}
0d4c3a7b 237
5abb18f3 238void CSLCommandHandler::TransmitVendorCommandSetDeviceMode(const cec_logical_address iSource, const cec_logical_address iDestination, const cec_device_type type)
0d4c3a7b 239{
468a1414
LOK
240 cec_command response;
241 cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
5abb18f3
LOK
242 response.PushBack(SL_COMMAND_SET_DEVICE_MODE);
243 response.PushBack((uint8_t)type);
468a1414 244 Transmit(response, false);
0d4c3a7b 245}
fe6f8e37 246
b818fb5a 247bool CSLCommandHandler::HandleGiveDeckStatus(const cec_command &command)
9f65e017 248{
b818fb5a
LOK
249 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
250 {
251 CCECBusDevice *device = GetDevice(command.destination);
252 if (device && (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE))
253 {
254 if (command.parameters.size > 0)
255 {
b818fb5a
LOK
256 if (command.parameters[0] == CEC_STATUS_REQUEST_ON)
257 {
2abe628c 258 ((CCECPlaybackDevice *) device)->SetDeckStatus(CEC_DECK_INFO_STOP);
b818fb5a
LOK
259 return ((CCECPlaybackDevice *) device)->TransmitDeckStatus(command.initiator) &&
260 device->TransmitImageViewOn() &&
261 device->TransmitPhysicalAddress();
262 }
263 else if (command.parameters[0] == CEC_STATUS_REQUEST_ONCE)
264 {
2abe628c 265 ((CCECPlaybackDevice *) device)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG);
b818fb5a
LOK
266 return ((CCECPlaybackDevice *) device)->TransmitDeckStatus(command.initiator);
267 }
268 }
269 }
270 return CCECCommandHandler::HandleGiveDeckStatus(command);
271 }
9f65e017 272
b818fb5a 273 return false;
9f65e017 274}
718b3632 275
718b3632
LOK
276bool CSLCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command)
277{
2abe628c 278 bool bReturn(false);
718b3632
LOK
279 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
280 {
281 CCECBusDevice *device = GetDevice(command.destination);
282 if (device && device->GetPowerStatus(false) != CEC_POWER_STATUS_ON)
2abe628c
LOK
283 {
284 bReturn = device->TransmitPowerState(command.initiator);
285 device->SetPowerStatus(CEC_POWER_STATUS_ON);
286 }
2abe628c
LOK
287 else
288 {
cc1b9bc4 289 if (!ActiveSourceSent())
3faa971c
LOK
290 {
291 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
292 bReturn = device->TransmitPowerState(command.initiator);
293 ActivateSource();
294 }
295 else if (m_resetPowerState.IsSet() && m_resetPowerState.TimeLeft() > 0)
296 {
297 /* assume that we've bugged out */
298 CLibCEC::AddLog(CEC_LOG_NOTICE, "LG seems to have bugged out. resetting to 'in transition standby to on'");
cc1b9bc4
LOK
299 {
300 CLockObject lock(m_SLMutex);
301 m_bActiveSourceSent = false;
302 }
3faa971c
LOK
303 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
304 bReturn = device->TransmitPowerState(command.initiator);
305 device->SetPowerStatus(CEC_POWER_STATUS_ON);
306 m_resetPowerState.Init(5000);
307 }
308 else
309 {
310 bReturn = device->TransmitPowerState(command.initiator);
311 m_resetPowerState.Init(5000);
312 }
b818fb5a 313 }
718b3632
LOK
314 }
315
2abe628c
LOK
316 return bReturn;
317}
318
319bool CSLCommandHandler::HandleRequestActiveSource(const cec_command &command)
320{
321 if (m_processor->IsRunning())
322 {
323 CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %i requests active source, ignored", (uint8_t) command.initiator);
324 return true;
325 }
718b3632
LOK
326 return false;
327}
5c3b1f92
LOK
328
329bool CSLCommandHandler::HandleFeatureAbort(const cec_command &command)
330{
cc1b9bc4 331 if (command.parameters.size == 0 && m_processor->GetPrimaryDevice()->GetPowerStatus() == CEC_POWER_STATUS_ON && !SLInitialised())
5c3b1f92
LOK
332 {
333 m_processor->GetPrimaryDevice()->TransmitPowerState(command.initiator);
334 m_processor->GetPrimaryDevice()->TransmitVendorID(CECDEVICE_BROADCAST, false);
335 }
336
337 return CCECCommandHandler::HandleFeatureAbort(command);
338}
339
340bool CSLCommandHandler::HandleStandby(const cec_command &command)
341{
342 if (command.initiator == CECDEVICE_TV)
cc1b9bc4 343 ResetSLState();
5c3b1f92 344
cc1b9bc4
LOK
345 return CCECCommandHandler::HandleStandby(command);
346}
5c3b1f92 347
cc1b9bc4
LOK
348void CSLCommandHandler::ResetSLState(void)
349{
350 CLibCEC::AddLog(CEC_LOG_NOTICE, "resetting SL initialised state");
351 CLockObject lock(m_SLMutex);
352 m_bSLEnabled = false;
353 m_bActiveSourceSent = false;
354 m_processor->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
355}
356
357void CSLCommandHandler::SetSLInitialised(void)
358{
359 CLibCEC::AddLog(CEC_LOG_NOTICE, "SL initialised");
360 CLockObject lock(m_SLMutex);
361 m_bSLEnabled = true;
362}
363
364bool CSLCommandHandler::SLInitialised(void)
365{
366 CLockObject lock(m_SLMutex);
367 return m_bSLEnabled;
368}
369
370bool CSLCommandHandler::ActiveSourceSent(void)
371{
372 CLockObject lock(m_SLMutex);
373 return m_bActiveSourceSent;
5c3b1f92 374}