cec: move some stuff that doesn't transmit messages from CSLCommandHandler::InitHandl...
[deb_libcec.git] / src / lib / implementations / SLCommandHandler.cpp
CommitLineData
e9de9629
LOK
1/*
2 * This file is part of the libCEC(R) library.
3 *
4 * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited. All rights reserved.
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"
b4c4ef7d 37#include "../platform/timeutils.h"
e9de9629
LOK
38
39using namespace CEC;
40
0d4c3a7b
LOK
41#define SL_COMMAND_UNKNOWN_01 0x01
42#define SL_COMMAND_UNKNOWN_02 0x02
43#define SL_COMMAND_UNKNOWN_03 0x05
11d13a02 44
0d4c3a7b 45#define SL_COMMAND_REQUEST_POWER_STATUS 0xa0
9a0d7b9f 46#define SL_COMMAND_POWER_ON 0x03
0d4c3a7b
LOK
47#define SL_COMMAND_CONNECT_REQUEST 0x04
48#define SL_COMMAND_CONNECT_ACCEPT 0x05
11d13a02 49
e9de9629 50CSLCommandHandler::CSLCommandHandler(CCECBusDevice *busDevice) :
855a3a98 51 CCECCommandHandler(busDevice),
b4c4ef7d
LOK
52 m_bAwaitingReceiveFailed(false),
53 m_bSLEnabled(false),
5f316715 54 m_bVendorIdSent(false)
e9de9629 55{
79f01d26
LOK
56 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
57
58 /* imitate LG devices */
59 if (m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress())
60 primary->SetVendorId(CEC_VENDOR_LG, false);
61
62 /* LG TVs don't always reply to CEC version requests, so just set it to 1.3a */
63 if (m_busDevice->GetLogicalAddress() == CECDEVICE_TV)
64 m_busDevice->SetCecVersion(CEC_VERSION_1_3A);
65
66 /* LG devices always return "korean" as language */
67 cec_menu_language lang;
68 lang.device = m_busDevice->GetLogicalAddress();
69 snprintf(lang.language, 4, "eng");
70 m_busDevice->SetMenuLanguage(lang);
9f65e017
LOK
71}
72
73void CSLCommandHandler::SetLGDeckStatus(void)
74{
75 /* LG TVs only route keypresses when the deck status is set to 0x20 */
76 CCECBusDevice *device = m_processor->GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
77 if (device)
78 ((CCECPlaybackDevice *)device)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG);
79
80 device = m_processor->GetDeviceByType(CEC_DEVICE_TYPE_RECORDING_DEVICE);
81 if (device)
82 ((CCECPlaybackDevice *)device)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG);
e9de9629 83}
e54fd7d2
LOK
84
85bool CSLCommandHandler::HandleVendorCommand(const cec_command &command)
86{
87 if (command.parameters.size == 1 &&
11d13a02 88 command.parameters[0] == SL_COMMAND_UNKNOWN_01)
e54fd7d2 89 {
5f316715 90 HandleVendorCommand01(command);
8d915412 91 return true;
e54fd7d2 92 }
9a0d7b9f
LOK
93 else if (command.parameters.size == 2 &&
94 command.parameters[0] == SL_COMMAND_POWER_ON)
95 {
0ecbcd4d 96 HandleVendorCommandPowerOn(command);
9a0d7b9f
LOK
97 return true;
98 }
797dd7c4 99 else if (command.parameters.size == 2 &&
11d13a02 100 command.parameters[0] == SL_COMMAND_CONNECT_REQUEST)
9902f4e8 101 {
0ecbcd4d 102 HandleVendorCommandSLConnect(command);
8d915412 103 return true;
9902f4e8
LOK
104 }
105 else if (command.parameters.size == 1 &&
0d4c3a7b 106 command.parameters[0] == SL_COMMAND_REQUEST_POWER_STATUS)
9902f4e8 107 {
0ecbcd4d 108 HandleVendorCommandPowerOnStatus(command);
8d915412 109 return true;
9902f4e8 110 }
e54fd7d2
LOK
111
112 return false;
113}
114
5f316715 115void CSLCommandHandler::HandleVendorCommand01(const cec_command &command)
fe6f8e37
LOK
116{
117 TransmitVendorCommand0205(command.destination, command.initiator);
118}
119
120void CSLCommandHandler::TransmitVendorCommand0205(const cec_logical_address iSource, const cec_logical_address iDestination)
5f316715
LOK
121{
122 cec_command response;
fe6f8e37 123 cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
5f316715
LOK
124 response.PushBack(SL_COMMAND_UNKNOWN_02);
125 response.PushBack(SL_COMMAND_UNKNOWN_03);
126
19cbfa8f 127 Transmit(response, false);
fe6f8e37 128}
5f316715 129
0e54ee2e 130void CSLCommandHandler::TransmitVendorCommand05(const cec_logical_address iSource, const cec_logical_address iDestination)
fe6f8e37
LOK
131{
132 m_bSLEnabled = true;
133 cec_command response;
134 cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
135 response.PushBack(SL_COMMAND_CONNECT_ACCEPT);
136 response.PushBack((uint8_t)iSource);
19cbfa8f 137 Transmit(response, false);
5f316715
LOK
138}
139
0ecbcd4d 140void CSLCommandHandler::HandleVendorCommandPowerOn(const cec_command &command)
9a0d7b9f 141{
842262d8 142 CCECBusDevice *device = m_processor->GetPrimaryDevice();
9a0d7b9f
LOK
143 if (device)
144 {
fe6f8e37 145 m_bSLEnabled = true;
9f65e017 146 SetLGDeckStatus();
fe6f8e37 147 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
9a0d7b9f 148 device->TransmitPowerState(command.initiator);
fe6f8e37 149 device->TransmitVendorID(command.initiator);
9a0d7b9f 150 TransmitPowerOn(device->GetLogicalAddress(), command.initiator);
9a0d7b9f
LOK
151 }
152}
153
0ecbcd4d 154void CSLCommandHandler::HandleVendorCommandSLConnect(const cec_command &command)
5f316715 155{
fe6f8e37 156 m_bSLEnabled = true;
37b0c572 157 m_processor->m_busDevices[command.initiator]->SetActiveSource();
0ecbcd4d 158 m_processor->m_busDevices[command.destination]->TransmitActiveSource();
0e54ee2e 159 TransmitVendorCommand05(command.destination, command.initiator);
9f65e017 160 SetLGDeckStatus();
5f316715
LOK
161}
162
0ecbcd4d 163void CSLCommandHandler::HandleVendorCommandPowerOnStatus(const cec_command &command)
5f316715
LOK
164{
165 if (command.destination != CECDEVICE_BROADCAST)
166 {
167 CCECBusDevice *device = m_processor->m_busDevices[m_processor->GetLogicalAddresses().primary];
168 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
169 device->TransmitPowerState(command.initiator);
170 device->SetPowerStatus(CEC_POWER_STATUS_ON);
171 }
172}
173
8d915412
LOK
174bool CSLCommandHandler::TransmitLGVendorId(const cec_logical_address iInitiator, const cec_logical_address iDestination)
175{
176 cec_command response;
0d4c3a7b 177 cec_command::Format(response, iInitiator, iDestination, CEC_OPCODE_DEVICE_VENDOR_ID);
8d915412
LOK
178 response.parameters.PushBack((uint8_t) (((uint64_t)CEC_VENDOR_LG >> 16) & 0xFF));
179 response.parameters.PushBack((uint8_t) (((uint64_t)CEC_VENDOR_LG >> 8) & 0xFF));
180 response.parameters.PushBack((uint8_t) ((uint64_t)CEC_VENDOR_LG & 0xFF));
181
19cbfa8f 182 Transmit(response, false);
8d915412 183
9f65e017
LOK
184 cec_command::Format(response, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
185 Transmit(response);
e54fd7d2 186
9f65e017 187 return true;
e54fd7d2
LOK
188}
189
190bool CSLCommandHandler::HandleCommand(const cec_command &command)
191{
192 bool bHandled(false);
5f316715 193
fe6f8e37
LOK
194 if (m_processor->IsStarted() && (m_busDevice->MyLogicalAddressContains(command.destination) ||
195 command.destination == CECDEVICE_BROADCAST))
e54fd7d2
LOK
196 {
197 switch(command.opcode)
198 {
199 case CEC_OPCODE_VENDOR_COMMAND:
200 bHandled = HandleVendorCommand(command);
201 break;
b4c4ef7d
LOK
202 case CEC_OPCODE_FEATURE_ABORT:
203 {
5f316715 204 if (!m_bVendorIdSent)
b4c4ef7d 205 {
5f316715
LOK
206 m_bVendorIdSent = true;
207 TransmitLGVendorId(m_processor->GetLogicalAddresses().primary, CECDEVICE_BROADCAST);
9f65e017 208 m_processor->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_STANDBY);
45b97de7 209 }
b4c4ef7d
LOK
210 }
211 bHandled = true;
9f65e017 212 break;
e54fd7d2
LOK
213 default:
214 break;
215 }
216 }
217
218 if (!bHandled)
219 bHandled = CCECCommandHandler::HandleCommand(command);
220
221 return bHandled;
222}
855a3a98 223
855a3a98
LOK
224void CSLCommandHandler::HandlePoll(const cec_logical_address iInitiator, const cec_logical_address iDestination)
225{
226 CCECCommandHandler::HandlePoll(iInitiator, iDestination);
227 m_bAwaitingReceiveFailed = true;
228}
229
230bool CSLCommandHandler::HandleReceiveFailed(void)
231{
232 if (m_bAwaitingReceiveFailed)
233 {
234 m_bAwaitingReceiveFailed = false;
235 return false;
236 }
237
238 return true;
239}
8d915412
LOK
240
241bool CSLCommandHandler::InitHandler(void)
242{
89a726fa
LOK
243 if (m_bHandlerInited)
244 return true;
245 m_bHandlerInited = true;
246
9f65e017 247 /* reply with LGs vendor id */
79f01d26 248 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
9f65e017
LOK
249 if (m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress())
250 primary->TransmitVendorID(CECDEVICE_TV, false);
37acf382 251
8d915412
LOK
252 return true;
253}
0d4c3a7b 254
0d4c3a7b
LOK
255bool CSLCommandHandler::TransmitPowerOn(const cec_logical_address iInitiator, const cec_logical_address iDestination)
256{
5f316715 257 if (iDestination != CECDEVICE_BROADCAST &&
5827f4aa 258 iDestination != CECDEVICE_TV &&
9a0d7b9f 259 m_processor->m_busDevices[iDestination]->GetVendorId(false) == CEC_VENDOR_LG)
5f316715
LOK
260 {
261 cec_command command;
262 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_VENDOR_COMMAND);
9a0d7b9f
LOK
263 command.parameters.PushBack((uint8_t)SL_COMMAND_POWER_ON);
264 command.parameters.PushBack(0x00);
19cbfa8f 265 return Transmit(command, false);
5f316715 266 }
0d4c3a7b 267
9f65e017 268 return CCECCommandHandler::TransmitImageViewOn(iInitiator, iDestination);
0d4c3a7b 269}
fe6f8e37 270
9f65e017
LOK
271bool CSLCommandHandler::HandleActiveSource(const cec_command &command)
272{
273 if (command.parameters.size == 2)
274 {
275 uint16_t iAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
276 if (iAddress == 0)
277 return m_processor->GetPrimaryDevice()->TransmitPhysicalAddress();
278 else
279 return m_processor->SetActiveSource(iAddress);
280 }
281
282 return true;
283}